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.

1316 lines
47KB

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