OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/gpu/fake_jpeg_decode_accelerator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "media/filters/jpeg_parser.h" |
| 11 #include "media/gpu/shared_memory_region.h" |
| 12 #include "third_party/libyuv/include/libyuv.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 FakeJpegDecodeAccelerator::FakeJpegDecodeAccelerator( |
| 17 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 18 : client_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 19 io_task_runner_(std::move(io_task_runner)), |
| 20 decoder_thread_("FakeJpegDecoderThread"), |
| 21 weak_factory_(this) {} |
| 22 |
| 23 FakeJpegDecodeAccelerator::~FakeJpegDecodeAccelerator() { |
| 24 DCHECK(client_task_runner_->BelongsToCurrentThread()); |
| 25 } |
| 26 |
| 27 bool FakeJpegDecodeAccelerator::Initialize( |
| 28 JpegDecodeAccelerator::Client* client) { |
| 29 DCHECK(client_task_runner_->BelongsToCurrentThread()); |
| 30 client_ = client; |
| 31 |
| 32 if (!decoder_thread_.Start()) { |
| 33 DLOG(ERROR) << "Failed to start decoding thread."; |
| 34 return false; |
| 35 } |
| 36 decoder_task_runner_ = decoder_thread_.task_runner(); |
| 37 |
| 38 return true; |
| 39 } |
| 40 |
| 41 void FakeJpegDecodeAccelerator::Decode( |
| 42 const BitstreamBuffer& bitstream_buffer, |
| 43 const scoped_refptr<VideoFrame>& video_frame) { |
| 44 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 45 |
| 46 // SharedMemoryRegion will take over the |bitstream_buffer.handle()|. |
| 47 std::unique_ptr<SharedMemoryRegion> src_shm( |
| 48 new SharedMemoryRegion(bitstream_buffer, true)); |
| 49 if (!src_shm->Map()) { |
| 50 DLOG(ERROR) << "Unable to map shared memory in FakeJpegDecodeAccelerator"; |
| 51 NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::UNREADABLE_INPUT); |
| 52 return; |
| 53 } |
| 54 |
| 55 // Unretained |this| is safe because |this| owns |decoder_thread_|. |
| 56 decoder_task_runner_->PostTask( |
| 57 FROM_HERE, base::Bind(&FakeJpegDecodeAccelerator::DecodeOnDecoderThread, |
| 58 base::Unretained(this), bitstream_buffer, |
| 59 video_frame, base::Passed(&src_shm))); |
| 60 } |
| 61 |
| 62 void FakeJpegDecodeAccelerator::DecodeOnDecoderThread( |
| 63 const BitstreamBuffer& bitstream_buffer, |
| 64 const scoped_refptr<VideoFrame>& video_frame, |
| 65 std::unique_ptr<SharedMemoryRegion> src_shm) { |
| 66 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); |
| 67 |
| 68 JpegParseResult parse_result; |
| 69 if (!ParseJpegPicture(static_cast<uint8_t*>(src_shm->memory()), |
| 70 src_shm->size(), &parse_result)) { |
| 71 DLOG(ERROR) << "ParseJpegPicture failed in FakeJpegDecodeAccelerator"; |
| 72 NotifyError(bitstream_buffer.id(), |
| 73 JpegDecodeAccelerator::PARSE_JPEG_FAILED); |
| 74 return; |
| 75 } |
| 76 const gfx::Size src_coded_size(parse_result.frame_header.coded_width, |
| 77 parse_result.frame_header.coded_height); |
| 78 DCHECK_GE(src_coded_size.width(), video_frame->coded_size().width()); |
| 79 DCHECK_GE(src_coded_size.height(), video_frame->coded_size().height()); |
| 80 |
| 81 if (libyuv::ConvertToI420( |
| 82 static_cast<uint8_t*>(src_shm->memory()), src_shm->size(), |
| 83 video_frame->data(VideoFrame::kYPlane), |
| 84 video_frame->stride(VideoFrame::kYPlane), |
| 85 video_frame->data(VideoFrame::kUPlane), |
| 86 video_frame->stride(VideoFrame::kUPlane), |
| 87 video_frame->data(VideoFrame::kVPlane), |
| 88 video_frame->stride(VideoFrame::kVPlane), 0, 0, |
| 89 src_coded_size.width(), src_coded_size.height(), |
| 90 video_frame->coded_size().width(), video_frame->coded_size().height(), |
| 91 libyuv::kRotate0, libyuv::FOURCC_MJPG) != 0) { |
| 92 DLOG(ERROR) << "Software decode failed."; |
| 93 NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 94 return; |
| 95 } |
| 96 client_task_runner_->PostTask( |
| 97 FROM_HERE, |
| 98 base::Bind(&FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread, |
| 99 weak_factory_.GetWeakPtr(), bitstream_buffer.id())); |
| 100 } |
| 101 |
| 102 bool FakeJpegDecodeAccelerator::IsSupported() { |
| 103 return true; |
| 104 } |
| 105 |
| 106 void FakeJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
| 107 Error error) { |
| 108 client_task_runner_->PostTask( |
| 109 FROM_HERE, |
| 110 base::Bind(&FakeJpegDecodeAccelerator::NotifyErrorOnClientThread, |
| 111 weak_factory_.GetWeakPtr(), bitstream_buffer_id, error)); |
| 112 } |
| 113 |
| 114 void FakeJpegDecodeAccelerator::NotifyErrorOnClientThread( |
| 115 int32_t bitstream_buffer_id, |
| 116 Error error) { |
| 117 DCHECK(client_task_runner_->BelongsToCurrentThread()); |
| 118 client_->NotifyError(bitstream_buffer_id, error); |
| 119 } |
| 120 |
| 121 void FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread( |
| 122 int32_t input_buffer_id) { |
| 123 DCHECK(client_task_runner_->BelongsToCurrentThread()); |
| 124 client_->VideoFrameReady(input_buffer_id); |
| 125 } |
| 126 |
| 127 } // namespace media |
OLD | NEW |