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"
28 // Preload VideoToolbox libraries, needed for sandbox warmup.
29 bool InitializeVideoToolbox();
31 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
32 // interface for Mac OS X (currently limited to 10.9+).
33 class VTVideoDecodeAccelerator
: public media::VideoDecodeAccelerator
{
35 explicit VTVideoDecodeAccelerator(
36 CGLContextObj cgl_context
,
37 const base::Callback
<bool(void)>& make_context_current
);
38 ~VTVideoDecodeAccelerator() override
;
40 // VideoDecodeAccelerator implementation.
41 bool Initialize(media::VideoCodecProfile profile
, Client
* client
) override
;
42 void Decode(const media::BitstreamBuffer
& bitstream
) override
;
43 void AssignPictureBuffers(
44 const std::vector
<media::PictureBuffer
>& pictures
) override
;
45 void ReusePictureBuffer(int32_t picture_id
) override
;
46 void Flush() override
;
47 void Reset() override
;
48 void Destroy() override
;
49 bool CanDecodeOnIOThread() override
;
51 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
53 void* source_frame_refcon
,
55 CVImageBufferRef image_buffer
);
57 static media::VideoDecodeAccelerator::SupportedProfiles
58 GetSupportedProfiles();
61 // Logged to UMA, so never reuse values. Make sure to update
62 // VTVDASessionFailureType in histograms.xml to match.
63 enum VTVDASessionFailureType
{
64 SFT_SUCCESSFULLY_INITIALIZED
= 0,
65 SFT_PLATFORM_ERROR
= 1,
66 SFT_INVALID_STREAM
= 2,
67 SFT_UNSUPPORTED_STREAM_PARAMETERS
= 3,
69 SFT_UNSUPPORTED_STREAM
= 5,
70 // Must always be equal to largest entry logged.
71 SFT_MAX
= SFT_UNSUPPORTED_STREAM
88 explicit Frame(int32_t bitstream_id
);
91 // ID of the bitstream buffer this Frame will be decoded from.
94 // Relative presentation order of this frame (see AVC spec).
95 int32_t pic_order_cnt
;
97 // Nnumber of frames after this one in decode order that can appear before
98 // before it in presentation order.
99 int32_t reorder_window
;
101 // Size of the decoded frame.
102 // TODO(sandersd): visible_rect.
103 gfx::Size coded_size
;
105 // VideoToolbox decoded image, if decoding was successful.
106 base::ScopedCFTypeRef
<CVImageBufferRef
> image
;
114 linked_ptr
<Frame
> frame
;
118 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
121 // Compute the |pic_order_cnt| for a frame. Returns true or calls
122 // NotifyError() before returning false.
123 bool ComputePicOrderCnt(
124 const media::H264SPS
* sps
,
125 const media::H264SliceHeader
& slice_hdr
,
128 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls
129 // NotifyError() before returning false.
130 bool ConfigureDecoder();
132 // Wait for VideoToolbox to output all pending frames. Returns true or calls
133 // NotifyError() before returning false.
134 bool FinishDelayedFrames();
136 // |frame| is owned by |pending_frames_|.
137 void DecodeTask(const media::BitstreamBuffer
&, Frame
* frame
);
138 void DecodeDone(Frame
* frame
);
141 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
144 Error vda_error_type
,
145 VTVDASessionFailureType session_failure_type
);
147 // |type| is the type of task that the flush will complete, one of TASK_FLUSH,
148 // TASK_RESET, or TASK_DESTROY.
149 void QueueFlush(TaskType type
);
150 void FlushTask(TaskType type
);
151 void FlushDone(TaskType type
);
153 // Try to make progress on tasks in the |task_queue_| or sending frames in the
155 void ProcessWorkQueues();
157 // These methods returns true if a task was completed, false otherwise.
158 bool ProcessTaskQueue();
159 bool ProcessReorderQueue();
160 bool ProcessFrame(const Frame
& frame
);
161 bool SendFrame(const Frame
& frame
);
166 CGLContextObj cgl_context_
;
167 base::Callback
<bool(void)> make_context_current_
;
168 media::VideoDecodeAccelerator::Client
* client_
;
171 // Queue of pending flush tasks. This is used to drop frames when a reset
173 std::queue
<TaskType
> pending_flush_tasks_
;
175 // Queue of tasks to complete in the GPU thread.
176 std::queue
<Task
> task_queue_
;
178 // Utility class to define the order of frames in the reorder queue.
181 const linked_ptr
<Frame
>& lhs
,
182 const linked_ptr
<Frame
>& rhs
) const;
185 // Queue of decoded frames in presentation order.
186 std::priority_queue
<linked_ptr
<Frame
>,
187 std::vector
<linked_ptr
<Frame
>>,
188 FrameOrder
> reorder_queue_
;
190 // Size of assigned picture buffers.
191 gfx::Size picture_size_
;
193 // Frames that have not yet been decoded, keyed by bitstream ID; maintains
194 // ownership of Frame objects while they flow through VideoToolbox.
195 std::map
<int32_t, linked_ptr
<Frame
>> pending_frames_
;
197 // Set of assigned bitstream IDs, so that Destroy() can release them all.
198 std::set
<int32_t> assigned_bitstream_ids_
;
200 // All picture buffers assigned to us. Used to check if reused picture buffers
201 // should be added back to the available list or released. (They are not
202 // released immediately because we need the reuse event to free the binding.)
203 std::set
<int32_t> assigned_picture_ids_
;
205 // Texture IDs of assigned pictures.
206 std::map
<int32_t, uint32_t> texture_ids_
;
208 // Pictures ready to be rendered to.
209 std::vector
<int32_t> available_picture_ids_
;
211 // Image buffers kept alive while they are bound to pictures.
212 std::map
<int32_t, base::ScopedCFTypeRef
<CVImageBufferRef
>> picture_bindings_
;
215 // Decoder thread state.
217 VTDecompressionOutputCallbackRecord callback_
;
218 base::ScopedCFTypeRef
<CMFormatDescriptionRef
> format_
;
219 base::ScopedCFTypeRef
<VTDecompressionSessionRef
> session_
;
220 media::H264Parser parser_
;
221 gfx::Size coded_size_
;
224 std::vector
<uint8_t> last_sps_
;
225 std::vector
<uint8_t> last_spsext_
;
227 std::vector
<uint8_t> last_pps_
;
231 // Shared state (set up and torn down on GPU thread).
233 scoped_refptr
<base::SingleThreadTaskRunner
> gpu_task_runner_
;
234 base::ThreadChecker gpu_thread_checker_
;
235 base::WeakPtr
<VTVideoDecodeAccelerator
> weak_this_
;
236 base::Thread decoder_thread_
;
238 // Declared last to ensure that all weak pointers are invalidated before
239 // other destructors run.
240 base::WeakPtrFactory
<VTVideoDecodeAccelerator
> weak_this_factory_
;
242 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator
);
245 } // namespace content
247 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_