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.

1020 lines
36KB

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