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