win32 fixes
[libav.git] / libav / asf.c
1 /*
2 * ASF compatible encoder and decoder.
3 * Copyright (c) 2000, 2001 Gerard Lantau.
4 *
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.
9 *
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.
14 *
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.
18 */
19 #include "avformat.h"
20 #include "avi.h"
21
22 #define PACKET_SIZE 3200
23 #define PACKET_HEADER_SIZE 12
24 #define FRAME_HEADER_SIZE 17
25
26 typedef struct {
27 int num;
28 int seq;
29 /* use for reading */
30 AVPacket pkt;
31 int frag_offset;
32 } ASFStream;
33
34 typedef struct {
35 int seqno;
36 int packet_size;
37
38 ASFStream streams[2];
39 /* non streamed additonnal info */
40 int data_offset;
41 INT64 nb_packets;
42 INT64 duration; /* in 100ns units */
43 /* packet filling */
44 int packet_size_left;
45 int packet_timestamp_start;
46 int packet_timestamp_end;
47 int packet_nb_frames;
48 UINT8 packet_buf[PACKET_SIZE];
49 ByteIOContext pb;
50 /* only for reading */
51 int packet_padsize;
52 } ASFContext;
53
54 typedef struct {
55 UINT32 v1;
56 UINT16 v2;
57 UINT16 v3;
58 UINT8 v4[8];
59 } GUID;
60
61 static const GUID asf_header = {
62 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
63 };
64
65 static const GUID file_header = {
66 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
67 };
68
69 static const GUID stream_header = {
70 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
71 };
72
73 static const GUID audio_stream = {
74 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
75 };
76
77 static const GUID audio_conceal_none = {
78 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
79 };
80
81 static const GUID video_stream = {
82 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
83 };
84
85 static const GUID video_conceal_none = {
86 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
87 };
88
89
90 static const GUID comment_header = {
91 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
92 };
93
94 static const GUID codec_comment_header = {
95 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
96 };
97 static const GUID codec_comment1_header = {
98 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
99 };
100
101 static const GUID data_header = {
102 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
103 };
104
105 static const GUID index_guid = {
106 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
107 };
108
109 static const GUID head1_guid = {
110 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
111 };
112
113 static const GUID head2_guid = {
114 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
115 };
116
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 },
121 };
122
123 static void put_guid(ByteIOContext *s, const GUID *g)
124 {
125 int i;
126
127 put_le32(s, g->v1);
128 put_le16(s, g->v2);
129 put_le16(s, g->v3);
130 for(i=0;i<8;i++)
131 put_byte(s, g->v4[i]);
132 }
133
134 static void put_str16(ByteIOContext *s, const char *tag)
135 {
136 int c;
137
138 put_le16(s,strlen(tag) + 1);
139 for(;;) {
140 c = (UINT8)*tag++;
141 put_le16(s, c);
142 if (c == '\0')
143 break;
144 }
145 }
146
147 static void put_str16_nolen(ByteIOContext *s, const char *tag)
148 {
149 int c;
150
151 for(;;) {
152 c = (UINT8)*tag++;
153 put_le16(s, c);
154 if (c == '\0')
155 break;
156 }
157 }
158
159 static INT64 put_header(ByteIOContext *pb, const GUID *g)
160 {
161 INT64 pos;
162
163 pos = url_ftell(pb);
164 put_guid(pb, g);
165 put_le64(pb, 24);
166 return pos;
167 }
168
169 /* update header size */
170 static void end_header(ByteIOContext *pb, INT64 pos)
171 {
172 INT64 pos1;
173
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);
178 }
179
180 /* write an asf chunk (only used in streaming case) */
181 static void put_chunk(AVFormatContext *s, int type, int payload_length)
182 {
183 ASFContext *asf = s->priv_data;
184 ByteIOContext *pb = &s->pb;
185 int length;
186
187 length = payload_length + 8;
188 put_le16(pb, type);
189 put_le16(pb, length);
190 put_le32(pb, asf->seqno);
191 put_le16(pb, 0); /* unknown bytes */
192 put_le16(pb, length);
193 asf->seqno++;
194 }
195
196 /* convert from unix to windows time */
197 static INT64 unix_to_file_time(int ti)
198 {
199 INT64 t;
200
201 t = ti * INT64_C(10000000);
202 t += INT64_C(116444736000000000);
203 return t;
204 }
205
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)
208 {
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;
212 int has_title;
213 AVCodecContext *enc;
214 INT64 header_offset, cur_pos, hpos;
215
216 has_title = (s->title[0] != '\0');
217
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); /* ??? */
224 } else {
225 put_chunk(s, 0x4824, 0); /* start of stream (length will be patched later) */
226 }
227
228 /* file header */
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);
233 file_time = 0;
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);
245
246 /* unknown headers */
247 hpos = put_header(pb, &head1_guid);
248 put_guid(pb, &head2_guid);
249 put_le32(pb, 6);
250 put_le16(pb, 0);
251 end_header(pb, hpos);
252
253 /* title and other infos */
254 if (has_title) {
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));
260 put_le16(pb, 0);
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);
266 }
267
268 /* stream headers */
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;
273
274 switch(enc->codec_type) {
275 case CODEC_TYPE_AUDIO:
276 wav_extra_size = 0;
277 extra_size = 18 + wav_extra_size;
278 extra_size2 = 0;
279 break;
280 default:
281 case CODEC_TYPE_VIDEO:
282 wav_extra_size = 0;
283 extra_size = 0x33;
284 extra_size2 = 0;
285 break;
286 }
287
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);
292 } else {
293 put_guid(pb, &video_stream);
294 put_guid(pb, &video_conceal_none);
295 }
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); /* ??? */
301
302 if (enc->codec_type == CODEC_TYPE_AUDIO) {
303 /* WAVEFORMATEX header */
304 put_wav_header(pb, enc);
305 } else {
306 put_le32(pb, enc->width);
307 put_le32(pb, enc->height);
308 put_byte(pb, 2); /* ??? */
309 put_le16(pb, 40); /* size */
310
311 /* BITMAPINFOHEADER header */
312 put_bmp_header(pb, enc);
313 }
314 end_header(pb, hpos);
315 }
316
317 /* media comments */
318
319 hpos = put_header(pb, &codec_comment_header);
320 put_guid(pb, &codec_comment1_header);
321 put_le32(pb, s->nb_streams);
322 for(n=0;n<s->nb_streams;n++) {
323 enc = &s->streams[n]->codec;
324
325 put_le16(pb, asf->streams[n].num);
326 put_str16(pb, enc->codec_name);
327 put_le16(pb, 0); /* no parameters */
328 /* id */
329 if (enc->codec_type == CODEC_TYPE_AUDIO) {
330 put_le16(pb, 2);
331 put_le16(pb, codec_get_tag(codec_wav_tags, enc->codec_id));
332 } else {
333 put_le16(pb, 4);
334 put_le32(pb, codec_get_tag(codec_bmp_tags, enc->codec_id));
335 }
336 }
337 end_header(pb, hpos);
338
339 /* patch the header size fields */
340
341 cur_pos = url_ftell(pb);
342 header_size = cur_pos - header_offset;
343 if (!url_is_streamed(&s->pb)) {
344 header_size += 24 + 6;
345 url_fseek(pb, header_offset - 14, SEEK_SET);
346 put_le64(pb, header_size);
347 } else {
348 header_size += 8 + 50;
349 url_fseek(pb, header_offset - 10, SEEK_SET);
350 put_le16(pb, header_size);
351 url_fseek(pb, header_offset - 2, SEEK_SET);
352 put_le16(pb, header_size);
353 }
354 url_fseek(pb, cur_pos, SEEK_SET);
355
356 /* movie chunk, followed by packets of packet_size */
357 asf->data_offset = cur_pos;
358 put_guid(pb, &data_header);
359 put_le64(pb, data_chunk_size);
360 put_guid(pb, &my_guid);
361 put_le64(pb, asf->nb_packets); /* nb packets */
362 put_byte(pb, 1); /* ??? */
363 put_byte(pb, 1); /* ??? */
364 return 0;
365 }
366
367 static int asf_write_header(AVFormatContext *s)
368 {
369 ASFContext *asf;
370
371 asf = av_mallocz(sizeof(ASFContext));
372 if (!asf)
373 return -1;
374 s->priv_data = asf;
375
376 asf->packet_size = PACKET_SIZE;
377 asf->nb_packets = 0;
378
379 asf_write_header1(s, 0, 24);
380
381 put_flush_packet(&s->pb);
382
383 asf->packet_nb_frames = 0;
384 asf->packet_timestamp_start = -1;
385 asf->packet_timestamp_end = -1;
386 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
387 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
388 NULL, NULL, NULL, NULL);
389
390 return 0;
391 }
392
393 /* write a fixed size packet */
394 static void put_packet(AVFormatContext *s,
395 unsigned int timestamp, unsigned int duration,
396 int nb_frames, int padsize)
397 {
398 ASFContext *asf = s->priv_data;
399 ByteIOContext *pb = &s->pb;
400 int flags;
401
402 if (url_is_streamed(&s->pb)) {
403 put_chunk(s, 0x4424, asf->packet_size);
404 }
405
406 put_byte(pb, 0x82);
407 put_le16(pb, 0);
408
409 flags = 0x01; /* nb segments present */
410 if (padsize > 0) {
411 if (padsize < 256)
412 flags |= 0x08;
413 else
414 flags |= 0x10;
415 }
416 put_byte(pb, flags); /* flags */
417 put_byte(pb, 0x5d);
418 if (flags & 0x10)
419 put_le16(pb, padsize);
420 if (flags & 0x08)
421 put_byte(pb, padsize);
422 put_le32(pb, timestamp);
423 put_le16(pb, duration);
424 put_byte(pb, nb_frames | 0x80);
425 }
426
427 static void flush_packet(AVFormatContext *s)
428 {
429 ASFContext *asf = s->priv_data;
430 int hdr_size, ptr;
431
432 put_packet(s, asf->packet_timestamp_start,
433 asf->packet_timestamp_end - asf->packet_timestamp_start,
434 asf->packet_nb_frames, asf->packet_size_left);
435
436 /* compute padding */
437 hdr_size = PACKET_HEADER_SIZE;
438 if (asf->packet_size_left > 0) {
439 /* if padding needed, don't forget to count the
440 padding byte in the header size */
441 hdr_size++;
442 asf->packet_size_left--;
443 /* XXX: I do not test again exact limit to avoid boundary problems */
444 if (asf->packet_size_left > 200) {
445 hdr_size++;
446 asf->packet_size_left--;
447 }
448 }
449 ptr = asf->packet_size - PACKET_HEADER_SIZE - asf->packet_size_left;
450 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
451
452 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
453
454 put_flush_packet(&s->pb);
455 asf->nb_packets++;
456 asf->packet_nb_frames = 0;
457 asf->packet_timestamp_start = -1;
458 asf->packet_timestamp_end = -1;
459 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
460 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
461 NULL, NULL, NULL, NULL);
462 }
463
464 static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
465 int payload_size, int frag_offset, int frag_len)
466 {
467 ASFContext *asf = s->priv_data;
468 ByteIOContext *pb = &asf->pb;
469 int val;
470
471 val = stream->num;
472 if (s->streams[val - 1]->codec.key_frame)
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
483
484 /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
485
486 It is there that you understand that the ASF format is really
487 crap. They have misread the MPEG Systems spec !
488 */
489 static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
490 UINT8 *buf, int payload_size)
491 {
492 ASFContext *asf = s->priv_data;
493 int frag_pos, frag_len, frag_len1;
494
495 frag_pos = 0;
496 while (frag_pos < payload_size) {
497 frag_len = payload_size - frag_pos;
498 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
499 if (frag_len1 > 0) {
500 if (frag_len > frag_len1)
501 frag_len = frag_len1;
502 put_frame_header(s, stream, timestamp, payload_size, frag_pos, frag_len);
503 put_buffer(&asf->pb, buf, frag_len);
504 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
505 asf->packet_timestamp_end = timestamp;
506 if (asf->packet_timestamp_start == -1)
507 asf->packet_timestamp_start = timestamp;
508 asf->packet_nb_frames++;
509 } else {
510 frag_len = 0;
511 }
512 frag_pos += frag_len;
513 buf += frag_len;
514 /* output the frame if filled */
515 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
516 flush_packet(s);
517 }
518 stream->seq++;
519 }
520
521
522 static int asf_write_packet(AVFormatContext *s, int stream_index,
523 UINT8 *buf, int size)
524 {
525 ASFContext *asf = s->priv_data;
526 int timestamp;
527 INT64 duration;
528 AVCodecContext *codec;
529
530 codec = &s->streams[stream_index]->codec;
531 if (codec->codec_type == CODEC_TYPE_AUDIO) {
532 timestamp = (int)((float)codec->frame_number * codec->frame_size * 1000.0 /
533 codec->sample_rate);
534 duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
535 codec->sample_rate;
536 } else {
537 timestamp = (int)((float)codec->frame_number * 1000.0 * FRAME_RATE_BASE /
538 codec->frame_rate);
539 duration = codec->frame_number *
540 ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
541 }
542 if (duration > asf->duration)
543 asf->duration = duration;
544
545 put_frame(s, &asf->streams[stream_index], (int)timestamp, buf, size);
546 return 0;
547 }
548
549 static int asf_write_trailer(AVFormatContext *s)
550 {
551 ASFContext *asf = s->priv_data;
552 INT64 file_size;
553
554 /* flush the current packet */
555 if (asf->pb.buf_ptr > asf->pb.buffer)
556 flush_packet(s);
557
558 if (url_is_streamed(&s->pb)) {
559 put_chunk(s, 0x4524, 0); /* end of stream */
560 } else {
561 /* rewrite an updated header */
562 file_size = url_ftell(&s->pb);
563 url_fseek(&s->pb, 0, SEEK_SET);
564 asf_write_header1(s, file_size, file_size - asf->data_offset);
565 }
566
567 put_flush_packet(&s->pb);
568
569 free(asf);
570 return 0;
571 }
572
573 /**********************************/
574 /* decoding */
575
576 //#define DEBUG
577
578 #ifdef DEBUG
579 static void print_guid(const GUID *g)
580 {
581 int i;
582 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
583 for(i=0;i<8;i++)
584 printf(" 0x%02x,", g->v4[i]);
585 printf("}\n");
586 }
587 #endif
588
589 static void get_guid(ByteIOContext *s, GUID *g)
590 {
591 int i;
592
593 g->v1 = get_le32(s);
594 g->v2 = get_le16(s);
595 g->v3 = get_le16(s);
596 for(i=0;i<8;i++)
597 g->v4[i] = get_byte(s);
598 }
599
600 #if 0
601 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
602 {
603 int len, c;
604 char *q;
605
606 len = get_le16(pb);
607 q = buf;
608 while (len > 0) {
609 c = get_le16(pb);
610 if ((q - buf) < buf_size - 1)
611 *q++ = c;
612 len--;
613 }
614 *q = '\0';
615 }
616 #endif
617
618 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
619 {
620 int c;
621 char *q;
622
623 q = buf;
624 while (len > 0) {
625 c = get_le16(pb);
626 if ((q - buf) < buf_size - 1)
627 *q++ = c;
628 len-=2;
629 }
630 *q = '\0';
631 }
632
633 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
634 {
635 ASFContext *asf;
636 GUID g;
637 ByteIOContext *pb = &s->pb;
638 AVStream *st;
639 ASFStream *asf_st;
640 int size, i;
641 INT64 gsize;
642
643 asf = av_mallocz(sizeof(ASFContext));
644 if (!asf)
645 return -1;
646 s->priv_data = asf;
647
648 get_guid(pb, &g);
649 if (memcmp(&g, &asf_header, sizeof(GUID)))
650 goto fail;
651 get_le64(pb);
652 get_le32(pb);
653 get_byte(pb);
654 get_byte(pb);
655
656 for(;;) {
657 get_guid(pb, &g);
658 gsize = get_le64(pb);
659 #ifdef DEBUG
660 printf("%08Lx: ", url_ftell(pb) - 24);
661 print_guid(&g);
662 printf(" size=0x%Lx\n", gsize);
663 #endif
664 if (gsize < 24)
665 goto fail;
666 if (!memcmp(&g, &file_header, sizeof(GUID))) {
667 get_guid(pb, &g);
668 get_le64(pb); /* file size */
669 get_le64(pb); /* file time */
670 get_le64(pb); /* nb_packets */
671 get_le64(pb); /* length 0 in us */
672 get_le64(pb); /* length 1 in us */
673 get_le32(pb);
674 get_le32(pb);
675 get_le32(pb);
676 asf->packet_size = get_le32(pb);
677 get_le32(pb);
678 get_le32(pb);
679 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
680 int type, id, total_size;
681 unsigned int tag1;
682 INT64 pos1, pos2;
683
684 pos1 = url_ftell(pb);
685
686 st = av_mallocz(sizeof(AVStream));
687 if (!st)
688 goto fail;
689 s->streams[s->nb_streams++] = st;
690 asf_st = av_mallocz(sizeof(ASFStream));
691 if (!asf_st)
692 goto fail;
693 st->priv_data = asf_st;
694
695 get_guid(pb, &g);
696 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
697 type = CODEC_TYPE_AUDIO;
698 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
699 type = CODEC_TYPE_VIDEO;
700 } else {
701 goto fail;
702 }
703 get_guid(pb, &g);
704 total_size = get_le64(pb);
705 get_le32(pb);
706 get_le32(pb);
707 st->id = get_le16(pb); /* stream id */
708 get_le32(pb);
709 st->codec.codec_type = type;
710 if (type == CODEC_TYPE_AUDIO) {
711 id = get_le16(pb);
712 st->codec.codec_tag = id;
713 st->codec.codec_id = codec_get_id(codec_wav_tags, id);
714 st->codec.channels = get_le16(pb);
715 st->codec.sample_rate = get_le32(pb);
716 st->codec.bit_rate = get_le32(pb) * 8;
717 get_le16(pb); /* block align */
718 get_le16(pb); /* bits per sample */
719 size = get_le16(pb);
720 url_fskip(pb, size);
721 } else {
722 get_le32(pb);
723 get_le32(pb);
724 get_byte(pb);
725 size = get_le16(pb); /* size */
726 get_le32(pb); /* size */
727 st->codec.width = get_le32(pb);
728 st->codec.height = get_le32(pb);
729 st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
730 get_le16(pb); /* panes */
731 get_le16(pb); /* depth */
732 tag1 = get_le32(pb);
733 st->codec.codec_tag = tag1;
734 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
735 url_fskip(pb, size - 5 * 4);
736 }
737 pos2 = url_ftell(pb);
738 url_fskip(pb, gsize - (pos2 - pos1 + 24));
739 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
740 break;
741 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
742 int len1, len2, len3, len4, len5;
743
744 len1 = get_le16(pb);
745 len2 = get_le16(pb);
746 len3 = get_le16(pb);
747 len4 = get_le16(pb);
748 len5 = get_le16(pb);
749 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
750 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
751 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
752 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
753 url_fskip(pb, len5);
754 #if 0
755 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
756 int v1, v2;
757 get_guid(pb, &g);
758 v1 = get_le32(pb);
759 v2 = get_le16(pb);
760 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
761 int len, v1, n, num;
762 char str[256], *q;
763 char tag[16];
764
765 get_guid(pb, &g);
766 print_guid(&g);
767
768 n = get_le32(pb);
769 for(i=0;i<n;i++) {
770 num = get_le16(pb); /* stream number */
771 get_str16(pb, str, sizeof(str));
772 get_str16(pb, str, sizeof(str));
773 len = get_le16(pb);
774 q = tag;
775 while (len > 0) {
776 v1 = get_byte(pb);
777 if ((q - tag) < sizeof(tag) - 1)
778 *q++ = v1;
779 len--;
780 }
781 *q = '\0';
782 }
783 #endif
784 } else if (url_feof(pb)) {
785 goto fail;
786 } else {
787 url_fseek(pb, gsize - 24, SEEK_CUR);
788 }
789 }
790 get_guid(pb, &g);
791 get_le64(pb);
792 get_byte(pb);
793 get_byte(pb);
794
795 asf->packet_size_left = 0;
796
797 return 0;
798
799 fail:
800 for(i=0;i<s->nb_streams;i++) {
801 AVStream *st = s->streams[i];
802 if (st)
803 free(st->priv_data);
804 free(st);
805 }
806 free(asf);
807 return -1;
808 }
809
810 static int asf_get_packet(AVFormatContext *s)
811 {
812 ASFContext *asf = s->priv_data;
813 ByteIOContext *pb = &s->pb;
814 int c, flags, timestamp, hdr_size;
815
816 hdr_size = 12;
817 c = get_byte(pb);
818 if (c != 0x82)
819 return -EIO;
820 get_le16(pb);
821 flags = get_byte(pb);
822 get_byte(pb);
823 asf->packet_padsize = 0;
824 if (flags & 0x10) {
825 asf->packet_padsize = get_le16(pb);
826 hdr_size += 2;
827 } else if (flags & 0x08) {
828 asf->packet_padsize = get_byte(pb);
829 hdr_size++;
830 }
831 timestamp = get_le32(pb);
832 get_le16(pb); /* duration */
833 get_byte(pb); /* nb_frames */
834 #ifdef DEBUG
835 printf("packet: size=%d padsize=%d\n", asf->packet_size, asf->packet_padsize);
836 #endif
837 asf->packet_size_left = asf->packet_size - hdr_size;
838 return 0;
839 }
840
841 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
842 {
843 ASFContext *asf = s->priv_data;
844 AVStream *st;
845 ASFStream *asf_st;
846 ByteIOContext *pb = &s->pb;
847 int ret, num, seq, frag_offset, payload_size, frag_len;
848 int timestamp, i;
849
850 for(;;) {
851 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
852 asf->packet_size_left <= asf->packet_padsize) {
853 /* fail safe */
854 if (asf->packet_size_left)
855 url_fskip(pb, asf->packet_size_left);
856 ret = asf_get_packet(s);
857 if (ret < 0)
858 return -EIO;
859 }
860 /* read frame header */
861 num = get_byte(pb) & 0x7f;
862 seq = get_byte(pb);
863 frag_offset = get_le32(pb);
864 get_byte(pb); /* flags */
865 payload_size = get_le32(pb);
866 timestamp = get_le32(pb);
867 frag_len = get_le16(pb);
868 #ifdef DEBUG
869 printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
870 num, seq, payload_size, frag_offset, frag_len);
871 #endif
872 st = NULL;
873 for(i=0;i<s->nb_streams;i++) {
874 st = s->streams[i];
875 if (st->id == num)
876 break;
877 }
878 asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len;
879 if (i == s->nb_streams) {
880 /* unhandled packet (should not happen) */
881 url_fskip(pb, frag_len);
882 } else {
883 asf_st = st->priv_data;
884 if (asf_st->frag_offset == 0) {
885 /* new packet */
886 av_new_packet(&asf_st->pkt, payload_size);
887 asf_st->seq = seq;
888 } else {
889 if (seq == asf_st->seq &&
890 frag_offset == asf_st->frag_offset) {
891 /* continuing packet */
892 } else {
893 /* cannot continue current packet: free it */
894 av_free_packet(&asf_st->pkt);
895 asf_st->frag_offset = 0;
896 if (frag_offset != 0) {
897 /* cannot create new packet */
898 url_fskip(pb, frag_len);
899 goto next_frame;
900 } else {
901 /* create new packet */
902 av_new_packet(&asf_st->pkt, payload_size);
903 asf_st->seq = seq;
904 }
905 }
906 }
907 /* read data */
908 get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len);
909 asf_st->frag_offset += frag_len;
910 /* test if whole packet read */
911 if (asf_st->frag_offset == asf_st->pkt.size) {
912 /* return packet */
913 asf_st->pkt.stream_index = i;
914 asf_st->frag_offset = 0;
915 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
916 break;
917 }
918 }
919 next_frame:;
920 }
921
922 return 0;
923 }
924
925 static int asf_read_close(AVFormatContext *s)
926 {
927 ASFContext *asf = s->priv_data;
928 int i;
929
930 for(i=0;i<s->nb_streams;i++) {
931 AVStream *st = s->streams[i];
932 free(st->priv_data);
933 }
934 free(asf);
935 return 0;
936 }
937
938 AVFormat asf_format = {
939 "asf",
940 "asf format",
941 "application/octet-stream",
942 "asf",
943 CODEC_ID_MP2,
944 CODEC_ID_MSMPEG4,
945 asf_write_header,
946 asf_write_packet,
947 asf_write_trailer,
948
949 asf_read_header,
950 asf_read_packet,
951 asf_read_close,
952 };