WebKit roll 109146:109201
[chromium-blink-merge.git] / media / filters / gpu_video_decoder.h
blobf17a4ccd7499d9debfcbe91fd9611ed2b158d0c4
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 <deque>
9 #include <list>
10 #include <map>
12 #include "media/base/filters.h"
13 #include "media/base/pipeline_status.h"
14 #include "media/video/video_decode_accelerator.h"
15 #include "ui/gfx/size.h"
17 class MessageLoop;
18 template <class T> class scoped_refptr;
19 namespace base {
20 class MessageLoopProxy;
21 class SharedMemory;
24 namespace media {
26 // GPU-accelerated video decoder implementation. Relies on
27 // AcceleratedVideoDecoderMsg_Decode and friends.
28 // All methods internally trampoline to the |message_loop| passed to the ctor.
29 class MEDIA_EXPORT GpuVideoDecoder
30 : public VideoDecoder,
31 public VideoDecodeAccelerator::Client {
32 public:
33 // Helper interface for specifying factories needed to instantiate a
34 // GpuVideoDecoder.
35 class MEDIA_EXPORT Factories : public base::RefCountedThreadSafe<Factories> {
36 public:
37 // Caller owns returned pointer.
38 virtual VideoDecodeAccelerator* CreateVideoDecodeAccelerator(
39 VideoDecodeAccelerator::Profile, VideoDecodeAccelerator::Client*) = 0;
41 // Allocate & delete native textures.
42 virtual bool CreateTextures(int32 count, const gfx::Size& size,
43 std::vector<uint32>* texture_ids,
44 uint32* texture_target) = 0;
45 virtual void DeleteTexture(uint32 texture_id) = 0;
47 // Allocate & return a shared memory segment. Caller is responsible for
48 // Close()ing the returned pointer.
49 virtual base::SharedMemory* CreateSharedMemory(size_t size) = 0;
51 protected:
52 friend class base::RefCountedThreadSafe<Factories>;
53 virtual ~Factories();
56 GpuVideoDecoder(MessageLoop* message_loop,
57 const scoped_refptr<Factories>& factories);
58 virtual ~GpuVideoDecoder();
60 // Filter implementation.
61 virtual void Stop(const base::Closure& callback) OVERRIDE;
62 virtual void Seek(base::TimeDelta time, const FilterStatusCB& cb) OVERRIDE;
63 virtual void Pause(const base::Closure& callback) OVERRIDE;
64 virtual void Flush(const base::Closure& callback) OVERRIDE;
66 // VideoDecoder implementation.
67 virtual void Initialize(DemuxerStream* demuxer_stream,
68 const PipelineStatusCB& callback,
69 const StatisticsCallback& stats_callback) OVERRIDE;
70 virtual void Read(const ReadCB& callback) OVERRIDE;
71 virtual const gfx::Size& natural_size() OVERRIDE;
72 virtual bool HasAlpha() const OVERRIDE;
73 virtual void PrepareForShutdownHack() OVERRIDE;
75 // VideoDecodeAccelerator::Client implementation.
76 virtual void NotifyInitializeDone() OVERRIDE;
77 virtual void ProvidePictureBuffers(uint32 count,
78 const gfx::Size& size) OVERRIDE;
79 virtual void DismissPictureBuffer(int32 id) OVERRIDE;
80 virtual void PictureReady(const media::Picture& picture) OVERRIDE;
81 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE;
82 virtual void NotifyFlushDone() OVERRIDE;
83 virtual void NotifyResetDone() OVERRIDE;
84 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE;
86 private:
87 enum State {
88 kNormal,
89 // Avoid the use of "flush" in these enums because the term is overloaded:
90 // Filter::Flush() means drop pending data on the floor, but
91 // VideoDecodeAccelerator::Flush() means drain pending data (Filter::Flush()
92 // actually corresponds to VideoDecodeAccelerator::Reset(), confusingly
93 // enough).
94 kDrainingDecoder,
95 kDecoderDrained,
98 // If no demuxer read is in flight and no bitstream buffers are in the
99 // decoder, kick some off demuxing/decoding.
100 void EnsureDemuxOrDecode();
102 // Callback to pass to demuxer_stream_->Read() for receiving encoded bits.
103 void RequestBufferDecode(const scoped_refptr<Buffer>& buffer);
105 // Enqueue a frame for later delivery (or drop it on the floor if a
106 // vda->Reset() is in progress) and trigger out-of-line delivery of the oldest
107 // ready frame to the client if there is a pending read. A NULL |frame|
108 // merely triggers delivery, and requires the ready_video_frames_ queue not be
109 // empty.
110 void EnqueueFrameAndTriggerFrameDelivery(
111 const scoped_refptr<VideoFrame>& frame);
113 // Indicate the picturebuffer can be reused by the decoder.
114 void ReusePictureBuffer(int64 picture_buffer_id);
116 void RecordBufferTimeData(
117 const BitstreamBuffer& bitstream_buffer, const Buffer& buffer);
118 void GetBufferTimeData(
119 int32 id, base::TimeDelta* timestamp, base::TimeDelta* duration);
121 // A shared memory segment and its allocated size.
122 struct SHMBuffer {
123 SHMBuffer(base::SharedMemory* m, size_t s);
124 ~SHMBuffer();
125 base::SharedMemory* shm;
126 size_t size;
129 // Request a shared-memory segment of at least |min_size| bytes. Will
130 // allocate as necessary. Caller does not own returned pointer.
131 SHMBuffer* GetSHM(size_t min_size);
133 // Return a shared-memory segment to the available pool.
134 void PutSHM(SHMBuffer* shm_buffer);
136 StatisticsCallback statistics_callback_;
138 // TODO(scherkus): I think this should be calculated by VideoRenderers based
139 // on information provided by VideoDecoders (i.e., aspect ratio).
140 gfx::Size natural_size_;
142 // Frame duration specified in the video stream's configuration, or 0 if not
143 // present.
144 base::TimeDelta config_frame_duration_;
146 // Pointer to the demuxer stream that will feed us compressed buffers.
147 scoped_refptr<DemuxerStream> demuxer_stream_;
149 // MessageLoop on which to fire callbacks and trampoline calls to this class
150 // if they arrive on other loops.
151 scoped_refptr<base::MessageLoopProxy> gvd_loop_proxy_;
153 // Creation message loop (typically the render thread). All calls to vda_
154 // must be made on this loop (and beware this loop is paused during the
155 // Pause/Flush/Stop dance PipelineImpl::Stop() goes through).
156 scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
158 scoped_refptr<Factories> factories_;
160 // Populated during Initialize() (on success) and unchanged thereafter.
161 scoped_refptr<VideoDecodeAccelerator> vda_;
163 // Callbacks that are !is_null() only during their respective operation being
164 // asynchronously executed.
165 ReadCB pending_read_cb_;
166 base::Closure pending_reset_cb_;
168 State state_;
170 // Is a demuxer read in flight?
171 bool demuxer_read_in_progress_;
173 // Shared-memory buffer pool. Since allocating SHM segments requires a
174 // round-trip to the browser process, we keep allocation out of the
175 // steady-state of the decoder.
176 std::vector<SHMBuffer*> available_shm_segments_;
178 // Book-keeping variables.
179 struct BufferPair {
180 BufferPair(SHMBuffer* s, const scoped_refptr<Buffer>& b);
181 ~BufferPair();
182 SHMBuffer* shm_buffer;
183 scoped_refptr<Buffer> buffer;
185 std::map<int32, BufferPair> bitstream_buffers_in_decoder_;
186 std::map<int32, PictureBuffer> picture_buffers_in_decoder_;
188 // The texture target used for decoded pictures.
189 uint32 decoder_texture_target_;
191 struct BufferTimeData {
192 BufferTimeData(int32 bbid, base::TimeDelta ts, base::TimeDelta dur);
193 ~BufferTimeData();
194 int32 bitstream_buffer_id;
195 base::TimeDelta timestamp;
196 base::TimeDelta duration;
198 std::list<BufferTimeData> input_buffer_time_data_;
200 // picture_buffer_id and the frame wrapping the corresponding Picture, for
201 // frames that have been decoded but haven't been requested by a Read() yet.
202 std::list<scoped_refptr<VideoFrame> > ready_video_frames_;
203 int64 next_picture_buffer_id_;
204 int64 next_bitstream_buffer_id_;
206 // Indicates PrepareForShutdownHack()'s been called. Makes further calls to
207 // this class not require the render thread's loop to be processing.
208 bool shutting_down_;
210 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder);
213 } // namespace media
215 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_