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