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.

1088 lines
40KB

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