ffmpeg_video_decoder.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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#ifndef MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
6#define MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
7
8#include <list>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "media/base/video_decoder.h"
14#include "media/base/video_decoder_config.h"
15#include "media/base/video_frame_pool.h"
16
17struct AVCodecContext;
18struct AVFrame;
19
20namespace base {
21class MessageLoopProxy;
22}
23
24namespace media {
25
26class DecoderBuffer;
27class ScopedPtrAVFreeContext;
28class ScopedPtrAVFreeFrame;
29
30class MEDIA_EXPORT FFmpegVideoDecoder : public VideoDecoder {
31 public:
32  explicit FFmpegVideoDecoder(
33      const scoped_refptr<base::MessageLoopProxy>& message_loop);
34  virtual ~FFmpegVideoDecoder();
35
36  // VideoDecoder implementation.
37  virtual void Initialize(const VideoDecoderConfig& config,
38                          const PipelineStatusCB& status_cb) OVERRIDE;
39  virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
40                      const DecodeCB& decode_cb) OVERRIDE;
41  virtual void Reset(const base::Closure& closure) OVERRIDE;
42  virtual void Stop(const base::Closure& closure) OVERRIDE;
43
44  // Callback called from within FFmpeg to allocate a buffer based on
45  // the dimensions of |codec_context|. See AVCodecContext.get_buffer
46  // documentation inside FFmpeg.
47  int GetVideoBuffer(AVCodecContext *codec_context, AVFrame* frame);
48
49 private:
50  enum DecoderState {
51    kUninitialized,
52    kNormal,
53    kFlushCodec,
54    kDecodeFinished,
55    kError
56  };
57
58  // Handles decoding an unencrypted encoded buffer.
59  void DecodeBuffer(const scoped_refptr<DecoderBuffer>& buffer);
60  bool FFmpegDecode(const scoped_refptr<DecoderBuffer>& buffer,
61                    scoped_refptr<VideoFrame>* video_frame);
62
63  // Handles (re-)initializing the decoder with a (new) config.
64  // Returns true if initialization was successful.
65  bool ConfigureDecoder();
66
67  // Releases resources associated with |codec_context_| and |av_frame_|
68  // and resets them to NULL.
69  void ReleaseFFmpegResources();
70
71  // Reset decoder and call |reset_cb_|.
72  void DoReset();
73
74  scoped_refptr<base::MessageLoopProxy> message_loop_;
75  base::WeakPtrFactory<FFmpegVideoDecoder> weak_factory_;
76  base::WeakPtr<FFmpegVideoDecoder> weak_this_;
77
78  DecoderState state_;
79
80  DecodeCB decode_cb_;
81  base::Closure reset_cb_;
82
83  // FFmpeg structures owned by this object.
84  scoped_ptr_malloc<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
85  scoped_ptr_malloc<AVFrame, ScopedPtrAVFreeFrame> av_frame_;
86
87  VideoDecoderConfig config_;
88
89  VideoFramePool frame_pool_;
90
91  DISALLOW_COPY_AND_ASSIGN(FFmpegVideoDecoder);
92};
93
94}  // namespace media
95
96#endif  // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
97