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>
31 #include "libavutil/avutil.h"
32 #include "libavutil/error.h"
33 #include "libavutil/log.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixfmt.h"
42 #include "mpegutils.h"
45 typedef struct H264ParseContext {
52 static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
55 H264Context *h = &p->h;
58 ParseContext *pc = &p->pc;
59 // mb_addr= pc->mb_addr - 1;
64 for (i = 0; i < buf_size; i++) {
66 i += h->h264dsp.startcode_find_candidate(buf + i, buf_size - i);
69 } else if (state <= 2) {
71 state ^= 5; // 2->7, 1->4, 0->5
75 state >>= 1; // 2->1, 1->0, 0->0
76 } else if (state <= 5) {
77 int nalu_type = buf[i] & 0x1F;
78 if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
79 nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
80 if (pc->frame_start_found) {
84 } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
85 nalu_type == NAL_IDR_SLICE) {
86 if (pc->frame_start_found) {
90 pc->frame_start_found = 1;
94 // first_mb_in_slice is 0, probably the first nal of a new slice
101 return END_NOT_FOUND;
105 pc->frame_start_found = 0;
106 return i - (state & 5);
109 static int scan_mmco_reset(AVCodecParserContext *s)
111 H264ParseContext *p = s->priv_data;
112 H264Context *h = &p->h;
113 H264SliceContext *sl = &h->slice_ctx[0];
115 sl->slice_type_nos = s->pict_type & 3;
117 if (h->pps.redundant_pic_cnt_present)
118 get_ue_golomb(&sl->gb); // redundant_pic_count
120 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
121 get_bits1(&sl->gb); // direct_spatial_mv_pred
123 if (ff_set_ref_count(h, sl) < 0)
124 return AVERROR_INVALIDDATA;
126 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
128 for (list = 0; list < sl->list_count; list++) {
129 if (get_bits1(&sl->gb)) {
131 for (index = 0; ; index++) {
132 unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
134 if (reordering_of_pic_nums_idc < 3)
135 get_ue_golomb(&sl->gb);
136 else if (reordering_of_pic_nums_idc > 3) {
137 av_log(h->avctx, AV_LOG_ERROR,
138 "illegal reordering_of_pic_nums_idc %d\n",
139 reordering_of_pic_nums_idc);
140 return AVERROR_INVALIDDATA;
144 if (index >= sl->ref_count[list]) {
145 av_log(h->avctx, AV_LOG_ERROR,
146 "reference count %d overflow\n", index);
147 return AVERROR_INVALIDDATA;
154 if ((h->pps.weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
155 (h->pps.weighted_bipred_idc == 1 && sl->slice_type_nos == AV_PICTURE_TYPE_B))
156 ff_h264_pred_weight_table(&sl->gb, &h->sps, sl->ref_count, sl->slice_type_nos,
159 if (get_bits1(&sl->gb)) { // adaptive_ref_pic_marking_mode_flag
161 for (i = 0; i < MAX_MMCO_COUNT; i++) {
162 MMCOOpcode opcode = get_ue_golomb_31(&sl->gb);
163 if (opcode > (unsigned) MMCO_LONG) {
164 av_log(h->avctx, AV_LOG_ERROR,
165 "illegal memory management control operation %d\n",
167 return AVERROR_INVALIDDATA;
169 if (opcode == MMCO_END)
171 else if (opcode == MMCO_RESET)
174 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
175 get_ue_golomb(&sl->gb);
176 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
177 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
178 get_ue_golomb_31(&sl->gb);
186 * Parse NAL units of found picture and decode some basic information.
188 * @param s parser context.
189 * @param avctx codec context.
190 * @param buf buffer with field/frame data.
191 * @param buf_size size of the buffer.
193 static inline int parse_nal_units(AVCodecParserContext *s,
194 AVCodecContext *avctx,
195 const uint8_t *buf, int buf_size)
197 H264ParseContext *p = s->priv_data;
198 H264Context *h = &p->h;
199 H264SliceContext *sl = &h->slice_ctx[0];
200 const uint8_t *buf_end = buf + buf_size;
202 H2645NAL nal = { NULL };
205 unsigned int slice_type;
206 int state = -1, got_reset = 0;
210 /* set some sane default values */
211 s->pict_type = AV_PICTURE_TYPE_I;
213 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
216 ff_h264_reset_sei(h);
222 int src_length, consumed;
223 buf = avpriv_find_start_code(buf, buf_end, &state);
227 src_length = buf_end - buf;
228 switch (state & 0x1f) {
231 // Do not walk the whole buffer just to decode slice header
232 if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
233 /* IDR or disposable slice
234 * No need to decode many bytes because MMCOs shall not be present. */
238 /* To decode up to MMCOs */
239 if (src_length > 1000)
245 consumed = ff_h2645_extract_rbsp(buf, src_length, &nal);
249 ret = init_get_bits(&nal.gb, nal.data, nal.size * 8);
253 nal.ref_idc = get_bits(&nal.gb, 2);
254 nal.type = get_bits(&nal.gb, 5);
257 h->nal_ref_idc = nal.ref_idc;
258 h->nal_unit_type = nal.type;
260 switch (h->nal_unit_type) {
262 ff_h264_decode_seq_parameter_set(h);
265 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
268 ff_h264_decode_sei(h);
273 h->prev_frame_num = 0;
274 h->prev_frame_num_offset = 0;
280 get_ue_golomb(&sl->gb); // skip first_mb_in_slice
281 slice_type = get_ue_golomb_31(&sl->gb);
282 s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
283 if (h->sei_recovery_frame_cnt >= 0) {
284 /* key frame, since recovery_frame_cnt is set */
287 pps_id = get_ue_golomb(&sl->gb);
288 if (pps_id >= MAX_PPS_COUNT) {
289 av_log(h->avctx, AV_LOG_ERROR,
290 "pps_id %u out of range\n", pps_id);
293 if (!h->pps_buffers[pps_id]) {
294 av_log(h->avctx, AV_LOG_ERROR,
295 "non-existing PPS %u referenced\n", pps_id);
298 h->pps = *h->pps_buffers[pps_id];
299 if (!h->sps_buffers[h->pps.sps_id]) {
300 av_log(h->avctx, AV_LOG_ERROR,
301 "non-existing SPS %u referenced\n", h->pps.sps_id);
304 h->sps = *h->sps_buffers[h->pps.sps_id];
305 h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
307 s->coded_width = 16 * h->sps.mb_width;
308 s->coded_height = 16 * h->sps.mb_height;
309 s->width = s->coded_width - (h->sps.crop_right + h->sps.crop_left);
310 s->height = s->coded_height - (h->sps.crop_top + h->sps.crop_bottom);
311 if (s->width <= 0 || s->height <= 0) {
312 s->width = s->coded_width;
313 s->height = s->coded_height;
316 switch (h->sps.bit_depth_luma) {
318 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P9;
319 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P9;
320 else s->format = AV_PIX_FMT_YUV420P9;
323 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P10;
324 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P10;
325 else s->format = AV_PIX_FMT_YUV420P10;
328 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P;
329 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P;
330 else s->format = AV_PIX_FMT_YUV420P;
333 s->format = AV_PIX_FMT_NONE;
336 avctx->profile = ff_h264_get_profile(&h->sps);
337 avctx->level = h->sps.level_idc;
339 if (h->sps.frame_mbs_only_flag) {
340 h->picture_structure = PICT_FRAME;
342 if (get_bits1(&sl->gb)) { // field_pic_flag
343 h->picture_structure = PICT_TOP_FIELD + get_bits1(&sl->gb); // bottom_field_flag
345 h->picture_structure = PICT_FRAME;
349 if (h->nal_unit_type == NAL_IDR_SLICE)
350 get_ue_golomb(&sl->gb); /* idr_pic_id */
351 if (h->sps.poc_type == 0) {
352 h->poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);
354 if (h->pps.pic_order_present == 1 &&
355 h->picture_structure == PICT_FRAME)
356 h->delta_poc_bottom = get_se_golomb(&sl->gb);
359 if (h->sps.poc_type == 1 &&
360 !h->sps.delta_pic_order_always_zero_flag) {
361 h->delta_poc[0] = get_se_golomb(&sl->gb);
363 if (h->pps.pic_order_present == 1 &&
364 h->picture_structure == PICT_FRAME)
365 h->delta_poc[1] = get_se_golomb(&sl->gb);
368 /* Decode POC of this picture.
369 * The prev_ values needed for decoding POC of the next picture are not set here. */
370 field_poc[0] = field_poc[1] = INT_MAX;
371 ff_init_poc(h, field_poc, &s->output_picture_number);
373 /* Continue parsing to check if MMCO_RESET is present.
374 * FIXME: MMCO_RESET could appear in non-first slice.
375 * Maybe, we should parse all undisposable non-IDR slice of this
376 * picture until encountering MMCO_RESET in a slice of it. */
377 if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
378 got_reset = scan_mmco_reset(s);
383 /* Set up the prev_ values for decoding POC of the next picture. */
384 h->prev_frame_num = got_reset ? 0 : h->frame_num;
385 h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
386 if (h->nal_ref_idc != 0) {
388 h->prev_poc_msb = h->poc_msb;
389 h->prev_poc_lsb = h->poc_lsb;
393 h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
397 if (h->sps.pic_struct_present_flag) {
398 switch (h->sei_pic_struct) {
399 case SEI_PIC_STRUCT_TOP_FIELD:
400 case SEI_PIC_STRUCT_BOTTOM_FIELD:
403 case SEI_PIC_STRUCT_FRAME:
404 case SEI_PIC_STRUCT_TOP_BOTTOM:
405 case SEI_PIC_STRUCT_BOTTOM_TOP:
408 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
409 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
412 case SEI_PIC_STRUCT_FRAME_DOUBLING:
415 case SEI_PIC_STRUCT_FRAME_TRIPLING:
419 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
423 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
426 if (h->picture_structure == PICT_FRAME) {
427 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
428 if (h->sps.pic_struct_present_flag) {
429 switch (h->sei_pic_struct) {
430 case SEI_PIC_STRUCT_TOP_BOTTOM:
431 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
432 s->field_order = AV_FIELD_TT;
434 case SEI_PIC_STRUCT_BOTTOM_TOP:
435 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
436 s->field_order = AV_FIELD_BB;
439 s->field_order = AV_FIELD_PROGRESSIVE;
443 if (field_poc[0] < field_poc[1])
444 s->field_order = AV_FIELD_TT;
445 else if (field_poc[0] > field_poc[1])
446 s->field_order = AV_FIELD_BB;
448 s->field_order = AV_FIELD_PROGRESSIVE;
451 if (h->picture_structure == PICT_TOP_FIELD)
452 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
454 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
455 s->field_order = AV_FIELD_UNKNOWN;
458 av_freep(&nal.rbsp_buffer);
459 return 0; /* no need to evaluate the rest */
463 /* didn't find a picture! */
464 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
466 av_freep(&nal.rbsp_buffer);
470 static int h264_parse(AVCodecParserContext *s,
471 AVCodecContext *avctx,
472 const uint8_t **poutbuf, int *poutbuf_size,
473 const uint8_t *buf, int buf_size)
475 H264ParseContext *p = s->priv_data;
476 H264Context *h = &p->h;
477 ParseContext *pc = &p->pc;
482 if (avctx->extradata_size) {
484 // must be done like in the decoder.
485 // otherwise opening the parser, creating extradata,
486 // and then closing and opening again
487 // will cause has_b_frames to be always set.
488 // NB: estimate_timings_from_pts behaves exactly like this.
489 if (!avctx->has_b_frames)
491 ff_h264_decode_extradata(h);
495 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
498 next = h264_find_frame_end(p, buf, buf_size);
500 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
506 if (next < 0 && next != END_NOT_FOUND) {
507 assert(pc->last_index + next >= 0);
508 h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
512 parse_nal_units(s, avctx, buf, buf_size);
514 if (h->sei_cpb_removal_delay >= 0) {
515 s->dts_sync_point = h->sei_buffering_period_present;
516 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
517 s->pts_dts_delta = h->sei_dpb_output_delay;
519 s->dts_sync_point = INT_MIN;
520 s->dts_ref_dts_delta = INT_MIN;
521 s->pts_dts_delta = INT_MIN;
524 if (s->flags & PARSER_FLAG_ONCE) {
525 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
529 *poutbuf_size = buf_size;
533 static int h264_split(AVCodecContext *avctx,
534 const uint8_t *buf, int buf_size)
540 for (i = 0; i <= buf_size; i++) {
541 if ((state & 0xFFFFFF1F) == 0x107)
543 /* if((state&0xFFFFFF1F) == 0x101 ||
544 * (state&0xFFFFFF1F) == 0x102 ||
545 * (state&0xFFFFFF1F) == 0x105) {
548 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
549 (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
550 (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
551 (state & 0xFFFFFF1F) != 0x10f) {
553 while (i > 4 && buf[i - 5] == 0)
559 state = (state << 8) | buf[i];
564 static void h264_close(AVCodecParserContext *s)
566 H264ParseContext *p = s->priv_data;
567 H264Context *h = &p->h;
568 ParseContext *pc = &p->pc;
571 ff_h264_free_context(h);
574 static av_cold int init(AVCodecParserContext *s)
576 H264ParseContext *p = s->priv_data;
577 H264Context *h = &p->h;
579 h->slice_ctx = av_mallocz(sizeof(*h->slice_ctx));
584 h->slice_context_count = 1;
585 ff_h264dsp_init(&h->h264dsp, 8, 1);
589 AVCodecParser ff_h264_parser = {
590 .codec_ids = { AV_CODEC_ID_H264 },
591 .priv_data_size = sizeof(H264ParseContext),
593 .parser_parse = h264_parse,
594 .parser_close = h264_close,