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.

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