1 // Copyright 2014 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 CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/threading/thread.h"
18 #include "content/common/gpu/media/vt.h"
19 #include "media/filters/h264_parser.h"
20 #include "media/video/video_decode_accelerator.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gl/gl_context_cgl.h"
25 class SingleThreadTaskRunner;
30 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
31 // interface for Mac OS X (currently limited to 10.9+).
32 class VTVideoDecodeAccelerator
33 : public media::VideoDecodeAccelerator,
34 public base::NonThreadSafe {
36 explicit VTVideoDecodeAccelerator(
37 CGLContextObj cgl_context,
38 const base::Callback<bool(void)>& make_context_current);
39 ~VTVideoDecodeAccelerator() override;
41 // VideoDecodeAccelerator implementation.
42 bool Initialize(media::VideoCodecProfile profile, Client* client) override;
43 void Decode(const media::BitstreamBuffer& bitstream) override;
44 void AssignPictureBuffers(
45 const std::vector<media::PictureBuffer>& pictures) override;
46 void ReusePictureBuffer(int32_t picture_id) override;
47 void Flush() override;
48 void Reset() override;
49 void Destroy() override;
50 bool CanDecodeOnIOThread() override;
52 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
56 CVImageBufferRef image_buffer);
60 DecodedFrame(int32_t bitstream_id, CVImageBufferRef image_buffer);
64 base::ScopedCFTypeRef<CVImageBufferRef> image_buffer;
67 // Actions are the possible types of pending operations, which are queued
68 // by Flush(), Reset(), and Destroy().
75 // PendingActions contain the |bitstream_id| of a frame that, once decoded and
76 // sent, a particular |action| should be completed at.
77 struct PendingAction {
78 PendingAction(Action action, int32_t bitstream_id);
85 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
86 bool ConfigureDecoder(
87 const std::vector<const uint8_t*>& nalu_data_ptrs,
88 const std::vector<size_t>& nalu_data_sizes);
89 void DecodeTask(const media::BitstreamBuffer&);
91 void DropBitstream(int32_t bitstream_id);
93 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
94 void OutputTask(DecodedFrame frame);
95 void NotifyError(Error error);
97 // Send decoded frames up to and including |up_to_bitstream_id|, and return
98 // the last sent |bitstream_id|.
99 int32_t SendPictures(int32_t up_to_bitstream_id);
101 // Internal helper for SendPictures(): Drop frames with no image data up to
102 // a particular bitstream ID, so that if there is still a frame in the queue
103 // when this function returns, it is guaranteed to have image data, and thus
104 // it is time to set up the GPU context. Returns the last bitstream ID that
105 // was dropped, or |last_sent_bitstream_id| if no frames were dropped.
106 int32_t ProcessDroppedFrames(
107 int32_t last_sent_bitstream_id,
108 int32_t up_to_bitstream_id);
110 // Internal helper for SendPictures(): Check if the next frame has a size
111 // different from the current picture buffers, and request new ones if so.
112 void ProcessSizeChangeIfNeeded();
114 // Since VideoToolbox has no reset feature (only flush), and the VDA API
115 // allows Decode() and Flush() calls during a reset operation, it's possible
116 // to have multiple pending actions at once. We handle the fully general case
117 // of an arbitrary sequence of pending actions (in reality, there should
118 // probably be at most one reset and one flush at a time).
119 void QueueAction(Action action);
121 // Process queued decoded frames, usually by sending them (unless there
122 // is a pending ACTION_RESET or ACTION_DESTROY, in which case they are
123 // dropped), completing queued actions along the way.
124 void ProcessDecodedFrames();
126 // Complete a particular action, by eg. calling NotifyFlushDone().
127 // Warning: Deletes |this| if |action| is ACTION_DESTROY.
128 void CompleteAction(Action action);
130 // Complete all actions pending for a particular |bitstream_id|.
131 // Warning: Do not call if there is a pending ACTION_DESTROY.
132 void CompleteActions(int32_t bitstream_id);
137 CGLContextObj cgl_context_;
138 base::Callback<bool(void)> make_context_current_;
139 media::VideoDecodeAccelerator::Client* client_;
141 // client_->NotifyError() called.
144 // Size of assigned picture buffers.
145 gfx::Size picture_size_;
147 // Queue of actions so that we can quickly discover what the next action will
148 // be; this is useful because we are dropping all frames when the next action
149 // is ACTION_RESET or ACTION_DESTROY.
150 std::queue<PendingAction> pending_actions_;
152 // Queue of bitstreams that have not yet been decoded. This is mostly needed
153 // to be sure we free them all in Destroy().
154 std::queue<int32_t> pending_bitstream_ids_;
156 // All picture buffers assigned to us. Used to check if reused picture buffers
157 // should be added back to the available list or released. (They are not
158 // released immediately because we need the reuse event to free the binding.)
159 std::set<int32_t> assigned_picture_ids_;
161 // Texture IDs of assigned pictures.
162 std::map<int32_t, uint32_t> texture_ids_;
164 // Pictures ready to be rendered to.
165 std::vector<int32_t> available_picture_ids_;
167 // Decoded frames ready to render.
168 std::queue<DecodedFrame> decoded_frames_;
170 // Image buffers kept alive while they are bound to pictures.
171 std::map<int32_t, base::ScopedCFTypeRef<CVImageBufferRef>> picture_bindings_;
174 // Decoder thread state.
176 VTDecompressionOutputCallbackRecord callback_;
177 base::ScopedCFTypeRef<CMFormatDescriptionRef> format_;
178 base::ScopedCFTypeRef<VTDecompressionSessionRef> session_;
179 media::H264Parser parser_;
181 std::vector<uint8_t> last_sps_;
182 std::vector<uint8_t> last_spsext_;
183 std::vector<uint8_t> last_pps_;
186 // Shared state (set up and torn down on GPU thread).
188 scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
190 // This WeakPtrFactory does not need to be last as its pointers are bound to
191 // the same thread it is destructed on (the GPU thread).
192 base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_;
194 // Declared last to ensure that all decoder thread tasks complete before any
195 // state is destructed.
196 base::Thread decoder_thread_;
198 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator);
201 } // namespace content
203 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_