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.

1216 lines
44KB

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