Change ticker_tick to use ticker_abs as we want the absolute number
[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] || s->author[0] || s->copyright[0] || s->comment[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 AVCodec *p;
376
377 enc = &s->streams[n]->codec;
378 p = avcodec_find_encoder(enc->codec_id);
379
380 put_le16(pb, asf->streams[n].num);
381 put_str16(pb, p ? p->name : enc->codec_name);
382 put_le16(pb, 0); /* no parameters */
383 /* id */
384 if (enc->codec_type == CODEC_TYPE_AUDIO) {
385 put_le16(pb, 2);
386 put_le16(pb, codec_get_tag(codec_wav_tags, enc->codec_id));
387 } else {
388 put_le16(pb, 4);
389 put_le32(pb, codec_get_tag(codec_asf_bmp_tags, enc->codec_id));
390 }
391 }
392 end_header(pb, hpos);
393
394 /* patch the header size fields */
395
396 cur_pos = url_ftell(pb);
397 header_size = cur_pos - header_offset;
398 if (url_is_streamed(&s->pb)) {
399 header_size += 8 + 30 + 50;
400
401 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
402 put_le16(pb, header_size);
403 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
404 put_le16(pb, header_size);
405
406 header_size -= 8 + 30 + 50;
407 }
408 header_size += 24 + 6;
409 url_fseek(pb, header_offset - 14, SEEK_SET);
410 put_le64(pb, header_size);
411 url_fseek(pb, cur_pos, SEEK_SET);
412
413 /* movie chunk, followed by packets of packet_size */
414 asf->data_offset = cur_pos;
415 put_guid(pb, &data_header);
416 put_le64(pb, data_chunk_size);
417 put_guid(pb, &my_guid);
418 put_le64(pb, asf->nb_packets); /* nb packets */
419 put_byte(pb, 1); /* ??? */
420 put_byte(pb, 1); /* ??? */
421 return 0;
422 }
423
424 static int asf_write_header(AVFormatContext *s)
425 {
426 ASFContext *asf = s->priv_data;
427
428 asf->packet_size = PACKET_SIZE;
429 asf->nb_packets = 0;
430
431 if (asf_write_header1(s, 0, 50) < 0) {
432 //av_free(asf);
433 return -1;
434 }
435
436 put_flush_packet(&s->pb);
437
438 asf->packet_nb_frames = 0;
439 asf->packet_timestamp_start = -1;
440 asf->packet_timestamp_end = -1;
441 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
442 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
443 NULL, NULL, NULL, NULL);
444
445 return 0;
446 }
447
448 /* write a fixed size packet */
449 static int put_packet(AVFormatContext *s,
450 unsigned int timestamp, unsigned int duration,
451 int nb_frames, int padsize)
452 {
453 ASFContext *asf = s->priv_data;
454 ByteIOContext *pb = &s->pb;
455 int flags;
456
457 if (url_is_streamed(&s->pb)) {
458 put_chunk(s, 0x4424, asf->packet_size, 0);
459 }
460
461 put_byte(pb, 0x82);
462 put_le16(pb, 0);
463
464 flags = 0x01; /* nb segments present */
465 if (padsize > 0) {
466 if (padsize < 256)
467 flags |= 0x08;
468 else
469 flags |= 0x10;
470 }
471 put_byte(pb, flags); /* flags */
472 put_byte(pb, 0x5d);
473 if (flags & 0x10)
474 put_le16(pb, padsize - 2);
475 if (flags & 0x08)
476 put_byte(pb, padsize - 1);
477 put_le32(pb, timestamp);
478 put_le16(pb, duration);
479 put_byte(pb, nb_frames | 0x80);
480
481 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
482 }
483
484 static void flush_packet(AVFormatContext *s)
485 {
486 ASFContext *asf = s->priv_data;
487 int hdr_size, ptr;
488
489 hdr_size = put_packet(s, asf->packet_timestamp_start,
490 asf->packet_timestamp_end - asf->packet_timestamp_start,
491 asf->packet_nb_frames, asf->packet_size_left);
492
493 /* Clear out the padding bytes */
494 ptr = asf->packet_size - hdr_size - asf->packet_size_left;
495 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
496
497 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
498
499 put_flush_packet(&s->pb);
500 asf->nb_packets++;
501 asf->packet_nb_frames = 0;
502 asf->packet_timestamp_start = -1;
503 asf->packet_timestamp_end = -1;
504 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
505 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
506 NULL, NULL, NULL, NULL);
507 }
508
509 static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
510 int payload_size, int frag_offset, int frag_len)
511 {
512 ASFContext *asf = s->priv_data;
513 ByteIOContext *pb = &asf->pb;
514 int val;
515
516 val = stream->num;
517 if (s->streams[val - 1]->codec.key_frame /* && frag_offset == 0 */)
518 val |= 0x80;
519 put_byte(pb, val);
520 put_byte(pb, stream->seq);
521 put_le32(pb, frag_offset); /* fragment offset */
522 put_byte(pb, 0x08); /* flags */
523 put_le32(pb, payload_size);
524 put_le32(pb, timestamp);
525 put_le16(pb, frag_len);
526 }
527
528
529 /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
530
531 It is there that you understand that the ASF format is really
532 crap. They have misread the MPEG Systems spec !
533 */
534 static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
535 UINT8 *buf, int payload_size)
536 {
537 ASFContext *asf = s->priv_data;
538 int frag_pos, frag_len, frag_len1;
539
540 frag_pos = 0;
541 while (frag_pos < payload_size) {
542 frag_len = payload_size - frag_pos;
543 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
544 if (frag_len1 > 0) {
545 if (frag_len > frag_len1)
546 frag_len = frag_len1;
547 put_frame_header(s, stream, timestamp, payload_size, frag_pos, frag_len);
548 put_buffer(&asf->pb, buf, frag_len);
549 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
550 asf->packet_timestamp_end = timestamp;
551 if (asf->packet_timestamp_start == -1)
552 asf->packet_timestamp_start = timestamp;
553 asf->packet_nb_frames++;
554 } else {
555 frag_len = 0;
556 }
557 frag_pos += frag_len;
558 buf += frag_len;
559 /* output the frame if filled */
560 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
561 flush_packet(s);
562 }
563 stream->seq++;
564 }
565
566
567 static int asf_write_packet(AVFormatContext *s, int stream_index,
568 UINT8 *buf, int size, int force_pts)
569 {
570 ASFContext *asf = s->priv_data;
571 ASFStream *stream;
572 int timestamp;
573 INT64 duration;
574 AVCodecContext *codec;
575
576 stream = &asf->streams[stream_index];
577 codec = &s->streams[stream_index]->codec;
578 stream = &asf->streams[stream_index];
579
580 if (codec->codec_type == CODEC_TYPE_AUDIO) {
581 timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number);
582 duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
583 codec->sample_rate;
584 } else {
585 timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number);
586 duration = codec->frame_number *
587 ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
588 }
589 if (duration > asf->duration)
590 asf->duration = duration;
591
592 put_frame(s, stream, timestamp, buf, size);
593 return 0;
594 }
595
596 static int asf_write_trailer(AVFormatContext *s)
597 {
598 ASFContext *asf = s->priv_data;
599 INT64 file_size;
600
601 /* flush the current packet */
602 if (asf->pb.buf_ptr > asf->pb.buffer)
603 flush_packet(s);
604
605 if (url_is_streamed(&s->pb)) {
606 put_chunk(s, 0x4524, 0, 0); /* end of stream */
607 } else {
608 /* rewrite an updated header */
609 file_size = url_ftell(&s->pb);
610 url_fseek(&s->pb, 0, SEEK_SET);
611 asf_write_header1(s, file_size, file_size - asf->data_offset);
612 }
613
614 put_flush_packet(&s->pb);
615 return 0;
616 }
617
618 /**********************************/
619 /* decoding */
620
621 //#define DEBUG
622
623 #ifdef DEBUG
624 static void print_guid(const GUID *g)
625 {
626 int i;
627 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
628 for(i=0;i<8;i++)
629 printf(" 0x%02x,", g->v4[i]);
630 printf("}\n");
631 }
632 #endif
633
634 static void get_guid(ByteIOContext *s, GUID *g)
635 {
636 int i;
637
638 g->v1 = get_le32(s);
639 g->v2 = get_le16(s);
640 g->v3 = get_le16(s);
641 for(i=0;i<8;i++)
642 g->v4[i] = get_byte(s);
643 }
644
645 #if 0
646 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
647 {
648 int len, c;
649 char *q;
650
651 len = get_le16(pb);
652 q = buf;
653 while (len > 0) {
654 c = get_le16(pb);
655 if ((q - buf) < buf_size - 1)
656 *q++ = c;
657 len--;
658 }
659 *q = '\0';
660 }
661 #endif
662
663 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
664 {
665 int c;
666 char *q;
667
668 q = buf;
669 while (len > 0) {
670 c = get_le16(pb);
671 if ((q - buf) < buf_size - 1)
672 *q++ = c;
673 len-=2;
674 }
675 *q = '\0';
676 }
677
678 static int asf_probe(AVProbeData *pd)
679 {
680 GUID g;
681 const unsigned char *p;
682 int i;
683
684 /* check file header */
685 if (pd->buf_size <= 32)
686 return 0;
687 p = pd->buf;
688 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
689 p += 4;
690 g.v2 = p[0] | (p[1] << 8);
691 p += 2;
692 g.v3 = p[0] | (p[1] << 8);
693 p += 2;
694 for(i=0;i<8;i++)
695 g.v4[i] = *p++;
696
697 if (!memcmp(&g, &asf_header, sizeof(GUID)))
698 return AVPROBE_SCORE_MAX;
699 else
700 return 0;
701 }
702
703 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
704 {
705 ASFContext *asf = s->priv_data;
706 GUID g;
707 ByteIOContext *pb = &s->pb;
708 AVStream *st;
709 ASFStream *asf_st;
710 int size, i, bps;
711 INT64 gsize;
712
713 get_guid(pb, &g);
714 if (memcmp(&g, &asf_header, sizeof(GUID)))
715 goto fail;
716 get_le64(pb);
717 get_le32(pb);
718 get_byte(pb);
719 get_byte(pb);
720
721 for(;;) {
722 get_guid(pb, &g);
723 gsize = get_le64(pb);
724 #ifdef DEBUG
725 printf("%08Lx: ", url_ftell(pb) - 24);
726 print_guid(&g);
727 printf(" size=0x%Lx\n", gsize);
728 #endif
729 if (gsize < 24)
730 goto fail;
731 if (!memcmp(&g, &file_header, sizeof(GUID))) {
732 get_guid(pb, &g);
733 get_le64(pb); /* file size */
734 get_le64(pb); /* file time */
735 get_le64(pb); /* nb_packets */
736 get_le64(pb); /* length 0 in us */
737 get_le64(pb); /* length 1 in us */
738 get_le32(pb);
739 get_le32(pb);
740 get_le32(pb);
741 asf->packet_size = get_le32(pb);
742 get_le32(pb);
743 get_le32(pb);
744 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
745 int type, id, total_size;
746 unsigned int tag1;
747 INT64 pos1, pos2;
748
749 pos1 = url_ftell(pb);
750
751 st = av_mallocz(sizeof(AVStream));
752 if (!st)
753 goto fail;
754 s->streams[s->nb_streams++] = st;
755 asf_st = av_mallocz(sizeof(ASFStream));
756 if (!asf_st)
757 goto fail;
758 st->priv_data = asf_st;
759
760 get_guid(pb, &g);
761 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
762 type = CODEC_TYPE_AUDIO;
763 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
764 type = CODEC_TYPE_VIDEO;
765 } else {
766 goto fail;
767 }
768 get_guid(pb, &g);
769 total_size = get_le64(pb);
770 get_le32(pb);
771 get_le32(pb);
772 st->id = get_le16(pb); /* stream id */
773 get_le32(pb);
774 st->codec.codec_type = type;
775 if (type == CODEC_TYPE_AUDIO) {
776 id = get_le16(pb);
777 st->codec.codec_tag = id;
778 st->codec.channels = get_le16(pb);
779 st->codec.sample_rate = get_le32(pb);
780 st->codec.bit_rate = get_le32(pb) * 8;
781 get_le16(pb); /* block align */
782 bps = get_le16(pb); /* bits per sample */
783 st->codec.codec_id = wav_codec_get_id(id, bps);
784 size = get_le16(pb);
785 url_fskip(pb, size);
786 /* We have to init the frame size at some point .... */
787 switch (st->codec.codec_id) {
788 case CODEC_ID_MP3LAME:
789 st->codec.frame_size = MPA_FRAME_SIZE;
790 break;
791 case CODEC_ID_PCM_S16LE:
792 case CODEC_ID_PCM_S16BE:
793 case CODEC_ID_PCM_U16LE:
794 case CODEC_ID_PCM_U16BE:
795 case CODEC_ID_PCM_S8:
796 case CODEC_ID_PCM_U8:
797 case CODEC_ID_PCM_ALAW:
798 case CODEC_ID_PCM_MULAW:
799 st->codec.frame_size = 1;
800 break;
801 default:
802 /* This is probably wrong, but it prevents a crash later */
803 st->codec.frame_size = 1;
804 break;
805 }
806 } else {
807 get_le32(pb);
808 get_le32(pb);
809 get_byte(pb);
810 size = get_le16(pb); /* size */
811 get_le32(pb); /* size */
812 st->codec.width = get_le32(pb);
813 st->codec.height = get_le32(pb);
814 st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
815 get_le16(pb); /* panes */
816 get_le16(pb); /* depth */
817 tag1 = get_le32(pb);
818 st->codec.codec_tag = tag1;
819 st->codec.codec_id = codec_get_id(codec_asf_bmp_tags, tag1);
820 url_fskip(pb, size - 5 * 4);
821 }
822 pos2 = url_ftell(pb);
823 url_fskip(pb, gsize - (pos2 - pos1 + 24));
824 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
825 break;
826 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
827 int len1, len2, len3, len4, len5;
828
829 len1 = get_le16(pb);
830 len2 = get_le16(pb);
831 len3 = get_le16(pb);
832 len4 = get_le16(pb);
833 len5 = get_le16(pb);
834 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
835 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
836 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
837 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
838 url_fskip(pb, len5);
839 #if 0
840 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
841 int v1, v2;
842 get_guid(pb, &g);
843 v1 = get_le32(pb);
844 v2 = get_le16(pb);
845 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
846 int len, v1, n, num;
847 char str[256], *q;
848 char tag[16];
849
850 get_guid(pb, &g);
851 print_guid(&g);
852
853 n = get_le32(pb);
854 for(i=0;i<n;i++) {
855 num = get_le16(pb); /* stream number */
856 get_str16(pb, str, sizeof(str));
857 get_str16(pb, str, sizeof(str));
858 len = get_le16(pb);
859 q = tag;
860 while (len > 0) {
861 v1 = get_byte(pb);
862 if ((q - tag) < sizeof(tag) - 1)
863 *q++ = v1;
864 len--;
865 }
866 *q = '\0';
867 }
868 #endif
869 } else if (url_feof(pb)) {
870 goto fail;
871 } else {
872 url_fseek(pb, gsize - 24, SEEK_CUR);
873 }
874 }
875 get_guid(pb, &g);
876 get_le64(pb);
877 get_byte(pb);
878 get_byte(pb);
879
880 asf->packet_size_left = 0;
881
882 return 0;
883
884 fail:
885 for(i=0;i<s->nb_streams;i++) {
886 AVStream *st = s->streams[i];
887 if (st)
888 av_free(st->priv_data);
889 av_free(st);
890 }
891 //av_free(asf);
892 return -1;
893 }
894
895 static int asf_get_packet(AVFormatContext *s)
896 {
897 ASFContext *asf = s->priv_data;
898 ByteIOContext *pb = &s->pb;
899 int c, flags, timestamp, hdr_size;
900
901 hdr_size = 12;
902 c = get_byte(pb);
903 if (c != 0x82)
904 return -EIO;
905 get_le16(pb);
906 flags = get_byte(pb);
907 get_byte(pb);
908 asf->packet_padsize = 0;
909 if (flags & 0x10) {
910 asf->packet_padsize = get_le16(pb);
911 hdr_size += 2;
912 } else if (flags & 0x08) {
913 asf->packet_padsize = get_byte(pb);
914 hdr_size++;
915 }
916 timestamp = get_le32(pb);
917 get_le16(pb); /* duration */
918 get_byte(pb); /* nb_frames */
919 #ifdef DEBUG
920 printf("packet: size=%d padsize=%d\n", asf->packet_size, asf->packet_padsize);
921 #endif
922 asf->packet_size_left = asf->packet_size - hdr_size;
923 return 0;
924 }
925
926 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
927 {
928 ASFContext *asf = s->priv_data;
929 AVStream *st;
930 ASFStream *asf_st;
931 ByteIOContext *pb = &s->pb;
932 int ret, num, seq, frag_offset, payload_size, frag_len;
933 int key_frame;
934 int timestamp, i;
935
936 for(;;) {
937 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
938 asf->packet_size_left <= asf->packet_padsize) {
939 /* fail safe */
940 if (asf->packet_size_left)
941 url_fskip(pb, asf->packet_size_left);
942 ret = asf_get_packet(s);
943 if (ret < 0)
944 return -EIO;
945 }
946 /* read frame header */
947 num = get_byte(pb);
948 if (num & 0x80)
949 key_frame = 1;
950 else
951 key_frame = 0;
952
953 num &= 0x7f;
954 seq = get_byte(pb);
955 frag_offset = get_le32(pb);
956 get_byte(pb); /* flags */
957 payload_size = get_le32(pb);
958 timestamp = get_le32(pb);
959 frag_len = get_le16(pb);
960 #ifdef DEBUG
961 printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
962 num, seq, payload_size, frag_offset, frag_len);
963 #endif
964 st = NULL;
965 for(i=0;i<s->nb_streams;i++) {
966 st = s->streams[i];
967 if (st->id == num)
968 break;
969 }
970 asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len;
971 if (i == s->nb_streams) {
972 /* unhandled packet (should not happen) */
973 url_fskip(pb, frag_len);
974 } else {
975 asf_st = st->priv_data;
976 if (asf_st->frag_offset == 0) {
977 /* new packet */
978 av_new_packet(&asf_st->pkt, payload_size);
979 asf_st->seq = seq;
980 if (key_frame)
981 asf_st->pkt.flags |= PKT_FLAG_KEY;
982
983 asf_st->pkt.pts = timestamp;
984 } else {
985 if (seq == asf_st->seq &&
986 frag_offset == asf_st->frag_offset) {
987 /* continuing packet */
988 } else {
989 /* cannot continue current packet: free it */
990 av_free_packet(&asf_st->pkt);
991 asf_st->frag_offset = 0;
992 if (frag_offset != 0) {
993 /* cannot create new packet */
994 url_fskip(pb, frag_len);
995 goto next_frame;
996 } else {
997 /* create new packet */
998 av_new_packet(&asf_st->pkt, payload_size);
999 asf_st->seq = seq;
1000 }
1001 }
1002 }
1003 /* read data */
1004 get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len);
1005 asf_st->frag_offset += frag_len;
1006 /* test if whole packet read */
1007 if (asf_st->frag_offset == asf_st->pkt.size) {
1008 /* return packet */
1009 asf_st->pkt.stream_index = i;
1010 asf_st->frag_offset = 0;
1011 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
1012 break;
1013 }
1014 }
1015 next_frame:;
1016 }
1017
1018 return 0;
1019 }
1020
1021 static int asf_read_close(AVFormatContext *s)
1022 {
1023 //ASFContext *asf = s->priv_data;
1024 int i;
1025
1026 for(i=0;i<s->nb_streams;i++) {
1027 AVStream *st = s->streams[i];
1028 av_free(st->priv_data);
1029 }
1030 //av_free(asf);
1031 return 0;
1032 }
1033
1034 AVInputFormat asf_iformat = {
1035 "asf",
1036 "asf format",
1037 sizeof(ASFContext),
1038 asf_probe,
1039 asf_read_header,
1040 asf_read_packet,
1041 asf_read_close,
1042 };
1043
1044 AVOutputFormat asf_oformat = {
1045 "asf",
1046 "asf format",
1047 "application/octet-stream",
1048 "asf,wmv",
1049 sizeof(ASFContext),
1050 #ifdef CONFIG_MP3LAME
1051 CODEC_ID_MP3LAME,
1052 #else
1053 CODEC_ID_MP2,
1054 #endif
1055 CODEC_ID_MSMPEG4,
1056 asf_write_header,
1057 asf_write_packet,
1058 asf_write_trailer,
1059 };
1060
1061 int asf_init(void)
1062 {
1063 av_register_input_format(&asf_iformat);
1064 av_register_output_format(&asf_oformat);
1065 return 0;
1066 }