Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2016)

Side by Side Diff: content/common/gpu/media/vt_video_decode_accelerator.cc

Issue 706023004: Collect VTVideoDecodeAccelerator frames into a work queue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@vt_config_change
Patch Set: Work around PPAPI test failure. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <OpenGL/gl.h> 7 #include <OpenGL/gl.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h" 10 #include "base/command_line.h"
12 #include "base/sys_byteorder.h" 11 #include "base/sys_byteorder.h"
13 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
14 #include "content/common/gpu/media/vt_video_decode_accelerator.h" 13 #include "content/common/gpu/media/vt_video_decode_accelerator.h"
15 #include "content/public/common/content_switches.h" 14 #include "content/public/common/content_switches.h"
15 #include "media/base/limits.h"
16 #include "media/filters/h264_parser.h" 16 #include "media/filters/h264_parser.h"
17 #include "ui/gl/scoped_binders.h" 17 #include "ui/gl/scoped_binders.h"
18 18
19 using content_common_gpu_media::kModuleVt; 19 using content_common_gpu_media::kModuleVt;
20 using content_common_gpu_media::InitializeStubs; 20 using content_common_gpu_media::InitializeStubs;
21 using content_common_gpu_media::IsVtInitialized; 21 using content_common_gpu_media::IsVtInitialized;
22 using content_common_gpu_media::StubPathMap; 22 using content_common_gpu_media::StubPathMap;
23 23
24 #define NOTIFY_STATUS(name, status) \ 24 #define NOTIFY_STATUS(name, status) \
25 do { \ 25 do { \
26 LOG(ERROR) << name << " failed with status " << status; \ 26 DLOG(ERROR) << name << " failed with status " << status; \
27 NotifyError(PLATFORM_FAILURE); \ 27 NotifyError(PLATFORM_FAILURE); \
28 } while (0) 28 } while (0)
29 29
30 namespace content { 30 namespace content {
31 31
32 // Size of NALU length headers in AVCC/MPEG-4 format (can be 1, 2, or 4). 32 // Size to use for NALU length headers in AVC format (can be 1, 2, or 4).
33 static const int kNALUHeaderLength = 4; 33 static const int kNALUHeaderLength = 4;
34 34
35 // We only request 5 picture buffers from the client which are used to hold the 35 // We request 5 picture buffers from the client, each of which has a texture ID
36 // decoded samples. These buffers are then reused when the client tells us that 36 // that we can bind decoded frames to. We need enough to satisfy preroll, and
37 // it is done with the buffer. 37 // enough to avoid unnecessary stalling, but no more than that. The resource
38 static const int kNumPictureBuffers = 5; 38 // requirements are low, as we don't need the textures to be backed by storage.
39 static const int kNumPictureBuffers = media::limits::kMaxVideoFrames + 1;
39 40
40 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator. 41 // Route decoded frame callbacks back into the VTVideoDecodeAccelerator.
41 static void OutputThunk( 42 static void OutputThunk(
42 void* decompression_output_refcon, 43 void* decompression_output_refcon,
43 void* source_frame_refcon, 44 void* source_frame_refcon,
44 OSStatus status, 45 OSStatus status,
45 VTDecodeInfoFlags info_flags, 46 VTDecodeInfoFlags info_flags,
46 CVImageBufferRef image_buffer, 47 CVImageBufferRef image_buffer,
47 CMTime presentation_time_stamp, 48 CMTime presentation_time_stamp,
48 CMTime presentation_duration) { 49 CMTime presentation_duration) {
49 VTVideoDecodeAccelerator* vda = 50 VTVideoDecodeAccelerator* vda =
50 reinterpret_cast<VTVideoDecodeAccelerator*>(decompression_output_refcon); 51 reinterpret_cast<VTVideoDecodeAccelerator*>(decompression_output_refcon);
51 int32_t bitstream_id = reinterpret_cast<intptr_t>(source_frame_refcon); 52 vda->Output(source_frame_refcon, status, image_buffer);
52 vda->Output(bitstream_id, status, image_buffer);
53 } 53 }
54 54
55 VTVideoDecodeAccelerator::DecodedFrame::DecodedFrame( 55 VTVideoDecodeAccelerator::Task::Task(TaskType type) : type(type) {
56 int32_t bitstream_id,
57 CVImageBufferRef image_buffer)
58 : bitstream_id(bitstream_id),
59 image_buffer(image_buffer) {
60 } 56 }
61 57
62 VTVideoDecodeAccelerator::DecodedFrame::~DecodedFrame() { 58 VTVideoDecodeAccelerator::Task::~Task() {
63 } 59 }
64 60
65 VTVideoDecodeAccelerator::PendingAction::PendingAction( 61 VTVideoDecodeAccelerator::Frame::Frame(int32_t bitstream_id)
66 Action action, 62 : bitstream_id(bitstream_id) {
67 int32_t bitstream_id)
68 : action(action),
69 bitstream_id(bitstream_id) {
70 } 63 }
71 64
72 VTVideoDecodeAccelerator::PendingAction::~PendingAction() { 65 VTVideoDecodeAccelerator::Frame::~Frame() {
73 } 66 }
74 67
75 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator( 68 VTVideoDecodeAccelerator::VTVideoDecodeAccelerator(
76 CGLContextObj cgl_context, 69 CGLContextObj cgl_context,
77 const base::Callback<bool(void)>& make_context_current) 70 const base::Callback<bool(void)>& make_context_current)
78 : cgl_context_(cgl_context), 71 : cgl_context_(cgl_context),
79 make_context_current_(make_context_current), 72 make_context_current_(make_context_current),
80 client_(NULL), 73 client_(NULL),
81 has_error_(false), 74 state_(STATE_DECODING),
82 format_(NULL), 75 format_(NULL),
83 session_(NULL), 76 session_(NULL),
84 gpu_task_runner_(base::ThreadTaskRunnerHandle::Get()), 77 gpu_task_runner_(base::ThreadTaskRunnerHandle::Get()),
85 weak_this_factory_(this), 78 decoder_thread_("VTDecoderThread"),
86 decoder_thread_("VTDecoderThread") { 79 weak_this_factory_(this) {
87 DCHECK(!make_context_current_.is_null()); 80 DCHECK(!make_context_current_.is_null());
88 callback_.decompressionOutputCallback = OutputThunk; 81 callback_.decompressionOutputCallback = OutputThunk;
89 callback_.decompressionOutputRefCon = this; 82 callback_.decompressionOutputRefCon = this;
83 weak_this_ = weak_this_factory_.GetWeakPtr();
90 } 84 }
91 85
92 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() { 86 VTVideoDecodeAccelerator::~VTVideoDecodeAccelerator() {
93 } 87 }
94 88
95 bool VTVideoDecodeAccelerator::Initialize( 89 bool VTVideoDecodeAccelerator::Initialize(
96 media::VideoCodecProfile profile, 90 media::VideoCodecProfile profile,
97 Client* client) { 91 Client* client) {
98 DCHECK(CalledOnValidThread()); 92 DCHECK(gpu_thread_checker_.CalledOnValidThread());
99 client_ = client; 93 client_ = client;
100 94
101 // Only H.264 is supported. 95 // Only H.264 is supported.
102 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX) 96 if (profile < media::H264PROFILE_MIN || profile > media::H264PROFILE_MAX)
103 return false; 97 return false;
104 98
105 // Require --no-sandbox until VideoToolbox library loading is part of sandbox 99 // Require --no-sandbox until VideoToolbox library loading is part of sandbox
106 // startup (and this VDA is ready for regular users). 100 // startup (and this VDA is ready for regular users).
107 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) 101 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox))
108 return false; 102 return false;
(...skipping 10 matching lines...) Expand all
119 return false; 113 return false;
120 } 114 }
121 115
122 // Spawn a thread to handle parsing and calling VideoToolbox. 116 // Spawn a thread to handle parsing and calling VideoToolbox.
123 if (!decoder_thread_.Start()) 117 if (!decoder_thread_.Start())
124 return false; 118 return false;
125 119
126 return true; 120 return true;
127 } 121 }
128 122
129 bool VTVideoDecodeAccelerator::ConfigureDecoder( 123 bool VTVideoDecodeAccelerator::FinishDelayedFrames() {
130 const std::vector<const uint8_t*>& nalu_data_ptrs,
131 const std::vector<size_t>& nalu_data_sizes) {
132 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); 124 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread());
125 if (session_) {
126 OSStatus status = VTDecompressionSessionFinishDelayedFrames(session_);
127 if (status) {
128 NOTIFY_STATUS("VTDecompressionSessionFinishDelayedFrames()", status);
129 return false;
130 }
131 }
132 return true;
133 }
134
135 bool VTVideoDecodeAccelerator::ConfigureDecoder() {
136 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread());
137 DCHECK(!last_sps_.empty());
138 DCHECK(!last_pps_.empty());
139
140 // Build the configuration records.
141 std::vector<const uint8_t*> nalu_data_ptrs;
142 std::vector<size_t> nalu_data_sizes;
143 nalu_data_ptrs.reserve(3);
144 nalu_data_sizes.reserve(3);
145 nalu_data_ptrs.push_back(&last_sps_.front());
146 nalu_data_sizes.push_back(last_sps_.size());
147 if (!last_spsext_.empty()) {
148 nalu_data_ptrs.push_back(&last_spsext_.front());
149 nalu_data_sizes.push_back(last_spsext_.size());
150 }
151 nalu_data_ptrs.push_back(&last_pps_.front());
152 nalu_data_sizes.push_back(last_pps_.size());
133 153
134 // Construct a new format description from the parameter sets. 154 // Construct a new format description from the parameter sets.
135 // TODO(sandersd): Replace this with custom code to support OS X < 10.9. 155 // TODO(sandersd): Replace this with custom code to support OS X < 10.9.
136 format_.reset(); 156 format_.reset();
137 OSStatus status = CMVideoFormatDescriptionCreateFromH264ParameterSets( 157 OSStatus status = CMVideoFormatDescriptionCreateFromH264ParameterSets(
138 kCFAllocatorDefault, 158 kCFAllocatorDefault,
139 nalu_data_ptrs.size(), // parameter_set_count 159 nalu_data_ptrs.size(), // parameter_set_count
140 &nalu_data_ptrs.front(), // &parameter_set_pointers 160 &nalu_data_ptrs.front(), // &parameter_set_pointers
141 &nalu_data_sizes.front(), // &parameter_set_sizes 161 &nalu_data_sizes.front(), // &parameter_set_sizes
142 kNALUHeaderLength, // nal_unit_header_length 162 kNALUHeaderLength, // nal_unit_header_length
143 format_.InitializeInto()); 163 format_.InitializeInto());
144 if (status) { 164 if (status) {
145 NOTIFY_STATUS("CMVideoFormatDescriptionCreateFromH264ParameterSets()", 165 NOTIFY_STATUS("CMVideoFormatDescriptionCreateFromH264ParameterSets()",
146 status); 166 status);
147 return false; 167 return false;
148 } 168 }
149 169
150 // If the session is compatible, there's nothing to do. 170 // Store the new configuration data.
171 CMVideoDimensions coded_dimensions =
172 CMVideoFormatDescriptionGetDimensions(format_);
173 coded_size_.SetSize(coded_dimensions.width, coded_dimensions.height);
174
175 // If the session is compatible, there's nothing else to do.
151 if (session_ && 176 if (session_ &&
152 VTDecompressionSessionCanAcceptFormatDescription(session_, format_)) { 177 VTDecompressionSessionCanAcceptFormatDescription(session_, format_)) {
153 return true; 178 return true;
154 } 179 }
155 180
156 // Prepare VideoToolbox configuration dictionaries. 181 // Prepare VideoToolbox configuration dictionaries.
157 base::ScopedCFTypeRef<CFMutableDictionaryRef> decoder_config( 182 base::ScopedCFTypeRef<CFMutableDictionaryRef> decoder_config(
158 CFDictionaryCreateMutable( 183 CFDictionaryCreateMutable(
159 kCFAllocatorDefault, 184 kCFAllocatorDefault,
160 1, // capacity 185 1, // capacity
161 &kCFTypeDictionaryKeyCallBacks, 186 &kCFTypeDictionaryKeyCallBacks,
162 &kCFTypeDictionaryValueCallBacks)); 187 &kCFTypeDictionaryValueCallBacks));
163 188
164 CFDictionarySetValue( 189 CFDictionarySetValue(
165 decoder_config, 190 decoder_config,
166 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder 191 // kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder
167 CFSTR("EnableHardwareAcceleratedVideoDecoder"), 192 CFSTR("EnableHardwareAcceleratedVideoDecoder"),
168 kCFBooleanTrue); 193 kCFBooleanTrue);
169 194
170 base::ScopedCFTypeRef<CFMutableDictionaryRef> image_config( 195 base::ScopedCFTypeRef<CFMutableDictionaryRef> image_config(
171 CFDictionaryCreateMutable( 196 CFDictionaryCreateMutable(
172 kCFAllocatorDefault, 197 kCFAllocatorDefault,
173 4, // capacity 198 4, // capacity
174 &kCFTypeDictionaryKeyCallBacks, 199 &kCFTypeDictionaryKeyCallBacks,
175 &kCFTypeDictionaryValueCallBacks)); 200 &kCFTypeDictionaryValueCallBacks));
176 201
177 CMVideoDimensions coded_dimensions =
178 CMVideoFormatDescriptionGetDimensions(format_);
179 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i) 202 #define CFINT(i) CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i)
180 // TODO(sandersd): RGBA option for 4:4:4 video. 203 // TODO(sandersd): RGBA option for 4:4:4 video.
181 int32_t pixel_format = kCVPixelFormatType_422YpCbCr8; 204 int32_t pixel_format = kCVPixelFormatType_422YpCbCr8;
182 base::ScopedCFTypeRef<CFNumberRef> cf_pixel_format(CFINT(pixel_format)); 205 base::ScopedCFTypeRef<CFNumberRef> cf_pixel_format(CFINT(pixel_format));
183 base::ScopedCFTypeRef<CFNumberRef> cf_width(CFINT(coded_dimensions.width)); 206 base::ScopedCFTypeRef<CFNumberRef> cf_width(CFINT(coded_dimensions.width));
184 base::ScopedCFTypeRef<CFNumberRef> cf_height(CFINT(coded_dimensions.height)); 207 base::ScopedCFTypeRef<CFNumberRef> cf_height(CFINT(coded_dimensions.height));
185 #undef CFINT 208 #undef CFINT
186 CFDictionarySetValue( 209 CFDictionarySetValue(
187 image_config, kCVPixelBufferPixelFormatTypeKey, cf_pixel_format); 210 image_config, kCVPixelBufferPixelFormatTypeKey, cf_pixel_format);
188 CFDictionarySetValue(image_config, kCVPixelBufferWidthKey, cf_width); 211 CFDictionarySetValue(image_config, kCVPixelBufferWidthKey, cf_width);
(...skipping 11 matching lines...) Expand all
200 &callback_, // output_callback 223 &callback_, // output_callback
201 session_.InitializeInto()); 224 session_.InitializeInto());
202 if (status) { 225 if (status) {
203 NOTIFY_STATUS("VTDecompressionSessionCreate()", status); 226 NOTIFY_STATUS("VTDecompressionSessionCreate()", status);
204 return false; 227 return false;
205 } 228 }
206 229
207 return true; 230 return true;
208 } 231 }
209 232
210 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) {
211 DCHECK(CalledOnValidThread());
212 // Not actually a requirement of the VDA API, but we're lazy and use negative
213 // values as flags internally. Revisit that if this actually happens.
214 if (bitstream.id() < 0) {
215 LOG(ERROR) << "Negative bitstream ID";
216 NotifyError(INVALID_ARGUMENT);
217 client_->NotifyEndOfBitstreamBuffer(bitstream.id());
218 return;
219 }
220 pending_bitstream_ids_.push(bitstream.id());
221 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind(
222 &VTVideoDecodeAccelerator::DecodeTask, base::Unretained(this),
223 bitstream));
224 }
225
226 void VTVideoDecodeAccelerator::DecodeTask( 233 void VTVideoDecodeAccelerator::DecodeTask(
227 const media::BitstreamBuffer& bitstream) { 234 const media::BitstreamBuffer& bitstream,
235 Frame* frame) {
228 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); 236 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread());
229 237
230 // Once we have a bitstream buffer, we must either decode it or drop it.
231 // This construct ensures that the buffer is always dropped unless we call
232 // drop_bitstream.Release().
233 base::ScopedClosureRunner drop_bitstream(base::Bind(
234 &VTVideoDecodeAccelerator::DropBitstream, base::Unretained(this),
235 bitstream.id()));
236
237 // Map the bitstream buffer. 238 // Map the bitstream buffer.
238 base::SharedMemory memory(bitstream.handle(), true); 239 base::SharedMemory memory(bitstream.handle(), true);
239 size_t size = bitstream.size(); 240 size_t size = bitstream.size();
240 if (!memory.Map(size)) { 241 if (!memory.Map(size)) {
241 LOG(ERROR) << "Failed to map bitstream buffer"; 242 DLOG(ERROR) << "Failed to map bitstream buffer";
242 NotifyError(PLATFORM_FAILURE); 243 NotifyError(PLATFORM_FAILURE);
243 return; 244 return;
244 } 245 }
245 const uint8_t* buf = static_cast<uint8_t*>(memory.memory()); 246 const uint8_t* buf = static_cast<uint8_t*>(memory.memory());
246 247
247 // NALUs are stored with Annex B format in the bitstream buffer (start codes), 248 // NALUs are stored with Annex B format in the bitstream buffer (start codes),
248 // but VideoToolbox expects AVCC/MPEG-4 format (length headers), so we must 249 // but VideoToolbox expects AVC format (length headers), so we must rewrite
249 // rewrite the data. 250 // the data.
250 // 251 //
251 // 1. Locate relevant NALUs and compute the size of the translated data. 252 // Locate relevant NALUs and compute the size of the rewritten data. Also
252 // Also record any parameter sets for VideoToolbox initialization. 253 // record any parameter sets for VideoToolbox initialization.
253 bool config_changed = false; 254 bool config_changed = false;
254 size_t data_size = 0; 255 size_t data_size = 0;
255 std::vector<media::H264NALU> nalus; 256 std::vector<media::H264NALU> nalus;
256 parser_.SetStream(buf, size); 257 parser_.SetStream(buf, size);
257 media::H264NALU nalu; 258 media::H264NALU nalu;
258 while (true) { 259 while (true) {
259 media::H264Parser::Result result = parser_.AdvanceToNextNALU(&nalu); 260 media::H264Parser::Result result = parser_.AdvanceToNextNALU(&nalu);
260 if (result == media::H264Parser::kEOStream) 261 if (result == media::H264Parser::kEOStream)
261 break; 262 break;
262 if (result != media::H264Parser::kOk) { 263 if (result != media::H264Parser::kOk) {
263 LOG(ERROR) << "Failed to find H.264 NALU"; 264 DLOG(ERROR) << "Failed to find H.264 NALU";
264 NotifyError(PLATFORM_FAILURE); 265 NotifyError(PLATFORM_FAILURE);
265 return; 266 return;
266 } 267 }
267 // TODO(sandersd): Strict ordering rules.
268 switch (nalu.nal_unit_type) { 268 switch (nalu.nal_unit_type) {
269 case media::H264NALU::kSPS: 269 case media::H264NALU::kSPS:
270 last_sps_.assign(nalu.data, nalu.data + nalu.size); 270 last_sps_.assign(nalu.data, nalu.data + nalu.size);
271 last_spsext_.clear(); 271 last_spsext_.clear();
272 config_changed = true; 272 config_changed = true;
273 break; 273 break;
274 case media::H264NALU::kSPSExt: 274 case media::H264NALU::kSPSExt:
275 // TODO(sandersd): Check that the previous NALU was an SPS. 275 // TODO(sandersd): Check that the previous NALU was an SPS.
276 last_spsext_.assign(nalu.data, nalu.data + nalu.size); 276 last_spsext_.assign(nalu.data, nalu.data + nalu.size);
277 config_changed = true; 277 config_changed = true;
278 break; 278 break;
279 case media::H264NALU::kPPS: 279 case media::H264NALU::kPPS:
280 last_pps_.assign(nalu.data, nalu.data + nalu.size); 280 last_pps_.assign(nalu.data, nalu.data + nalu.size);
281 config_changed = true; 281 config_changed = true;
282 break; 282 break;
283 case media::H264NALU::kSliceDataA:
284 case media::H264NALU::kSliceDataB:
285 case media::H264NALU::kSliceDataC:
286 DLOG(ERROR) << "Coded slide data partitions not implemented.";
287 NotifyError(PLATFORM_FAILURE);
288 return;
289 case media::H264NALU::kIDRSlice:
290 case media::H264NALU::kNonIDRSlice:
291 // TODO(sandersd): Compute pic_order_count.
283 default: 292 default:
284 nalus.push_back(nalu); 293 nalus.push_back(nalu);
285 data_size += kNALUHeaderLength + nalu.size; 294 data_size += kNALUHeaderLength + nalu.size;
286 break; 295 break;
287 } 296 }
288 } 297 }
289 298
290 // 2. Initialize VideoToolbox. 299 // Initialize VideoToolbox.
291 // TODO(sandersd): Check if the new configuration is identical before 300 // TODO(sandersd): Instead of assuming that the last SPS and PPS units are
292 // reconfiguring. 301 // always the correct ones, maintain a cache of recent SPS and PPS units and
302 // select from them using the slice header.
293 if (config_changed) { 303 if (config_changed) {
294 if (last_sps_.size() == 0 || last_pps_.size() == 0) { 304 if (last_sps_.size() == 0 || last_pps_.size() == 0) {
295 LOG(ERROR) << "Invalid configuration data"; 305 DLOG(ERROR) << "Invalid configuration data";
296 NotifyError(INVALID_ARGUMENT); 306 NotifyError(INVALID_ARGUMENT);
297 return; 307 return;
298 } 308 }
299 // TODO(sandersd): Check that the SPS and PPS IDs match. 309 if (!ConfigureDecoder())
300 std::vector<const uint8_t*> nalu_data_ptrs;
301 std::vector<size_t> nalu_data_sizes;
302 nalu_data_ptrs.push_back(&last_sps_.front());
303 nalu_data_sizes.push_back(last_sps_.size());
304 if (last_spsext_.size() != 0) {
305 nalu_data_ptrs.push_back(&last_spsext_.front());
306 nalu_data_sizes.push_back(last_spsext_.size());
307 }
308 nalu_data_ptrs.push_back(&last_pps_.front());
309 nalu_data_sizes.push_back(last_pps_.size());
310
311 // If ConfigureDecoder() fails, it already called NotifyError().
312 if (!ConfigureDecoder(nalu_data_ptrs, nalu_data_sizes))
313 return; 310 return;
314 } 311 }
315 312
316 // If there are no non-configuration units, immediately return an empty 313 // If there are no non-configuration units, drop the bitstream buffer by
317 // (ie. dropped) frame. It is an error to create a MemoryBlock with zero 314 // returning an empty frame.
318 // size. 315 if (!data_size) {
319 if (!data_size) 316 if (!FinishDelayedFrames())
317 return;
318 gpu_task_runner_->PostTask(FROM_HERE, base::Bind(
319 &VTVideoDecodeAccelerator::DecodeDone, weak_this_, frame));
320 return; 320 return;
321 }
321 322
322 // If the session is not configured, fail. 323 // If the session is not configured by this point, fail.
323 if (!session_) { 324 if (!session_) {
324 LOG(ERROR) << "Image slice without configuration data"; 325 DLOG(ERROR) << "Image slice without configuration";
325 NotifyError(INVALID_ARGUMENT); 326 NotifyError(INVALID_ARGUMENT);
326 return; 327 return;
327 } 328 }
328 329
329 // 3. Allocate a memory-backed CMBlockBuffer for the translated data. 330 // Create a memory-backed CMBlockBuffer for the translated data.
330 // TODO(sandersd): Check that the slice's PPS matches the current PPS. 331 // TODO(sandersd): Pool of memory blocks.
331 base::ScopedCFTypeRef<CMBlockBufferRef> data; 332 base::ScopedCFTypeRef<CMBlockBufferRef> data;
332 OSStatus status = CMBlockBufferCreateWithMemoryBlock( 333 OSStatus status = CMBlockBufferCreateWithMemoryBlock(
333 kCFAllocatorDefault, 334 kCFAllocatorDefault,
334 NULL, // &memory_block 335 NULL, // &memory_block
335 data_size, // block_length 336 data_size, // block_length
336 kCFAllocatorDefault, // block_allocator 337 kCFAllocatorDefault, // block_allocator
337 NULL, // &custom_block_source 338 NULL, // &custom_block_source
338 0, // offset_to_data 339 0, // offset_to_data
339 data_size, // data_length 340 data_size, // data_length
340 0, // flags 341 0, // flags
341 data.InitializeInto()); 342 data.InitializeInto());
342 if (status) { 343 if (status) {
343 NOTIFY_STATUS("CMBlockBufferCreateWithMemoryBlock()", status); 344 NOTIFY_STATUS("CMBlockBufferCreateWithMemoryBlock()", status);
344 return; 345 return;
345 } 346 }
346 347
347 // 4. Copy NALU data, inserting length headers. 348 // Copy NALU data into the CMBlockBuffer, inserting length headers.
348 size_t offset = 0; 349 size_t offset = 0;
349 for (size_t i = 0; i < nalus.size(); i++) { 350 for (size_t i = 0; i < nalus.size(); i++) {
350 media::H264NALU& nalu = nalus[i]; 351 media::H264NALU& nalu = nalus[i];
351 uint32_t header = base::HostToNet32(static_cast<uint32_t>(nalu.size)); 352 uint32_t header = base::HostToNet32(static_cast<uint32_t>(nalu.size));
352 status = CMBlockBufferReplaceDataBytes( 353 status = CMBlockBufferReplaceDataBytes(
353 &header, data, offset, kNALUHeaderLength); 354 &header, data, offset, kNALUHeaderLength);
354 if (status) { 355 if (status) {
355 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status); 356 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status);
356 return; 357 return;
357 } 358 }
358 offset += kNALUHeaderLength; 359 offset += kNALUHeaderLength;
359 status = CMBlockBufferReplaceDataBytes(nalu.data, data, offset, nalu.size); 360 status = CMBlockBufferReplaceDataBytes(nalu.data, data, offset, nalu.size);
360 if (status) { 361 if (status) {
361 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status); 362 NOTIFY_STATUS("CMBlockBufferReplaceDataBytes()", status);
362 return; 363 return;
363 } 364 }
364 offset += nalu.size; 365 offset += nalu.size;
365 } 366 }
366 367
367 // 5. Package the data for VideoToolbox and request decoding. 368 // Package the data in a CMSampleBuffer.
368 base::ScopedCFTypeRef<CMSampleBufferRef> frame; 369 base::ScopedCFTypeRef<CMSampleBufferRef> sample;
369 status = CMSampleBufferCreate( 370 status = CMSampleBufferCreate(
370 kCFAllocatorDefault, 371 kCFAllocatorDefault,
371 data, // data_buffer 372 data, // data_buffer
372 true, // data_ready 373 true, // data_ready
373 NULL, // make_data_ready_callback 374 NULL, // make_data_ready_callback
374 NULL, // make_data_ready_refcon 375 NULL, // make_data_ready_refcon
375 format_, // format_description 376 format_, // format_description
376 1, // num_samples 377 1, // num_samples
377 0, // num_sample_timing_entries 378 0, // num_sample_timing_entries
378 NULL, // &sample_timing_array 379 NULL, // &sample_timing_array
379 0, // num_sample_size_entries 380 0, // num_sample_size_entries
380 NULL, // &sample_size_array 381 NULL, // &sample_size_array
381 frame.InitializeInto()); 382 sample.InitializeInto());
382 if (status) { 383 if (status) {
383 NOTIFY_STATUS("CMSampleBufferCreate()", status); 384 NOTIFY_STATUS("CMSampleBufferCreate()", status);
384 return; 385 return;
385 } 386 }
386 387
388 // Update the frame data.
389 frame->coded_size = coded_size_;
390
391 // Send the frame for decoding.
387 // Asynchronous Decompression allows for parallel submission of frames 392 // Asynchronous Decompression allows for parallel submission of frames
388 // (without it, DecodeFrame() does not return until the frame has been 393 // (without it, DecodeFrame() does not return until the frame has been
389 // decoded). We don't enable Temporal Processing so that frames are always 394 // decoded). We don't enable Temporal Processing so that frames are always
390 // returned in decode order; this makes it easier to avoid deadlock. 395 // returned in decode order; this makes it easier to avoid deadlock.
391 VTDecodeFrameFlags decode_flags = 396 VTDecodeFrameFlags decode_flags =
392 kVTDecodeFrame_EnableAsynchronousDecompression; 397 kVTDecodeFrame_EnableAsynchronousDecompression;
393
394 intptr_t bitstream_id = bitstream.id();
395 status = VTDecompressionSessionDecodeFrame( 398 status = VTDecompressionSessionDecodeFrame(
396 session_, 399 session_,
397 frame, // sample_buffer 400 sample, // sample_buffer
398 decode_flags, // decode_flags 401 decode_flags, // decode_flags
399 reinterpret_cast<void*>(bitstream_id), // source_frame_refcon 402 reinterpret_cast<void*>(frame), // source_frame_refcon
400 NULL); // &info_flags_out 403 NULL); // &info_flags_out
401 if (status) { 404 if (status) {
402 NOTIFY_STATUS("VTDecompressionSessionDecodeFrame()", status); 405 NOTIFY_STATUS("VTDecompressionSessionDecodeFrame()", status);
403 return; 406 return;
404 } 407 }
405
406 // Now that the bitstream is decoding, don't drop it.
407 (void)drop_bitstream.Release();
408 } 408 }
409 409
410 // This method may be called on any VideoToolbox thread. 410 // This method may be called on any VideoToolbox thread.
411 void VTVideoDecodeAccelerator::Output( 411 void VTVideoDecodeAccelerator::Output(
412 int32_t bitstream_id, 412 void* source_frame_refcon,
413 OSStatus status, 413 OSStatus status,
414 CVImageBufferRef image_buffer) { 414 CVImageBufferRef image_buffer) {
415 if (status) { 415 if (status) {
416 // TODO(sandersd): Handle dropped frames.
417 NOTIFY_STATUS("Decoding", status); 416 NOTIFY_STATUS("Decoding", status);
418 image_buffer = NULL;
419 } else if (CFGetTypeID(image_buffer) != CVPixelBufferGetTypeID()) { 417 } else if (CFGetTypeID(image_buffer) != CVPixelBufferGetTypeID()) {
420 LOG(ERROR) << "Decoded frame is not a CVPixelBuffer"; 418 DLOG(ERROR) << "Decoded frame is not a CVPixelBuffer";
421 NotifyError(PLATFORM_FAILURE); 419 NotifyError(PLATFORM_FAILURE);
422 image_buffer = NULL;
423 } else { 420 } else {
424 CFRetain(image_buffer); 421 Frame* frame = reinterpret_cast<Frame*>(source_frame_refcon);
425 } 422 frame->image.reset(image_buffer, base::scoped_policy::RETAIN);
423 gpu_task_runner_->PostTask(FROM_HERE, base::Bind(
424 &VTVideoDecodeAccelerator::DecodeDone, weak_this_, frame));
425 }
426 }
427
428 void VTVideoDecodeAccelerator::DecodeDone(Frame* frame) {
429 DCHECK(gpu_thread_checker_.CalledOnValidThread());
430 DCHECK_EQ(frame->bitstream_id, pending_frames_.front()->bitstream_id);
431 Task task(TASK_FRAME);
432 task.frame = pending_frames_.front();
433 pending_frames_.pop();
434 pending_tasks_.push(task);
435 ProcessTasks();
436 }
437
438 void VTVideoDecodeAccelerator::FlushTask(TaskType type) {
439 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread());
440 FinishDelayedFrames();
441
442 // Always queue a task, even if FinishDelayedFrames() fails, so that
443 // destruction always completes.
426 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( 444 gpu_task_runner_->PostTask(FROM_HERE, base::Bind(
427 &VTVideoDecodeAccelerator::OutputTask, 445 &VTVideoDecodeAccelerator::FlushDone, weak_this_, type));
428 weak_this_factory_.GetWeakPtr(), 446 }
429 DecodedFrame(bitstream_id, image_buffer))); 447
430 } 448 void VTVideoDecodeAccelerator::FlushDone(TaskType type) {
431 449 DCHECK(gpu_thread_checker_.CalledOnValidThread());
432 void VTVideoDecodeAccelerator::OutputTask(DecodedFrame frame) { 450 pending_tasks_.push(Task(type));
433 DCHECK(CalledOnValidThread()); 451 ProcessTasks();
434 decoded_frames_.push(frame); 452 }
435 ProcessDecodedFrames(); 453
454 void VTVideoDecodeAccelerator::Decode(const media::BitstreamBuffer& bitstream) {
455 DCHECK(gpu_thread_checker_.CalledOnValidThread());
456 DCHECK_EQ(assigned_bitstream_ids_.count(bitstream.id()), 0u);
457 assigned_bitstream_ids_.insert(bitstream.id());
458 Frame* frame = new Frame(bitstream.id());
459 pending_frames_.push(make_linked_ptr(frame));
460 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind(
461 &VTVideoDecodeAccelerator::DecodeTask, base::Unretained(this),
462 bitstream, frame));
436 } 463 }
437 464
438 void VTVideoDecodeAccelerator::AssignPictureBuffers( 465 void VTVideoDecodeAccelerator::AssignPictureBuffers(
439 const std::vector<media::PictureBuffer>& pictures) { 466 const std::vector<media::PictureBuffer>& pictures) {
440 DCHECK(CalledOnValidThread()); 467 DCHECK(gpu_thread_checker_.CalledOnValidThread());
441 468
442 for (size_t i = 0; i < pictures.size(); i++) { 469 for (const media::PictureBuffer& picture : pictures) {
443 DCHECK(!texture_ids_.count(pictures[i].id())); 470 DCHECK(!texture_ids_.count(picture.id()));
444 assigned_picture_ids_.insert(pictures[i].id()); 471 assigned_picture_ids_.insert(picture.id());
445 available_picture_ids_.push_back(pictures[i].id()); 472 available_picture_ids_.push_back(picture.id());
446 texture_ids_[pictures[i].id()] = pictures[i].texture_id(); 473 texture_ids_[picture.id()] = picture.texture_id();
447 } 474 }
448 475
449 // Pictures are not marked as uncleared until after this method returns, and 476 // Pictures are not marked as uncleared until after this method returns, and
450 // they will be broken if they are used before that happens. So, schedule 477 // they will be broken if they are used before that happens. So, schedule
451 // future work after that happens. 478 // future work after that happens.
452 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( 479 gpu_task_runner_->PostTask(FROM_HERE, base::Bind(
453 &VTVideoDecodeAccelerator::ProcessDecodedFrames, 480 &VTVideoDecodeAccelerator::ProcessTasks, weak_this_));
454 weak_this_factory_.GetWeakPtr()));
455 } 481 }
456 482
457 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) { 483 void VTVideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_id) {
458 DCHECK(CalledOnValidThread()); 484 DCHECK(gpu_thread_checker_.CalledOnValidThread());
459 DCHECK_EQ(CFGetRetainCount(picture_bindings_[picture_id]), 1); 485 DCHECK_EQ(CFGetRetainCount(picture_bindings_[picture_id]), 1);
460 picture_bindings_.erase(picture_id); 486 picture_bindings_.erase(picture_id);
461 // Don't put the picture back in the available list if has been dismissed.
462 if (assigned_picture_ids_.count(picture_id) != 0) { 487 if (assigned_picture_ids_.count(picture_id) != 0) {
463 available_picture_ids_.push_back(picture_id); 488 available_picture_ids_.push_back(picture_id);
464 ProcessDecodedFrames(); 489 ProcessTasks();
465 } 490 } else {
466 } 491 client_->DismissPictureBuffer(picture_id);
467 492 }
468 void VTVideoDecodeAccelerator::CompleteAction(Action action) { 493 }
469 DCHECK(CalledOnValidThread()); 494
470 495 void VTVideoDecodeAccelerator::ProcessTasks() {
471 switch (action) { 496 DCHECK(gpu_thread_checker_.CalledOnValidThread());
472 case ACTION_FLUSH: 497
498 while (!pending_tasks_.empty()) {
499 const Task& task = pending_tasks_.front();
500
501 switch (state_) {
502 case STATE_DECODING:
503 if (!ProcessTask(task))
504 return;
505 pending_tasks_.pop();
506 break;
507
508 case STATE_ERROR:
509 // Do nothing until Destroy() is called.
510 return;
511
512 case STATE_DESTROYING:
513 // Discard tasks until destruction is complete.
514 if (task.type == TASK_DESTROY) {
515 delete this;
516 return;
517 }
518 pending_tasks_.pop();
519 break;
520 }
521 }
522 }
523
524 bool VTVideoDecodeAccelerator::ProcessTask(const Task& task) {
525 DCHECK(gpu_thread_checker_.CalledOnValidThread());
526 DCHECK_EQ(state_, STATE_DECODING);
527
528 switch (task.type) {
529 case TASK_FRAME:
530 return ProcessFrame(*task.frame);
531
532 case TASK_FLUSH:
533 DCHECK_EQ(task.type, pending_flush_tasks_.front());
534 pending_flush_tasks_.pop();
473 client_->NotifyFlushDone(); 535 client_->NotifyFlushDone();
474 break; 536 return true;
475 case ACTION_RESET: 537
538 case TASK_RESET:
539 DCHECK_EQ(task.type, pending_flush_tasks_.front());
540 pending_flush_tasks_.pop();
476 client_->NotifyResetDone(); 541 client_->NotifyResetDone();
477 break; 542 return true;
478 case ACTION_DESTROY: 543
479 delete this; 544 case TASK_DESTROY:
480 break; 545 NOTREACHED() << "Can't destroy while in STATE_DECODING.";
481 } 546 NotifyError(ILLEGAL_STATE);
482 } 547 return false;
483 548 }
484 void VTVideoDecodeAccelerator::CompleteActions(int32_t bitstream_id) { 549 }
485 DCHECK(CalledOnValidThread()); 550
486 while (!pending_actions_.empty() && 551 bool VTVideoDecodeAccelerator::ProcessFrame(const Frame& frame) {
487 pending_actions_.front().bitstream_id == bitstream_id) { 552 DCHECK(gpu_thread_checker_.CalledOnValidThread());
488 CompleteAction(pending_actions_.front().action); 553 DCHECK_EQ(state_, STATE_DECODING);
489 pending_actions_.pop(); 554 // If the next pending flush is for a reset, then the frame will be dropped.
490 } 555 bool resetting = !pending_flush_tasks_.empty() &&
491 } 556 pending_flush_tasks_.front() == TASK_RESET;
492 557 if (!resetting && frame.image.get()) {
493 void VTVideoDecodeAccelerator::ProcessDecodedFrames() { 558 // If the |coded_size| has changed, request new picture buffers and then
494 DCHECK(CalledOnValidThread()); 559 // wait for them.
495 560 // TODO(sandersd): If GpuVideoDecoder didn't specifically check the size of
496 while (!decoded_frames_.empty()) { 561 // textures, this would be unnecessary, as the size is actually a property
497 if (pending_actions_.empty()) { 562 // of the texture binding, not the texture. We rebind every frame, so the
498 // No pending actions; send frames normally. 563 // size passed to ProvidePictureBuffers() is meaningless.
499 if (!has_error_) 564 if (picture_size_ != frame.coded_size) {
500 SendPictures(pending_bitstream_ids_.back()); 565 // Dismiss current pictures.
501 return; 566 for (int32_t picture_id : assigned_picture_ids_)
567 client_->DismissPictureBuffer(picture_id);
568 assigned_picture_ids_.clear();
569 available_picture_ids_.clear();
570
571 // Request new pictures.
572 picture_size_ = frame.coded_size;
573 client_->ProvidePictureBuffers(
574 kNumPictureBuffers, coded_size_, GL_TEXTURE_RECTANGLE_ARB);
575 return false;
502 } 576 }
503 577 if (!SendFrame(frame))
504 int32_t next_action_bitstream_id = pending_actions_.front().bitstream_id; 578 return false;
505 int32_t last_sent_bitstream_id = -1; 579 }
506 switch (pending_actions_.front().action) { 580 assigned_bitstream_ids_.erase(frame.bitstream_id);
507 case ACTION_FLUSH: 581 client_->NotifyEndOfBitstreamBuffer(frame.bitstream_id);
508 // Send frames normally. 582 return true;
509 if (has_error_) 583 }
510 return; 584
511 last_sent_bitstream_id = SendPictures(next_action_bitstream_id); 585 bool VTVideoDecodeAccelerator::SendFrame(const Frame& frame) {
512 break; 586 DCHECK(gpu_thread_checker_.CalledOnValidThread());
513 587 DCHECK_EQ(state_, STATE_DECODING);
514 case ACTION_RESET: 588
515 // Drop decoded frames.
516 if (has_error_)
517 return;
518 while (!decoded_frames_.empty() &&
519 last_sent_bitstream_id != next_action_bitstream_id) {
520 last_sent_bitstream_id = decoded_frames_.front().bitstream_id;
521 decoded_frames_.pop();
522 DCHECK_EQ(pending_bitstream_ids_.front(), last_sent_bitstream_id);
523 pending_bitstream_ids_.pop();
524 client_->NotifyEndOfBitstreamBuffer(last_sent_bitstream_id);
525 }
526 break;
527
528 case ACTION_DESTROY:
529 // Drop decoded frames, without bookkeeping.
530 while (!decoded_frames_.empty()) {
531 last_sent_bitstream_id = decoded_frames_.front().bitstream_id;
532 decoded_frames_.pop();
533 }
534
535 // Handle completing the action specially, as it is important not to
536 // access |this| after calling CompleteAction().
537 if (last_sent_bitstream_id == next_action_bitstream_id)
538 CompleteAction(ACTION_DESTROY);
539
540 // Either |this| was deleted or no more progress can be made.
541 return;
542 }
543
544 // If we ran out of buffers (or pictures), no more progress can be made
545 // until more frames are decoded.
546 if (last_sent_bitstream_id != next_action_bitstream_id)
547 return;
548
549 // Complete all actions pending for this |bitstream_id|, then loop to see
550 // if progress can be made on the next action.
551 CompleteActions(next_action_bitstream_id);
552 }
553 }
554
555 int32_t VTVideoDecodeAccelerator::ProcessDroppedFrames(
556 int32_t last_sent_bitstream_id,
557 int32_t up_to_bitstream_id) {
558 DCHECK(CalledOnValidThread());
559 // Drop frames as long as there is a frame, we have not reached the next
560 // action, and the next frame has no image.
561 while (!decoded_frames_.empty() &&
562 last_sent_bitstream_id != up_to_bitstream_id &&
563 decoded_frames_.front().image_buffer.get() == NULL) {
564 const DecodedFrame& frame = decoded_frames_.front();
565 DCHECK_EQ(pending_bitstream_ids_.front(), frame.bitstream_id);
566 client_->NotifyEndOfBitstreamBuffer(frame.bitstream_id);
567 last_sent_bitstream_id = frame.bitstream_id;
568 decoded_frames_.pop();
569 pending_bitstream_ids_.pop();
570 }
571 return last_sent_bitstream_id;
572 }
573
574 // TODO(sandersd): If GpuVideoDecoder didn't specifically check the size of
575 // textures, this would be unnecessary, as the size is actually a property of
576 // the texture binding, not the texture. We rebind every frame, so the size
577 // passed to ProvidePictureBuffers() is meaningless.
578 void VTVideoDecodeAccelerator::ProcessSizeChangeIfNeeded() {
579 DCHECK(CalledOnValidThread());
580 DCHECK(!decoded_frames_.empty());
581
582 // Find the size of the next image.
583 const DecodedFrame& frame = decoded_frames_.front();
584 CVImageBufferRef image_buffer = frame.image_buffer.get();
585 size_t width = CVPixelBufferGetWidth(image_buffer);
586 size_t height = CVPixelBufferGetHeight(image_buffer);
587 gfx::Size image_size(width, height);
588
589 if (picture_size_ != image_size) {
590 // Dismiss all assigned picture buffers.
591 for (int32_t picture_id : assigned_picture_ids_)
592 client_->DismissPictureBuffer(picture_id);
593 assigned_picture_ids_.clear();
594 available_picture_ids_.clear();
595
596 // Request new pictures.
597 client_->ProvidePictureBuffers(
598 kNumPictureBuffers, image_size, GL_TEXTURE_RECTANGLE_ARB);
599 picture_size_ = image_size;
600 }
601 }
602
603 int32_t VTVideoDecodeAccelerator::SendPictures(int32_t up_to_bitstream_id) {
604 DCHECK(CalledOnValidThread());
605 DCHECK(!decoded_frames_.empty());
606
607 // TODO(sandersd): Store the actual last sent bitstream ID?
608 int32_t last_sent_bitstream_id = -1;
609
610 last_sent_bitstream_id =
611 ProcessDroppedFrames(last_sent_bitstream_id, up_to_bitstream_id);
612 if (last_sent_bitstream_id == up_to_bitstream_id || decoded_frames_.empty())
613 return last_sent_bitstream_id;
614
615 ProcessSizeChangeIfNeeded();
616 if (available_picture_ids_.empty()) 589 if (available_picture_ids_.empty())
617 return last_sent_bitstream_id; 590 return false;
591
592 int32_t picture_id = available_picture_ids_.back();
593 IOSurfaceRef surface = CVPixelBufferGetIOSurface(frame.image.get());
618 594
619 if (!make_context_current_.Run()) { 595 if (!make_context_current_.Run()) {
620 LOG(ERROR) << "Failed to make GL context current"; 596 DLOG(ERROR) << "Failed to make GL context current";
621 NotifyError(PLATFORM_FAILURE); 597 NotifyError(PLATFORM_FAILURE);
622 return last_sent_bitstream_id; 598 return false;
623 } 599 }
624 600
625 glEnable(GL_TEXTURE_RECTANGLE_ARB); 601 glEnable(GL_TEXTURE_RECTANGLE_ARB);
626 while (!available_picture_ids_.empty() && !has_error_) { 602 gfx::ScopedTextureBinder
627 DCHECK_NE(last_sent_bitstream_id, up_to_bitstream_id); 603 texture_binder(GL_TEXTURE_RECTANGLE_ARB, texture_ids_[picture_id]);
628 DCHECK(!decoded_frames_.empty()); 604 CGLError status = CGLTexImageIOSurface2D(
629 605 cgl_context_, // ctx
630 // We don't pop |frame| or |picture_id| until they are consumed, which may 606 GL_TEXTURE_RECTANGLE_ARB, // target
631 // not happen if an error occurs. Conveniently, this also removes some 607 GL_RGB, // internal_format
632 // refcounting. 608 frame.coded_size.width(), // width
633 const DecodedFrame& frame = decoded_frames_.front(); 609 frame.coded_size.height(), // height
634 DCHECK_EQ(pending_bitstream_ids_.front(), frame.bitstream_id); 610 GL_YCBCR_422_APPLE, // format
635 int32_t picture_id = available_picture_ids_.back(); 611 GL_UNSIGNED_SHORT_8_8_APPLE, // type
636 612 surface, // io_surface
637 CVImageBufferRef image_buffer = frame.image_buffer.get(); 613 0); // plane
638 IOSurfaceRef surface = CVPixelBufferGetIOSurface(image_buffer); 614 if (status != kCGLNoError) {
639 615 NOTIFY_STATUS("CGLTexImageIOSurface2D()", status);
640 gfx::ScopedTextureBinder 616 return false;
641 texture_binder(GL_TEXTURE_RECTANGLE_ARB, texture_ids_[picture_id]);
642 CGLError status = CGLTexImageIOSurface2D(
643 cgl_context_, // ctx
644 GL_TEXTURE_RECTANGLE_ARB, // target
645 GL_RGB, // internal_format
646 picture_size_.width(), // width
647 picture_size_.height(), // height
648 GL_YCBCR_422_APPLE, // format
649 GL_UNSIGNED_SHORT_8_8_APPLE, // type
650 surface, // io_surface
651 0); // plane
652 if (status != kCGLNoError) {
653 NOTIFY_STATUS("CGLTexImageIOSurface2D()", status);
654 break;
655 }
656
657 picture_bindings_[picture_id] = frame.image_buffer;
658 client_->PictureReady(media::Picture(
659 picture_id, frame.bitstream_id, gfx::Rect(picture_size_)));
660 available_picture_ids_.pop_back();
661 client_->NotifyEndOfBitstreamBuffer(frame.bitstream_id);
662 last_sent_bitstream_id = frame.bitstream_id;
663 decoded_frames_.pop();
664 pending_bitstream_ids_.pop();
665
666 last_sent_bitstream_id =
667 ProcessDroppedFrames(last_sent_bitstream_id, up_to_bitstream_id);
668 if (last_sent_bitstream_id == up_to_bitstream_id || decoded_frames_.empty())
669 break;
670
671 ProcessSizeChangeIfNeeded();
672 } 617 }
673 glDisable(GL_TEXTURE_RECTANGLE_ARB); 618 glDisable(GL_TEXTURE_RECTANGLE_ARB);
674 619
675 return last_sent_bitstream_id; 620 available_picture_ids_.pop_back();
676 } 621 picture_bindings_[picture_id] = frame.image;
677 622 client_->PictureReady(media::Picture(
678 void VTVideoDecodeAccelerator::FlushTask() { 623 picture_id, frame.bitstream_id, gfx::Rect(frame.coded_size)));
679 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); 624 return true;
680 OSStatus status = VTDecompressionSessionFinishDelayedFrames(session_);
681 if (status)
682 NOTIFY_STATUS("VTDecompressionSessionFinishDelayedFrames()", status);
683 }
684
685 void VTVideoDecodeAccelerator::QueueAction(Action action) {
686 DCHECK(CalledOnValidThread());
687 if (pending_bitstream_ids_.empty()) {
688 // If there are no pending frames, all actions complete immediately.
689 CompleteAction(action);
690 } else {
691 // Otherwise, queue the action.
692 pending_actions_.push(PendingAction(action, pending_bitstream_ids_.back()));
693
694 // Request a flush to make sure the action will eventually complete.
695 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind(
696 &VTVideoDecodeAccelerator::FlushTask, base::Unretained(this)));
697
698 // See if we can make progress now that there is a new pending action.
699 ProcessDecodedFrames();
700 }
701 } 625 }
702 626
703 void VTVideoDecodeAccelerator::NotifyError(Error error) { 627 void VTVideoDecodeAccelerator::NotifyError(Error error) {
704 if (!CalledOnValidThread()) { 628 if (!gpu_thread_checker_.CalledOnValidThread()) {
705 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( 629 gpu_task_runner_->PostTask(FROM_HERE, base::Bind(
706 &VTVideoDecodeAccelerator::NotifyError, 630 &VTVideoDecodeAccelerator::NotifyError, weak_this_, error));
707 weak_this_factory_.GetWeakPtr(), 631 } else if (state_ == STATE_DECODING) {
708 error)); 632 state_ = STATE_ERROR;
633 client_->NotifyError(error);
634 }
635 }
636
637 void VTVideoDecodeAccelerator::QueueFlush(TaskType type) {
638 DCHECK(gpu_thread_checker_.CalledOnValidThread());
639 pending_flush_tasks_.push(type);
640 decoder_thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind(
641 &VTVideoDecodeAccelerator::FlushTask, base::Unretained(this),
642 type));
643
644 // If this is a new flush request, see if we can make progress.
645 if (pending_flush_tasks_.size() == 1)
646 ProcessTasks();
647 }
648
649 void VTVideoDecodeAccelerator::Flush() {
650 DCHECK(gpu_thread_checker_.CalledOnValidThread());
651 QueueFlush(TASK_FLUSH);
652 }
653
654 void VTVideoDecodeAccelerator::Reset() {
655 DCHECK(gpu_thread_checker_.CalledOnValidThread());
656 QueueFlush(TASK_RESET);
657 }
658
659 void VTVideoDecodeAccelerator::Destroy() {
660 DCHECK(gpu_thread_checker_.CalledOnValidThread());
661
662 // In a forceful shutdown, the decoder thread may be dead already.
663 if (!decoder_thread_.IsRunning()) {
664 delete this;
709 return; 665 return;
710 } 666 }
711 has_error_ = true; 667
712 client_->NotifyError(error); 668 // For a graceful shutdown, return assigned buffers and flush before
713 } 669 // destructing |this|.
714 670 for (int32_t bitstream_id : assigned_bitstream_ids_)
715 void VTVideoDecodeAccelerator::DropBitstream(int32_t bitstream_id) { 671 client_->NotifyEndOfBitstreamBuffer(bitstream_id);
716 DCHECK(decoder_thread_.message_loop_proxy()->BelongsToCurrentThread()); 672 assigned_bitstream_ids_.clear();
717 gpu_task_runner_->PostTask(FROM_HERE, base::Bind( 673 state_ = STATE_DESTROYING;
718 &VTVideoDecodeAccelerator::OutputTask, 674 QueueFlush(TASK_DESTROY);
719 weak_this_factory_.GetWeakPtr(),
720 DecodedFrame(bitstream_id, NULL)));
721 }
722
723 void VTVideoDecodeAccelerator::Flush() {
724 DCHECK(CalledOnValidThread());
725 QueueAction(ACTION_FLUSH);
726 }
727
728 void VTVideoDecodeAccelerator::Reset() {
729 DCHECK(CalledOnValidThread());
730 QueueAction(ACTION_RESET);
731 }
732
733 void VTVideoDecodeAccelerator::Destroy() {
734 DCHECK(CalledOnValidThread());
735 // Drop any other pending actions.
736 while (!pending_actions_.empty())
737 pending_actions_.pop();
738 // Return all bitstream buffers.
739 while (!pending_bitstream_ids_.empty()) {
740 client_->NotifyEndOfBitstreamBuffer(pending_bitstream_ids_.front());
741 pending_bitstream_ids_.pop();
742 }
743 QueueAction(ACTION_DESTROY);
744 } 675 }
745 676
746 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() { 677 bool VTVideoDecodeAccelerator::CanDecodeOnIOThread() {
747 return false; 678 return false;
748 } 679 }
749 680
750 } // namespace content 681 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/media/vt_video_decode_accelerator.h ('k') | media/filters/gpu_video_decoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698