dont calculate stuff if its not used ...
[libav.git] / libavformat / 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"
f80c1ac0 21#include "mpegaudio.h"
de6d9b64
FB
22
23#define PACKET_SIZE 3200
24#define PACKET_HEADER_SIZE 12
25#define FRAME_HEADER_SIZE 17
26
27typedef struct {
28 int num;
29 int seq;
30 /* use for reading */
31 AVPacket pkt;
32 int frag_offset;
2a10020b 33 int timestamp;
0c1a9eda 34 int64_t duration;
2a10020b
ZK
35
36 int ds_span; /* descrambling */
37 int ds_packet_size;
38 int ds_chunk_size;
39 int ds_data_size;
40 int ds_silence_data;
41
de6d9b64
FB
42} ASFStream;
43
44typedef struct {
0c1a9eda
ZK
45 uint32_t v1;
46 uint16_t v2;
47 uint16_t v3;
48 uint8_t v4[8];
2a10020b
ZK
49} GUID;
50
247eadca 51typedef struct {
2a10020b
ZK
52 GUID guid; // generated by client computer
53 uint64_t file_size; // in bytes
54 // invalid if broadcasting
55 uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601
56 // invalid if broadcasting
57 uint64_t packets_count; // how many packets are there in the file
58 // invalid if broadcasting
59 uint64_t play_time; // play time, in 100-nanosecond units
60 // invalid if broadcasting
61 uint64_t send_time; // time to send file, in 100-nanosecond units
62 // invalid if broadcasting (could be ignored)
63 uint32_t preroll; // timestamp of the first packet, in milliseconds
64 // if nonzero - substract from time
65 uint32_t ignore; // preroll is 64bit - but let's just ignore it
66 uint32_t flags; // 0x01 - broadcast
67 // 0x02 - seekable
68 // rest is reserved should be 0
69 uint32_t min_pktsize; // size of a data packet
70 // invalid if broadcasting
71 uint32_t max_pktsize; // shall be the same as for min_pktsize
72 // invalid if broadcasting
73 uint32_t max_bitrate; // bandwith of stream in bps
74 // should be the sum of bitrates of the
75 // individual media streams
76} ASFMainHeader;
77
78
79typedef struct {
de6d9b64
FB
80 int seqno;
81 int packet_size;
8b3c13f9 82 int is_streamed;
2141dc37
ZK
83 int asfid2avid[128]; /* conversion table from asf ID 2 AVStream ID */
84 ASFStream streams[128]; /* it's max number and it's not that big */
de6d9b64 85 /* non streamed additonnal info */
0c1a9eda
ZK
86 int64_t nb_packets;
87 int64_t duration; /* in 100ns units */
de6d9b64
FB
88 /* packet filling */
89 int packet_size_left;
90 int packet_timestamp_start;
91 int packet_timestamp_end;
92 int packet_nb_frames;
0c1a9eda 93 uint8_t packet_buf[PACKET_SIZE];
de6d9b64
FB
94 ByteIOContext pb;
95 /* only for reading */
2a10020b
ZK
96 uint64_t data_offset; /* begining of the first data packet */
97
98 ASFMainHeader hdr;
99
100 int packet_flags;
101 int packet_property;
102 int packet_timestamp;
103 int packet_segsizetype;
104 int packet_segments;
105 int packet_seq;
106 int packet_replic_size;
107 int packet_key_frame;
de6d9b64 108 int packet_padsize;
2a10020b
ZK
109 int packet_frag_offset;
110 int packet_frag_size;
111 int packet_frag_timestamp;
2141dc37 112 int packet_multi_size;
2a10020b
ZK
113 int packet_obj_size;
114 int packet_time_delta;
115 int packet_time_start;
116
117 int stream_index;
118 ASFStream* asf_st; /* currently decoded stream */
de6d9b64
FB
119} ASFContext;
120
de6d9b64
FB
121static const GUID asf_header = {
122 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
123};
124
125static const GUID file_header = {
126 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
127};
128
129static const GUID stream_header = {
130 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
131};
132
133static const GUID audio_stream = {
134 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
135};
136
137static const GUID audio_conceal_none = {
f80c1ac0
PG
138 // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
139 // New value lifted from avifile
140 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b },
de6d9b64
FB
141};
142
143static const GUID video_stream = {
144 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
145};
146
147static const GUID video_conceal_none = {
148 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
149};
150
151
152static const GUID comment_header = {
153 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
154};
155
156static const GUID codec_comment_header = {
157 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
158};
159static const GUID codec_comment1_header = {
160 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
161};
162
163static const GUID data_header = {
164 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
165};
166
167static const GUID index_guid = {
168 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
169};
170
171static const GUID head1_guid = {
172 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
173};
174
175static const GUID head2_guid = {
176 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
177};
2a10020b 178
d13431cd
KK
179static const GUID extended_content_header = {
180 0xD2D0A440, 0xE307, 0x11D2, { 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50 },
181};
182
de6d9b64
FB
183/* I am not a number !!! This GUID is the one found on the PC used to
184 generate the stream */
185static const GUID my_guid = {
186 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
187};
188
764ef400 189#ifdef CONFIG_ENCODERS
de6d9b64
FB
190static void put_guid(ByteIOContext *s, const GUID *g)
191{
192 int i;
193
194 put_le32(s, g->v1);
195 put_le16(s, g->v2);
196 put_le16(s, g->v3);
197 for(i=0;i<8;i++)
198 put_byte(s, g->v4[i]);
199}
200
201static void put_str16(ByteIOContext *s, const char *tag)
202{
203 int c;
204
205 put_le16(s,strlen(tag) + 1);
206 for(;;) {
0c1a9eda 207 c = (uint8_t)*tag++;
de6d9b64 208 put_le16(s, c);
2a10020b 209 if (c == '\0')
de6d9b64
FB
210 break;
211 }
212}
213
214static void put_str16_nolen(ByteIOContext *s, const char *tag)
215{
216 int c;
217
218 for(;;) {
0c1a9eda 219 c = (uint8_t)*tag++;
de6d9b64 220 put_le16(s, c);
2a10020b 221 if (c == '\0')
de6d9b64
FB
222 break;
223 }
224}
225
0c1a9eda 226static int64_t put_header(ByteIOContext *pb, const GUID *g)
de6d9b64 227{
0c1a9eda 228 int64_t pos;
de6d9b64
FB
229
230 pos = url_ftell(pb);
231 put_guid(pb, g);
232 put_le64(pb, 24);
233 return pos;
234}
235
236/* update header size */
0c1a9eda 237static void end_header(ByteIOContext *pb, int64_t pos)
de6d9b64 238{
0c1a9eda 239 int64_t pos1;
de6d9b64
FB
240
241 pos1 = url_ftell(pb);
242 url_fseek(pb, pos + 16, SEEK_SET);
243 put_le64(pb, pos1 - pos);
244 url_fseek(pb, pos1, SEEK_SET);
245}
246
247/* write an asf chunk (only used in streaming case) */
f80c1ac0 248static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
de6d9b64
FB
249{
250 ASFContext *asf = s->priv_data;
251 ByteIOContext *pb = &s->pb;
252 int length;
253
254 length = payload_length + 8;
2a10020b
ZK
255 put_le16(pb, type);
256 put_le16(pb, length);
de6d9b64 257 put_le32(pb, asf->seqno);
f80c1ac0 258 put_le16(pb, flags); /* unknown bytes */
de6d9b64
FB
259 put_le16(pb, length);
260 asf->seqno++;
261}
262
263/* convert from unix to windows time */
0c1a9eda 264static int64_t unix_to_file_time(int ti)
de6d9b64 265{
0c1a9eda 266 int64_t t;
2a10020b 267
0c1a9eda
ZK
268 t = ti * int64_t_C(10000000);
269 t += int64_t_C(116444736000000000);
de6d9b64
FB
270 return t;
271}
272
273/* write the header (used two times if non streamed) */
0c1a9eda 274static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
de6d9b64
FB
275{
276 ASFContext *asf = s->priv_data;
277 ByteIOContext *pb = &s->pb;
278 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
279 int has_title;
280 AVCodecContext *enc;
0c1a9eda 281 int64_t header_offset, cur_pos, hpos;
f80c1ac0 282 int bit_rate;
de6d9b64 283
084fada8 284 has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
de6d9b64 285
f80c1ac0
PG
286 bit_rate = 0;
287 for(n=0;n<s->nb_streams;n++) {
288 enc = &s->streams[n]->codec;
289
290 bit_rate += enc->bit_rate;
291 }
292
8b3c13f9 293 if (asf->is_streamed) {
f80c1ac0 294 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
de6d9b64 295 }
f80c1ac0
PG
296
297 put_guid(pb, &asf_header);
2a10020b 298 put_le64(pb, -1); /* header length, will be patched after */
f80c1ac0
PG
299 put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
300 put_byte(pb, 1); /* ??? */
301 put_byte(pb, 2); /* ??? */
2a10020b 302
de6d9b64
FB
303 /* file header */
304 header_offset = url_ftell(pb);
305 hpos = put_header(pb, &file_header);
306 put_guid(pb, &my_guid);
307 put_le64(pb, file_size);
308 file_time = 0;
309 put_le64(pb, unix_to_file_time(file_time));
310 put_le64(pb, asf->nb_packets); /* number of packets */
311 put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
312 put_le64(pb, asf->duration); /* duration (in 100ns units) */
2a10020b
ZK
313 put_le32(pb, 0); /* start time stamp */
314 put_le32(pb, 0); /* ??? */
315 put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
de6d9b64
FB
316 put_le32(pb, asf->packet_size); /* packet size */
317 put_le32(pb, asf->packet_size); /* packet size */
f80c1ac0 318 put_le32(pb, bit_rate); /* Nominal data rate in bps */
de6d9b64
FB
319 end_header(pb, hpos);
320
321 /* unknown headers */
322 hpos = put_header(pb, &head1_guid);
323 put_guid(pb, &head2_guid);
324 put_le32(pb, 6);
325 put_le16(pb, 0);
326 end_header(pb, hpos);
327
328 /* title and other infos */
329 if (has_title) {
330 hpos = put_header(pb, &comment_header);
331 put_le16(pb, 2 * (strlen(s->title) + 1));
332 put_le16(pb, 2 * (strlen(s->author) + 1));
333 put_le16(pb, 2 * (strlen(s->copyright) + 1));
334 put_le16(pb, 2 * (strlen(s->comment) + 1));
335 put_le16(pb, 0);
336 put_str16_nolen(pb, s->title);
337 put_str16_nolen(pb, s->author);
338 put_str16_nolen(pb, s->copyright);
339 put_str16_nolen(pb, s->comment);
340 end_header(pb, hpos);
341 }
342
343 /* stream headers */
344 for(n=0;n<s->nb_streams;n++) {
0c1a9eda 345 int64_t es_pos;
17a241dd 346 // ASFStream *stream = &asf->streams[n];
f80c1ac0 347
de6d9b64
FB
348 enc = &s->streams[n]->codec;
349 asf->streams[n].num = n + 1;
350 asf->streams[n].seq = 0;
2a10020b 351
de6d9b64
FB
352 switch(enc->codec_type) {
353 case CODEC_TYPE_AUDIO:
354 wav_extra_size = 0;
355 extra_size = 18 + wav_extra_size;
356 extra_size2 = 0;
357 break;
358 default:
359 case CODEC_TYPE_VIDEO:
360 wav_extra_size = 0;
361 extra_size = 0x33;
362 extra_size2 = 0;
363 break;
364 }
365
366 hpos = put_header(pb, &stream_header);
367 if (enc->codec_type == CODEC_TYPE_AUDIO) {
368 put_guid(pb, &audio_stream);
369 put_guid(pb, &audio_conceal_none);
370 } else {
371 put_guid(pb, &video_stream);
372 put_guid(pb, &video_conceal_none);
373 }
374 put_le64(pb, 0); /* ??? */
f80c1ac0 375 es_pos = url_ftell(pb);
de6d9b64
FB
376 put_le32(pb, extra_size); /* wav header len */
377 put_le32(pb, extra_size2); /* additional data len */
378 put_le16(pb, n + 1); /* stream number */
379 put_le32(pb, 0); /* ??? */
2a10020b 380
de6d9b64
FB
381 if (enc->codec_type == CODEC_TYPE_AUDIO) {
382 /* WAVEFORMATEX header */
f80c1ac0
PG
383 int wavsize = put_wav_header(pb, enc);
384
385 if (wavsize < 0)
46a3d068 386 return -1;
f80c1ac0
PG
387 if (wavsize != extra_size) {
388 cur_pos = url_ftell(pb);
389 url_fseek(pb, es_pos, SEEK_SET);
390 put_le32(pb, wavsize); /* wav header len */
391 url_fseek(pb, cur_pos, SEEK_SET);
392 }
de6d9b64
FB
393 } else {
394 put_le32(pb, enc->width);
395 put_le32(pb, enc->height);
396 put_byte(pb, 2); /* ??? */
397 put_le16(pb, 40); /* size */
398
399 /* BITMAPINFOHEADER header */
2727c35e 400 put_bmp_header(pb, enc, codec_bmp_tags, 1);
de6d9b64
FB
401 }
402 end_header(pb, hpos);
403 }
404
405 /* media comments */
406
407 hpos = put_header(pb, &codec_comment_header);
408 put_guid(pb, &codec_comment1_header);
409 put_le32(pb, s->nb_streams);
410 for(n=0;n<s->nb_streams;n++) {
084fada8
PG
411 AVCodec *p;
412
de6d9b64 413 enc = &s->streams[n]->codec;
084fada8 414 p = avcodec_find_encoder(enc->codec_id);
de6d9b64
FB
415
416 put_le16(pb, asf->streams[n].num);
084fada8 417 put_str16(pb, p ? p->name : enc->codec_name);
de6d9b64 418 put_le16(pb, 0); /* no parameters */
bd5a6020
MN
419
420
de6d9b64
FB
421 /* id */
422 if (enc->codec_type == CODEC_TYPE_AUDIO) {
423 put_le16(pb, 2);
bd5a6020
MN
424 if(!enc->codec_tag)
425 enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
426 if(!enc->codec_tag)
427 return -1;
428 put_le16(pb, enc->codec_tag);
de6d9b64
FB
429 } else {
430 put_le16(pb, 4);
bd5a6020
MN
431 if(!enc->codec_tag)
432 enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id);
433 if(!enc->codec_tag)
434 return -1;
435 put_le32(pb, enc->codec_tag);
de6d9b64
FB
436 }
437 }
438 end_header(pb, hpos);
439
440 /* patch the header size fields */
441
442 cur_pos = url_ftell(pb);
443 header_size = cur_pos - header_offset;
8b3c13f9 444 if (asf->is_streamed) {
f80c1ac0
PG
445 header_size += 8 + 30 + 50;
446
447 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
de6d9b64 448 put_le16(pb, header_size);
f80c1ac0 449 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
de6d9b64 450 put_le16(pb, header_size);
f80c1ac0
PG
451
452 header_size -= 8 + 30 + 50;
de6d9b64 453 }
f80c1ac0
PG
454 header_size += 24 + 6;
455 url_fseek(pb, header_offset - 14, SEEK_SET);
456 put_le64(pb, header_size);
de6d9b64
FB
457 url_fseek(pb, cur_pos, SEEK_SET);
458
459 /* movie chunk, followed by packets of packet_size */
460 asf->data_offset = cur_pos;
461 put_guid(pb, &data_header);
462 put_le64(pb, data_chunk_size);
463 put_guid(pb, &my_guid);
464 put_le64(pb, asf->nb_packets); /* nb packets */
465 put_byte(pb, 1); /* ??? */
466 put_byte(pb, 1); /* ??? */
467 return 0;
468}
469
470static int asf_write_header(AVFormatContext *s)
471{
c9a65ca8 472 ASFContext *asf = s->priv_data;
de6d9b64 473
17a241dd
FB
474 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
475
de6d9b64
FB
476 asf->packet_size = PACKET_SIZE;
477 asf->nb_packets = 0;
478
f80c1ac0 479 if (asf_write_header1(s, 0, 50) < 0) {
2824c473 480 //av_free(asf);
46a3d068
FB
481 return -1;
482 }
de6d9b64
FB
483
484 put_flush_packet(&s->pb);
485
486 asf->packet_nb_frames = 0;
487 asf->packet_timestamp_start = -1;
488 asf->packet_timestamp_end = -1;
489 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
490 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
491 NULL, NULL, NULL, NULL);
492
493 return 0;
494}
495
8b3c13f9
PG
496static int asf_write_stream_header(AVFormatContext *s)
497{
498 ASFContext *asf = s->priv_data;
499
500 asf->is_streamed = 1;
501
502 return asf_write_header(s);
503}
504
de6d9b64 505/* write a fixed size packet */
2a10020b
ZK
506static int put_packet(AVFormatContext *s,
507 unsigned int timestamp, unsigned int duration,
de6d9b64
FB
508 int nb_frames, int padsize)
509{
510 ASFContext *asf = s->priv_data;
511 ByteIOContext *pb = &s->pb;
512 int flags;
513
8b3c13f9 514 if (asf->is_streamed) {
f80c1ac0 515 put_chunk(s, 0x4424, asf->packet_size, 0);
de6d9b64
FB
516 }
517
518 put_byte(pb, 0x82);
519 put_le16(pb, 0);
2a10020b 520
de6d9b64
FB
521 flags = 0x01; /* nb segments present */
522 if (padsize > 0) {
523 if (padsize < 256)
524 flags |= 0x08;
525 else
526 flags |= 0x10;
527 }
528 put_byte(pb, flags); /* flags */
529 put_byte(pb, 0x5d);
530 if (flags & 0x10)
f80c1ac0 531 put_le16(pb, padsize - 2);
de6d9b64 532 if (flags & 0x08)
f80c1ac0 533 put_byte(pb, padsize - 1);
de6d9b64
FB
534 put_le32(pb, timestamp);
535 put_le16(pb, duration);
536 put_byte(pb, nb_frames | 0x80);
f80c1ac0
PG
537
538 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
de6d9b64
FB
539}
540
541static void flush_packet(AVFormatContext *s)
542{
543 ASFContext *asf = s->priv_data;
544 int hdr_size, ptr;
2a10020b
ZK
545
546 hdr_size = put_packet(s, asf->packet_timestamp_start,
547 asf->packet_timestamp_end - asf->packet_timestamp_start,
de6d9b64 548 asf->packet_nb_frames, asf->packet_size_left);
2a10020b 549
f80c1ac0
PG
550 /* Clear out the padding bytes */
551 ptr = asf->packet_size - hdr_size - asf->packet_size_left;
de6d9b64 552 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
2a10020b 553
de6d9b64 554 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
2a10020b 555
de6d9b64
FB
556 put_flush_packet(&s->pb);
557 asf->nb_packets++;
558 asf->packet_nb_frames = 0;
559 asf->packet_timestamp_start = -1;
560 asf->packet_timestamp_end = -1;
561 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
562 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
563 NULL, NULL, NULL, NULL);
564}
565
566static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
567 int payload_size, int frag_offset, int frag_len)
568{
569 ASFContext *asf = s->priv_data;
570 ByteIOContext *pb = &asf->pb;
571 int val;
572
573 val = stream->num;
492cd3a9 574 if (s->streams[val - 1]->codec.coded_frame->key_frame /* && frag_offset == 0 */)
de6d9b64
FB
575 val |= 0x80;
576 put_byte(pb, val);
577 put_byte(pb, stream->seq);
578 put_le32(pb, frag_offset); /* fragment offset */
579 put_byte(pb, 0x08); /* flags */
580 put_le32(pb, payload_size);
581 put_le32(pb, timestamp);
582 put_le16(pb, frag_len);
583}
584
585
586/* Output a frame. We suppose that payload_size <= PACKET_SIZE.
587
588 It is there that you understand that the ASF format is really
2a10020b 589 crap. They have misread the MPEG Systems spec !
de6d9b64
FB
590 */
591static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
49057904 592 const uint8_t *buf, int payload_size)
de6d9b64
FB
593{
594 ASFContext *asf = s->priv_data;
595 int frag_pos, frag_len, frag_len1;
2a10020b 596
de6d9b64
FB
597 frag_pos = 0;
598 while (frag_pos < payload_size) {
599 frag_len = payload_size - frag_pos;
600 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
601 if (frag_len1 > 0) {
602 if (frag_len > frag_len1)
603 frag_len = frag_len1;
b13a517f 604 put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len);
de6d9b64
FB
605 put_buffer(&asf->pb, buf, frag_len);
606 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
607 asf->packet_timestamp_end = timestamp;
608 if (asf->packet_timestamp_start == -1)
609 asf->packet_timestamp_start = timestamp;
610 asf->packet_nb_frames++;
611 } else {
612 frag_len = 0;
613 }
614 frag_pos += frag_len;
615 buf += frag_len;
616 /* output the frame if filled */
617 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
618 flush_packet(s);
619 }
620 stream->seq++;
621}
622
623
624static int asf_write_packet(AVFormatContext *s, int stream_index,
49057904 625 const uint8_t *buf, int size, int64_t timestamp)
de6d9b64
FB
626{
627 ASFContext *asf = s->priv_data;
e0d2714a 628 ASFStream *stream;
0c1a9eda 629 int64_t duration;
de6d9b64
FB
630 AVCodecContext *codec;
631
632 codec = &s->streams[stream_index]->codec;
e0d2714a 633 stream = &asf->streams[stream_index];
2a10020b 634
de6d9b64 635 if (codec->codec_type == CODEC_TYPE_AUDIO) {
0c1a9eda 636 duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) /
de6d9b64
FB
637 codec->sample_rate;
638 } else {
14bea432 639 duration = av_rescale(codec->frame_number * codec->frame_rate_base, 10000000, codec->frame_rate);
de6d9b64
FB
640 }
641 if (duration > asf->duration)
642 asf->duration = duration;
2a10020b 643
e0d2714a 644 put_frame(s, stream, timestamp, buf, size);
de6d9b64
FB
645 return 0;
646}
2a10020b 647
de6d9b64
FB
648static int asf_write_trailer(AVFormatContext *s)
649{
650 ASFContext *asf = s->priv_data;
0c1a9eda 651 int64_t file_size;
de6d9b64
FB
652
653 /* flush the current packet */
654 if (asf->pb.buf_ptr > asf->pb.buffer)
655 flush_packet(s);
656
8b3c13f9 657 if (asf->is_streamed) {
f80c1ac0 658 put_chunk(s, 0x4524, 0, 0); /* end of stream */
de6d9b64
FB
659 } else {
660 /* rewrite an updated header */
661 file_size = url_ftell(&s->pb);
662 url_fseek(&s->pb, 0, SEEK_SET);
663 asf_write_header1(s, file_size, file_size - asf->data_offset);
664 }
665
666 put_flush_packet(&s->pb);
de6d9b64
FB
667 return 0;
668}
764ef400 669#endif //CONFIG_ENCODERS
de6d9b64
FB
670
671/**********************************/
672/* decoding */
673
674//#define DEBUG
675
676#ifdef DEBUG
5082f759
FR
677#define PRINT_IF_GUID(g,cmp) \
678if (!memcmp(g, &cmp, sizeof(GUID))) \
679 printf("(GUID: %s) ", #cmp)
680
de6d9b64
FB
681static void print_guid(const GUID *g)
682{
683 int i;
5082f759
FR
684 PRINT_IF_GUID(g, asf_header);
685 else PRINT_IF_GUID(g, file_header);
686 else PRINT_IF_GUID(g, stream_header);
687 else PRINT_IF_GUID(g, audio_stream);
688 else PRINT_IF_GUID(g, audio_conceal_none);
689 else PRINT_IF_GUID(g, video_stream);
690 else PRINT_IF_GUID(g, video_conceal_none);
691 else PRINT_IF_GUID(g, comment_header);
692 else PRINT_IF_GUID(g, codec_comment_header);
693 else PRINT_IF_GUID(g, codec_comment1_header);
694 else PRINT_IF_GUID(g, data_header);
695 else PRINT_IF_GUID(g, index_guid);
696 else PRINT_IF_GUID(g, head1_guid);
697 else PRINT_IF_GUID(g, head2_guid);
698 else PRINT_IF_GUID(g, my_guid);
699 else
700 printf("(GUID: unknown) ");
de6d9b64 701 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
2a10020b 702 for(i=0;i<8;i++)
de6d9b64
FB
703 printf(" 0x%02x,", g->v4[i]);
704 printf("}\n");
705}
5082f759 706#undef PRINT_IF_GUID(g,cmp)
de6d9b64
FB
707#endif
708
709static void get_guid(ByteIOContext *s, GUID *g)
710{
711 int i;
712
713 g->v1 = get_le32(s);
714 g->v2 = get_le16(s);
715 g->v3 = get_le16(s);
716 for(i=0;i<8;i++)
717 g->v4[i] = get_byte(s);
718}
719
720#if 0
721static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
722{
723 int len, c;
724 char *q;
725
726 len = get_le16(pb);
727 q = buf;
728 while (len > 0) {
729 c = get_le16(pb);
730 if ((q - buf) < buf_size - 1)
731 *q++ = c;
732 len--;
733 }
734 *q = '\0';
735}
736#endif
737
738static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
739{
740 int c;
741 char *q;
742
743 q = buf;
744 while (len > 0) {
745 c = get_le16(pb);
746 if ((q - buf) < buf_size - 1)
747 *q++ = c;
748 len-=2;
749 }
750 *q = '\0';
751}
752
c9a65ca8
FB
753static int asf_probe(AVProbeData *pd)
754{
755 GUID g;
756 const unsigned char *p;
757 int i;
758
759 /* check file header */
760 if (pd->buf_size <= 32)
761 return 0;
762 p = pd->buf;
763 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
764 p += 4;
765 g.v2 = p[0] | (p[1] << 8);
766 p += 2;
767 g.v3 = p[0] | (p[1] << 8);
768 p += 2;
769 for(i=0;i<8;i++)
770 g.v4[i] = *p++;
771
772 if (!memcmp(&g, &asf_header, sizeof(GUID)))
773 return AVPROBE_SCORE_MAX;
774 else
775 return 0;
776}
777
de6d9b64
FB
778static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
779{
c9a65ca8 780 ASFContext *asf = s->priv_data;
de6d9b64
FB
781 GUID g;
782 ByteIOContext *pb = &s->pb;
783 AVStream *st;
784 ASFStream *asf_st;
e095026a 785 int size, i;
0c1a9eda 786 int64_t gsize;
de6d9b64 787
17a241dd
FB
788 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
789
de6d9b64
FB
790 get_guid(pb, &g);
791 if (memcmp(&g, &asf_header, sizeof(GUID)))
792 goto fail;
793 get_le64(pb);
794 get_le32(pb);
795 get_byte(pb);
796 get_byte(pb);
2141dc37 797 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
de6d9b64
FB
798 for(;;) {
799 get_guid(pb, &g);
800 gsize = get_le64(pb);
801#ifdef DEBUG
802 printf("%08Lx: ", url_ftell(pb) - 24);
803 print_guid(&g);
804 printf(" size=0x%Lx\n", gsize);
805#endif
806 if (gsize < 24)
807 goto fail;
808 if (!memcmp(&g, &file_header, sizeof(GUID))) {
2a10020b
ZK
809 get_guid(pb, &asf->hdr.guid);
810 asf->hdr.file_size = get_le64(pb);
811 asf->hdr.create_time = get_le64(pb);
812 asf->hdr.packets_count = get_le64(pb);
813 asf->hdr.play_time = get_le64(pb);
814 asf->hdr.send_time = get_le64(pb);
815 asf->hdr.preroll = get_le32(pb);
816 asf->hdr.ignore = get_le32(pb);
817 asf->hdr.flags = get_le32(pb);
818 asf->hdr.min_pktsize = get_le32(pb);
819 asf->hdr.max_pktsize = get_le32(pb);
820 asf->hdr.max_bitrate = get_le32(pb);
2141dc37
ZK
821 asf->packet_size = asf->hdr.max_pktsize;
822 asf->nb_packets = asf->hdr.packets_count;
de6d9b64 823 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
2e7973bb 824 int type, total_size, type_specific_size;
de6d9b64 825 unsigned int tag1;
0c1a9eda 826 int64_t pos1, pos2;
2a10020b 827
de6d9b64
FB
828 pos1 = url_ftell(pb);
829
247eadca 830 st = av_new_stream(s, 0);
de6d9b64
FB
831 if (!st)
832 goto fail;
de6d9b64
FB
833 asf_st = av_mallocz(sizeof(ASFStream));
834 if (!asf_st)
835 goto fail;
836 st->priv_data = asf_st;
247eadca
FB
837 st->start_time = asf->hdr.preroll / (10000000 / AV_TIME_BASE);
838 st->duration = (asf->hdr.send_time - asf->hdr.preroll) /
839 (10000000 / AV_TIME_BASE);
de6d9b64
FB
840 get_guid(pb, &g);
841 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
842 type = CODEC_TYPE_AUDIO;
843 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
844 type = CODEC_TYPE_VIDEO;
845 } else {
846 goto fail;
847 }
848 get_guid(pb, &g);
849 total_size = get_le64(pb);
2e7973bb 850 type_specific_size = get_le32(pb);
de6d9b64 851 get_le32(pb);
2a10020b
ZK
852 st->id = get_le16(pb) & 0x7f; /* stream id */
853 // mapping of asf ID to AV stream ID;
247eadca 854 asf->asfid2avid[st->id] = s->nb_streams - 1;
2a10020b 855
de6d9b64 856 get_le32(pb);
2141dc37 857 st->codec.codec_type = type;
afda223c
FB
858 /* 1 fps default (XXX: put 0 fps instead) */
859 st->codec.frame_rate = 1;
860 st->codec.frame_rate_base = 1;
de6d9b64 861 if (type == CODEC_TYPE_AUDIO) {
2e7973bb 862 get_wav_header(pb, &st->codec, type_specific_size);
afda223c 863 st->need_parsing = 1;
2a10020b
ZK
864 /* We have to init the frame size at some point .... */
865 pos2 = url_ftell(pb);
2141dc37 866 if (gsize > (pos2 + 8 - pos1 + 24)) {
2a10020b
ZK
867 asf_st->ds_span = get_byte(pb);
868 asf_st->ds_packet_size = get_le16(pb);
869 asf_st->ds_chunk_size = get_le16(pb);
870 asf_st->ds_data_size = get_le16(pb);
871 asf_st->ds_silence_data = get_byte(pb);
872 }
873 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
874 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
875 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
2141dc37 876 if (asf_st->ds_span > 1) {
2a10020b
ZK
877 if (!asf_st->ds_chunk_size
878 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
879 asf_st->ds_span = 0; // disable descrambling
880 }
f80c1ac0 881 switch (st->codec.codec_id) {
80783dc2 882 case CODEC_ID_MP3:
f80c1ac0
PG
883 st->codec.frame_size = MPA_FRAME_SIZE;
884 break;
885 case CODEC_ID_PCM_S16LE:
886 case CODEC_ID_PCM_S16BE:
887 case CODEC_ID_PCM_U16LE:
888 case CODEC_ID_PCM_U16BE:
889 case CODEC_ID_PCM_S8:
890 case CODEC_ID_PCM_U8:
891 case CODEC_ID_PCM_ALAW:
892 case CODEC_ID_PCM_MULAW:
893 st->codec.frame_size = 1;
894 break;
895 default:
896 /* This is probably wrong, but it prevents a crash later */
897 st->codec.frame_size = 1;
898 break;
899 }
de6d9b64 900 } else {
2141dc37 901 get_le32(pb);
de6d9b64
FB
902 get_le32(pb);
903 get_byte(pb);
904 size = get_le16(pb); /* size */
905 get_le32(pb); /* size */
906 st->codec.width = get_le32(pb);
2a10020b
ZK
907 st->codec.height = get_le32(pb);
908 /* not available for asf */
de6d9b64 909 get_le16(pb); /* panes */
693feac5 910 st->codec.bits_per_sample = get_le16(pb); /* depth */
de6d9b64 911 tag1 = get_le32(pb);
2141dc37
ZK
912 url_fskip(pb, 20);
913 if (size > 40) {
e40fed0b
ZK
914 st->codec.extradata_size = size - 40;
915 st->codec.extradata = av_mallocz(st->codec.extradata_size);
916 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
2141dc37 917 }
5e29abf8
RT
918
919 /* Extract palette from extradata if bpp <= 8 */
920 /* This code assumes that extradata contains only palette */
921 /* This is true for all paletted codecs implemented in ffmpeg */
922 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
923 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
924#ifdef WORDS_BIGENDIAN
19d053c5
RS
925 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
926 st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
5e29abf8
RT
927#else
928 memcpy(st->codec.palctrl->palette, st->codec.extradata,
929 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
930#endif
931 st->codec.palctrl->palette_changed = 1;
932 }
933
7004ffb3 934 st->codec.codec_tag = tag1;
17a241dd 935 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
de6d9b64
FB
936 }
937 pos2 = url_ftell(pb);
938 url_fskip(pb, gsize - (pos2 - pos1 + 24));
939 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
940 break;
941 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
942 int len1, len2, len3, len4, len5;
943
944 len1 = get_le16(pb);
945 len2 = get_le16(pb);
946 len3 = get_le16(pb);
947 len4 = get_le16(pb);
948 len5 = get_le16(pb);
949 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
950 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
951 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
952 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
2141dc37 953 url_fskip(pb, len5);
d13431cd
KK
954 } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) {
955 int desc_count, i;
956
957 desc_count = get_le16(pb);
958 for(i=0;i<desc_count;i++)
959 {
960 int name_len,value_type,value_len,value_num = 0;
961 char *name, *value;
962
963 name_len = get_le16(pb);
964 name = (char *)av_mallocz(name_len);
965 get_str16_nolen(pb, name_len, name, name_len);
966 value_type = get_le16(pb);
967 value_len = get_le16(pb);
968 if ((value_type == 0) || (value_type == 1)) // unicode or byte
969 {
970 value = (char *)av_mallocz(value_len);
971 get_str16_nolen(pb, value_len, value, value_len);
972 if (strcmp(name,"WM/AlbumTitle")==0) { strcpy(s->album, value); }
973 av_free(value);
974 }
975 if ((value_type >= 2) || (value_type <= 5)) // boolean or DWORD or QWORD or WORD
976 {
977 if (value_type==2) value_num = get_le32(pb);
978 if (value_type==3) value_num = get_le32(pb);
979 if (value_type==4) value_num = get_le64(pb);
980 if (value_type==5) value_num = get_le16(pb);
981 if (strcmp(name,"WM/Track")==0) s->track = value_num + 1;
982 if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num;
983 }
984 av_free(name);
985 }
de6d9b64
FB
986#if 0
987 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
988 int v1, v2;
989 get_guid(pb, &g);
990 v1 = get_le32(pb);
991 v2 = get_le16(pb);
992 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
993 int len, v1, n, num;
994 char str[256], *q;
995 char tag[16];
996
997 get_guid(pb, &g);
998 print_guid(&g);
2a10020b 999
de6d9b64
FB
1000 n = get_le32(pb);
1001 for(i=0;i<n;i++) {
1002 num = get_le16(pb); /* stream number */
1003 get_str16(pb, str, sizeof(str));
1004 get_str16(pb, str, sizeof(str));
1005 len = get_le16(pb);
1006 q = tag;
1007 while (len > 0) {
1008 v1 = get_byte(pb);
1009 if ((q - tag) < sizeof(tag) - 1)
1010 *q++ = v1;
1011 len--;
1012 }
1013 *q = '\0';
1014 }
1015#endif
1016 } else if (url_feof(pb)) {
1017 goto fail;
1018 } else {
1019 url_fseek(pb, gsize - 24, SEEK_CUR);
1020 }
1021 }
1022 get_guid(pb, &g);
1023 get_le64(pb);
1024 get_byte(pb);
1025 get_byte(pb);
2141dc37
ZK
1026 if (url_feof(pb))
1027 goto fail;
1028 asf->data_offset = url_ftell(pb);
de6d9b64
FB
1029 asf->packet_size_left = 0;
1030
1031 return 0;
1032
1033 fail:
4c3dff6d 1034 for(i=0;i<s->nb_streams;i++) {
de6d9b64 1035 AVStream *st = s->streams[i];
4c3dff6d 1036 if (st) {
2141dc37 1037 av_free(st->priv_data);
4c3dff6d
ZK
1038 av_free(st->codec.extradata);
1039 }
1ea4f593 1040 av_free(st);
de6d9b64 1041 }
de6d9b64
FB
1042 return -1;
1043}
1044
2a10020b
ZK
1045#define DO_2BITS(bits, var, defval) \
1046 switch (bits & 3) \
1047 { \
1048 case 3: var = get_le32(pb); rsize += 4; break; \
1049 case 2: var = get_le16(pb); rsize += 2; break; \
1050 case 1: var = get_byte(pb); rsize++; break; \
1051 default: var = defval; break; \
1052 }
1053
de6d9b64
FB
1054static int asf_get_packet(AVFormatContext *s)
1055{
1056 ASFContext *asf = s->priv_data;
1057 ByteIOContext *pb = &s->pb;
2a10020b
ZK
1058 uint32_t packet_length, padsize;
1059 int rsize = 11;
1060 int c = get_byte(pb);
1359c2d4 1061 if (c != 0x82) {
5acdd6e6 1062 if (!url_feof(pb))
4cfbf61b 1063 printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb));
2a10020b
ZK
1064 return -EIO;
1065 }
1066 if ((c & 0x0f) == 2) { // always true for now
1359c2d4 1067 if (get_le16(pb) != 0) {
5acdd6e6
ZK
1068 if (!url_feof(pb))
1069 printf("ff asf bad non zero\n");
2a10020b
ZK
1070 return -EIO;
1071 }
de6d9b64 1072 }
2a10020b
ZK
1073
1074 asf->packet_flags = get_byte(pb);
1075 asf->packet_property = get_byte(pb);
1076
1077 DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
1078 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
1079 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
1080
1081 asf->packet_timestamp = get_le32(pb);
de6d9b64 1082 get_le16(pb); /* duration */
2141dc37 1083 // rsize has at least 11 bytes which have to be present
2a10020b
ZK
1084
1085 if (asf->packet_flags & 0x01) {
1086 asf->packet_segsizetype = get_byte(pb); rsize++;
1087 asf->packet_segments = asf->packet_segsizetype & 0x3f;
1088 } else {
1089 asf->packet_segments = 1;
1090 asf->packet_segsizetype = 0x80;
1091 }
1092 asf->packet_size_left = packet_length - padsize - rsize;
acbe6cfa
ZK
1093 if (packet_length < asf->hdr.min_pktsize)
1094 padsize += asf->hdr.min_pktsize - packet_length;
2a10020b 1095 asf->packet_padsize = padsize;
de6d9b64 1096#ifdef DEBUG
2a10020b 1097 printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
de6d9b64 1098#endif
de6d9b64
FB
1099 return 0;
1100}
1101
1102static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1103{
1104 ASFContext *asf = s->priv_data;
2a10020b 1105 ASFStream *asf_st = 0;
de6d9b64 1106 ByteIOContext *pb = &s->pb;
1359c2d4 1107 //static int pc = 0;
2a10020b
ZK
1108 for (;;) {
1109 int rsize = 0;
2141dc37 1110 if (asf->packet_size_left < FRAME_HEADER_SIZE
1359c2d4 1111 || asf->packet_segments < 1) {
2a10020b 1112 //asf->packet_size_left <= asf->packet_padsize) {
1359c2d4
ZK
1113 int ret = asf->packet_size_left + asf->packet_padsize;
1114 //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
1115 /* fail safe */
1116 url_fskip(pb, ret);
2a10020b
ZK
1117 ret = asf_get_packet(s);
1118 //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
5acdd6e6 1119 if (ret < 0 || url_feof(pb))
2a10020b 1120 return -EIO;
2141dc37 1121 asf->packet_time_start = 0;
2a10020b
ZK
1122 continue;
1123 }
2141dc37 1124 if (asf->packet_time_start == 0) {
2a10020b 1125 /* read frame header */
1359c2d4
ZK
1126 int num = get_byte(pb);
1127 asf->packet_segments--;
2a10020b
ZK
1128 rsize++;
1129 asf->packet_key_frame = (num & 0x80) >> 7;
1130 asf->stream_index = asf->asfid2avid[num & 0x7f];
2a10020b
ZK
1131 // sequence should be ignored!
1132 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1133 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1134 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1135
1136 if (asf->packet_replic_size > 1) {
1137 // it should be always at least 8 bytes - FIXME validate
1138 asf->packet_obj_size = get_le32(pb);
1139 asf->packet_frag_timestamp = get_le32(pb); // timestamp
acbe6cfa
ZK
1140 if (asf->packet_replic_size > 8)
1141 url_fskip(pb, asf->packet_replic_size - 8);
2a10020b
ZK
1142 rsize += asf->packet_replic_size; // FIXME - check validity
1143 } else {
acbe6cfa 1144 // multipacket - frag_offset is begining timestamp
2a10020b
ZK
1145 asf->packet_time_start = asf->packet_frag_offset;
1146 asf->packet_frag_offset = 0;
1147 asf->packet_frag_timestamp = asf->packet_timestamp;
2141dc37 1148
2a10020b
ZK
1149 if (asf->packet_replic_size == 1) {
1150 asf->packet_time_delta = get_byte(pb);
1151 rsize++;
1152 }
1153 }
2a10020b
ZK
1154 if (asf->packet_flags & 0x01) {
1155 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
acbe6cfa 1156#undef DO_2BITS
2a10020b
ZK
1157 //printf("Fragsize %d\n", asf->packet_frag_size);
1158 } else {
1159 asf->packet_frag_size = asf->packet_size_left - rsize;
1160 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
1161 }
1359c2d4 1162 if (asf->packet_replic_size == 1) {
2141dc37 1163 asf->packet_multi_size = asf->packet_frag_size;
1359c2d4 1164 if (asf->packet_multi_size > asf->packet_size_left) {
2141dc37
ZK
1165 asf->packet_segments = 0;
1166 continue;
1167 }
1168 }
2a10020b
ZK
1169 asf->packet_size_left -= rsize;
1170 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
1359c2d4
ZK
1171
1172 if (asf->stream_index < 0) {
1173 asf->packet_time_start = 0;
1174 /* unhandled packet (should not happen) */
1175 url_fskip(pb, asf->packet_frag_size);
1176 asf->packet_size_left -= asf->packet_frag_size;
1177 printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
1178 continue;
1179 }
1180 asf->asf_st = s->streams[asf->stream_index]->priv_data;
2a10020b
ZK
1181 }
1182 asf_st = asf->asf_st;
1183
1184 if ((asf->packet_frag_offset != asf_st->frag_offset
1185 || (asf->packet_frag_offset
1186 && asf->packet_seq != asf_st->seq)) // seq should be ignored
1187 ) {
1188 /* cannot continue current packet: free it */
1189 // FIXME better check if packet was already allocated
2141dc37 1190 printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
2a10020b 1191 asf_st->pkt.size,
2141dc37 1192 asf->packet_obj_size,
2a10020b
ZK
1193 asf->packet_frag_offset, asf_st->frag_offset,
1194 asf->packet_seq, asf_st->seq, asf->packet_frag_size);
2141dc37 1195 if (asf_st->pkt.size)
2a10020b
ZK
1196 av_free_packet(&asf_st->pkt);
1197 asf_st->frag_offset = 0;
1198 if (asf->packet_frag_offset != 0) {
2a10020b 1199 url_fskip(pb, asf->packet_frag_size);
2141dc37
ZK
1200 printf("ff asf parser skiping %db\n", asf->packet_frag_size);
1201 asf->packet_size_left -= asf->packet_frag_size;
2a10020b
ZK
1202 continue;
1203 }
1204 }
2141dc37
ZK
1205 if (asf->packet_replic_size == 1) {
1206 // frag_offset is here used as the begining timestamp
2a10020b 1207 asf->packet_frag_timestamp = asf->packet_time_start;
2141dc37 1208 asf->packet_time_start += asf->packet_time_delta;
2a10020b 1209 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
2141dc37
ZK
1210 asf->packet_size_left--;
1211 asf->packet_multi_size--;
1212 if (asf->packet_multi_size < asf->packet_obj_size)
1213 {
1214 asf->packet_time_start = 0;
1215 url_fskip(pb, asf->packet_multi_size);
1216 asf->packet_size_left -= asf->packet_multi_size;
1217 continue;
1218 }
1219 asf->packet_multi_size -= asf->packet_obj_size;
1220 //printf("COMPRESS size %d %d %d ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size);
2a10020b 1221 }
2a10020b
ZK
1222 if (asf_st->frag_offset == 0) {
1223 /* new packet */
1224 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1225 asf_st->seq = asf->packet_seq;
1226 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1227 asf_st->pkt.stream_index = asf->stream_index;
1228 if (asf->packet_key_frame)
1229 asf_st->pkt.flags |= PKT_FLAG_KEY;
1230 }
1231
1232 /* read data */
1233 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1234 // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1235 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
2141dc37
ZK
1236 asf->packet_size_left -= asf->packet_frag_size;
1237 if (asf->packet_size_left < 0)
1238 continue;
2a10020b
ZK
1239 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
1240 asf->packet_frag_size);
1241 asf_st->frag_offset += asf->packet_frag_size;
2a10020b
ZK
1242 /* test if whole packet is read */
1243 if (asf_st->frag_offset == asf_st->pkt.size) {
1244 /* return packet */
1245 if (asf_st->ds_span > 1) {
2141dc37 1246 /* packet descrambling */
2a10020b
ZK
1247 char* newdata = av_malloc(asf_st->pkt.size);
1248 if (newdata) {
1249 int offset = 0;
1250 while (offset < asf_st->pkt.size) {
1251 int off = offset / asf_st->ds_chunk_size;
1252 int row = off / asf_st->ds_span;
1253 int col = off % asf_st->ds_span;
1254 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1255 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
1256 memcpy(newdata + offset,
1257 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1258 asf_st->ds_chunk_size);
1259 offset += asf_st->ds_chunk_size;
1260 }
1261 av_free(asf_st->pkt.data);
1262 asf_st->pkt.data = newdata;
1263 }
1264 }
2a10020b
ZK
1265 asf_st->frag_offset = 0;
1266 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
1267 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1268 asf_st->pkt.size = 0;
1269 asf_st->pkt.data = 0;
2a10020b
ZK
1270 break; // packet completed
1271 }
de6d9b64 1272 }
de6d9b64
FB
1273 return 0;
1274}
1275
1276static int asf_read_close(AVFormatContext *s)
1277{
de6d9b64
FB
1278 int i;
1279
1280 for(i=0;i<s->nb_streams;i++) {
4c3dff6d
ZK
1281 AVStream *st = s->streams[i];
1282 av_free(st->priv_data);
1283 av_free(st->codec.extradata);
5e29abf8 1284 av_free(st->codec.palctrl);
de6d9b64 1285 }
de6d9b64
FB
1286 return 0;
1287}
1288
afda223c 1289static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts)
2a10020b 1290{
afda223c
FB
1291#if 0
1292 ASFContext *asf = s->priv_data;
1293 int i;
1294
1295 for(i = 0;; i++) {
1296 url_fseek(&s->pb, asf->data_offset + i * asf->packet_size, SEEK_SET);
1297 if (asf_get_packet(s) < 0)
1298 break;
1299 printf("timestamp=%0.3f\n", asf->packet_timestamp / 1000.0);
1300 }
1301#endif
2a10020b
ZK
1302 return -1;
1303}
1304
0570bf06 1305static AVInputFormat asf_iformat = {
c9a65ca8
FB
1306 "asf",
1307 "asf format",
1308 sizeof(ASFContext),
1309 asf_probe,
1310 asf_read_header,
1311 asf_read_packet,
1312 asf_read_close,
2a10020b 1313 asf_read_seek,
c9a65ca8
FB
1314};
1315
764ef400 1316#ifdef CONFIG_ENCODERS
0570bf06 1317static AVOutputFormat asf_oformat = {
de6d9b64
FB
1318 "asf",
1319 "asf format",
c6c11cb6 1320 "video/x-ms-asf",
a56c66a7 1321 "asf,wmv",
c9a65ca8 1322 sizeof(ASFContext),
4606ac8d 1323#ifdef CONFIG_MP3LAME
80783dc2 1324 CODEC_ID_MP3,
4606ac8d 1325#else
de6d9b64 1326 CODEC_ID_MP2,
4606ac8d 1327#endif
d7425f59 1328 CODEC_ID_MSMPEG4V3,
de6d9b64
FB
1329 asf_write_header,
1330 asf_write_packet,
1331 asf_write_trailer,
de6d9b64 1332};
c9a65ca8 1333
0570bf06 1334static AVOutputFormat asf_stream_oformat = {
8b3c13f9
PG
1335 "asf_stream",
1336 "asf format",
c6c11cb6 1337 "video/x-ms-asf",
8b3c13f9
PG
1338 "asf,wmv",
1339 sizeof(ASFContext),
1340#ifdef CONFIG_MP3LAME
80783dc2 1341 CODEC_ID_MP3,
8b3c13f9
PG
1342#else
1343 CODEC_ID_MP2,
1344#endif
d7425f59 1345 CODEC_ID_MSMPEG4V3,
8b3c13f9
PG
1346 asf_write_stream_header,
1347 asf_write_packet,
1348 asf_write_trailer,
1349};
764ef400 1350#endif //CONFIG_ENCODERS
8b3c13f9 1351
c9a65ca8
FB
1352int asf_init(void)
1353{
1354 av_register_input_format(&asf_iformat);
764ef400 1355#ifdef CONFIG_ENCODERS
c9a65ca8 1356 av_register_output_format(&asf_oformat);
8b3c13f9 1357 av_register_output_format(&asf_stream_oformat);
764ef400 1358#endif //CONFIG_ENCODERS
c9a65ca8
FB
1359 return 0;
1360}