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/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/threading/thread.h"
18 #include "base/threading/thread_checker.h"
19 #include "content/common/gpu/media/vt.h"
20 #include "media/filters/h264_parser.h"
21 #include "media/video/h264_poc.h"
22 #include "media/video/video_decode_accelerator.h"
23 #include "ui/gfx/geometry/size.h"
24 #include "ui/gl/gl_context_cgl.h"
27 class SingleThreadTaskRunner
;
32 // Preload VideoToolbox libraries, needed for sandbox warmup.
33 bool InitializeVideoToolbox();
35 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
36 // interface for Mac OS X (currently limited to 10.9+).
37 class VTVideoDecodeAccelerator
: public media::VideoDecodeAccelerator
{
39 explicit VTVideoDecodeAccelerator(
40 CGLContextObj cgl_context
,
41 const base::Callback
<bool(void)>& make_context_current
);
42 ~VTVideoDecodeAccelerator() override
;
44 // VideoDecodeAccelerator implementation.
45 bool Initialize(media::VideoCodecProfile profile
, Client
* client
) override
;
46 void Decode(const media::BitstreamBuffer
& bitstream
) override
;
47 void AssignPictureBuffers(
48 const std::vector
<media::PictureBuffer
>& pictures
) override
;
49 void ReusePictureBuffer(int32_t picture_id
) override
;
50 void Flush() override
;
51 void Reset() override
;
52 void Destroy() override
;
53 bool CanDecodeOnIOThread() override
;
55 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
57 void* source_frame_refcon
,
59 CVImageBufferRef image_buffer
);
61 static media::VideoDecodeAccelerator::SupportedProfiles
62 GetSupportedProfiles();
65 // Logged to UMA, so never reuse values. Make sure to update
66 // VTVDASessionFailureType in histograms.xml to match.
67 enum VTVDASessionFailureType
{
68 SFT_SUCCESSFULLY_INITIALIZED
= 0,
69 SFT_PLATFORM_ERROR
= 1,
70 SFT_INVALID_STREAM
= 2,
71 SFT_UNSUPPORTED_STREAM_PARAMETERS
= 3,
73 SFT_UNSUPPORTED_STREAM
= 5,
74 // Must always be equal to largest entry logged.
75 SFT_MAX
= SFT_UNSUPPORTED_STREAM
92 Frame(int32_t bitstream_id
);
95 // ID of the bitstream buffer this Frame will be decoded from.
98 // Relative presentation order of this frame (see AVC spec).
99 int32_t pic_order_cnt
;
101 // Nnumber of frames after this one in decode order that can appear before
102 // before it in presentation order.
103 int32_t reorder_window
;
105 // Size of the decoded frame.
106 // TODO(sandersd): visible_rect.
107 gfx::Size coded_size
;
109 // VideoToolbox decoded image, if decoding was successful.
110 base::ScopedCFTypeRef
<CVImageBufferRef
> image
;
118 linked_ptr
<Frame
> frame
;
122 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
125 // Compute the |pic_order_cnt| for a frame. Returns true or calls
126 // NotifyError() before returning false.
127 bool ComputePicOrderCnt(
128 const media::H264SPS
* sps
,
129 const media::H264SliceHeader
& slice_hdr
,
132 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls
133 // NotifyError() before returning false.
134 bool ConfigureDecoder();
136 // Wait for VideoToolbox to output all pending frames. Returns true or calls
137 // NotifyError() before returning false.
138 bool FinishDelayedFrames();
140 // |frame| is owned by |pending_frames_|.
141 void DecodeTask(const media::BitstreamBuffer
&, Frame
* frame
);
142 void DecodeDone(Frame
* frame
);
145 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
148 Error vda_error_type
,
149 VTVDASessionFailureType session_failure_type
);
151 // |type| is the type of task that the flush will complete, one of TASK_FLUSH,
152 // TASK_RESET, or TASK_DESTROY.
153 void QueueFlush(TaskType type
);
154 void FlushTask(TaskType type
);
155 void FlushDone(TaskType type
);
157 // Try to make progress on tasks in the |task_queue_| or sending frames in the
159 void ProcessWorkQueues();
161 // These methods returns true if a task was completed, false otherwise.
162 bool ProcessTaskQueue();
163 bool ProcessReorderQueue();
164 bool ProcessFrame(const Frame
& frame
);
165 bool SendFrame(const Frame
& frame
);
170 CGLContextObj cgl_context_
;
171 base::Callback
<bool(void)> make_context_current_
;
172 media::VideoDecodeAccelerator::Client
* client_
;
175 // Queue of pending flush tasks. This is used to drop frames when a reset
177 std::queue
<TaskType
> pending_flush_tasks_
;
179 // Queue of tasks to complete in the GPU thread.
180 std::queue
<Task
> task_queue_
;
182 // Utility class to define the order of frames in the reorder queue.
185 const linked_ptr
<Frame
>& lhs
,
186 const linked_ptr
<Frame
>& rhs
) const;
189 // Queue of decoded frames in presentation order.
190 std::priority_queue
<linked_ptr
<Frame
>,
191 std::vector
<linked_ptr
<Frame
>>,
192 FrameOrder
> reorder_queue_
;
194 // Size of assigned picture buffers.
195 gfx::Size picture_size_
;
197 // Frames that have not yet been decoded, keyed by bitstream ID; maintains
198 // ownership of Frame objects while they flow through VideoToolbox.
199 std::map
<int32_t, linked_ptr
<Frame
>> pending_frames_
;
201 // Set of assigned bitstream IDs, so that Destroy() can release them all.
202 std::set
<int32_t> assigned_bitstream_ids_
;
204 // All picture buffers assigned to us. Used to check if reused picture buffers
205 // should be added back to the available list or released. (They are not
206 // released immediately because we need the reuse event to free the binding.)
207 std::set
<int32_t> assigned_picture_ids_
;
209 // Texture IDs of assigned pictures.
210 std::map
<int32_t, uint32_t> texture_ids_
;
212 // Pictures ready to be rendered to.
213 std::vector
<int32_t> available_picture_ids_
;
215 // Image buffers kept alive while they are bound to pictures.
216 std::map
<int32_t, base::ScopedCFTypeRef
<CVImageBufferRef
>> picture_bindings_
;
219 // Decoder thread state.
221 VTDecompressionOutputCallbackRecord callback_
;
222 base::ScopedCFTypeRef
<CMFormatDescriptionRef
> format_
;
223 base::ScopedCFTypeRef
<VTDecompressionSessionRef
> session_
;
224 media::H264Parser parser_
;
225 gfx::Size coded_size_
;
228 std::vector
<uint8_t> last_sps_
;
229 std::vector
<uint8_t> last_spsext_
;
231 std::vector
<uint8_t> last_pps_
;
235 // Shared state (set up and torn down on GPU thread).
237 scoped_refptr
<base::SingleThreadTaskRunner
> gpu_task_runner_
;
238 base::ThreadChecker gpu_thread_checker_
;
239 base::WeakPtr
<VTVideoDecodeAccelerator
> weak_this_
;
240 base::Thread decoder_thread_
;
242 // Declared last to ensure that all weak pointers are invalidated before
243 // other destructors run.
244 base::WeakPtrFactory
<VTVideoDecodeAccelerator
> weak_this_factory_
;
246 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator
);
249 } // namespace content
251 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_