Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: content/common/gpu/media/vaapi_jpeg_decode_accelerator.cc

Issue 1016773002: MJPEG acceleration for video capture using VAAPI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move JPEG related code to separated file. And address some other comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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"
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 // Post Cleanup() as a task so we don't recursively acquire lock_.
46 message_loop_->PostTask(
47 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::Cleanup, weak_this_));
48
49 LOG(ERROR) << "Notifying of error " << error;
50 if (client_) {
51 client_->NotifyError(bitstream_buffer_id, error);
52 client_ptr_factory_.reset();
53 }
54 }
55
56 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator(
57 const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
58 : state_(kUninitialized),
59 message_loop_(base::MessageLoop::current()),
60 io_message_loop_(io_message_loop),
61 decoder_thread_("VaapiDecoderThread"),
62 weak_this_factory_(this) {
63 weak_this_ = weak_this_factory_.GetWeakPtr();
64 }
65
66 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() {
67 DCHECK_EQ(message_loop_, base::MessageLoop::current());
68 }
69
70 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) {
71 DCHECK_EQ(message_loop_, base::MessageLoop::current());
72
73 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
74 client_ = client_ptr_factory_->GetWeakPtr();
75
76 base::AutoLock auto_lock(lock_);
77 DCHECK_EQ(state_, kUninitialized);
78
79 vaapi_wrapper_ =
80 VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline,
81 base::Bind(&ReportVaapiError));
82
83 if (!vaapi_wrapper_.get()) {
84 DLOG(ERROR) << "Failed initializing VAAPI";
85 return false;
86 }
87
88 CHECK(decoder_thread_.Start());
89 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
90
91 state_ = kIdle;
92 return true;
93 }
94
95 bool VaapiJpegDecodeAccelerator::OutputPicture(
96 VASurfaceID va_surface_id,
97 int32_t input_id,
98 const scoped_refptr<media::VideoFrame>& video_frame) {
99 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
100
101 DVLOG(3) << "Outputting VASurface " << va_surface_id
102 << " into video_frame associate to input buffer id " << input_id;
103
104 VAImage image;
105 VAImageFormat format;
106 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0');
107 memset(&image, 0, sizeof(image));
108 memset(&format, 0, sizeof(format));
109 format.fourcc = kI420Fourcc;
110 format.byte_order = VA_LSB_FIRST;
111 format.bits_per_pixel = 12; // 12 for I420
112
113 void* mem;
114 gfx::Size coded_size = video_frame->coded_size();
115 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image,
116 &mem)) {
117 DLOG(ERROR) << "Cannot get VAImage";
118 return false;
119 }
120
121 uint8* frame_mem = video_frame->data(media::VideoFrame::kYPlane);
122 size_t frame_buffer_size =
123 media::VideoFrame::AllocationSize(media::VideoFrame::I420, coded_size);
124 memcpy(frame_mem, mem, frame_buffer_size);
125
126 vaapi_wrapper_->ReturnVaImage(&image);
127
128 if (client_)
129 client_->VideoFrameReady(input_id);
130
131 return true;
132 }
133
134 void VaapiJpegDecodeAccelerator::QueueNewDecodeRequest(
135 const media::BitstreamBuffer& bitstream_buffer,
136 const scoped_refptr<media::VideoFrame>& video_frame) {
137 DCHECK(io_message_loop_->BelongsToCurrentThread());
138
139 // Set up a new decode request and queue it for later.
140 linked_ptr<DecodeRequest> input_buffer(
141 new DecodeRequest(bitstream_buffer, video_frame));
142 base::AutoLock auto_lock(lock_);
143 decode_requests_.push(input_buffer);
144 }
145
146 void VaapiJpegDecodeAccelerator::DecodeTask() {
147 DVLOG(3) << __func__;
148 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
149 base::AutoLock auto_lock(lock_);
150
151 if (state_ != kIdle) {
152 DLOG(ERROR) << "not idle state";
153 return;
154 }
155
156 state_ = kDecoding;
157
158 DCHECK(!decode_requests_.empty());
159 linked_ptr<DecodeRequest> request = decode_requests_.front();
160 decode_requests_.pop();
161
162 do {
163 base::AutoUnlock auto_unlock(lock_);
164
165 DVLOG(4) << "Mapping new input buffer id: "
166 << request->bitstream_buffer.id()
167 << " size: " << (int)request->bitstream_buffer.size();
168
169 scoped_ptr<base::SharedMemory> shm(
170 new base::SharedMemory(request->bitstream_buffer.handle(), true));
171 if (!shm->Map(request->bitstream_buffer.size())) {
172 LOG(ERROR) << "Failed to map input buffer";
173 NotifyError(request->bitstream_buffer.id(), UNREADABLE_INPUT);
174 break;
175 }
176
177 media::JpegParseResult parse_result;
178
179 if (!media::ParseJpegPicture(
180 reinterpret_cast<const uint8_t*>(shm->memory()),
181 request->bitstream_buffer.size(), &parse_result)) {
182 NotifyError(request->bitstream_buffer.id(),
183 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED);
184 break;
185 }
186
187 gfx::Size coded_size(parse_result.frame_header.coded_width,
188 parse_result.frame_header.coded_height);
189
190 std::vector<VASurfaceID> va_surfaces;
191 if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces))
192 break;
193
194 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result,
195 va_surfaces[0])) {
196 LOG(ERROR) << "Decode failed";
197 NotifyError(request->bitstream_buffer.id(),
198 media::JpegDecodeAccelerator::PLATFORM_FAILURE);
199 break;
200 }
201
202 if (!OutputPicture(va_surfaces[0], request->bitstream_buffer.id(),
203 request->video_frame)) {
204 LOG(ERROR) << "Output failed";
205 NotifyError(request->bitstream_buffer.id(),
206 media::JpegDecodeAccelerator::PLATFORM_FAILURE);
207 break;
208 }
209 } while (0);
210 vaapi_wrapper_->DestroySurfaces();
211
212 state_ = kIdle;
213 }
214
215 void VaapiJpegDecodeAccelerator::Decode(
216 const media::BitstreamBuffer& bitstream_buffer,
217 const scoped_refptr<media::VideoFrame>& video_frame) {
218 DVLOG(3) << __func__;
219 DCHECK(io_message_loop_->BelongsToCurrentThread());
220
221 QueueNewDecodeRequest(bitstream_buffer, video_frame);
222
223 decoder_thread_proxy_->PostTask(
224 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask,
225 base::Unretained(this)));
226 }
227
228 void VaapiJpegDecodeAccelerator::Cleanup() {
229 DCHECK_EQ(message_loop_, base::MessageLoop::current());
230
231 base::AutoLock auto_lock(lock_);
232 if (state_ == kUninitialized || state_ == kDestroying)
233 return;
234
235 DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator";
236 state_ = kDestroying;
237
238 client_ptr_factory_.reset();
239 weak_this_factory_.InvalidateWeakPtrs();
240
241 {
242 base::AutoUnlock auto_unlock(lock_);
243 decoder_thread_.Stop();
244 }
245
246 state_ = kUninitialized;
247 }
248
249 void VaapiJpegDecodeAccelerator::Destroy() {
250 DCHECK_EQ(message_loop_, base::MessageLoop::current());
251 Cleanup();
252 delete this;
253 }
254
255 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698