2 * ASF compatible encoder and decoder.
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
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
22 #include "mpegaudio.h"
24 #define PACKET_SIZE 3200
25 #define PACKET_HEADER_SIZE 12
26 #define FRAME_HEADER_SIZE 17
43 /* non streamed additonnal info */
46 INT64 duration
; /* in 100ns units */
49 int packet_timestamp_start
;
50 int packet_timestamp_end
;
52 UINT8 packet_buf
[PACKET_SIZE
];
54 /* only for reading */
65 static const GUID asf_header
= {
66 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
69 static const GUID file_header
= {
70 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
73 static const GUID stream_header
= {
74 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
77 static const GUID audio_stream
= {
78 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
81 static const GUID audio_conceal_none
= {
82 // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
83 // New value lifted from avifile
84 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b },
87 static const GUID video_stream
= {
88 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
91 static const GUID video_conceal_none
= {
92 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
96 static const GUID comment_header
= {
97 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
100 static const GUID codec_comment_header
= {
101 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
103 static const GUID codec_comment1_header
= {
104 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
107 static const GUID data_header
= {
108 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
111 static const GUID index_guid
= {
112 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
115 static const GUID head1_guid
= {
116 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
119 static const GUID head2_guid
= {
120 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
123 /* I am not a number !!! This GUID is the one found on the PC used to
124 generate the stream */
125 static const GUID my_guid
= {
126 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
129 CodecTag codec_asf_bmp_tags
[] = {
130 { CODEC_ID_H263
, MKTAG('H', '2', '6', '3') },
131 { CODEC_ID_H263P
, MKTAG('H', '2', '6', '3') },
132 { CODEC_ID_H263I
, MKTAG('I', '2', '6', '3') }, /* intel h263 */
133 { CODEC_ID_MJPEG
, MKTAG('M', 'J', 'P', 'G') },
134 { CODEC_ID_MPEG4
, MKTAG('D', 'I', 'V', 'X') },
135 { CODEC_ID_MPEG4
, MKTAG('d', 'i', 'v', 'x') },
136 { CODEC_ID_MPEG4
, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
137 { CODEC_ID_MSMPEG4V3
, MKTAG('M', 'P', '4', '3') }, /* default signature when using MSMPEG4 */
138 { CODEC_ID_MSMPEG4V3
, MKTAG('D', 'I', 'V', '3') },
139 { CODEC_ID_MSMPEG4V2
, MKTAG('M', 'P', '4', '2') },
140 { CODEC_ID_MSMPEG4V1
, MKTAG('M', 'P', 'G', '4') },
141 { CODEC_ID_WMV1
, MKTAG('W', 'M', 'V', '1') },
147 static void put_guid(ByteIOContext
*s
, const GUID
*g
)
155 put_byte(s
, g
->v4
[i
]);
158 static void put_str16(ByteIOContext
*s
, const char *tag
)
162 put_le16(s
,strlen(tag
) + 1);
171 static void put_str16_nolen(ByteIOContext
*s
, const char *tag
)
183 static INT64
put_header(ByteIOContext
*pb
, const GUID
*g
)
193 /* update header size */
194 static void end_header(ByteIOContext
*pb
, INT64 pos
)
198 pos1
= url_ftell(pb
);
199 url_fseek(pb
, pos
+ 16, SEEK_SET
);
200 put_le64(pb
, pos1
- pos
);
201 url_fseek(pb
, pos1
, SEEK_SET
);
204 /* write an asf chunk (only used in streaming case) */
205 static void put_chunk(AVFormatContext
*s
, int type
, int payload_length
, int flags
)
207 ASFContext
*asf
= s
->priv_data
;
208 ByteIOContext
*pb
= &s
->pb
;
211 length
= payload_length
+ 8;
213 put_le16(pb
, length
);
214 put_le32(pb
, asf
->seqno
);
215 put_le16(pb
, flags
); /* unknown bytes */
216 put_le16(pb
, length
);
220 /* convert from unix to windows time */
221 static INT64
unix_to_file_time(int ti
)
225 t
= ti
* INT64_C(10000000);
226 t
+= INT64_C(116444736000000000);
230 /* write the header (used two times if non streamed) */
231 static int asf_write_header1(AVFormatContext
*s
, INT64 file_size
, INT64 data_chunk_size
)
233 ASFContext
*asf
= s
->priv_data
;
234 ByteIOContext
*pb
= &s
->pb
;
235 int header_size
, n
, extra_size
, extra_size2
, wav_extra_size
, file_time
;
238 INT64 header_offset
, cur_pos
, hpos
;
241 has_title
= (s
->title
[0] || s
->author
[0] || s
->copyright
[0] || s
->comment
[0]);
244 for(n
=0;n
<s
->nb_streams
;n
++) {
245 enc
= &s
->streams
[n
]->codec
;
247 bit_rate
+= enc
->bit_rate
;
250 if (url_is_streamed(&s
->pb
)) {
251 put_chunk(s
, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
254 put_guid(pb
, &asf_header
);
255 put_le64(pb
, 0); /* header length, will be patched after */
256 put_le32(pb
, 3 + has_title
+ s
->nb_streams
); /* number of chunks in header */
257 put_byte(pb
, 1); /* ??? */
258 put_byte(pb
, 2); /* ??? */
261 header_offset
= url_ftell(pb
);
262 hpos
= put_header(pb
, &file_header
);
263 put_guid(pb
, &my_guid
);
264 put_le64(pb
, file_size
);
266 put_le64(pb
, unix_to_file_time(file_time
));
267 put_le64(pb
, asf
->nb_packets
); /* number of packets */
268 put_le64(pb
, asf
->duration
); /* end time stamp (in 100ns units) */
269 put_le64(pb
, asf
->duration
); /* duration (in 100ns units) */
270 put_le32(pb
, 0); /* start time stamp */
271 put_le32(pb
, 0); /* ??? */
272 put_le32(pb
, url_is_streamed(&s
->pb
) ?
1 : 0); /* ??? */
273 put_le32(pb
, asf
->packet_size
); /* packet size */
274 put_le32(pb
, asf
->packet_size
); /* packet size */
275 put_le32(pb
, bit_rate
); /* Nominal data rate in bps */
276 end_header(pb
, hpos
);
278 /* unknown headers */
279 hpos
= put_header(pb
, &head1_guid
);
280 put_guid(pb
, &head2_guid
);
283 end_header(pb
, hpos
);
285 /* title and other infos */
287 hpos
= put_header(pb
, &comment_header
);
288 put_le16(pb
, 2 * (strlen(s
->title
) + 1));
289 put_le16(pb
, 2 * (strlen(s
->author
) + 1));
290 put_le16(pb
, 2 * (strlen(s
->copyright
) + 1));
291 put_le16(pb
, 2 * (strlen(s
->comment
) + 1));
293 put_str16_nolen(pb
, s
->title
);
294 put_str16_nolen(pb
, s
->author
);
295 put_str16_nolen(pb
, s
->copyright
);
296 put_str16_nolen(pb
, s
->comment
);
297 end_header(pb
, hpos
);
301 for(n
=0;n
<s
->nb_streams
;n
++) {
303 ASFStream
*stream
= &asf
->streams
[n
];
305 enc
= &s
->streams
[n
]->codec
;
306 asf
->streams
[n
].num
= n
+ 1;
307 asf
->streams
[n
].seq
= 0;
309 switch(enc
->codec_type
) {
310 case CODEC_TYPE_AUDIO
:
312 extra_size
= 18 + wav_extra_size
;
314 /* Init the ticker */
315 ticker_init(&stream
->pts_ticker
,
317 1000 * enc
->frame_size
);
320 case CODEC_TYPE_VIDEO
:
324 /* Init the ticker */
325 ticker_init(&stream
->pts_ticker
,
327 1000 * FRAME_RATE_BASE
);
331 hpos
= put_header(pb
, &stream_header
);
332 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
333 put_guid(pb
, &audio_stream
);
334 put_guid(pb
, &audio_conceal_none
);
336 put_guid(pb
, &video_stream
);
337 put_guid(pb
, &video_conceal_none
);
339 put_le64(pb
, 0); /* ??? */
340 es_pos
= url_ftell(pb
);
341 put_le32(pb
, extra_size
); /* wav header len */
342 put_le32(pb
, extra_size2
); /* additional data len */
343 put_le16(pb
, n
+ 1); /* stream number */
344 put_le32(pb
, 0); /* ??? */
346 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
347 /* WAVEFORMATEX header */
348 int wavsize
= put_wav_header(pb
, enc
);
352 if (wavsize
!= extra_size
) {
353 cur_pos
= url_ftell(pb
);
354 url_fseek(pb
, es_pos
, SEEK_SET
);
355 put_le32(pb
, wavsize
); /* wav header len */
356 url_fseek(pb
, cur_pos
, SEEK_SET
);
359 put_le32(pb
, enc
->width
);
360 put_le32(pb
, enc
->height
);
361 put_byte(pb
, 2); /* ??? */
362 put_le16(pb
, 40); /* size */
364 /* BITMAPINFOHEADER header */
365 put_bmp_header(pb
, enc
, codec_asf_bmp_tags
);
367 end_header(pb
, hpos
);
372 hpos
= put_header(pb
, &codec_comment_header
);
373 put_guid(pb
, &codec_comment1_header
);
374 put_le32(pb
, s
->nb_streams
);
375 for(n
=0;n
<s
->nb_streams
;n
++) {
378 enc
= &s
->streams
[n
]->codec
;
379 p
= avcodec_find_encoder(enc
->codec_id
);
381 put_le16(pb
, asf
->streams
[n
].num
);
382 put_str16(pb
, p ? p
->name
: enc
->codec_name
);
383 put_le16(pb
, 0); /* no parameters */
385 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
387 put_le16(pb
, codec_get_tag(codec_wav_tags
, enc
->codec_id
));
390 put_le32(pb
, codec_get_tag(codec_asf_bmp_tags
, enc
->codec_id
));
393 end_header(pb
, hpos
);
395 /* patch the header size fields */
397 cur_pos
= url_ftell(pb
);
398 header_size
= cur_pos
- header_offset
;
399 if (url_is_streamed(&s
->pb
)) {
400 header_size
+= 8 + 30 + 50;
402 url_fseek(pb
, header_offset
- 10 - 30, SEEK_SET
);
403 put_le16(pb
, header_size
);
404 url_fseek(pb
, header_offset
- 2 - 30, SEEK_SET
);
405 put_le16(pb
, header_size
);
407 header_size
-= 8 + 30 + 50;
409 header_size
+= 24 + 6;
410 url_fseek(pb
, header_offset
- 14, SEEK_SET
);
411 put_le64(pb
, header_size
);
412 url_fseek(pb
, cur_pos
, SEEK_SET
);
414 /* movie chunk, followed by packets of packet_size */
415 asf
->data_offset
= cur_pos
;
416 put_guid(pb
, &data_header
);
417 put_le64(pb
, data_chunk_size
);
418 put_guid(pb
, &my_guid
);
419 put_le64(pb
, asf
->nb_packets
); /* nb packets */
420 put_byte(pb
, 1); /* ??? */
421 put_byte(pb
, 1); /* ??? */
425 static int asf_write_header(AVFormatContext
*s
)
427 ASFContext
*asf
= s
->priv_data
;
429 asf
->packet_size
= PACKET_SIZE
;
432 if (asf_write_header1(s
, 0, 50) < 0) {
437 put_flush_packet(&s
->pb
);
439 asf
->packet_nb_frames
= 0;
440 asf
->packet_timestamp_start
= -1;
441 asf
->packet_timestamp_end
= -1;
442 asf
->packet_size_left
= asf
->packet_size
- PACKET_HEADER_SIZE
;
443 init_put_byte(&asf
->pb
, asf
->packet_buf
, asf
->packet_size
, 1,
444 NULL
, NULL
, NULL
, NULL
);
449 /* write a fixed size packet */
450 static int put_packet(AVFormatContext
*s
,
451 unsigned int timestamp
, unsigned int duration
,
452 int nb_frames
, int padsize
)
454 ASFContext
*asf
= s
->priv_data
;
455 ByteIOContext
*pb
= &s
->pb
;
458 if (url_is_streamed(&s
->pb
)) {
459 put_chunk(s
, 0x4424, asf
->packet_size
, 0);
465 flags
= 0x01; /* nb segments present */
472 put_byte(pb
, flags
); /* flags */
475 put_le16(pb
, padsize
- 2);
477 put_byte(pb
, padsize
- 1);
478 put_le32(pb
, timestamp
);
479 put_le16(pb
, duration
);
480 put_byte(pb
, nb_frames
| 0x80);
482 return PACKET_HEADER_SIZE
+ ((flags
& 0x18) >> 3);
485 static void flush_packet(AVFormatContext
*s
)
487 ASFContext
*asf
= s
->priv_data
;
490 hdr_size
= put_packet(s
, asf
->packet_timestamp_start
,
491 asf
->packet_timestamp_end
- asf
->packet_timestamp_start
,
492 asf
->packet_nb_frames
, asf
->packet_size_left
);
494 /* Clear out the padding bytes */
495 ptr
= asf
->packet_size
- hdr_size
- asf
->packet_size_left
;
496 memset(asf
->packet_buf
+ ptr
, 0, asf
->packet_size_left
);
498 put_buffer(&s
->pb
, asf
->packet_buf
, asf
->packet_size
- hdr_size
);
500 put_flush_packet(&s
->pb
);
502 asf
->packet_nb_frames
= 0;
503 asf
->packet_timestamp_start
= -1;
504 asf
->packet_timestamp_end
= -1;
505 asf
->packet_size_left
= asf
->packet_size
- PACKET_HEADER_SIZE
;
506 init_put_byte(&asf
->pb
, asf
->packet_buf
, asf
->packet_size
, 1,
507 NULL
, NULL
, NULL
, NULL
);
510 static void put_frame_header(AVFormatContext
*s
, ASFStream
*stream
, int timestamp
,
511 int payload_size
, int frag_offset
, int frag_len
)
513 ASFContext
*asf
= s
->priv_data
;
514 ByteIOContext
*pb
= &asf
->pb
;
518 if (s
->streams
[val
- 1]->codec
.key_frame
/* && frag_offset == 0 */)
521 put_byte(pb
, stream
->seq
);
522 put_le32(pb
, frag_offset
); /* fragment offset */
523 put_byte(pb
, 0x08); /* flags */
524 put_le32(pb
, payload_size
);
525 put_le32(pb
, timestamp
);
526 put_le16(pb
, frag_len
);
530 /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
532 It is there that you understand that the ASF format is really
533 crap. They have misread the MPEG Systems spec !
535 static void put_frame(AVFormatContext
*s
, ASFStream
*stream
, int timestamp
,
536 UINT8
*buf
, int payload_size
)
538 ASFContext
*asf
= s
->priv_data
;
539 int frag_pos
, frag_len
, frag_len1
;
542 while (frag_pos
< payload_size
) {
543 frag_len
= payload_size
- frag_pos
;
544 frag_len1
= asf
->packet_size_left
- FRAME_HEADER_SIZE
;
546 if (frag_len
> frag_len1
)
547 frag_len
= frag_len1
;
548 put_frame_header(s
, stream
, timestamp
, payload_size
, frag_pos
, frag_len
);
549 put_buffer(&asf
->pb
, buf
, frag_len
);
550 asf
->packet_size_left
-= (frag_len
+ FRAME_HEADER_SIZE
);
551 asf
->packet_timestamp_end
= timestamp
;
552 if (asf
->packet_timestamp_start
== -1)
553 asf
->packet_timestamp_start
= timestamp
;
554 asf
->packet_nb_frames
++;
558 frag_pos
+= frag_len
;
560 /* output the frame if filled */
561 if (asf
->packet_size_left
<= FRAME_HEADER_SIZE
)
568 static int asf_write_packet(AVFormatContext
*s
, int stream_index
,
569 UINT8
*buf
, int size
, int force_pts
)
571 ASFContext
*asf
= s
->priv_data
;
575 AVCodecContext
*codec
;
577 stream
= &asf
->streams
[stream_index
];
578 codec
= &s
->streams
[stream_index
]->codec
;
579 stream
= &asf
->streams
[stream_index
];
581 if (codec
->codec_type
== CODEC_TYPE_AUDIO
) {
582 timestamp
= (int)ticker_abs(&stream
->pts_ticker
, codec
->frame_number
);
583 duration
= (codec
->frame_number
* codec
->frame_size
* INT64_C(10000000)) /
586 timestamp
= (int)ticker_abs(&stream
->pts_ticker
, codec
->frame_number
);
587 duration
= codec
->frame_number
*
588 ((INT64_C(10000000) * FRAME_RATE_BASE
) / codec
->frame_rate
);
590 if (duration
> asf
->duration
)
591 asf
->duration
= duration
;
593 put_frame(s
, stream
, timestamp
, buf
, size
);
597 static int asf_write_trailer(AVFormatContext
*s
)
599 ASFContext
*asf
= s
->priv_data
;
602 /* flush the current packet */
603 if (asf
->pb
.buf_ptr
> asf
->pb
.buffer
)
606 if (url_is_streamed(&s
->pb
)) {
607 put_chunk(s
, 0x4524, 0, 0); /* end of stream */
609 /* rewrite an updated header */
610 file_size
= url_ftell(&s
->pb
);
611 url_fseek(&s
->pb
, 0, SEEK_SET
);
612 asf_write_header1(s
, file_size
, file_size
- asf
->data_offset
);
615 put_flush_packet(&s
->pb
);
619 /**********************************/
625 static void print_guid(const GUID
*g
)
628 printf("0x%08x, 0x%04x, 0x%04x, {", g
->v1
, g
->v2
, g
->v3
);
630 printf(" 0x%02x,", g
->v4
[i
]);
635 static void get_guid(ByteIOContext
*s
, GUID
*g
)
643 g
->v4
[i
] = get_byte(s
);
647 static void get_str16(ByteIOContext
*pb
, char *buf
, int buf_size
)
656 if ((q
- buf
) < buf_size
- 1)
664 static void get_str16_nolen(ByteIOContext
*pb
, int len
, char *buf
, int buf_size
)
672 if ((q
- buf
) < buf_size
- 1)
679 static int asf_probe(AVProbeData
*pd
)
682 const unsigned char *p
;
685 /* check file header */
686 if (pd
->buf_size
<= 32)
689 g
.v1
= p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
691 g
.v2
= p
[0] | (p
[1] << 8);
693 g
.v3
= p
[0] | (p
[1] << 8);
698 if (!memcmp(&g
, &asf_header
, sizeof(GUID
)))
699 return AVPROBE_SCORE_MAX
;
704 static int asf_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
706 ASFContext
*asf
= s
->priv_data
;
708 ByteIOContext
*pb
= &s
->pb
;
715 if (memcmp(&g
, &asf_header
, sizeof(GUID
)))
724 gsize
= get_le64(pb
);
726 printf("%08Lx: ", url_ftell(pb
) - 24);
728 printf(" size=0x%Lx\n", gsize
);
732 if (!memcmp(&g
, &file_header
, sizeof(GUID
))) {
734 get_le64(pb
); /* file size */
735 get_le64(pb
); /* file time */
736 get_le64(pb
); /* nb_packets */
737 get_le64(pb
); /* length 0 in us */
738 get_le64(pb
); /* length 1 in us */
742 asf
->packet_size
= get_le32(pb
);
745 } else if (!memcmp(&g
, &stream_header
, sizeof(GUID
))) {
746 int type
, id
, total_size
;
750 pos1
= url_ftell(pb
);
752 st
= av_mallocz(sizeof(AVStream
));
755 s
->streams
[s
->nb_streams
++] = st
;
756 asf_st
= av_mallocz(sizeof(ASFStream
));
759 st
->priv_data
= asf_st
;
762 if (!memcmp(&g
, &audio_stream
, sizeof(GUID
))) {
763 type
= CODEC_TYPE_AUDIO
;
764 } else if (!memcmp(&g
, &video_stream
, sizeof(GUID
))) {
765 type
= CODEC_TYPE_VIDEO
;
770 total_size
= get_le64(pb
);
773 st
->id
= get_le16(pb
); /* stream id */
775 st
->codec
.codec_type
= type
;
776 if (type
== CODEC_TYPE_AUDIO
) {
778 st
->codec
.codec_tag
= id
;
779 st
->codec
.channels
= get_le16(pb
);
780 st
->codec
.sample_rate
= get_le32(pb
);
781 st
->codec
.bit_rate
= get_le32(pb
) * 8;
782 get_le16(pb
); /* block align */
783 bps
= get_le16(pb
); /* bits per sample */
784 st
->codec
.codec_id
= wav_codec_get_id(id
, bps
);
787 /* We have to init the frame size at some point .... */
788 switch (st
->codec
.codec_id
) {
789 case CODEC_ID_MP3LAME
:
790 st
->codec
.frame_size
= MPA_FRAME_SIZE
;
792 case CODEC_ID_PCM_S16LE
:
793 case CODEC_ID_PCM_S16BE
:
794 case CODEC_ID_PCM_U16LE
:
795 case CODEC_ID_PCM_U16BE
:
796 case CODEC_ID_PCM_S8
:
797 case CODEC_ID_PCM_U8
:
798 case CODEC_ID_PCM_ALAW
:
799 case CODEC_ID_PCM_MULAW
:
800 st
->codec
.frame_size
= 1;
803 /* This is probably wrong, but it prevents a crash later */
804 st
->codec
.frame_size
= 1;
811 size
= get_le16(pb
); /* size */
812 get_le32(pb
); /* size */
813 st
->codec
.width
= get_le32(pb
);
814 st
->codec
.height
= get_le32(pb
);
815 st
->codec
.frame_rate
= 25 * FRAME_RATE_BASE
; /* XXX: find it */
816 get_le16(pb
); /* panes */
817 get_le16(pb
); /* depth */
819 st
->codec
.codec_tag
= tag1
;
820 st
->codec
.codec_id
= codec_get_id(codec_asf_bmp_tags
, tag1
);
821 url_fskip(pb
, size
- 5 * 4);
823 pos2
= url_ftell(pb
);
824 url_fskip(pb
, gsize
- (pos2
- pos1
+ 24));
825 } else if (!memcmp(&g
, &data_header
, sizeof(GUID
))) {
827 } else if (!memcmp(&g
, &comment_header
, sizeof(GUID
))) {
828 int len1
, len2
, len3
, len4
, len5
;
835 get_str16_nolen(pb
, len1
, s
->title
, sizeof(s
->title
));
836 get_str16_nolen(pb
, len2
, s
->author
, sizeof(s
->author
));
837 get_str16_nolen(pb
, len3
, s
->copyright
, sizeof(s
->copyright
));
838 get_str16_nolen(pb
, len4
, s
->comment
, sizeof(s
->comment
));
841 } else if (!memcmp(&g
, &head1_guid
, sizeof(GUID
))) {
846 } else if (!memcmp(&g
, &codec_comment_header
, sizeof(GUID
))) {
856 num
= get_le16(pb
); /* stream number */
857 get_str16(pb
, str
, sizeof(str
));
858 get_str16(pb
, str
, sizeof(str
));
863 if ((q
- tag
) < sizeof(tag
) - 1)
870 } else if (url_feof(pb
)) {
873 url_fseek(pb
, gsize
- 24, SEEK_CUR
);
881 asf
->packet_size_left
= 0;
886 for(i
=0;i
<s
->nb_streams
;i
++) {
887 AVStream
*st
= s
->streams
[i
];
889 av_free(st
->priv_data
);
896 static int asf_get_packet(AVFormatContext
*s
)
898 ASFContext
*asf
= s
->priv_data
;
899 ByteIOContext
*pb
= &s
->pb
;
900 int c
, flags
, timestamp
, hdr_size
;
907 flags
= get_byte(pb
);
909 asf
->packet_padsize
= 0;
911 asf
->packet_padsize
= get_le16(pb
);
913 } else if (flags
& 0x08) {
914 asf
->packet_padsize
= get_byte(pb
);
917 timestamp
= get_le32(pb
);
918 get_le16(pb
); /* duration */
919 get_byte(pb
); /* nb_frames */
921 printf("packet: size=%d padsize=%d\n", asf
->packet_size
, asf
->packet_padsize
);
923 asf
->packet_size_left
= asf
->packet_size
- hdr_size
;
927 static int asf_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
929 ASFContext
*asf
= s
->priv_data
;
932 ByteIOContext
*pb
= &s
->pb
;
933 int ret
, num
, seq
, frag_offset
, payload_size
, frag_len
;
938 if (asf
->packet_size_left
< FRAME_HEADER_SIZE
||
939 asf
->packet_size_left
<= asf
->packet_padsize
) {
941 if (asf
->packet_size_left
)
942 url_fskip(pb
, asf
->packet_size_left
);
943 ret
= asf_get_packet(s
);
947 /* read frame header */
956 frag_offset
= get_le32(pb
);
957 get_byte(pb
); /* flags */
958 payload_size
= get_le32(pb
);
959 timestamp
= get_le32(pb
);
960 frag_len
= get_le16(pb
);
962 printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
963 num
, seq
, payload_size
, frag_offset
, frag_len
);
966 for(i
=0;i
<s
->nb_streams
;i
++) {
971 asf
->packet_size_left
-= FRAME_HEADER_SIZE
+ frag_len
;
972 if (i
== s
->nb_streams
) {
973 /* unhandled packet (should not happen) */
974 url_fskip(pb
, frag_len
);
976 asf_st
= st
->priv_data
;
977 if (asf_st
->frag_offset
== 0) {
979 av_new_packet(&asf_st
->pkt
, payload_size
);
982 asf_st
->pkt
.flags
|= PKT_FLAG_KEY
;
984 asf_st
->pkt
.pts
= timestamp
;
986 if (seq
== asf_st
->seq
&&
987 frag_offset
== asf_st
->frag_offset
) {
988 /* continuing packet */
990 /* cannot continue current packet: free it */
991 av_free_packet(&asf_st
->pkt
);
992 asf_st
->frag_offset
= 0;
993 if (frag_offset
!= 0) {
994 /* cannot create new packet */
995 url_fskip(pb
, frag_len
);
998 /* create new packet */
999 av_new_packet(&asf_st
->pkt
, payload_size
);
1005 get_buffer(pb
, asf_st
->pkt
.data
+ frag_offset
, frag_len
);
1006 asf_st
->frag_offset
+= frag_len
;
1007 /* test if whole packet read */
1008 if (asf_st
->frag_offset
== asf_st
->pkt
.size
) {
1010 asf_st
->pkt
.stream_index
= i
;
1011 asf_st
->frag_offset
= 0;
1012 memcpy(pkt
, &asf_st
->pkt
, sizeof(AVPacket
));
1022 static int asf_read_close(AVFormatContext
*s
)
1024 //ASFContext *asf = s->priv_data;
1027 for(i
=0;i
<s
->nb_streams
;i
++) {
1028 AVStream
*st
= s
->streams
[i
];
1029 av_free(st
->priv_data
);
1035 AVInputFormat asf_iformat
= {
1045 AVOutputFormat asf_oformat
= {
1048 "application/octet-stream",
1051 #ifdef CONFIG_MP3LAME
1064 av_register_input_format(&asf_iformat
);
1065 av_register_output_format(&asf_oformat
);