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 <dlfcn.h> | 5 #include <dlfcn.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "content/common/gpu/gl_scoped_binders.h" | 12 #include "content/common/gpu/gl_scoped_binders.h" |
12 #include "content/common/gpu/media/vaapi_h264_decoder.h" | 13 #include "content/common/gpu/media/vaapi_h264_decoder.h" |
13 #include "third_party/libva/va/va.h" | 14 #include "third_party/libva/va/va.h" |
14 #include "third_party/libva/va/va_x11.h" | 15 #include "third_party/libva/va/va_x11.h" |
15 #include "ui/gl/gl_bindings.h" | 16 #include "ui/gl/gl_bindings.h" |
16 | 17 |
17 #define VA_LOG_ON_ERROR(va_res, err_msg) \ | 18 #define VA_LOG_ON_ERROR(va_res, err_msg) \ |
18 do { \ | 19 do { \ |
19 if ((va_res) != VA_STATUS_SUCCESS) { \ | 20 if ((va_res) != VA_STATUS_SUCCESS) { \ |
20 DVLOG(1) << err_msg \ | 21 DVLOG(1) << err_msg \ |
21 << " VA error: " << VAAPI_ErrorStr(va_res); \ | 22 << " VA error: " << VAAPI_ErrorStr(va_res); \ |
22 } \ | 23 } \ |
23 } while(0) | 24 } while (0) |
24 | 25 |
25 #define VA_SUCCESS_OR_RETURN(va_res, err_msg, ret) \ | 26 #define VA_SUCCESS_OR_RETURN(va_res, err_msg, ret) \ |
26 do { \ | 27 do { \ |
27 if ((va_res) != VA_STATUS_SUCCESS) { \ | 28 if ((va_res) != VA_STATUS_SUCCESS) { \ |
28 DVLOG(1) << err_msg \ | 29 DVLOG(1) << err_msg \ |
29 << " VA error: " << VAAPI_ErrorStr(va_res); \ | 30 << " VA error: " << VAAPI_ErrorStr(va_res); \ |
30 return (ret); \ | 31 return (ret); \ |
31 } \ | 32 } \ |
32 } while (0) | 33 } while (0) |
33 | 34 |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 frame_num_ = 0; | 365 frame_num_ = 0; |
365 prev_frame_num_ = -1; | 366 prev_frame_num_ = -1; |
366 prev_frame_num_offset_ = -1; | 367 prev_frame_num_offset_ = -1; |
367 | 368 |
368 prev_ref_has_memmgmnt5_ = false; | 369 prev_ref_has_memmgmnt5_ = false; |
369 prev_ref_top_field_order_cnt_ = -1; | 370 prev_ref_top_field_order_cnt_ = -1; |
370 prev_ref_pic_order_cnt_msb_ = -1; | 371 prev_ref_pic_order_cnt_msb_ = -1; |
371 prev_ref_pic_order_cnt_lsb_ = -1; | 372 prev_ref_pic_order_cnt_lsb_ = -1; |
372 prev_ref_field_ = H264Picture::FIELD_NONE; | 373 prev_ref_field_ = H264Picture::FIELD_NONE; |
373 | 374 |
375 // When called from the constructor, although va_display_ is invalid, | |
376 // |pending_slice_bufs_| and |pending_va_bufs_| are empty. | |
377 DestroyPendingBuffers(); | |
378 | |
374 pending_slice_bufs_ = std::queue<VABufferID>(); | 379 pending_slice_bufs_ = std::queue<VABufferID>(); |
375 pending_va_bufs_ = std::queue<VABufferID>(); | 380 pending_va_bufs_ = std::queue<VABufferID>(); |
376 | 381 |
377 ref_pic_list0_.clear(); | 382 ref_pic_list0_.clear(); |
378 ref_pic_list1_.clear(); | 383 ref_pic_list1_.clear(); |
379 | 384 |
380 for (POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.begin(); | 385 for (POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.begin(); |
381 it != poc_to_decode_surfaces_.end(); ) { | 386 it != poc_to_decode_surfaces_.end(); ) { |
382 int poc = it->second->poc(); | 387 int poc = it->second->poc(); |
383 // Must be incremented before UnassignSurfaceFromPoC as this call | 388 // Must be incremented before UnassignSurfaceFromPoC as this call |
(...skipping 25 matching lines...) Expand all Loading... | |
409 case kDecoding: | 414 case kDecoding: |
410 case kAfterReset: | 415 case kAfterReset: |
411 case kError: | 416 case kError: |
412 destroy_surfaces = true; | 417 destroy_surfaces = true; |
413 // fallthrough | 418 // fallthrough |
414 case kInitialized: | 419 case kInitialized: |
415 if (!make_context_current_.Run()) | 420 if (!make_context_current_.Run()) |
416 break; | 421 break; |
417 if (destroy_surfaces) | 422 if (destroy_surfaces) |
418 DestroyVASurfaces(); | 423 DestroyVASurfaces(); |
424 DestroyPendingBuffers(); | |
419 va_res = VAAPI_DestroyConfig(va_display_, va_config_id_); | 425 va_res = VAAPI_DestroyConfig(va_display_, va_config_id_); |
420 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed"); | 426 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed"); |
421 va_res = VAAPI_Terminate(va_display_); | 427 va_res = VAAPI_Terminate(va_display_); |
422 VA_LOG_ON_ERROR(va_res, "vaTerminate failed"); | 428 VA_LOG_ON_ERROR(va_res, "vaTerminate failed"); |
423 // fallthrough | 429 // fallthrough |
424 case kUninitialized: | 430 case kUninitialized: |
425 break; | 431 break; |
426 } | 432 } |
427 | 433 |
428 state_ = kUninitialized; | 434 state_ = kUninitialized; |
429 } | 435 } |
430 | 436 |
431 // Maps Profile enum values to VaProfile values. | 437 // Maps Profile enum values to VaProfile values. |
432 bool VaapiH264Decoder::SetProfile(media::VideoCodecProfile profile) { | 438 bool VaapiH264Decoder::SetProfile(media::VideoCodecProfile profile) { |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 GetRequiredNumOfPictures(), | 599 GetRequiredNumOfPictures(), |
594 va_surface_ids_); | 600 va_surface_ids_); |
595 VA_SUCCESS_OR_RETURN(va_res, "vaCreateSurfaces failed", false); | 601 VA_SUCCESS_OR_RETURN(va_res, "vaCreateSurfaces failed", false); |
596 | 602 |
597 DCHECK(decode_surfaces_.empty()); | 603 DCHECK(decode_surfaces_.empty()); |
598 // And create a context associated with them. | 604 // And create a context associated with them. |
599 va_res = VAAPI_CreateContext(va_display_, va_config_id_, | 605 va_res = VAAPI_CreateContext(va_display_, va_config_id_, |
600 pic_width_, pic_height_, VA_PROGRESSIVE, | 606 pic_width_, pic_height_, VA_PROGRESSIVE, |
601 va_surface_ids_, GetRequiredNumOfPictures(), | 607 va_surface_ids_, GetRequiredNumOfPictures(), |
602 &va_context_id_); | 608 &va_context_id_); |
603 VA_SUCCESS_OR_RETURN(va_res, "vaCreateContext failed", false); | 609 |
610 if (va_res != VA_STATUS_SUCCESS) { | |
611 DVLOG(1) << "Error creating a decoding surface (binding to texture?)"; | |
Pawel Osciak
2012/08/04 02:43:58
FWIW this is not really binding to textures yet...
| |
612 VAAPI_DestroySurfaces(va_display_, va_surface_ids_, | |
613 GetRequiredNumOfPictures()); | |
614 return false; | |
615 } | |
604 | 616 |
605 return true; | 617 return true; |
606 } | 618 } |
607 | 619 |
608 void VaapiH264Decoder::DestroyVASurfaces() { | 620 void VaapiH264Decoder::DestroyVASurfaces() { |
609 DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset); | 621 DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset); |
610 decode_surfaces_.clear(); | 622 decode_surfaces_.clear(); |
611 | 623 |
612 VAStatus va_res = VAAPI_DestroyContext(va_display_, va_context_id_); | 624 VAStatus va_res = VAAPI_DestroyContext(va_display_, va_context_id_); |
613 VA_LOG_ON_ERROR(va_res, "vaDestroyContext failed"); | 625 VA_LOG_ON_ERROR(va_res, "vaDestroyContext failed"); |
614 | 626 |
615 va_res = VAAPI_DestroySurfaces(va_display_, va_surface_ids_, | 627 va_res = VAAPI_DestroySurfaces(va_display_, va_surface_ids_, |
616 GetRequiredNumOfPictures()); | 628 GetRequiredNumOfPictures()); |
617 VA_LOG_ON_ERROR(va_res, "vaDestroySurfaces failed"); | 629 VA_LOG_ON_ERROR(va_res, "vaDestroySurfaces failed"); |
618 } | 630 } |
619 | 631 |
632 void VaapiH264Decoder::DestroyPendingBuffers() { | |
633 while (!pending_slice_bufs_.empty()) { | |
634 VABufferID buffer = pending_slice_bufs_.front(); | |
635 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer); | |
636 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
637 pending_slice_bufs_.pop(); | |
638 } | |
639 while (!pending_va_bufs_.empty()) { | |
640 VABufferID buffer = pending_va_bufs_.front(); | |
641 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer); | |
642 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
643 pending_va_bufs_.pop(); | |
644 } | |
645 } | |
646 | |
620 // Fill |va_pic| with default/neutral values. | 647 // Fill |va_pic| with default/neutral values. |
621 static void InitVAPicture(VAPictureH264* va_pic) { | 648 static void InitVAPicture(VAPictureH264* va_pic) { |
622 memset(va_pic, 0, sizeof(*va_pic)); | 649 memset(va_pic, 0, sizeof(*va_pic)); |
623 va_pic->picture_id = VA_INVALID_ID; | 650 va_pic->picture_id = VA_INVALID_ID; |
624 va_pic->flags = VA_PICTURE_H264_INVALID; | 651 va_pic->flags = VA_PICTURE_H264_INVALID; |
625 } | 652 } |
626 | 653 |
627 void VaapiH264Decoder::FillVAPicture(VAPictureH264 *va_pic, H264Picture* pic) { | 654 void VaapiH264Decoder::FillVAPicture(VAPictureH264 *va_pic, H264Picture* pic) { |
628 POCToDecodeSurfaces::iterator iter = poc_to_decode_surfaces_.find( | 655 POCToDecodeSurfaces::iterator iter = poc_to_decode_surfaces_.find( |
629 pic->pic_order_cnt); | 656 pic->pic_order_cnt); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 | 1015 |
989 if (!SendVASliceParam(slice_hdr)) | 1016 if (!SendVASliceParam(slice_hdr)) |
990 return false; | 1017 return false; |
991 | 1018 |
992 if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size)) | 1019 if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size)) |
993 return false; | 1020 return false; |
994 | 1021 |
995 return true; | 1022 return true; |
996 } | 1023 } |
997 | 1024 |
1025 void VaapiH264Decoder::DestroyBuffers(size_t num_va_buffers, | |
1026 const VABufferID* va_buffers) { | |
1027 for (size_t i = 0; i < num_va_buffers; ++i) { | |
1028 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, va_buffers[i]); | |
1029 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
1030 } | |
1031 } | |
1032 | |
998 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever | 1033 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever |
999 // possible. | 1034 // possible. |
1000 | 1035 |
1001 bool VaapiH264Decoder::DecodePicture() { | 1036 bool VaapiH264Decoder::DecodePicture() { |
1002 DCHECK(!frame_ready_at_hw_); | 1037 DCHECK(!frame_ready_at_hw_); |
1003 DCHECK(curr_pic_.get()); | 1038 DCHECK(curr_pic_.get()); |
1004 | 1039 |
1005 static const size_t kMaxVABuffers = 32; | 1040 static const size_t kMaxVABuffers = 32; |
1006 DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers); | 1041 DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers); |
1007 DCHECK_LE(pending_slice_bufs_.size(), kMaxVABuffers); | 1042 DCHECK_LE(pending_slice_bufs_.size(), kMaxVABuffers); |
1008 | 1043 |
1009 DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size(); | 1044 DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size(); |
1010 DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size(); | 1045 DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size(); |
1011 | 1046 |
1012 // Find the surface associated with the picture to be decoded. | 1047 // Find the surface associated with the picture to be decoded. |
1013 DCHECK(pending_slice_bufs_.size()); | 1048 DCHECK(pending_slice_bufs_.size()); |
1014 DecodeSurface* dec_surface = | 1049 DecodeSurface* dec_surface = |
1015 poc_to_decode_surfaces_[curr_pic_->pic_order_cnt]; | 1050 poc_to_decode_surfaces_[curr_pic_->pic_order_cnt]; |
1016 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt | 1051 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt |
1017 << " into surface " << dec_surface->va_surface_id(); | 1052 << " into surface " << dec_surface->va_surface_id(); |
1018 | 1053 |
1019 // Get ready to decode into surface. | 1054 // Get ready to decode into surface. |
1020 VAStatus va_res = VAAPI_BeginPicture(va_display_, va_context_id_, | 1055 VAStatus va_res = VAAPI_BeginPicture(va_display_, va_context_id_, |
1021 dec_surface->va_surface_id()); | 1056 dec_surface->va_surface_id()); |
1022 VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false); | 1057 VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false); |
1023 | 1058 |
1024 // Put buffer IDs for pending parameter buffers into buffers[]. | 1059 // Put buffer IDs for pending parameter buffers into va_buffers[]. |
1025 VABufferID buffers[kMaxVABuffers]; | 1060 VABufferID va_buffers[kMaxVABuffers]; |
1026 size_t num_buffers = pending_va_bufs_.size(); | 1061 size_t num_va_buffers = pending_va_bufs_.size(); |
1027 for (size_t i = 0; i < num_buffers && i < kMaxVABuffers; ++i) { | 1062 for (size_t i = 0; i < num_va_buffers && i < kMaxVABuffers; ++i) { |
1028 buffers[i] = pending_va_bufs_.front(); | 1063 va_buffers[i] = pending_va_bufs_.front(); |
1029 pending_va_bufs_.pop(); | 1064 pending_va_bufs_.pop(); |
1030 } | 1065 } |
1066 base::Closure va_buffers_callback = | |
1067 base::Bind(&VaapiH264Decoder::DestroyBuffers, base::Unretained(this), | |
1068 num_va_buffers, va_buffers); | |
1069 base::ScopedClosureRunner va_buffers_deleter(va_buffers_callback); | |
1031 | 1070 |
1032 // And send them to the HW decoder. | 1071 // And send them to the HW decoder. |
1033 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, buffers, | 1072 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, va_buffers, |
1034 num_buffers); | 1073 num_va_buffers); |
1035 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false); | 1074 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false); |
1036 | 1075 |
1037 DVLOG(4) << "Committed " << num_buffers << "VA buffers"; | 1076 DVLOG(4) << "Committed " << num_va_buffers << "VA buffers"; |
1038 | 1077 |
1039 for (size_t i = 0; i < num_buffers; ++i) { | 1078 // Put buffer IDs for pending slice data buffers into slice_buffers[]. |
1040 va_res = VAAPI_DestroyBuffer(va_display_, buffers[i]); | 1079 VABufferID slice_buffers[kMaxVABuffers]; |
1041 VA_SUCCESS_OR_RETURN(va_res, "vaDestroyBuffer for va_bufs failed", false); | 1080 size_t num_slice_buffers = pending_slice_bufs_.size(); |
1042 } | 1081 for (size_t i = 0; i < num_slice_buffers && i < kMaxVABuffers; ++i) { |
1043 | 1082 slice_buffers[i] = pending_slice_bufs_.front(); |
1044 // Put buffer IDs for pending slice data buffers into buffers[]. | |
1045 num_buffers = pending_slice_bufs_.size(); | |
1046 for (size_t i = 0; i < num_buffers && i < kMaxVABuffers; ++i) { | |
1047 buffers[i] = pending_slice_bufs_.front(); | |
1048 pending_slice_bufs_.pop(); | 1083 pending_slice_bufs_.pop(); |
1049 } | 1084 } |
1085 base::Closure va_slices_callback = | |
1086 base::Bind(&VaapiH264Decoder::DestroyBuffers, base::Unretained(this), | |
1087 num_slice_buffers, slice_buffers); | |
1088 base::ScopedClosureRunner slice_buffers_deleter(va_slices_callback); | |
1050 | 1089 |
1051 // And send them to the Hw decoder. | 1090 // And send them to the Hw decoder. |
1052 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, buffers, | 1091 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, slice_buffers, |
1053 num_buffers); | 1092 num_slice_buffers); |
1054 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false); | 1093 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false); |
1055 | 1094 |
1056 DVLOG(4) << "Committed " << num_buffers << "slice buffers"; | 1095 DVLOG(4) << "Committed " << num_slice_buffers << "slice buffers"; |
1057 | |
1058 for (size_t i = 0; i < num_buffers; ++i) { | |
1059 va_res = VAAPI_DestroyBuffer(va_display_, buffers[i]); | |
1060 VA_SUCCESS_OR_RETURN(va_res, "vaDestroyBuffer for slices failed", false); | |
1061 } | |
1062 | 1096 |
1063 // Instruct HW decoder to start processing committed buffers (decode this | 1097 // Instruct HW decoder to start processing committed buffers (decode this |
1064 // picture). This does not block until the end of decode. | 1098 // picture). This does not block until the end of decode. |
1065 va_res = VAAPI_EndPicture(va_display_, va_context_id_); | 1099 va_res = VAAPI_EndPicture(va_display_, va_context_id_); |
1066 VA_SUCCESS_OR_RETURN(va_res, "vaEndPicture failed", false); | 1100 VA_SUCCESS_OR_RETURN(va_res, "vaEndPicture failed", false); |
1067 | 1101 |
1068 // Used to notify clients that we had sufficient data to start decoding | 1102 // Used to notify clients that we had sufficient data to start decoding |
1069 // a new frame. | 1103 // a new frame. |
1070 frame_ready_at_hw_ = true; | 1104 frame_ready_at_hw_ = true; |
1071 return true; | 1105 return true; |
(...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2092 } | 2126 } |
2093 } | 2127 } |
2094 } | 2128 } |
2095 | 2129 |
2096 // static | 2130 // static |
2097 size_t VaapiH264Decoder::GetRequiredNumOfPictures() { | 2131 size_t VaapiH264Decoder::GetRequiredNumOfPictures() { |
2098 return kNumReqPictures; | 2132 return kNumReqPictures; |
2099 } | 2133 } |
2100 | 2134 |
2101 } // namespace content | 2135 } // namespace content |
OLD | NEW |