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