per context frame_rate_base, this should finally fix frame_rate related av sync issues
[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
51typedef struct __attribute__((packed)) {
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
de6d9b64
FB
179/* I am not a number !!! This GUID is the one found on the PC used to
180 generate the stream */
181static const GUID my_guid = {
182 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
183};
184
185static void put_guid(ByteIOContext *s, const GUID *g)
186{
187 int i;
188
189 put_le32(s, g->v1);
190 put_le16(s, g->v2);
191 put_le16(s, g->v3);
192 for(i=0;i<8;i++)
193 put_byte(s, g->v4[i]);
194}
195
196static void put_str16(ByteIOContext *s, const char *tag)
197{
198 int c;
199
200 put_le16(s,strlen(tag) + 1);
201 for(;;) {
0c1a9eda 202 c = (uint8_t)*tag++;
de6d9b64 203 put_le16(s, c);
2a10020b 204 if (c == '\0')
de6d9b64
FB
205 break;
206 }
207}
208
209static void put_str16_nolen(ByteIOContext *s, const char *tag)
210{
211 int c;
212
213 for(;;) {
0c1a9eda 214 c = (uint8_t)*tag++;
de6d9b64 215 put_le16(s, c);
2a10020b 216 if (c == '\0')
de6d9b64
FB
217 break;
218 }
219}
220
0c1a9eda 221static int64_t put_header(ByteIOContext *pb, const GUID *g)
de6d9b64 222{
0c1a9eda 223 int64_t pos;
de6d9b64
FB
224
225 pos = url_ftell(pb);
226 put_guid(pb, g);
227 put_le64(pb, 24);
228 return pos;
229}
230
231/* update header size */
0c1a9eda 232static void end_header(ByteIOContext *pb, int64_t pos)
de6d9b64 233{
0c1a9eda 234 int64_t pos1;
de6d9b64
FB
235
236 pos1 = url_ftell(pb);
237 url_fseek(pb, pos + 16, SEEK_SET);
238 put_le64(pb, pos1 - pos);
239 url_fseek(pb, pos1, SEEK_SET);
240}
241
242/* write an asf chunk (only used in streaming case) */
f80c1ac0 243static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
de6d9b64
FB
244{
245 ASFContext *asf = s->priv_data;
246 ByteIOContext *pb = &s->pb;
247 int length;
248
249 length = payload_length + 8;
2a10020b
ZK
250 put_le16(pb, type);
251 put_le16(pb, length);
de6d9b64 252 put_le32(pb, asf->seqno);
f80c1ac0 253 put_le16(pb, flags); /* unknown bytes */
de6d9b64
FB
254 put_le16(pb, length);
255 asf->seqno++;
256}
257
258/* convert from unix to windows time */
0c1a9eda 259static int64_t unix_to_file_time(int ti)
de6d9b64 260{
0c1a9eda 261 int64_t t;
2a10020b 262
0c1a9eda
ZK
263 t = ti * int64_t_C(10000000);
264 t += int64_t_C(116444736000000000);
de6d9b64
FB
265 return t;
266}
267
268/* write the header (used two times if non streamed) */
0c1a9eda 269static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
de6d9b64
FB
270{
271 ASFContext *asf = s->priv_data;
272 ByteIOContext *pb = &s->pb;
273 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
274 int has_title;
275 AVCodecContext *enc;
0c1a9eda 276 int64_t header_offset, cur_pos, hpos;
f80c1ac0 277 int bit_rate;
de6d9b64 278
084fada8 279 has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
de6d9b64 280
f80c1ac0
PG
281 bit_rate = 0;
282 for(n=0;n<s->nb_streams;n++) {
283 enc = &s->streams[n]->codec;
284
285 bit_rate += enc->bit_rate;
286 }
287
8b3c13f9 288 if (asf->is_streamed) {
f80c1ac0 289 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
de6d9b64 290 }
f80c1ac0
PG
291
292 put_guid(pb, &asf_header);
2a10020b 293 put_le64(pb, -1); /* header length, will be patched after */
f80c1ac0
PG
294 put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
295 put_byte(pb, 1); /* ??? */
296 put_byte(pb, 2); /* ??? */
2a10020b 297
de6d9b64
FB
298 /* file header */
299 header_offset = url_ftell(pb);
300 hpos = put_header(pb, &file_header);
301 put_guid(pb, &my_guid);
302 put_le64(pb, file_size);
303 file_time = 0;
304 put_le64(pb, unix_to_file_time(file_time));
305 put_le64(pb, asf->nb_packets); /* number of packets */
306 put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
307 put_le64(pb, asf->duration); /* duration (in 100ns units) */
2a10020b
ZK
308 put_le32(pb, 0); /* start time stamp */
309 put_le32(pb, 0); /* ??? */
310 put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
de6d9b64
FB
311 put_le32(pb, asf->packet_size); /* packet size */
312 put_le32(pb, asf->packet_size); /* packet size */
f80c1ac0 313 put_le32(pb, bit_rate); /* Nominal data rate in bps */
de6d9b64
FB
314 end_header(pb, hpos);
315
316 /* unknown headers */
317 hpos = put_header(pb, &head1_guid);
318 put_guid(pb, &head2_guid);
319 put_le32(pb, 6);
320 put_le16(pb, 0);
321 end_header(pb, hpos);
322
323 /* title and other infos */
324 if (has_title) {
325 hpos = put_header(pb, &comment_header);
326 put_le16(pb, 2 * (strlen(s->title) + 1));
327 put_le16(pb, 2 * (strlen(s->author) + 1));
328 put_le16(pb, 2 * (strlen(s->copyright) + 1));
329 put_le16(pb, 2 * (strlen(s->comment) + 1));
330 put_le16(pb, 0);
331 put_str16_nolen(pb, s->title);
332 put_str16_nolen(pb, s->author);
333 put_str16_nolen(pb, s->copyright);
334 put_str16_nolen(pb, s->comment);
335 end_header(pb, hpos);
336 }
337
338 /* stream headers */
339 for(n=0;n<s->nb_streams;n++) {
0c1a9eda 340 int64_t es_pos;
17a241dd 341 // ASFStream *stream = &asf->streams[n];
f80c1ac0 342
de6d9b64
FB
343 enc = &s->streams[n]->codec;
344 asf->streams[n].num = n + 1;
345 asf->streams[n].seq = 0;
2a10020b 346
de6d9b64
FB
347 switch(enc->codec_type) {
348 case CODEC_TYPE_AUDIO:
349 wav_extra_size = 0;
350 extra_size = 18 + wav_extra_size;
351 extra_size2 = 0;
352 break;
353 default:
354 case CODEC_TYPE_VIDEO:
355 wav_extra_size = 0;
356 extra_size = 0x33;
357 extra_size2 = 0;
358 break;
359 }
360
361 hpos = put_header(pb, &stream_header);
362 if (enc->codec_type == CODEC_TYPE_AUDIO) {
363 put_guid(pb, &audio_stream);
364 put_guid(pb, &audio_conceal_none);
365 } else {
366 put_guid(pb, &video_stream);
367 put_guid(pb, &video_conceal_none);
368 }
369 put_le64(pb, 0); /* ??? */
f80c1ac0 370 es_pos = url_ftell(pb);
de6d9b64
FB
371 put_le32(pb, extra_size); /* wav header len */
372 put_le32(pb, extra_size2); /* additional data len */
373 put_le16(pb, n + 1); /* stream number */
374 put_le32(pb, 0); /* ??? */
2a10020b 375
de6d9b64
FB
376 if (enc->codec_type == CODEC_TYPE_AUDIO) {
377 /* WAVEFORMATEX header */
f80c1ac0
PG
378 int wavsize = put_wav_header(pb, enc);
379
380 if (wavsize < 0)
46a3d068 381 return -1;
f80c1ac0
PG
382 if (wavsize != extra_size) {
383 cur_pos = url_ftell(pb);
384 url_fseek(pb, es_pos, SEEK_SET);
385 put_le32(pb, wavsize); /* wav header len */
386 url_fseek(pb, cur_pos, SEEK_SET);
387 }
de6d9b64
FB
388 } else {
389 put_le32(pb, enc->width);
390 put_le32(pb, enc->height);
391 put_byte(pb, 2); /* ??? */
392 put_le16(pb, 40); /* size */
393
394 /* BITMAPINFOHEADER header */
2727c35e 395 put_bmp_header(pb, enc, codec_bmp_tags, 1);
de6d9b64
FB
396 }
397 end_header(pb, hpos);
398 }
399
400 /* media comments */
401
402 hpos = put_header(pb, &codec_comment_header);
403 put_guid(pb, &codec_comment1_header);
404 put_le32(pb, s->nb_streams);
405 for(n=0;n<s->nb_streams;n++) {
084fada8
PG
406 AVCodec *p;
407
de6d9b64 408 enc = &s->streams[n]->codec;
084fada8 409 p = avcodec_find_encoder(enc->codec_id);
de6d9b64
FB
410
411 put_le16(pb, asf->streams[n].num);
084fada8 412 put_str16(pb, p ? p->name : enc->codec_name);
de6d9b64
FB
413 put_le16(pb, 0); /* no parameters */
414 /* id */
415 if (enc->codec_type == CODEC_TYPE_AUDIO) {
416 put_le16(pb, 2);
417 put_le16(pb, codec_get_tag(codec_wav_tags, enc->codec_id));
418 } else {
419 put_le16(pb, 4);
17a241dd 420 put_le32(pb, codec_get_tag(codec_bmp_tags, enc->codec_id));
de6d9b64
FB
421 }
422 }
423 end_header(pb, hpos);
424
425 /* patch the header size fields */
426
427 cur_pos = url_ftell(pb);
428 header_size = cur_pos - header_offset;
8b3c13f9 429 if (asf->is_streamed) {
f80c1ac0
PG
430 header_size += 8 + 30 + 50;
431
432 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
de6d9b64 433 put_le16(pb, header_size);
f80c1ac0 434 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
de6d9b64 435 put_le16(pb, header_size);
f80c1ac0
PG
436
437 header_size -= 8 + 30 + 50;
de6d9b64 438 }
f80c1ac0
PG
439 header_size += 24 + 6;
440 url_fseek(pb, header_offset - 14, SEEK_SET);
441 put_le64(pb, header_size);
de6d9b64
FB
442 url_fseek(pb, cur_pos, SEEK_SET);
443
444 /* movie chunk, followed by packets of packet_size */
445 asf->data_offset = cur_pos;
446 put_guid(pb, &data_header);
447 put_le64(pb, data_chunk_size);
448 put_guid(pb, &my_guid);
449 put_le64(pb, asf->nb_packets); /* nb packets */
450 put_byte(pb, 1); /* ??? */
451 put_byte(pb, 1); /* ??? */
452 return 0;
453}
454
455static int asf_write_header(AVFormatContext *s)
456{
c9a65ca8 457 ASFContext *asf = s->priv_data;
de6d9b64 458
17a241dd
FB
459 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
460
de6d9b64
FB
461 asf->packet_size = PACKET_SIZE;
462 asf->nb_packets = 0;
463
f80c1ac0 464 if (asf_write_header1(s, 0, 50) < 0) {
2824c473 465 //av_free(asf);
46a3d068
FB
466 return -1;
467 }
de6d9b64
FB
468
469 put_flush_packet(&s->pb);
470
471 asf->packet_nb_frames = 0;
472 asf->packet_timestamp_start = -1;
473 asf->packet_timestamp_end = -1;
474 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
475 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
476 NULL, NULL, NULL, NULL);
477
478 return 0;
479}
480
8b3c13f9
PG
481static int asf_write_stream_header(AVFormatContext *s)
482{
483 ASFContext *asf = s->priv_data;
484
485 asf->is_streamed = 1;
486
487 return asf_write_header(s);
488}
489
de6d9b64 490/* write a fixed size packet */
2a10020b
ZK
491static int put_packet(AVFormatContext *s,
492 unsigned int timestamp, unsigned int duration,
de6d9b64
FB
493 int nb_frames, int padsize)
494{
495 ASFContext *asf = s->priv_data;
496 ByteIOContext *pb = &s->pb;
497 int flags;
498
8b3c13f9 499 if (asf->is_streamed) {
f80c1ac0 500 put_chunk(s, 0x4424, asf->packet_size, 0);
de6d9b64
FB
501 }
502
503 put_byte(pb, 0x82);
504 put_le16(pb, 0);
2a10020b 505
de6d9b64
FB
506 flags = 0x01; /* nb segments present */
507 if (padsize > 0) {
508 if (padsize < 256)
509 flags |= 0x08;
510 else
511 flags |= 0x10;
512 }
513 put_byte(pb, flags); /* flags */
514 put_byte(pb, 0x5d);
515 if (flags & 0x10)
f80c1ac0 516 put_le16(pb, padsize - 2);
de6d9b64 517 if (flags & 0x08)
f80c1ac0 518 put_byte(pb, padsize - 1);
de6d9b64
FB
519 put_le32(pb, timestamp);
520 put_le16(pb, duration);
521 put_byte(pb, nb_frames | 0x80);
f80c1ac0
PG
522
523 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3);
de6d9b64
FB
524}
525
526static void flush_packet(AVFormatContext *s)
527{
528 ASFContext *asf = s->priv_data;
529 int hdr_size, ptr;
2a10020b
ZK
530
531 hdr_size = put_packet(s, asf->packet_timestamp_start,
532 asf->packet_timestamp_end - asf->packet_timestamp_start,
de6d9b64 533 asf->packet_nb_frames, asf->packet_size_left);
2a10020b 534
f80c1ac0
PG
535 /* Clear out the padding bytes */
536 ptr = asf->packet_size - hdr_size - asf->packet_size_left;
de6d9b64 537 memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
2a10020b 538
de6d9b64 539 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
2a10020b 540
de6d9b64
FB
541 put_flush_packet(&s->pb);
542 asf->nb_packets++;
543 asf->packet_nb_frames = 0;
544 asf->packet_timestamp_start = -1;
545 asf->packet_timestamp_end = -1;
546 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
547 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
548 NULL, NULL, NULL, NULL);
549}
550
551static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
552 int payload_size, int frag_offset, int frag_len)
553{
554 ASFContext *asf = s->priv_data;
555 ByteIOContext *pb = &asf->pb;
556 int val;
557
558 val = stream->num;
492cd3a9 559 if (s->streams[val - 1]->codec.coded_frame->key_frame /* && frag_offset == 0 */)
de6d9b64
FB
560 val |= 0x80;
561 put_byte(pb, val);
562 put_byte(pb, stream->seq);
563 put_le32(pb, frag_offset); /* fragment offset */
564 put_byte(pb, 0x08); /* flags */
565 put_le32(pb, payload_size);
566 put_le32(pb, timestamp);
567 put_le16(pb, frag_len);
568}
569
570
571/* Output a frame. We suppose that payload_size <= PACKET_SIZE.
572
573 It is there that you understand that the ASF format is really
2a10020b 574 crap. They have misread the MPEG Systems spec !
de6d9b64
FB
575 */
576static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
0c1a9eda 577 uint8_t *buf, int payload_size)
de6d9b64
FB
578{
579 ASFContext *asf = s->priv_data;
580 int frag_pos, frag_len, frag_len1;
2a10020b 581
de6d9b64
FB
582 frag_pos = 0;
583 while (frag_pos < payload_size) {
584 frag_len = payload_size - frag_pos;
585 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
586 if (frag_len1 > 0) {
587 if (frag_len > frag_len1)
588 frag_len = frag_len1;
b13a517f 589 put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len);
de6d9b64
FB
590 put_buffer(&asf->pb, buf, frag_len);
591 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
592 asf->packet_timestamp_end = timestamp;
593 if (asf->packet_timestamp_start == -1)
594 asf->packet_timestamp_start = timestamp;
595 asf->packet_nb_frames++;
596 } else {
597 frag_len = 0;
598 }
599 frag_pos += frag_len;
600 buf += frag_len;
601 /* output the frame if filled */
602 if (asf->packet_size_left <= FRAME_HEADER_SIZE)
603 flush_packet(s);
604 }
605 stream->seq++;
606}
607
608
609static int asf_write_packet(AVFormatContext *s, int stream_index,
0c1a9eda 610 uint8_t *buf, int size, int timestamp)
de6d9b64
FB
611{
612 ASFContext *asf = s->priv_data;
e0d2714a 613 ASFStream *stream;
0c1a9eda 614 int64_t duration;
de6d9b64
FB
615 AVCodecContext *codec;
616
617 codec = &s->streams[stream_index]->codec;
e0d2714a 618 stream = &asf->streams[stream_index];
2a10020b 619
de6d9b64 620 if (codec->codec_type == CODEC_TYPE_AUDIO) {
0c1a9eda 621 duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) /
de6d9b64
FB
622 codec->sample_rate;
623 } else {
14bea432 624 duration = av_rescale(codec->frame_number * codec->frame_rate_base, 10000000, codec->frame_rate);
de6d9b64
FB
625 }
626 if (duration > asf->duration)
627 asf->duration = duration;
2a10020b 628
e0d2714a 629 put_frame(s, stream, timestamp, buf, size);
de6d9b64
FB
630 return 0;
631}
2a10020b 632
de6d9b64
FB
633static int asf_write_trailer(AVFormatContext *s)
634{
635 ASFContext *asf = s->priv_data;
0c1a9eda 636 int64_t file_size;
de6d9b64
FB
637
638 /* flush the current packet */
639 if (asf->pb.buf_ptr > asf->pb.buffer)
640 flush_packet(s);
641
8b3c13f9 642 if (asf->is_streamed) {
f80c1ac0 643 put_chunk(s, 0x4524, 0, 0); /* end of stream */
de6d9b64
FB
644 } else {
645 /* rewrite an updated header */
646 file_size = url_ftell(&s->pb);
647 url_fseek(&s->pb, 0, SEEK_SET);
648 asf_write_header1(s, file_size, file_size - asf->data_offset);
649 }
650
651 put_flush_packet(&s->pb);
de6d9b64
FB
652 return 0;
653}
654
655/**********************************/
656/* decoding */
657
658//#define DEBUG
659
660#ifdef DEBUG
5082f759
FR
661#define PRINT_IF_GUID(g,cmp) \
662if (!memcmp(g, &cmp, sizeof(GUID))) \
663 printf("(GUID: %s) ", #cmp)
664
de6d9b64
FB
665static void print_guid(const GUID *g)
666{
667 int i;
5082f759
FR
668 PRINT_IF_GUID(g, asf_header);
669 else PRINT_IF_GUID(g, file_header);
670 else PRINT_IF_GUID(g, stream_header);
671 else PRINT_IF_GUID(g, audio_stream);
672 else PRINT_IF_GUID(g, audio_conceal_none);
673 else PRINT_IF_GUID(g, video_stream);
674 else PRINT_IF_GUID(g, video_conceal_none);
675 else PRINT_IF_GUID(g, comment_header);
676 else PRINT_IF_GUID(g, codec_comment_header);
677 else PRINT_IF_GUID(g, codec_comment1_header);
678 else PRINT_IF_GUID(g, data_header);
679 else PRINT_IF_GUID(g, index_guid);
680 else PRINT_IF_GUID(g, head1_guid);
681 else PRINT_IF_GUID(g, head2_guid);
682 else PRINT_IF_GUID(g, my_guid);
683 else
684 printf("(GUID: unknown) ");
de6d9b64 685 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
2a10020b 686 for(i=0;i<8;i++)
de6d9b64
FB
687 printf(" 0x%02x,", g->v4[i]);
688 printf("}\n");
689}
5082f759 690#undef PRINT_IF_GUID(g,cmp)
de6d9b64
FB
691#endif
692
693static void get_guid(ByteIOContext *s, GUID *g)
694{
695 int i;
696
697 g->v1 = get_le32(s);
698 g->v2 = get_le16(s);
699 g->v3 = get_le16(s);
700 for(i=0;i<8;i++)
701 g->v4[i] = get_byte(s);
702}
703
704#if 0
705static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
706{
707 int len, c;
708 char *q;
709
710 len = get_le16(pb);
711 q = buf;
712 while (len > 0) {
713 c = get_le16(pb);
714 if ((q - buf) < buf_size - 1)
715 *q++ = c;
716 len--;
717 }
718 *q = '\0';
719}
720#endif
721
722static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
723{
724 int c;
725 char *q;
726
727 q = buf;
728 while (len > 0) {
729 c = get_le16(pb);
730 if ((q - buf) < buf_size - 1)
731 *q++ = c;
732 len-=2;
733 }
734 *q = '\0';
735}
736
c9a65ca8
FB
737static int asf_probe(AVProbeData *pd)
738{
739 GUID g;
740 const unsigned char *p;
741 int i;
742
743 /* check file header */
744 if (pd->buf_size <= 32)
745 return 0;
746 p = pd->buf;
747 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
748 p += 4;
749 g.v2 = p[0] | (p[1] << 8);
750 p += 2;
751 g.v3 = p[0] | (p[1] << 8);
752 p += 2;
753 for(i=0;i<8;i++)
754 g.v4[i] = *p++;
755
756 if (!memcmp(&g, &asf_header, sizeof(GUID)))
757 return AVPROBE_SCORE_MAX;
758 else
759 return 0;
760}
761
de6d9b64
FB
762static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
763{
c9a65ca8 764 ASFContext *asf = s->priv_data;
de6d9b64
FB
765 GUID g;
766 ByteIOContext *pb = &s->pb;
767 AVStream *st;
768 ASFStream *asf_st;
e095026a 769 int size, i;
0c1a9eda 770 int64_t gsize;
de6d9b64 771
17a241dd
FB
772 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */
773
de6d9b64
FB
774 get_guid(pb, &g);
775 if (memcmp(&g, &asf_header, sizeof(GUID)))
776 goto fail;
777 get_le64(pb);
778 get_le32(pb);
779 get_byte(pb);
780 get_byte(pb);
2141dc37 781 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
de6d9b64
FB
782 for(;;) {
783 get_guid(pb, &g);
784 gsize = get_le64(pb);
785#ifdef DEBUG
786 printf("%08Lx: ", url_ftell(pb) - 24);
787 print_guid(&g);
788 printf(" size=0x%Lx\n", gsize);
789#endif
790 if (gsize < 24)
791 goto fail;
792 if (!memcmp(&g, &file_header, sizeof(GUID))) {
2a10020b
ZK
793 get_guid(pb, &asf->hdr.guid);
794 asf->hdr.file_size = get_le64(pb);
795 asf->hdr.create_time = get_le64(pb);
796 asf->hdr.packets_count = get_le64(pb);
797 asf->hdr.play_time = get_le64(pb);
798 asf->hdr.send_time = get_le64(pb);
799 asf->hdr.preroll = get_le32(pb);
800 asf->hdr.ignore = get_le32(pb);
801 asf->hdr.flags = get_le32(pb);
802 asf->hdr.min_pktsize = get_le32(pb);
803 asf->hdr.max_pktsize = get_le32(pb);
804 asf->hdr.max_bitrate = get_le32(pb);
2141dc37
ZK
805 asf->packet_size = asf->hdr.max_pktsize;
806 asf->nb_packets = asf->hdr.packets_count;
de6d9b64 807 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
2e7973bb 808 int type, total_size, type_specific_size;
de6d9b64 809 unsigned int tag1;
0c1a9eda 810 int64_t pos1, pos2;
2a10020b 811
de6d9b64
FB
812 pos1 = url_ftell(pb);
813
814 st = av_mallocz(sizeof(AVStream));
815 if (!st)
816 goto fail;
1e491e29 817 avcodec_get_context_defaults(&st->codec);
2a10020b 818 s->streams[s->nb_streams] = st;
de6d9b64
FB
819 asf_st = av_mallocz(sizeof(ASFStream));
820 if (!asf_st)
821 goto fail;
822 st->priv_data = asf_st;
1359c2d4 823 st->time_length = (asf->hdr.send_time - asf->hdr.preroll) / 10; // us
de6d9b64
FB
824 get_guid(pb, &g);
825 if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
826 type = CODEC_TYPE_AUDIO;
827 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
828 type = CODEC_TYPE_VIDEO;
829 } else {
830 goto fail;
831 }
832 get_guid(pb, &g);
833 total_size = get_le64(pb);
2e7973bb 834 type_specific_size = get_le32(pb);
de6d9b64 835 get_le32(pb);
2a10020b
ZK
836 st->id = get_le16(pb) & 0x7f; /* stream id */
837 // mapping of asf ID to AV stream ID;
838 asf->asfid2avid[st->id] = s->nb_streams++;
839
de6d9b64 840 get_le32(pb);
2141dc37 841 st->codec.codec_type = type;
41568e4a 842 st->codec.frame_rate = 15 * s->pts_den / s->pts_num; // 15 fps default
de6d9b64 843 if (type == CODEC_TYPE_AUDIO) {
2e7973bb 844 get_wav_header(pb, &st->codec, type_specific_size);
2a10020b
ZK
845 /* We have to init the frame size at some point .... */
846 pos2 = url_ftell(pb);
2141dc37 847 if (gsize > (pos2 + 8 - pos1 + 24)) {
2a10020b
ZK
848 asf_st->ds_span = get_byte(pb);
849 asf_st->ds_packet_size = get_le16(pb);
850 asf_st->ds_chunk_size = get_le16(pb);
851 asf_st->ds_data_size = get_le16(pb);
852 asf_st->ds_silence_data = get_byte(pb);
853 }
854 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
855 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
856 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
2141dc37 857 if (asf_st->ds_span > 1) {
2a10020b
ZK
858 if (!asf_st->ds_chunk_size
859 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
860 asf_st->ds_span = 0; // disable descrambling
861 }
f80c1ac0
PG
862 switch (st->codec.codec_id) {
863 case CODEC_ID_MP3LAME:
864 st->codec.frame_size = MPA_FRAME_SIZE;
865 break;
866 case CODEC_ID_PCM_S16LE:
867 case CODEC_ID_PCM_S16BE:
868 case CODEC_ID_PCM_U16LE:
869 case CODEC_ID_PCM_U16BE:
870 case CODEC_ID_PCM_S8:
871 case CODEC_ID_PCM_U8:
872 case CODEC_ID_PCM_ALAW:
873 case CODEC_ID_PCM_MULAW:
874 st->codec.frame_size = 1;
875 break;
876 default:
877 /* This is probably wrong, but it prevents a crash later */
878 st->codec.frame_size = 1;
879 break;
880 }
de6d9b64 881 } else {
2141dc37 882 get_le32(pb);
de6d9b64
FB
883 get_le32(pb);
884 get_byte(pb);
885 size = get_le16(pb); /* size */
886 get_le32(pb); /* size */
887 st->codec.width = get_le32(pb);
2a10020b
ZK
888 st->codec.height = get_le32(pb);
889 /* not available for asf */
de6d9b64
FB
890 get_le16(pb); /* panes */
891 get_le16(pb); /* depth */
892 tag1 = get_le32(pb);
2141dc37
ZK
893 url_fskip(pb, 20);
894 if (size > 40) {
e40fed0b
ZK
895 st->codec.extradata_size = size - 40;
896 st->codec.extradata = av_mallocz(st->codec.extradata_size);
897 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
2141dc37 898 }
7004ffb3 899 st->codec.codec_tag = tag1;
17a241dd 900 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
de6d9b64
FB
901 }
902 pos2 = url_ftell(pb);
903 url_fskip(pb, gsize - (pos2 - pos1 + 24));
904 } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
905 break;
906 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
907 int len1, len2, len3, len4, len5;
908
909 len1 = get_le16(pb);
910 len2 = get_le16(pb);
911 len3 = get_le16(pb);
912 len4 = get_le16(pb);
913 len5 = get_le16(pb);
914 get_str16_nolen(pb, len1, s->title, sizeof(s->title));
915 get_str16_nolen(pb, len2, s->author, sizeof(s->author));
916 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
917 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment));
2141dc37 918 url_fskip(pb, len5);
de6d9b64
FB
919#if 0
920 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
921 int v1, v2;
922 get_guid(pb, &g);
923 v1 = get_le32(pb);
924 v2 = get_le16(pb);
925 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
926 int len, v1, n, num;
927 char str[256], *q;
928 char tag[16];
929
930 get_guid(pb, &g);
931 print_guid(&g);
2a10020b 932
de6d9b64
FB
933 n = get_le32(pb);
934 for(i=0;i<n;i++) {
935 num = get_le16(pb); /* stream number */
936 get_str16(pb, str, sizeof(str));
937 get_str16(pb, str, sizeof(str));
938 len = get_le16(pb);
939 q = tag;
940 while (len > 0) {
941 v1 = get_byte(pb);
942 if ((q - tag) < sizeof(tag) - 1)
943 *q++ = v1;
944 len--;
945 }
946 *q = '\0';
947 }
948#endif
949 } else if (url_feof(pb)) {
950 goto fail;
951 } else {
952 url_fseek(pb, gsize - 24, SEEK_CUR);
953 }
954 }
955 get_guid(pb, &g);
956 get_le64(pb);
957 get_byte(pb);
958 get_byte(pb);
2141dc37
ZK
959 if (url_feof(pb))
960 goto fail;
961 asf->data_offset = url_ftell(pb);
de6d9b64
FB
962 asf->packet_size_left = 0;
963
964 return 0;
965
966 fail:
4c3dff6d 967 for(i=0;i<s->nb_streams;i++) {
de6d9b64 968 AVStream *st = s->streams[i];
4c3dff6d 969 if (st) {
2141dc37 970 av_free(st->priv_data);
4c3dff6d
ZK
971 av_free(st->codec.extradata);
972 }
1ea4f593 973 av_free(st);
de6d9b64 974 }
de6d9b64
FB
975 return -1;
976}
977
2a10020b
ZK
978#define DO_2BITS(bits, var, defval) \
979 switch (bits & 3) \
980 { \
981 case 3: var = get_le32(pb); rsize += 4; break; \
982 case 2: var = get_le16(pb); rsize += 2; break; \
983 case 1: var = get_byte(pb); rsize++; break; \
984 default: var = defval; break; \
985 }
986
de6d9b64
FB
987static int asf_get_packet(AVFormatContext *s)
988{
989 ASFContext *asf = s->priv_data;
990 ByteIOContext *pb = &s->pb;
2a10020b
ZK
991 uint32_t packet_length, padsize;
992 int rsize = 11;
993 int c = get_byte(pb);
1359c2d4 994 if (c != 0x82) {
5acdd6e6
ZK
995 if (!url_feof(pb))
996 printf("ff asf bad header %x at:%Ld\n", c, url_ftell(pb));
2a10020b
ZK
997 return -EIO;
998 }
999 if ((c & 0x0f) == 2) { // always true for now
1359c2d4 1000 if (get_le16(pb) != 0) {
5acdd6e6
ZK
1001 if (!url_feof(pb))
1002 printf("ff asf bad non zero\n");
2a10020b
ZK
1003 return -EIO;
1004 }
de6d9b64 1005 }
2a10020b
ZK
1006
1007 asf->packet_flags = get_byte(pb);
1008 asf->packet_property = get_byte(pb);
1009
1010 DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
1011 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
1012 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
1013
1014 asf->packet_timestamp = get_le32(pb);
de6d9b64 1015 get_le16(pb); /* duration */
2141dc37 1016 // rsize has at least 11 bytes which have to be present
2a10020b
ZK
1017
1018 if (asf->packet_flags & 0x01) {
1019 asf->packet_segsizetype = get_byte(pb); rsize++;
1020 asf->packet_segments = asf->packet_segsizetype & 0x3f;
1021 } else {
1022 asf->packet_segments = 1;
1023 asf->packet_segsizetype = 0x80;
1024 }
1025 asf->packet_size_left = packet_length - padsize - rsize;
acbe6cfa
ZK
1026 if (packet_length < asf->hdr.min_pktsize)
1027 padsize += asf->hdr.min_pktsize - packet_length;
2a10020b 1028 asf->packet_padsize = padsize;
de6d9b64 1029#ifdef DEBUG
2a10020b 1030 printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
de6d9b64 1031#endif
de6d9b64
FB
1032 return 0;
1033}
1034
1035static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1036{
1037 ASFContext *asf = s->priv_data;
2a10020b 1038 ASFStream *asf_st = 0;
de6d9b64 1039 ByteIOContext *pb = &s->pb;
1359c2d4 1040 //static int pc = 0;
2a10020b
ZK
1041 for (;;) {
1042 int rsize = 0;
2141dc37 1043 if (asf->packet_size_left < FRAME_HEADER_SIZE
1359c2d4 1044 || asf->packet_segments < 1) {
2a10020b 1045 //asf->packet_size_left <= asf->packet_padsize) {
1359c2d4
ZK
1046 int ret = asf->packet_size_left + asf->packet_padsize;
1047 //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
1048 /* fail safe */
1049 url_fskip(pb, ret);
2a10020b
ZK
1050 ret = asf_get_packet(s);
1051 //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
5acdd6e6 1052 if (ret < 0 || url_feof(pb))
2a10020b 1053 return -EIO;
2141dc37 1054 asf->packet_time_start = 0;
2a10020b
ZK
1055 continue;
1056 }
2141dc37 1057 if (asf->packet_time_start == 0) {
2a10020b 1058 /* read frame header */
1359c2d4
ZK
1059 int num = get_byte(pb);
1060 asf->packet_segments--;
2a10020b
ZK
1061 rsize++;
1062 asf->packet_key_frame = (num & 0x80) >> 7;
1063 asf->stream_index = asf->asfid2avid[num & 0x7f];
2a10020b
ZK
1064 // sequence should be ignored!
1065 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1066 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1067 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1068
1069 if (asf->packet_replic_size > 1) {
1070 // it should be always at least 8 bytes - FIXME validate
1071 asf->packet_obj_size = get_le32(pb);
1072 asf->packet_frag_timestamp = get_le32(pb); // timestamp
acbe6cfa
ZK
1073 if (asf->packet_replic_size > 8)
1074 url_fskip(pb, asf->packet_replic_size - 8);
2a10020b
ZK
1075 rsize += asf->packet_replic_size; // FIXME - check validity
1076 } else {
acbe6cfa 1077 // multipacket - frag_offset is begining timestamp
2a10020b
ZK
1078 asf->packet_time_start = asf->packet_frag_offset;
1079 asf->packet_frag_offset = 0;
1080 asf->packet_frag_timestamp = asf->packet_timestamp;
2141dc37 1081
2a10020b
ZK
1082 if (asf->packet_replic_size == 1) {
1083 asf->packet_time_delta = get_byte(pb);
1084 rsize++;
1085 }
1086 }
2a10020b
ZK
1087 if (asf->packet_flags & 0x01) {
1088 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
acbe6cfa 1089#undef DO_2BITS
2a10020b
ZK
1090 //printf("Fragsize %d\n", asf->packet_frag_size);
1091 } else {
1092 asf->packet_frag_size = asf->packet_size_left - rsize;
1093 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
1094 }
1359c2d4 1095 if (asf->packet_replic_size == 1) {
2141dc37 1096 asf->packet_multi_size = asf->packet_frag_size;
1359c2d4 1097 if (asf->packet_multi_size > asf->packet_size_left) {
2141dc37
ZK
1098 asf->packet_segments = 0;
1099 continue;
1100 }
1101 }
2a10020b
ZK
1102 asf->packet_size_left -= rsize;
1103 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
1359c2d4
ZK
1104
1105 if (asf->stream_index < 0) {
1106 asf->packet_time_start = 0;
1107 /* unhandled packet (should not happen) */
1108 url_fskip(pb, asf->packet_frag_size);
1109 asf->packet_size_left -= asf->packet_frag_size;
1110 printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f);
1111 continue;
1112 }
1113 asf->asf_st = s->streams[asf->stream_index]->priv_data;
2a10020b
ZK
1114 }
1115 asf_st = asf->asf_st;
1116
1117 if ((asf->packet_frag_offset != asf_st->frag_offset
1118 || (asf->packet_frag_offset
1119 && asf->packet_seq != asf_st->seq)) // seq should be ignored
1120 ) {
1121 /* cannot continue current packet: free it */
1122 // FIXME better check if packet was already allocated
2141dc37 1123 printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
2a10020b 1124 asf_st->pkt.size,
2141dc37 1125 asf->packet_obj_size,
2a10020b
ZK
1126 asf->packet_frag_offset, asf_st->frag_offset,
1127 asf->packet_seq, asf_st->seq, asf->packet_frag_size);
2141dc37 1128 if (asf_st->pkt.size)
2a10020b
ZK
1129 av_free_packet(&asf_st->pkt);
1130 asf_st->frag_offset = 0;
1131 if (asf->packet_frag_offset != 0) {
2a10020b 1132 url_fskip(pb, asf->packet_frag_size);
2141dc37
ZK
1133 printf("ff asf parser skiping %db\n", asf->packet_frag_size);
1134 asf->packet_size_left -= asf->packet_frag_size;
2a10020b
ZK
1135 continue;
1136 }
1137 }
2141dc37
ZK
1138 if (asf->packet_replic_size == 1) {
1139 // frag_offset is here used as the begining timestamp
2a10020b 1140 asf->packet_frag_timestamp = asf->packet_time_start;
2141dc37 1141 asf->packet_time_start += asf->packet_time_delta;
2a10020b 1142 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
2141dc37
ZK
1143 asf->packet_size_left--;
1144 asf->packet_multi_size--;
1145 if (asf->packet_multi_size < asf->packet_obj_size)
1146 {
1147 asf->packet_time_start = 0;
1148 url_fskip(pb, asf->packet_multi_size);
1149 asf->packet_size_left -= asf->packet_multi_size;
1150 continue;
1151 }
1152 asf->packet_multi_size -= asf->packet_obj_size;
1153 //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 1154 }
2a10020b
ZK
1155 if (asf_st->frag_offset == 0) {
1156 /* new packet */
1157 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1158 asf_st->seq = asf->packet_seq;
1159 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1160 asf_st->pkt.stream_index = asf->stream_index;
1161 if (asf->packet_key_frame)
1162 asf_st->pkt.flags |= PKT_FLAG_KEY;
1163 }
1164
1165 /* read data */
1166 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1167 // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1168 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
2141dc37
ZK
1169 asf->packet_size_left -= asf->packet_frag_size;
1170 if (asf->packet_size_left < 0)
1171 continue;
2a10020b
ZK
1172 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
1173 asf->packet_frag_size);
1174 asf_st->frag_offset += asf->packet_frag_size;
2a10020b
ZK
1175 /* test if whole packet is read */
1176 if (asf_st->frag_offset == asf_st->pkt.size) {
1177 /* return packet */
1178 if (asf_st->ds_span > 1) {
2141dc37 1179 /* packet descrambling */
2a10020b
ZK
1180 char* newdata = av_malloc(asf_st->pkt.size);
1181 if (newdata) {
1182 int offset = 0;
1183 while (offset < asf_st->pkt.size) {
1184 int off = offset / asf_st->ds_chunk_size;
1185 int row = off / asf_st->ds_span;
1186 int col = off % asf_st->ds_span;
1187 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1188 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
1189 memcpy(newdata + offset,
1190 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1191 asf_st->ds_chunk_size);
1192 offset += asf_st->ds_chunk_size;
1193 }
1194 av_free(asf_st->pkt.data);
1195 asf_st->pkt.data = newdata;
1196 }
1197 }
2a10020b
ZK
1198 asf_st->frag_offset = 0;
1199 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
1200 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1201 asf_st->pkt.size = 0;
1202 asf_st->pkt.data = 0;
2a10020b
ZK
1203 break; // packet completed
1204 }
de6d9b64 1205 }
de6d9b64
FB
1206 return 0;
1207}
1208
1209static int asf_read_close(AVFormatContext *s)
1210{
de6d9b64
FB
1211 int i;
1212
1213 for(i=0;i<s->nb_streams;i++) {
4c3dff6d
ZK
1214 AVStream *st = s->streams[i];
1215 av_free(st->priv_data);
1216 av_free(st->codec.extradata);
de6d9b64 1217 }
de6d9b64
FB
1218 return 0;
1219}
1220
2a10020b
ZK
1221static int asf_read_seek(AVFormatContext *s, int64_t pts)
1222{
1223 printf("SEEK TO %Ld", pts);
1224 return -1;
1225}
1226
0570bf06 1227static AVInputFormat asf_iformat = {
c9a65ca8
FB
1228 "asf",
1229 "asf format",
1230 sizeof(ASFContext),
1231 asf_probe,
1232 asf_read_header,
1233 asf_read_packet,
1234 asf_read_close,
2a10020b 1235 asf_read_seek,
c9a65ca8
FB
1236};
1237
0570bf06 1238static AVOutputFormat asf_oformat = {
de6d9b64
FB
1239 "asf",
1240 "asf format",
c6c11cb6 1241 "video/x-ms-asf",
a56c66a7 1242 "asf,wmv",
c9a65ca8 1243 sizeof(ASFContext),
4606ac8d
ZK
1244#ifdef CONFIG_MP3LAME
1245 CODEC_ID_MP3LAME,
1246#else
de6d9b64 1247 CODEC_ID_MP2,
4606ac8d 1248#endif
d7425f59 1249 CODEC_ID_MSMPEG4V3,
de6d9b64
FB
1250 asf_write_header,
1251 asf_write_packet,
1252 asf_write_trailer,
de6d9b64 1253};
c9a65ca8 1254
0570bf06 1255static AVOutputFormat asf_stream_oformat = {
8b3c13f9
PG
1256 "asf_stream",
1257 "asf format",
c6c11cb6 1258 "video/x-ms-asf",
8b3c13f9
PG
1259 "asf,wmv",
1260 sizeof(ASFContext),
1261#ifdef CONFIG_MP3LAME
1262 CODEC_ID_MP3LAME,
1263#else
1264 CODEC_ID_MP2,
1265#endif
d7425f59 1266 CODEC_ID_MSMPEG4V3,
8b3c13f9
PG
1267 asf_write_stream_header,
1268 asf_write_packet,
1269 asf_write_trailer,
1270};
1271
c9a65ca8
FB
1272int asf_init(void)
1273{
1274 av_register_input_format(&asf_iformat);
1275 av_register_output_format(&asf_oformat);
8b3c13f9 1276 av_register_output_format(&asf_stream_oformat);
c9a65ca8
FB
1277 return 0;
1278}