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.

178 lines
7.2KB

  1. /*
  2. * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through CUVID
  3. *
  4. * Copyright (c) 2016 Anton Khirnov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <cuviddec.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include "avcodec.h"
  26. #include "cuvid.h"
  27. #include "decode.h"
  28. #include "internal.h"
  29. #include "h264dec.h"
  30. static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src,
  31. int frame_idx)
  32. {
  33. FrameDecodeData *fdd = (FrameDecodeData*)src->f->opaque_ref->data;
  34. const CUVIDFrame *cf = fdd->hwaccel_priv;
  35. dst->PicIdx = cf ? cf->idx : -1;
  36. dst->FrameIdx = frame_idx;
  37. dst->is_long_term = src->long_ref;
  38. dst->not_existing = 0;
  39. dst->used_for_reference = src->reference & 3;
  40. dst->FieldOrderCnt[0] = src->field_poc[0];
  41. dst->FieldOrderCnt[1] = src->field_poc[1];
  42. }
  43. static int cuvid_h264_start_frame(AVCodecContext *avctx,
  44. const uint8_t *buffer, uint32_t size)
  45. {
  46. const H264Context *h = avctx->priv_data;
  47. const PPS *pps = h->ps.pps;
  48. const SPS *sps = h->ps.sps;
  49. CUVIDContext *ctx = avctx->internal->hwaccel_priv_data;
  50. CUVIDPICPARAMS *pp = &ctx->pic_params;
  51. CUVIDH264PICPARAMS *ppc = &pp->CodecSpecific.h264;
  52. FrameDecodeData *fdd;
  53. CUVIDFrame *cf;
  54. int i, dpb_size, ret;
  55. ret = ff_cuvid_start_frame(avctx, h->cur_pic_ptr->f);
  56. if (ret < 0)
  57. return ret;
  58. fdd = (FrameDecodeData*)h->cur_pic_ptr->f->opaque_ref->data;
  59. cf = (CUVIDFrame*)fdd->hwaccel_priv;
  60. *pp = (CUVIDPICPARAMS) {
  61. .PicWidthInMbs = h->mb_width,
  62. .FrameHeightInMbs = h->mb_height,
  63. .CurrPicIdx = cf->idx,
  64. .field_pic_flag = FIELD_PICTURE(h),
  65. .bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD,
  66. .second_field = FIELD_PICTURE(h) && !h->first_field,
  67. .ref_pic_flag = h->nal_ref_idc != 0,
  68. .intra_pic_flag = 0,
  69. .CodecSpecific.h264 = {
  70. .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
  71. .pic_order_cnt_type = sps->poc_type,
  72. .log2_max_pic_order_cnt_lsb_minus4 = FFMAX(sps->log2_max_poc_lsb - 4, 0),
  73. .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
  74. .frame_mbs_only_flag = sps->frame_mbs_only_flag,
  75. .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
  76. .num_ref_frames = sps->ref_frame_count,
  77. .residual_colour_transform_flag = sps->residual_color_transform_flag,
  78. .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
  79. .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
  80. .qpprime_y_zero_transform_bypass_flag = sps->transform_bypass,
  81. .entropy_coding_mode_flag = pps->cabac,
  82. .pic_order_present_flag = pps->pic_order_present,
  83. .num_ref_idx_l0_active_minus1 = pps->ref_count[0] - 1,
  84. .num_ref_idx_l1_active_minus1 = pps->ref_count[1] - 1,
  85. .weighted_pred_flag = pps->weighted_pred,
  86. .weighted_bipred_idc = pps->weighted_bipred_idc,
  87. .pic_init_qp_minus26 = pps->init_qp - 26,
  88. .deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present,
  89. .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present,
  90. .transform_8x8_mode_flag = pps->transform_8x8_mode,
  91. .MbaffFrameFlag = sps->mb_aff && !FIELD_PICTURE(h),
  92. .constrained_intra_pred_flag = pps->constrained_intra_pred,
  93. .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
  94. .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
  95. .ref_pic_flag = h->nal_ref_idc != 0,
  96. .frame_num = h->poc.frame_num,
  97. .CurrFieldOrderCnt[0] = h->cur_pic_ptr->field_poc[0],
  98. .CurrFieldOrderCnt[1] = h->cur_pic_ptr->field_poc[1],
  99. },
  100. };
  101. memcpy(ppc->WeightScale4x4, pps->scaling_matrix4, sizeof(ppc->WeightScale4x4));
  102. memcpy(ppc->WeightScale8x8[0], pps->scaling_matrix8[0], sizeof(ppc->WeightScale8x8[0]));
  103. memcpy(ppc->WeightScale8x8[1], pps->scaling_matrix8[3], sizeof(ppc->WeightScale8x8[0]));
  104. dpb_size = 0;
  105. for (i = 0; i < h->short_ref_count; i++)
  106. dpb_add(h, &ppc->dpb[dpb_size++], h->short_ref[i], h->short_ref[i]->frame_num);
  107. for (i = 0; i < 16; i++) {
  108. if (h->long_ref[i])
  109. dpb_add(h, &ppc->dpb[dpb_size++], h->long_ref[i], i);
  110. }
  111. for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->dpb); i++)
  112. ppc->dpb[i].PicIdx = -1;
  113. return 0;
  114. }
  115. static int cuvid_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
  116. uint32_t size)
  117. {
  118. CUVIDContext *ctx = avctx->internal->hwaccel_priv_data;
  119. void *tmp;
  120. tmp = av_fast_realloc(ctx->bitstream, &ctx->bitstream_allocated,
  121. ctx->bitstream_len + size + 3);
  122. if (!tmp)
  123. return AVERROR(ENOMEM);
  124. ctx->bitstream = tmp;
  125. tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
  126. (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
  127. if (!tmp)
  128. return AVERROR(ENOMEM);
  129. ctx->slice_offsets = tmp;
  130. AV_WB24(ctx->bitstream + ctx->bitstream_len, 1);
  131. memcpy(ctx->bitstream + ctx->bitstream_len + 3, buffer, size);
  132. ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
  133. ctx->bitstream_len += size + 3;
  134. ctx->nb_slices++;
  135. return 0;
  136. }
  137. static int cuvid_h264_decode_init(AVCodecContext *avctx)
  138. {
  139. const H264Context *h = avctx->priv_data;
  140. const SPS *sps = h->ps.sps;
  141. return ff_cuvid_decode_init(avctx, sps->ref_frame_count + sps->num_reorder_frames);
  142. }
  143. AVHWAccel ff_h264_cuvid_hwaccel = {
  144. .name = "h264_cuvid",
  145. .type = AVMEDIA_TYPE_VIDEO,
  146. .id = AV_CODEC_ID_H264,
  147. .pix_fmt = AV_PIX_FMT_CUDA,
  148. .start_frame = cuvid_h264_start_frame,
  149. .end_frame = ff_cuvid_end_frame,
  150. .decode_slice = cuvid_h264_decode_slice,
  151. .init = cuvid_h264_decode_init,
  152. .uninit = ff_cuvid_decode_uninit,
  153. .priv_data_size = sizeof(CUVIDContext),
  154. };