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 / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
36
42
43
45 int buf_size)
46 {
48 int i, j;
51
52 int next_avc= h->
is_avc ? 0 : buf_size;
53 // mb_addr= pc->mb_addr - 1;
55 if (state > 13)
56 state = 7;
57
60
61 for (i = 0; i < buf_size; i++) {
62 if (i >= next_avc) {
63 int nalsize = 0;
64 i = next_avc;
66 nalsize = (nalsize << 8) | buf[i++];
67 if (nalsize <= 0 || nalsize > buf_size - i) {
69 return buf_size;
70 }
71 next_avc = i + nalsize;
72 state = 5;
73 }
74
75 if (state == 7) {
77 if (i < next_avc)
78 state = 2;
79 } else if (state <= 2) {
80 if (buf[i] == 1)
81 state ^= 5; // 2->7, 1->4, 0->5
82 else if (buf[i])
83 state = 7;
84 else
85 state >>= 1; // 2->1, 1->0, 0->0
86 } else if (state <= 5) {
87 int nalu_type = buf[i] & 0x1F;
91 i++;
92 goto found;
93 }
96 state += 8;
97 continue;
98 }
99 state = 7;
100 } else {
105
111 if (mb <= last_mb)
112 goto found;
113 } else
115 state = 7;
116 }
117 }
118 }
121 return next_avc;
123
124 found:
128 return next_avc;
129 return i - (state & 5) - 5 * (state > 7);
130 }
131
133 {
136
138
141
144
146 int list;
147 for (list = 0; list < h->
list_count; list++) {
150 for (index = 0; ; index++) {
152
153 if (reordering_of_pic_nums_idc < 3)
155 else if (reordering_of_pic_nums_idc > 3) {
157 "illegal reordering_of_pic_nums_idc %d\n",
158 reordering_of_pic_nums_idc);
160 } else
161 break;
162
165 "reference count %d overflow\n", index);
167 }
168 }
169 }
170 }
171 }
172
176
177 if (
get_bits1(&h->
gb)) {
// adaptive_ref_pic_marking_mode_flag
178 int i;
183 "illegal memory management control operation %d\n",
184 opcode);
186 }
188 return 0;
190 return 1;
191
197 }
198 }
199
200 return 0;
201 }
202
203 /**
204 * Parse NAL units of found picture and decode some basic information.
205 *
206 * @param s parser context.
207 * @param avctx codec context.
208 * @param buf buffer with field/frame data.
209 * @param buf_size size of the buffer.
210 */
214 {
217 int buf_index, next_avc;
218 unsigned int pps_id;
219 unsigned int slice_type;
220 int state = -1, got_reset = 0;
222 int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
223 int field_poc[2];
224
225 /* set some sane default values */
229
233
234 if (!buf_size)
235 return 0;
236
237 buf_index = 0;
238 next_avc = h->
is_avc ? 0 : buf_size;
239 for (;;) {
240 int src_length, dst_length, consumed, nalsize = 0;
241
242 if (buf_index >= next_avc) {
244 if (nalsize < 0)
245 break;
246 next_avc = buf_index + nalsize;
247 } else {
249 if (buf_index >= buf_size)
250 break;
251 if (buf_index >= next_avc)
252 continue;
253 }
254 src_length = next_avc - buf_index;
255
256 state = buf[buf_index];
257 switch (state & 0x1f) {
260 // Do not walk the whole buffer just to decode slice header
261 if ((state & 0x1f) ==
NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
262 /* IDR or disposable slice
263 * No need to decode many bytes because MMCOs shall not be present. */
264 if (src_length > 60)
265 src_length = 60;
266 } else {
267 /* To decode up to MMCOs */
268 if (src_length > 1000)
269 src_length = 1000;
270 }
271 break;
272 }
274 &consumed, src_length);
275 if (!ptr || dst_length < 0)
276 break;
277
278 buf_index += consumed;
279
284 break;
287 break;
290 break;
293
298 /* fall through */
304 /* key frame, since recovery_frame_cnt is set */
306 }
310 "pps_id %u out of range\n", pps_id);
311 return -1;
312 }
315 "non-existing PPS %u referenced\n", pps_id);
316 return -1;
317 }
321 "non-existing SPS %u referenced\n", h->
pps.
sps_id);
322 return -1;
323 }
326
329
337 }
338
340 case 9:
344 break;
345 case 10:
349 break;
350 case 8:
354 break;
355 default:
357 }
358
361
364 } else {
367 } else {
369 }
370 }
371
376
380 }
381
385
389 }
390
391 /* Decode POC of this picture.
392 * The prev_ values needed for decoding POC of the next picture are not set here. */
393 field_poc[0] = field_poc[1] = INT_MAX;
395
396 /* Continue parsing to check if MMCO_RESET is present.
397 * FIXME: MMCO_RESET could appear in non-first slice.
398 * Maybe, we should parse all undisposable non-IDR slice of this
399 * picture until encountering MMCO_RESET in a slice of it. */
402 if (got_reset < 0)
403 return got_reset;
404 }
405
406 /* Set up the prev_ values for decoding POC of the next picture. */
410 if (!got_reset) {
413 } else {
417 }
418 }
419
425 break;
430 break;
434 break;
437 break;
440 break;
441 default:
443 break;
444 }
445 } else {
447 }
448
456 break;
460 break;
461 default:
463 break;
464 }
465 } else {
466 if (field_poc[0] < field_poc[1])
468 else if (field_poc[0] > field_poc[1])
470 else
472 }
473 } else {
476 else
479 }
480
481 return 0; /* no need to evaluate the rest */
482 }
483 }
484 if (q264)
485 return 0;
486 /* didn't find a picture! */
488 return -1;
489 }
490
493 const uint8_t **poutbuf,
int *poutbuf_size,
495 {
499 int next;
500
505 // must be done like in decoder, otherwise opening the parser,
506 // letting it create extradata and then closing and opening again
507 // will cause has_b_frames to be always set.
508 // Note that estimate_timings_from_pts does exactly this.
512 }
513 }
514
516 next = buf_size;
517 } else {
519
522 *poutbuf_size = 0;
523 return buf_size;
524 }
525
529 }
530 }
531
533
536 if (h->sei_cpb_removal_delay >= 0) {
537 s->dts_sync_point = h->sei_buffering_period_present;
538 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
539 s->pts_dts_delta = h->sei_dpb_output_delay;
540 } else {
541 s->dts_sync_point = INT_MIN;
542 s->dts_ref_dts_delta = INT_MIN;
543 s->pts_dts_delta = INT_MIN;
544 }
545
548 }
549
551 *poutbuf_size = buf_size;
552 return next;
553 }
554
557 {
559 int has_sps = 0;
560 int has_pps = 0;
562 int nalu_type;
563
564 while (ptr < end) {
566 if ((state & 0xFFFFFF00) != 0x100)
567 break;
568 nalu_type = state & 0x1F;
570 has_sps = 1;
571 }
else if (nalu_type ==
NAL_PPS)
572 has_pps = 1;
573 /* else if (nalu_type == 0x01 ||
574 * nalu_type == 0x02 ||
575 * nalu_type == 0x05) {
576 * }
577 */
578 else if ((nalu_type !=
NAL_SEI || has_pps) &&
580 nalu_type != 0x0f) {
581 if (has_sps) {
582 while (ptr - 4 > buf && ptr[-5] == 0)
583 ptr--;
584 return ptr - 4 -
buf;
585 }
586 }
587 }
588
589 return 0;
590 }
591
593 {
597
600 }
601
603 {
609 return 0;
610 }
611
619 };