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/threading/thread_task_runner_handle.h" | |
9 #include "media/filters/jpeg_parser.h" | |
10 #include "media/gpu/shared_memory_region.h" | |
11 #include "third_party/libyuv/include/libyuv.h" | |
12 | |
13 namespace media { | |
14 | |
15 FakeJpegDecodeAccelerator::FakeJpegDecodeAccelerator( | |
16 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
17 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
18 io_task_runner_(std::move(io_task_runner)), | |
19 weak_factory_(this) {} | |
20 | |
21 FakeJpegDecodeAccelerator::~FakeJpegDecodeAccelerator() = default; | |
22 | |
23 bool FakeJpegDecodeAccelerator::Initialize( | |
24 JpegDecodeAccelerator::Client* client) { | |
25 client_ = client; | |
26 return true; | |
27 } | |
28 | |
29 void FakeJpegDecodeAccelerator::Decode( | |
30 const BitstreamBuffer& bitstream_buffer, | |
31 const scoped_refptr<VideoFrame>& video_frame) { | |
32 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
33 // SharedMemoryRegion will take over the |bitstream_buffer.handle()|. | |
34 std::unique_ptr<SharedMemoryRegion> src_shm_( | |
35 new SharedMemoryRegion(bitstream_buffer, true)); | |
36 if (!src_shm_->Map()) { | |
37 DLOG(ERROR) << "Unable to map shared memory in FakeJpegDecodeAccelerator"; | |
38 NotifyErrorFromIoThread(bitstream_buffer.id(), | |
39 JpegDecodeAccelerator::UNREADABLE_INPUT); | |
40 return; | |
41 } | |
42 | |
43 JpegParseResult parse_result; | |
44 if (!ParseJpegPicture(static_cast<uint8_t*>(src_shm_->memory()), | |
45 src_shm_->size(), &parse_result)) { | |
46 DLOG(ERROR) << "ParseJpegPicture failed in FakeJpegDecodeAccelerator"; | |
47 NotifyErrorFromIoThread(bitstream_buffer.id(), | |
48 JpegDecodeAccelerator::PARSE_JPEG_FAILED); | |
49 return; | |
50 } | |
51 gfx::Size src_coded_size(parse_result.frame_header.coded_width, | |
mcasas
2017/03/07 19:39:38
nit: const.
chfremer
2017/03/07 22:30:38
Done.
| |
52 parse_result.frame_header.coded_height); | |
53 DCHECK_GE(src_coded_size.width(), video_frame->coded_size().width()); | |
54 DCHECK_GE(src_coded_size.height(), video_frame->coded_size().height()); | |
55 | |
56 uint8_t* dst_y = video_frame->data(VideoFrame::kYPlane); | |
57 uint8_t* dst_u = video_frame->data(VideoFrame::kUPlane); | |
58 uint8_t* dst_v = video_frame->data(VideoFrame::kVPlane); | |
59 size_t dst_y_stride = video_frame->stride(VideoFrame::kYPlane); | |
60 size_t dst_u_stride = video_frame->stride(VideoFrame::kUPlane); | |
61 size_t dst_v_stride = video_frame->stride(VideoFrame::kVPlane); | |
62 | |
63 if (libyuv::ConvertToI420( | |
64 static_cast<uint8_t*>(src_shm_->memory()), src_shm_->size(), dst_y, | |
65 dst_y_stride, dst_u, dst_u_stride, dst_v, dst_v_stride, 0, 0, | |
66 src_coded_size.width(), src_coded_size.height(), | |
67 video_frame->coded_size().width(), video_frame->coded_size().height(), | |
68 libyuv::kRotate0, libyuv::FOURCC_MJPG) != 0) { | |
69 LOG(ERROR) << "Software decode failed."; | |
mcasas
2017/03/07 19:39:38
s/LOG/DLOG/ for consistency. Note that
release bo
chfremer
2017/03/07 22:30:38
Done.
| |
70 NotifyErrorFromIoThread(bitstream_buffer.id(), | |
71 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::NotifyErrorFromIoThread( | |
85 int32_t bitstream_buffer_id, | |
mcasas
2017/03/07 19:39:38
s/NotifyErrorFromIoThread/NotifyError/
same as D
chfremer
2017/03/07 22:30:38
Done.
| |
86 Error error) { | |
87 task_runner_->PostTask( | |
mcasas
2017/03/07 19:39:38
DCHECK(io_task_runner_->BelongsToCurrentThread());
chfremer
2017/03/07 22:30:38
Done.
| |
88 FROM_HERE, | |
89 base::Bind(&FakeJpegDecodeAccelerator::OnNotifyErrorOnClientThread, | |
90 weak_factory_.GetWeakPtr(), bitstream_buffer_id, error)); | |
91 } | |
92 | |
93 void FakeJpegDecodeAccelerator::OnNotifyErrorOnClientThread( | |
mcasas
2017/03/07 19:39:38
s/OnNotifyErrorOnClientThread/NotifyErrorOnClientT
chfremer
2017/03/07 22:30:38
Done.
| |
94 int32_t bitstream_buffer_id, | |
95 Error error) { | |
96 client_->NotifyError(bitstream_buffer_id, error); | |
mcasas
2017/03/07 19:39:38
Add DCHECK(task_runner_->BelongsToCurrentThread())
chfremer
2017/03/07 22:30:38
Done.
| |
97 } | |
98 | |
99 void FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread( | |
100 int32_t input_buffer_id) { | |
101 client_->VideoFrameReady(input_buffer_id); | |
mcasas
2017/03/07 19:39:38
Add DCHECK(task_runner_->BelongsToCurrentThread())
chfremer
2017/03/07 22:30:38
Done.
| |
102 } | |
103 | |
104 } // namespace media | |
OLD | NEW |