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.

1203 lines
44KB

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