Revert of Set defaultPageScaleLimits before setIgnoreViewportTagScaleLimits (patchset...
[chromium-blink-merge.git] / media / filters / ffmpeg_video_decoder.cc
bloba269afbfdf335a17e512e8a51c978cf57c38bb64
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.
5 #include "media/filters/ffmpeg_video_decoder.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/command_line.h"
13 #include "base/location.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/decoder_buffer.h"
18 #include "media/base/limits.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 #include "media/ffmpeg/ffmpeg_common.h"
25 #include "media/filters/ffmpeg_glue.h"
27 namespace media {
29 // Always try to use three threads for video decoding. There is little reason
30 // not to since current day CPUs tend to be multi-core and we measured
31 // performance benefits on older machines such as P4s with hyperthreading.
33 // Handling decoding on separate threads also frees up the pipeline thread to
34 // continue processing. Although it'd be nice to have the option of a single
35 // decoding thread, FFmpeg treats having one thread the same as having zero
36 // threads (i.e., avcodec_decode_video() will execute on the calling thread).
37 // Yet another reason for having two threads :)
38 static const int kDecodeThreads = 2;
39 static const int kMaxDecodeThreads = 16;
41 // Returns the number of threads given the FFmpeg CodecID. Also inspects the
42 // command line for a valid --video-threads flag.
43 static int GetThreadCount(AVCodecID codec_id) {
44 // Refer to http://crbug.com/93932 for tsan suppressions on decoding.
45 int decode_threads = kDecodeThreads;
47 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
48 std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads));
49 if (threads.empty() || !base::StringToInt(threads, &decode_threads))
50 return decode_threads;
52 decode_threads = std::max(decode_threads, 0);
53 decode_threads = std::min(decode_threads, kMaxDecodeThreads);
54 return decode_threads;
57 static int GetVideoBufferImpl(struct AVCodecContext* s,
58 AVFrame* frame,
59 int flags) {
60 FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque);
61 return decoder->GetVideoBuffer(s, frame, flags);
64 static void ReleaseVideoBufferImpl(void* opaque, uint8* data) {
65 scoped_refptr<VideoFrame> video_frame;
66 video_frame.swap(reinterpret_cast<VideoFrame**>(&opaque));
69 static size_t RoundUp(size_t value, size_t alignment) {
70 // Check that |alignment| is a power of 2.
71 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
72 return ((value + (alignment - 1)) & ~(alignment - 1));
75 FFmpegVideoDecoder::FFmpegVideoDecoder(
76 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
77 : task_runner_(task_runner), state_(kUninitialized),
78 decode_nalus_(false) {}
80 int FFmpegVideoDecoder::GetVideoBuffer(struct AVCodecContext* codec_context,
81 AVFrame* frame,
82 int flags) {
83 // Don't use |codec_context_| here! With threaded decoding,
84 // it will contain unsynchronized width/height/pix_fmt values,
85 // whereas |codec_context| contains the current threads's
86 // updated width/height/pix_fmt, which can change for adaptive
87 // content.
88 VideoFrame::Format format = PixelFormatToVideoFormat(codec_context->pix_fmt);
89 if (format == VideoFrame::YV12 &&
90 codec_context->colorspace == AVCOL_SPC_BT709) {
91 format = VideoFrame::YV12HD;
94 if (format == VideoFrame::UNKNOWN)
95 return AVERROR(EINVAL);
96 DCHECK(format == VideoFrame::YV12 || format == VideoFrame::YV16 ||
97 format == VideoFrame::YV12J || format == VideoFrame::YV24 ||
98 format == VideoFrame::YV12HD);
100 gfx::Size size(codec_context->width, codec_context->height);
101 const int ret = av_image_check_size(size.width(), size.height(), 0, NULL);
102 if (ret < 0)
103 return ret;
105 gfx::Size natural_size;
106 if (codec_context->sample_aspect_ratio.num > 0) {
107 natural_size = GetNaturalSize(size,
108 codec_context->sample_aspect_ratio.num,
109 codec_context->sample_aspect_ratio.den);
110 } else {
111 natural_size = config_.natural_size();
114 // FFmpeg has specific requirements on the allocation size of the frame. The
115 // following logic replicates FFmpeg's allocation strategy to ensure buffers
116 // are not overread / overwritten. See ff_init_buffer_info() for details.
118 // When lowres is non-zero, dimensions should be divided by 2^(lowres), but
119 // since we don't use this, just DCHECK that it's zero.
121 // Always round up to a multiple of two to match VideoFrame restrictions on
122 // frame alignment.
123 DCHECK_EQ(codec_context->lowres, 0);
124 gfx::Size coded_size(
125 RoundUp(std::max(size.width(), codec_context->coded_width), 2),
126 RoundUp(std::max(size.height(), codec_context->coded_height), 2));
128 if (!VideoFrame::IsValidConfig(
129 format, coded_size, gfx::Rect(size), natural_size))
130 return AVERROR(EINVAL);
132 scoped_refptr<VideoFrame> video_frame = frame_pool_.CreateFrame(
133 format, coded_size, gfx::Rect(size), natural_size, kNoTimestamp());
135 for (int i = 0; i < 3; i++) {
136 frame->data[i] = video_frame->data(i);
137 frame->linesize[i] = video_frame->stride(i);
140 frame->width = coded_size.width();
141 frame->height = coded_size.height();
142 frame->format = codec_context->pix_fmt;
143 frame->reordered_opaque = codec_context->reordered_opaque;
145 // Now create an AVBufferRef for the data just allocated. It will own the
146 // reference to the VideoFrame object.
147 void* opaque = NULL;
148 video_frame.swap(reinterpret_cast<VideoFrame**>(&opaque));
149 frame->buf[0] =
150 av_buffer_create(frame->data[0],
151 VideoFrame::AllocationSize(format, coded_size),
152 ReleaseVideoBufferImpl,
153 opaque,
155 return 0;
158 std::string FFmpegVideoDecoder::GetDisplayName() const {
159 return "FFmpegVideoDecoder";
162 void FFmpegVideoDecoder::Initialize(const VideoDecoderConfig& config,
163 bool low_delay,
164 const PipelineStatusCB& status_cb,
165 const OutputCB& output_cb) {
166 DCHECK(task_runner_->BelongsToCurrentThread());
167 DCHECK(!config.is_encrypted());
168 DCHECK(!output_cb.is_null());
170 FFmpegGlue::InitializeFFmpeg();
172 config_ = config;
173 PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb);
175 if (!config.IsValidConfig() || !ConfigureDecoder(low_delay)) {
176 initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
177 return;
180 output_cb_ = BindToCurrentLoop(output_cb);
182 // Success!
183 state_ = kNormal;
184 initialize_cb.Run(PIPELINE_OK);
187 void FFmpegVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
188 const DecodeCB& decode_cb) {
189 DCHECK(task_runner_->BelongsToCurrentThread());
190 DCHECK(buffer.get());
191 DCHECK(!decode_cb.is_null());
192 CHECK_NE(state_, kUninitialized);
194 DecodeCB decode_cb_bound = BindToCurrentLoop(decode_cb);
196 if (state_ == kError) {
197 decode_cb_bound.Run(kDecodeError);
198 return;
201 if (state_ == kDecodeFinished) {
202 decode_cb_bound.Run(kOk);
203 return;
206 DCHECK_EQ(state_, kNormal);
208 // During decode, because reads are issued asynchronously, it is possible to
209 // receive multiple end of stream buffers since each decode is acked. When the
210 // first end of stream buffer is read, FFmpeg may still have frames queued
211 // up in the decoder so we need to go through the decode loop until it stops
212 // giving sensible data. After that, the decoder should output empty
213 // frames. There are three states the decoder can be in:
215 // kNormal: This is the starting state. Buffers are decoded. Decode errors
216 // are discarded.
217 // kDecodeFinished: All calls return empty frames.
218 // kError: Unexpected error happened.
220 // These are the possible state transitions.
222 // kNormal -> kDecodeFinished:
223 // When EOS buffer is received and the codec has been flushed.
224 // kNormal -> kError:
225 // A decoding error occurs and decoding needs to stop.
226 // (any state) -> kNormal:
227 // Any time Reset() is called.
229 bool has_produced_frame;
230 do {
231 has_produced_frame = false;
232 if (!FFmpegDecode(buffer, &has_produced_frame)) {
233 state_ = kError;
234 decode_cb_bound.Run(kDecodeError);
235 return;
237 // Repeat to flush the decoder after receiving EOS buffer.
238 } while (buffer->end_of_stream() && has_produced_frame);
240 if (buffer->end_of_stream())
241 state_ = kDecodeFinished;
243 // VideoDecoderShim expects that |decode_cb| is called only after
244 // |output_cb_|.
245 decode_cb_bound.Run(kOk);
248 void FFmpegVideoDecoder::Reset(const base::Closure& closure) {
249 DCHECK(task_runner_->BelongsToCurrentThread());
251 avcodec_flush_buffers(codec_context_.get());
252 state_ = kNormal;
253 task_runner_->PostTask(FROM_HERE, closure);
256 FFmpegVideoDecoder::~FFmpegVideoDecoder() {
257 DCHECK(task_runner_->BelongsToCurrentThread());
259 if (state_ != kUninitialized)
260 ReleaseFFmpegResources();
263 bool FFmpegVideoDecoder::FFmpegDecode(
264 const scoped_refptr<DecoderBuffer>& buffer,
265 bool* has_produced_frame) {
266 DCHECK(!*has_produced_frame);
268 // Create a packet for input data.
269 // Due to FFmpeg API changes we no longer have const read-only pointers.
270 AVPacket packet;
271 av_init_packet(&packet);
272 if (buffer->end_of_stream()) {
273 packet.data = NULL;
274 packet.size = 0;
275 } else {
276 packet.data = const_cast<uint8*>(buffer->data());
277 packet.size = buffer->data_size();
279 // Let FFmpeg handle presentation timestamp reordering.
280 codec_context_->reordered_opaque = buffer->timestamp().InMicroseconds();
283 int frame_decoded = 0;
284 int result = avcodec_decode_video2(codec_context_.get(),
285 av_frame_.get(),
286 &frame_decoded,
287 &packet);
288 // Log the problem if we can't decode a video frame and exit early.
289 if (result < 0) {
290 LOG(ERROR) << "Error decoding video: " << buffer->AsHumanReadableString();
291 return false;
294 // FFmpeg says some codecs might have multiple frames per packet. Previous
295 // discussions with rbultje@ indicate this shouldn't be true for the codecs
296 // we use.
297 DCHECK_EQ(result, packet.size);
299 // If no frame was produced then signal that more data is required to
300 // produce more frames. This can happen under two circumstances:
301 // 1) Decoder was recently initialized/flushed
302 // 2) End of stream was reached and all internal frames have been output
303 if (frame_decoded == 0) {
304 return true;
307 // TODO(fbarchard): Work around for FFmpeg http://crbug.com/27675
308 // The decoder is in a bad state and not decoding correctly.
309 // Checking for NULL avoids a crash in CopyPlane().
310 if (!av_frame_->data[VideoFrame::kYPlane] ||
311 !av_frame_->data[VideoFrame::kUPlane] ||
312 !av_frame_->data[VideoFrame::kVPlane]) {
313 LOG(ERROR) << "Video frame was produced yet has invalid frame data.";
314 av_frame_unref(av_frame_.get());
315 return false;
318 scoped_refptr<VideoFrame> frame =
319 reinterpret_cast<VideoFrame*>(av_buffer_get_opaque(av_frame_->buf[0]));
320 frame->set_timestamp(
321 base::TimeDelta::FromMicroseconds(av_frame_->reordered_opaque));
322 *has_produced_frame = true;
323 output_cb_.Run(frame);
325 av_frame_unref(av_frame_.get());
326 return true;
329 void FFmpegVideoDecoder::ReleaseFFmpegResources() {
330 codec_context_.reset();
331 av_frame_.reset();
334 bool FFmpegVideoDecoder::ConfigureDecoder(bool low_delay) {
335 // Release existing decoder resources if necessary.
336 ReleaseFFmpegResources();
338 // Initialize AVCodecContext structure.
339 codec_context_.reset(avcodec_alloc_context3(NULL));
340 VideoDecoderConfigToAVCodecContext(config_, codec_context_.get());
342 codec_context_->thread_count = GetThreadCount(codec_context_->codec_id);
343 codec_context_->thread_type = low_delay ? FF_THREAD_SLICE : FF_THREAD_FRAME;
344 codec_context_->opaque = this;
345 codec_context_->flags |= CODEC_FLAG_EMU_EDGE;
346 codec_context_->get_buffer2 = GetVideoBufferImpl;
347 codec_context_->refcounted_frames = 1;
349 if (decode_nalus_)
350 codec_context_->flags2 |= CODEC_FLAG2_CHUNKS;
352 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
353 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) {
354 ReleaseFFmpegResources();
355 return false;
358 av_frame_.reset(av_frame_alloc());
359 return true;
362 } // namespace media