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.

1325 lines
48KB

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