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.

954 lines
34KB

  1. /*
  2. * ASF compatible decoder.
  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. #undef NDEBUG
  26. #include <assert.h>
  27. #define FRAME_HEADER_SIZE 17
  28. // Fix Me! FRAME_HEADER_SIZE may be different.
  29. static const GUID index_guid = {
  30. 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
  31. };
  32. /**********************************/
  33. /* decoding */
  34. //#define DEBUG
  35. #ifdef DEBUG
  36. #define PRINT_IF_GUID(g,cmp) \
  37. if (!memcmp(g, &cmp, sizeof(GUID))) \
  38. printf("(GUID: %s) ", #cmp)
  39. static void print_guid(const GUID *g)
  40. {
  41. int i;
  42. PRINT_IF_GUID(g, asf_header);
  43. else PRINT_IF_GUID(g, file_header);
  44. else PRINT_IF_GUID(g, stream_header);
  45. else PRINT_IF_GUID(g, audio_stream);
  46. else PRINT_IF_GUID(g, audio_conceal_none);
  47. else PRINT_IF_GUID(g, video_stream);
  48. else PRINT_IF_GUID(g, video_conceal_none);
  49. else PRINT_IF_GUID(g, command_stream);
  50. else PRINT_IF_GUID(g, comment_header);
  51. else PRINT_IF_GUID(g, codec_comment_header);
  52. else PRINT_IF_GUID(g, codec_comment1_header);
  53. else PRINT_IF_GUID(g, data_header);
  54. else PRINT_IF_GUID(g, index_guid);
  55. else PRINT_IF_GUID(g, head1_guid);
  56. else PRINT_IF_GUID(g, head2_guid);
  57. else PRINT_IF_GUID(g, my_guid);
  58. else PRINT_IF_GUID(g, ext_stream_header);
  59. else PRINT_IF_GUID(g, extended_content_header);
  60. else PRINT_IF_GUID(g, ext_stream_embed_stream_header);
  61. else PRINT_IF_GUID(g, ext_stream_audio_stream);
  62. else
  63. printf("(GUID: unknown) ");
  64. printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
  65. for(i=0;i<8;i++)
  66. printf(" 0x%02x,", g->v4[i]);
  67. printf("}\n");
  68. }
  69. #undef PRINT_IF_GUID
  70. #endif
  71. static void get_guid(ByteIOContext *s, GUID *g)
  72. {
  73. int i;
  74. g->v1 = get_le32(s);
  75. g->v2 = get_le16(s);
  76. g->v3 = get_le16(s);
  77. for(i=0;i<8;i++)
  78. g->v4[i] = get_byte(s);
  79. }
  80. #if 0
  81. static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
  82. {
  83. int len, c;
  84. char *q;
  85. len = get_le16(pb);
  86. q = buf;
  87. while (len > 0) {
  88. c = get_le16(pb);
  89. if ((q - buf) < buf_size - 1)
  90. *q++ = c;
  91. len--;
  92. }
  93. *q = '\0';
  94. }
  95. #endif
  96. static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
  97. {
  98. int c;
  99. char *q;
  100. q = buf;
  101. while (len > 0) {
  102. c = get_le16(pb);
  103. if ((q - buf) < buf_size - 1)
  104. *q++ = c;
  105. len-=2;
  106. }
  107. *q = '\0';
  108. }
  109. static int asf_probe(AVProbeData *pd)
  110. {
  111. GUID g;
  112. const unsigned char *p;
  113. int i;
  114. /* check file header */
  115. if (pd->buf_size <= 32)
  116. return 0;
  117. p = pd->buf;
  118. g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  119. p += 4;
  120. g.v2 = p[0] | (p[1] << 8);
  121. p += 2;
  122. g.v3 = p[0] | (p[1] << 8);
  123. p += 2;
  124. for(i=0;i<8;i++)
  125. g.v4[i] = *p++;
  126. if (!memcmp(&g, &asf_header, sizeof(GUID)))
  127. return AVPROBE_SCORE_MAX;
  128. else
  129. return 0;
  130. }
  131. static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  132. {
  133. ASFContext *asf = s->priv_data;
  134. GUID g;
  135. ByteIOContext *pb = &s->pb;
  136. AVStream *st;
  137. ASFStream *asf_st;
  138. int size, i;
  139. int64_t gsize;
  140. get_guid(pb, &g);
  141. if (memcmp(&g, &asf_header, sizeof(GUID)))
  142. goto fail;
  143. get_le64(pb);
  144. get_le32(pb);
  145. get_byte(pb);
  146. get_byte(pb);
  147. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  148. for(;;) {
  149. get_guid(pb, &g);
  150. gsize = get_le64(pb);
  151. #ifdef DEBUG
  152. printf("%08Lx: ", url_ftell(pb) - 24);
  153. print_guid(&g);
  154. printf(" size=0x%Lx\n", gsize);
  155. #endif
  156. if (gsize < 24)
  157. goto fail;
  158. if (!memcmp(&g, &file_header, sizeof(GUID))) {
  159. get_guid(pb, &asf->hdr.guid);
  160. asf->hdr.file_size = get_le64(pb);
  161. asf->hdr.create_time = get_le64(pb);
  162. asf->hdr.packets_count = get_le64(pb);
  163. asf->hdr.send_time = get_le64(pb);
  164. asf->hdr.play_time = get_le64(pb);
  165. asf->hdr.preroll = get_le32(pb);
  166. asf->hdr.ignore = get_le32(pb);
  167. asf->hdr.flags = get_le32(pb);
  168. asf->hdr.min_pktsize = get_le32(pb);
  169. asf->hdr.max_pktsize = get_le32(pb);
  170. asf->hdr.max_bitrate = get_le32(pb);
  171. asf->packet_size = asf->hdr.max_pktsize;
  172. asf->nb_packets = asf->hdr.packets_count;
  173. } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
  174. int type, type_specific_size, sizeX;
  175. uint64_t total_size;
  176. unsigned int tag1;
  177. int64_t pos1, pos2;
  178. int test_for_ext_stream_audio;
  179. pos1 = url_ftell(pb);
  180. st = av_new_stream(s, 0);
  181. if (!st)
  182. goto fail;
  183. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  184. asf_st = av_mallocz(sizeof(ASFStream));
  185. if (!asf_st)
  186. goto fail;
  187. st->priv_data = asf_st;
  188. st->start_time = asf->hdr.preroll;
  189. st->duration = asf->hdr.send_time /
  190. (10000000 / 1000) - st->start_time;
  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 (gsize != (uint64_t)-1 && gsize >= 24) {
  309. asf->data_object_size = gsize - 24;
  310. } else {
  311. asf->data_object_size = (uint64_t)-1;
  312. }
  313. break;
  314. } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
  315. int len1, len2, len3, len4, len5;
  316. len1 = get_le16(pb);
  317. len2 = get_le16(pb);
  318. len3 = get_le16(pb);
  319. len4 = get_le16(pb);
  320. len5 = get_le16(pb);
  321. get_str16_nolen(pb, len1, s->title, sizeof(s->title));
  322. get_str16_nolen(pb, len2, s->author, sizeof(s->author));
  323. get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
  324. get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
  325. url_fskip(pb, len5);
  326. } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) {
  327. int desc_count, i;
  328. desc_count = get_le16(pb);
  329. for(i=0;i<desc_count;i++)
  330. {
  331. int name_len,value_type,value_len;
  332. uint64_t value_num = 0;
  333. char *name, *value;
  334. name_len = get_le16(pb);
  335. name = (char *)av_mallocz(name_len);
  336. get_str16_nolen(pb, name_len, name, name_len);
  337. value_type = get_le16(pb);
  338. value_len = get_le16(pb);
  339. if ((value_type == 0) || (value_type == 1)) // unicode or byte
  340. {
  341. value = (char *)av_mallocz(value_len);
  342. get_str16_nolen(pb, value_len, value, value_len);
  343. if (strcmp(name,"WM/AlbumTitle")==0) { pstrcpy(s->album, sizeof(s->album), value); }
  344. av_free(value);
  345. }
  346. if ((value_type >= 2) && (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  347. {
  348. if (value_type==2) value_num = get_le32(pb);
  349. if (value_type==3) value_num = get_le32(pb);
  350. if (value_type==4) value_num = get_le64(pb);
  351. if (value_type==5) value_num = get_le16(pb);
  352. if (strcmp(name,"WM/Track")==0) s->track = value_num + 1;
  353. if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num;
  354. }
  355. av_free(name);
  356. }
  357. } else if (!memcmp(&g, &ext_stream_header, sizeof(GUID))) {
  358. int ext_len, payload_ext_ct, stream_ct;
  359. uint32_t ext_d;
  360. int64_t pos_ex_st;
  361. pos_ex_st = url_ftell(pb);
  362. get_le64(pb);
  363. get_le64(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_le32(pb);
  372. get_le16(pb);
  373. get_le16(pb);
  374. get_le64(pb);
  375. stream_ct = get_le16(pb);
  376. payload_ext_ct = get_le16(pb);
  377. for (i=0; i<stream_ct; i++){
  378. get_le16(pb);
  379. ext_len = get_le16(pb);
  380. url_fseek(pb, ext_len, SEEK_CUR);
  381. }
  382. for (i=0; i<payload_ext_ct; i++){
  383. get_guid(pb, &g);
  384. ext_d=get_le16(pb);
  385. ext_len=get_le32(pb);
  386. url_fseek(pb, ext_len, SEEK_CUR);
  387. }
  388. // there could be a optional stream properties object to follow
  389. // if so the next iteration will pick it up
  390. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  391. int v1, v2;
  392. get_guid(pb, &g);
  393. v1 = get_le32(pb);
  394. v2 = get_le16(pb);
  395. #if 0
  396. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  397. int len, v1, n, num;
  398. char str[256], *q;
  399. char tag[16];
  400. get_guid(pb, &g);
  401. print_guid(&g);
  402. n = get_le32(pb);
  403. for(i=0;i<n;i++) {
  404. num = get_le16(pb); /* stream number */
  405. get_str16(pb, str, sizeof(str));
  406. get_str16(pb, str, sizeof(str));
  407. len = get_le16(pb);
  408. q = tag;
  409. while (len > 0) {
  410. v1 = get_byte(pb);
  411. if ((q - tag) < sizeof(tag) - 1)
  412. *q++ = v1;
  413. len--;
  414. }
  415. *q = '\0';
  416. }
  417. #endif
  418. } else if (url_feof(pb)) {
  419. goto fail;
  420. } else {
  421. url_fseek(pb, gsize - 24, SEEK_CUR);
  422. }
  423. }
  424. get_guid(pb, &g);
  425. get_le64(pb);
  426. get_byte(pb);
  427. get_byte(pb);
  428. if (url_feof(pb))
  429. goto fail;
  430. asf->data_offset = url_ftell(pb);
  431. asf->packet_size_left = 0;
  432. return 0;
  433. fail:
  434. for(i=0;i<s->nb_streams;i++) {
  435. AVStream *st = s->streams[i];
  436. if (st) {
  437. av_free(st->priv_data);
  438. av_free(st->codec->extradata);
  439. }
  440. av_free(st);
  441. }
  442. return -1;
  443. }
  444. #define DO_2BITS(bits, var, defval) \
  445. switch (bits & 3) \
  446. { \
  447. case 3: var = get_le32(pb); rsize += 4; break; \
  448. case 2: var = get_le16(pb); rsize += 2; break; \
  449. case 1: var = get_byte(pb); rsize++; break; \
  450. default: var = defval; break; \
  451. }
  452. static int asf_get_packet(AVFormatContext *s)
  453. {
  454. ASFContext *asf = s->priv_data;
  455. ByteIOContext *pb = &s->pb;
  456. uint32_t packet_length, padsize;
  457. int rsize = 9;
  458. int c;
  459. assert((url_ftell(&s->pb) - s->data_offset) % asf->packet_size == 0);
  460. c = get_byte(pb);
  461. if (c != 0x82) {
  462. if (!url_feof(pb))
  463. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  464. }
  465. if ((c & 0x0f) == 2) { // always true for now
  466. if (get_le16(pb) != 0) {
  467. if (!url_feof(pb))
  468. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  469. return AVERROR_IO;
  470. }
  471. rsize+=2;
  472. /* }else{
  473. if (!url_feof(pb))
  474. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  475. return AVERROR_IO;*/
  476. }
  477. asf->packet_flags = get_byte(pb);
  478. asf->packet_property = get_byte(pb);
  479. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  480. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  481. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  482. //the following checks prevent overflows and infinite loops
  483. if(packet_length >= (1U<<29)){
  484. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  485. return 0; // FIXME this should be -1
  486. }
  487. if(padsize >= (1U<<29)){
  488. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  489. return 0; // FIXME this should be -1
  490. }
  491. asf->packet_timestamp = get_le32(pb);
  492. get_le16(pb); /* duration */
  493. // rsize has at least 11 bytes which have to be present
  494. if (asf->packet_flags & 0x01) {
  495. asf->packet_segsizetype = get_byte(pb); rsize++;
  496. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  497. } else {
  498. asf->packet_segments = 1;
  499. asf->packet_segsizetype = 0x80;
  500. }
  501. asf->packet_size_left = packet_length - padsize - rsize;
  502. if (packet_length < asf->hdr.min_pktsize)
  503. padsize += asf->hdr.min_pktsize - packet_length;
  504. asf->packet_padsize = padsize;
  505. #ifdef DEBUG
  506. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  507. #endif
  508. return 0;
  509. }
  510. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  511. {
  512. ASFContext *asf = s->priv_data;
  513. ASFStream *asf_st = 0;
  514. ByteIOContext *pb = &s->pb;
  515. //static int pc = 0;
  516. for (;;) {
  517. int rsize = 0;
  518. if (asf->packet_size_left < FRAME_HEADER_SIZE
  519. || asf->packet_segments < 1) {
  520. //asf->packet_size_left <= asf->packet_padsize) {
  521. int ret = asf->packet_size_left + asf->packet_padsize;
  522. //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  523. if((url_ftell(&s->pb) + ret - s->data_offset) % asf->packet_size)
  524. ret += asf->packet_size - ((url_ftell(&s->pb) + ret - s->data_offset) % asf->packet_size);
  525. assert(ret>=0);
  526. /* fail safe */
  527. url_fskip(pb, ret);
  528. asf->packet_pos= url_ftell(&s->pb);
  529. if (asf->data_object_size != (uint64_t)-1 &&
  530. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  531. return AVERROR_IO; /* Do not exceed the size of the data object */
  532. ret = asf_get_packet(s);
  533. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  534. if (ret < 0 || url_feof(pb))
  535. return AVERROR_IO;
  536. asf->packet_time_start = 0;
  537. continue;
  538. }
  539. if (asf->packet_time_start == 0) {
  540. /* read frame header */
  541. int num = get_byte(pb);
  542. asf->packet_segments--;
  543. rsize++;
  544. asf->packet_key_frame = (num & 0x80) >> 7;
  545. asf->stream_index = asf->asfid2avid[num & 0x7f];
  546. // sequence should be ignored!
  547. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  548. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  549. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  550. //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);
  551. if (asf->packet_replic_size > 1) {
  552. assert(asf->packet_replic_size >= 8);
  553. // it should be always at least 8 bytes - FIXME validate
  554. asf->packet_obj_size = get_le32(pb);
  555. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  556. if (asf->packet_replic_size > 8)
  557. url_fskip(pb, asf->packet_replic_size - 8);
  558. rsize += asf->packet_replic_size; // FIXME - check validity
  559. } else if (asf->packet_replic_size==1){
  560. // multipacket - frag_offset is begining timestamp
  561. asf->packet_time_start = asf->packet_frag_offset;
  562. asf->packet_frag_offset = 0;
  563. asf->packet_frag_timestamp = asf->packet_timestamp;
  564. asf->packet_time_delta = get_byte(pb);
  565. rsize++;
  566. }else{
  567. assert(asf->packet_replic_size==0);
  568. }
  569. if (asf->packet_flags & 0x01) {
  570. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  571. #undef DO_2BITS
  572. //printf("Fragsize %d\n", asf->packet_frag_size);
  573. } else {
  574. asf->packet_frag_size = asf->packet_size_left - rsize;
  575. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  576. }
  577. if (asf->packet_replic_size == 1) {
  578. asf->packet_multi_size = asf->packet_frag_size;
  579. if (asf->packet_multi_size > asf->packet_size_left) {
  580. asf->packet_segments = 0;
  581. continue;
  582. }
  583. }
  584. asf->packet_size_left -= rsize;
  585. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  586. if (asf->stream_index < 0
  587. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  588. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  589. ) {
  590. asf->packet_time_start = 0;
  591. /* unhandled packet (should not happen) */
  592. url_fskip(pb, asf->packet_frag_size);
  593. asf->packet_size_left -= asf->packet_frag_size;
  594. if(asf->stream_index < 0)
  595. av_log(s, AV_LOG_ERROR, "ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
  596. continue;
  597. }
  598. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  599. }
  600. asf_st = asf->asf_st;
  601. if ((asf->packet_frag_offset != asf_st->frag_offset
  602. || (asf->packet_frag_offset
  603. && asf->packet_seq != asf_st->seq)) // seq should be ignored
  604. ) {
  605. /* cannot continue current packet: free it */
  606. // FIXME better check if packet was already allocated
  607. av_log(s, AV_LOG_INFO, "ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
  608. asf_st->pkt.size,
  609. asf->packet_obj_size,
  610. asf->packet_frag_offset, asf_st->frag_offset,
  611. asf->packet_seq, asf_st->seq, asf->packet_frag_size);
  612. if (asf_st->pkt.size)
  613. av_free_packet(&asf_st->pkt);
  614. asf_st->frag_offset = 0;
  615. if (asf->packet_frag_offset != 0) {
  616. url_fskip(pb, asf->packet_frag_size);
  617. av_log(s, AV_LOG_INFO, "ff asf parser skipping %db\n", asf->packet_frag_size);
  618. asf->packet_size_left -= asf->packet_frag_size;
  619. continue;
  620. }
  621. }
  622. if (asf->packet_replic_size == 1) {
  623. // frag_offset is here used as the begining timestamp
  624. asf->packet_frag_timestamp = asf->packet_time_start;
  625. asf->packet_time_start += asf->packet_time_delta;
  626. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  627. asf->packet_size_left--;
  628. asf->packet_multi_size--;
  629. if (asf->packet_multi_size < asf->packet_obj_size)
  630. {
  631. asf->packet_time_start = 0;
  632. url_fskip(pb, asf->packet_multi_size);
  633. asf->packet_size_left -= asf->packet_multi_size;
  634. continue;
  635. }
  636. asf->packet_multi_size -= asf->packet_obj_size;
  637. //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);
  638. }
  639. if (asf_st->frag_offset == 0) {
  640. /* new packet */
  641. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  642. asf_st->seq = asf->packet_seq;
  643. asf_st->pkt.pts = asf->packet_frag_timestamp;
  644. asf_st->pkt.stream_index = asf->stream_index;
  645. asf_st->pkt.pos =
  646. asf_st->packet_pos= asf->packet_pos;
  647. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  648. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  649. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  650. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  651. asf->packet_key_frame = 1;
  652. if (asf->packet_key_frame)
  653. asf_st->pkt.flags |= PKT_FLAG_KEY;
  654. }
  655. /* read data */
  656. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  657. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  658. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  659. asf->packet_size_left -= asf->packet_frag_size;
  660. if (asf->packet_size_left < 0)
  661. continue;
  662. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  663. asf->packet_frag_size);
  664. asf_st->frag_offset += asf->packet_frag_size;
  665. /* test if whole packet is read */
  666. if (asf_st->frag_offset == asf_st->pkt.size) {
  667. /* return packet */
  668. if (asf_st->ds_span > 1) {
  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. memcpy(newdata + offset,
  680. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  681. asf_st->ds_chunk_size);
  682. offset += asf_st->ds_chunk_size;
  683. }
  684. av_free(asf_st->pkt.data);
  685. asf_st->pkt.data = newdata;
  686. }
  687. }
  688. asf_st->frag_offset = 0;
  689. memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
  690. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  691. asf_st->pkt.size = 0;
  692. asf_st->pkt.data = 0;
  693. break; // packet completed
  694. }
  695. }
  696. return 0;
  697. }
  698. static int asf_read_close(AVFormatContext *s)
  699. {
  700. int i;
  701. for(i=0;i<s->nb_streams;i++) {
  702. AVStream *st = s->streams[i];
  703. av_free(st->priv_data);
  704. av_free(st->codec->palctrl);
  705. }
  706. return 0;
  707. }
  708. // Added to support seeking after packets have been read
  709. // If information is not reset, read_packet fails due to
  710. // leftover information from previous reads
  711. static void asf_reset_header(AVFormatContext *s)
  712. {
  713. ASFContext *asf = s->priv_data;
  714. ASFStream *asf_st;
  715. int i;
  716. asf->packet_nb_frames = 0;
  717. asf->packet_timestamp_start = -1;
  718. asf->packet_timestamp_end = -1;
  719. asf->packet_size_left = 0;
  720. asf->packet_segments = 0;
  721. asf->packet_flags = 0;
  722. asf->packet_property = 0;
  723. asf->packet_timestamp = 0;
  724. asf->packet_segsizetype = 0;
  725. asf->packet_segments = 0;
  726. asf->packet_seq = 0;
  727. asf->packet_replic_size = 0;
  728. asf->packet_key_frame = 0;
  729. asf->packet_padsize = 0;
  730. asf->packet_frag_offset = 0;
  731. asf->packet_frag_size = 0;
  732. asf->packet_frag_timestamp = 0;
  733. asf->packet_multi_size = 0;
  734. asf->packet_obj_size = 0;
  735. asf->packet_time_delta = 0;
  736. asf->packet_time_start = 0;
  737. for(i=0; i<s->nb_streams; i++){
  738. asf_st= s->streams[i]->priv_data;
  739. av_free_packet(&asf_st->pkt);
  740. asf_st->frag_offset=0;
  741. asf_st->seq=0;
  742. }
  743. asf->asf_st= NULL;
  744. }
  745. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  746. {
  747. ASFContext *asf = s->priv_data;
  748. AVPacket pkt1, *pkt = &pkt1;
  749. ASFStream *asf_st;
  750. int64_t pts;
  751. int64_t pos= *ppos;
  752. int i;
  753. int64_t start_pos[s->nb_streams];
  754. for(i=0; i<s->nb_streams; i++){
  755. start_pos[i]= pos;
  756. }
  757. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  758. *ppos= pos;
  759. url_fseek(&s->pb, pos, SEEK_SET);
  760. //printf("asf_read_pts\n");
  761. asf_reset_header(s);
  762. for(;;){
  763. if (av_read_frame(s, pkt) < 0){
  764. av_log(s, AV_LOG_INFO, "seek failed\n");
  765. return AV_NOPTS_VALUE;
  766. }
  767. pts= pkt->pts;
  768. av_free_packet(pkt);
  769. if(pkt->flags&PKT_FLAG_KEY){
  770. i= pkt->stream_index;
  771. asf_st= s->streams[i]->priv_data;
  772. assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  773. pos= asf_st->packet_pos;
  774. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  775. start_pos[i]= asf_st->packet_pos + 1;
  776. if(pkt->stream_index == stream_index)
  777. break;
  778. }
  779. }
  780. *ppos= pos;
  781. //printf("found keyframe at %Ld stream %d stamp:%Ld\n", *ppos, stream_index, pts);
  782. return pts;
  783. }
  784. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  785. {
  786. GUID g;
  787. ASFContext *asf = s->priv_data;
  788. int64_t gsize, itime;
  789. int64_t pos, current_pos, index_pts;
  790. int i;
  791. int pct,ict;
  792. current_pos = url_ftell(&s->pb);
  793. url_fseek(&s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  794. get_guid(&s->pb, &g);
  795. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  796. gsize = get_le64(&s->pb);
  797. get_guid(&s->pb, &g);
  798. itime=get_le64(&s->pb);
  799. pct=get_le32(&s->pb);
  800. ict=get_le32(&s->pb);
  801. av_log(NULL, AV_LOG_DEBUG, "itime:0x%Lx, pct:%d, ict:%d\n",itime,pct,ict);
  802. for (i=0;i<ict;i++){
  803. int pktnum=get_le32(&s->pb);
  804. int pktct =get_le16(&s->pb);
  805. av_log(NULL, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  806. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  807. index_pts=av_rescale(itime, i, 10000);
  808. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  809. }
  810. asf->index_read= 1;
  811. }
  812. url_fseek(&s->pb, current_pos, SEEK_SET);
  813. }
  814. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  815. {
  816. ASFContext *asf = s->priv_data;
  817. AVStream *st = s->streams[stream_index];
  818. int64_t pos;
  819. int index;
  820. if (asf->packet_size <= 0)
  821. return -1;
  822. if (!asf->index_read)
  823. asf_build_simple_index(s, stream_index);
  824. if(!(asf->index_read && st->index_entries)){
  825. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  826. return -1;
  827. }else{
  828. index= av_index_search_timestamp(st, pts, flags);
  829. if(index<0)
  830. return -1;
  831. /* find the position */
  832. pos = st->index_entries[index].pos;
  833. pts = st->index_entries[index].timestamp;
  834. // various attempts to find key frame have failed so far
  835. // asf_reset_header(s);
  836. // url_fseek(&s->pb, pos, SEEK_SET);
  837. // key_pos = pos;
  838. // for(i=0;i<16;i++){
  839. // pos = url_ftell(&s->pb);
  840. // if (av_read_frame(s, &pkt) < 0){
  841. // av_log(s, AV_LOG_INFO, "seek failed\n");
  842. // return -1;
  843. // }
  844. // asf_st = s->streams[stream_index]->priv_data;
  845. // pos += st->parser->frame_offset;
  846. //
  847. // if (pkt.size > b) {
  848. // b = pkt.size;
  849. // key_pos = pos;
  850. // }
  851. //
  852. // av_free_packet(&pkt);
  853. // }
  854. /* do the seek */
  855. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %Ld\n", pos);
  856. url_fseek(&s->pb, pos, SEEK_SET);
  857. }
  858. asf_reset_header(s);
  859. return 0;
  860. }
  861. AVInputFormat asf_demuxer = {
  862. "asf",
  863. "asf format",
  864. sizeof(ASFContext),
  865. asf_probe,
  866. asf_read_header,
  867. asf_read_packet,
  868. asf_read_close,
  869. asf_read_seek,
  870. asf_read_pts,
  871. };