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.

1415 lines
50KB

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