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.

893 lines
29KB

  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. av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
  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.play_time = get_le64(pb);
  158. asf->hdr.send_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;
  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. 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 / (10000000 / AV_TIME_BASE);
  180. st->duration = (asf->hdr.send_time - asf->hdr.preroll) /
  181. (10000000 / AV_TIME_BASE);
  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 = 1;
  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);
  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. }
  276. pos2 = url_ftell(pb);
  277. url_fskip(pb, gsize - (pos2 - pos1 + 24));
  278. } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
  279. break;
  280. } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
  281. int len1, len2, len3, len4, len5;
  282. len1 = get_le16(pb);
  283. len2 = get_le16(pb);
  284. len3 = get_le16(pb);
  285. len4 = get_le16(pb);
  286. len5 = get_le16(pb);
  287. get_str16_nolen(pb, len1, s->title, sizeof(s->title));
  288. get_str16_nolen(pb, len2, s->author, sizeof(s->author));
  289. get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
  290. get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
  291. url_fskip(pb, len5);
  292. } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) {
  293. int desc_count, i;
  294. desc_count = get_le16(pb);
  295. for(i=0;i<desc_count;i++)
  296. {
  297. int name_len,value_type,value_len,value_num = 0;
  298. char *name, *value;
  299. name_len = get_le16(pb);
  300. name = (char *)av_mallocz(name_len);
  301. get_str16_nolen(pb, name_len, name, name_len);
  302. value_type = get_le16(pb);
  303. value_len = get_le16(pb);
  304. if ((value_type == 0) || (value_type == 1)) // unicode or byte
  305. {
  306. value = (char *)av_mallocz(value_len);
  307. get_str16_nolen(pb, value_len, value, value_len);
  308. if (strcmp(name,"WM/AlbumTitle")==0) { strcpy(s->album, value); }
  309. av_free(value);
  310. }
  311. if ((value_type >= 2) || (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  312. {
  313. if (value_type==2) value_num = get_le32(pb);
  314. if (value_type==3) value_num = get_le32(pb);
  315. if (value_type==4) value_num = get_le64(pb);
  316. if (value_type==5) value_num = get_le16(pb);
  317. if (strcmp(name,"WM/Track")==0) s->track = value_num + 1;
  318. if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num;
  319. }
  320. av_free(name);
  321. }
  322. #if 0
  323. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  324. int v1, v2;
  325. get_guid(pb, &g);
  326. v1 = get_le32(pb);
  327. v2 = get_le16(pb);
  328. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  329. int len, v1, n, num;
  330. char str[256], *q;
  331. char tag[16];
  332. get_guid(pb, &g);
  333. print_guid(&g);
  334. n = get_le32(pb);
  335. for(i=0;i<n;i++) {
  336. num = get_le16(pb); /* stream number */
  337. get_str16(pb, str, sizeof(str));
  338. get_str16(pb, str, sizeof(str));
  339. len = get_le16(pb);
  340. q = tag;
  341. while (len > 0) {
  342. v1 = get_byte(pb);
  343. if ((q - tag) < sizeof(tag) - 1)
  344. *q++ = v1;
  345. len--;
  346. }
  347. *q = '\0';
  348. }
  349. #endif
  350. } else if (url_feof(pb)) {
  351. goto fail;
  352. } else {
  353. url_fseek(pb, gsize - 24, SEEK_CUR);
  354. }
  355. }
  356. get_guid(pb, &g);
  357. get_le64(pb);
  358. get_byte(pb);
  359. get_byte(pb);
  360. if (url_feof(pb))
  361. goto fail;
  362. asf->data_offset = url_ftell(pb);
  363. asf->packet_size_left = 0;
  364. return 0;
  365. fail:
  366. for(i=0;i<s->nb_streams;i++) {
  367. AVStream *st = s->streams[i];
  368. if (st) {
  369. av_free(st->priv_data);
  370. av_free(st->codec.extradata);
  371. }
  372. av_free(st);
  373. }
  374. return -1;
  375. }
  376. #define DO_2BITS(bits, var, defval) \
  377. switch (bits & 3) \
  378. { \
  379. case 3: var = get_le32(pb); rsize += 4; break; \
  380. case 2: var = get_le16(pb); rsize += 2; break; \
  381. case 1: var = get_byte(pb); rsize++; break; \
  382. default: var = defval; break; \
  383. }
  384. static int asf_get_packet(AVFormatContext *s)
  385. {
  386. ASFContext *asf = s->priv_data;
  387. ByteIOContext *pb = &s->pb;
  388. uint32_t packet_length, padsize;
  389. int rsize = 9;
  390. int c;
  391. assert((url_ftell(&s->pb) - s->data_offset) % asf->packet_size == 0);
  392. c = get_byte(pb);
  393. if (c != 0x82) {
  394. if (!url_feof(pb))
  395. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  396. }
  397. if ((c & 0x0f) == 2) { // always true for now
  398. if (get_le16(pb) != 0) {
  399. if (!url_feof(pb))
  400. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  401. return -EIO;
  402. }
  403. rsize+=2;
  404. /* }else{
  405. if (!url_feof(pb))
  406. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  407. return -EIO;*/
  408. }
  409. asf->packet_flags = get_byte(pb);
  410. asf->packet_property = get_byte(pb);
  411. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  412. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  413. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  414. asf->packet_timestamp = get_le32(pb);
  415. get_le16(pb); /* duration */
  416. // rsize has at least 11 bytes which have to be present
  417. if (asf->packet_flags & 0x01) {
  418. asf->packet_segsizetype = get_byte(pb); rsize++;
  419. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  420. } else {
  421. asf->packet_segments = 1;
  422. asf->packet_segsizetype = 0x80;
  423. }
  424. asf->packet_size_left = packet_length - padsize - rsize;
  425. if (packet_length < asf->hdr.min_pktsize)
  426. padsize += asf->hdr.min_pktsize - packet_length;
  427. asf->packet_padsize = padsize;
  428. #ifdef DEBUG
  429. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  430. #endif
  431. return 0;
  432. }
  433. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  434. {
  435. ASFContext *asf = s->priv_data;
  436. ASFStream *asf_st = 0;
  437. ByteIOContext *pb = &s->pb;
  438. //static int pc = 0;
  439. for (;;) {
  440. int rsize = 0;
  441. if (asf->packet_size_left < FRAME_HEADER_SIZE
  442. || asf->packet_segments < 1) {
  443. //asf->packet_size_left <= asf->packet_padsize) {
  444. int ret = asf->packet_size_left + asf->packet_padsize;
  445. //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  446. /* fail safe */
  447. url_fskip(pb, ret);
  448. asf->packet_pos= url_ftell(&s->pb);
  449. ret = asf_get_packet(s);
  450. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  451. if (ret < 0 || url_feof(pb))
  452. return -EIO;
  453. asf->packet_time_start = 0;
  454. continue;
  455. }
  456. if (asf->packet_time_start == 0) {
  457. /* read frame header */
  458. int num = get_byte(pb);
  459. asf->packet_segments--;
  460. rsize++;
  461. asf->packet_key_frame = (num & 0x80) >> 7;
  462. asf->stream_index = asf->asfid2avid[num & 0x7f];
  463. // sequence should be ignored!
  464. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  465. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  466. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  467. //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);
  468. if (asf->packet_replic_size > 1) {
  469. assert(asf->packet_replic_size >= 8);
  470. // it should be always at least 8 bytes - FIXME validate
  471. asf->packet_obj_size = get_le32(pb);
  472. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  473. if (asf->packet_replic_size > 8)
  474. url_fskip(pb, asf->packet_replic_size - 8);
  475. rsize += asf->packet_replic_size; // FIXME - check validity
  476. } else if (asf->packet_replic_size==1){
  477. // multipacket - frag_offset is begining timestamp
  478. asf->packet_time_start = asf->packet_frag_offset;
  479. asf->packet_frag_offset = 0;
  480. asf->packet_frag_timestamp = asf->packet_timestamp;
  481. asf->packet_time_delta = get_byte(pb);
  482. rsize++;
  483. }else{
  484. assert(asf->packet_replic_size==0);
  485. }
  486. if (asf->packet_flags & 0x01) {
  487. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  488. #undef DO_2BITS
  489. //printf("Fragsize %d\n", asf->packet_frag_size);
  490. } else {
  491. asf->packet_frag_size = asf->packet_size_left - rsize;
  492. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  493. }
  494. if (asf->packet_replic_size == 1) {
  495. asf->packet_multi_size = asf->packet_frag_size;
  496. if (asf->packet_multi_size > asf->packet_size_left) {
  497. asf->packet_segments = 0;
  498. continue;
  499. }
  500. }
  501. asf->packet_size_left -= rsize;
  502. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  503. if (asf->stream_index < 0) {
  504. asf->packet_time_start = 0;
  505. /* unhandled packet (should not happen) */
  506. url_fskip(pb, asf->packet_frag_size);
  507. asf->packet_size_left -= asf->packet_frag_size;
  508. av_log(s, AV_LOG_ERROR, "ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
  509. continue;
  510. }
  511. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  512. }
  513. asf_st = asf->asf_st;
  514. if ((asf->packet_frag_offset != asf_st->frag_offset
  515. || (asf->packet_frag_offset
  516. && asf->packet_seq != asf_st->seq)) // seq should be ignored
  517. ) {
  518. /* cannot continue current packet: free it */
  519. // FIXME better check if packet was already allocated
  520. av_log(s, AV_LOG_INFO, "ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
  521. asf_st->pkt.size,
  522. asf->packet_obj_size,
  523. asf->packet_frag_offset, asf_st->frag_offset,
  524. asf->packet_seq, asf_st->seq, asf->packet_frag_size);
  525. if (asf_st->pkt.size)
  526. av_free_packet(&asf_st->pkt);
  527. asf_st->frag_offset = 0;
  528. if (asf->packet_frag_offset != 0) {
  529. url_fskip(pb, asf->packet_frag_size);
  530. av_log(s, AV_LOG_INFO, "ff asf parser skiping %db\n", asf->packet_frag_size);
  531. asf->packet_size_left -= asf->packet_frag_size;
  532. continue;
  533. }
  534. }
  535. if (asf->packet_replic_size == 1) {
  536. // frag_offset is here used as the begining timestamp
  537. asf->packet_frag_timestamp = asf->packet_time_start;
  538. asf->packet_time_start += asf->packet_time_delta;
  539. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  540. asf->packet_size_left--;
  541. asf->packet_multi_size--;
  542. if (asf->packet_multi_size < asf->packet_obj_size)
  543. {
  544. asf->packet_time_start = 0;
  545. url_fskip(pb, asf->packet_multi_size);
  546. asf->packet_size_left -= asf->packet_multi_size;
  547. continue;
  548. }
  549. asf->packet_multi_size -= asf->packet_obj_size;
  550. //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);
  551. }
  552. if (asf_st->frag_offset == 0) {
  553. /* new packet */
  554. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  555. asf_st->seq = asf->packet_seq;
  556. asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
  557. asf_st->pkt.stream_index = asf->stream_index;
  558. asf_st->packet_pos= asf->packet_pos;
  559. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  560. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  561. //s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  562. if (s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO)
  563. asf->packet_key_frame = 1;
  564. if (asf->packet_key_frame)
  565. asf_st->pkt.flags |= PKT_FLAG_KEY;
  566. }
  567. /* read data */
  568. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  569. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  570. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  571. asf->packet_size_left -= asf->packet_frag_size;
  572. if (asf->packet_size_left < 0)
  573. continue;
  574. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  575. asf->packet_frag_size);
  576. asf_st->frag_offset += asf->packet_frag_size;
  577. /* test if whole packet is read */
  578. if (asf_st->frag_offset == asf_st->pkt.size) {
  579. /* return packet */
  580. if (asf_st->ds_span > 1) {
  581. /* packet descrambling */
  582. char* newdata = av_malloc(asf_st->pkt.size);
  583. if (newdata) {
  584. int offset = 0;
  585. while (offset < asf_st->pkt.size) {
  586. int off = offset / asf_st->ds_chunk_size;
  587. int row = off / asf_st->ds_span;
  588. int col = off % asf_st->ds_span;
  589. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  590. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  591. memcpy(newdata + offset,
  592. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  593. asf_st->ds_chunk_size);
  594. offset += asf_st->ds_chunk_size;
  595. }
  596. av_free(asf_st->pkt.data);
  597. asf_st->pkt.data = newdata;
  598. }
  599. }
  600. asf_st->frag_offset = 0;
  601. memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
  602. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  603. asf_st->pkt.size = 0;
  604. asf_st->pkt.data = 0;
  605. break; // packet completed
  606. }
  607. }
  608. return 0;
  609. }
  610. static int asf_read_close(AVFormatContext *s)
  611. {
  612. int i;
  613. for(i=0;i<s->nb_streams;i++) {
  614. AVStream *st = s->streams[i];
  615. av_free(st->priv_data);
  616. av_free(st->codec.extradata);
  617. av_free(st->codec.palctrl);
  618. }
  619. return 0;
  620. }
  621. // Added to support seeking after packets have been read
  622. // If information is not reset, read_packet fails due to
  623. // leftover information from previous reads
  624. static void asf_reset_header(AVFormatContext *s)
  625. {
  626. ASFContext *asf = s->priv_data;
  627. ASFStream *asf_st;
  628. int i;
  629. asf->packet_nb_frames = 0;
  630. asf->packet_timestamp_start = -1;
  631. asf->packet_timestamp_end = -1;
  632. asf->packet_size_left = 0;
  633. asf->packet_segments = 0;
  634. asf->packet_flags = 0;
  635. asf->packet_property = 0;
  636. asf->packet_timestamp = 0;
  637. asf->packet_segsizetype = 0;
  638. asf->packet_segments = 0;
  639. asf->packet_seq = 0;
  640. asf->packet_replic_size = 0;
  641. asf->packet_key_frame = 0;
  642. asf->packet_padsize = 0;
  643. asf->packet_frag_offset = 0;
  644. asf->packet_frag_size = 0;
  645. asf->packet_frag_timestamp = 0;
  646. asf->packet_multi_size = 0;
  647. asf->packet_obj_size = 0;
  648. asf->packet_time_delta = 0;
  649. asf->packet_time_start = 0;
  650. for(i=0; i<s->nb_streams; i++){
  651. asf_st= s->streams[i]->priv_data;
  652. av_free_packet(&asf_st->pkt);
  653. asf_st->frag_offset=0;
  654. asf_st->seq=0;
  655. }
  656. asf->asf_st= NULL;
  657. }
  658. static int64_t asf_read_pts(AVFormatContext *s, int64_t *ppos, int stream_index)
  659. {
  660. ASFContext *asf = s->priv_data;
  661. AVPacket pkt1, *pkt = &pkt1;
  662. ASFStream *asf_st;
  663. int64_t pts;
  664. int64_t pos= *ppos;
  665. int i;
  666. int64_t start_pos[s->nb_streams];
  667. for(i=0; i<s->nb_streams; i++){
  668. start_pos[i]= pos;
  669. }
  670. //printf("asf_read_pts\n");
  671. url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET);
  672. asf_reset_header(s);
  673. for(;;){
  674. if (av_read_frame(s, pkt) < 0){
  675. av_log(s, AV_LOG_INFO, "seek failed\n");
  676. return AV_NOPTS_VALUE;
  677. }
  678. pts= pkt->pts;
  679. av_free_packet(pkt);
  680. if(pkt->flags&PKT_FLAG_KEY){
  681. i= pkt->stream_index;
  682. asf_st= s->streams[i]->priv_data;
  683. assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  684. pos= (asf_st->packet_pos - s->data_offset) / asf->packet_size;
  685. av_add_index_entry(s->streams[i], pos, pts, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  686. start_pos[i]= pos + 1;
  687. if(pkt->stream_index == stream_index)
  688. break;
  689. }
  690. }
  691. *ppos= pos;
  692. //printf("found keyframe at %Ld stream %d stamp:%Ld\n", *ppos, stream_index, pts);
  693. return pts;
  694. }
  695. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts)
  696. {
  697. ASFContext *asf = s->priv_data;
  698. AVStream *st;
  699. int64_t pos;
  700. int64_t pos_min, pos_max, pts_min, pts_max, cur_pts, pos_limit;
  701. int no_change;
  702. if (stream_index == -1)
  703. stream_index= av_find_default_stream_index(s);
  704. if (asf->packet_size <= 0)
  705. return -1;
  706. pts_max=
  707. pts_min= AV_NOPTS_VALUE;
  708. pos_max= pos_limit= -1; // gcc thinks its uninitalized
  709. st= s->streams[stream_index];
  710. if(st->index_entries){
  711. AVIndexEntry *e;
  712. int index;
  713. index= av_index_search_timestamp(st, pts);
  714. e= &st->index_entries[index];
  715. if(e->timestamp <= pts){
  716. pos_min= e->pos;
  717. pts_min= e->timestamp;
  718. #ifdef DEBUG_SEEK
  719. printf("unsing cached pos_min=0x%llx dts_min=%0.3f\n",
  720. pos_min,pts_min / 90000.0);
  721. #endif
  722. }else{
  723. assert(index==0);
  724. }
  725. index++;
  726. if(index < st->nb_index_entries){
  727. e= &st->index_entries[index];
  728. assert(e->timestamp >= pts);
  729. pos_max= e->pos;
  730. pts_max= e->timestamp;
  731. pos_limit= pos_max - e->min_distance;
  732. #ifdef DEBUG_SEEK
  733. printf("unsing cached pos_max=0x%llx dts_max=%0.3f\n",
  734. pos_max,pts_max / 90000.0);
  735. #endif
  736. }
  737. }
  738. if(pts_min == AV_NOPTS_VALUE){
  739. pos_min = 0;
  740. pts_min = asf_read_pts(s, &pos_min, stream_index);
  741. if (pts_min == AV_NOPTS_VALUE) return -1;
  742. }
  743. if(pts_max == AV_NOPTS_VALUE){
  744. pos_max = (url_filesize(url_fileno(&s->pb)) - 1 - s->data_offset) / asf->packet_size; //FIXME wrong
  745. pts_max = s->duration; //FIXME wrong
  746. pos_limit= pos_max;
  747. }
  748. no_change=0;
  749. while (pos_min < pos_limit) {
  750. int64_t start_pos;
  751. assert(pos_limit <= pos_max);
  752. if(no_change==0){
  753. int64_t approximate_keyframe_distance= pos_max - pos_limit;
  754. // interpolate position (better than dichotomy)
  755. pos = (int64_t)((double)(pos_max - pos_min) *
  756. (double)(pts - pts_min) /
  757. (double)(pts_max - pts_min)) + pos_min - approximate_keyframe_distance;
  758. }else if(no_change==1){
  759. // bisection, if interpolation failed to change min or max pos last time
  760. pos = (pos_min + pos_limit)>>1;
  761. }else{
  762. // linear search if bisection failed, can only happen if there are very few or no keyframes between min/max
  763. pos=pos_min;
  764. }
  765. if(pos <= pos_min)
  766. pos= pos_min + 1;
  767. else if(pos > pos_limit)
  768. pos= pos_limit;
  769. start_pos= pos;
  770. // read the next timestamp
  771. cur_pts = asf_read_pts(s, &pos, stream_index);
  772. if(pos == pos_max)
  773. no_change++;
  774. else
  775. no_change=0;
  776. #ifdef DEBUG_SEEK
  777. printf("%Ld %Ld %Ld / %Ld %Ld %Ld target:%Ld limit:%Ld start:%Ld\n", pos_min, pos, pos_max, pts_min, cur_pts, pts_max, pts, pos_limit, start_pos);
  778. #endif
  779. assert (cur_pts != AV_NOPTS_VALUE);
  780. if (pts < cur_pts) {
  781. pos_limit = start_pos - 1;
  782. pos_max = pos;
  783. pts_max = cur_pts;
  784. } else {
  785. pos_min = pos;
  786. pts_min = cur_pts;
  787. /* check if we are lucky */
  788. if (pts == cur_pts)
  789. break;
  790. }
  791. }
  792. pos = pos_min;
  793. url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET);
  794. asf_reset_header(s);
  795. return 0;
  796. }
  797. static AVInputFormat asf_iformat = {
  798. "asf",
  799. "asf format",
  800. sizeof(ASFContext),
  801. asf_probe,
  802. asf_read_header,
  803. asf_read_packet,
  804. asf_read_close,
  805. asf_read_seek,
  806. };
  807. #ifdef CONFIG_ENCODERS
  808. extern AVOutputFormat asf_oformat;
  809. extern AVOutputFormat asf_stream_oformat;
  810. #endif //CONFIG_ENCODERS
  811. int asf_init(void)
  812. {
  813. av_register_input_format(&asf_iformat);
  814. #ifdef CONFIG_ENCODERS
  815. av_register_output_format(&asf_oformat);
  816. av_register_output_format(&asf_stream_oformat);
  817. #endif //CONFIG_ENCODERS
  818. return 0;
  819. }