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.

1424 lines
51KB

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