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

Side by Side Diff: media/filters/vpx_video_decoder.cc

Issue 11468018: Add libvpx wrapper to media to support VP9 video decoding. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add work around for lack of VP9 in ffmpeg. Created 8 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "media/filters/vpx_video_decoder.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/message_loop_proxy.h"
14 #include "base/string_number_conversions.h"
15 #include "media/base/bind_to_loop.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/demuxer_stream.h"
19 #include "media/base/media_switches.h"
20 #include "media/base/pipeline.h"
21 #include "media/base/video_decoder_config.h"
22 #include "media/base/video_frame.h"
23 #include "media/base/video_util.h"
24
25 // Include libvpx header files.
26 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide
27 // backwards compatibility for legacy applications using the library.
28 #define VPX_CODEC_DISABLE_COMPAT 1
29 extern "C" {
30 #include "third_party/libvpx/libvpx.h"
31 }
32
33 namespace media {
34
35 // Always try to use three threads for video decoding. There is little reason
36 // not to since current day CPUs tend to be multi-core and we measured
37 // performance benefits on older machines such as P4s with hyperthreading.
38 static const int kDecodeThreads = 2;
39 static const int kMaxDecodeThreads = 16;
40
41 // Returns the number of threads.
42 static int GetThreadCount() {
scherkus (not reviewing) 2012/12/18 01:01:56 can you add a TODO(scherkus) to de-duplicate this
Tom Finegan 2012/12/18 01:47:42 Done.
43 // Refer to http://crbug.com/93932 for tsan suppressions on decoding.
44 int decode_threads = kDecodeThreads;
45
46 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
47 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads));
48 if (threads.empty() || !base::StringToInt(threads, &decode_threads))
49 return decode_threads;
50
51 decode_threads = std::max(decode_threads, 0);
52 decode_threads = std::min(decode_threads, kMaxDecodeThreads);
53 return decode_threads;
54 }
55
56 VpxVideoDecoder::VpxVideoDecoder(
57 const scoped_refptr<base::MessageLoopProxy>& message_loop)
58 : message_loop_(message_loop),
59 state_(kUninitialized),
60 vpx_codec_(NULL) {
61 }
62
63 VpxVideoDecoder::~VpxVideoDecoder() {
64 DCHECK_EQ(kUninitialized, state_);
65 CloseDecoder();
66 }
67
68 void VpxVideoDecoder::Initialize(
69 const scoped_refptr<DemuxerStream>& stream,
70 const PipelineStatusCB& status_cb,
71 const StatisticsCB& statistics_cb) {
72 DCHECK(message_loop_->BelongsToCurrentThread());
73 DCHECK(!demuxer_stream_) << "Already initialized.";
74
75 if (!stream) {
76 status_cb.Run(PIPELINE_ERROR_DECODE);
77 return;
78 }
79
80 demuxer_stream_ = stream;
81 statistics_cb_ = statistics_cb;
82
83 if (!ConfigureDecoder()) {
84 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
85 return;
86 }
87
88 // Success!
89 state_ = kNormal;
90 status_cb.Run(PIPELINE_OK);
91 }
92
93 bool VpxVideoDecoder::ConfigureDecoder() {
94 const VideoDecoderConfig& config = demuxer_stream_->video_decoder_config();
95 if (!config.IsValidConfig()) {
96 DLOG(ERROR) << "Invalid video stream config: "
97 << config.AsHumanReadableString();
98 return false;
99 }
100
101 if (config.codec() != kCodecVP9)
102 return false;
103
104 CloseDecoder();
105
106 vpx_codec_ = new vpx_codec_ctx();
107 vpx_codec_dec_cfg_t vpx_config = {0};
108 vpx_config.w = config.coded_size().width();
109 vpx_config.h = config.coded_size().height();
110 vpx_config.threads = GetThreadCount();
111
112 vpx_codec_err_t status = vpx_codec_dec_init(vpx_codec_,
113 vpx_codec_vp9_dx(),
114 &vpx_config,
115 0);
116 if (status != VPX_CODEC_OK) {
117 LOG(ERROR) << "vpx_codec_dec_init failed, status=" << status;
118 delete vpx_codec_;
119 vpx_codec_ = NULL;
120 return false;
121 }
122
123 return true;
124 }
125
126 void VpxVideoDecoder::CloseDecoder() {
127 if (vpx_codec_) {
128 vpx_codec_destroy(vpx_codec_);
129 delete vpx_codec_;
130 vpx_codec_ = NULL;
131 }
132 }
133
134 void VpxVideoDecoder::Read(const ReadCB& read_cb) {
135 DCHECK(message_loop_->BelongsToCurrentThread());
136 DCHECK(!read_cb.is_null());
137 CHECK_NE(state_, kUninitialized);
138 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported.";
139 read_cb_ = BindToCurrentLoop(read_cb);
140
141 // Return empty frames if decoding has finished.
142 if (state_ == kDecodeFinished) {
143 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame());
144 return;
145 }
146
147 ReadFromDemuxerStream();
148 }
149
150 void VpxVideoDecoder::Reset(const base::Closure& closure) {
151 DCHECK(message_loop_->BelongsToCurrentThread());
152 DCHECK(reset_cb_.is_null());
153 reset_cb_ = BindToCurrentLoop(closure);
154
155 // Defer the reset if a read is pending.
156 if (!read_cb_.is_null())
157 return;
158
159 DoReset();
160 }
161
162 void VpxVideoDecoder::Stop(const base::Closure& closure) {
163 DCHECK(message_loop_->BelongsToCurrentThread());
164
165 if (state_ == kUninitialized) {
166 closure.Run();
167 return;
168 }
169
170 if (!read_cb_.is_null())
171 base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
172
173 state_ = kUninitialized;
174 closure.Run();
175 }
176
177 void VpxVideoDecoder::ReadFromDemuxerStream() {
178 DCHECK_NE(state_, kUninitialized);
179 DCHECK_NE(state_, kDecodeFinished);
180 DCHECK(!read_cb_.is_null());
181
182 demuxer_stream_->Read(base::Bind(
183 &VpxVideoDecoder::DoDecryptOrDecodeBuffer, this));
184 }
185
186 void VpxVideoDecoder::DoDecryptOrDecodeBuffer(
187 DemuxerStream::Status status,
188 const scoped_refptr<DecoderBuffer>& buffer) {
189 DCHECK(message_loop_->BelongsToCurrentThread());
190 DCHECK_NE(state_, kDecodeFinished);
191 DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status;
192
193 if (state_ == kUninitialized)
194 return;
195
196 DCHECK(!read_cb_.is_null());
197
198 if (!reset_cb_.is_null()) {
199 base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
200 DoReset();
201 return;
202 }
203
204 if (status == DemuxerStream::kAborted) {
205 base::ResetAndReturn(&read_cb_).Run(kOk, NULL);
206 return;
207 }
208
209 if (status == DemuxerStream::kConfigChanged) {
210 if (!ConfigureDecoder()) {
211 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
212 return;
213 }
214
215 ReadFromDemuxerStream();
216 return;
217 }
218
219 DCHECK_EQ(status, DemuxerStream::kOk);
220 DecodeBuffer(buffer);
221 }
222
223 void VpxVideoDecoder::DecodeBuffer(
224 const scoped_refptr<DecoderBuffer>& buffer) {
225 DCHECK(message_loop_->BelongsToCurrentThread());
226 DCHECK_NE(state_, kUninitialized);
227 DCHECK_NE(state_, kDecodeFinished);
228 DCHECK(reset_cb_.is_null());
229 DCHECK(!read_cb_.is_null());
230 DCHECK(buffer);
231
232 // Transition to kDecodeFinished on the first end of stream buffer.
233 if (state_ == kNormal && buffer->IsEndOfStream()) {
234 state_ = kDecodeFinished;
235 base::ResetAndReturn(&read_cb_).Run(kOk, VideoFrame::CreateEmptyFrame());
236 return;
237 }
238
239 scoped_refptr<VideoFrame> video_frame;
240 if (!Decode(buffer, &video_frame)) {
241 state_ = kDecodeFinished;
242 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL);
243 return;
244 }
245
246 // Any successful decode counts!
247 if (buffer->GetDataSize()) {
248 PipelineStatistics statistics;
249 statistics.video_bytes_decoded = buffer->GetDataSize();
250 statistics_cb_.Run(statistics);
251 }
252
253 // If we didn't get a frame we need more data.
254 if (!video_frame) {
255 ReadFromDemuxerStream();
256 return;
257 }
258
259 base::ResetAndReturn(&read_cb_).Run(kOk, video_frame);
260 }
261
262 bool VpxVideoDecoder::Decode(
263 const scoped_refptr<DecoderBuffer>& buffer,
264 scoped_refptr<VideoFrame>* video_frame) {
265 DCHECK(video_frame);
266
267 // Pass |buffer| to libvpx.
268 int64 timestamp = buffer->GetTimestamp().InMicroseconds();
269 void* user_priv = reinterpret_cast<void*>(&timestamp);
270 vpx_codec_err_t status = vpx_codec_decode(vpx_codec_,
271 buffer->GetData(),
272 buffer->GetDataSize(),
273 user_priv,
274 0);
275 if (status != VPX_CODEC_OK) {
276 LOG(ERROR) << "vpx_codec_decode() failed, status=" << status;
277 return false;
278 }
279
280 // Gets pointer to decoded data.
281 vpx_codec_iter_t iter = NULL;
282 const vpx_image_t* vpx_image = vpx_codec_get_frame(vpx_codec_, &iter);
283 if (!vpx_image) {
284 *video_frame = NULL;
285 return true;
286 }
287
288 if (vpx_image->user_priv != reinterpret_cast<void*>(&timestamp)) {
289 LOG(ERROR) << "Invalid output timestamp.";
290 return false;
291 }
292
293 CopyVpxImageTo(vpx_image, video_frame);
294 (*video_frame)->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp));
295 return true;
296 }
297
298 void VpxVideoDecoder::DoReset() {
299 DCHECK(read_cb_.is_null());
300
301 state_ = kNormal;
302 reset_cb_.Run();
303 reset_cb_.Reset();
304 }
305
306 void VpxVideoDecoder::CopyVpxImageTo(
307 const vpx_image* vpx_image,
308 scoped_refptr<VideoFrame>* video_frame) {
309 CHECK(vpx_image);
310 CHECK_EQ(vpx_image->d_w % 2, 0U);
311 CHECK_EQ(vpx_image->d_h % 2, 0U);
312 CHECK(vpx_image->fmt == VPX_IMG_FMT_I420 ||
313 vpx_image->fmt == VPX_IMG_FMT_YV12);
314
315 gfx::Size size(vpx_image->d_w, vpx_image->d_h);
316 gfx::Size natural_size =
317 demuxer_stream_->video_decoder_config().natural_size();
318
319 *video_frame = VideoFrame::CreateFrame(VideoFrame::YV12,
320 size,
321 gfx::Rect(size),
322 natural_size,
323 kNoTimestamp());
324 CopyYPlane(vpx_image->planes[VPX_PLANE_Y],
325 vpx_image->stride[VPX_PLANE_Y],
326 vpx_image->d_h,
327 *video_frame);
328 CopyUPlane(vpx_image->planes[VPX_PLANE_U],
329 vpx_image->stride[VPX_PLANE_U],
330 vpx_image->d_h / 2,
331 *video_frame);
332 CopyVPlane(vpx_image->planes[VPX_PLANE_V],
333 vpx_image->stride[VPX_PLANE_V],
334 vpx_image->d_h / 2,
335 *video_frame);
336 }
337
338 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698