Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / filters / gpu_video_decoder.cc
blobd059c0be894fc9594c161bc94d5f3ff1cf8531e7
1 // Copyright (c) 2012 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 #include "media/filters/gpu_video_decoder.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h"
12 #include "base/cpu.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/stl_util.h"
16 #include "base/task_runner_util.h"
17 #include "gpu/command_buffer/common/mailbox_holder.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/media_switches.h"
21 #include "media/base/pipeline.h"
22 #include "media/base/pipeline_status.h"
23 #include "media/base/video_decoder_config.h"
24 #include "media/renderers/gpu_video_accelerator_factories.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
27 namespace media {
29 const char GpuVideoDecoder::kDecoderName[] = "GpuVideoDecoder";
31 // Maximum number of concurrent VDA::Decode() operations GVD will maintain.
32 // Higher values allow better pipelining in the GPU, but also require more
33 // resources.
34 enum { kMaxInFlightDecodes = 4 };
36 // Size of shared-memory segments we allocate. Since we reuse them we let them
37 // be on the beefy side.
38 static const size_t kSharedMemorySegmentBytes = 100 << 10;
40 GpuVideoDecoder::SHMBuffer::SHMBuffer(scoped_ptr<base::SharedMemory> m,
41 size_t s)
42 : shm(m.Pass()), size(s) {
45 GpuVideoDecoder::SHMBuffer::~SHMBuffer() {}
47 GpuVideoDecoder::PendingDecoderBuffer::PendingDecoderBuffer(
48 SHMBuffer* s,
49 const scoped_refptr<DecoderBuffer>& b,
50 const DecodeCB& done_cb)
51 : shm_buffer(s), buffer(b), done_cb(done_cb) {
54 GpuVideoDecoder::PendingDecoderBuffer::~PendingDecoderBuffer() {}
56 GpuVideoDecoder::BufferData::BufferData(
57 int32 bbid, base::TimeDelta ts, const gfx::Rect& vr, const gfx::Size& ns)
58 : bitstream_buffer_id(bbid), timestamp(ts), visible_rect(vr),
59 natural_size(ns) {
62 GpuVideoDecoder::BufferData::~BufferData() {}
64 GpuVideoDecoder::GpuVideoDecoder(
65 const scoped_refptr<GpuVideoAcceleratorFactories>& factories)
66 : needs_bitstream_conversion_(false),
67 factories_(factories),
68 state_(kNormal),
69 decoder_texture_target_(0),
70 next_picture_buffer_id_(0),
71 next_bitstream_buffer_id_(0),
72 available_pictures_(0),
73 weak_factory_(this) {
74 DCHECK(factories_.get());
77 void GpuVideoDecoder::Reset(const base::Closure& closure) {
78 DVLOG(3) << "Reset()";
79 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
81 if (state_ == kDrainingDecoder) {
82 base::MessageLoop::current()->PostTask(
83 FROM_HERE,
84 base::Bind(
85 &GpuVideoDecoder::Reset, weak_factory_.GetWeakPtr(), closure));
86 return;
89 if (!vda_) {
90 base::MessageLoop::current()->PostTask(FROM_HERE, closure);
91 return;
94 DCHECK(pending_reset_cb_.is_null());
95 pending_reset_cb_ = BindToCurrentLoop(closure);
97 vda_->Reset();
100 static bool IsCodedSizeSupported(const gfx::Size& coded_size,
101 VideoCodecProfile profile) {
102 #if defined(OS_WIN)
103 // Windows Media Foundation H.264 decoding does not support decoding videos
104 // with any dimension smaller than 48 pixels:
105 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd797815
106 if (coded_size.width() < 48 || coded_size.height() < 48)
107 return false;
108 #endif
110 // Only non-Windows, Ivy Bridge+ platforms can support more than 1920x1080.
111 // We test against 1088 to account for 16x16 macroblocks.
112 if (coded_size.width() <= 1920 && coded_size.height() <= 1088)
113 return true;
115 // NOTE: additional autodetection logic may require updating input buffer size
116 // selection in platform-specific implementations, such as
117 // V4L2VideoDecodeAccelerator.
118 base::CPU cpu;
119 bool hw_large_video_support =
120 base::CommandLine::ForCurrentProcess()->HasSwitch(
121 switches::kIgnoreResolutionLimitsForAcceleratedVideoDecode) ||
122 ((cpu.vendor_name() == "GenuineIntel") && cpu.model() >= 55 &&
123 // TODO(posciak, henryhsu): Remove this once we can query in runtime.
124 profile >= H264PROFILE_MIN && profile <= H264PROFILE_MAX);
125 bool os_large_video_support = true;
126 #if defined(OS_WIN)
127 os_large_video_support = false;
128 #endif
129 return os_large_video_support && hw_large_video_support;
132 // Report |status| to UMA and run |cb| with it. This is super-specific to the
133 // UMA stat reported because the UMA_HISTOGRAM_ENUMERATION API requires a
134 // callsite to always be called with the same stat name (can't parameterize it).
135 static void ReportGpuVideoDecoderInitializeStatusToUMAAndRunCB(
136 const PipelineStatusCB& cb,
137 PipelineStatus status) {
138 UMA_HISTOGRAM_ENUMERATION(
139 "Media.GpuVideoDecoderInitializeStatus", status, PIPELINE_STATUS_MAX + 1);
140 cb.Run(status);
143 std::string GpuVideoDecoder::GetDisplayName() const {
144 return kDecoderName;
147 void GpuVideoDecoder::Initialize(const VideoDecoderConfig& config,
148 bool /* low_delay */,
149 const PipelineStatusCB& orig_status_cb,
150 const OutputCB& output_cb) {
151 DVLOG(3) << "Initialize()";
152 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
153 DCHECK(config.IsValidConfig());
154 DCHECK(!config.is_encrypted());
156 PipelineStatusCB status_cb =
157 base::Bind(&ReportGpuVideoDecoderInitializeStatusToUMAAndRunCB,
158 BindToCurrentLoop(orig_status_cb));
160 bool previously_initialized = config_.IsValidConfig();
161 DVLOG(1) << "(Re)initializing GVD with config: "
162 << config.AsHumanReadableString();
164 // TODO(posciak): destroy and create a new VDA on codec/profile change
165 // (http://crbug.com/260224).
166 if (previously_initialized && (config_.profile() != config.profile())) {
167 DVLOG(1) << "Codec or profile changed, cannot reinitialize.";
168 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
169 return;
172 if (!IsCodedSizeSupported(config.coded_size(), config.profile())) {
173 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
174 return;
177 config_ = config;
178 needs_bitstream_conversion_ = (config.codec() == kCodecH264);
179 output_cb_ = BindToCurrentLoop(output_cb);
181 if (previously_initialized) {
182 // Reinitialization with a different config (but same codec and profile).
183 // VDA should handle it by detecting this in-stream by itself,
184 // no need to notify it.
185 status_cb.Run(PIPELINE_OK);
186 return;
189 vda_ = factories_->CreateVideoDecodeAccelerator().Pass();
190 if (!vda_ || !vda_->Initialize(config.profile(), this)) {
191 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED);
192 return;
195 DVLOG(3) << "GpuVideoDecoder::Initialize() succeeded.";
196 status_cb.Run(PIPELINE_OK);
199 void GpuVideoDecoder::DestroyPictureBuffers(PictureBufferMap* buffers) {
200 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
201 for (PictureBufferMap::iterator it = buffers->begin(); it != buffers->end();
202 ++it) {
203 factories_->DeleteTexture(it->second.texture_id());
206 buffers->clear();
209 void GpuVideoDecoder::DestroyVDA() {
210 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
212 vda_.reset();
214 // Not destroying PictureBuffers in |picture_buffers_at_display_| yet, since
215 // their textures may still be in use by the user of this GpuVideoDecoder.
216 for (PictureBufferTextureMap::iterator it =
217 picture_buffers_at_display_.begin();
218 it != picture_buffers_at_display_.end();
219 ++it) {
220 assigned_picture_buffers_.erase(it->first);
222 DestroyPictureBuffers(&assigned_picture_buffers_);
225 void GpuVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
226 const DecodeCB& decode_cb) {
227 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
228 DCHECK(pending_reset_cb_.is_null());
230 DecodeCB bound_decode_cb = BindToCurrentLoop(decode_cb);
232 if (state_ == kError || !vda_) {
233 bound_decode_cb.Run(kDecodeError);
234 return;
237 switch (state_) {
238 case kDecoderDrained:
239 state_ = kNormal;
240 // Fall-through.
241 case kNormal:
242 break;
243 case kDrainingDecoder:
244 case kError:
245 NOTREACHED();
246 return;
249 DCHECK_EQ(state_, kNormal);
251 if (buffer->end_of_stream()) {
252 state_ = kDrainingDecoder;
253 eos_decode_cb_ = bound_decode_cb;
254 vda_->Flush();
255 return;
258 size_t size = buffer->data_size();
259 scoped_ptr<SHMBuffer> shm_buffer = GetSHM(size);
260 if (!shm_buffer) {
261 bound_decode_cb.Run(kDecodeError);
262 return;
265 memcpy(shm_buffer->shm->memory(), buffer->data(), size);
266 BitstreamBuffer bitstream_buffer(
267 next_bitstream_buffer_id_, shm_buffer->shm->handle(), size);
268 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
269 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
270 DCHECK(!ContainsKey(bitstream_buffers_in_decoder_, bitstream_buffer.id()));
271 bitstream_buffers_in_decoder_.insert(std::make_pair(
272 bitstream_buffer.id(),
273 PendingDecoderBuffer(shm_buffer.release(), buffer, decode_cb)));
274 DCHECK_LE(static_cast<int>(bitstream_buffers_in_decoder_.size()),
275 kMaxInFlightDecodes);
276 RecordBufferData(bitstream_buffer, *buffer.get());
278 vda_->Decode(bitstream_buffer);
281 void GpuVideoDecoder::RecordBufferData(const BitstreamBuffer& bitstream_buffer,
282 const DecoderBuffer& buffer) {
283 input_buffer_data_.push_front(BufferData(bitstream_buffer.id(),
284 buffer.timestamp(),
285 config_.visible_rect(),
286 config_.natural_size()));
287 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but
288 // that's too small for some pathological B-frame test videos. The cost of
289 // using too-high a value is low (192 bits per extra slot).
290 static const size_t kMaxInputBufferDataSize = 128;
291 // Pop from the back of the list, because that's the oldest and least likely
292 // to be useful in the future data.
293 if (input_buffer_data_.size() > kMaxInputBufferDataSize)
294 input_buffer_data_.pop_back();
297 void GpuVideoDecoder::GetBufferData(int32 id, base::TimeDelta* timestamp,
298 gfx::Rect* visible_rect,
299 gfx::Size* natural_size) {
300 for (std::list<BufferData>::const_iterator it =
301 input_buffer_data_.begin(); it != input_buffer_data_.end();
302 ++it) {
303 if (it->bitstream_buffer_id != id)
304 continue;
305 *timestamp = it->timestamp;
306 *visible_rect = it->visible_rect;
307 *natural_size = it->natural_size;
308 return;
310 NOTREACHED() << "Missing bitstreambuffer id: " << id;
313 bool GpuVideoDecoder::NeedsBitstreamConversion() const {
314 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
315 return needs_bitstream_conversion_;
318 bool GpuVideoDecoder::CanReadWithoutStalling() const {
319 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
320 return
321 next_picture_buffer_id_ == 0 || // Decode() will ProvidePictureBuffers().
322 available_pictures_ > 0;
325 int GpuVideoDecoder::GetMaxDecodeRequests() const {
326 return kMaxInFlightDecodes;
329 void GpuVideoDecoder::ProvidePictureBuffers(uint32 count,
330 const gfx::Size& size,
331 uint32 texture_target) {
332 DVLOG(3) << "ProvidePictureBuffers(" << count << ", "
333 << size.width() << "x" << size.height() << ")";
334 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
336 std::vector<uint32> texture_ids;
337 std::vector<gpu::Mailbox> texture_mailboxes;
338 decoder_texture_target_ = texture_target;
339 if (!factories_->CreateTextures(count,
340 size,
341 &texture_ids,
342 &texture_mailboxes,
343 decoder_texture_target_)) {
344 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE);
345 return;
347 DCHECK_EQ(count, texture_ids.size());
348 DCHECK_EQ(count, texture_mailboxes.size());
350 if (!vda_)
351 return;
353 std::vector<PictureBuffer> picture_buffers;
354 for (size_t i = 0; i < texture_ids.size(); ++i) {
355 picture_buffers.push_back(PictureBuffer(
356 next_picture_buffer_id_++, size, texture_ids[i], texture_mailboxes[i]));
357 bool inserted = assigned_picture_buffers_.insert(std::make_pair(
358 picture_buffers.back().id(), picture_buffers.back())).second;
359 DCHECK(inserted);
362 available_pictures_ += count;
364 vda_->AssignPictureBuffers(picture_buffers);
367 void GpuVideoDecoder::DismissPictureBuffer(int32 id) {
368 DVLOG(3) << "DismissPictureBuffer(" << id << ")";
369 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
371 PictureBufferMap::iterator it = assigned_picture_buffers_.find(id);
372 if (it == assigned_picture_buffers_.end()) {
373 NOTREACHED() << "Missing picture buffer: " << id;
374 return;
377 PictureBuffer buffer_to_dismiss = it->second;
378 assigned_picture_buffers_.erase(it);
380 if (!picture_buffers_at_display_.count(id)) {
381 // We can delete the texture immediately as it's not being displayed.
382 factories_->DeleteTexture(buffer_to_dismiss.texture_id());
383 CHECK_GT(available_pictures_, 0);
384 --available_pictures_;
386 // Not destroying a texture in display in |picture_buffers_at_display_|.
387 // Postpone deletion until after it's returned to us.
390 void GpuVideoDecoder::PictureReady(const media::Picture& picture) {
391 DVLOG(3) << "PictureReady()";
392 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
394 PictureBufferMap::iterator it =
395 assigned_picture_buffers_.find(picture.picture_buffer_id());
396 if (it == assigned_picture_buffers_.end()) {
397 NOTREACHED() << "Missing picture buffer: " << picture.picture_buffer_id();
398 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE);
399 return;
401 const PictureBuffer& pb = it->second;
403 // Validate picture rectangle from GPU. This is for sanity/security check
404 // even the rectangle is not used in this class.
405 if (picture.visible_rect().IsEmpty() ||
406 !gfx::Rect(pb.size()).Contains(picture.visible_rect())) {
407 NOTREACHED() << "Invalid picture size from VDA: "
408 << picture.visible_rect().ToString() << " should fit in "
409 << pb.size().ToString();
410 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
411 return;
414 // Update frame's timestamp.
415 base::TimeDelta timestamp;
416 // Some of the VDAs don't support and thus don't provide us with visible
417 // size in picture.size, passing coded size instead, so always drop it and
418 // use config information instead.
419 gfx::Rect visible_rect;
420 gfx::Size natural_size;
421 GetBufferData(picture.bitstream_buffer_id(), &timestamp, &visible_rect,
422 &natural_size);
423 DCHECK(decoder_texture_target_);
425 scoped_refptr<VideoFrame> frame(VideoFrame::WrapNativeTexture(
426 make_scoped_ptr(new gpu::MailboxHolder(
427 pb.texture_mailbox(), decoder_texture_target_, 0 /* sync_point */)),
428 BindToCurrentLoop(base::Bind(
429 &GpuVideoDecoder::ReleaseMailbox, weak_factory_.GetWeakPtr(),
430 factories_, picture.picture_buffer_id(), pb.texture_id())),
431 pb.size(), visible_rect, natural_size, timestamp,
432 picture.allow_overlay()));
433 CHECK_GT(available_pictures_, 0);
434 --available_pictures_;
435 bool inserted =
436 picture_buffers_at_display_.insert(std::make_pair(
437 picture.picture_buffer_id(),
438 pb.texture_id())).second;
439 DCHECK(inserted);
441 DeliverFrame(frame);
444 void GpuVideoDecoder::DeliverFrame(
445 const scoped_refptr<VideoFrame>& frame) {
446 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
448 // During a pending vda->Reset(), we don't accumulate frames. Drop it on the
449 // floor and return.
450 if (!pending_reset_cb_.is_null())
451 return;
453 output_cb_.Run(frame);
456 // static
457 void GpuVideoDecoder::ReleaseMailbox(
458 base::WeakPtr<GpuVideoDecoder> decoder,
459 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
460 int64 picture_buffer_id,
461 uint32 texture_id,
462 uint32 release_sync_point) {
463 DCHECK(factories->GetTaskRunner()->BelongsToCurrentThread());
464 factories->WaitSyncPoint(release_sync_point);
466 if (decoder) {
467 decoder->ReusePictureBuffer(picture_buffer_id);
468 return;
470 // It's the last chance to delete the texture after display,
471 // because GpuVideoDecoder was destructed.
472 factories->DeleteTexture(texture_id);
475 void GpuVideoDecoder::ReusePictureBuffer(int64 picture_buffer_id) {
476 DVLOG(3) << "ReusePictureBuffer(" << picture_buffer_id << ")";
477 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
479 DCHECK(!picture_buffers_at_display_.empty());
480 PictureBufferTextureMap::iterator display_iterator =
481 picture_buffers_at_display_.find(picture_buffer_id);
482 uint32 texture_id = display_iterator->second;
483 DCHECK(display_iterator != picture_buffers_at_display_.end());
484 picture_buffers_at_display_.erase(display_iterator);
486 if (!assigned_picture_buffers_.count(picture_buffer_id)) {
487 // This picture was dismissed while in display, so we postponed deletion.
488 factories_->DeleteTexture(texture_id);
489 return;
492 ++available_pictures_;
494 // DestroyVDA() might already have been called.
495 if (vda_)
496 vda_->ReusePictureBuffer(picture_buffer_id);
499 scoped_ptr<GpuVideoDecoder::SHMBuffer> GpuVideoDecoder::GetSHM(
500 size_t min_size) {
501 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
502 if (available_shm_segments_.empty() ||
503 available_shm_segments_.back()->size < min_size) {
504 size_t size_to_allocate = std::max(min_size, kSharedMemorySegmentBytes);
505 scoped_ptr<base::SharedMemory> shm =
506 factories_->CreateSharedMemory(size_to_allocate);
507 // CreateSharedMemory() can return NULL during Shutdown.
508 if (!shm)
509 return NULL;
510 return make_scoped_ptr(new SHMBuffer(shm.Pass(), size_to_allocate));
512 scoped_ptr<SHMBuffer> ret(available_shm_segments_.back());
513 available_shm_segments_.pop_back();
514 return ret.Pass();
517 void GpuVideoDecoder::PutSHM(scoped_ptr<SHMBuffer> shm_buffer) {
518 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
519 available_shm_segments_.push_back(shm_buffer.release());
522 void GpuVideoDecoder::NotifyEndOfBitstreamBuffer(int32 id) {
523 DVLOG(3) << "NotifyEndOfBitstreamBuffer(" << id << ")";
524 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
526 std::map<int32, PendingDecoderBuffer>::iterator it =
527 bitstream_buffers_in_decoder_.find(id);
528 if (it == bitstream_buffers_in_decoder_.end()) {
529 NotifyError(VideoDecodeAccelerator::PLATFORM_FAILURE);
530 NOTREACHED() << "Missing bitstream buffer: " << id;
531 return;
534 PutSHM(make_scoped_ptr(it->second.shm_buffer));
535 it->second.done_cb.Run(state_ == kError ? kDecodeError : kOk);
536 bitstream_buffers_in_decoder_.erase(it);
539 GpuVideoDecoder::~GpuVideoDecoder() {
540 DVLOG(3) << __FUNCTION__;
541 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
543 if (vda_)
544 DestroyVDA();
545 DCHECK(assigned_picture_buffers_.empty());
547 for (size_t i = 0; i < available_shm_segments_.size(); ++i) {
548 delete available_shm_segments_[i];
550 available_shm_segments_.clear();
552 for (std::map<int32, PendingDecoderBuffer>::iterator it =
553 bitstream_buffers_in_decoder_.begin();
554 it != bitstream_buffers_in_decoder_.end(); ++it) {
555 delete it->second.shm_buffer;
556 it->second.done_cb.Run(kAborted);
558 bitstream_buffers_in_decoder_.clear();
560 if (!pending_reset_cb_.is_null())
561 base::ResetAndReturn(&pending_reset_cb_).Run();
564 void GpuVideoDecoder::NotifyFlushDone() {
565 DVLOG(3) << "NotifyFlushDone()";
566 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
567 DCHECK_EQ(state_, kDrainingDecoder);
568 state_ = kDecoderDrained;
569 base::ResetAndReturn(&eos_decode_cb_).Run(kOk);
572 void GpuVideoDecoder::NotifyResetDone() {
573 DVLOG(3) << "NotifyResetDone()";
574 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
575 DCHECK(bitstream_buffers_in_decoder_.empty());
577 // This needs to happen after the Reset() on vda_ is done to ensure pictures
578 // delivered during the reset can find their time data.
579 input_buffer_data_.clear();
581 if (!pending_reset_cb_.is_null())
582 base::ResetAndReturn(&pending_reset_cb_).Run();
585 void GpuVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error) {
586 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
587 if (!vda_)
588 return;
590 state_ = kError;
592 DLOG(ERROR) << "VDA Error: " << error;
593 DestroyVDA();
596 void GpuVideoDecoder::DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent()
597 const {
598 DCHECK(factories_->GetTaskRunner()->BelongsToCurrentThread());
601 } // namespace media