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