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.

274 lines
10KB

  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. #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
  110. VdpPictureInfoH264Predictive *info2 = &pic_ctx->info.h264_predictive;
  111. #endif
  112. /* init VdpPictureInfoH264 */
  113. info->slice_count = 0;
  114. info->field_order_cnt[0] = h264_foc(pic->field_poc[0]);
  115. info->field_order_cnt[1] = h264_foc(pic->field_poc[1]);
  116. info->is_reference = h->nal_ref_idc != 0;
  117. info->frame_num = h->frame_num;
  118. info->field_pic_flag = h->picture_structure != PICT_FRAME;
  119. info->bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD;
  120. info->num_ref_frames = h->sps.ref_frame_count;
  121. info->mb_adaptive_frame_field_flag = h->sps.mb_aff && !info->field_pic_flag;
  122. info->constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  123. info->weighted_pred_flag = h->pps.weighted_pred;
  124. info->weighted_bipred_idc = h->pps.weighted_bipred_idc;
  125. info->frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  126. info->transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  127. info->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  128. info->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  129. info->pic_init_qp_minus26 = h->pps.init_qp - 26;
  130. info->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  131. info->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  132. info->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  133. info->pic_order_cnt_type = h->sps.poc_type;
  134. info->log2_max_pic_order_cnt_lsb_minus4 = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
  135. info->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  136. info->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  137. #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
  138. info2->qpprime_y_zero_transform_bypass_flag = h->sps.transform_bypass;
  139. info2->separate_colour_plane_flag = h->sps.residual_color_transform_flag;
  140. #endif
  141. info->entropy_coding_mode_flag = h->pps.cabac;
  142. info->pic_order_present_flag = h->pps.pic_order_present;
  143. info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  144. info->redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  145. memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
  146. sizeof(info->scaling_lists_4x4));
  147. memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
  148. sizeof(info->scaling_lists_8x8[0]));
  149. memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
  150. sizeof(info->scaling_lists_8x8[1]));
  151. vdpau_h264_set_reference_frames(avctx);
  152. return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
  153. }
  154. static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
  155. static int vdpau_h264_decode_slice(AVCodecContext *avctx,
  156. const uint8_t *buffer, uint32_t size)
  157. {
  158. H264Context *h = avctx->priv_data;
  159. H264Picture *pic = h->cur_pic_ptr;
  160. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  161. int val;
  162. val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
  163. if (val)
  164. return val;
  165. val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
  166. if (val)
  167. return val;
  168. pic_ctx->info.h264.slice_count++;
  169. return 0;
  170. }
  171. static int vdpau_h264_end_frame(AVCodecContext *avctx)
  172. {
  173. H264Context *h = avctx->priv_data;
  174. H264SliceContext *sl = &h->slice_ctx[0];
  175. H264Picture *pic = h->cur_pic_ptr;
  176. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  177. int val;
  178. val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
  179. if (val < 0)
  180. return val;
  181. ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
  182. return 0;
  183. }
  184. static int vdpau_h264_init(AVCodecContext *avctx)
  185. {
  186. VdpDecoderProfile profile;
  187. uint32_t level = avctx->level;
  188. switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
  189. case FF_PROFILE_H264_BASELINE:
  190. profile = VDP_DECODER_PROFILE_H264_BASELINE;
  191. break;
  192. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  193. #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
  194. profile = VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE;
  195. break;
  196. #endif
  197. case FF_PROFILE_H264_MAIN:
  198. profile = VDP_DECODER_PROFILE_H264_MAIN;
  199. break;
  200. case FF_PROFILE_H264_HIGH:
  201. profile = VDP_DECODER_PROFILE_H264_HIGH;
  202. break;
  203. #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
  204. case FF_PROFILE_H264_EXTENDED:
  205. profile = VDP_DECODER_PROFILE_H264_EXTENDED;
  206. break;
  207. #endif
  208. case FF_PROFILE_H264_HIGH_10:
  209. /* XXX: High 10 can be treated as High so long as only 8-bits per
  210. * formats are supported. */
  211. profile = VDP_DECODER_PROFILE_H264_HIGH;
  212. break;
  213. #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
  214. case FF_PROFILE_H264_HIGH_422:
  215. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  216. case FF_PROFILE_H264_CAVLC_444:
  217. profile = VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE;
  218. break;
  219. #endif
  220. default:
  221. return AVERROR(ENOTSUP);
  222. }
  223. if ((avctx->profile & FF_PROFILE_H264_INTRA) && avctx->level == 11)
  224. level = VDP_DECODER_LEVEL_H264_1b;
  225. return ff_vdpau_common_init(avctx, profile, level);
  226. }
  227. AVHWAccel ff_h264_vdpau_hwaccel = {
  228. .name = "h264_vdpau",
  229. .type = AVMEDIA_TYPE_VIDEO,
  230. .id = AV_CODEC_ID_H264,
  231. .pix_fmt = AV_PIX_FMT_VDPAU,
  232. .start_frame = vdpau_h264_start_frame,
  233. .end_frame = vdpau_h264_end_frame,
  234. .decode_slice = vdpau_h264_decode_slice,
  235. .frame_priv_data_size = sizeof(struct vdpau_picture_context),
  236. .init = vdpau_h264_init,
  237. .uninit = ff_vdpau_common_uninit,
  238. .priv_data_size = sizeof(VDPAUContext),
  239. };