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

Unified 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: rebase Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: content/common/gpu/media/vaapi_jpeg_decode_accelerator.cc
diff --git a/content/common/gpu/media/vaapi_jpeg_decode_accelerator.cc b/content/common/gpu/media/vaapi_jpeg_decode_accelerator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e19ae0c223897e1bcec05ac2ca669f80fe071320
--- /dev/null
+++ b/content/common/gpu/media/vaapi_jpeg_decode_accelerator.cc
@@ -0,0 +1,255 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/metrics/histogram.h"
wuchengli 2015/05/13 15:04:07 not used?
kcwu 2015/05/25 18:20:35 Done.
+#include "base/stl_util.h"
+#include "base/strings/string_util.h"
+#include "base/trace_event/trace_event.h"
+#include "content/common/gpu/gpu_channel.h"
+#include "content/common/gpu/media/vaapi_picture.h"
+#include "media/base/bind_to_current_loop.h"
wuchengli 2015/05/13 15:04:07 not used?
kcwu 2015/05/25 18:20:35 Done.
+#include "media/base/video_frame.h"
+#include "media/filters/jpeg_parser.h"
+#include "media/video/picture.h"
wuchengli 2015/05/13 15:04:07 not used?
kcwu 2015/05/25 18:20:35 Done.
+
+static void ReportVaapiError() {
wuchengli 2015/05/13 15:04:06 Add a TODO to add UMA
kcwu 2015/05/25 18:20:35 Done.
+}
+
+namespace content {
+
+VaapiJpegDecodeAccelerator::DecodeRequest::DecodeRequest(
+ const media::BitstreamBuffer& bitstream_buffer,
+ scoped_refptr<media::VideoFrame> video_frame)
+ : bitstream_buffer(bitstream_buffer), video_frame(video_frame) {
+}
+
+VaapiJpegDecodeAccelerator::DecodeRequest::~DecodeRequest() {
+}
+
+void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id,
+ Error error) {
+ if (message_loop_ != base::MessageLoop::current()) {
+ DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&VaapiJpegDecodeAccelerator::NotifyError,
+ weak_this_, bitstream_buffer_id, error));
+ return;
+ }
+
+ Cleanup();
wuchengli 2015/05/13 15:04:06 Move this after line 50. Otherwise, Cleanup will r
kcwu 2015/05/25 18:20:35 Done.
+
+ LOG(ERROR) << "Notifying of error " << error;
+ if (client_) {
+ client_->NotifyError(bitstream_buffer_id, error);
+ client_ptr_factory_.reset();
+ }
+}
+
+VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator(
+ const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
+ : initialized_(false),
+ message_loop_(base::MessageLoop::current()),
+ io_message_loop_(io_message_loop),
+ decoder_thread_("VaapiJpegDecoderThread"),
+ va_surface_(VA_INVALID_SURFACE),
+ weak_this_factory_(this) {
+ weak_this_ = weak_this_factory_.GetWeakPtr();
+}
+
+VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() {
+ DCHECK_EQ(message_loop_, base::MessageLoop::current());
+}
+
+bool VaapiJpegDecodeAccelerator::Initialize(Client* client) {
+ DCHECK_EQ(message_loop_, base::MessageLoop::current());
+
+ client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
+ client_ = client_ptr_factory_->GetWeakPtr();
+
+ base::AutoLock auto_lock(lock_);
+ DCHECK(!initialized_);
+
+ vaapi_wrapper_ =
+ VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline,
+ base::Bind(&ReportVaapiError));
+
+ if (!vaapi_wrapper_.get()) {
+ DLOG(ERROR) << "Failed initializing VAAPI";
+ return false;
+ }
+
+ if (!decoder_thread_.Start()) {
+ LOG(ERROR) << "Failed to start decoding thread.";
+ return false;
+ }
+ decoder_thread_proxy_ = decoder_thread_.message_loop_proxy();
+
+ initialized_ = true;
+ return true;
+}
+
+bool VaapiJpegDecodeAccelerator::OutputPicture(
+ VASurfaceID va_surface_id,
+ int32_t input_buffer_id,
+ const scoped_refptr<media::VideoFrame>& video_frame) {
+ DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
+
+ TRACE_EVENT1("jpeg", "VaapiJpegDecodeAccelerator::OutputPicture",
+ "input_buffer_id", input_buffer_id);
+
+ DVLOG(3) << "Outputting VASurface " << va_surface_id
+ << " into video_frame associate to input buffer id "
wuchengli 2015/05/13 15:04:06 s/associate to/associated with/
kcwu 2015/05/25 18:20:35 Done.
+ << input_buffer_id;
+
+ VAImage image;
+ VAImageFormat format;
+ const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0');
+ memset(&image, 0, sizeof(image));
+ memset(&format, 0, sizeof(format));
+ format.fourcc = kI420Fourcc;
+ format.byte_order = VA_LSB_FIRST;
+ format.bits_per_pixel = 12; // 12 for I420
+
+ void* mem;
+ gfx::Size coded_size = video_frame->coded_size();
+ if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image,
+ &mem)) {
+ DLOG(ERROR) << "Cannot get VAImage";
+ return false;
+ }
+
+ uint8* frame_mem = video_frame->data(media::VideoFrame::kYPlane);
+ size_t frame_buffer_size =
+ media::VideoFrame::AllocationSize(media::VideoFrame::I420, coded_size);
+ memcpy(frame_mem, mem, frame_buffer_size);
+
+ vaapi_wrapper_->ReturnVaImage(&image);
+
+ if (client_)
wuchengli 2015/05/13 15:04:06 |client_| can only be dereferenced on the child th
kcwu 2015/05/25 18:20:35 Done.
+ client_->VideoFrameReady(input_buffer_id);
+
+ return true;
+}
+
+void VaapiJpegDecodeAccelerator::DecodeTask() {
+ DVLOG(3) << __func__;
+ DCHECK(decoder_thread_proxy_->BelongsToCurrentThread());
+ TRACE_EVENT0("jpeg", "DecodeTask");
+ linked_ptr<DecodeRequest> request;
+ {
+ base::AutoLock auto_lock(lock_);
+
+ DCHECK(!decode_requests_.empty());
+ request = decode_requests_.front();
+ decode_requests_.pop();
+ }
+
+ do {
+ DVLOG(4) << "Mapping new input buffer id: "
+ << request->bitstream_buffer.id()
+ << " size: " << (int)request->bitstream_buffer.size();
+
+ scoped_ptr<base::SharedMemory> shm(
+ new base::SharedMemory(request->bitstream_buffer.handle(), true));
+ if (!shm->Map(request->bitstream_buffer.size())) {
+ LOG(ERROR) << "Failed to map input buffer";
+ NotifyError(request->bitstream_buffer.id(), UNREADABLE_INPUT);
+ break;
+ }
+
+ media::JpegParseResult parse_result;
+
+ if (!media::ParseJpegPicture(
+ reinterpret_cast<const uint8_t*>(shm->memory()),
+ request->bitstream_buffer.size(), &parse_result)) {
+ DLOG(ERROR) << "ParseJpegPicture failed";
+ NotifyError(request->bitstream_buffer.id(),
+ media::JpegDecodeAccelerator::PARSE_JPEG_FAILED);
+ break;
+ }
+
+ // Reuse VASurface if size doesn't change.
+ // This is not only optimization, but also to avoid libva resoruce leak.
+ // crosbug.com/p/39584
wuchengli 2015/05/13 15:04:07 Remove the leak comment because it's being fixed.
kcwu 2015/05/25 18:20:35 Done.
+ gfx::Size coded_size(parse_result.frame_header.coded_width,
+ parse_result.frame_header.coded_height);
+ if (coded_size != last_coded_size_ || va_surface_ == VA_INVALID_SURFACE) {
+ vaapi_wrapper_->DestroySurfaces();
+ std::vector<VASurfaceID> va_surfaces;
+ if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces))
+ break;
+ va_surface_ = va_surfaces[0];
+ last_coded_size_ = coded_size;
+ }
+
+ if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result,
+ va_surface_)) {
+ LOG(ERROR) << "Decode failed";
wuchengli 2015/05/13 15:04:07 Decode JPEG failed
kcwu 2015/05/25 18:20:35 Done.
+ NotifyError(request->bitstream_buffer.id(),
+ media::JpegDecodeAccelerator::PLATFORM_FAILURE);
+ break;
+ }
+
+ if (!OutputPicture(va_surface_, request->bitstream_buffer.id(),
+ request->video_frame)) {
+ LOG(ERROR) << "Output failed";
wuchengli 2015/05/13 15:04:06 Output picture failed
kcwu 2015/05/25 18:20:35 Done.
+ NotifyError(request->bitstream_buffer.id(),
+ media::JpegDecodeAccelerator::PLATFORM_FAILURE);
+ break;
+ }
+ } while (0);
wuchengli 2015/05/13 15:04:06 There's no code after while. Remove do/while and r
kcwu 2015/05/25 18:20:35 Done.
+}
+
+void VaapiJpegDecodeAccelerator::Decode(
+ const media::BitstreamBuffer& bitstream_buffer,
+ const scoped_refptr<media::VideoFrame>& video_frame) {
+ DVLOG(3) << __func__;
+ DCHECK(io_message_loop_->BelongsToCurrentThread());
+ TRACE_EVENT1("jpeg", "Decode", "input_id", bitstream_buffer.id());
+
+ if (!initialized_)
wuchengli 2015/05/13 15:04:06 Move auto_lock before this.
kcwu 2015/05/25 18:20:35 Done.
+ return;
+
+ // Set up a new decode request and queue it for later.
+ linked_ptr<DecodeRequest> input_buffer(
+ new DecodeRequest(bitstream_buffer, video_frame));
+ base::AutoLock auto_lock(lock_);
+ decode_requests_.push(input_buffer);
+
+ decoder_thread_proxy_->PostTask(
+ FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask,
+ base::Unretained(this)));
+}
+
+void VaapiJpegDecodeAccelerator::Cleanup() {
+ DCHECK_EQ(message_loop_, base::MessageLoop::current());
+
+ base::AutoLock auto_lock(lock_);
+ if (!initialized_)
+ return;
+
+ DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator";
+
+ client_ptr_factory_.reset();
+ weak_this_factory_.InvalidateWeakPtrs();
+
+ {
+ base::AutoUnlock auto_unlock(lock_);
+ decoder_thread_.Stop();
+ }
+
+ initialized_ = false;
+}
+
+void VaapiJpegDecodeAccelerator::Destroy() {
+ DCHECK_EQ(message_loop_, base::MessageLoop::current());
+ Cleanup();
+ delete this;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698