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.

1257 lines
37KB

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