FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "hwaccel_internal.h"
29 #include "internal.h"
30 #include "vp9shared.h"
31 
33  const AVBufferRef *buffer_ref,
34  const uint8_t *buffer, uint32_t size)
35 {
36  VP9SharedContext *h = avctx->priv_data;
37  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
38 
40  CUVIDPICPARAMS *pp = &ctx->pic_params;
41  CUVIDVP9PICPARAMS *ppc = &pp->CodecSpecific.vp9;
42  FrameDecodeData *fdd;
43  NVDECFrame *cf;
44  AVFrame *cur_frame = h->frames[CUR_FRAME].tf.f;
45 
46  int ret, i;
47 
48  ret = ff_nvdec_start_frame(avctx, cur_frame);
49  if (ret < 0)
50  return ret;
51 
52  fdd = (FrameDecodeData*)cur_frame->private_ref->data;
53  cf = (NVDECFrame*)fdd->hwaccel_priv;
54 
55  *pp = (CUVIDPICPARAMS) {
56  .PicWidthInMbs = (cur_frame->width + 15) / 16,
57  .FrameHeightInMbs = (cur_frame->height + 15) / 16,
58  .CurrPicIdx = cf->idx,
59 
60  .CodecSpecific.vp9 = {
61  .width = cur_frame->width,
62  .height = cur_frame->height,
63 
64  .LastRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[0]].f),
65  .GoldenRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[1]].f),
66  .AltRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[2]].f),
67 
68  .profile = h->h.profile,
69  .frameContextIdx = h->h.framectxid,
70  .frameType = !h->h.keyframe,
71  .showFrame = !h->h.invisible,
72  .errorResilient = h->h.errorres,
73  .frameParallelDecoding = h->h.parallelmode,
74  .subSamplingX = pixdesc->log2_chroma_w,
75  .subSamplingY = pixdesc->log2_chroma_h,
76  .intraOnly = h->h.intraonly,
77  .allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs,
78  .refreshEntropyProbs = h->h.refreshctx,
79 
80  .bitDepthMinus8Luma = pixdesc->comp[0].depth - 8,
81  .bitDepthMinus8Chroma = pixdesc->comp[1].depth - 8,
82 
83  .loopFilterLevel = h->h.filter.level,
84  .loopFilterSharpness = h->h.filter.sharpness,
85  .modeRefLfEnabled = h->h.lf_delta.enabled,
86 
87  .log2_tile_columns = h->h.tiling.log2_tile_cols,
88  .log2_tile_rows = h->h.tiling.log2_tile_rows,
89 
90  .segmentEnabled = h->h.segmentation.enabled,
91  .segmentMapUpdate = h->h.segmentation.update_map,
92  .segmentMapTemporalUpdate = h->h.segmentation.temporal,
93  .segmentFeatureMode = h->h.segmentation.absolute_vals,
94 
95  .qpYAc = h->h.yac_qi,
96  .qpYDc = h->h.ydc_qdelta,
97  .qpChDc = h->h.uvdc_qdelta,
98  .qpChAc = h->h.uvac_qdelta,
99 
100  .resetFrameContext = h->h.resetctx,
101  .mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1),
102 
103  .frameTagSize = h->h.uncompressed_header_size,
104  .offsetToDctParts = h->h.compressed_header_size,
105 
106  .refFrameSignBias[0] = 0,
107  }
108  };
109 
110  for (i = 0; i < 2; i++)
111  ppc->mbModeLfDelta[i] = h->h.lf_delta.mode[i];
112 
113  for (i = 0; i < 4; i++)
114  ppc->mbRefLfDelta[i] = h->h.lf_delta.ref[i];
115 
116  for (i = 0; i < 7; i++)
117  ppc->mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
118 
119  for (i = 0; i < 3; i++) {
120  ppc->activeRefIdx[i] = h->h.refidx[i];
121  ppc->segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
122  ppc->refFrameSignBias[i + 1] = h->h.signbias[i];
123  }
124 
125  for (i = 0; i < 8; i++) {
126  ppc->segmentFeatureEnable[i][0] = h->h.segmentation.feat[i].q_enabled;
127  ppc->segmentFeatureEnable[i][1] = h->h.segmentation.feat[i].lf_enabled;
128  ppc->segmentFeatureEnable[i][2] = h->h.segmentation.feat[i].ref_enabled;
129  ppc->segmentFeatureEnable[i][3] = h->h.segmentation.feat[i].skip_enabled;
130 
131  ppc->segmentFeatureData[i][0] = h->h.segmentation.feat[i].q_val;
132  ppc->segmentFeatureData[i][1] = h->h.segmentation.feat[i].lf_val;
133  ppc->segmentFeatureData[i][2] = h->h.segmentation.feat[i].ref_val;
134  ppc->segmentFeatureData[i][3] = 0;
135  }
136 
137  switch (avctx->colorspace) {
138  default:
140  ppc->colorSpace = 0;
141  break;
142  case AVCOL_SPC_BT470BG:
143  ppc->colorSpace = 1;
144  break;
145  case AVCOL_SPC_BT709:
146  ppc->colorSpace = 2;
147  break;
148  case AVCOL_SPC_SMPTE170M:
149  ppc->colorSpace = 3;
150  break;
151  case AVCOL_SPC_SMPTE240M:
152  ppc->colorSpace = 4;
153  break;
155  ppc->colorSpace = 5;
156  break;
157  case AVCOL_SPC_RESERVED:
158  ppc->colorSpace = 6;
159  break;
160  case AVCOL_SPC_RGB:
161  ppc->colorSpace = 7;
162  break;
163  }
164 
165  return 0;
166 }
167 
169  AVBufferRef *hw_frames_ctx)
170 {
171  // VP9 uses a fixed size pool of 8 possible reference frames
172  return ff_nvdec_frame_params(avctx, hw_frames_ctx, 8, 0);
173 }
174 
176  .p.name = "vp9_nvdec",
177  .p.type = AVMEDIA_TYPE_VIDEO,
178  .p.id = AV_CODEC_ID_VP9,
179  .p.pix_fmt = AV_PIX_FMT_CUDA,
180  .start_frame = nvdec_vp9_start_frame,
181  .end_frame = ff_nvdec_simple_end_frame,
182  .decode_slice = ff_nvdec_simple_decode_slice,
183  .frame_params = nvdec_vp9_frame_params,
184  .init = ff_nvdec_decode_init,
185  .uninit = ff_nvdec_decode_uninit,
186  .priv_data_size = sizeof(NVDECContext),
187 };
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:699
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3341
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_nvdec_get_ref_idx
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:775
FFHWAccel::p
AVHWAccel p
The public AVHWAccel.
Definition: hwaccel_internal.h:38
FrameDecodeData
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
pixdesc.h
AVFrame::width
int width
Definition: frame.h:482
internal.h
AVComponentDescriptor::depth
int depth
Number of bits in the component.
Definition: pixdesc.h:57
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:674
AVCOL_SPC_BT470BG
@ 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:679
AVCOL_SPC_RESERVED
@ AVCOL_SPC_RESERVED
reserved for future use by ITU-T and ISO/IEC just like 15-255 are
Definition: pixfmt.h:677
FFHWAccel
Definition: hwaccel_internal.h:34
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:575
vp9shared.h
nvdec_vp9_start_frame
static int nvdec_vp9_start_frame(AVCodecContext *avctx, const AVBufferRef *buffer_ref, const uint8_t *buffer, uint32_t size)
Definition: nvdec_vp9.c:32
VP9SharedContext
Definition: vp9shared.h:164
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:680
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
NVDECFrame
Definition: nvdec.h:49
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
ff_nvdec_simple_end_frame
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:677
hwaccel_internal.h
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:327
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
nvdec_vp9_frame_params
static int nvdec_vp9_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_vp9.c:168
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:130
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
NVDECFrame::idx
unsigned int idx
Definition: nvdec.h:50
size
int size
Definition: twinvq_data.h:10344
nvdec.h
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:259
AVFrame::private_ref
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:814
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2124
AVCOL_SPC_SMPTE240M
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition: pixfmt.h:681
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:684
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:676
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVFrame::height
int height
Definition: frame.h:482
ff_nvdec_simple_decode_slice
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:687
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_nvdec_frame_params
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:709
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
CUR_FRAME
#define CUR_FRAME
Definition: vp9shared.h:168
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:51
h
h
Definition: vp9dsp_template.c:2070
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:678
NVDECContext
Definition: nvdec.h:57
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:675
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
ff_vp9_nvdec_hwaccel
const FFHWAccel ff_vp9_nvdec_hwaccel
Definition: nvdec_vp9.c:175