]> git.sesse.net Git - ffmpeg/blob - libavcodec/vdpau_vp9.c
avcodec/alsdec: Discard frames for which no channel could be decoded
[ffmpeg] / libavcodec / vdpau_vp9.c
1 /*
2  * VP9 HW decode acceleration through VDPAU
3  *
4  * Copyright (c) 2019 Manoj Gupta Bonda
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 Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <vdpau/vdpau.h>
24 #include "libavutil/pixdesc.h"
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "vp9data.h"
28 #include "vp9dec.h"
29 #include "hwaccel.h"
30 #include "vdpau.h"
31 #include "vdpau_internal.h"
32
33 static int vdpau_vp9_start_frame(AVCodecContext *avctx,
34                                   const uint8_t *buffer, uint32_t size)
35 {
36     VP9Context *s = avctx->priv_data;
37     VP9SharedContext *h = &(s->s);
38     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
39     if (!pixdesc) {
40         return AV_PIX_FMT_NONE;
41     }
42
43     VP9Frame pic = h->frames[CUR_FRAME];
44     struct vdpau_picture_context *pic_ctx = pic.hwaccel_picture_private;
45     int i;
46
47     VdpPictureInfoVP9 *info = &pic_ctx->info.vp9;
48
49     info->width = avctx->width;
50     info->height = avctx->height;
51     /*  fill LvPictureInfoVP9 struct */
52     info->lastReference  = VDP_INVALID_HANDLE;
53     info->goldenReference = VDP_INVALID_HANDLE;
54     info->altReference = VDP_INVALID_HANDLE;
55
56     if (h->refs[h->h.refidx[0]].f && h->refs[h->h.refidx[0]].f->private_ref) {
57         info->lastReference               = ff_vdpau_get_surface_id(h->refs[h->h.refidx[0]].f);
58     }
59     if (h->refs[h->h.refidx[1]].f && h->refs[h->h.refidx[1]].f->private_ref) {
60         info->goldenReference             = ff_vdpau_get_surface_id(h->refs[h->h.refidx[1]].f);
61     }
62     if (h->refs[h->h.refidx[2]].f && h->refs[h->h.refidx[2]].f->private_ref) {
63         info->altReference                = ff_vdpau_get_surface_id(h->refs[h->h.refidx[2]].f);
64     }
65
66     info->profile                  = h->h.profile;
67     info->frameContextIdx          = h->h.framectxid;
68     info->keyFrame                 = h->h.keyframe;
69     info->showFrame                = !h->h.invisible;
70     info->errorResilient           = h->h.errorres;
71     info->frameParallelDecoding    = h->h.parallelmode;
72
73     info->subSamplingX             = pixdesc->log2_chroma_w;
74     info->subSamplingY             = pixdesc->log2_chroma_h;
75
76     info->intraOnly                = h->h.intraonly;
77     info->allowHighPrecisionMv     = h->h.keyframe ? 0 : h->h.highprecisionmvs;
78     info->refreshEntropyProbs      = h->h.refreshctx;
79
80     info->bitDepthMinus8Luma       = pixdesc->comp[0].depth - 8;
81     info->bitDepthMinus8Chroma     = pixdesc->comp[1].depth - 8;
82
83     info->loopFilterLevel          = h->h.filter.level;
84     info->loopFilterSharpness      = h->h.filter.sharpness;
85     info->modeRefLfEnabled         = h->h.lf_delta.enabled;
86
87     info->log2TileColumns          = h->h.tiling.log2_tile_cols;
88     info->log2TileRows             = h->h.tiling.log2_tile_rows;
89
90     info->segmentEnabled           = h->h.segmentation.enabled;
91     info->segmentMapUpdate         = h->h.segmentation.update_map;
92     info->segmentMapTemporalUpdate = h->h.segmentation.temporal;
93     info->segmentFeatureMode       = h->h.segmentation.absolute_vals;
94
95     info->qpYAc                    = h->h.yac_qi;
96     info->qpYDc                    = h->h.ydc_qdelta;
97     info->qpChDc                   = h->h.uvdc_qdelta;
98     info->qpChAc                   = h->h.uvac_qdelta;
99
100     info->resetFrameContext        = h->h.resetctx;
101     info->mcompFilterType          = h->h.filtermode ^ (h->h.filtermode <= 1);
102     info->uncompressedHeaderSize   = h->h.uncompressed_header_size;
103     info->compressedHeaderSize     = h->h.compressed_header_size;
104     info->refFrameSignBias[0]      = 0;
105
106
107     for (i = 0; i < FF_ARRAY_ELEMS(info->mbModeLfDelta); i++)
108         info->mbModeLfDelta[i] = h->h.lf_delta.mode[i];
109
110     for (i = 0; i < FF_ARRAY_ELEMS(info->mbRefLfDelta); i++)
111         info->mbRefLfDelta[i] = h->h.lf_delta.ref[i];
112
113     for (i = 0; i < FF_ARRAY_ELEMS(info->mbSegmentTreeProbs); i++)
114         info->mbSegmentTreeProbs[i] = h->h.segmentation.prob[i];
115
116     for (i = 0; i < FF_ARRAY_ELEMS(info->activeRefIdx); i++) {
117         info->activeRefIdx[i] = h->h.refidx[i];
118         info->segmentPredProbs[i] = h->h.segmentation.pred_prob[i];
119         info->refFrameSignBias[i + 1] = h->h.signbias[i];
120     }
121
122     for (i = 0; i < FF_ARRAY_ELEMS(info->segmentFeatureEnable); i++) {
123         info->segmentFeatureEnable[i][0] = h->h.segmentation.feat[i].q_enabled;
124         info->segmentFeatureEnable[i][1] = h->h.segmentation.feat[i].lf_enabled;
125         info->segmentFeatureEnable[i][2] = h->h.segmentation.feat[i].ref_enabled;
126         info->segmentFeatureEnable[i][3] = h->h.segmentation.feat[i].skip_enabled;
127
128         info->segmentFeatureData[i][0] = h->h.segmentation.feat[i].q_val;
129         info->segmentFeatureData[i][1] = h->h.segmentation.feat[i].lf_val;
130         info->segmentFeatureData[i][2] = h->h.segmentation.feat[i].ref_val;
131         info->segmentFeatureData[i][3] = 0;
132     }
133
134     switch (avctx->colorspace) {
135     default:
136     case AVCOL_SPC_UNSPECIFIED:
137         info->colorSpace = 0;
138         break;
139     case AVCOL_SPC_BT470BG:
140         info->colorSpace = 1;
141         break;
142     case AVCOL_SPC_BT709:
143         info->colorSpace = 2;
144         break;
145     case AVCOL_SPC_SMPTE170M:
146         info->colorSpace = 3;
147         break;
148     case AVCOL_SPC_SMPTE240M:
149         info->colorSpace = 4;
150         break;
151     case AVCOL_SPC_BT2020_NCL:
152         info->colorSpace = 5;
153         break;
154     case AVCOL_SPC_RESERVED:
155         info->colorSpace = 6;
156         break;
157     case AVCOL_SPC_RGB:
158         info->colorSpace = 7;
159         break;
160     }
161
162     return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
163
164 }
165
166 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
167
168 static int vdpau_vp9_decode_slice(AVCodecContext *avctx,
169                                    const uint8_t *buffer, uint32_t size)
170 {
171     VP9SharedContext *h = avctx->priv_data;
172     VP9Frame pic = h->frames[CUR_FRAME];
173     struct vdpau_picture_context *pic_ctx = pic.hwaccel_picture_private;
174
175     int val;
176
177     val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
178     if (val)
179         return val;
180
181     val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
182     if (val)
183         return val;
184
185     return 0;
186 }
187
188 static int vdpau_vp9_end_frame(AVCodecContext *avctx)
189 {
190     VP9SharedContext *h = avctx->priv_data;
191     VP9Frame pic = h->frames[CUR_FRAME];
192     struct vdpau_picture_context *pic_ctx = pic.hwaccel_picture_private;
193
194     int val;
195
196     val = ff_vdpau_common_end_frame(avctx, pic.tf.f, pic_ctx);
197     if (val < 0)
198         return val;
199
200     return 0;
201 }
202
203 static int vdpau_vp9_init(AVCodecContext *avctx)
204 {
205     VdpDecoderProfile profile;
206     uint32_t level = avctx->level;
207
208     switch (avctx->profile) {
209     case FF_PROFILE_VP9_0:
210         profile = VDP_DECODER_PROFILE_VP9_PROFILE_0;
211         break;
212     case FF_PROFILE_VP9_1:
213         profile = VDP_DECODER_PROFILE_VP9_PROFILE_1;
214         break;
215     case FF_PROFILE_VP9_2:
216         profile = VDP_DECODER_PROFILE_VP9_PROFILE_2;
217         break;
218     case FF_PROFILE_VP9_3:
219         profile = VDP_DECODER_PROFILE_VP9_PROFILE_3;
220         break;
221     default:
222         return AVERROR(ENOTSUP);
223     }
224
225     return ff_vdpau_common_init(avctx, profile, level);
226 }
227
228 const AVHWAccel ff_vp9_vdpau_hwaccel = {
229     .name           = "vp9_vdpau",
230     .type           = AVMEDIA_TYPE_VIDEO,
231     .id             = AV_CODEC_ID_VP9,
232     .pix_fmt        = AV_PIX_FMT_VDPAU,
233     .start_frame    = vdpau_vp9_start_frame,
234     .end_frame      = vdpau_vp9_end_frame,
235     .decode_slice   = vdpau_vp9_decode_slice,
236     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
237     .init           = vdpau_vp9_init,
238     .uninit         = ff_vdpau_common_uninit,
239     .frame_params   = ff_vdpau_common_frame_params,
240     .priv_data_size = sizeof(VDPAUContext),
241     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
242 };