Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / media / vt_video_decode_accelerator.h
blob0ec7114243e3a8582e1119a5e80b57a23df65b8b
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_
8 #include <stdint.h>
10 #include <map>
11 #include <queue>
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"
26 namespace base {
27 class SingleThreadTaskRunner;
28 } // namespace base
30 namespace content {
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 {
38 public:
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.
56 void Output(
57 void* source_frame_refcon,
58 OSStatus status,
59 CVImageBufferRef image_buffer);
61 static media::VideoDecodeAccelerator::SupportedProfiles
62 GetSupportedProfiles();
64 private:
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,
72 SFT_DECODE_ERROR = 4,
73 SFT_UNSUPPORTED_STREAM = 5,
74 // Must always be equal to largest entry logged.
75 SFT_MAX = SFT_UNSUPPORTED_STREAM
78 enum State {
79 STATE_DECODING,
80 STATE_ERROR,
81 STATE_DESTROYING,
84 enum TaskType {
85 TASK_FRAME,
86 TASK_FLUSH,
87 TASK_RESET,
88 TASK_DESTROY,
91 struct Frame {
92 Frame(int32_t bitstream_id);
93 ~Frame();
95 // ID of the bitstream buffer this Frame will be decoded from.
96 int32_t bitstream_id;
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;
113 struct Task {
114 Task(TaskType type);
115 ~Task();
117 TaskType type;
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,
130 Frame* frame);
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_|.
147 void NotifyError(
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
158 // |reorder_queue_|.
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);
168 // GPU thread state.
170 CGLContextObj cgl_context_;
171 base::Callback<bool(void)> make_context_current_;
172 media::VideoDecodeAccelerator::Client* client_;
173 State state_;
175 // Queue of pending flush tasks. This is used to drop frames when a reset
176 // is pending.
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.
183 struct FrameOrder {
184 bool operator()(
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_;
227 int last_sps_id_;
228 std::vector<uint8_t> last_sps_;
229 std::vector<uint8_t> last_spsext_;
230 int last_pps_id_;
231 std::vector<uint8_t> last_pps_;
232 media::H264POC poc_;
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_