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 | |
palmer
2015/06/12 00:29:16
It's probably best to file a bug and mention the b
wuchengli
2015/06/12 02:29:42
There is a separate CL to add UMA. https://coderev
| |
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 uint8_t* mem = nullptr; | |
123 gfx::Size coded_size = video_frame->coded_size(); | |
124 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image, | |
125 reinterpret_cast<void**>(&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 const uint8_t* src_y = mem + image.offsets[0]; | |
136 const uint8_t* src_u = mem + image.offsets[1]; | |
137 const uint8_t* src_v = 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_t* dst_y = video_frame->data(media::VideoFrame::kYPlane); | |
142 uint8_t* dst_u = video_frame->data(media::VideoFrame::kUPlane); | |
143 uint8_t* 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 if (!libyuv::I420Copy(src_y, src_y_stride, // Y | |
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 DLOG(ERROR) << "I420Copy failed"; | |
156 return false; | |
157 } | |
158 | |
159 vaapi_wrapper_->ReturnVaImage(&image); | |
160 | |
161 task_runner_->PostTask( | |
162 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::VideoFrameReady, | |
163 weak_this_, input_buffer_id)); | |
164 | |
165 return true; | |
166 } | |
167 | |
168 void VaapiJpegDecodeAccelerator::DecodeTask( | |
169 const scoped_ptr<DecodeRequest>& request) { | |
170 DVLOG(3) << __func__; | |
171 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
172 TRACE_EVENT0("jpeg", "DecodeTask"); | |
173 | |
174 media::JpegParseResult parse_result; | |
175 if (!media::ParseJpegPicture( | |
176 reinterpret_cast<const uint8_t*>(request->shm->memory()), | |
177 request->bitstream_buffer.size(), &parse_result)) { | |
178 DLOG(ERROR) << "ParseJpegPicture failed"; | |
179 NotifyErrorFromDecoderThread( | |
180 request->bitstream_buffer.id(), | |
181 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED); | |
182 return; | |
183 } | |
184 | |
185 // Reuse VASurface if size doesn't change. | |
186 gfx::Size new_coded_size(parse_result.frame_header.coded_width, | |
187 parse_result.frame_header.coded_height); | |
188 if (new_coded_size != coded_size_ || va_surface_id_ == VA_INVALID_SURFACE) { | |
189 vaapi_wrapper_->DestroySurfaces(); | |
190 va_surface_id_ = VA_INVALID_SURFACE; | |
191 | |
192 std::vector<VASurfaceID> va_surfaces; | |
193 if (!vaapi_wrapper_->CreateSurfaces(new_coded_size, 1, &va_surfaces)) { | |
194 LOG(ERROR) << "Create VA surface failed"; | |
195 NotifyErrorFromDecoderThread( | |
196 request->bitstream_buffer.id(), | |
197 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
198 return; | |
199 } | |
200 va_surface_id_ = va_surfaces[0]; | |
201 coded_size_ = new_coded_size; | |
202 } | |
203 | |
204 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, | |
205 va_surface_id_)) { | |
206 LOG(ERROR) << "Decode JPEG failed"; | |
207 NotifyErrorFromDecoderThread( | |
208 request->bitstream_buffer.id(), | |
209 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
210 return; | |
211 } | |
212 | |
213 if (!OutputPicture(va_surface_id_, request->bitstream_buffer.id(), | |
214 request->video_frame)) { | |
215 LOG(ERROR) << "Output picture failed"; | |
216 NotifyErrorFromDecoderThread( | |
217 request->bitstream_buffer.id(), | |
218 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
219 return; | |
220 } | |
221 } | |
222 | |
223 void VaapiJpegDecodeAccelerator::Decode( | |
224 const media::BitstreamBuffer& bitstream_buffer, | |
225 const scoped_refptr<media::VideoFrame>& video_frame) { | |
226 DVLOG(3) << __func__; | |
227 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
228 TRACE_EVENT1("jpeg", "Decode", "input_id", bitstream_buffer.id()); | |
229 | |
230 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id() | |
231 << " size: " << bitstream_buffer.size(); | |
232 scoped_ptr<base::SharedMemory> shm( | |
233 new base::SharedMemory(bitstream_buffer.handle(), true)); | |
234 | |
235 if (!shm->Map(bitstream_buffer.size())) { | |
236 LOG(ERROR) << "Failed to map input buffer"; | |
237 NotifyErrorFromDecoderThread(bitstream_buffer.id(), UNREADABLE_INPUT); | |
238 return; | |
239 } | |
240 | |
241 scoped_ptr<DecodeRequest> request( | |
242 new DecodeRequest(bitstream_buffer, shm.Pass(), video_frame)); | |
243 | |
244 decoder_task_runner_->PostTask( | |
245 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask, | |
246 base::Unretained(this), base::Passed(&request))); | |
247 } | |
248 | |
249 } // namespace content | |
OLD | NEW |