asfdec.c 42 KB

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