asfdec.c 39 KB

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