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.

809 lines
27KB

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