Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / media / vt_video_decode_accelerator.h
blob44f563b81bff3fe6fd0e96ee9015063f4878df74
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 content {
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 {
34 public:
35 explicit VTVideoDecodeAccelerator(
36 const base::Callback<bool(void)>& make_context_current);
37 ~VTVideoDecodeAccelerator() override;
39 // VideoDecodeAccelerator implementation.
40 bool Initialize(media::VideoCodecProfile profile, Client* client) override;
41 void Decode(const media::BitstreamBuffer& bitstream) override;
42 void AssignPictureBuffers(
43 const std::vector<media::PictureBuffer>& pictures) override;
44 void ReusePictureBuffer(int32_t picture_id) override;
45 void Flush() override;
46 void Reset() override;
47 void Destroy() override;
48 bool CanDecodeOnIOThread() override;
50 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
51 void Output(
52 void* source_frame_refcon,
53 OSStatus status,
54 CVImageBufferRef image_buffer);
56 static media::VideoDecodeAccelerator::SupportedProfiles
57 GetSupportedProfiles();
59 private:
60 // Logged to UMA, so never reuse values. Make sure to update
61 // VTVDASessionFailureType in histograms.xml to match.
62 enum VTVDASessionFailureType {
63 SFT_SUCCESSFULLY_INITIALIZED = 0,
64 SFT_PLATFORM_ERROR = 1,
65 SFT_INVALID_STREAM = 2,
66 SFT_UNSUPPORTED_STREAM_PARAMETERS = 3,
67 SFT_DECODE_ERROR = 4,
68 SFT_UNSUPPORTED_STREAM = 5,
69 // Must always be equal to largest entry logged.
70 SFT_MAX = SFT_UNSUPPORTED_STREAM
73 enum State {
74 STATE_DECODING,
75 STATE_ERROR,
76 STATE_DESTROYING,
79 enum TaskType {
80 TASK_FRAME,
81 TASK_FLUSH,
82 TASK_RESET,
83 TASK_DESTROY,
86 struct Frame {
87 explicit Frame(int32_t bitstream_id);
88 ~Frame();
90 // ID of the bitstream buffer this Frame will be decoded from.
91 int32_t bitstream_id;
93 // Relative presentation order of this frame (see AVC spec).
94 int32_t pic_order_cnt;
96 // Nnumber of frames after this one in decode order that can appear before
97 // before it in presentation order.
98 int32_t reorder_window;
100 // Size of the decoded frame.
101 // TODO(sandersd): visible_rect.
102 gfx::Size coded_size;
104 // VideoToolbox decoded image, if decoding was successful.
105 base::ScopedCFTypeRef<CVImageBufferRef> image;
108 struct Task {
109 Task(TaskType type);
110 ~Task();
112 TaskType type;
113 linked_ptr<Frame> frame;
117 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
120 // Compute the |pic_order_cnt| for a frame. Returns true or calls
121 // NotifyError() before returning false.
122 bool ComputePicOrderCnt(
123 const media::H264SPS* sps,
124 const media::H264SliceHeader& slice_hdr,
125 Frame* frame);
127 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls
128 // NotifyError() before returning false.
129 bool ConfigureDecoder();
131 // Wait for VideoToolbox to output all pending frames. Returns true or calls
132 // NotifyError() before returning false.
133 bool FinishDelayedFrames();
135 // |frame| is owned by |pending_frames_|.
136 void DecodeTask(const media::BitstreamBuffer&, Frame* frame);
137 void DecodeDone(Frame* frame);
140 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
142 void NotifyError(
143 Error vda_error_type,
144 VTVDASessionFailureType session_failure_type);
146 // |type| is the type of task that the flush will complete, one of TASK_FLUSH,
147 // TASK_RESET, or TASK_DESTROY.
148 void QueueFlush(TaskType type);
149 void FlushTask(TaskType type);
150 void FlushDone(TaskType type);
152 // Try to make progress on tasks in the |task_queue_| or sending frames in the
153 // |reorder_queue_|.
154 void ProcessWorkQueues();
156 // These methods returns true if a task was completed, false otherwise.
157 bool ProcessTaskQueue();
158 bool ProcessReorderQueue();
159 bool ProcessFrame(const Frame& frame);
160 bool SendFrame(const Frame& frame);
163 // GPU thread state.
165 base::Callback<bool(void)> make_context_current_;
166 media::VideoDecodeAccelerator::Client* client_;
167 State state_;
169 // Queue of pending flush tasks. This is used to drop frames when a reset
170 // is pending.
171 std::queue<TaskType> pending_flush_tasks_;
173 // Queue of tasks to complete in the GPU thread.
174 std::queue<Task> task_queue_;
176 // Utility class to define the order of frames in the reorder queue.
177 struct FrameOrder {
178 bool operator()(
179 const linked_ptr<Frame>& lhs,
180 const linked_ptr<Frame>& rhs) const;
183 // Queue of decoded frames in presentation order.
184 std::priority_queue<linked_ptr<Frame>,
185 std::vector<linked_ptr<Frame>>,
186 FrameOrder> reorder_queue_;
188 // Size of assigned picture buffers.
189 gfx::Size picture_size_;
191 // Frames that have not yet been decoded, keyed by bitstream ID; maintains
192 // ownership of Frame objects while they flow through VideoToolbox.
193 std::map<int32_t, linked_ptr<Frame>> pending_frames_;
195 // Set of assigned bitstream IDs, so that Destroy() can release them all.
196 std::set<int32_t> assigned_bitstream_ids_;
198 // All picture buffers assigned to us. Used to check if reused picture buffers
199 // should be added back to the available list or released. (They are not
200 // released immediately because we need the reuse event to free the binding.)
201 std::set<int32_t> assigned_picture_ids_;
203 // Texture IDs of assigned pictures.
204 std::map<int32_t, uint32_t> texture_ids_;
206 // Pictures ready to be rendered to.
207 std::vector<int32_t> available_picture_ids_;
209 // Image buffers kept alive while they are bound to pictures.
210 std::map<int32_t, base::ScopedCFTypeRef<CVImageBufferRef>> picture_bindings_;
213 // Decoder thread state.
215 VTDecompressionOutputCallbackRecord callback_;
216 base::ScopedCFTypeRef<CMFormatDescriptionRef> format_;
217 base::ScopedCFTypeRef<VTDecompressionSessionRef> session_;
218 media::H264Parser parser_;
219 gfx::Size coded_size_;
221 int last_sps_id_;
222 std::vector<uint8_t> last_sps_;
223 std::vector<uint8_t> last_spsext_;
224 int last_pps_id_;
225 std::vector<uint8_t> last_pps_;
226 media::H264POC poc_;
229 // Shared state (set up and torn down on GPU thread).
231 scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
232 base::ThreadChecker gpu_thread_checker_;
233 base::WeakPtr<VTVideoDecodeAccelerator> weak_this_;
234 base::Thread decoder_thread_;
236 // Declared last to ensure that all weak pointers are invalidated before
237 // other destructors run.
238 base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_;
240 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator);
243 } // namespace content
245 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_