h264: factor out calculating the POC count into a separate file
[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
02cd8bb9
DB
28#include <assert.h>
29#include <stdint.h>
30
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"
36
37#include "get_bits.h"
ff6474dd 38#include "golomb.h"
02cd8bb9
DB
39#include "h264.h"
40#include "h264data.h"
f1e93986 41#include "internal.h"
e0c16e4e 42#include "mpegutils.h"
02cd8bb9 43#include "parser.h"
26b4fe82 44
9404a47a
AK
45typedef struct H264ParseContext {
46 H264Context h;
47 ParseContext pc;
3176217c 48 H264ParamSets ps;
113aeee6 49 H264DSPContext h264dsp;
c8dcff0c 50 H264POCContext poc;
9404a47a
AK
51 int got_first;
52} H264ParseContext;
26b4fe82 53
9404a47a
AK
54
55static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
f8a4d5e9 56 int buf_size)
26b4fe82
AJ
57{
58 int i;
59 uint32_t state;
9404a47a 60 ParseContext *pc = &p->pc;
26b4fe82 61// mb_addr= pc->mb_addr - 1;
f8a4d5e9
LB
62 state = pc->state;
63 if (state > 13)
64 state = 7;
26b4fe82 65
f8a4d5e9
LB
66 for (i = 0; i < buf_size; i++) {
67 if (state == 7) {
113aeee6 68 i += p->h264dsp.startcode_find_candidate(buf + i, buf_size - i);
218d6844
BA
69 if (i < buf_size)
70 state = 2;
f8a4d5e9
LB
71 } else if (state <= 2) {
72 if (buf[i] == 1)
73 state ^= 5; // 2->7, 1->4, 0->5
74 else if (buf[i])
75 state = 7;
76 else
77 state >>= 1; // 2->1, 1->0, 0->0
78 } else if (state <= 5) {
dc971acf
VG
79 int nalu_type = buf[i] & 0x1F;
80 if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
81 nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
f8a4d5e9 82 if (pc->frame_start_found) {
26b4fe82 83 i++;
9aa1cfec 84 goto found;
26b4fe82 85 }
dc971acf
VG
86 } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
87 nalu_type == NAL_IDR_SLICE) {
f8a4d5e9
LB
88 if (pc->frame_start_found) {
89 state += 8;
26b4fe82 90 continue;
f8a4d5e9 91 } else
26b4fe82
AJ
92 pc->frame_start_found = 1;
93 }
f8a4d5e9
LB
94 state = 7;
95 } else {
58ae8d59 96 // first_mb_in_slice is 0, probably the first nal of a new slice
f8a4d5e9 97 if (buf[i] & 0x80)
26b4fe82 98 goto found;
f8a4d5e9 99 state = 7;
26b4fe82
AJ
100 }
101 }
f8a4d5e9 102 pc->state = state;
26b4fe82 103 return END_NOT_FOUND;
9aa1cfec
DP
104
105found:
f8a4d5e9
LB
106 pc->state = 7;
107 pc->frame_start_found = 0;
108 return i - (state & 5);
26b4fe82
AJ
109}
110
44d16df4 111static int scan_mmco_reset(AVCodecParserContext *s, GetBitContext *gb)
4baba6c8 112{
44d16df4
AK
113 H264PredWeightTable pwt;
114 int slice_type_nos = s->pict_type & 3;
9404a47a
AK
115 H264ParseContext *p = s->priv_data;
116 H264Context *h = &p->h;
a6e27f7a 117 int list_count, ref_count[2];
4baba6c8 118
4baba6c8 119
3176217c 120 if (p->ps.pps->redundant_pic_cnt_present)
44d16df4 121 get_ue_golomb(gb); // redundant_pic_count
4baba6c8 122
44d16df4
AK
123 if (slice_type_nos == AV_PICTURE_TYPE_B)
124 get_bits1(gb); // direct_spatial_mv_pred
e9f88441 125
3176217c 126 if (ff_h264_parse_ref_count(&list_count, ref_count, gb, p->ps.pps,
44d16df4 127 slice_type_nos, h->picture_structure) < 0)
4baba6c8
YN
128 return AVERROR_INVALIDDATA;
129
44d16df4 130 if (slice_type_nos != AV_PICTURE_TYPE_I) {
4baba6c8 131 int list;
a6e27f7a 132 for (list = 0; list < list_count; list++) {
44d16df4 133 if (get_bits1(gb)) {
4baba6c8
YN
134 int index;
135 for (index = 0; ; index++) {
44d16df4 136 unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(gb);
4baba6c8
YN
137
138 if (reordering_of_pic_nums_idc < 3)
44d16df4 139 get_ue_golomb(gb);
4baba6c8
YN
140 else if (reordering_of_pic_nums_idc > 3) {
141 av_log(h->avctx, AV_LOG_ERROR,
142 "illegal reordering_of_pic_nums_idc %d\n",
143 reordering_of_pic_nums_idc);
144 return AVERROR_INVALIDDATA;
145 } else
146 break;
147
a6e27f7a 148 if (index >= ref_count[list]) {
73e8fab3
VG
149 av_log(h->avctx, AV_LOG_ERROR,
150 "reference count %d overflow\n", index);
4baba6c8
YN
151 return AVERROR_INVALIDDATA;
152 }
153 }
154 }
155 }
156 }
157
3176217c
AK
158 if ((p->ps.pps->weighted_pred && slice_type_nos == AV_PICTURE_TYPE_P) ||
159 (p->ps.pps->weighted_bipred_idc == 1 && slice_type_nos == AV_PICTURE_TYPE_B))
160 ff_h264_pred_weight_table(gb, p->ps.sps, ref_count, slice_type_nos,
44d16df4 161 &pwt);
4baba6c8 162
44d16df4 163 if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
4baba6c8
YN
164 int i;
165 for (i = 0; i < MAX_MMCO_COUNT; i++) {
44d16df4 166 MMCOOpcode opcode = get_ue_golomb_31(gb);
4baba6c8
YN
167 if (opcode > (unsigned) MMCO_LONG) {
168 av_log(h->avctx, AV_LOG_ERROR,
169 "illegal memory management control operation %d\n",
170 opcode);
171 return AVERROR_INVALIDDATA;
172 }
173 if (opcode == MMCO_END)
174 return 0;
175 else if (opcode == MMCO_RESET)
176 return 1;
177
178 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
44d16df4 179 get_ue_golomb(gb);
4baba6c8
YN
180 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
181 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
44d16df4 182 get_ue_golomb_31(gb);
4baba6c8
YN
183 }
184 }
185
186 return 0;
187}
188
adbfc605 189/**
ff6474dd
IS
190 * Parse NAL units of found picture and decode some basic information.
191 *
192 * @param s parser context.
193 * @param avctx codec context.
194 * @param buf buffer with field/frame data.
195 * @param buf_size size of the buffer.
196 */
197static inline int parse_nal_units(AVCodecParserContext *s,
198 AVCodecContext *avctx,
199 const uint8_t *buf, int buf_size)
200{
9404a47a
AK
201 H264ParseContext *p = s->priv_data;
202 H264Context *h = &p->h;
ff6474dd 203 const uint8_t *buf_end = buf + buf_size;
8d0cc8ca
AK
204
205 H2645NAL nal = { NULL };
206
96c3da93 207 unsigned int pps_id;
ff6474dd 208 unsigned int slice_type;
4baba6c8 209 int state = -1, got_reset = 0;
3f1a7ceb 210 int field_poc[2];
8d0cc8ca 211 int ret;
ff6474dd
IS
212
213 /* set some sane default values */
f8a4d5e9
LB
214 s->pict_type = AV_PICTURE_TYPE_I;
215 s->key_frame = 0;
3f1a7ceb 216 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
ff6474dd 217
103d073d
YN
218 h->avctx = avctx;
219 ff_h264_reset_sei(h);
ff6474dd 220
9479415e
BC
221 if (!buf_size)
222 return 0;
223
f8a4d5e9 224 for (;;) {
3176217c 225 const SPS *sps;
8d0cc8ca 226 int src_length, consumed;
f1e93986 227 buf = avpriv_find_start_code(buf, buf_end, &state);
f8a4d5e9 228 if (buf >= buf_end)
ff6474dd
IS
229 break;
230 --buf;
231 src_length = buf_end - buf;
232 switch (state & 0x1f) {
233 case NAL_SLICE:
234 case NAL_IDR_SLICE:
235 // Do not walk the whole buffer just to decode slice header
19f53840 236 if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
4baba6c8
YN
237 /* IDR or disposable slice
238 * No need to decode many bytes because MMCOs shall not be present. */
239 if (src_length > 60)
240 src_length = 60;
241 } else {
242 /* To decode up to MMCOs */
243 if (src_length > 1000)
244 src_length = 1000;
245 }
ff6474dd
IS
246 break;
247 }
8d0cc8ca
AK
248
249 consumed = ff_h2645_extract_rbsp(buf, src_length, &nal);
250 if (consumed < 0)
ff6474dd
IS
251 break;
252
8d0cc8ca
AK
253 ret = init_get_bits(&nal.gb, nal.data, nal.size * 8);
254 if (ret < 0)
255 goto fail;
256 get_bits1(&nal.gb);
257 nal.ref_idc = get_bits(&nal.gb, 2);
258 nal.type = get_bits(&nal.gb, 5);
259
260 h->gb = nal.gb;
261 h->nal_ref_idc = nal.ref_idc;
262 h->nal_unit_type = nal.type;
263
f8a4d5e9 264 switch (h->nal_unit_type) {
ff6474dd 265 case NAL_SPS:
3176217c 266 ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps);
ff6474dd
IS
267 break;
268 case NAL_PPS:
3176217c
AK
269 ff_h264_decode_picture_parameter_set(&nal.gb, avctx, &p->ps,
270 nal.size_bits);
ff6474dd
IS
271 break;
272 case NAL_SEI:
273 ff_h264_decode_sei(h);
274 break;
275 case NAL_IDR_SLICE:
0ed260c7 276 s->key_frame = 1;
3f1a7ceb 277
c8dcff0c
AK
278 p->poc.prev_frame_num = 0;
279 p->poc.prev_frame_num_offset = 0;
280 p->poc.prev_poc_msb =
281 p->poc.prev_poc_lsb = 0;
f8a4d5e9 282 /* fall through */
ff6474dd 283 case NAL_SLICE:
44d16df4
AK
284 get_ue_golomb(&nal.gb); // skip first_mb_in_slice
285 slice_type = get_ue_golomb_31(&nal.gb);
a7da517f 286 s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
0ed260c7
IS
287 if (h->sei_recovery_frame_cnt >= 0) {
288 /* key frame, since recovery_frame_cnt is set */
289 s->key_frame = 1;
290 }
44d16df4 291 pps_id = get_ue_golomb(&nal.gb);
f8a4d5e9
LB
292 if (pps_id >= MAX_PPS_COUNT) {
293 av_log(h->avctx, AV_LOG_ERROR,
73e8fab3 294 "pps_id %u out of range\n", pps_id);
8d0cc8ca 295 goto fail;
96c3da93 296 }
3176217c 297 if (!p->ps.pps_list[pps_id]) {
f8a4d5e9 298 av_log(h->avctx, AV_LOG_ERROR,
73e8fab3 299 "non-existing PPS %u referenced\n", pps_id);
8d0cc8ca 300 goto fail;
96c3da93 301 }
3176217c
AK
302 p->ps.pps = (const PPS*)p->ps.pps_list[pps_id]->data;
303 if (!p->ps.sps_list[p->ps.pps->sps_id]) {
f8a4d5e9 304 av_log(h->avctx, AV_LOG_ERROR,
3176217c 305 "non-existing SPS %u referenced\n", p->ps.pps->sps_id);
8d0cc8ca 306 goto fail;
96c3da93 307 }
3176217c 308 p->ps.sps = (SPS*)p->ps.sps_list[p->ps.pps->sps_id]->data;
96c3da93 309
3176217c
AK
310 h->ps.sps = p->ps.sps;
311 h->ps.pps = p->ps.pps;
312 sps = p->ps.sps;
313
c8dcff0c 314 p->poc.frame_num = get_bits(&nal.gb, sps->log2_max_frame_num);
3176217c
AK
315
316 s->coded_width = 16 * sps->mb_width;
317 s->coded_height = 16 * sps->mb_height;
318 s->width = s->coded_width - (sps->crop_right + sps->crop_left);
319 s->height = s->coded_height - (sps->crop_top + sps->crop_bottom);
31d2039c
AK
320 if (s->width <= 0 || s->height <= 0) {
321 s->width = s->coded_width;
322 s->height = s->coded_height;
323 }
324
3176217c 325 switch (sps->bit_depth_luma) {
31d2039c
AK
326 case 9:
327 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P9;
328 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P9;
329 else s->format = AV_PIX_FMT_YUV420P9;
330 break;
331 case 10:
332 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P10;
333 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P10;
334 else s->format = AV_PIX_FMT_YUV420P10;
335 break;
336 case 8:
337 if (CHROMA444(h)) s->format = AV_PIX_FMT_YUV444P;
338 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P;
339 else s->format = AV_PIX_FMT_YUV420P;
340 break;
341 default:
342 s->format = AV_PIX_FMT_NONE;
343 }
344
3176217c
AK
345 avctx->profile = ff_h264_get_profile(sps);
346 avctx->level = sps->level_idc;
dd0cd3d2 347
3176217c 348 if (sps->frame_mbs_only_flag) {
f8a4d5e9
LB
349 h->picture_structure = PICT_FRAME;
350 } else {
44d16df4
AK
351 if (get_bits1(&nal.gb)) { // field_pic_flag
352 h->picture_structure = PICT_TOP_FIELD + get_bits1(&nal.gb); // bottom_field_flag
96c3da93 353 } else {
f8a4d5e9 354 h->picture_structure = PICT_FRAME;
96c3da93
IS
355 }
356 }
357
3f1a7ceb 358 if (h->nal_unit_type == NAL_IDR_SLICE)
44d16df4 359 get_ue_golomb(&nal.gb); /* idr_pic_id */
3176217c 360 if (sps->poc_type == 0) {
c8dcff0c 361 p->poc.poc_lsb = get_bits(&nal.gb, sps->log2_max_poc_lsb);
3f1a7ceb 362
3176217c 363 if (p->ps.pps->pic_order_present == 1 &&
f8a4d5e9 364 h->picture_structure == PICT_FRAME)
c8dcff0c 365 p->poc.delta_poc_bottom = get_se_golomb(&nal.gb);
3f1a7ceb
YN
366 }
367
3176217c
AK
368 if (sps->poc_type == 1 &&
369 !sps->delta_pic_order_always_zero_flag) {
c8dcff0c 370 p->poc.delta_poc[0] = get_se_golomb(&nal.gb);
3f1a7ceb 371
3176217c 372 if (p->ps.pps->pic_order_present == 1 &&
f8a4d5e9 373 h->picture_structure == PICT_FRAME)
c8dcff0c 374 p->poc.delta_poc[1] = get_se_golomb(&nal.gb);
3f1a7ceb
YN
375 }
376
4baba6c8
YN
377 /* Decode POC of this picture.
378 * The prev_ values needed for decoding POC of the next picture are not set here. */
b81dbd6c 379 field_poc[0] = field_poc[1] = INT_MAX;
c8dcff0c
AK
380 ff_h264_init_poc(field_poc, &s->output_picture_number, sps,
381 &p->poc, h->picture_structure, nal.ref_idc);
3f1a7ceb 382
4baba6c8
YN
383 /* Continue parsing to check if MMCO_RESET is present.
384 * FIXME: MMCO_RESET could appear in non-first slice.
385 * Maybe, we should parse all undisposable non-IDR slice of this
386 * picture until encountering MMCO_RESET in a slice of it. */
387 if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
44d16df4 388 got_reset = scan_mmco_reset(s, &nal.gb);
4baba6c8 389 if (got_reset < 0)
8d0cc8ca 390 goto fail;
4baba6c8
YN
391 }
392
b81dbd6c 393 /* Set up the prev_ values for decoding POC of the next picture. */
c8dcff0c
AK
394 p->poc.prev_frame_num = got_reset ? 0 : p->poc.frame_num;
395 p->poc.prev_frame_num_offset = got_reset ? 0 : p->poc.frame_num_offset;
b81dbd6c 396 if (h->nal_ref_idc != 0) {
4baba6c8 397 if (!got_reset) {
c8dcff0c
AK
398 p->poc.prev_poc_msb = p->poc.poc_msb;
399 p->poc.prev_poc_lsb = p->poc.poc_lsb;
4baba6c8 400 } else {
c8dcff0c
AK
401 p->poc.prev_poc_msb = 0;
402 p->poc.prev_poc_lsb =
4baba6c8
YN
403 h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
404 }
b81dbd6c
YN
405 }
406
3176217c 407 if (sps->pic_struct_present_flag) {
346db3ef 408 switch (h->sei_pic_struct) {
f8a4d5e9
LB
409 case SEI_PIC_STRUCT_TOP_FIELD:
410 case SEI_PIC_STRUCT_BOTTOM_FIELD:
411 s->repeat_pict = 0;
412 break;
413 case SEI_PIC_STRUCT_FRAME:
414 case SEI_PIC_STRUCT_TOP_BOTTOM:
415 case SEI_PIC_STRUCT_BOTTOM_TOP:
416 s->repeat_pict = 1;
417 break;
418 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
419 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
420 s->repeat_pict = 2;
421 break;
422 case SEI_PIC_STRUCT_FRAME_DOUBLING:
423 s->repeat_pict = 3;
424 break;
425 case SEI_PIC_STRUCT_FRAME_TRIPLING:
426 s->repeat_pict = 5;
427 break;
428 default:
429 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
430 break;
346db3ef
IS
431 }
432 } else {
2c541554 433 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
346db3ef
IS
434 }
435
3f1a7ceb
YN
436 if (h->picture_structure == PICT_FRAME) {
437 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
3176217c 438 if (sps->pic_struct_present_flag) {
3f1a7ceb 439 switch (h->sei_pic_struct) {
f8a4d5e9
LB
440 case SEI_PIC_STRUCT_TOP_BOTTOM:
441 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
442 s->field_order = AV_FIELD_TT;
443 break;
444 case SEI_PIC_STRUCT_BOTTOM_TOP:
445 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
446 s->field_order = AV_FIELD_BB;
447 break;
448 default:
449 s->field_order = AV_FIELD_PROGRESSIVE;
450 break;
3f1a7ceb
YN
451 }
452 } else {
453 if (field_poc[0] < field_poc[1])
454 s->field_order = AV_FIELD_TT;
455 else if (field_poc[0] > field_poc[1])
456 s->field_order = AV_FIELD_BB;
457 else
458 s->field_order = AV_FIELD_PROGRESSIVE;
459 }
460 } else {
461 if (h->picture_structure == PICT_TOP_FIELD)
462 s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
463 else
464 s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
465 s->field_order = AV_FIELD_UNKNOWN;
466 }
467
8d0cc8ca 468 av_freep(&nal.rbsp_buffer);
ff6474dd
IS
469 return 0; /* no need to evaluate the rest */
470 }
471 buf += consumed;
472 }
473 /* didn't find a picture! */
2c541554 474 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
8d0cc8ca
AK
475fail:
476 av_freep(&nal.rbsp_buffer);
ff6474dd
IS
477 return -1;
478}
479
26b4fe82
AJ
480static int h264_parse(AVCodecParserContext *s,
481 AVCodecContext *avctx,
482 const uint8_t **poutbuf, int *poutbuf_size,
483 const uint8_t *buf, int buf_size)
484{
9404a47a
AK
485 H264ParseContext *p = s->priv_data;
486 H264Context *h = &p->h;
487 ParseContext *pc = &p->pc;
26b4fe82
AJ
488 int next;
489
9404a47a
AK
490 if (!p->got_first) {
491 p->got_first = 1;
23584bec 492 if (avctx->extradata_size) {
2c541554 493 h->avctx = avctx;
790a367d
RT
494 // must be done like in the decoder.
495 // otherwise opening the parser, creating extradata,
496 // and then closing and opening again
497 // will cause has_b_frames to be always set.
498 // NB: estimate_timings_from_pts behaves exactly like this.
499 if (!avctx->has_b_frames)
2c541554 500 h->low_delay = 1;
23584bec
HC
501 ff_h264_decode_extradata(h);
502 }
503 }
504
f8a4d5e9
LB
505 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
506 next = buf_size;
507 } else {
9404a47a 508 next = h264_find_frame_end(p, buf, buf_size);
26b4fe82
AJ
509
510 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
f8a4d5e9 511 *poutbuf = NULL;
26b4fe82
AJ
512 *poutbuf_size = 0;
513 return buf_size;
514 }
515
f8a4d5e9
LB
516 if (next < 0 && next != END_NOT_FOUND) {
517 assert(pc->last_index + next >= 0);
9404a47a 518 h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
26b4fe82 519 }
a26ce1e2 520 }
ff6474dd 521
25f05ddb 522 parse_nal_units(s, avctx, buf, buf_size);
c733922e 523
25f05ddb
PL
524 if (h->sei_cpb_removal_delay >= 0) {
525 s->dts_sync_point = h->sei_buffering_period_present;
526 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
527 s->pts_dts_delta = h->sei_dpb_output_delay;
528 } else {
529 s->dts_sync_point = INT_MIN;
530 s->dts_ref_dts_delta = INT_MIN;
531 s->pts_dts_delta = INT_MIN;
532 }
533
534 if (s->flags & PARSER_FLAG_ONCE) {
535 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
536 }
26b4fe82 537
f8a4d5e9 538 *poutbuf = buf;
26b4fe82
AJ
539 *poutbuf_size = buf_size;
540 return next;
541}
542
543static int h264_split(AVCodecContext *avctx,
544 const uint8_t *buf, int buf_size)
545{
546 int i;
547 uint32_t state = -1;
f8a4d5e9
LB
548 int has_sps = 0;
549
550 for (i = 0; i <= buf_size; i++) {
551 if ((state & 0xFFFFFF1F) == 0x107)
552 has_sps = 1;
553 /* if((state&0xFFFFFF1F) == 0x101 ||
554 * (state&0xFFFFFF1F) == 0x102 ||
555 * (state&0xFFFFFF1F) == 0x105) {
556 * }
557 */
1b667269
JS
558 if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
559 (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
560 (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
561 (state & 0xFFFFFF1F) != 0x10f) {
f8a4d5e9
LB
562 if (has_sps) {
563 while (i > 4 && buf[i - 5] == 0)
564 i--;
565 return i - 4;
26b4fe82
AJ
566 }
567 }
f8a4d5e9
LB
568 if (i < buf_size)
569 state = (state << 8) | buf[i];
26b4fe82
AJ
570 }
571 return 0;
572}
573
8161220e 574static void h264_close(AVCodecParserContext *s)
3ee4f5e4 575{
9404a47a
AK
576 H264ParseContext *p = s->priv_data;
577 H264Context *h = &p->h;
578 ParseContext *pc = &p->pc;
3176217c 579 int i;
3ee4f5e4
MN
580
581 av_free(pc->buffer);
15861962 582 ff_h264_free_context(h);
3176217c
AK
583
584 for (i = 0; i < FF_ARRAY_ELEMS(p->ps.sps_list); i++)
585 av_buffer_unref(&p->ps.sps_list[i]);
586
587 for (i = 0; i < FF_ARRAY_ELEMS(p->ps.pps_list); i++)
588 av_buffer_unref(&p->ps.pps_list[i]);
3ee4f5e4
MN
589}
590
6fee1b90 591static av_cold int init(AVCodecParserContext *s)
e9ca315d 592{
9404a47a
AK
593 H264ParseContext *p = s->priv_data;
594 H264Context *h = &p->h;
92c6c2a6
AK
595
596 h->slice_ctx = av_mallocz(sizeof(*h->slice_ctx));
597 if (!h->slice_ctx)
598 return 0;
599 h->nb_slice_ctx = 1;
600
2c541554 601 h->slice_context_count = 1;
113aeee6 602 ff_h264dsp_init(&p->h264dsp, 8, 1);
e9ca315d
RC
603 return 0;
604}
26b4fe82 605
d36beb3f 606AVCodecParser ff_h264_parser = {
36ef5369 607 .codec_ids = { AV_CODEC_ID_H264 },
9404a47a 608 .priv_data_size = sizeof(H264ParseContext),
5511ad14
AK
609 .parser_init = init,
610 .parser_parse = h264_parse,
8161220e 611 .parser_close = h264_close,
5511ad14 612 .split = h264_split,
26b4fe82 613};