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.

185 lines
6.3KB

  1. /*
  2. * VC1 HW decode acceleration through NVDEC
  3. *
  4. * Copyright (c) 2017 Philip Langdale
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "nvdec.h"
  24. #include "decode.h"
  25. #include "vc1.h"
  26. static unsigned char get_ref_idx(AVFrame *frame)
  27. {
  28. FrameDecodeData *fdd;
  29. NVDECFrame *cf;
  30. if (!frame || !frame->private_ref)
  31. return 255;
  32. fdd = (FrameDecodeData*)frame->private_ref->data;
  33. cf = (NVDECFrame*)fdd->hwaccel_priv;
  34. return cf->idx;
  35. }
  36. static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  37. {
  38. VC1Context *v = avctx->priv_data;
  39. MpegEncContext *s = &v->s;
  40. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  41. CUVIDPICPARAMS *pp = &ctx->pic_params;
  42. FrameDecodeData *fdd;
  43. NVDECFrame *cf;
  44. AVFrame *cur_frame = s->current_picture.f;
  45. int ret;
  46. ret = ff_nvdec_start_frame(avctx, cur_frame);
  47. if (ret < 0)
  48. return ret;
  49. fdd = (FrameDecodeData*)cur_frame->private_ref->data;
  50. cf = (NVDECFrame*)fdd->hwaccel_priv;
  51. *pp = (CUVIDPICPARAMS) {
  52. .PicWidthInMbs = (cur_frame->width + 15) / 16,
  53. .FrameHeightInMbs = (cur_frame->height + 15) / 16,
  54. .CurrPicIdx = cf->idx,
  55. .field_pic_flag = v->field_mode,
  56. .bottom_field_flag = v->cur_field_type,
  57. .second_field = v->second_field,
  58. .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  59. s->pict_type == AV_PICTURE_TYPE_BI,
  60. .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  61. s->pict_type == AV_PICTURE_TYPE_P,
  62. .CodecSpecific.vc1 = {
  63. .ForwardRefIdx = get_ref_idx(s->last_picture.f),
  64. .BackwardRefIdx = get_ref_idx(s->next_picture.f),
  65. .FrameWidth = cur_frame->width,
  66. .FrameHeight = cur_frame->height,
  67. .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  68. s->pict_type == AV_PICTURE_TYPE_BI,
  69. .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  70. s->pict_type == AV_PICTURE_TYPE_P,
  71. .progressive_fcm = v->fcm == 0,
  72. .profile = v->profile,
  73. .postprocflag = v->postprocflag,
  74. .pulldown = v->broadcast,
  75. .interlace = v->interlace,
  76. .tfcntrflag = v->tfcntrflag,
  77. .finterpflag = v->finterpflag,
  78. .psf = v->psf,
  79. .multires = v->multires,
  80. .syncmarker = v->resync_marker,
  81. .rangered = v->rangered,
  82. .maxbframes = s->max_b_frames,
  83. .panscan_flag = v->panscanflag,
  84. .refdist_flag = v->refdist_flag,
  85. .extended_mv = v->extended_mv,
  86. .dquant = v->dquant,
  87. .vstransform = v->vstransform,
  88. .loopfilter = v->s.loop_filter,
  89. .fastuvmc = v->fastuvmc,
  90. .overlap = v->overlap,
  91. .quantizer = v->quantizer_mode,
  92. .extended_dmv = v->extended_dmv,
  93. .range_mapy_flag = v->range_mapy_flag,
  94. .range_mapy = v->range_mapy,
  95. .range_mapuv_flag = v->range_mapuv_flag,
  96. .range_mapuv = v->range_mapuv,
  97. .rangeredfrm = v->rangeredfrm,
  98. }
  99. };
  100. return 0;
  101. }
  102. static int nvdec_vc1_end_frame(AVCodecContext *avctx)
  103. {
  104. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  105. int ret = ff_nvdec_end_frame(avctx);
  106. ctx->bitstream = NULL;
  107. return ret;
  108. }
  109. static int nvdec_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  110. {
  111. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  112. void *tmp;
  113. tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
  114. (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
  115. if (!tmp)
  116. return AVERROR(ENOMEM);
  117. ctx->slice_offsets = tmp;
  118. if (!ctx->bitstream)
  119. ctx->bitstream = (uint8_t*)buffer;
  120. ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
  121. ctx->bitstream_len += size;
  122. ctx->nb_slices++;
  123. return 0;
  124. }
  125. static int nvdec_vc1_frame_params(AVCodecContext *avctx,
  126. AVBufferRef *hw_frames_ctx)
  127. {
  128. // Each frame can at most have one P and one B reference
  129. return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2);
  130. }
  131. AVHWAccel ff_vc1_nvdec_hwaccel = {
  132. .name = "vc1_nvdec",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .id = AV_CODEC_ID_VC1,
  135. .pix_fmt = AV_PIX_FMT_CUDA,
  136. .start_frame = nvdec_vc1_start_frame,
  137. .end_frame = nvdec_vc1_end_frame,
  138. .decode_slice = nvdec_vc1_decode_slice,
  139. .frame_params = nvdec_vc1_frame_params,
  140. .init = ff_nvdec_decode_init,
  141. .uninit = ff_nvdec_decode_uninit,
  142. .priv_data_size = sizeof(NVDECContext),
  143. };
  144. #if CONFIG_WMV3_NVDEC_HWACCEL
  145. AVHWAccel ff_wmv3_nvdec_hwaccel = {
  146. .name = "wmv3_nvdec",
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .id = AV_CODEC_ID_WMV3,
  149. .pix_fmt = AV_PIX_FMT_CUDA,
  150. .start_frame = nvdec_vc1_start_frame,
  151. .end_frame = nvdec_vc1_end_frame,
  152. .decode_slice = nvdec_vc1_decode_slice,
  153. .frame_params = nvdec_vc1_frame_params,
  154. .init = ff_nvdec_decode_init,
  155. .uninit = ff_nvdec_decode_uninit,
  156. .priv_data_size = sizeof(NVDECContext),
  157. };
  158. #endif