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.

1173 lines
43KB

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