28#define UNCHECKED_BITSTREAM_READER 1
73 int buf_index,
int next_avc)
79 return FFMIN(buf_index, buf_size);
83 int buf_size,
void *logctx)
89 int next_avc = p->is_avc ? 0 : buf_size;
95 if (p->is_avc && !p->nal_length_size)
98 for (
i = 0;
i < buf_size;
i++) {
102 for (j = 0; j < p->nal_length_size; j++)
103 nalsize = (nalsize << 8) | buf[
i++];
104 if (!nalsize || nalsize > buf_size -
i) {
106 "remaining %d\n", nalsize, buf_size -
i);
109 next_avc =
i + nalsize;
114 i += p->h264dsp.startcode_find_candidate(buf +
i, next_avc -
i);
117 }
else if (
state <= 2) {
124 }
else if (
state <= 5) {
125 int nalu_type = buf[
i] & 0x1F;
139 unsigned int mb, last_mb = p->parse_last_mb;
141 p->parse_history[p->parse_history_count++] = buf[
i];
143 init_get_bits(&gb, p->parse_history, 8*p->parse_history_count);
146 p->parse_last_mb =
mb;
149 i -= p->parse_history_count - 1;
150 p->parse_history_count = 0;
155 p->parse_history_count = 0;
177 int slice_type_nos =
s->pict_type & 3;
179 int list_count, ref_count[2];
182 if (p->ps.pps->redundant_pic_cnt_present)
189 slice_type_nos, p->picture_structure, logctx) < 0)
194 for (list = 0; list < list_count; list++) {
200 if (reordering_of_pic_nums_idc < 3)
202 else if (reordering_of_pic_nums_idc > 3) {
204 "illegal reordering_of_pic_nums_idc %d\n",
205 reordering_of_pic_nums_idc);
210 if (
index >= ref_count[list]) {
212 "reference count %d overflow\n",
index);
223 &pwt, p->picture_structure, logctx);
234 "illegal memory management control operation %d\n",
264 const uint8_t *
const buf,
int buf_size)
269 int buf_index, next_avc;
271 unsigned int slice_type;
272 int state = -1, got_reset = 0;
273 int q264 = buf_size >=4 && !memcmp(
"Q264", buf, 4);
283 p->sei.common.frame_packing.arrangement_cancel_flag = -1;
284 p->sei.common.unregistered.x264_build = -1;
294 next_avc = p->is_avc ? 0 : buf_size;
297 int src_length, consumed, nalsize = 0;
299 if (buf_index >= next_avc) {
300 nalsize =
get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx);
303 next_avc = buf_index + nalsize;
306 if (buf_index >= buf_size)
308 if (buf_index >= next_avc)
311 src_length = next_avc - buf_index;
313 state = buf[buf_index];
314 switch (
state & 0x1f) {
325 if (src_length > 1000)
334 buf_index += consumed;
357 p->poc.prev_frame_num = 0;
358 p->poc.prev_frame_num_offset = 0;
359 p->poc.prev_poc_msb =
360 p->poc.prev_poc_lsb = 0;
366 if (p->sei.recovery_point.recovery_frame_cnt >= 0) {
373 "pps_id %u out of range\n", pps_id);
376 if (!p->ps.pps_list[pps_id]) {
378 "non-existing PPS %u referenced\n", pps_id);
383 p->ps.sps = p->ps.pps->sps;
387 if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 &&
s->pict_type ==
AV_PICTURE_TYPE_I)
392 s->coded_width = 16 *
sps->mb_width;
393 s->coded_height = 16 *
sps->mb_height;
394 s->width =
s->coded_width - (
sps->crop_right +
sps->crop_left);
395 s->height =
s->coded_height - (
sps->crop_top +
sps->crop_bottom);
396 if (
s->width <= 0 ||
s->height <= 0) {
397 s->width =
s->coded_width;
398 s->height =
s->coded_height;
401 switch (
sps->bit_depth_luma) {
424 if (
sps->frame_mbs_only_flag) {
437 if (
sps->poc_type == 0) {
440 if (p->ps.pps->pic_order_present == 1 &&
445 if (
sps->poc_type == 1 &&
446 !
sps->delta_pic_order_always_zero_flag) {
449 if (p->ps.pps->pic_order_present == 1 &&
456 field_poc[0] = field_poc[1] = INT_MAX;
458 &p->poc, p->picture_structure,
nal.ref_idc);
473 p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
474 p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
475 if (
nal.ref_idc != 0) {
477 p->poc.prev_poc_msb = p->poc.poc_msb;
478 p->poc.prev_poc_lsb = p->poc.poc_lsb;
480 p->poc.prev_poc_msb = 0;
481 p->poc.prev_poc_lsb =
486 if (p->sei.picture_timing.present) {
491 p->sei.picture_timing.present = 0;
495 if (
sps->pic_struct_present_flag && p->sei.picture_timing.present) {
496 switch (p->sei.picture_timing.pic_struct) {
517 s->repeat_pict = p->picture_structure ==
PICT_FRAME ? 1 : 0;
521 s->repeat_pict = p->picture_structure ==
PICT_FRAME ? 1 : 0;
526 if (
sps->pic_struct_present_flag && p->sei.picture_timing.present) {
527 switch (p->sei.picture_timing.pic_struct) {
541 if (field_poc[0] < field_poc[1])
543 else if (field_poc[0] > field_poc[1])
545 else if (
sps->mb_aff) {
556 if (p->poc.frame_num == p->last_frame_num &&
559 p->last_picture_structure !=
s->picture_structure) {
567 p->last_picture_structure =
s->picture_structure;
568 p->last_frame_num = p->poc.frame_num;
570 if (
sps->timing_info_present_flag) {
572 if (p->sei.common.unregistered.x264_build < 44U)
575 sps->num_units_in_tick * 2, den, 1 << 30);
587 av_log(avctx,
AV_LOG_ERROR,
"missing picture in access unit with size %d\n", buf_size);
595 const uint8_t **poutbuf,
int *poutbuf_size,
596 const uint8_t *buf,
int buf_size)
607 &p->ps, &p->is_avc, &p->nal_length_size,
633 if (p->sei.picture_timing.cpb_removal_delay >= 0) {
634 s->dts_sync_point = p->sei.buffering_period.present;
635 s->dts_ref_dts_delta = p->sei.picture_timing.cpb_removal_delay;
636 s->pts_dts_delta = p->sei.picture_timing.dpb_output_delay;
638 s->dts_sync_point = INT_MIN;
639 s->dts_ref_dts_delta = INT_MIN;
640 s->pts_dts_delta = INT_MIN;
647 if (
s->dts_sync_point >= 0) {
661 uint64_t
pts = (uint64_t)
s->dts + pts_dts_delta;
666 if (
s->dts_sync_point > 0)
667 p->reference_dts =
s->dts;
672 *poutbuf_size = buf_size;
692 p->last_frame_num = INT_MAX;
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static av_cold void close(AVCodecParserContext *s)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Libavcodec external API header.
#define PARSER_FLAG_COMPLETE_FRAMES
Convenience header that includes libavutil's core.
#define i(width, name, range_min, range_max)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
@ AV_FIELD_TT
Top coded_first, top displayed first.
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
int(* init)(AVBSFContext *ctx)
static struct @346255127015250356166251341105367306144006377143 state
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
@ AV_PICTURE_STRUCTURE_BOTTOM_FIELD
coded as bottom field
@ AV_PICTURE_STRUCTURE_TOP_FIELD
coded as top field
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
#define AV_NOPTS_VALUE
Undefined timestamp value.
static int get_nalsize(int nal_length_size, const uint8_t *buf, int buf_size, int *buf_index, void *logctx)
H.264 common definitions.
int ff_h264_get_profile(const SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
int ff_h264_parse_ref_count(int *plist_count, int ref_count[2], GetBitContext *gb, const PPS *pps, int slice_type_nos, int picture_structure, void *logctx)
int ff_h264_init_poc(int pic_field_poc[2], int *pic_poc, const SPS *sps, H264POCContext *pc, int picture_structure, int nal_ref_idc)
int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps, const int *ref_count, int slice_type_nos, H264PredWeightTable *pwt, int picture_structure, void *logctx)
int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps, int *is_avc, int *nal_length_size, int err_recognition, void *logctx)
H.264 decoder/parser shared code.
MMCOOpcode
Memory management control operation opcode.
const FFCodecParser ff_h264_parser
static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf, int buf_size, void *logctx)
static int find_start_code(const uint8_t *buf, int buf_size, int buf_index, int next_avc)
static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *const buf, int buf_size)
Parse NAL units of found picture and decode some basic information.
static av_cold void h264_close(AVCodecParserContext *s)
static int h264_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb, void *logctx)
void ff_h264_ps_uninit(H264ParamSets *ps)
Uninit H264 param sets structure.
int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx, H264ParamSets *ps, int bit_length)
Decode PPS.
int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, H264ParamSets *ps, int ignore_truncation)
Decode SPS.
H.264 parameter set handling.
int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, const H264ParamSets *ps, void *logctx)
void ff_h264_sei_uninit(H264SEIContext *h)
Reset SEI values at the beginning of the frame.
int ff_h264_sei_process_picture_timing(H264SEIPictureTiming *h, const SPS *sps, void *logctx)
Parse the contents of a picture timing message given an active SPS.
@ H264_SEI_PIC_STRUCT_BOTTOM_FIELD
2: bottom field
@ H264_SEI_PIC_STRUCT_BOTTOM_TOP
4: bottom field, top field, in that order
@ H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP
5: top field, bottom field, top field repeated, in that order
@ H264_SEI_PIC_STRUCT_TOP_FIELD
1: top field
@ H264_SEI_PIC_STRUCT_FRAME_TRIPLING
8: frame tripling
@ H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM
6: bottom field, top field, bottom field repeated, in that order
@ H264_SEI_PIC_STRUCT_TOP_BOTTOM
3: top field, bottom field, in that order
@ H264_SEI_PIC_STRUCT_FRAME_DOUBLING
7: frame doubling
@ H264_SEI_PIC_STRUCT_FRAME
0: frame
const uint8_t ff_h264_golomb_to_pict_type[5]
int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
Extract the raw (unescaped) bitstream.
av_cold void ff_h264dsp_init(H264DSPContext *c, const int bit_depth, const int chroma_format_idc)
Macro definitions for various function/variable attributes.
Memory handling functions.
#define PICT_BOTTOM_FIELD
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
#define PARSER_CODEC_LIST(...)
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV420P9
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_PIX_FMT_YUV444P10
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
main external API structure.
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
int level
Encoding level descriptor.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Rational number (pair of numerator and denominator).
int rbsp_buffer_alloc_size
Context for storing H.264 DSP functions.
int last_picture_structure
uint32_t state
contains the last few bytes in MSB order