vpx_video_decoder.h revision 116680a4aac90f2aa7413d9095a592090648e557
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_VPX_VIDEO_DECODER_H_
6#define MEDIA_FILTERS_VPX_VIDEO_DECODER_H_
7
8#include "base/callback.h"
9#include "media/base/demuxer_stream.h"
10#include "media/base/video_decoder.h"
11#include "media/base/video_decoder_config.h"
12#include "media/base/video_frame.h"
13#include "media/base/video_frame_pool.h"
14
15struct vpx_codec_ctx;
16struct vpx_image;
17
18namespace base {
19class SingleThreadTaskRunner;
20}
21
22namespace media {
23
24// Libvpx video decoder wrapper.
25// Note: VpxVideoDecoder accepts only YV12A VP8 content or VP9 content. This is
26// done to avoid usurping FFmpeg for all vp8 decoding, because the FFmpeg VP8
27// decoder is faster than the libvpx VP8 decoder.
28class MEDIA_EXPORT VpxVideoDecoder : public VideoDecoder {
29 public:
30  explicit VpxVideoDecoder(
31      const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
32  virtual ~VpxVideoDecoder();
33
34  // VideoDecoder implementation.
35  virtual void Initialize(const VideoDecoderConfig& config,
36                          bool low_delay,
37                          const PipelineStatusCB& status_cb,
38                          const OutputCB& output_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
43 private:
44  enum DecoderState {
45    kUninitialized,
46    kNormal,
47    kFlushCodec,
48    kDecodeFinished,
49    kError
50  };
51
52  // Handles (re-)initializing the decoder with a (new) config.
53  // Returns true when initialization was successful.
54  bool ConfigureDecoder(const VideoDecoderConfig& config);
55
56  void CloseDecoder();
57
58  void DecodeBuffer(const scoped_refptr<DecoderBuffer>& buffer);
59  bool VpxDecode(const scoped_refptr<DecoderBuffer>& buffer,
60                 scoped_refptr<VideoFrame>* video_frame);
61
62  void CopyVpxImageTo(const vpx_image* vpx_image,
63                      const struct vpx_image* vpx_image_alpha,
64                      scoped_refptr<VideoFrame>* video_frame);
65
66  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
67
68  DecoderState state_;
69
70  OutputCB output_cb_;
71
72  // TODO(xhwang): Merge DecodeBuffer() into Decode() and remove this.
73  DecodeCB decode_cb_;
74
75  VideoDecoderConfig config_;
76
77  vpx_codec_ctx* vpx_codec_;
78  vpx_codec_ctx* vpx_codec_alpha_;
79
80  // Memory pool used for VP9 decoding.
81  class MemoryPool;
82  scoped_refptr<MemoryPool> memory_pool_;
83
84  VideoFramePool frame_pool_;
85
86  DISALLOW_COPY_AND_ASSIGN(VpxVideoDecoder);
87};
88
89}  // namespace media
90
91#endif  // MEDIA_FILTERS_VPX_VIDEO_DECODER_H_
92