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" |
(...skipping 13 matching lines...) Expand all Loading... | |
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 |
34 #define VA_LOG_ERROR(va_res, err_msg) \ | |
35 do { \ | |
36 DVLOG(1) << err_msg \ | |
37 << " VA error: " << VAAPI_ErrorStr(va_res); \ | |
38 } while (0) | |
39 | |
34 namespace content { | 40 namespace content { |
35 | 41 |
36 void *vaapi_handle = dlopen("libva.so", RTLD_NOW); | 42 void *vaapi_handle = dlopen("libva.so", RTLD_NOW); |
37 void *vaapi_x11_handle = dlopen("libva-x11.so", RTLD_NOW); | 43 void *vaapi_x11_handle = dlopen("libva-x11.so", RTLD_NOW); |
38 | 44 |
39 typedef VADisplay (*VaapiGetDisplay)(Display *dpy); | 45 typedef VADisplay (*VaapiGetDisplay)(Display *dpy); |
40 typedef int (*VaapiDisplayIsValid)(VADisplay dpy); | 46 typedef int (*VaapiDisplayIsValid)(VADisplay dpy); |
41 typedef VAStatus (*VaapiInitialize)(VADisplay dpy, | 47 typedef VAStatus (*VaapiInitialize)(VADisplay dpy, |
42 int *major_version, | 48 int *major_version, |
43 int *minor_version); | 49 int *minor_version); |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 frame_num_ = 0; | 370 frame_num_ = 0; |
365 prev_frame_num_ = -1; | 371 prev_frame_num_ = -1; |
366 prev_frame_num_offset_ = -1; | 372 prev_frame_num_offset_ = -1; |
367 | 373 |
368 prev_ref_has_memmgmnt5_ = false; | 374 prev_ref_has_memmgmnt5_ = false; |
369 prev_ref_top_field_order_cnt_ = -1; | 375 prev_ref_top_field_order_cnt_ = -1; |
370 prev_ref_pic_order_cnt_msb_ = -1; | 376 prev_ref_pic_order_cnt_msb_ = -1; |
371 prev_ref_pic_order_cnt_lsb_ = -1; | 377 prev_ref_pic_order_cnt_lsb_ = -1; |
372 prev_ref_field_ = H264Picture::FIELD_NONE; | 378 prev_ref_field_ = H264Picture::FIELD_NONE; |
373 | 379 |
380 // When called from the constructor, although va_display_ is invalid, | |
381 // |pending_slice_bufs_| and |pending_va_bufs_| are empty. | |
382 while (!pending_slice_bufs_.empty()) { | |
383 VABufferID buffer = pending_slice_bufs_.front(); | |
384 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer); | |
385 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
386 pending_slice_bufs_.pop(); | |
387 } | |
388 while (!pending_va_bufs_.empty()) { | |
389 VABufferID buffer = pending_va_bufs_.front(); | |
390 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, buffer); | |
391 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
392 pending_va_bufs_.pop(); | |
393 } | |
394 | |
374 pending_slice_bufs_ = std::queue<VABufferID>(); | 395 pending_slice_bufs_ = std::queue<VABufferID>(); |
375 pending_va_bufs_ = std::queue<VABufferID>(); | 396 pending_va_bufs_ = std::queue<VABufferID>(); |
376 | 397 |
377 ref_pic_list0_.clear(); | 398 ref_pic_list0_.clear(); |
378 ref_pic_list1_.clear(); | 399 ref_pic_list1_.clear(); |
379 | 400 |
380 for (POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.begin(); | 401 for (POCToDecodeSurfaces::iterator it = poc_to_decode_surfaces_.begin(); |
381 it != poc_to_decode_surfaces_.end(); ) { | 402 it != poc_to_decode_surfaces_.end(); ) { |
382 int poc = it->second->poc(); | 403 int poc = it->second->poc(); |
383 // Must be incremented before UnassignSurfaceFromPoC as this call | 404 // Must be incremented before UnassignSurfaceFromPoC as this call |
(...skipping 27 matching lines...) Expand all Loading... | |
411 case kError: | 432 case kError: |
412 destroy_surfaces = true; | 433 destroy_surfaces = true; |
413 // fallthrough | 434 // fallthrough |
414 case kInitialized: | 435 case kInitialized: |
415 if (!make_context_current_.Run()) | 436 if (!make_context_current_.Run()) |
416 break; | 437 break; |
417 if (destroy_surfaces) | 438 if (destroy_surfaces) |
418 DestroyVASurfaces(); | 439 DestroyVASurfaces(); |
419 va_res = VAAPI_DestroyConfig(va_display_, va_config_id_); | 440 va_res = VAAPI_DestroyConfig(va_display_, va_config_id_); |
420 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed"); | 441 VA_LOG_ON_ERROR(va_res, "vaDestroyConfig failed"); |
421 va_res = VAAPI_Terminate(va_display_); | 442 va_res = VAAPI_Terminate(va_display_); |
422 VA_LOG_ON_ERROR(va_res, "vaTerminate failed"); | 443 VA_LOG_ON_ERROR(va_res, "vaTerminate failed"); |
423 // fallthrough | 444 // fallthrough |
424 case kUninitialized: | 445 case kUninitialized: |
425 break; | 446 break; |
426 } | 447 } |
427 | 448 |
428 state_ = kUninitialized; | 449 state_ = kUninitialized; |
429 } | 450 } |
430 | 451 |
431 // Maps Profile enum values to VaProfile values. | 452 // Maps Profile enum values to VaProfile values. |
432 bool VaapiH264Decoder::SetProfile(media::VideoCodecProfile profile) { | 453 bool VaapiH264Decoder::SetProfile(media::VideoCodecProfile profile) { |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 GetRequiredNumOfPictures(), | 614 GetRequiredNumOfPictures(), |
594 va_surface_ids_); | 615 va_surface_ids_); |
595 VA_SUCCESS_OR_RETURN(va_res, "vaCreateSurfaces failed", false); | 616 VA_SUCCESS_OR_RETURN(va_res, "vaCreateSurfaces failed", false); |
596 | 617 |
597 DCHECK(decode_surfaces_.empty()); | 618 DCHECK(decode_surfaces_.empty()); |
598 // And create a context associated with them. | 619 // And create a context associated with them. |
599 va_res = VAAPI_CreateContext(va_display_, va_config_id_, | 620 va_res = VAAPI_CreateContext(va_display_, va_config_id_, |
600 pic_width_, pic_height_, VA_PROGRESSIVE, | 621 pic_width_, pic_height_, VA_PROGRESSIVE, |
601 va_surface_ids_, GetRequiredNumOfPictures(), | 622 va_surface_ids_, GetRequiredNumOfPictures(), |
602 &va_context_id_); | 623 &va_context_id_); |
603 VA_SUCCESS_OR_RETURN(va_res, "vaCreateContext failed", false); | 624 |
625 if (va_res != VA_STATUS_SUCCESS) { | |
626 VAAPI_DestroySurfaces(va_display_, va_surface_ids_, | |
627 GetRequiredNumOfPictures()); | |
628 DVLOG(1) << "vaCreateContext failed" | |
629 << " VA error: " << VAAPI_ErrorStr(va_res); | |
630 return false; | |
631 } | |
604 | 632 |
605 return true; | 633 return true; |
606 } | 634 } |
607 | 635 |
608 void VaapiH264Decoder::DestroyVASurfaces() { | 636 void VaapiH264Decoder::DestroyVASurfaces() { |
609 DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset); | 637 DCHECK(state_ == kDecoding || state_ == kError || state_ == kAfterReset); |
610 decode_surfaces_.clear(); | 638 decode_surfaces_.clear(); |
611 | 639 |
612 VAStatus va_res = VAAPI_DestroyContext(va_display_, va_context_id_); | 640 VAStatus va_res = VAAPI_DestroyContext(va_display_, va_context_id_); |
613 VA_LOG_ON_ERROR(va_res, "vaDestroyContext failed"); | 641 VA_LOG_ON_ERROR(va_res, "vaDestroyContext failed"); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 | 1016 |
989 if (!SendVASliceParam(slice_hdr)) | 1017 if (!SendVASliceParam(slice_hdr)) |
990 return false; | 1018 return false; |
991 | 1019 |
992 if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size)) | 1020 if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size)) |
993 return false; | 1021 return false; |
994 | 1022 |
995 return true; | 1023 return true; |
996 } | 1024 } |
997 | 1025 |
1026 void VaapiH264Decoder::DestroyBuffers(VABufferID* va_buffers, | |
1027 size_t num_va_buffers) { | |
1028 for (size_t i = 0; i < num_va_buffers; ++i) { | |
1029 VAStatus va_res = VAAPI_DestroyBuffer(va_display_, va_buffers[i]); | |
1030 VA_LOG_ON_ERROR(va_res, "vaDestroyBuffer failed"); | |
1031 } | |
1032 } | |
1033 | |
998 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever | 1034 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever |
999 // possible. | 1035 // possible. |
1000 | 1036 |
1001 bool VaapiH264Decoder::DecodePicture() { | 1037 bool VaapiH264Decoder::DecodePicture() { |
1002 DCHECK(!frame_ready_at_hw_); | 1038 DCHECK(!frame_ready_at_hw_); |
1003 DCHECK(curr_pic_.get()); | 1039 DCHECK(curr_pic_.get()); |
1004 | 1040 |
1005 static const size_t kMaxVABuffers = 32; | 1041 static const size_t kMaxVABuffers = 32; |
1006 DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers); | 1042 DCHECK_LE(pending_va_bufs_.size(), kMaxVABuffers); |
1007 DCHECK_LE(pending_slice_bufs_.size(), kMaxVABuffers); | 1043 DCHECK_LE(pending_slice_bufs_.size(), kMaxVABuffers); |
1008 | 1044 |
1009 DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size(); | 1045 DVLOG(4) << "Pending VA bufs to commit: " << pending_va_bufs_.size(); |
1010 DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size(); | 1046 DVLOG(4) << "Pending slice bufs to commit: " << pending_slice_bufs_.size(); |
1011 | 1047 |
1012 // Find the surface associated with the picture to be decoded. | 1048 // Find the surface associated with the picture to be decoded. |
1013 DCHECK(pending_slice_bufs_.size()); | 1049 DCHECK(pending_slice_bufs_.size()); |
1014 DecodeSurface* dec_surface = | 1050 DecodeSurface* dec_surface = |
1015 poc_to_decode_surfaces_[curr_pic_->pic_order_cnt]; | 1051 poc_to_decode_surfaces_[curr_pic_->pic_order_cnt]; |
1016 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt | 1052 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt |
1017 << " into surface " << dec_surface->va_surface_id(); | 1053 << " into surface " << dec_surface->va_surface_id(); |
1018 | 1054 |
1019 // Get ready to decode into surface. | 1055 // Get ready to decode into surface. |
1020 VAStatus va_res = VAAPI_BeginPicture(va_display_, va_context_id_, | 1056 VAStatus va_res = VAAPI_BeginPicture(va_display_, va_context_id_, |
1021 dec_surface->va_surface_id()); | 1057 dec_surface->va_surface_id()); |
1022 VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false); | 1058 VA_SUCCESS_OR_RETURN(va_res, "vaBeginPicture failed", false); |
Pawel Osciak
2012/08/03 18:06:39
Actually, here we've already created va buffers, s
| |
1023 | 1059 |
1024 // Put buffer IDs for pending parameter buffers into buffers[]. | 1060 // Put buffer IDs for pending parameter buffers into va_buffers[]. |
1025 VABufferID buffers[kMaxVABuffers]; | 1061 VABufferID va_buffers[kMaxVABuffers]; |
1026 size_t num_buffers = pending_va_bufs_.size(); | 1062 size_t num_va_buffers = pending_va_bufs_.size(); |
1027 for (size_t i = 0; i < num_buffers && i < kMaxVABuffers; ++i) { | 1063 for (size_t i = 0; i < num_va_buffers && i < kMaxVABuffers; ++i) { |
1028 buffers[i] = pending_va_bufs_.front(); | 1064 va_buffers[i] = pending_va_bufs_.front(); |
1029 pending_va_bufs_.pop(); | 1065 pending_va_bufs_.pop(); |
1030 } | 1066 } |
1031 | 1067 |
1032 // And send them to the HW decoder. | 1068 // And send them to the HW decoder. |
1033 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, buffers, | 1069 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, va_buffers, |
1034 num_buffers); | 1070 num_va_buffers); |
1035 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for va_bufs failed", false); | 1071 if (va_res != VA_STATUS_SUCCESS) { |
1036 | 1072 DestroyBuffers(va_buffers, num_va_buffers); |
1037 DVLOG(4) << "Committed " << num_buffers << "VA buffers"; | 1073 VA_LOG_ERROR(va_res, "vaRenderPicture for va_bufs failed"); |
1038 | 1074 return false; |
1039 for (size_t i = 0; i < num_buffers; ++i) { | |
1040 va_res = VAAPI_DestroyBuffer(va_display_, buffers[i]); | |
1041 VA_SUCCESS_OR_RETURN(va_res, "vaDestroyBuffer for va_bufs failed", false); | |
1042 } | 1075 } |
1043 | 1076 |
1044 // Put buffer IDs for pending slice data buffers into buffers[]. | 1077 DVLOG(4) << "Committed " << num_va_buffers << "VA buffers"; |
1045 num_buffers = pending_slice_bufs_.size(); | 1078 |
1046 for (size_t i = 0; i < num_buffers && i < kMaxVABuffers; ++i) { | 1079 // Put buffer IDs for pending slice data buffers into slice_buffers[]. |
1047 buffers[i] = pending_slice_bufs_.front(); | 1080 VABufferID slice_buffers[kMaxVABuffers]; |
1081 size_t num_slice_buffers = pending_slice_bufs_.size(); | |
1082 for (size_t i = 0; i < num_slice_buffers && i < kMaxVABuffers; ++i) { | |
1083 slice_buffers[i] = pending_slice_bufs_.front(); | |
1048 pending_slice_bufs_.pop(); | 1084 pending_slice_bufs_.pop(); |
1049 } | 1085 } |
1050 | 1086 |
1051 // And send them to the Hw decoder. | 1087 // And send them to the Hw decoder. |
1052 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, buffers, | 1088 va_res = VAAPI_RenderPicture(va_display_, va_context_id_, slice_buffers, |
1053 num_buffers); | 1089 num_slice_buffers); |
1054 VA_SUCCESS_OR_RETURN(va_res, "vaRenderPicture for slices failed", false); | 1090 if (va_res != VA_STATUS_SUCCESS) { |
1091 DestroyBuffers(va_buffers, num_va_buffers); | |
1092 DestroyBuffers(slice_buffers, num_slice_buffers); | |
1093 VA_LOG_ERROR(va_res, "vaRenderPicture for slices failed"); | |
1094 return false; | |
1095 } | |
1055 | 1096 |
1056 DVLOG(4) << "Committed " << num_buffers << "slice buffers"; | 1097 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 | 1098 |
1063 // Instruct HW decoder to start processing committed buffers (decode this | 1099 // Instruct HW decoder to start processing committed buffers (decode this |
1064 // picture). This does not block until the end of decode. | 1100 // picture). This does not block until the end of decode. |
1065 va_res = VAAPI_EndPicture(va_display_, va_context_id_); | 1101 va_res = VAAPI_EndPicture(va_display_, va_context_id_); |
1066 VA_SUCCESS_OR_RETURN(va_res, "vaEndPicture failed", false); | 1102 VA_LOG_ON_ERROR(va_res, "vaEndPicture failed"); |
1103 | |
1104 // Now that EndPicture has passed we can destroy our buffers. | |
1105 DestroyBuffers(va_buffers, num_va_buffers); | |
1106 DestroyBuffers(slice_buffers, num_slice_buffers); | |
1067 | 1107 |
1068 // Used to notify clients that we had sufficient data to start decoding | 1108 // Used to notify clients that we had sufficient data to start decoding |
1069 // a new frame. | 1109 // a new frame. |
1070 frame_ready_at_hw_ = true; | 1110 frame_ready_at_hw_ = true; |
1071 return true; | 1111 return true; |
1072 } | 1112 } |
1073 | 1113 |
1074 | 1114 |
1075 bool VaapiH264Decoder::InitCurrPicture(H264SliceHeader* slice_hdr) { | 1115 bool VaapiH264Decoder::InitCurrPicture(H264SliceHeader* slice_hdr) { |
1076 DCHECK(curr_pic_.get()); | 1116 DCHECK(curr_pic_.get()); |
(...skipping 1015 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2092 } | 2132 } |
2093 } | 2133 } |
2094 } | 2134 } |
2095 | 2135 |
2096 // static | 2136 // static |
2097 size_t VaapiH264Decoder::GetRequiredNumOfPictures() { | 2137 size_t VaapiH264Decoder::GetRequiredNumOfPictures() { |
2098 return kNumReqPictures; | 2138 return kNumReqPictures; |
2099 } | 2139 } |
2100 | 2140 |
2101 } // namespace content | 2141 } // namespace content |
OLD | NEW |