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