OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #ifndef MEDIA_FILTERS_GPU_VIDEO_DECODER_H_ |
| 6 #define MEDIA_FILTERS_GPU_VIDEO_DECODER_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <list> |
| 10 #include <map> |
| 11 |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "media/base/filters.h" |
| 14 #include "media/base/pipeline_status.h" |
| 15 #include "media/base/pts_stream.h" |
| 16 #include "media/video/video_decode_accelerator.h" |
| 17 #include "ui/gfx/size.h" |
| 18 |
| 19 class MessageLoop; |
| 20 namespace base { |
| 21 class SharedMemory; |
| 22 } |
| 23 |
| 24 namespace media { |
| 25 |
| 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 Factories { |
| 36 public: |
| 37 // Caller owns returned pointer. |
| 38 virtual VideoDecodeAccelerator* CreateVideoDecodeAccelerator( |
| 39 VideoDecodeAccelerator::Profile, VideoDecodeAccelerator::Client*) = 0; |
| 40 // Allocate & delete native textures. Return true on success. |
| 41 virtual bool CreateTextures(int32 count, const gfx::Size& size, |
| 42 std::vector<uint32>* texture_ids) = 0; |
| 43 virtual bool DeleteTexture(uint32 texture_id) = 0; |
| 44 // Allocate & return a shared memory segment. Caller is responsible for |
| 45 // Close()ing the returned pointer. |
| 46 virtual base::SharedMemory* CreateSharedMemory(size_t size) = 0; |
| 47 }; |
| 48 |
| 49 // Takes ownership of |factories| but not |message_loop|. |
| 50 explicit GpuVideoDecoder(MessageLoop* message_loop, |
| 51 Factories* factories); |
| 52 virtual ~GpuVideoDecoder(); |
| 53 |
| 54 // Filter implementation. |
| 55 virtual void Stop(const base::Closure& callback) OVERRIDE; |
| 56 virtual void Seek(base::TimeDelta time, const FilterStatusCB& cb) OVERRIDE; |
| 57 virtual void Pause(const base::Closure& callback) OVERRIDE; |
| 58 virtual void Flush(const base::Closure& callback) OVERRIDE; |
| 59 |
| 60 // VideoDecoder implementation. |
| 61 virtual void Initialize(DemuxerStream* demuxer_stream, |
| 62 const PipelineStatusCB& callback, |
| 63 const StatisticsCallback& stats_callback) OVERRIDE; |
| 64 virtual void Read(const ReadCB& callback) OVERRIDE; |
| 65 virtual const gfx::Size& natural_size() OVERRIDE; |
| 66 |
| 67 // VideoDecodeAccelerator::Client implementation. |
| 68 virtual void NotifyInitializeDone(); |
| 69 virtual void ProvidePictureBuffers(uint32 count, const gfx::Size& size); |
| 70 virtual void DismissPictureBuffer(int32 id); |
| 71 virtual void PictureReady(const media::Picture& picture); |
| 72 virtual void NotifyEndOfStream(); |
| 73 virtual void NotifyEndOfBitstreamBuffer(int32 id); |
| 74 virtual void NotifyFlushDone(); |
| 75 virtual void NotifyResetDone(); |
| 76 virtual void NotifyError(media::VideoDecodeAccelerator::Error error); |
| 77 |
| 78 private: |
| 79 // If no demuxer read is in flight and no bitstream buffers are in the |
| 80 // decoder, kick some off demuxing/decoding. |
| 81 void EnsureDemuxOrDecode(); |
| 82 |
| 83 // Callback to pass to demuxer_stream_->Read() for receiving encoded bits. |
| 84 void RequestBufferDecode(const scoped_refptr<Buffer>& buffer); |
| 85 |
| 86 // Deliver a frame to the client. Because VideoDecoder::Read() promises not |
| 87 // to run its callback before returning, we need an out-of-line helper here. |
| 88 void DeliverFrame(const scoped_refptr<VideoFrame>& frame); |
| 89 void DeliverFrameOutOfLine(const scoped_refptr<VideoFrame>& frame); |
| 90 |
| 91 // Indicate the picturebuffer can be reused by the decoder. |
| 92 void ReusePictureBuffer(int64 picture_buffer_id); |
| 93 |
| 94 // A shared memory segment and its allocated size. |
| 95 typedef std::pair<base::SharedMemory*, size_t> SHMBuffer; |
| 96 |
| 97 // Request a shared-memory segment of at least |min_size| bytes. Will |
| 98 // allocate as necessary. Caller does not own returned pointer. |
| 99 SHMBuffer* GetSHM(size_t min_size); |
| 100 // Return a shared-memory segment to the available pool. |
| 101 void PutSHM(SHMBuffer* shm_buffer); |
| 102 |
| 103 PtsStream pts_stream_; |
| 104 StatisticsCallback statistics_callback_; |
| 105 |
| 106 // TODO(scherkus): I think this should be calculated by VideoRenderers based |
| 107 // on information provided by VideoDecoders (i.e., aspect ratio). |
| 108 gfx::Size natural_size_; |
| 109 |
| 110 // Pointer to the demuxer stream that will feed us compressed buffers. |
| 111 scoped_refptr<DemuxerStream> demuxer_stream_; |
| 112 |
| 113 // MessageLoop on which to do fire callbacks and to which trampoline calls to |
| 114 // this class if they arrive on other loops. |
| 115 MessageLoop* message_loop_; |
| 116 |
| 117 scoped_ptr<Factories> factories_; |
| 118 // Populated during Initialize() (on success) and unchanged thereafter. |
| 119 scoped_refptr<VideoDecodeAccelerator> vda_; |
| 120 |
| 121 // Callbacks that are !is_null() only during their respective operation being |
| 122 // asynchronously executed. |
| 123 ReadCB pending_read_cb_; |
| 124 base::Closure pending_flush_cb_; |
| 125 |
| 126 // Status of the decoder. |
| 127 bool flush_in_progress_; |
| 128 |
| 129 // Is a demuxer read in flight? |
| 130 bool demuxer_read_in_progress_; |
| 131 |
| 132 // Shared-memory buffer pool. Since allocating SHM segments requires a |
| 133 // round-trip to the browser process, we keep allocation out of the |
| 134 // steady-state of the decoder. |
| 135 std::vector<SHMBuffer*> available_shm_segments_; |
| 136 |
| 137 // Book-keeping variables. |
| 138 typedef std::pair<SHMBuffer*, scoped_refptr<Buffer> > BufferPair; |
| 139 std::map<int32, BufferPair> bitstream_buffers_in_decoder_; |
| 140 std::map<int32, PictureBuffer> picture_buffers_in_decoder_; |
| 141 // picture_buffer_id and the frame wrapping the corresponding Picture, for |
| 142 // frames that have been decoded but haven't been requested by a Read() yet. |
| 143 std::list<scoped_refptr<VideoFrame> > ready_video_frames_; |
| 144 int64 next_picture_buffer_id_; |
| 145 int64 next_bitstream_buffer_id_; |
| 146 |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder); |
| 149 }; |
| 150 |
| 151 } // namespace media |
| 152 |
| 153 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_ |
OLD | NEW |