FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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 "avcodec.h"
27 #include "nvdec.h"
28 #include "decode.h"
29 #include "internal.h"
30 #include "h264dec.h"
31 
32 static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src,
33  int frame_idx)
34 {
36  const NVDECFrame *cf = fdd->hwaccel_priv;
37 
38  dst->PicIdx = cf ? cf->idx : -1;
39  dst->FrameIdx = frame_idx;
40  dst->is_long_term = src->long_ref;
41  dst->not_existing = 0;
42  dst->used_for_reference = src->reference & 3;
43  dst->FieldOrderCnt[0] = src->field_poc[0];
44  dst->FieldOrderCnt[1] = src->field_poc[1];
45 }
46 
48  const uint8_t *buffer, uint32_t size)
49 {
50  const H264Context *h = avctx->priv_data;
51  const PPS *pps = h->ps.pps;
52  const SPS *sps = h->ps.sps;
53 
55  CUVIDPICPARAMS *pp = &ctx->pic_params;
57  FrameDecodeData *fdd;
58  NVDECFrame *cf;
59 
60  int i, dpb_size, ret;
61 
62  ret = ff_nvdec_start_frame(avctx, h->cur_pic_ptr->f);
63  if (ret < 0)
64  return ret;
65 
67  cf = (NVDECFrame*)fdd->hwaccel_priv;
68 
69  *pp = (CUVIDPICPARAMS) {
70  .PicWidthInMbs = h->mb_width,
71  .FrameHeightInMbs = h->mb_height,
72  .CurrPicIdx = cf->idx,
73  .field_pic_flag = FIELD_PICTURE(h),
74  .bottom_field_flag = h->picture_structure == PICT_BOTTOM_FIELD,
75  .second_field = FIELD_PICTURE(h) && !h->first_field,
76  .ref_pic_flag = h->nal_ref_idc != 0,
77  .intra_pic_flag = 0,
78 
79  .CodecSpecific.h264 = {
80  .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
81  .pic_order_cnt_type = sps->poc_type,
82  .log2_max_pic_order_cnt_lsb_minus4 = FFMAX(sps->log2_max_poc_lsb - 4, 0),
83  .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
84  .frame_mbs_only_flag = sps->frame_mbs_only_flag,
85  .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
86  .num_ref_frames = sps->ref_frame_count,
87  .residual_colour_transform_flag = sps->residual_color_transform_flag,
88  .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
89  .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
90  .qpprime_y_zero_transform_bypass_flag = sps->transform_bypass,
91 
92  .entropy_coding_mode_flag = pps->cabac,
93  .pic_order_present_flag = pps->pic_order_present,
94  .num_ref_idx_l0_active_minus1 = pps->ref_count[0] - 1,
95  .num_ref_idx_l1_active_minus1 = pps->ref_count[1] - 1,
96  .weighted_pred_flag = pps->weighted_pred,
97  .weighted_bipred_idc = pps->weighted_bipred_idc,
98  .pic_init_qp_minus26 = pps->init_qp - 26,
99  .deblocking_filter_control_present_flag = pps->deblocking_filter_parameters_present,
100  .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present,
101  .transform_8x8_mode_flag = pps->transform_8x8_mode,
102  .MbaffFrameFlag = sps->mb_aff && !FIELD_PICTURE(h),
103  .constrained_intra_pred_flag = pps->constrained_intra_pred,
104  .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
105  .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
106  .ref_pic_flag = h->nal_ref_idc != 0,
107  .frame_num = h->poc.frame_num,
108  .CurrFieldOrderCnt[0] = h->cur_pic_ptr->field_poc[0],
109  .CurrFieldOrderCnt[1] = h->cur_pic_ptr->field_poc[1],
110  },
111  };
112 
113  memcpy(ppc->WeightScale4x4, pps->scaling_matrix4, sizeof(ppc->WeightScale4x4));
114  memcpy(ppc->WeightScale8x8[0], pps->scaling_matrix8[0], sizeof(ppc->WeightScale8x8[0]));
115  memcpy(ppc->WeightScale8x8[1], pps->scaling_matrix8[3], sizeof(ppc->WeightScale8x8[0]));
116 
117  dpb_size = 0;
118  for (i = 0; i < h->short_ref_count; i++)
119  dpb_add(h, &ppc->dpb[dpb_size++], h->short_ref[i], h->short_ref[i]->frame_num);
120  for (i = 0; i < 16; i++) {
121  if (h->long_ref[i])
122  dpb_add(h, &ppc->dpb[dpb_size++], h->long_ref[i], i);
123  }
124 
125  for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->dpb); i++)
126  ppc->dpb[i].PicIdx = -1;
127 
128  return 0;
129 }
130 
132  uint32_t size)
133 {
135  void *tmp;
136 
138  ctx->bitstream_len + size + 3);
139  if (!tmp)
140  return AVERROR(ENOMEM);
141  ctx->bitstream = tmp;
142 
144  (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
145  if (!tmp)
146  return AVERROR(ENOMEM);
147  ctx->slice_offsets = tmp;
148 
149  AV_WB24(ctx->bitstream + ctx->bitstream_len, 1);
150  memcpy(ctx->bitstream + ctx->bitstream_len + 3, buffer, size);
151  ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
152  ctx->bitstream_len += size + 3;
153  ctx->nb_slices++;
154 
155  return 0;
156 }
157 
159  AVBufferRef *hw_frames_ctx)
160 {
161  const H264Context *h = avctx->priv_data;
162  const SPS *sps = h->ps.sps;
163  return ff_nvdec_frame_params(avctx, hw_frames_ctx, sps->ref_frame_count + sps->num_reorder_frames);
164 }
165 
167  .name = "h264_nvdec",
168  .type = AVMEDIA_TYPE_VIDEO,
169  .id = AV_CODEC_ID_H264,
170  .pix_fmt = AV_PIX_FMT_CUDA,
171  .start_frame = nvdec_h264_start_frame,
172  .end_frame = ff_nvdec_end_frame,
173  .decode_slice = nvdec_h264_decode_slice,
174  .frame_params = nvdec_h264_frame_params,
175  .init = ff_nvdec_decode_init,
176  .uninit = ff_nvdec_decode_uninit,
177  .priv_data_size = sizeof(NVDECContext),
178 };
int PicWidthInMbs
IN: Coded frame size in macroblocks.
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:154
H264POCContext poc
Definition: h264dec.h:460
int first_field
Definition: h264dec.h:408
int weighted_bipred_idc
Definition: h264_ps.h:116
int chroma_qp_index_offset[2]
Definition: h264_ps.h:119
int is_long_term
0=short term reference, 1=long term reference
Sequence parameter set.
Definition: h264_ps.h:43
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264_ps.h:114
unsigned * slice_offsets
Definition: nvdec.h:52
static int nvdec_h264_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_h264.c:158
Picture parameter set.
Definition: h264_ps.h:108
int frame_mbs_only_flag
Definition: h264_ps.h:61
int mb_height
Definition: h264dec.h:435
int FrameIdx
frame_num(short-term) or LongTermFrameIdx(long-term)
H264Context.
Definition: h264dec.h:337
AVFrame * f
Definition: h264dec.h:129
int nb_slices
Definition: nvdec.h:53
H264Picture * long_ref[32]
Definition: h264dec.h:464
#define src
Definition: vp8dsp.c:254
static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src, int frame_idx)
Definition: nvdec_h264.c:32
int picture_structure
Definition: h264dec.h:407
CUVIDH264PICPARAMS h264
uint8_t scaling_matrix4[6][16]
Definition: h264_ps.h:124
uint8_t * bitstream
Definition: nvdec.h:48
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264_ps.h:120
const PPS * pps
Definition: h264_ps.h:144
uint8_t
unsigned char WeightScale4x4[6][16]
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: h264_ps.h:98
int PicIdx
picture index of reference frame
CUVIDPICPARAMS pic_params
Definition: nvdec.h:42
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:578
int cabac
entropy_coding_mode_flag
Definition: h264_ps.h:110
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: h264_ps.h:48
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264_ps.h:122
ptrdiff_t size
Definition: opengl_enc.c:101
H.264 DPB entry This structure is used in CUVIDH264PICPARAMS structure.
union CUVIDPICPARAMS::@31 CodecSpecific
int not_existing
non-existing reference frame (corresponding PicIdx should be set to -1)
Picture parameters for decoding This structure is used in cuvidDecodePicture API IN for cuvidDecodePi...
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264_ps.h:62
int poc_type
pic_order_cnt_type
Definition: h264_ps.h:50
int constrained_intra_pred
constrained_intra_pred_flag
Definition: h264_ps.h:121
int num_reorder_frames
Definition: h264_ps.h:85
#define AVERROR(e)
Definition: error.h:43
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:459
#define FIELD_PICTURE(h)
Definition: h264dec.h:74
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:417
int weighted_pred
weighted_pred_flag
Definition: h264_ps.h:115
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:149
int residual_color_transform_flag
residual_colour_transform_flag
Definition: h264_ps.h:99
#define FFMAX(a, b)
Definition: common.h:94
unsigned int idx
Definition: nvdec.h:36
int delta_pic_order_always_zero_flag
Definition: h264_ps.h:52
uint8_t scaling_matrix8[6][64]
Definition: h264_ps.h:125
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:263
static int nvdec_h264_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_h264.c:47
int ref_frame_count
num_ref_frames
Definition: h264_ps.h:56
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size)
Definition: nvdec.c:523
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3536
int reference
Definition: h264dec.h:160
int bitstream_len
Definition: nvdec.h:49
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
int init_qp
pic_init_qp_minus26 + 26
Definition: h264_ps.h:117
H.264 / AVC / MPEG-4 part10 codec.
int direct_8x8_inference_flag
Definition: h264_ps.h:63
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:464
HW acceleration through CUDA.
Definition: pixfmt.h:231
#define FF_ARRAY_ELEMS(a)
int pic_order_present
pic_order_present_flag
Definition: h264_ps.h:111
Libavcodec external API header.
unsigned int bitstream_allocated
Definition: nvdec.h:50
CUVIDH264DPBENTRY dpb[16]
H264Picture * short_ref[32]
Definition: h264dec.h:463
int field_poc[2]
top/bottom POC
Definition: h264dec.h:147
main external API structure.
Definition: avcodec.h:1502
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:245
uint8_t * data
The data buffer.
Definition: buffer.h:89
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
unsigned int slice_offsets_allocated
Definition: nvdec.h:54
H264Picture * cur_pic_ptr
Definition: h264dec.h:346
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
const SPS * sps
Definition: h264_ps.h:145
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264_ps.h:51
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264_ps.h:123
const AVHWAccel ff_h264_nvdec_hwaccel
Definition: nvdec_h264.c:166
int used_for_reference
0=unused, 1=top_field, 2=bottom_field, 3=both_fields
A reference to a data buffer.
Definition: buffer.h:81
static int nvdec_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_h264.c:131
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 log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264_ps.h:49
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:188
H264ParamSets ps
Definition: h264dec.h:456
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264_ps.h:97
void * priv_data
Definition: avcodec.h:1529
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1537
int mb_width
Definition: h264dec.h:435
unsigned char WeightScale8x8[2][64]
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:52
H.264 picture parameters This structure is used in CUVIDPICPARAMS structure.
int FieldOrderCnt[2]
field order count of top and bottom fields
int nal_ref_idc
Definition: h264dec.h:442
GLuint buffer
Definition: opengl_enc.c:102
int short_ref_count
number of actual short term references
Definition: h264dec.h:479
static uint8_t tmp[11]
Definition: aes_ctr.c:26