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.

1091 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. int name_len,value_type,value_len;
  375. char name[1024];
  376. name_len = get_le16(pb);
  377. get_str16_nolen(pb, name_len, name, sizeof(name));
  378. value_type = get_le16(pb);
  379. value_len = get_le16(pb);
  380. get_tag(s, name, value_type, value_len);
  381. }
  382. } else if (!memcmp(&g, &ff_asf_metadata_header, sizeof(GUID))) {
  383. int n, stream_num, name_len, value_len, value_type, value_num;
  384. n = get_le16(pb);
  385. for(i=0;i<n;i++) {
  386. char name[1024];
  387. get_le16(pb); //lang_list_index
  388. stream_num= get_le16(pb);
  389. name_len= get_le16(pb);
  390. value_type= get_le16(pb);
  391. value_len= get_le32(pb);
  392. get_str16_nolen(pb, name_len, name, sizeof(name));
  393. //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
  394. value_num= get_le16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
  395. url_fskip(pb, value_len - 2);
  396. if(stream_num<128){
  397. if (!strcmp(name, "AspectRatioX")) dar[stream_num].num= value_num;
  398. else if(!strcmp(name, "AspectRatioY")) dar[stream_num].den= value_num;
  399. }
  400. }
  401. } else if (!memcmp(&g, &ff_asf_ext_stream_header, sizeof(GUID))) {
  402. int ext_len, payload_ext_ct, stream_ct;
  403. uint32_t ext_d, leak_rate, stream_num;
  404. int64_t pos_ex_st;
  405. pos_ex_st = url_ftell(pb);
  406. get_le64(pb); // starttime
  407. get_le64(pb); // endtime
  408. leak_rate = get_le32(pb); // leak-datarate
  409. get_le32(pb); // bucket-datasize
  410. get_le32(pb); // init-bucket-fullness
  411. get_le32(pb); // alt-leak-datarate
  412. get_le32(pb); // alt-bucket-datasize
  413. get_le32(pb); // alt-init-bucket-fullness
  414. get_le32(pb); // max-object-size
  415. get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  416. stream_num = get_le16(pb); // stream-num
  417. get_le16(pb); // stream-language-id-index
  418. get_le64(pb); // avg frametime in 100ns units
  419. stream_ct = get_le16(pb); //stream-name-count
  420. payload_ext_ct = get_le16(pb); //payload-extension-system-count
  421. if (stream_num < 128)
  422. bitrate[stream_num] = leak_rate;
  423. for (i=0; i<stream_ct; i++){
  424. get_le16(pb);
  425. ext_len = get_le16(pb);
  426. url_fseek(pb, ext_len, SEEK_CUR);
  427. }
  428. for (i=0; i<payload_ext_ct; i++){
  429. get_guid(pb, &g);
  430. ext_d=get_le16(pb);
  431. ext_len=get_le32(pb);
  432. url_fseek(pb, ext_len, SEEK_CUR);
  433. }
  434. // there could be a optional stream properties object to follow
  435. // if so the next iteration will pick it up
  436. } else if (!memcmp(&g, &ff_asf_head1_guid, sizeof(GUID))) {
  437. int v1, v2;
  438. get_guid(pb, &g);
  439. v1 = get_le32(pb);
  440. v2 = get_le16(pb);
  441. #if 0
  442. } else if (!memcmp(&g, &ff_asf_codec_comment_header, sizeof(GUID))) {
  443. int len, v1, n, num;
  444. char str[256], *q;
  445. char tag[16];
  446. get_guid(pb, &g);
  447. print_guid(&g);
  448. n = get_le32(pb);
  449. for(i=0;i<n;i++) {
  450. num = get_le16(pb); /* stream number */
  451. get_str16(pb, str, sizeof(str));
  452. get_str16(pb, str, sizeof(str));
  453. len = get_le16(pb);
  454. q = tag;
  455. while (len > 0) {
  456. v1 = get_byte(pb);
  457. if ((q - tag) < sizeof(tag) - 1)
  458. *q++ = v1;
  459. len--;
  460. }
  461. *q = '\0';
  462. }
  463. #endif
  464. } else if (url_feof(pb)) {
  465. return -1;
  466. } else {
  467. url_fseek(pb, gsize - 24, SEEK_CUR);
  468. }
  469. }
  470. get_guid(pb, &g);
  471. get_le64(pb);
  472. get_byte(pb);
  473. get_byte(pb);
  474. if (url_feof(pb))
  475. return -1;
  476. asf->data_offset = url_ftell(pb);
  477. asf->packet_size_left = 0;
  478. for(i=0; i<128; i++){
  479. int stream_num= asf->asfid2avid[i];
  480. if(stream_num>=0){
  481. AVStream *st = s->streams[stream_num];
  482. if (!st->codec->bit_rate)
  483. st->codec->bit_rate = bitrate[i];
  484. if (dar[i].num > 0 && dar[i].den > 0)
  485. av_reduce(&st->sample_aspect_ratio.num,
  486. &st->sample_aspect_ratio.den,
  487. dar[i].num, dar[i].den, INT_MAX);
  488. //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);
  489. }
  490. }
  491. return 0;
  492. }
  493. #define DO_2BITS(bits, var, defval) \
  494. switch (bits & 3) \
  495. { \
  496. case 3: var = get_le32(pb); rsize += 4; break; \
  497. case 2: var = get_le16(pb); rsize += 2; break; \
  498. case 1: var = get_byte(pb); rsize++; break; \
  499. default: var = defval; break; \
  500. }
  501. int ff_asf_get_packet(AVFormatContext *s, ByteIOContext *pb)
  502. {
  503. ASFContext *asf = s->priv_data;
  504. uint32_t packet_length, padsize;
  505. int rsize = 8;
  506. int c, d, e, off;
  507. off= (url_ftell(pb) - s->data_offset) % asf->packet_size + 3;
  508. c=d=e=-1;
  509. while(off-- > 0){
  510. c=d; d=e;
  511. e= get_byte(pb);
  512. if(c == 0x82 && !d && !e)
  513. break;
  514. }
  515. if (c != 0x82) {
  516. if (!url_feof(pb))
  517. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  518. }
  519. if ((c & 0x8f) == 0x82) {
  520. if (d || e) {
  521. if (!url_feof(pb))
  522. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  523. return -1;
  524. }
  525. c= get_byte(pb);
  526. d= get_byte(pb);
  527. rsize+=3;
  528. }else{
  529. url_fseek(pb, -1, SEEK_CUR); //FIXME
  530. }
  531. asf->packet_flags = c;
  532. asf->packet_property = d;
  533. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  534. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  535. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  536. //the following checks prevent overflows and infinite loops
  537. if(packet_length >= (1U<<29)){
  538. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  539. return -1;
  540. }
  541. if(padsize >= packet_length){
  542. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  543. return -1;
  544. }
  545. asf->packet_timestamp = get_le32(pb);
  546. get_le16(pb); /* duration */
  547. // rsize has at least 11 bytes which have to be present
  548. if (asf->packet_flags & 0x01) {
  549. asf->packet_segsizetype = get_byte(pb); rsize++;
  550. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  551. } else {
  552. asf->packet_segments = 1;
  553. asf->packet_segsizetype = 0x80;
  554. }
  555. asf->packet_size_left = packet_length - padsize - rsize;
  556. if (packet_length < asf->hdr.min_pktsize)
  557. padsize += asf->hdr.min_pktsize - packet_length;
  558. asf->packet_padsize = padsize;
  559. dprintf(s, "packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  560. return 0;
  561. }
  562. /**
  563. *
  564. * @return <0 if error
  565. */
  566. static int asf_read_frame_header(AVFormatContext *s, ByteIOContext *pb){
  567. ASFContext *asf = s->priv_data;
  568. int rsize = 1;
  569. int num = get_byte(pb);
  570. int64_t ts0, ts1;
  571. asf->packet_segments--;
  572. asf->packet_key_frame = num >> 7;
  573. asf->stream_index = asf->asfid2avid[num & 0x7f];
  574. // sequence should be ignored!
  575. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  576. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  577. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  578. //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);
  579. if (asf->packet_replic_size >= 8) {
  580. asf->packet_obj_size = get_le32(pb);
  581. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  582. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  583. return -1;
  584. }
  585. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  586. if(asf->packet_replic_size >= 8+38+4){
  587. // for(i=0; i<asf->packet_replic_size-8; i++)
  588. // av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
  589. // av_log(s, AV_LOG_DEBUG, "\n");
  590. url_fskip(pb, 10);
  591. ts0= get_le64(pb);
  592. ts1= get_le64(pb);
  593. url_fskip(pb, 12);
  594. get_le32(pb);
  595. url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
  596. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  597. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  598. }else
  599. url_fskip(pb, asf->packet_replic_size - 8);
  600. rsize += asf->packet_replic_size; // FIXME - check validity
  601. } else if (asf->packet_replic_size==1){
  602. // multipacket - frag_offset is beginning timestamp
  603. asf->packet_time_start = asf->packet_frag_offset;
  604. asf->packet_frag_offset = 0;
  605. asf->packet_frag_timestamp = asf->packet_timestamp;
  606. asf->packet_time_delta = get_byte(pb);
  607. rsize++;
  608. }else if(asf->packet_replic_size!=0){
  609. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  610. return -1;
  611. }
  612. if (asf->packet_flags & 0x01) {
  613. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  614. if(asf->packet_frag_size > asf->packet_size_left - rsize){
  615. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
  616. return -1;
  617. }
  618. //printf("Fragsize %d\n", asf->packet_frag_size);
  619. } else {
  620. asf->packet_frag_size = asf->packet_size_left - rsize;
  621. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  622. }
  623. if (asf->packet_replic_size == 1) {
  624. asf->packet_multi_size = asf->packet_frag_size;
  625. if (asf->packet_multi_size > asf->packet_size_left)
  626. return -1;
  627. }
  628. asf->packet_size_left -= rsize;
  629. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  630. return 0;
  631. }
  632. int ff_asf_parse_packet(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
  633. {
  634. ASFContext *asf = s->priv_data;
  635. ASFStream *asf_st = 0;
  636. for (;;) {
  637. if(url_feof(pb))
  638. return AVERROR(EIO);
  639. if (asf->packet_size_left < FRAME_HEADER_SIZE
  640. || asf->packet_segments < 1) {
  641. //asf->packet_size_left <= asf->packet_padsize) {
  642. int ret = asf->packet_size_left + asf->packet_padsize;
  643. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  644. assert(ret>=0);
  645. /* fail safe */
  646. url_fskip(pb, ret);
  647. asf->packet_pos= url_ftell(pb);
  648. if (asf->data_object_size != (uint64_t)-1 &&
  649. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  650. return AVERROR(EIO); /* Do not exceed the size of the data object */
  651. return 1;
  652. }
  653. if (asf->packet_time_start == 0) {
  654. if(asf_read_frame_header(s, pb) < 0){
  655. asf->packet_segments= 0;
  656. continue;
  657. }
  658. if (asf->stream_index < 0
  659. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  660. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  661. ) {
  662. asf->packet_time_start = 0;
  663. /* unhandled packet (should not happen) */
  664. url_fskip(pb, asf->packet_frag_size);
  665. asf->packet_size_left -= asf->packet_frag_size;
  666. if(asf->stream_index < 0)
  667. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  668. continue;
  669. }
  670. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  671. }
  672. asf_st = asf->asf_st;
  673. if (asf->packet_replic_size == 1) {
  674. // frag_offset is here used as the beginning timestamp
  675. asf->packet_frag_timestamp = asf->packet_time_start;
  676. asf->packet_time_start += asf->packet_time_delta;
  677. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  678. asf->packet_size_left--;
  679. asf->packet_multi_size--;
  680. if (asf->packet_multi_size < asf->packet_obj_size)
  681. {
  682. asf->packet_time_start = 0;
  683. url_fskip(pb, asf->packet_multi_size);
  684. asf->packet_size_left -= asf->packet_multi_size;
  685. continue;
  686. }
  687. asf->packet_multi_size -= asf->packet_obj_size;
  688. //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);
  689. }
  690. if( /*asf->packet_frag_size == asf->packet_obj_size*/
  691. asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
  692. && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
  693. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  694. asf_st->frag_offset, asf->packet_frag_size,
  695. asf->packet_obj_size, asf_st->pkt.size);
  696. asf->packet_obj_size= asf_st->pkt.size;
  697. }
  698. if ( asf_st->pkt.size != asf->packet_obj_size
  699. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  700. if(asf_st->pkt.data){
  701. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  702. asf_st->frag_offset = 0;
  703. av_free_packet(&asf_st->pkt);
  704. }
  705. /* new packet */
  706. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  707. asf_st->seq = asf->packet_seq;
  708. asf_st->pkt.dts = asf->packet_frag_timestamp;
  709. asf_st->pkt.stream_index = asf->stream_index;
  710. asf_st->pkt.pos =
  711. asf_st->packet_pos= asf->packet_pos;
  712. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  713. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  714. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  715. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  716. asf->packet_key_frame = 1;
  717. if (asf->packet_key_frame)
  718. asf_st->pkt.flags |= PKT_FLAG_KEY;
  719. }
  720. /* read data */
  721. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  722. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  723. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  724. asf->packet_size_left -= asf->packet_frag_size;
  725. if (asf->packet_size_left < 0)
  726. continue;
  727. if( asf->packet_frag_offset >= asf_st->pkt.size
  728. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  729. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  730. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  731. continue;
  732. }
  733. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  734. asf->packet_frag_size);
  735. if (s->key && s->keylen == 20)
  736. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  737. asf->packet_frag_size);
  738. asf_st->frag_offset += asf->packet_frag_size;
  739. /* test if whole packet is read */
  740. if (asf_st->frag_offset == asf_st->pkt.size) {
  741. //workaround for macroshit radio DVR-MS files
  742. if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
  743. && asf_st->pkt.size > 100){
  744. int i;
  745. for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
  746. if(i == asf_st->pkt.size){
  747. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  748. asf_st->frag_offset = 0;
  749. av_free_packet(&asf_st->pkt);
  750. continue;
  751. }
  752. }
  753. /* return packet */
  754. if (asf_st->ds_span > 1) {
  755. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  756. 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);
  757. }else{
  758. /* packet descrambling */
  759. uint8_t *newdata = av_malloc(asf_st->pkt.size);
  760. if (newdata) {
  761. int offset = 0;
  762. while (offset < asf_st->pkt.size) {
  763. int off = offset / asf_st->ds_chunk_size;
  764. int row = off / asf_st->ds_span;
  765. int col = off % asf_st->ds_span;
  766. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  767. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  768. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  769. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  770. memcpy(newdata + offset,
  771. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  772. asf_st->ds_chunk_size);
  773. offset += asf_st->ds_chunk_size;
  774. }
  775. av_free(asf_st->pkt.data);
  776. asf_st->pkt.data = newdata;
  777. }
  778. }
  779. }
  780. asf_st->frag_offset = 0;
  781. *pkt= asf_st->pkt;
  782. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  783. asf_st->pkt.size = 0;
  784. asf_st->pkt.data = 0;
  785. break; // packet completed
  786. }
  787. }
  788. return 0;
  789. }
  790. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  791. {
  792. ASFContext *asf = s->priv_data;
  793. for (;;) {
  794. int ret;
  795. /* parse cached packets, if any */
  796. if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
  797. return ret;
  798. if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
  799. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  800. asf->packet_time_start = 0;
  801. }
  802. return 0;
  803. }
  804. // Added to support seeking after packets have been read
  805. // If information is not reset, read_packet fails due to
  806. // leftover information from previous reads
  807. static void asf_reset_header(AVFormatContext *s)
  808. {
  809. ASFContext *asf = s->priv_data;
  810. ASFStream *asf_st;
  811. int i;
  812. asf->packet_nb_frames = 0;
  813. asf->packet_size_left = 0;
  814. asf->packet_segments = 0;
  815. asf->packet_flags = 0;
  816. asf->packet_property = 0;
  817. asf->packet_timestamp = 0;
  818. asf->packet_segsizetype = 0;
  819. asf->packet_segments = 0;
  820. asf->packet_seq = 0;
  821. asf->packet_replic_size = 0;
  822. asf->packet_key_frame = 0;
  823. asf->packet_padsize = 0;
  824. asf->packet_frag_offset = 0;
  825. asf->packet_frag_size = 0;
  826. asf->packet_frag_timestamp = 0;
  827. asf->packet_multi_size = 0;
  828. asf->packet_obj_size = 0;
  829. asf->packet_time_delta = 0;
  830. asf->packet_time_start = 0;
  831. for(i=0; i<s->nb_streams; i++){
  832. asf_st= s->streams[i]->priv_data;
  833. av_free_packet(&asf_st->pkt);
  834. asf_st->frag_offset=0;
  835. asf_st->seq=0;
  836. }
  837. asf->asf_st= NULL;
  838. }
  839. static int asf_read_close(AVFormatContext *s)
  840. {
  841. int i;
  842. asf_reset_header(s);
  843. for(i=0;i<s->nb_streams;i++) {
  844. AVStream *st = s->streams[i];
  845. av_free(st->codec->palctrl);
  846. }
  847. return 0;
  848. }
  849. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  850. {
  851. ASFContext *asf = s->priv_data;
  852. AVPacket pkt1, *pkt = &pkt1;
  853. ASFStream *asf_st;
  854. int64_t pts;
  855. int64_t pos= *ppos;
  856. int i;
  857. int64_t start_pos[s->nb_streams];
  858. for(i=0; i<s->nb_streams; i++){
  859. start_pos[i]= pos;
  860. }
  861. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  862. *ppos= pos;
  863. url_fseek(s->pb, pos, SEEK_SET);
  864. //printf("asf_read_pts\n");
  865. asf_reset_header(s);
  866. for(;;){
  867. if (av_read_frame(s, pkt) < 0){
  868. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  869. return AV_NOPTS_VALUE;
  870. }
  871. pts= pkt->pts;
  872. av_free_packet(pkt);
  873. if(pkt->flags&PKT_FLAG_KEY){
  874. i= pkt->stream_index;
  875. asf_st= s->streams[i]->priv_data;
  876. // assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  877. pos= asf_st->packet_pos;
  878. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  879. start_pos[i]= asf_st->packet_pos + 1;
  880. if(pkt->stream_index == stream_index)
  881. break;
  882. }
  883. }
  884. *ppos= pos;
  885. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  886. return pts;
  887. }
  888. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  889. {
  890. GUID g;
  891. ASFContext *asf = s->priv_data;
  892. int64_t gsize, itime;
  893. int64_t pos, current_pos, index_pts;
  894. int i;
  895. int pct,ict;
  896. current_pos = url_ftell(s->pb);
  897. url_fseek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  898. get_guid(s->pb, &g);
  899. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  900. gsize = get_le64(s->pb);
  901. get_guid(s->pb, &g);
  902. itime=get_le64(s->pb);
  903. pct=get_le32(s->pb);
  904. ict=get_le32(s->pb);
  905. av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  906. for (i=0;i<ict;i++){
  907. int pktnum=get_le32(s->pb);
  908. int pktct =get_le16(s->pb);
  909. av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  910. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  911. index_pts=av_rescale(itime, i, 10000);
  912. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  913. }
  914. asf->index_read= 1;
  915. }
  916. url_fseek(s->pb, current_pos, SEEK_SET);
  917. }
  918. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  919. {
  920. ASFContext *asf = s->priv_data;
  921. AVStream *st = s->streams[stream_index];
  922. int64_t pos;
  923. int index;
  924. if (asf->packet_size <= 0)
  925. return -1;
  926. /* Try using the protocol's read_seek if available */
  927. if(s->pb) {
  928. int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
  929. if(ret >= 0)
  930. asf_reset_header(s);
  931. if (ret != AVERROR(ENOSYS))
  932. return ret;
  933. }
  934. if (!asf->index_read)
  935. asf_build_simple_index(s, stream_index);
  936. if(!(asf->index_read && st->index_entries)){
  937. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  938. return -1;
  939. }else{
  940. index= av_index_search_timestamp(st, pts, flags);
  941. if(index<0)
  942. return -1;
  943. /* find the position */
  944. pos = st->index_entries[index].pos;
  945. pts = st->index_entries[index].timestamp;
  946. // various attempts to find key frame have failed so far
  947. // asf_reset_header(s);
  948. // url_fseek(s->pb, pos, SEEK_SET);
  949. // key_pos = pos;
  950. // for(i=0;i<16;i++){
  951. // pos = url_ftell(s->pb);
  952. // if (av_read_frame(s, &pkt) < 0){
  953. // av_log(s, AV_LOG_INFO, "seek failed\n");
  954. // return -1;
  955. // }
  956. // asf_st = s->streams[stream_index]->priv_data;
  957. // pos += st->parser->frame_offset;
  958. //
  959. // if (pkt.size > b) {
  960. // b = pkt.size;
  961. // key_pos = pos;
  962. // }
  963. //
  964. // av_free_packet(&pkt);
  965. // }
  966. /* do the seek */
  967. av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  968. url_fseek(s->pb, pos, SEEK_SET);
  969. }
  970. asf_reset_header(s);
  971. return 0;
  972. }
  973. AVInputFormat asf_demuxer = {
  974. "asf",
  975. NULL_IF_CONFIG_SMALL("ASF format"),
  976. sizeof(ASFContext),
  977. asf_probe,
  978. asf_read_header,
  979. asf_read_packet,
  980. asf_read_close,
  981. asf_read_seek,
  982. asf_read_pts,
  983. .metadata_conv = ff_asf_metadata_conv,
  984. };