You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1491 lines
52KB

  1. /*
  2. * ASF compatible demuxer
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "avformat.h"
  30. #include "avio_internal.h"
  31. #include "avlanguage.h"
  32. #include "id3v2.h"
  33. #include "internal.h"
  34. #include "riff.h"
  35. #include "asf.h"
  36. #include "asfcrypt.h"
  37. typedef struct {
  38. const AVClass *class;
  39. int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
  40. ASFStream streams[128]; ///< it's max number and it's not that big
  41. uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
  42. AVRational dar[128];
  43. char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
  44. /* non streamed additonnal info */
  45. /* packet filling */
  46. int packet_size_left;
  47. /* only for reading */
  48. uint64_t data_offset; ///< beginning of the first data packet
  49. uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
  50. uint64_t data_object_size; ///< size of the data object
  51. int index_read;
  52. ASFMainHeader hdr;
  53. int packet_flags;
  54. int packet_property;
  55. int packet_timestamp;
  56. int packet_segsizetype;
  57. int packet_segments;
  58. int packet_seq;
  59. int packet_replic_size;
  60. int packet_key_frame;
  61. int packet_padsize;
  62. unsigned int packet_frag_offset;
  63. unsigned int packet_frag_size;
  64. int64_t packet_frag_timestamp;
  65. int packet_multi_size;
  66. int packet_obj_size;
  67. int packet_time_delta;
  68. int packet_time_start;
  69. int64_t packet_pos;
  70. int stream_index;
  71. ASFStream *asf_st; ///< currently decoded stream
  72. int no_resync_search;
  73. } ASFContext;
  74. static const AVOption options[] = {
  75. { "no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  76. { NULL },
  77. };
  78. static const AVClass asf_class = {
  79. .class_name = "asf demuxer",
  80. .item_name = av_default_item_name,
  81. .option = options,
  82. .version = LIBAVUTIL_VERSION_INT,
  83. };
  84. #undef NDEBUG
  85. #include <assert.h>
  86. #define ASF_MAX_STREAMS 127
  87. #define FRAME_HEADER_SIZE 17
  88. // Fix Me! FRAME_HEADER_SIZE may be different.
  89. static const ff_asf_guid index_guid = {
  90. 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
  91. };
  92. #ifdef DEBUG
  93. static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
  94. 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
  95. };
  96. #define PRINT_IF_GUID(g, cmp) \
  97. if (!ff_guidcmp(g, &cmp)) \
  98. av_dlog(NULL, "(GUID: %s) ", # cmp)
  99. static void print_guid(ff_asf_guid *g)
  100. {
  101. int i;
  102. PRINT_IF_GUID(g, ff_asf_header);
  103. else PRINT_IF_GUID(g, ff_asf_file_header);
  104. else PRINT_IF_GUID(g, ff_asf_stream_header);
  105. else PRINT_IF_GUID(g, ff_asf_audio_stream);
  106. else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
  107. else PRINT_IF_GUID(g, ff_asf_video_stream);
  108. else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
  109. else PRINT_IF_GUID(g, ff_asf_command_stream);
  110. else PRINT_IF_GUID(g, ff_asf_comment_header);
  111. else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
  112. else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
  113. else PRINT_IF_GUID(g, ff_asf_data_header);
  114. else PRINT_IF_GUID(g, index_guid);
  115. else PRINT_IF_GUID(g, ff_asf_head1_guid);
  116. else PRINT_IF_GUID(g, ff_asf_head2_guid);
  117. else PRINT_IF_GUID(g, ff_asf_my_guid);
  118. else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
  119. else PRINT_IF_GUID(g, ff_asf_extended_content_header);
  120. else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
  121. else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
  122. else PRINT_IF_GUID(g, ff_asf_metadata_header);
  123. else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
  124. else PRINT_IF_GUID(g, ff_asf_marker_header);
  125. else PRINT_IF_GUID(g, stream_bitrate_guid);
  126. else PRINT_IF_GUID(g, ff_asf_language_guid);
  127. else
  128. av_dlog(NULL, "(GUID: unknown) ");
  129. for (i = 0; i < 16; i++)
  130. av_dlog(NULL, " 0x%02x,", (*g)[i]);
  131. av_dlog(NULL, "}\n");
  132. }
  133. #undef PRINT_IF_GUID
  134. #else
  135. #define print_guid(g)
  136. #endif
  137. static int asf_probe(AVProbeData *pd)
  138. {
  139. /* check file header */
  140. if (!ff_guidcmp(pd->buf, &ff_asf_header))
  141. return AVPROBE_SCORE_MAX;
  142. else
  143. return 0;
  144. }
  145. /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
  146. * but 16 bit for "Metadata Object" and "Metadata Library Object" */
  147. static int get_value(AVIOContext *pb, int type, int type2_size)
  148. {
  149. switch (type) {
  150. case 2:
  151. return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
  152. case 3:
  153. return avio_rl32(pb);
  154. case 4:
  155. return avio_rl64(pb);
  156. case 5:
  157. return avio_rl16(pb);
  158. default:
  159. return INT_MIN;
  160. }
  161. }
  162. /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
  163. * but in reality this is only loosely similar */
  164. static int asf_read_picture(AVFormatContext *s, int len)
  165. {
  166. AVPacket pkt = { 0 };
  167. const CodecMime *mime = ff_id3v2_mime_tags;
  168. enum AVCodecID id = AV_CODEC_ID_NONE;
  169. char mimetype[64];
  170. uint8_t *desc = NULL;
  171. AVStream *st = NULL;
  172. int ret, type, picsize, desc_len;
  173. /* type + picsize + mime + desc */
  174. if (len < 1 + 4 + 2 + 2) {
  175. av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. /* picture type */
  179. type = avio_r8(s->pb);
  180. len--;
  181. if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
  182. av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
  183. type = 0;
  184. }
  185. /* picture data size */
  186. picsize = avio_rl32(s->pb);
  187. len -= 4;
  188. /* picture MIME type */
  189. len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
  190. while (mime->id != AV_CODEC_ID_NONE) {
  191. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  192. id = mime->id;
  193. break;
  194. }
  195. mime++;
  196. }
  197. if (id == AV_CODEC_ID_NONE) {
  198. av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
  199. mimetype);
  200. return 0;
  201. }
  202. if (picsize >= len) {
  203. av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
  204. picsize, len);
  205. return AVERROR_INVALIDDATA;
  206. }
  207. /* picture description */
  208. desc_len = (len - picsize) * 2 + 1;
  209. desc = av_malloc(desc_len);
  210. if (!desc)
  211. return AVERROR(ENOMEM);
  212. len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
  213. ret = av_get_packet(s->pb, &pkt, picsize);
  214. if (ret < 0)
  215. goto fail;
  216. st = avformat_new_stream(s, NULL);
  217. if (!st) {
  218. ret = AVERROR(ENOMEM);
  219. goto fail;
  220. }
  221. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  222. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  223. st->codec->codec_id = id;
  224. st->attached_pic = pkt;
  225. st->attached_pic.stream_index = st->index;
  226. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  227. if (*desc)
  228. av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
  229. else
  230. av_freep(&desc);
  231. av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
  232. return 0;
  233. fail:
  234. av_freep(&desc);
  235. av_free_packet(&pkt);
  236. return ret;
  237. }
  238. static void get_id3_tag(AVFormatContext *s, int len)
  239. {
  240. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  241. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
  242. if (id3v2_extra_meta)
  243. ff_id3v2_parse_apic(s, &id3v2_extra_meta);
  244. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  245. }
  246. static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
  247. {
  248. char *value;
  249. int64_t off = avio_tell(s->pb);
  250. if ((unsigned)len >= (UINT_MAX - 1) / 2)
  251. return;
  252. value = av_malloc(2 * len + 1);
  253. if (!value)
  254. goto finish;
  255. if (type == 0) { // UTF16-LE
  256. avio_get_str16le(s->pb, len, value, 2 * len + 1);
  257. } else if (type == 1) { // byte array
  258. if (!strcmp(key, "WM/Picture")) { // handle cover art
  259. asf_read_picture(s, len);
  260. } else if (!strcmp(key, "ID3")) { // handle ID3 tag
  261. get_id3_tag(s, len);
  262. } else {
  263. av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
  264. }
  265. goto finish;
  266. } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
  267. uint64_t num = get_value(s->pb, type, type2_size);
  268. snprintf(value, len, "%"PRIu64, num);
  269. } else if (type == 6) { // (don't) handle GUID
  270. av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
  271. goto finish;
  272. } else {
  273. av_log(s, AV_LOG_DEBUG,
  274. "Unsupported value type %d in tag %s.\n", type, key);
  275. goto finish;
  276. }
  277. if (*value)
  278. av_dict_set(&s->metadata, key, value, 0);
  279. finish:
  280. av_freep(&value);
  281. avio_seek(s->pb, off + len, SEEK_SET);
  282. }
  283. static int asf_read_file_properties(AVFormatContext *s, int64_t size)
  284. {
  285. ASFContext *asf = s->priv_data;
  286. AVIOContext *pb = s->pb;
  287. ff_get_guid(pb, &asf->hdr.guid);
  288. asf->hdr.file_size = avio_rl64(pb);
  289. asf->hdr.create_time = avio_rl64(pb);
  290. avio_rl64(pb); /* number of packets */
  291. asf->hdr.play_time = avio_rl64(pb);
  292. asf->hdr.send_time = avio_rl64(pb);
  293. asf->hdr.preroll = avio_rl32(pb);
  294. asf->hdr.ignore = avio_rl32(pb);
  295. asf->hdr.flags = avio_rl32(pb);
  296. asf->hdr.min_pktsize = avio_rl32(pb);
  297. asf->hdr.max_pktsize = avio_rl32(pb);
  298. if (asf->hdr.min_pktsize >= (1U << 29))
  299. return AVERROR_INVALIDDATA;
  300. asf->hdr.max_bitrate = avio_rl32(pb);
  301. s->packet_size = asf->hdr.max_pktsize;
  302. return 0;
  303. }
  304. static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
  305. {
  306. ASFContext *asf = s->priv_data;
  307. AVIOContext *pb = s->pb;
  308. AVStream *st;
  309. ASFStream *asf_st;
  310. ff_asf_guid g;
  311. enum AVMediaType type;
  312. int type_specific_size, sizeX;
  313. unsigned int tag1;
  314. int64_t pos1, pos2, start_time;
  315. int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
  316. if (s->nb_streams == ASF_MAX_STREAMS) {
  317. av_log(s, AV_LOG_ERROR, "too many streams\n");
  318. return AVERROR(EINVAL);
  319. }
  320. pos1 = avio_tell(pb);
  321. st = avformat_new_stream(s, NULL);
  322. if (!st)
  323. return AVERROR(ENOMEM);
  324. avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  325. asf_st = av_mallocz(sizeof(ASFStream));
  326. if (!asf_st)
  327. return AVERROR(ENOMEM);
  328. st->priv_data = asf_st;
  329. st->start_time = 0;
  330. start_time = asf->hdr.preroll;
  331. asf_st->stream_language_index = 128; // invalid stream index means no language info
  332. if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
  333. st->duration = asf->hdr.play_time /
  334. (10000000 / 1000) - start_time;
  335. }
  336. ff_get_guid(pb, &g);
  337. test_for_ext_stream_audio = 0;
  338. if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
  339. type = AVMEDIA_TYPE_AUDIO;
  340. } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
  341. type = AVMEDIA_TYPE_VIDEO;
  342. } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
  343. type = AVMEDIA_TYPE_VIDEO;
  344. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  345. } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
  346. type = AVMEDIA_TYPE_DATA;
  347. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
  348. test_for_ext_stream_audio = 1;
  349. type = AVMEDIA_TYPE_UNKNOWN;
  350. } else {
  351. return -1;
  352. }
  353. ff_get_guid(pb, &g);
  354. avio_skip(pb, 8); /* total_size */
  355. type_specific_size = avio_rl32(pb);
  356. avio_rl32(pb);
  357. st->id = avio_rl16(pb) & 0x7f; /* stream id */
  358. // mapping of asf ID to AV stream ID;
  359. asf->asfid2avid[st->id] = s->nb_streams - 1;
  360. avio_rl32(pb);
  361. if (test_for_ext_stream_audio) {
  362. ff_get_guid(pb, &g);
  363. if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
  364. type = AVMEDIA_TYPE_AUDIO;
  365. is_dvr_ms_audio = 1;
  366. ff_get_guid(pb, &g);
  367. avio_rl32(pb);
  368. avio_rl32(pb);
  369. avio_rl32(pb);
  370. ff_get_guid(pb, &g);
  371. avio_rl32(pb);
  372. }
  373. }
  374. st->codec->codec_type = type;
  375. if (type == AVMEDIA_TYPE_AUDIO) {
  376. int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
  377. if (ret < 0)
  378. return ret;
  379. if (is_dvr_ms_audio) {
  380. // codec_id and codec_tag are unreliable in dvr_ms
  381. // files. Set them later by probing stream.
  382. st->codec->codec_id = AV_CODEC_ID_PROBE;
  383. st->codec->codec_tag = 0;
  384. }
  385. if (st->codec->codec_id == AV_CODEC_ID_AAC)
  386. st->need_parsing = AVSTREAM_PARSE_NONE;
  387. else
  388. st->need_parsing = AVSTREAM_PARSE_FULL;
  389. /* We have to init the frame size at some point .... */
  390. pos2 = avio_tell(pb);
  391. if (size >= (pos2 + 8 - pos1 + 24)) {
  392. asf_st->ds_span = avio_r8(pb);
  393. asf_st->ds_packet_size = avio_rl16(pb);
  394. asf_st->ds_chunk_size = avio_rl16(pb);
  395. avio_rl16(pb); // ds_data_size
  396. avio_r8(pb); // ds_silence_data
  397. }
  398. if (asf_st->ds_span > 1) {
  399. if (!asf_st->ds_chunk_size ||
  400. (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
  401. asf_st->ds_packet_size % asf_st->ds_chunk_size)
  402. asf_st->ds_span = 0; // disable descrambling
  403. }
  404. } else if (type == AVMEDIA_TYPE_VIDEO &&
  405. size - (avio_tell(pb) - pos1 + 24) >= 51) {
  406. avio_rl32(pb);
  407. avio_rl32(pb);
  408. avio_r8(pb);
  409. avio_rl16(pb); /* size */
  410. sizeX = avio_rl32(pb); /* size */
  411. st->codec->width = avio_rl32(pb);
  412. st->codec->height = avio_rl32(pb);
  413. /* not available for asf */
  414. avio_rl16(pb); /* panes */
  415. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  416. tag1 = avio_rl32(pb);
  417. avio_skip(pb, 20);
  418. if (sizeX > 40) {
  419. st->codec->extradata_size = sizeX - 40;
  420. st->codec->extradata = av_mallocz(st->codec->extradata_size +
  421. FF_INPUT_BUFFER_PADDING_SIZE);
  422. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  423. }
  424. /* Extract palette from extradata if bpp <= 8 */
  425. /* This code assumes that extradata contains only palette */
  426. /* This is true for all paletted codecs implemented in libavcodec */
  427. if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
  428. #if HAVE_BIGENDIAN
  429. int i;
  430. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE) / 4; i++)
  431. asf_st->palette[i] = av_bswap32(((uint32_t *)st->codec->extradata)[i]);
  432. #else
  433. memcpy(asf_st->palette, st->codec->extradata,
  434. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  435. #endif
  436. asf_st->palette_changed = 1;
  437. }
  438. st->codec->codec_tag = tag1;
  439. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
  440. if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
  441. st->need_parsing = AVSTREAM_PARSE_FULL;
  442. /* issue658 contains wrong w/h and MS even puts a fake seq header
  443. * with wrong w/h in extradata while a correct one is in the stream.
  444. * maximum lameness */
  445. st->codec->width =
  446. st->codec->height = 0;
  447. av_freep(&st->codec->extradata);
  448. st->codec->extradata_size = 0;
  449. }
  450. if (st->codec->codec_id == AV_CODEC_ID_H264)
  451. st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
  452. }
  453. pos2 = avio_tell(pb);
  454. avio_skip(pb, size - (pos2 - pos1 + 24));
  455. return 0;
  456. }
  457. static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
  458. {
  459. ASFContext *asf = s->priv_data;
  460. AVIOContext *pb = s->pb;
  461. ff_asf_guid g;
  462. int ext_len, payload_ext_ct, stream_ct, i;
  463. uint32_t leak_rate, stream_num;
  464. unsigned int stream_languageid_index;
  465. avio_rl64(pb); // starttime
  466. avio_rl64(pb); // endtime
  467. leak_rate = avio_rl32(pb); // leak-datarate
  468. avio_rl32(pb); // bucket-datasize
  469. avio_rl32(pb); // init-bucket-fullness
  470. avio_rl32(pb); // alt-leak-datarate
  471. avio_rl32(pb); // alt-bucket-datasize
  472. avio_rl32(pb); // alt-init-bucket-fullness
  473. avio_rl32(pb); // max-object-size
  474. avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  475. stream_num = avio_rl16(pb); // stream-num
  476. stream_languageid_index = avio_rl16(pb); // stream-language-id-index
  477. if (stream_num < 128)
  478. asf->streams[stream_num].stream_language_index = stream_languageid_index;
  479. avio_rl64(pb); // avg frametime in 100ns units
  480. stream_ct = avio_rl16(pb); // stream-name-count
  481. payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
  482. if (stream_num < 128)
  483. asf->stream_bitrates[stream_num] = leak_rate;
  484. for (i = 0; i < stream_ct; i++) {
  485. avio_rl16(pb);
  486. ext_len = avio_rl16(pb);
  487. avio_skip(pb, ext_len);
  488. }
  489. for (i = 0; i < payload_ext_ct; i++) {
  490. ff_get_guid(pb, &g);
  491. avio_skip(pb, 2);
  492. ext_len = avio_rl32(pb);
  493. avio_skip(pb, ext_len);
  494. }
  495. return 0;
  496. }
  497. static int asf_read_content_desc(AVFormatContext *s, int64_t size)
  498. {
  499. AVIOContext *pb = s->pb;
  500. int len1, len2, len3, len4, len5;
  501. len1 = avio_rl16(pb);
  502. len2 = avio_rl16(pb);
  503. len3 = avio_rl16(pb);
  504. len4 = avio_rl16(pb);
  505. len5 = avio_rl16(pb);
  506. get_tag(s, "title", 0, len1, 32);
  507. get_tag(s, "author", 0, len2, 32);
  508. get_tag(s, "copyright", 0, len3, 32);
  509. get_tag(s, "comment", 0, len4, 32);
  510. avio_skip(pb, len5);
  511. return 0;
  512. }
  513. static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
  514. {
  515. AVIOContext *pb = s->pb;
  516. ASFContext *asf = s->priv_data;
  517. int desc_count, i, ret;
  518. desc_count = avio_rl16(pb);
  519. for (i = 0; i < desc_count; i++) {
  520. int name_len, value_type, value_len;
  521. char name[1024];
  522. name_len = avio_rl16(pb);
  523. if (name_len % 2) // must be even, broken lavf versions wrote len-1
  524. name_len += 1;
  525. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  526. avio_skip(pb, name_len - ret);
  527. value_type = avio_rl16(pb);
  528. value_len = avio_rl16(pb);
  529. if (!value_type && value_len % 2)
  530. value_len += 1;
  531. /* My sample has that stream set to 0 maybe that mean the container.
  532. * ASF stream count starts at 1. I am using 0 to the container value
  533. * since it's unused. */
  534. if (!strcmp(name, "AspectRatioX"))
  535. asf->dar[0].num = get_value(s->pb, value_type, 32);
  536. else if (!strcmp(name, "AspectRatioY"))
  537. asf->dar[0].den = get_value(s->pb, value_type, 32);
  538. else
  539. get_tag(s, name, value_type, value_len, 32);
  540. }
  541. return 0;
  542. }
  543. static int asf_read_language_list(AVFormatContext *s, int64_t size)
  544. {
  545. AVIOContext *pb = s->pb;
  546. ASFContext *asf = s->priv_data;
  547. int j, ret;
  548. int stream_count = avio_rl16(pb);
  549. for (j = 0; j < stream_count; j++) {
  550. char lang[6];
  551. unsigned int lang_len = avio_r8(pb);
  552. if ((ret = avio_get_str16le(pb, lang_len, lang,
  553. sizeof(lang))) < lang_len)
  554. avio_skip(pb, lang_len - ret);
  555. if (j < 128)
  556. av_strlcpy(asf->stream_languages[j], lang,
  557. sizeof(*asf->stream_languages));
  558. }
  559. return 0;
  560. }
  561. static int asf_read_metadata(AVFormatContext *s, int64_t size)
  562. {
  563. AVIOContext *pb = s->pb;
  564. ASFContext *asf = s->priv_data;
  565. int n, stream_num, name_len, value_len;
  566. int ret, i;
  567. n = avio_rl16(pb);
  568. for (i = 0; i < n; i++) {
  569. char name[1024];
  570. int value_type;
  571. avio_rl16(pb); // lang_list_index
  572. stream_num = avio_rl16(pb);
  573. name_len = avio_rl16(pb);
  574. value_type = avio_rl16(pb); /* value_type */
  575. value_len = avio_rl32(pb);
  576. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  577. avio_skip(pb, name_len - ret);
  578. av_dlog(s, "%d stream %d name_len %2d type %d len %4d <%s>\n",
  579. i, stream_num, name_len, value_type, value_len, name);
  580. if (!strcmp(name, "AspectRatioX")){
  581. int aspect_x = get_value(s->pb, value_type, 16);
  582. if(stream_num < 128)
  583. asf->dar[stream_num].num = aspect_x;
  584. } else if(!strcmp(name, "AspectRatioY")){
  585. int aspect_y = get_value(s->pb, value_type, 16);
  586. if(stream_num < 128)
  587. asf->dar[stream_num].den = aspect_y;
  588. } else {
  589. get_tag(s, name, value_type, value_len, 16);
  590. }
  591. }
  592. return 0;
  593. }
  594. static int asf_read_marker(AVFormatContext *s, int64_t size)
  595. {
  596. AVIOContext *pb = s->pb;
  597. int i, count, name_len, ret;
  598. char name[1024];
  599. avio_rl64(pb); // reserved 16 bytes
  600. avio_rl64(pb); // ...
  601. count = avio_rl32(pb); // markers count
  602. avio_rl16(pb); // reserved 2 bytes
  603. name_len = avio_rl16(pb); // name length
  604. for (i = 0; i < name_len; i++)
  605. avio_r8(pb); // skip the name
  606. for (i = 0; i < count; i++) {
  607. int64_t pres_time;
  608. int name_len;
  609. avio_rl64(pb); // offset, 8 bytes
  610. pres_time = avio_rl64(pb); // presentation time
  611. avio_rl16(pb); // entry length
  612. avio_rl32(pb); // send time
  613. avio_rl32(pb); // flags
  614. name_len = avio_rl32(pb); // name length
  615. if ((ret = avio_get_str16le(pb, name_len * 2, name,
  616. sizeof(name))) < name_len)
  617. avio_skip(pb, name_len - ret);
  618. avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
  619. AV_NOPTS_VALUE, name);
  620. }
  621. return 0;
  622. }
  623. static int asf_read_header(AVFormatContext *s)
  624. {
  625. ASFContext *asf = s->priv_data;
  626. ff_asf_guid g;
  627. AVIOContext *pb = s->pb;
  628. int i;
  629. int64_t gsize;
  630. ff_get_guid(pb, &g);
  631. if (ff_guidcmp(&g, &ff_asf_header))
  632. return -1;
  633. avio_rl64(pb);
  634. avio_rl32(pb);
  635. avio_r8(pb);
  636. avio_r8(pb);
  637. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  638. for (;;) {
  639. uint64_t gpos = avio_tell(pb);
  640. ff_get_guid(pb, &g);
  641. gsize = avio_rl64(pb);
  642. print_guid(&g);
  643. if (!ff_guidcmp(&g, &ff_asf_data_header)) {
  644. asf->data_object_offset = avio_tell(pb);
  645. /* If not streaming, gsize is not unlimited (how?),
  646. * and there is enough space in the file.. */
  647. if (!(asf->hdr.flags & 0x01) && gsize >= 100)
  648. asf->data_object_size = gsize - 24;
  649. else
  650. asf->data_object_size = (uint64_t)-1;
  651. break;
  652. }
  653. if (gsize < 24)
  654. return -1;
  655. if (!ff_guidcmp(&g, &ff_asf_file_header)) {
  656. int ret = asf_read_file_properties(s, gsize);
  657. if (ret < 0)
  658. return ret;
  659. } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
  660. asf_read_stream_properties(s, gsize);
  661. } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
  662. asf_read_content_desc(s, gsize);
  663. } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
  664. asf_read_language_list(s, gsize);
  665. } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
  666. asf_read_ext_content_desc(s, gsize);
  667. } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
  668. asf_read_metadata(s, gsize);
  669. } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
  670. asf_read_metadata(s, gsize);
  671. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
  672. asf_read_ext_stream_properties(s, gsize);
  673. // there could be a optional stream properties object to follow
  674. // if so the next iteration will pick it up
  675. continue;
  676. } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
  677. ff_get_guid(pb, &g);
  678. avio_skip(pb, 6);
  679. continue;
  680. } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
  681. asf_read_marker(s, gsize);
  682. } else if (pb->eof_reached) {
  683. return -1;
  684. } else {
  685. if (!s->keylen) {
  686. if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
  687. av_log(s, AV_LOG_WARNING,
  688. "DRM protected stream detected, decoding will likely fail!\n");
  689. } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
  690. av_log(s, AV_LOG_WARNING,
  691. "Ext DRM protected stream detected, decoding will likely fail!\n");
  692. } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
  693. av_log(s, AV_LOG_WARNING,
  694. "Digital signature detected, decoding will likely fail!\n");
  695. }
  696. }
  697. }
  698. if (avio_tell(pb) != gpos + gsize)
  699. av_log(s, AV_LOG_DEBUG,
  700. "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
  701. avio_tell(pb) - gpos, gsize);
  702. avio_seek(pb, gpos + gsize, SEEK_SET);
  703. }
  704. ff_get_guid(pb, &g);
  705. avio_rl64(pb);
  706. avio_r8(pb);
  707. avio_r8(pb);
  708. if (pb->eof_reached)
  709. return -1;
  710. asf->data_offset = avio_tell(pb);
  711. asf->packet_size_left = 0;
  712. for (i = 0; i < 128; i++) {
  713. int stream_num = asf->asfid2avid[i];
  714. if (stream_num >= 0) {
  715. AVStream *st = s->streams[stream_num];
  716. if (!st->codec->bit_rate)
  717. st->codec->bit_rate = asf->stream_bitrates[i];
  718. if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
  719. av_reduce(&st->sample_aspect_ratio.num,
  720. &st->sample_aspect_ratio.den,
  721. asf->dar[i].num, asf->dar[i].den, INT_MAX);
  722. } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
  723. // Use ASF container value if the stream doesn't set AR.
  724. (st->codec->codec_type == AVMEDIA_TYPE_VIDEO))
  725. av_reduce(&st->sample_aspect_ratio.num,
  726. &st->sample_aspect_ratio.den,
  727. asf->dar[0].num, asf->dar[0].den, INT_MAX);
  728. av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
  729. i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
  730. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  731. // copy and convert language codes to the frontend
  732. if (asf->streams[i].stream_language_index < 128) {
  733. const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
  734. if (rfc1766 && strlen(rfc1766) > 1) {
  735. const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
  736. const char *iso6392 = av_convert_lang_to(primary_tag,
  737. AV_LANG_ISO639_2_BIBL);
  738. if (iso6392)
  739. av_dict_set(&st->metadata, "language", iso6392, 0);
  740. }
  741. }
  742. }
  743. }
  744. ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
  745. return 0;
  746. }
  747. #define DO_2BITS(bits, var, defval) \
  748. switch (bits & 3) { \
  749. case 3: \
  750. var = avio_rl32(pb); \
  751. rsize += 4; \
  752. break; \
  753. case 2: \
  754. var = avio_rl16(pb); \
  755. rsize += 2; \
  756. break; \
  757. case 1: \
  758. var = avio_r8(pb); \
  759. rsize++; \
  760. break; \
  761. default: \
  762. var = defval; \
  763. break; \
  764. }
  765. /**
  766. * Load a single ASF packet into the demuxer.
  767. * @param s demux context
  768. * @param pb context to read data from
  769. * @return 0 on success, <0 on error
  770. */
  771. static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
  772. {
  773. ASFContext *asf = s->priv_data;
  774. uint32_t packet_length, padsize;
  775. int rsize = 8;
  776. int c, d, e, off;
  777. // if we do not know packet size, allow skipping up to 32 kB
  778. off = 32768;
  779. if (asf->no_resync_search)
  780. off = 3;
  781. else if (s->packet_size > 0)
  782. off = (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
  783. c = d = e = -1;
  784. while (off-- > 0) {
  785. c = d;
  786. d = e;
  787. e = avio_r8(pb);
  788. if (c == 0x82 && !d && !e)
  789. break;
  790. }
  791. if (c != 0x82) {
  792. /* This code allows handling of -EAGAIN at packet boundaries (i.e.
  793. * if the packet sync code above triggers -EAGAIN). This does not
  794. * imply complete -EAGAIN handling support at random positions in
  795. * the stream. */
  796. if (pb->error == AVERROR(EAGAIN))
  797. return AVERROR(EAGAIN);
  798. if (!pb->eof_reached)
  799. av_log(s, AV_LOG_ERROR,
  800. "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
  801. }
  802. if ((c & 0x8f) == 0x82) {
  803. if (d || e) {
  804. if (!pb->eof_reached)
  805. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  806. return -1;
  807. }
  808. c = avio_r8(pb);
  809. d = avio_r8(pb);
  810. rsize += 3;
  811. } else if (!pb->eof_reached) {
  812. avio_seek(pb, -1, SEEK_CUR); // FIXME
  813. }
  814. asf->packet_flags = c;
  815. asf->packet_property = d;
  816. DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
  817. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  818. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  819. // the following checks prevent overflows and infinite loops
  820. if (!packet_length || packet_length >= (1U << 29)) {
  821. av_log(s, AV_LOG_ERROR,
  822. "invalid packet_length %d at:%"PRId64"\n",
  823. packet_length, avio_tell(pb));
  824. return -1;
  825. }
  826. if (padsize >= packet_length) {
  827. av_log(s, AV_LOG_ERROR,
  828. "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
  829. return -1;
  830. }
  831. asf->packet_timestamp = avio_rl32(pb);
  832. avio_rl16(pb); /* duration */
  833. // rsize has at least 11 bytes which have to be present
  834. if (asf->packet_flags & 0x01) {
  835. asf->packet_segsizetype = avio_r8(pb);
  836. rsize++;
  837. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  838. } else {
  839. asf->packet_segments = 1;
  840. asf->packet_segsizetype = 0x80;
  841. }
  842. if (rsize > packet_length - padsize) {
  843. asf->packet_size_left = 0;
  844. av_log(s, AV_LOG_ERROR,
  845. "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
  846. rsize, packet_length, padsize, avio_tell(pb));
  847. return -1;
  848. }
  849. asf->packet_size_left = packet_length - padsize - rsize;
  850. if (packet_length < asf->hdr.min_pktsize)
  851. padsize += asf->hdr.min_pktsize - packet_length;
  852. asf->packet_padsize = padsize;
  853. av_dlog(s, "packet: size=%d padsize=%d left=%d\n",
  854. s->packet_size, asf->packet_padsize, asf->packet_size_left);
  855. return 0;
  856. }
  857. /**
  858. *
  859. * @return <0 if error
  860. */
  861. static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
  862. {
  863. ASFContext *asf = s->priv_data;
  864. int rsize = 1;
  865. int num = avio_r8(pb);
  866. int64_t ts0;
  867. asf->packet_segments--;
  868. asf->packet_key_frame = num >> 7;
  869. asf->stream_index = asf->asfid2avid[num & 0x7f];
  870. // sequence should be ignored!
  871. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  872. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  873. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  874. av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
  875. asf->packet_key_frame, asf->stream_index, asf->packet_seq,
  876. asf->packet_frag_offset, asf->packet_replic_size);
  877. if (asf->packet_replic_size >= 8) {
  878. asf->packet_obj_size = avio_rl32(pb);
  879. if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
  880. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  881. return -1;
  882. }
  883. asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
  884. if (asf->packet_replic_size >= 8 + 38 + 4) {
  885. avio_skip(pb, 10);
  886. ts0 = avio_rl64(pb);
  887. avio_skip(pb, 8);
  888. avio_skip(pb, 12);
  889. avio_rl32(pb);
  890. avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
  891. if (ts0 != -1)
  892. asf->packet_frag_timestamp = ts0 / 10000;
  893. else
  894. asf->packet_frag_timestamp = AV_NOPTS_VALUE;
  895. } else
  896. avio_skip(pb, asf->packet_replic_size - 8);
  897. rsize += asf->packet_replic_size; // FIXME - check validity
  898. } else if (asf->packet_replic_size == 1) {
  899. // multipacket - frag_offset is beginning timestamp
  900. asf->packet_time_start = asf->packet_frag_offset;
  901. asf->packet_frag_offset = 0;
  902. asf->packet_frag_timestamp = asf->packet_timestamp;
  903. asf->packet_time_delta = avio_r8(pb);
  904. rsize++;
  905. } else if (asf->packet_replic_size != 0) {
  906. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
  907. asf->packet_replic_size);
  908. return -1;
  909. }
  910. if (asf->packet_flags & 0x01) {
  911. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  912. if (rsize > asf->packet_size_left) {
  913. av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
  914. return -1;
  915. } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
  916. if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
  917. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n",
  918. asf->packet_size_left, rsize);
  919. return -1;
  920. } else {
  921. int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
  922. asf->packet_size_left += diff;
  923. asf->packet_padsize -= diff;
  924. }
  925. }
  926. } else {
  927. if (rsize > asf->packet_size_left) {
  928. av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
  929. return -1;
  930. }
  931. asf->packet_frag_size = asf->packet_size_left - rsize;
  932. }
  933. if (asf->packet_replic_size == 1) {
  934. asf->packet_multi_size = asf->packet_frag_size;
  935. if (asf->packet_multi_size > asf->packet_size_left)
  936. return -1;
  937. }
  938. asf->packet_size_left -= rsize;
  939. return 0;
  940. }
  941. /**
  942. * Parse data from individual ASF packets (which were previously loaded
  943. * with asf_get_packet()).
  944. * @param s demux context
  945. * @param pb context to read data from
  946. * @param pkt pointer to store packet data into
  947. * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
  948. * packets need to be loaded (through asf_get_packet())
  949. */
  950. static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
  951. {
  952. ASFContext *asf = s->priv_data;
  953. ASFStream *asf_st = 0;
  954. for (;;) {
  955. int ret;
  956. if (pb->eof_reached)
  957. return AVERROR_EOF;
  958. if (asf->packet_size_left < FRAME_HEADER_SIZE ||
  959. asf->packet_segments < 1) {
  960. int ret = asf->packet_size_left + asf->packet_padsize;
  961. assert(ret >= 0);
  962. /* fail safe */
  963. avio_skip(pb, ret);
  964. asf->packet_pos = avio_tell(pb);
  965. if (asf->data_object_size != (uint64_t)-1 &&
  966. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  967. return AVERROR_EOF; /* Do not exceed the size of the data object */
  968. return 1;
  969. }
  970. if (asf->packet_time_start == 0) {
  971. if (asf_read_frame_header(s, pb) < 0) {
  972. asf->packet_segments = 0;
  973. continue;
  974. }
  975. if (asf->stream_index < 0 ||
  976. s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
  977. (!asf->packet_key_frame &&
  978. s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
  979. asf->packet_time_start = 0;
  980. /* unhandled packet (should not happen) */
  981. avio_skip(pb, asf->packet_frag_size);
  982. asf->packet_size_left -= asf->packet_frag_size;
  983. if (asf->stream_index < 0)
  984. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
  985. asf->packet_frag_size);
  986. continue;
  987. }
  988. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  989. }
  990. asf_st = asf->asf_st;
  991. av_assert0(asf_st);
  992. if (asf->packet_replic_size == 1) {
  993. // frag_offset is here used as the beginning timestamp
  994. asf->packet_frag_timestamp = asf->packet_time_start;
  995. asf->packet_time_start += asf->packet_time_delta;
  996. asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
  997. asf->packet_size_left--;
  998. asf->packet_multi_size--;
  999. if (asf->packet_multi_size < asf->packet_obj_size) {
  1000. asf->packet_time_start = 0;
  1001. avio_skip(pb, asf->packet_multi_size);
  1002. asf->packet_size_left -= asf->packet_multi_size;
  1003. continue;
  1004. }
  1005. asf->packet_multi_size -= asf->packet_obj_size;
  1006. }
  1007. if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
  1008. asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
  1009. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  1010. asf_st->frag_offset, asf->packet_frag_size,
  1011. asf->packet_obj_size, asf_st->pkt.size);
  1012. asf->packet_obj_size = asf_st->pkt.size;
  1013. }
  1014. if (asf_st->pkt.size != asf->packet_obj_size ||
  1015. // FIXME is this condition sufficient?
  1016. asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
  1017. if (asf_st->pkt.data) {
  1018. av_log(s, AV_LOG_INFO,
  1019. "freeing incomplete packet size %d, new %d\n",
  1020. asf_st->pkt.size, asf->packet_obj_size);
  1021. asf_st->frag_offset = 0;
  1022. av_free_packet(&asf_st->pkt);
  1023. }
  1024. /* new packet */
  1025. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  1026. asf_st->seq = asf->packet_seq;
  1027. asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
  1028. asf_st->pkt.stream_index = asf->stream_index;
  1029. asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
  1030. if (asf_st->pkt.data && asf_st->palette_changed) {
  1031. uint8_t *pal;
  1032. pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
  1033. AVPALETTE_SIZE);
  1034. if (!pal) {
  1035. av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
  1036. } else {
  1037. memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
  1038. asf_st->palette_changed = 0;
  1039. }
  1040. }
  1041. av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  1042. asf->stream_index, asf->packet_key_frame,
  1043. asf_st->pkt.flags & AV_PKT_FLAG_KEY,
  1044. s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
  1045. asf->packet_obj_size);
  1046. if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  1047. asf->packet_key_frame = 1;
  1048. if (asf->packet_key_frame)
  1049. asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
  1050. }
  1051. /* read data */
  1052. av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  1053. s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  1054. asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  1055. asf->packet_size_left -= asf->packet_frag_size;
  1056. if (asf->packet_size_left < 0)
  1057. continue;
  1058. if (asf->packet_frag_offset >= asf_st->pkt.size ||
  1059. asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
  1060. av_log(s, AV_LOG_ERROR,
  1061. "packet fragment position invalid %u,%u not in %u\n",
  1062. asf->packet_frag_offset, asf->packet_frag_size,
  1063. asf_st->pkt.size);
  1064. continue;
  1065. }
  1066. ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
  1067. asf->packet_frag_size);
  1068. if (ret != asf->packet_frag_size) {
  1069. if (ret < 0 || asf->packet_frag_offset + ret == 0)
  1070. return ret < 0 ? ret : AVERROR_EOF;
  1071. if (asf_st->ds_span > 1) {
  1072. // scrambling, we can either drop it completely or fill the remainder
  1073. // TODO: should we fill the whole packet instead of just the current
  1074. // fragment?
  1075. memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
  1076. asf->packet_frag_size - ret);
  1077. ret = asf->packet_frag_size;
  1078. } else {
  1079. // no scrambling, so we can return partial packets
  1080. av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
  1081. }
  1082. }
  1083. if (s->key && s->keylen == 20)
  1084. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  1085. ret);
  1086. asf_st->frag_offset += ret;
  1087. /* test if whole packet is read */
  1088. if (asf_st->frag_offset == asf_st->pkt.size) {
  1089. // workaround for macroshit radio DVR-MS files
  1090. if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  1091. asf_st->pkt.size > 100) {
  1092. int i;
  1093. for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
  1094. ;
  1095. if (i == asf_st->pkt.size) {
  1096. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  1097. asf_st->frag_offset = 0;
  1098. av_free_packet(&asf_st->pkt);
  1099. continue;
  1100. }
  1101. }
  1102. /* return packet */
  1103. if (asf_st->ds_span > 1) {
  1104. if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
  1105. av_log(s, AV_LOG_ERROR,
  1106. "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
  1107. asf_st->pkt.size, asf_st->ds_packet_size,
  1108. asf_st->ds_span);
  1109. } else {
  1110. /* packet descrambling */
  1111. AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
  1112. FF_INPUT_BUFFER_PADDING_SIZE);
  1113. if (buf) {
  1114. uint8_t *newdata = buf->data;
  1115. int offset = 0;
  1116. memset(newdata + asf_st->pkt.size, 0,
  1117. FF_INPUT_BUFFER_PADDING_SIZE);
  1118. while (offset < asf_st->pkt.size) {
  1119. int off = offset / asf_st->ds_chunk_size;
  1120. int row = off / asf_st->ds_span;
  1121. int col = off % asf_st->ds_span;
  1122. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  1123. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  1124. assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  1125. memcpy(newdata + offset,
  1126. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  1127. asf_st->ds_chunk_size);
  1128. offset += asf_st->ds_chunk_size;
  1129. }
  1130. av_buffer_unref(&asf_st->pkt.buf);
  1131. asf_st->pkt.buf = buf;
  1132. asf_st->pkt.data = buf->data;
  1133. }
  1134. }
  1135. }
  1136. asf_st->frag_offset = 0;
  1137. *pkt = asf_st->pkt;
  1138. #if FF_API_DESTRUCT_PACKET
  1139. asf_st->pkt.destruct = NULL;
  1140. #endif
  1141. asf_st->pkt.buf = 0;
  1142. asf_st->pkt.size = 0;
  1143. asf_st->pkt.data = 0;
  1144. asf_st->pkt.side_data_elems = 0;
  1145. asf_st->pkt.side_data = NULL;
  1146. break; // packet completed
  1147. }
  1148. }
  1149. return 0;
  1150. }
  1151. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  1152. {
  1153. ASFContext *asf = s->priv_data;
  1154. for (;;) {
  1155. int ret;
  1156. /* parse cached packets, if any */
  1157. if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
  1158. return ret;
  1159. if ((ret = asf_get_packet(s, s->pb)) < 0)
  1160. assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
  1161. asf->packet_segments < 1);
  1162. asf->packet_time_start = 0;
  1163. }
  1164. }
  1165. // Added to support seeking after packets have been read
  1166. // If information is not reset, read_packet fails due to
  1167. // leftover information from previous reads
  1168. static void asf_reset_header(AVFormatContext *s)
  1169. {
  1170. ASFContext *asf = s->priv_data;
  1171. ASFStream *asf_st;
  1172. int i;
  1173. asf->packet_size_left = 0;
  1174. asf->packet_segments = 0;
  1175. asf->packet_flags = 0;
  1176. asf->packet_property = 0;
  1177. asf->packet_timestamp = 0;
  1178. asf->packet_segsizetype = 0;
  1179. asf->packet_segments = 0;
  1180. asf->packet_seq = 0;
  1181. asf->packet_replic_size = 0;
  1182. asf->packet_key_frame = 0;
  1183. asf->packet_padsize = 0;
  1184. asf->packet_frag_offset = 0;
  1185. asf->packet_frag_size = 0;
  1186. asf->packet_frag_timestamp = 0;
  1187. asf->packet_multi_size = 0;
  1188. asf->packet_obj_size = 0;
  1189. asf->packet_time_delta = 0;
  1190. asf->packet_time_start = 0;
  1191. for (i = 0; i < s->nb_streams; i++) {
  1192. asf_st = s->streams[i]->priv_data;
  1193. if (!asf_st)
  1194. continue;
  1195. av_free_packet(&asf_st->pkt);
  1196. asf_st->frag_offset = 0;
  1197. asf_st->seq = 0;
  1198. }
  1199. asf->asf_st = NULL;
  1200. }
  1201. static int asf_read_close(AVFormatContext *s)
  1202. {
  1203. asf_reset_header(s);
  1204. return 0;
  1205. }
  1206. static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
  1207. int64_t *ppos, int64_t pos_limit)
  1208. {
  1209. AVPacket pkt1, *pkt = &pkt1;
  1210. ASFStream *asf_st;
  1211. int64_t pts;
  1212. int64_t pos = *ppos;
  1213. int i;
  1214. int64_t start_pos[ASF_MAX_STREAMS];
  1215. for (i = 0; i < s->nb_streams; i++)
  1216. start_pos[i] = pos;
  1217. if (s->packet_size > 0)
  1218. pos = (pos + s->packet_size - 1 - s->data_offset) /
  1219. s->packet_size * s->packet_size +
  1220. s->data_offset;
  1221. *ppos = pos;
  1222. avio_seek(s->pb, pos, SEEK_SET);
  1223. asf_reset_header(s);
  1224. for (;;) {
  1225. if (asf_read_packet(s, pkt) < 0) {
  1226. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  1227. return AV_NOPTS_VALUE;
  1228. }
  1229. pts = pkt->dts;
  1230. av_free_packet(pkt);
  1231. if (pkt->flags & AV_PKT_FLAG_KEY) {
  1232. i = pkt->stream_index;
  1233. asf_st = s->streams[i]->priv_data;
  1234. av_assert0(asf_st);
  1235. // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
  1236. pos = asf_st->packet_pos;
  1237. av_add_index_entry(s->streams[i], pos, pts, pkt->size,
  1238. pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  1239. start_pos[i] = asf_st->packet_pos + 1;
  1240. if (pkt->stream_index == stream_index)
  1241. break;
  1242. }
  1243. }
  1244. *ppos = pos;
  1245. return pts;
  1246. }
  1247. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  1248. {
  1249. ff_asf_guid g;
  1250. ASFContext *asf = s->priv_data;
  1251. int64_t current_pos = avio_tell(s->pb);
  1252. int i;
  1253. avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  1254. ff_get_guid(s->pb, &g);
  1255. /* the data object can be followed by other top-level objects,
  1256. * skip them until the simple index object is reached */
  1257. while (ff_guidcmp(&g, &index_guid)) {
  1258. int64_t gsize = avio_rl64(s->pb);
  1259. if (gsize < 24 || s->pb->eof_reached) {
  1260. avio_seek(s->pb, current_pos, SEEK_SET);
  1261. return;
  1262. }
  1263. avio_skip(s->pb, gsize - 24);
  1264. ff_get_guid(s->pb, &g);
  1265. }
  1266. {
  1267. int64_t itime, last_pos = -1;
  1268. int pct, ict;
  1269. int64_t av_unused gsize = avio_rl64(s->pb);
  1270. ff_get_guid(s->pb, &g);
  1271. itime = avio_rl64(s->pb);
  1272. pct = avio_rl32(s->pb);
  1273. ict = avio_rl32(s->pb);
  1274. av_log(s, AV_LOG_DEBUG,
  1275. "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
  1276. for (i = 0; i < ict; i++) {
  1277. int pktnum = avio_rl32(s->pb);
  1278. int pktct = avio_rl16(s->pb);
  1279. int64_t pos = s->data_offset + s->packet_size * (int64_t)pktnum;
  1280. int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
  1281. if (pos != last_pos) {
  1282. av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
  1283. pktnum, pktct, index_pts);
  1284. av_add_index_entry(s->streams[stream_index], pos, index_pts,
  1285. s->packet_size, 0, AVINDEX_KEYFRAME);
  1286. last_pos = pos;
  1287. }
  1288. }
  1289. asf->index_read = ict > 0;
  1290. }
  1291. avio_seek(s->pb, current_pos, SEEK_SET);
  1292. }
  1293. static int asf_read_seek(AVFormatContext *s, int stream_index,
  1294. int64_t pts, int flags)
  1295. {
  1296. ASFContext *asf = s->priv_data;
  1297. AVStream *st = s->streams[stream_index];
  1298. int64_t pos;
  1299. int index;
  1300. if (s->packet_size <= 0)
  1301. return -1;
  1302. /* Try using the protocol's read_seek if available */
  1303. if (s->pb) {
  1304. int ret = avio_seek_time(s->pb, stream_index, pts, flags);
  1305. if (ret >= 0)
  1306. asf_reset_header(s);
  1307. if (ret != AVERROR(ENOSYS))
  1308. return ret;
  1309. }
  1310. if (!asf->index_read)
  1311. asf_build_simple_index(s, stream_index);
  1312. if ((asf->index_read && st->index_entries)) {
  1313. index = av_index_search_timestamp(st, pts, flags);
  1314. if (index >= 0) {
  1315. /* find the position */
  1316. pos = st->index_entries[index].pos;
  1317. /* do the seek */
  1318. av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  1319. avio_seek(s->pb, pos, SEEK_SET);
  1320. asf_reset_header(s);
  1321. return 0;
  1322. }
  1323. }
  1324. /* no index or seeking by index failed */
  1325. if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
  1326. return -1;
  1327. asf_reset_header(s);
  1328. return 0;
  1329. }
  1330. AVInputFormat ff_asf_demuxer = {
  1331. .name = "asf",
  1332. .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
  1333. .priv_data_size = sizeof(ASFContext),
  1334. .read_probe = asf_probe,
  1335. .read_header = asf_read_header,
  1336. .read_packet = asf_read_packet,
  1337. .read_close = asf_read_close,
  1338. .read_seek = asf_read_seek,
  1339. .read_timestamp = asf_read_pts,
  1340. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
  1341. .priv_class = &asf_class,
  1342. };