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.

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