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.

122 lines
4.7KB

  1. /*
  2. * MPEG-4 Part 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 "mpeg4video.h"
  24. #include "nvdec.h"
  25. #include "decode.h"
  26. static int nvdec_mpeg4_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  27. {
  28. Mpeg4DecContext *m = avctx->priv_data;
  29. MpegEncContext *s = &m->m;
  30. NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
  31. CUVIDPICPARAMS *pp = &ctx->pic_params;
  32. CUVIDMPEG4PICPARAMS *ppc = &pp->CodecSpecific.mpeg4;
  33. FrameDecodeData *fdd;
  34. NVDECFrame *cf;
  35. AVFrame *cur_frame = s->current_picture.f;
  36. int ret, i;
  37. ret = ff_nvdec_start_frame(avctx, cur_frame);
  38. if (ret < 0)
  39. return ret;
  40. fdd = (FrameDecodeData*)cur_frame->private_ref->data;
  41. cf = (NVDECFrame*)fdd->hwaccel_priv;
  42. *pp = (CUVIDPICPARAMS) {
  43. .PicWidthInMbs = (cur_frame->width + 15) / 16,
  44. .FrameHeightInMbs = (cur_frame->height + 15) / 16,
  45. .CurrPicIdx = cf->idx,
  46. .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I,
  47. .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
  48. s->pict_type == AV_PICTURE_TYPE_P ||
  49. s->pict_type == AV_PICTURE_TYPE_S,
  50. .CodecSpecific.mpeg4 = {
  51. .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
  52. .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f),
  53. .video_object_layer_width = s->width,
  54. .video_object_layer_height = s->height,
  55. .vop_time_increment_bitcount = m->time_increment_bits,
  56. .top_field_first = s->top_field_first,
  57. .resync_marker_disable = !m->resync_marker,
  58. .quant_type = s->mpeg_quant,
  59. .quarter_sample = s->quarter_sample,
  60. .short_video_header = avctx->codec->id == AV_CODEC_ID_H263,
  61. .divx_flags = s->divx_packed ? 5 : 0,
  62. .vop_coding_type = s->pict_type - AV_PICTURE_TYPE_I,
  63. .vop_coded = 1,
  64. .vop_rounding_type = s->no_rounding,
  65. .alternate_vertical_scan_flag = s->alternate_scan,
  66. .interlaced = !s->progressive_sequence,
  67. .vop_fcode_forward = s->f_code,
  68. .vop_fcode_backward = s->b_code,
  69. .trd = { s->pp_time, s->pp_field_time >> 1 },
  70. .trb = { s->pb_time, s->pb_field_time >> 1 },
  71. .gmc_enabled = s->pict_type == AV_PICTURE_TYPE_S &&
  72. m->vol_sprite_usage == GMC_SPRITE,
  73. }
  74. };
  75. for (i = 0; i < 64; ++i) {
  76. ppc->QuantMatrixIntra[i] = s->intra_matrix[i];
  77. ppc->QuantMatrixInter[i] = s->inter_matrix[i];
  78. }
  79. // We need to pass the full frame buffer and not just the slice
  80. return ff_nvdec_simple_decode_slice(avctx, buffer, size);
  81. }
  82. static int nvdec_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  83. {
  84. return 0;
  85. }
  86. static int nvdec_mpeg4_frame_params(AVCodecContext *avctx,
  87. AVBufferRef *hw_frames_ctx)
  88. {
  89. // Each frame can at most have one P and one B reference
  90. return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
  91. }
  92. const AVHWAccel ff_mpeg4_nvdec_hwaccel = {
  93. .name = "mpeg4_nvdec",
  94. .type = AVMEDIA_TYPE_VIDEO,
  95. .id = AV_CODEC_ID_MPEG4,
  96. .pix_fmt = AV_PIX_FMT_CUDA,
  97. .start_frame = nvdec_mpeg4_start_frame,
  98. .end_frame = ff_nvdec_simple_end_frame,
  99. .decode_slice = nvdec_mpeg4_decode_slice,
  100. .frame_params = nvdec_mpeg4_frame_params,
  101. .init = ff_nvdec_decode_init,
  102. .uninit = ff_nvdec_decode_uninit,
  103. .priv_data_size = sizeof(NVDECContext),
  104. };