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.

959 lines
34KB

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