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.

124 lines
4.6KB

  1. /*
  2. * MPEG-1/2 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 "mpegvideo.h"
  24. #include "nvdec.h"
  25. #include "decode.h"
  26. static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  27. {
  28. MpegEncContext *s = avctx->priv_data;
  29. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  30. CUVIDPICPARAMS *pp = &ctx->pic_params;
  31. CUVIDMPEG2PICPARAMS *ppc = &pp->CodecSpecific.mpeg2;
  32. FrameDecodeData *fdd;
  33. NVDECFrame *cf;
  34. AVFrame *cur_frame = s->current_picture.f;
  35. int ret, i;
  36. ret = ff_nvdec_start_frame(avctx, cur_frame);
  37. if (ret < 0)
  38. return ret;
  39. fdd = (FrameDecodeData*)cur_frame->private_ref->data;
  40. cf = (NVDECFrame*)fdd->hwaccel_priv;
  41. *pp = (CUVIDPICPARAMS) {
  42. .PicWidthInMbs = (cur_frame->width + 15) / 16,
  43. .FrameHeightInMbs = (cur_frame->height + 15) / 16,
  44. .CurrPicIdx = cf->idx,
  45. .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I,
  46. .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  47. s->pict_type == AV_PICTURE_TYPE_P,
  48. .CodecSpecific.mpeg2 = {
  49. .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
  50. .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f),
  51. .picture_coding_type = s->pict_type,
  52. .full_pel_forward_vector = s->full_pel[0],
  53. .full_pel_backward_vector = s->full_pel[1],
  54. .f_code = { { s->mpeg_f_code[0][0],
  55. s->mpeg_f_code[0][1] },
  56. { s->mpeg_f_code[1][0],
  57. s->mpeg_f_code[1][1] } },
  58. .intra_dc_precision = s->intra_dc_precision,
  59. .frame_pred_frame_dct = s->frame_pred_frame_dct,
  60. .concealment_motion_vectors = s->concealment_motion_vectors,
  61. .q_scale_type = s->q_scale_type,
  62. .intra_vlc_format = s->intra_vlc_format,
  63. .alternate_scan = s->alternate_scan,
  64. .top_field_first = s->top_field_first,
  65. }
  66. };
  67. for (i = 0; i < 64; ++i) {
  68. ppc->QuantMatrixIntra[i] = s->intra_matrix[i];
  69. ppc->QuantMatrixInter[i] = s->inter_matrix[i];
  70. }
  71. return 0;
  72. }
  73. static int nvdec_mpeg12_frame_params(AVCodecContext *avctx,
  74. AVBufferRef *hw_frames_ctx)
  75. {
  76. // Each frame can at most have one P and one B reference
  77. return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
  78. }
  79. #if CONFIG_MPEG2_NVDEC_HWACCEL
  80. const AVHWAccel ff_mpeg2_nvdec_hwaccel = {
  81. .name = "mpeg2_nvdec",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .id = AV_CODEC_ID_MPEG2VIDEO,
  84. .pix_fmt = AV_PIX_FMT_CUDA,
  85. .start_frame = nvdec_mpeg12_start_frame,
  86. .end_frame = ff_nvdec_simple_end_frame,
  87. .decode_slice = ff_nvdec_simple_decode_slice,
  88. .frame_params = nvdec_mpeg12_frame_params,
  89. .init = ff_nvdec_decode_init,
  90. .uninit = ff_nvdec_decode_uninit,
  91. .priv_data_size = sizeof(NVDECContext),
  92. };
  93. #endif
  94. #if CONFIG_MPEG1_NVDEC_HWACCEL
  95. const AVHWAccel ff_mpeg1_nvdec_hwaccel = {
  96. .name = "mpeg1_nvdec",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. .id = AV_CODEC_ID_MPEG1VIDEO,
  99. .pix_fmt = AV_PIX_FMT_CUDA,
  100. .start_frame = nvdec_mpeg12_start_frame,
  101. .end_frame = ff_nvdec_simple_end_frame,
  102. .decode_slice = ff_nvdec_simple_decode_slice,
  103. .frame_params = nvdec_mpeg12_frame_params,
  104. .init = ff_nvdec_decode_init,
  105. .uninit = ff_nvdec_decode_uninit,
  106. .priv_data_size = sizeof(NVDECContext),
  107. };
  108. #endif