OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/renderer/media/crypto/ppapi/libvpx_cdm_video_decoder.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "media/base/buffers.h" | |
10 #include "media/base/limits.h" | |
11 | |
12 // Include libvpx header files. | |
13 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide | |
14 // backwards compatibility for legacy applications using the library. | |
15 #define VPX_CODEC_DISABLE_COMPAT 1 | |
16 extern "C" { | |
17 #include "third_party/libvpx/source/libvpx/vpx/vp8dx.h" | |
18 #include "third_party/libvpx/source/libvpx/vpx/vpx_decoder.h" | |
19 } | |
20 | |
21 #include "webkit/renderer/media/crypto/ppapi/cdm/content_decryption_module.h" | |
22 | |
23 // Enable USE_COPYPLANE_WITH_LIBVPX to use |CopyPlane()| instead of memcpy to | |
24 // copy video frame data. | |
25 // #define USE_COPYPLANE_WITH_LIBVPX 1 | |
26 | |
27 namespace webkit_media { | |
28 | |
29 static const int kDecodeThreads = 2; | |
30 | |
31 LibvpxCdmVideoDecoder::LibvpxCdmVideoDecoder(cdm::Host* host) | |
32 : is_initialized_(false), | |
33 host_(host), | |
34 vpx_codec_(NULL), | |
35 vpx_image_(NULL) { | |
36 } | |
37 | |
38 LibvpxCdmVideoDecoder::~LibvpxCdmVideoDecoder() { | |
39 Deinitialize(); | |
40 } | |
41 | |
42 bool LibvpxCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) { | |
43 DVLOG(1) << "Initialize()"; | |
44 | |
45 if (!IsValidOutputConfig(config.format, config.coded_size)) { | |
46 LOG(ERROR) << "Initialize(): invalid video decoder configuration."; | |
47 return false; | |
48 } | |
49 | |
50 if (is_initialized_) { | |
51 LOG(ERROR) << "Initialize(): Already initialized."; | |
52 return false; | |
53 } | |
54 | |
55 vpx_codec_ = new vpx_codec_ctx(); | |
56 vpx_codec_dec_cfg_t vpx_config = {0}; | |
57 vpx_config.w = config.coded_size.width; | |
58 vpx_config.h = config.coded_size.height; | |
59 vpx_config.threads = kDecodeThreads; | |
60 | |
61 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_, | |
62 vpx_codec_vp8_dx(), | |
63 &vpx_config, | |
64 0); | |
65 if (status != VPX_CODEC_OK) { | |
66 LOG(ERROR) << "InitializeLibvpx(): vpx_codec_dec_init failed, ret=" | |
67 << status; | |
68 delete vpx_codec_; | |
69 vpx_codec_ = NULL; | |
70 } | |
71 | |
72 is_initialized_ = true; | |
73 return true; | |
74 } | |
75 | |
76 void LibvpxCdmVideoDecoder::Deinitialize() { | |
77 DVLOG(1) << "Deinitialize()"; | |
78 | |
79 if (vpx_codec_) { | |
80 vpx_codec_destroy(vpx_codec_); | |
81 vpx_codec_ = NULL; | |
82 } | |
83 | |
84 is_initialized_ = false; | |
85 } | |
86 | |
87 void LibvpxCdmVideoDecoder::Reset() { | |
88 DVLOG(1) << "Reset()"; | |
89 } | |
90 | |
91 // static | |
92 bool LibvpxCdmVideoDecoder::IsValidOutputConfig(cdm::VideoFormat format, | |
93 const cdm::Size& data_size) { | |
94 return ((format == cdm::kYv12 || format == cdm::kI420) && | |
95 (data_size.width % 2) == 0 && (data_size.height % 2) == 0 && | |
96 data_size.width > 0 && data_size.height > 0 && | |
97 data_size.width <= media::limits::kMaxDimension && | |
98 data_size.height <= media::limits::kMaxDimension && | |
99 data_size.width * data_size.height <= media::limits::kMaxCanvas); | |
100 } | |
101 | |
102 cdm::Status LibvpxCdmVideoDecoder::DecodeFrame( | |
103 const uint8_t* compressed_frame, | |
104 int32_t compressed_frame_size, | |
105 int64_t timestamp, | |
106 cdm::VideoFrame* decoded_frame) { | |
107 DVLOG(1) << "DecodeFrame()"; | |
108 DCHECK(decoded_frame); | |
109 | |
110 // Pass |compressed_frame| to libvpx. | |
111 void* user_priv = reinterpret_cast<void*>(×tamp); | |
112 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_, | |
113 compressed_frame, | |
114 compressed_frame_size, | |
115 user_priv, | |
116 0); | |
117 if (status != VPX_CODEC_OK) { | |
118 LOG(ERROR) << "DecodeFrameLibvpx(): vpx_codec_decode failed, status=" | |
119 << status; | |
120 return cdm::kDecodeError; | |
121 } | |
122 | |
123 // Gets pointer to decoded data. | |
124 vpx_codec_iter_t iter = NULL; | |
125 vpx_image_ = vpx_codec_get_frame(vpx_codec_, &iter); | |
126 if (!vpx_image_) | |
127 return cdm::kNeedMoreData; | |
128 | |
129 if (vpx_image_->user_priv != reinterpret_cast<void*>(×tamp)) { | |
130 LOG(ERROR) << "DecodeFrameLibvpx() invalid output timestamp."; | |
131 return cdm::kDecodeError; | |
132 } | |
133 decoded_frame->SetTimestamp(timestamp); | |
134 | |
135 if (!CopyVpxImageTo(decoded_frame)) { | |
136 LOG(ERROR) << "DecodeFrameLibvpx() could not copy vpx image to output " | |
137 << "buffer."; | |
138 return cdm::kDecodeError; | |
139 } | |
140 | |
141 return cdm::kSuccess; | |
142 } | |
143 | |
144 bool LibvpxCdmVideoDecoder::CopyVpxImageTo(cdm::VideoFrame* cdm_video_frame) { | |
145 DCHECK(cdm_video_frame); | |
146 DCHECK_EQ(vpx_image_->fmt, VPX_IMG_FMT_I420); | |
147 DCHECK_EQ(vpx_image_->d_w % 2, 0U); | |
148 DCHECK_EQ(vpx_image_->d_h % 2, 0U); | |
149 | |
150 const int y_size = vpx_image_->stride[VPX_PLANE_Y] * vpx_image_->d_h; | |
151 const int uv_rows = vpx_image_->d_h / 2; | |
152 const int u_size = vpx_image_->stride[VPX_PLANE_U] * uv_rows; | |
153 const int v_size = vpx_image_->stride[VPX_PLANE_V] * uv_rows; | |
154 const int space_required = y_size + u_size + v_size; | |
155 | |
156 DCHECK(!cdm_video_frame->FrameBuffer()); | |
157 cdm_video_frame->SetFrameBuffer(host_->Allocate(space_required)); | |
158 if (!cdm_video_frame->FrameBuffer()) { | |
159 LOG(ERROR) << "CopyVpxImageTo() cdm::Host::Allocate failed."; | |
160 return false; | |
161 } | |
162 cdm_video_frame->FrameBuffer()->SetSize(space_required); | |
163 | |
164 memcpy(cdm_video_frame->FrameBuffer()->Data(), | |
165 vpx_image_->planes[VPX_PLANE_Y], | |
166 y_size); | |
167 memcpy(cdm_video_frame->FrameBuffer()->Data() + y_size, | |
168 vpx_image_->planes[VPX_PLANE_U], | |
169 u_size); | |
170 memcpy(cdm_video_frame->FrameBuffer()->Data() + y_size + u_size, | |
171 vpx_image_->planes[VPX_PLANE_V], | |
172 v_size); | |
173 | |
174 cdm_video_frame->SetFormat(cdm::kYv12); | |
175 | |
176 cdm::Size video_frame_size; | |
177 video_frame_size.width = vpx_image_->d_w; | |
178 video_frame_size.height = vpx_image_->d_h; | |
179 cdm_video_frame->SetSize(video_frame_size); | |
180 | |
181 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kYPlane, 0); | |
182 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kUPlane, y_size); | |
183 cdm_video_frame->SetPlaneOffset(cdm::VideoFrame::kVPlane, | |
184 y_size + u_size); | |
185 | |
186 cdm_video_frame->SetStride(cdm::VideoFrame::kYPlane, | |
187 vpx_image_->stride[VPX_PLANE_Y]); | |
188 cdm_video_frame->SetStride(cdm::VideoFrame::kUPlane, | |
189 vpx_image_->stride[VPX_PLANE_U]); | |
190 cdm_video_frame->SetStride(cdm::VideoFrame::kVPlane, | |
191 vpx_image_->stride[VPX_PLANE_V]); | |
192 | |
193 return true; | |
194 } | |
195 | |
196 } // namespace webkit_media | |
OLD | NEW |