1 /*
2 * ASF compatible demuxer
3 * Copyright (c) 2000, 2001 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
22 #include <inttypes.h>
23
42
47
51 /* use for reading */
59
63
65
67
70
74
77 int asfid2avid[128];
///< conversion table from asf ID 2 AVStream ID
79 uint32_t
stream_bitrates[128];
///< max number of streams, bitrate for each (for streaming)
81 char stream_languages[128][6];
///< max number of streams, language for each (RFC1766, e.g. en-US)
82 /* non streamed additonnal info */
83 /* packet filling */
85 /* only for reading */
90
92
110
112
114
117
120
125 };
126
132 };
133
134 #undef NDEBUG
135 #include <assert.h>
136
137 #define ASF_MAX_STREAMS 127
138 #define FRAME_HEADER_SIZE 6
139 // Fix Me! FRAME_HEADER_SIZE may be different.
140 // (7 is known to be too large for GipsyGuitar.wmv)
141
142 #ifdef DEBUG
143 static const ff_asf_guid stream_bitrate_guid = {
/* (http://get.to/sdp) */
144 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
145 };
146
147 static const ff_asf_guid asf_audio_conceal_none = {
148 // 0x40, 0xa4, 0xf1, 0x49, 0x4ece, 0x11d0, 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
149 // New value lifted from avifile
150 0x00, 0x57, 0xfb, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b
151 };
152
153 #define PRINT_IF_GUID(g, cmp) \
154 if (!ff_guidcmp(g, &(cmp))) \
155 av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
156
158 {
164 else PRINT_IF_GUID(
g, asf_audio_conceal_none);
183 else PRINT_IF_GUID(
g, stream_bitrate_guid);
185 else
187 for (
i = 0;
i < 16;
i++)
190 }
191 #undef PRINT_IF_GUID
192 #else
193 #define print_guid(g) while(0)
194 #endif
195
197 {
198 /* check file header */
201 else
202 return 0;
203 }
204
205 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
206 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
208 {
218 default:
219 return INT_MIN;
220 }
221 }
222
224 {
228 #define LEN 22
229
231
234
238
242 break;
243 case -1:; // ASCII
248 break;
259 break;
260 }
264 default:
266 "Unsupported value type %d in tag %s.\n",
type,
key);
268 }
271
275 }
276
278 {
281
297
298 return 0;
299 }
300
302 {
310 int type_specific_size, sizeX;
311 unsigned int tag1;
313 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
314
318 }
319
321
323 if (!st)
328
329 if (!(asf->
hdr.
flags & 0x01)) {
// if we aren't streaming...
335 }
337
338 test_for_ext_stream_audio = 0;
349 test_for_ext_stream_audio = 1;
351 } else {
352 return -1;
353 }
359 // mapping of asf ID to AV stream ID;
362
364
365 if (test_for_ext_stream_audio) {
369 is_dvr_ms_audio = 1;
376 }
377 }
378
384 if (is_dvr_ms_audio) {
385 // codec_id and codec_tag are unreliable in dvr_ms
386 // files. Set them later by probing stream.
389 }
392 else
394 /* We have to init the frame size at some point .... */
396 if (
size >= (pos2 + 8 - pos1 + 24)) {
401 avio_r8(pb);
// ds_silence_data
402 }
407 asf_st->
ds_span = 0;
// disable descrambling
408 }
418 /* not available for asf */
423 if (sizeX > 40) {
432 }
433
434 /* Extract palette from extradata if bpp <= 8 */
435 /* This code assumes that extradata contains only palette */
436 /* This is true for all paletted codecs implemented in libavcodec */
438 #if HAVE_BIGENDIAN
442 #else
445 #endif
447 }
448
453 if (tag1 ==
MKTAG(
'D',
'V',
'R',
' ')) {
455 /* issue658 contains wrong w/h and MS even puts a fake seq header
456 * with wrong w/h in extradata while a correct one is in the stream.
457 * maximum lameness */
462 }
469 }
472
473 return 0;
474 }
475
477 {
481 int ext_len, payload_ext_ct, stream_ct,
i;
482 uint32_t leak_rate, stream_num;
483 unsigned int stream_languageid_index;
484
487 leak_rate =
avio_rl32(pb);
// leak-datarate
492 avio_rl32(pb);
// alt-init-bucket-fullness
494 avio_rl32(pb);
// flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
495 stream_num =
avio_rl16(pb);
// stream-num
496
497 stream_languageid_index =
avio_rl16(pb);
// stream-language-id-index
498 if (stream_num < 128)
500
501 avio_rl64(pb);
// avg frametime in 100ns units
502 stream_ct =
avio_rl16(pb);
// stream-name-count
503 payload_ext_ct =
avio_rl16(pb);
// payload-extension-system-count
504
505 if (stream_num < 128) {
508 }
509
510 for (
i = 0;
i < stream_ct;
i++) {
514 }
515
516 for (
i = 0;
i < payload_ext_ct;
i++) {
521 if (ext_len < 0)
530 }
531 }
532
533 return 0;
534 }
535
537 {
539 int len1, len2, len3, len4, len5;
540
548 get_tag(
s,
"copyright", 0, len3, 32);
551
552 return 0;
553 }
554
556 {
559 int desc_count,
i,
ret;
560
562 for (
i = 0;
i < desc_count;
i++) {
563 int name_len, value_type, value_len;
565
567 if (name_len % 2) // must be even, broken lavf versions wrote len-1
568 name_len += 1;
573 if (!value_type && value_len % 2)
574 value_len += 1;
575 /* My sample has that stream set to 0 maybe that mean the container.
576 * ASF stream count starts at 1. I am using 0 to the container value
577 * since it's unused. */
578 if (!strcmp(
name,
"AspectRatioX"))
580 else if (!strcmp(
name,
"AspectRatioY"))
582 else
584 }
585
586 return 0;
587 }
588
590 {
595 for (j = 0; j < stream_count; j++) {
596 char lang[6];
597 unsigned int lang_len =
avio_r8(pb);
599 sizeof(lang))) < lang_len)
601 if (j < 128)
604 }
605
606 return 0;
607 }
608
610 {
613 int n, stream_num, name_len_utf16, name_len_utf8;
614 unsigned int value_len;
617
618 for (
i = 0;
i < n;
i++) {
620 int value_type;
621
625 value_type =
avio_rl16(pb);
/* value_type */
627
628 if (value_len >= (INT_MAX -
LEN) / 2)
630
631 name_len_utf8 = 2*name_len_utf16 + 1;
635
639 i, stream_num, name_len_utf16, value_type, value_len,
name);
640
641 if (!strcmp(
name,
"AspectRatioX")){
642 int aspect_x =
get_value(
s->pb, value_type, 16);
643 if(stream_num < 128)
644 asf->
dar[stream_num].
num = aspect_x;
645 }
else if(!strcmp(
name,
"AspectRatioY")){
646 int aspect_y =
get_value(
s->pb, value_type, 16);
647 if(stream_num < 128)
648 asf->
dar[stream_num].
den = aspect_y;
649 } else {
651 }
653 }
654
655 return 0;
656 }
657
659 {
662 int i, count, name_len,
ret;
664
671
672 for (
i = 0;
i < count;
i++) {
674 int name_len;
675
678
680 pres_time =
avio_rl64(pb);
// presentation time
686 if ((unsigned)name_len > INT_MAX / 2)
689 sizeof(
name))) < name_len)
693 }
694
695 return 0;
696 }
697
699 {
705
714
715 for (
i = 0;
i<128;
i++)
717
726 /* If not streaming, gsize is not unlimited (how?),
727 * and there is enough space in the file.. */
728 if (!(asf->
hdr.
flags & 0x01) && gsize >= 100)
730 else
732 break;
733 }
734 if (gsize < 24)
752
753 // there could be an optional stream properties object to follow
754 // if so the next iteration will pick it up
755 continue;
759 continue;
764 } else {
770 "DRM protected stream detected, decoding will likely fail!\n");
773
778
780 if (
len > UINT16_MAX)
783
785 if (
len > UINT16_MAX)
788
790 if (
len > UINT16_MAX)
795 "Ext DRM protected stream detected, decoding will likely fail!\n");
796 av_dict_set(&
s->metadata,
"encryption",
"ASF Extended Content Encryption", 0);
799 }
800 }
801 }
804
807 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
810 }
819
820 for (
i = 0;
i < 128;
i++) {
822 if (stream_num >= 0) {
830 }
else if ((asf->
dar[0].
num > 0) && (asf->
dar[0].
den > 0) &&
831 // Use ASF container value if the stream doesn't set AR.
836
840
841 // copy and convert language codes to the frontend
844 if (rfc1766 && strlen(rfc1766) > 1) {
845 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '0円' }; // ignore country code if any
848 if (iso6392)
850 }
851 }
852 }
853 }
854
856
857 return 0;
858 }
859
860 #define DO_2BITS(bits, var, defval) \
861 switch (bits & 3) { \
862 case 3: \
863 var = avio_rl32(pb); \
864 rsize += 4; \
865 break; \
866 case 2: \
867 var = avio_rl16(pb); \
868 rsize += 2; \
869 break; \
870 case 1: \
871 var = avio_r8(pb); \
872 rsize++; \
873 break; \
874 default: \
875 var = defval; \
876 break; \
877 }
878
879 /**
880 * Load a single ASF packet into the demuxer.
881 * @param s demux context
882 * @param pb context to read data from
883 * @return 0 on success, <0 on error
884 */
886 {
888 uint32_t packet_length, padsize;
889 int rsize = 8;
891
893 // if we do not know packet size, allow skipping up to 32 kB
894 off = 32768;
896 off = 3;
897 // else if (s->packet_size > 0 && !asf->uses_std_ecc)
898 // off = (avio_tell(pb) - ffformatcontext(s)->data_offset) % s->packet_size + 3;
899
901 while (off-- > 0) {
903 d = e;
905 if (
c == 0x82 && !d && !e)
906 break;
907 }
908
910 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
911 * if the packet sync code above triggers -EAGAIN). This does not
912 * imply complete -EAGAIN handling support at random positions in
913 * the stream. */
918 "ff asf bad header %x at:%"PRId64
"\n",
c,
avio_tell(pb));
919 }
920 if ((
c & 0x8f) == 0x82) {
921 if (d || e) {
925 }
928 rsize += 3;
931 }
932 } else {
935 rsize ++;
941 }
942
945
948
950 } else
953 }
954
957
961
962 // the following checks prevent overflows and infinite loops
963 if (!packet_length || packet_length >= (1
U << 29)) {
965 "invalid packet_length %"PRIu32" at:%"PRId64"\n",
968 }
969 if (padsize >= packet_length) {
971 "invalid padsize %"PRIu32
" at:%"PRId64
"\n", padsize,
avio_tell(pb));
973 }
974
977 // rsize has at least 11 bytes which have to be present
978
981 rsize++;
983 } else {
986 }
987 if (rsize > packet_length - padsize) {
990 "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
991 rsize, packet_length, padsize,
avio_tell(pb));
993 }
995 if (packet_length < asf->hdr.min_pktsize)
1000 return 0;
1001 }
1002
1003 /**
1004 *
1005 * @return <0 if error
1006 */
1008 {
1011 int rsize = 1;
1015
1019 asfst = &asf->
streams[num & 0x7f];
1020 // sequence should be ignored!
1024 av_log(asf,
AV_LOG_TRACE,
"key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1030 }
1039 }
1041
1049 if (payend > end) {
1051 break;
1052 }
1054 case 0x50:
1055 // duration = avio_rl16(pb);
1056 break;
1057 case 0x54:
1061 s->streams[asf->
stream_index]->sample_aspect_ratio = aspect;
1062 }
1063 break;
1064 case 0x2A:
1071 break;
1072 case 0x5B:
1073 case 0xB7:
1074 case 0xCC:
1075 case 0xC0:
1076 case 0xA0:
1077 //unknown
1078 break;
1079 }
1081 }
1082
1086 // multipacket - frag_offset is beginning timestamp
1090
1092 rsize++;
1097 }
1108 } else {
1112 }
1113 }
1114 } else {
1116 }
1121 }
1123
1124 return 0;
1125 }
1126
1127 /**
1128 * Parse data from individual ASF packets (which were previously loaded
1129 * with asf_get_packet()).
1130 * @param s demux context
1131 * @param pb context to read data from
1132 * @param pkt pointer to store packet data into
1133 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1134 * packets need to be loaded (through asf_get_packet())
1135 */
1137 {
1140 for (;;) {
1147
1150
1152 /* fail safe */
1154
1158 return AVERROR_EOF;
/* Do not exceed the size of the data object */
1159 return 1;
1160 }
1164 continue;
1165 }
1171 /* unhandled packet (should not happen) */
1177 continue;
1178 }
1182 }
1185
1188 "stream:%d, expected:%d but got %d from pkt)\n",
1193 continue;
1194 }
1195
1197 // frag_offset is here used as the beginning timestamp
1207 continue;
1208 }
1210 }
1211
1213 // FIXME is this condition sufficient?
1216
1219 "freeing incomplete packet size %d, new %d\n",
1223 }
1224 /* new packet */
1231 } else
1233 }
1237
1239 uint8_t *pal;
1242 if (!pal) {
1244 } else {
1247 }
1248 }
1249 av_log(asf,
AV_LOG_TRACE,
"new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1258 }
1259
1260 /* read data */
1266 continue;
1267
1271 "packet fragment position invalid %u,%u not in %u\n",
1274 continue;
1275 }
1276
1280 }
1281
1285 if (ret < 0 || asf->packet_frag_offset +
ret == 0)
1287
1289 // scrambling, we can either drop it completely or fill the remainder
1290 // TODO: should we fill the whole packet instead of just the current
1291 // fragment?
1295 } else {
1296 // no scrambling, so we can return partial packets
1298 }
1299 }
1300 if (
s->key &&
s->keylen == 20)
1304 /* test if whole packet is read */
1306 // workaround for macroshit radio DVR-MS files
1311 ;
1316 continue;
1317 }
1318 }
1319
1320 /* return packet */
1324 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1327 } else {
1328 /* packet descrambling */
1331 if (buf) {
1332 uint8_t *newdata = buf->
data;
1334 memset(newdata + asf_st->
pkt.
size, 0,
1336 while (offset < asf_st->
pkt.
size) {
1338 int row = off / asf_st->
ds_span;
1339 int col = off % asf_st->
ds_span;
1347 }
1351 }
1352 }
1353 }
1361 break; // packet completed
1362 }
1363 }
1364 return 0;
1365 }
1366
1368 {
1370
1371 for (;;) {
1373
1374 /* parse cached packets, if any */
1381 }
1382 }
1383
1384 // Added to support seeking after packets have been read
1385 // If information is not reset, read_packet fails due to
1386 // leftover information from previous reads
1388 {
1392
1409
1410 for (
i = 0;
i < 128;
i++) {
1416 }
1418 }
1419
1421 {
1424
1425 for (
i = 0;
i < 128;
i++) {
1429 continue;
1430
1432 }
1433 }
1434
1436 {
1438
1439 return 0;
1440 }
1441
1444 {
1453
1454 for (
i = 0;
i <
s->nb_streams;
i++)
1456
1457 if (
s->packet_size > 0)
1459 s->packet_size *
s->packet_size +
1464
1467 for (;;) {
1471 }
1472
1474
1477
1478 asf_st = &asf->
streams[
s->streams[
i]->id];
1479
1480 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1483
1487
1490 break;
1491 }
1492 }
1494 }
1495
1498 }
1499
1501 {
1506
1509 }
1510
1512 goto end;
1513
1514 /* the data object can be followed by other top-level objects,
1515 * skip them until the simple index object is reached */
1519 goto end;
1520 }
1523 goto end;
1524 }
1525
1526 {
1528 int pct, ict;
1532 goto end;
1537 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1538
1539 for (
i = 0;
i < ict;
i++) {
1544
1547 goto end;
1548 }
1549
1550 if (
pos != last_pos) {
1552 pktnum, pktct, index_pts);
1556 }
1557 }
1559 }
1560 end:
1561 // if (avio_feof(s->pb)) {
1562 // ret = 0;
1563 // }
1566 }
1567
1570 {
1572 AVStream *st =
s->streams[stream_index];
1575
1576 if (
s->packet_size <= 0)
1577 return -1;
1578
1579 /* Try using the protocol's read_seek if available */
1586 }
1587
1588 /* explicitly handle the case of seeking to 0 */
1592 return 0;
1593 }
1594
1599 }
1600
1604 /* find the position */
1606
1607 /* do the seek */
1610 return -1;
1613 return 0;
1614 }
1615 }
1616 /* no index or seeking by index failed */
1618 return -1;
1621 return 0;
1622 }
1623
1636 };