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.

1420 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. int64_t fsize = avio_size(pb);
  310. if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 || FFABS(fsize - (int64_t)asf->hdr.file_size) < 10000)
  311. st->duration = asf->hdr.play_time /
  312. (10000000 / 1000) - start_time;
  313. }
  314. ff_get_guid(pb, &g);
  315. test_for_ext_stream_audio = 0;
  316. if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
  317. type = AVMEDIA_TYPE_AUDIO;
  318. } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
  319. type = AVMEDIA_TYPE_VIDEO;
  320. } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
  321. type = AVMEDIA_TYPE_VIDEO;
  322. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  323. } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
  324. type = AVMEDIA_TYPE_DATA;
  325. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
  326. test_for_ext_stream_audio = 1;
  327. type = AVMEDIA_TYPE_UNKNOWN;
  328. } else {
  329. return -1;
  330. }
  331. ff_get_guid(pb, &g);
  332. avio_skip(pb, 8); /* total_size */
  333. type_specific_size = avio_rl32(pb);
  334. avio_rl32(pb);
  335. st->id = avio_rl16(pb) & 0x7f; /* stream id */
  336. // mapping of asf ID to AV stream ID;
  337. asf->asfid2avid[st->id] = s->nb_streams - 1;
  338. avio_rl32(pb);
  339. if (test_for_ext_stream_audio) {
  340. ff_get_guid(pb, &g);
  341. if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
  342. type = AVMEDIA_TYPE_AUDIO;
  343. is_dvr_ms_audio=1;
  344. ff_get_guid(pb, &g);
  345. avio_rl32(pb);
  346. avio_rl32(pb);
  347. avio_rl32(pb);
  348. ff_get_guid(pb, &g);
  349. avio_rl32(pb);
  350. }
  351. }
  352. st->codec->codec_type = type;
  353. if (type == AVMEDIA_TYPE_AUDIO) {
  354. int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
  355. if (ret < 0)
  356. return ret;
  357. if (is_dvr_ms_audio) {
  358. // codec_id and codec_tag are unreliable in dvr_ms
  359. // files. Set them later by probing stream.
  360. st->request_probe= 1;
  361. st->codec->codec_tag = 0;
  362. }
  363. if (st->codec->codec_id == AV_CODEC_ID_AAC) {
  364. st->need_parsing = AVSTREAM_PARSE_NONE;
  365. } else {
  366. st->need_parsing = AVSTREAM_PARSE_FULL;
  367. }
  368. /* We have to init the frame size at some point .... */
  369. pos2 = avio_tell(pb);
  370. if (size >= (pos2 + 8 - pos1 + 24)) {
  371. asf_st->ds_span = avio_r8(pb);
  372. asf_st->ds_packet_size = avio_rl16(pb);
  373. asf_st->ds_chunk_size = avio_rl16(pb);
  374. avio_rl16(pb); //ds_data_size
  375. avio_r8(pb); //ds_silence_data
  376. }
  377. if (asf_st->ds_span > 1) {
  378. if (!asf_st->ds_chunk_size
  379. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
  380. || asf_st->ds_packet_size % asf_st->ds_chunk_size)
  381. asf_st->ds_span = 0; // disable descrambling
  382. }
  383. } else if (type == AVMEDIA_TYPE_VIDEO &&
  384. size - (avio_tell(pb) - pos1 + 24) >= 51) {
  385. avio_rl32(pb);
  386. avio_rl32(pb);
  387. avio_r8(pb);
  388. avio_rl16(pb); /* size */
  389. sizeX= avio_rl32(pb); /* size */
  390. st->codec->width = avio_rl32(pb);
  391. st->codec->height = avio_rl32(pb);
  392. /* not available for asf */
  393. avio_rl16(pb); /* panes */
  394. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  395. tag1 = avio_rl32(pb);
  396. avio_skip(pb, 20);
  397. if (sizeX > 40) {
  398. st->codec->extradata_size = sizeX - 40;
  399. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  400. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  401. }
  402. /* Extract palette from extradata if bpp <= 8 */
  403. /* This code assumes that extradata contains only palette */
  404. /* This is true for all paletted codecs implemented in libavcodec */
  405. if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
  406. #if HAVE_BIGENDIAN
  407. int i;
  408. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  409. asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
  410. #else
  411. memcpy(asf_st->palette, st->codec->extradata,
  412. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  413. #endif
  414. asf_st->palette_changed = 1;
  415. }
  416. st->codec->codec_tag = tag1;
  417. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
  418. if(tag1 == MKTAG('D', 'V', 'R', ' ')){
  419. st->need_parsing = AVSTREAM_PARSE_FULL;
  420. // 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
  421. st->codec->width =
  422. st->codec->height = 0;
  423. av_freep(&st->codec->extradata);
  424. st->codec->extradata_size=0;
  425. }
  426. if(st->codec->codec_id == AV_CODEC_ID_H264)
  427. st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
  428. }
  429. pos2 = avio_tell(pb);
  430. avio_skip(pb, size - (pos2 - pos1 + 24));
  431. return 0;
  432. }
  433. static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
  434. {
  435. ASFContext *asf = s->priv_data;
  436. AVIOContext *pb = s->pb;
  437. ff_asf_guid g;
  438. int ext_len, payload_ext_ct, stream_ct, i;
  439. uint32_t leak_rate, stream_num;
  440. unsigned int stream_languageid_index;
  441. avio_rl64(pb); // starttime
  442. avio_rl64(pb); // endtime
  443. leak_rate = avio_rl32(pb); // leak-datarate
  444. avio_rl32(pb); // bucket-datasize
  445. avio_rl32(pb); // init-bucket-fullness
  446. avio_rl32(pb); // alt-leak-datarate
  447. avio_rl32(pb); // alt-bucket-datasize
  448. avio_rl32(pb); // alt-init-bucket-fullness
  449. avio_rl32(pb); // max-object-size
  450. avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  451. stream_num = avio_rl16(pb); // stream-num
  452. stream_languageid_index = avio_rl16(pb); // stream-language-id-index
  453. if (stream_num < 128)
  454. asf->streams[stream_num].stream_language_index = stream_languageid_index;
  455. avio_rl64(pb); // avg frametime in 100ns units
  456. stream_ct = avio_rl16(pb); //stream-name-count
  457. payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
  458. if (stream_num < 128)
  459. asf->stream_bitrates[stream_num] = leak_rate;
  460. for (i=0; i<stream_ct; i++){
  461. avio_rl16(pb);
  462. ext_len = avio_rl16(pb);
  463. avio_skip(pb, ext_len);
  464. }
  465. for (i=0; i<payload_ext_ct; i++){
  466. ff_get_guid(pb, &g);
  467. avio_skip(pb, 2);
  468. ext_len=avio_rl32(pb);
  469. avio_skip(pb, ext_len);
  470. }
  471. return 0;
  472. }
  473. static int asf_read_content_desc(AVFormatContext *s, int64_t size)
  474. {
  475. AVIOContext *pb = s->pb;
  476. int len1, len2, len3, len4, len5;
  477. len1 = avio_rl16(pb);
  478. len2 = avio_rl16(pb);
  479. len3 = avio_rl16(pb);
  480. len4 = avio_rl16(pb);
  481. len5 = avio_rl16(pb);
  482. get_tag(s, "title" , 0, len1);
  483. get_tag(s, "author" , 0, len2);
  484. get_tag(s, "copyright", 0, len3);
  485. get_tag(s, "comment" , 0, len4);
  486. avio_skip(pb, len5);
  487. return 0;
  488. }
  489. static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
  490. {
  491. AVIOContext *pb = s->pb;
  492. ASFContext *asf = s->priv_data;
  493. int desc_count, i, ret;
  494. desc_count = avio_rl16(pb);
  495. for(i=0;i<desc_count;i++) {
  496. int name_len,value_type,value_len;
  497. char name[1024];
  498. name_len = avio_rl16(pb);
  499. if (name_len%2) // must be even, broken lavf versions wrote len-1
  500. name_len += 1;
  501. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  502. avio_skip(pb, name_len - ret);
  503. value_type = avio_rl16(pb);
  504. value_len = avio_rl16(pb);
  505. if (!value_type && value_len%2)
  506. value_len += 1;
  507. /**
  508. * My sample has that stream set to 0 maybe that mean the container.
  509. * Asf stream count start at 1. I am using 0 to the container value since it's unused
  510. */
  511. if (!strcmp(name, "AspectRatioX")){
  512. asf->dar[0].num= get_value(s->pb, value_type);
  513. } else if(!strcmp(name, "AspectRatioY")){
  514. asf->dar[0].den= get_value(s->pb, value_type);
  515. } else
  516. get_tag(s, name, value_type, value_len);
  517. }
  518. return 0;
  519. }
  520. static int asf_read_language_list(AVFormatContext *s, int64_t size)
  521. {
  522. AVIOContext *pb = s->pb;
  523. ASFContext *asf = s->priv_data;
  524. int j, ret;
  525. int stream_count = avio_rl16(pb);
  526. for(j = 0; j < stream_count; j++) {
  527. char lang[6];
  528. unsigned int lang_len = avio_r8(pb);
  529. if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
  530. avio_skip(pb, lang_len - ret);
  531. if (j < 128)
  532. av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
  533. }
  534. return 0;
  535. }
  536. static int asf_read_metadata(AVFormatContext *s, int64_t size)
  537. {
  538. AVIOContext *pb = s->pb;
  539. ASFContext *asf = s->priv_data;
  540. int n, stream_num, name_len, value_len, value_num;
  541. int ret, i;
  542. n = avio_rl16(pb);
  543. for(i=0;i<n;i++) {
  544. char name[1024];
  545. int av_unused value_type;
  546. avio_rl16(pb); //lang_list_index
  547. stream_num= avio_rl16(pb);
  548. name_len= avio_rl16(pb);
  549. value_type = avio_rl16(pb); /* value_type */
  550. value_len= avio_rl32(pb);
  551. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  552. avio_skip(pb, name_len - ret);
  553. av_dlog(s, "%d %d %d %d %d <%s>\n",
  554. i, stream_num, name_len, value_type, value_len, name);
  555. value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
  556. avio_skip(pb, value_len - 2);
  557. if(stream_num<128){
  558. if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
  559. else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
  560. }
  561. }
  562. return 0;
  563. }
  564. static int asf_read_marker(AVFormatContext *s, int64_t size)
  565. {
  566. AVIOContext *pb = s->pb;
  567. int i, count, name_len, ret;
  568. char name[1024];
  569. avio_rl64(pb); // reserved 16 bytes
  570. avio_rl64(pb); // ...
  571. count = avio_rl32(pb); // markers count
  572. avio_rl16(pb); // reserved 2 bytes
  573. name_len = avio_rl16(pb); // name length
  574. for(i=0;i<name_len;i++){
  575. avio_r8(pb); // skip the name
  576. }
  577. for(i=0;i<count;i++){
  578. int64_t pres_time;
  579. int name_len;
  580. avio_rl64(pb); // offset, 8 bytes
  581. pres_time = avio_rl64(pb); // presentation time
  582. avio_rl16(pb); // entry length
  583. avio_rl32(pb); // send time
  584. avio_rl32(pb); // flags
  585. name_len = avio_rl32(pb); // name length
  586. if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
  587. avio_skip(pb, name_len - ret);
  588. avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
  589. }
  590. return 0;
  591. }
  592. static int asf_read_header(AVFormatContext *s)
  593. {
  594. ASFContext *asf = s->priv_data;
  595. ff_asf_guid g;
  596. AVIOContext *pb = s->pb;
  597. int i;
  598. int64_t gsize;
  599. ff_get_guid(pb, &g);
  600. if (ff_guidcmp(&g, &ff_asf_header))
  601. return AVERROR_INVALIDDATA;
  602. avio_rl64(pb);
  603. avio_rl32(pb);
  604. avio_r8(pb);
  605. avio_r8(pb);
  606. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  607. for(;;) {
  608. uint64_t gpos= avio_tell(pb);
  609. ff_get_guid(pb, &g);
  610. gsize = avio_rl64(pb);
  611. print_guid(&g);
  612. if (!ff_guidcmp(&g, &ff_asf_data_header)) {
  613. asf->data_object_offset = avio_tell(pb);
  614. // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
  615. if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
  616. asf->data_object_size = gsize - 24;
  617. } else {
  618. asf->data_object_size = (uint64_t)-1;
  619. }
  620. break;
  621. }
  622. if (gsize < 24)
  623. return AVERROR_INVALIDDATA;
  624. if (!ff_guidcmp(&g, &ff_asf_file_header)) {
  625. int ret = asf_read_file_properties(s, gsize);
  626. if (ret < 0)
  627. return ret;
  628. } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
  629. asf_read_stream_properties(s, gsize);
  630. } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
  631. asf_read_content_desc(s, gsize);
  632. } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
  633. asf_read_language_list(s, gsize);
  634. } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
  635. asf_read_ext_content_desc(s, gsize);
  636. } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
  637. asf_read_metadata(s, gsize);
  638. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
  639. asf_read_ext_stream_properties(s, gsize);
  640. // there could be a optional stream properties object to follow
  641. // if so the next iteration will pick it up
  642. continue;
  643. } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
  644. ff_get_guid(pb, &g);
  645. avio_skip(pb, 6);
  646. continue;
  647. } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
  648. asf_read_marker(s, gsize);
  649. } else if (url_feof(pb)) {
  650. return AVERROR_EOF;
  651. } else {
  652. if (!s->keylen) {
  653. if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
  654. unsigned int len;
  655. AVPacket pkt;
  656. av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
  657. len= avio_rl32(pb);
  658. av_log(s, AV_LOG_DEBUG, "Secret data:\n");
  659. av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
  660. len= avio_rl32(pb);
  661. get_tag(s, "ASF_Protection_Type", -1, len);
  662. len= avio_rl32(pb);
  663. get_tag(s, "ASF_Key_ID", -1, len);
  664. len= avio_rl32(pb);
  665. get_tag(s, "ASF_License_URL", -1, len);
  666. } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
  667. av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
  668. av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
  669. } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
  670. av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
  671. }
  672. }
  673. }
  674. if(avio_tell(pb) != gpos + gsize)
  675. av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
  676. avio_seek(pb, gpos + gsize, SEEK_SET);
  677. }
  678. ff_get_guid(pb, &g);
  679. avio_rl64(pb);
  680. avio_r8(pb);
  681. avio_r8(pb);
  682. if (url_feof(pb))
  683. return AVERROR_EOF;
  684. asf->data_offset = avio_tell(pb);
  685. asf->packet_size_left = 0;
  686. for(i=0; i<128; i++){
  687. int stream_num= asf->asfid2avid[i];
  688. if(stream_num>=0){
  689. AVStream *st = s->streams[stream_num];
  690. if (!st->codec->bit_rate)
  691. st->codec->bit_rate = asf->stream_bitrates[i];
  692. if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
  693. av_reduce(&st->sample_aspect_ratio.den,
  694. &st->sample_aspect_ratio.num,
  695. asf->dar[i].num, asf->dar[i].den, INT_MAX);
  696. } 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.
  697. av_reduce(&st->sample_aspect_ratio.den,
  698. &st->sample_aspect_ratio.num,
  699. asf->dar[0].num, asf->dar[0].den, INT_MAX);
  700. av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
  701. i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
  702. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  703. // copy and convert language codes to the frontend
  704. if (asf->streams[i].stream_language_index < 128) {
  705. const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
  706. if (rfc1766 && strlen(rfc1766) > 1) {
  707. const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
  708. const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
  709. if (iso6392)
  710. av_dict_set(&st->metadata, "language", iso6392, 0);
  711. }
  712. }
  713. }
  714. }
  715. ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
  716. return 0;
  717. }
  718. #define DO_2BITS(bits, var, defval) \
  719. switch (bits & 3) \
  720. { \
  721. case 3: var = avio_rl32(pb); rsize += 4; break; \
  722. case 2: var = avio_rl16(pb); rsize += 2; break; \
  723. case 1: var = avio_r8(pb); rsize++; break; \
  724. default: var = defval; break; \
  725. }
  726. /**
  727. * Load a single ASF packet into the demuxer.
  728. * @param s demux context
  729. * @param pb context to read data from
  730. * @return 0 on success, <0 on error
  731. */
  732. static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
  733. {
  734. ASFContext *asf = s->priv_data;
  735. uint32_t packet_length, padsize;
  736. int rsize = 8;
  737. int c, d, e, off;
  738. // if we do not know packet size, allow skipping up to 32 kB
  739. off= 32768;
  740. if (asf->no_resync_search)
  741. off = 3;
  742. else if (s->packet_size > 0)
  743. off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
  744. c=d=e=-1;
  745. while(off-- > 0){
  746. c=d; d=e;
  747. e= avio_r8(pb);
  748. if(c == 0x82 && !d && !e)
  749. break;
  750. }
  751. if (c != 0x82) {
  752. /**
  753. * This code allows handling of -EAGAIN at packet boundaries (i.e.
  754. * if the packet sync code above triggers -EAGAIN). This does not
  755. * imply complete -EAGAIN handling support at random positions in
  756. * the stream.
  757. */
  758. if (pb->error == AVERROR(EAGAIN))
  759. return AVERROR(EAGAIN);
  760. if (!url_feof(pb))
  761. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
  762. }
  763. if ((c & 0x8f) == 0x82) {
  764. if (d || e) {
  765. if (!url_feof(pb))
  766. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  767. return AVERROR_INVALIDDATA;
  768. }
  769. c= avio_r8(pb);
  770. d= avio_r8(pb);
  771. rsize+=3;
  772. }else if(!url_feof(pb)){
  773. avio_seek(pb, -1, SEEK_CUR); //FIXME
  774. }
  775. asf->packet_flags = c;
  776. asf->packet_property = d;
  777. DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
  778. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  779. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  780. //the following checks prevent overflows and infinite loops
  781. if(!packet_length || packet_length >= (1U<<29)){
  782. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
  783. return AVERROR_INVALIDDATA;
  784. }
  785. if(padsize >= packet_length){
  786. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
  787. return AVERROR_INVALIDDATA;
  788. }
  789. asf->packet_timestamp = avio_rl32(pb);
  790. avio_rl16(pb); /* duration */
  791. // rsize has at least 11 bytes which have to be present
  792. if (asf->packet_flags & 0x01) {
  793. asf->packet_segsizetype = avio_r8(pb); rsize++;
  794. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  795. } else {
  796. asf->packet_segments = 1;
  797. asf->packet_segsizetype = 0x80;
  798. }
  799. if (rsize > packet_length - padsize) {
  800. asf->packet_size_left = 0;
  801. av_log(s, AV_LOG_ERROR,
  802. "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
  803. rsize, packet_length, padsize, avio_tell(pb));
  804. return AVERROR_INVALIDDATA;
  805. }
  806. asf->packet_size_left = packet_length - padsize - rsize;
  807. if (packet_length < asf->hdr.min_pktsize)
  808. padsize += asf->hdr.min_pktsize - packet_length;
  809. asf->packet_padsize = padsize;
  810. av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
  811. return 0;
  812. }
  813. /**
  814. *
  815. * @return <0 if error
  816. */
  817. static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
  818. ASFContext *asf = s->priv_data;
  819. int rsize = 1;
  820. int num = avio_r8(pb);
  821. int64_t ts0, ts1 av_unused;
  822. asf->packet_segments--;
  823. asf->packet_key_frame = num >> 7;
  824. asf->stream_index = asf->asfid2avid[num & 0x7f];
  825. // sequence should be ignored!
  826. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  827. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  828. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  829. av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
  830. asf->packet_key_frame, asf->stream_index, asf->packet_seq,
  831. asf->packet_frag_offset, asf->packet_replic_size);
  832. if (rsize+asf->packet_replic_size > asf->packet_size_left) {
  833. av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
  834. return AVERROR_INVALIDDATA;
  835. }
  836. if (asf->packet_replic_size >= 8) {
  837. asf->packet_obj_size = avio_rl32(pb);
  838. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  839. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  840. return AVERROR_INVALIDDATA;
  841. }
  842. asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
  843. if(asf->packet_replic_size >= 8+38+4){
  844. avio_skip(pb, 10);
  845. ts0= avio_rl64(pb);
  846. ts1= avio_rl64(pb);
  847. avio_skip(pb, 12);
  848. avio_rl32(pb);
  849. avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
  850. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  851. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  852. }else
  853. avio_skip(pb, asf->packet_replic_size - 8);
  854. rsize += asf->packet_replic_size; // FIXME - check validity
  855. } else if (asf->packet_replic_size==1){
  856. // multipacket - frag_offset is beginning timestamp
  857. asf->packet_time_start = asf->packet_frag_offset;
  858. asf->packet_frag_offset = 0;
  859. asf->packet_frag_timestamp = asf->packet_timestamp;
  860. asf->packet_time_delta = avio_r8(pb);
  861. rsize++;
  862. }else if(asf->packet_replic_size!=0){
  863. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  864. return AVERROR_INVALIDDATA;
  865. }
  866. if (asf->packet_flags & 0x01) {
  867. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  868. if (rsize > asf->packet_size_left) {
  869. av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
  870. return AVERROR_INVALIDDATA;
  871. } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
  872. if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
  873. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
  874. return AVERROR_INVALIDDATA;
  875. } else {
  876. int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
  877. asf->packet_size_left += diff;
  878. asf->packet_padsize -= diff;
  879. }
  880. }
  881. } else {
  882. asf->packet_frag_size = asf->packet_size_left - rsize;
  883. }
  884. if (asf->packet_replic_size == 1) {
  885. asf->packet_multi_size = asf->packet_frag_size;
  886. if (asf->packet_multi_size > asf->packet_size_left)
  887. return AVERROR_INVALIDDATA;
  888. }
  889. asf->packet_size_left -= rsize;
  890. return 0;
  891. }
  892. /**
  893. * Parse data from individual ASF packets (which were previously loaded
  894. * with asf_get_packet()).
  895. * @param s demux context
  896. * @param pb context to read data from
  897. * @param pkt pointer to store packet data into
  898. * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
  899. * packets need to be loaded (through asf_get_packet())
  900. */
  901. static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
  902. {
  903. ASFContext *asf = s->priv_data;
  904. ASFStream *asf_st = 0;
  905. for (;;) {
  906. int ret;
  907. if (url_feof(pb))
  908. return AVERROR_EOF;
  909. if (asf->packet_size_left < FRAME_HEADER_SIZE ||
  910. asf->packet_segments < 1) {
  911. int ret = asf->packet_size_left + asf->packet_padsize;
  912. assert(ret >= 0);
  913. /* fail safe */
  914. avio_skip(pb, ret);
  915. asf->packet_pos= avio_tell(pb);
  916. if (asf->data_object_size != (uint64_t)-1 &&
  917. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  918. return AVERROR_EOF; /* Do not exceed the size of the data object */
  919. return 1;
  920. }
  921. if (asf->packet_time_start == 0) {
  922. if (asf_read_frame_header(s, pb) < 0) {
  923. asf->packet_segments = 0;
  924. continue;
  925. }
  926. if (asf->stream_index < 0 ||
  927. s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
  928. (!asf->packet_key_frame &&
  929. s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
  930. asf->packet_time_start = 0;
  931. /* unhandled packet (should not happen) */
  932. avio_skip(pb, asf->packet_frag_size);
  933. asf->packet_size_left -= asf->packet_frag_size;
  934. if (asf->stream_index < 0)
  935. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  936. continue;
  937. }
  938. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  939. }
  940. asf_st = asf->asf_st;
  941. if (asf->packet_replic_size == 1) {
  942. // frag_offset is here used as the beginning timestamp
  943. asf->packet_frag_timestamp = asf->packet_time_start;
  944. asf->packet_time_start += asf->packet_time_delta;
  945. asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
  946. asf->packet_size_left--;
  947. asf->packet_multi_size--;
  948. if (asf->packet_multi_size < asf->packet_obj_size) {
  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_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
  957. asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
  958. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  959. asf_st->frag_offset, asf->packet_frag_size,
  960. asf->packet_obj_size, asf_st->pkt.size);
  961. asf->packet_obj_size = asf_st->pkt.size;
  962. }
  963. if (asf_st->pkt.size != asf->packet_obj_size ||
  964. //FIXME is this condition sufficient?
  965. asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
  966. if (asf_st->pkt.data) {
  967. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, "
  968. "new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  969. asf_st->frag_offset = 0;
  970. av_free_packet(&asf_st->pkt);
  971. }
  972. /* new packet */
  973. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  974. asf_st->seq = asf->packet_seq;
  975. asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
  976. asf_st->pkt.stream_index = asf->stream_index;
  977. asf_st->pkt.pos = 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. }
  1029. if (s->key && s->keylen == 20)
  1030. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  1031. ret);
  1032. asf_st->frag_offset += ret;
  1033. /* test if whole packet is read */
  1034. if (asf_st->frag_offset == asf_st->pkt.size) {
  1035. //workaround for macroshit radio DVR-MS files
  1036. if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  1037. asf_st->pkt.size > 100) {
  1038. int i;
  1039. for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
  1040. if (i == asf_st->pkt.size) {
  1041. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  1042. asf_st->frag_offset = 0;
  1043. av_free_packet(&asf_st->pkt);
  1044. continue;
  1045. }
  1046. }
  1047. /* return packet */
  1048. if (asf_st->ds_span > 1) {
  1049. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
  1050. av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * "
  1051. "ds_span (%d %d %d)\n", asf_st->pkt.size,
  1052. asf_st->ds_packet_size, asf_st->ds_span);
  1053. } else {
  1054. /* packet descrambling */
  1055. uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
  1056. if (newdata) {
  1057. int offset = 0;
  1058. memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1059. while (offset < asf_st->pkt.size) {
  1060. int off = offset / asf_st->ds_chunk_size;
  1061. int row = off / asf_st->ds_span;
  1062. int col = off % asf_st->ds_span;
  1063. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  1064. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  1065. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  1066. memcpy(newdata + offset,
  1067. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  1068. asf_st->ds_chunk_size);
  1069. offset += asf_st->ds_chunk_size;
  1070. }
  1071. av_free(asf_st->pkt.data);
  1072. asf_st->pkt.data = newdata;
  1073. }
  1074. }
  1075. }
  1076. asf_st->frag_offset = 0;
  1077. *pkt = asf_st->pkt;
  1078. asf_st->pkt.size = 0;
  1079. asf_st->pkt.data = 0;
  1080. asf_st->pkt.side_data_elems = 0;
  1081. asf_st->pkt.side_data = NULL;
  1082. break; // packet completed
  1083. }
  1084. }
  1085. return 0;
  1086. }
  1087. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  1088. {
  1089. ASFContext *asf = s->priv_data;
  1090. for (;;) {
  1091. int ret;
  1092. /* parse cached packets, if any */
  1093. if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
  1094. return ret;
  1095. if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
  1096. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  1097. asf->packet_time_start = 0;
  1098. }
  1099. }
  1100. // Added to support seeking after packets have been read
  1101. // If information is not reset, read_packet fails due to
  1102. // leftover information from previous reads
  1103. static void asf_reset_header(AVFormatContext *s)
  1104. {
  1105. ASFContext *asf = s->priv_data;
  1106. ASFStream *asf_st;
  1107. int i;
  1108. asf->packet_size_left = 0;
  1109. asf->packet_segments = 0;
  1110. asf->packet_flags = 0;
  1111. asf->packet_property = 0;
  1112. asf->packet_timestamp = 0;
  1113. asf->packet_segsizetype = 0;
  1114. asf->packet_segments = 0;
  1115. asf->packet_seq = 0;
  1116. asf->packet_replic_size = 0;
  1117. asf->packet_key_frame = 0;
  1118. asf->packet_padsize = 0;
  1119. asf->packet_frag_offset = 0;
  1120. asf->packet_frag_size = 0;
  1121. asf->packet_frag_timestamp = 0;
  1122. asf->packet_multi_size = 0;
  1123. asf->packet_obj_size = 0;
  1124. asf->packet_time_delta = 0;
  1125. asf->packet_time_start = 0;
  1126. for(i=0; i<s->nb_streams; i++){
  1127. asf_st= s->streams[i]->priv_data;
  1128. av_free_packet(&asf_st->pkt);
  1129. asf_st->frag_offset=0;
  1130. asf_st->seq=0;
  1131. }
  1132. asf->asf_st= NULL;
  1133. }
  1134. static int asf_read_close(AVFormatContext *s)
  1135. {
  1136. asf_reset_header(s);
  1137. return 0;
  1138. }
  1139. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  1140. {
  1141. AVPacket pkt1, *pkt = &pkt1;
  1142. ASFStream *asf_st;
  1143. int64_t pts;
  1144. int64_t pos= *ppos;
  1145. int i;
  1146. int64_t start_pos[ASF_MAX_STREAMS];
  1147. for(i=0; i<s->nb_streams; i++){
  1148. start_pos[i]= pos;
  1149. }
  1150. if (s->packet_size > 0)
  1151. pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
  1152. *ppos= pos;
  1153. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1154. return AV_NOPTS_VALUE;
  1155. asf_reset_header(s);
  1156. for(;;){
  1157. if (av_read_frame(s, pkt) < 0){
  1158. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  1159. return AV_NOPTS_VALUE;
  1160. }
  1161. pts = pkt->dts;
  1162. av_free_packet(pkt);
  1163. if(pkt->flags&AV_PKT_FLAG_KEY){
  1164. i= pkt->stream_index;
  1165. asf_st= s->streams[i]->priv_data;
  1166. // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
  1167. pos= asf_st->packet_pos;
  1168. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  1169. start_pos[i]= asf_st->packet_pos + 1;
  1170. if(pkt->stream_index == stream_index)
  1171. break;
  1172. }
  1173. }
  1174. *ppos= pos;
  1175. return pts;
  1176. }
  1177. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  1178. {
  1179. ff_asf_guid g;
  1180. ASFContext *asf = s->priv_data;
  1181. int64_t current_pos= avio_tell(s->pb);
  1182. if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
  1183. asf->index_read= -1;
  1184. return;
  1185. }
  1186. ff_get_guid(s->pb, &g);
  1187. /* the data object can be followed by other top-level objects,
  1188. skip them until the simple index object is reached */
  1189. while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
  1190. int64_t gsize= avio_rl64(s->pb);
  1191. if (gsize < 24 || url_feof(s->pb)) {
  1192. avio_seek(s->pb, current_pos, SEEK_SET);
  1193. asf->index_read= -1;
  1194. return;
  1195. }
  1196. avio_skip(s->pb, gsize-24);
  1197. ff_get_guid(s->pb, &g);
  1198. }
  1199. {
  1200. int64_t itime, last_pos=-1;
  1201. int pct, ict;
  1202. int i;
  1203. int64_t av_unused gsize= avio_rl64(s->pb);
  1204. ff_get_guid(s->pb, &g);
  1205. itime=avio_rl64(s->pb);
  1206. pct=avio_rl32(s->pb);
  1207. ict=avio_rl32(s->pb);
  1208. av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  1209. for (i=0;i<ict;i++){
  1210. int pktnum=avio_rl32(s->pb);
  1211. int pktct =avio_rl16(s->pb);
  1212. int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
  1213. int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
  1214. if(pos != last_pos){
  1215. av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
  1216. av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
  1217. last_pos=pos;
  1218. }
  1219. }
  1220. asf->index_read= ict > 1;
  1221. }
  1222. avio_seek(s->pb, current_pos, SEEK_SET);
  1223. }
  1224. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  1225. {
  1226. ASFContext *asf = s->priv_data;
  1227. AVStream *st = s->streams[stream_index];
  1228. if (s->packet_size <= 0)
  1229. return -1;
  1230. /* Try using the protocol's read_seek if available */
  1231. if(s->pb) {
  1232. int ret = avio_seek_time(s->pb, stream_index, pts, flags);
  1233. if(ret >= 0)
  1234. asf_reset_header(s);
  1235. if (ret != AVERROR(ENOSYS))
  1236. return ret;
  1237. }
  1238. if (!asf->index_read)
  1239. asf_build_simple_index(s, stream_index);
  1240. if((asf->index_read > 0 && st->index_entries)){
  1241. int index= av_index_search_timestamp(st, pts, flags);
  1242. if(index >= 0) {
  1243. /* find the position */
  1244. uint64_t pos = st->index_entries[index].pos;
  1245. /* do the seek */
  1246. av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  1247. if(avio_seek(s->pb, pos, SEEK_SET) < 0)
  1248. return -1;
  1249. asf_reset_header(s);
  1250. return 0;
  1251. }
  1252. }
  1253. /* no index or seeking by index failed */
  1254. if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
  1255. return -1;
  1256. asf_reset_header(s);
  1257. return 0;
  1258. }
  1259. AVInputFormat ff_asf_demuxer = {
  1260. .name = "asf",
  1261. .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
  1262. .priv_data_size = sizeof(ASFContext),
  1263. .read_probe = asf_probe,
  1264. .read_header = asf_read_header,
  1265. .read_packet = asf_read_packet,
  1266. .read_close = asf_read_close,
  1267. .read_seek = asf_read_seek,
  1268. .read_timestamp = asf_read_pts,
  1269. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
  1270. .priv_class = &asf_class,
  1271. };