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.

1305 lines
46KB

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