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.

1325 lines
39KB

  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. /* 1 fps default (XXX: put 0 fps instead) */
  734. st->codec.frame_rate = 1;
  735. st->codec.frame_rate_base = 1;
  736. if (type == CODEC_TYPE_AUDIO) {
  737. get_wav_header(pb, &st->codec, type_specific_size);
  738. st->need_parsing = 1;
  739. /* We have to init the frame size at some point .... */
  740. pos2 = url_ftell(pb);
  741. if (gsize > (pos2 + 8 - pos1 + 24)) {
  742. asf_st->ds_span = get_byte(pb);
  743. asf_st->ds_packet_size = get_le16(pb);
  744. asf_st->ds_chunk_size = get_le16(pb);
  745. asf_st->ds_data_size = get_le16(pb);
  746. asf_st->ds_silence_data = get_byte(pb);
  747. }
  748. //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
  749. // asf_st->ds_packet_size, asf_st->ds_chunk_size,
  750. // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
  751. if (asf_st->ds_span > 1) {
  752. if (!asf_st->ds_chunk_size
  753. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
  754. asf_st->ds_span = 0; // disable descrambling
  755. }
  756. switch (st->codec.codec_id) {
  757. case CODEC_ID_MP3:
  758. st->codec.frame_size = MPA_FRAME_SIZE;
  759. break;
  760. case CODEC_ID_PCM_S16LE:
  761. case CODEC_ID_PCM_S16BE:
  762. case CODEC_ID_PCM_U16LE:
  763. case CODEC_ID_PCM_U16BE:
  764. case CODEC_ID_PCM_S8:
  765. case CODEC_ID_PCM_U8:
  766. case CODEC_ID_PCM_ALAW:
  767. case CODEC_ID_PCM_MULAW:
  768. st->codec.frame_size = 1;
  769. break;
  770. default:
  771. /* This is probably wrong, but it prevents a crash later */
  772. st->codec.frame_size = 1;
  773. break;
  774. }
  775. } else {
  776. get_le32(pb);
  777. get_le32(pb);
  778. get_byte(pb);
  779. size = get_le16(pb); /* size */
  780. get_le32(pb); /* size */
  781. st->codec.width = get_le32(pb);
  782. st->codec.height = get_le32(pb);
  783. /* not available for asf */
  784. get_le16(pb); /* panes */
  785. st->codec.bits_per_sample = get_le16(pb); /* depth */
  786. tag1 = get_le32(pb);
  787. url_fskip(pb, 20);
  788. if (size > 40) {
  789. st->codec.extradata_size = size - 40;
  790. st->codec.extradata = av_mallocz(st->codec.extradata_size);
  791. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  792. }
  793. /* Extract palette from extradata if bpp <= 8 */
  794. /* This code assumes that extradata contains only palette */
  795. /* This is true for all paletted codecs implemented in ffmpeg */
  796. if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
  797. st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
  798. #ifdef WORDS_BIGENDIAN
  799. for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
  800. st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
  801. #else
  802. memcpy(st->codec.palctrl->palette, st->codec.extradata,
  803. FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
  804. #endif
  805. st->codec.palctrl->palette_changed = 1;
  806. }
  807. st->codec.codec_tag = tag1;
  808. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  809. }
  810. pos2 = url_ftell(pb);
  811. url_fskip(pb, gsize - (pos2 - pos1 + 24));
  812. } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
  813. break;
  814. } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
  815. int len1, len2, len3, len4, len5;
  816. len1 = get_le16(pb);
  817. len2 = get_le16(pb);
  818. len3 = get_le16(pb);
  819. len4 = get_le16(pb);
  820. len5 = get_le16(pb);
  821. get_str16_nolen(pb, len1, s->title, sizeof(s->title));
  822. get_str16_nolen(pb, len2, s->author, sizeof(s->author));
  823. get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
  824. get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
  825. url_fskip(pb, len5);
  826. #if 0
  827. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  828. int v1, v2;
  829. get_guid(pb, &g);
  830. v1 = get_le32(pb);
  831. v2 = get_le16(pb);
  832. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  833. int len, v1, n, num;
  834. char str[256], *q;
  835. char tag[16];
  836. get_guid(pb, &g);
  837. print_guid(&g);
  838. n = get_le32(pb);
  839. for(i=0;i<n;i++) {
  840. num = get_le16(pb); /* stream number */
  841. get_str16(pb, str, sizeof(str));
  842. get_str16(pb, str, sizeof(str));
  843. len = get_le16(pb);
  844. q = tag;
  845. while (len > 0) {
  846. v1 = get_byte(pb);
  847. if ((q - tag) < sizeof(tag) - 1)
  848. *q++ = v1;
  849. len--;
  850. }
  851. *q = '\0';
  852. }
  853. #endif
  854. } else if (url_feof(pb)) {
  855. goto fail;
  856. } else {
  857. url_fseek(pb, gsize - 24, SEEK_CUR);
  858. }
  859. }
  860. get_guid(pb, &g);
  861. get_le64(pb);
  862. get_byte(pb);
  863. get_byte(pb);
  864. if (url_feof(pb))
  865. goto fail;
  866. asf->data_offset = url_ftell(pb);
  867. asf->packet_size_left = 0;
  868. return 0;
  869. fail:
  870. for(i=0;i<s->nb_streams;i++) {
  871. AVStream *st = s->streams[i];
  872. if (st) {
  873. av_free(st->priv_data);
  874. av_free(st->codec.extradata);
  875. }
  876. av_free(st);
  877. }
  878. return -1;
  879. }
  880. #define DO_2BITS(bits, var, defval) \
  881. switch (bits & 3) \
  882. { \
  883. case 3: var = get_le32(pb); rsize += 4; break; \
  884. case 2: var = get_le16(pb); rsize += 2; break; \
  885. case 1: var = get_byte(pb); rsize++; break; \
  886. default: var = defval; break; \
  887. }
  888. static int asf_get_packet(AVFormatContext *s)
  889. {
  890. ASFContext *asf = s->priv_data;
  891. ByteIOContext *pb = &s->pb;
  892. uint32_t packet_length, padsize;
  893. int rsize = 11;
  894. int c = get_byte(pb);
  895. if (c != 0x82) {
  896. if (!url_feof(pb))
  897. printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
  898. return -EIO;
  899. }
  900. if ((c & 0x0f) == 2) { // always true for now
  901. if (get_le16(pb) != 0) {
  902. if (!url_feof(pb))
  903. printf("ff asf bad non zero\n");
  904. return -EIO;
  905. }
  906. }
  907. asf->packet_flags = get_byte(pb);
  908. asf->packet_property = get_byte(pb);
  909. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  910. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  911. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  912. asf->packet_timestamp = get_le32(pb);
  913. get_le16(pb); /* duration */
  914. // rsize has at least 11 bytes which have to be present
  915. if (asf->packet_flags & 0x01) {
  916. asf->packet_segsizetype = get_byte(pb); rsize++;
  917. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  918. } else {
  919. asf->packet_segments = 1;
  920. asf->packet_segsizetype = 0x80;
  921. }
  922. asf->packet_size_left = packet_length - padsize - rsize;
  923. if (packet_length < asf->hdr.min_pktsize)
  924. padsize += asf->hdr.min_pktsize - packet_length;
  925. asf->packet_padsize = padsize;
  926. #ifdef DEBUG
  927. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  928. #endif
  929. return 0;
  930. }
  931. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  932. {
  933. ASFContext *asf = s->priv_data;
  934. ASFStream *asf_st = 0;
  935. ByteIOContext *pb = &s->pb;
  936. //static int pc = 0;
  937. for (;;) {
  938. int rsize = 0;
  939. if (asf->packet_size_left < FRAME_HEADER_SIZE
  940. || asf->packet_segments < 1) {
  941. //asf->packet_size_left <= asf->packet_padsize) {
  942. int ret = asf->packet_size_left + asf->packet_padsize;
  943. //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  944. /* fail safe */
  945. url_fskip(pb, ret);
  946. ret = asf_get_packet(s);
  947. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  948. if (ret < 0 || url_feof(pb))
  949. return -EIO;
  950. asf->packet_time_start = 0;
  951. continue;
  952. }
  953. if (asf->packet_time_start == 0) {
  954. /* read frame header */
  955. int num = get_byte(pb);
  956. asf->packet_segments--;
  957. rsize++;
  958. asf->packet_key_frame = (num & 0x80) >> 7;
  959. asf->stream_index = asf->asfid2avid[num & 0x7f];
  960. // sequence should be ignored!
  961. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  962. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  963. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  964. if (asf->packet_replic_size > 1) {
  965. // it should be always at least 8 bytes - FIXME validate
  966. asf->packet_obj_size = get_le32(pb);
  967. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  968. if (asf->packet_replic_size > 8)
  969. url_fskip(pb, asf->packet_replic_size - 8);
  970. rsize += asf->packet_replic_size; // FIXME - check validity
  971. } else {
  972. // multipacket - frag_offset is begining timestamp
  973. asf->packet_time_start = asf->packet_frag_offset;
  974. asf->packet_frag_offset = 0;
  975. asf->packet_frag_timestamp = asf->packet_timestamp;
  976. if (asf->packet_replic_size == 1) {
  977. asf->packet_time_delta = get_byte(pb);
  978. rsize++;
  979. }
  980. }
  981. if (asf->packet_flags & 0x01) {
  982. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  983. #undef DO_2BITS
  984. //printf("Fragsize %d\n", asf->packet_frag_size);
  985. } else {
  986. asf->packet_frag_size = asf->packet_size_left - rsize;
  987. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  988. }
  989. if (asf->packet_replic_size == 1) {
  990. asf->packet_multi_size = asf->packet_frag_size;
  991. if (asf->packet_multi_size > asf->packet_size_left) {
  992. asf->packet_segments = 0;
  993. continue;
  994. }
  995. }
  996. asf->packet_size_left -= rsize;
  997. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  998. if (asf->stream_index < 0) {
  999. asf->packet_time_start = 0;
  1000. /* unhandled packet (should not happen) */
  1001. url_fskip(pb, asf->packet_frag_size);
  1002. asf->packet_size_left -= asf->packet_frag_size;
  1003. printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
  1004. continue;
  1005. }
  1006. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  1007. }
  1008. asf_st = asf->asf_st;
  1009. if ((asf->packet_frag_offset != asf_st->frag_offset
  1010. || (asf->packet_frag_offset
  1011. && asf->packet_seq != asf_st->seq)) // seq should be ignored
  1012. ) {
  1013. /* cannot continue current packet: free it */
  1014. // FIXME better check if packet was already allocated
  1015. printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
  1016. asf_st->pkt.size,
  1017. asf->packet_obj_size,
  1018. asf->packet_frag_offset, asf_st->frag_offset,
  1019. asf->packet_seq, asf_st->seq, asf->packet_frag_size);
  1020. if (asf_st->pkt.size)
  1021. av_free_packet(&asf_st->pkt);
  1022. asf_st->frag_offset = 0;
  1023. if (asf->packet_frag_offset != 0) {
  1024. url_fskip(pb, asf->packet_frag_size);
  1025. printf("ff asf parser skiping %db\n", asf->packet_frag_size);
  1026. asf->packet_size_left -= asf->packet_frag_size;
  1027. continue;
  1028. }
  1029. }
  1030. if (asf->packet_replic_size == 1) {
  1031. // frag_offset is here used as the begining timestamp
  1032. asf->packet_frag_timestamp = asf->packet_time_start;
  1033. asf->packet_time_start += asf->packet_time_delta;
  1034. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  1035. asf->packet_size_left--;
  1036. asf->packet_multi_size--;
  1037. if (asf->packet_multi_size < asf->packet_obj_size)
  1038. {
  1039. asf->packet_time_start = 0;
  1040. url_fskip(pb, asf->packet_multi_size);
  1041. asf->packet_size_left -= asf->packet_multi_size;
  1042. continue;
  1043. }
  1044. asf->packet_multi_size -= asf->packet_obj_size;
  1045. //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);
  1046. }
  1047. if (asf_st->frag_offset == 0) {
  1048. /* new packet */
  1049. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  1050. asf_st->seq = asf->packet_seq;
  1051. asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
  1052. asf_st->pkt.stream_index = asf->stream_index;
  1053. if (asf->packet_key_frame)
  1054. asf_st->pkt.flags |= PKT_FLAG_KEY;
  1055. }
  1056. /* read data */
  1057. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  1058. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  1059. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  1060. asf->packet_size_left -= asf->packet_frag_size;
  1061. if (asf->packet_size_left < 0)
  1062. continue;
  1063. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  1064. asf->packet_frag_size);
  1065. asf_st->frag_offset += asf->packet_frag_size;
  1066. /* test if whole packet is read */
  1067. if (asf_st->frag_offset == asf_st->pkt.size) {
  1068. /* return packet */
  1069. if (asf_st->ds_span > 1) {
  1070. /* packet descrambling */
  1071. char* newdata = av_malloc(asf_st->pkt.size);
  1072. if (newdata) {
  1073. int offset = 0;
  1074. while (offset < asf_st->pkt.size) {
  1075. int off = offset / asf_st->ds_chunk_size;
  1076. int row = off / asf_st->ds_span;
  1077. int col = off % asf_st->ds_span;
  1078. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  1079. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  1080. memcpy(newdata + offset,
  1081. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  1082. asf_st->ds_chunk_size);
  1083. offset += asf_st->ds_chunk_size;
  1084. }
  1085. av_free(asf_st->pkt.data);
  1086. asf_st->pkt.data = newdata;
  1087. }
  1088. }
  1089. asf_st->frag_offset = 0;
  1090. memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
  1091. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  1092. asf_st->pkt.size = 0;
  1093. asf_st->pkt.data = 0;
  1094. break; // packet completed
  1095. }
  1096. }
  1097. return 0;
  1098. }
  1099. static int asf_read_close(AVFormatContext *s)
  1100. {
  1101. int i;
  1102. for(i=0;i<s->nb_streams;i++) {
  1103. AVStream *st = s->streams[i];
  1104. av_free(st->priv_data);
  1105. av_free(st->codec.extradata);
  1106. av_free(st->codec.palctrl);
  1107. }
  1108. return 0;
  1109. }
  1110. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts)
  1111. {
  1112. #if 0
  1113. ASFContext *asf = s->priv_data;
  1114. int i;
  1115. for(i = 0;; i++) {
  1116. url_fseek(&s->pb, asf->data_offset + i * asf->packet_size, SEEK_SET);
  1117. if (asf_get_packet(s) < 0)
  1118. break;
  1119. printf("timestamp=%0.3f\n", asf->packet_timestamp / 1000.0);
  1120. }
  1121. #endif
  1122. return -1;
  1123. }
  1124. static AVInputFormat asf_iformat = {
  1125. "asf",
  1126. "asf format",
  1127. sizeof(ASFContext),
  1128. asf_probe,
  1129. asf_read_header,
  1130. asf_read_packet,
  1131. asf_read_close,
  1132. asf_read_seek,
  1133. };
  1134. #ifdef CONFIG_ENCODERS
  1135. static AVOutputFormat asf_oformat = {
  1136. "asf",
  1137. "asf format",
  1138. "video/x-ms-asf",
  1139. "asf,wmv",
  1140. sizeof(ASFContext),
  1141. #ifdef CONFIG_MP3LAME
  1142. CODEC_ID_MP3,
  1143. #else
  1144. CODEC_ID_MP2,
  1145. #endif
  1146. CODEC_ID_MSMPEG4V3,
  1147. asf_write_header,
  1148. asf_write_packet,
  1149. asf_write_trailer,
  1150. };
  1151. static AVOutputFormat asf_stream_oformat = {
  1152. "asf_stream",
  1153. "asf format",
  1154. "video/x-ms-asf",
  1155. "asf,wmv",
  1156. sizeof(ASFContext),
  1157. #ifdef CONFIG_MP3LAME
  1158. CODEC_ID_MP3,
  1159. #else
  1160. CODEC_ID_MP2,
  1161. #endif
  1162. CODEC_ID_MSMPEG4V3,
  1163. asf_write_stream_header,
  1164. asf_write_packet,
  1165. asf_write_trailer,
  1166. };
  1167. #endif //CONFIG_ENCODERS
  1168. int asf_init(void)
  1169. {
  1170. av_register_input_format(&asf_iformat);
  1171. #ifdef CONFIG_ENCODERS
  1172. av_register_output_format(&asf_oformat);
  1173. av_register_output_format(&asf_stream_oformat);
  1174. #endif //CONFIG_ENCODERS
  1175. return 0;
  1176. }