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"
37 static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
42 ParseContext *pc = &h->parse_context;
43 // mb_addr= pc->mb_addr - 1;
48 for (i = 0; i < buf_size; i++) {
50 i += h->h264dsp.h264_find_start_code_candidate(buf + i, buf_size - i);
53 } else if (state <= 2) {
55 state ^= 5; // 2->7, 1->4, 0->5
59 state >>= 1; // 2->1, 1->0, 0->0
60 } else if (state <= 5) {
61 int v = buf[i] & 0x1F;
62 if (v == 6 || v == 7 || v == 8 || v == 9) {
63 if (pc->frame_start_found) {
67 } else if (v == 1 || v == 2 || v == 5) {
68 if (pc->frame_start_found) {
72 pc->frame_start_found = 1;
86 pc->frame_start_found = 0;
87 return i - (state & 5);
91 * Parse NAL units of found picture and decode some basic information.
93 * @param s parser context.
94 * @param avctx codec context.
95 * @param buf buffer with field/frame data.
96 * @param buf_size size of the buffer.
98 static inline int parse_nal_units(AVCodecParserContext *s,
99 AVCodecContext *avctx,
100 const uint8_t *buf, int buf_size)
102 H264Context *h = s->priv_data;
103 const uint8_t *buf_end = buf + buf_size;
105 unsigned int slice_type;
110 /* set some sane default values */
111 s->pict_type = AV_PICTURE_TYPE_I;
113 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
116 h->sei_recovery_frame_cnt = -1;
117 h->sei_dpb_output_delay = 0;
118 h->sei_cpb_removal_delay = -1;
119 h->sei_buffering_period_present = 0;
125 int src_length, dst_length, consumed;
126 buf = avpriv_find_start_code(buf, buf_end, &state);
130 src_length = buf_end - buf;
131 switch (state & 0x1f) {
134 // Do not walk the whole buffer just to decode slice header
139 ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
140 if (ptr == NULL || dst_length < 0)
143 init_get_bits(&h->gb, ptr, 8 * dst_length);
144 switch (h->nal_unit_type) {
146 ff_h264_decode_seq_parameter_set(h);
149 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
152 ff_h264_decode_sei(h);
157 h->prev_frame_num = 0;
158 h->prev_frame_num_offset = 0;
163 get_ue_golomb(&h->gb); // skip first_mb_in_slice
164 slice_type = get_ue_golomb_31(&h->gb);
165 s->pict_type = golomb_to_pict_type[slice_type % 5];
166 if (h->sei_recovery_frame_cnt >= 0) {
167 /* key frame, since recovery_frame_cnt is set */
170 pps_id = get_ue_golomb(&h->gb);
171 if (pps_id >= MAX_PPS_COUNT) {
172 av_log(h->avctx, AV_LOG_ERROR,
173 "pps_id out of range\n");
176 if (!h->pps_buffers[pps_id]) {
177 av_log(h->avctx, AV_LOG_ERROR,
178 "non-existing PPS referenced\n");
181 h->pps = *h->pps_buffers[pps_id];
182 if (!h->sps_buffers[h->pps.sps_id]) {
183 av_log(h->avctx, AV_LOG_ERROR,
184 "non-existing SPS referenced\n");
187 h->sps = *h->sps_buffers[h->pps.sps_id];
188 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
190 avctx->profile = ff_h264_get_profile(&h->sps);
191 avctx->level = h->sps.level_idc;
193 if (h->sps.frame_mbs_only_flag) {
194 h->picture_structure = PICT_FRAME;
196 if (get_bits1(&h->gb)) { // field_pic_flag
197 h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
199 h->picture_structure = PICT_FRAME;
203 if (h->nal_unit_type == NAL_IDR_SLICE)
204 get_ue_golomb(&h->gb); /* idr_pic_id */
205 if (h->sps.poc_type == 0) {
206 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
208 if (h->pps.pic_order_present == 1 &&
209 h->picture_structure == PICT_FRAME)
210 h->delta_poc_bottom = get_se_golomb(&h->gb);
213 if (h->sps.poc_type == 1 &&
214 !h->sps.delta_pic_order_always_zero_flag) {
215 h->delta_poc[0] = get_se_golomb(&h->gb);
217 if (h->pps.pic_order_present == 1 &&
218 h->picture_structure == PICT_FRAME)
219 h->delta_poc[1] = get_se_golomb(&h->gb);
222 ff_init_poc(h, field_poc, &s->output_picture_number);
224 if (h->sps.pic_struct_present_flag) {
225 switch (h->sei_pic_struct) {
226 case SEI_PIC_STRUCT_TOP_FIELD:
227 case SEI_PIC_STRUCT_BOTTOM_FIELD:
230 case SEI_PIC_STRUCT_FRAME:
231 case SEI_PIC_STRUCT_TOP_BOTTOM:
232 case SEI_PIC_STRUCT_BOTTOM_TOP:
235 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
236 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
239 case SEI_PIC_STRUCT_FRAME_DOUBLING:
242 case SEI_PIC_STRUCT_FRAME_TRIPLING:
246 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
250 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
253 if (h->picture_structure == PICT_FRAME) {
254 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
255 if (h->sps.pic_struct_present_flag) {
256 switch (h->sei_pic_struct) {
257 case SEI_PIC_STRUCT_TOP_BOTTOM:
258 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
259 s->field_order = AV_FIELD_TT;
261 case SEI_PIC_STRUCT_BOTTOM_TOP:
262 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
263 s->field_order = AV_FIELD_BB;
266 s->field_order = AV_FIELD_PROGRESSIVE;
270 if (field_poc[0] < field_poc[1])
271 s->field_order = AV_FIELD_TT;
272 else if (field_poc[0] > field_poc[1])
273 s->field_order = AV_FIELD_BB;
275 s->field_order = AV_FIELD_PROGRESSIVE;
278 if (h->picture_structure == PICT_TOP_FIELD)
279 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
281 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
282 s->field_order = AV_FIELD_UNKNOWN;
285 return 0; /* no need to evaluate the rest */
289 /* didn't find a picture! */
290 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
294 static int h264_parse(AVCodecParserContext *s,
295 AVCodecContext *avctx,
296 const uint8_t **poutbuf, int *poutbuf_size,
297 const uint8_t *buf, int buf_size)
299 H264Context *h = s->priv_data;
300 ParseContext *pc = &h->parse_context;
305 if (avctx->extradata_size) {
307 // must be done like in the decoder.
308 // otherwise opening the parser, creating extradata,
309 // and then closing and opening again
310 // will cause has_b_frames to be always set.
311 // NB: estimate_timings_from_pts behaves exactly like this.
312 if (!avctx->has_b_frames)
314 ff_h264_decode_extradata(h);
318 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
321 next = h264_find_frame_end(h, buf, buf_size);
323 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
329 if (next < 0 && next != END_NOT_FOUND) {
330 assert(pc->last_index + next >= 0);
331 h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
335 parse_nal_units(s, avctx, buf, buf_size);
337 if (h->sei_cpb_removal_delay >= 0) {
338 s->dts_sync_point = h->sei_buffering_period_present;
339 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
340 s->pts_dts_delta = h->sei_dpb_output_delay;
342 s->dts_sync_point = INT_MIN;
343 s->dts_ref_dts_delta = INT_MIN;
344 s->pts_dts_delta = INT_MIN;
347 if (s->flags & PARSER_FLAG_ONCE) {
348 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
352 *poutbuf_size = buf_size;
356 static int h264_split(AVCodecContext *avctx,
357 const uint8_t *buf, int buf_size)
363 for (i = 0; i <= buf_size; i++) {
364 if ((state & 0xFFFFFF1F) == 0x107)
366 /* if((state&0xFFFFFF1F) == 0x101 ||
367 * (state&0xFFFFFF1F) == 0x102 ||
368 * (state&0xFFFFFF1F) == 0x105) {
371 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
372 (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
374 while (i > 4 && buf[i - 5] == 0)
380 state = (state << 8) | buf[i];
385 static void close(AVCodecParserContext *s)
387 H264Context *h = s->priv_data;
388 ParseContext *pc = &h->parse_context;
391 ff_h264_free_context(h);
394 static av_cold int init(AVCodecParserContext *s)
396 H264Context *h = s->priv_data;
397 h->thread_context[0] = h;
398 h->slice_context_count = 1;
399 ff_h264dsp_init(&h->h264dsp, 8, 1);
403 AVCodecParser ff_h264_parser = {
404 .codec_ids = { AV_CODEC_ID_H264 },
405 .priv_data_size = sizeof(H264Context),
407 .parser_parse = h264_parse,
408 .parser_close = close,