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/containers/scoped_ptr_map.h"
14 #include "base/mac/scoped_cftyperef.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/threading/thread.h"
19 #include "base/threading/thread_checker.h"
20 #include "content/common/gpu/media/vt.h"
21 #include "media/filters/h264_parser.h"
22 #include "media/video/h264_poc.h"
23 #include "media/video/video_decode_accelerator.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gl/gl_context_cgl.h"
26 #include "ui/gl/gl_image_io_surface.h"
30 // Preload VideoToolbox libraries, needed for sandbox warmup.
31 bool InitializeVideoToolbox();
33 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
34 // interface for Mac OS X (currently limited to 10.9+).
35 class VTVideoDecodeAccelerator
: public media::VideoDecodeAccelerator
{
37 explicit VTVideoDecodeAccelerator(
38 const base::Callback
<bool(void)>& make_context_current
,
39 const base::Callback
<void(uint32
, uint32
, scoped_refptr
<gfx::GLImage
>)>&
41 ~VTVideoDecodeAccelerator() override
;
43 // VideoDecodeAccelerator implementation.
44 bool Initialize(media::VideoCodecProfile profile
, Client
* client
) override
;
45 void Decode(const media::BitstreamBuffer
& bitstream
) override
;
46 void AssignPictureBuffers(
47 const std::vector
<media::PictureBuffer
>& pictures
) override
;
48 void ReusePictureBuffer(int32_t picture_id
) override
;
49 void Flush() override
;
50 void Reset() override
;
51 void Destroy() override
;
52 bool CanDecodeOnIOThread() override
;
54 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
56 void* source_frame_refcon
,
58 CVImageBufferRef image_buffer
);
60 static media::VideoDecodeAccelerator::SupportedProfiles
61 GetSupportedProfiles();
64 // Logged to UMA, so never reuse values. Make sure to update
65 // VTVDASessionFailureType in histograms.xml to match.
66 enum VTVDASessionFailureType
{
67 SFT_SUCCESSFULLY_INITIALIZED
= 0,
68 SFT_PLATFORM_ERROR
= 1,
69 SFT_INVALID_STREAM
= 2,
70 SFT_UNSUPPORTED_STREAM_PARAMETERS
= 3,
72 SFT_UNSUPPORTED_STREAM
= 5,
73 // Must always be equal to largest entry logged.
74 SFT_MAX
= SFT_UNSUPPORTED_STREAM
91 explicit Frame(int32_t bitstream_id
);
94 // ID of the bitstream buffer this Frame will be decoded from.
97 // Relative presentation order of this frame (see AVC spec).
98 int32_t pic_order_cnt
;
100 // Nnumber of frames after this one in decode order that can appear before
101 // before it in presentation order.
102 int32_t reorder_window
;
104 // Size of the decoded frame.
105 // TODO(sandersd): visible_rect.
106 gfx::Size coded_size
;
108 // VideoToolbox decoded image, if decoding was successful.
109 base::ScopedCFTypeRef
<CVImageBufferRef
> image
;
117 linked_ptr
<Frame
> frame
;
121 PictureInfo(uint32_t client_texture_id
, uint32_t service_texture_id
);
124 // Image buffer, kept alive while they are bound to pictures.
125 base::ScopedCFTypeRef
<CVImageBufferRef
> cv_image
;
127 // The GLImage representation of |cv_image|. This is kept around to ensure
128 // that Destroy is called on it before it hits its destructor (there is a
129 // DCHECK that requires this).
130 scoped_refptr
<gfx::GLImageIOSurface
> gl_image
;
132 // Texture IDs for the image buffer.
133 const uint32_t client_texture_id
;
134 const uint32_t service_texture_id
;
137 DISALLOW_COPY_AND_ASSIGN(PictureInfo
);
141 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
144 // Compute the |pic_order_cnt| for a frame. Returns true or calls
145 // NotifyError() before returning false.
146 bool ComputePicOrderCnt(
147 const media::H264SPS
* sps
,
148 const media::H264SliceHeader
& slice_hdr
,
151 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls
152 // NotifyError() before returning false.
153 bool ConfigureDecoder();
155 // Wait for VideoToolbox to output all pending frames. Returns true or calls
156 // NotifyError() before returning false.
157 bool FinishDelayedFrames();
159 // |frame| is owned by |pending_frames_|.
160 void DecodeTask(const media::BitstreamBuffer
&, Frame
* frame
);
161 void DecodeDone(Frame
* frame
);
164 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
167 Error vda_error_type
,
168 VTVDASessionFailureType session_failure_type
);
170 // |type| is the type of task that the flush will complete, one of TASK_FLUSH,
171 // TASK_RESET, or TASK_DESTROY.
172 void QueueFlush(TaskType type
);
173 void FlushTask(TaskType type
);
174 void FlushDone(TaskType type
);
176 // Try to make progress on tasks in the |task_queue_| or sending frames in the
178 void ProcessWorkQueues();
180 // These methods returns true if a task was completed, false otherwise.
181 bool ProcessTaskQueue();
182 bool ProcessReorderQueue();
183 bool ProcessFrame(const Frame
& frame
);
184 bool SendFrame(const Frame
& frame
);
189 base::Callback
<bool(void)> make_context_current_
;
190 base::Callback
<void(uint32
, uint32
, scoped_refptr
<gfx::GLImage
>)> bind_image_
;
191 media::VideoDecodeAccelerator::Client
* client_
;
194 // Queue of pending flush tasks. This is used to drop frames when a reset
196 std::queue
<TaskType
> pending_flush_tasks_
;
198 // Queue of tasks to complete in the GPU thread.
199 std::queue
<Task
> task_queue_
;
201 // Utility class to define the order of frames in the reorder queue.
204 const linked_ptr
<Frame
>& lhs
,
205 const linked_ptr
<Frame
>& rhs
) const;
208 // Queue of decoded frames in presentation order.
209 std::priority_queue
<linked_ptr
<Frame
>,
210 std::vector
<linked_ptr
<Frame
>>,
211 FrameOrder
> reorder_queue_
;
213 // Size of assigned picture buffers.
214 gfx::Size picture_size_
;
216 // Frames that have not yet been decoded, keyed by bitstream ID; maintains
217 // ownership of Frame objects while they flow through VideoToolbox.
218 std::map
<int32_t, linked_ptr
<Frame
>> pending_frames_
;
220 // Set of assigned bitstream IDs, so that Destroy() can release them all.
221 std::set
<int32_t> assigned_bitstream_ids_
;
223 // All picture buffers assigned to us. Used to check if reused picture buffers
224 // should be added back to the available list or released. (They are not
225 // released immediately because we need the reuse event to free the binding.)
226 std::set
<int32_t> assigned_picture_ids_
;
228 // Texture IDs and image buffers of assigned pictures.
229 base::ScopedPtrMap
<int32_t, scoped_ptr
<PictureInfo
>> picture_info_map_
;
231 // Pictures ready to be rendered to.
232 std::vector
<int32_t> available_picture_ids_
;
235 // Decoder thread state.
237 VTDecompressionOutputCallbackRecord callback_
;
238 base::ScopedCFTypeRef
<CMFormatDescriptionRef
> format_
;
239 base::ScopedCFTypeRef
<VTDecompressionSessionRef
> session_
;
240 media::H264Parser parser_
;
241 gfx::Size coded_size_
;
244 std::vector
<uint8_t> last_sps_
;
245 std::vector
<uint8_t> last_spsext_
;
247 std::vector
<uint8_t> last_pps_
;
251 // Shared state (set up and torn down on GPU thread).
253 scoped_refptr
<base::SingleThreadTaskRunner
> gpu_task_runner_
;
254 base::ThreadChecker gpu_thread_checker_
;
255 base::WeakPtr
<VTVideoDecodeAccelerator
> weak_this_
;
256 base::Thread decoder_thread_
;
258 // Declared last to ensure that all weak pointers are invalidated before
259 // other destructors run.
260 base::WeakPtrFactory
<VTVideoDecodeAccelerator
> weak_this_factory_
;
262 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator
);
265 } // namespace content
267 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_