Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/gpu/media/dxva_video_decode_accelerator.h" | 5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h" |
| 6 | 6 |
| 7 #if !defined(OS_WIN) | 7 #if !defined(OS_WIN) |
| 8 #error This file should only be built on Windows. | 8 #error This file should only be built on Windows. |
| 9 #endif // !defined(OS_WIN) | 9 #endif // !defined(OS_WIN) |
| 10 | 10 |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 481 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( | 481 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( |
| 482 const base::Callback<bool(void)>& make_context_current) | 482 const base::Callback<bool(void)>& make_context_current) |
| 483 : client_(NULL), | 483 : client_(NULL), |
| 484 dev_manager_reset_token_(0), | 484 dev_manager_reset_token_(0), |
| 485 egl_config_(NULL), | 485 egl_config_(NULL), |
| 486 state_(kUninitialized), | 486 state_(kUninitialized), |
| 487 pictures_requested_(false), | 487 pictures_requested_(false), |
| 488 inputs_before_decode_(0), | 488 inputs_before_decode_(0), |
| 489 make_context_current_(make_context_current), | 489 make_context_current_(make_context_current), |
| 490 weak_this_factory_(this), | 490 weak_this_factory_(this), |
| 491 codec_(media::kUnknownVideoCodec) { | 491 codec_(media::kUnknownVideoCodec), |
| 492 decoder_thread_("DXVAVideoDecoderThread") { | |
| 492 memset(&input_stream_info_, 0, sizeof(input_stream_info_)); | 493 memset(&input_stream_info_, 0, sizeof(input_stream_info_)); |
| 493 memset(&output_stream_info_, 0, sizeof(output_stream_info_)); | 494 memset(&output_stream_info_, 0, sizeof(output_stream_info_)); |
| 494 } | 495 } |
| 495 | 496 |
| 496 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() { | 497 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() { |
| 497 client_ = NULL; | 498 client_ = NULL; |
| 498 } | 499 } |
| 499 | 500 |
| 500 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, | 501 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
| 501 Client* client) { | 502 Client* client) { |
| 502 DCHECK(CalledOnValidThread()); | 503 client_ = client; |
| 503 | 504 |
| 504 client_ = client; | 505 main_thread_task_runner_ = base::MessageLoop::current()->task_runner(); |
| 505 | 506 |
| 506 // Not all versions of Windows 7 and later include Media Foundation DLLs. | 507 // Not all versions of Windows 7 and later include Media Foundation DLLs. |
| 507 // Instead of crashing while delay loading the DLL when calling MFStartup() | 508 // Instead of crashing while delay loading the DLL when calling MFStartup() |
| 508 // below, probe whether we can successfully load the DLL now. | 509 // below, probe whether we can successfully load the DLL now. |
| 509 // | 510 // |
| 510 // See http://crbug.com/339678 for details. | 511 // See http://crbug.com/339678 for details. |
| 511 HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll"); | 512 HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll"); |
| 512 RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false); | 513 RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false); |
| 513 | 514 |
| 514 // TODO(ananta) | 515 // TODO(ananta) |
| 515 // H264PROFILE_HIGH video decoding is janky at times. Needs more | 516 // H264PROFILE_HIGH video decoding is janky at times. Needs more |
| 516 // investigation. http://crbug.com/426707 | 517 // investigation. http://crbug.com/426707 |
| 517 if (profile != media::H264PROFILE_BASELINE && | 518 if (profile != media::H264PROFILE_BASELINE && |
| 518 profile != media::H264PROFILE_MAIN && | 519 profile != media::H264PROFILE_MAIN && |
| 519 profile != media::VP8PROFILE_ANY && | 520 profile != media::VP8PROFILE_ANY && |
| 520 profile != media::VP9PROFILE_ANY) { | 521 profile != media::VP9PROFILE_ANY) { |
| 521 RETURN_AND_NOTIFY_ON_FAILURE(false, | 522 RETURN_AND_NOTIFY_ON_FAILURE(false, |
| 522 "Unsupported h.264, vp8, or vp9 profile", PLATFORM_FAILURE, false); | 523 "Unsupported h.264, vp8, or vp9 profile", PLATFORM_FAILURE, false); |
| 523 } | 524 } |
| 524 | 525 |
| 525 RETURN_AND_NOTIFY_ON_FAILURE( | 526 RETURN_AND_NOTIFY_ON_FAILURE( |
| 526 gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle, | 527 gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle, |
| 527 "EGL_ANGLE_surface_d3d_texture_2d_share_handle unavailable", | 528 "EGL_ANGLE_surface_d3d_texture_2d_share_handle unavailable", |
| 528 PLATFORM_FAILURE, | 529 PLATFORM_FAILURE, |
| 529 false); | 530 false); |
| 530 | 531 |
| 531 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized), | 532 State state = GetState(); |
| 532 "Initialize: invalid state: " << state_, ILLEGAL_STATE, false); | 533 RETURN_AND_NOTIFY_ON_FAILURE((state == kUninitialized), |
| 534 "Initialize: invalid state: " << state, ILLEGAL_STATE, false); | |
| 533 | 535 |
| 534 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); | 536 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); |
| 535 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE, | 537 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE, |
| 536 false); | 538 false); |
| 537 | 539 |
| 538 RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(), | 540 RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(), |
| 539 "Failed to initialize D3D device and manager", | 541 "Failed to initialize D3D device and manager", |
| 540 PLATFORM_FAILURE, | 542 PLATFORM_FAILURE, |
| 541 false); | 543 false); |
| 542 | 544 |
| 543 RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile), | 545 RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile), |
| 544 "Failed to initialize decoder", PLATFORM_FAILURE, false); | 546 "Failed to initialize decoder", PLATFORM_FAILURE, false); |
| 545 | 547 |
| 546 RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(), | 548 RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(), |
| 547 "Failed to get input/output stream info.", PLATFORM_FAILURE, false); | 549 "Failed to get input/output stream info.", PLATFORM_FAILURE, false); |
| 548 | 550 |
| 549 RETURN_AND_NOTIFY_ON_FAILURE( | 551 RETURN_AND_NOTIFY_ON_FAILURE( |
| 550 SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0), | 552 SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0), |
| 551 "Send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING notification failed", | 553 "Send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING notification failed", |
| 552 PLATFORM_FAILURE, false); | 554 PLATFORM_FAILURE, false); |
| 553 | 555 |
| 554 RETURN_AND_NOTIFY_ON_FAILURE( | 556 RETURN_AND_NOTIFY_ON_FAILURE( |
| 555 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0), | 557 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0), |
| 556 "Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed", | 558 "Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed", |
| 557 PLATFORM_FAILURE, false); | 559 PLATFORM_FAILURE, false); |
| 558 | 560 |
| 559 state_ = kNormal; | 561 SetState(kNormal); |
| 562 | |
| 563 StartDecoderThread(); | |
| 560 return true; | 564 return true; |
| 561 } | 565 } |
| 562 | 566 |
| 563 void DXVAVideoDecodeAccelerator::Decode( | 567 void DXVAVideoDecodeAccelerator::Decode( |
| 564 const media::BitstreamBuffer& bitstream_buffer) { | 568 const media::BitstreamBuffer& bitstream_buffer) { |
| 565 DCHECK(CalledOnValidThread()); | 569 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 566 | 570 |
| 567 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped || | 571 State state = GetState(); |
| 568 state_ == kFlushing), | 572 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped || |
| 569 "Invalid state: " << state_, ILLEGAL_STATE,); | 573 state == kFlushing), |
| 574 "Invalid state: " << state, ILLEGAL_STATE,); | |
| 570 | 575 |
| 571 base::win::ScopedComPtr<IMFSample> sample; | 576 base::win::ScopedComPtr<IMFSample> sample; |
| 572 sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer, | 577 sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer, |
| 573 input_stream_info_.cbSize, | 578 input_stream_info_.cbSize, |
| 574 input_stream_info_.cbAlignment)); | 579 input_stream_info_.cbAlignment)); |
| 575 RETURN_AND_NOTIFY_ON_FAILURE(sample.get(), "Failed to create input sample", | 580 RETURN_AND_NOTIFY_ON_FAILURE(sample.get(), "Failed to create input sample", |
| 576 PLATFORM_FAILURE, ); | 581 PLATFORM_FAILURE, ); |
| 577 | 582 |
| 578 RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()), | 583 RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()), |
| 579 "Failed to associate input buffer id with sample", PLATFORM_FAILURE,); | 584 "Failed to associate input buffer id with sample", PLATFORM_FAILURE,); |
| 580 | 585 |
| 581 DecodeInternal(sample); | 586 decoder_thread_task_runner_->PostTask( |
| 587 FROM_HERE, | |
| 588 base::Bind(&DXVAVideoDecodeAccelerator::DecodeInternal, | |
| 589 base::Unretained(this), sample)); | |
| 582 } | 590 } |
| 583 | 591 |
| 584 void DXVAVideoDecodeAccelerator::AssignPictureBuffers( | 592 void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
| 585 const std::vector<media::PictureBuffer>& buffers) { | 593 const std::vector<media::PictureBuffer>& buffers) { |
| 586 DCHECK(CalledOnValidThread()); | 594 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 587 | 595 |
| 588 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), | 596 State state = GetState(); |
| 589 "Invalid state: " << state_, ILLEGAL_STATE,); | 597 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
| 598 "Invalid state: " << state, ILLEGAL_STATE,); | |
| 590 RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()), | 599 RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()), |
| 591 "Failed to provide requested picture buffers. (Got " << buffers.size() << | 600 "Failed to provide requested picture buffers. (Got " << buffers.size() << |
| 592 ", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,); | 601 ", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,); |
| 593 | 602 |
| 594 // Copy the picture buffers provided by the client to the available list, | 603 // Copy the picture buffers provided by the client to the available list, |
| 595 // and mark these buffers as available for use. | 604 // and mark these buffers as available for use. |
| 596 for (size_t buffer_index = 0; buffer_index < buffers.size(); | 605 for (size_t buffer_index = 0; buffer_index < buffers.size(); |
| 597 ++buffer_index) { | 606 ++buffer_index) { |
| 598 linked_ptr<DXVAPictureBuffer> picture_buffer = | 607 linked_ptr<DXVAPictureBuffer> picture_buffer = |
| 599 DXVAPictureBuffer::Create(*this, buffers[buffer_index], egl_config_); | 608 DXVAPictureBuffer::Create(*this, buffers[buffer_index], egl_config_); |
| 600 RETURN_AND_NOTIFY_ON_FAILURE(picture_buffer.get(), | 609 RETURN_AND_NOTIFY_ON_FAILURE(picture_buffer.get(), |
| 601 "Failed to allocate picture buffer", PLATFORM_FAILURE,); | 610 "Failed to allocate picture buffer", PLATFORM_FAILURE,); |
| 602 | 611 |
| 603 bool inserted = output_picture_buffers_.insert(std::make_pair( | 612 bool inserted = output_picture_buffers_.insert(std::make_pair( |
| 604 buffers[buffer_index].id(), picture_buffer)).second; | 613 buffers[buffer_index].id(), picture_buffer)).second; |
| 605 DCHECK(inserted); | 614 DCHECK(inserted); |
| 606 } | 615 } |
| 607 ProcessPendingSamples(); | 616 ProcessPendingSamples(); |
| 608 if (state_ == kFlushing && pending_output_samples_.empty()) | 617 |
| 618 if (state == kFlushing) | |
| 609 FlushInternal(); | 619 FlushInternal(); |
| 610 } | 620 } |
| 611 | 621 |
| 612 void DXVAVideoDecodeAccelerator::ReusePictureBuffer( | 622 void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
| 613 int32 picture_buffer_id) { | 623 int32 picture_buffer_id) { |
| 614 DCHECK(CalledOnValidThread()); | 624 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 615 | 625 |
| 616 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), | 626 State state = GetState(); |
| 617 "Invalid state: " << state_, ILLEGAL_STATE,); | 627 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
| 628 "Invalid state: " << state, ILLEGAL_STATE,); | |
| 618 | 629 |
| 619 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) | 630 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) |
| 620 return; | 631 return; |
| 621 | 632 |
| 622 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id); | 633 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id); |
| 623 // If we didn't find the picture id in the |output_picture_buffers_| map we | 634 // If we didn't find the picture id in the |output_picture_buffers_| map we |
| 624 // try the |stale_output_picture_buffers_| map, as this may have been an | 635 // try the |stale_output_picture_buffers_| map, as this may have been an |
| 625 // output picture buffer from before a resolution change, that at resolution | 636 // output picture buffer from before a resolution change, that at resolution |
| 626 // change time had yet to be displayed. The client is calling us back to tell | 637 // change time had yet to be displayed. The client is calling us back to tell |
| 627 // us that we can now recycle this picture buffer, so if we were waiting to | 638 // us that we can now recycle this picture buffer, so if we were waiting to |
| 628 // dispose of it we now can. | 639 // dispose of it we now can. |
| 629 if (it == output_picture_buffers_.end()) { | 640 if (it == output_picture_buffers_.end()) { |
| 630 it = stale_output_picture_buffers_.find(picture_buffer_id); | 641 it = stale_output_picture_buffers_.find(picture_buffer_id); |
| 631 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(), | 642 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(), |
| 632 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,); | 643 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,); |
| 633 base::MessageLoop::current()->PostTask(FROM_HERE, | 644 main_thread_task_runner_->PostTask( |
| 645 FROM_HERE, | |
| 634 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer, | 646 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer, |
| 635 weak_this_factory_.GetWeakPtr(), picture_buffer_id)); | 647 weak_this_factory_.GetWeakPtr(), picture_buffer_id)); |
| 636 return; | 648 return; |
| 637 } | 649 } |
| 638 | 650 |
| 639 it->second->ReusePictureBuffer(); | 651 it->second->ReusePictureBuffer(); |
| 640 ProcessPendingSamples(); | 652 ProcessPendingSamples(); |
| 641 | 653 |
| 642 if (state_ == kFlushing && pending_output_samples_.empty()) | 654 if (state == kFlushing) |
| 643 FlushInternal(); | 655 FlushInternal(); |
| 644 } | 656 } |
| 645 | 657 |
| 646 void DXVAVideoDecodeAccelerator::Flush() { | 658 void DXVAVideoDecodeAccelerator::Flush() { |
| 647 DCHECK(CalledOnValidThread()); | 659 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 648 | 660 |
| 649 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; | 661 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; |
| 650 | 662 |
| 651 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), | 663 State state = GetState(); |
| 652 "Unexpected decoder state: " << state_, ILLEGAL_STATE,); | 664 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
| 665 "Unexpected decoder state: " << state, ILLEGAL_STATE,); | |
| 653 | 666 |
| 654 state_ = kFlushing; | 667 SetState(kFlushing); |
| 655 | 668 |
| 656 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), | 669 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), |
| 657 "Failed to send drain message", PLATFORM_FAILURE,); | 670 "Failed to send drain message", PLATFORM_FAILURE,); |
| 658 | 671 |
| 659 if (!pending_output_samples_.empty()) | |
| 660 return; | |
| 661 | |
| 662 FlushInternal(); | 672 FlushInternal(); |
| 663 } | 673 } |
| 664 | 674 |
| 665 void DXVAVideoDecodeAccelerator::Reset() { | 675 void DXVAVideoDecodeAccelerator::Reset() { |
| 666 DCHECK(CalledOnValidThread()); | 676 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 667 | 677 |
| 668 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; | 678 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; |
| 669 | 679 |
| 670 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), | 680 State state = GetState(); |
| 671 "Reset: invalid state: " << state_, ILLEGAL_STATE,); | 681 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
| 682 "Reset: invalid state: " << state, ILLEGAL_STATE,); | |
| 672 | 683 |
| 673 state_ = kResetting; | 684 decoder_thread_.Stop(); |
| 685 | |
| 686 SetState(kResetting); | |
| 674 | 687 |
| 675 pending_output_samples_.clear(); | 688 pending_output_samples_.clear(); |
| 676 | 689 |
| 677 NotifyInputBuffersDropped(); | 690 NotifyInputBuffersDropped(); |
| 678 | 691 |
| 679 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), | 692 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), |
| 680 "Reset: Failed to send message.", PLATFORM_FAILURE,); | 693 "Reset: Failed to send message.", PLATFORM_FAILURE,); |
| 681 | 694 |
| 682 base::MessageLoop::current()->PostTask( | 695 main_thread_task_runner_->PostTask( |
| 683 FROM_HERE, | 696 FROM_HERE, |
| 684 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, | 697 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, |
| 685 weak_this_factory_.GetWeakPtr())); | 698 weak_this_factory_.GetWeakPtr())); |
| 686 | 699 |
| 687 state_ = DXVAVideoDecodeAccelerator::kNormal; | 700 StartDecoderThread(); |
| 701 SetState(kNormal); | |
| 688 } | 702 } |
| 689 | 703 |
| 690 void DXVAVideoDecodeAccelerator::Destroy() { | 704 void DXVAVideoDecodeAccelerator::Destroy() { |
| 691 DCHECK(CalledOnValidThread()); | 705 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 692 Invalidate(); | 706 Invalidate(); |
| 693 delete this; | 707 delete this; |
| 694 } | 708 } |
| 695 | 709 |
| 696 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() { | 710 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() { |
| 697 return false; | 711 return false; |
| 698 } | 712 } |
| 699 | 713 |
| 700 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) { | 714 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) { |
| 701 HMODULE decoder_dll = NULL; | 715 HMODULE decoder_dll = NULL; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 922 << std::hex << std::showbase << output_stream_info_.dwFlags; | 936 << std::hex << std::showbase << output_stream_info_.dwFlags; |
| 923 if (codec_ == media::kCodecH264) { | 937 if (codec_ == media::kCodecH264) { |
| 924 CHECK_EQ(output_stream_info_.dwFlags, 0x107u); | 938 CHECK_EQ(output_stream_info_.dwFlags, 0x107u); |
| 925 } | 939 } |
| 926 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize; | 940 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize; |
| 927 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment; | 941 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment; |
| 928 return true; | 942 return true; |
| 929 } | 943 } |
| 930 | 944 |
| 931 void DXVAVideoDecodeAccelerator::DoDecode() { | 945 void DXVAVideoDecodeAccelerator::DoDecode() { |
| 946 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread()); | |
| 932 // This function is also called from FlushInternal in a loop which could | 947 // This function is also called from FlushInternal in a loop which could |
| 933 // result in the state transitioning to kStopped due to no decoded output. | 948 // result in the state transitioning to kStopped due to no decoded output. |
| 949 State state = GetState(); | |
| 934 RETURN_AND_NOTIFY_ON_FAILURE( | 950 RETURN_AND_NOTIFY_ON_FAILURE( |
| 935 (state_ == kNormal || state_ == kFlushing || | 951 (state == kNormal || state == kFlushing || |
| 936 state_ == kStopped || state_ == kFlushingPendingInputBuffers), | 952 state == kStopped || state == kFlushingPendingInputBuffers), |
| 937 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); | 953 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); |
| 938 | 954 |
| 939 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; | 955 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; |
| 940 DWORD status = 0; | 956 DWORD status = 0; |
| 941 | 957 |
| 942 HRESULT hr = decoder_->ProcessOutput(0, // No flags | 958 HRESULT hr = decoder_->ProcessOutput(0, // No flags |
| 943 1, // # of out streams to pull from | 959 1, // # of out streams to pull from |
| 944 &output_data_buffer, | 960 &output_data_buffer, |
| 945 &status); | 961 &status); |
| 946 IMFCollection* events = output_data_buffer.pEvents; | 962 IMFCollection* events = output_data_buffer.pEvents; |
| 947 if (events != NULL) { | 963 if (events != NULL) { |
| 948 DVLOG(1) << "Got events from ProcessOuput, but discarding"; | 964 DVLOG(1) << "Got events from ProcessOuput, but discarding"; |
| 949 events->Release(); | 965 events->Release(); |
| 950 } | 966 } |
| 951 if (FAILED(hr)) { | 967 if (FAILED(hr)) { |
| 952 // A stream change needs further ProcessInput calls to get back decoder | 968 // A stream change needs further ProcessInput calls to get back decoder |
| 953 // output which is why we need to set the state to stopped. | 969 // output which is why we need to set the state to stopped. |
| 954 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) { | 970 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) { |
| 955 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) { | 971 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) { |
| 956 // Decoder didn't let us set NV12 output format. Not sure as to why | 972 // Decoder didn't let us set NV12 output format. Not sure as to why |
| 957 // this can happen. Give up in disgust. | 973 // this can happen. Give up in disgust. |
| 958 NOTREACHED() << "Failed to set decoder output media type to NV12"; | 974 NOTREACHED() << "Failed to set decoder output media type to NV12"; |
| 959 state_ = kStopped; | 975 SetState(kStopped); |
| 960 } else { | 976 } else { |
| 961 DVLOG(1) << "Received output format change from the decoder." | 977 DVLOG(1) << "Received output format change from the decoder." |
| 962 " Recursively invoking DoDecode"; | 978 " Recursively invoking DoDecode"; |
| 963 DoDecode(); | 979 DoDecode(); |
| 964 } | 980 } |
| 965 return; | 981 return; |
| 966 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { | 982 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { |
| 967 // No more output from the decoder. Stop playback. | 983 // No more output from the decoder. Stop playback. |
| 968 if (state_ != kFlushingPendingInputBuffers) | 984 if (state != kFlushingPendingInputBuffers) |
| 969 state_ = kStopped; | 985 SetState(kStopped); |
| 970 return; | 986 return; |
| 971 } else { | 987 } else { |
| 972 NOTREACHED() << "Unhandled error in DoDecode()"; | 988 NOTREACHED() << "Unhandled error in DoDecode()"; |
| 973 return; | 989 return; |
| 974 } | 990 } |
| 975 } | 991 } |
| 976 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); | 992 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); |
| 977 | 993 |
| 978 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode", | 994 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode", |
| 979 inputs_before_decode_); | 995 inputs_before_decode_); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 995 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE, | 1011 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE, |
| 996 IID_PPV_ARGS(surface.Receive())); | 1012 IID_PPV_ARGS(surface.Receive())); |
| 997 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample", | 1013 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample", |
| 998 false); | 1014 false); |
| 999 | 1015 |
| 1000 LONGLONG input_buffer_id = 0; | 1016 LONGLONG input_buffer_id = 0; |
| 1001 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), | 1017 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), |
| 1002 "Failed to get input buffer id associated with sample", | 1018 "Failed to get input buffer id associated with sample", |
| 1003 false); | 1019 false); |
| 1004 | 1020 |
| 1021 decoder_lock_.Acquire(); | |
|
DaleCurtis
2014/12/04 01:54:27
It's generally preferred to use an AutoLock here w
ananta
2014/12/04 08:04:50
Done.
| |
| 1005 pending_output_samples_.push_back( | 1022 pending_output_samples_.push_back( |
| 1006 PendingSampleInfo(input_buffer_id, sample)); | 1023 PendingSampleInfo(input_buffer_id, sample)); |
| 1024 decoder_lock_.Release(); | |
| 1007 | 1025 |
| 1008 // If we have available picture buffers to copy the output data then use the | |
| 1009 // first one and then flag it as not being available for use. | |
| 1010 if (output_picture_buffers_.size()) { | |
| 1011 ProcessPendingSamples(); | |
| 1012 return true; | |
| 1013 } | |
| 1014 if (pictures_requested_) { | 1026 if (pictures_requested_) { |
| 1015 DVLOG(1) << "Waiting for picture slots from the client."; | 1027 DVLOG(1) << "Waiting for picture slots from the client."; |
| 1028 main_thread_task_runner_->PostTask( | |
| 1029 FROM_HERE, | |
| 1030 base::Bind(&DXVAVideoDecodeAccelerator::ProcessPendingSamples, | |
| 1031 weak_this_factory_.GetWeakPtr())); | |
| 1016 return true; | 1032 return true; |
| 1017 } | 1033 } |
| 1018 | 1034 |
| 1019 // We only read the surface description, which contains its width/height when | 1035 // We only read the surface description, which contains its width/height when |
| 1020 // we need the picture buffers from the client. Once we have those, then they | 1036 // we need the picture buffers from the client. Once we have those, then they |
| 1021 // are reused. | 1037 // are reused. |
| 1022 D3DSURFACE_DESC surface_desc; | 1038 D3DSURFACE_DESC surface_desc; |
| 1023 hr = surface->GetDesc(&surface_desc); | 1039 hr = surface->GetDesc(&surface_desc); |
| 1024 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); | 1040 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); |
| 1025 | 1041 |
| 1026 // Go ahead and request picture buffers. | 1042 // Go ahead and request picture buffers. |
| 1027 base::MessageLoop::current()->PostTask( | 1043 main_thread_task_runner_->PostTask( |
| 1028 FROM_HERE, | 1044 FROM_HERE, |
| 1029 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, | 1045 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, |
| 1030 weak_this_factory_.GetWeakPtr(), | 1046 weak_this_factory_.GetWeakPtr(), |
| 1031 surface_desc.Width, | 1047 surface_desc.Width, |
| 1032 surface_desc.Height)); | 1048 surface_desc.Height)); |
| 1033 | 1049 |
| 1034 pictures_requested_ = true; | 1050 pictures_requested_ = true; |
| 1035 return true; | 1051 return true; |
| 1036 } | 1052 } |
| 1037 | 1053 |
| 1038 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { | 1054 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
| 1055 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
| 1056 | |
| 1057 if (!output_picture_buffers_.size()) | |
| 1058 return; | |
| 1059 | |
| 1039 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(), | 1060 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(), |
| 1040 "Failed to make context current", PLATFORM_FAILURE,); | 1061 "Failed to make context current", PLATFORM_FAILURE,); |
| 1041 | 1062 |
| 1042 OutputBuffers::iterator index; | 1063 OutputBuffers::iterator index; |
| 1043 | 1064 |
| 1065 base::AutoLock lock(decoder_lock_); | |
|
DaleCurtis
2014/12/04 01:54:27
Notably since this section may take a while to run
ananta
2014/12/04 08:04:51
Done.
| |
| 1066 | |
| 1044 for (index = output_picture_buffers_.begin(); | 1067 for (index = output_picture_buffers_.begin(); |
| 1045 index != output_picture_buffers_.end() && | 1068 index != output_picture_buffers_.end() && |
| 1046 !pending_output_samples_.empty(); | 1069 !pending_output_samples_.empty(); |
| 1047 ++index) { | 1070 ++index) { |
| 1048 if (index->second->available()) { | 1071 if (index->second->available()) { |
| 1049 PendingSampleInfo sample_info = pending_output_samples_.front(); | 1072 PendingSampleInfo sample_info = pending_output_samples_.front(); |
| 1050 | 1073 |
| 1051 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; | 1074 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; |
| 1052 HRESULT hr = sample_info.output_sample->GetBufferByIndex( | 1075 HRESULT hr = sample_info.output_sample->GetBufferByIndex( |
| 1053 0, output_buffer.Receive()); | 1076 0, output_buffer.Receive()); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1075 } | 1098 } |
| 1076 | 1099 |
| 1077 RETURN_AND_NOTIFY_ON_FAILURE( | 1100 RETURN_AND_NOTIFY_ON_FAILURE( |
| 1078 index->second->CopyOutputSampleDataToPictureBuffer(*this, | 1101 index->second->CopyOutputSampleDataToPictureBuffer(*this, |
| 1079 surface.get()), | 1102 surface.get()), |
| 1080 "Failed to copy output sample", PLATFORM_FAILURE, ); | 1103 "Failed to copy output sample", PLATFORM_FAILURE, ); |
| 1081 | 1104 |
| 1082 media::Picture output_picture(index->second->id(), | 1105 media::Picture output_picture(index->second->id(), |
| 1083 sample_info.input_buffer_id, | 1106 sample_info.input_buffer_id, |
| 1084 gfx::Rect(index->second->size())); | 1107 gfx::Rect(index->second->size())); |
| 1085 base::MessageLoop::current()->PostTask( | 1108 main_thread_task_runner_->PostTask( |
| 1086 FROM_HERE, | 1109 FROM_HERE, |
| 1087 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, | 1110 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, |
| 1088 weak_this_factory_.GetWeakPtr(), | 1111 weak_this_factory_.GetWeakPtr(), |
| 1089 output_picture)); | 1112 output_picture)); |
| 1090 | 1113 |
| 1091 index->second->set_available(false); | 1114 index->second->set_available(false); |
| 1092 pending_output_samples_.pop_front(); | 1115 pending_output_samples_.pop_front(); |
| 1093 } | 1116 } |
| 1094 } | 1117 } |
| 1095 | 1118 if (GetState() == kFlushing) { |
| 1096 if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) { | 1119 decoder_thread_task_runner_->PostTask( |
|
DaleCurtis
2014/12/04 01:54:27
Seems you can just early return after this PostTas
ananta
2014/12/04 08:04:51
The state transitions in Flush are a touch complic
| |
| 1097 base::MessageLoop::current()->PostTask( | |
| 1098 FROM_HERE, | 1120 FROM_HERE, |
| 1099 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, | 1121 base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal, |
| 1100 weak_this_factory_.GetWeakPtr())); | 1122 base::Unretained(this))); |
| 1101 } | 1123 } |
| 1124 decoder_thread_task_runner_->PostTask( | |
| 1125 FROM_HERE, | |
| 1126 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, | |
| 1127 base::Unretained(this))); | |
| 1102 } | 1128 } |
| 1103 | 1129 |
| 1104 void DXVAVideoDecodeAccelerator::StopOnError( | 1130 void DXVAVideoDecodeAccelerator::StopOnError( |
| 1105 media::VideoDecodeAccelerator::Error error) { | 1131 media::VideoDecodeAccelerator::Error error) { |
| 1106 DCHECK(CalledOnValidThread()); | 1132 if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
| 1133 main_thread_task_runner_->PostTask( | |
| 1134 FROM_HERE, | |
| 1135 base::Bind(&DXVAVideoDecodeAccelerator::StopOnError, | |
| 1136 weak_this_factory_.GetWeakPtr(), | |
| 1137 error)); | |
| 1138 return; | |
| 1139 } | |
| 1107 | 1140 |
| 1108 if (client_) | 1141 if (client_) |
| 1109 client_->NotifyError(error); | 1142 client_->NotifyError(error); |
| 1110 client_ = NULL; | 1143 client_ = NULL; |
| 1111 | 1144 |
| 1112 if (state_ != kUninitialized) { | 1145 if (GetState() != kUninitialized) { |
| 1113 Invalidate(); | 1146 Invalidate(); |
| 1114 } | 1147 } |
| 1115 } | 1148 } |
| 1116 | 1149 |
| 1117 void DXVAVideoDecodeAccelerator::Invalidate() { | 1150 void DXVAVideoDecodeAccelerator::Invalidate() { |
| 1118 if (state_ == kUninitialized) | 1151 if (GetState() == kUninitialized) |
| 1119 return; | 1152 return; |
| 1153 decoder_thread_.Stop(); | |
| 1120 weak_this_factory_.InvalidateWeakPtrs(); | 1154 weak_this_factory_.InvalidateWeakPtrs(); |
| 1121 output_picture_buffers_.clear(); | 1155 output_picture_buffers_.clear(); |
| 1122 stale_output_picture_buffers_.clear(); | 1156 stale_output_picture_buffers_.clear(); |
| 1123 pending_output_samples_.clear(); | 1157 pending_output_samples_.clear(); |
| 1124 pending_input_buffers_.clear(); | 1158 pending_input_buffers_.clear(); |
| 1125 decoder_.Release(); | 1159 decoder_.Release(); |
| 1126 MFShutdown(); | 1160 MFShutdown(); |
| 1127 state_ = kUninitialized; | 1161 SetState(kUninitialized); |
| 1128 } | 1162 } |
| 1129 | 1163 |
| 1130 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { | 1164 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { |
| 1165 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
| 1131 if (client_) | 1166 if (client_) |
| 1132 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); | 1167 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); |
| 1133 } | 1168 } |
| 1134 | 1169 |
| 1135 void DXVAVideoDecodeAccelerator::NotifyFlushDone() { | 1170 void DXVAVideoDecodeAccelerator::NotifyFlushDone() { |
| 1136 if (client_) | 1171 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 1172 if (client_ && GetState() == kStopped) | |
| 1137 client_->NotifyFlushDone(); | 1173 client_->NotifyFlushDone(); |
| 1138 } | 1174 } |
| 1139 | 1175 |
| 1140 void DXVAVideoDecodeAccelerator::NotifyResetDone() { | 1176 void DXVAVideoDecodeAccelerator::NotifyResetDone() { |
| 1177 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
| 1141 if (client_) | 1178 if (client_) |
| 1142 client_->NotifyResetDone(); | 1179 client_->NotifyResetDone(); |
| 1143 } | 1180 } |
| 1144 | 1181 |
| 1145 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { | 1182 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { |
| 1183 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
| 1146 // This task could execute after the decoder has been torn down. | 1184 // This task could execute after the decoder has been torn down. |
| 1147 if (state_ != kUninitialized && client_) { | 1185 if (GetState() != kUninitialized && client_) { |
| 1148 client_->ProvidePictureBuffers( | 1186 client_->ProvidePictureBuffers( |
| 1149 kNumPictureBuffers, | 1187 kNumPictureBuffers, |
| 1150 gfx::Size(width, height), | 1188 gfx::Size(width, height), |
| 1151 GL_TEXTURE_2D); | 1189 GL_TEXTURE_2D); |
| 1152 } | 1190 } |
| 1153 } | 1191 } |
| 1154 | 1192 |
| 1155 void DXVAVideoDecodeAccelerator::NotifyPictureReady( | 1193 void DXVAVideoDecodeAccelerator::NotifyPictureReady( |
| 1156 const media::Picture& picture) { | 1194 const media::Picture& picture) { |
| 1195 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
| 1157 // This task could execute after the decoder has been torn down. | 1196 // This task could execute after the decoder has been torn down. |
| 1158 if (state_ != kUninitialized && client_) | 1197 if (GetState() != kUninitialized && client_) |
| 1159 client_->PictureReady(picture); | 1198 client_->PictureReady(picture); |
| 1160 } | 1199 } |
| 1161 | 1200 |
| 1162 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { | 1201 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { |
| 1163 if (!client_ || !pending_output_samples_.empty()) | 1202 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 1203 if (!client_) | |
| 1164 return; | 1204 return; |
| 1165 | 1205 |
| 1166 for (PendingInputs::iterator it = pending_input_buffers_.begin(); | 1206 for (PendingInputs::iterator it = pending_input_buffers_.begin(); |
| 1167 it != pending_input_buffers_.end(); ++it) { | 1207 it != pending_input_buffers_.end(); ++it) { |
| 1168 LONGLONG input_buffer_id = 0; | 1208 LONGLONG input_buffer_id = 0; |
| 1169 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), | 1209 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), |
| 1170 "Failed to get buffer id associated with sample",); | 1210 "Failed to get buffer id associated with sample",); |
| 1171 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); | 1211 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); |
| 1172 } | 1212 } |
| 1173 pending_input_buffers_.clear(); | 1213 pending_input_buffers_.clear(); |
| 1174 } | 1214 } |
| 1175 | 1215 |
| 1176 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { | 1216 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
| 1177 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), | 1217 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread()); |
| 1178 "Invalid state: " << state_, ILLEGAL_STATE,); | 1218 State state = GetState(); |
| 1219 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), | |
| 1220 "Invalid state: " << state, ILLEGAL_STATE,); | |
| 1179 | 1221 |
| 1180 if (pending_input_buffers_.empty() || !pending_output_samples_.empty()) | 1222 if (pending_input_buffers_.empty() || OutputSamplesPresent()) |
|
DaleCurtis
2014/12/04 01:54:27
Is it necessary to check OutputSamplesPresent() he
ananta
2014/12/04 08:04:51
Yes. The Media foundation decoder has a pool of su
| |
| 1181 return; | 1223 return; |
| 1182 | 1224 |
| 1183 PendingInputs pending_input_buffers_copy; | 1225 PendingInputs pending_input_buffers_copy; |
| 1184 std::swap(pending_input_buffers_, pending_input_buffers_copy); | 1226 std::swap(pending_input_buffers_, pending_input_buffers_copy); |
| 1185 | 1227 |
| 1186 for (PendingInputs::iterator it = pending_input_buffers_copy.begin(); | 1228 for (PendingInputs::iterator it = pending_input_buffers_copy.begin(); |
| 1187 it != pending_input_buffers_copy.end(); ++it) { | 1229 it != pending_input_buffers_copy.end(); ++it) { |
| 1188 DecodeInternal(*it); | 1230 DecodeInternal(*it); |
| 1189 } | 1231 } |
| 1190 | 1232 |
| 1191 if (state_ != kFlushingPendingInputBuffers) | 1233 if (state != kFlushingPendingInputBuffers) |
| 1192 return; | 1234 return; |
| 1193 | 1235 |
| 1194 // If we are scheduled during a flush operation then mark the flush as | 1236 // If we are scheduled during a flush operation then mark the flush as |
|
DaleCurtis
2014/12/04 19:21:34
Does the bug this resolved no longer happen?
ananta
2014/12/04 19:48:55
This code is not needed with ProcessPendingSamples
| |
| 1195 // complete if we have no pending input and pending output frames. | 1237 // complete if we have no pending input and pending output frames. |
| 1196 // If we don't have available output slots then this function will be | 1238 // If we don't have available output slots then this function will be |
| 1197 // scheduled again by the ProcessPendingSamples function once slots become | 1239 // scheduled again by the ProcessPendingSamples function once slots become |
| 1198 // available. | 1240 // available. |
| 1199 if (pending_input_buffers_.empty() && pending_output_samples_.empty()) { | 1241 if (pending_input_buffers_.empty() && !OutputSamplesPresent()) { |
| 1200 state_ = kNormal; | 1242 SetState(kNormal); |
| 1201 base::MessageLoop::current()->PostTask( | 1243 main_thread_task_runner_->PostTask( |
| 1202 FROM_HERE, | 1244 FROM_HERE, |
| 1203 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, | 1245 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
| 1204 weak_this_factory_.GetWeakPtr())); | 1246 weak_this_factory_.GetWeakPtr())); |
| 1205 } | 1247 } |
| 1206 } | 1248 } |
| 1207 | 1249 |
| 1208 void DXVAVideoDecodeAccelerator::FlushInternal() { | 1250 void DXVAVideoDecodeAccelerator::FlushInternal() { |
| 1251 if (!decoder_thread_task_runner_->BelongsToCurrentThread()) { | |
|
DaleCurtis
2014/12/04 01:54:27
There's only a couple callers of this, so it'd be
ananta
2014/12/04 08:04:51
Done.
| |
| 1252 decoder_thread_task_runner_->PostTask( | |
| 1253 FROM_HERE, | |
| 1254 base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal, | |
| 1255 base::Unretained(this))); | |
| 1256 return; | |
| 1257 } | |
| 1258 | |
| 1259 // We allow only one output frame to be present at any given time. If we have | |
| 1260 // an output frame, then we cannot complete the flush at this time. | |
| 1261 if (OutputSamplesPresent()) | |
| 1262 return; | |
| 1263 | |
| 1209 // The DoDecode function sets the state to kStopped when the decoder returns | 1264 // The DoDecode function sets the state to kStopped when the decoder returns |
| 1210 // MF_E_TRANSFORM_NEED_MORE_INPUT. | 1265 // MF_E_TRANSFORM_NEED_MORE_INPUT. |
| 1211 // The MFT decoder can buffer upto 30 frames worth of input before returning | 1266 // The MFT decoder can buffer upto 30 frames worth of input before returning |
| 1212 // an output frame. This loop here attempts to retrieve as many output frames | 1267 // an output frame. This loop here attempts to retrieve as many output frames |
| 1213 // as possible from the buffered set. | 1268 // as possible from the buffered set. |
| 1214 while (state_ != kStopped) { | 1269 while (GetState() != kStopped) { |
| 1215 DoDecode(); | 1270 DoDecode(); |
| 1216 if (!pending_output_samples_.empty()) | 1271 if (OutputSamplesPresent()) |
| 1217 return; | 1272 return; |
| 1218 } | 1273 } |
| 1219 | 1274 |
| 1220 // TODO(ananta) | 1275 // TODO(ananta) |
| 1221 // Look into whether we can simplify this function by combining the while | 1276 // Look into whether we can simplify this function by combining the while |
| 1222 // above and the code below into a single block which achieves both. The Flush | 1277 // above and the code below into a single block which achieves both. The Flush |
| 1223 // transitions in the decoder are a touch intertwined with other portions of | 1278 // transitions in the decoder are a touch intertwined with other portions of |
| 1224 // the code like AssignPictureBuffers, ReusePictureBuffers etc. | 1279 // the code like AssignPictureBuffers, ReusePictureBuffers etc. |
| 1225 if (!pending_input_buffers_.empty()) { | 1280 if (!pending_input_buffers_.empty()) { |
| 1226 state_ = kFlushingPendingInputBuffers; | 1281 SetState(kFlushingPendingInputBuffers); |
| 1227 base::MessageLoop::current()->PostTask( | 1282 decoder_thread_task_runner_->PostTask( |
| 1228 FROM_HERE, | 1283 FROM_HERE, |
| 1229 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, | 1284 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
| 1230 weak_this_factory_.GetWeakPtr())); | 1285 base::Unretained(this))); |
| 1231 return; | 1286 return; |
| 1232 } | 1287 } |
| 1233 | 1288 |
| 1234 base::MessageLoop::current()->PostTask( | 1289 main_thread_task_runner_->PostTask( |
| 1235 FROM_HERE, | 1290 FROM_HERE, |
| 1236 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, | 1291 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
| 1237 weak_this_factory_.GetWeakPtr())); | 1292 weak_this_factory_.GetWeakPtr())); |
| 1238 | 1293 |
| 1239 state_ = kNormal; | 1294 SetState(kNormal); |
| 1240 } | 1295 } |
| 1241 | 1296 |
| 1242 void DXVAVideoDecodeAccelerator::DecodeInternal( | 1297 void DXVAVideoDecodeAccelerator::DecodeInternal( |
| 1243 const base::win::ScopedComPtr<IMFSample>& sample) { | 1298 const base::win::ScopedComPtr<IMFSample>& sample) { |
| 1244 DCHECK(CalledOnValidThread()); | 1299 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread()); |
| 1245 | 1300 |
| 1246 if (state_ == kUninitialized) | 1301 if (GetState() == kUninitialized) |
| 1247 return; | 1302 return; |
| 1248 | 1303 |
| 1249 if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) { | 1304 if (OutputSamplesPresent() || !pending_input_buffers_.empty()) { |
| 1250 pending_input_buffers_.push_back(sample); | 1305 pending_input_buffers_.push_back(sample); |
| 1251 return; | 1306 return; |
| 1252 } | 1307 } |
| 1253 | 1308 |
| 1254 if (!inputs_before_decode_) { | 1309 if (!inputs_before_decode_) { |
| 1255 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); | 1310 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); |
| 1256 } | 1311 } |
| 1257 inputs_before_decode_++; | 1312 inputs_before_decode_++; |
| 1258 | 1313 |
| 1259 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0); | 1314 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0); |
| 1260 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it | 1315 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it |
| 1261 // has enough data to produce one or more output samples. In this case the | 1316 // has enough data to produce one or more output samples. In this case the |
| 1262 // recommended options are to | 1317 // recommended options are to |
| 1263 // 1. Generate new output by calling IMFTransform::ProcessOutput until it | 1318 // 1. Generate new output by calling IMFTransform::ProcessOutput until it |
| 1264 // returns MF_E_TRANSFORM_NEED_MORE_INPUT. | 1319 // returns MF_E_TRANSFORM_NEED_MORE_INPUT. |
| 1265 // 2. Flush the input data | 1320 // 2. Flush the input data |
| 1266 // We implement the first option, i.e to retrieve the output sample and then | 1321 // We implement the first option, i.e to retrieve the output sample and then |
| 1267 // process the input again. Failure in either of these steps is treated as a | 1322 // process the input again. Failure in either of these steps is treated as a |
| 1268 // decoder failure. | 1323 // decoder failure. |
| 1269 if (hr == MF_E_NOTACCEPTING) { | 1324 if (hr == MF_E_NOTACCEPTING) { |
| 1270 DoDecode(); | 1325 DoDecode(); |
| 1271 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal), | 1326 State state = GetState(); |
| 1272 "Failed to process output. Unexpected decoder state: " << state_, | 1327 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
| 1328 state == kFlushing), | |
| 1329 "Failed to process output. Unexpected decoder state: " << state, | |
| 1273 PLATFORM_FAILURE,); | 1330 PLATFORM_FAILURE,); |
| 1274 hr = decoder_->ProcessInput(0, sample.get(), 0); | 1331 hr = decoder_->ProcessInput(0, sample.get(), 0); |
| 1275 // If we continue to get the MF_E_NOTACCEPTING error we do the following:- | 1332 // If we continue to get the MF_E_NOTACCEPTING error we do the following:- |
| 1276 // 1. Add the input sample to the pending queue. | 1333 // 1. Add the input sample to the pending queue. |
| 1277 // 2. If we don't have any output samples we post the | 1334 // 2. If we don't have any output samples we post the |
| 1278 // DecodePendingInputBuffers task to process the pending input samples. | 1335 // DecodePendingInputBuffers task to process the pending input samples. |
| 1279 // If we have an output sample then the above task is posted when the | 1336 // If we have an output sample then the above task is posted when the |
| 1280 // output samples are sent to the client. | 1337 // output samples are sent to the client. |
| 1281 // This is because we only support 1 pending output sample at any | 1338 // This is because we only support 1 pending output sample at any |
| 1282 // given time due to the limitation with the Microsoft media foundation | 1339 // given time due to the limitation with the Microsoft media foundation |
| 1283 // decoder where it recycles the output Decoder surfaces. | 1340 // decoder where it recycles the output Decoder surfaces. |
| 1284 if (hr == MF_E_NOTACCEPTING) { | 1341 if (hr == MF_E_NOTACCEPTING) { |
| 1285 pending_input_buffers_.push_back(sample); | 1342 pending_input_buffers_.push_back(sample); |
| 1286 if (pending_output_samples_.empty()) { | 1343 if (!OutputSamplesPresent()) { |
|
DaleCurtis
2014/12/04 01:54:27
Why not just always call this? GpuVideoDecoder its
ananta
2014/12/04 08:04:50
Done.
| |
| 1287 base::MessageLoop::current()->PostTask( | 1344 decoder_thread_task_runner_->PostTask( |
| 1288 FROM_HERE, | 1345 FROM_HERE, |
| 1289 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, | 1346 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
| 1290 weak_this_factory_.GetWeakPtr())); | 1347 base::Unretained(this))); |
| 1291 } | 1348 } |
| 1292 return; | 1349 return; |
| 1293 } | 1350 } |
| 1294 } | 1351 } |
| 1295 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample", | 1352 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample", |
| 1296 PLATFORM_FAILURE,); | 1353 PLATFORM_FAILURE,); |
| 1297 | 1354 |
| 1298 DoDecode(); | 1355 DoDecode(); |
| 1299 | 1356 |
| 1300 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal || | 1357 State state = GetState(); |
| 1301 state_ == kFlushingPendingInputBuffers), | 1358 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
| 1302 "Failed to process output. Unexpected decoder state: " << state_, | 1359 state == kFlushingPendingInputBuffers || |
| 1360 state == kFlushing), | |
| 1361 "Failed to process output. Unexpected decoder state: " << state, | |
| 1303 ILLEGAL_STATE,); | 1362 ILLEGAL_STATE,); |
| 1304 | 1363 |
| 1305 LONGLONG input_buffer_id = 0; | 1364 LONGLONG input_buffer_id = 0; |
| 1306 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), | 1365 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), |
| 1307 "Failed to get input buffer id associated with sample",); | 1366 "Failed to get input buffer id associated with sample",); |
| 1308 // The Microsoft Media foundation decoder internally buffers up to 30 frames | 1367 // The Microsoft Media foundation decoder internally buffers up to 30 frames |
| 1309 // before returning a decoded frame. We need to inform the client that this | 1368 // before returning a decoded frame. We need to inform the client that this |
| 1310 // input buffer is processed as it may stop sending us further input. | 1369 // input buffer is processed as it may stop sending us further input. |
| 1311 // Note: This may break clients which expect every input buffer to be | 1370 // Note: This may break clients which expect every input buffer to be |
| 1312 // associated with a decoded output buffer. | 1371 // associated with a decoded output buffer. |
| 1313 // TODO(ananta) | 1372 // TODO(ananta) |
| 1314 // Do some more investigation into whether it is possible to get the MFT | 1373 // Do some more investigation into whether it is possible to get the MFT |
| 1315 // decoder to emit an output packet for every input packet. | 1374 // decoder to emit an output packet for every input packet. |
| 1316 // http://code.google.com/p/chromium/issues/detail?id=108121 | 1375 // http://code.google.com/p/chromium/issues/detail?id=108121 |
| 1317 // http://code.google.com/p/chromium/issues/detail?id=150925 | 1376 // http://code.google.com/p/chromium/issues/detail?id=150925 |
| 1318 base::MessageLoop::current()->PostTask( | 1377 main_thread_task_runner_->PostTask( |
| 1319 FROM_HERE, | 1378 FROM_HERE, |
| 1320 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, | 1379 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, |
| 1321 weak_this_factory_.GetWeakPtr(), | 1380 weak_this_factory_.GetWeakPtr(), |
| 1322 input_buffer_id)); | 1381 input_buffer_id)); |
| 1323 } | 1382 } |
| 1324 | 1383 |
| 1325 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width, | 1384 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width, |
| 1326 int height) { | 1385 int height) { |
| 1327 base::MessageLoop::current()->PostTask( | 1386 main_thread_task_runner_->PostTask( |
| 1328 FROM_HERE, | 1387 FROM_HERE, |
| 1329 base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers, | 1388 base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers, |
| 1330 weak_this_factory_.GetWeakPtr())); | 1389 weak_this_factory_.GetWeakPtr())); |
| 1331 | 1390 |
| 1332 base::MessageLoop::current()->PostTask( | 1391 main_thread_task_runner_->PostTask( |
| 1333 FROM_HERE, | 1392 FROM_HERE, |
| 1334 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, | 1393 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, |
| 1335 weak_this_factory_.GetWeakPtr(), | 1394 weak_this_factory_.GetWeakPtr(), |
| 1336 width, | 1395 width, |
| 1337 height)); | 1396 height)); |
| 1338 } | 1397 } |
| 1339 | 1398 |
| 1340 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() { | 1399 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() { |
| 1341 OutputBuffers::iterator index; | 1400 OutputBuffers::iterator index; |
| 1342 | 1401 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 1359 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( | 1418 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( |
| 1360 int32 picture_buffer_id) { | 1419 int32 picture_buffer_id) { |
| 1361 OutputBuffers::iterator it = stale_output_picture_buffers_.find( | 1420 OutputBuffers::iterator it = stale_output_picture_buffers_.find( |
| 1362 picture_buffer_id); | 1421 picture_buffer_id); |
| 1363 DCHECK(it != stale_output_picture_buffers_.end()); | 1422 DCHECK(it != stale_output_picture_buffers_.end()); |
| 1364 DVLOG(1) << "Dismissing picture id: " << it->second->id(); | 1423 DVLOG(1) << "Dismissing picture id: " << it->second->id(); |
| 1365 client_->DismissPictureBuffer(it->second->id()); | 1424 client_->DismissPictureBuffer(it->second->id()); |
| 1366 stale_output_picture_buffers_.erase(it); | 1425 stale_output_picture_buffers_.erase(it); |
| 1367 } | 1426 } |
| 1368 | 1427 |
| 1428 DXVAVideoDecodeAccelerator::State | |
| 1429 DXVAVideoDecodeAccelerator::GetState() const { | |
| 1430 State state = kUninitialized; | |
| 1431 ::InterlockedExchange(reinterpret_cast<long*>(&state), | |
| 1432 state_); | |
| 1433 return state; | |
| 1434 } | |
| 1435 | |
| 1436 void DXVAVideoDecodeAccelerator::SetState(State new_state) { | |
| 1437 if (!main_thread_task_runner_->BelongsToCurrentThread()) { | |
| 1438 main_thread_task_runner_->PostTask( | |
| 1439 FROM_HERE, | |
| 1440 base::Bind(&DXVAVideoDecodeAccelerator::SetState, | |
| 1441 weak_this_factory_.GetWeakPtr(), | |
| 1442 new_state)); | |
| 1443 return; | |
| 1444 } | |
| 1445 ::InterlockedCompareExchange(reinterpret_cast<long*>(&state_), | |
|
DaleCurtis
2014/12/04 01:54:27
Formatting is wrong. Also you should at least DCHE
ananta
2014/12/04 08:04:51
Done.
| |
| 1446 new_state, state_); | |
| 1447 } | |
| 1448 | |
| 1449 void DXVAVideoDecodeAccelerator::StartDecoderThread() { | |
| 1450 decoder_thread_.init_com_with_mta(false); | |
| 1451 decoder_thread_.Start(); | |
| 1452 decoder_thread_task_runner_ = decoder_thread_.task_runner(); | |
| 1453 } | |
| 1454 | |
| 1455 bool DXVAVideoDecodeAccelerator::OutputSamplesPresent() { | |
| 1456 base::AutoLock lock(decoder_lock_); | |
| 1457 return !pending_output_samples_.empty(); | |
| 1458 } | |
| 1459 | |
| 1369 } // namespace content | 1460 } // namespace content |
| OLD | NEW |