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 FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 #define UNCHECKED_BITSTREAM_READER 1
30 #include "libavutil/attributes.h"
37 static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
42 ParseContext *pc = &h->parse_context;
43 int next_avc= h->is_avc ? 0 : buf_size;
45 // mb_addr= pc->mb_addr - 1;
50 if (h->is_avc && !h->nal_length_size)
51 av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
53 for (i = 0; i < buf_size; i++) {
57 for (j = 0; j < h->nal_length_size; j++)
58 nalsize = (nalsize << 8) | buf[i++];
59 if (nalsize <= 0 || nalsize > buf_size - i) {
60 av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
63 next_avc = i + nalsize;
68 i += h->h264dsp.h264_find_start_code_candidate(buf + i, next_avc - i);
71 } else if (state <= 2) {
73 state ^= 5; // 2->7, 1->4, 0->5
77 state >>= 1; // 2->1, 1->0, 0->0
78 } else if (state <= 5) {
79 int v = buf[i] & 0x1F;
80 if (v == 6 || v == 7 || v == 8 || v == 9) {
81 if (pc->frame_start_found) {
85 } else if (v == 1 || v == 2 || v == 5) {
91 h->parse_history[h->parse_history_count++]= buf[i];
92 if (h->parse_history_count>3) {
93 unsigned int mb, last_mb= h->parse_last_mb;
96 init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
97 h->parse_history_count=0;
98 mb= get_ue_golomb_long(&gb);
99 last_mb= h->parse_last_mb;
100 h->parse_last_mb= mb;
101 if (pc->frame_start_found) {
105 pc->frame_start_found = 1;
113 return END_NOT_FOUND;
117 pc->frame_start_found = 0;
120 return i - (state & 5) - 3 * (state > 7);
124 * Parse NAL units of found picture and decode some basic information.
126 * @param s parser context.
127 * @param avctx codec context.
128 * @param buf buffer with field/frame data.
129 * @param buf_size size of the buffer.
131 static inline int parse_nal_units(AVCodecParserContext *s,
132 AVCodecContext *avctx,
133 const uint8_t *buf, int buf_size)
135 H264Context *h = s->priv_data;
136 const uint8_t *buf_end = buf + buf_size;
138 unsigned int slice_type;
141 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
144 /* set some sane default values */
145 s->pict_type = AV_PICTURE_TYPE_I;
147 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
150 h->sei_recovery_frame_cnt = -1;
151 h->sei_dpb_output_delay = 0;
152 h->sei_cpb_removal_delay = -1;
153 h->sei_buffering_period_present = 0;
154 h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
160 int src_length, dst_length, consumed, nalsize = 0;
163 if (h->nal_length_size >= buf_end - buf) break;
165 for (i = 0; i < h->nal_length_size; i++)
166 nalsize = (nalsize << 8) | *buf++;
167 if (nalsize <= 0 || nalsize > buf_end - buf) {
168 av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
171 src_length = nalsize;
173 buf = avpriv_find_start_code(buf, buf_end, &state);
177 src_length = buf_end - buf;
179 switch (state & 0x1f) {
182 // Do not walk the whole buffer just to decode slice header
187 ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
188 if (ptr == NULL || dst_length < 0)
191 init_get_bits(&h->gb, ptr, 8 * dst_length);
192 switch (h->nal_unit_type) {
194 ff_h264_decode_seq_parameter_set(h);
197 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
200 ff_h264_decode_sei(h);
205 h->prev_frame_num = 0;
206 h->prev_frame_num_offset = 0;
211 get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
212 slice_type = get_ue_golomb_31(&h->gb);
213 s->pict_type = golomb_to_pict_type[slice_type % 5];
214 if (h->sei_recovery_frame_cnt >= 0) {
215 /* key frame, since recovery_frame_cnt is set */
218 pps_id = get_ue_golomb(&h->gb);
219 if (pps_id >= MAX_PPS_COUNT) {
220 av_log(h->avctx, AV_LOG_ERROR,
221 "pps_id out of range\n");
224 if (!h->pps_buffers[pps_id]) {
225 av_log(h->avctx, AV_LOG_ERROR,
226 "non-existing PPS referenced\n");
229 h->pps = *h->pps_buffers[pps_id];
230 if (!h->sps_buffers[h->pps.sps_id]) {
231 av_log(h->avctx, AV_LOG_ERROR,
232 "non-existing SPS referenced\n");
235 h->sps = *h->sps_buffers[h->pps.sps_id];
236 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
238 avctx->profile = ff_h264_get_profile(&h->sps);
239 avctx->level = h->sps.level_idc;
241 if (h->sps.frame_mbs_only_flag) {
242 h->picture_structure = PICT_FRAME;
244 if (get_bits1(&h->gb)) { // field_pic_flag
245 h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
247 h->picture_structure = PICT_FRAME;
251 if (h->nal_unit_type == NAL_IDR_SLICE)
252 get_ue_golomb(&h->gb); /* idr_pic_id */
253 if (h->sps.poc_type == 0) {
254 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
256 if (h->pps.pic_order_present == 1 &&
257 h->picture_structure == PICT_FRAME)
258 h->delta_poc_bottom = get_se_golomb(&h->gb);
261 if (h->sps.poc_type == 1 &&
262 !h->sps.delta_pic_order_always_zero_flag) {
263 h->delta_poc[0] = get_se_golomb(&h->gb);
265 if (h->pps.pic_order_present == 1 &&
266 h->picture_structure == PICT_FRAME)
267 h->delta_poc[1] = get_se_golomb(&h->gb);
270 ff_init_poc(h, field_poc, &s->output_picture_number);
272 if (h->sps.pic_struct_present_flag) {
273 switch (h->sei_pic_struct) {
274 case SEI_PIC_STRUCT_TOP_FIELD:
275 case SEI_PIC_STRUCT_BOTTOM_FIELD:
278 case SEI_PIC_STRUCT_FRAME:
279 case SEI_PIC_STRUCT_TOP_BOTTOM:
280 case SEI_PIC_STRUCT_BOTTOM_TOP:
283 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
284 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
287 case SEI_PIC_STRUCT_FRAME_DOUBLING:
290 case SEI_PIC_STRUCT_FRAME_TRIPLING:
294 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
298 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
301 if (h->picture_structure == PICT_FRAME) {
302 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
303 if (h->sps.pic_struct_present_flag) {
304 switch (h->sei_pic_struct) {
305 case SEI_PIC_STRUCT_TOP_BOTTOM:
306 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
307 s->field_order = AV_FIELD_TT;
309 case SEI_PIC_STRUCT_BOTTOM_TOP:
310 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
311 s->field_order = AV_FIELD_BB;
314 s->field_order = AV_FIELD_PROGRESSIVE;
318 if (field_poc[0] < field_poc[1])
319 s->field_order = AV_FIELD_TT;
320 else if (field_poc[0] > field_poc[1])
321 s->field_order = AV_FIELD_BB;
323 s->field_order = AV_FIELD_PROGRESSIVE;
326 if (h->picture_structure == PICT_TOP_FIELD)
327 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
329 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
330 s->field_order = AV_FIELD_UNKNOWN;
333 return 0; /* no need to evaluate the rest */
335 buf += h->is_avc ? nalsize : consumed;
339 /* didn't find a picture! */
340 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
344 static int h264_parse(AVCodecParserContext *s,
345 AVCodecContext *avctx,
346 const uint8_t **poutbuf, int *poutbuf_size,
347 const uint8_t *buf, int buf_size)
349 H264Context *h = s->priv_data;
350 ParseContext *pc = &h->parse_context;
355 if (avctx->extradata_size) {
357 // must be done like in decoder, otherwise opening the parser,
358 // letting it create extradata and then closing and opening again
359 // will cause has_b_frames to be always set.
360 // Note that estimate_timings_from_pts does exactly this.
361 if (!avctx->has_b_frames)
363 ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
367 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
370 next = h264_find_frame_end(h, buf, buf_size);
372 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
378 if (next < 0 && next != END_NOT_FOUND) {
379 av_assert1(pc->last_index + next >= 0);
380 h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
384 parse_nal_units(s, avctx, buf, buf_size);
386 if (h->sei_cpb_removal_delay >= 0) {
387 s->dts_sync_point = h->sei_buffering_period_present;
388 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
389 s->pts_dts_delta = h->sei_dpb_output_delay;
391 s->dts_sync_point = INT_MIN;
392 s->dts_ref_dts_delta = INT_MIN;
393 s->pts_dts_delta = INT_MIN;
396 if (s->flags & PARSER_FLAG_ONCE) {
397 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
401 *poutbuf_size = buf_size;
405 static int h264_split(AVCodecContext *avctx,
406 const uint8_t *buf, int buf_size)
412 for (i = 0; i <= buf_size; i++) {
413 if ((state & 0xFFFFFF1F) == 0x107)
415 /* if ((state&0xFFFFFF1F) == 0x101 ||
416 * (state&0xFFFFFF1F) == 0x102 ||
417 * (state&0xFFFFFF1F) == 0x105) {
420 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
421 (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
423 while (i > 4 && buf[i - 5] == 0)
429 state = (state << 8) | buf[i];
434 static void close(AVCodecParserContext *s)
436 H264Context *h = s->priv_data;
437 ParseContext *pc = &h->parse_context;
440 ff_h264_free_context(h);
443 static av_cold int init(AVCodecParserContext *s)
445 H264Context *h = s->priv_data;
446 h->thread_context[0] = h;
447 h->slice_context_count = 1;
448 ff_h264dsp_init(&h->h264dsp, 8, 1);
452 AVCodecParser ff_h264_parser = {
453 .codec_ids = { AV_CODEC_ID_H264 },
454 .priv_data_size = sizeof(H264Context),
456 .parser_parse = h264_parse,
457 .parser_close = close,