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