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