1 /*
2 * MPEG1/2 demuxer
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
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
25
26 #if CONFIG_VOBSUB_DEMUXER
29 #endif
30
32
33 /*********************************************/
34 /* demux code */
35
36 #define MAX_SYNC_SIZE 100000
37
39 {
40 int pes1;
41 int pes2 = (p[3] & 0xC0) == 0x80 &&
42 (p[4] & 0xC0) != 0x40 &&
43 ((p[4] & 0xC0) == 0x00 ||
44 (p[4] & 0xC0) >> 2 == (p[6] & 0xF0));
45
46 for (p += 3; p < end && *p == 0xFF; p++) ;
47 if ((*p & 0xC0) == 0x40)
48 p += 2;
49
50 if ((*p & 0xF0) == 0x20)
51 pes1 = p[0] & p[2] & p[4] & 1;
52 else if ((*p & 0xF0) == 0x30)
53 pes1 = p[0] & p[2] & p[4] & p[5] & p[7] & p[9] & 1;
54 else
55 pes1 = *p == 0x0F;
56
57 return pes1 || pes2;
58 }
59
61 {
62 return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
63 }
64
66 {
67 uint32_t code = -1;
68 int i;
69 int sys = 0, pspack = 0, priv1 = 0, vid = 0;
70 int audio = 0, invalid = 0, score = 0;
71 int endpes = 0;
72
74 code = (code << 8) + p->
buf[i];
75 if ((code & 0xffffff00) == 0x100) {
76 int len = p->
buf[i + 1] << 8 | p->
buf[i + 2];
79
81 sys++;
83 pspack++;
84 else if ((code & 0xf0) ==
VIDEO_ID && pes) {
86 vid++;
87 }
88 // skip pes payload to avoid start code emulation for private
89 // and audio streams
90 else if ((code & 0xe0) ==
AUDIO_ID && pes) {audio++; i+=
len;}
92 else if (code == 0x1fd && pes) vid++; //VC1
93
94 else if ((code & 0xf0) ==
VIDEO_ID && !pes) invalid++;
95 else if ((code & 0xe0) ==
AUDIO_ID && !pes) invalid++;
97 }
98 }
99
100 if (vid + audio > invalid + 1) /* invalid VDR files nd short PES streams */
102
103 // av_log(NULL, AV_LOG_ERROR, "vid:%d aud:%d sys:%d pspack:%d invalid:%d size:%d \n",
104 // vid, audio, sys, pspack, invalid, p->buf_size);
105
106 if (sys > invalid && sys * 9 <= pspack * 10)
109 if (pspack > invalid && (priv1 + vid + audio) * 10 >= pspack * 9)
112 if ((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys &&
113 !pspack && p->
buf_size > 2048 && vid + audio > invalid)
/* PES stream */
116
117 // 02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
118 // mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
119 // Have\ Yourself\ a\ Merry\ Little\ Christmas.mp3 0 0 0 5 0 1 len:21618
120 return score;
121 }
122
129 #if CONFIG_VOBSUB_DEMUXER
132 #endif
134
136 {
140
143
145 if (!memcmp("IMKH", buffer, 4)) {
147 } else if (!memcmp("Sofdec", buffer, 6)) {
149 } else
151
152 /* no need to do more */
153 return 0;
154 }
155
157 {
159
162
164 }
165
168 {
171
172 state = *header_state;
173 n = *size_ptr;
174 while (n > 0) {
176 break;
178 n--;
179 if (state == 0x000001) {
180 state = ((state << 8) | v) & 0xffffff;
182 goto found;
183 }
184 state = ((state << 8) | v) & 0xffffff;
185 }
186 val = -1;
187
188 found:
189 *header_state =
state;
192 }
193
194 /**
195 * Extract stream types from a program stream map
196 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
197 *
198 * @return number of bytes occupied by PSM in the bitstream
199 */
201 {
202 int psm_length, ps_info_length, es_map_length;
203
208
209 /* skip program_stream_info */
212 /* Ignore es_map_length, trust psm_length */
213 es_map_length = psm_length - ps_info_length - 10;
214
215 /* at least one es available? */
216 while (es_map_length >= 4) {
218 unsigned char es_id =
avio_r8(pb);
220
221 /* remember mapping from stream id to stream type */
223 /* skip program_stream_info */
225 es_map_length -= 4 + es_info_length;
226 }
228 return 2 + psm_length;
229 }
230
231 /* read the next PES header. Return its position in ppos
232 * (if not NULL), and its start code, pts and dts.
233 */
235 int64_t *ppos, int *pstart_code,
236 int64_t *ppts, int64_t *pdts)
237 {
240 int pes_ext, ext2_len, id_ext, skip;
243
244 error_redo:
246 redo:
247 /* next start code (should be immediately after) */
252 if (startcode < 0) {
255 // FIXME we should remember header_state
257 }
258
260 goto redo;
262 goto redo;
265 goto redo;
266 }
269 /* Need to detect whether this from a DVD or a 'Sofdec' stream */
271 int bytesread = 0;
273
274 if (ps2buf) {
276
277 if (bytesread != len) {
279 } else {
281 if (len >= 6)
282 p = memchr(ps2buf, 'S', len - 5);
283
284 if (p)
285 m->
sofdec = !memcmp(p+1,
"ofdec", 5);
286
288
290 if (len == 980 && ps2buf[0] == 0) {
291 /* PCI structure? */
292 uint32_t startpts =
AV_RB32(ps2buf + 0x0d);
293 uint32_t endpts =
AV_RB32(ps2buf + 0x11);
294 uint8_t hours = ((ps2buf[0x19] >> 4) * 10) + (ps2buf[0x19] & 0x0f);
295 uint8_t mins = ((ps2buf[0x1a] >> 4) * 10) + (ps2buf[0x1a] & 0x0f);
296 uint8_t secs = ((ps2buf[0x1b] >> 4) * 10) + (ps2buf[0x1b] & 0x0f);
297
298 m->
dvd = (hours <= 23 &&
299 mins <= 59 &&
300 secs <= 59 &&
301 (ps2buf[0x19] & 0x0f) < 10 &&
302 (ps2buf[0x1a] & 0x0f) < 10 &&
303 (ps2buf[0x1b] & 0x0f) < 10 &&
304 endpts >= startpts);
305 } else if (len == 1018 && ps2buf[0] == 1) {
306 /* DSI structure? */
307 uint8_t hours = ((ps2buf[0x1d] >> 4) * 10) + (ps2buf[0x1d] & 0x0f);
308 uint8_t mins = ((ps2buf[0x1e] >> 4) * 10) + (ps2buf[0x1e] & 0x0f);
309 uint8_t secs = ((ps2buf[0x1f] >> 4) * 10) + (ps2buf[0x1f] & 0x0f);
310
311 m->
dvd = (hours <= 23 &&
312 mins <= 59 &&
313 secs <= 59 &&
314 (ps2buf[0x1d] & 0x0f) < 10 &&
315 (ps2buf[0x1e] & 0x0f) < 10 &&
316 (ps2buf[0x1f] & 0x0f) < 10);
317 }
318 }
319 }
320
322
323 /* If this isn't a DVD packet or no memory
324 * could be allocated, just ignore it.
325 * If we did, move back to the start of the
326 * packet (plus 'length' field) */
328 /* Skip back failed.
329 * This packet will be lost but that can't be helped
330 * if we can't skip back
331 */
332 goto redo;
333 }
334 } else {
335 /* No memory */
337 goto redo;
338 }
339 }
else if (!m->
dvd) {
342 goto redo;
343 }
344 }
347 goto redo;
348 }
349
350 /* find matching stream */
351 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
352 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
353 (startcode == 0x1bd) ||
355 (startcode == 0x1fd)))
356 goto redo;
357 if (ppos) {
359 }
361 pts =
364 {
365 /* stuffing */
366 for (;;) {
367 if (len < 1)
368 goto error_redo;
370 len--;
371 /* XXX: for mpeg1, should test only bit 7 */
372 if (c != 0xff)
373 break;
374 }
375 if ((c & 0xc0) == 0x40) {
376 /* buffer scale & size */
379 len -= 2;
380 }
381 if ((c & 0xe0) == 0x20) {
382 dts =
384 len -= 4;
385 if (c & 0x10) {
387 len -= 5;
388 }
389 } else if ((c & 0xc0) == 0x80) {
390 /* mpeg 2 PES */
393 len -= 2;
394 if (header_len > len)
395 goto error_redo;
396 len -= header_len;
397 if (flags & 0x80) {
399 header_len -= 5;
400 if (flags & 0x40) {
402 header_len -= 5;
403 }
404 }
405 if (flags & 0x3f && header_len == 0) {
406 flags &= 0xC0;
408 }
409 if (flags & 0x01) { /* PES extension */
411 header_len--;
412 /* Skip PES private data, program packet sequence counter
413 * and P-STD buffer */
414 skip = (pes_ext >> 4) & 0xb;
415 skip += skip & 0x9;
416 if (pes_ext & 0x40 || skip > header_len) {
418 pes_ext = skip = 0;
419 }
421 header_len -= skip;
422
423 if (pes_ext & 0x01) { /* PES extension 2 */
425 header_len--;
426 if ((ext2_len & 0x7f) > 0) {
428 if ((id_ext & 0x80) == 0)
429 startcode = ((startcode & 0xff) << 8) | id_ext;
430 header_len--;
431 }
432 }
433 }
434 if (header_len < 0)
435 goto error_redo;
437 } else if (c != 0xf)
438 goto redo;
439 }
440
443 len--;
444 }
445 if (len < 0)
446 goto error_redo;
448 int i;
451 s->
pb->
seekable /* index useless on streams anyway */) {
455 }
456 }
457 }
458
459 *pstart_code = startcode;
461 *pdts = dts;
463 }
464
467 {
470 int len, startcode, i, es_type,
ret;
471 int lpcm_header_len = -1; //Init to suppress warning
472 int request_probe= 0;
475 int64_t
pts, dts, dummy_pos;
// dummy_pos is needed for the index building to work
476
477 redo:
479 if (len < 0)
481
482 if (startcode >= 0x80 && startcode <= 0xcf) {
483 if (len < 4)
484 goto skip;
485
486 /* audio: skip header */
489 len -= 3;
490 if (startcode >= 0xb0 && startcode <= 0xbf) {
491 /* MLP/TrueHD audio has a 4-byte header */
493 len--;
494 }
495 }
496
497 /* now find stream */
500 if (st->
id == startcode)
501 goto found;
502 }
503
527 }
else if (m->
imkh_cctv && es_type == 0x91) {
530 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
531 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
532 unsigned char buf[8];
533
536 if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
538 else
539 request_probe= 1;
544 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
548 // Auto-detect AC-3
549 request_probe = 50;
550 } else {
552 }
553 } else if (startcode >= 0x80 && startcode <= 0x87) {
556 } else if ((startcode >= 0x88 && startcode <= 0x8f) ||
557 (startcode >= 0x98 && startcode <= 0x9f)) {
558 /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
561 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
563 if (lpcm_header_len == 6) {
565 } else {
567 }
568 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
571 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
572 /* Used for both AC-3 and E-AC-3 in EVOB files */
575 } else if (startcode >= 0x20 && startcode <= 0x3f) {
578 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
581 } else {
582 skip:
583 /* skip packet */
585 goto redo;
586 }
587 /* no stream found: add a new stream */
589 if (!st)
590 goto skip;
598 }
601
602 found:
604 goto skip;
605 if (startcode >= 0xa0 && startcode <= 0xaf) {
607 if (len < 6)
608 goto skip;
610 len -=6;
611 }
612 }
614
617 pkt->
pos = dummy_pos;
619 av_dlog(s,
"%d: pts=%0.3f dts=%0.3f size=%d\n",
622
623 return (ret < 0) ? ret : 0;
624 }
625
627 int64_t *ppos, int64_t pos_limit)
628 {
630 int64_t pos,
pts, dts;
631
632 pos = *ppos;
635
636 for (;;) {
638 if (len < 0) {
639 av_dlog(s,
"none (ret=%d)\n", len);
641 }
642 if (startcode == s->
streams[stream_index]->
id &&
644 break;
645 }
647 }
648 av_dlog(s,
"pos=0x%"PRIx64
" dts=0x%"PRIx64
" %0.3f\n",
649 pos, dts, dts / 90000.0);
650 *ppos = pos;
651 return dts;
652 }
653
663 };
664
665 #if CONFIG_VOBSUB_DEMUXER
666
667 #define REF_STRING "# VobSub index file,"
668 #define MAX_LINE_SIZE 2048
669
671 {
672 if (!strncmp(p->
buf, REF_STRING,
sizeof(REF_STRING) - 1))
674 return 0;
675 }
676
678 {
679 int i,
ret = 0, header_parsed = 0, langidx = 0;
681 char *sub_name =
NULL;
682 size_t fname_len;
683 char *ext, *header_str;
685 int64_t delay = 0;
687 int stream_id = -1;
688 char id[64] = {0};
691
693 fname_len = strlen(sub_name);
694 ext = sub_name - 3 + fname_len;
695 if (fname_len < 4 || *(ext - 1) != '.') {
697 "to guess the associated .SUB file\n");
700 }
701 memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3);
703
707 }
708
710 if (!vobsub->sub_ctx) {
713 }
714
717
719 if (ret < 0) {
722 }
723
728
729 if (!len)
730 break;
731
732 line[strcspn(line, "\r\n")] = 0;
733
734 if (!strncmp(line, "id:", 3)) {
735 if (sscanf(line, "id: %63[^,], index: %u", id, &stream_id) != 2) {
737 "assuming 'id: und, index: 0'\n", line);
738 strcpy(id, "und");
739 stream_id = 0;
740 }
741
746 }
747
748 header_parsed = 1;
749 alt[0] = '0円';
750 /* We do not create the stream immediately to avoid adding empty
751 * streams. See the following timestamp entry. */
752
754
755 } else if (!strncmp(line, "timestamp:", 10)) {
757 int hh, mm, ss, ms;
758 int64_t pos, timestamp;
759 const char *p = line + 10;
760
761 if (stream_id == -1) {
765 }
766
767 if (!st || st->
id != stream_id) {
769 if (!st) {
772 }
778 if (alt[0])
780 }
781
782 if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"SCNx64,
783 &hh, &mm, &ss, &ms, &pos) != 5) {
785 "abort parsing\n", line);
788 }
789 timestamp = (hh*3600LL + mm*60LL + ss) * 1000LL + ms + delay;
791
793 if (!sub) {
796 }
798 sub->
pts = timestamp;
800
801 } else if (!strncmp(line, "alt:", 4)) {
802 const char *p = line + 4;
803
804 while (*p == ' ')
805 p++;
808 header_parsed = 1;
809
810 } else if (!strncmp(line, "delay:", 6)) {
811 int sign = 1, hh = 0, mm = 0, ss = 0, ms = 0;
812 const char *p = line + 6;
813
814 while (*p == ' ')
815 p++;
816 if (*p == '-' || *p == '+') {
817 sign = *p == '-' ? -1 : 1;
818 p++;
819 }
820 sscanf(p, "%d:%d:%d:%d", &hh, &mm, &ss, &ms);
821 delay = ((hh*3600LL + mm*60LL + ss) * 1000LL + ms) * sign;
822
823 } else if (!strncmp(line, "langidx:", 8)) {
824 const char *p = line + 8;
825
826 if (sscanf(p, "%d", &langidx) != 1)
828
829 } else if (!header_parsed) {
830 if (line[0] && line[0] != '#')
832 }
833 }
834
837
841 }
842
847 }
853 }
855
859 }
860
862 {
866 int ret, psize, total_read = 0, i;
868
869 int64_t min_ts = INT64_MAX;
870 int sid = 0;
873 int64_t ts;
876 if (ts < min_ts) {
877 min_ts = ts;
878 sid = i;
879 }
880 }
881 q = &vobsub->q[sid];
883 if (ret < 0)
885
886 /* compute maximum packet size using the next packet position. This is
887 * useful when the len in the header is non-sense */
890 } else {
892 psize = fsize < 0 ? 0xffff : fsize - idx_pkt.
pos;
893 }
894
896
900
901 do {
902 int n, to_read, startcode;
904 int64_t old_pos =
avio_tell(pb), new_pos;
905 int pkt_size;
906
908 if (ret < 0) {
909 if (pkt->
size)
// raise packet even if incomplete
910 break;
911 goto fail;
912 }
913 to_read = ret & 0xffff;
915 pkt_size = ret + (new_pos - old_pos);
916
917 /* this prevents reads above the current packet */
918 if (total_read + pkt_size > psize)
919 break;
920 total_read += pkt_size;
921
922 /* the current chunk doesn't match the stream index (unlikely) */
924 break;
925
927 if (ret < 0)
928 goto fail;
929
931 if (n < to_read)
932 pkt->
size -= to_read -
n;
933 } while (total_read < psize);
934
938
940 return 0;
941
942 fail:
946 }
947
949 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
950 {
952
953 /* Rescale requested timestamps based on the first stream (timebase is the
954 * same for all subtitles stream within a .idx/.sub). Rescaling is done just
955 * like in avformat_seek_file(). */
956 if (stream_index == -1 && s->
nb_streams != 1) {
964 time_base.
num * (int64_t)AV_TIME_BASE,
968 min_ts, ts, max_ts, flags);
969 if (r < 0)
971 }
973 }
974
975 if (stream_index == -1) // only 1 stream
976 stream_index = 0;
978 min_ts, ts, max_ts, flags);
979 }
980
982 {
983 int i;
985
988 if (vobsub->sub_ctx)
990 return 0;
991 }
992
1000 .read_seek2 = vobsub_read_seek,
1003 .extensions = "idx",
1004 };
1005 #endif