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.

1310 lines
47KB

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