2 * VC1 HW decode acceleration through NVDEC
4 * Copyright (c) 2017 Philip Langdale
6 * This file is part of FFmpeg.
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.
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.
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
28 static int get_ref_idx(AVFrame *frame)
33 if (!frame || !frame->private_ref)
36 fdd = (FrameDecodeData*)frame->private_ref->data;
37 cf = (NVDECFrame*)fdd->hwaccel_priv;
42 static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
44 VC1Context *v = avctx->priv_data;
45 MpegEncContext *s = &v->s;
47 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
48 CUVIDPICPARAMS *pp = &ctx->pic_params;
51 AVFrame *cur_frame = s->current_picture.f;
55 ret = ff_nvdec_start_frame(avctx, cur_frame);
59 fdd = (FrameDecodeData*)cur_frame->private_ref->data;
60 cf = (NVDECFrame*)fdd->hwaccel_priv;
62 *pp = (CUVIDPICPARAMS) {
63 .PicWidthInMbs = (cur_frame->width + 15) / 16,
64 .FrameHeightInMbs = (cur_frame->height + 15) / 16,
65 .CurrPicIdx = cf->idx,
66 .field_pic_flag = v->field_mode,
67 .bottom_field_flag = v->cur_field_type,
68 .second_field = v->second_field,
70 .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
71 s->pict_type == AV_PICTURE_TYPE_BI,
72 .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
73 s->pict_type == AV_PICTURE_TYPE_P,
75 .CodecSpecific.vc1 = {
76 .ForwardRefIdx = get_ref_idx(s->last_picture.f),
77 .BackwardRefIdx = get_ref_idx(s->next_picture.f),
78 .FrameWidth = cur_frame->width,
79 .FrameHeight = cur_frame->height,
81 .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
82 s->pict_type == AV_PICTURE_TYPE_BI,
83 .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
84 s->pict_type == AV_PICTURE_TYPE_P,
85 .progressive_fcm = v->fcm == 0,
87 .profile = v->profile,
88 .postprocflag = v->postprocflag,
89 .pulldown = v->broadcast,
90 .interlace = v->interlace,
91 .tfcntrflag = v->tfcntrflag,
92 .finterpflag = v->finterpflag,
94 .multires = v->multires,
95 .syncmarker = v->resync_marker,
96 .rangered = v->rangered,
97 .maxbframes = s->max_b_frames,
99 .panscan_flag = v->panscanflag,
100 .refdist_flag = v->refdist_flag,
101 .extended_mv = v->extended_mv,
103 .vstransform = v->vstransform,
104 .loopfilter = v->s.loop_filter,
105 .fastuvmc = v->fastuvmc,
106 .overlap = v->overlap,
107 .quantizer = v->quantizer_mode,
108 .extended_dmv = v->extended_dmv,
109 .range_mapy_flag = v->range_mapy_flag,
110 .range_mapy = v->range_mapy,
111 .range_mapuv_flag = v->range_mapuv_flag,
112 .range_mapuv = v->range_mapuv,
113 .rangeredfrm = v->rangeredfrm,
120 static int nvdec_vc1_end_frame(AVCodecContext *avctx)
122 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
123 int ret = ff_nvdec_end_frame(avctx);
124 ctx->bitstream = NULL;
128 static int nvdec_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
130 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
133 tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
134 (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
136 return AVERROR(ENOMEM);
137 ctx->slice_offsets = tmp;
140 ctx->bitstream = (uint8_t*)buffer;
142 ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
143 ctx->bitstream_len += size;
149 static int nvdec_vc1_frame_params(AVCodecContext *avctx,
150 AVBufferRef *hw_frames_ctx)
152 // Each frame can at most have one P and one B reference
153 return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2);
156 AVHWAccel ff_vc1_nvdec_hwaccel = {
158 .type = AVMEDIA_TYPE_VIDEO,
159 .id = AV_CODEC_ID_VC1,
160 .pix_fmt = AV_PIX_FMT_CUDA,
161 .start_frame = nvdec_vc1_start_frame,
162 .end_frame = nvdec_vc1_end_frame,
163 .decode_slice = nvdec_vc1_decode_slice,
164 .frame_params = nvdec_vc1_frame_params,
165 .init = ff_nvdec_decode_init,
166 .uninit = ff_nvdec_decode_uninit,
167 .priv_data_size = sizeof(NVDECContext),
170 #if CONFIG_WMV3_NVDEC_HWACCEL
171 AVHWAccel ff_wmv3_nvdec_hwaccel = {
172 .name = "wmv3_nvdec",
173 .type = AVMEDIA_TYPE_VIDEO,
174 .id = AV_CODEC_ID_WMV3,
175 .pix_fmt = AV_PIX_FMT_CUDA,
176 .start_frame = nvdec_vc1_start_frame,
177 .end_frame = nvdec_vc1_end_frame,
178 .decode_slice = nvdec_vc1_decode_slice,
179 .frame_params = nvdec_vc1_frame_params,
180 .init = ff_nvdec_decode_init,
181 .uninit = ff_nvdec_decode_uninit,
182 .priv_data_size = sizeof(NVDECContext),