OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/ffmpeg_video_decoder.h" | 5 #include "media/filters/ffmpeg_video_decoder.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 DCHECK(!output_cb.is_null()); | 233 DCHECK(!output_cb.is_null()); |
234 | 234 |
235 InitCB bound_init_cb = BindToCurrentLoop(init_cb); | 235 InitCB bound_init_cb = BindToCurrentLoop(init_cb); |
236 | 236 |
237 if (config.is_encrypted()) { | 237 if (config.is_encrypted()) { |
238 bound_init_cb.Run(false); | 238 bound_init_cb.Run(false); |
239 return; | 239 return; |
240 } | 240 } |
241 | 241 |
242 FFmpegGlue::InitializeFFmpeg(); | 242 FFmpegGlue::InitializeFFmpeg(); |
243 config_ = config; | |
244 | 243 |
245 // TODO(xhwang): Only set |config_| after we successfully configure the | 244 if (!ConfigureDecoder(config, low_delay)) { |
246 // decoder. | |
247 if (!ConfigureDecoder(low_delay)) { | |
248 bound_init_cb.Run(false); | 245 bound_init_cb.Run(false); |
249 return; | 246 return; |
250 } | 247 } |
251 | 248 |
| 249 // Success! |
| 250 config_ = config; |
252 output_cb_ = output_cb; | 251 output_cb_ = output_cb; |
253 | |
254 // Success! | |
255 state_ = kNormal; | 252 state_ = kNormal; |
256 bound_init_cb.Run(true); | 253 bound_init_cb.Run(true); |
257 } | 254 } |
258 | 255 |
259 void FFmpegVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 256 void FFmpegVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
260 const DecodeCB& decode_cb) { | 257 const DecodeCB& decode_cb) { |
261 DCHECK(thread_checker_.CalledOnValidThread()); | 258 DCHECK(thread_checker_.CalledOnValidThread()); |
262 DCHECK(buffer.get()); | 259 DCHECK(buffer.get()); |
263 DCHECK(!decode_cb.is_null()); | 260 DCHECK(!decode_cb.is_null()); |
264 CHECK_NE(state_, kUninitialized); | 261 CHECK_NE(state_, kUninitialized); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 | 397 |
401 av_frame_unref(av_frame_.get()); | 398 av_frame_unref(av_frame_.get()); |
402 return true; | 399 return true; |
403 } | 400 } |
404 | 401 |
405 void FFmpegVideoDecoder::ReleaseFFmpegResources() { | 402 void FFmpegVideoDecoder::ReleaseFFmpegResources() { |
406 codec_context_.reset(); | 403 codec_context_.reset(); |
407 av_frame_.reset(); | 404 av_frame_.reset(); |
408 } | 405 } |
409 | 406 |
410 bool FFmpegVideoDecoder::ConfigureDecoder(bool low_delay) { | 407 bool FFmpegVideoDecoder::ConfigureDecoder(const VideoDecoderConfig& config, |
411 DCHECK(config_.IsValidConfig()); | 408 bool low_delay) { |
412 DCHECK(!config_.is_encrypted()); | 409 DCHECK(config.IsValidConfig()); |
| 410 DCHECK(!config.is_encrypted()); |
413 | 411 |
414 // Release existing decoder resources if necessary. | 412 // Release existing decoder resources if necessary. |
415 ReleaseFFmpegResources(); | 413 ReleaseFFmpegResources(); |
416 | 414 |
417 // Initialize AVCodecContext structure. | 415 // Initialize AVCodecContext structure. |
418 codec_context_.reset(avcodec_alloc_context3(NULL)); | 416 codec_context_.reset(avcodec_alloc_context3(NULL)); |
419 VideoDecoderConfigToAVCodecContext(config_, codec_context_.get()); | 417 VideoDecoderConfigToAVCodecContext(config, codec_context_.get()); |
420 | 418 |
421 codec_context_->thread_count = GetThreadCount(config_); | 419 codec_context_->thread_count = GetThreadCount(config); |
422 codec_context_->thread_type = | 420 codec_context_->thread_type = |
423 FF_THREAD_SLICE | (low_delay ? 0 : FF_THREAD_FRAME); | 421 FF_THREAD_SLICE | (low_delay ? 0 : FF_THREAD_FRAME); |
424 codec_context_->opaque = this; | 422 codec_context_->opaque = this; |
425 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; | 423 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; |
426 codec_context_->get_buffer2 = GetVideoBufferImpl; | 424 codec_context_->get_buffer2 = GetVideoBufferImpl; |
427 codec_context_->refcounted_frames = 1; | 425 codec_context_->refcounted_frames = 1; |
428 | 426 |
429 if (decode_nalus_) | 427 if (decode_nalus_) |
430 codec_context_->flags2 |= CODEC_FLAG2_CHUNKS; | 428 codec_context_->flags2 |= CODEC_FLAG2_CHUNKS; |
431 | 429 |
432 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 430 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
433 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { | 431 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { |
434 ReleaseFFmpegResources(); | 432 ReleaseFFmpegResources(); |
435 return false; | 433 return false; |
436 } | 434 } |
437 | 435 |
438 av_frame_.reset(av_frame_alloc()); | 436 av_frame_.reset(av_frame_alloc()); |
439 return true; | 437 return true; |
440 } | 438 } |
441 | 439 |
442 } // namespace media | 440 } // namespace media |
OLD | NEW |