2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "libavutil/attributes.h"
33 #include "mpegutils.h"
37 typedef struct H264ParseContext {
44 static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
47 H264Context *h = &p->h;
50 ParseContext *pc = &p->pc;
51 // mb_addr= pc->mb_addr - 1;
56 for (i = 0; i < buf_size; i++) {
58 i += h->h264dsp.startcode_find_candidate(buf + i, buf_size - i);
61 } else if (state <= 2) {
63 state ^= 5; // 2->7, 1->4, 0->5
67 state >>= 1; // 2->1, 1->0, 0->0
68 } else if (state <= 5) {
69 int nalu_type = buf[i] & 0x1F;
70 if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
71 nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
72 if (pc->frame_start_found) {
76 } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
77 nalu_type == NAL_IDR_SLICE) {
78 if (pc->frame_start_found) {
82 pc->frame_start_found = 1;
86 // first_mb_in_slice is 0, probably the first nal of a new slice
97 pc->frame_start_found = 0;
98 return i - (state & 5);
101 static int scan_mmco_reset(AVCodecParserContext *s)
103 H264ParseContext *p = s->priv_data;
104 H264Context *h = &p->h;
106 h->slice_type_nos = s->pict_type & 3;
108 if (h->pps.redundant_pic_cnt_present)
109 get_ue_golomb(&h->gb); // redundant_pic_count
111 if (ff_set_ref_count(h) < 0)
112 return AVERROR_INVALIDDATA;
114 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
116 for (list = 0; list < h->list_count; list++) {
117 if (get_bits1(&h->gb)) {
119 for (index = 0; ; index++) {
120 unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
122 if (reordering_of_pic_nums_idc < 3)
123 get_ue_golomb(&h->gb);
124 else if (reordering_of_pic_nums_idc > 3) {
125 av_log(h->avctx, AV_LOG_ERROR,
126 "illegal reordering_of_pic_nums_idc %d\n",
127 reordering_of_pic_nums_idc);
128 return AVERROR_INVALIDDATA;
132 if (index >= h->ref_count[list]) {
133 av_log(h->avctx, AV_LOG_ERROR,
134 "reference count %d overflow\n", index);
135 return AVERROR_INVALIDDATA;
142 if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
143 (h->pps.weighted_bipred_idc == 1 && h->slice_type_nos == AV_PICTURE_TYPE_B))
144 ff_pred_weight_table(h);
146 if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
148 for (i = 0; i < MAX_MMCO_COUNT; i++) {
149 MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
150 if (opcode > (unsigned) MMCO_LONG) {
151 av_log(h->avctx, AV_LOG_ERROR,
152 "illegal memory management control operation %d\n",
154 return AVERROR_INVALIDDATA;
156 if (opcode == MMCO_END)
158 else if (opcode == MMCO_RESET)
161 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
162 get_ue_golomb(&h->gb);
163 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
164 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
165 get_ue_golomb_31(&h->gb);
173 * Parse NAL units of found picture and decode some basic information.
175 * @param s parser context.
176 * @param avctx codec context.
177 * @param buf buffer with field/frame data.
178 * @param buf_size size of the buffer.
180 static inline int parse_nal_units(AVCodecParserContext *s,
181 AVCodecContext *avctx,
182 const uint8_t *buf, int buf_size)
184 H264ParseContext *p = s->priv_data;
185 H264Context *h = &p->h;
186 const uint8_t *buf_end = buf + buf_size;
188 unsigned int slice_type;
189 int state = -1, got_reset = 0;
193 /* set some sane default values */
194 s->pict_type = AV_PICTURE_TYPE_I;
196 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
199 ff_h264_reset_sei(h);
205 int src_length, dst_length, consumed;
206 buf = avpriv_find_start_code(buf, buf_end, &state);
210 src_length = buf_end - buf;
211 switch (state & 0x1f) {
214 // Do not walk the whole buffer just to decode slice header
215 if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
216 /* IDR or disposable slice
217 * No need to decode many bytes because MMCOs shall not be present. */
221 /* To decode up to MMCOs */
222 if (src_length > 1000)
227 ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
228 if (!ptr || dst_length < 0)
231 init_get_bits(&h->gb, ptr, 8 * dst_length);
232 switch (h->nal_unit_type) {
234 ff_h264_decode_seq_parameter_set(h);
237 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
240 ff_h264_decode_sei(h);
245 h->prev_frame_num = 0;
246 h->prev_frame_num_offset = 0;
251 get_ue_golomb(&h->gb); // skip first_mb_in_slice
252 slice_type = get_ue_golomb_31(&h->gb);
253 s->pict_type = golomb_to_pict_type[slice_type % 5];
254 if (h->sei_recovery_frame_cnt >= 0) {
255 /* key frame, since recovery_frame_cnt is set */
258 pps_id = get_ue_golomb(&h->gb);
259 if (pps_id >= MAX_PPS_COUNT) {
260 av_log(h->avctx, AV_LOG_ERROR,
261 "pps_id %u out of range\n", pps_id);
264 if (!h->pps_buffers[pps_id]) {
265 av_log(h->avctx, AV_LOG_ERROR,
266 "non-existing PPS %u referenced\n", pps_id);
269 h->pps = *h->pps_buffers[pps_id];
270 if (!h->sps_buffers[h->pps.sps_id]) {
271 av_log(h->avctx, AV_LOG_ERROR,
272 "non-existing SPS %u referenced\n", h->pps.sps_id);
275 h->sps = *h->sps_buffers[h->pps.sps_id];
276 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
278 avctx->profile = ff_h264_get_profile(&h->sps);
279 avctx->level = h->sps.level_idc;
281 if (h->sps.frame_mbs_only_flag) {
282 h->picture_structure = PICT_FRAME;
284 if (get_bits1(&h->gb)) { // field_pic_flag
285 h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
287 h->picture_structure = PICT_FRAME;
291 if (h->nal_unit_type == NAL_IDR_SLICE)
292 get_ue_golomb(&h->gb); /* idr_pic_id */
293 if (h->sps.poc_type == 0) {
294 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
296 if (h->pps.pic_order_present == 1 &&
297 h->picture_structure == PICT_FRAME)
298 h->delta_poc_bottom = get_se_golomb(&h->gb);
301 if (h->sps.poc_type == 1 &&
302 !h->sps.delta_pic_order_always_zero_flag) {
303 h->delta_poc[0] = get_se_golomb(&h->gb);
305 if (h->pps.pic_order_present == 1 &&
306 h->picture_structure == PICT_FRAME)
307 h->delta_poc[1] = get_se_golomb(&h->gb);
310 /* Decode POC of this picture.
311 * The prev_ values needed for decoding POC of the next picture are not set here. */
312 field_poc[0] = field_poc[1] = INT_MAX;
313 ff_init_poc(h, field_poc, &s->output_picture_number);
315 /* Continue parsing to check if MMCO_RESET is present.
316 * FIXME: MMCO_RESET could appear in non-first slice.
317 * Maybe, we should parse all undisposable non-IDR slice of this
318 * picture until encountering MMCO_RESET in a slice of it. */
319 if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
320 got_reset = scan_mmco_reset(s);
325 /* Set up the prev_ values for decoding POC of the next picture. */
326 h->prev_frame_num = got_reset ? 0 : h->frame_num;
327 h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
328 if (h->nal_ref_idc != 0) {
330 h->prev_poc_msb = h->poc_msb;
331 h->prev_poc_lsb = h->poc_lsb;
335 h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
339 if (h->sps.pic_struct_present_flag) {
340 switch (h->sei_pic_struct) {
341 case SEI_PIC_STRUCT_TOP_FIELD:
342 case SEI_PIC_STRUCT_BOTTOM_FIELD:
345 case SEI_PIC_STRUCT_FRAME:
346 case SEI_PIC_STRUCT_TOP_BOTTOM:
347 case SEI_PIC_STRUCT_BOTTOM_TOP:
350 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
351 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
354 case SEI_PIC_STRUCT_FRAME_DOUBLING:
357 case SEI_PIC_STRUCT_FRAME_TRIPLING:
361 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
365 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
368 if (h->picture_structure == PICT_FRAME) {
369 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
370 if (h->sps.pic_struct_present_flag) {
371 switch (h->sei_pic_struct) {
372 case SEI_PIC_STRUCT_TOP_BOTTOM:
373 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
374 s->field_order = AV_FIELD_TT;
376 case SEI_PIC_STRUCT_BOTTOM_TOP:
377 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
378 s->field_order = AV_FIELD_BB;
381 s->field_order = AV_FIELD_PROGRESSIVE;
385 if (field_poc[0] < field_poc[1])
386 s->field_order = AV_FIELD_TT;
387 else if (field_poc[0] > field_poc[1])
388 s->field_order = AV_FIELD_BB;
390 s->field_order = AV_FIELD_PROGRESSIVE;
393 if (h->picture_structure == PICT_TOP_FIELD)
394 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
396 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
397 s->field_order = AV_FIELD_UNKNOWN;
400 return 0; /* no need to evaluate the rest */
404 /* didn't find a picture! */
405 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
409 static int h264_parse(AVCodecParserContext *s,
410 AVCodecContext *avctx,
411 const uint8_t **poutbuf, int *poutbuf_size,
412 const uint8_t *buf, int buf_size)
414 H264ParseContext *p = s->priv_data;
415 H264Context *h = &p->h;
416 ParseContext *pc = &p->pc;
421 if (avctx->extradata_size) {
423 // must be done like in the decoder.
424 // otherwise opening the parser, creating extradata,
425 // and then closing and opening again
426 // will cause has_b_frames to be always set.
427 // NB: estimate_timings_from_pts behaves exactly like this.
428 if (!avctx->has_b_frames)
430 ff_h264_decode_extradata(h);
434 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
437 next = h264_find_frame_end(p, buf, buf_size);
439 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
445 if (next < 0 && next != END_NOT_FOUND) {
446 assert(pc->last_index + next >= 0);
447 h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
451 parse_nal_units(s, avctx, buf, buf_size);
453 if (h->sei_cpb_removal_delay >= 0) {
454 s->dts_sync_point = h->sei_buffering_period_present;
455 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
456 s->pts_dts_delta = h->sei_dpb_output_delay;
458 s->dts_sync_point = INT_MIN;
459 s->dts_ref_dts_delta = INT_MIN;
460 s->pts_dts_delta = INT_MIN;
463 if (s->flags & PARSER_FLAG_ONCE) {
464 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
468 *poutbuf_size = buf_size;
472 static int h264_split(AVCodecContext *avctx,
473 const uint8_t *buf, int buf_size)
479 for (i = 0; i <= buf_size; i++) {
480 if ((state & 0xFFFFFF1F) == 0x107)
482 /* if((state&0xFFFFFF1F) == 0x101 ||
483 * (state&0xFFFFFF1F) == 0x102 ||
484 * (state&0xFFFFFF1F) == 0x105) {
487 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
488 (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
489 (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
490 (state & 0xFFFFFF1F) != 0x10f) {
492 while (i > 4 && buf[i - 5] == 0)
498 state = (state << 8) | buf[i];
503 static void close(AVCodecParserContext *s)
505 H264ParseContext *p = s->priv_data;
506 H264Context *h = &p->h;
507 ParseContext *pc = &p->pc;
510 ff_h264_free_context(h);
513 static av_cold int init(AVCodecParserContext *s)
515 H264ParseContext *p = s->priv_data;
516 H264Context *h = &p->h;
517 h->thread_context[0] = h;
518 h->slice_context_count = 1;
519 ff_h264dsp_init(&h->h264dsp, 8, 1);
523 AVCodecParser ff_h264_parser = {
524 .codec_ids = { AV_CODEC_ID_H264 },
525 .priv_data_size = sizeof(H264ParseContext),
527 .parser_parse = h264_parse,
528 .parser_close = close,