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.

1008 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. codec->sample_aspect_ratio=
  446. av_div_q(
  447. dar[i],
  448. (AVRational){codec->width, codec->height}
  449. );
  450. //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);
  451. }
  452. }
  453. return 0;
  454. fail:
  455. for(i=0;i<s->nb_streams;i++) {
  456. AVStream *st = s->streams[i];
  457. if (st) {
  458. av_free(st->priv_data);
  459. av_free(st->codec->extradata);
  460. }
  461. av_free(st);
  462. }
  463. return -1;
  464. }
  465. #define DO_2BITS(bits, var, defval) \
  466. switch (bits & 3) \
  467. { \
  468. case 3: var = get_le32(pb); rsize += 4; break; \
  469. case 2: var = get_le16(pb); rsize += 2; break; \
  470. case 1: var = get_byte(pb); rsize++; break; \
  471. default: var = defval; break; \
  472. }
  473. /**
  474. *
  475. * @return <0 in case of an error
  476. */
  477. static int asf_get_packet(AVFormatContext *s)
  478. {
  479. ASFContext *asf = s->priv_data;
  480. ByteIOContext *pb = &s->pb;
  481. uint32_t packet_length, padsize;
  482. int rsize = 9;
  483. int c, d, e, off;
  484. off= (url_ftell(&s->pb) - s->data_offset) % asf->packet_size + 3;
  485. c=d=e=-1;
  486. while(off-- > 0){
  487. c=d; d=e;
  488. e= get_byte(pb);
  489. if(c == 0x82 && !d && !e)
  490. break;
  491. }
  492. if (c != 0x82) {
  493. if (!url_feof(pb))
  494. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  495. }
  496. if ((c & 0x0f) == 2) { // always true for now
  497. if (d || e) {
  498. if (!url_feof(pb))
  499. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  500. return -1;
  501. }
  502. d= get_byte(pb);
  503. e= get_byte(pb);
  504. rsize+=2;
  505. /* }else{
  506. if (!url_feof(pb))
  507. printf("ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  508. return AVERROR_IO;*/
  509. }
  510. asf->packet_flags = d;
  511. asf->packet_property = e;
  512. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  513. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  514. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  515. //the following checks prevent overflows and infinite loops
  516. if(packet_length >= (1U<<29)){
  517. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  518. return -1;
  519. }
  520. if(padsize >= packet_length){
  521. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  522. return -1;
  523. }
  524. asf->packet_timestamp = get_le32(pb);
  525. get_le16(pb); /* duration */
  526. // rsize has at least 11 bytes which have to be present
  527. if (asf->packet_flags & 0x01) {
  528. asf->packet_segsizetype = get_byte(pb); rsize++;
  529. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  530. } else {
  531. asf->packet_segments = 1;
  532. asf->packet_segsizetype = 0x80;
  533. }
  534. asf->packet_size_left = packet_length - padsize - rsize;
  535. if (packet_length < asf->hdr.min_pktsize)
  536. padsize += asf->hdr.min_pktsize - packet_length;
  537. asf->packet_padsize = padsize;
  538. #ifdef DEBUG
  539. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  540. #endif
  541. return 0;
  542. }
  543. /**
  544. *
  545. * @return <0 if error
  546. */
  547. static int asf_read_frame_header(AVFormatContext *s){
  548. ASFContext *asf = s->priv_data;
  549. ByteIOContext *pb = &s->pb;
  550. int rsize = 1;
  551. int num = get_byte(pb);
  552. asf->packet_segments--;
  553. asf->packet_key_frame = num >> 7;
  554. asf->stream_index = asf->asfid2avid[num & 0x7f];
  555. // sequence should be ignored!
  556. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  557. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  558. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  559. //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);
  560. if (asf->packet_replic_size >= 8) {
  561. asf->packet_obj_size = get_le32(pb);
  562. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  563. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  564. return -1;
  565. }
  566. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  567. url_fskip(pb, asf->packet_replic_size - 8);
  568. rsize += asf->packet_replic_size; // FIXME - check validity
  569. } else if (asf->packet_replic_size==1){
  570. // multipacket - frag_offset is begining timestamp
  571. asf->packet_time_start = asf->packet_frag_offset;
  572. asf->packet_frag_offset = 0;
  573. asf->packet_frag_timestamp = asf->packet_timestamp;
  574. asf->packet_time_delta = get_byte(pb);
  575. rsize++;
  576. }else if(asf->packet_replic_size!=0){
  577. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  578. return -1;
  579. }
  580. if (asf->packet_flags & 0x01) {
  581. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  582. if(asf->packet_frag_size > asf->packet_size_left - rsize){
  583. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
  584. return -1;
  585. }
  586. //printf("Fragsize %d\n", asf->packet_frag_size);
  587. } else {
  588. asf->packet_frag_size = asf->packet_size_left - rsize;
  589. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  590. }
  591. if (asf->packet_replic_size == 1) {
  592. asf->packet_multi_size = asf->packet_frag_size;
  593. if (asf->packet_multi_size > asf->packet_size_left)
  594. return -1;
  595. }
  596. asf->packet_size_left -= rsize;
  597. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  598. return 0;
  599. }
  600. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  601. {
  602. ASFContext *asf = s->priv_data;
  603. ASFStream *asf_st = 0;
  604. ByteIOContext *pb = &s->pb;
  605. //static int pc = 0;
  606. for (;;) {
  607. if(url_feof(pb))
  608. return AVERROR_IO;
  609. if (asf->packet_size_left < FRAME_HEADER_SIZE
  610. || asf->packet_segments < 1) {
  611. //asf->packet_size_left <= asf->packet_padsize) {
  612. int ret = asf->packet_size_left + asf->packet_padsize;
  613. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  614. assert(ret>=0);
  615. /* fail safe */
  616. url_fskip(pb, ret);
  617. asf->packet_pos= url_ftell(&s->pb);
  618. if (asf->data_object_size != (uint64_t)-1 &&
  619. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  620. return AVERROR_IO; /* Do not exceed the size of the data object */
  621. ret = asf_get_packet(s);
  622. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  623. if (ret < 0)
  624. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  625. asf->packet_time_start = 0;
  626. continue;
  627. }
  628. if (asf->packet_time_start == 0) {
  629. if(asf_read_frame_header(s) < 0){
  630. asf->packet_segments= 0;
  631. continue;
  632. }
  633. if (asf->stream_index < 0
  634. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  635. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  636. ) {
  637. asf->packet_time_start = 0;
  638. /* unhandled packet (should not happen) */
  639. url_fskip(pb, asf->packet_frag_size);
  640. asf->packet_size_left -= asf->packet_frag_size;
  641. if(asf->stream_index < 0)
  642. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  643. continue;
  644. }
  645. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  646. }
  647. asf_st = asf->asf_st;
  648. if (asf->packet_replic_size == 1) {
  649. // frag_offset is here used as the begining timestamp
  650. asf->packet_frag_timestamp = asf->packet_time_start;
  651. asf->packet_time_start += asf->packet_time_delta;
  652. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  653. asf->packet_size_left--;
  654. asf->packet_multi_size--;
  655. if (asf->packet_multi_size < asf->packet_obj_size)
  656. {
  657. asf->packet_time_start = 0;
  658. url_fskip(pb, asf->packet_multi_size);
  659. asf->packet_size_left -= asf->packet_multi_size;
  660. continue;
  661. }
  662. asf->packet_multi_size -= asf->packet_obj_size;
  663. //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);
  664. }
  665. if ( asf_st->pkt.size != asf->packet_obj_size
  666. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  667. if(asf_st->pkt.data){
  668. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  669. asf_st->frag_offset = 0;
  670. av_free_packet(&asf_st->pkt);
  671. }
  672. /* new packet */
  673. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  674. asf_st->seq = asf->packet_seq;
  675. asf_st->pkt.pts = asf->packet_frag_timestamp;
  676. asf_st->pkt.stream_index = asf->stream_index;
  677. asf_st->pkt.pos =
  678. asf_st->packet_pos= asf->packet_pos;
  679. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  680. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  681. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  682. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  683. asf->packet_key_frame = 1;
  684. if (asf->packet_key_frame)
  685. asf_st->pkt.flags |= PKT_FLAG_KEY;
  686. }
  687. /* read data */
  688. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  689. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  690. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  691. asf->packet_size_left -= asf->packet_frag_size;
  692. if (asf->packet_size_left < 0)
  693. continue;
  694. if( asf->packet_frag_offset >= asf_st->pkt.size
  695. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  696. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  697. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  698. continue;
  699. }
  700. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  701. asf->packet_frag_size);
  702. asf_st->frag_offset += asf->packet_frag_size;
  703. /* test if whole packet is read */
  704. if (asf_st->frag_offset == asf_st->pkt.size) {
  705. /* return packet */
  706. if (asf_st->ds_span > 1) {
  707. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  708. av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span\n");
  709. }else{
  710. /* packet descrambling */
  711. uint8_t *newdata = av_malloc(asf_st->pkt.size);
  712. if (newdata) {
  713. int offset = 0;
  714. while (offset < asf_st->pkt.size) {
  715. int off = offset / asf_st->ds_chunk_size;
  716. int row = off / asf_st->ds_span;
  717. int col = off % asf_st->ds_span;
  718. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  719. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  720. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  721. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  722. memcpy(newdata + offset,
  723. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  724. asf_st->ds_chunk_size);
  725. offset += asf_st->ds_chunk_size;
  726. }
  727. av_free(asf_st->pkt.data);
  728. asf_st->pkt.data = newdata;
  729. }
  730. }
  731. }
  732. asf_st->frag_offset = 0;
  733. *pkt= asf_st->pkt;
  734. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  735. asf_st->pkt.size = 0;
  736. asf_st->pkt.data = 0;
  737. break; // packet completed
  738. }
  739. }
  740. return 0;
  741. }
  742. static int asf_read_close(AVFormatContext *s)
  743. {
  744. int i;
  745. for(i=0;i<s->nb_streams;i++) {
  746. AVStream *st = s->streams[i];
  747. av_free(st->priv_data);
  748. av_free(st->codec->palctrl);
  749. }
  750. return 0;
  751. }
  752. // Added to support seeking after packets have been read
  753. // If information is not reset, read_packet fails due to
  754. // leftover information from previous reads
  755. static void asf_reset_header(AVFormatContext *s)
  756. {
  757. ASFContext *asf = s->priv_data;
  758. ASFStream *asf_st;
  759. int i;
  760. asf->packet_nb_frames = 0;
  761. asf->packet_size_left = 0;
  762. asf->packet_segments = 0;
  763. asf->packet_flags = 0;
  764. asf->packet_property = 0;
  765. asf->packet_timestamp = 0;
  766. asf->packet_segsizetype = 0;
  767. asf->packet_segments = 0;
  768. asf->packet_seq = 0;
  769. asf->packet_replic_size = 0;
  770. asf->packet_key_frame = 0;
  771. asf->packet_padsize = 0;
  772. asf->packet_frag_offset = 0;
  773. asf->packet_frag_size = 0;
  774. asf->packet_frag_timestamp = 0;
  775. asf->packet_multi_size = 0;
  776. asf->packet_obj_size = 0;
  777. asf->packet_time_delta = 0;
  778. asf->packet_time_start = 0;
  779. for(i=0; i<s->nb_streams; i++){
  780. asf_st= s->streams[i]->priv_data;
  781. av_free_packet(&asf_st->pkt);
  782. asf_st->frag_offset=0;
  783. asf_st->seq=0;
  784. }
  785. asf->asf_st= NULL;
  786. }
  787. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  788. {
  789. ASFContext *asf = s->priv_data;
  790. AVPacket pkt1, *pkt = &pkt1;
  791. ASFStream *asf_st;
  792. int64_t pts;
  793. int64_t pos= *ppos;
  794. int i;
  795. int64_t start_pos[s->nb_streams];
  796. for(i=0; i<s->nb_streams; i++){
  797. start_pos[i]= pos;
  798. }
  799. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  800. *ppos= pos;
  801. url_fseek(&s->pb, pos, SEEK_SET);
  802. //printf("asf_read_pts\n");
  803. asf_reset_header(s);
  804. for(;;){
  805. if (av_read_frame(s, pkt) < 0){
  806. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  807. return AV_NOPTS_VALUE;
  808. }
  809. pts= pkt->pts;
  810. av_free_packet(pkt);
  811. if(pkt->flags&PKT_FLAG_KEY){
  812. i= pkt->stream_index;
  813. asf_st= s->streams[i]->priv_data;
  814. // assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  815. pos= asf_st->packet_pos;
  816. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  817. start_pos[i]= asf_st->packet_pos + 1;
  818. if(pkt->stream_index == stream_index)
  819. break;
  820. }
  821. }
  822. *ppos= pos;
  823. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  824. return pts;
  825. }
  826. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  827. {
  828. GUID g;
  829. ASFContext *asf = s->priv_data;
  830. int64_t gsize, itime;
  831. int64_t pos, current_pos, index_pts;
  832. int i;
  833. int pct,ict;
  834. current_pos = url_ftell(&s->pb);
  835. url_fseek(&s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  836. get_guid(&s->pb, &g);
  837. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  838. gsize = get_le64(&s->pb);
  839. get_guid(&s->pb, &g);
  840. itime=get_le64(&s->pb);
  841. pct=get_le32(&s->pb);
  842. ict=get_le32(&s->pb);
  843. av_log(NULL, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  844. for (i=0;i<ict;i++){
  845. int pktnum=get_le32(&s->pb);
  846. int pktct =get_le16(&s->pb);
  847. av_log(NULL, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  848. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  849. index_pts=av_rescale(itime, i, 10000);
  850. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  851. }
  852. asf->index_read= 1;
  853. }
  854. url_fseek(&s->pb, current_pos, SEEK_SET);
  855. }
  856. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  857. {
  858. ASFContext *asf = s->priv_data;
  859. AVStream *st = s->streams[stream_index];
  860. int64_t pos;
  861. int index;
  862. if (asf->packet_size <= 0)
  863. return -1;
  864. if (!asf->index_read)
  865. asf_build_simple_index(s, stream_index);
  866. if(!(asf->index_read && st->index_entries)){
  867. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  868. return -1;
  869. }else{
  870. index= av_index_search_timestamp(st, pts, flags);
  871. if(index<0)
  872. return -1;
  873. /* find the position */
  874. pos = st->index_entries[index].pos;
  875. pts = st->index_entries[index].timestamp;
  876. // various attempts to find key frame have failed so far
  877. // asf_reset_header(s);
  878. // url_fseek(&s->pb, pos, SEEK_SET);
  879. // key_pos = pos;
  880. // for(i=0;i<16;i++){
  881. // pos = url_ftell(&s->pb);
  882. // if (av_read_frame(s, &pkt) < 0){
  883. // av_log(s, AV_LOG_INFO, "seek failed\n");
  884. // return -1;
  885. // }
  886. // asf_st = s->streams[stream_index]->priv_data;
  887. // pos += st->parser->frame_offset;
  888. //
  889. // if (pkt.size > b) {
  890. // b = pkt.size;
  891. // key_pos = pos;
  892. // }
  893. //
  894. // av_free_packet(&pkt);
  895. // }
  896. /* do the seek */
  897. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  898. url_fseek(&s->pb, pos, SEEK_SET);
  899. }
  900. asf_reset_header(s);
  901. return 0;
  902. }
  903. AVInputFormat asf_demuxer = {
  904. "asf",
  905. "asf format",
  906. sizeof(ASFContext),
  907. asf_probe,
  908. asf_read_header,
  909. asf_read_packet,
  910. asf_read_close,
  911. asf_read_seek,
  912. asf_read_pts,
  913. };