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.

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