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.

1413 lines
50KB

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