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
37
52
69
71 int buf_index, int next_avc)
72 {
74
76
77 return FFMIN(buf_index, buf_size);
78 }
79
81 int buf_size, void *logctx)
82 {
86
87 int next_avc = p->
is_avc ? 0 : buf_size;
88 // mb_addr= pc->mb_addr - 1;
92
95
96 for (
i = 0;
i < buf_size;
i++) {
101 nalsize = (nalsize << 8) | buf[
i++];
102 if (!nalsize || nalsize > buf_size -
i) {
104 "remaining %d\n", nalsize, buf_size -
i);
105 return buf_size;
106 }
107 next_avc =
i + nalsize;
109 }
110
115 }
else if (
state <= 2) {
117 state ^= 5;
// 2->7, 1->4, 0->5
120 else
121 state >>= 1;
// 2->1, 1->0, 0->0
122 }
else if (
state <= 5) {
123 int nalu_type = buf[
i] & 0x1F;
128 goto found;
129 }
133 continue;
134 }
136 } else {
140
149 goto found;
150 }
151 } else
155 }
156 }
157 }
160 return next_avc;
162
163 found:
167 return next_avc;
169 }
170
172 void *logctx)
173 {
175 int slice_type_nos =
s->pict_type & 3;
177 int list_count, ref_count[2];
178
179
182
185
189
197
198 if (reordering_of_pic_nums_idc < 3)
200 else if (reordering_of_pic_nums_idc > 3) {
202 "illegal reordering_of_pic_nums_idc %d\n",
203 reordering_of_pic_nums_idc);
205 } else
206 break;
207
210 "reference count %d overflow\n",
index);
212 }
213 }
214 }
215 }
216 }
217
222
223 if (
get_bits1(gb)) {
// adaptive_ref_pic_marking_mode_flag
229 "illegal memory management control operation %d\n",
230 opcode);
232 }
234 return 0;
236 return 1;
237
243 }
244 }
245
246 return 0;
247 }
248
249 /**
250 * Parse NAL units of found picture and decode some basic information.
251 *
252 * @param s parser context.
253 * @param avctx codec context.
254 * @param buf buffer with field/frame data.
255 * @param buf_size size of the buffer.
256 */
259 const uint8_t * const buf, int buf_size)
260 {
264 int buf_index, next_avc;
265 unsigned int pps_id;
266 unsigned int slice_type;
267 int state = -1, got_reset = 0;
268 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
269 int field_poc[2];
271
272 /* set some sane default values */
276
280
281 if (!buf_size)
282 return 0;
283
287
288 buf_index = 0;
289 next_avc = p->
is_avc ? 0 : buf_size;
290 for (;;) {
292 int src_length, consumed, nalsize = 0;
293
294 if (buf_index >= next_avc) {
296 if (nalsize < 0)
297 break;
298 next_avc = buf_index + nalsize;
299 } else {
301 if (buf_index >= buf_size)
302 break;
303 if (buf_index >= next_avc)
304 continue;
305 }
306 src_length = next_avc - buf_index;
307
308 state = buf[buf_index];
309 switch (
state & 0x1f) {
312 // Do not walk the whole buffer just to decode slice header
314 /* IDR or disposable slice
315 * No need to decode many bytes because MMCOs shall not be present. */
316 if (src_length > 60)
317 src_length = 60;
318 } else {
319 /* To decode up to MMCOs */
320 if (src_length > 1000)
321 src_length = 1000;
322 }
323 break;
324 }
326 if (consumed < 0)
327 break;
328
329 buf_index += consumed;
330
337
341 break;
345 break;
348 break;
351
356 /* fall through */
362 /* key frame, since recovery_frame_cnt is set */
364 }
368 "pps_id %u out of range\n", pps_id);
370 }
373 "non-existing PPS %u referenced\n", pps_id);
375 }
376
380
381 // heuristic to detect non marked keyframes
384
386
387 s->coded_width = 16 *
sps->mb_width;
388 s->coded_height = 16 *
sps->mb_height;
389 s->width =
s->coded_width - (
sps->crop_right +
sps->crop_left);
390 s->height =
s->coded_height - (
sps->crop_top +
sps->crop_bottom);
391 if (
s->width <= 0 ||
s->height <= 0) {
392 s->width =
s->coded_width;
393 s->height =
s->coded_height;
394 }
395
396 switch (
sps->bit_depth_luma) {
397 case 9:
401 break;
402 case 10:
406 break;
407 case 8:
411 break;
412 default:
414 }
415
418
419 if (
sps->frame_mbs_only_flag) {
421 } else {
424 } else {
426 }
427 }
428
431 if (
sps->poc_type == 0) {
433
437 }
438
439 if (
sps->poc_type == 1 &&
440 !
sps->delta_pic_order_always_zero_flag) {
442
446 }
447
448 /* Decode POC of this picture.
449 * The prev_ values needed for decoding POC of the next picture are not set here. */
450 field_poc[0] = field_poc[1] = INT_MAX;
455
456 /* Continue parsing to check if MMCO_RESET is present.
457 * FIXME: MMCO_RESET could appear in non-first slice.
458 * Maybe, we should parse all undisposable non-IDR slice of this
459 * picture until encountering MMCO_RESET in a slice of it. */
462 if (got_reset < 0)
464 }
465
466 /* Set up the prev_ values for decoding POC of the next picture. */
470 if (!got_reset) {
473 } else {
477 }
478 }
479
486 }
487 }
488
494 break;
499 break;
503 break;
506 break;
509 break;
510 default:
512 break;
513 }
514 } else {
516 }
517
525 break;
529 break;
530 default:
532 break;
533 }
534 } else {
535 if (field_poc[0] < field_poc[1])
537 else if (field_poc[0] > field_poc[1])
539 else
541 }
542 } else {
545 else
553 else
555 } else {
557 }
560 }
561 if (
sps->timing_info_present_flag) {
564 den *= 2;
566 sps->num_units_in_tick * 2, den, 1 << 30);
567 }
568
570 return 0; /* no need to evaluate the rest */
571 }
572 }
573 if (q264) {
575 return 0;
576 }
577 /* didn't find a picture! */
578 av_log(avctx,
AV_LOG_ERROR,
"missing picture in access unit with size %d\n", buf_size);
581 return -1;
582 }
583
586 const uint8_t **poutbuf, int *poutbuf_size,
587 const uint8_t *buf, int buf_size)
588 {
592 int next;
593
600 }
601 }
602
604 next = buf_size;
605 } else {
607
610 *poutbuf_size = 0;
611 return buf_size;
612 }
613
617 }
618 }
619
621
628 } else {
629 s->dts_sync_point = INT_MIN;
630 s->dts_ref_dts_delta = INT_MIN;
631 s->pts_dts_delta = INT_MIN;
632 }
633
636 }
637
638 if (
s->dts_sync_point >= 0) {
643 // got DTS from the stream, update reference timestamp
646 // compute DTS based on reference timestamp
648 }
649
652
653 if (
s->dts_sync_point > 0)
655 }
656 }
657
658 *poutbuf = buf;
659 *poutbuf_size = buf_size;
660 return next;
661 }
662
664 {
667
669
672 }
673
675 {
677
681 return 0;
682 }
683
690 };