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 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 19 io_task_runner_(std::move(io_task_runner)), |
| 20 weak_factory_(this) {} |
| 21 |
| 22 FakeJpegDecodeAccelerator::~FakeJpegDecodeAccelerator() = default; |
| 23 |
| 24 bool FakeJpegDecodeAccelerator::Initialize( |
| 25 JpegDecodeAccelerator::Client* client) { |
| 26 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 27 client_ = client; |
| 28 return true; |
| 29 } |
| 30 |
| 31 void FakeJpegDecodeAccelerator::Decode( |
| 32 const BitstreamBuffer& bitstream_buffer, |
| 33 const scoped_refptr<VideoFrame>& video_frame) { |
| 34 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 35 // SharedMemoryRegion will take over the |bitstream_buffer.handle()|. |
| 36 std::unique_ptr<SharedMemoryRegion> src_shm_( |
| 37 new SharedMemoryRegion(bitstream_buffer, true)); |
| 38 if (!src_shm_->Map()) { |
| 39 DLOG(ERROR) << "Unable to map shared memory in FakeJpegDecodeAccelerator"; |
| 40 NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::UNREADABLE_INPUT); |
| 41 return; |
| 42 } |
| 43 |
| 44 JpegParseResult parse_result; |
| 45 if (!ParseJpegPicture(static_cast<uint8_t*>(src_shm_->memory()), |
| 46 src_shm_->size(), &parse_result)) { |
| 47 DLOG(ERROR) << "ParseJpegPicture failed in FakeJpegDecodeAccelerator"; |
| 48 NotifyError(bitstream_buffer.id(), |
| 49 JpegDecodeAccelerator::PARSE_JPEG_FAILED); |
| 50 return; |
| 51 } |
| 52 const gfx::Size src_coded_size(parse_result.frame_header.coded_width, |
| 53 parse_result.frame_header.coded_height); |
| 54 DCHECK_GE(src_coded_size.width(), video_frame->coded_size().width()); |
| 55 DCHECK_GE(src_coded_size.height(), video_frame->coded_size().height()); |
| 56 |
| 57 uint8_t* dst_y = video_frame->data(VideoFrame::kYPlane); |
| 58 uint8_t* dst_u = video_frame->data(VideoFrame::kUPlane); |
| 59 uint8_t* dst_v = video_frame->data(VideoFrame::kVPlane); |
| 60 size_t dst_y_stride = video_frame->stride(VideoFrame::kYPlane); |
| 61 size_t dst_u_stride = video_frame->stride(VideoFrame::kUPlane); |
| 62 size_t dst_v_stride = video_frame->stride(VideoFrame::kVPlane); |
| 63 |
| 64 if (libyuv::ConvertToI420( |
| 65 static_cast<uint8_t*>(src_shm_->memory()), src_shm_->size(), dst_y, |
| 66 dst_y_stride, dst_u, dst_u_stride, dst_v, dst_v_stride, 0, 0, |
| 67 src_coded_size.width(), src_coded_size.height(), |
| 68 video_frame->coded_size().width(), video_frame->coded_size().height(), |
| 69 libyuv::kRotate0, libyuv::FOURCC_MJPG) != 0) { |
| 70 DLOG(ERROR) << "Software decode failed."; |
| 71 NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 72 return; |
| 73 } |
| 74 task_runner_->PostTask( |
| 75 FROM_HERE, |
| 76 base::Bind(&FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread, |
| 77 weak_factory_.GetWeakPtr(), bitstream_buffer.id())); |
| 78 } |
| 79 |
| 80 bool FakeJpegDecodeAccelerator::IsSupported() { |
| 81 return true; |
| 82 } |
| 83 |
| 84 void FakeJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
| 85 Error error) { |
| 86 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 87 task_runner_->PostTask( |
| 88 FROM_HERE, |
| 89 base::Bind(&FakeJpegDecodeAccelerator::NotifyErrorOnClientThread, |
| 90 weak_factory_.GetWeakPtr(), bitstream_buffer_id, error)); |
| 91 } |
| 92 |
| 93 void FakeJpegDecodeAccelerator::NotifyErrorOnClientThread( |
| 94 int32_t bitstream_buffer_id, |
| 95 Error error) { |
| 96 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 97 client_->NotifyError(bitstream_buffer_id, error); |
| 98 } |
| 99 |
| 100 void FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread( |
| 101 int32_t input_buffer_id) { |
| 102 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 103 client_->VideoFrameReady(input_buffer_id); |
| 104 } |
| 105 |
| 106 } // namespace media |
OLD | NEW |