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