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.

803 lines
26KB

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