summaryrefslogtreecommitdiffstats
path: root/media/video/ffmpeg_video_allocator.h
blob: d70f019a466040833f193f154acec4181441a18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_VIDEO_FFMPEG_VIDEO_ALLOCATOR_H_
#define MEDIA_VIDEO_FFMPEG_VIDEO_ALLOCATOR_H_

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/video_frame.h"
#include "media/ffmpeg/ffmpeg_common.h"

#include <deque>
#include <map>

// FFmpeg types.
struct AVCodecContext;
struct AVFrame;
struct AVStream;

namespace media {

class FFmpegVideoAllocator {
 public:
  FFmpegVideoAllocator();
  virtual ~FFmpegVideoAllocator();

  struct RefCountedAVFrame {
    explicit RefCountedAVFrame(AVFrame* av_frame)
        : av_frame_(*av_frame),
          usage_count_(0) {}

    // TODO(jiesun): we had commented out "DCHECK_EQ(usage_count_, 0);" here.
    // Because the way FFMPEG-MT handle release buffer in delayed fashion.
    // Probably we could wait FFMPEG-MT release all buffers before we callback
    // the flush completion.
    ~RefCountedAVFrame() {}

    void AddRef() {
      base::AtomicRefCountIncN(&usage_count_, 1);
    }

    bool Release() {
      return base::AtomicRefCountDecN(&usage_count_, 1);
    }

    // Technically AVFrame should *always* be heap-allocated via
    // avcodec_alloc_frame() otherwise (while rare) we can run into nasty binary
    // mismatch incompatibility stuff if people swap binaries (which might
    // happen with some Linux distributions). See http://crbug.com/77629.
    AVFrame av_frame_;
    base::AtomicRefCount usage_count_;
  };

  static int AllocateBuffer(AVCodecContext* codec_context, AVFrame* av_frame);
  static void ReleaseBuffer(AVCodecContext* codec_context, AVFrame* av_frame);

  void Initialize(AVCodecContext* codec_context,
                  VideoFrame::Format surface_format);
  void Stop(AVCodecContext* codec_context);

  // DisplayDone() is called when renderer has finished using a frame.
  void DisplayDone(AVCodecContext* codec_context,
                   scoped_refptr<VideoFrame> video_frame);

  // DecodeDone() is called after avcodec_video_decode() finish so that we can
  // acquire a reference to the video frame before we hand it to the renderer.
  scoped_refptr<VideoFrame> DecodeDone(AVCodecContext* codec_context,
                                       AVFrame* av_frame);

 private:
  int InternalAllocateBuffer(AVCodecContext* codec_context, AVFrame* av_frame);
  void InternalReleaseBuffer(AVCodecContext* codec_context, AVFrame* av_frame);

  VideoFrame::Format surface_format_;

  // This queue keeps reference count for all VideoFrame allocated.
  std::deque<RefCountedAVFrame*> frame_pool_;

  // This queue keeps per-AVCodecContext VideoFrame allocation that
  // was available for recycling.
  static const int kMaxFFmpegThreads = 3;
  std::deque<RefCountedAVFrame*> available_frames_[kMaxFFmpegThreads];

  // This map is used to map from AVCodecContext* to index to
  // |available_frames_|, because ffmpeg-mt maintain multiple
  // AVCodecContext (per thread).
  std::map<AVCodecContext*, int> codec_index_map_;

  // These function pointers store the original AVCodecContext's
  // get_buffer()/release_buffer() function pointers. We use these functions
  // to delegate the allocation request.
  int (*get_buffer_)(AVCodecContext* c, AVFrame* pic);
  void (*release_buffer_)(AVCodecContext* c, AVFrame* pic);

  DISALLOW_COPY_AND_ASSIGN(FFmpegVideoAllocator);
};

}  // namespace media

#endif  // MEDIA_VIDEO_FFMPEG_VIDEO_ALLOCATOR_H_