Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / vt_video_decode_accelerator.h
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/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"
23
24 namespace base {
25 class SingleThreadTaskRunner;
26 }  // namespace base
27
28 namespace content {
29
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(
37       CGLContextObj cgl_context,
38       const base::Callback<bool(void)>& make_context_current);
39   ~VTVideoDecodeAccelerator() override;
40
41   // VideoDecodeAccelerator implementation.
42   bool Initialize(media::VideoCodecProfile profile, Client* client) override;
43   void Decode(const media::BitstreamBuffer& bitstream) override;
44   void AssignPictureBuffers(
45       const std::vector<media::PictureBuffer>& pictures) override;
46   void ReusePictureBuffer(int32_t picture_id) override;
47   void Flush() override;
48   void Reset() override;
49   void Destroy() override;
50   bool CanDecodeOnIOThread() override;
51
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);
57
58  private:
59   struct DecodedFrame {
60     DecodedFrame(int32_t bitstream_id, CVImageBufferRef image_buffer);
61     ~DecodedFrame();
62
63     int32_t bitstream_id;
64     base::ScopedCFTypeRef<CVImageBufferRef> image_buffer;
65   };
66
67   // Actions are the possible types of pending operations, which are queued
68   // by Flush(), Reset(), and Destroy().
69   enum Action {
70     ACTION_FLUSH,
71     ACTION_RESET,
72     ACTION_DESTROY
73   };
74
75   // PendingActions contain the |bitstream_id| of a frame that, once decoded and
76   // sent, a particular |action| should be completed at.
77   struct PendingAction {
78     PendingAction(Action action, int32_t bitstream_id);
79     ~PendingAction();
80
81     Action action;
82     int32_t bitstream_id;
83   };
84
85   // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
86   bool ConfigureDecoder(
87       const std::vector<const uint8_t*>& nalu_data_ptrs,
88       const std::vector<size_t>& nalu_data_sizes);
89   void DecodeTask(const media::BitstreamBuffer&);
90   void FlushTask();
91   void DropBitstream(int32_t bitstream_id);
92
93   // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
94   void OutputTask(DecodedFrame frame);
95   void NotifyError(Error error);
96
97   // Send decoded frames up to and including |up_to_bitstream_id|, and return
98   // the last sent |bitstream_id|.
99   int32_t SendPictures(int32_t up_to_bitstream_id);
100
101   // Internal helper for SendPictures(): Drop frames with no image data up to
102   // a particular bitstream ID, so that if there is still a frame in the queue
103   // when this function returns, it is guaranteed to have image data, and thus
104   // it is time to set up the GPU context. Returns the last bitstream ID that
105   // was dropped, or |last_sent_bitstream_id| if no frames were dropped.
106   int32_t ProcessDroppedFrames(
107       int32_t last_sent_bitstream_id,
108       int32_t up_to_bitstream_id);
109
110   // Internal helper for SendPictures(): Check if the next frame has a size
111   // different from the current picture buffers, and request new ones if so.
112   void ProcessSizeChangeIfNeeded();
113
114   // Since VideoToolbox has no reset feature (only flush), and the VDA API
115   // allows Decode() and Flush() calls during a reset operation, it's possible
116   // to have multiple pending actions at once. We handle the fully general case
117   // of an arbitrary sequence of pending actions (in reality, there should
118   // probably be at most one reset and one flush at a time).
119   void QueueAction(Action action);
120
121   // Process queued decoded frames, usually by sending them (unless there
122   // is a pending ACTION_RESET or ACTION_DESTROY, in which case they are
123   // dropped), completing queued actions along the way.
124   void ProcessDecodedFrames();
125
126   // Complete a particular action, by eg. calling NotifyFlushDone().
127   // Warning: Deletes |this| if |action| is ACTION_DESTROY.
128   void CompleteAction(Action action);
129
130   // Complete all actions pending for a particular |bitstream_id|.
131   // Warning: Do not call if there is a pending ACTION_DESTROY.
132   void CompleteActions(int32_t bitstream_id);
133
134   //
135   // GPU thread state.
136   //
137   CGLContextObj cgl_context_;
138   base::Callback<bool(void)> make_context_current_;
139   media::VideoDecodeAccelerator::Client* client_;
140
141   // client_->NotifyError() called.
142   bool has_error_;
143
144   // Size of assigned picture buffers.
145   gfx::Size picture_size_;
146
147   // Queue of actions so that we can quickly discover what the next action will
148   // be; this is useful because we are dropping all frames when the next action
149   // is ACTION_RESET or ACTION_DESTROY.
150   std::queue<PendingAction> pending_actions_;
151
152   // Queue of bitstreams that have not yet been decoded. This is mostly needed
153   // to be sure we free them all in Destroy().
154   std::queue<int32_t> pending_bitstream_ids_;
155
156   // All picture buffers assigned to us. Used to check if reused picture buffers
157   // should be added back to the available list or released. (They are not
158   // released immediately because we need the reuse event to free the binding.)
159   std::set<int32_t> assigned_picture_ids_;
160
161   // Texture IDs of assigned pictures.
162   std::map<int32_t, uint32_t> texture_ids_;
163
164   // Pictures ready to be rendered to.
165   std::vector<int32_t> available_picture_ids_;
166
167   // Decoded frames ready to render.
168   std::queue<DecodedFrame> decoded_frames_;
169
170   // Image buffers kept alive while they are bound to pictures.
171   std::map<int32_t, base::ScopedCFTypeRef<CVImageBufferRef>> picture_bindings_;
172
173   //
174   // Decoder thread state.
175   //
176   VTDecompressionOutputCallbackRecord callback_;
177   base::ScopedCFTypeRef<CMFormatDescriptionRef> format_;
178   base::ScopedCFTypeRef<VTDecompressionSessionRef> session_;
179   media::H264Parser parser_;
180
181   std::vector<uint8_t> last_sps_;
182   std::vector<uint8_t> last_spsext_;
183   std::vector<uint8_t> last_pps_;
184
185   //
186   // Shared state (set up and torn down on GPU thread).
187   //
188   scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
189
190   // This WeakPtrFactory does not need to be last as its pointers are bound to
191   // the same thread it is destructed on (the GPU thread).
192   base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_;
193
194   // Declared last to ensure that all decoder thread tasks complete before any
195   // state is destructed.
196   base::Thread decoder_thread_;
197
198   DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator);
199 };
200
201 }  // namespace content
202
203 #endif  // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_