You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.4KB

  1. /*
  2. * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through VDPAU
  3. *
  4. * Copyright (c) 2008 NVIDIA
  5. * Copyright (c) 2013 Rémi Denis-Courmont
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <vdpau/vdpau.h>
  24. #include "avcodec.h"
  25. #include "h264.h"
  26. #include "vdpau.h"
  27. #include "vdpau_internal.h"
  28. static int32_t h264_foc(int foc)
  29. {
  30. if (foc == INT_MAX)
  31. foc = 0;
  32. return foc;
  33. }
  34. static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
  35. {
  36. rf->surface = VDP_INVALID_HANDLE;
  37. rf->is_long_term = VDP_FALSE;
  38. rf->top_is_reference = VDP_FALSE;
  39. rf->bottom_is_reference = VDP_FALSE;
  40. rf->field_order_cnt[0] = 0;
  41. rf->field_order_cnt[1] = 0;
  42. rf->frame_idx = 0;
  43. }
  44. static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, Picture *pic,
  45. int pic_structure)
  46. {
  47. VdpVideoSurface surface = ff_vdpau_get_surface_id(pic);
  48. if (pic_structure == 0)
  49. pic_structure = pic->reference;
  50. rf->surface = surface;
  51. rf->is_long_term = pic->reference && pic->long_ref;
  52. rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0;
  53. rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
  54. rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
  55. rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
  56. rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  57. }
  58. static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
  59. {
  60. H264Context * const h = avctx->priv_data;
  61. struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private;
  62. VdpPictureInfoH264 *info = &pic_ctx->info.h264;
  63. int list;
  64. VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
  65. #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
  66. for (list = 0; list < 2; ++list) {
  67. Picture **lp = list ? h->long_ref : h->short_ref;
  68. int i, ls = list ? 16 : h->short_ref_count;
  69. for (i = 0; i < ls; ++i) {
  70. Picture *pic = lp[i];
  71. VdpReferenceFrameH264 *rf2;
  72. VdpVideoSurface surface_ref;
  73. int pic_frame_idx;
  74. if (!pic || !pic->reference)
  75. continue;
  76. pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  77. surface_ref = ff_vdpau_get_surface_id(pic);
  78. rf2 = &info->referenceFrames[0];
  79. while (rf2 != rf) {
  80. if ((rf2->surface == surface_ref) &&
  81. (rf2->is_long_term == pic->long_ref) &&
  82. (rf2->frame_idx == pic_frame_idx))
  83. break;
  84. ++rf2;
  85. }
  86. if (rf2 != rf) {
  87. rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  88. rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  89. continue;
  90. }
  91. if (rf >= &info->referenceFrames[H264_RF_COUNT])
  92. continue;
  93. vdpau_h264_set_rf(rf, pic, pic->reference);
  94. ++rf;
  95. }
  96. }
  97. for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
  98. vdpau_h264_clear_rf(rf);
  99. }
  100. static int vdpau_h264_start_frame(AVCodecContext *avctx,
  101. const uint8_t *buffer, uint32_t size)
  102. {
  103. H264Context * const h = avctx->priv_data;
  104. Picture *pic = h->cur_pic_ptr;
  105. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  106. VdpPictureInfoH264 *info = &pic_ctx->info.h264;
  107. /* init VdpPictureInfoH264 */
  108. info->slice_count = 0;
  109. info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
  110. info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
  111. info->is_reference = h->nal_ref_idc != 0;
  112. info->frame_num = h->frame_num;
  113. info->field_pic_flag = h->picture_structure != PICT_FRAME;
  114. info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
  115. info->num_ref_frames = h->sps.ref_frame_count;
  116. info->mb_adaptive_frame_field_flag = h->sps.mb_aff && !info->field_pic_flag;
  117. info->constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  118. info->weighted_pred_flag = h->pps.weighted_pred;
  119. info->weighted_bipred_idc = h->pps.weighted_bipred_idc;
  120. info->frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  121. info->transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  122. info->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  123. info->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  124. info->pic_init_qp_minus26 = h->pps.init_qp - 26;
  125. info->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  126. info->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  127. info->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  128. info->pic_order_cnt_type = h->sps.poc_type;
  129. info->log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
  130. info->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  131. info->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  132. info->entropy_coding_mode_flag = h->pps.cabac;
  133. info->pic_order_present_flag = h->pps.pic_order_present;
  134. info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  135. info->redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  136. memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
  137. sizeof(info->scaling_lists_4x4));
  138. memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
  139. sizeof(info->scaling_lists_8x8[0]));
  140. memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
  141. sizeof(info->scaling_lists_8x8[1]));
  142. vdpau_h264_set_reference_frames(avctx);
  143. return ff_vdpau_common_start_frame(pic, buffer, size);
  144. }
  145. static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
  146. static int vdpau_h264_decode_slice(AVCodecContext *avctx,
  147. const uint8_t *buffer, uint32_t size)
  148. {
  149. H264Context *h = avctx->priv_data;
  150. Picture *pic = h->cur_pic_ptr;
  151. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  152. int val;
  153. val = ff_vdpau_add_buffer(pic, start_code_prefix, 3);
  154. if (val)
  155. return val;
  156. val = ff_vdpau_add_buffer(pic, buffer, size);
  157. if (val)
  158. return val;
  159. pic_ctx->info.h264.slice_count++;
  160. return 0;
  161. }
  162. static int vdpau_h264_end_frame(AVCodecContext *avctx)
  163. {
  164. AVVDPAUContext *hwctx = avctx->hwaccel_context;
  165. H264Context *h = avctx->priv_data;
  166. Picture *pic = h->cur_pic_ptr;
  167. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  168. VdpVideoSurface surf = ff_vdpau_get_surface_id(pic);
  169. hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
  170. pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
  171. ff_h264_draw_horiz_band(h, 0, h->avctx->height);
  172. av_freep(&pic_ctx->bitstream_buffers);
  173. return 0;
  174. }
  175. AVHWAccel ff_h264_vdpau_hwaccel = {
  176. .name = "h264_vdpau",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .id = AV_CODEC_ID_H264,
  179. .pix_fmt = AV_PIX_FMT_VDPAU,
  180. .start_frame = vdpau_h264_start_frame,
  181. .end_frame = vdpau_h264_end_frame,
  182. .decode_slice = vdpau_h264_decode_slice,
  183. .priv_data_size = sizeof(struct vdpau_picture_context),
  184. };