suppressed mpeg demux mess - use now dynamic stream creation api
[libav.git] / libav / asf.c
CommitLineData
de6d9b64
FB
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 */
de6d9b64
FB
19#include "avformat.h"
20#include "avi.h"
e0d2714a 21#include "tick.h"
f80c1ac0 22#include "mpegaudio.h"
de6d9b64
FB
23
24#define PACKET_SIZE 3200
25#define PACKET_HEADER_SIZE 12
26#define FRAME_HEADER_SIZE 17
27
28typedef struct {
29 int num;
30 int seq;
e0d2714a 31 Ticker pts_ticker;
de6d9b64
FB
32 /* use for reading */
33 AVPacket pkt;
34 int frag_offset;
4606ac8d 35 INT64 duration;
de6d9b64
FB
36} ASFStream;
37
38typedef 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
58typedef struct {
59 UINT32 v1;
60 UINT16 v2;
61 UINT16 v3;
62 UINT8 v4[8];
63} GUID;
64
65static const GUID asf_header = {
66 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
67};
68
69static const GUID file_header = {
70 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
71};
72
73static const GUID stream_header = {
74 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
75};
76
77static const GUID audio_stream = {
78 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
79};
80
81static const GUID audio_conceal_none = {
f80c1ac0
PG
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 },
de6d9b64
FB
85};
86
87static const GUID video_stream = {
88 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
89};
90
91static const GUID video_conceal_none = {
92 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
93};
94
95
96static const GUID comment_header = {
97 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
98};
99
100static const GUID codec_comment_header = {
101 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
102};
103static const GUID codec_comment1_header = {
104 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
105};
106
107static const GUID data_header = {
108 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
109};
110
111static const GUID index_guid = {
112 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
113};
114
115static const GUID head1_guid = {
116 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
117};
118
119static 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 */
125static const GUID my_guid = {
126 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
127};
128
4606ac8d 129CodecTag codec_asf_bmp_tags[] = {
95c79a24
J
130 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
131 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
4606ac8d
ZK
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 */
bb3debab
GV
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') },
4606ac8d
ZK
141 { 0, 0 },
142};
143
144
145
de6d9b64
FB
146static 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
157static 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
170static 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
182static 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 */
193static 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) */
f80c1ac0 204static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
de6d9b64
FB
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);
f80c1ac0 214 put_le16(pb, flags); /* unknown bytes */
de6d9b64
FB
215 put_le16(pb, length);
216 asf->seqno++;
217}
218
219/* convert from unix to windows time */
220static INT64 unix_to_file_time(int ti)
221{
222 INT64 t;
223
8be1c656
FB
224 t = ti * INT64_C(10000000);
225 t += INT64_C(116444736000000000);
de6d9b64
FB
226 return t;
227}
228
229/* write the header (used two times if non streamed) */
230static 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;
f80c1ac0 238 int bit_rate;
de6d9b64
FB
239
240 has_title = (s->title[0] != '\0');
241
f80c1ac0
PG
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) */
de6d9b64 251 }
f80c1ac0
PG
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); /* ??? */
de6d9b64
FB
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); /* ??? */
4606ac8d 271 put_le32(pb, url_is_streamed(&s->pb) ? 1 : 0); /* ??? */
de6d9b64
FB
272 put_le32(pb, asf->packet_size); /* packet size */
273 put_le32(pb, asf->packet_size); /* packet size */
f80c1ac0 274 put_le32(pb, bit_rate); /* Nominal data rate in bps */
de6d9b64
FB
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++) {
f80c1ac0 301 INT64 es_pos;
e0d2714a 302 ASFStream *stream = &asf->streams[n];
f80c1ac0 303
de6d9b64
FB
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;
e0d2714a
J
313 /* Init the ticker */
314 ticker_init(&stream->pts_ticker,
315 enc->sample_rate,
316 1000 * enc->frame_size);
de6d9b64
FB
317 break;
318 default:
319 case CODEC_TYPE_VIDEO:
320 wav_extra_size = 0;
321 extra_size = 0x33;
322 extra_size2 = 0;
e0d2714a
J
323 /* Init the ticker */
324 ticker_init(&stream->pts_ticker,
325 enc->frame_rate,
326 1000 * FRAME_RATE_BASE);
de6d9b64
FB
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); /* ??? */
f80c1ac0 339 es_pos = url_ftell(pb);
de6d9b64
FB
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 */
f80c1ac0
PG
347 int wavsize = put_wav_header(pb, enc);
348
349 if (wavsize < 0)
46a3d068 350 return -1;
f80c1ac0
PG
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 }
de6d9b64
FB
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 */
4606ac8d 364 put_bmp_header(pb, enc, codec_asf_bmp_tags);
de6d9b64
FB
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);
4606ac8d 386 put_le32(pb, codec_get_tag(codec_asf_bmp_tags, enc->codec_id));
de6d9b64
FB
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;
f80c1ac0
PG
395 if (url_is_streamed(&s->pb)) {
396 header_size += 8 + 30 + 50;
397
398 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
de6d9b64 399 put_le16(pb, header_size);
f80c1ac0 400 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
de6d9b64 401 put_le16(pb, header_size);
f80c1ac0
PG
402
403 header_size -= 8 + 30 + 50;
de6d9b64 404 }
f80c1ac0
PG
405 header_size += 24 + 6;
406 url_fseek(pb, header_offset - 14, SEEK_SET);
407 put_le64(pb, header_size);
de6d9b64
FB
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
421static int asf_write_header(AVFormatContext *s)
422{
423 ASFContext *asf;
424
425 asf = av_mallocz(sizeof(ASFContext));
426 if (!asf)
427 return -1;
428 s->priv_data = asf;
429
430 asf->packet_size = PACKET_SIZE;
431 asf->nb_packets = 0;
432
f80c1ac0 433 if (asf_write_header1(s, 0, 50) < 0) {
1ea4f593 434 av_free(asf);
46a3d068
FB
435 return -1;
436 }
de6d9b64
FB
437
438 put_flush_packet(&s->pb);
439
440 asf->packet_nb_frames = 0;
441 asf->packet_timestamp_start = -1;
442 asf->packet_timestamp_end = -1;
443 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
444 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
445 NULL, NULL, NULL, NULL);
446
447 return 0;
448}
449
450/* write a fixed size packet */
f80c1ac0 451static int put_packet(AVFormatContext *s,
de6d9b64
FB
452 unsigned int timestamp, unsigned int duration,
453 int nb_frames, int padsize)
454{
455 ASFContext *asf = s->priv_data;
456 ByteIOContext *pb = &s->pb;
457 int flags;
458
459 if (url_is_streamed(&s->pb)) {
f80c1ac0 460 put_chunk(s, 0x4424, asf->packet_size, 0);
de6d9b64
FB
461 }
462
463 put_byte(pb, 0x82);
464 put_le16(pb, 0);
465
466 flags = 0x01; /* nb segments present */
467 if (padsize > 0) {
468 if (padsize < 256)
469 flags |= 0x08;
470 else
471 flags |= 0x10;
472 }
473 put_byte(pb, flags); /* flags */
474 put_byte(pb, 0x5d);
475 if (flags & 0x10)
f80c1ac0 476 put_le16(pb, padsize - 2);
de6d9b64 477 if (flags & 0x08)
f80c1ac0 478 put_byte(pb, padsize - 1);
de6d9b64
FB
479 put_le32(pb, timestamp);
480 put_le16(pb, duration);
481 put_byte(pb, nb_frames | 0x80);
f80c1ac0
PG
482
483 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
de6d9b64
FB
484}
485
486static void flush_packet(AVFormatContext *s)
487{
488 ASFContext *asf = s->priv_data;
489 int hdr_size, ptr;
490
f80c1ac0 491 hdr_size = put_packet(s, asf->packet_timestamp_start,
de6d9b64
FB
492 asf->packet_timestamp_end - asf->packet_timestamp_start,
493 asf->packet_nb_frames, asf->packet_size_left);
494
f80c1ac0
PG
495 /* Clear out the padding bytes */
496 ptr = asf->packet_size - hdr_size - asf->packet_size_left;
de6d9b64
FB
497 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
498
499 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
500
501 put_flush_packet(&s->pb);
502 asf->nb_packets++;
503 asf->packet_nb_frames = 0;
504 asf->packet_timestamp_start = -1;
505 asf->packet_timestamp_end = -1;
506 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
507 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
508 NULL, NULL, NULL, NULL);
509}
510
511static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
512 int payload_size, int frag_offset, int frag_len)
513{
514 ASFContext *asf = s->priv_data;
515 ByteIOContext *pb = &asf->pb;
516 int val;
517
518 val = stream->num;
f80c1ac0 519 if (s->streams[val - 1]->codec.key_frame /* && frag_offset == 0 */)
de6d9b64
FB
520 val |= 0x80;
521 put_byte(pb, val);
522 put_byte(pb, stream->seq);
523 put_le32(pb, frag_offset); /* fragment offset */
524 put_byte(pb, 0x08); /* flags */
525 put_le32(pb, payload_size);
526 put_le32(pb, timestamp);
527 put_le16(pb, frag_len);
528}
529
530
531/* Output a frame. We suppose that payload_size <= PACKET_SIZE.
532
533 It is there that you understand that the ASF format is really
534 crap. They have misread the MPEG Systems spec !
535 */
536static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
537 UINT8 *buf, int payload_size)
538{
539 ASFContext *asf = s->priv_data;
540 int frag_pos, frag_len, frag_len1;
541
542 frag_pos = 0;
543 while (frag_pos < payload_size) {
544 frag_len = payload_size - frag_pos;
545 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
546 if (frag_len1 > 0) {
547 if (frag_len > frag_len1)
548 frag_len = frag_len1;
549 put_frame_header(s, stream, timestamp, payload_size, frag_pos, frag_len);
550 put_buffer(&asf->pb, buf, frag_len);
551 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
552 asf->packet_timestamp_end = timestamp;
553 if (asf->packet_timestamp_start == -1)
554 asf->packet_timestamp_start = timestamp;
555 asf->packet_nb_frames++;
556 } else {
557 frag_len = 0;
558 }
559 frag_pos += frag_len;
560 buf += frag_len;
561 /* output the frame if filled */
562 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
563 flush_packet(s);
564 }
565 stream->seq++;
566}
567
568
569static int asf_write_packet(AVFormatContext *s, int stream_index,
10bb7023 570 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
571{
572 ASFContext *asf = s->priv_data;
e0d2714a 573 ASFStream *stream;
de6d9b64
FB
574 int timestamp;
575 INT64 duration;
576 AVCodecContext *codec;
577
f80c1ac0 578 stream = &asf->streams[stream_index];
de6d9b64 579 codec = &s->streams[stream_index]->codec;
e0d2714a
J
580 stream = &asf->streams[stream_index];
581
de6d9b64 582 if (codec->codec_type == CODEC_TYPE_AUDIO) {
e0d2714a 583 timestamp = (int)ticker_tick(&stream->pts_ticker, codec->frame_number);
8be1c656 584 duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
de6d9b64
FB
585 codec->sample_rate;
586 } else {
e0d2714a 587 timestamp = (int)ticker_tick(&stream->pts_ticker, codec->frame_number);
de6d9b64 588 duration = codec->frame_number *
8be1c656 589 ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
de6d9b64
FB
590 }
591 if (duration > asf->duration)
592 asf->duration = duration;
593
e0d2714a 594 put_frame(s, stream, timestamp, buf, size);
de6d9b64
FB
595 return 0;
596}
597
598static int asf_write_trailer(AVFormatContext *s)
599{
600 ASFContext *asf = s->priv_data;
8be1c656 601 INT64 file_size;
de6d9b64
FB
602
603 /* flush the current packet */
604 if (asf->pb.buf_ptr > asf->pb.buffer)
605 flush_packet(s);
606
607 if (url_is_streamed(&s->pb)) {
f80c1ac0 608 put_chunk(s, 0x4524, 0, 0); /* end of stream */
de6d9b64
FB
609 } else {
610 /* rewrite an updated header */
611 file_size = url_ftell(&s->pb);
612 url_fseek(&s->pb, 0, SEEK_SET);
613 asf_write_header1(s, file_size, file_size - asf->data_offset);
614 }
615
616 put_flush_packet(&s->pb);
617
1ea4f593 618 av_free(asf);
de6d9b64
FB
619 return 0;
620}
621
622/**********************************/
623/* decoding */
624
625//#define DEBUG
626
627#ifdef DEBUG
628static void print_guid(const GUID *g)
629{
630 int i;
631 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
632 for(i=0;i<8;i++)
633 printf(" 0x%02x,", g->v4[i]);
634 printf("}\n");
635}
636#endif
637
638static void get_guid(ByteIOContext *s, GUID *g)
639{
640 int i;
641
642 g->v1 = get_le32(s);
643 g->v2 = get_le16(s);
644 g->v3 = get_le16(s);
645 for(i=0;i<8;i++)
646 g->v4[i] = get_byte(s);
647}
648
649#if 0
650static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
651{
652 int len, c;
653 char *q;
654
655 len = get_le16(pb);
656 q = buf;
657 while (len > 0) {
658 c = get_le16(pb);
659 if ((q - buf) < buf_size - 1)
660 *q++ = c;
661 len--;
662 }
663 *q = '\0';
664}
665#endif
666
667static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
668{
669 int c;
670 char *q;
671
672 q = buf;
673 while (len > 0) {
674 c = get_le16(pb);
675 if ((q - buf) < buf_size - 1)
676 *q++ = c;
677 len-=2;
678 }
679 *q = '\0';
680}
681
682static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
683{
684 ASFContext *asf;
685 GUID g;
686 ByteIOContext *pb = &s->pb;
687 AVStream *st;
688 ASFStream *asf_st;
46a3d068 689 int size, i, bps;
de6d9b64
FB
690 INT64 gsize;
691
692 asf = av_mallocz(sizeof(ASFContext));
693 if (!asf)
694 return -1;
695 s->priv_data = asf;
696
697 get_guid(pb, &g);
698 if (memcmp(&g, &asf_header, sizeof(GUID)))
699 goto fail;
700 get_le64(pb);
701 get_le32(pb);
702 get_byte(pb);
703 get_byte(pb);
704
705 for(;;) {
706 get_guid(pb, &g);
707 gsize = get_le64(pb);
708#ifdef DEBUG
709 printf("%08Lx: ", url_ftell(pb) - 24);
710 print_guid(&g);
711 printf(" size=0x%Lx\n", gsize);
712#endif
713 if (gsize < 24)
714 goto fail;
715 if (!memcmp(&g, &file_header, sizeof(GUID))) {
716 get_guid(pb, &g);
717 get_le64(pb); /* file size */
718 get_le64(pb); /* file time */
719 get_le64(pb); /* nb_packets */
720 get_le64(pb); /* length 0 in us */
721 get_le64(pb); /* length 1 in us */
722 get_le32(pb);
723 get_le32(pb);
724 get_le32(pb);
725 asf->packet_size = get_le32(pb);
726 get_le32(pb);
727 get_le32(pb);
728 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
729 int type, id, total_size;
730 unsigned int tag1;
731 INT64 pos1, pos2;
732
733 pos1 = url_ftell(pb);
734
735 st = av_mallocz(sizeof(AVStream));
736 if (!st)
737 goto fail;
738 s->streams[s->nb_streams++] = st;
739 asf_st = av_mallocz(sizeof(ASFStream));
740 if (!asf_st)
741 goto fail;
742 st->priv_data = asf_st;
743
744 get_guid(pb, &g);
745 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
746 type = CODEC_TYPE_AUDIO;
747 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
748 type = CODEC_TYPE_VIDEO;
749 } else {
750 goto fail;
751 }
752 get_guid(pb, &g);
753 total_size = get_le64(pb);
754 get_le32(pb);
755 get_le32(pb);
756 st->id = get_le16(pb); /* stream id */
757 get_le32(pb);
758 st->codec.codec_type = type;
759 if (type == CODEC_TYPE_AUDIO) {
760 id = get_le16(pb);
761 st->codec.codec_tag = id;
de6d9b64
FB
762 st->codec.channels = get_le16(pb);
763 st->codec.sample_rate = get_le32(pb);
764 st->codec.bit_rate = get_le32(pb) * 8;
765 get_le16(pb); /* block align */
46a3d068
FB
766 bps = get_le16(pb); /* bits per sample */
767 st->codec.codec_id = wav_codec_get_id(id, bps);
768 size = get_le16(pb);
de6d9b64 769 url_fskip(pb, size);
f80c1ac0
PG
770 /* We have to init the frame size at some point .... */
771 switch (st->codec.codec_id) {
772 case CODEC_ID_MP3LAME:
773 st->codec.frame_size = MPA_FRAME_SIZE;
774 break;
775 case CODEC_ID_PCM_S16LE:
776 case CODEC_ID_PCM_S16BE:
777 case CODEC_ID_PCM_U16LE:
778 case CODEC_ID_PCM_U16BE:
779 case CODEC_ID_PCM_S8:
780 case CODEC_ID_PCM_U8:
781 case CODEC_ID_PCM_ALAW:
782 case CODEC_ID_PCM_MULAW:
783 st->codec.frame_size = 1;
784 break;
785 default:
786 /* This is probably wrong, but it prevents a crash later */
787 st->codec.frame_size = 1;
788 break;
789 }
de6d9b64
FB
790 } else {
791 get_le32(pb);
792 get_le32(pb);
793 get_byte(pb);
794 size = get_le16(pb); /* size */
795 get_le32(pb); /* size */
796 st->codec.width = get_le32(pb);
797 st->codec.height = get_le32(pb);
798 st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
799 get_le16(pb); /* panes */
800 get_le16(pb); /* depth */
801 tag1 = get_le32(pb);
802 st->codec.codec_tag = tag1;
4606ac8d 803 st->codec.codec_id = codec_get_id(codec_asf_bmp_tags, tag1);
de6d9b64
FB
804 url_fskip(pb, size - 5 * 4);
805 }
806 pos2 = url_ftell(pb);
807 url_fskip(pb, gsize - (pos2 - pos1 + 24));
808 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
809 break;
810 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
811 int len1, len2, len3, len4, len5;
812
813 len1 = get_le16(pb);
814 len2 = get_le16(pb);
815 len3 = get_le16(pb);
816 len4 = get_le16(pb);
817 len5 = get_le16(pb);
818 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
819 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
820 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
821 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
822 url_fskip(pb, len5);
823#if 0
824 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
825 int v1, v2;
826 get_guid(pb, &g);
827 v1 = get_le32(pb);
828 v2 = get_le16(pb);
829 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
830 int len, v1, n, num;
831 char str[256], *q;
832 char tag[16];
833
834 get_guid(pb, &g);
835 print_guid(&g);
836
837 n = get_le32(pb);
838 for(i=0;i<n;i++) {
839 num = get_le16(pb); /* stream number */
840 get_str16(pb, str, sizeof(str));
841 get_str16(pb, str, sizeof(str));
842 len = get_le16(pb);
843 q = tag;
844 while (len > 0) {
845 v1 = get_byte(pb);
846 if ((q - tag) < sizeof(tag) - 1)
847 *q++ = v1;
848 len--;
849 }
850 *q = '\0';
851 }
852#endif
853 } else if (url_feof(pb)) {
854 goto fail;
855 } else {
856 url_fseek(pb, gsize - 24, SEEK_CUR);
857 }
858 }
859 get_guid(pb, &g);
860 get_le64(pb);
861 get_byte(pb);
862 get_byte(pb);
863
864 asf->packet_size_left = 0;
865
866 return 0;
867
868 fail:
869 for(i=0;i<s->nb_streams;i++) {
870 AVStream *st = s->streams[i];
871 if (st)
1ea4f593
FB
872 av_free(st->priv_data);
873 av_free(st);
de6d9b64 874 }
1ea4f593 875 av_free(asf);
de6d9b64
FB
876 return -1;
877}
878
879static int asf_get_packet(AVFormatContext *s)
880{
881 ASFContext *asf = s->priv_data;
882 ByteIOContext *pb = &s->pb;
883 int c, flags, timestamp, hdr_size;
884
885 hdr_size = 12;
886 c = get_byte(pb);
887 if (c != 0x82)
888 return -EIO;
889 get_le16(pb);
890 flags = get_byte(pb);
891 get_byte(pb);
892 asf->packet_padsize = 0;
893 if (flags & 0x10) {
894 asf->packet_padsize = get_le16(pb);
895 hdr_size += 2;
896 } else if (flags & 0x08) {
897 asf->packet_padsize = get_byte(pb);
898 hdr_size++;
899 }
900 timestamp = get_le32(pb);
901 get_le16(pb); /* duration */
902 get_byte(pb); /* nb_frames */
903#ifdef DEBUG
904 printf("packet: size=%d padsize=%d\n", asf->packet_size, asf->packet_padsize);
905#endif
906 asf->packet_size_left = asf->packet_size - hdr_size;
907 return 0;
908}
909
910static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
911{
912 ASFContext *asf = s->priv_data;
913 AVStream *st;
914 ASFStream *asf_st;
915 ByteIOContext *pb = &s->pb;
916 int ret, num, seq, frag_offset, payload_size, frag_len;
f80c1ac0 917 int key_frame;
de6d9b64
FB
918 int timestamp, i;
919
920 for(;;) {
921 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
922 asf->packet_size_left <= asf->packet_padsize) {
923 /* fail safe */
924 if (asf->packet_size_left)
925 url_fskip(pb, asf->packet_size_left);
926 ret = asf_get_packet(s);
927 if (ret < 0)
928 return -EIO;
929 }
930 /* read frame header */
f80c1ac0
PG
931 num = get_byte(pb);
932 if (num & 0x80)
933 key_frame = 1;
934 else
935 key_frame = 0;
936
937 num &= 0x7f;
de6d9b64
FB
938 seq = get_byte(pb);
939 frag_offset = get_le32(pb);
940 get_byte(pb); /* flags */
941 payload_size = get_le32(pb);
942 timestamp = get_le32(pb);
943 frag_len = get_le16(pb);
944#ifdef DEBUG
945 printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
946 num, seq, payload_size, frag_offset, frag_len);
947#endif
948 st = NULL;
949 for(i=0;i<s->nb_streams;i++) {
950 st = s->streams[i];
951 if (st->id == num)
952 break;
953 }
954 asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len;
955 if (i == s->nb_streams) {
956 /* unhandled packet (should not happen) */
957 url_fskip(pb, frag_len);
958 } else {
959 asf_st = st->priv_data;
960 if (asf_st->frag_offset == 0) {
961 /* new packet */
962 av_new_packet(&asf_st->pkt, payload_size);
963 asf_st->seq = seq;
f80c1ac0
PG
964 if (key_frame)
965 asf_st->pkt.flags |= PKT_FLAG_KEY;
966
967 asf_st->pkt.pts = timestamp;
de6d9b64
FB
968 } else {
969 if (seq == asf_st->seq &&
970 frag_offset == asf_st->frag_offset) {
971 /* continuing packet */
972 } else {
973 /* cannot continue current packet: free it */
974 av_free_packet(&asf_st->pkt);
975 asf_st->frag_offset = 0;
976 if (frag_offset != 0) {
977 /* cannot create new packet */
978 url_fskip(pb, frag_len);
979 goto next_frame;
980 } else {
981 /* create new packet */
982 av_new_packet(&asf_st->pkt, payload_size);
983 asf_st->seq = seq;
984 }
985 }
986 }
987 /* read data */
988 get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len);
989 asf_st->frag_offset += frag_len;
990 /* test if whole packet read */
991 if (asf_st->frag_offset == asf_st->pkt.size) {
992 /* return packet */
993 asf_st->pkt.stream_index = i;
994 asf_st->frag_offset = 0;
995 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
996 break;
997 }
998 }
8be1c656 999 next_frame:;
de6d9b64
FB
1000 }
1001
1002 return 0;
1003}
1004
1005static int asf_read_close(AVFormatContext *s)
1006{
1007 ASFContext *asf = s->priv_data;
1008 int i;
1009
1010 for(i=0;i<s->nb_streams;i++) {
1011 AVStream *st = s->streams[i];
1ea4f593 1012 av_free(st->priv_data);
de6d9b64 1013 }
1ea4f593 1014 av_free(asf);
de6d9b64
FB
1015 return 0;
1016}
1017
1018AVFormat asf_format = {
1019 "asf",
1020 "asf format",
1021 "application/octet-stream",
a56c66a7 1022 "asf,wmv",
4606ac8d
ZK
1023#ifdef CONFIG_MP3LAME
1024 CODEC_ID_MP3LAME,
1025#else
de6d9b64 1026 CODEC_ID_MP2,
4606ac8d 1027#endif
de6d9b64
FB
1028 CODEC_ID_MSMPEG4,
1029 asf_write_header,
1030 asf_write_packet,
1031 asf_write_trailer,
1032
1033 asf_read_header,
1034 asf_read_packet,
1035 asf_read_close,
1036};