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.

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