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