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.

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