FFmpeg  4.0
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 "libavutil/pixdesc.h"
24 
25 #include "avcodec.h"
26 #include "nvdec.h"
27 #include "decode.h"
28 #include "internal.h"
29 #include "vp9shared.h"
30 
31 static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
32 {
33  VP9SharedContext *h = avctx->priv_data;
34  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
35 
37  CUVIDPICPARAMS *pp = &ctx->pic_params;
38  CUVIDVP9PICPARAMS *ppc = &pp->CodecSpecific.vp9;
39  FrameDecodeData *fdd;
40  NVDECFrame *cf;
41  AVFrame *cur_frame = h->frames[CUR_FRAME].tf.f;
42 
43  int ret, i;
44 
45  ret = ff_nvdec_start_frame(avctx, cur_frame);
46  if (ret < 0)
47  return ret;
48 
49  fdd = (FrameDecodeData*)cur_frame->private_ref->data;
50  cf = (NVDECFrame*)fdd->hwaccel_priv;
51 
52  *pp = (CUVIDPICPARAMS) {
53  .PicWidthInMbs = (cur_frame->width + 15) / 16,
54  .FrameHeightInMbs = (cur_frame->height + 15) / 16,
55  .CurrPicIdx = cf->idx,
56 
57  .CodecSpecific.vp9 = {
58  .width = cur_frame->width,
59  .height = cur_frame->height,
60 
61  .LastRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[0]].f),
62  .GoldenRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[1]].f),
63  .AltRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[2]].f),
64 
65  .profile = h->h.profile,
66  .frameContextIdx = h->h.framectxid,
67  .frameType = !h->h.keyframe,
68  .showFrame = !h->h.invisible,
69  .errorResilient = h->h.errorres,
70  .frameParallelDecoding = h->h.parallelmode,
71  .subSamplingX = pixdesc->log2_chroma_w,
72  .subSamplingY = pixdesc->log2_chroma_h,
73  .intraOnly = h->h.intraonly,
74  .allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs,
75  .refreshEntropyProbs = h->h.refreshctx,
76 
77  .bitDepthMinus8Luma = pixdesc->comp[0].depth - 8,
78  .bitDepthMinus8Chroma = pixdesc->comp[1].depth - 8,
79 
80  .loopFilterLevel = h->h.filter.level,
81  .loopFilterSharpness = h->h.filter.sharpness,
82  .modeRefLfEnabled = h->h.lf_delta.enabled,
83 
84  .log2_tile_columns = h->h.tiling.log2_tile_cols,
85  .log2_tile_rows = h->h.tiling.log2_tile_rows,
86 
87  .segmentEnabled = h->h.segmentation.enabled,
88  .segmentMapUpdate = h->h.segmentation.update_map,
89  .segmentMapTemporalUpdate = h->h.segmentation.temporal,
90  .segmentFeatureMode = h->h.segmentation.absolute_vals,
91 
92  .qpYAc = h->h.yac_qi,
93  .qpYDc = h->h.ydc_qdelta,
94  .qpChDc = h->h.uvdc_qdelta,
95  .qpChAc = h->h.uvac_qdelta,
96 
97  .resetFrameContext = h->h.resetctx,
98  .mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1),
99 
100  .frameTagSize = h->h.uncompressed_header_size,
101  .offsetToDctParts = h->h.compressed_header_size,
102 
103  .refFrameSignBias[0] = 0,
104  }
105  };
106 
107  for (i = 0; i < 2; i++)
108  ppc->mbModeLfDelta[i] = h->h.lf_delta.mode[i];
109 
110  for (i = 0; i < 4; i++)
111  ppc->mbRefLfDelta[i] = h->h.lf_delta.ref[i];
112 
113  for (i = 0; i < 7; i++)
114  ppc->mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
115 
116  for (i = 0; i < 3; i++) {
117  ppc->activeRefIdx[i] = h->h.refidx[i];
118  ppc->segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
119  ppc->refFrameSignBias[i + 1] = h->h.signbias[i];
120  }
121 
122  for (i = 0; i < 8; i++) {
123  ppc->segmentFeatureEnable[i][0] = h->h.segmentation.feat[i].q_enabled;
124  ppc->segmentFeatureEnable[i][1] = h->h.segmentation.feat[i].lf_enabled;
125  ppc->segmentFeatureEnable[i][2] = h->h.segmentation.feat[i].ref_enabled;
126  ppc->segmentFeatureEnable[i][3] = h->h.segmentation.feat[i].skip_enabled;
127 
128  ppc->segmentFeatureData[i][0] = h->h.segmentation.feat[i].q_val;
129  ppc->segmentFeatureData[i][1] = h->h.segmentation.feat[i].lf_val;
130  ppc->segmentFeatureData[i][2] = h->h.segmentation.feat[i].ref_val;
131  ppc->segmentFeatureData[i][3] = 0;
132  }
133 
134  switch (avctx->colorspace) {
135  default:
137  ppc->colorSpace = 0;
138  break;
139  case AVCOL_SPC_BT470BG:
140  ppc->colorSpace = 1;
141  break;
142  case AVCOL_SPC_BT709:
143  ppc->colorSpace = 2;
144  break;
145  case AVCOL_SPC_SMPTE170M:
146  ppc->colorSpace = 3;
147  break;
148  case AVCOL_SPC_SMPTE240M:
149  ppc->colorSpace = 4;
150  break;
152  ppc->colorSpace = 5;
153  break;
154  case AVCOL_SPC_RESERVED:
155  ppc->colorSpace = 6;
156  break;
157  case AVCOL_SPC_RGB:
158  ppc->colorSpace = 7;
159  break;
160  }
161 
162  return 0;
163 }
164 
166  AVBufferRef *hw_frames_ctx)
167 {
168  // VP9 uses a fixed size pool of 8 possible reference frames
169  return ff_nvdec_frame_params(avctx, hw_frames_ctx, 8);
170 }
171 
173  .name = "vp9_nvdec",
174  .type = AVMEDIA_TYPE_VIDEO,
175  .id = AV_CODEC_ID_VP9,
176  .pix_fmt = AV_PIX_FMT_CUDA,
177  .start_frame = nvdec_vp9_start_frame,
178  .end_frame = ff_nvdec_simple_end_frame,
179  .decode_slice = ff_nvdec_simple_decode_slice,
180  .frame_params = nvdec_vp9_frame_params,
181  .init = ff_nvdec_decode_init,
182  .uninit = ff_nvdec_decode_uninit,
183  .priv_data_size = sizeof(NVDECContext),
184 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:475
ThreadFrame tf
Definition: vp9shared.h:60
uint8_t parallelmode
Definition: vp9shared.h:108
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
uint8_t update_map
Definition: vp9shared.h:133
AVFrame * f
Definition: thread.h:35
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:479
VP9BitstreamHeader h
Definition: vp9shared.h:160
uint8_t prob[7]
Definition: vp9shared.h:134
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:480
uint8_t framectxid
Definition: vp9shared.h:109
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:494
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:474
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
functionally identical to above
Definition: pixfmt.h:481
unsigned log2_tile_rows
Definition: vp9shared.h:151
int uncompressed_header_size
Definition: vp9shared.h:155
enum FilterMode filtermode
Definition: vp9shared.h:105
static char buffer[20]
Definition: seek.c:32
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
uint8_t absolute_vals
Definition: vp9shared.h:132
VP9Frame frames[3]
Definition: vp9shared.h:166
CUVIDPICPARAMS pic_params
Definition: nvdec.h:51
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:596
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:502
unsigned log2_tile_cols
Definition: vp9shared.h:151
uint8_t refidx[3]
Definition: vp9shared.h:111
struct VP9BitstreamHeader::@161::@163 feat[MAX_SEGMENT]
int width
Definition: frame.h:276
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
uint8_t signbias[3]
Definition: vp9shared.h:112
uint8_t refreshctx
Definition: vp9shared.h:107
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:418
struct VP9BitstreamHeader::@159 filter
unsigned int idx
Definition: nvdec.h:45
struct VP9BitstreamHeader::@160 lf_delta
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:264
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size)
Definition: nvdec.c:524
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3582
struct VP9BitstreamHeader::@162 tiling
uint8_t keyframe
Definition: vp9shared.h:98
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:484
AVFormatContext * ctx
Definition: movenc.c:48
HW acceleration through CUDA.
Definition: pixfmt.h:231
struct VP9BitstreamHeader::@161 segmentation
static int nvdec_vp9_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_vp9.c:165
Libavcodec external API header.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1518
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:246
uint8_t * data
The data buffer.
Definition: buffer.h:89
int8_t mode[2]
Definition: vp9shared.h:122
#define CUR_FRAME
Definition: vp9shared.h:163
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2141
static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_vp9.c:31
A reference to a data buffer.
Definition: buffer.h:81
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:34
common internal api header.
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:570
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:190
const AVHWAccel ff_vp9_nvdec_hwaccel
Definition: nvdec_vp9.c:172
void * priv_data
Definition: avcodec.h:1545
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1553
uint8_t invisible
Definition: vp9shared.h:99
int height
Definition: frame.h:276
ThreadFrame refs[8]
Definition: vp9shared.h:162
uint8_t pred_prob[3]
Definition: vp9shared.h:135
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:52
int depth
Number of bits in the component.
Definition: pixdesc.h:58
uint8_t highprecisionmvs
Definition: vp9shared.h:104
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3047
for(j=16;j >0;--j)
int compressed_header_size
Definition: vp9shared.h:156