Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / filters / ffmpeg_video_decoder.h
blob8acc4c8e02d4f5afd1c07477cab41b67a10253c1
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 #ifndef MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
8 #include <list>
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "media/base/video_decoder.h"
13 #include "media/base/video_decoder_config.h"
14 #include "media/base/video_frame_pool.h"
15 #include "media/ffmpeg/ffmpeg_deleters.h"
17 struct AVCodecContext;
18 struct AVFrame;
20 namespace base {
21 class SingleThreadTaskRunner;
24 namespace media {
26 class DecoderBuffer;
28 class MEDIA_EXPORT FFmpegVideoDecoder : public VideoDecoder {
29 public:
30 explicit FFmpegVideoDecoder(
31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
32 ~FFmpegVideoDecoder() override;
34 // Allow decoding of individual NALU. Entire frames are required by default.
35 // Disables low-latency mode. Must be called before Initialize().
36 void set_decode_nalus(bool decode_nalus) { decode_nalus_ = decode_nalus; }
38 // VideoDecoder implementation.
39 std::string GetDisplayName() const override;
40 void Initialize(const VideoDecoderConfig& config,
41 bool low_delay,
42 const PipelineStatusCB& status_cb,
43 const OutputCB& output_cb) override;
44 void Decode(const scoped_refptr<DecoderBuffer>& buffer,
45 const DecodeCB& decode_cb) override;
46 void Reset(const base::Closure& closure) override;
48 // Callback called from within FFmpeg to allocate a buffer based on
49 // the dimensions of |codec_context|. See AVCodecContext.get_buffer2
50 // documentation inside FFmpeg.
51 int GetVideoBuffer(struct AVCodecContext* codec_context,
52 AVFrame* frame,
53 int flags);
55 private:
56 enum DecoderState {
57 kUninitialized,
58 kNormal,
59 kDecodeFinished,
60 kError
63 // Handles decoding an unencrypted encoded buffer.
64 bool FFmpegDecode(const scoped_refptr<DecoderBuffer>& buffer,
65 bool* has_produced_frame);
67 // Handles (re-)initializing the decoder with a (new) config.
68 // Returns true if initialization was successful.
69 bool ConfigureDecoder(bool low_delay);
71 // Releases resources associated with |codec_context_| and |av_frame_|
72 // and resets them to NULL.
73 void ReleaseFFmpegResources();
75 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
77 DecoderState state_;
79 OutputCB output_cb_;
81 // FFmpeg structures owned by this object.
82 scoped_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
83 scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame_;
85 VideoDecoderConfig config_;
87 VideoFramePool frame_pool_;
89 bool decode_nalus_;
91 DISALLOW_COPY_AND_ASSIGN(FFmpegVideoDecoder);
94 } // namespace media
96 #endif // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_