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.

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