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.

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