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