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.

1295 lines
38KB

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