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.

245 lines
9.1KB

  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 "internal.h"
  26. #include "h264.h"
  27. #include "mpegutils.h"
  28. #include "vdpau.h"
  29. #include "vdpau_internal.h"
  30. static int32_t h264_foc(int foc)
  31. {
  32. if (foc == INT_MAX)
  33. foc = 0;
  34. return foc;
  35. }
  36. static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
  37. {
  38. rf->surface = VDP_INVALID_HANDLE;
  39. rf->is_long_term = VDP_FALSE;
  40. rf->top_is_reference = VDP_FALSE;
  41. rf->bottom_is_reference = VDP_FALSE;
  42. rf->field_order_cnt[0] = 0;
  43. rf->field_order_cnt[1] = 0;
  44. rf->frame_idx = 0;
  45. }
  46. static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic,
  47. int pic_structure)
  48. {
  49. VdpVideoSurface surface = ff_vdpau_get_surface_id(&pic->f);
  50. if (pic_structure == 0)
  51. pic_structure = pic->reference;
  52. rf->surface = surface;
  53. rf->is_long_term = pic->reference && pic->long_ref;
  54. rf->top_is_reference = (pic_structure & PICT_TOP_FIELD) != 0;
  55. rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
  56. rf->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
  57. rf->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
  58. rf->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  59. }
  60. static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
  61. {
  62. H264Context * const h = avctx->priv_data;
  63. struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private;
  64. VdpPictureInfoH264 *info = &pic_ctx->info.h264;
  65. int list;
  66. VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
  67. #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
  68. for (list = 0; list < 2; ++list) {
  69. H264Picture **lp = list ? h->long_ref : h->short_ref;
  70. int i, ls = list ? 16 : h->short_ref_count;
  71. for (i = 0; i < ls; ++i) {
  72. H264Picture *pic = lp[i];
  73. VdpReferenceFrameH264 *rf2;
  74. VdpVideoSurface surface_ref;
  75. int pic_frame_idx;
  76. if (!pic || !pic->reference)
  77. continue;
  78. pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  79. surface_ref = ff_vdpau_get_surface_id(&pic->f);
  80. rf2 = &info->referenceFrames[0];
  81. while (rf2 != rf) {
  82. if ((rf2->surface == surface_ref) &&
  83. (rf2->is_long_term == pic->long_ref) &&
  84. (rf2->frame_idx == pic_frame_idx))
  85. break;
  86. ++rf2;
  87. }
  88. if (rf2 != rf) {
  89. rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  90. rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  91. continue;
  92. }
  93. if (rf >= &info->referenceFrames[H264_RF_COUNT])
  94. continue;
  95. vdpau_h264_set_rf(rf, pic, pic->reference);
  96. ++rf;
  97. }
  98. }
  99. for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
  100. vdpau_h264_clear_rf(rf);
  101. }
  102. static int vdpau_h264_start_frame(AVCodecContext *avctx,
  103. const uint8_t *buffer, uint32_t size)
  104. {
  105. H264Context * const h = avctx->priv_data;
  106. H264Picture *pic = h->cur_pic_ptr;
  107. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  108. VdpPictureInfoH264 *info = &pic_ctx->info.h264;
  109. /* init VdpPictureInfoH264 */
  110. info->slice_count = 0;
  111. info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
  112. info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
  113. info->is_reference = h->nal_ref_idc != 0;
  114. info->frame_num = h->frame_num;
  115. info->field_pic_flag = h->picture_structure != PICT_FRAME;
  116. info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
  117. info->num_ref_frames = h->sps.ref_frame_count;
  118. info->mb_adaptive_frame_field_flag = h->sps.mb_aff && !info->field_pic_flag;
  119. info->constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  120. info->weighted_pred_flag = h->pps.weighted_pred;
  121. info->weighted_bipred_idc = h->pps.weighted_bipred_idc;
  122. info->frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  123. info->transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  124. info->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  125. info->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  126. info->pic_init_qp_minus26 = h->pps.init_qp - 26;
  127. info->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  128. info->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  129. info->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  130. info->pic_order_cnt_type = h->sps.poc_type;
  131. info->log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
  132. info->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  133. info->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  134. info->entropy_coding_mode_flag = h->pps.cabac;
  135. info->pic_order_present_flag = h->pps.pic_order_present;
  136. info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  137. info->redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  138. memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
  139. sizeof(info->scaling_lists_4x4));
  140. memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
  141. sizeof(info->scaling_lists_8x8[0]));
  142. memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
  143. sizeof(info->scaling_lists_8x8[1]));
  144. vdpau_h264_set_reference_frames(avctx);
  145. return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
  146. }
  147. static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
  148. static int vdpau_h264_decode_slice(AVCodecContext *avctx,
  149. const uint8_t *buffer, uint32_t size)
  150. {
  151. H264Context *h = avctx->priv_data;
  152. H264Picture *pic = h->cur_pic_ptr;
  153. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  154. int val;
  155. val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
  156. if (val)
  157. return val;
  158. val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
  159. if (val)
  160. return val;
  161. pic_ctx->info.h264.slice_count++;
  162. return 0;
  163. }
  164. static int vdpau_h264_end_frame(AVCodecContext *avctx)
  165. {
  166. H264Context *h = avctx->priv_data;
  167. H264Picture *pic = h->cur_pic_ptr;
  168. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  169. int val;
  170. val = ff_vdpau_common_end_frame(avctx, &pic->f, pic_ctx);
  171. if (val < 0)
  172. return val;
  173. ff_h264_draw_horiz_band(h, 0, h->avctx->height);
  174. return 0;
  175. }
  176. static int vdpau_h264_init(AVCodecContext *avctx)
  177. {
  178. VdpDecoderProfile profile;
  179. uint32_t level = avctx->level;
  180. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  181. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  182. case FF_PROFILE_H264_BASELINE:
  183. profile = VDP_DECODER_PROFILE_H264_BASELINE;
  184. break;
  185. case FF_PROFILE_H264_MAIN:
  186. profile = VDP_DECODER_PROFILE_H264_MAIN;
  187. break;
  188. case FF_PROFILE_H264_HIGH:
  189. profile = VDP_DECODER_PROFILE_H264_HIGH;
  190. break;
  191. default:
  192. return AVERROR(ENOTSUP);
  193. }
  194. if ((avctx->profile & FF_PROFILE_H264_INTRA) && avctx->level == 11)
  195. level = VDP_DECODER_LEVEL_H264_1b;
  196. return ff_vdpau_common_init(avctx, profile, level);
  197. }
  198. AVHWAccel ff_h264_vdpau_hwaccel = {
  199. .name = "h264_vdpau",
  200. .type = AVMEDIA_TYPE_VIDEO,
  201. .id = AV_CODEC_ID_H264,
  202. .pix_fmt = AV_PIX_FMT_VDPAU,
  203. .start_frame = vdpau_h264_start_frame,
  204. .end_frame = vdpau_h264_end_frame,
  205. .decode_slice = vdpau_h264_decode_slice,
  206. .frame_priv_data_size = sizeof(struct vdpau_picture_context),
  207. .init = vdpau_h264_init,
  208. .uninit = ff_vdpau_common_uninit,
  209. .priv_data_size = sizeof(VDPAUContext),
  210. };