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