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.

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