2 * ASF compatible encoder and decoder.
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
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.
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
21 #include "mpegaudio.h"
23 #define PACKET_SIZE 3200
24 #define PACKET_HEADER_SIZE 12
25 #define FRAME_HEADER_SIZE 17
36 int ds_span; /* descrambling */
51 typedef struct __attribute__((packed)) {
52 GUID guid; // generated by client computer
53 uint64_t file_size; // in bytes
54 // invalid if broadcasting
55 uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601
56 // invalid if broadcasting
57 uint64_t packets_count; // how many packets are there in the file
58 // invalid if broadcasting
59 uint64_t play_time; // play time, in 100-nanosecond units
60 // invalid if broadcasting
61 uint64_t send_time; // time to send file, in 100-nanosecond units
62 // invalid if broadcasting (could be ignored)
63 uint32_t preroll; // timestamp of the first packet, in milliseconds
64 // if nonzero - substract from time
65 uint32_t ignore; // preroll is 64bit - but let's just ignore it
66 uint32_t flags; // 0x01 - broadcast
68 // rest is reserved should be 0
69 uint32_t min_pktsize; // size of a data packet
70 // invalid if broadcasting
71 uint32_t max_pktsize; // shall be the same as for min_pktsize
72 // invalid if broadcasting
73 uint32_t max_bitrate; // bandwith of stream in bps
74 // should be the sum of bitrates of the
75 // individual media streams
83 int asfid2avid[128]; /* conversion table from asf ID 2 AVStream ID */
84 ASFStream streams[128]; /* it's max number and it's not that big */
85 /* non streamed additonnal info */
87 INT64 duration; /* in 100ns units */
90 int packet_timestamp_start;
91 int packet_timestamp_end;
93 UINT8 packet_buf[PACKET_SIZE];
95 /* only for reading */
96 uint64_t data_offset; /* begining of the first data packet */
102 int packet_timestamp;
103 int packet_segsizetype;
106 int packet_replic_size;
107 int packet_key_frame;
109 int packet_frag_offset;
110 int packet_frag_size;
111 int packet_frag_timestamp;
112 int packet_multi_size;
114 int packet_time_delta;
115 int packet_time_start;
118 ASFStream* asf_st; /* currently decoded stream */
121 static const GUID asf_header = {
122 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
125 static const GUID file_header = {
126 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
129 static const GUID stream_header = {
130 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
133 static const GUID audio_stream = {
134 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
137 static const GUID audio_conceal_none = {
138 // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
139 // New value lifted from avifile
140 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b },
143 static const GUID video_stream = {
144 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
147 static const GUID video_conceal_none = {
148 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
152 static const GUID comment_header = {
153 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
156 static const GUID codec_comment_header = {
157 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
159 static const GUID codec_comment1_header = {
160 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
163 static const GUID data_header = {
164 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
167 static const GUID index_guid = {
168 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
171 static const GUID head1_guid = {
172 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
175 static const GUID head2_guid = {
176 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
179 /* I am not a number !!! This GUID is the one found on the PC used to
180 generate the stream */
181 static const GUID my_guid = {
182 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
185 static void put_guid(ByteIOContext *s, const GUID *g)
193 put_byte(s, g->v4[i]);
196 static void put_str16(ByteIOContext *s, const char *tag)
200 put_le16(s,strlen(tag) + 1);
209 static void put_str16_nolen(ByteIOContext *s, const char *tag)
221 static INT64 put_header(ByteIOContext *pb, const GUID *g)
231 /* update header size */
232 static void end_header(ByteIOContext *pb, INT64 pos)
236 pos1 = url_ftell(pb);
237 url_fseek(pb, pos + 16, SEEK_SET);
238 put_le64(pb, pos1 - pos);
239 url_fseek(pb, pos1, SEEK_SET);
242 /* write an asf chunk (only used in streaming case) */
243 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
245 ASFContext *asf = s->priv_data;
246 ByteIOContext *pb = &s->pb;
249 length = payload_length + 8;
251 put_le16(pb, length);
252 put_le32(pb, asf->seqno);
253 put_le16(pb, flags); /* unknown bytes */
254 put_le16(pb, length);
258 /* convert from unix to windows time */
259 static INT64 unix_to_file_time(int ti)
263 t = ti * INT64_C(10000000);
264 t += INT64_C(116444736000000000);
268 /* write the header (used two times if non streamed) */
269 static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chunk_size)
271 ASFContext *asf = s->priv_data;
272 ByteIOContext *pb = &s->pb;
273 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
276 INT64 header_offset, cur_pos, hpos;
279 has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
282 for(n=0;n<s->nb_streams;n++) {
283 enc = &s->streams[n]->codec;
285 bit_rate += enc->bit_rate;
288 if (asf->is_streamed) {
289 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
292 put_guid(pb, &asf_header);
293 put_le64(pb, -1); /* header length, will be patched after */
294 put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
295 put_byte(pb, 1); /* ??? */
296 put_byte(pb, 2); /* ??? */
299 header_offset = url_ftell(pb);
300 hpos = put_header(pb, &file_header);
301 put_guid(pb, &my_guid);
302 put_le64(pb, file_size);
304 put_le64(pb, unix_to_file_time(file_time));
305 put_le64(pb, asf->nb_packets); /* number of packets */
306 put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
307 put_le64(pb, asf->duration); /* duration (in 100ns units) */
308 put_le32(pb, 0); /* start time stamp */
309 put_le32(pb, 0); /* ??? */
310 put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
311 put_le32(pb, asf->packet_size); /* packet size */
312 put_le32(pb, asf->packet_size); /* packet size */
313 put_le32(pb, bit_rate); /* Nominal data rate in bps */
314 end_header(pb, hpos);
316 /* unknown headers */
317 hpos = put_header(pb, &head1_guid);
318 put_guid(pb, &head2_guid);
321 end_header(pb, hpos);
323 /* title and other infos */
325 hpos = put_header(pb, &comment_header);
326 put_le16(pb, 2 * (strlen(s->title) + 1));
327 put_le16(pb, 2 * (strlen(s->author) + 1));
328 put_le16(pb, 2 * (strlen(s->copyright) + 1));
329 put_le16(pb, 2 * (strlen(s->comment) + 1));
331 put_str16_nolen(pb, s->title);
332 put_str16_nolen(pb, s->author);
333 put_str16_nolen(pb, s->copyright);
334 put_str16_nolen(pb, s->comment);
335 end_header(pb, hpos);
339 for(n=0;n<s->nb_streams;n++) {
341 // ASFStream *stream = &asf->streams[n];
343 enc = &s->streams[n]->codec;
344 asf->streams[n].num = n + 1;
345 asf->streams[n].seq = 0;
347 switch(enc->codec_type) {
348 case CODEC_TYPE_AUDIO:
350 extra_size = 18 + wav_extra_size;
354 case CODEC_TYPE_VIDEO:
361 hpos = put_header(pb, &stream_header);
362 if (enc->codec_type == CODEC_TYPE_AUDIO) {
363 put_guid(pb, &audio_stream);
364 put_guid(pb, &audio_conceal_none);
366 put_guid(pb, &video_stream);
367 put_guid(pb, &video_conceal_none);
369 put_le64(pb, 0); /* ??? */
370 es_pos = url_ftell(pb);
371 put_le32(pb, extra_size); /* wav header len */
372 put_le32(pb, extra_size2); /* additional data len */
373 put_le16(pb, n + 1); /* stream number */
374 put_le32(pb, 0); /* ??? */
376 if (enc->codec_type == CODEC_TYPE_AUDIO) {
377 /* WAVEFORMATEX header */
378 int wavsize = put_wav_header(pb, enc);
382 if (wavsize != extra_size) {
383 cur_pos = url_ftell(pb);
384 url_fseek(pb, es_pos, SEEK_SET);
385 put_le32(pb, wavsize); /* wav header len */
386 url_fseek(pb, cur_pos, SEEK_SET);
389 put_le32(pb, enc->width);
390 put_le32(pb, enc->height);
391 put_byte(pb, 2); /* ??? */
392 put_le16(pb, 40); /* size */
394 /* BITMAPINFOHEADER header */
395 put_bmp_header(pb, enc, codec_bmp_tags, 1);
397 end_header(pb, hpos);
402 hpos = put_header(pb, &codec_comment_header);
403 put_guid(pb, &codec_comment1_header);
404 put_le32(pb, s->nb_streams);
405 for(n=0;n<s->nb_streams;n++) {
408 enc = &s->streams[n]->codec;
409 p = avcodec_find_encoder(enc->codec_id);
411 put_le16(pb, asf->streams[n].num);
412 put_str16(pb, p ? p->name : enc->codec_name);
413 put_le16(pb, 0); /* no parameters */
415 if (enc->codec_type == CODEC_TYPE_AUDIO) {
417 put_le16(pb, codec_get_tag(codec_wav_tags, enc->codec_id));
420 put_le32(pb, codec_get_tag(codec_bmp_tags, enc->codec_id));
423 end_header(pb, hpos);
425 /* patch the header size fields */
427 cur_pos = url_ftell(pb);
428 header_size = cur_pos - header_offset;
429 if (asf->is_streamed) {
430 header_size += 8 + 30 + 50;
432 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
433 put_le16(pb, header_size);
434 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
435 put_le16(pb, header_size);
437 header_size -= 8 + 30 + 50;
439 header_size += 24 + 6;
440 url_fseek(pb, header_offset - 14, SEEK_SET);
441 put_le64(pb, header_size);
442 url_fseek(pb, cur_pos, SEEK_SET);
444 /* movie chunk, followed by packets of packet_size */
445 asf->data_offset = cur_pos;
446 put_guid(pb, &data_header);
447 put_le64(pb, data_chunk_size);
448 put_guid(pb, &my_guid);
449 put_le64(pb, asf->nb_packets); /* nb packets */
450 put_byte(pb, 1); /* ??? */
451 put_byte(pb, 1); /* ??? */
455 static int asf_write_header(AVFormatContext *s)
457 ASFContext *asf = s->priv_data;
459 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
461 asf->packet_size = PACKET_SIZE;
464 if (asf_write_header1(s, 0, 50) < 0) {
469 put_flush_packet(&s->pb);
471 asf->packet_nb_frames = 0;
472 asf->packet_timestamp_start = -1;
473 asf->packet_timestamp_end = -1;
474 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
475 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
476 NULL, NULL, NULL, NULL);
481 static int asf_write_stream_header(AVFormatContext *s)
483 ASFContext *asf = s->priv_data;
485 asf->is_streamed = 1;
487 return asf_write_header(s);
490 /* write a fixed size packet */
491 static int put_packet(AVFormatContext *s,
492 unsigned int timestamp, unsigned int duration,
493 int nb_frames, int padsize)
495 ASFContext *asf = s->priv_data;
496 ByteIOContext *pb = &s->pb;
499 if (asf->is_streamed) {
500 put_chunk(s, 0x4424, asf->packet_size, 0);
506 flags = 0x01; /* nb segments present */
513 put_byte(pb, flags); /* flags */
516 put_le16(pb, padsize - 2);
518 put_byte(pb, padsize - 1);
519 put_le32(pb, timestamp);
520 put_le16(pb, duration);
521 put_byte(pb, nb_frames | 0x80);
523 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
526 static void flush_packet(AVFormatContext *s)
528 ASFContext *asf = s->priv_data;
531 hdr_size = put_packet(s, asf->packet_timestamp_start,
532 asf->packet_timestamp_end - asf->packet_timestamp_start,
533 asf->packet_nb_frames, asf->packet_size_left);
535 /* Clear out the padding bytes */
536 ptr = asf->packet_size - hdr_size - asf->packet_size_left;
537 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
539 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
541 put_flush_packet(&s->pb);
543 asf->packet_nb_frames = 0;
544 asf->packet_timestamp_start = -1;
545 asf->packet_timestamp_end = -1;
546 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
547 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
548 NULL, NULL, NULL, NULL);
551 static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
552 int payload_size, int frag_offset, int frag_len)
554 ASFContext *asf = s->priv_data;
555 ByteIOContext *pb = &asf->pb;
559 if (s->streams[val - 1]->codec.key_frame /* && frag_offset == 0 */)
562 put_byte(pb, stream->seq);
563 put_le32(pb, frag_offset); /* fragment offset */
564 put_byte(pb, 0x08); /* flags */
565 put_le32(pb, payload_size);
566 put_le32(pb, timestamp);
567 put_le16(pb, frag_len);
571 /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
573 It is there that you understand that the ASF format is really
574 crap. They have misread the MPEG Systems spec !
576 static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
577 UINT8 *buf, int payload_size)
579 ASFContext *asf = s->priv_data;
580 int frag_pos, frag_len, frag_len1;
583 while (frag_pos < payload_size) {
584 frag_len = payload_size - frag_pos;
585 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
587 if (frag_len > frag_len1)
588 frag_len = frag_len1;
589 put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len);
590 put_buffer(&asf->pb, buf, frag_len);
591 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
592 asf->packet_timestamp_end = timestamp;
593 if (asf->packet_timestamp_start == -1)
594 asf->packet_timestamp_start = timestamp;
595 asf->packet_nb_frames++;
599 frag_pos += frag_len;
601 /* output the frame if filled */
602 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
609 static int asf_write_packet(AVFormatContext *s, int stream_index,
610 UINT8 *buf, int size, int timestamp)
612 ASFContext *asf = s->priv_data;
615 AVCodecContext *codec;
617 codec = &s->streams[stream_index]->codec;
618 stream = &asf->streams[stream_index];
620 if (codec->codec_type == CODEC_TYPE_AUDIO) {
621 duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
624 duration = codec->frame_number *
625 ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
627 if (duration > asf->duration)
628 asf->duration = duration;
630 put_frame(s, stream, timestamp, buf, size);
634 static int asf_write_trailer(AVFormatContext *s)
636 ASFContext *asf = s->priv_data;
639 /* flush the current packet */
640 if (asf->pb.buf_ptr > asf->pb.buffer)
643 if (asf->is_streamed) {
644 put_chunk(s, 0x4524, 0, 0); /* end of stream */
646 /* rewrite an updated header */
647 file_size = url_ftell(&s->pb);
648 url_fseek(&s->pb, 0, SEEK_SET);
649 asf_write_header1(s, file_size, file_size - asf->data_offset);
652 put_flush_packet(&s->pb);
656 /**********************************/
662 static void print_guid(const GUID *g)
665 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
667 printf(" 0x%02x,", g->v4[i]);
672 static void get_guid(ByteIOContext *s, GUID *g)
680 g->v4[i] = get_byte(s);
684 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
693 if ((q - buf) < buf_size - 1)
701 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
709 if ((q - buf) < buf_size - 1)
716 static int asf_probe(AVProbeData *pd)
719 const unsigned char *p;
722 /* check file header */
723 if (pd->buf_size <= 32)
726 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
728 g.v2 = p[0] | (p[1] << 8);
730 g.v3 = p[0] | (p[1] << 8);
735 if (!memcmp(&g, &asf_header, sizeof(GUID)))
736 return AVPROBE_SCORE_MAX;
741 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
743 ASFContext *asf = s->priv_data;
745 ByteIOContext *pb = &s->pb;
751 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
754 if (memcmp(&g, &asf_header, sizeof(GUID)))
760 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
763 gsize = get_le64(pb);
765 printf("%08Lx: ", url_ftell(pb) - 24);
767 printf(" size=0x%Lx\n", gsize);
771 if (!memcmp(&g, &file_header, sizeof(GUID))) {
772 get_guid(pb, &asf->hdr.guid);
773 asf->hdr.file_size = get_le64(pb);
774 asf->hdr.create_time = get_le64(pb);
775 asf->hdr.packets_count = get_le64(pb);
776 asf->hdr.play_time = get_le64(pb);
777 asf->hdr.send_time = get_le64(pb);
778 asf->hdr.preroll = get_le32(pb);
779 asf->hdr.ignore = get_le32(pb);
780 asf->hdr.flags = get_le32(pb);
781 asf->hdr.min_pktsize = get_le32(pb);
782 asf->hdr.max_pktsize = get_le32(pb);
783 asf->hdr.max_bitrate = get_le32(pb);
784 asf->packet_size = asf->hdr.max_pktsize;
785 asf->nb_packets = asf->hdr.packets_count;
786 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
787 int type, total_size;
791 pos1 = url_ftell(pb);
793 st = av_mallocz(sizeof(AVStream));
796 s->streams[s->nb_streams] = st;
797 asf_st = av_mallocz(sizeof(ASFStream));
800 st->priv_data = asf_st;
801 st->time_length = (asf->hdr.send_time - asf->hdr.preroll) / 10; // us
803 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
804 type = CODEC_TYPE_AUDIO;
805 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
806 type = CODEC_TYPE_VIDEO;
811 total_size = get_le64(pb);
814 st->id = get_le16(pb) & 0x7f; /* stream id */
815 // mapping of asf ID to AV stream ID;
816 asf->asfid2avid[st->id] = s->nb_streams++;
819 st->codec.codec_type = type;
820 st->codec.frame_rate = 15 * s->pts_den / s->pts_num; // 15 fps default
821 if (type == CODEC_TYPE_AUDIO) {
822 get_wav_header(pb, &st->codec, 1);
823 /* We have to init the frame size at some point .... */
824 pos2 = url_ftell(pb);
825 if (gsize > (pos2 + 8 - pos1 + 24)) {
826 asf_st->ds_span = get_byte(pb);
827 asf_st->ds_packet_size = get_le16(pb);
828 asf_st->ds_chunk_size = get_le16(pb);
829 asf_st->ds_data_size = get_le16(pb);
830 asf_st->ds_silence_data = get_byte(pb);
832 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
833 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
834 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
835 if (asf_st->ds_span > 1) {
836 if (!asf_st->ds_chunk_size
837 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
838 asf_st->ds_span = 0; // disable descrambling
840 switch (st->codec.codec_id) {
841 case CODEC_ID_MP3LAME:
842 st->codec.frame_size = MPA_FRAME_SIZE;
844 case CODEC_ID_PCM_S16LE:
845 case CODEC_ID_PCM_S16BE:
846 case CODEC_ID_PCM_U16LE:
847 case CODEC_ID_PCM_U16BE:
848 case CODEC_ID_PCM_S8:
849 case CODEC_ID_PCM_U8:
850 case CODEC_ID_PCM_ALAW:
851 case CODEC_ID_PCM_MULAW:
852 st->codec.frame_size = 1;
855 /* This is probably wrong, but it prevents a crash later */
856 st->codec.frame_size = 1;
863 size = get_le16(pb); /* size */
864 get_le32(pb); /* size */
865 st->codec.width = get_le32(pb);
866 st->codec.height = get_le32(pb);
867 /* not available for asf */
868 get_le16(pb); /* panes */
869 get_le16(pb); /* depth */
873 st->codec.extradata_size = size - 40;
874 st->codec.extradata = av_mallocz(st->codec.extradata_size);
875 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
877 st->codec.codec_tag = st->codec.fourcc = tag1;
878 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
880 pos2 = url_ftell(pb);
881 url_fskip(pb, gsize - (pos2 - pos1 + 24));
882 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
884 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
885 int len1, len2, len3, len4, len5;
892 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
893 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
894 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
895 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
898 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
903 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
913 num = get_le16(pb); /* stream number */
914 get_str16(pb, str, sizeof(str));
915 get_str16(pb, str, sizeof(str));
920 if ((q - tag) < sizeof(tag) - 1)
927 } else if (url_feof(pb)) {
930 url_fseek(pb, gsize - 24, SEEK_CUR);
939 asf->data_offset = url_ftell(pb);
940 asf->packet_size_left = 0;
945 for(i=0;i<s->nb_streams;i++) {
946 AVStream *st = s->streams[i];
948 av_free(st->priv_data);
949 av_free(st->codec.extradata);
956 #define DO_2BITS(bits, var, defval) \
959 case 3: var = get_le32(pb); rsize += 4; break; \
960 case 2: var = get_le16(pb); rsize += 2; break; \
961 case 1: var = get_byte(pb); rsize++; break; \
962 default: var = defval; break; \
965 static int asf_get_packet(AVFormatContext *s)
967 ASFContext *asf = s->priv_data;
968 ByteIOContext *pb = &s->pb;
969 uint32_t packet_length, padsize;
971 int c = get_byte(pb);
974 printf("ff asf bad header %x at:%Ld\n", c, url_ftell(pb));
977 if ((c & 0x0f) == 2) { // always true for now
978 if (get_le16(pb) != 0) {
980 printf("ff asf bad non zero\n");
985 asf->packet_flags = get_byte(pb);
986 asf->packet_property = get_byte(pb);
988 DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
989 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
990 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
992 asf->packet_timestamp = get_le32(pb);
993 get_le16(pb); /* duration */
994 // rsize has at least 11 bytes which have to be present
996 if (asf->packet_flags & 0x01) {
997 asf->packet_segsizetype = get_byte(pb); rsize++;
998 asf->packet_segments = asf->packet_segsizetype & 0x3f;
1000 asf->packet_segments = 1;
1001 asf->packet_segsizetype = 0x80;
1003 asf->packet_size_left = packet_length - padsize - rsize;
1004 if (packet_length < asf->hdr.min_pktsize)
1005 padsize += asf->hdr.min_pktsize - packet_length;
1006 asf->packet_padsize = padsize;
1008 printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
1013 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1015 ASFContext *asf = s->priv_data;
1016 ASFStream *asf_st = 0;
1017 ByteIOContext *pb = &s->pb;
1018 //static int pc = 0;
1021 if (asf->packet_size_left < FRAME_HEADER_SIZE
1022 || asf->packet_segments < 1) {
1023 //asf->packet_size_left <= asf->packet_padsize) {
1024 int ret = asf->packet_size_left + asf->packet_padsize;
1025 //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
1028 ret = asf_get_packet(s);
1029 //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
1030 if (ret < 0 || url_feof(pb))
1032 asf->packet_time_start = 0;
1035 if (asf->packet_time_start == 0) {
1036 /* read frame header */
1037 int num = get_byte(pb);
1038 asf->packet_segments--;
1040 asf->packet_key_frame = (num & 0x80) >> 7;
1041 asf->stream_index = asf->asfid2avid[num & 0x7f];
1042 // sequence should be ignored!
1043 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1044 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1045 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1047 if (asf->packet_replic_size > 1) {
1048 // it should be always at least 8 bytes - FIXME validate
1049 asf->packet_obj_size = get_le32(pb);
1050 asf->packet_frag_timestamp = get_le32(pb); // timestamp
1051 if (asf->packet_replic_size > 8)
1052 url_fskip(pb, asf->packet_replic_size - 8);
1053 rsize += asf->packet_replic_size; // FIXME - check validity
1055 // multipacket - frag_offset is begining timestamp
1056 asf->packet_time_start = asf->packet_frag_offset;
1057 asf->packet_frag_offset = 0;
1058 asf->packet_frag_timestamp = asf->packet_timestamp;
1060 if (asf->packet_replic_size == 1) {
1061 asf->packet_time_delta = get_byte(pb);
1065 if (asf->packet_flags & 0x01) {
1066 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1068 //printf("Fragsize %d\n", asf->packet_frag_size);
1070 asf->packet_frag_size = asf->packet_size_left - rsize;
1071 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
1073 if (asf->packet_replic_size == 1) {
1074 asf->packet_multi_size = asf->packet_frag_size;
1075 if (asf->packet_multi_size > asf->packet_size_left) {
1076 asf->packet_segments = 0;
1080 asf->packet_size_left -= rsize;
1081 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
1083 if (asf->stream_index < 0) {
1084 asf->packet_time_start = 0;
1085 /* unhandled packet (should not happen) */
1086 url_fskip(pb, asf->packet_frag_size);
1087 asf->packet_size_left -= asf->packet_frag_size;
1088 printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
1091 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1093 asf_st = asf->asf_st;
1095 if ((asf->packet_frag_offset != asf_st->frag_offset
1096 || (asf->packet_frag_offset
1097 && asf->packet_seq != asf_st->seq)) // seq should be ignored
1099 /* cannot continue current packet: free it */
1100 // FIXME better check if packet was already allocated
1101 printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
1103 asf->packet_obj_size,
1104 asf->packet_frag_offset, asf_st->frag_offset,
1105 asf->packet_seq, asf_st->seq, asf->packet_frag_size);
1106 if (asf_st->pkt.size)
1107 av_free_packet(&asf_st->pkt);
1108 asf_st->frag_offset = 0;
1109 if (asf->packet_frag_offset != 0) {
1110 url_fskip(pb, asf->packet_frag_size);
1111 printf("ff asf parser skiping %db\n", asf->packet_frag_size);
1112 asf->packet_size_left -= asf->packet_frag_size;
1116 if (asf->packet_replic_size == 1) {
1117 // frag_offset is here used as the begining timestamp
1118 asf->packet_frag_timestamp = asf->packet_time_start;
1119 asf->packet_time_start += asf->packet_time_delta;
1120 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
1121 asf->packet_size_left--;
1122 asf->packet_multi_size--;
1123 if (asf->packet_multi_size < asf->packet_obj_size)
1125 asf->packet_time_start = 0;
1126 url_fskip(pb, asf->packet_multi_size);
1127 asf->packet_size_left -= asf->packet_multi_size;
1130 asf->packet_multi_size -= asf->packet_obj_size;
1131 //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);
1133 if (asf_st->frag_offset == 0) {
1135 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1136 asf_st->seq = asf->packet_seq;
1137 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1138 asf_st->pkt.stream_index = asf->stream_index;
1139 if (asf->packet_key_frame)
1140 asf_st->pkt.flags |= PKT_FLAG_KEY;
1144 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1145 // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1146 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1147 asf->packet_size_left -= asf->packet_frag_size;
1148 if (asf->packet_size_left < 0)
1150 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
1151 asf->packet_frag_size);
1152 asf_st->frag_offset += asf->packet_frag_size;
1153 /* test if whole packet is read */
1154 if (asf_st->frag_offset == asf_st->pkt.size) {
1156 if (asf_st->ds_span > 1) {
1157 /* packet descrambling */
1158 char* newdata = av_malloc(asf_st->pkt.size);
1161 while (offset < asf_st->pkt.size) {
1162 int off = offset / asf_st->ds_chunk_size;
1163 int row = off / asf_st->ds_span;
1164 int col = off % asf_st->ds_span;
1165 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1166 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
1167 memcpy(newdata + offset,
1168 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1169 asf_st->ds_chunk_size);
1170 offset += asf_st->ds_chunk_size;
1172 av_free(asf_st->pkt.data);
1173 asf_st->pkt.data = newdata;
1176 asf_st->frag_offset = 0;
1177 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
1178 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1179 asf_st->pkt.size = 0;
1180 asf_st->pkt.data = 0;
1181 break; // packet completed
1187 static int asf_read_close(AVFormatContext *s)
1191 for(i=0;i<s->nb_streams;i++) {
1192 AVStream *st = s->streams[i];
1193 av_free(st->priv_data);
1194 av_free(st->codec.extradata);
1199 static int asf_read_seek(AVFormatContext *s, int64_t pts)
1201 printf("SEEK TO %Ld", pts);
1205 static AVInputFormat asf_iformat = {
1216 static AVOutputFormat asf_oformat = {
1219 "application/octet-stream",
1222 #ifdef CONFIG_MP3LAME
1233 static AVOutputFormat asf_stream_oformat = {
1236 "application/octet-stream",
1239 #ifdef CONFIG_MP3LAME
1245 asf_write_stream_header,
1252 av_register_input_format(&asf_iformat);
1253 av_register_output_format(&asf_oformat);
1254 av_register_output_format(&asf_stream_oformat);