| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/sys_info.h" |
| 19 #include "media/base/bind_to_current_loop.h" | 20 #include "media/base/bind_to_current_loop.h" |
| 20 #include "media/base/decoder_buffer.h" | 21 #include "media/base/decoder_buffer.h" |
| 21 #include "media/base/limits.h" | 22 #include "media/base/limits.h" |
| 22 #include "media/base/media_switches.h" | 23 #include "media/base/media_switches.h" |
| 23 #include "media/base/timestamp_constants.h" | 24 #include "media/base/timestamp_constants.h" |
| 24 #include "media/base/video_frame.h" | 25 #include "media/base/video_frame.h" |
| 25 #include "media/base/video_util.h" | 26 #include "media/base/video_util.h" |
| 26 #include "media/ffmpeg/ffmpeg_common.h" | 27 #include "media/ffmpeg/ffmpeg_common.h" |
| 27 #include "media/filters/ffmpeg_glue.h" | 28 #include "media/filters/ffmpeg_glue.h" |
| 28 | 29 |
| 29 namespace media { | 30 namespace media { |
| 30 | 31 |
| 31 // Always try to use three threads for video decoding. There is little reason | 32 // Always use 2 or more threads for video decoding. Most machines today will |
| 32 // not to since current day CPUs tend to be multi-core and we measured | 33 // have 2-8 execution contexts. Using more cores generally doesn't seem to |
| 33 // performance benefits on older machines such as P4s with hyperthreading. | 34 // increase power usage and allows us to decode video faster. |
| 34 // | 35 // |
| 35 // Handling decoding on separate threads also frees up the pipeline thread to | 36 // Handling decoding on separate threads also frees up the pipeline thread to |
| 36 // continue processing. Although it'd be nice to have the option of a single | 37 // continue processing. Although it'd be nice to have the option of a single |
| 37 // decoding thread, FFmpeg treats having one thread the same as having zero | 38 // decoding thread, FFmpeg treats having one thread the same as having zero |
| 38 // threads (i.e., avcodec_decode_video() will execute on the calling thread). | 39 // threads (i.e., avcodec_decode_video() will execute on the calling thread). |
| 39 // Yet another reason for having two threads :) | 40 // Yet another reason for having two threads :) |
| 40 static const int kDecodeThreads = 2; | 41 static const int kDecodeThreads = 2; |
| 41 static const int kMaxDecodeThreads = 16; | 42 static const int kMaxDecodeThreads = 16; |
| 42 | 43 |
| 43 // Returns the number of threads given the FFmpeg CodecID. Also inspects the | 44 // Returns the number of threads given the FFmpeg CodecID. Also inspects the |
| 44 // command line for a valid --video-threads flag. | 45 // command line for a valid --video-threads flag. |
| 45 static int GetThreadCount(AVCodecID codec_id) { | 46 static int GetThreadCount(const VideoDecoderConfig& config) { |
| 46 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. | 47 // Refer to http://crbug.com/93932 for tsan suppressions on decoding. |
| 47 int decode_threads = kDecodeThreads; | 48 int decode_threads = kDecodeThreads; |
| 48 | 49 |
| 49 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 50 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 50 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); | 51 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads)); |
| 51 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) | 52 if (threads.empty() || !base::StringToInt(threads, &decode_threads)) { |
| 52 return decode_threads; | 53 // Normalize to three threads for 1080p content, then scale linearly |
| 54 // with number of pixels. |
| 55 // Examples: |
| 56 // 4k: 12 threads |
| 57 // 1440p: 5 threads |
| 58 // 1080p: 3 threads |
| 59 // anything lower than 1080p: 2 threads |
| 60 decode_threads = config.coded_size().width() * |
| 61 config.coded_size().height() * 3 / 1920 / 1080; |
| 62 |
| 63 int cores = base::SysInfo::NumberOfProcessors(); |
| 64 // Leave two execution contexts for other things to run. |
| 65 decode_threads = std::min(decode_threads, cores - 2); |
| 66 // Use at least two threads, or ffmpeg will decode on the calling thread. |
| 67 decode_threads = std::max(decode_threads, kDecodeThreads); |
| 68 } |
| 53 | 69 |
| 54 decode_threads = std::max(decode_threads, 0); | 70 decode_threads = std::max(decode_threads, 0); |
| 55 decode_threads = std::min(decode_threads, kMaxDecodeThreads); | 71 decode_threads = std::min(decode_threads, kMaxDecodeThreads); |
| 56 return decode_threads; | 72 return decode_threads; |
| 57 } | 73 } |
| 58 | 74 |
| 59 static int GetVideoBufferImpl(struct AVCodecContext* s, | 75 static int GetVideoBufferImpl(struct AVCodecContext* s, |
| 60 AVFrame* frame, | 76 AVFrame* frame, |
| 61 int flags) { | 77 int flags) { |
| 62 FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque); | 78 FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque); |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 DCHECK(config_.IsValidConfig()); | 384 DCHECK(config_.IsValidConfig()); |
| 369 DCHECK(!config_.is_encrypted()); | 385 DCHECK(!config_.is_encrypted()); |
| 370 | 386 |
| 371 // Release existing decoder resources if necessary. | 387 // Release existing decoder resources if necessary. |
| 372 ReleaseFFmpegResources(); | 388 ReleaseFFmpegResources(); |
| 373 | 389 |
| 374 // Initialize AVCodecContext structure. | 390 // Initialize AVCodecContext structure. |
| 375 codec_context_.reset(avcodec_alloc_context3(NULL)); | 391 codec_context_.reset(avcodec_alloc_context3(NULL)); |
| 376 VideoDecoderConfigToAVCodecContext(config_, codec_context_.get()); | 392 VideoDecoderConfigToAVCodecContext(config_, codec_context_.get()); |
| 377 | 393 |
| 378 codec_context_->thread_count = GetThreadCount(codec_context_->codec_id); | 394 codec_context_->thread_count = GetThreadCount(config_); |
| 379 codec_context_->thread_type = low_delay ? FF_THREAD_SLICE : FF_THREAD_FRAME; | 395 codec_context_->thread_type = |
| 396 FF_THREAD_SLICE | (low_delay ? 0 : FF_THREAD_FRAME); |
| 380 codec_context_->opaque = this; | 397 codec_context_->opaque = this; |
| 381 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; | 398 codec_context_->flags |= CODEC_FLAG_EMU_EDGE; |
| 382 codec_context_->get_buffer2 = GetVideoBufferImpl; | 399 codec_context_->get_buffer2 = GetVideoBufferImpl; |
| 383 codec_context_->refcounted_frames = 1; | 400 codec_context_->refcounted_frames = 1; |
| 384 | 401 |
| 385 if (decode_nalus_) | 402 if (decode_nalus_) |
| 386 codec_context_->flags2 |= CODEC_FLAG2_CHUNKS; | 403 codec_context_->flags2 |= CODEC_FLAG2_CHUNKS; |
| 387 | 404 |
| 388 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 405 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| 389 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { | 406 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { |
| 390 ReleaseFFmpegResources(); | 407 ReleaseFFmpegResources(); |
| 391 return false; | 408 return false; |
| 392 } | 409 } |
| 393 | 410 |
| 394 av_frame_.reset(av_frame_alloc()); | 411 av_frame_.reset(av_frame_alloc()); |
| 395 return true; | 412 return true; |
| 396 } | 413 } |
| 397 | 414 |
| 398 } // namespace media | 415 } // namespace media |
| OLD | NEW |