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.

1250 lines
47KB

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