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.

1092 lines
39KB

  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. #include "libavutil/common.h"
  22. #include "libavcodec/mpegaudio.h"
  23. #include "avformat.h"
  24. #include "riff.h"
  25. #include "asf.h"
  26. #include "asfcrypt.h"
  27. void ff_mms_set_stream_selection(URLContext *h, AVFormatContext *format);
  28. #undef NDEBUG
  29. #include <assert.h>
  30. #define FRAME_HEADER_SIZE 17
  31. // Fix Me! FRAME_HEADER_SIZE may be different.
  32. static const GUID index_guid = {
  33. 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
  34. };
  35. static const GUID stream_bitrate_guid = { /* (http://get.to/sdp) */
  36. 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
  37. };
  38. /**********************************/
  39. /* decoding */
  40. //#define DEBUG
  41. #ifdef DEBUG
  42. #define PRINT_IF_GUID(g,cmp) \
  43. if (!memcmp(g, &cmp, sizeof(GUID))) \
  44. dprintf(NULL, "(GUID: %s) ", #cmp)
  45. static void print_guid(const GUID *g)
  46. {
  47. int i;
  48. PRINT_IF_GUID(g, ff_asf_header);
  49. else PRINT_IF_GUID(g, ff_asf_file_header);
  50. else PRINT_IF_GUID(g, ff_asf_stream_header);
  51. else PRINT_IF_GUID(g, ff_asf_audio_stream);
  52. else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
  53. else PRINT_IF_GUID(g, ff_asf_video_stream);
  54. else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
  55. else PRINT_IF_GUID(g, ff_asf_command_stream);
  56. else PRINT_IF_GUID(g, ff_asf_comment_header);
  57. else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
  58. else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
  59. else PRINT_IF_GUID(g, ff_asf_data_header);
  60. else PRINT_IF_GUID(g, index_guid);
  61. else PRINT_IF_GUID(g, ff_asf_head1_guid);
  62. else PRINT_IF_GUID(g, ff_asf_head2_guid);
  63. else PRINT_IF_GUID(g, ff_asf_my_guid);
  64. else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
  65. else PRINT_IF_GUID(g, ff_asf_extended_content_header);
  66. else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
  67. else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
  68. else PRINT_IF_GUID(g, ff_asf_metadata_header);
  69. else PRINT_IF_GUID(g, stream_bitrate_guid);
  70. else
  71. dprintf(NULL, "(GUID: unknown) ");
  72. for(i=0;i<16;i++)
  73. dprintf(NULL, " 0x%02x,", (*g)[i]);
  74. dprintf(NULL, "}\n");
  75. }
  76. #undef PRINT_IF_GUID
  77. #else
  78. #define print_guid(g)
  79. #endif
  80. static void get_guid(ByteIOContext *s, GUID *g)
  81. {
  82. assert(sizeof(*g) == 16);
  83. get_buffer(s, *g, sizeof(*g));
  84. }
  85. #if 0
  86. static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
  87. {
  88. int len, c;
  89. char *q;
  90. len = get_le16(pb);
  91. q = buf;
  92. while (len > 0) {
  93. c = get_le16(pb);
  94. if ((q - buf) < buf_size - 1)
  95. *q++ = c;
  96. len--;
  97. }
  98. *q = '\0';
  99. }
  100. #endif
  101. static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
  102. {
  103. char* q = buf;
  104. len /= 2;
  105. while (len--) {
  106. uint8_t tmp;
  107. PUT_UTF8(get_le16(pb), tmp, if (q - buf < buf_size - 1) *q++ = tmp;)
  108. }
  109. *q = '\0';
  110. }
  111. static int asf_probe(AVProbeData *pd)
  112. {
  113. /* check file header */
  114. if (!memcmp(pd->buf, &ff_asf_header, sizeof(GUID)))
  115. return AVPROBE_SCORE_MAX;
  116. else
  117. return 0;
  118. }
  119. static int get_value(ByteIOContext *pb, int type){
  120. switch(type){
  121. case 2: return get_le32(pb);
  122. case 3: return get_le32(pb);
  123. case 4: return get_le64(pb);
  124. case 5: return get_le16(pb);
  125. default:return INT_MIN;
  126. }
  127. }
  128. static void get_tag(AVFormatContext *s, const char *key, int type, int len)
  129. {
  130. char value[1024];
  131. if (type <= 1) { // unicode or byte
  132. get_str16_nolen(s->pb, len, value, sizeof(value));
  133. } else if (type <= 5) { // boolean or DWORD or QWORD or WORD
  134. uint64_t num = get_value(s->pb, type);
  135. snprintf(value, sizeof(value), "%"PRIu64, num);
  136. } else {
  137. url_fskip(s->pb, len);
  138. return;
  139. }
  140. if (strncmp(key, "WM/", 3))
  141. key += 3;
  142. av_metadata_set(&s->metadata, key, value);
  143. }
  144. static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  145. {
  146. ASFContext *asf = s->priv_data;
  147. GUID g;
  148. ByteIOContext *pb = s->pb;
  149. AVStream *st;
  150. ASFStream *asf_st;
  151. int size, i;
  152. int64_t gsize;
  153. AVRational dar[128];
  154. uint32_t bitrate[128];
  155. memset(dar, 0, sizeof(dar));
  156. memset(bitrate, 0, sizeof(bitrate));
  157. get_guid(pb, &g);
  158. if (memcmp(&g, &ff_asf_header, sizeof(GUID)))
  159. return -1;
  160. get_le64(pb);
  161. get_le32(pb);
  162. get_byte(pb);
  163. get_byte(pb);
  164. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  165. for(;;) {
  166. get_guid(pb, &g);
  167. gsize = get_le64(pb);
  168. dprintf(s, "%08"PRIx64": ", url_ftell(pb) - 24);
  169. print_guid(&g);
  170. dprintf(s, " size=0x%"PRIx64"\n", gsize);
  171. if (!memcmp(&g, &ff_asf_data_header, sizeof(GUID))) {
  172. asf->data_object_offset = url_ftell(pb);
  173. // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
  174. if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
  175. asf->data_object_size = gsize - 24;
  176. } else {
  177. asf->data_object_size = (uint64_t)-1;
  178. }
  179. break;
  180. }
  181. if (gsize < 24)
  182. return -1;
  183. if (!memcmp(&g, &ff_asf_file_header, sizeof(GUID))) {
  184. get_guid(pb, &asf->hdr.guid);
  185. asf->hdr.file_size = get_le64(pb);
  186. asf->hdr.create_time = get_le64(pb);
  187. asf->nb_packets = get_le64(pb);
  188. asf->hdr.play_time = get_le64(pb);
  189. asf->hdr.send_time = get_le64(pb);
  190. asf->hdr.preroll = get_le32(pb);
  191. asf->hdr.ignore = get_le32(pb);
  192. asf->hdr.flags = get_le32(pb);
  193. asf->hdr.min_pktsize = get_le32(pb);
  194. asf->hdr.max_pktsize = get_le32(pb);
  195. asf->hdr.max_bitrate = get_le32(pb);
  196. asf->packet_size = asf->hdr.max_pktsize;
  197. } else if (!memcmp(&g, &ff_asf_stream_header, sizeof(GUID))) {
  198. enum CodecType type;
  199. int type_specific_size, sizeX;
  200. uint64_t total_size;
  201. unsigned int tag1;
  202. int64_t pos1, pos2, start_time;
  203. int test_for_ext_stream_audio, is_dvr_ms_audio=0;
  204. pos1 = url_ftell(pb);
  205. st = av_new_stream(s, 0);
  206. if (!st)
  207. return AVERROR(ENOMEM);
  208. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  209. asf_st = av_mallocz(sizeof(ASFStream));
  210. if (!asf_st)
  211. return AVERROR(ENOMEM);
  212. st->priv_data = asf_st;
  213. start_time = asf->hdr.preroll;
  214. if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
  215. st->duration = asf->hdr.send_time /
  216. (10000000 / 1000) - start_time;
  217. }
  218. get_guid(pb, &g);
  219. test_for_ext_stream_audio = 0;
  220. if (!memcmp(&g, &ff_asf_audio_stream, sizeof(GUID))) {
  221. type = CODEC_TYPE_AUDIO;
  222. } else if (!memcmp(&g, &ff_asf_video_stream, sizeof(GUID))) {
  223. type = CODEC_TYPE_VIDEO;
  224. } else if (!memcmp(&g, &ff_asf_command_stream, sizeof(GUID))) {
  225. type = CODEC_TYPE_DATA;
  226. } else if (!memcmp(&g, &ff_asf_ext_stream_embed_stream_header, sizeof(GUID))) {
  227. test_for_ext_stream_audio = 1;
  228. type = CODEC_TYPE_UNKNOWN;
  229. } else {
  230. return -1;
  231. }
  232. get_guid(pb, &g);
  233. total_size = get_le64(pb);
  234. type_specific_size = get_le32(pb);
  235. get_le32(pb);
  236. st->id = get_le16(pb) & 0x7f; /* stream id */
  237. // mapping of asf ID to AV stream ID;
  238. asf->asfid2avid[st->id] = s->nb_streams - 1;
  239. get_le32(pb);
  240. if (test_for_ext_stream_audio) {
  241. get_guid(pb, &g);
  242. if (!memcmp(&g, &ff_asf_ext_stream_audio_stream, sizeof(GUID))) {
  243. type = CODEC_TYPE_AUDIO;
  244. is_dvr_ms_audio=1;
  245. get_guid(pb, &g);
  246. get_le32(pb);
  247. get_le32(pb);
  248. get_le32(pb);
  249. get_guid(pb, &g);
  250. get_le32(pb);
  251. }
  252. }
  253. st->codec->codec_type = type;
  254. if (type == CODEC_TYPE_AUDIO) {
  255. get_wav_header(pb, st->codec, type_specific_size);
  256. if (is_dvr_ms_audio) {
  257. // codec_id and codec_tag are unreliable in dvr_ms
  258. // files. Set them later by probing stream.
  259. st->codec->codec_id = CODEC_ID_PROBE;
  260. st->codec->codec_tag = 0;
  261. }
  262. if (st->codec->codec_id == CODEC_ID_AAC) {
  263. st->need_parsing = AVSTREAM_PARSE_NONE;
  264. } else {
  265. st->need_parsing = AVSTREAM_PARSE_FULL;
  266. }
  267. /* We have to init the frame size at some point .... */
  268. pos2 = url_ftell(pb);
  269. if (gsize >= (pos2 + 8 - pos1 + 24)) {
  270. asf_st->ds_span = get_byte(pb);
  271. asf_st->ds_packet_size = get_le16(pb);
  272. asf_st->ds_chunk_size = get_le16(pb);
  273. get_le16(pb); //ds_data_size
  274. get_byte(pb); //ds_silence_data
  275. }
  276. //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
  277. // asf_st->ds_packet_size, asf_st->ds_chunk_size,
  278. // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
  279. if (asf_st->ds_span > 1) {
  280. if (!asf_st->ds_chunk_size
  281. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
  282. || asf_st->ds_packet_size % asf_st->ds_chunk_size)
  283. asf_st->ds_span = 0; // disable descrambling
  284. }
  285. switch (st->codec->codec_id) {
  286. case CODEC_ID_MP3:
  287. st->codec->frame_size = MPA_FRAME_SIZE;
  288. break;
  289. case CODEC_ID_PCM_S16LE:
  290. case CODEC_ID_PCM_S16BE:
  291. case CODEC_ID_PCM_U16LE:
  292. case CODEC_ID_PCM_U16BE:
  293. case CODEC_ID_PCM_S8:
  294. case CODEC_ID_PCM_U8:
  295. case CODEC_ID_PCM_ALAW:
  296. case CODEC_ID_PCM_MULAW:
  297. st->codec->frame_size = 1;
  298. break;
  299. default:
  300. /* This is probably wrong, but it prevents a crash later */
  301. st->codec->frame_size = 1;
  302. break;
  303. }
  304. } else if (type == CODEC_TYPE_VIDEO) {
  305. get_le32(pb);
  306. get_le32(pb);
  307. get_byte(pb);
  308. size = get_le16(pb); /* size */
  309. sizeX= get_le32(pb); /* size */
  310. st->codec->width = get_le32(pb);
  311. st->codec->height = get_le32(pb);
  312. /* not available for asf */
  313. get_le16(pb); /* panes */
  314. st->codec->bits_per_coded_sample = get_le16(pb); /* depth */
  315. tag1 = get_le32(pb);
  316. url_fskip(pb, 20);
  317. // av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
  318. size= sizeX;
  319. if (size > 40) {
  320. st->codec->extradata_size = size - 40;
  321. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  322. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  323. }
  324. /* Extract palette from extradata if bpp <= 8 */
  325. /* This code assumes that extradata contains only palette */
  326. /* This is true for all paletted codecs implemented in ffmpeg */
  327. if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
  328. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  329. #ifdef WORDS_BIGENDIAN
  330. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  331. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  332. #else
  333. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  334. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  335. #endif
  336. st->codec->palctrl->palette_changed = 1;
  337. }
  338. st->codec->codec_tag = tag1;
  339. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  340. if(tag1 == MKTAG('D', 'V', 'R', ' '))
  341. st->need_parsing = AVSTREAM_PARSE_FULL;
  342. }
  343. pos2 = url_ftell(pb);
  344. url_fskip(pb, gsize - (pos2 - pos1 + 24));
  345. } else if (!memcmp(&g, &ff_asf_comment_header, sizeof(GUID))) {
  346. int len1, len2, len3, len4, len5;
  347. len1 = get_le16(pb);
  348. len2 = get_le16(pb);
  349. len3 = get_le16(pb);
  350. len4 = get_le16(pb);
  351. len5 = get_le16(pb);
  352. get_tag(s, "title" , 0, len1);
  353. get_tag(s, "author" , 0, len2);
  354. get_tag(s, "copyright", 0, len3);
  355. get_tag(s, "comment" , 0, len4);
  356. url_fskip(pb, len5);
  357. } else if (!memcmp(&g, &stream_bitrate_guid, sizeof(GUID))) {
  358. int stream_count = get_le16(pb);
  359. int j;
  360. // av_log(s, AV_LOG_ERROR, "stream bitrate properties\n");
  361. // av_log(s, AV_LOG_ERROR, "streams %d\n", streams);
  362. for(j = 0; j < stream_count; j++) {
  363. int flags, bitrate, stream_id;
  364. flags= get_le16(pb);
  365. bitrate= get_le32(pb);
  366. stream_id= (flags & 0x7f);
  367. // av_log(s, AV_LOG_ERROR, "flags: 0x%x stream id %d, bitrate %d\n", flags, stream_id, bitrate);
  368. asf->stream_bitrates[stream_id]= bitrate;
  369. }
  370. } else if (!memcmp(&g, &ff_asf_extended_content_header, sizeof(GUID))) {
  371. int desc_count, i;
  372. desc_count = get_le16(pb);
  373. for(i=0;i<desc_count;i++)
  374. {
  375. int name_len,value_type,value_len;
  376. char name[1024];
  377. name_len = get_le16(pb);
  378. get_str16_nolen(pb, name_len, name, sizeof(name));
  379. value_type = get_le16(pb);
  380. value_len = get_le16(pb);
  381. get_tag(s, name, value_type, value_len);
  382. }
  383. } else if (!memcmp(&g, &ff_asf_metadata_header, sizeof(GUID))) {
  384. int n, stream_num, name_len, value_len, value_type, value_num;
  385. n = get_le16(pb);
  386. for(i=0;i<n;i++) {
  387. char name[1024];
  388. get_le16(pb); //lang_list_index
  389. stream_num= get_le16(pb);
  390. name_len= get_le16(pb);
  391. value_type= get_le16(pb);
  392. value_len= get_le32(pb);
  393. get_str16_nolen(pb, name_len, name, sizeof(name));
  394. //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
  395. value_num= get_le16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
  396. url_fskip(pb, value_len - 2);
  397. if(stream_num<128){
  398. if (!strcmp(name, "AspectRatioX")) dar[stream_num].num= value_num;
  399. else if(!strcmp(name, "AspectRatioY")) dar[stream_num].den= value_num;
  400. }
  401. }
  402. } else if (!memcmp(&g, &ff_asf_ext_stream_header, sizeof(GUID))) {
  403. int ext_len, payload_ext_ct, stream_ct;
  404. uint32_t ext_d, leak_rate, stream_num;
  405. int64_t pos_ex_st;
  406. pos_ex_st = url_ftell(pb);
  407. get_le64(pb); // starttime
  408. get_le64(pb); // endtime
  409. leak_rate = get_le32(pb); // leak-datarate
  410. get_le32(pb); // bucket-datasize
  411. get_le32(pb); // init-bucket-fullness
  412. get_le32(pb); // alt-leak-datarate
  413. get_le32(pb); // alt-bucket-datasize
  414. get_le32(pb); // alt-init-bucket-fullness
  415. get_le32(pb); // max-object-size
  416. get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  417. stream_num = get_le16(pb); // stream-num
  418. get_le16(pb); // stream-language-id-index
  419. get_le64(pb); // avg frametime in 100ns units
  420. stream_ct = get_le16(pb); //stream-name-count
  421. payload_ext_ct = get_le16(pb); //payload-extension-system-count
  422. if (stream_num < 128)
  423. bitrate[stream_num] = leak_rate;
  424. for (i=0; i<stream_ct; i++){
  425. get_le16(pb);
  426. ext_len = get_le16(pb);
  427. url_fseek(pb, ext_len, SEEK_CUR);
  428. }
  429. for (i=0; i<payload_ext_ct; i++){
  430. get_guid(pb, &g);
  431. ext_d=get_le16(pb);
  432. ext_len=get_le32(pb);
  433. url_fseek(pb, ext_len, SEEK_CUR);
  434. }
  435. // there could be a optional stream properties object to follow
  436. // if so the next iteration will pick it up
  437. } else if (!memcmp(&g, &ff_asf_head1_guid, sizeof(GUID))) {
  438. int v1, v2;
  439. get_guid(pb, &g);
  440. v1 = get_le32(pb);
  441. v2 = get_le16(pb);
  442. #if 0
  443. } else if (!memcmp(&g, &ff_asf_codec_comment_header, sizeof(GUID))) {
  444. int len, v1, n, num;
  445. char str[256], *q;
  446. char tag[16];
  447. get_guid(pb, &g);
  448. print_guid(&g);
  449. n = get_le32(pb);
  450. for(i=0;i<n;i++) {
  451. num = get_le16(pb); /* stream number */
  452. get_str16(pb, str, sizeof(str));
  453. get_str16(pb, str, sizeof(str));
  454. len = get_le16(pb);
  455. q = tag;
  456. while (len > 0) {
  457. v1 = get_byte(pb);
  458. if ((q - tag) < sizeof(tag) - 1)
  459. *q++ = v1;
  460. len--;
  461. }
  462. *q = '\0';
  463. }
  464. #endif
  465. } else if (url_feof(pb)) {
  466. return -1;
  467. } else {
  468. url_fseek(pb, gsize - 24, SEEK_CUR);
  469. }
  470. }
  471. get_guid(pb, &g);
  472. get_le64(pb);
  473. get_byte(pb);
  474. get_byte(pb);
  475. if (url_feof(pb))
  476. return -1;
  477. asf->data_offset = url_ftell(pb);
  478. asf->packet_size_left = 0;
  479. for(i=0; i<128; i++){
  480. int stream_num= asf->asfid2avid[i];
  481. if(stream_num>=0){
  482. AVStream *st = s->streams[stream_num];
  483. if (!st->codec->bit_rate)
  484. st->codec->bit_rate = bitrate[i];
  485. if (dar[i].num > 0 && dar[i].den > 0)
  486. av_reduce(&st->sample_aspect_ratio.num,
  487. &st->sample_aspect_ratio.den,
  488. dar[i].num, dar[i].den, INT_MAX);
  489. //av_log(s, AV_LOG_ERROR, "dar %d:%d sar=%d:%d\n", dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  490. }
  491. }
  492. return 0;
  493. }
  494. #define DO_2BITS(bits, var, defval) \
  495. switch (bits & 3) \
  496. { \
  497. case 3: var = get_le32(pb); rsize += 4; break; \
  498. case 2: var = get_le16(pb); rsize += 2; break; \
  499. case 1: var = get_byte(pb); rsize++; break; \
  500. default: var = defval; break; \
  501. }
  502. int ff_asf_get_packet(AVFormatContext *s, ByteIOContext *pb)
  503. {
  504. ASFContext *asf = s->priv_data;
  505. uint32_t packet_length, padsize;
  506. int rsize = 8;
  507. int c, d, e, off;
  508. off= (url_ftell(pb) - s->data_offset) % asf->packet_size + 3;
  509. c=d=e=-1;
  510. while(off-- > 0){
  511. c=d; d=e;
  512. e= get_byte(pb);
  513. if(c == 0x82 && !d && !e)
  514. break;
  515. }
  516. if (c != 0x82) {
  517. if (!url_feof(pb))
  518. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  519. }
  520. if ((c & 0x8f) == 0x82) {
  521. if (d || e) {
  522. if (!url_feof(pb))
  523. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  524. return -1;
  525. }
  526. c= get_byte(pb);
  527. d= get_byte(pb);
  528. rsize+=3;
  529. }else{
  530. url_fseek(pb, -1, SEEK_CUR); //FIXME
  531. }
  532. asf->packet_flags = c;
  533. asf->packet_property = d;
  534. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  535. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  536. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  537. //the following checks prevent overflows and infinite loops
  538. if(packet_length >= (1U<<29)){
  539. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  540. return -1;
  541. }
  542. if(padsize >= packet_length){
  543. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  544. return -1;
  545. }
  546. asf->packet_timestamp = get_le32(pb);
  547. get_le16(pb); /* duration */
  548. // rsize has at least 11 bytes which have to be present
  549. if (asf->packet_flags & 0x01) {
  550. asf->packet_segsizetype = get_byte(pb); rsize++;
  551. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  552. } else {
  553. asf->packet_segments = 1;
  554. asf->packet_segsizetype = 0x80;
  555. }
  556. asf->packet_size_left = packet_length - padsize - rsize;
  557. if (packet_length < asf->hdr.min_pktsize)
  558. padsize += asf->hdr.min_pktsize - packet_length;
  559. asf->packet_padsize = padsize;
  560. dprintf(s, "packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  561. return 0;
  562. }
  563. /**
  564. *
  565. * @return <0 if error
  566. */
  567. static int asf_read_frame_header(AVFormatContext *s, ByteIOContext *pb){
  568. ASFContext *asf = s->priv_data;
  569. int rsize = 1;
  570. int num = get_byte(pb);
  571. int64_t ts0, ts1;
  572. asf->packet_segments--;
  573. asf->packet_key_frame = num >> 7;
  574. asf->stream_index = asf->asfid2avid[num & 0x7f];
  575. // sequence should be ignored!
  576. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  577. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  578. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  579. //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);
  580. if (asf->packet_replic_size >= 8) {
  581. asf->packet_obj_size = get_le32(pb);
  582. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  583. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  584. return -1;
  585. }
  586. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  587. if(asf->packet_replic_size >= 8+38+4){
  588. // for(i=0; i<asf->packet_replic_size-8; i++)
  589. // av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
  590. // av_log(s, AV_LOG_DEBUG, "\n");
  591. url_fskip(pb, 10);
  592. ts0= get_le64(pb);
  593. ts1= get_le64(pb);
  594. url_fskip(pb, 12);
  595. get_le32(pb);
  596. url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
  597. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  598. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  599. }else
  600. url_fskip(pb, asf->packet_replic_size - 8);
  601. rsize += asf->packet_replic_size; // FIXME - check validity
  602. } else if (asf->packet_replic_size==1){
  603. // multipacket - frag_offset is beginning timestamp
  604. asf->packet_time_start = asf->packet_frag_offset;
  605. asf->packet_frag_offset = 0;
  606. asf->packet_frag_timestamp = asf->packet_timestamp;
  607. asf->packet_time_delta = get_byte(pb);
  608. rsize++;
  609. }else if(asf->packet_replic_size!=0){
  610. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  611. return -1;
  612. }
  613. if (asf->packet_flags & 0x01) {
  614. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  615. if(asf->packet_frag_size > asf->packet_size_left - rsize){
  616. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
  617. return -1;
  618. }
  619. //printf("Fragsize %d\n", asf->packet_frag_size);
  620. } else {
  621. asf->packet_frag_size = asf->packet_size_left - rsize;
  622. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  623. }
  624. if (asf->packet_replic_size == 1) {
  625. asf->packet_multi_size = asf->packet_frag_size;
  626. if (asf->packet_multi_size > asf->packet_size_left)
  627. return -1;
  628. }
  629. asf->packet_size_left -= rsize;
  630. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  631. return 0;
  632. }
  633. int ff_asf_parse_packet(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
  634. {
  635. ASFContext *asf = s->priv_data;
  636. ASFStream *asf_st = 0;
  637. for (;;) {
  638. if(url_feof(pb))
  639. return AVERROR(EIO);
  640. if (asf->packet_size_left < FRAME_HEADER_SIZE
  641. || asf->packet_segments < 1) {
  642. //asf->packet_size_left <= asf->packet_padsize) {
  643. int ret = asf->packet_size_left + asf->packet_padsize;
  644. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  645. assert(ret>=0);
  646. /* fail safe */
  647. url_fskip(pb, ret);
  648. asf->packet_pos= url_ftell(pb);
  649. if (asf->data_object_size != (uint64_t)-1 &&
  650. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  651. return AVERROR(EIO); /* Do not exceed the size of the data object */
  652. return 1;
  653. }
  654. if (asf->packet_time_start == 0) {
  655. if(asf_read_frame_header(s, pb) < 0){
  656. asf->packet_segments= 0;
  657. continue;
  658. }
  659. if (asf->stream_index < 0
  660. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  661. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  662. ) {
  663. asf->packet_time_start = 0;
  664. /* unhandled packet (should not happen) */
  665. url_fskip(pb, asf->packet_frag_size);
  666. asf->packet_size_left -= asf->packet_frag_size;
  667. if(asf->stream_index < 0)
  668. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  669. continue;
  670. }
  671. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  672. }
  673. asf_st = asf->asf_st;
  674. if (asf->packet_replic_size == 1) {
  675. // frag_offset is here used as the beginning timestamp
  676. asf->packet_frag_timestamp = asf->packet_time_start;
  677. asf->packet_time_start += asf->packet_time_delta;
  678. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  679. asf->packet_size_left--;
  680. asf->packet_multi_size--;
  681. if (asf->packet_multi_size < asf->packet_obj_size)
  682. {
  683. asf->packet_time_start = 0;
  684. url_fskip(pb, asf->packet_multi_size);
  685. asf->packet_size_left -= asf->packet_multi_size;
  686. continue;
  687. }
  688. asf->packet_multi_size -= asf->packet_obj_size;
  689. //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);
  690. }
  691. if( /*asf->packet_frag_size == asf->packet_obj_size*/
  692. asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
  693. && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
  694. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  695. asf_st->frag_offset, asf->packet_frag_size,
  696. asf->packet_obj_size, asf_st->pkt.size);
  697. asf->packet_obj_size= asf_st->pkt.size;
  698. }
  699. if ( asf_st->pkt.size != asf->packet_obj_size
  700. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  701. if(asf_st->pkt.data){
  702. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  703. asf_st->frag_offset = 0;
  704. av_free_packet(&asf_st->pkt);
  705. }
  706. /* new packet */
  707. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  708. asf_st->seq = asf->packet_seq;
  709. asf_st->pkt.dts = asf->packet_frag_timestamp;
  710. asf_st->pkt.stream_index = asf->stream_index;
  711. asf_st->pkt.pos =
  712. asf_st->packet_pos= asf->packet_pos;
  713. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  714. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  715. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  716. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  717. asf->packet_key_frame = 1;
  718. if (asf->packet_key_frame)
  719. asf_st->pkt.flags |= PKT_FLAG_KEY;
  720. }
  721. /* read data */
  722. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  723. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  724. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  725. asf->packet_size_left -= asf->packet_frag_size;
  726. if (asf->packet_size_left < 0)
  727. continue;
  728. if( asf->packet_frag_offset >= asf_st->pkt.size
  729. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  730. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  731. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  732. continue;
  733. }
  734. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  735. asf->packet_frag_size);
  736. if (s->key && s->keylen == 20)
  737. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  738. asf->packet_frag_size);
  739. asf_st->frag_offset += asf->packet_frag_size;
  740. /* test if whole packet is read */
  741. if (asf_st->frag_offset == asf_st->pkt.size) {
  742. //workaround for macroshit radio DVR-MS files
  743. if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
  744. && asf_st->pkt.size > 100){
  745. int i;
  746. for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
  747. if(i == asf_st->pkt.size){
  748. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  749. asf_st->frag_offset = 0;
  750. av_free_packet(&asf_st->pkt);
  751. continue;
  752. }
  753. }
  754. /* return packet */
  755. if (asf_st->ds_span > 1) {
  756. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  757. 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);
  758. }else{
  759. /* packet descrambling */
  760. uint8_t *newdata = av_malloc(asf_st->pkt.size);
  761. if (newdata) {
  762. int offset = 0;
  763. while (offset < asf_st->pkt.size) {
  764. int off = offset / asf_st->ds_chunk_size;
  765. int row = off / asf_st->ds_span;
  766. int col = off % asf_st->ds_span;
  767. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  768. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  769. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  770. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  771. memcpy(newdata + offset,
  772. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  773. asf_st->ds_chunk_size);
  774. offset += asf_st->ds_chunk_size;
  775. }
  776. av_free(asf_st->pkt.data);
  777. asf_st->pkt.data = newdata;
  778. }
  779. }
  780. }
  781. asf_st->frag_offset = 0;
  782. *pkt= asf_st->pkt;
  783. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  784. asf_st->pkt.size = 0;
  785. asf_st->pkt.data = 0;
  786. break; // packet completed
  787. }
  788. }
  789. return 0;
  790. }
  791. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  792. {
  793. ASFContext *asf = s->priv_data;
  794. for (;;) {
  795. int ret;
  796. /* parse cached packets, if any */
  797. if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
  798. return ret;
  799. if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
  800. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  801. asf->packet_time_start = 0;
  802. }
  803. return 0;
  804. }
  805. // Added to support seeking after packets have been read
  806. // If information is not reset, read_packet fails due to
  807. // leftover information from previous reads
  808. static void asf_reset_header(AVFormatContext *s)
  809. {
  810. ASFContext *asf = s->priv_data;
  811. ASFStream *asf_st;
  812. int i;
  813. asf->packet_nb_frames = 0;
  814. asf->packet_size_left = 0;
  815. asf->packet_segments = 0;
  816. asf->packet_flags = 0;
  817. asf->packet_property = 0;
  818. asf->packet_timestamp = 0;
  819. asf->packet_segsizetype = 0;
  820. asf->packet_segments = 0;
  821. asf->packet_seq = 0;
  822. asf->packet_replic_size = 0;
  823. asf->packet_key_frame = 0;
  824. asf->packet_padsize = 0;
  825. asf->packet_frag_offset = 0;
  826. asf->packet_frag_size = 0;
  827. asf->packet_frag_timestamp = 0;
  828. asf->packet_multi_size = 0;
  829. asf->packet_obj_size = 0;
  830. asf->packet_time_delta = 0;
  831. asf->packet_time_start = 0;
  832. for(i=0; i<s->nb_streams; i++){
  833. asf_st= s->streams[i]->priv_data;
  834. av_free_packet(&asf_st->pkt);
  835. asf_st->frag_offset=0;
  836. asf_st->seq=0;
  837. }
  838. asf->asf_st= NULL;
  839. }
  840. static int asf_read_close(AVFormatContext *s)
  841. {
  842. int i;
  843. asf_reset_header(s);
  844. for(i=0;i<s->nb_streams;i++) {
  845. AVStream *st = s->streams[i];
  846. av_free(st->codec->palctrl);
  847. }
  848. return 0;
  849. }
  850. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  851. {
  852. ASFContext *asf = s->priv_data;
  853. AVPacket pkt1, *pkt = &pkt1;
  854. ASFStream *asf_st;
  855. int64_t pts;
  856. int64_t pos= *ppos;
  857. int i;
  858. int64_t start_pos[s->nb_streams];
  859. for(i=0; i<s->nb_streams; i++){
  860. start_pos[i]= pos;
  861. }
  862. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  863. *ppos= pos;
  864. url_fseek(s->pb, pos, SEEK_SET);
  865. //printf("asf_read_pts\n");
  866. asf_reset_header(s);
  867. for(;;){
  868. if (av_read_frame(s, pkt) < 0){
  869. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  870. return AV_NOPTS_VALUE;
  871. }
  872. pts= pkt->pts;
  873. av_free_packet(pkt);
  874. if(pkt->flags&PKT_FLAG_KEY){
  875. i= pkt->stream_index;
  876. asf_st= s->streams[i]->priv_data;
  877. // assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  878. pos= asf_st->packet_pos;
  879. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  880. start_pos[i]= asf_st->packet_pos + 1;
  881. if(pkt->stream_index == stream_index)
  882. break;
  883. }
  884. }
  885. *ppos= pos;
  886. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  887. return pts;
  888. }
  889. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  890. {
  891. GUID g;
  892. ASFContext *asf = s->priv_data;
  893. int64_t gsize, itime;
  894. int64_t pos, current_pos, index_pts;
  895. int i;
  896. int pct,ict;
  897. current_pos = url_ftell(s->pb);
  898. url_fseek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  899. get_guid(s->pb, &g);
  900. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  901. gsize = get_le64(s->pb);
  902. get_guid(s->pb, &g);
  903. itime=get_le64(s->pb);
  904. pct=get_le32(s->pb);
  905. ict=get_le32(s->pb);
  906. av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  907. for (i=0;i<ict;i++){
  908. int pktnum=get_le32(s->pb);
  909. int pktct =get_le16(s->pb);
  910. av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  911. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  912. index_pts=av_rescale(itime, i, 10000);
  913. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  914. }
  915. asf->index_read= 1;
  916. }
  917. url_fseek(s->pb, current_pos, SEEK_SET);
  918. }
  919. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  920. {
  921. ASFContext *asf = s->priv_data;
  922. AVStream *st = s->streams[stream_index];
  923. int64_t pos;
  924. int index;
  925. if (asf->packet_size <= 0)
  926. return -1;
  927. /* Try using the protocol's read_seek if available */
  928. if(s->pb) {
  929. int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
  930. if(ret >= 0)
  931. asf_reset_header(s);
  932. if (ret != AVERROR(ENOSYS))
  933. return ret;
  934. }
  935. if (!asf->index_read)
  936. asf_build_simple_index(s, stream_index);
  937. if(!(asf->index_read && st->index_entries)){
  938. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  939. return -1;
  940. }else{
  941. index= av_index_search_timestamp(st, pts, flags);
  942. if(index<0)
  943. return -1;
  944. /* find the position */
  945. pos = st->index_entries[index].pos;
  946. pts = st->index_entries[index].timestamp;
  947. // various attempts to find key frame have failed so far
  948. // asf_reset_header(s);
  949. // url_fseek(s->pb, pos, SEEK_SET);
  950. // key_pos = pos;
  951. // for(i=0;i<16;i++){
  952. // pos = url_ftell(s->pb);
  953. // if (av_read_frame(s, &pkt) < 0){
  954. // av_log(s, AV_LOG_INFO, "seek failed\n");
  955. // return -1;
  956. // }
  957. // asf_st = s->streams[stream_index]->priv_data;
  958. // pos += st->parser->frame_offset;
  959. //
  960. // if (pkt.size > b) {
  961. // b = pkt.size;
  962. // key_pos = pos;
  963. // }
  964. //
  965. // av_free_packet(&pkt);
  966. // }
  967. /* do the seek */
  968. av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  969. url_fseek(s->pb, pos, SEEK_SET);
  970. }
  971. asf_reset_header(s);
  972. return 0;
  973. }
  974. AVInputFormat asf_demuxer = {
  975. "asf",
  976. NULL_IF_CONFIG_SMALL("ASF format"),
  977. sizeof(ASFContext),
  978. asf_probe,
  979. asf_read_header,
  980. asf_read_packet,
  981. asf_read_close,
  982. asf_read_seek,
  983. asf_read_pts,
  984. .metadata_conv = ff_asf_metadata_conv,
  985. };