Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / filters / gpu_video_decoder.h
blob28eceb34f256340a927a460ab9a2a4858f61da6b
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_GPU_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_GPU_VIDEO_DECODER_H_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <utility>
12 #include <vector>
14 #include "base/memory/weak_ptr.h"
15 #include "media/base/pipeline_status.h"
16 #include "media/base/video_decoder.h"
17 #include "media/video/video_decode_accelerator.h"
19 template <class T> class scoped_refptr;
21 namespace base {
22 class SharedMemory;
23 class SingleThreadTaskRunner;
26 namespace media {
28 class DecoderBuffer;
29 class GpuVideoAcceleratorFactories;
30 class MediaLog;
32 // GPU-accelerated video decoder implementation. Relies on
33 // AcceleratedVideoDecoderMsg_Decode and friends. Can be created on any thread
34 // but must be accessed and destroyed on GpuVideoAcceleratorFactories's
35 // GetMessageLoop().
36 class MEDIA_EXPORT GpuVideoDecoder
37 : public VideoDecoder,
38 public VideoDecodeAccelerator::Client {
39 public:
40 explicit GpuVideoDecoder(
41 const scoped_refptr<GpuVideoAcceleratorFactories>& factories);
43 // VideoDecoder implementation.
44 std::string GetDisplayName() const override;
45 void Initialize(const VideoDecoderConfig& config,
46 bool low_delay,
47 const InitCB& init_cb,
48 const OutputCB& output_cb) override;
49 void Decode(const scoped_refptr<DecoderBuffer>& buffer,
50 const DecodeCB& decode_cb) override;
51 void Reset(const base::Closure& closure) override;
52 bool NeedsBitstreamConversion() const override;
53 bool CanReadWithoutStalling() const override;
54 int GetMaxDecodeRequests() const override;
56 // VideoDecodeAccelerator::Client implementation.
57 void ProvidePictureBuffers(uint32 count,
58 const gfx::Size& size,
59 uint32 texture_target) override;
60 void DismissPictureBuffer(int32 id) override;
61 void PictureReady(const media::Picture& picture) override;
62 void NotifyEndOfBitstreamBuffer(int32 id) override;
63 void NotifyFlushDone() override;
64 void NotifyResetDone() override;
65 void NotifyError(media::VideoDecodeAccelerator::Error error) override;
67 static const char kDecoderName[];
69 protected:
70 ~GpuVideoDecoder() override;
72 private:
73 enum State {
74 kNormal,
75 kDrainingDecoder,
76 kDecoderDrained,
77 kError
80 // A shared memory segment and its allocated size.
81 struct SHMBuffer {
82 SHMBuffer(scoped_ptr<base::SharedMemory> m, size_t s);
83 ~SHMBuffer();
84 scoped_ptr<base::SharedMemory> shm;
85 size_t size;
88 // A SHMBuffer and the DecoderBuffer its data came from.
89 struct PendingDecoderBuffer {
90 PendingDecoderBuffer(SHMBuffer* s,
91 const scoped_refptr<DecoderBuffer>& b,
92 const DecodeCB& done_cb);
93 ~PendingDecoderBuffer();
94 SHMBuffer* shm_buffer;
95 scoped_refptr<DecoderBuffer> buffer;
96 DecodeCB done_cb;
99 typedef std::map<int32, PictureBuffer> PictureBufferMap;
101 void DeliverFrame(const scoped_refptr<VideoFrame>& frame);
103 // Static method is to allow it to run even after GVD is deleted.
104 static void ReleaseMailbox(
105 base::WeakPtr<GpuVideoDecoder> decoder,
106 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
107 int64 picture_buffer_id,
108 uint32 texture_id,
109 uint32 release_sync_point);
110 // Indicate the picture buffer can be reused by the decoder.
111 void ReusePictureBuffer(int64 picture_buffer_id);
113 void RecordBufferData(
114 const BitstreamBuffer& bitstream_buffer, const DecoderBuffer& buffer);
115 void GetBufferData(int32 id, base::TimeDelta* timetamp,
116 gfx::Rect* visible_rect, gfx::Size* natural_size);
118 void DestroyVDA();
120 // Request a shared-memory segment of at least |min_size| bytes. Will
121 // allocate as necessary.
122 scoped_ptr<SHMBuffer> GetSHM(size_t min_size);
124 // Return a shared-memory segment to the available pool.
125 void PutSHM(scoped_ptr<SHMBuffer> shm_buffer);
127 // Destroy all PictureBuffers in |buffers|, and delete their textures.
128 void DestroyPictureBuffers(PictureBufferMap* buffers);
130 // Returns true if the video decoder can support |profile| and |coded_size|.
131 bool IsProfileSupported(VideoCodecProfile profile,
132 const gfx::Size& coded_size);
134 // Assert the contract that this class is operated on the right thread.
135 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
137 bool needs_bitstream_conversion_;
139 scoped_refptr<GpuVideoAcceleratorFactories> factories_;
141 // Populated during Initialize() (on success) and unchanged until an error
142 // occurs.
143 scoped_ptr<VideoDecodeAccelerator> vda_;
145 OutputCB output_cb_;
147 DecodeCB eos_decode_cb_;
149 // Not null only during reset.
150 base::Closure pending_reset_cb_;
152 State state_;
154 VideoDecoderConfig config_;
156 // Shared-memory buffer pool. Since allocating SHM segments requires a
157 // round-trip to the browser process, we keep allocation out of the
158 // steady-state of the decoder.
159 std::vector<SHMBuffer*> available_shm_segments_;
161 std::map<int32, PendingDecoderBuffer> bitstream_buffers_in_decoder_;
162 PictureBufferMap assigned_picture_buffers_;
163 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
164 // as VideoFrames to be rendered via decode_cb_, and which will be returned
165 // to us via ReusePictureBuffer.
166 typedef std::map<int32 /* picture_buffer_id */, uint32 /* texture_id */>
167 PictureBufferTextureMap;
168 PictureBufferTextureMap picture_buffers_at_display_;
170 // The texture target used for decoded pictures.
171 uint32 decoder_texture_target_;
173 struct BufferData {
174 BufferData(int32 bbid, base::TimeDelta ts, const gfx::Rect& visible_rect,
175 const gfx::Size& natural_size);
176 ~BufferData();
177 int32 bitstream_buffer_id;
178 base::TimeDelta timestamp;
179 gfx::Rect visible_rect;
180 gfx::Size natural_size;
182 std::list<BufferData> input_buffer_data_;
184 // picture_buffer_id and the frame wrapping the corresponding Picture, for
185 // frames that have been decoded but haven't been requested by a Decode() yet.
186 int32 next_picture_buffer_id_;
187 int32 next_bitstream_buffer_id_;
189 // Set during ProvidePictureBuffers(), used for checking and implementing
190 // HasAvailableOutputFrames().
191 int available_pictures_;
193 // Bound to factories_->GetMessageLoop().
194 // NOTE: Weak pointers must be invalidated before all other member variables.
195 base::WeakPtrFactory<GpuVideoDecoder> weak_factory_;
197 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder);
200 } // namespace media
202 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_