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_
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
;
23 class SingleThreadTaskRunner
;
29 class GpuVideoAcceleratorFactories
;
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
36 class MEDIA_EXPORT GpuVideoDecoder
37 : public VideoDecoder
,
38 public VideoDecodeAccelerator::Client
{
40 explicit GpuVideoDecoder(
41 const scoped_refptr
<GpuVideoAcceleratorFactories
>& factories
);
43 // VideoDecoder implementation.
44 std::string
GetDisplayName() const override
;
45 void Initialize(const VideoDecoderConfig
& config
,
47 const PipelineStatusCB
& status_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
[];
70 ~GpuVideoDecoder() override
;
80 // A shared memory segment and its allocated size.
82 SHMBuffer(scoped_ptr
<base::SharedMemory
> m
, size_t s
);
84 scoped_ptr
<base::SharedMemory
> shm
;
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
;
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
,
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
);
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 // Assert the contract that this class is operated on the right thread.
131 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
133 bool needs_bitstream_conversion_
;
135 scoped_refptr
<GpuVideoAcceleratorFactories
> factories_
;
137 // Populated during Initialize() (on success) and unchanged until an error
139 scoped_ptr
<VideoDecodeAccelerator
> vda_
;
143 DecodeCB eos_decode_cb_
;
145 // Not null only during reset.
146 base::Closure pending_reset_cb_
;
150 VideoDecoderConfig config_
;
152 // Shared-memory buffer pool. Since allocating SHM segments requires a
153 // round-trip to the browser process, we keep allocation out of the
154 // steady-state of the decoder.
155 std::vector
<SHMBuffer
*> available_shm_segments_
;
157 std::map
<int32
, PendingDecoderBuffer
> bitstream_buffers_in_decoder_
;
158 PictureBufferMap assigned_picture_buffers_
;
159 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
160 // as VideoFrames to be rendered via decode_cb_, and which will be returned
161 // to us via ReusePictureBuffer.
162 typedef std::map
<int32
/* picture_buffer_id */, uint32
/* texture_id */>
163 PictureBufferTextureMap
;
164 PictureBufferTextureMap picture_buffers_at_display_
;
166 // The texture target used for decoded pictures.
167 uint32 decoder_texture_target_
;
170 BufferData(int32 bbid
, base::TimeDelta ts
, const gfx::Rect
& visible_rect
,
171 const gfx::Size
& natural_size
);
173 int32 bitstream_buffer_id
;
174 base::TimeDelta timestamp
;
175 gfx::Rect visible_rect
;
176 gfx::Size natural_size
;
178 std::list
<BufferData
> input_buffer_data_
;
180 // picture_buffer_id and the frame wrapping the corresponding Picture, for
181 // frames that have been decoded but haven't been requested by a Decode() yet.
182 int32 next_picture_buffer_id_
;
183 int32 next_bitstream_buffer_id_
;
185 // Set during ProvidePictureBuffers(), used for checking and implementing
186 // HasAvailableOutputFrames().
187 int available_pictures_
;
189 // Bound to factories_->GetMessageLoop().
190 // NOTE: Weak pointers must be invalidated before all other member variables.
191 base::WeakPtrFactory
<GpuVideoDecoder
> weak_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder
);
198 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_