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.

1361 lines
41KB

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