OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/thread_task_runner_handle.h" | |
10 #include "base/trace_event/trace_event.h" | |
11 #include "content/common/gpu/gpu_channel.h" | |
12 #include "content/common/gpu/media/vaapi_picture.h" | |
13 #include "media/base/video_frame.h" | |
14 #include "media/filters/jpeg_parser.h" | |
15 #include "third_party/libyuv/include/libyuv.h" | |
16 | |
17 namespace content { | |
18 | |
19 namespace { | |
20 static void ReportVaapiError() { | |
21 // TODO(kcwu) report error to UMA | |
Pawel Osciak
2015/06/10 08:02:45
This should be simple enough to do already, if we
wuchengli
2015/06/11 05:36:53
Let's make it a separate CL because it will need a
| |
22 } | |
23 } // namespace | |
24 | |
25 VaapiJpegDecodeAccelerator::DecodeRequest::DecodeRequest( | |
26 const media::BitstreamBuffer& bitstream_buffer, | |
27 scoped_ptr<base::SharedMemory> shm, | |
28 const scoped_refptr<media::VideoFrame>& video_frame) | |
29 : bitstream_buffer(bitstream_buffer), | |
30 shm(shm.Pass()), | |
31 video_frame(video_frame) { | |
32 } | |
33 | |
34 VaapiJpegDecodeAccelerator::DecodeRequest::~DecodeRequest() { | |
35 } | |
36 | |
37 void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, | |
38 Error error) { | |
39 DCHECK(task_runner_->BelongsToCurrentThread()); | |
40 DLOG(ERROR) << "Notifying of error " << error; | |
41 DCHECK(client_); | |
42 client_->NotifyError(bitstream_buffer_id, error); | |
43 } | |
44 | |
45 void VaapiJpegDecodeAccelerator::NotifyErrorFromDecoderThread( | |
46 int32_t bitstream_buffer_id, | |
47 Error error) { | |
48 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
49 task_runner_->PostTask(FROM_HERE, | |
50 base::Bind(&VaapiJpegDecodeAccelerator::NotifyError, | |
51 weak_this_, bitstream_buffer_id, error)); | |
52 } | |
53 | |
54 void VaapiJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { | |
55 DCHECK(task_runner_->BelongsToCurrentThread()); | |
56 client_->VideoFrameReady(bitstream_buffer_id); | |
57 } | |
58 | |
59 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator( | |
60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
61 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
62 io_task_runner_(io_task_runner), | |
63 decoder_thread_("VaapiJpegDecoderThread"), | |
64 va_surface_id_(VA_INVALID_SURFACE), | |
65 weak_this_factory_(this) { | |
66 weak_this_ = weak_this_factory_.GetWeakPtr(); | |
67 } | |
68 | |
69 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() { | |
70 DCHECK(task_runner_->BelongsToCurrentThread()); | |
71 DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator"; | |
72 | |
73 weak_this_factory_.InvalidateWeakPtrs(); | |
74 decoder_thread_.Stop(); | |
75 } | |
76 | |
77 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) { | |
78 DCHECK(task_runner_->BelongsToCurrentThread()); | |
79 | |
80 client_ = client; | |
81 | |
82 vaapi_wrapper_ = | |
83 VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline, | |
84 base::Bind(&ReportVaapiError)); | |
85 | |
86 if (!vaapi_wrapper_.get()) { | |
87 DLOG(ERROR) << "Failed initializing VAAPI"; | |
88 return false; | |
89 } | |
90 | |
91 if (!decoder_thread_.Start()) { | |
92 DLOG(ERROR) << "Failed to start decoding thread."; | |
93 return false; | |
94 } | |
95 decoder_task_runner_ = decoder_thread_.task_runner(); | |
96 | |
97 return true; | |
98 } | |
99 | |
100 bool VaapiJpegDecodeAccelerator::OutputPicture( | |
101 VASurfaceID va_surface_id, | |
102 int32_t input_buffer_id, | |
103 const scoped_refptr<media::VideoFrame>& video_frame) { | |
104 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
105 | |
106 TRACE_EVENT1("jpeg", "VaapiJpegDecodeAccelerator::OutputPicture", | |
107 "input_buffer_id", input_buffer_id); | |
108 | |
109 DVLOG(3) << "Outputting VASurface " << va_surface_id | |
110 << " into video_frame associated with input buffer id " | |
111 << input_buffer_id; | |
112 | |
113 VAImage image; | |
114 VAImageFormat format; | |
115 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0'); | |
116 memset(&image, 0, sizeof(image)); | |
117 memset(&format, 0, sizeof(format)); | |
118 format.fourcc = kI420Fourcc; | |
119 format.byte_order = VA_LSB_FIRST; | |
120 format.bits_per_pixel = 12; // 12 for I420 | |
121 | |
122 void* mem = nullptr; | |
Pawel Osciak
2015/06/10 08:02:44
const possible?
kcwu
2015/06/11 07:29:15
GetVaImage (and vaMapBuffer) want void**. If mem i
| |
123 gfx::Size coded_size = video_frame->coded_size(); | |
124 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image, | |
125 &mem)) { | |
126 DLOG(ERROR) << "Cannot get VAImage"; | |
127 return false; | |
128 } | |
129 | |
130 // Copy image content from VAImage to VideoFrame. | |
131 // The component order of VAImage I420 are Y, U, and V. | |
132 DCHECK_EQ(image.num_planes, 3u); | |
133 DCHECK_GE(image.width, coded_size.width()); | |
134 DCHECK_GE(image.height, coded_size.height()); | |
135 uint8* src_y = static_cast<uint8*>(mem) + image.offsets[0]; | |
Pawel Osciak
2015/06/10 08:02:45
s/uint8*/char*/ (or uint8_t*). But could we just h
kcwu
2015/06/11 07:29:15
Done.
| |
136 uint8* src_u = static_cast<uint8*>(mem) + image.offsets[1]; | |
137 uint8* src_v = static_cast<uint8*>(mem) + image.offsets[2]; | |
138 size_t src_y_stride = image.pitches[0]; | |
139 size_t src_u_stride = image.pitches[1]; | |
140 size_t src_v_stride = image.pitches[2]; | |
141 uint8* dst_y = video_frame->data(media::VideoFrame::kYPlane); | |
142 uint8* dst_u = video_frame->data(media::VideoFrame::kUPlane); | |
143 uint8* dst_v = video_frame->data(media::VideoFrame::kVPlane); | |
144 size_t dst_y_stride = video_frame->stride(media::VideoFrame::kYPlane); | |
145 size_t dst_u_stride = video_frame->stride(media::VideoFrame::kUPlane); | |
146 size_t dst_v_stride = video_frame->stride(media::VideoFrame::kVPlane); | |
147 | |
148 libyuv::I420Copy(src_y, src_y_stride, // Y | |
Pawel Osciak
2015/06/10 08:02:44
Please check return value.
kcwu
2015/06/11 07:29:15
Done.
| |
149 src_u, src_u_stride, // U | |
150 src_v, src_v_stride, // V | |
151 dst_y, dst_y_stride, // Y | |
152 dst_u, dst_u_stride, // U | |
153 dst_v, dst_v_stride, // V | |
154 coded_size.width(), coded_size.height()); | |
155 | |
156 vaapi_wrapper_->ReturnVaImage(&image); | |
157 | |
158 task_runner_->PostTask( | |
159 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::VideoFrameReady, | |
160 weak_this_, input_buffer_id)); | |
161 | |
162 return true; | |
163 } | |
164 | |
165 void VaapiJpegDecodeAccelerator::DecodeTask( | |
166 const scoped_ptr<DecodeRequest>& request) { | |
167 DVLOG(3) << __func__; | |
168 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
169 TRACE_EVENT0("jpeg", "DecodeTask"); | |
170 | |
171 media::JpegParseResult parse_result; | |
172 if (!media::ParseJpegPicture( | |
173 reinterpret_cast<const uint8_t*>(request->shm->memory()), | |
174 request->bitstream_buffer.size(), &parse_result)) { | |
175 DLOG(ERROR) << "ParseJpegPicture failed"; | |
176 NotifyErrorFromDecoderThread( | |
177 request->bitstream_buffer.id(), | |
178 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED); | |
179 return; | |
180 } | |
181 | |
182 // Reuse VASurface if size doesn't change. | |
183 gfx::Size coded_size(parse_result.frame_header.coded_width, | |
Pawel Osciak
2015/06/10 08:02:45
s/coded_size/new_coded_size/ perhaps?
kcwu
2015/06/11 07:29:15
Done.
| |
184 parse_result.frame_header.coded_height); | |
185 if (coded_size != coded_size_ || va_surface_id_ == VA_INVALID_SURFACE) { | |
186 vaapi_wrapper_->DestroySurfaces(); | |
187 va_surface_id_ = VA_INVALID_SURFACE; | |
188 | |
189 std::vector<VASurfaceID> va_surfaces; | |
Pawel Osciak
2015/06/10 08:02:45
va_surfaces(1)
kcwu
2015/06/11 07:29:15
no, VaapiWrapper::CreateSurfaces expect an empty v
| |
190 if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces)) { | |
Pawel Osciak
2015/06/10 08:02:45
Ouch, &va_surfaces[0].
kcwu
2015/06/11 07:29:15
No, VaapiWrapper::CreateSurfaces takes pointer of
Pawel Osciak
2015/06/11 09:12:07
Ah sorry, got it mixed up with vaCreateSurfaces. E
| |
191 LOG(ERROR) << "Create VA surface failed"; | |
192 NotifyErrorFromDecoderThread( | |
193 request->bitstream_buffer.id(), | |
194 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
195 return; | |
196 } | |
197 va_surface_id_ = va_surfaces[0]; | |
198 coded_size_ = coded_size; | |
199 } | |
200 | |
201 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, | |
202 va_surface_id_)) { | |
203 LOG(ERROR) << "Decode JPEG failed"; | |
204 NotifyErrorFromDecoderThread( | |
205 request->bitstream_buffer.id(), | |
206 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
207 return; | |
208 } | |
209 | |
210 if (!OutputPicture(va_surface_id_, request->bitstream_buffer.id(), | |
211 request->video_frame)) { | |
212 LOG(ERROR) << "Output picture failed"; | |
213 NotifyErrorFromDecoderThread( | |
214 request->bitstream_buffer.id(), | |
215 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
216 return; | |
217 } | |
218 } | |
219 | |
220 void VaapiJpegDecodeAccelerator::Decode( | |
221 const media::BitstreamBuffer& bitstream_buffer, | |
222 const scoped_refptr<media::VideoFrame>& video_frame) { | |
223 DVLOG(3) << __func__; | |
224 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
225 TRACE_EVENT1("jpeg", "Decode", "input_id", bitstream_buffer.id()); | |
226 | |
227 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id() | |
228 << " size: " << bitstream_buffer.size(); | |
229 scoped_ptr<base::SharedMemory> shm( | |
230 new base::SharedMemory(bitstream_buffer.handle(), true)); | |
231 | |
232 if (!shm->Map(bitstream_buffer.size())) { | |
233 LOG(ERROR) << "Failed to map input buffer"; | |
234 NotifyErrorFromDecoderThread(bitstream_buffer.id(), UNREADABLE_INPUT); | |
235 return; | |
236 } | |
237 | |
238 scoped_ptr<DecodeRequest> request( | |
239 new DecodeRequest(bitstream_buffer, shm.Pass(), video_frame)); | |
240 | |
241 decoder_task_runner_->PostTask( | |
242 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask, | |
243 base::Unretained(this), base::Passed(&request))); | |
244 } | |
245 | |
246 } // namespace content | |
OLD | NEW |