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

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

Issue 765533005: Move the DXVA decoder off the GPU thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed flush Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/gpu/media/dxva_video_decode_accelerator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include <ks.h> 11 #include <ks.h>
12 #include <codecapi.h> 12 #include <codecapi.h>
13 #include <mfapi.h> 13 #include <mfapi.h>
14 #include <mferror.h> 14 #include <mferror.h>
15 #include <wmcodecdsp.h> 15 #include <wmcodecdsp.h>
16 16
17 #include "base/base_paths_win.h" 17 #include "base/base_paths_win.h"
18 #include "base/bind.h" 18 #include "base/bind.h"
19 #include "base/callback.h" 19 #include "base/callback.h"
20 #include "base/command_line.h" 20 #include "base/command_line.h"
21 #include "base/debug/trace_event.h" 21 #include "base/debug/trace_event.h"
22 #include "base/file_version_info.h" 22 #include "base/file_version_info.h"
23 #include "base/files/file_path.h" 23 #include "base/files/file_path.h"
24 #include "base/logging.h" 24 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/shared_memory.h" 26 #include "base/memory/shared_memory.h"
27 #include "base/message_loop/message_loop.h" 27 #include "base/message_loop/message_loop.h"
28 #include "base/path_service.h" 28 #include "base/path_service.h"
29 #include "base/thread_task_runner_handle.h"
29 #include "base/win/windows_version.h" 30 #include "base/win/windows_version.h"
30 #include "media/video/video_decode_accelerator.h" 31 #include "media/video/video_decode_accelerator.h"
31 #include "ui/gl/gl_bindings.h" 32 #include "ui/gl/gl_bindings.h"
32 #include "ui/gl/gl_surface_egl.h" 33 #include "ui/gl/gl_surface_egl.h"
33 #include "ui/gl/gl_switches.h" 34 #include "ui/gl/gl_switches.h"
34 35
35 namespace { 36 namespace {
36 37
37 // Path is appended on to the PROGRAM_FILES base path. 38 // Path is appended on to the PROGRAM_FILES base path.
38 const wchar_t kVPXDecoderDLLPath[] = L"Intel\\Media SDK\\"; 39 const wchar_t kVPXDecoderDLLPath[] = L"Intel\\Media SDK\\";
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( 482 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator(
482 const base::Callback<bool(void)>& make_context_current) 483 const base::Callback<bool(void)>& make_context_current)
483 : client_(NULL), 484 : client_(NULL),
484 dev_manager_reset_token_(0), 485 dev_manager_reset_token_(0),
485 egl_config_(NULL), 486 egl_config_(NULL),
486 state_(kUninitialized), 487 state_(kUninitialized),
487 pictures_requested_(false), 488 pictures_requested_(false),
488 inputs_before_decode_(0), 489 inputs_before_decode_(0),
489 make_context_current_(make_context_current), 490 make_context_current_(make_context_current),
490 weak_this_factory_(this), 491 weak_this_factory_(this),
491 codec_(media::kUnknownVideoCodec) { 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
619 if (state == kFlushing)
609 FlushInternal(); 620 FlushInternal();
610 } 621 }
611 622
612 void DXVAVideoDecodeAccelerator::ReusePictureBuffer( 623 void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
613 int32 picture_buffer_id) { 624 int32 picture_buffer_id) {
614 DCHECK(CalledOnValidThread()); 625 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
615 626
616 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), 627 State state = GetState();
617 "Invalid state: " << state_, ILLEGAL_STATE,); 628 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized),
629 "Invalid state: " << state, ILLEGAL_STATE,);
618 630
619 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) 631 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty())
620 return; 632 return;
621 633
622 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id); 634 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 635 // 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 636 // 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 637 // 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 638 // 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 639 // us that we can now recycle this picture buffer, so if we were waiting to
628 // dispose of it we now can. 640 // dispose of it we now can.
629 if (it == output_picture_buffers_.end()) { 641 if (it == output_picture_buffers_.end()) {
630 it = stale_output_picture_buffers_.find(picture_buffer_id); 642 it = stale_output_picture_buffers_.find(picture_buffer_id);
631 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(), 643 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(),
632 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,); 644 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,);
633 base::MessageLoop::current()->PostTask(FROM_HERE, 645 main_thread_task_runner_->PostTask(
646 FROM_HERE,
634 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer, 647 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer,
635 weak_this_factory_.GetWeakPtr(), picture_buffer_id)); 648 weak_this_factory_.GetWeakPtr(), picture_buffer_id));
636 return; 649 return;
637 } 650 }
638 651
639 it->second->ReusePictureBuffer(); 652 it->second->ReusePictureBuffer();
640 ProcessPendingSamples(); 653 ProcessPendingSamples();
641 654
642 if (state_ == kFlushing && pending_output_samples_.empty()) 655 if (state == kFlushing)
643 FlushInternal(); 656 FlushInternal();
644 } 657 }
645 658
646 void DXVAVideoDecodeAccelerator::Flush() { 659 void DXVAVideoDecodeAccelerator::Flush() {
647 DCHECK(CalledOnValidThread()); 660 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
648 661
649 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; 662 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
650 663
651 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), 664 State state = GetState();
652 "Unexpected decoder state: " << state_, ILLEGAL_STATE,); 665 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped),
666 "Unexpected decoder state: " << state, ILLEGAL_STATE,);
653 667
654 state_ = kFlushing; 668 SetState(kFlushing);
655 669
656 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), 670 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0),
657 "Failed to send drain message", PLATFORM_FAILURE,); 671 "Failed to send drain message", PLATFORM_FAILURE,);
658 672
659 if (!pending_output_samples_.empty())
660 return;
661
662 FlushInternal(); 673 FlushInternal();
663 } 674 }
664 675
665 void DXVAVideoDecodeAccelerator::Reset() { 676 void DXVAVideoDecodeAccelerator::Reset() {
666 DCHECK(CalledOnValidThread()); 677 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
667 678
668 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; 679 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
669 680
670 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), 681 State state = GetState();
671 "Reset: invalid state: " << state_, ILLEGAL_STATE,); 682 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped),
683 "Reset: invalid state: " << state, ILLEGAL_STATE,);
672 684
673 state_ = kResetting; 685 decoder_thread_.Stop();
686
687 SetState(kResetting);
674 688
675 pending_output_samples_.clear(); 689 pending_output_samples_.clear();
676 690
677 NotifyInputBuffersDropped(); 691 NotifyInputBuffersDropped();
678 692
679 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), 693 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0),
680 "Reset: Failed to send message.", PLATFORM_FAILURE,); 694 "Reset: Failed to send message.", PLATFORM_FAILURE,);
681 695
682 base::MessageLoop::current()->PostTask( 696 main_thread_task_runner_->PostTask(
683 FROM_HERE, 697 FROM_HERE,
684 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, 698 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone,
685 weak_this_factory_.GetWeakPtr())); 699 weak_this_factory_.GetWeakPtr()));
686 700
687 state_ = DXVAVideoDecodeAccelerator::kNormal; 701 StartDecoderThread();
702 SetState(kNormal);
688 } 703 }
689 704
690 void DXVAVideoDecodeAccelerator::Destroy() { 705 void DXVAVideoDecodeAccelerator::Destroy() {
691 DCHECK(CalledOnValidThread()); 706 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
692 Invalidate(); 707 Invalidate();
693 delete this; 708 delete this;
694 } 709 }
695 710
696 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() { 711 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() {
697 return false; 712 return false;
698 } 713 }
699 714
700 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) { 715 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) {
701 HMODULE decoder_dll = NULL; 716 HMODULE decoder_dll = NULL;
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 << std::hex << std::showbase << output_stream_info_.dwFlags; 937 << std::hex << std::showbase << output_stream_info_.dwFlags;
923 if (codec_ == media::kCodecH264) { 938 if (codec_ == media::kCodecH264) {
924 CHECK_EQ(output_stream_info_.dwFlags, 0x107u); 939 CHECK_EQ(output_stream_info_.dwFlags, 0x107u);
925 } 940 }
926 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize; 941 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize;
927 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment; 942 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment;
928 return true; 943 return true;
929 } 944 }
930 945
931 void DXVAVideoDecodeAccelerator::DoDecode() { 946 void DXVAVideoDecodeAccelerator::DoDecode() {
947 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
932 // This function is also called from FlushInternal in a loop which could 948 // 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. 949 // result in the state transitioning to kStopped due to no decoded output.
950 State state = GetState();
934 RETURN_AND_NOTIFY_ON_FAILURE( 951 RETURN_AND_NOTIFY_ON_FAILURE(
935 (state_ == kNormal || state_ == kFlushing || 952 (state == kNormal || state == kFlushing ||
936 state_ == kStopped || state_ == kFlushingPendingInputBuffers), 953 state == kStopped || state == kFlushingPendingInputBuffers),
937 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); 954 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,);
938 955
939 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; 956 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0};
940 DWORD status = 0; 957 DWORD status = 0;
941 958
942 HRESULT hr = decoder_->ProcessOutput(0, // No flags 959 HRESULT hr = decoder_->ProcessOutput(0, // No flags
943 1, // # of out streams to pull from 960 1, // # of out streams to pull from
944 &output_data_buffer, 961 &output_data_buffer,
945 &status); 962 &status);
946 IMFCollection* events = output_data_buffer.pEvents; 963 IMFCollection* events = output_data_buffer.pEvents;
947 if (events != NULL) { 964 if (events != NULL) {
948 DVLOG(1) << "Got events from ProcessOuput, but discarding"; 965 DVLOG(1) << "Got events from ProcessOuput, but discarding";
949 events->Release(); 966 events->Release();
950 } 967 }
951 if (FAILED(hr)) { 968 if (FAILED(hr)) {
952 // A stream change needs further ProcessInput calls to get back decoder 969 // A stream change needs further ProcessInput calls to get back decoder
953 // output which is why we need to set the state to stopped. 970 // output which is why we need to set the state to stopped.
954 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) { 971 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
955 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) { 972 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) {
956 // Decoder didn't let us set NV12 output format. Not sure as to why 973 // Decoder didn't let us set NV12 output format. Not sure as to why
957 // this can happen. Give up in disgust. 974 // this can happen. Give up in disgust.
958 NOTREACHED() << "Failed to set decoder output media type to NV12"; 975 NOTREACHED() << "Failed to set decoder output media type to NV12";
959 state_ = kStopped; 976 SetState(kStopped);
960 } else { 977 } else {
961 DVLOG(1) << "Received output format change from the decoder." 978 DVLOG(1) << "Received output format change from the decoder."
962 " Recursively invoking DoDecode"; 979 " Recursively invoking DoDecode";
963 DoDecode(); 980 DoDecode();
964 } 981 }
965 return; 982 return;
966 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { 983 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
967 // No more output from the decoder. Stop playback. 984 // No more output from the decoder. Stop playback.
968 if (state_ != kFlushingPendingInputBuffers) 985 if (state != kFlushingPendingInputBuffers)
969 state_ = kStopped; 986 SetState(kStopped);
970 return; 987 return;
971 } else { 988 } else {
972 NOTREACHED() << "Unhandled error in DoDecode()"; 989 NOTREACHED() << "Unhandled error in DoDecode()";
973 return; 990 return;
974 } 991 }
975 } 992 }
976 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); 993 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
977 994
978 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode", 995 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode",
979 inputs_before_decode_); 996 inputs_before_decode_);
(...skipping 15 matching lines...) Expand all
995 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE, 1012 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE,
996 IID_PPV_ARGS(surface.Receive())); 1013 IID_PPV_ARGS(surface.Receive()));
997 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample", 1014 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample",
998 false); 1015 false);
999 1016
1000 LONGLONG input_buffer_id = 0; 1017 LONGLONG input_buffer_id = 0;
1001 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), 1018 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
1002 "Failed to get input buffer id associated with sample", 1019 "Failed to get input buffer id associated with sample",
1003 false); 1020 false);
1004 1021
1022 decoder_lock_.Acquire();
1005 pending_output_samples_.push_back( 1023 pending_output_samples_.push_back(
1006 PendingSampleInfo(input_buffer_id, sample)); 1024 PendingSampleInfo(input_buffer_id, sample));
1025 decoder_lock_.Release();
1007 1026
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_) { 1027 if (pictures_requested_) {
1015 DVLOG(1) << "Waiting for picture slots from the client."; 1028 DVLOG(1) << "Waiting for picture slots from the client.";
1029 main_thread_task_runner_->PostTask(
1030 FROM_HERE,
1031 base::Bind(&DXVAVideoDecodeAccelerator::ProcessPendingSamples,
1032 weak_this_factory_.GetWeakPtr()));
1016 return true; 1033 return true;
1017 } 1034 }
1018 1035
1019 // We only read the surface description, which contains its width/height when 1036 // 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 1037 // we need the picture buffers from the client. Once we have those, then they
1021 // are reused. 1038 // are reused.
1022 D3DSURFACE_DESC surface_desc; 1039 D3DSURFACE_DESC surface_desc;
1023 hr = surface->GetDesc(&surface_desc); 1040 hr = surface->GetDesc(&surface_desc);
1024 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); 1041 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false);
1025 1042
1026 // Go ahead and request picture buffers. 1043 // Go ahead and request picture buffers.
1027 base::MessageLoop::current()->PostTask( 1044 main_thread_task_runner_->PostTask(
1028 FROM_HERE, 1045 FROM_HERE,
1029 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, 1046 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
1030 weak_this_factory_.GetWeakPtr(), 1047 weak_this_factory_.GetWeakPtr(),
1031 surface_desc.Width, 1048 surface_desc.Width,
1032 surface_desc.Height)); 1049 surface_desc.Height));
1033 1050
1034 pictures_requested_ = true; 1051 pictures_requested_ = true;
1035 return true; 1052 return true;
1036 } 1053 }
1037 1054
1038 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { 1055 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() {
1056 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1057
1058 if (!output_picture_buffers_.size())
1059 return;
1060
1039 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(), 1061 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(),
1040 "Failed to make context current", PLATFORM_FAILURE,); 1062 "Failed to make context current", PLATFORM_FAILURE,);
1041 1063
1042 OutputBuffers::iterator index; 1064 OutputBuffers::iterator index;
1043 1065
1066 base::AutoLock lock(decoder_lock_);
1067
1044 for (index = output_picture_buffers_.begin(); 1068 for (index = output_picture_buffers_.begin();
1045 index != output_picture_buffers_.end() && 1069 index != output_picture_buffers_.end() &&
1046 !pending_output_samples_.empty(); 1070 !pending_output_samples_.empty();
1047 ++index) { 1071 ++index) {
1048 if (index->second->available()) { 1072 if (index->second->available()) {
1049 PendingSampleInfo sample_info = pending_output_samples_.front(); 1073 PendingSampleInfo sample_info = pending_output_samples_.front();
1050 1074
1051 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; 1075 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
1052 HRESULT hr = sample_info.output_sample->GetBufferByIndex( 1076 HRESULT hr = sample_info.output_sample->GetBufferByIndex(
1053 0, output_buffer.Receive()); 1077 0, output_buffer.Receive());
(...skipping 21 matching lines...) Expand all
1075 } 1099 }
1076 1100
1077 RETURN_AND_NOTIFY_ON_FAILURE( 1101 RETURN_AND_NOTIFY_ON_FAILURE(
1078 index->second->CopyOutputSampleDataToPictureBuffer(*this, 1102 index->second->CopyOutputSampleDataToPictureBuffer(*this,
1079 surface.get()), 1103 surface.get()),
1080 "Failed to copy output sample", PLATFORM_FAILURE, ); 1104 "Failed to copy output sample", PLATFORM_FAILURE, );
1081 1105
1082 media::Picture output_picture(index->second->id(), 1106 media::Picture output_picture(index->second->id(),
1083 sample_info.input_buffer_id, 1107 sample_info.input_buffer_id,
1084 gfx::Rect(index->second->size())); 1108 gfx::Rect(index->second->size()));
1085 base::MessageLoop::current()->PostTask( 1109 main_thread_task_runner_->PostTask(
1086 FROM_HERE, 1110 FROM_HERE,
1087 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, 1111 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady,
1088 weak_this_factory_.GetWeakPtr(), 1112 weak_this_factory_.GetWeakPtr(),
1089 output_picture)); 1113 output_picture));
1090 1114
1091 index->second->set_available(false); 1115 index->second->set_available(false);
1092 pending_output_samples_.pop_front(); 1116 pending_output_samples_.pop_front();
1093 } 1117 }
1094 } 1118 }
1095 1119 if (GetState() == kFlushing) {
1096 if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) { 1120 decoder_thread_task_runner_->PostTask(
1097 base::MessageLoop::current()->PostTask(
1098 FROM_HERE, 1121 FROM_HERE,
1099 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, 1122 base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal,
1100 weak_this_factory_.GetWeakPtr())); 1123 base::Unretained(this)));
1101 } 1124 }
1125 decoder_thread_task_runner_->PostTask(
1126 FROM_HERE,
1127 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1128 base::Unretained(this)));
1102 } 1129 }
1103 1130
1104 void DXVAVideoDecodeAccelerator::StopOnError( 1131 void DXVAVideoDecodeAccelerator::StopOnError(
1105 media::VideoDecodeAccelerator::Error error) { 1132 media::VideoDecodeAccelerator::Error error) {
1106 DCHECK(CalledOnValidThread()); 1133 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
1134 main_thread_task_runner_->PostTask(
1135 FROM_HERE,
1136 base::Bind(&DXVAVideoDecodeAccelerator::StopOnError,
1137 weak_this_factory_.GetWeakPtr(),
1138 error));
1139 return;
1140 }
1107 1141
1108 if (client_) 1142 if (client_)
1109 client_->NotifyError(error); 1143 client_->NotifyError(error);
1110 client_ = NULL; 1144 client_ = NULL;
1111 1145
1112 if (state_ != kUninitialized) { 1146 if (GetState() != kUninitialized) {
1113 Invalidate(); 1147 Invalidate();
1114 } 1148 }
1115 } 1149 }
1116 1150
1117 void DXVAVideoDecodeAccelerator::Invalidate() { 1151 void DXVAVideoDecodeAccelerator::Invalidate() {
1118 if (state_ == kUninitialized) 1152 if (GetState() == kUninitialized)
1119 return; 1153 return;
1154 decoder_thread_.Stop();
1120 weak_this_factory_.InvalidateWeakPtrs(); 1155 weak_this_factory_.InvalidateWeakPtrs();
1121 output_picture_buffers_.clear(); 1156 output_picture_buffers_.clear();
1122 stale_output_picture_buffers_.clear(); 1157 stale_output_picture_buffers_.clear();
1123 pending_output_samples_.clear(); 1158 pending_output_samples_.clear();
1124 pending_input_buffers_.clear(); 1159 pending_input_buffers_.clear();
1125 decoder_.Release(); 1160 decoder_.Release();
1126 MFShutdown(); 1161 MFShutdown();
1127 state_ = kUninitialized; 1162 SetState(kUninitialized);
1128 } 1163 }
1129 1164
1130 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { 1165 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) {
1166 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1131 if (client_) 1167 if (client_)
1132 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); 1168 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1133 } 1169 }
1134 1170
1135 void DXVAVideoDecodeAccelerator::NotifyFlushDone() { 1171 void DXVAVideoDecodeAccelerator::NotifyFlushDone() {
1136 if (client_) 1172 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1173 if (client_ && GetState() == kStopped)
ananta 2014/12/04 00:45:24 Safety check to prevent multiple notifications goi
1137 client_->NotifyFlushDone(); 1174 client_->NotifyFlushDone();
1138 } 1175 }
1139 1176
1140 void DXVAVideoDecodeAccelerator::NotifyResetDone() { 1177 void DXVAVideoDecodeAccelerator::NotifyResetDone() {
1178 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1141 if (client_) 1179 if (client_)
1142 client_->NotifyResetDone(); 1180 client_->NotifyResetDone();
1143 } 1181 }
1144 1182
1145 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { 1183 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) {
1184 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1146 // This task could execute after the decoder has been torn down. 1185 // This task could execute after the decoder has been torn down.
1147 if (state_ != kUninitialized && client_) { 1186 if (GetState() != kUninitialized && client_) {
1148 client_->ProvidePictureBuffers( 1187 client_->ProvidePictureBuffers(
1149 kNumPictureBuffers, 1188 kNumPictureBuffers,
1150 gfx::Size(width, height), 1189 gfx::Size(width, height),
1151 GL_TEXTURE_2D); 1190 GL_TEXTURE_2D);
1152 } 1191 }
1153 } 1192 }
1154 1193
1155 void DXVAVideoDecodeAccelerator::NotifyPictureReady( 1194 void DXVAVideoDecodeAccelerator::NotifyPictureReady(
1156 const media::Picture& picture) { 1195 const media::Picture& picture) {
1196 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1157 // This task could execute after the decoder has been torn down. 1197 // This task could execute after the decoder has been torn down.
1158 if (state_ != kUninitialized && client_) 1198 if (GetState() != kUninitialized && client_)
1159 client_->PictureReady(picture); 1199 client_->PictureReady(picture);
1160 } 1200 }
1161 1201
1162 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { 1202 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() {
1163 if (!client_ || !pending_output_samples_.empty()) 1203 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
1204 if (!client_)
1164 return; 1205 return;
1165 1206
1166 for (PendingInputs::iterator it = pending_input_buffers_.begin(); 1207 for (PendingInputs::iterator it = pending_input_buffers_.begin();
1167 it != pending_input_buffers_.end(); ++it) { 1208 it != pending_input_buffers_.end(); ++it) {
1168 LONGLONG input_buffer_id = 0; 1209 LONGLONG input_buffer_id = 0;
1169 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), 1210 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id),
1170 "Failed to get buffer id associated with sample",); 1211 "Failed to get buffer id associated with sample",);
1171 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); 1212 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1172 } 1213 }
1173 pending_input_buffers_.clear(); 1214 pending_input_buffers_.clear();
1174 } 1215 }
1175 1216
1176 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { 1217 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() {
1177 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), 1218 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
1178 "Invalid state: " << state_, ILLEGAL_STATE,); 1219 State state = GetState();
1220 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized),
1221 "Invalid state: " << state, ILLEGAL_STATE,);
1179 1222
1180 if (pending_input_buffers_.empty() || !pending_output_samples_.empty()) 1223 if (pending_input_buffers_.empty() || OutputSamplesPresent())
1181 return; 1224 return;
1182 1225
1183 PendingInputs pending_input_buffers_copy; 1226 PendingInputs pending_input_buffers_copy;
1184 std::swap(pending_input_buffers_, pending_input_buffers_copy); 1227 std::swap(pending_input_buffers_, pending_input_buffers_copy);
1185 1228
1186 for (PendingInputs::iterator it = pending_input_buffers_copy.begin(); 1229 for (PendingInputs::iterator it = pending_input_buffers_copy.begin();
1187 it != pending_input_buffers_copy.end(); ++it) { 1230 it != pending_input_buffers_copy.end(); ++it) {
1188 DecodeInternal(*it); 1231 DecodeInternal(*it);
1189 } 1232 }
1190 1233
1191 if (state_ != kFlushingPendingInputBuffers) 1234 if (state != kFlushingPendingInputBuffers)
1192 return; 1235 return;
1193 1236
1194 // If we are scheduled during a flush operation then mark the flush as 1237 // 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. 1238 // 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 1239 // If we don't have available output slots then this function will be
1197 // scheduled again by the ProcessPendingSamples function once slots become 1240 // scheduled again by the ProcessPendingSamples function once slots become
1198 // available. 1241 // available.
1199 if (pending_input_buffers_.empty() && pending_output_samples_.empty()) { 1242 if (pending_input_buffers_.empty() && !OutputSamplesPresent()) {
1200 state_ = kNormal; 1243 SetState(kNormal);
1201 base::MessageLoop::current()->PostTask( 1244 main_thread_task_runner_->PostTask(
1202 FROM_HERE, 1245 FROM_HERE,
1203 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, 1246 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1204 weak_this_factory_.GetWeakPtr())); 1247 weak_this_factory_.GetWeakPtr()));
1205 } 1248 }
1206 } 1249 }
1207 1250
1208 void DXVAVideoDecodeAccelerator::FlushInternal() { 1251 void DXVAVideoDecodeAccelerator::FlushInternal() {
1252 if (!decoder_thread_task_runner_->BelongsToCurrentThread()) {
1253 decoder_thread_task_runner_->PostTask(
1254 FROM_HERE,
1255 base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal,
1256 base::Unretained(this)));
1257 return;
1258 }
1259
1260 // We allow only one output frame to be present at any given time. If we have
1261 // an output frame, then we cannot complete the flush at this time.
1262 if (OutputSamplesPresent())
1263 return;
1264
1209 // The DoDecode function sets the state to kStopped when the decoder returns 1265 // The DoDecode function sets the state to kStopped when the decoder returns
1210 // MF_E_TRANSFORM_NEED_MORE_INPUT. 1266 // MF_E_TRANSFORM_NEED_MORE_INPUT.
1211 // The MFT decoder can buffer upto 30 frames worth of input before returning 1267 // 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 1268 // an output frame. This loop here attempts to retrieve as many output frames
1213 // as possible from the buffered set. 1269 // as possible from the buffered set.
1214 while (state_ != kStopped) { 1270 while (GetState() != kStopped) {
1215 DoDecode(); 1271 DoDecode();
1216 if (!pending_output_samples_.empty()) 1272 if (OutputSamplesPresent())
1217 return; 1273 return;
1218 } 1274 }
1219 1275
1220 // TODO(ananta) 1276 // TODO(ananta)
1221 // Look into whether we can simplify this function by combining the while 1277 // 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 1278 // 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 1279 // transitions in the decoder are a touch intertwined with other portions of
1224 // the code like AssignPictureBuffers, ReusePictureBuffers etc. 1280 // the code like AssignPictureBuffers, ReusePictureBuffers etc.
1225 if (!pending_input_buffers_.empty()) { 1281 if (!pending_input_buffers_.empty()) {
1226 state_ = kFlushingPendingInputBuffers; 1282 SetState(kFlushingPendingInputBuffers);
1227 base::MessageLoop::current()->PostTask( 1283 decoder_thread_task_runner_->PostTask(
1228 FROM_HERE, 1284 FROM_HERE,
1229 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, 1285 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1230 weak_this_factory_.GetWeakPtr())); 1286 base::Unretained(this)));
1231 return; 1287 return;
1232 } 1288 }
1233 1289
1234 base::MessageLoop::current()->PostTask( 1290 main_thread_task_runner_->PostTask(
1235 FROM_HERE, 1291 FROM_HERE,
1236 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, 1292 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1237 weak_this_factory_.GetWeakPtr())); 1293 weak_this_factory_.GetWeakPtr()));
1238 1294
1239 state_ = kNormal; 1295 SetState(kNormal);
1240 } 1296 }
1241 1297
1242 void DXVAVideoDecodeAccelerator::DecodeInternal( 1298 void DXVAVideoDecodeAccelerator::DecodeInternal(
1243 const base::win::ScopedComPtr<IMFSample>& sample) { 1299 const base::win::ScopedComPtr<IMFSample>& sample) {
1244 DCHECK(CalledOnValidThread()); 1300 DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
1245 1301
1246 if (state_ == kUninitialized) 1302 if (GetState() == kUninitialized)
1247 return; 1303 return;
1248 1304
1249 if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) { 1305 if (OutputSamplesPresent() || !pending_input_buffers_.empty()) {
1250 pending_input_buffers_.push_back(sample); 1306 pending_input_buffers_.push_back(sample);
1251 return; 1307 return;
1252 } 1308 }
1253 1309
1254 if (!inputs_before_decode_) { 1310 if (!inputs_before_decode_) {
1255 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); 1311 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
1256 } 1312 }
1257 inputs_before_decode_++; 1313 inputs_before_decode_++;
1258 1314
1259 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0); 1315 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0);
1260 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it 1316 // 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 1317 // has enough data to produce one or more output samples. In this case the
1262 // recommended options are to 1318 // recommended options are to
1263 // 1. Generate new output by calling IMFTransform::ProcessOutput until it 1319 // 1. Generate new output by calling IMFTransform::ProcessOutput until it
1264 // returns MF_E_TRANSFORM_NEED_MORE_INPUT. 1320 // returns MF_E_TRANSFORM_NEED_MORE_INPUT.
1265 // 2. Flush the input data 1321 // 2. Flush the input data
1266 // We implement the first option, i.e to retrieve the output sample and then 1322 // 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 1323 // process the input again. Failure in either of these steps is treated as a
1268 // decoder failure. 1324 // decoder failure.
1269 if (hr == MF_E_NOTACCEPTING) { 1325 if (hr == MF_E_NOTACCEPTING) {
1270 DoDecode(); 1326 DoDecode();
1271 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal), 1327 State state = GetState();
1272 "Failed to process output. Unexpected decoder state: " << state_, 1328 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal ||
1329 state == kFlushing),
1330 "Failed to process output. Unexpected decoder state: " << state,
1273 PLATFORM_FAILURE,); 1331 PLATFORM_FAILURE,);
1274 hr = decoder_->ProcessInput(0, sample.get(), 0); 1332 hr = decoder_->ProcessInput(0, sample.get(), 0);
1275 // If we continue to get the MF_E_NOTACCEPTING error we do the following:- 1333 // If we continue to get the MF_E_NOTACCEPTING error we do the following:-
1276 // 1. Add the input sample to the pending queue. 1334 // 1. Add the input sample to the pending queue.
1277 // 2. If we don't have any output samples we post the 1335 // 2. If we don't have any output samples we post the
1278 // DecodePendingInputBuffers task to process the pending input samples. 1336 // DecodePendingInputBuffers task to process the pending input samples.
1279 // If we have an output sample then the above task is posted when the 1337 // If we have an output sample then the above task is posted when the
1280 // output samples are sent to the client. 1338 // output samples are sent to the client.
1281 // This is because we only support 1 pending output sample at any 1339 // This is because we only support 1 pending output sample at any
1282 // given time due to the limitation with the Microsoft media foundation 1340 // given time due to the limitation with the Microsoft media foundation
1283 // decoder where it recycles the output Decoder surfaces. 1341 // decoder where it recycles the output Decoder surfaces.
1284 if (hr == MF_E_NOTACCEPTING) { 1342 if (hr == MF_E_NOTACCEPTING) {
1285 pending_input_buffers_.push_back(sample); 1343 pending_input_buffers_.push_back(sample);
1286 if (pending_output_samples_.empty()) { 1344 if (!OutputSamplesPresent()) {
1287 base::MessageLoop::current()->PostTask( 1345 decoder_thread_task_runner_->PostTask(
1288 FROM_HERE, 1346 FROM_HERE,
1289 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, 1347 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1290 weak_this_factory_.GetWeakPtr())); 1348 base::Unretained(this)));
1291 } 1349 }
1292 return; 1350 return;
1293 } 1351 }
1294 } 1352 }
1295 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample", 1353 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample",
1296 PLATFORM_FAILURE,); 1354 PLATFORM_FAILURE,);
1297 1355
1298 DoDecode(); 1356 DoDecode();
1299 1357
1300 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal || 1358 State state = GetState();
1301 state_ == kFlushingPendingInputBuffers), 1359 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal ||
1302 "Failed to process output. Unexpected decoder state: " << state_, 1360 state == kFlushingPendingInputBuffers ||
1361 state == kFlushing),
1362 "Failed to process output. Unexpected decoder state: " << state,
1303 ILLEGAL_STATE,); 1363 ILLEGAL_STATE,);
1304 1364
1305 LONGLONG input_buffer_id = 0; 1365 LONGLONG input_buffer_id = 0;
1306 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), 1366 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
1307 "Failed to get input buffer id associated with sample",); 1367 "Failed to get input buffer id associated with sample",);
1308 // The Microsoft Media foundation decoder internally buffers up to 30 frames 1368 // 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 1369 // 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. 1370 // 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 1371 // Note: This may break clients which expect every input buffer to be
1312 // associated with a decoded output buffer. 1372 // associated with a decoded output buffer.
1313 // TODO(ananta) 1373 // TODO(ananta)
1314 // Do some more investigation into whether it is possible to get the MFT 1374 // Do some more investigation into whether it is possible to get the MFT
1315 // decoder to emit an output packet for every input packet. 1375 // decoder to emit an output packet for every input packet.
1316 // http://code.google.com/p/chromium/issues/detail?id=108121 1376 // http://code.google.com/p/chromium/issues/detail?id=108121
1317 // http://code.google.com/p/chromium/issues/detail?id=150925 1377 // http://code.google.com/p/chromium/issues/detail?id=150925
1318 base::MessageLoop::current()->PostTask( 1378 main_thread_task_runner_->PostTask(
1319 FROM_HERE, 1379 FROM_HERE,
1320 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, 1380 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead,
1321 weak_this_factory_.GetWeakPtr(), 1381 weak_this_factory_.GetWeakPtr(),
1322 input_buffer_id)); 1382 input_buffer_id));
1323 } 1383 }
1324 1384
1325 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width, 1385 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width,
1326 int height) { 1386 int height) {
1327 base::MessageLoop::current()->PostTask( 1387 main_thread_task_runner_->PostTask(
1328 FROM_HERE, 1388 FROM_HERE,
1329 base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers, 1389 base::Bind(&DXVAVideoDecodeAccelerator::DismissStaleBuffers,
1330 weak_this_factory_.GetWeakPtr())); 1390 weak_this_factory_.GetWeakPtr()));
1331 1391
1332 base::MessageLoop::current()->PostTask( 1392 main_thread_task_runner_->PostTask(
1333 FROM_HERE, 1393 FROM_HERE,
1334 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, 1394 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
1335 weak_this_factory_.GetWeakPtr(), 1395 weak_this_factory_.GetWeakPtr(),
1336 width, 1396 width,
1337 height)); 1397 height));
1338 } 1398 }
1339 1399
1340 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() { 1400 void DXVAVideoDecodeAccelerator::DismissStaleBuffers() {
1341 OutputBuffers::iterator index; 1401 OutputBuffers::iterator index;
1342 1402
(...skipping 16 matching lines...) Expand all
1359 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( 1419 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer(
1360 int32 picture_buffer_id) { 1420 int32 picture_buffer_id) {
1361 OutputBuffers::iterator it = stale_output_picture_buffers_.find( 1421 OutputBuffers::iterator it = stale_output_picture_buffers_.find(
1362 picture_buffer_id); 1422 picture_buffer_id);
1363 DCHECK(it != stale_output_picture_buffers_.end()); 1423 DCHECK(it != stale_output_picture_buffers_.end());
1364 DVLOG(1) << "Dismissing picture id: " << it->second->id(); 1424 DVLOG(1) << "Dismissing picture id: " << it->second->id();
1365 client_->DismissPictureBuffer(it->second->id()); 1425 client_->DismissPictureBuffer(it->second->id());
1366 stale_output_picture_buffers_.erase(it); 1426 stale_output_picture_buffers_.erase(it);
1367 } 1427 }
1368 1428
1429 DXVAVideoDecodeAccelerator::State
1430 DXVAVideoDecodeAccelerator::GetState() const {
1431 State state = kUninitialized;
1432 ::InterlockedExchange(reinterpret_cast<long*>(&state),
1433 state_);
1434 return state;
1435 }
1436
1437 void DXVAVideoDecodeAccelerator::SetState(State new_state) {
1438 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
1439 main_thread_task_runner_->PostTask(
1440 FROM_HERE,
1441 base::Bind(&DXVAVideoDecodeAccelerator::SetState,
1442 weak_this_factory_.GetWeakPtr(),
1443 new_state));
1444 return;
1445 }
1446 ::InterlockedCompareExchange(reinterpret_cast<long*>(&state_),
1447 new_state, state_);
1448 }
1449
1450 void DXVAVideoDecodeAccelerator::StartDecoderThread() {
1451 decoder_thread_.init_com_with_mta(false);
1452 decoder_thread_.Start();
1453 decoder_thread_task_runner_ = decoder_thread_.task_runner();
1454 }
1455
1456 bool DXVAVideoDecodeAccelerator::OutputSamplesPresent() {
1457 base::AutoLock lock(decoder_lock_);
1458 return !pending_output_samples_.empty();
1459 }
1460
1369 } // namespace content 1461 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/media/dxva_video_decode_accelerator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698