FFmpeg
hw_base_encode_h264.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "hw_base_encode_h264.h"
20 
21 #include "h2645data.h"
22 #include "h264_levels.h"
23 
24 #include "libavutil/pixdesc.h"
25 
27  AVCodecContext *avctx,
28  FFHWBaseEncodeH264 *common,
30 {
31  H264RawSPS *sps = &common->raw_sps;
32  H264RawPPS *pps = &common->raw_pps;
33  const AVPixFmtDescriptor *desc;
34  int bit_depth;
35 
36  memset(sps, 0, sizeof(*sps));
37  memset(pps, 0, sizeof(*pps));
38 
41  if (desc->nb_components == 1 || desc->log2_chroma_w != 1 || desc->log2_chroma_h != 1) {
42  av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
43  "%s is not supported.\n", desc->name);
44  return AVERROR(EINVAL);
45  }
46  bit_depth = desc->comp[0].depth;
47 
48  sps->nal_unit_header.nal_ref_idc = 3;
49  sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
50 
51  sps->profile_idc = avctx->profile & 0xff;
52 
54  avctx->profile == AV_PROFILE_H264_MAIN)
55  sps->constraint_set1_flag = 1;
56 
58  sps->constraint_set3_flag = base_ctx->gop_size == 1;
59 
60  if (avctx->profile == AV_PROFILE_H264_MAIN ||
62  sps->constraint_set4_flag = 1;
63  sps->constraint_set5_flag = base_ctx->b_per_p == 0;
64  }
65 
66  if (base_ctx->gop_size == 1)
67  common->dpb_frames = 0;
68  else
69  common->dpb_frames = 1 + base_ctx->max_b_depth;
70 
71  if (avctx->level != AV_LEVEL_UNKNOWN) {
72  sps->level_idc = avctx->level;
73  } else {
75  int framerate;
76 
77  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
78  framerate = avctx->framerate.num / avctx->framerate.den;
79  else
80  framerate = 0;
81 
82  level = ff_h264_guess_level(sps->profile_idc,
83  opts->bit_rate,
84  framerate,
85  opts->mb_width * 16,
86  opts->mb_height * 16,
87  common->dpb_frames);
88  if (level) {
89  av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
90  if (level->constraint_set3_flag)
91  sps->constraint_set3_flag = 1;
92  sps->level_idc = level->level_idc;
93  } else {
94  av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
95  "to any level: using level 6.2.\n");
96  sps->level_idc = 62;
97  }
98  }
99 
100  sps->seq_parameter_set_id = 0;
101  sps->chroma_format_idc = 1;
102  sps->bit_depth_luma_minus8 = bit_depth - 8;
103  sps->bit_depth_chroma_minus8 = bit_depth - 8;
104 
105  sps->log2_max_frame_num_minus4 = 4;
106  sps->pic_order_cnt_type = base_ctx->max_b_depth ? 0 : 2;
107  if (sps->pic_order_cnt_type == 0) {
108  sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
109  }
110 
111  sps->max_num_ref_frames = common->dpb_frames;
112 
113  sps->pic_width_in_mbs_minus1 = opts->mb_width - 1;
114  sps->pic_height_in_map_units_minus1 = opts->mb_height - 1;
115 
116  sps->frame_mbs_only_flag = 1;
117  sps->direct_8x8_inference_flag = 1;
118 
119  if (avctx->width != 16 * opts->mb_width ||
120  avctx->height != 16 * opts->mb_height) {
121  sps->frame_cropping_flag = 1;
122 
123  sps->frame_crop_left_offset = 0;
124  sps->frame_crop_right_offset =
125  (16 * opts->mb_width - avctx->width) / 2;
126  sps->frame_crop_top_offset = 0;
127  sps->frame_crop_bottom_offset =
128  (16 * opts->mb_height - avctx->height) / 2;
129  } else {
130  sps->frame_cropping_flag = 0;
131  }
132 
133  sps->vui_parameters_present_flag = 1;
134 
135  if (avctx->sample_aspect_ratio.num != 0 &&
136  avctx->sample_aspect_ratio.den != 0) {
137  int num, den, i;
138  av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
139  avctx->sample_aspect_ratio.den, 65535);
140  for (i = 0; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) {
141  if (num == ff_h2645_pixel_aspect[i].num &&
142  den == ff_h2645_pixel_aspect[i].den) {
143  sps->vui.aspect_ratio_idc = i;
144  break;
145  }
146  }
148  sps->vui.aspect_ratio_idc = 255;
149  sps->vui.sar_width = num;
150  sps->vui.sar_height = den;
151  }
152  sps->vui.aspect_ratio_info_present_flag = 1;
153  }
154 
155  // Unspecified video format, from table E-2.
156  sps->vui.video_format = 5;
157  sps->vui.video_full_range_flag =
158  avctx->color_range == AVCOL_RANGE_JPEG;
159  sps->vui.colour_primaries = avctx->color_primaries;
160  sps->vui.transfer_characteristics = avctx->color_trc;
161  sps->vui.matrix_coefficients = avctx->colorspace;
162  if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
163  avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
165  sps->vui.colour_description_present_flag = 1;
166  if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
167  sps->vui.colour_description_present_flag)
168  sps->vui.video_signal_type_present_flag = 1;
169 
171  sps->vui.chroma_loc_info_present_flag = 1;
172  sps->vui.chroma_sample_loc_type_top_field =
173  sps->vui.chroma_sample_loc_type_bottom_field =
174  avctx->chroma_sample_location - 1;
175  }
176 
177  sps->vui.timing_info_present_flag = 1;
178  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
179  sps->vui.num_units_in_tick = avctx->framerate.den;
180  sps->vui.time_scale = 2 * avctx->framerate.num;
181  sps->vui.fixed_frame_rate_flag = 1;
182  } else {
183  sps->vui.num_units_in_tick = avctx->time_base.num;
184  sps->vui.time_scale = 2 * avctx->time_base.den;
185  sps->vui.fixed_frame_rate_flag = 0;
186  }
187 
188  if (opts->flags & FF_HW_H264_SEI_TIMING) {
189  H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
191 
192  sps->vui.nal_hrd_parameters_present_flag = 1;
193 
194  hrd->cpb_cnt_minus1 = 0;
195 
196  // Try to scale these to a sensible range so that the
197  // golomb encode of the value is not overlong.
198  hrd->bit_rate_scale =
199  av_clip_uintp2(av_log2(opts->bit_rate) - 15 - 6, 4);
200  hrd->bit_rate_value_minus1[0] =
201  (opts->bit_rate >> hrd->bit_rate_scale + 6) - 1;
202 
203  hrd->cpb_size_scale =
204  av_clip_uintp2(av_log2(opts->hrd_buffer_size) - 15 - 4, 4);
205  hrd->cpb_size_value_minus1[0] =
206  (opts->hrd_buffer_size >> hrd->cpb_size_scale + 4) - 1;
207 
208  // CBR mode as defined for the HRD cannot be achieved without filler
209  // data, so this flag cannot be set even with VAAPI CBR modes.
210  hrd->cbr_flag[0] = 0;
211 
215  hrd->time_offset_length = 0;
216 
217  bp->seq_parameter_set_id = sps->seq_parameter_set_id;
218 
219  // This calculation can easily overflow 32 bits.
220  bp->nal.initial_cpb_removal_delay[0] = 90000 *
221  (uint64_t)opts->initial_buffer_fullness /
222  opts->hrd_buffer_size;
224  } else {
225  sps->vui.nal_hrd_parameters_present_flag = 0;
226  sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
227  }
228 
229  sps->vui.bitstream_restriction_flag = 1;
230  sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
231  sps->vui.log2_max_mv_length_horizontal = 15;
232  sps->vui.log2_max_mv_length_vertical = 15;
233  sps->vui.max_num_reorder_frames = base_ctx->max_b_depth;
234  sps->vui.max_dec_frame_buffering = base_ctx->max_b_depth + 1;
235 
236  pps->nal_unit_header.nal_ref_idc = 3;
237  pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
238 
239  pps->pic_parameter_set_id = 0;
240  pps->seq_parameter_set_id = 0;
241 
242  pps->entropy_coding_mode_flag =
243  !(sps->profile_idc == AV_PROFILE_H264_BASELINE ||
244  sps->profile_idc == AV_PROFILE_H264_EXTENDED ||
245  sps->profile_idc == AV_PROFILE_H264_CAVLC_444);
246  if (!opts->cabac && pps->entropy_coding_mode_flag)
247  pps->entropy_coding_mode_flag = 0;
248 
249  pps->num_ref_idx_l0_default_active_minus1 = 0;
250  pps->num_ref_idx_l1_default_active_minus1 = 0;
251 
252  pps->pic_init_qp_minus26 = opts->fixed_qp_idr - 26;
253 
254  if (sps->profile_idc == AV_PROFILE_H264_BASELINE ||
255  sps->profile_idc == AV_PROFILE_H264_EXTENDED ||
256  sps->profile_idc == AV_PROFILE_H264_MAIN) {
257  pps->more_rbsp_data = 0;
258  } else {
259  pps->more_rbsp_data = 1;
260 
261  pps->transform_8x8_mode_flag = 1;
262  }
263 
264  return 0;
265 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
level
uint8_t level
Definition: svq3.c:205
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
FFHWBaseEncodeH264Opts
Definition: hw_base_encode_h264.h:34
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:691
h264_levels.h
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:112
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:684
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:717
H264RawHRD::dpb_output_delay_length_minus1
uint8_t dpb_output_delay_length_minus1
Definition: cbs_h264.h:54
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:614
H264RawHRD
Definition: cbs_h264.h:43
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
ff_hw_base_encode_init_params_h264
int ff_hw_base_encode_init_params_h264(FFHWBaseEncodeContext *base_ctx, AVCodecContext *avctx, FFHWBaseEncodeH264 *common, FFHWBaseEncodeH264Opts *opts)
Definition: hw_base_encode_h264.c:26
H264LevelDescriptor
Definition: h264_levels.h:25
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:246
H264RawHRD::cpb_size_scale
uint8_t cpb_size_scale
Definition: cbs_h264.h:46
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
AV_PROFILE_H264_EXTENDED
#define AV_PROFILE_H264_EXTENDED
Definition: defs.h:113
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:566
H264RawHRD::bit_rate_scale
uint8_t bit_rate_scale
Definition: cbs_h264.h:45
ff_h264_guess_level
const H264LevelDescriptor * ff_h264_guess_level(int profile_idc, int64_t bitrate, int framerate, int width, int height, int max_dec_frame_buffering)
Guess the level of a stream from some parameters.
Definition: h264_levels.c:79
FFHWBaseEncodeH264
Definition: hw_base_encode_h264.h:25
H264RawHRD::cpb_size_value_minus1
uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:49
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
FFHWBaseEncodeH264::dpb_frames
int dpb_frames
Definition: hw_base_encode_h264.h:31
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:677
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
H264RawSEIBufferingPeriod::initial_cpb_removal_delay
uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:227
AV_PROFILE_H264_HIGH_10
#define AV_PROFILE_H264_HIGH_10
Definition: defs.h:115
FFHWBaseEncodeContext::max_b_depth
int max_b_depth
Definition: hw_base_encode.h:188
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
h2645data.h
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:589
FFHWBaseEncodeH264::raw_sps
H264RawSPS raw_sps
Definition: hw_base_encode_h264.h:26
FFHWBaseEncodeContext::b_per_p
int b_per_p
Definition: hw_base_encode.h:189
opts
AVDictionary * opts
Definition: movenc.c:51
framerate
float framerate
Definition: av1_levels.c:29
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:701
hw_base_encode_h264.h
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:198
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1794
H264RawSEIBufferingPeriod::nal
struct H264RawSEIBufferingPeriod::@69 nal
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:683
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:550
FFHWBaseEncodeH264::sei_buffering_period
H264RawSEIBufferingPeriod sei_buffering_period
Definition: hw_base_encode_h264.h:29
AV_PROFILE_H264_CAVLC_444
#define AV_PROFILE_H264_CAVLC_444
Definition: defs.h:124
FFHWBaseEncodeH264::raw_pps
H264RawPPS raw_pps
Definition: hw_base_encode_h264.h:27
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:737
H264RawSEIBufferingPeriod::initial_cpb_removal_delay_offset
uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:228
H264RawHRD::cpb_cnt_minus1
uint8_t cpb_cnt_minus1
Definition: cbs_h264.h:44
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
H264RawHRD::cbr_flag
uint8_t cbr_flag[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:50
ff_h2645_pixel_aspect
const AVRational ff_h2645_pixel_aspect[]
Definition: h2645data.c:21
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:708
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:643
AVCodecContext::height
int height
Definition: avcodec.h:624
FFHWBaseEncodeContext::gop_size
int gop_size
Definition: hw_base_encode.h:184
AV_PROFILE_H264_BASELINE
#define AV_PROFILE_H264_BASELINE
Definition: defs.h:110
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
H264RawSEIBufferingPeriod::seq_parameter_set_id
uint8_t seq_parameter_set_id
Definition: cbs_h264.h:225
H264RawHRD::bit_rate_value_minus1
uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT]
Definition: cbs_h264.h:48
H264RawHRD::cpb_removal_delay_length_minus1
uint8_t cpb_removal_delay_length_minus1
Definition: cbs_h264.h:53
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
FFHWBaseEncodeContext::input_frames
AVHWFramesContext * input_frames
Definition: hw_base_encode.h:153
AV_PROFILE_H264_CONSTRAINED_BASELINE
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition: defs.h:111
desc
const char * desc
Definition: libsvtav1.c:79
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FF_HW_H264_SEI_TIMING
#define FF_HW_H264_SEI_TIMING
Definition: hw_base_encode_h264.h:36
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
H264RawHRD::initial_cpb_removal_delay_length_minus1
uint8_t initial_cpb_removal_delay_length_minus1
Definition: cbs_h264.h:52
H264RawSEIBufferingPeriod
Definition: cbs_h264.h:224
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:648
H264RawHRD::time_offset_length
uint8_t time_offset_length
Definition: cbs_h264.h:55
H264RawSPS
Definition: cbs_h264.h:102
H264RawPPS
Definition: cbs_h264.h:171