1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
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 *
12 * FFmpeg is distributed in the hope that it will be useful,
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
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG-4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include <stdint.h>
31
38
54
71
73 int buf_index, int next_avc)
74 {
76
78
79 return FFMIN(buf_index, buf_size);
80 }
81
83 int buf_size, void *logctx)
84 {
88
89 int next_avc =
p->is_avc ? 0 : buf_size;
90 // mb_addr= pc->mb_addr - 1;
94
95 if (
p->is_avc && !
p->nal_length_size)
97
98 for (
i = 0;
i < buf_size;
i++) {
102 for (j = 0; j <
p->nal_length_size; j++)
103 nalsize = (nalsize << 8) | buf[
i++];
104 if (!nalsize || nalsize > buf_size -
i) {
106 "remaining %d\n", nalsize, buf_size -
i);
107 return buf_size;
108 }
109 next_avc =
i + nalsize;
111 }
112
114 i +=
p->h264dsp.startcode_find_candidate(buf +
i, next_avc -
i);
117 }
else if (
state <= 2) {
119 state ^= 5;
// 2->7, 1->4, 0->5
122 else
123 state >>= 1;
// 2->1, 1->0, 0->0
124 }
else if (
state <= 5) {
125 int nalu_type = buf[
i] & 0x1F;
130 goto found;
131 }
135 continue;
136 }
138 } else {
139 unsigned int mb, last_mb =
p->parse_last_mb;
141 p->parse_history[
p->parse_history_count++] = buf[
i];
142
146 p->parse_last_mb =
mb;
149 i -=
p->parse_history_count - 1;
150 p->parse_history_count = 0;
151 goto found;
152 }
153 } else
155 p->parse_history_count = 0;
157 }
158 }
159 }
162 return next_avc;
164
165 found:
169 return next_avc;
171 }
172
174 void *logctx)
175 {
177 int slice_type_nos =
s->pict_type & 3;
179 int list_count, ref_count[2];
180
181
182 if (
p->ps.pps->redundant_pic_cnt_present)
184
187
189 slice_type_nos,
p->picture_structure, logctx) < 0)
191
199
200 if (reordering_of_pic_nums_idc < 3)
202 else if (reordering_of_pic_nums_idc > 3) {
204 "illegal reordering_of_pic_nums_idc %d\n",
205 reordering_of_pic_nums_idc);
207 } else
208 break;
209
212 "reference count %d overflow\n",
index);
214 }
215 }
216 }
217 }
218 }
219
223 &pwt,
p->picture_structure, logctx);
224
225 if (
get_bits1(gb)) {
// adaptive_ref_pic_marking_mode_flag
231 "illegal memory management control operation %d\n",
232 opcode);
234 }
236 return 0;
238 return 1;
239
245 }
246 }
247
248 return 0;
249 }
250
251 /**
252 * Parse NAL units of found picture and decode some basic information.
253 *
254 * @param s parser context.
255 * @param avctx codec context.
256 * @param buf buffer with field/frame data.
257 * @param buf_size size of the buffer.
258 */
261 const uint8_t * const buf, int buf_size)
262 {
266 int buf_index, next_avc;
267 unsigned int pps_id;
268 unsigned int slice_type;
269 int state = -1, got_reset = 0;
270 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
271 int field_poc[2];
273
274 /* set some sane default values */
278
280 p->sei.common.frame_packing.arrangement_cancel_flag = -1;
281 p->sei.common.unregistered.x264_build = -1;
282
283 if (!buf_size)
284 return 0;
285
289
290 buf_index = 0;
291 next_avc =
p->is_avc ? 0 : buf_size;
292 for (;;) {
294 int src_length, consumed, nalsize = 0;
295
296 if (buf_index >= next_avc) {
297 nalsize =
get_nalsize(
p->nal_length_size, buf, buf_size, &buf_index, avctx);
298 if (nalsize < 0)
299 break;
300 next_avc = buf_index + nalsize;
301 } else {
303 if (buf_index >= buf_size)
304 break;
305 if (buf_index >= next_avc)
306 continue;
307 }
308 src_length = next_avc - buf_index;
309
310 state = buf[buf_index];
311 switch (
state & 0x1f) {
314 // Do not walk the whole buffer just to decode slice header
316 /* IDR or disposable slice
317 * No need to decode many bytes because MMCOs shall not be present. */
318 if (src_length > 60)
319 src_length = 60;
320 } else {
321 /* To decode up to MMCOs */
322 if (src_length > 1000)
323 src_length = 1000;
324 }
325 break;
326 }
328 if (consumed < 0)
329 break;
330
331 buf_index += consumed;
332
339
343 break;
347 break;
350 break;
353
354 p->poc.prev_frame_num = 0;
355 p->poc.prev_frame_num_offset = 0;
356 p->poc.prev_poc_msb =
357 p->poc.prev_poc_lsb = 0;
358 /* fall through */
363 if (
p->sei.recovery_point.recovery_frame_cnt >= 0) {
364 /* key frame, since recovery_frame_cnt is set */
366 }
370 "pps_id %u out of range\n", pps_id);
372 }
373 if (!
p->ps.pps_list[pps_id]) {
375 "non-existing PPS %u referenced\n", pps_id);
377 }
378
380 p->ps.sps =
p->ps.pps->sps;
382
383 // heuristic to detect non marked keyframes
384 if (
p->ps.sps->ref_frame_count <= 1 &&
p->ps.pps->ref_count[0] <= 1 &&
s->pict_type ==
AV_PICTURE_TYPE_I)
386
388
389 s->coded_width = 16 *
sps->mb_width;
390 s->coded_height = 16 *
sps->mb_height;
391 s->width =
s->coded_width - (
sps->crop_right +
sps->crop_left);
392 s->height =
s->coded_height - (
sps->crop_top +
sps->crop_bottom);
393 if (
s->width <= 0 ||
s->height <= 0) {
394 s->width =
s->coded_width;
395 s->height =
s->coded_height;
396 }
397
398 switch (
sps->bit_depth_luma) {
399 case 9:
403 break;
404 case 10:
408 break;
409 case 8:
413 break;
414 default:
416 }
417
420
421 if (
sps->frame_mbs_only_flag) {
423 } else {
426 } else {
428 }
429 }
430
433 if (
sps->poc_type == 0) {
435
436 if (
p->ps.pps->pic_order_present == 1 &&
439 }
440
441 if (
sps->poc_type == 1 &&
442 !
sps->delta_pic_order_always_zero_flag) {
444
445 if (
p->ps.pps->pic_order_present == 1 &&
448 }
449
450 /* Decode POC of this picture.
451 * The prev_ values needed for decoding POC of the next picture are not set here. */
452 field_poc[0] = field_poc[1] = INT_MAX;
454 &
p->poc,
p->picture_structure, nal.
ref_idc);
457
458 /* Continue parsing to check if MMCO_RESET is present.
459 * FIXME: MMCO_RESET could appear in non-first slice.
460 * Maybe, we should parse all undisposable non-IDR slice of this
461 * picture until encountering MMCO_RESET in a slice of it. */
464 if (got_reset < 0)
466 }
467
468 /* Set up the prev_ values for decoding POC of the next picture. */
469 p->poc.prev_frame_num = got_reset ? 0 :
p->poc.frame_num;
470 p->poc.prev_frame_num_offset = got_reset ? 0 :
p->poc.frame_num_offset;
472 if (!got_reset) {
473 p->poc.prev_poc_msb =
p->poc.poc_msb;
474 p->poc.prev_poc_lsb =
p->poc.poc_lsb;
475 } else {
476 p->poc.prev_poc_msb = 0;
477 p->poc.prev_poc_lsb =
479 }
480 }
481
482 if (
p->sei.picture_timing.present) {
487 p->sei.picture_timing.present = 0;
488 }
489 }
490
491 if (
sps->pic_struct_present_flag &&
p->sei.picture_timing.present) {
492 switch (
p->sei.picture_timing.pic_struct) {
496 break;
501 break;
505 break;
508 break;
511 break;
512 default:
513 s->repeat_pict =
p->picture_structure ==
PICT_FRAME ? 1 : 0;
514 break;
515 }
516 } else {
517 s->repeat_pict =
p->picture_structure ==
PICT_FRAME ? 1 : 0;
518 }
519
522 if (
sps->pic_struct_present_flag &&
p->sei.picture_timing.present) {
523 switch (
p->sei.picture_timing.pic_struct) {
527 break;
531 break;
532 default:
534 break;
535 }
536 } else {
537 if (field_poc[0] < field_poc[1])
539 else if (field_poc[0] > field_poc[1])
541 else
543 }
544 } else {
547 else
549 if (
p->poc.frame_num ==
p->last_frame_num &&
552 p->last_picture_structure !=
s->picture_structure) {
555 else
557 } else {
559 }
560 p->last_picture_structure =
s->picture_structure;
561 p->last_frame_num =
p->poc.frame_num;
562 }
563 if (
sps->timing_info_present_flag) {
565 if (
p->sei.common.unregistered.x264_build < 44
U)
566 den *= 2;
568 sps->num_units_in_tick * 2, den, 1 << 30);
569 }
570
572 return 0; /* no need to evaluate the rest */
573 }
574 }
575 if (q264) {
577 return 0;
578 }
579 /* didn't find a picture! */
580 av_log(avctx,
AV_LOG_ERROR,
"missing picture in access unit with size %d\n", buf_size);
583 return -1;
584 }
585
588 const uint8_t **poutbuf, int *poutbuf_size,
589 const uint8_t *buf, int buf_size)
590 {
594 int next;
595
600 &
p->ps, &
p->is_avc, &
p->nal_length_size,
602 }
603 }
604
606 next = buf_size;
607 } else {
609
612 *poutbuf_size = 0;
613 return buf_size;
614 }
615
619 }
620 }
621
623
626 if (
p->sei.picture_timing.cpb_removal_delay >= 0) {
627 s->dts_sync_point =
p->sei.buffering_period.present;
628 s->dts_ref_dts_delta =
p->sei.picture_timing.cpb_removal_delay;
629 s->pts_dts_delta =
p->sei.picture_timing.dpb_output_delay;
630 } else {
631 s->dts_sync_point = INT_MIN;
632 s->dts_ref_dts_delta = INT_MIN;
633 s->pts_dts_delta = INT_MIN;
634 }
635
638 }
639
640 if (
s->dts_sync_point >= 0) {
645 // got DTS from the stream, update reference timestamp
648 // compute DTS based on reference timestamp
650 }
651
654
655 if (
s->dts_sync_point > 0)
656 p->reference_dts =
s->dts;
// new reference
657 }
658 }
659
660 *poutbuf = buf;
661 *poutbuf_size = buf_size;
662 return next;
663 }
664
666 {
669
671
674 }
675
677 {
679
681 p->last_frame_num = INT_MAX;
683 return 0;
684 }
685
692 };