3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/dict.h"
24 #include "avio_internal.h"
33 #define ASF_INDEXED_INTERVAL 10000000
34 #define ASF_INDEX_BLOCK 600
36 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
37 #define ASF_PACKET_ERROR_CORRECTION_FLAGS \
38 (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
39 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
41 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
42 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
44 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
47 #define ASF_PPI_PROPERTY_FLAGS \
48 (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
49 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
50 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
51 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE)
53 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
55 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
57 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
58 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
60 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
61 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
63 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
64 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
66 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
67 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
70 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
71 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
73 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
74 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
76 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
77 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
79 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
80 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
83 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
84 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
86 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
87 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
89 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
90 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
92 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
93 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
96 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
97 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
99 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
100 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
102 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
103 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
105 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
106 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
109 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
110 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
112 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
113 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
115 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
116 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
118 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
119 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
122 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
123 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
125 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
126 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
128 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
129 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
131 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
132 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
135 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
136 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
138 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
139 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
141 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
142 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
145 #define PACKET_HEADER_MIN_SIZE \
146 (ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
147 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
148 1 + /* Length Type Flags */ \
149 1 + /* Property Flags */ \
150 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
151 ASF_PPI_SEQUENCE_FIELD_SIZE + \
152 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
153 4 + /* Send Time Field */ \
154 2) /* Duration Field */
156 // Replicated Data shall be at least 8 bytes long.
157 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
159 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
160 (1 + /* Stream Number */ \
161 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
162 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
163 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
164 ASF_PAYLOAD_REPLICATED_DATA_LENGTH)
166 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
167 (1 + /* Stream Number */ \
168 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
169 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
170 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
171 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
172 ASF_PAYLOAD_LENGTH_FIELD_SIZE)
174 #define SINGLE_PAYLOAD_DATA_LENGTH \
176 PACKET_HEADER_MIN_SIZE - \
177 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
179 #define MULTI_PAYLOAD_CONSTANT \
181 PACKET_HEADER_MIN_SIZE - \
182 1 - /* Payload Flags */ \
183 2 * PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS)
185 #define DATA_HEADER_SIZE 50
190 ASFStream streams[128]; ///< it's max number and it's not that big
191 /* non streamed additonnal info */
192 uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
193 int64_t duration; ///< in 100ns units
195 unsigned char multi_payloads_present;
196 int packet_size_left;
197 int packet_timestamp_start;
198 int packet_timestamp_end;
199 unsigned int packet_nb_payloads;
200 uint8_t packet_buf[PACKET_SIZE];
202 /* only for reading */
203 uint64_t data_offset; ///< beginning of the first data packet
205 int64_t last_indexed_pts;
207 uint32_t nb_index_count;
208 uint32_t nb_index_memory_alloc;
209 uint16_t maximum_packet;
212 static const AVCodecTag codec_asf_bmp_tags[] = {
213 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
214 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
215 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
216 { AV_CODEC_ID_NONE, 0 },
219 #define PREROLL_TIME 3100
221 static void put_guid(AVIOContext *s, const ff_asf_guid *g)
223 assert(sizeof(*g) == 16);
224 avio_write(s, *g, sizeof(*g));
227 static void put_str16(AVIOContext *s, const char *tag)
231 AVIOContext *dyn_buf;
232 if (avio_open_dyn_buf(&dyn_buf) < 0)
235 avio_put_str16le(dyn_buf, tag);
236 len = avio_close_dyn_buf(dyn_buf, &pb);
238 avio_write(s, pb, len);
242 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
252 /* update header size */
253 static void end_header(AVIOContext *pb, int64_t pos)
257 pos1 = avio_tell(pb);
258 avio_seek(pb, pos + 16, SEEK_SET);
259 avio_wl64(pb, pos1 - pos);
260 avio_seek(pb, pos1, SEEK_SET);
263 /* write an asf chunk (only used in streaming case) */
264 static void put_chunk(AVFormatContext *s, int type,
265 int payload_length, int flags)
267 ASFContext *asf = s->priv_data;
268 AVIOContext *pb = s->pb;
271 length = payload_length + 8;
273 avio_wl16(pb, length); // size
274 avio_wl32(pb, asf->seqno); // sequence number
275 avio_wl16(pb, flags); // unknown bytes
276 avio_wl16(pb, length); // size_confirm
280 /* convert from unix to windows time */
281 static int64_t unix_to_file_time(int ti)
285 t = ti * INT64_C(10000000);
286 t += INT64_C(116444736000000000);
290 /* write the header (used two times if non streamed) */
291 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
292 int64_t data_chunk_size)
294 ASFContext *asf = s->priv_data;
295 AVIOContext *pb = s->pb;
296 AVDictionaryEntry *tags[5];
297 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
301 int64_t header_offset, cur_pos, hpos;
305 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
307 tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
308 tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
309 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
310 tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
311 tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
313 duration = asf->duration + PREROLL_TIME * 10000;
314 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
315 metadata_count = av_dict_count(s->metadata);
318 for (n = 0; n < s->nb_streams; n++) {
319 enc = s->streams[n]->codec;
321 avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
323 bit_rate += enc->bit_rate;
326 if (asf->is_streamed) {
327 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
330 put_guid(pb, &ff_asf_header);
331 avio_wl64(pb, -1); /* header length, will be patched after */
332 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
333 avio_w8(pb, 1); /* ??? */
334 avio_w8(pb, 2); /* ??? */
337 header_offset = avio_tell(pb);
338 hpos = put_header(pb, &ff_asf_file_header);
339 put_guid(pb, &ff_asf_my_guid);
340 avio_wl64(pb, file_size);
342 avio_wl64(pb, unix_to_file_time(file_time));
343 avio_wl64(pb, asf->nb_packets); /* number of packets */
344 avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
345 avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
346 avio_wl64(pb, PREROLL_TIME); /* start time stamp */
347 avio_wl32(pb, (asf->is_streamed || !pb->seekable) ? 3 : 2); /* ??? */
348 avio_wl32(pb, s->packet_size); /* packet size */
349 avio_wl32(pb, s->packet_size); /* packet size */
350 avio_wl32(pb, bit_rate); /* Nominal data rate in bps */
351 end_header(pb, hpos);
353 /* unknown headers */
354 hpos = put_header(pb, &ff_asf_head1_guid);
355 put_guid(pb, &ff_asf_head2_guid);
358 end_header(pb, hpos);
360 /* title and other infos */
364 AVIOContext *dyn_buf;
366 if (avio_open_dyn_buf(&dyn_buf) < 0)
367 return AVERROR(ENOMEM);
369 hpos = put_header(pb, &ff_asf_comment_header);
371 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
372 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
375 len = avio_close_dyn_buf(dyn_buf, &buf);
376 avio_write(pb, buf, len);
378 end_header(pb, hpos);
380 if (metadata_count) {
381 AVDictionaryEntry *tag = NULL;
382 hpos = put_header(pb, &ff_asf_extended_content_header);
383 avio_wl16(pb, metadata_count);
384 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
385 put_str16(pb, tag->key);
387 put_str16(pb, tag->value);
389 end_header(pb, hpos);
393 for (n = 0; n < s->nb_streams; n++) {
395 // ASFStream *stream = &asf->streams[n];
397 enc = s->streams[n]->codec;
398 asf->streams[n].num = n + 1;
399 asf->streams[n].seq = 0;
401 switch (enc->codec_type) {
402 case AVMEDIA_TYPE_AUDIO:
404 extra_size = 18 + wav_extra_size;
408 case AVMEDIA_TYPE_VIDEO:
409 wav_extra_size = enc->extradata_size;
410 extra_size = 0x33 + wav_extra_size;
415 hpos = put_header(pb, &ff_asf_stream_header);
416 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
417 put_guid(pb, &ff_asf_audio_stream);
418 put_guid(pb, &ff_asf_audio_conceal_spread);
420 put_guid(pb, &ff_asf_video_stream);
421 put_guid(pb, &ff_asf_video_conceal_none);
423 avio_wl64(pb, 0); /* ??? */
424 es_pos = avio_tell(pb);
425 avio_wl32(pb, extra_size); /* wav header len */
426 avio_wl32(pb, extra_size2); /* additional data len */
427 avio_wl16(pb, n + 1); /* stream number */
428 avio_wl32(pb, 0); /* ??? */
430 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
431 /* WAVEFORMATEX header */
432 int wavsize = ff_put_wav_header(pb, enc);
436 if (wavsize != extra_size) {
437 cur_pos = avio_tell(pb);
438 avio_seek(pb, es_pos, SEEK_SET);
439 avio_wl32(pb, wavsize); /* wav header len */
440 avio_seek(pb, cur_pos, SEEK_SET);
442 /* ERROR Correction */
444 if (enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align) {
445 avio_wl16(pb, 0x0190);
446 avio_wl16(pb, 0x0190);
448 avio_wl16(pb, enc->block_align);
449 avio_wl16(pb, enc->block_align);
454 avio_wl32(pb, enc->width);
455 avio_wl32(pb, enc->height);
456 avio_w8(pb, 2); /* ??? */
457 avio_wl16(pb, 40 + enc->extradata_size); /* size */
459 /* BITMAPINFOHEADER header */
460 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
462 end_header(pb, hpos);
467 hpos = put_header(pb, &ff_asf_codec_comment_header);
468 put_guid(pb, &ff_asf_codec_comment1_header);
469 avio_wl32(pb, s->nb_streams);
470 for (n = 0; n < s->nb_streams; n++) {
475 AVIOContext *dyn_buf;
477 enc = s->streams[n]->codec;
478 p = avcodec_find_encoder(enc->codec_id);
480 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
482 else if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
487 if (enc->codec_id == AV_CODEC_ID_WMAV2)
488 desc = "Windows Media Audio V8";
490 desc = p ? p->name : enc->codec_name;
492 if (avio_open_dyn_buf(&dyn_buf) < 0)
493 return AVERROR(ENOMEM);
495 avio_put_str16le(dyn_buf, desc);
496 len = avio_close_dyn_buf(dyn_buf, &buf);
497 avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
499 avio_write(pb, buf, len);
502 avio_wl16(pb, 0); /* no parameters */
505 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
507 avio_wl16(pb, enc->codec_tag);
510 avio_wl32(pb, enc->codec_tag);
515 end_header(pb, hpos);
517 /* patch the header size fields */
519 cur_pos = avio_tell(pb);
520 header_size = cur_pos - header_offset;
521 if (asf->is_streamed) {
522 header_size += 8 + 30 + DATA_HEADER_SIZE;
524 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
525 avio_wl16(pb, header_size);
526 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
527 avio_wl16(pb, header_size);
529 header_size -= 8 + 30 + DATA_HEADER_SIZE;
531 header_size += 24 + 6;
532 avio_seek(pb, header_offset - 14, SEEK_SET);
533 avio_wl64(pb, header_size);
534 avio_seek(pb, cur_pos, SEEK_SET);
536 /* movie chunk, followed by packets of packet_size */
537 asf->data_offset = cur_pos;
538 put_guid(pb, &ff_asf_data_header);
539 avio_wl64(pb, data_chunk_size);
540 put_guid(pb, &ff_asf_my_guid);
541 avio_wl64(pb, asf->nb_packets); /* nb packets */
542 avio_w8(pb, 1); /* ??? */
543 avio_w8(pb, 1); /* ??? */
547 static int asf_write_header(AVFormatContext *s)
549 ASFContext *asf = s->priv_data;
551 s->packet_size = PACKET_SIZE;
554 asf->last_indexed_pts = 0;
555 asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
556 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
557 asf->nb_index_count = 0;
558 asf->maximum_packet = 0;
560 /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
561 * data_size - asf->data_offset at the moment this function is done.
562 * It is needed to use asf as a streamable format. */
563 if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
570 asf->packet_nb_payloads = 0;
571 asf->packet_timestamp_start = -1;
572 asf->packet_timestamp_end = -1;
573 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
574 NULL, NULL, NULL, NULL);
579 static int asf_write_stream_header(AVFormatContext *s)
581 ASFContext *asf = s->priv_data;
583 asf->is_streamed = 1;
585 return asf_write_header(s);
588 static int put_payload_parsing_info(AVFormatContext *s,
589 unsigned sendtime, unsigned duration,
590 int nb_payloads, int padsize)
592 ASFContext *asf = s->priv_data;
593 AVIOContext *pb = s->pb;
595 int64_t start = avio_tell(pb);
597 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
599 padsize -= PACKET_HEADER_MIN_SIZE;
600 if (asf->multi_payloads_present)
602 assert(padsize >= 0);
604 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
605 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
608 if (asf->multi_payloads_present)
609 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
613 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
615 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
617 avio_w8(pb, iLengthTypeFlags);
619 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
621 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
622 avio_wl16(pb, padsize - 2);
623 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
624 avio_w8(pb, padsize - 1);
626 avio_wl32(pb, sendtime);
627 avio_wl16(pb, duration);
628 if (asf->multi_payloads_present)
629 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
631 ppi_size = avio_tell(pb) - start;
636 static void flush_packet(AVFormatContext *s)
638 ASFContext *asf = s->priv_data;
639 int packet_hdr_size, packet_filled_size;
641 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
643 if (asf->is_streamed)
644 put_chunk(s, 0x4424, s->packet_size, 0);
646 packet_hdr_size = put_payload_parsing_info(s,
647 asf->packet_timestamp_start,
648 asf->packet_timestamp_end -
649 asf->packet_timestamp_start,
650 asf->packet_nb_payloads,
651 asf->packet_size_left);
653 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
654 assert(packet_hdr_size <= asf->packet_size_left);
655 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
657 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
661 asf->packet_nb_payloads = 0;
662 asf->packet_timestamp_start = -1;
663 asf->packet_timestamp_end = -1;
664 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
665 NULL, NULL, NULL, NULL);
668 static void put_payload_header(AVFormatContext *s, ASFStream *stream,
669 int presentation_time, int m_obj_size,
670 int m_obj_offset, int payload_len, int flags)
672 ASFContext *asf = s->priv_data;
673 AVIOContext *pb = &asf->pb;
677 if (flags & AV_PKT_FLAG_KEY)
678 val |= ASF_PL_FLAG_KEY_FRAME;
681 avio_w8(pb, stream->seq); // Media object number
682 avio_wl32(pb, m_obj_offset); // Offset Into Media Object
684 // Replicated Data shall be at least 8 bytes long.
685 // The first 4 bytes of data shall contain the
686 // Size of the Media Object that the payload belongs to.
687 // The next 4 bytes of data shall contain the
688 // Presentation Time for the media object that the payload belongs to.
689 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
691 avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
692 avio_wl32(pb, presentation_time); // Replicated Data - Presentation Time
694 if (asf->multi_payloads_present) {
695 avio_wl16(pb, payload_len); // payload length
699 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
700 int timestamp, const uint8_t *buf,
701 int m_obj_size, int flags)
703 ASFContext *asf = s->priv_data;
704 int m_obj_offset, payload_len, frag_len1;
707 while (m_obj_offset < m_obj_size) {
708 payload_len = m_obj_size - m_obj_offset;
709 if (asf->packet_timestamp_start == -1) {
710 asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
712 asf->packet_size_left = PACKET_SIZE;
713 if (asf->multi_payloads_present) {
714 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
716 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
718 asf->packet_timestamp_start = timestamp;
721 frag_len1 = asf->packet_size_left -
722 PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS -
723 PACKET_HEADER_MIN_SIZE - 1;
725 if (frag_len1 < payload_len &&
726 avst->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
732 if (payload_len > frag_len1)
733 payload_len = frag_len1;
734 else if (payload_len == (frag_len1 - 1))
735 payload_len = frag_len1 - 2; // additional byte need to put padding length
737 put_payload_header(s, stream, timestamp + PREROLL_TIME,
738 m_obj_size, m_obj_offset, payload_len, flags);
739 avio_write(&asf->pb, buf, payload_len);
741 if (asf->multi_payloads_present)
742 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
744 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
745 asf->packet_timestamp_end = timestamp;
747 asf->packet_nb_payloads++;
751 m_obj_offset += payload_len;
754 if (!asf->multi_payloads_present)
756 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
762 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
764 ASFContext *asf = s->priv_data;
767 AVCodecContext *codec;
768 int64_t packet_st, pts;
770 int flags = pkt->flags;
772 codec = s->streams[pkt->stream_index]->codec;
773 stream = &asf->streams[pkt->stream_index];
775 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
776 flags &= ~AV_PKT_FLAG_KEY;
778 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
779 assert(pts != AV_NOPTS_VALUE);
780 duration = pts * 10000;
781 asf->duration = FFMAX(asf->duration, duration + pkt->duration * 10000);
783 packet_st = asf->nb_packets;
784 put_frame(s, stream, s->streams[pkt->stream_index],
785 pkt->dts, pkt->data, pkt->size, flags);
788 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
789 start_sec = (int)(duration / INT64_C(10000000));
790 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
791 for (i = asf->nb_index_count; i < start_sec; i++) {
792 if (i >= asf->nb_index_memory_alloc) {
794 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
795 if ((err = av_reallocp_array(&asf->index_ptr,
796 asf->nb_index_memory_alloc,
797 sizeof(*asf->index_ptr))) < 0) {
798 asf->nb_index_memory_alloc = 0;
803 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
804 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets - packet_st);
805 asf->maximum_packet = FFMAX(asf->maximum_packet,
806 (uint16_t)(asf->nb_packets - packet_st));
808 asf->nb_index_count = start_sec;
809 asf->last_indexed_pts = duration;
815 static int asf_write_index(AVFormatContext *s, ASFIndex *index,
816 uint16_t max, uint32_t count)
818 AVIOContext *pb = s->pb;
821 put_guid(pb, &ff_asf_simple_index_header);
822 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
823 put_guid(pb, &ff_asf_my_guid);
824 avio_wl64(pb, ASF_INDEXED_INTERVAL);
826 avio_wl32(pb, count);
827 for (i = 0; i < count; i++) {
828 avio_wl32(pb, index[i].packet_number);
829 avio_wl16(pb, index[i].packet_count);
835 static int asf_write_trailer(AVFormatContext *s)
837 ASFContext *asf = s->priv_data;
838 int64_t file_size, data_size;
840 /* flush the current packet */
841 if (asf->pb.buf_ptr > asf->pb.buffer)
845 data_size = avio_tell(s->pb);
846 if ((!asf->is_streamed) && (asf->nb_index_count != 0))
847 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
850 if (asf->is_streamed || !s->pb->seekable) {
851 put_chunk(s, 0x4524, 0, 0); /* end of stream */
853 /* rewrite an updated header */
854 file_size = avio_tell(s->pb);
855 avio_seek(s->pb, 0, SEEK_SET);
856 asf_write_header1(s, file_size, data_size - asf->data_offset);
859 av_free(asf->index_ptr);
864 AVOutputFormat ff_asf_muxer = {
866 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
867 .mime_type = "video/x-ms-asf",
868 .extensions = "asf,wmv,wma",
869 .priv_data_size = sizeof(ASFContext),
870 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
871 .video_codec = AV_CODEC_ID_MSMPEG4V3,
872 .write_header = asf_write_header,
873 .write_packet = asf_write_packet,
874 .write_trailer = asf_write_trailer,
875 .flags = AVFMT_GLOBALHEADER,
876 .codec_tag = (const AVCodecTag * const []) {
877 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
880 #endif /* CONFIG_ASF_MUXER */
882 #if CONFIG_ASF_STREAM_MUXER
883 AVOutputFormat ff_asf_stream_muxer = {
884 .name = "asf_stream",
885 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
886 .mime_type = "video/x-ms-asf",
887 .extensions = "asf,wmv,wma",
888 .priv_data_size = sizeof(ASFContext),
889 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
890 .video_codec = AV_CODEC_ID_MSMPEG4V3,
891 .write_header = asf_write_stream_header,
892 .write_packet = asf_write_packet,
893 .write_trailer = asf_write_trailer,
894 .flags = AVFMT_GLOBALHEADER,
895 .codec_tag = (const AVCodecTag * const []) {
896 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
899 #endif /* CONFIG_ASF_STREAM_MUXER */