Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / filters / ffmpeg_video_decoder.cc
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/ffmpeg_video_decoder.h"
6
7 #include <algorithm>
8 #include <string>
9
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"
26
27 namespace media {
28
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.
32 //
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;
40
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;
46
47   const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
48   std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads));
49   if (threads.empty() || !base::StringToInt(threads, &decode_threads))
50     return decode_threads;
51
52   decode_threads = std::max(decode_threads, 0);
53   decode_threads = std::min(decode_threads, kMaxDecodeThreads);
54   return decode_threads;
55 }
56
57 FFmpegVideoDecoder::FFmpegVideoDecoder(
58     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
59     : task_runner_(task_runner), state_(kUninitialized) {}
60
61 int FFmpegVideoDecoder::GetVideoBuffer(AVCodecContext* codec_context,
62                                        AVFrame* frame) {
63   // Don't use |codec_context_| here! With threaded decoding,
64   // it will contain unsynchronized width/height/pix_fmt values,
65   // whereas |codec_context| contains the current threads's
66   // updated width/height/pix_fmt, which can change for adaptive
67   // content.
68   VideoFrame::Format format = PixelFormatToVideoFormat(codec_context->pix_fmt);
69   if (format == VideoFrame::UNKNOWN)
70     return AVERROR(EINVAL);
71   DCHECK(format == VideoFrame::YV12 || format == VideoFrame::YV16 ||
72          format == VideoFrame::YV12J);
73
74   gfx::Size size(codec_context->width, codec_context->height);
75   const int ret = av_image_check_size(size.width(), size.height(), 0, NULL);
76   if (ret < 0)
77     return ret;
78
79   gfx::Size natural_size;
80   if (codec_context->sample_aspect_ratio.num > 0) {
81     natural_size = GetNaturalSize(size,
82                                   codec_context->sample_aspect_ratio.num,
83                                   codec_context->sample_aspect_ratio.den);
84   } else {
85     natural_size = config_.natural_size();
86   }
87
88   // FFmpeg has specific requirements on the allocation size of the frame.  The
89   // following logic replicates FFmpeg's allocation strategy to ensure buffers
90   // are not overread / overwritten.  See ff_init_buffer_info() for details.
91   //
92   // When lowres is non-zero, dimensions should be divided by 2^(lowres), but
93   // since we don't use this, just DCHECK that it's zero.
94   DCHECK_EQ(codec_context->lowres, 0);
95   gfx::Size coded_size(std::max(size.width(), codec_context->coded_width),
96                        std::max(size.height(), codec_context->coded_height));
97
98   if (!VideoFrame::IsValidConfig(
99           format, coded_size, gfx::Rect(size), natural_size))
100     return AVERROR(EINVAL);
101
102   scoped_refptr<VideoFrame> video_frame = frame_pool_.CreateFrame(
103       format, coded_size, gfx::Rect(size), natural_size, kNoTimestamp());
104
105   for (int i = 0; i < 3; i++) {
106     frame->base[i] = video_frame->data(i);
107     frame->data[i] = video_frame->data(i);
108     frame->linesize[i] = video_frame->stride(i);
109   }
110
111   frame->opaque = NULL;
112   video_frame.swap(reinterpret_cast<VideoFrame**>(&frame->opaque));
113   frame->type = FF_BUFFER_TYPE_USER;
114   frame->width = coded_size.width();
115   frame->height = coded_size.height();
116   frame->format = codec_context->pix_fmt;
117
118   return 0;
119 }
120
121 static int GetVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
122   FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque);
123   return decoder->GetVideoBuffer(s, frame);
124 }
125
126 static void ReleaseVideoBufferImpl(AVCodecContext* s, AVFrame* frame) {
127   scoped_refptr<VideoFrame> video_frame;
128   video_frame.swap(reinterpret_cast<VideoFrame**>(&frame->opaque));
129
130   // The FFmpeg API expects us to zero the data pointers in
131   // this callback
132   memset(frame->data, 0, sizeof(frame->data));
133   frame->opaque = NULL;
134 }
135
136 void FFmpegVideoDecoder::Initialize(const VideoDecoderConfig& config,
137                                     bool low_delay,
138                                     const PipelineStatusCB& status_cb) {
139   DCHECK(task_runner_->BelongsToCurrentThread());
140   DCHECK(decode_cb_.is_null());
141   DCHECK(!config.is_encrypted());
142
143   FFmpegGlue::InitializeFFmpeg();
144
145   config_ = config;
146   PipelineStatusCB initialize_cb = BindToCurrentLoop(status_cb);
147
148   if (!config.IsValidConfig() || !ConfigureDecoder(low_delay)) {
149     initialize_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
150     return;
151   }
152
153   // Success!
154   state_ = kNormal;
155   initialize_cb.Run(PIPELINE_OK);
156 }
157
158 void FFmpegVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
159                                 const DecodeCB& decode_cb) {
160   DCHECK(task_runner_->BelongsToCurrentThread());
161   DCHECK(!decode_cb.is_null());
162   CHECK_NE(state_, kUninitialized);
163   CHECK(decode_cb_.is_null()) << "Overlapping decodes are not supported.";
164   decode_cb_ = BindToCurrentLoop(decode_cb);
165
166   if (state_ == kError) {
167     base::ResetAndReturn(&decode_cb_).Run(kDecodeError, NULL);
168     return;
169   }
170
171   // Return empty frames if decoding has finished.
172   if (state_ == kDecodeFinished) {
173     base::ResetAndReturn(&decode_cb_).Run(kOk, VideoFrame::CreateEOSFrame());
174     return;
175   }
176
177   DecodeBuffer(buffer);
178 }
179
180 void FFmpegVideoDecoder::Reset(const base::Closure& closure) {
181   DCHECK(task_runner_->BelongsToCurrentThread());
182   DCHECK(decode_cb_.is_null());
183
184   avcodec_flush_buffers(codec_context_.get());
185   state_ = kNormal;
186   task_runner_->PostTask(FROM_HERE, closure);
187 }
188
189 void FFmpegVideoDecoder::Stop() {
190   DCHECK(task_runner_->BelongsToCurrentThread());
191
192   if (state_ == kUninitialized)
193     return;
194
195   ReleaseFFmpegResources();
196   state_ = kUninitialized;
197 }
198
199 FFmpegVideoDecoder::~FFmpegVideoDecoder() {
200   DCHECK_EQ(kUninitialized, state_);
201   DCHECK(!codec_context_);
202   DCHECK(!av_frame_);
203 }
204
205 void FFmpegVideoDecoder::DecodeBuffer(
206     const scoped_refptr<DecoderBuffer>& buffer) {
207   DCHECK(task_runner_->BelongsToCurrentThread());
208   DCHECK_NE(state_, kUninitialized);
209   DCHECK_NE(state_, kDecodeFinished);
210   DCHECK_NE(state_, kError);
211   DCHECK(!decode_cb_.is_null());
212   DCHECK(buffer);
213
214   // During decode, because reads are issued asynchronously, it is possible to
215   // receive multiple end of stream buffers since each decode is acked. When the
216   // first end of stream buffer is read, FFmpeg may still have frames queued
217   // up in the decoder so we need to go through the decode loop until it stops
218   // giving sensible data.  After that, the decoder should output empty
219   // frames.  There are three states the decoder can be in:
220   //
221   //   kNormal: This is the starting state. Buffers are decoded. Decode errors
222   //            are discarded.
223   //   kFlushCodec: There isn't any more input data. Call avcodec_decode_video2
224   //                until no more data is returned to flush out remaining
225   //                frames. The input buffer is ignored at this point.
226   //   kDecodeFinished: All calls return empty frames.
227   //   kError: Unexpected error happened.
228   //
229   // These are the possible state transitions.
230   //
231   // kNormal -> kFlushCodec:
232   //     When buffer->end_of_stream() is first true.
233   // kNormal -> kError:
234   //     A decoding error occurs and decoding needs to stop.
235   // kFlushCodec -> kDecodeFinished:
236   //     When avcodec_decode_video2() returns 0 data.
237   // kFlushCodec -> kError:
238   //     When avcodec_decode_video2() errors out.
239   // (any state) -> kNormal:
240   //     Any time Reset() is called.
241
242   // Transition to kFlushCodec on the first end of stream buffer.
243   if (state_ == kNormal && buffer->end_of_stream()) {
244     state_ = kFlushCodec;
245   }
246
247   scoped_refptr<VideoFrame> video_frame;
248   if (!FFmpegDecode(buffer, &video_frame)) {
249     state_ = kError;
250     base::ResetAndReturn(&decode_cb_).Run(kDecodeError, NULL);
251     return;
252   }
253
254   if (!video_frame.get()) {
255     if (state_ == kFlushCodec) {
256       DCHECK(buffer->end_of_stream());
257       state_ = kDecodeFinished;
258       base::ResetAndReturn(&decode_cb_)
259           .Run(kOk, VideoFrame::CreateEOSFrame());
260       return;
261     }
262
263     base::ResetAndReturn(&decode_cb_).Run(kNotEnoughData, NULL);
264     return;
265   }
266
267   base::ResetAndReturn(&decode_cb_).Run(kOk, video_frame);
268 }
269
270 bool FFmpegVideoDecoder::FFmpegDecode(
271     const scoped_refptr<DecoderBuffer>& buffer,
272     scoped_refptr<VideoFrame>* video_frame) {
273   DCHECK(video_frame);
274
275   // Reset frame to default values.
276   avcodec_get_frame_defaults(av_frame_.get());
277
278   // Create a packet for input data.
279   // Due to FFmpeg API changes we no longer have const read-only pointers.
280   AVPacket packet;
281   av_init_packet(&packet);
282   if (buffer->end_of_stream()) {
283     packet.data = NULL;
284     packet.size = 0;
285   } else {
286     packet.data = const_cast<uint8*>(buffer->data());
287     packet.size = buffer->data_size();
288
289     // Let FFmpeg handle presentation timestamp reordering.
290     codec_context_->reordered_opaque = buffer->timestamp().InMicroseconds();
291
292     // This is for codecs not using get_buffer to initialize
293     // |av_frame_->reordered_opaque|
294     av_frame_->reordered_opaque = codec_context_->reordered_opaque;
295   }
296
297   int frame_decoded = 0;
298   int result = avcodec_decode_video2(codec_context_.get(),
299                                      av_frame_.get(),
300                                      &frame_decoded,
301                                      &packet);
302   // Log the problem if we can't decode a video frame and exit early.
303   if (result < 0) {
304     LOG(ERROR) << "Error decoding video: " << buffer->AsHumanReadableString();
305     *video_frame = NULL;
306     return false;
307   }
308
309   // If no frame was produced then signal that more data is required to
310   // produce more frames. This can happen under two circumstances:
311   //   1) Decoder was recently initialized/flushed
312   //   2) End of stream was reached and all internal frames have been output
313   if (frame_decoded == 0) {
314     *video_frame = NULL;
315     return true;
316   }
317
318   // TODO(fbarchard): Work around for FFmpeg http://crbug.com/27675
319   // The decoder is in a bad state and not decoding correctly.
320   // Checking for NULL avoids a crash in CopyPlane().
321   if (!av_frame_->data[VideoFrame::kYPlane] ||
322       !av_frame_->data[VideoFrame::kUPlane] ||
323       !av_frame_->data[VideoFrame::kVPlane]) {
324     LOG(ERROR) << "Video frame was produced yet has invalid frame data.";
325     *video_frame = NULL;
326     return false;
327   }
328
329   if (!av_frame_->opaque) {
330     LOG(ERROR) << "VideoFrame object associated with frame data not set.";
331     return false;
332   }
333   *video_frame = static_cast<VideoFrame*>(av_frame_->opaque);
334
335   (*video_frame)->set_timestamp(
336       base::TimeDelta::FromMicroseconds(av_frame_->reordered_opaque));
337
338   return true;
339 }
340
341 void FFmpegVideoDecoder::ReleaseFFmpegResources() {
342   codec_context_.reset();
343   av_frame_.reset();
344 }
345
346 bool FFmpegVideoDecoder::ConfigureDecoder(bool low_delay) {
347   // Release existing decoder resources if necessary.
348   ReleaseFFmpegResources();
349
350   // Initialize AVCodecContext structure.
351   codec_context_.reset(avcodec_alloc_context3(NULL));
352   VideoDecoderConfigToAVCodecContext(config_, codec_context_.get());
353
354   // Enable motion vector search (potentially slow), strong deblocking filter
355   // for damaged macroblocks, and set our error detection sensitivity.
356   codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
357   codec_context_->thread_count = GetThreadCount(codec_context_->codec_id);
358   codec_context_->thread_type = low_delay ? FF_THREAD_SLICE : FF_THREAD_FRAME;
359   codec_context_->opaque = this;
360   codec_context_->flags |= CODEC_FLAG_EMU_EDGE;
361   codec_context_->get_buffer = GetVideoBufferImpl;
362   codec_context_->release_buffer = ReleaseVideoBufferImpl;
363
364   AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
365   if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) {
366     ReleaseFFmpegResources();
367     return false;
368   }
369
370   av_frame_.reset(av_frame_alloc());
371   return true;
372 }
373
374 }  // namespace media