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