OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <CoreVideo/CoreVideo.h> | 5 #include <CoreVideo/CoreVideo.h> |
6 #include <OpenGL/CGLIOSurface.h> | 6 #include <OpenGL/CGLIOSurface.h> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
10 #include "content/common/gpu/media/vt_video_decode_accelerator.h" | 10 #include "content/common/gpu/media/vt_video_decode_accelerator.h" |
11 #include "media/filters/h264_parser.h" | 11 #include "media/filters/h264_parser.h" |
12 | 12 |
13 using content_common_gpu_media::kModuleVt; | 13 using content_common_gpu_media::kModuleVt; |
14 using content_common_gpu_media::InitializeStubs; | 14 using content_common_gpu_media::InitializeStubs; |
15 using content_common_gpu_media::IsVtInitialized; | 15 using content_common_gpu_media::IsVtInitialized; |
16 using content_common_gpu_media::StubPathMap; | 16 using content_common_gpu_media::StubPathMap; |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 // Size of length headers prepended to NALUs in MPEG-4 framing. (1, 2, or 4.) | 20 // Size of NALU length headers in AVCC/MPEG-4 format (can be 1, 2, or 4). |
21 static const int kNALUHeaderLength = 4; | 21 static const int kNALUHeaderLength = 4; |
22 | 22 |
23 // We only request 5 picture buffers from the client which are used to hold the | |
24 // decoded samples. These buffers are then reused when the client tells us that | |
25 // it is done with the buffer. | |
26 static const int kNumPictureBuffers = 5; | |
27 | |
23 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator. | 28 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator. |
24 static void OutputThunk( | 29 static void OutputThunk( |
25 void* decompression_output_refcon, | 30 void* decompression_output_refcon, |
26 void* source_frame_refcon, | 31 void* source_frame_refcon, |
27 OSStatus status, | 32 OSStatus status, |
28 VTDecodeInfoFlags info_flags, | 33 VTDecodeInfoFlags info_flags, |
29 CVImageBufferRef image_buffer, | 34 CVImageBufferRef image_buffer, |
30 CMTime presentation_time_stamp, | 35 CMTime presentation_time_stamp, |
31 CMTime presentation_duration) { | 36 CMTime presentation_duration) { |
37 // TODO(sandersd): Implement flush-before-delete to guarantee validity. | |
32 VTVideoDecodeAccelerator* vda = | 38 VTVideoDecodeAccelerator* vda = |
33 reinterpret_cast<VTVideoDecodeAccelerator*>(decompression_output_refcon); | 39 reinterpret_cast<VTVideoDecodeAccelerator*>(decompression_output_refcon); |
34 int32_t* bitstream_id_ptr = reinterpret_cast<int32_t*>(source_frame_refcon); | 40 intptr_t bitstream_id = reinterpret_cast<intptr_t>(source_frame_refcon); |
35 int32_t bitstream_id = *bitstream_id_ptr; | 41 vda->Output(bitstream_id, status, image_buffer); |
36 delete bitstream_id_ptr; | 42 } |
37 CFRetain(image_buffer); | 43 |
38 vda->Output(bitstream_id, status, info_flags, image_buffer); | 44 VTVideoDecodeAccelerator::DecodedFrame::DecodedFrame( |
45 uint32_t bitstream_id, | |
46 CVImageBufferRef image_buffer) | |
47 : bitstream_id(bitstream_id), | |
48 image_buffer(image_buffer) { | |
49 } | |
50 | |
51 VTVideoDecodeAccelerator::DecodedFrame::~DecodedFrame() { | |
39 } | 52 } |
40 | 53 |
41 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(CGLContextObj cgl_context) | 54 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(CGLContextObj cgl_context) |
42 : cgl_context_(cgl_context), | 55 : cgl_context_(cgl_context), |
43 client_(NULL), | 56 client_(NULL), |
44 decoder_thread_("VTDecoderThread"), | |
45 format_(NULL), | 57 format_(NULL), |
46 session_(NULL), | 58 session_(NULL), |
47 weak_this_factory_(this) { | 59 gpu_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
60 weak_this_factory_(this), | |
61 decoder_thread_("VTDecoderThread") { | |
48 callback_.decompressionOutputCallback = OutputThunk; | 62 callback_.decompressionOutputCallback = OutputThunk; |
49 callback_.decompressionOutputRefCon = this; | 63 callback_.decompressionOutputRefCon = this; |
50 } | 64 } |
51 | 65 |
52 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() { | 66 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() { |
53 } | 67 } |
54 | 68 |
55 bool VTVideoDecodeAccelerator::Initialize( | 69 bool VTVideoDecodeAccelerator::Initialize( |
56 media::VideoCodecProfile profile, | 70 media::VideoCodecProfile profile, |
57 Client* client) { | 71 Client* client) { |
58 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
73 | |
59 client_ = client; | 74 client_ = client; |
75 weak_client_factory_.reset( | |
76 new base::WeakPtrFactory<media::VideoDecodeAccelerator::Client>(client)); | |
60 | 77 |
61 // Only H.264 is supported. | 78 // Only H.264 is supported. |
62 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX) | 79 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX) |
63 return false; | 80 return false; |
64 | 81 |
65 // TODO(sandersd): Move VideoToolbox library loading to sandbox startup; | 82 // TODO(sandersd): Move VideoToolbox library loading to sandbox startup; |
66 // until then, --no-sandbox is required. | 83 // until then, --no-sandbox is required. |
67 if (!IsVtInitialized()) { | 84 if (!IsVtInitialized()) { |
68 StubPathMap paths; | 85 StubPathMap paths; |
69 // CoreVideo is also required, but the loader stops after the first | 86 // CoreVideo is also required, but the loader stops after the first |
(...skipping 11 matching lines...) Expand all Loading... | |
81 return false; | 98 return false; |
82 | 99 |
83 // Note that --ignore-gpu-blacklist is still required to get here. | 100 // Note that --ignore-gpu-blacklist is still required to get here. |
84 return true; | 101 return true; |
85 } | 102 } |
86 | 103 |
87 // TODO(sandersd): Proper error reporting instead of CHECKs. | 104 // TODO(sandersd): Proper error reporting instead of CHECKs. |
88 void VTVideoDecodeAccelerator::ConfigureDecoder( | 105 void VTVideoDecodeAccelerator::ConfigureDecoder( |
89 const std::vector<const uint8_t*>& nalu_data_ptrs, | 106 const std::vector<const uint8_t*>& nalu_data_ptrs, |
90 const std::vector<size_t>& nalu_data_sizes) { | 107 const std::vector<size_t>& nalu_data_sizes) { |
108 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); | |
109 // Construct a new format description from the parameter sets. | |
110 // TODO(sandersd): Replace this with custom code to support OS X < 10.9. | |
91 format_.reset(); | 111 format_.reset(); |
92 CHECK(!CMVideoFormatDescriptionCreateFromH264ParameterSets( | 112 CHECK(!CMVideoFormatDescriptionCreateFromH264ParameterSets( |
93 kCFAllocatorDefault, | 113 kCFAllocatorDefault, |
94 nalu_data_ptrs.size(), // parameter_set_count | 114 nalu_data_ptrs.size(), // parameter_set_count |
95 &nalu_data_ptrs.front(), // ¶meter_set_pointers | 115 &nalu_data_ptrs.front(), // ¶meter_set_pointers |
96 &nalu_data_sizes.front(), // ¶meter_set_sizes | 116 &nalu_data_sizes.front(), // ¶meter_set_sizes |
97 kNALUHeaderLength, // nal_unit_header_length | 117 kNALUHeaderLength, // nal_unit_header_length |
98 format_.InitializeInto() | 118 format_.InitializeInto())); |
99 )); | 119 CMVideoDimensions coded_dimensions = |
120 CMVideoFormatDescriptionGetDimensions(format_); | |
100 | 121 |
101 // TODO(sandersd): Check if the size has changed and handle picture requests. | 122 // Prepare VideoToolbox configuration dictionaries. |
102 CMVideoDimensions coded_size = CMVideoFormatDescriptionGetDimensions(format_); | |
103 coded_size_.SetSize(coded_size.width, coded_size.height); | |
104 | |
105 base::ScopedCFTypeRef<CFMutableDictionaryRef> decoder_config( | 123 base::ScopedCFTypeRef<CFMutableDictionaryRef> decoder_config( |
106 CFDictionaryCreateMutable( | 124 CFDictionaryCreateMutable( |
107 kCFAllocatorDefault, | 125 kCFAllocatorDefault, |
108 1, // capacity | 126 1, // capacity |
109 &kCFTypeDictionaryKeyCallBacks, | 127 &kCFTypeDictionaryKeyCallBacks, |
110 &kCFTypeDictionaryValueCallBacks)); | 128 &kCFTypeDictionaryValueCallBacks)); |
111 | 129 |
112 CFDictionarySetValue( | 130 CFDictionarySetValue( |
113 decoder_config, | 131 decoder_config, |
114 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder | 132 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder |
115 CFSTR("EnableHardwareAcceleratedVideoDecoder"), | 133 CFSTR("EnableHardwareAcceleratedVideoDecoder"), |
116 kCFBooleanTrue); | 134 kCFBooleanTrue); |
117 | 135 |
118 base::ScopedCFTypeRef<CFMutableDictionaryRef> image_config( | 136 base::ScopedCFTypeRef<CFMutableDictionaryRef> image_config( |
119 CFDictionaryCreateMutable( | 137 CFDictionaryCreateMutable( |
120 kCFAllocatorDefault, | 138 kCFAllocatorDefault, |
121 4, // capacity | 139 4, // capacity |
122 &kCFTypeDictionaryKeyCallBacks, | 140 &kCFTypeDictionaryKeyCallBacks, |
123 &kCFTypeDictionaryValueCallBacks)); | 141 &kCFTypeDictionaryValueCallBacks)); |
124 | 142 |
125 // TODO(sandersd): ARGB for video that is not 4:2:0. | |
126 int32_t pixel_format = '2vuy'; | |
127 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i) | 143 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i) |
144 // TODO(sandersd): RGBA option for 4:4:4 video. | |
145 int32_t pixel_format = kCVPixelFormatType_422YpCbCr8; | |
128 base::ScopedCFTypeRef<CFNumberRef> cf_pixel_format(CFINT(pixel_format)); | 146 base::ScopedCFTypeRef<CFNumberRef> cf_pixel_format(CFINT(pixel_format)); |
129 base::ScopedCFTypeRef<CFNumberRef> cf_width(CFINT(coded_size.width)); | 147 base::ScopedCFTypeRef<CFNumberRef> cf_width(CFINT(coded_dimensions.width)); |
130 base::ScopedCFTypeRef<CFNumberRef> cf_height(CFINT(coded_size.height)); | 148 base::ScopedCFTypeRef<CFNumberRef> cf_height(CFINT(coded_dimensions.height)); |
131 #undef CFINT | 149 #undef CFINT |
132 CFDictionarySetValue( | 150 CFDictionarySetValue( |
133 image_config, kCVPixelBufferPixelFormatTypeKey, cf_pixel_format); | 151 image_config, kCVPixelBufferPixelFormatTypeKey, cf_pixel_format); |
134 CFDictionarySetValue(image_config, kCVPixelBufferWidthKey, cf_width); | 152 CFDictionarySetValue(image_config, kCVPixelBufferWidthKey, cf_width); |
135 CFDictionarySetValue(image_config, kCVPixelBufferHeightKey, cf_height); | 153 CFDictionarySetValue(image_config, kCVPixelBufferHeightKey, cf_height); |
136 CFDictionarySetValue( | 154 CFDictionarySetValue( |
137 image_config, kCVPixelBufferOpenGLCompatibilityKey, kCFBooleanTrue); | 155 image_config, kCVPixelBufferOpenGLCompatibilityKey, kCFBooleanTrue); |
138 | 156 |
139 // TODO(sandersd): Skip if the session is compatible. | 157 // TODO(sandersd): Check if the session is already compatible. |
140 // TODO(sandersd): Flush frames when resetting. | 158 // TODO(sandersd): Flush. |
141 session_.reset(); | 159 session_.reset(); |
scherkus (not reviewing)
2014/07/17 21:17:26
shouldn't this already be NULL / DCHECK it is?
sandersd (OOO until July 31)
2014/07/17 23:22:46
That's true now that I've changed DecodeTask to on
| |
142 CHECK(!VTDecompressionSessionCreate( | 160 CHECK(!VTDecompressionSessionCreate( |
143 kCFAllocatorDefault, | 161 kCFAllocatorDefault, |
144 format_, // video_format_description | 162 format_, // video_format_description |
145 decoder_config, // video_decoder_specification | 163 decoder_config, // video_decoder_specification |
146 image_config, // destination_image_buffer_attributes | 164 image_config, // destination_image_buffer_attributes |
147 &callback_, // output_callback | 165 &callback_, // output_callback |
148 session_.InitializeInto() | 166 session_.InitializeInto())); |
149 )); | 167 |
150 DVLOG(2) << "Created VTDecompressionSession"; | 168 // If the size has changed, request new picture buffers. |
169 gfx::Size new_coded_size(coded_dimensions.width, coded_dimensions.height); | |
170 if (coded_size_ != new_coded_size) { | |
scherkus (not reviewing)
2014/07/17 21:17:26
FYI coded_size_ is being read/written to from mult
sandersd (OOO until July 31)
2014/07/17 23:22:46
Good catch on the data race.
This is a tricky ope
| |
171 coded_size_ = new_coded_size; | |
172 // TODO(sandersd): Dismiss existing picture buffers. | |
173 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( | |
174 &media::VideoDecodeAccelerator::Client::ProvidePictureBuffers, | |
175 weak_client_factory_->GetWeakPtr(), | |
176 kNumPictureBuffers, | |
177 coded_size_, | |
178 GL_TEXTURE_RECTANGLE_ARB)); | |
179 } | |
151 } | 180 } |
152 | 181 |
153 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) { | 182 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) { |
154 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
155 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind( | 184 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind( |
156 &VTVideoDecodeAccelerator::DecodeTask, base::Unretained(this), | 185 &VTVideoDecodeAccelerator::DecodeTask, base::Unretained(this), |
157 bitstream)); | 186 bitstream)); |
158 } | 187 } |
159 | 188 |
160 void VTVideoDecodeAccelerator::DecodeTask( | 189 void VTVideoDecodeAccelerator::DecodeTask( |
161 const media::BitstreamBuffer bitstream) { | 190 const media::BitstreamBuffer bitstream) { |
162 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 191 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
163 | 192 |
164 // Map the bitstream buffer. | 193 // Map the bitstream buffer. |
165 base::SharedMemory memory(bitstream.handle(), true); | 194 base::SharedMemory memory(bitstream.handle(), true); |
166 size_t size = bitstream.size(); | 195 size_t size = bitstream.size(); |
167 CHECK(memory.Map(size)); | 196 CHECK(memory.Map(size)); |
168 const uint8_t* buf = static_cast<uint8_t*>(memory.memory()); | 197 const uint8_t* buf = static_cast<uint8_t*>(memory.memory()); |
169 | 198 |
170 // Locate relevant NALUs in the buffer. | 199 // NALUs are stored with Annex B format in the bitstream buffer (3-byte start |
200 // codes), but VideoToolbox expects AVCC/MPEG-4 format (length headers), so we | |
201 // must to rewrite the data. | |
202 // | |
203 // 1. Locate relevant NALUs and compute the size of the translated data. | |
204 // Also record any parameter sets for VideoToolbox initialization. | |
171 size_t data_size = 0; | 205 size_t data_size = 0; |
172 std::vector<media::H264NALU> nalus; | 206 std::vector<media::H264NALU> nalus; |
173 std::vector<const uint8_t*> config_nalu_data_ptrs; | 207 std::vector<const uint8_t*> config_nalu_data_ptrs; |
174 std::vector<size_t> config_nalu_data_sizes; | 208 std::vector<size_t> config_nalu_data_sizes; |
175 parser_.SetStream(buf, size); | 209 parser_.SetStream(buf, size); |
176 media::H264NALU nalu; | 210 media::H264NALU nalu; |
177 while (true) { | 211 while (true) { |
178 media::H264Parser::Result result = parser_.AdvanceToNextNALU(&nalu); | 212 media::H264Parser::Result result = parser_.AdvanceToNextNALU(&nalu); |
179 if (result == media::H264Parser::kEOStream) | 213 if (result == media::H264Parser::kEOStream) |
180 break; | 214 break; |
181 CHECK_EQ(result, media::H264Parser::kOk); | 215 CHECK_EQ(result, media::H264Parser::kOk); |
216 // TODO(sandersd): Check that these are only at the start. | |
182 if (nalu.nal_unit_type == media::H264NALU::kSPS || | 217 if (nalu.nal_unit_type == media::H264NALU::kSPS || |
183 nalu.nal_unit_type == media::H264NALU::kPPS || | 218 nalu.nal_unit_type == media::H264NALU::kPPS || |
184 nalu.nal_unit_type == media::H264NALU::kSPSExt) { | 219 nalu.nal_unit_type == media::H264NALU::kSPSExt) { |
220 DVLOG(2) << "Parameter set " << nalu.nal_unit_type; | |
185 config_nalu_data_ptrs.push_back(nalu.data); | 221 config_nalu_data_ptrs.push_back(nalu.data); |
186 config_nalu_data_sizes.push_back(nalu.size); | 222 config_nalu_data_sizes.push_back(nalu.size); |
223 } else { | |
224 nalus.push_back(nalu); | |
225 data_size += kNALUHeaderLength + nalu.size; | |
187 } | 226 } |
188 nalus.push_back(nalu); | |
189 // Each NALU will have a 4-byte length header prepended. | |
190 data_size += kNALUHeaderLength + nalu.size; | |
191 } | 227 } |
192 | 228 |
193 if (!config_nalu_data_ptrs.empty()) | 229 // 2. Initialize VideoToolbox. |
230 // TODO(sandersd): Reinitialize when there are new parameter sets. | |
231 if (!session_) | |
194 ConfigureDecoder(config_nalu_data_ptrs, config_nalu_data_sizes); | 232 ConfigureDecoder(config_nalu_data_ptrs, config_nalu_data_sizes); |
195 | 233 |
196 // TODO(sandersd): Rewrite slice NALU headers and send for decoding. | 234 // 3. Allocate a memory-backed CMBlockBuffer for the translated data. |
235 base::ScopedCFTypeRef<CMBlockBufferRef> data; | |
236 CHECK(!CMBlockBufferCreateWithMemoryBlock( | |
237 kCFAllocatorDefault, | |
238 NULL, // &memory_block | |
239 data_size, // block_length | |
240 kCFAllocatorDefault, // block_allocator | |
241 NULL, // &custom_block_source | |
242 0, // offset_to_data | |
243 data_size, // data_length | |
244 0, // flags | |
245 data.InitializeInto())); | |
246 | |
247 // 4. Copy NALU data, inserting length headers. | |
248 size_t offset = 0; | |
249 for (size_t i = 0; i < nalus.size(); i++) { | |
250 media::H264NALU& nalu = nalus[i]; | |
251 uint8_t header[4] = {0xff & nalu.size >> 24, | |
252 0xff & nalu.size >> 16, | |
253 0xff & nalu.size >> 8, | |
254 0xff & nalu.size}; | |
255 CHECK(!CMBlockBufferReplaceDataBytes(header, data, offset, 4)); | |
256 offset += 4; | |
257 CHECK(!CMBlockBufferReplaceDataBytes(nalu.data, data, offset, nalu.size)); | |
258 offset += nalu.size; | |
259 } | |
260 | |
261 // 5. Package the data for VideoToolbox and request decoding. | |
262 base::ScopedCFTypeRef<CMSampleBufferRef> frame; | |
263 CHECK(!CMSampleBufferCreate( | |
264 kCFAllocatorDefault, | |
265 data, // data_buffer | |
266 true, // data_ready | |
267 NULL, // make_data_ready_callback | |
268 NULL, // make_data_ready_refcon | |
269 format_, // format_description | |
270 1, // num_samples | |
271 0, // num_sample_timing_entries | |
272 NULL, // &sample_timing_array | |
273 0, // num_sample_size_entries | |
274 NULL, // &sample_size_array | |
275 frame.InitializeInto())); | |
276 | |
277 VTDecodeFrameFlags decode_flags = | |
278 kVTDecodeFrame_EnableAsynchronousDecompression | | |
279 kVTDecodeFrame_EnableTemporalProcessing; | |
280 | |
281 intptr_t bitstream_id = bitstream.id(); | |
282 CHECK(!VTDecompressionSessionDecodeFrame( | |
283 session_, | |
284 frame, // sample_buffer | |
285 decode_flags, // decode_flags | |
286 reinterpret_cast<void*>(bitstream_id), // source_frame_refcon | |
287 NULL)); // &info_flags_out | |
197 } | 288 } |
198 | 289 |
199 // This method may be called on any VideoToolbox thread. | 290 // This method may be called on any VideoToolbox thread. |
200 void VTVideoDecodeAccelerator::Output( | 291 void VTVideoDecodeAccelerator::Output( |
201 int32_t bitstream_id, | 292 int32_t bitstream_id, |
202 OSStatus status, | 293 OSStatus status, |
203 VTDecodeInfoFlags info_flags, | |
204 CVImageBufferRef image_buffer) { | 294 CVImageBufferRef image_buffer) { |
205 // TODO(sandersd): Store the frame in a queue. | 295 CHECK(!status); |
206 CFRelease(image_buffer); | 296 CHECK_EQ(CFGetTypeID(image_buffer), CVPixelBufferGetTypeID()); |
297 CFRetain(image_buffer); | |
298 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( | |
299 &VTVideoDecodeAccelerator::OutputTask, | |
300 weak_this_factory_.GetWeakPtr(), | |
301 DecodedFrame(bitstream_id, image_buffer))); | |
302 } | |
303 | |
304 void VTVideoDecodeAccelerator::OutputTask(DecodedFrame frame) { | |
305 DCHECK(CalledOnValidThread()); | |
306 decoded_frames_.push(frame); | |
307 SendPictures(); | |
207 } | 308 } |
208 | 309 |
209 void VTVideoDecodeAccelerator::AssignPictureBuffers( | 310 void VTVideoDecodeAccelerator::AssignPictureBuffers( |
210 const std::vector<media::PictureBuffer>& pictures) { | 311 const std::vector<media::PictureBuffer>& pictures) { |
211 DCHECK(CalledOnValidThread()); | 312 DCHECK(CalledOnValidThread()); |
313 | |
314 for (size_t i = 0; i < pictures.size(); i++) { | |
315 picture_ids_.push(pictures[i].id()); | |
316 texture_ids_[pictures[i].id()] = pictures[i].texture_id(); | |
317 } | |
318 | |
319 // Pictures are not marked as uncleared until this method returns. They will | |
320 // become broken if they are used before that happens. | |
scherkus (not reviewing)
2014/07/17 21:17:26
to confirm -- we need the post task here, right?
sandersd (OOO until July 31)
2014/07/17 23:22:46
Yes.
| |
321 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( | |
322 &VTVideoDecodeAccelerator::SendPictures, | |
323 weak_this_factory_.GetWeakPtr())); | |
212 } | 324 } |
213 | 325 |
214 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) { | 326 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) { |
215 DCHECK(CalledOnValidThread()); | 327 DCHECK(CalledOnValidThread()); |
328 DCHECK_EQ(CFGetRetainCount(picture_bindings_[picture_id]), 1); | |
329 picture_bindings_.erase(picture_id); | |
330 picture_ids_.push(picture_id); | |
331 SendPictures(); | |
332 } | |
333 | |
334 void VTVideoDecodeAccelerator::SendPictures() { | |
335 DCHECK(CalledOnValidThread()); | |
336 if (picture_ids_.empty() || decoded_frames_.empty()) | |
337 return; | |
338 | |
339 CGLContextObj prev_context = CGLGetCurrentContext(); | |
340 CHECK(!CGLSetCurrentContext(cgl_context_)); | |
341 glEnable(GL_TEXTURE_RECTANGLE_ARB); | |
342 | |
343 while (!picture_ids_.empty() && !decoded_frames_.empty()) { | |
344 int32_t picture_id = picture_ids_.front(); | |
345 picture_ids_.pop(); | |
346 DecodedFrame frame = decoded_frames_.front(); | |
347 decoded_frames_.pop(); | |
348 IOSurfaceRef surface = CVPixelBufferGetIOSurface(frame.image_buffer); | |
349 | |
350 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_ids_[picture_id]); | |
351 CHECK(!CGLTexImageIOSurface2D( | |
352 cgl_context_, // ctx | |
353 GL_TEXTURE_RECTANGLE_ARB, // target | |
354 GL_RGB, // internal_format | |
355 coded_size_.width(), // width | |
356 coded_size_.height(), // height | |
357 GL_YCBCR_422_APPLE, // format | |
358 GL_UNSIGNED_SHORT_8_8_APPLE, // type | |
359 surface, // io_surface | |
360 0)); // plane | |
361 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
362 | |
363 picture_bindings_[picture_id] = frame.image_buffer; | |
364 client_->PictureReady(media::Picture(picture_id, frame.bitstream_id)); | |
365 client_->NotifyEndOfBitstreamBuffer(frame.bitstream_id); | |
366 } | |
367 | |
368 glDisable(GL_TEXTURE_RECTANGLE_ARB); | |
369 CHECK(!CGLSetCurrentContext(prev_context)); | |
216 } | 370 } |
217 | 371 |
218 void VTVideoDecodeAccelerator::Flush() { | 372 void VTVideoDecodeAccelerator::Flush() { |
219 DCHECK(CalledOnValidThread()); | 373 DCHECK(CalledOnValidThread()); |
220 // TODO(sandersd): Trigger flush, sending frames. | 374 // TODO(sandersd): Trigger flush, sending frames. |
221 } | 375 } |
222 | 376 |
223 void VTVideoDecodeAccelerator::Reset() { | 377 void VTVideoDecodeAccelerator::Reset() { |
224 DCHECK(CalledOnValidThread()); | 378 DCHECK(CalledOnValidThread()); |
225 // TODO(sandersd): Trigger flush, discarding frames. | 379 // TODO(sandersd): Trigger flush, discarding frames. |
226 } | 380 } |
227 | 381 |
228 void VTVideoDecodeAccelerator::Destroy() { | 382 void VTVideoDecodeAccelerator::Destroy() { |
229 DCHECK(CalledOnValidThread()); | 383 DCHECK(CalledOnValidThread()); |
230 // TODO(sandersd): Trigger flush, discarding frames, and wait for them. | 384 // TODO(sandersd): Trigger flush, discarding frames, and wait for them. |
231 delete this; | 385 delete this; |
232 } | 386 } |
233 | 387 |
234 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() { | 388 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() { |
235 return false; | 389 return false; |
236 } | 390 } |
237 | 391 |
238 } // namespace content | 392 } // namespace content |
OLD | NEW |