FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nvdec_h264.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through NVDEC
3  *
4  * Copyright (c) 2016 Anton Khirnov
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 <stdint.h>
24 #include <string.h>
25 
26 #include "libavutil/mem.h"
27 #include "avcodec.h"
28 #include "nvdec.h"
29 #include "decode.h"
30 #include "internal.h"
31 #include "h264dec.h"
32 #include "hwaccel_internal.h"
33 
34 static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src,
35  int frame_idx)
36 {
37  FrameDecodeData *fdd = (FrameDecodeData*)src->f->private_ref->data;
38  const NVDECFrame *cf = fdd->hwaccel_priv;
39 
40  dst->PicIdx = cf ? cf->idx : -1;
41  dst->FrameIdx = frame_idx;
42  dst->is_long_term = src->long_ref;
43  dst->not_existing = 0;
44  dst->used_for_reference = src->reference & 3;
45  dst->FieldOrderCnt[0] = src->field_poc[0];
46  dst->FieldOrderCnt[1] = src->field_poc[1];
47 }
48 
50  const AVBufferRef *buffer_ref,
51  const uint8_t *buffer, uint32_t size)
52 {
53  const H264Context *h = avctx->priv_data;
54  const PPS *pps = h->ps.pps;
55  const SPS *sps = h->ps.sps;
56 
58  CUVIDPICPARAMS *pp = &ctx->pic_params;
59  CUVIDH264PICPARAMS *ppc = &pp->CodecSpecific.h264;
60  FrameDecodeData *fdd;
61  NVDECFrame *cf;
62 
63  int i, dpb_size, ret;
64 
65  ret = ff_nvdec_start_frame(avctx, h->cur_pic_ptr->f);
66  if (ret < 0)
67  return ret;
68 
69  fdd = (FrameDecodeData*)h->cur_pic_ptr->f->private_ref->data;
70  cf = (NVDECFrame*)fdd->hwaccel_priv;
71 
72  *pp = (CUVIDPICPARAMS) {
73  .PicWidthInMbs = h->mb_width,
74  .FrameHeightInMbs = h->mb_height,
75  .CurrPicIdx = cf->idx,
76  .field_pic_flag = FIELD_PICTURE(h),
77  .bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD,
78  .second_field = FIELD_PICTURE(h) && !h->first_field,
79  .ref_pic_flag = h->nal_ref_idc != 0,
80  .intra_pic_flag = 1,
81 
82  .CodecSpecific.h264 = {
83  .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
84  .pic_order_cnt_type = sps->poc_type,
85  .log2_max_pic_order_cnt_lsb_minus4 = FFMAX(sps->log2_max_poc_lsb - 4, 0),
86  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
87  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
88  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
89  .num_ref_frames = sps->ref_frame_count,
90  .residual_colour_transform_flag = sps->residual_color_transform_flag,
91  .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
92  .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
93  .qpprime_y_zero_transform_bypass_flag = sps->transform_bypass,
94 
95  .entropy_coding_mode_flag = pps->cabac,
96  .pic_order_present_flag = pps->pic_order_present,
97  .num_ref_idx_l0_active_minus1 = pps->ref_count[0] - 1,
98  .num_ref_idx_l1_active_minus1 = pps->ref_count[1] - 1,
99  .weighted_pred_flag = pps->weighted_pred,
100  .weighted_bipred_idc = pps->weighted_bipred_idc,
101  .pic_init_qp_minus26 = pps->init_qp - 26 - 6 * (sps->bit_depth_luma - 8),
102  .deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present,
103  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present,
104  .transform_8x8_mode_flag = pps->transform_8x8_mode,
105  .MbaffFrameFlag = sps->mb_aff && !FIELD_PICTURE(h),
106  .constrained_intra_pred_flag = pps->constrained_intra_pred,
107  .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
108  .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
109  .ref_pic_flag = h->nal_ref_idc != 0,
110  .frame_num = h->poc.frame_num,
111  .CurrFieldOrderCnt[0] = h->cur_pic_ptr->field_poc[0],
112  .CurrFieldOrderCnt[1] = h->cur_pic_ptr->field_poc[1],
113  },
114  };
115 
116  memcpy(ppc->WeightScale4x4, pps->scaling_matrix4, sizeof(ppc->WeightScale4x4));
117  memcpy(ppc->WeightScale8x8[0], pps->scaling_matrix8[0], sizeof(ppc->WeightScale8x8[0]));
118  memcpy(ppc->WeightScale8x8[1], pps->scaling_matrix8[3], sizeof(ppc->WeightScale8x8[0]));
119 
120  dpb_size = 0;
121  for (i = 0; i < h->short_ref_count; i++)
122  dpb_add(h, &ppc->dpb[dpb_size++], h->short_ref[i], h->short_ref[i]->frame_num);
123  for (i = 0; i < 16; i++) {
124  if (h->long_ref[i])
125  dpb_add(h, &ppc->dpb[dpb_size++], h->long_ref[i], i);
126  }
127 
128  for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->dpb); i++)
129  ppc->dpb[i].PicIdx = -1;
130 
131  return 0;
132 }
133 
134 static int nvdec_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
135  uint32_t size)
136 {
138  CUVIDPICPARAMS *pp = &ctx->pic_params;
139  const H264Context *h = avctx->priv_data;
140  const H264SliceContext *sl = &h->slice_ctx[0];
141  void *tmp;
142 
143  tmp = av_fast_realloc(ctx->bitstream_internal, &ctx->bitstream_allocated,
144  ctx->bitstream_len + size + 3);
145  if (!tmp)
146  return AVERROR(ENOMEM);
147  ctx->bitstream = ctx->bitstream_internal = tmp;
148 
149  tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
150  (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
151  if (!tmp)
152  return AVERROR(ENOMEM);
153  ctx->slice_offsets = tmp;
154 
155  AV_WB24(ctx->bitstream_internal + ctx->bitstream_len, 1);
156  memcpy(ctx->bitstream_internal + ctx->bitstream_len + 3, buffer, size);
157  ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
158  ctx->bitstream_len += size + 3;
159  ctx->nb_slices++;
160 
162  pp->intra_pic_flag = 0;
163 
164  return 0;
165 }
166 
168  AVBufferRef *hw_frames_ctx)
169 {
170  const H264Context *h = avctx->priv_data;
171  const SPS *sps = h->ps.sps;
172  return ff_nvdec_frame_params(avctx, hw_frames_ctx, sps->ref_frame_count + sps->num_reorder_frames, 0);
173 }
174 
176  .p.name = "h264_nvdec",
177  .p.type = AVMEDIA_TYPE_VIDEO,
178  .p.id = AV_CODEC_ID_H264,
179  .p.pix_fmt = AV_PIX_FMT_CUDA,
180  .start_frame = nvdec_h264_start_frame,
181  .end_frame = ff_nvdec_end_frame,
182  .decode_slice = nvdec_h264_decode_slice,
183  .frame_params = nvdec_h264_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
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
nvdec_h264_start_frame
static int nvdec_h264_start_frame(AVCodecContext *avctx, const AVBufferRef *buffer_ref, const uint8_t *buffer, uint32_t size)
Definition: nvdec_h264.c:49
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
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
internal.h
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:32
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
H264SliceContext
Definition: h264dec.h:180
FFHWAccel
Definition: hwaccel_internal.h:34
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:575
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
FIELD_PICTURE
#define FIELD_PICTURE(h)
Definition: h264dec.h:67
NVDECFrame
Definition: nvdec.h:49
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
H264SliceContext::slice_type
int slice_type
Definition: h264dec.h:186
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
dpb_size
int dpb_size
Definition: h264_levels.c:111
hwaccel_internal.h
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:327
SPS
Sequence parameter set.
Definition: h264_ps.h:44
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
AV_PICTURE_TYPE_SI
@ AV_PICTURE_TYPE_SI
Switching Intra.
Definition: avutil.h:283
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
PPS
Picture parameter set.
Definition: h264_ps.h:110
dpb_add
static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src, int frame_idx)
Definition: nvdec_h264.c:34
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:130
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
NVDECFrame::idx
unsigned int idx
Definition: nvdec.h:50
size
int size
Definition: twinvq_data.h:10344
AV_WB24
#define AV_WB24(p, d)
Definition: intreadwrite.h:446
nvdec.h
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:259
nvdec_h264_frame_params
static int nvdec_h264_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_h264.c:167
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2124
h264dec.h
H264Context
H264Context.
Definition: h264dec.h:340
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_nvdec_end_frame
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:647
avcodec.h
nvdec_h264_decode_slice
static int nvdec_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_h264.c:134
ret
ret
Definition: filter_design.txt:187
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
AVCodecContext
main external API structure.
Definition: avcodec.h:451
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
H264Picture
Definition: h264dec.h:114
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
ff_h264_nvdec_hwaccel
const FFHWAccel ff_h264_nvdec_hwaccel
Definition: nvdec_h264.c:175
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:51
h
h
Definition: vp9dsp_template.c:2070
NVDECContext
Definition: nvdec.h:57
src
#define src
Definition: vp8dsp.c:248