FFmpeg
Loading...
Searching...
No Matches
nvdec_vp9.c
Go to the documentation of this file.
1/*
2 * VP9 HW decode acceleration through NVDEC
3 *
4 * Copyright (c) 2016 Timo Rothenpieler
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 "libavutil/pixdesc.h"
26
27#include "avcodec.h"
28#include "nvdec.h"
29#include "decode.h"
30#include "hwaccel_internal.h"
31#include "internal.h"
32#include "vp9shared.h"
33
35 const AVBufferRef *buffer_ref,
36 const uint8_t *buffer, uint32_t size)
37{
39 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
40
42 CUVIDPICPARAMS *pp = &ctx->pic_params;
43 CUVIDVP9PICPARAMS *ppc = &pp->CodecSpecific.vp9;
44 FrameDecodeData *fdd;
45 NVDECFrame *cf;
46 AVFrame *cur_frame = h->frames[CUR_FRAME].tf.f;
47
48 int ret, i;
49
50 ret = ff_nvdec_start_frame(avctx, cur_frame);
51 if (ret < 0)
52 return ret;
53
54 fdd = cur_frame->private_ref;
55 cf = (NVDECFrame*)fdd->hwaccel_priv;
56
57 *pp = (CUVIDPICPARAMS) {
58 .PicWidthInMbs = (cur_frame->width + 15) / 16,
59 .FrameHeightInMbs = (cur_frame->height + 15) / 16,
60 .CurrPicIdx = cf->idx,
61
62 .CodecSpecific.vp9 = {
63 .width = cur_frame->width,
64 .height = cur_frame->height,
65
66 .LastRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[0]].f),
67 .GoldenRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[1]].f),
68 .AltRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[2]].f),
69
70 .profile = h->h.profile,
71 .frameContextIdx = h->h.framectxid,
72 .frameType = !h->h.keyframe,
73 .showFrame = !h->h.invisible,
74 .errorResilient = h->h.errorres,
75 .frameParallelDecoding = h->h.parallelmode,
76 .subSamplingX = pixdesc->log2_chroma_w,
77 .subSamplingY = pixdesc->log2_chroma_h,
78 .intraOnly = h->h.intraonly,
79 .allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs,
80 .refreshEntropyProbs = h->h.refreshctx,
81
82 .bitDepthMinus8Luma = pixdesc->comp[0].depth - 8,
83 .bitDepthMinus8Chroma = pixdesc->comp[1].depth - 8,
84
85 .loopFilterLevel = h->h.filter.level,
86 .loopFilterSharpness = h->h.filter.sharpness,
87 .modeRefLfEnabled = h->h.lf_delta.enabled,
88
89 .log2_tile_columns = h->h.tiling.log2_tile_cols,
90 .log2_tile_rows = h->h.tiling.log2_tile_rows,
91
92 .segmentEnabled = h->h.segmentation.enabled,
93 .segmentMapUpdate = h->h.segmentation.update_map,
94 .segmentMapTemporalUpdate = h->h.segmentation.temporal,
95 .segmentFeatureMode = h->h.segmentation.absolute_vals,
96
97 .qpYAc = h->h.yac_qi,
98 .qpYDc = h->h.ydc_qdelta,
99 .qpChDc = h->h.uvdc_qdelta,
100 .qpChAc = h->h.uvac_qdelta,
101
102 .resetFrameContext = h->h.resetctx,
103 .mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1),
104
105 .frameTagSize = h->h.uncompressed_header_size,
106 .offsetToDctParts = h->h.compressed_header_size,
107
108 .refFrameSignBias[0] = 0,
109 }
110 };
111
112 for (i = 0; i < 2; i++)
113 ppc->mbModeLfDelta[i] = h->h.lf_delta.mode[i];
114
115 for (i = 0; i < 4; i++)
116 ppc->mbRefLfDelta[i] = h->h.lf_delta.ref[i];
117
118 for (i = 0; i < 7; i++)
119 ppc->mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
120
121 for (i = 0; i < 3; i++) {
122 ppc->activeRefIdx[i] = h->h.refidx[i];
123 ppc->segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
124 ppc->refFrameSignBias[i + 1] = h->h.signbias[i];
125 }
126
127 for (i = 0; i < 8; i++) {
128 ppc->segmentFeatureEnable[i][0] = h->h.segmentation.feat[i].q_enabled;
129 ppc->segmentFeatureEnable[i][1] = h->h.segmentation.feat[i].lf_enabled;
130 ppc->segmentFeatureEnable[i][2] = h->h.segmentation.feat[i].ref_enabled;
131 ppc->segmentFeatureEnable[i][3] = h->h.segmentation.feat[i].skip_enabled;
132
133 ppc->segmentFeatureData[i][0] = h->h.segmentation.feat[i].q_val;
134 ppc->segmentFeatureData[i][1] = h->h.segmentation.feat[i].lf_val;
135 ppc->segmentFeatureData[i][2] = h->h.segmentation.feat[i].ref_val;
136 ppc->segmentFeatureData[i][3] = 0;
137 }
138
139 switch (avctx->colorspace) {
140 default:
142 ppc->colorSpace = 0;
143 break;
145 ppc->colorSpace = 1;
146 break;
147 case AVCOL_SPC_BT709:
148 ppc->colorSpace = 2;
149 break;
151 ppc->colorSpace = 3;
152 break;
154 ppc->colorSpace = 4;
155 break;
157 ppc->colorSpace = 5;
158 break;
160 ppc->colorSpace = 6;
161 break;
162 case AVCOL_SPC_RGB:
163 ppc->colorSpace = 7;
164 break;
165 }
166
167 return 0;
168}
169
171 AVBufferRef *hw_frames_ctx,
172 enum AVPixelFormat hw_format)
173{
174 // VP9 uses a fixed size pool of 8 possible reference frames
175 return ff_nvdec_frame_params(avctx, hw_frames_ctx, hw_format, 8, 0);
176}
177
178#if CONFIG_VP9_NVDEC_HWACCEL
179static int nvdec_vp9_cuda_frame_params(AVCodecContext *avctx,
180 AVBufferRef *hw_frames_ctx)
181{
182 return nvdec_vp9_frame_params(avctx, hw_frames_ctx, AV_PIX_FMT_CUDA);
183}
184
186 .p.name = "vp9_nvdec",
187 .p.type = AVMEDIA_TYPE_VIDEO,
188 .p.id = AV_CODEC_ID_VP9,
189 .p.pix_fmt = AV_PIX_FMT_CUDA,
190 .start_frame = nvdec_vp9_start_frame,
191 .end_frame = ff_nvdec_simple_end_frame,
192 .decode_slice = ff_nvdec_simple_decode_slice,
193 .frame_params = nvdec_vp9_cuda_frame_params,
194 .init = ff_nvdec_decode_init,
195 .uninit = ff_nvdec_decode_uninit,
196 .priv_data_size = sizeof(NVDECContext),
197};
198#endif
199
200#if CONFIG_VP9_NVDEC_CUARRAY_HWACCEL
201static int nvdec_vp9_cuarray_frame_params(AVCodecContext *avctx,
202 AVBufferRef *hw_frames_ctx)
203{
204 return nvdec_vp9_frame_params(avctx, hw_frames_ctx, AV_PIX_FMT_CUARRAY);
205}
206
208 .p.name = "vp9_nvdec_cuarray",
209 .p.type = AVMEDIA_TYPE_VIDEO,
210 .p.id = AV_CODEC_ID_VP9,
211 .p.pix_fmt = AV_PIX_FMT_CUARRAY,
212 .start_frame = nvdec_vp9_start_frame,
213 .end_frame = ff_nvdec_simple_end_frame,
214 .decode_slice = ff_nvdec_simple_decode_slice,
215 .frame_params = nvdec_vp9_cuarray_frame_params,
216 .init = ff_nvdec_decode_init,
217 .uninit = ff_nvdec_decode_uninit,
218 .priv_data_size = sizeof(NVDECContext),
219};
220#endif
static AVFormatContext * ctx
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const struct FFHWAccel ff_vp9_nvdec_hwaccel
const struct FFHWAccel ff_vp9_nvdec_cuarray_hwaccel
common internal api header.
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition nvdec.c:1051
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition nvdec.c:1061
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition nvdec.c:404
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition nvdec.c:1180
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition nvdec.c:946
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition nvdec.c:297
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, enum AVPixelFormat hw_format, int dpb_size, int supports_444)
Definition nvdec.c:1083
static int nvdec_vp9_start_frame(AVCodecContext *avctx, const AVBufferRef *buffer_ref, const uint8_t *buffer, uint32_t size)
Definition nvdec_vp9.c:34
static int nvdec_vp9_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, enum AVPixelFormat hw_format)
Definition nvdec_vp9.c:170
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_CUARRAY
hardware decoding through openharmony
Definition pixfmt.h:506
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:708
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:712
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:717
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:713
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition pixfmt.h:714
@ AVCOL_SPC_RESERVED
reserved for future use by ITU-T and ISO/IEC just like 15-255 are
Definition pixfmt.h:710
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
int depth
Number of bits in the component.
Definition pixdesc.h:57
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int width
Definition frame.h:544
int height
Definition frame.h:544
void * private_ref
RefStruct reference for internal use by a single libav* library.
Definition frame.h:810
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition decode.h:33
void * hwaccel_priv
Definition decode.h:54
unsigned int idx
Definition nvdec.h:55
static char buffer[20]
Definition seek.c:32
int size
#define CUR_FRAME
Definition vp9shared.h:172