2 * ASF compatible encoder and decoder.
3 * Copyright (c) 2000, 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define PACKET_SIZE 3200
23 #define PACKET_HEADER_SIZE 12
24 #define FRAME_HEADER_SIZE 17
39 /* non streamed additonnal info */
42 INT64 duration; /* in 100ns units */
45 int packet_timestamp_start;
46 int packet_timestamp_end;
48 UINT8 packet_buf[PACKET_SIZE];
50 /* only for reading */
61 static const GUID asf_header = {
62 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
65 static const GUID file_header = {
66 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
69 static const GUID stream_header = {
70 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
73 static const GUID audio_stream = {
74 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
77 static const GUID audio_conceal_none = {
78 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
81 static const GUID video_stream = {
82 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
85 static const GUID video_conceal_none = {
86 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
90 static const GUID comment_header = {
91 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
94 static const GUID codec_comment_header = {
95 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
97 static const GUID codec_comment1_header = {
98 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
101 static const GUID data_header = {
102 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
105 static const GUID index_guid = {
106 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
109 static const GUID head1_guid = {
110 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
113 static const GUID head2_guid = {
114 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
117 /* I am not a number !!! This GUID is the one found on the PC used to
118 generate the stream */
119 static const GUID my_guid = {
120 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
123 static void put_guid(ByteIOContext *s, const GUID *g)
131 put_byte(s, g->v4[i]);
134 static void put_str16(ByteIOContext *s, const char *tag)
138 put_le16(s,strlen(tag) + 1);
147 static void put_str16_nolen(ByteIOContext *s, const char *tag)
159 static INT64 put_header(ByteIOContext *pb, const GUID *g)
169 /* update header size */
170 static void end_header(ByteIOContext *pb, INT64 pos)
174 pos1 = url_ftell(pb);
175 url_fseek(pb, pos + 16, SEEK_SET);
176 put_le64(pb, pos1 - pos);
177 url_fseek(pb, pos1, SEEK_SET);
180 /* write an asf chunk (only used in streaming case) */
181 static void put_chunk(AVFormatContext *s, int type, int payload_length)
183 ASFContext *asf = s->priv_data;
184 ByteIOContext *pb = &s->pb;
187 length = payload_length + 8;
189 put_le16(pb, length);
190 put_le32(pb, asf->seqno);
191 put_le16(pb, 0); /* unknown bytes */
192 put_le16(pb, length);
196 /* convert from unix to windows time */
197 static INT64 unix_to_file_time(int ti)
201 t = ti * INT64_C(10000000);
202 t += INT64_C(116444736000000000);
206 /* write the header (used two times if non streamed) */
207 static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chunk_size)
209 ASFContext *asf = s->priv_data;
210 ByteIOContext *pb = &s->pb;
211 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
214 INT64 header_offset, cur_pos, hpos;
216 has_title = (s->title[0] != '\0');
218 if (!url_is_streamed(&s->pb)) {
219 put_guid(pb, &asf_header);
220 put_le64(pb, 0); /* header length, will be patched after */
221 put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
222 put_byte(pb, 1); /* ??? */
223 put_byte(pb, 2); /* ??? */
225 put_chunk(s, 0x4824, 0); /* start of stream (length will be patched later) */
229 header_offset = url_ftell(pb);
230 hpos = put_header(pb, &file_header);
231 put_guid(pb, &my_guid);
232 put_le64(pb, file_size);
234 put_le64(pb, unix_to_file_time(file_time));
235 put_le64(pb, asf->nb_packets); /* number of packets */
236 put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
237 put_le64(pb, asf->duration); /* duration (in 100ns units) */
238 put_le32(pb, 0); /* start time stamp */
239 put_le32(pb, 0); /* ??? */
240 put_le32(pb, 0); /* ??? */
241 put_le32(pb, asf->packet_size); /* packet size */
242 put_le32(pb, asf->packet_size); /* packet size */
243 put_le32(pb, 80 * asf->packet_size); /* frame_size ??? */
244 end_header(pb, hpos);
246 /* unknown headers */
247 hpos = put_header(pb, &head1_guid);
248 put_guid(pb, &head2_guid);
251 end_header(pb, hpos);
253 /* title and other infos */
255 hpos = put_header(pb, &comment_header);
256 put_le16(pb, 2 * (strlen(s->title) + 1));
257 put_le16(pb, 2 * (strlen(s->author) + 1));
258 put_le16(pb, 2 * (strlen(s->copyright) + 1));
259 put_le16(pb, 2 * (strlen(s->comment) + 1));
261 put_str16_nolen(pb, s->title);
262 put_str16_nolen(pb, s->author);
263 put_str16_nolen(pb, s->copyright);
264 put_str16_nolen(pb, s->comment);
265 end_header(pb, hpos);
269 for(n=0;n<s->nb_streams;n++) {
270 enc = &s->streams[n]->codec;
271 asf->streams[n].num = n + 1;
272 asf->streams[n].seq = 0;
274 switch(enc->codec_type) {
275 case CODEC_TYPE_AUDIO:
277 extra_size = 18 + wav_extra_size;
281 case CODEC_TYPE_VIDEO:
288 hpos = put_header(pb, &stream_header);
289 if (enc->codec_type == CODEC_TYPE_AUDIO) {
290 put_guid(pb, &audio_stream);
291 put_guid(pb, &audio_conceal_none);
293 put_guid(pb, &video_stream);
294 put_guid(pb, &video_conceal_none);
296 put_le64(pb, 0); /* ??? */
297 put_le32(pb, extra_size); /* wav header len */
298 put_le32(pb, extra_size2); /* additional data len */
299 put_le16(pb, n + 1); /* stream number */
300 put_le32(pb, 0); /* ??? */
302 if (enc->codec_type == CODEC_TYPE_AUDIO) {
303 /* WAVEFORMATEX header */
304 if (put_wav_header(pb, enc) < 0)
307 put_le32(pb, enc->width);
308 put_le32(pb, enc->height);
309 put_byte(pb, 2); /* ??? */
310 put_le16(pb, 40); /* size */
312 /* BITMAPINFOHEADER header */
313 put_bmp_header(pb, enc);
315 end_header(pb, hpos);
320 hpos = put_header(pb, &codec_comment_header);
321 put_guid(pb, &codec_comment1_header);
322 put_le32(pb, s->nb_streams);
323 for(n=0;n<s->nb_streams;n++) {
324 enc = &s->streams[n]->codec;
326 put_le16(pb, asf->streams[n].num);
327 put_str16(pb, enc->codec_name);
328 put_le16(pb, 0); /* no parameters */
330 if (enc->codec_type == CODEC_TYPE_AUDIO) {
332 put_le16(pb, codec_get_tag(codec_wav_tags, enc->codec_id));
335 put_le32(pb, codec_get_tag(codec_bmp_tags, enc->codec_id));
338 end_header(pb, hpos);
340 /* patch the header size fields */
342 cur_pos = url_ftell(pb);
343 header_size = cur_pos - header_offset;
344 if (!url_is_streamed(&s->pb)) {
345 header_size += 24 + 6;
346 url_fseek(pb, header_offset - 14, SEEK_SET);
347 put_le64(pb, header_size);
349 header_size += 8 + 50;
350 url_fseek(pb, header_offset - 10, SEEK_SET);
351 put_le16(pb, header_size);
352 url_fseek(pb, header_offset - 2, SEEK_SET);
353 put_le16(pb, header_size);
355 url_fseek(pb, cur_pos, SEEK_SET);
357 /* movie chunk, followed by packets of packet_size */
358 asf->data_offset = cur_pos;
359 put_guid(pb, &data_header);
360 put_le64(pb, data_chunk_size);
361 put_guid(pb, &my_guid);
362 put_le64(pb, asf->nb_packets); /* nb packets */
363 put_byte(pb, 1); /* ??? */
364 put_byte(pb, 1); /* ??? */
368 static int asf_write_header(AVFormatContext *s)
372 asf = av_mallocz(sizeof(ASFContext));
377 asf->packet_size = PACKET_SIZE;
380 if (asf_write_header1(s, 0, 24) < 0) {
385 put_flush_packet(&s->pb);
387 asf->packet_nb_frames = 0;
388 asf->packet_timestamp_start = -1;
389 asf->packet_timestamp_end = -1;
390 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
391 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
392 NULL, NULL, NULL, NULL);
397 /* write a fixed size packet */
398 static void put_packet(AVFormatContext *s,
399 unsigned int timestamp, unsigned int duration,
400 int nb_frames, int padsize)
402 ASFContext *asf = s->priv_data;
403 ByteIOContext *pb = &s->pb;
406 if (url_is_streamed(&s->pb)) {
407 put_chunk(s, 0x4424, asf->packet_size);
413 flags = 0x01; /* nb segments present */
420 put_byte(pb, flags); /* flags */
423 put_le16(pb, padsize);
425 put_byte(pb, padsize);
426 put_le32(pb, timestamp);
427 put_le16(pb, duration);
428 put_byte(pb, nb_frames | 0x80);
431 static void flush_packet(AVFormatContext *s)
433 ASFContext *asf = s->priv_data;
436 put_packet(s, asf->packet_timestamp_start,
437 asf->packet_timestamp_end - asf->packet_timestamp_start,
438 asf->packet_nb_frames, asf->packet_size_left);
440 /* compute padding */
441 hdr_size = PACKET_HEADER_SIZE;
442 if (asf->packet_size_left > 0) {
443 /* if padding needed, don't forget to count the
444 padding byte in the header size */
446 asf->packet_size_left--;
447 /* XXX: I do not test again exact limit to avoid boundary problems */
448 if (asf->packet_size_left > 200) {
450 asf->packet_size_left--;
453 ptr = asf->packet_size - PACKET_HEADER_SIZE - asf->packet_size_left;
454 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
456 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
458 put_flush_packet(&s->pb);
460 asf->packet_nb_frames = 0;
461 asf->packet_timestamp_start = -1;
462 asf->packet_timestamp_end = -1;
463 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
464 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
465 NULL, NULL, NULL, NULL);
468 static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
469 int payload_size, int frag_offset, int frag_len)
471 ASFContext *asf = s->priv_data;
472 ByteIOContext *pb = &asf->pb;
476 if (s->streams[val - 1]->codec.key_frame)
479 put_byte(pb, stream->seq);
480 put_le32(pb, frag_offset); /* fragment offset */
481 put_byte(pb, 0x08); /* flags */
482 put_le32(pb, payload_size);
483 put_le32(pb, timestamp);
484 put_le16(pb, frag_len);
488 /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
490 It is there that you understand that the ASF format is really
491 crap. They have misread the MPEG Systems spec !
493 static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
494 UINT8 *buf, int payload_size)
496 ASFContext *asf = s->priv_data;
497 int frag_pos, frag_len, frag_len1;
500 while (frag_pos < payload_size) {
501 frag_len = payload_size - frag_pos;
502 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
504 if (frag_len > frag_len1)
505 frag_len = frag_len1;
506 put_frame_header(s, stream, timestamp, payload_size, frag_pos, frag_len);
507 put_buffer(&asf->pb, buf, frag_len);
508 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
509 asf->packet_timestamp_end = timestamp;
510 if (asf->packet_timestamp_start == -1)
511 asf->packet_timestamp_start = timestamp;
512 asf->packet_nb_frames++;
516 frag_pos += frag_len;
518 /* output the frame if filled */
519 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
526 static int asf_write_packet(AVFormatContext *s, int stream_index,
527 UINT8 *buf, int size)
529 ASFContext *asf = s->priv_data;
532 AVCodecContext *codec;
534 codec = &s->streams[stream_index]->codec;
535 if (codec->codec_type == CODEC_TYPE_AUDIO) {
536 timestamp = (int)((float)codec->frame_number * codec->frame_size * 1000.0 /
538 duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
541 timestamp = (int)((float)codec->frame_number * 1000.0 * FRAME_RATE_BASE /
543 duration = codec->frame_number *
544 ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
546 if (duration > asf->duration)
547 asf->duration = duration;
549 put_frame(s, &asf->streams[stream_index], (int)timestamp, buf, size);
553 static int asf_write_trailer(AVFormatContext *s)
555 ASFContext *asf = s->priv_data;
558 /* flush the current packet */
559 if (asf->pb.buf_ptr > asf->pb.buffer)
562 if (url_is_streamed(&s->pb)) {
563 put_chunk(s, 0x4524, 0); /* end of stream */
565 /* rewrite an updated header */
566 file_size = url_ftell(&s->pb);
567 url_fseek(&s->pb, 0, SEEK_SET);
568 asf_write_header1(s, file_size, file_size - asf->data_offset);
571 put_flush_packet(&s->pb);
577 /**********************************/
583 static void print_guid(const GUID *g)
586 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
588 printf(" 0x%02x,", g->v4[i]);
593 static void get_guid(ByteIOContext *s, GUID *g)
601 g->v4[i] = get_byte(s);
605 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
614 if ((q - buf) < buf_size - 1)
622 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
630 if ((q - buf) < buf_size - 1)
637 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
641 ByteIOContext *pb = &s->pb;
647 asf = av_mallocz(sizeof(ASFContext));
653 if (memcmp(&g, &asf_header, sizeof(GUID)))
662 gsize = get_le64(pb);
664 printf("%08Lx: ", url_ftell(pb) - 24);
666 printf(" size=0x%Lx\n", gsize);
670 if (!memcmp(&g, &file_header, sizeof(GUID))) {
672 get_le64(pb); /* file size */
673 get_le64(pb); /* file time */
674 get_le64(pb); /* nb_packets */
675 get_le64(pb); /* length 0 in us */
676 get_le64(pb); /* length 1 in us */
680 asf->packet_size = get_le32(pb);
683 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
684 int type, id, total_size;
688 pos1 = url_ftell(pb);
690 st = av_mallocz(sizeof(AVStream));
693 s->streams[s->nb_streams++] = st;
694 asf_st = av_mallocz(sizeof(ASFStream));
697 st->priv_data = asf_st;
700 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
701 type = CODEC_TYPE_AUDIO;
702 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
703 type = CODEC_TYPE_VIDEO;
708 total_size = get_le64(pb);
711 st->id = get_le16(pb); /* stream id */
713 st->codec.codec_type = type;
714 if (type == CODEC_TYPE_AUDIO) {
716 st->codec.codec_tag = id;
717 st->codec.channels = get_le16(pb);
718 st->codec.sample_rate = get_le32(pb);
719 st->codec.bit_rate = get_le32(pb) * 8;
720 get_le16(pb); /* block align */
721 bps = get_le16(pb); /* bits per sample */
722 st->codec.codec_id = wav_codec_get_id(id, bps);
729 size = get_le16(pb); /* size */
730 get_le32(pb); /* size */
731 st->codec.width = get_le32(pb);
732 st->codec.height = get_le32(pb);
733 st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
734 get_le16(pb); /* panes */
735 get_le16(pb); /* depth */
737 st->codec.codec_tag = tag1;
738 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
739 url_fskip(pb, size - 5 * 4);
741 pos2 = url_ftell(pb);
742 url_fskip(pb, gsize - (pos2 - pos1 + 24));
743 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
745 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
746 int len1, len2, len3, len4, len5;
753 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
754 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
755 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
756 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
759 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
764 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
774 num = get_le16(pb); /* stream number */
775 get_str16(pb, str, sizeof(str));
776 get_str16(pb, str, sizeof(str));
781 if ((q - tag) < sizeof(tag) - 1)
788 } else if (url_feof(pb)) {
791 url_fseek(pb, gsize - 24, SEEK_CUR);
799 asf->packet_size_left = 0;
804 for(i=0;i<s->nb_streams;i++) {
805 AVStream *st = s->streams[i];
814 static int asf_get_packet(AVFormatContext *s)
816 ASFContext *asf = s->priv_data;
817 ByteIOContext *pb = &s->pb;
818 int c, flags, timestamp, hdr_size;
825 flags = get_byte(pb);
827 asf->packet_padsize = 0;
829 asf->packet_padsize = get_le16(pb);
831 } else if (flags & 0x08) {
832 asf->packet_padsize = get_byte(pb);
835 timestamp = get_le32(pb);
836 get_le16(pb); /* duration */
837 get_byte(pb); /* nb_frames */
839 printf("packet: size=%d padsize=%d\n", asf->packet_size, asf->packet_padsize);
841 asf->packet_size_left = asf->packet_size - hdr_size;
845 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
847 ASFContext *asf = s->priv_data;
850 ByteIOContext *pb = &s->pb;
851 int ret, num, seq, frag_offset, payload_size, frag_len;
855 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
856 asf->packet_size_left <= asf->packet_padsize) {
858 if (asf->packet_size_left)
859 url_fskip(pb, asf->packet_size_left);
860 ret = asf_get_packet(s);
864 /* read frame header */
865 num = get_byte(pb) & 0x7f;
867 frag_offset = get_le32(pb);
868 get_byte(pb); /* flags */
869 payload_size = get_le32(pb);
870 timestamp = get_le32(pb);
871 frag_len = get_le16(pb);
873 printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
874 num, seq, payload_size, frag_offset, frag_len);
877 for(i=0;i<s->nb_streams;i++) {
882 asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len;
883 if (i == s->nb_streams) {
884 /* unhandled packet (should not happen) */
885 url_fskip(pb, frag_len);
887 asf_st = st->priv_data;
888 if (asf_st->frag_offset == 0) {
890 av_new_packet(&asf_st->pkt, payload_size);
893 if (seq == asf_st->seq &&
894 frag_offset == asf_st->frag_offset) {
895 /* continuing packet */
897 /* cannot continue current packet: free it */
898 av_free_packet(&asf_st->pkt);
899 asf_st->frag_offset = 0;
900 if (frag_offset != 0) {
901 /* cannot create new packet */
902 url_fskip(pb, frag_len);
905 /* create new packet */
906 av_new_packet(&asf_st->pkt, payload_size);
912 get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len);
913 asf_st->frag_offset += frag_len;
914 /* test if whole packet read */
915 if (asf_st->frag_offset == asf_st->pkt.size) {
917 asf_st->pkt.stream_index = i;
918 asf_st->frag_offset = 0;
919 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
929 static int asf_read_close(AVFormatContext *s)
931 ASFContext *asf = s->priv_data;
934 for(i=0;i<s->nb_streams;i++) {
935 AVStream *st = s->streams[i];
942 AVFormat asf_format = {
945 "application/octet-stream",