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.

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