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.

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