OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <queue> | |
12 | |
13 #include "base/mac/scoped_cftyperef.h" | |
14 #include "base/macros.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/gpu_video_decode_accelerator_helpers.h" | |
21 #include "content/common/gpu/media/vt_mac.h" | |
22 #include "media/filters/h264_parser.h" | |
23 #include "media/video/h264_poc.h" | |
24 #include "media/video/video_decode_accelerator.h" | |
25 #include "ui/gfx/geometry/size.h" | |
26 #include "ui/gl/gl_context_cgl.h" | |
27 #include "ui/gl/gl_image_io_surface.h" | |
28 | |
29 namespace content { | |
30 | |
31 // Preload VideoToolbox libraries, needed for sandbox warmup. | |
32 bool InitializeVideoToolbox(); | |
33 | |
34 // VideoToolbox.framework implementation of the VideoDecodeAccelerator | |
35 // interface for Mac OS X (currently limited to 10.9+). | |
36 class VTVideoDecodeAccelerator : public media::VideoDecodeAccelerator { | |
37 public: | |
38 explicit VTVideoDecodeAccelerator( | |
39 const MakeGLContextCurrentCallback& make_context_current_cb, | |
40 const BindGLImageCallback& bind_image_cb); | |
41 | |
42 ~VTVideoDecodeAccelerator() override; | |
43 | |
44 // VideoDecodeAccelerator implementation. | |
45 bool Initialize(const Config& config, 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 TryToSetupDecodeOnSeparateThread( | |
54 const base::WeakPtr<Client>& decode_client, | |
55 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) | |
56 override; | |
57 | |
58 // Called by OutputThunk() when VideoToolbox finishes decoding a frame. | |
59 void Output( | |
60 void* source_frame_refcon, | |
61 OSStatus status, | |
62 CVImageBufferRef image_buffer); | |
63 | |
64 static media::VideoDecodeAccelerator::SupportedProfiles | |
65 GetSupportedProfiles(); | |
66 | |
67 private: | |
68 // Logged to UMA, so never reuse values. Make sure to update | |
69 // VTVDASessionFailureType in histograms.xml to match. | |
70 enum VTVDASessionFailureType { | |
71 SFT_SUCCESSFULLY_INITIALIZED = 0, | |
72 SFT_PLATFORM_ERROR = 1, | |
73 SFT_INVALID_STREAM = 2, | |
74 SFT_UNSUPPORTED_STREAM_PARAMETERS = 3, | |
75 SFT_DECODE_ERROR = 4, | |
76 SFT_UNSUPPORTED_STREAM = 5, | |
77 // Must always be equal to largest entry logged. | |
78 SFT_MAX = SFT_UNSUPPORTED_STREAM | |
79 }; | |
80 | |
81 enum State { | |
82 STATE_DECODING, | |
83 STATE_ERROR, | |
84 STATE_DESTROYING, | |
85 }; | |
86 | |
87 enum TaskType { | |
88 TASK_FRAME, | |
89 TASK_FLUSH, | |
90 TASK_RESET, | |
91 TASK_DESTROY, | |
92 }; | |
93 | |
94 struct Frame { | |
95 explicit Frame(int32_t bitstream_id); | |
96 ~Frame(); | |
97 | |
98 // ID of the bitstream buffer this Frame will be decoded from. | |
99 int32_t bitstream_id; | |
100 | |
101 // Relative presentation order of this frame (see AVC spec). | |
102 int32_t pic_order_cnt; | |
103 | |
104 // Whether this frame is an IDR. | |
105 bool is_idr; | |
106 | |
107 // Number of frames after this one in decode order that can appear before | |
108 // before it in presentation order. | |
109 int32_t reorder_window; | |
110 | |
111 // Size of the decoded frame. | |
112 // TODO(sandersd): visible_rect. | |
113 gfx::Size coded_size; | |
114 | |
115 // VideoToolbox decoded image, if decoding was successful. | |
116 base::ScopedCFTypeRef<CVImageBufferRef> image; | |
117 }; | |
118 | |
119 struct Task { | |
120 Task(TaskType type); | |
121 Task(const Task& other); | |
122 ~Task(); | |
123 | |
124 TaskType type; | |
125 linked_ptr<Frame> frame; | |
126 }; | |
127 | |
128 struct PictureInfo { | |
129 PictureInfo(uint32_t client_texture_id, uint32_t service_texture_id); | |
130 ~PictureInfo(); | |
131 | |
132 // Image buffer, kept alive while they are bound to pictures. | |
133 base::ScopedCFTypeRef<CVImageBufferRef> cv_image; | |
134 | |
135 // The GLImage representation of |cv_image|. This is kept around to ensure | |
136 // that Destroy is called on it before it hits its destructor (there is a | |
137 // DCHECK that requires this). | |
138 scoped_refptr<gl::GLImageIOSurface> gl_image; | |
139 | |
140 // Texture IDs for the image buffer. | |
141 const uint32_t client_texture_id; | |
142 const uint32_t service_texture_id; | |
143 | |
144 private: | |
145 DISALLOW_COPY_AND_ASSIGN(PictureInfo); | |
146 }; | |
147 | |
148 // | |
149 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|. | |
150 // | |
151 | |
152 // Compute the |pic_order_cnt| for a frame. Returns true or calls | |
153 // NotifyError() before returning false. | |
154 bool ComputePicOrderCnt( | |
155 const media::H264SPS* sps, | |
156 const media::H264SliceHeader& slice_hdr, | |
157 Frame* frame); | |
158 | |
159 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls | |
160 // NotifyError() before returning false. | |
161 bool ConfigureDecoder(); | |
162 | |
163 // Wait for VideoToolbox to output all pending frames. Returns true or calls | |
164 // NotifyError() before returning false. | |
165 bool FinishDelayedFrames(); | |
166 | |
167 // |frame| is owned by |pending_frames_|. | |
168 void DecodeTask(const media::BitstreamBuffer&, Frame* frame); | |
169 void DecodeDone(Frame* frame); | |
170 | |
171 // | |
172 // Methods for interacting with |client_|. Run on |gpu_task_runner_|. | |
173 // | |
174 void NotifyError( | |
175 Error vda_error_type, | |
176 VTVDASessionFailureType session_failure_type); | |
177 | |
178 // |type| is the type of task that the flush will complete, one of TASK_FLUSH, | |
179 // TASK_RESET, or TASK_DESTROY. | |
180 void QueueFlush(TaskType type); | |
181 void FlushTask(TaskType type); | |
182 void FlushDone(TaskType type); | |
183 | |
184 // Try to make progress on tasks in the |task_queue_| or sending frames in the | |
185 // |reorder_queue_|. | |
186 void ProcessWorkQueues(); | |
187 | |
188 // These methods returns true if a task was completed, false otherwise. | |
189 bool ProcessTaskQueue(); | |
190 bool ProcessReorderQueue(); | |
191 bool ProcessFrame(const Frame& frame); | |
192 bool SendFrame(const Frame& frame); | |
193 | |
194 // | |
195 // GPU thread state. | |
196 // | |
197 MakeGLContextCurrentCallback make_context_current_cb_; | |
198 BindGLImageCallback bind_image_cb_; | |
199 | |
200 media::VideoDecodeAccelerator::Client* client_; | |
201 State state_; | |
202 | |
203 // Queue of pending flush tasks. This is used to drop frames when a reset | |
204 // is pending. | |
205 std::queue<TaskType> pending_flush_tasks_; | |
206 | |
207 // Queue of tasks to complete in the GPU thread. | |
208 std::queue<Task> task_queue_; | |
209 | |
210 // Utility class to define the order of frames in the reorder queue. | |
211 struct FrameOrder { | |
212 bool operator()( | |
213 const linked_ptr<Frame>& lhs, | |
214 const linked_ptr<Frame>& rhs) const; | |
215 }; | |
216 | |
217 // Queue of decoded frames in presentation order. | |
218 std::priority_queue<linked_ptr<Frame>, | |
219 std::vector<linked_ptr<Frame>>, | |
220 FrameOrder> reorder_queue_; | |
221 | |
222 // Size of assigned picture buffers. | |
223 gfx::Size picture_size_; | |
224 | |
225 // Frames that have not yet been decoded, keyed by bitstream ID; maintains | |
226 // ownership of Frame objects while they flow through VideoToolbox. | |
227 std::map<int32_t, linked_ptr<Frame>> pending_frames_; | |
228 | |
229 // Set of assigned bitstream IDs, so that Destroy() can release them all. | |
230 std::set<int32_t> assigned_bitstream_ids_; | |
231 | |
232 // All picture buffers assigned to us. Used to check if reused picture buffers | |
233 // should be added back to the available list or released. (They are not | |
234 // released immediately because we need the reuse event to free the binding.) | |
235 std::set<int32_t> assigned_picture_ids_; | |
236 | |
237 // Texture IDs and image buffers of assigned pictures. | |
238 std::map<int32_t, std::unique_ptr<PictureInfo>> picture_info_map_; | |
239 | |
240 // Pictures ready to be rendered to. | |
241 std::vector<int32_t> available_picture_ids_; | |
242 | |
243 // | |
244 // Decoder thread state. | |
245 // | |
246 VTDecompressionOutputCallbackRecord callback_; | |
247 base::ScopedCFTypeRef<CMFormatDescriptionRef> format_; | |
248 base::ScopedCFTypeRef<VTDecompressionSessionRef> session_; | |
249 media::H264Parser parser_; | |
250 gfx::Size coded_size_; | |
251 | |
252 int last_sps_id_; | |
253 std::vector<uint8_t> last_sps_; | |
254 std::vector<uint8_t> last_spsext_; | |
255 int last_pps_id_; | |
256 std::vector<uint8_t> last_pps_; | |
257 bool config_changed_; | |
258 bool waiting_for_idr_; | |
259 bool missing_idr_logged_; | |
260 media::H264POC poc_; | |
261 | |
262 // | |
263 // Shared state (set up and torn down on GPU thread). | |
264 // | |
265 scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_; | |
266 base::ThreadChecker gpu_thread_checker_; | |
267 base::WeakPtr<VTVideoDecodeAccelerator> weak_this_; | |
268 base::Thread decoder_thread_; | |
269 | |
270 // Declared last to ensure that all weak pointers are invalidated before | |
271 // other destructors run. | |
272 base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_; | |
273 | |
274 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator); | |
275 }; | |
276 | |
277 } // namespace content | |
278 | |
279 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_ | |
OLD | NEW |