Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / common / gpu / media / vt_video_decode_accelerator.h
blob185d0fb1b77b039553a79feed89f3d5495f1163e
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/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/threading/thread.h"
18 #include "content/common/gpu/media/vt.h"
19 #include "media/filters/h264_parser.h"
20 #include "media/video/video_decode_accelerator.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gl/gl_context_cgl.h"
24 namespace base {
25 class SingleThreadTaskRunner;
26 } // namespace base
28 namespace content {
30 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
31 // interface for Mac OS X (currently limited to 10.9+).
32 class VTVideoDecodeAccelerator
33 : public media::VideoDecodeAccelerator,
34 public base::NonThreadSafe {
35 public:
36 explicit VTVideoDecodeAccelerator(CGLContextObj cgl_context);
37 virtual ~VTVideoDecodeAccelerator();
39 // VideoDecodeAccelerator implementation.
40 virtual bool Initialize(
41 media::VideoCodecProfile profile,
42 Client* client) OVERRIDE;
43 virtual void Decode(const media::BitstreamBuffer& bitstream) OVERRIDE;
44 virtual void AssignPictureBuffers(
45 const std::vector<media::PictureBuffer>& pictures) OVERRIDE;
46 virtual void ReusePictureBuffer(int32_t picture_id) OVERRIDE;
47 virtual void Flush() OVERRIDE;
48 virtual void Reset() OVERRIDE;
49 virtual void Destroy() OVERRIDE;
50 virtual bool CanDecodeOnIOThread() OVERRIDE;
52 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
53 void Output(
54 int32_t bitstream_id,
55 OSStatus status,
56 CVImageBufferRef image_buffer);
58 private:
59 struct DecodedFrame {
60 DecodedFrame(int32_t bitstream_id, CVImageBufferRef image_buffer);
61 ~DecodedFrame();
63 int32_t bitstream_id;
64 base::ScopedCFTypeRef<CVImageBufferRef> image_buffer;
67 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
68 void ConfigureDecoder(
69 const std::vector<const uint8_t*>& nalu_data_ptrs,
70 const std::vector<size_t>& nalu_data_sizes);
71 void DecodeTask(const media::BitstreamBuffer);
73 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
74 void OutputTask(DecodedFrame frame);
75 void SizeChangedTask(gfx::Size coded_size);
76 void SendPictures();
79 // GPU thread state.
81 CGLContextObj cgl_context_;
82 media::VideoDecodeAccelerator::Client* client_;
83 gfx::Size texture_size_;
85 // Texture IDs of pictures.
86 // TODO(sandersd): A single map of structs holding picture data.
87 std::map<int32_t, uint32_t> texture_ids_;
89 // Pictures ready to be rendered to.
90 std::queue<int32_t> available_picture_ids_;
92 // Decoded frames ready to render.
93 std::queue<DecodedFrame> decoded_frames_;
95 // Image buffers kept alive while they are bound to pictures.
96 std::map<int32_t, base::ScopedCFTypeRef<CVImageBufferRef>> picture_bindings_;
99 // Decoder thread state.
101 VTDecompressionOutputCallbackRecord callback_;
102 base::ScopedCFTypeRef<CMFormatDescriptionRef> format_;
103 base::ScopedCFTypeRef<VTDecompressionSessionRef> session_;
104 media::H264Parser parser_;
105 gfx::Size coded_size_;
108 // Unprotected shared state (set up and torn down on GPU thread).
110 scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
112 // This WeakPtrFactory does not need to be last as its pointers are bound to
113 // the same thread it is destructed on (the GPU thread).
114 base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_;
116 // Declared last to ensure that all decoder thread tasks complete before any
117 // state is destructed.
118 base::Thread decoder_thread_;
120 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator);
123 } // namespace content
125 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_