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