h264_parser: Fix POC parsing for the case where MMCO_RESET is absent.
[libav.git] / libavcodec / h264_parser.c
CommitLineData
26b4fe82
AJ
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
26b4fe82 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
26b4fe82
AJ
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.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
26b4fe82
AJ
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
26b4fe82 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26b4fe82
AJ
20 */
21
22/**
ba87f080 23 * @file
26b4fe82
AJ
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
6fee1b90 28#include "libavutil/attributes.h"
26b4fe82 29#include "parser.h"
ff6474dd
IS
30#include "h264data.h"
31#include "golomb.h"
f1e93986 32#include "internal.h"
26b4fe82
AJ
33
34#include <assert.h>
35
36
f8a4d5e9
LB
37static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
38 int buf_size)
26b4fe82
AJ
39{
40 int i;
41 uint32_t state;
2c541554 42 ParseContext *pc = &h->parse_context;
26b4fe82 43// mb_addr= pc->mb_addr - 1;
f8a4d5e9
LB
44 state = pc->state;
45 if (state > 13)
46 state = 7;
26b4fe82 47
f8a4d5e9
LB
48 for (i = 0; i < buf_size; i++) {
49 if (state == 7) {
218d6844
BA
50 i += h->h264dsp.h264_find_start_code_candidate(buf + i, buf_size - i);
51 if (i < buf_size)
52 state = 2;
f8a4d5e9
LB
53 } else if (state <= 2) {
54 if (buf[i] == 1)
55 state ^= 5; // 2->7, 1->4, 0->5
56 else if (buf[i])
57 state = 7;
58 else
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) {
26b4fe82 64 i++;
9aa1cfec 65 goto found;
26b4fe82 66 }
f8a4d5e9
LB
67 } else if (v == 1 || v == 2 || v == 5) {
68 if (pc->frame_start_found) {
69 state += 8;
26b4fe82 70 continue;
f8a4d5e9 71 } else
26b4fe82
AJ
72 pc->frame_start_found = 1;
73 }
f8a4d5e9
LB
74 state = 7;
75 } else {
76 if (buf[i] & 0x80)
26b4fe82 77 goto found;
f8a4d5e9 78 state = 7;
26b4fe82
AJ
79 }
80 }
f8a4d5e9 81 pc->state = state;
26b4fe82 82 return END_NOT_FOUND;
9aa1cfec
DP
83
84found:
f8a4d5e9
LB
85 pc->state = 7;
86 pc->frame_start_found = 0;
87 return i - (state & 5);
26b4fe82
AJ
88}
89
adbfc605 90/**
ff6474dd
IS
91 * Parse NAL units of found picture and decode some basic information.
92 *
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.
97 */
98static inline int parse_nal_units(AVCodecParserContext *s,
99 AVCodecContext *avctx,
100 const uint8_t *buf, int buf_size)
101{
f8a4d5e9 102 H264Context *h = s->priv_data;
ff6474dd 103 const uint8_t *buf_end = buf + buf_size;
96c3da93 104 unsigned int pps_id;
ff6474dd 105 unsigned int slice_type;
8fa0ae06 106 int state = -1;
ff6474dd 107 const uint8_t *ptr;
3f1a7ceb 108 int field_poc[2];
ff6474dd
IS
109
110 /* set some sane default values */
f8a4d5e9
LB
111 s->pict_type = AV_PICTURE_TYPE_I;
112 s->key_frame = 0;
3f1a7ceb 113 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
ff6474dd 114
f8a4d5e9
LB
115 h->avctx = avctx;
116 h->sei_recovery_frame_cnt = -1;
117 h->sei_dpb_output_delay = 0;
c733922e 118 h->sei_cpb_removal_delay = -1;
f8a4d5e9 119 h->sei_buffering_period_present = 0;
ff6474dd 120
9479415e
BC
121 if (!buf_size)
122 return 0;
123
f8a4d5e9 124 for (;;) {
ff6474dd 125 int src_length, dst_length, consumed;
f1e93986 126 buf = avpriv_find_start_code(buf, buf_end, &state);
f8a4d5e9 127 if (buf >= buf_end)
ff6474dd
IS
128 break;
129 --buf;
130 src_length = buf_end - buf;
131 switch (state & 0x1f) {
132 case NAL_SLICE:
133 case NAL_IDR_SLICE:
134 // Do not walk the whole buffer just to decode slice header
b81dbd6c
YN
135 if (src_length > 60)
136 src_length = 60;
ff6474dd
IS
137 break;
138 }
f8a4d5e9
LB
139 ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
140 if (ptr == NULL || dst_length < 0)
ff6474dd
IS
141 break;
142
f8a4d5e9
LB
143 init_get_bits(&h->gb, ptr, 8 * dst_length);
144 switch (h->nal_unit_type) {
ff6474dd
IS
145 case NAL_SPS:
146 ff_h264_decode_seq_parameter_set(h);
147 break;
148 case NAL_PPS:
2c541554 149 ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
ff6474dd
IS
150 break;
151 case NAL_SEI:
152 ff_h264_decode_sei(h);
153 break;
154 case NAL_IDR_SLICE:
0ed260c7 155 s->key_frame = 1;
3f1a7ceb
YN
156
157 h->prev_frame_num = 0;
158 h->prev_frame_num_offset = 0;
159 h->prev_poc_msb =
160 h->prev_poc_lsb = 0;
f8a4d5e9 161 /* fall through */
ff6474dd 162 case NAL_SLICE:
2c541554 163 get_ue_golomb(&h->gb); // skip first_mb_in_slice
f8a4d5e9 164 slice_type = get_ue_golomb_31(&h->gb);
ff6474dd 165 s->pict_type = golomb_to_pict_type[slice_type % 5];
0ed260c7
IS
166 if (h->sei_recovery_frame_cnt >= 0) {
167 /* key frame, since recovery_frame_cnt is set */
168 s->key_frame = 1;
169 }
f8a4d5e9
LB
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");
96c3da93
IS
174 return -1;
175 }
f8a4d5e9
LB
176 if (!h->pps_buffers[pps_id]) {
177 av_log(h->avctx, AV_LOG_ERROR,
178 "non-existing PPS referenced\n");
96c3da93
IS
179 return -1;
180 }
f8a4d5e9
LB
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");
96c3da93
IS
185 return -1;
186 }
f8a4d5e9 187 h->sps = *h->sps_buffers[h->pps.sps_id];
2c541554 188 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
96c3da93 189
fe9a3fbe 190 avctx->profile = ff_h264_get_profile(&h->sps);
dd0cd3d2
RC
191 avctx->level = h->sps.level_idc;
192
f8a4d5e9
LB
193 if (h->sps.frame_mbs_only_flag) {
194 h->picture_structure = PICT_FRAME;
195 } else {
196 if (get_bits1(&h->gb)) { // field_pic_flag
197 h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
96c3da93 198 } else {
f8a4d5e9 199 h->picture_structure = PICT_FRAME;
96c3da93
IS
200 }
201 }
202
3f1a7ceb
YN
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);
207
f8a4d5e9
LB
208 if (h->pps.pic_order_present == 1 &&
209 h->picture_structure == PICT_FRAME)
3f1a7ceb
YN
210 h->delta_poc_bottom = get_se_golomb(&h->gb);
211 }
212
f8a4d5e9
LB
213 if (h->sps.poc_type == 1 &&
214 !h->sps.delta_pic_order_always_zero_flag) {
3f1a7ceb
YN
215 h->delta_poc[0] = get_se_golomb(&h->gb);
216
f8a4d5e9
LB
217 if (h->pps.pic_order_present == 1 &&
218 h->picture_structure == PICT_FRAME)
3f1a7ceb
YN
219 h->delta_poc[1] = get_se_golomb(&h->gb);
220 }
221
b81dbd6c
YN
222 /* Decode POC of this picture. */
223 field_poc[0] = field_poc[1] = INT_MAX;
a8b19271 224 ff_init_poc(h, field_poc, &s->output_picture_number);
3f1a7ceb 225
b81dbd6c
YN
226 /* Set up the prev_ values for decoding POC of the next picture. */
227 h->prev_frame_num = h->frame_num;
228 h->prev_frame_num_offset = h->frame_num_offset;
229 if (h->nal_ref_idc != 0) {
230 h->prev_poc_msb = h->poc_msb;
231 h->prev_poc_lsb = h->poc_lsb;
232 }
233
f8a4d5e9 234 if (h->sps.pic_struct_present_flag) {
346db3ef 235 switch (h->sei_pic_struct) {
f8a4d5e9
LB
236 case SEI_PIC_STRUCT_TOP_FIELD:
237 case SEI_PIC_STRUCT_BOTTOM_FIELD:
238 s->repeat_pict = 0;
239 break;
240 case SEI_PIC_STRUCT_FRAME:
241 case SEI_PIC_STRUCT_TOP_BOTTOM:
242 case SEI_PIC_STRUCT_BOTTOM_TOP:
243 s->repeat_pict = 1;
244 break;
245 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
246 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
247 s->repeat_pict = 2;
248 break;
249 case SEI_PIC_STRUCT_FRAME_DOUBLING:
250 s->repeat_pict = 3;
251 break;
252 case SEI_PIC_STRUCT_FRAME_TRIPLING:
253 s->repeat_pict = 5;
254 break;
255 default:
256 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
257 break;
346db3ef
IS
258 }
259 } else {
2c541554 260 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
261 }
262
3f1a7ceb
YN
263 if (h->picture_structure == PICT_FRAME) {
264 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
265 if (h->sps.pic_struct_present_flag) {
266 switch (h->sei_pic_struct) {
f8a4d5e9
LB
267 case SEI_PIC_STRUCT_TOP_BOTTOM:
268 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
269 s->field_order = AV_FIELD_TT;
270 break;
271 case SEI_PIC_STRUCT_BOTTOM_TOP:
272 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
273 s->field_order = AV_FIELD_BB;
274 break;
275 default:
276 s->field_order = AV_FIELD_PROGRESSIVE;
277 break;
3f1a7ceb
YN
278 }
279 } else {
280 if (field_poc[0] < field_poc[1])
281 s->field_order = AV_FIELD_TT;
282 else if (field_poc[0] > field_poc[1])
283 s->field_order = AV_FIELD_BB;
284 else
285 s->field_order = AV_FIELD_PROGRESSIVE;
286 }
287 } else {
288 if (h->picture_structure == PICT_TOP_FIELD)
289 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
290 else
291 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
292 s->field_order = AV_FIELD_UNKNOWN;
293 }
294
ff6474dd
IS
295 return 0; /* no need to evaluate the rest */
296 }
297 buf += consumed;
298 }
299 /* didn't find a picture! */
2c541554 300 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
ff6474dd
IS
301 return -1;
302}
303
26b4fe82
AJ
304static int h264_parse(AVCodecParserContext *s,
305 AVCodecContext *avctx,
306 const uint8_t **poutbuf, int *poutbuf_size,
307 const uint8_t *buf, int buf_size)
308{
f8a4d5e9 309 H264Context *h = s->priv_data;
2c541554 310 ParseContext *pc = &h->parse_context;
26b4fe82
AJ
311 int next;
312
82f1ffc7
HC
313 if (!h->got_first) {
314 h->got_first = 1;
23584bec 315 if (avctx->extradata_size) {
2c541554 316 h->avctx = avctx;
790a367d
RT
317 // must be done like in the decoder.
318 // otherwise opening the parser, creating extradata,
319 // and then closing and opening again
320 // will cause has_b_frames to be always set.
321 // NB: estimate_timings_from_pts behaves exactly like this.
322 if (!avctx->has_b_frames)
2c541554 323 h->low_delay = 1;
23584bec
HC
324 ff_h264_decode_extradata(h);
325 }
326 }
327
f8a4d5e9
LB
328 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
329 next = buf_size;
330 } else {
088f38a4 331 next = h264_find_frame_end(h, buf, buf_size);
26b4fe82
AJ
332
333 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
f8a4d5e9 334 *poutbuf = NULL;
26b4fe82
AJ
335 *poutbuf_size = 0;
336 return buf_size;
337 }
338
f8a4d5e9
LB
339 if (next < 0 && next != END_NOT_FOUND) {
340 assert(pc->last_index + next >= 0);
088f38a4 341 h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
26b4fe82 342 }
a26ce1e2 343 }
ff6474dd 344
25f05ddb 345 parse_nal_units(s, avctx, buf, buf_size);
c733922e 346
25f05ddb
PL
347 if (h->sei_cpb_removal_delay >= 0) {
348 s->dts_sync_point = h->sei_buffering_period_present;
349 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
350 s->pts_dts_delta = h->sei_dpb_output_delay;
351 } else {
352 s->dts_sync_point = INT_MIN;
353 s->dts_ref_dts_delta = INT_MIN;
354 s->pts_dts_delta = INT_MIN;
355 }
356
357 if (s->flags & PARSER_FLAG_ONCE) {
358 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
359 }
26b4fe82 360
f8a4d5e9 361 *poutbuf = buf;
26b4fe82
AJ
362 *poutbuf_size = buf_size;
363 return next;
364}
365
366static int h264_split(AVCodecContext *avctx,
367 const uint8_t *buf, int buf_size)
368{
369 int i;
370 uint32_t state = -1;
f8a4d5e9
LB
371 int has_sps = 0;
372
373 for (i = 0; i <= buf_size; i++) {
374 if ((state & 0xFFFFFF1F) == 0x107)
375 has_sps = 1;
376 /* if((state&0xFFFFFF1F) == 0x101 ||
377 * (state&0xFFFFFF1F) == 0x102 ||
378 * (state&0xFFFFFF1F) == 0x105) {
379 * }
380 */
381 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
382 (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
383 if (has_sps) {
384 while (i > 4 && buf[i - 5] == 0)
385 i--;
386 return i - 4;
26b4fe82
AJ
387 }
388 }
f8a4d5e9
LB
389 if (i < buf_size)
390 state = (state << 8) | buf[i];
26b4fe82
AJ
391 }
392 return 0;
393}
394
dd990075 395static void close(AVCodecParserContext *s)
3ee4f5e4 396{
f8a4d5e9 397 H264Context *h = s->priv_data;
2c541554 398 ParseContext *pc = &h->parse_context;
3ee4f5e4
MN
399
400 av_free(pc->buffer);
15861962 401 ff_h264_free_context(h);
3ee4f5e4
MN
402}
403
6fee1b90 404static av_cold int init(AVCodecParserContext *s)
e9ca315d
RC
405{
406 H264Context *h = s->priv_data;
f8a4d5e9 407 h->thread_context[0] = h;
2c541554 408 h->slice_context_count = 1;
7a82022e 409 ff_h264dsp_init(&h->h264dsp, 8, 1);
e9ca315d
RC
410 return 0;
411}
26b4fe82 412
d36beb3f 413AVCodecParser ff_h264_parser = {
36ef5369 414 .codec_ids = { AV_CODEC_ID_H264 },
5511ad14
AK
415 .priv_data_size = sizeof(H264Context),
416 .parser_init = init,
417 .parser_parse = h264_parse,
418 .parser_close = close,
419 .split = h264_split,
26b4fe82 420};