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
;
22 class MessageLoopProxy
;
29 class GpuVideoAcceleratorFactories
;
32 // GPU-accelerated video decoder implementation. Relies on
33 // AcceleratedVideoDecoderMsg_Decode and friends.
34 class MEDIA_EXPORT GpuVideoDecoder
35 : public VideoDecoder
,
36 public VideoDecodeAccelerator::Client
{
38 // The message loop of |factories| will be saved to |gvd_loop_proxy_|.
39 explicit GpuVideoDecoder(
40 const scoped_refptr
<GpuVideoAcceleratorFactories
>& factories
,
41 const scoped_refptr
<MediaLog
>& media_log
);
43 // VideoDecoder implementation.
44 virtual void Initialize(const VideoDecoderConfig
& config
,
45 const PipelineStatusCB
& status_cb
) OVERRIDE
;
46 virtual void Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
47 const DecodeCB
& decode_cb
) OVERRIDE
;
48 virtual void Reset(const base::Closure
& closure
) OVERRIDE
;
49 virtual void Stop(const base::Closure
& closure
) OVERRIDE
;
50 virtual bool HasAlpha() const OVERRIDE
;
51 virtual bool NeedsBitstreamConversion() const OVERRIDE
;
52 virtual bool CanReadWithoutStalling() const OVERRIDE
;
54 // VideoDecodeAccelerator::Client implementation.
55 virtual void NotifyInitializeDone() OVERRIDE
;
56 virtual void ProvidePictureBuffers(uint32 count
,
57 const gfx::Size
& size
,
58 uint32 texture_target
) OVERRIDE
;
59 virtual void DismissPictureBuffer(int32 id
) OVERRIDE
;
60 virtual void PictureReady(const media::Picture
& picture
) OVERRIDE
;
61 virtual void NotifyEndOfBitstreamBuffer(int32 id
) OVERRIDE
;
62 virtual void NotifyFlushDone() OVERRIDE
;
63 virtual void NotifyResetDone() OVERRIDE
;
64 virtual void NotifyError(media::VideoDecodeAccelerator::Error error
) OVERRIDE
;
67 virtual ~GpuVideoDecoder();
77 // Return true if more decode work can be piled on to the VDA.
78 bool CanMoreDecodeWorkBeDone();
80 // Enqueue a frame for later delivery (or drop it on the floor if a
81 // vda->Reset() is in progress) and trigger out-of-line delivery of the oldest
82 // ready frame to the client if there is a pending read. A NULL |frame|
83 // merely triggers delivery, and requires the ready_video_frames_ queue not be
85 void EnqueueFrameAndTriggerFrameDelivery(
86 const scoped_refptr
<VideoFrame
>& frame
);
88 // Indicate the picture buffer can be reused by the decoder.
89 void ReusePictureBuffer(int64 picture_buffer_id
, uint32 sync_point
);
91 void RecordBufferData(
92 const BitstreamBuffer
& bitstream_buffer
, const DecoderBuffer
& buffer
);
93 void GetBufferData(int32 id
, base::TimeDelta
* timetamp
,
94 gfx::Rect
* visible_rect
, gfx::Size
* natural_size
);
98 // A shared memory segment and its allocated size.
100 SHMBuffer(base::SharedMemory
* m
, size_t s
);
102 base::SharedMemory
* shm
;
106 // Request a shared-memory segment of at least |min_size| bytes. Will
107 // allocate as necessary. Caller does not own returned pointer.
108 SHMBuffer
* GetSHM(size_t min_size
);
110 // Return a shared-memory segment to the available pool.
111 void PutSHM(SHMBuffer
* shm_buffer
);
113 void DestroyTextures();
115 bool needs_bitstream_conversion_
;
117 // Message loop which this class and |factories_| run on.
118 scoped_refptr
<base::MessageLoopProxy
> gvd_loop_proxy_
;
119 base::WeakPtrFactory
<GpuVideoDecoder
> weak_factory_
;
120 base::WeakPtr
<GpuVideoDecoder
> weak_this_
;
122 scoped_refptr
<GpuVideoAcceleratorFactories
> factories_
;
124 // Populated during Initialize() (on success) and unchanged until an error
126 scoped_ptr
<VideoDecodeAccelerator
> vda_
;
128 // Callbacks that are !is_null() only during their respective operation being
129 // asynchronously executed.
130 DecodeCB pending_decode_cb_
;
131 base::Closure pending_reset_cb_
;
135 VideoDecoderConfig config_
;
137 // Shared-memory buffer pool. Since allocating SHM segments requires a
138 // round-trip to the browser process, we keep allocation out of the
139 // steady-state of the decoder.
140 std::vector
<SHMBuffer
*> available_shm_segments_
;
142 scoped_refptr
<MediaLog
> media_log_
;
144 // Book-keeping variables.
146 BufferPair(SHMBuffer
* s
, const scoped_refptr
<DecoderBuffer
>& b
);
148 SHMBuffer
* shm_buffer
;
149 scoped_refptr
<DecoderBuffer
> buffer
;
151 std::map
<int32
, BufferPair
> bitstream_buffers_in_decoder_
;
152 std::map
<int32
, PictureBuffer
> assigned_picture_buffers_
;
153 std::map
<int32
, PictureBuffer
> dismissed_picture_buffers_
;
154 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
155 // as VideoFrames to be rendered via decode_cb_, and which will be returned
156 // to us via ReusePictureBuffer.
157 std::set
<int32
> picture_buffers_at_display_
;
159 // The texture target used for decoded pictures.
160 uint32 decoder_texture_target_
;
163 BufferData(int32 bbid
, base::TimeDelta ts
, const gfx::Rect
& visible_rect
,
164 const gfx::Size
& natural_size
);
166 int32 bitstream_buffer_id
;
167 base::TimeDelta timestamp
;
168 gfx::Rect visible_rect
;
169 gfx::Size natural_size
;
171 std::list
<BufferData
> input_buffer_data_
;
173 // picture_buffer_id and the frame wrapping the corresponding Picture, for
174 // frames that have been decoded but haven't been requested by a Decode() yet.
175 std::list
<scoped_refptr
<VideoFrame
> > ready_video_frames_
;
176 int32 next_picture_buffer_id_
;
177 int32 next_bitstream_buffer_id_
;
179 // Set during ProvidePictureBuffers(), used for checking and implementing
180 // HasAvailableOutputFrames().
181 int available_pictures_
;
183 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder
);
188 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_