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.

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