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.

1254 lines
48KB

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