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.

1567 lines
48KB

  1. /*
  2. * ASF compatible encoder and 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. #undef NDEBUG
  23. #include <assert.h>
  24. #define PACKET_SIZE 3200
  25. #define PACKET_HEADER_SIZE 12
  26. #define FRAME_HEADER_SIZE 17
  27. typedef struct {
  28. int num;
  29. int seq;
  30. /* use for reading */
  31. AVPacket pkt;
  32. int frag_offset;
  33. int timestamp;
  34. int64_t duration;
  35. int ds_span; /* descrambling */
  36. int ds_packet_size;
  37. int ds_chunk_size;
  38. int ds_data_size;
  39. int ds_silence_data;
  40. int packet_pos;
  41. } ASFStream;
  42. typedef struct {
  43. uint32_t v1;
  44. uint16_t v2;
  45. uint16_t v3;
  46. uint8_t v4[8];
  47. } GUID;
  48. typedef struct {
  49. GUID guid; // generated by client computer
  50. uint64_t file_size; // in bytes
  51. // invalid if broadcasting
  52. uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601
  53. // invalid if broadcasting
  54. uint64_t packets_count; // how many packets are there in the file
  55. // invalid if broadcasting
  56. uint64_t play_time; // play time, in 100-nanosecond units
  57. // invalid if broadcasting
  58. uint64_t send_time; // time to send file, in 100-nanosecond units
  59. // invalid if broadcasting (could be ignored)
  60. uint32_t preroll; // timestamp of the first packet, in milliseconds
  61. // if nonzero - substract from time
  62. uint32_t ignore; // preroll is 64bit - but let's just ignore it
  63. uint32_t flags; // 0x01 - broadcast
  64. // 0x02 - seekable
  65. // rest is reserved should be 0
  66. uint32_t min_pktsize; // size of a data packet
  67. // invalid if broadcasting
  68. uint32_t max_pktsize; // shall be the same as for min_pktsize
  69. // invalid if broadcasting
  70. uint32_t max_bitrate; // bandwith of stream in bps
  71. // should be the sum of bitrates of the
  72. // individual media streams
  73. } ASFMainHeader;
  74. typedef struct {
  75. int seqno;
  76. int packet_size;
  77. int is_streamed;
  78. int asfid2avid[128]; /* conversion table from asf ID 2 AVStream ID */
  79. ASFStream streams[128]; /* it's max number and it's not that big */
  80. /* non streamed additonnal info */
  81. int64_t nb_packets;
  82. int64_t duration; /* in 100ns units */
  83. /* packet filling */
  84. int packet_size_left;
  85. int packet_timestamp_start;
  86. int packet_timestamp_end;
  87. int packet_nb_frames;
  88. uint8_t packet_buf[PACKET_SIZE];
  89. ByteIOContext pb;
  90. /* only for reading */
  91. uint64_t data_offset; /* begining of the first data packet */
  92. ASFMainHeader hdr;
  93. int packet_flags;
  94. int packet_property;
  95. int packet_timestamp;
  96. int packet_segsizetype;
  97. int packet_segments;
  98. int packet_seq;
  99. int packet_replic_size;
  100. int packet_key_frame;
  101. int packet_padsize;
  102. int packet_frag_offset;
  103. int packet_frag_size;
  104. int packet_frag_timestamp;
  105. int packet_multi_size;
  106. int packet_obj_size;
  107. int packet_time_delta;
  108. int packet_time_start;
  109. int packet_pos;
  110. int stream_index;
  111. ASFStream* asf_st; /* currently decoded stream */
  112. } ASFContext;
  113. static const GUID asf_header = {
  114. 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
  115. };
  116. static const GUID file_header = {
  117. 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
  118. };
  119. static const GUID stream_header = {
  120. 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
  121. };
  122. static const GUID audio_stream = {
  123. 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
  124. };
  125. static const GUID audio_conceal_none = {
  126. // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
  127. // New value lifted from avifile
  128. 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b },
  129. };
  130. static const GUID video_stream = {
  131. 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
  132. };
  133. static const GUID video_conceal_none = {
  134. 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
  135. };
  136. static const GUID comment_header = {
  137. 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
  138. };
  139. static const GUID codec_comment_header = {
  140. 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
  141. };
  142. static const GUID codec_comment1_header = {
  143. 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
  144. };
  145. static const GUID data_header = {
  146. 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
  147. };
  148. static const GUID index_guid = {
  149. 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
  150. };
  151. static const GUID head1_guid = {
  152. 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
  153. };
  154. static const GUID head2_guid = {
  155. 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
  156. };
  157. static const GUID extended_content_header = {
  158. 0xD2D0A440, 0xE307, 0x11D2, { 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50 },
  159. };
  160. /* I am not a number !!! This GUID is the one found on the PC used to
  161. generate the stream */
  162. static const GUID my_guid = {
  163. 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
  164. };
  165. #ifdef CONFIG_ENCODERS
  166. static void put_guid(ByteIOContext *s, const GUID *g)
  167. {
  168. int i;
  169. put_le32(s, g->v1);
  170. put_le16(s, g->v2);
  171. put_le16(s, g->v3);
  172. for(i=0;i<8;i++)
  173. put_byte(s, g->v4[i]);
  174. }
  175. static void put_str16(ByteIOContext *s, const char *tag)
  176. {
  177. int c;
  178. put_le16(s,strlen(tag) + 1);
  179. for(;;) {
  180. c = (uint8_t)*tag++;
  181. put_le16(s, c);
  182. if (c == '\0')
  183. break;
  184. }
  185. }
  186. static void put_str16_nolen(ByteIOContext *s, const char *tag)
  187. {
  188. int c;
  189. for(;;) {
  190. c = (uint8_t)*tag++;
  191. put_le16(s, c);
  192. if (c == '\0')
  193. break;
  194. }
  195. }
  196. static int64_t put_header(ByteIOContext *pb, const GUID *g)
  197. {
  198. int64_t pos;
  199. pos = url_ftell(pb);
  200. put_guid(pb, g);
  201. put_le64(pb, 24);
  202. return pos;
  203. }
  204. /* update header size */
  205. static void end_header(ByteIOContext *pb, int64_t pos)
  206. {
  207. int64_t pos1;
  208. pos1 = url_ftell(pb);
  209. url_fseek(pb, pos + 16, SEEK_SET);
  210. put_le64(pb, pos1 - pos);
  211. url_fseek(pb, pos1, SEEK_SET);
  212. }
  213. /* write an asf chunk (only used in streaming case) */
  214. static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
  215. {
  216. ASFContext *asf = s->priv_data;
  217. ByteIOContext *pb = &s->pb;
  218. int length;
  219. length = payload_length + 8;
  220. put_le16(pb, type);
  221. put_le16(pb, length);
  222. put_le32(pb, asf->seqno);
  223. put_le16(pb, flags); /* unknown bytes */
  224. put_le16(pb, length);
  225. asf->seqno++;
  226. }
  227. /* convert from unix to windows time */
  228. static int64_t unix_to_file_time(int ti)
  229. {
  230. int64_t t;
  231. t = ti * int64_t_C(10000000);
  232. t += int64_t_C(116444736000000000);
  233. return t;
  234. }
  235. /* write the header (used two times if non streamed) */
  236. static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
  237. {
  238. ASFContext *asf = s->priv_data;
  239. ByteIOContext *pb = &s->pb;
  240. int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
  241. int has_title;
  242. AVCodecContext *enc;
  243. int64_t header_offset, cur_pos, hpos;
  244. int bit_rate;
  245. has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
  246. bit_rate = 0;
  247. for(n=0;n<s->nb_streams;n++) {
  248. enc = &s->streams[n]->codec;
  249. bit_rate += enc->bit_rate;
  250. }
  251. if (asf->is_streamed) {
  252. put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
  253. }
  254. put_guid(pb, &asf_header);
  255. put_le64(pb, -1); /* header length, will be patched after */
  256. put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
  257. put_byte(pb, 1); /* ??? */
  258. put_byte(pb, 2); /* ??? */
  259. /* file header */
  260. header_offset = url_ftell(pb);
  261. hpos = put_header(pb, &file_header);
  262. put_guid(pb, &my_guid);
  263. put_le64(pb, file_size);
  264. file_time = 0;
  265. put_le64(pb, unix_to_file_time(file_time));
  266. put_le64(pb, asf->nb_packets); /* number of packets */
  267. put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
  268. put_le64(pb, asf->duration); /* duration (in 100ns units) */
  269. put_le32(pb, 0); /* start time stamp */
  270. put_le32(pb, 0); /* ??? */
  271. put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
  272. put_le32(pb, asf->packet_size); /* packet size */
  273. put_le32(pb, asf->packet_size); /* packet size */
  274. put_le32(pb, bit_rate); /* Nominal data rate in bps */
  275. end_header(pb, hpos);
  276. /* unknown headers */
  277. hpos = put_header(pb, &head1_guid);
  278. put_guid(pb, &head2_guid);
  279. put_le32(pb, 6);
  280. put_le16(pb, 0);
  281. end_header(pb, hpos);
  282. /* title and other infos */
  283. if (has_title) {
  284. hpos = put_header(pb, &comment_header);
  285. put_le16(pb, 2 * (strlen(s->title) + 1));
  286. put_le16(pb, 2 * (strlen(s->author) + 1));
  287. put_le16(pb, 2 * (strlen(s->copyright) + 1));
  288. put_le16(pb, 2 * (strlen(s->comment) + 1));
  289. put_le16(pb, 0);
  290. put_str16_nolen(pb, s->title);
  291. put_str16_nolen(pb, s->author);
  292. put_str16_nolen(pb, s->copyright);
  293. put_str16_nolen(pb, s->comment);
  294. end_header(pb, hpos);
  295. }
  296. /* stream headers */
  297. for(n=0;n<s->nb_streams;n++) {
  298. int64_t es_pos;
  299. // ASFStream *stream = &asf->streams[n];
  300. enc = &s->streams[n]->codec;
  301. asf->streams[n].num = n + 1;
  302. asf->streams[n].seq = 0;
  303. switch(enc->codec_type) {
  304. case CODEC_TYPE_AUDIO:
  305. wav_extra_size = 0;
  306. extra_size = 18 + wav_extra_size;
  307. extra_size2 = 0;
  308. break;
  309. default:
  310. case CODEC_TYPE_VIDEO:
  311. wav_extra_size = 0;
  312. extra_size = 0x33;
  313. extra_size2 = 0;
  314. break;
  315. }
  316. hpos = put_header(pb, &stream_header);
  317. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  318. put_guid(pb, &audio_stream);
  319. put_guid(pb, &audio_conceal_none);
  320. } else {
  321. put_guid(pb, &video_stream);
  322. put_guid(pb, &video_conceal_none);
  323. }
  324. put_le64(pb, 0); /* ??? */
  325. es_pos = url_ftell(pb);
  326. put_le32(pb, extra_size); /* wav header len */
  327. put_le32(pb, extra_size2); /* additional data len */
  328. put_le16(pb, n + 1); /* stream number */
  329. put_le32(pb, 0); /* ??? */
  330. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  331. /* WAVEFORMATEX header */
  332. int wavsize = put_wav_header(pb, enc);
  333. if (wavsize < 0)
  334. return -1;
  335. if (wavsize != extra_size) {
  336. cur_pos = url_ftell(pb);
  337. url_fseek(pb, es_pos, SEEK_SET);
  338. put_le32(pb, wavsize); /* wav header len */
  339. url_fseek(pb, cur_pos, SEEK_SET);
  340. }
  341. } else {
  342. put_le32(pb, enc->width);
  343. put_le32(pb, enc->height);
  344. put_byte(pb, 2); /* ??? */
  345. put_le16(pb, 40); /* size */
  346. /* BITMAPINFOHEADER header */
  347. put_bmp_header(pb, enc, codec_bmp_tags, 1);
  348. }
  349. end_header(pb, hpos);
  350. }
  351. /* media comments */
  352. hpos = put_header(pb, &codec_comment_header);
  353. put_guid(pb, &codec_comment1_header);
  354. put_le32(pb, s->nb_streams);
  355. for(n=0;n<s->nb_streams;n++) {
  356. AVCodec *p;
  357. enc = &s->streams[n]->codec;
  358. p = avcodec_find_encoder(enc->codec_id);
  359. put_le16(pb, asf->streams[n].num);
  360. put_str16(pb, p ? p->name : enc->codec_name);
  361. put_le16(pb, 0); /* no parameters */
  362. /* id */
  363. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  364. put_le16(pb, 2);
  365. if(!enc->codec_tag)
  366. enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  367. if(!enc->codec_tag)
  368. return -1;
  369. put_le16(pb, enc->codec_tag);
  370. } else {
  371. put_le16(pb, 4);
  372. if(!enc->codec_tag)
  373. enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id);
  374. if(!enc->codec_tag)
  375. return -1;
  376. put_le32(pb, enc->codec_tag);
  377. }
  378. }
  379. end_header(pb, hpos);
  380. /* patch the header size fields */
  381. cur_pos = url_ftell(pb);
  382. header_size = cur_pos - header_offset;
  383. if (asf->is_streamed) {
  384. header_size += 8 + 30 + 50;
  385. url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
  386. put_le16(pb, header_size);
  387. url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
  388. put_le16(pb, header_size);
  389. header_size -= 8 + 30 + 50;
  390. }
  391. header_size += 24 + 6;
  392. url_fseek(pb, header_offset - 14, SEEK_SET);
  393. put_le64(pb, header_size);
  394. url_fseek(pb, cur_pos, SEEK_SET);
  395. /* movie chunk, followed by packets of packet_size */
  396. asf->data_offset = cur_pos;
  397. put_guid(pb, &data_header);
  398. put_le64(pb, data_chunk_size);
  399. put_guid(pb, &my_guid);
  400. put_le64(pb, asf->nb_packets); /* nb packets */
  401. put_byte(pb, 1); /* ??? */
  402. put_byte(pb, 1); /* ??? */
  403. return 0;
  404. }
  405. static int asf_write_header(AVFormatContext *s)
  406. {
  407. ASFContext *asf = s->priv_data;
  408. av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
  409. asf->packet_size = PACKET_SIZE;
  410. asf->nb_packets = 0;
  411. if (asf_write_header1(s, 0, 50) < 0) {
  412. //av_free(asf);
  413. return -1;
  414. }
  415. put_flush_packet(&s->pb);
  416. asf->packet_nb_frames = 0;
  417. asf->packet_timestamp_start = -1;
  418. asf->packet_timestamp_end = -1;
  419. asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
  420. init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
  421. NULL, NULL, NULL, NULL);
  422. return 0;
  423. }
  424. static int asf_write_stream_header(AVFormatContext *s)
  425. {
  426. ASFContext *asf = s->priv_data;
  427. asf->is_streamed = 1;
  428. return asf_write_header(s);
  429. }
  430. /* write a fixed size packet */
  431. static int put_packet(AVFormatContext *s,
  432. unsigned int timestamp, unsigned int duration,
  433. int nb_frames, int padsize)
  434. {
  435. ASFContext *asf = s->priv_data;
  436. ByteIOContext *pb = &s->pb;
  437. int flags;
  438. if (asf->is_streamed) {
  439. put_chunk(s, 0x4424, asf->packet_size, 0);
  440. }
  441. put_byte(pb, 0x82);
  442. put_le16(pb, 0);
  443. flags = 0x01; /* nb segments present */
  444. if (padsize > 0) {
  445. if (padsize < 256)
  446. flags |= 0x08;
  447. else
  448. flags |= 0x10;
  449. }
  450. put_byte(pb, flags); /* flags */
  451. put_byte(pb, 0x5d);
  452. if (flags & 0x10)
  453. put_le16(pb, padsize - 2);
  454. if (flags & 0x08)
  455. put_byte(pb, padsize - 1);
  456. put_le32(pb, timestamp);
  457. put_le16(pb, duration);
  458. put_byte(pb, nb_frames | 0x80);
  459. return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
  460. }
  461. static void flush_packet(AVFormatContext *s)
  462. {
  463. ASFContext *asf = s->priv_data;
  464. int hdr_size, ptr;
  465. hdr_size = put_packet(s, asf->packet_timestamp_start,
  466. asf->packet_timestamp_end - asf->packet_timestamp_start,
  467. asf->packet_nb_frames, asf->packet_size_left);
  468. /* Clear out the padding bytes */
  469. ptr = asf->packet_size - hdr_size - asf->packet_size_left;
  470. memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
  471. put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
  472. put_flush_packet(&s->pb);
  473. asf->nb_packets++;
  474. asf->packet_nb_frames = 0;
  475. asf->packet_timestamp_start = -1;
  476. asf->packet_timestamp_end = -1;
  477. asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
  478. init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
  479. NULL, NULL, NULL, NULL);
  480. }
  481. static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
  482. int payload_size, int frag_offset, int frag_len)
  483. {
  484. ASFContext *asf = s->priv_data;
  485. ByteIOContext *pb = &asf->pb;
  486. int val;
  487. val = stream->num;
  488. if (s->streams[val - 1]->codec.coded_frame->key_frame /* && frag_offset == 0 */)
  489. val |= 0x80;
  490. put_byte(pb, val);
  491. put_byte(pb, stream->seq);
  492. put_le32(pb, frag_offset); /* fragment offset */
  493. put_byte(pb, 0x08); /* flags */
  494. put_le32(pb, payload_size);
  495. put_le32(pb, timestamp);
  496. put_le16(pb, frag_len);
  497. }
  498. /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
  499. It is there that you understand that the ASF format is really
  500. crap. They have misread the MPEG Systems spec !
  501. */
  502. static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
  503. const uint8_t *buf, int payload_size)
  504. {
  505. ASFContext *asf = s->priv_data;
  506. int frag_pos, frag_len, frag_len1;
  507. frag_pos = 0;
  508. while (frag_pos < payload_size) {
  509. frag_len = payload_size - frag_pos;
  510. frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
  511. if (frag_len1 > 0) {
  512. if (frag_len > frag_len1)
  513. frag_len = frag_len1;
  514. put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len);
  515. put_buffer(&asf->pb, buf, frag_len);
  516. asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
  517. asf->packet_timestamp_end = timestamp;
  518. if (asf->packet_timestamp_start == -1)
  519. asf->packet_timestamp_start = timestamp;
  520. asf->packet_nb_frames++;
  521. } else {
  522. frag_len = 0;
  523. }
  524. frag_pos += frag_len;
  525. buf += frag_len;
  526. /* output the frame if filled */
  527. if (asf->packet_size_left <= FRAME_HEADER_SIZE)
  528. flush_packet(s);
  529. }
  530. stream->seq++;
  531. }
  532. static int asf_write_packet(AVFormatContext *s, int stream_index,
  533. const uint8_t *buf, int size, int64_t timestamp)
  534. {
  535. ASFContext *asf = s->priv_data;
  536. ASFStream *stream;
  537. int64_t duration;
  538. AVCodecContext *codec;
  539. codec = &s->streams[stream_index]->codec;
  540. stream = &asf->streams[stream_index];
  541. if (codec->codec_type == CODEC_TYPE_AUDIO) {
  542. duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) /
  543. codec->sample_rate;
  544. } else {
  545. duration = av_rescale(codec->frame_number * codec->frame_rate_base, 10000000, codec->frame_rate);
  546. }
  547. if (duration > asf->duration)
  548. asf->duration = duration;
  549. put_frame(s, stream, timestamp, buf, size);
  550. return 0;
  551. }
  552. static int asf_write_trailer(AVFormatContext *s)
  553. {
  554. ASFContext *asf = s->priv_data;
  555. int64_t file_size;
  556. /* flush the current packet */
  557. if (asf->pb.buf_ptr > asf->pb.buffer)
  558. flush_packet(s);
  559. if (asf->is_streamed) {
  560. put_chunk(s, 0x4524, 0, 0); /* end of stream */
  561. } else {
  562. /* rewrite an updated header */
  563. file_size = url_ftell(&s->pb);
  564. url_fseek(&s->pb, 0, SEEK_SET);
  565. asf_write_header1(s, file_size, file_size - asf->data_offset);
  566. }
  567. put_flush_packet(&s->pb);
  568. return 0;
  569. }
  570. #endif //CONFIG_ENCODERS
  571. /**********************************/
  572. /* decoding */
  573. //#define DEBUG
  574. #ifdef DEBUG
  575. #define PRINT_IF_GUID(g,cmp) \
  576. if (!memcmp(g, &cmp, sizeof(GUID))) \
  577. printf("(GUID: %s) ", #cmp)
  578. static void print_guid(const GUID *g)
  579. {
  580. int i;
  581. PRINT_IF_GUID(g, asf_header);
  582. else PRINT_IF_GUID(g, file_header);
  583. else PRINT_IF_GUID(g, stream_header);
  584. else PRINT_IF_GUID(g, audio_stream);
  585. else PRINT_IF_GUID(g, audio_conceal_none);
  586. else PRINT_IF_GUID(g, video_stream);
  587. else PRINT_IF_GUID(g, video_conceal_none);
  588. else PRINT_IF_GUID(g, comment_header);
  589. else PRINT_IF_GUID(g, codec_comment_header);
  590. else PRINT_IF_GUID(g, codec_comment1_header);
  591. else PRINT_IF_GUID(g, data_header);
  592. else PRINT_IF_GUID(g, index_guid);
  593. else PRINT_IF_GUID(g, head1_guid);
  594. else PRINT_IF_GUID(g, head2_guid);
  595. else PRINT_IF_GUID(g, my_guid);
  596. else
  597. printf("(GUID: unknown) ");
  598. printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
  599. for(i=0;i<8;i++)
  600. printf(" 0x%02x,", g->v4[i]);
  601. printf("}\n");
  602. }
  603. #undef PRINT_IF_GUID(g,cmp)
  604. #endif
  605. static void get_guid(ByteIOContext *s, GUID *g)
  606. {
  607. int i;
  608. g->v1 = get_le32(s);
  609. g->v2 = get_le16(s);
  610. g->v3 = get_le16(s);
  611. for(i=0;i<8;i++)
  612. g->v4[i] = get_byte(s);
  613. }
  614. #if 0
  615. static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
  616. {
  617. int len, c;
  618. char *q;
  619. len = get_le16(pb);
  620. q = buf;
  621. while (len > 0) {
  622. c = get_le16(pb);
  623. if ((q - buf) < buf_size - 1)
  624. *q++ = c;
  625. len--;
  626. }
  627. *q = '\0';
  628. }
  629. #endif
  630. static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
  631. {
  632. int c;
  633. char *q;
  634. q = buf;
  635. while (len > 0) {
  636. c = get_le16(pb);
  637. if ((q - buf) < buf_size - 1)
  638. *q++ = c;
  639. len-=2;
  640. }
  641. *q = '\0';
  642. }
  643. static int asf_probe(AVProbeData *pd)
  644. {
  645. GUID g;
  646. const unsigned char *p;
  647. int i;
  648. /* check file header */
  649. if (pd->buf_size <= 32)
  650. return 0;
  651. p = pd->buf;
  652. g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  653. p += 4;
  654. g.v2 = p[0] | (p[1] << 8);
  655. p += 2;
  656. g.v3 = p[0] | (p[1] << 8);
  657. p += 2;
  658. for(i=0;i<8;i++)
  659. g.v4[i] = *p++;
  660. if (!memcmp(&g, &asf_header, sizeof(GUID)))
  661. return AVPROBE_SCORE_MAX;
  662. else
  663. return 0;
  664. }
  665. static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  666. {
  667. ASFContext *asf = s->priv_data;
  668. GUID g;
  669. ByteIOContext *pb = &s->pb;
  670. AVStream *st;
  671. ASFStream *asf_st;
  672. int size, i;
  673. int64_t gsize;
  674. av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
  675. get_guid(pb, &g);
  676. if (memcmp(&g, &asf_header, sizeof(GUID)))
  677. goto fail;
  678. get_le64(pb);
  679. get_le32(pb);
  680. get_byte(pb);
  681. get_byte(pb);
  682. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  683. for(;;) {
  684. get_guid(pb, &g);
  685. gsize = get_le64(pb);
  686. #ifdef DEBUG
  687. printf("%08Lx: ", url_ftell(pb) - 24);
  688. print_guid(&g);
  689. printf(" size=0x%Lx\n", gsize);
  690. #endif
  691. if (gsize < 24)
  692. goto fail;
  693. if (!memcmp(&g, &file_header, sizeof(GUID))) {
  694. get_guid(pb, &asf->hdr.guid);
  695. asf->hdr.file_size = get_le64(pb);
  696. asf->hdr.create_time = get_le64(pb);
  697. asf->hdr.packets_count = get_le64(pb);
  698. asf->hdr.play_time = get_le64(pb);
  699. asf->hdr.send_time = get_le64(pb);
  700. asf->hdr.preroll = get_le32(pb);
  701. asf->hdr.ignore = get_le32(pb);
  702. asf->hdr.flags = get_le32(pb);
  703. asf->hdr.min_pktsize = get_le32(pb);
  704. asf->hdr.max_pktsize = get_le32(pb);
  705. asf->hdr.max_bitrate = get_le32(pb);
  706. asf->packet_size = asf->hdr.max_pktsize;
  707. asf->nb_packets = asf->hdr.packets_count;
  708. } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
  709. int type, total_size, type_specific_size;
  710. unsigned int tag1;
  711. int64_t pos1, pos2;
  712. pos1 = url_ftell(pb);
  713. st = av_new_stream(s, 0);
  714. if (!st)
  715. goto fail;
  716. asf_st = av_mallocz(sizeof(ASFStream));
  717. if (!asf_st)
  718. goto fail;
  719. st->priv_data = asf_st;
  720. st->start_time = asf->hdr.preroll / (10000000 / AV_TIME_BASE);
  721. st->duration = (asf->hdr.send_time - asf->hdr.preroll) /
  722. (10000000 / AV_TIME_BASE);
  723. get_guid(pb, &g);
  724. if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
  725. type = CODEC_TYPE_AUDIO;
  726. } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
  727. type = CODEC_TYPE_VIDEO;
  728. } else {
  729. goto fail;
  730. }
  731. get_guid(pb, &g);
  732. total_size = get_le64(pb);
  733. type_specific_size = get_le32(pb);
  734. get_le32(pb);
  735. st->id = get_le16(pb) & 0x7f; /* stream id */
  736. // mapping of asf ID to AV stream ID;
  737. asf->asfid2avid[st->id] = s->nb_streams - 1;
  738. get_le32(pb);
  739. st->codec.codec_type = type;
  740. /* 1 fps default (XXX: put 0 fps instead) */
  741. st->codec.frame_rate = 1;
  742. st->codec.frame_rate_base = 1;
  743. if (type == CODEC_TYPE_AUDIO) {
  744. get_wav_header(pb, &st->codec, type_specific_size);
  745. st->need_parsing = 1;
  746. /* We have to init the frame size at some point .... */
  747. pos2 = url_ftell(pb);
  748. if (gsize > (pos2 + 8 - pos1 + 24)) {
  749. asf_st->ds_span = get_byte(pb);
  750. asf_st->ds_packet_size = get_le16(pb);
  751. asf_st->ds_chunk_size = get_le16(pb);
  752. asf_st->ds_data_size = get_le16(pb);
  753. asf_st->ds_silence_data = get_byte(pb);
  754. }
  755. //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
  756. // asf_st->ds_packet_size, asf_st->ds_chunk_size,
  757. // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
  758. if (asf_st->ds_span > 1) {
  759. if (!asf_st->ds_chunk_size
  760. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
  761. asf_st->ds_span = 0; // disable descrambling
  762. }
  763. switch (st->codec.codec_id) {
  764. case CODEC_ID_MP3:
  765. st->codec.frame_size = MPA_FRAME_SIZE;
  766. break;
  767. case CODEC_ID_PCM_S16LE:
  768. case CODEC_ID_PCM_S16BE:
  769. case CODEC_ID_PCM_U16LE:
  770. case CODEC_ID_PCM_U16BE:
  771. case CODEC_ID_PCM_S8:
  772. case CODEC_ID_PCM_U8:
  773. case CODEC_ID_PCM_ALAW:
  774. case CODEC_ID_PCM_MULAW:
  775. st->codec.frame_size = 1;
  776. break;
  777. default:
  778. /* This is probably wrong, but it prevents a crash later */
  779. st->codec.frame_size = 1;
  780. break;
  781. }
  782. } else {
  783. get_le32(pb);
  784. get_le32(pb);
  785. get_byte(pb);
  786. size = get_le16(pb); /* size */
  787. get_le32(pb); /* size */
  788. st->codec.width = get_le32(pb);
  789. st->codec.height = get_le32(pb);
  790. /* not available for asf */
  791. get_le16(pb); /* panes */
  792. st->codec.bits_per_sample = get_le16(pb); /* depth */
  793. tag1 = get_le32(pb);
  794. url_fskip(pb, 20);
  795. if (size > 40) {
  796. st->codec.extradata_size = size - 40;
  797. st->codec.extradata = av_mallocz(st->codec.extradata_size);
  798. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  799. }
  800. /* Extract palette from extradata if bpp <= 8 */
  801. /* This code assumes that extradata contains only palette */
  802. /* This is true for all paletted codecs implemented in ffmpeg */
  803. if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
  804. st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
  805. #ifdef WORDS_BIGENDIAN
  806. for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
  807. st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
  808. #else
  809. memcpy(st->codec.palctrl->palette, st->codec.extradata,
  810. FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
  811. #endif
  812. st->codec.palctrl->palette_changed = 1;
  813. }
  814. st->codec.codec_tag = tag1;
  815. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  816. }
  817. pos2 = url_ftell(pb);
  818. url_fskip(pb, gsize - (pos2 - pos1 + 24));
  819. } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
  820. break;
  821. } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
  822. int len1, len2, len3, len4, len5;
  823. len1 = get_le16(pb);
  824. len2 = get_le16(pb);
  825. len3 = get_le16(pb);
  826. len4 = get_le16(pb);
  827. len5 = get_le16(pb);
  828. get_str16_nolen(pb, len1, s->title, sizeof(s->title));
  829. get_str16_nolen(pb, len2, s->author, sizeof(s->author));
  830. get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
  831. get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
  832. url_fskip(pb, len5);
  833. } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) {
  834. int desc_count, i;
  835. desc_count = get_le16(pb);
  836. for(i=0;i<desc_count;i++)
  837. {
  838. int name_len,value_type,value_len,value_num = 0;
  839. char *name, *value;
  840. name_len = get_le16(pb);
  841. name = (char *)av_mallocz(name_len);
  842. get_str16_nolen(pb, name_len, name, name_len);
  843. value_type = get_le16(pb);
  844. value_len = get_le16(pb);
  845. if ((value_type == 0) || (value_type == 1)) // unicode or byte
  846. {
  847. value = (char *)av_mallocz(value_len);
  848. get_str16_nolen(pb, value_len, value, value_len);
  849. if (strcmp(name,"WM/AlbumTitle")==0) { strcpy(s->album, value); }
  850. av_free(value);
  851. }
  852. if ((value_type >= 2) || (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  853. {
  854. if (value_type==2) value_num = get_le32(pb);
  855. if (value_type==3) value_num = get_le32(pb);
  856. if (value_type==4) value_num = get_le64(pb);
  857. if (value_type==5) value_num = get_le16(pb);
  858. if (strcmp(name,"WM/Track")==0) s->track = value_num + 1;
  859. if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num;
  860. }
  861. av_free(name);
  862. }
  863. #if 0
  864. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  865. int v1, v2;
  866. get_guid(pb, &g);
  867. v1 = get_le32(pb);
  868. v2 = get_le16(pb);
  869. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  870. int len, v1, n, num;
  871. char str[256], *q;
  872. char tag[16];
  873. get_guid(pb, &g);
  874. print_guid(&g);
  875. n = get_le32(pb);
  876. for(i=0;i<n;i++) {
  877. num = get_le16(pb); /* stream number */
  878. get_str16(pb, str, sizeof(str));
  879. get_str16(pb, str, sizeof(str));
  880. len = get_le16(pb);
  881. q = tag;
  882. while (len > 0) {
  883. v1 = get_byte(pb);
  884. if ((q - tag) < sizeof(tag) - 1)
  885. *q++ = v1;
  886. len--;
  887. }
  888. *q = '\0';
  889. }
  890. #endif
  891. } else if (url_feof(pb)) {
  892. goto fail;
  893. } else {
  894. url_fseek(pb, gsize - 24, SEEK_CUR);
  895. }
  896. }
  897. get_guid(pb, &g);
  898. get_le64(pb);
  899. get_byte(pb);
  900. get_byte(pb);
  901. if (url_feof(pb))
  902. goto fail;
  903. asf->data_offset = url_ftell(pb);
  904. asf->packet_size_left = 0;
  905. return 0;
  906. fail:
  907. for(i=0;i<s->nb_streams;i++) {
  908. AVStream *st = s->streams[i];
  909. if (st) {
  910. av_free(st->priv_data);
  911. av_free(st->codec.extradata);
  912. }
  913. av_free(st);
  914. }
  915. return -1;
  916. }
  917. #define DO_2BITS(bits, var, defval) \
  918. switch (bits & 3) \
  919. { \
  920. case 3: var = get_le32(pb); rsize += 4; break; \
  921. case 2: var = get_le16(pb); rsize += 2; break; \
  922. case 1: var = get_byte(pb); rsize++; break; \
  923. default: var = defval; break; \
  924. }
  925. static int asf_get_packet(AVFormatContext *s)
  926. {
  927. ASFContext *asf = s->priv_data;
  928. ByteIOContext *pb = &s->pb;
  929. uint32_t packet_length, padsize;
  930. int rsize = 9;
  931. int c;
  932. assert((url_ftell(&s->pb) - s->data_offset) % asf->packet_size == 0);
  933. c = get_byte(pb);
  934. if (c != 0x82) {
  935. if (!url_feof(pb))
  936. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  937. }
  938. if ((c & 0x0f) == 2) { // always true for now
  939. if (get_le16(pb) != 0) {
  940. if (!url_feof(pb))
  941. printf("ff asf bad non zero\n");
  942. return -EIO;
  943. }
  944. rsize+=2;
  945. /* }else{
  946. if (!url_feof(pb))
  947. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  948. return -EIO;*/
  949. }
  950. asf->packet_flags = get_byte(pb);
  951. asf->packet_property = get_byte(pb);
  952. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  953. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  954. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  955. asf->packet_timestamp = get_le32(pb);
  956. get_le16(pb); /* duration */
  957. // rsize has at least 11 bytes which have to be present
  958. if (asf->packet_flags & 0x01) {
  959. asf->packet_segsizetype = get_byte(pb); rsize++;
  960. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  961. } else {
  962. asf->packet_segments = 1;
  963. asf->packet_segsizetype = 0x80;
  964. }
  965. asf->packet_size_left = packet_length - padsize - rsize;
  966. if (packet_length < asf->hdr.min_pktsize)
  967. padsize += asf->hdr.min_pktsize - packet_length;
  968. asf->packet_padsize = padsize;
  969. #ifdef DEBUG
  970. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  971. #endif
  972. return 0;
  973. }
  974. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  975. {
  976. ASFContext *asf = s->priv_data;
  977. ASFStream *asf_st = 0;
  978. ByteIOContext *pb = &s->pb;
  979. //static int pc = 0;
  980. for (;;) {
  981. int rsize = 0;
  982. if (asf->packet_size_left < FRAME_HEADER_SIZE
  983. || asf->packet_segments < 1) {
  984. //asf->packet_size_left <= asf->packet_padsize) {
  985. int ret = asf->packet_size_left + asf->packet_padsize;
  986. //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  987. /* fail safe */
  988. url_fskip(pb, ret);
  989. asf->packet_pos= url_ftell(&s->pb);
  990. ret = asf_get_packet(s);
  991. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  992. if (ret < 0 || url_feof(pb))
  993. return -EIO;
  994. asf->packet_time_start = 0;
  995. continue;
  996. }
  997. if (asf->packet_time_start == 0) {
  998. /* read frame header */
  999. int num = get_byte(pb);
  1000. asf->packet_segments--;
  1001. rsize++;
  1002. asf->packet_key_frame = (num & 0x80) >> 7;
  1003. asf->stream_index = asf->asfid2avid[num & 0x7f];
  1004. // sequence should be ignored!
  1005. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  1006. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  1007. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  1008. //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);
  1009. if (asf->packet_replic_size > 1) {
  1010. assert(asf->packet_replic_size >= 8);
  1011. // it should be always at least 8 bytes - FIXME validate
  1012. asf->packet_obj_size = get_le32(pb);
  1013. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  1014. if (asf->packet_replic_size > 8)
  1015. url_fskip(pb, asf->packet_replic_size - 8);
  1016. rsize += asf->packet_replic_size; // FIXME - check validity
  1017. } else if (asf->packet_replic_size==1){
  1018. // multipacket - frag_offset is begining timestamp
  1019. asf->packet_time_start = asf->packet_frag_offset;
  1020. asf->packet_frag_offset = 0;
  1021. asf->packet_frag_timestamp = asf->packet_timestamp;
  1022. asf->packet_time_delta = get_byte(pb);
  1023. rsize++;
  1024. }else{
  1025. assert(asf->packet_replic_size==0);
  1026. }
  1027. if (asf->packet_flags & 0x01) {
  1028. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  1029. #undef DO_2BITS
  1030. //printf("Fragsize %d\n", asf->packet_frag_size);
  1031. } else {
  1032. asf->packet_frag_size = asf->packet_size_left - rsize;
  1033. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  1034. }
  1035. if (asf->packet_replic_size == 1) {
  1036. asf->packet_multi_size = asf->packet_frag_size;
  1037. if (asf->packet_multi_size > asf->packet_size_left) {
  1038. asf->packet_segments = 0;
  1039. continue;
  1040. }
  1041. }
  1042. asf->packet_size_left -= rsize;
  1043. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  1044. if (asf->stream_index < 0) {
  1045. asf->packet_time_start = 0;
  1046. /* unhandled packet (should not happen) */
  1047. url_fskip(pb, asf->packet_frag_size);
  1048. asf->packet_size_left -= asf->packet_frag_size;
  1049. printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
  1050. continue;
  1051. }
  1052. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  1053. }
  1054. asf_st = asf->asf_st;
  1055. if ((asf->packet_frag_offset != asf_st->frag_offset
  1056. || (asf->packet_frag_offset
  1057. && asf->packet_seq != asf_st->seq)) // seq should be ignored
  1058. ) {
  1059. /* cannot continue current packet: free it */
  1060. // FIXME better check if packet was already allocated
  1061. printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
  1062. asf_st->pkt.size,
  1063. asf->packet_obj_size,
  1064. asf->packet_frag_offset, asf_st->frag_offset,
  1065. asf->packet_seq, asf_st->seq, asf->packet_frag_size);
  1066. if (asf_st->pkt.size)
  1067. av_free_packet(&asf_st->pkt);
  1068. asf_st->frag_offset = 0;
  1069. if (asf->packet_frag_offset != 0) {
  1070. url_fskip(pb, asf->packet_frag_size);
  1071. printf("ff asf parser skiping %db\n", asf->packet_frag_size);
  1072. asf->packet_size_left -= asf->packet_frag_size;
  1073. continue;
  1074. }
  1075. }
  1076. if (asf->packet_replic_size == 1) {
  1077. // frag_offset is here used as the begining timestamp
  1078. asf->packet_frag_timestamp = asf->packet_time_start;
  1079. asf->packet_time_start += asf->packet_time_delta;
  1080. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  1081. asf->packet_size_left--;
  1082. asf->packet_multi_size--;
  1083. if (asf->packet_multi_size < asf->packet_obj_size)
  1084. {
  1085. asf->packet_time_start = 0;
  1086. url_fskip(pb, asf->packet_multi_size);
  1087. asf->packet_size_left -= asf->packet_multi_size;
  1088. continue;
  1089. }
  1090. asf->packet_multi_size -= asf->packet_obj_size;
  1091. //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);
  1092. }
  1093. if (asf_st->frag_offset == 0) {
  1094. /* new packet */
  1095. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  1096. asf_st->seq = asf->packet_seq;
  1097. asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
  1098. asf_st->pkt.stream_index = asf->stream_index;
  1099. asf_st->packet_pos= asf->packet_pos;
  1100. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  1101. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  1102. //s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  1103. if (s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO)
  1104. asf->packet_key_frame = 1;
  1105. if (asf->packet_key_frame)
  1106. asf_st->pkt.flags |= PKT_FLAG_KEY;
  1107. }
  1108. /* read data */
  1109. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  1110. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  1111. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  1112. asf->packet_size_left -= asf->packet_frag_size;
  1113. if (asf->packet_size_left < 0)
  1114. continue;
  1115. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  1116. asf->packet_frag_size);
  1117. asf_st->frag_offset += asf->packet_frag_size;
  1118. /* test if whole packet is read */
  1119. if (asf_st->frag_offset == asf_st->pkt.size) {
  1120. /* return packet */
  1121. if (asf_st->ds_span > 1) {
  1122. /* packet descrambling */
  1123. char* newdata = av_malloc(asf_st->pkt.size);
  1124. if (newdata) {
  1125. int offset = 0;
  1126. while (offset < asf_st->pkt.size) {
  1127. int off = offset / asf_st->ds_chunk_size;
  1128. int row = off / asf_st->ds_span;
  1129. int col = off % asf_st->ds_span;
  1130. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  1131. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  1132. memcpy(newdata + offset,
  1133. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  1134. asf_st->ds_chunk_size);
  1135. offset += asf_st->ds_chunk_size;
  1136. }
  1137. av_free(asf_st->pkt.data);
  1138. asf_st->pkt.data = newdata;
  1139. }
  1140. }
  1141. asf_st->frag_offset = 0;
  1142. memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
  1143. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  1144. asf_st->pkt.size = 0;
  1145. asf_st->pkt.data = 0;
  1146. break; // packet completed
  1147. }
  1148. }
  1149. return 0;
  1150. }
  1151. static int asf_read_close(AVFormatContext *s)
  1152. {
  1153. int i;
  1154. for(i=0;i<s->nb_streams;i++) {
  1155. AVStream *st = s->streams[i];
  1156. av_free(st->priv_data);
  1157. av_free(st->codec.extradata);
  1158. av_free(st->codec.palctrl);
  1159. }
  1160. return 0;
  1161. }
  1162. // Added to support seeking after packets have been read
  1163. // If information is not reset, read_packet fails due to
  1164. // leftover information from previous reads
  1165. static void asf_reset_header(AVFormatContext *s)
  1166. {
  1167. ASFContext *asf = s->priv_data;
  1168. ASFStream *asf_st;
  1169. int i;
  1170. asf->packet_nb_frames = 0;
  1171. asf->packet_timestamp_start = -1;
  1172. asf->packet_timestamp_end = -1;
  1173. asf->packet_size_left = 0;
  1174. asf->packet_segments = 0;
  1175. asf->packet_flags = 0;
  1176. asf->packet_property = 0;
  1177. asf->packet_timestamp = 0;
  1178. asf->packet_segsizetype = 0;
  1179. asf->packet_segments = 0;
  1180. asf->packet_seq = 0;
  1181. asf->packet_replic_size = 0;
  1182. asf->packet_key_frame = 0;
  1183. asf->packet_padsize = 0;
  1184. asf->packet_frag_offset = 0;
  1185. asf->packet_frag_size = 0;
  1186. asf->packet_frag_timestamp = 0;
  1187. asf->packet_multi_size = 0;
  1188. asf->packet_obj_size = 0;
  1189. asf->packet_time_delta = 0;
  1190. asf->packet_time_start = 0;
  1191. for(i=0; i<s->nb_streams; i++){
  1192. asf_st= s->streams[i]->priv_data;
  1193. av_free_packet(&asf_st->pkt);
  1194. asf_st->frag_offset=0;
  1195. asf_st->seq=0;
  1196. }
  1197. asf->asf_st= NULL;
  1198. }
  1199. static int64_t asf_read_pts(AVFormatContext *s, int64_t *ppos, int stream_index)
  1200. {
  1201. ASFContext *asf = s->priv_data;
  1202. AVPacket pkt1, *pkt = &pkt1;
  1203. ASFStream *asf_st;
  1204. int64_t pts;
  1205. int64_t pos= *ppos;
  1206. int i;
  1207. int64_t start_pos[s->nb_streams];
  1208. for(i=0; i<s->nb_streams; i++){
  1209. start_pos[i]= pos;
  1210. }
  1211. //printf("asf_read_pts\n");
  1212. url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET);
  1213. asf_reset_header(s);
  1214. for(;;){
  1215. if (av_read_frame(s, pkt) < 0){
  1216. printf("seek failed\n");
  1217. return AV_NOPTS_VALUE;
  1218. }
  1219. pts= pkt->pts;
  1220. av_free_packet(pkt);
  1221. if(pkt->flags&PKT_FLAG_KEY){
  1222. i= pkt->stream_index;
  1223. asf_st= s->streams[i]->priv_data;
  1224. assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  1225. pos= (asf_st->packet_pos - s->data_offset) / asf->packet_size;
  1226. av_add_index_entry(s->streams[i], pos, pts, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  1227. start_pos[i]= pos + 1;
  1228. if(pkt->stream_index == stream_index)
  1229. break;
  1230. }
  1231. }
  1232. *ppos= pos;
  1233. //printf("found keyframe at %Ld stream %d stamp:%Ld\n", *ppos, stream_index, pts);
  1234. return pts;
  1235. }
  1236. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts)
  1237. {
  1238. ASFContext *asf = s->priv_data;
  1239. AVStream *st;
  1240. int64_t pos;
  1241. int64_t pos_min, pos_max, pts_min, pts_max, cur_pts, pos_limit;
  1242. int no_change;
  1243. if (stream_index == -1)
  1244. stream_index= av_find_default_stream_index(s);
  1245. if (asf->packet_size <= 0)
  1246. return -1;
  1247. pts_max=
  1248. pts_min= AV_NOPTS_VALUE;
  1249. pos_max= pos_limit= -1; // gcc thinks its uninitalized
  1250. st= s->streams[stream_index];
  1251. if(st->index_entries){
  1252. AVIndexEntry *e;
  1253. int index;
  1254. index= av_index_search_timestamp(st, pts);
  1255. e= &st->index_entries[index];
  1256. if(e->timestamp <= pts){
  1257. pos_min= e->pos;
  1258. pts_min= e->timestamp;
  1259. #ifdef DEBUG_SEEK
  1260. printf("unsing cached pos_min=0x%llx dts_min=%0.3f\n",
  1261. pos_min,pts_min / 90000.0);
  1262. #endif
  1263. }else{
  1264. assert(index==0);
  1265. }
  1266. index++;
  1267. if(index < st->nb_index_entries){
  1268. e= &st->index_entries[index];
  1269. assert(e->timestamp >= pts);
  1270. pos_max= e->pos;
  1271. pts_max= e->timestamp;
  1272. pos_limit= pos_max - e->min_distance;
  1273. #ifdef DEBUG_SEEK
  1274. printf("unsing cached pos_max=0x%llx dts_max=%0.3f\n",
  1275. pos_max,pts_max / 90000.0);
  1276. #endif
  1277. }
  1278. }
  1279. if(pts_min == AV_NOPTS_VALUE){
  1280. pos_min = 0;
  1281. pts_min = asf_read_pts(s, &pos_min, stream_index);
  1282. if (pts_min == AV_NOPTS_VALUE) return -1;
  1283. }
  1284. if(pts_max == AV_NOPTS_VALUE){
  1285. pos_max = (url_filesize(url_fileno(&s->pb)) - 1 - s->data_offset) / asf->packet_size; //FIXME wrong
  1286. pts_max = s->duration; //FIXME wrong
  1287. pos_limit= pos_max;
  1288. }
  1289. no_change=0;
  1290. while (pos_min < pos_limit) {
  1291. int64_t start_pos;
  1292. assert(pos_limit <= pos_max);
  1293. if(no_change==0){
  1294. int64_t approximate_keyframe_distance= pos_max - pos_limit;
  1295. // interpolate position (better than dichotomy)
  1296. pos = (int64_t)((double)(pos_max - pos_min) *
  1297. (double)(pts - pts_min) /
  1298. (double)(pts_max - pts_min)) + pos_min - approximate_keyframe_distance;
  1299. }else if(no_change==1){
  1300. // bisection, if interpolation failed to change min or max pos last time
  1301. pos = (pos_min + pos_limit)>>1;
  1302. }else{
  1303. // linear search if bisection failed, can only happen if there are very few or no keyframes between min/max
  1304. pos=pos_min;
  1305. }
  1306. if(pos <= pos_min)
  1307. pos= pos_min + 1;
  1308. else if(pos > pos_limit)
  1309. pos= pos_limit;
  1310. start_pos= pos;
  1311. // read the next timestamp
  1312. cur_pts = asf_read_pts(s, &pos, stream_index);
  1313. if(pos == pos_max)
  1314. no_change++;
  1315. else
  1316. no_change=0;
  1317. #ifdef DEBUG_SEEK
  1318. 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);
  1319. #endif
  1320. assert (cur_pts != AV_NOPTS_VALUE);
  1321. if (pts < cur_pts) {
  1322. pos_limit = start_pos - 1;
  1323. pos_max = pos;
  1324. pts_max = cur_pts;
  1325. } else {
  1326. pos_min = pos;
  1327. pts_min = cur_pts;
  1328. /* check if we are lucky */
  1329. if (pts == cur_pts)
  1330. break;
  1331. }
  1332. }
  1333. pos = pos_min;
  1334. url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET);
  1335. asf_reset_header(s);
  1336. return 0;
  1337. }
  1338. static AVInputFormat asf_iformat = {
  1339. "asf",
  1340. "asf format",
  1341. sizeof(ASFContext),
  1342. asf_probe,
  1343. asf_read_header,
  1344. asf_read_packet,
  1345. asf_read_close,
  1346. asf_read_seek,
  1347. };
  1348. #ifdef CONFIG_ENCODERS
  1349. static AVOutputFormat asf_oformat = {
  1350. "asf",
  1351. "asf format",
  1352. "video/x-ms-asf",
  1353. "asf,wmv",
  1354. sizeof(ASFContext),
  1355. #ifdef CONFIG_MP3LAME
  1356. CODEC_ID_MP3,
  1357. #else
  1358. CODEC_ID_MP2,
  1359. #endif
  1360. CODEC_ID_MSMPEG4V3,
  1361. asf_write_header,
  1362. asf_write_packet,
  1363. asf_write_trailer,
  1364. };
  1365. static AVOutputFormat asf_stream_oformat = {
  1366. "asf_stream",
  1367. "asf format",
  1368. "video/x-ms-asf",
  1369. "asf,wmv",
  1370. sizeof(ASFContext),
  1371. #ifdef CONFIG_MP3LAME
  1372. CODEC_ID_MP3,
  1373. #else
  1374. CODEC_ID_MP2,
  1375. #endif
  1376. CODEC_ID_MSMPEG4V3,
  1377. asf_write_stream_header,
  1378. asf_write_packet,
  1379. asf_write_trailer,
  1380. };
  1381. #endif //CONFIG_ENCODERS
  1382. int asf_init(void)
  1383. {
  1384. av_register_input_format(&asf_iformat);
  1385. #ifdef CONFIG_ENCODERS
  1386. av_register_output_format(&asf_oformat);
  1387. av_register_output_format(&asf_stream_oformat);
  1388. #endif //CONFIG_ENCODERS
  1389. return 0;
  1390. }