]> git.sesse.net Git - ffmpeg/blob - libavcodec/nvdec_vc1.c
Merge commit 'bad7ce1d82f0b7da55086b8c6124eff0d35a1b1a'
[ffmpeg] / libavcodec / nvdec_vc1.c
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 "avcodec.h"
24 #include "nvdec.h"
25 #include "decode.h"
26 #include "vc1.h"
27
28 static unsigned char get_ref_idx(AVFrame *frame)
29 {
30     FrameDecodeData *fdd;
31     NVDECFrame *cf;
32
33     if (!frame || !frame->private_ref)
34         return 255;
35
36     fdd = (FrameDecodeData*)frame->private_ref->data;
37     cf  = (NVDECFrame*)fdd->hwaccel_priv;
38
39     return cf->idx;
40 }
41
42 static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
43 {
44     VC1Context *v = avctx->priv_data;
45     MpegEncContext *s = &v->s;
46
47     NVDECContext      *ctx = avctx->internal->hwaccel_priv_data;
48     CUVIDPICPARAMS     *pp = &ctx->pic_params;
49     FrameDecodeData *fdd;
50     NVDECFrame *cf;
51     AVFrame *cur_frame = s->current_picture.f;
52
53     int ret;
54
55     ret = ff_nvdec_start_frame(avctx, cur_frame);
56     if (ret < 0)
57         return ret;
58
59     fdd = (FrameDecodeData*)cur_frame->private_ref->data;
60     cf  = (NVDECFrame*)fdd->hwaccel_priv;
61
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,
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
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,
80
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,
86
87             .profile           = v->profile,
88             .postprocflag      = v->postprocflag,
89             .pulldown          = v->broadcast,
90             .interlace         = v->interlace,
91             .tfcntrflag        = v->tfcntrflag,
92             .finterpflag       = v->finterpflag,
93             .psf               = v->psf,
94             .multires          = v->multires,
95             .syncmarker        = v->resync_marker,
96             .rangered          = v->rangered,
97             .maxbframes        = s->max_b_frames,
98
99             .panscan_flag      = v->panscanflag,
100             .refdist_flag      = v->refdist_flag,
101             .extended_mv       = v->extended_mv,
102             .dquant            = v->dquant,
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,
114         }
115     };
116
117     return 0;
118 }
119
120 static int nvdec_vc1_end_frame(AVCodecContext *avctx)
121 {
122     NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
123     int ret = ff_nvdec_end_frame(avctx);
124     ctx->bitstream = NULL;
125     return ret;
126 }
127
128 static int nvdec_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
129 {
130     NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
131     void *tmp;
132
133     tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
134                           (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
135     if (!tmp)
136         return AVERROR(ENOMEM);
137     ctx->slice_offsets = tmp;
138
139     if (!ctx->bitstream)
140         ctx->bitstream = (uint8_t*)buffer;
141
142     ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
143     ctx->bitstream_len += size;
144     ctx->nb_slices++;
145
146     return 0;
147 }
148
149 static int nvdec_vc1_frame_params(AVCodecContext *avctx,
150                                   AVBufferRef *hw_frames_ctx)
151 {
152     // Each frame can at most have one P and one B reference
153     return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2);
154 }
155
156 AVHWAccel ff_vc1_nvdec_hwaccel = {
157     .name                 = "vc1_nvdec",
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),
168 };
169
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),
183 };
184 #endif