Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cdm / ppapi / external_clear_key / libvpx_cdm_video_decoder.cc
blob7aed711f88fc95bc1e0ddeffc648afdc6b9332dd
1 // Copyright 2013 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.
5 #include "media/cdm/ppapi/external_clear_key/libvpx_cdm_video_decoder.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "media/base/limits.h"
11 // Include libvpx header files.
12 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide
13 // backwards compatibility for legacy applications using the library.
14 #define VPX_CODEC_DISABLE_COMPAT 1
15 extern "C" {
16 // Note: vpx_decoder.h must be first or compile will fail.
17 #include "third_party/libvpx_new/source/libvpx/vpx/vp8dx.h"
18 #include "third_party/libvpx_new/source/libvpx/vpx/vpx_decoder.h" // NOLINT
21 // Enable USE_COPYPLANE_WITH_LIBVPX to use |CopyPlane()| instead of memcpy to
22 // copy video frame data.
23 // #define USE_COPYPLANE_WITH_LIBVPX 1
25 namespace media {
27 static const int kDecodeThreads = 2;
29 LibvpxCdmVideoDecoder::LibvpxCdmVideoDecoder(CdmHost* host)
30 : is_initialized_(false),
31 host_(host),
32 vpx_codec_(NULL),
33 vpx_image_(NULL) {
36 LibvpxCdmVideoDecoder::~LibvpxCdmVideoDecoder() {
37 Deinitialize();
40 bool LibvpxCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) {
41 DVLOG(1) << "Initialize()";
43 if (!IsValidOutputConfig(config.format, config.coded_size)) {
44 LOG(ERROR) << "Initialize(): invalid video decoder configuration.";
45 return false;
48 if (is_initialized_) {
49 LOG(ERROR) << "Initialize(): Already initialized.";
50 return false;
53 vpx_codec_ = new vpx_codec_ctx();
54 vpx_codec_dec_cfg_t vpx_config = {0};
55 vpx_config.w = config.coded_size.width;
56 vpx_config.h = config.coded_size.height;
57 vpx_config.threads = kDecodeThreads;
59 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_,
60 vpx_codec_vp8_dx(),
61 &vpx_config,
62 0);
63 if (status != VPX_CODEC_OK) {
64 LOG(ERROR) << "InitializeLibvpx(): vpx_codec_dec_init failed, ret="
65 << status;
66 delete vpx_codec_;
67 vpx_codec_ = NULL;
70 is_initialized_ = true;
71 return true;
74 void LibvpxCdmVideoDecoder::Deinitialize() {
75 DVLOG(1) << "Deinitialize()";
77 if (vpx_codec_) {
78 vpx_codec_destroy(vpx_codec_);
79 vpx_codec_ = NULL;
82 is_initialized_ = false;
85 void LibvpxCdmVideoDecoder::Reset() {
86 DVLOG(1) << "Reset()";
89 // static
90 bool LibvpxCdmVideoDecoder::IsValidOutputConfig(cdm::VideoFormat format,
91 const cdm::Size& data_size) {
92 return ((format == cdm::kYv12 || format == cdm::kI420) &&
93 (data_size.width % 2) == 0 && (data_size.height % 2) == 0 &&
94 data_size.width > 0 && data_size.height > 0 &&
95 data_size.width <= limits::kMaxDimension &&
96 data_size.height <= limits::kMaxDimension &&
97 data_size.width * data_size.height <= limits::kMaxCanvas);
100 cdm::Status LibvpxCdmVideoDecoder::DecodeFrame(
101 const uint8_t* compressed_frame,
102 int32_t compressed_frame_size,
103 int64_t timestamp,
104 cdm::VideoFrame* decoded_frame) {
105 DVLOG(1) << "DecodeFrame()";
106 DCHECK(decoded_frame);
108 // Pass |compressed_frame| to libvpx.
109 void* user_priv = reinterpret_cast<void*>(&timestamp);
110 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_,
111 compressed_frame,
112 compressed_frame_size,
113 user_priv,
115 if (status != VPX_CODEC_OK) {
116 LOG(ERROR) << "DecodeFrameLibvpx(): vpx_codec_decode failed, status="
117 << status;
118 return cdm::kDecodeError;
121 // Gets pointer to decoded data.
122 vpx_codec_iter_t iter = NULL;
123 vpx_image_ = vpx_codec_get_frame(vpx_codec_, &iter);
124 if (!vpx_image_)
125 return cdm::kNeedMoreData;
127 if (vpx_image_->user_priv != reinterpret_cast<void*>(&timestamp)) {
128 LOG(ERROR) << "DecodeFrameLibvpx() invalid output timestamp.";
129 return cdm::kDecodeError;
131 decoded_frame->SetTimestamp(timestamp);
133 if (!CopyVpxImageTo(decoded_frame)) {
134 LOG(ERROR) << "DecodeFrameLibvpx() could not copy vpx image to output "
135 << "buffer.";
136 return cdm::kDecodeError;
139 return cdm::kSuccess;
142 bool LibvpxCdmVideoDecoder::CopyVpxImageTo(cdm::VideoFrame* cdm_video_frame) {
143 DCHECK(cdm_video_frame);
144 DCHECK_EQ(vpx_image_->fmt, VPX_IMG_FMT_I420);
145 DCHECK_EQ(vpx_image_->d_w % 2, 0U);
146 DCHECK_EQ(vpx_image_->d_h % 2, 0U);
148 const int y_size = vpx_image_->stride[VPX_PLANE_Y] * vpx_image_->d_h;
149 const int uv_rows = vpx_image_->d_h / 2;
150 const int u_size = vpx_image_->stride[VPX_PLANE_U] * uv_rows;
151 const int v_size = vpx_image_->stride[VPX_PLANE_V] * uv_rows;
152 const int space_required = y_size + u_size + v_size;
154 DCHECK(!cdm_video_frame->FrameBuffer());
155 cdm_video_frame->SetFrameBuffer(host_->Allocate(space_required));
156 if (!cdm_video_frame->FrameBuffer()) {
157 LOG(ERROR) << "CopyVpxImageTo() CdmHost::Allocate failed.";
158 return false;
160 cdm_video_frame->FrameBuffer()->SetSize(space_required);
162 memcpy(cdm_video_frame->FrameBuffer()->Data(),
163 vpx_image_->planes[VPX_PLANE_Y],
164 y_size);
165 memcpy(cdm_video_frame->FrameBuffer()->Data() + y_size,
166 vpx_image_->planes[VPX_PLANE_U],
167 u_size);
168 memcpy(cdm_video_frame->FrameBuffer()->Data() + y_size + u_size,
169 vpx_image_->planes[VPX_PLANE_V],
170 v_size);
172 cdm_video_frame->SetFormat(cdm::kYv12);
174 cdm::Size video_frame_size;
175 video_frame_size.width = vpx_image_->d_w;
176 video_frame_size.height = vpx_image_->d_h;
177 cdm_video_frame->SetSize(video_frame_size);
179 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kYPlane, 0);
180 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kUPlane, y_size);
181 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kVPlane,
182 y_size + u_size);
184 cdm_video_frame->SetStride(cdm::VideoFrame::kYPlane,
185 vpx_image_->stride[VPX_PLANE_Y]);
186 cdm_video_frame->SetStride(cdm::VideoFrame::kUPlane,
187 vpx_image_->stride[VPX_PLANE_U]);
188 cdm_video_frame->SetStride(cdm::VideoFrame::kVPlane,
189 vpx_image_->stride[VPX_PLANE_V]);
191 return true;
194 } // namespace media