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.

941 lines
33KB

  1. /*
  2. * ASF compatible decoder.
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "riff.h"
  21. #include "mpegaudio.h"
  22. #include "asf.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. #define FRAME_HEADER_SIZE 17
  26. // Fix Me! FRAME_HEADER_SIZE may be different.
  27. static const GUID index_guid = {
  28. 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
  29. };
  30. /**********************************/
  31. /* decoding */
  32. //#define DEBUG
  33. #ifdef DEBUG
  34. #define PRINT_IF_GUID(g,cmp) \
  35. if (!memcmp(g, &cmp, sizeof(GUID))) \
  36. printf("(GUID: %s) ", #cmp)
  37. static void print_guid(const GUID *g)
  38. {
  39. int i;
  40. PRINT_IF_GUID(g, asf_header);
  41. else PRINT_IF_GUID(g, file_header);
  42. else PRINT_IF_GUID(g, stream_header);
  43. else PRINT_IF_GUID(g, audio_stream);
  44. else PRINT_IF_GUID(g, audio_conceal_none);
  45. else PRINT_IF_GUID(g, video_stream);
  46. else PRINT_IF_GUID(g, video_conceal_none);
  47. else PRINT_IF_GUID(g, command_stream);
  48. else PRINT_IF_GUID(g, comment_header);
  49. else PRINT_IF_GUID(g, codec_comment_header);
  50. else PRINT_IF_GUID(g, codec_comment1_header);
  51. else PRINT_IF_GUID(g, data_header);
  52. else PRINT_IF_GUID(g, index_guid);
  53. else PRINT_IF_GUID(g, head1_guid);
  54. else PRINT_IF_GUID(g, head2_guid);
  55. else PRINT_IF_GUID(g, my_guid);
  56. else PRINT_IF_GUID(g, ext_stream_header);
  57. else PRINT_IF_GUID(g, extended_content_header);
  58. else PRINT_IF_GUID(g, ext_stream_embed_stream_header);
  59. else PRINT_IF_GUID(g, ext_stream_audio_stream);
  60. else
  61. printf("(GUID: unknown) ");
  62. printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
  63. for(i=0;i<8;i++)
  64. printf(" 0x%02x,", g->v4[i]);
  65. printf("}\n");
  66. }
  67. #undef PRINT_IF_GUID
  68. #endif
  69. static void get_guid(ByteIOContext *s, GUID *g)
  70. {
  71. int i;
  72. g->v1 = get_le32(s);
  73. g->v2 = get_le16(s);
  74. g->v3 = get_le16(s);
  75. for(i=0;i<8;i++)
  76. g->v4[i] = get_byte(s);
  77. }
  78. #if 0
  79. static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
  80. {
  81. int len, c;
  82. char *q;
  83. len = get_le16(pb);
  84. q = buf;
  85. while (len > 0) {
  86. c = get_le16(pb);
  87. if ((q - buf) < buf_size - 1)
  88. *q++ = c;
  89. len--;
  90. }
  91. *q = '\0';
  92. }
  93. #endif
  94. static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
  95. {
  96. int c;
  97. char *q;
  98. q = buf;
  99. while (len > 0) {
  100. c = get_le16(pb);
  101. if ((q - buf) < buf_size - 1)
  102. *q++ = c;
  103. len-=2;
  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("%08Lx: ", url_ftell(pb) - 24);
  151. print_guid(&g);
  152. printf(" size=0x%Lx\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_mallocz(name_len);
  334. get_str16_nolen(pb, name_len, name, name_len);
  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_mallocz(value_len);
  340. get_str16_nolen(pb, value_len, value, value_len);
  341. if (strcmp(name,"WM/AlbumTitle")==0) { pstrcpy(s->album, sizeof(s->album), value); }
  342. av_free(value);
  343. }
  344. if ((value_type >= 2) && (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  345. {
  346. if (value_type==2) value_num = get_le32(pb);
  347. if (value_type==3) value_num = get_le32(pb);
  348. if (value_type==4) value_num = get_le64(pb);
  349. if (value_type==5) value_num = get_le16(pb);
  350. if (strcmp(name,"WM/Track")==0) s->track = value_num + 1;
  351. if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num;
  352. }
  353. av_free(name);
  354. }
  355. } else if (!memcmp(&g, &ext_stream_header, sizeof(GUID))) {
  356. int ext_len, payload_ext_ct, stream_ct;
  357. uint32_t ext_d;
  358. int64_t pos_ex_st;
  359. pos_ex_st = url_ftell(pb);
  360. get_le64(pb);
  361. get_le64(pb);
  362. get_le32(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_le16(pb);
  371. get_le16(pb);
  372. get_le64(pb);
  373. stream_ct = get_le16(pb);
  374. payload_ext_ct = get_le16(pb);
  375. for (i=0; i<stream_ct; i++){
  376. get_le16(pb);
  377. ext_len = get_le16(pb);
  378. url_fseek(pb, ext_len, SEEK_CUR);
  379. }
  380. for (i=0; i<payload_ext_ct; i++){
  381. get_guid(pb, &g);
  382. ext_d=get_le16(pb);
  383. ext_len=get_le32(pb);
  384. url_fseek(pb, ext_len, SEEK_CUR);
  385. }
  386. // there could be a optional stream properties object to follow
  387. // if so the next iteration will pick it up
  388. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  389. int v1, v2;
  390. get_guid(pb, &g);
  391. v1 = get_le32(pb);
  392. v2 = get_le16(pb);
  393. #if 0
  394. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  395. int len, v1, n, num;
  396. char str[256], *q;
  397. char tag[16];
  398. get_guid(pb, &g);
  399. print_guid(&g);
  400. n = get_le32(pb);
  401. for(i=0;i<n;i++) {
  402. num = get_le16(pb); /* stream number */
  403. get_str16(pb, str, sizeof(str));
  404. get_str16(pb, str, sizeof(str));
  405. len = get_le16(pb);
  406. q = tag;
  407. while (len > 0) {
  408. v1 = get_byte(pb);
  409. if ((q - tag) < sizeof(tag) - 1)
  410. *q++ = v1;
  411. len--;
  412. }
  413. *q = '\0';
  414. }
  415. #endif
  416. } else if (url_feof(pb)) {
  417. goto fail;
  418. } else {
  419. url_fseek(pb, gsize - 24, SEEK_CUR);
  420. }
  421. }
  422. get_guid(pb, &g);
  423. get_le64(pb);
  424. get_byte(pb);
  425. get_byte(pb);
  426. if (url_feof(pb))
  427. goto fail;
  428. asf->data_offset = url_ftell(pb);
  429. asf->packet_size_left = 0;
  430. return 0;
  431. fail:
  432. for(i=0;i<s->nb_streams;i++) {
  433. AVStream *st = s->streams[i];
  434. if (st) {
  435. av_free(st->priv_data);
  436. av_free(st->codec->extradata);
  437. }
  438. av_free(st);
  439. }
  440. return -1;
  441. }
  442. #define DO_2BITS(bits, var, defval) \
  443. switch (bits & 3) \
  444. { \
  445. case 3: var = get_le32(pb); rsize += 4; break; \
  446. case 2: var = get_le16(pb); rsize += 2; break; \
  447. case 1: var = get_byte(pb); rsize++; break; \
  448. default: var = defval; break; \
  449. }
  450. static int asf_get_packet(AVFormatContext *s)
  451. {
  452. ASFContext *asf = s->priv_data;
  453. ByteIOContext *pb = &s->pb;
  454. uint32_t packet_length, padsize;
  455. int rsize = 9;
  456. int c;
  457. assert((url_ftell(&s->pb) - s->data_offset) % asf->packet_size == 0);
  458. c = get_byte(pb);
  459. if (c != 0x82) {
  460. if (!url_feof(pb))
  461. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  462. }
  463. if ((c & 0x0f) == 2) { // always true for now
  464. if (get_le16(pb) != 0) {
  465. if (!url_feof(pb))
  466. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  467. return AVERROR_IO;
  468. }
  469. rsize+=2;
  470. /* }else{
  471. if (!url_feof(pb))
  472. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  473. return AVERROR_IO;*/
  474. }
  475. asf->packet_flags = get_byte(pb);
  476. asf->packet_property = get_byte(pb);
  477. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  478. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  479. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  480. asf->packet_timestamp = get_le32(pb);
  481. get_le16(pb); /* duration */
  482. // rsize has at least 11 bytes which have to be present
  483. if (asf->packet_flags & 0x01) {
  484. asf->packet_segsizetype = get_byte(pb); rsize++;
  485. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  486. } else {
  487. asf->packet_segments = 1;
  488. asf->packet_segsizetype = 0x80;
  489. }
  490. asf->packet_size_left = packet_length - padsize - rsize;
  491. if (packet_length < asf->hdr.min_pktsize)
  492. padsize += asf->hdr.min_pktsize - packet_length;
  493. asf->packet_padsize = padsize;
  494. #ifdef DEBUG
  495. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  496. #endif
  497. return 0;
  498. }
  499. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  500. {
  501. ASFContext *asf = s->priv_data;
  502. ASFStream *asf_st = 0;
  503. ByteIOContext *pb = &s->pb;
  504. //static int pc = 0;
  505. for (;;) {
  506. int rsize = 0;
  507. if (asf->packet_size_left < FRAME_HEADER_SIZE
  508. || asf->packet_segments < 1) {
  509. //asf->packet_size_left <= asf->packet_padsize) {
  510. int ret = asf->packet_size_left + asf->packet_padsize;
  511. //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  512. if((url_ftell(&s->pb) + ret - s->data_offset) % asf->packet_size)
  513. ret += asf->packet_size - ((url_ftell(&s->pb) + ret - s->data_offset) % asf->packet_size);
  514. /* fail safe */
  515. url_fskip(pb, ret);
  516. asf->packet_pos= url_ftell(&s->pb);
  517. if (asf->data_object_size != (uint64_t)-1 &&
  518. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  519. return AVERROR_IO; /* Do not exceed the size of the data object */
  520. ret = asf_get_packet(s);
  521. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  522. if (ret < 0 || url_feof(pb))
  523. return AVERROR_IO;
  524. asf->packet_time_start = 0;
  525. continue;
  526. }
  527. if (asf->packet_time_start == 0) {
  528. /* read frame header */
  529. int num = get_byte(pb);
  530. asf->packet_segments--;
  531. rsize++;
  532. asf->packet_key_frame = (num & 0x80) >> 7;
  533. asf->stream_index = asf->asfid2avid[num & 0x7f];
  534. // sequence should be ignored!
  535. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  536. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  537. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  538. //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);
  539. if (asf->packet_replic_size > 1) {
  540. assert(asf->packet_replic_size >= 8);
  541. // it should be always at least 8 bytes - FIXME validate
  542. asf->packet_obj_size = get_le32(pb);
  543. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  544. if (asf->packet_replic_size > 8)
  545. url_fskip(pb, asf->packet_replic_size - 8);
  546. rsize += asf->packet_replic_size; // FIXME - check validity
  547. } else if (asf->packet_replic_size==1){
  548. // multipacket - frag_offset is begining timestamp
  549. asf->packet_time_start = asf->packet_frag_offset;
  550. asf->packet_frag_offset = 0;
  551. asf->packet_frag_timestamp = asf->packet_timestamp;
  552. asf->packet_time_delta = get_byte(pb);
  553. rsize++;
  554. }else{
  555. assert(asf->packet_replic_size==0);
  556. }
  557. if (asf->packet_flags & 0x01) {
  558. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  559. #undef DO_2BITS
  560. //printf("Fragsize %d\n", asf->packet_frag_size);
  561. } else {
  562. asf->packet_frag_size = asf->packet_size_left - rsize;
  563. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  564. }
  565. if (asf->packet_replic_size == 1) {
  566. asf->packet_multi_size = asf->packet_frag_size;
  567. if (asf->packet_multi_size > asf->packet_size_left) {
  568. asf->packet_segments = 0;
  569. continue;
  570. }
  571. }
  572. asf->packet_size_left -= rsize;
  573. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  574. if (asf->stream_index < 0
  575. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  576. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  577. ) {
  578. asf->packet_time_start = 0;
  579. /* unhandled packet (should not happen) */
  580. url_fskip(pb, asf->packet_frag_size);
  581. asf->packet_size_left -= asf->packet_frag_size;
  582. if(asf->stream_index < 0)
  583. av_log(s, AV_LOG_ERROR, "ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
  584. continue;
  585. }
  586. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  587. }
  588. asf_st = asf->asf_st;
  589. if ((asf->packet_frag_offset != asf_st->frag_offset
  590. || (asf->packet_frag_offset
  591. && asf->packet_seq != asf_st->seq)) // seq should be ignored
  592. ) {
  593. /* cannot continue current packet: free it */
  594. // FIXME better check if packet was already allocated
  595. av_log(s, AV_LOG_INFO, "ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
  596. asf_st->pkt.size,
  597. asf->packet_obj_size,
  598. asf->packet_frag_offset, asf_st->frag_offset,
  599. asf->packet_seq, asf_st->seq, asf->packet_frag_size);
  600. if (asf_st->pkt.size)
  601. av_free_packet(&asf_st->pkt);
  602. asf_st->frag_offset = 0;
  603. if (asf->packet_frag_offset != 0) {
  604. url_fskip(pb, asf->packet_frag_size);
  605. av_log(s, AV_LOG_INFO, "ff asf parser skipping %db\n", asf->packet_frag_size);
  606. asf->packet_size_left -= asf->packet_frag_size;
  607. continue;
  608. }
  609. }
  610. if (asf->packet_replic_size == 1) {
  611. // frag_offset is here used as the begining timestamp
  612. asf->packet_frag_timestamp = asf->packet_time_start;
  613. asf->packet_time_start += asf->packet_time_delta;
  614. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  615. asf->packet_size_left--;
  616. asf->packet_multi_size--;
  617. if (asf->packet_multi_size < asf->packet_obj_size)
  618. {
  619. asf->packet_time_start = 0;
  620. url_fskip(pb, asf->packet_multi_size);
  621. asf->packet_size_left -= asf->packet_multi_size;
  622. continue;
  623. }
  624. asf->packet_multi_size -= asf->packet_obj_size;
  625. //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);
  626. }
  627. if (asf_st->frag_offset == 0) {
  628. /* new packet */
  629. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  630. asf_st->seq = asf->packet_seq;
  631. asf_st->pkt.pts = asf->packet_frag_timestamp;
  632. asf_st->pkt.stream_index = asf->stream_index;
  633. asf_st->pkt.pos =
  634. asf_st->packet_pos= asf->packet_pos;
  635. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  636. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  637. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  638. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  639. asf->packet_key_frame = 1;
  640. if (asf->packet_key_frame)
  641. asf_st->pkt.flags |= PKT_FLAG_KEY;
  642. }
  643. /* read data */
  644. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  645. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  646. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  647. asf->packet_size_left -= asf->packet_frag_size;
  648. if (asf->packet_size_left < 0)
  649. continue;
  650. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  651. asf->packet_frag_size);
  652. asf_st->frag_offset += asf->packet_frag_size;
  653. /* test if whole packet is read */
  654. if (asf_st->frag_offset == asf_st->pkt.size) {
  655. /* return packet */
  656. if (asf_st->ds_span > 1) {
  657. /* packet descrambling */
  658. char* newdata = av_malloc(asf_st->pkt.size);
  659. if (newdata) {
  660. int offset = 0;
  661. while (offset < asf_st->pkt.size) {
  662. int off = offset / asf_st->ds_chunk_size;
  663. int row = off / asf_st->ds_span;
  664. int col = off % asf_st->ds_span;
  665. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  666. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  667. memcpy(newdata + offset,
  668. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  669. asf_st->ds_chunk_size);
  670. offset += asf_st->ds_chunk_size;
  671. }
  672. av_free(asf_st->pkt.data);
  673. asf_st->pkt.data = newdata;
  674. }
  675. }
  676. asf_st->frag_offset = 0;
  677. memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
  678. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  679. asf_st->pkt.size = 0;
  680. asf_st->pkt.data = 0;
  681. break; // packet completed
  682. }
  683. }
  684. return 0;
  685. }
  686. static int asf_read_close(AVFormatContext *s)
  687. {
  688. int i;
  689. for(i=0;i<s->nb_streams;i++) {
  690. AVStream *st = s->streams[i];
  691. av_free(st->priv_data);
  692. av_free(st->codec->palctrl);
  693. }
  694. return 0;
  695. }
  696. // Added to support seeking after packets have been read
  697. // If information is not reset, read_packet fails due to
  698. // leftover information from previous reads
  699. static void asf_reset_header(AVFormatContext *s)
  700. {
  701. ASFContext *asf = s->priv_data;
  702. ASFStream *asf_st;
  703. int i;
  704. asf->packet_nb_frames = 0;
  705. asf->packet_timestamp_start = -1;
  706. asf->packet_timestamp_end = -1;
  707. asf->packet_size_left = 0;
  708. asf->packet_segments = 0;
  709. asf->packet_flags = 0;
  710. asf->packet_property = 0;
  711. asf->packet_timestamp = 0;
  712. asf->packet_segsizetype = 0;
  713. asf->packet_segments = 0;
  714. asf->packet_seq = 0;
  715. asf->packet_replic_size = 0;
  716. asf->packet_key_frame = 0;
  717. asf->packet_padsize = 0;
  718. asf->packet_frag_offset = 0;
  719. asf->packet_frag_size = 0;
  720. asf->packet_frag_timestamp = 0;
  721. asf->packet_multi_size = 0;
  722. asf->packet_obj_size = 0;
  723. asf->packet_time_delta = 0;
  724. asf->packet_time_start = 0;
  725. for(i=0; i<s->nb_streams; i++){
  726. asf_st= s->streams[i]->priv_data;
  727. av_free_packet(&asf_st->pkt);
  728. asf_st->frag_offset=0;
  729. asf_st->seq=0;
  730. }
  731. asf->asf_st= NULL;
  732. }
  733. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  734. {
  735. ASFContext *asf = s->priv_data;
  736. AVPacket pkt1, *pkt = &pkt1;
  737. ASFStream *asf_st;
  738. int64_t pts;
  739. int64_t pos= *ppos;
  740. int i;
  741. int64_t start_pos[s->nb_streams];
  742. for(i=0; i<s->nb_streams; i++){
  743. start_pos[i]= pos;
  744. }
  745. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  746. *ppos= pos;
  747. url_fseek(&s->pb, pos, SEEK_SET);
  748. //printf("asf_read_pts\n");
  749. asf_reset_header(s);
  750. for(;;){
  751. if (av_read_frame(s, pkt) < 0){
  752. av_log(s, AV_LOG_INFO, "seek failed\n");
  753. return AV_NOPTS_VALUE;
  754. }
  755. pts= pkt->pts;
  756. av_free_packet(pkt);
  757. if(pkt->flags&PKT_FLAG_KEY){
  758. i= pkt->stream_index;
  759. asf_st= s->streams[i]->priv_data;
  760. assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  761. pos= asf_st->packet_pos;
  762. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  763. start_pos[i]= asf_st->packet_pos + 1;
  764. if(pkt->stream_index == stream_index)
  765. break;
  766. }
  767. }
  768. *ppos= pos;
  769. //printf("found keyframe at %Ld stream %d stamp:%Ld\n", *ppos, stream_index, pts);
  770. return pts;
  771. }
  772. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  773. {
  774. GUID g;
  775. ASFContext *asf = s->priv_data;
  776. int64_t gsize, itime;
  777. int64_t pos, current_pos, index_pts;
  778. int i;
  779. int pct,ict;
  780. current_pos = url_ftell(&s->pb);
  781. url_fseek(&s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  782. get_guid(&s->pb, &g);
  783. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  784. gsize = get_le64(&s->pb);
  785. get_guid(&s->pb, &g);
  786. itime=get_le64(&s->pb);
  787. pct=get_le32(&s->pb);
  788. ict=get_le32(&s->pb);
  789. av_log(NULL, AV_LOG_DEBUG, "itime:0x%Lx, pct:%d, ict:%d\n",itime,pct,ict);
  790. for (i=0;i<ict;i++){
  791. int pktnum=get_le32(&s->pb);
  792. int pktct =get_le16(&s->pb);
  793. av_log(NULL, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  794. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  795. index_pts=av_rescale(itime, i, 10000);
  796. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  797. }
  798. asf->index_read= 1;
  799. }
  800. url_fseek(&s->pb, current_pos, SEEK_SET);
  801. }
  802. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  803. {
  804. ASFContext *asf = s->priv_data;
  805. AVStream *st = s->streams[stream_index];
  806. int64_t pos;
  807. int index;
  808. if (asf->packet_size <= 0)
  809. return -1;
  810. if (!asf->index_read)
  811. asf_build_simple_index(s, stream_index);
  812. if(!(asf->index_read && st->index_entries)){
  813. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  814. return -1;
  815. }else{
  816. index= av_index_search_timestamp(st, pts, flags);
  817. if(index<0)
  818. return -1;
  819. /* find the position */
  820. pos = st->index_entries[index].pos;
  821. pts = st->index_entries[index].timestamp;
  822. // various attempts to find key frame have failed so far
  823. // asf_reset_header(s);
  824. // url_fseek(&s->pb, pos, SEEK_SET);
  825. // key_pos = pos;
  826. // for(i=0;i<16;i++){
  827. // pos = url_ftell(&s->pb);
  828. // if (av_read_frame(s, &pkt) < 0){
  829. // av_log(s, AV_LOG_INFO, "seek failed\n");
  830. // return -1;
  831. // }
  832. // asf_st = s->streams[stream_index]->priv_data;
  833. // pos += st->parser->frame_offset;
  834. //
  835. // if (pkt.size > b) {
  836. // b = pkt.size;
  837. // key_pos = pos;
  838. // }
  839. //
  840. // av_free_packet(&pkt);
  841. // }
  842. /* do the seek */
  843. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %Ld\n", pos);
  844. url_fseek(&s->pb, pos, SEEK_SET);
  845. }
  846. asf_reset_header(s);
  847. return 0;
  848. }
  849. AVInputFormat asf_demuxer = {
  850. "asf",
  851. "asf format",
  852. sizeof(ASFContext),
  853. asf_probe,
  854. asf_read_header,
  855. asf_read_packet,
  856. asf_read_close,
  857. asf_read_seek,
  858. asf_read_pts,
  859. };