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