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
23 #include "config_components.h"
24
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "nvdec.h"
28 #include "decode.h"
29 #include "vc1.h"
30
nvdec_vc1_start_frame(AVCodecContext * avctx,const uint8_t * buffer,uint32_t size)31 static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
32 {
33 VC1Context *v = avctx->priv_data;
34 MpegEncContext *s = &v->s;
35
36 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
37 CUVIDPICPARAMS *pp = &ctx->pic_params;
38 FrameDecodeData *fdd;
39 NVDECFrame *cf;
40 AVFrame *cur_frame = s->current_picture.f;
41
42 int ret;
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 .field_pic_flag = v->field_mode,
56 .bottom_field_flag = v->cur_field_type,
57 .second_field = v->second_field,
58
59 .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
60 s->pict_type == AV_PICTURE_TYPE_BI,
61 .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
62 s->pict_type == AV_PICTURE_TYPE_P,
63
64 .CodecSpecific.vc1 = {
65 .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
66 .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f),
67 .FrameWidth = cur_frame->width,
68 .FrameHeight = cur_frame->height,
69
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,
74 .progressive_fcm = v->fcm == 0,
75
76 .profile = v->profile,
77 .postprocflag = v->postprocflag,
78 .pulldown = v->broadcast,
79 .interlace = v->interlace,
80 .tfcntrflag = v->tfcntrflag,
81 .finterpflag = v->finterpflag,
82 .psf = v->psf,
83 .multires = v->multires,
84 .syncmarker = v->resync_marker,
85 .rangered = v->rangered,
86 .maxbframes = s->max_b_frames,
87
88 .panscan_flag = v->panscanflag,
89 .refdist_flag = v->refdist_flag,
90 .extended_mv = v->extended_mv,
91 .dquant = v->dquant,
92 .vstransform = v->vstransform,
93 .loopfilter = v->s.loop_filter,
94 .fastuvmc = v->fastuvmc,
95 .overlap = v->overlap,
96 .quantizer = v->quantizer_mode,
97 .extended_dmv = v->extended_dmv,
98 .range_mapy_flag = v->range_mapy_flag,
99 .range_mapy = v->range_mapy,
100 .range_mapuv_flag = v->range_mapuv_flag,
101 .range_mapuv = v->range_mapuv,
102 .rangeredfrm = v->rangeredfrm,
103 }
104 };
105
106 return 0;
107 }
108
nvdec_vc1_frame_params(AVCodecContext * avctx,AVBufferRef * hw_frames_ctx)109 static int nvdec_vc1_frame_params(AVCodecContext *avctx,
110 AVBufferRef *hw_frames_ctx)
111 {
112 // Each frame can at most have one P and one B reference
113 return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
114 }
115
116 const AVHWAccel ff_vc1_nvdec_hwaccel = {
117 .name = "vc1_nvdec",
118 .type = AVMEDIA_TYPE_VIDEO,
119 .id = AV_CODEC_ID_VC1,
120 .pix_fmt = AV_PIX_FMT_CUDA,
121 .start_frame = nvdec_vc1_start_frame,
122 .end_frame = ff_nvdec_simple_end_frame,
123 .decode_slice = ff_nvdec_simple_decode_slice,
124 .frame_params = nvdec_vc1_frame_params,
125 .init = ff_nvdec_decode_init,
126 .uninit = ff_nvdec_decode_uninit,
127 .priv_data_size = sizeof(NVDECContext),
128 };
129
130 #if CONFIG_WMV3_NVDEC_HWACCEL
131 const AVHWAccel ff_wmv3_nvdec_hwaccel = {
132 .name = "wmv3_nvdec",
133 .type = AVMEDIA_TYPE_VIDEO,
134 .id = AV_CODEC_ID_WMV3,
135 .pix_fmt = AV_PIX_FMT_CUDA,
136 .start_frame = nvdec_vc1_start_frame,
137 .end_frame = ff_nvdec_simple_end_frame,
138 .decode_slice = ff_nvdec_simple_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 #endif
145