1 /*
2 * MPEG-1/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
27
28 /*********************************************/
29 /* demux code */
30
31 #define MAX_SYNC_SIZE 100000
32
33 static int check_pes(
const uint8_t *p,
const uint8_t *end)
34 {
35 int pes1;
36 int pes2 = (p[3] & 0xC0) == 0x80 &&
37 (p[4] & 0xC0) != 0x40 &&
38 ((p[4] & 0xC0) == 0x00 ||
39 (p[4] & 0xC0) >> 2 == (p[6] & 0xF0));
40
41 for (p += 3; p < end && *p == 0xFF; p++) ;
42 if ((*p & 0xC0) == 0x40)
43 p += 2;
44
45 if ((*p & 0xF0) == 0x20)
46 pes1 = p[0] & p[2] & p[4] & 1;
47 else if ((*p & 0xF0) == 0x30)
48 pes1 = p[0] & p[2] & p[4] & p[5] & p[7] & p[9] & 1;
49 else
50 pes1 = *p == 0x0F;
51
52 return pes1 || pes2;
53 }
54
56 {
57 return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
58 }
59
61 {
64 int sys = 0, pspack = 0, priv1 = 0, vid = 0;
65 int audio = 0, invalid = 0, score = 0;
66 int endpes = 0;
67
70 if ((
code & 0xffffff00) == 0x100) {
74
76 sys++;
78 pspack++;
81 vid++;
82 }
83 // skip pes payload to avoid start code emulation for private
84 // and audio streams
87 else if (
code == 0x1fd && pes) vid++;
//VC1
88
92 }
93 }
94
95 if (vid + audio > invalid + 1) /* invalid VDR files nd short PES streams */
97
98 // av_log(NULL, AV_LOG_ERROR, "vid:%d aud:%d sys:%d pspack:%d invalid:%d size:%d \n",
99 // vid, audio, sys, pspack, invalid, p->buf_size);
100
101 if (sys > invalid && sys * 9 <= pspack * 10)
104 if (pspack > invalid && (priv1 + vid + audio) * 10 >= pspack * 9)
107 if ((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys &&
108 !pspack && p->
buf_size > 2048 && vid + audio > invalid)
/* PES stream */
111
112 // 02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
113 // mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
114 // Have\ Yourself\ a\ Merry\ Little\ Christmas.mp3 0 0 0 5 0 1 len:21618
115 return score;
116 }
117
126
128 {
132
135
137 if (!memcmp(
"IMKH",
buffer, 4)) {
139 }
else if (!memcmp(
"Sofdec",
buffer, 6)) {
141 } else
143
144 /* no need to do more */
145 return 0;
146 }
147
149 {
150 uint8_t buf[5];
152
157
159 }
160
163 {
164 unsigned int state, v;
166
167 state = *header_state;
168 n = *size_ptr;
169 while (n > 0) {
171 break;
173 n--;
174 if (
state == 0x000001) {
177 goto found;
178 }
180 }
182
183 found:
184 *header_state =
state;
185 *size_ptr = n;
187 }
188
189 /**
190 * Extract stream types from a program stream map
191 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
192 *
193 * @return number of bytes occupied by PSM in the bitstream
194 */
196 {
197 int psm_length, ps_info_length, es_map_length;
198
203
204 /* skip program_stream_info */
207 /* Ignore es_map_length, trust psm_length */
208 es_map_length = psm_length - ps_info_length - 10;
209
210 /* at least one es available? */
211 while (es_map_length >= 4) {
213 unsigned char es_id =
avio_r8(pb);
215
216 /* remember mapping from stream id to stream type */
218 /* skip program_stream_info */
220 es_map_length -= 4 + es_info_length;
221 }
223 return 2 + psm_length;
224 }
225
226 /* read the next PES header. Return its position in ppos
227 * (if not NULL), and its start code, pts and dts.
228 */
230 int64_t *ppos, int *pstart_code,
231 int64_t *ppts, int64_t *pdts)
232 {
235 int pes_ext, ext2_len, id_ext, skip;
238
239 error_redo:
241 redo:
242 /* next start code (should be immediately after) */
247 if (startcode < 0) {
250 // FIXME we should remember header_state
252 }
253
255 goto redo;
257 goto redo;
260 goto redo;
261 }
264 /* Need to detect whether this from a DVD or a 'Sofdec' stream */
266 int bytesread = 0;
268
269 if (ps2buf) {
271
272 if (bytesread !=
len) {
274 } else {
275 uint8_t *p = 0;
277 p = memchr(ps2buf,
'S',
len - 5);
278
279 if (p)
280 m->
sofdec = !memcmp(p+1,
"ofdec", 5);
281
283
285 if (
len == 980 && ps2buf[0] == 0) {
286 /* PCI structure? */
287 uint32_t startpts =
AV_RB32(ps2buf + 0x0d);
288 uint32_t endpts =
AV_RB32(ps2buf + 0x11);
289 uint8_t hours = ((ps2buf[0x19] >> 4) * 10) + (ps2buf[0x19] & 0x0f);
290 uint8_t mins = ((ps2buf[0x1a] >> 4) * 10) + (ps2buf[0x1a] & 0x0f);
291 uint8_t secs = ((ps2buf[0x1b] >> 4) * 10) + (ps2buf[0x1b] & 0x0f);
292
293 m->
dvd = (hours <= 23 &&
294 mins <= 59 &&
295 secs <= 59 &&
296 (ps2buf[0x19] & 0x0f) < 10 &&
297 (ps2buf[0x1a] & 0x0f) < 10 &&
298 (ps2buf[0x1b] & 0x0f) < 10 &&
299 endpts >= startpts);
300 }
else if (
len == 1018 && ps2buf[0] == 1) {
301 /* DSI structure? */
302 uint8_t hours = ((ps2buf[0x1d] >> 4) * 10) + (ps2buf[0x1d] & 0x0f);
303 uint8_t mins = ((ps2buf[0x1e] >> 4) * 10) + (ps2buf[0x1e] & 0x0f);
304 uint8_t secs = ((ps2buf[0x1f] >> 4) * 10) + (ps2buf[0x1f] & 0x0f);
305
306 m->
dvd = (hours <= 23 &&
307 mins <= 59 &&
308 secs <= 59 &&
309 (ps2buf[0x1d] & 0x0f) < 10 &&
310 (ps2buf[0x1e] & 0x0f) < 10 &&
311 (ps2buf[0x1f] & 0x0f) < 10);
312 }
313 }
314 }
315
317
318 /* If this isn't a DVD packet or no memory
319 * could be allocated, just ignore it.
320 * If we did, move back to the start of the
321 * packet (plus 'length' field) */
323 /* Skip back failed.
324 * This packet will be lost but that can't be helped
325 * if we can't skip back
326 */
327 goto redo;
328 }
329 } else {
330 /* No memory */
332 goto redo;
333 }
334 }
else if (!m->
dvd) {
337 goto redo;
338 }
339 }
342 goto redo;
343 }
344
345 /* find matching stream */
346 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
347 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
348 (startcode == 0x1bd) ||
350 (startcode == 0x1fd)))
351 goto redo;
352 if (ppos) {
354 }
359 {
360 /* stuffing */
361 for (;;) {
363 goto error_redo;
366 /* XXX: for MPEG-1, should test only bit 7 */
368 break;
369 }
370 if ((
c & 0xc0) == 0x40) {
371 /* buffer scale & size */
375 }
376 if ((
c & 0xe0) == 0x20) {
377 dts =
383 }
384 }
else if ((
c & 0xc0) == 0x80) {
385 /* mpeg 2 PES */
389 if (header_len >
len)
390 goto error_redo;
394 header_len -= 5;
397 header_len -= 5;
398 }
399 }
400 if (
flags & 0x3f && header_len == 0) {
403 }
404 if (
flags & 0x01) {
/* PES extension */
406 header_len--;
407 /* Skip PES private data, program packet sequence counter
408 * and P-STD buffer */
409 skip = (pes_ext >> 4) & 0xb;
410 skip += skip & 0x9;
411 if (pes_ext & 0x40 || skip > header_len) {
413 pes_ext = skip = 0;
414 }
416 header_len -= skip;
417
418 if (pes_ext & 0x01) { /* PES extension 2 */
420 header_len--;
421 if ((ext2_len & 0x7f) > 0) {
423 if ((id_ext & 0x80) == 0)
424 startcode = ((startcode & 0xff) << 8) | id_ext;
425 header_len--;
426 }
427 }
428 }
429 if (header_len < 0)
430 goto error_redo;
433 goto redo;
434 }
435
438
441
444 if (startcode == 0x0b) {
446 startcode = 0x80;
449 } else {
451 }
452 } else {
454 }
455 }
457 goto error_redo;
460 for (
i = 0;
i <
s->nb_streams;
i++) {
461 if (startcode ==
s->streams[
i]->id &&
466 }
467 }
468 }
469
470 *pstart_code = startcode;
472 *pdts = dts;
474 }
475
478 {
482 int len, startcode,
i, es_type,
ret;
483 int pcm_dvd = 0;
484 int request_probe= 0;
487 int64_t
pts, dts, dummy_pos;
// dummy_pos is needed for the index building to work
488
489 redo:
493
494 if (startcode >= 0x80 && startcode <= 0xcf) {
496 goto skip;
497
499 /* audio: skip header */
502 if (startcode >= 0xb0 && startcode <= 0xbf) {
503 /* MLP/TrueHD audio has a 4-byte header */
506 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
512 }
513 }
514 }
515
516 /* now find stream */
517 for (
i = 0;
i <
s->nb_streams;
i++) {
519 if (st->
id == startcode)
520 goto found;
521 }
522
549 }
else if (m->
imkh_cctv && es_type == 0x91) {
552 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
553 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
554 unsigned char buf[8];
555
558 if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
560 else
561 request_probe= 1;
566 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
570 // Auto-detect AC-3
571 request_probe = 50;
572 }
else if (m->
imkh_cctv && startcode == 0x1c0 &&
len > 80) {
574 request_probe = 50;
575 } else {
578 request_probe = 25;
579 }
580 } else if (startcode >= 0x80 && startcode <= 0x87) {
583 } else if ((startcode >= 0x88 && startcode <= 0x8f) ||
584 (startcode >= 0x98 && startcode <= 0x9f)) {
585 /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
588 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
590 if (!pcm_dvd) {
592 } else {
594 }
595 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
598 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
599 /* Used for both AC-3 and E-AC-3 in EVOB files */
602 } else if (startcode >= 0x20 && startcode <= 0x3f) {
605 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
608 } else {
609 skip:
610 /* skip packet */
612 goto redo;
613 }
614 /* no stream found: add a new stream */
616 if (!st)
617 goto skip;
627 }
630
631 found:
633 goto skip;
634 if (startcode >= 0xa0 && startcode <= 0xaf) {
637 goto skip;
640 }
641 }
643
648
653
654 return (
ret < 0) ?
ret : 0;
655 }
656
658 int64_t *ppos, int64_t pos_limit)
659 {
662
666
667 for (;;) {
673 }
674 if (startcode ==
s->streams[stream_index]->id &&
676 break;
677 }
679 }
682 pos, dts, dts / 90000.0);
684 return dts;
685 }
686
696 };
697
698 #if CONFIG_VOBSUB_DEMUXER
699
704
705 #define REF_STRING "# VobSub index file,"
706 #define MAX_LINE_SIZE 2048
707
708 typedef struct VobSubDemuxContext {
712 char *sub_name;
713 } VobSubDemuxContext;
714
716 {
717 if (!strncmp(p->
buf, REF_STRING,
sizeof(REF_STRING) - 1))
719 return 0;
720 }
721
723 {
724 VobSubDemuxContext *vobsub =
s->priv_data;
726
727 for (
i = 0;
i <
s->nb_streams;
i++)
730 return 0;
731 }
732
734 {
735 int i,
ret = 0, header_parsed = 0, langidx = 0;
736 VobSubDemuxContext *vobsub =
s->priv_data;
738 size_t fname_len;
740 int64_t delay = 0;
742 int stream_id = -1;
743 char id[64] = {0};
745
746 if (!vobsub->sub_name) {
747 char *ext;
749 if (!vobsub->sub_name) {
751 }
752
753 fname_len = strlen(vobsub->sub_name);
754 ext = vobsub->sub_name - 3 + fname_len;
755 if (fname_len < 4 || *(ext - 1) != '.') {
757 "to guess the associated .SUB file\n");
759 }
760 memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3);
762 }
763
766 }
767
769 if (!vobsub->sub_ctx) {
771 }
772
775
780 }
781
783
787
789 break;
790
792
793 if (!strncmp(
line,
"id:", 3)) {
794 if (sscanf(
line,
"id: %63[^,], index: %u",
id, &stream_id) != 2) {
796 "assuming 'id: und, index: 0'\n",
line);
797 strcpy(id, "und");
798 stream_id = 0;
799 }
800
804 goto end;
805 }
806
807 header_parsed = 1;
808 alt[0] = '0円';
809 /* We do not create the stream immediately to avoid adding empty
810 * streams. See the following timestamp entry. */
811
813
814 }
else if (!strncmp(
line,
"timestamp:", 10)) {
817 int64_t
pos, timestamp;
818 const char *p =
line + 10;
819
820 if (stream_id == -1) {
823 goto end;
824 }
825
826 if (!st || st->
id != stream_id) {
828 if (!st) {
830 goto end;
831 }
837 if (alt[0])
839 }
840
841 if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"SCNx64,
842 &hh, &mm, &
ss, &ms, &
pos) != 5) {
844 "abort parsing\n",
line);
846 goto end;
847 }
848 timestamp = (hh*3600LL + mm*60LL +
ss) * 1000LL + ms + delay;
850
854 goto end;
855 }
857 sub->pts = timestamp;
858 sub->stream_index =
s->nb_streams - 1;
859
860 }
else if (!strncmp(
line,
"alt:", 4)) {
861 const char *p =
line + 4;
862
863 while (*p == ' ')
864 p++;
867 header_parsed = 1;
868
869 }
else if (!strncmp(
line,
"delay:", 6)) {
870 int sign = 1, hh = 0, mm = 0,
ss = 0, ms = 0;
871 const char *p =
line + 6;
872
873 while (*p == ' ')
874 p++;
875 if (*p == '-' || *p == '+') {
876 sign = *p == '-' ? -1 : 1;
877 p++;
878 }
879 sscanf(p,
"%d:%d:%d:%d", &hh, &mm, &
ss, &ms);
880 delay = ((hh*3600LL + mm*60LL +
ss) * 1000LL + ms) * sign;
881
882 }
else if (!strncmp(
line,
"langidx:", 8)) {
883 const char *p =
line + 8;
884
885 if (sscanf(p, "%d", &langidx) != 1)
887
888 } else if (!header_parsed) {
891 }
892 }
893
896
897 for (
i = 0;
i <
s->nb_streams;
i++) {
899 vobsub->q[
i].keep_duplicates = 1;
901 }
902
905 goto end;
906 }
907 for (
i = 0;
i <
s->nb_streams;
i++) {
911 goto end;
912 }
914 }
915 end:
918 }
919
921 {
922 VobSubDemuxContext *vobsub =
s->priv_data;
925 int ret, psize, total_read = 0,
i;
926
927 int64_t min_ts = INT64_MAX;
928 int sid = 0;
929 for (
i = 0;
i <
s->nb_streams;
i++) {
931 int64_t ts;
933
935 continue;
936
938 if (ts < min_ts) {
939 min_ts = ts;
941 }
942 }
943 q = &vobsub->q[sid];
944 /* The returned packet will have size zero,
945 * so that it can be directly used with av_grow_packet. */
949
950 /* compute maximum packet size using the next packet position. This is
951 * useful when the len in the header is non-sense */
954 } else {
957 }
958
960
961 do {
962 int n, to_read, startcode;
964 int64_t old_pos =
avio_tell(pb), new_pos;
965 int pkt_size;
966
969 if (
pkt->
size)
// raise packet even if incomplete
970 break;
972 }
973 to_read =
ret & 0xffff;
975 pkt_size =
ret + (new_pos - old_pos);
976
977 /* this prevents reads above the current packet */
978 if (total_read + pkt_size > psize)
979 break;
980 total_read += pkt_size;
981
982 /* the current chunk doesn't match the stream index (unlikely) */
984 break;
985
989
991 if (n < to_read)
993 } while (total_read < psize);
994
995 return 0;
996 }
997
999 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
1000 {
1001 VobSubDemuxContext *vobsub =
s->priv_data;
1002
1003 /* Rescale requested timestamps based on the first stream (timebase is the
1004 * same for all subtitles stream within a .idx/.sub). Rescaling is done just
1005 * like in avformat_seek_file(). */
1006 if (stream_index == -1 &&
s->nb_streams != 1) {
1016 for (
i = 0;
i <
s->nb_streams;
i++) {
1018 min_ts, ts, max_ts,
flags);
1021 }
1023 }
1024
1025 if (stream_index == -1) // only 1 stream
1026 stream_index = 0;
1028 min_ts, ts, max_ts,
flags);
1029 }
1030
1034 };
1035
1036 static const AVClass vobsub_demuxer_class = {
1041 };
1042
1046 .priv_data_size = sizeof(VobSubDemuxContext),
1051 .read_seek2 = vobsub_read_seek,
1054 .extensions = "idx",
1055 .priv_class = &vobsub_demuxer_class,
1056 };
1057 #endif