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/metrics/histogram.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/synchronization/waitable_event.h" | |
13 #include "content/common/gpu/gpu_channel.h" | |
14 #include "content/common/gpu/media/vaapi_picture.h" | |
15 #include "media/base/bind_to_current_loop.h" | |
16 #include "media/base/video_frame.h" | |
17 #include "media/filters/jpeg_parser.h" | |
18 #include "media/video/picture.h" | |
19 #include "ui/gl/gl_bindings.h" | |
wuchengli
2015/04/23 09:35:18
not used?
kcwu
2015/05/08 14:42:41
Done.
| |
20 | |
21 static void ReportVaapiError() { | |
22 } | |
23 | |
24 namespace content { | |
25 | |
26 VaapiJpegDecodeAccelerator::DecodeRequest::DecodeRequest( | |
27 media::BitstreamBuffer bitstream_buffer, | |
28 scoped_refptr<media::VideoFrame> video_frame) | |
29 : bitstream_buffer(bitstream_buffer), video_frame(video_frame) { | |
30 } | |
31 | |
32 VaapiJpegDecodeAccelerator::DecodeRequest::~DecodeRequest() { | |
33 } | |
34 | |
35 void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, | |
36 Error error) { | |
37 if (message_loop_ != base::MessageLoop::current()) { | |
38 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); | |
39 message_loop_->PostTask(FROM_HERE, | |
40 base::Bind(&VaapiJpegDecodeAccelerator::NotifyError, | |
41 weak_this_, bitstream_buffer_id, error)); | |
42 return; | |
43 } | |
44 | |
45 Cleanup(); | |
46 | |
47 LOG(ERROR) << "Notifying of error " << error; | |
48 if (client_) { | |
49 client_->NotifyError(bitstream_buffer_id, error); | |
50 client_ptr_factory_.reset(); | |
51 } | |
52 } | |
53 | |
54 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator( | |
55 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) | |
56 : state_(kUninitialized), | |
57 message_loop_(base::MessageLoop::current()), | |
58 io_message_loop_(io_message_loop), | |
59 decoder_thread_("VaapiJpegDecoderThread"), | |
60 weak_this_factory_(this) { | |
61 weak_this_ = weak_this_factory_.GetWeakPtr(); | |
62 } | |
63 | |
64 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() { | |
65 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | |
66 } | |
67 | |
68 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) { | |
69 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | |
70 | |
71 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client)); | |
72 client_ = client_ptr_factory_->GetWeakPtr(); | |
73 | |
74 base::AutoLock auto_lock(lock_); | |
75 DCHECK_EQ(state_, kUninitialized); | |
76 | |
77 vaapi_wrapper_ = | |
78 VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline, | |
79 base::Bind(&ReportVaapiError)); | |
80 | |
81 if (!vaapi_wrapper_.get()) { | |
82 DLOG(ERROR) << "Failed initializing VAAPI"; | |
83 return false; | |
84 } | |
85 | |
86 CHECK(decoder_thread_.Start()); | |
wuchengli
2015/04/23 09:35:18
Log an error and return false here. Let's not cras
kcwu
2015/05/08 14:42:41
Done.
| |
87 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy(); | |
88 | |
89 state_ = kInitialized; | |
90 return true; | |
91 } | |
92 | |
93 bool VaapiJpegDecodeAccelerator::OutputPicture( | |
94 VASurfaceID va_surface_id, | |
95 int32_t input_id, | |
96 const scoped_refptr<media::VideoFrame>& video_frame) { | |
97 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); | |
98 | |
99 DVLOG(3) << "Outputting VASurface " << va_surface_id | |
100 << " into video_frame associate to input buffer id " << input_id; | |
101 | |
102 VAImage image; | |
103 VAImageFormat format; | |
104 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0'); | |
105 memset(&image, 0, sizeof(image)); | |
106 memset(&format, 0, sizeof(format)); | |
107 format.fourcc = kI420Fourcc; | |
108 format.byte_order = VA_LSB_FIRST; | |
109 format.bits_per_pixel = 12; // 12 for I420 | |
110 | |
111 void* mem; | |
112 gfx::Size coded_size = video_frame->coded_size(); | |
113 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image, | |
114 &mem)) { | |
115 DLOG(ERROR) << "Cannot get VAImage"; | |
116 return false; | |
117 } | |
118 | |
119 uint8* frame_mem = video_frame->data(media::VideoFrame::kYPlane); | |
120 size_t frame_buffer_size = | |
121 media::VideoFrame::AllocationSize(media::VideoFrame::I420, coded_size); | |
122 memcpy(frame_mem, mem, frame_buffer_size); | |
123 | |
124 vaapi_wrapper_->ReturnVaImage(&image); | |
125 | |
126 if (client_) | |
127 client_->VideoFrameReady(input_id); | |
128 | |
129 return true; | |
130 } | |
131 | |
132 void VaapiJpegDecodeAccelerator::QueueNewDecodeRequest( | |
133 const media::BitstreamBuffer& bitstream_buffer, | |
134 const scoped_refptr<media::VideoFrame>& video_frame) { | |
wuchengli
2015/04/23 09:35:18
This function and Decode are both small and the pr
kcwu
2015/05/08 14:42:41
Done.
| |
135 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
136 | |
137 // Set up a new decode request and queue it for later. | |
138 linked_ptr<DecodeRequest> input_buffer( | |
139 new DecodeRequest(bitstream_buffer, video_frame)); | |
140 base::AutoLock auto_lock(lock_); | |
141 decode_requests_.push(input_buffer); | |
142 } | |
143 | |
144 void VaapiJpegDecodeAccelerator::DecodeTask() { | |
145 DVLOG(3) << __func__; | |
146 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); | |
147 linked_ptr<DecodeRequest> request; | |
148 { | |
149 base::AutoLock auto_lock(lock_); | |
150 | |
151 DCHECK(!decode_requests_.empty()); | |
152 request = decode_requests_.front(); | |
153 decode_requests_.pop(); | |
154 } | |
155 | |
156 do { | |
157 DVLOG(4) << "Mapping new input buffer id: " | |
158 << request->bitstream_buffer.id() | |
159 << " size: " << (int)request->bitstream_buffer.size(); | |
160 | |
161 scoped_ptr<base::SharedMemory> shm( | |
162 new base::SharedMemory(request->bitstream_buffer.handle(), true)); | |
163 if (!shm->Map(request->bitstream_buffer.size())) { | |
164 LOG(ERROR) << "Failed to map input buffer"; | |
165 NotifyError(request->bitstream_buffer.id(), UNREADABLE_INPUT); | |
166 break; | |
167 } | |
168 | |
169 media::JpegParseResult parse_result; | |
170 | |
171 if (!media::ParseJpegPicture( | |
172 reinterpret_cast<const uint8_t*>(shm->memory()), | |
173 request->bitstream_buffer.size(), &parse_result)) { | |
174 DLOG(ERROR) << "ParseJpegPicture failed"; | |
175 NotifyError(request->bitstream_buffer.id(), | |
176 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED); | |
177 break; | |
178 } | |
179 | |
180 gfx::Size coded_size(parse_result.frame_header.coded_width, | |
181 parse_result.frame_header.coded_height); | |
182 | |
183 std::vector<VASurfaceID> va_surfaces; | |
184 if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces)) | |
185 break; | |
186 | |
187 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, | |
188 va_surfaces[0])) { | |
189 LOG(ERROR) << "Decode failed"; | |
190 NotifyError(request->bitstream_buffer.id(), | |
191 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
192 break; | |
193 } | |
194 | |
195 if (!OutputPicture(va_surfaces[0], request->bitstream_buffer.id(), | |
196 request->video_frame)) { | |
197 LOG(ERROR) << "Output failed"; | |
198 NotifyError(request->bitstream_buffer.id(), | |
199 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
200 break; | |
201 } | |
202 } while (0); | |
203 | |
204 base::AutoLock auto_lock(lock_); | |
wuchengli
2015/04/23 09:35:18
no need to acquire |lock_|
kcwu
2015/05/08 14:42:41
Done.
| |
205 vaapi_wrapper_->DestroySurfaces(); | |
206 } | |
207 | |
208 void VaapiJpegDecodeAccelerator::Decode( | |
209 const media::BitstreamBuffer& bitstream_buffer, | |
210 const scoped_refptr<media::VideoFrame>& video_frame) { | |
211 DVLOG(3) << __func__; | |
212 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
213 | |
wuchengli
2015/04/23 09:35:18
Check the state? Otherwise, all access of |state_|
kcwu
2015/05/08 14:42:41
Done.
| |
214 QueueNewDecodeRequest(bitstream_buffer, video_frame); | |
215 | |
216 decoder_thread_proxy_->PostTask( | |
217 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask, | |
218 base::Unretained(this))); | |
219 } | |
220 | |
221 void VaapiJpegDecodeAccelerator::Cleanup() { | |
222 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | |
223 | |
224 base::AutoLock auto_lock(lock_); | |
225 if (state_ == kUninitialized) | |
226 return; | |
227 | |
228 DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator"; | |
229 | |
230 client_ptr_factory_.reset(); | |
231 weak_this_factory_.InvalidateWeakPtrs(); | |
232 | |
233 { | |
234 base::AutoUnlock auto_unlock(lock_); | |
235 decoder_thread_.Stop(); | |
236 } | |
237 | |
238 state_ = kUninitialized; | |
239 } | |
240 | |
241 void VaapiJpegDecodeAccelerator::Destroy() { | |
242 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | |
243 Cleanup(); | |
244 delete this; | |
245 } | |
246 | |
247 } // namespace content | |
OLD | NEW |