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