3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/opt.h"
28 #include "avlanguage.h"
29 #include "avio_internal.h"
34 #define ASF_INDEXED_INTERVAL 10000000
35 #define ASF_INDEX_BLOCK (1<<9)
36 #define ASF_PAYLOADS_PER_PACKET 63
38 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
39 #define ASF_PACKET_ERROR_CORRECTION_FLAGS \
40 (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
41 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
43 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
44 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
46 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
49 #define ASF_PPI_PROPERTY_FLAGS \
50 (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
51 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
52 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
53 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE)
55 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
57 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
59 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
60 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
62 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
63 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
65 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
66 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
68 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
69 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
72 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
73 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
75 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
76 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
78 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
79 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
81 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
82 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
85 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
86 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
88 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
89 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
91 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
92 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
94 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
95 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
98 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
99 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
101 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
102 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
104 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
105 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
107 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
108 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
111 #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))
112 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
114 #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))
115 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
117 #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))
118 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
120 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
121 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
124 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
125 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
127 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
128 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
130 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
131 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
133 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
134 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
137 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
138 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
140 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
141 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
143 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
144 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
147 #define PACKET_HEADER_MIN_SIZE \
148 (ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
149 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
150 1 + /* Length Type Flags */ \
151 1 + /* Property Flags */ \
152 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
153 ASF_PPI_SEQUENCE_FIELD_SIZE + \
154 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
155 4 + /* Send Time Field */ \
156 2) /* Duration Field */
158 // Replicated Data shall be at least 8 bytes long.
159 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
161 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
162 (1 + /* Stream Number */ \
163 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
164 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
165 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
166 ASF_PAYLOAD_REPLICATED_DATA_LENGTH)
168 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
169 (1 + /* Stream Number */ \
170 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
171 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
172 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
173 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
174 ASF_PAYLOAD_LENGTH_FIELD_SIZE)
176 #define SINGLE_PAYLOAD_HEADERS \
177 (PACKET_HEADER_MIN_SIZE + \
178 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
180 #define MULTI_PAYLOAD_HEADERS \
181 (PACKET_HEADER_MIN_SIZE + \
182 1 + /* Payload Flags */ \
183 2 * PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS)
185 #define DATA_HEADER_SIZE 50
187 #define PACKET_SIZE_MAX 65536
188 #define PACKET_SIZE_MIN 100
190 typedef struct ASFPayload {
195 typedef struct ASFStream {
198 /* use for reading */
207 int ds_span; /* descrambling */
213 uint16_t stream_language_index;
216 uint32_t palette[256];
219 ASFPayload payload[8];
222 typedef struct ASFContext {
226 ASFStream streams[128]; ///< it's max number and it's not that big
227 const char *languages[128];
229 int64_t creation_time;
230 /* non-streamed additional info */
231 uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
232 int64_t duration; ///< in 100ns units
234 unsigned char multi_payloads_present;
235 int packet_size_left;
236 int64_t packet_timestamp_start;
237 int64_t packet_timestamp_end;
238 unsigned int packet_nb_payloads;
239 uint8_t packet_buf[PACKET_SIZE_MAX];
241 /* only for reading */
242 uint64_t data_offset; ///< beginning of the first data packet
245 uint32_t nb_index_memory_alloc;
246 uint16_t maximum_packet;
247 uint32_t next_packet_number;
248 uint16_t next_packet_count;
249 uint64_t next_packet_offset;
255 static const AVCodecTag codec_asf_bmp_tags[] = {
256 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
257 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
258 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
259 { AV_CODEC_ID_NONE, 0 },
262 #define PREROLL_TIME 3100
264 static void put_str16(AVIOContext *s, const char *tag)
268 AVIOContext *dyn_buf;
269 if (avio_open_dyn_buf(&dyn_buf) < 0)
272 avio_put_str16le(dyn_buf, tag);
273 len = avio_close_dyn_buf(dyn_buf, &pb);
275 avio_write(s, pb, len);
279 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
289 /* update header size */
290 static void end_header(AVIOContext *pb, int64_t pos)
294 pos1 = avio_tell(pb);
295 avio_seek(pb, pos + 16, SEEK_SET);
296 avio_wl64(pb, pos1 - pos);
297 avio_seek(pb, pos1, SEEK_SET);
300 /* write an asf chunk (only used in streaming case) */
301 static void put_chunk(AVFormatContext *s, int type,
302 int payload_length, int flags)
304 ASFContext *asf = s->priv_data;
305 AVIOContext *pb = s->pb;
308 length = payload_length + 8;
310 avio_wl16(pb, length); // size
311 avio_wl32(pb, asf->seqno); // sequence number
312 avio_wl16(pb, flags); // unknown bytes
313 avio_wl16(pb, length); // size_confirm
317 /* convert from av time to windows time */
318 static int64_t unix_to_file_time(int64_t ti)
322 t = ti * INT64_C(10);
323 t += INT64_C(116444736000000000);
327 static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
330 int32_t send_time = 0;
331 *offset = asf->data_offset + DATA_HEADER_SIZE;
332 for (i = 0; i < asf->next_start_sec; i++) {
333 if (pres_time <= asf->index_ptr[i].send_time)
335 send_time = asf->index_ptr[i].send_time;
336 *offset = asf->index_ptr[i].offset;
339 return send_time / 10000;
342 static int asf_write_markers(AVFormatContext *s)
344 ASFContext *asf = s->priv_data;
345 AVIOContext *pb = s->pb;
347 AVRational scale = {1, 10000000};
348 int64_t hpos = put_header(pb, &ff_asf_marker_header);
350 ff_put_guid(pb, &ff_asf_reserved_4);// ASF spec mandates this reserved value
351 avio_wl32(pb, s->nb_chapters); // markers count
352 avio_wl16(pb, 0); // ASF spec mandates 0 for this
353 avio_wl16(pb, 0); // name length 0, no name given
355 for (i = 0; i < s->nb_chapters; i++) {
356 AVChapter *c = s->chapters[i];
357 AVDictionaryEntry *t = av_dict_get(c->metadata, "title", NULL, 0);
358 int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
360 int32_t send_time = get_send_time(asf, pres_time, &offset);
363 AVIOContext *dyn_buf;
365 if (avio_open_dyn_buf(&dyn_buf) < 0)
366 return AVERROR(ENOMEM);
367 avio_put_str16le(dyn_buf, t->value);
368 len = avio_close_dyn_buf(dyn_buf, &buf);
370 avio_wl64(pb, offset); // offset of the packet with send_time
371 avio_wl64(pb, pres_time + PREROLL_TIME * 10000); // presentation time
372 avio_wl16(pb, 12 + len); // entry length
373 avio_wl32(pb, send_time); // send time
374 avio_wl32(pb, 0); // flags, should be 0
375 avio_wl32(pb, len / 2); // marker desc length in WCHARS!
377 avio_write(pb, buf, len); // marker desc
381 end_header(pb, hpos);
385 /* write the header (used two times if non streamed) */
386 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
387 int64_t data_chunk_size)
389 ASFContext *asf = s->priv_data;
390 AVIOContext *pb = s->pb;
391 AVDictionaryEntry *tags[5];
392 int header_size, n, extra_size, extra_size2, wav_extra_size;
393 int has_title, has_aspect_ratio = 0;
395 AVCodecParameters *par;
396 int64_t header_offset, cur_pos, hpos;
399 int audio_language_counts[128] = { 0 };
401 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
403 tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
404 tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
405 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
406 tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
407 tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
409 duration = asf->duration + PREROLL_TIME * 10000;
410 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
413 if (ff_parse_creation_time_metadata(s, &asf->creation_time, 0) != 0)
414 av_dict_set(&s->metadata, "creation_time", NULL, 0);
417 metadata_count = av_dict_count(s->metadata);
420 for (n = 0; n < s->nb_streams; n++) {
421 AVDictionaryEntry *entry;
422 par = s->streams[n]->codecpar;
424 avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
426 bit_rate += par->bit_rate;
427 if ( par->codec_type == AVMEDIA_TYPE_VIDEO
428 && par->sample_aspect_ratio.num > 0
429 && par->sample_aspect_ratio.den > 0)
432 entry = av_dict_get(s->streams[n]->metadata, "language", NULL, 0);
434 const char *iso6391lang = ff_convert_lang_to(entry->value, AV_LANG_ISO639_1);
437 for (i = 0; i < asf->nb_languages; i++) {
438 if (!strcmp(asf->languages[i], iso6391lang)) {
439 asf->streams[n].stream_language_index = i;
443 if (i >= asf->nb_languages) {
444 asf->languages[asf->nb_languages] = iso6391lang;
445 asf->streams[n].stream_language_index = asf->nb_languages;
448 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
449 audio_language_counts[asf->streams[n].stream_language_index]++;
452 asf->streams[n].stream_language_index = 128;
456 if (asf->is_streamed) {
457 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
460 ff_put_guid(pb, &ff_asf_header);
461 avio_wl64(pb, -1); /* header length, will be patched after */
462 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
463 avio_w8(pb, 1); /* ??? */
464 avio_w8(pb, 2); /* ??? */
467 header_offset = avio_tell(pb);
468 hpos = put_header(pb, &ff_asf_file_header);
469 ff_put_guid(pb, &ff_asf_my_guid);
470 avio_wl64(pb, file_size);
471 avio_wl64(pb, unix_to_file_time(asf->creation_time));
472 avio_wl64(pb, asf->nb_packets); /* number of packets */
473 avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
474 avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
475 avio_wl64(pb, PREROLL_TIME); /* start time stamp */
476 avio_wl32(pb, (asf->is_streamed || !(pb->seekable & AVIO_SEEKABLE_NORMAL)) ? 3 : 2); /* ??? */
477 avio_wl32(pb, s->packet_size); /* packet size */
478 avio_wl32(pb, s->packet_size); /* packet size */
479 avio_wl32(pb, bit_rate ? bit_rate : -1); /* Maximum data rate in bps */
480 end_header(pb, hpos);
482 /* header_extension */
483 hpos = put_header(pb, &ff_asf_head1_guid);
484 ff_put_guid(pb, &ff_asf_head2_guid);
486 avio_wl32(pb, 0); /* length, to be filled later */
487 if (asf->nb_languages) {
490 int nb_audio_languages = 0;
492 hpos2 = put_header(pb, &ff_asf_language_guid);
493 avio_wl16(pb, asf->nb_languages);
494 for (i = 0; i < asf->nb_languages; i++) {
496 avio_put_str16le(pb, asf->languages[i]);
498 end_header(pb, hpos2);
500 for (i = 0; i < asf->nb_languages; i++)
501 if (audio_language_counts[i])
502 nb_audio_languages++;
504 if (nb_audio_languages > 1) {
505 hpos2 = put_header(pb, &ff_asf_group_mutual_exclusion_object);
506 ff_put_guid(pb, &ff_asf_mutex_language);
507 avio_wl16(pb, nb_audio_languages);
508 for (i = 0; i < asf->nb_languages; i++) {
509 if (audio_language_counts[i]) {
510 avio_wl16(pb, audio_language_counts[i]);
511 for (n = 0; n < s->nb_streams; n++)
512 if (asf->streams[n].stream_language_index == i && s->streams[n]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
513 avio_wl16(pb, n + 1);
516 end_header(pb, hpos2);
519 for (n = 0; n < s->nb_streams; n++) {
521 if (asf->streams[n].stream_language_index > 127)
523 es_pos = put_header(pb, &ff_asf_extended_stream_properties_object);
524 avio_wl64(pb, 0); /* start time */
525 avio_wl64(pb, 0); /* end time */
526 avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* data bitrate bps */
527 avio_wl32(pb, 5000); /* buffer size ms */
528 avio_wl32(pb, 0); /* initial buffer fullness */
529 avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* peak data bitrate */
530 avio_wl32(pb, 5000); /* maximum buffer size ms */
531 avio_wl32(pb, 0); /* max initial buffer fullness */
532 avio_wl32(pb, 0); /* max object size */
533 avio_wl32(pb, (!asf->is_streamed && (pb->seekable & AVIO_SEEKABLE_NORMAL)) << 1); /* flags - seekable */
534 avio_wl16(pb, n + 1); /* stream number */
535 avio_wl16(pb, asf->streams[n].stream_language_index); /* language id index */
536 avio_wl64(pb, 0); /* avg time per frame */
537 avio_wl16(pb, 0); /* stream name count */
538 avio_wl16(pb, 0); /* payload extension system count */
539 end_header(pb, es_pos);
542 if (has_aspect_ratio) {
544 hpos2 = put_header(pb, &ff_asf_metadata_header);
545 avio_wl16(pb, 2 * has_aspect_ratio);
546 for (n = 0; n < s->nb_streams; n++) {
547 par = s->streams[n]->codecpar;
548 if ( par->codec_type == AVMEDIA_TYPE_VIDEO
549 && par->sample_aspect_ratio.num > 0
550 && par->sample_aspect_ratio.den > 0) {
551 AVRational sar = par->sample_aspect_ratio;
553 // the stream number is set like this below
554 avio_wl16(pb, n + 1);
555 avio_wl16(pb, 26); // name_len
556 avio_wl16(pb, 3); // value_type
557 avio_wl32(pb, 4); // value_len
558 avio_put_str16le(pb, "AspectRatioX");
559 avio_wl32(pb, sar.num);
561 // the stream number is set like this below
562 avio_wl16(pb, n + 1);
563 avio_wl16(pb, 26); // name_len
564 avio_wl16(pb, 3); // value_type
565 avio_wl32(pb, 4); // value_len
566 avio_put_str16le(pb, "AspectRatioY");
567 avio_wl32(pb, sar.den);
570 end_header(pb, hpos2);
574 pos1 = avio_tell(pb);
575 avio_seek(pb, hpos + 42, SEEK_SET);
576 avio_wl32(pb, pos1 - hpos - 46);
577 avio_seek(pb, pos1, SEEK_SET);
579 end_header(pb, hpos);
581 /* title and other info */
585 AVIOContext *dyn_buf;
587 if (avio_open_dyn_buf(&dyn_buf) < 0)
588 return AVERROR(ENOMEM);
590 hpos = put_header(pb, &ff_asf_comment_header);
592 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
593 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
596 len = avio_close_dyn_buf(dyn_buf, &buf);
597 avio_write(pb, buf, len);
599 end_header(pb, hpos);
601 if (metadata_count) {
602 AVDictionaryEntry *tag = NULL;
603 hpos = put_header(pb, &ff_asf_extended_content_header);
604 avio_wl16(pb, metadata_count);
605 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
606 put_str16(pb, tag->key);
608 put_str16(pb, tag->value);
610 end_header(pb, hpos);
612 /* chapters using ASF markers */
613 if (!asf->is_streamed && s->nb_chapters) {
615 if ((ret = asf_write_markers(s)) < 0)
619 for (n = 0; n < s->nb_streams; n++) {
621 // ASFStream *stream = &asf->streams[n];
623 par = s->streams[n]->codecpar;
624 asf->streams[n].num = n + 1;
625 asf->streams[n].seq = 1;
627 switch (par->codec_type) {
628 case AVMEDIA_TYPE_AUDIO:
630 extra_size = 18 + wav_extra_size;
634 case AVMEDIA_TYPE_VIDEO:
635 wav_extra_size = par->extradata_size;
636 extra_size = 0x33 + wav_extra_size;
641 hpos = put_header(pb, &ff_asf_stream_header);
642 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
643 ff_put_guid(pb, &ff_asf_audio_stream);
644 ff_put_guid(pb, &ff_asf_audio_conceal_spread);
646 ff_put_guid(pb, &ff_asf_video_stream);
647 ff_put_guid(pb, &ff_asf_video_conceal_none);
649 avio_wl64(pb, 0); /* ??? */
650 es_pos = avio_tell(pb);
651 avio_wl32(pb, extra_size); /* wav header len */
652 avio_wl32(pb, extra_size2); /* additional data len */
653 avio_wl16(pb, n + 1); /* stream number */
654 avio_wl32(pb, 0); /* ??? */
656 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
657 /* WAVEFORMATEX header */
658 int wavsize = ff_put_wav_header(s, pb, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
662 if (wavsize != extra_size) {
663 cur_pos = avio_tell(pb);
664 avio_seek(pb, es_pos, SEEK_SET);
665 avio_wl32(pb, wavsize); /* wav header len */
666 avio_seek(pb, cur_pos, SEEK_SET);
668 /* ERROR Correction */
670 if (par->codec_id == AV_CODEC_ID_ADPCM_G726 || !par->block_align) {
671 avio_wl16(pb, 0x0190);
672 avio_wl16(pb, 0x0190);
674 avio_wl16(pb, par->block_align);
675 avio_wl16(pb, par->block_align);
680 avio_wl32(pb, par->width);
681 avio_wl32(pb, par->height);
682 avio_w8(pb, 2); /* ??? */
683 avio_wl16(pb, 40 + par->extradata_size); /* size */
685 /* BITMAPINFOHEADER header */
686 ff_put_bmp_header(pb, par, 1, 0);
688 end_header(pb, hpos);
693 hpos = put_header(pb, &ff_asf_codec_comment_header);
694 ff_put_guid(pb, &ff_asf_codec_comment1_header);
695 avio_wl32(pb, s->nb_streams);
696 for (n = 0; n < s->nb_streams; n++) {
697 const AVCodecDescriptor *codec_desc;
700 par = s->streams[n]->codecpar;
701 codec_desc = avcodec_descriptor_get(par->codec_id);
703 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
705 else if (par->codec_type == AVMEDIA_TYPE_VIDEO)
710 if (par->codec_id == AV_CODEC_ID_WMAV2)
711 desc = "Windows Media Audio V8";
713 desc = codec_desc ? codec_desc->name : NULL;
716 AVIOContext *dyn_buf;
720 if (avio_open_dyn_buf(&dyn_buf) < 0)
721 return AVERROR(ENOMEM);
723 avio_put_str16le(dyn_buf, desc);
724 len = avio_close_dyn_buf(dyn_buf, &buf);
725 avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
727 avio_write(pb, buf, len);
732 avio_wl16(pb, 0); /* no parameters */
735 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
737 avio_wl16(pb, par->codec_tag);
740 avio_wl32(pb, par->codec_tag);
745 end_header(pb, hpos);
747 /* patch the header size fields */
749 cur_pos = avio_tell(pb);
750 header_size = cur_pos - header_offset;
751 if (asf->is_streamed) {
752 header_size += 8 + 30 + DATA_HEADER_SIZE;
754 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
755 avio_wl16(pb, header_size);
756 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
757 avio_wl16(pb, header_size);
759 header_size -= 8 + 30 + DATA_HEADER_SIZE;
761 header_size += 24 + 6;
762 avio_seek(pb, header_offset - 14, SEEK_SET);
763 avio_wl64(pb, header_size);
764 avio_seek(pb, cur_pos, SEEK_SET);
766 /* movie chunk, followed by packets of packet_size */
767 asf->data_offset = cur_pos;
768 ff_put_guid(pb, &ff_asf_data_header);
769 avio_wl64(pb, data_chunk_size);
770 ff_put_guid(pb, &ff_asf_my_guid);
771 avio_wl64(pb, asf->nb_packets); /* nb packets */
772 avio_w8(pb, 1); /* ??? */
773 avio_w8(pb, 1); /* ??? */
777 static int asf_write_header(AVFormatContext *s)
779 ASFContext *asf = s->priv_data;
781 s->packet_size = asf->packet_size;
782 s->max_interleave_delta = 0;
785 if (s->nb_streams > 127) {
786 av_log(s, AV_LOG_ERROR, "ASF can only handle 127 streams\n");
787 return AVERROR(EINVAL);
790 asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
792 return AVERROR(ENOMEM);
793 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
794 asf->maximum_packet = 0;
796 /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
797 * data_size - asf->data_offset at the moment this function is done.
798 * It is needed to use asf as a streamable format. */
799 if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
801 av_freep(&asf->index_ptr);
807 asf->packet_nb_payloads = 0;
808 asf->packet_timestamp_start = -1;
809 asf->packet_timestamp_end = -1;
810 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
811 NULL, NULL, NULL, NULL);
813 if (s->avoid_negative_ts < 0)
814 s->avoid_negative_ts = 1;
819 static int asf_write_stream_header(AVFormatContext *s)
821 ASFContext *asf = s->priv_data;
823 asf->is_streamed = 1;
825 return asf_write_header(s);
828 static int put_payload_parsing_info(AVFormatContext *s,
829 unsigned sendtime, unsigned duration,
830 int nb_payloads, int padsize)
832 ASFContext *asf = s->priv_data;
833 AVIOContext *pb = s->pb;
835 int64_t start = avio_tell(pb);
837 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
839 padsize -= PACKET_HEADER_MIN_SIZE;
840 if (asf->multi_payloads_present)
842 av_assert0(padsize >= 0);
844 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
845 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
848 if (asf->multi_payloads_present)
849 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
853 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
855 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
857 avio_w8(pb, iLengthTypeFlags);
859 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
861 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
862 avio_wl16(pb, padsize - 2);
863 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
864 avio_w8(pb, padsize - 1);
866 avio_wl32(pb, sendtime);
867 avio_wl16(pb, duration);
868 if (asf->multi_payloads_present)
869 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
871 ppi_size = avio_tell(pb) - start;
876 static void flush_packet(AVFormatContext *s)
878 ASFContext *asf = s->priv_data;
879 int packet_hdr_size, packet_filled_size;
881 av_assert0(asf->packet_timestamp_end >= asf->packet_timestamp_start);
883 if (asf->is_streamed)
884 put_chunk(s, 0x4424, s->packet_size, 0);
886 packet_hdr_size = put_payload_parsing_info(s,
887 asf->packet_timestamp_start,
888 asf->packet_timestamp_end - asf->packet_timestamp_start,
889 asf->packet_nb_payloads,
890 asf->packet_size_left);
892 packet_filled_size = asf->packet_size - asf->packet_size_left;
893 av_assert0(packet_hdr_size <= asf->packet_size_left);
894 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
896 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
900 asf->packet_nb_payloads = 0;
901 asf->packet_timestamp_start = -1;
902 asf->packet_timestamp_end = -1;
903 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
904 NULL, NULL, NULL, NULL);
907 static void put_payload_header(AVFormatContext *s, ASFStream *stream,
908 int64_t presentation_time, int m_obj_size,
909 int m_obj_offset, int payload_len, int flags)
911 ASFContext *asf = s->priv_data;
912 AVIOContext *pb = &asf->pb;
916 if (flags & AV_PKT_FLAG_KEY)
917 val |= ASF_PL_FLAG_KEY_FRAME;
920 avio_w8(pb, stream->seq); // Media object number
921 avio_wl32(pb, m_obj_offset); // Offset Into Media Object
923 // Replicated Data shall be at least 8 bytes long.
924 // The first 4 bytes of data shall contain the
925 // Size of the Media Object that the payload belongs to.
926 // The next 4 bytes of data shall contain the
927 // Presentation Time for the media object that the payload belongs to.
928 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
930 avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
931 avio_wl32(pb, (uint32_t) presentation_time); // Replicated Data - Presentation Time
933 if (asf->multi_payloads_present) {
934 avio_wl16(pb, payload_len); // payload length
938 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
939 int64_t timestamp, const uint8_t *buf,
940 int m_obj_size, int flags)
942 ASFContext *asf = s->priv_data;
943 int m_obj_offset, payload_len, frag_len1;
946 while (m_obj_offset < m_obj_size) {
947 payload_len = m_obj_size - m_obj_offset;
948 if (asf->packet_timestamp_start == -1) {
949 const int multi_payload_constant = (asf->packet_size - MULTI_PAYLOAD_HEADERS);
950 asf->multi_payloads_present = (payload_len < multi_payload_constant);
952 asf->packet_size_left = asf->packet_size;
953 if (asf->multi_payloads_present) {
954 frag_len1 = multi_payload_constant - 1;
956 frag_len1 = asf->packet_size - SINGLE_PAYLOAD_HEADERS;
958 asf->packet_timestamp_start = timestamp;
961 frag_len1 = asf->packet_size_left -
962 PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS -
963 PACKET_HEADER_MIN_SIZE - 1;
965 if (frag_len1 < payload_len &&
966 avst->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
970 if (asf->packet_timestamp_start > INT64_MAX - UINT16_MAX ||
971 timestamp > asf->packet_timestamp_start + UINT16_MAX) {
977 if (payload_len > frag_len1)
978 payload_len = frag_len1;
979 else if (payload_len == (frag_len1 - 1))
980 payload_len = frag_len1 - 2; // additional byte need to put padding length
982 put_payload_header(s, stream, timestamp + PREROLL_TIME,
983 m_obj_size, m_obj_offset, payload_len, flags);
984 avio_write(&asf->pb, buf, payload_len);
986 if (asf->multi_payloads_present)
987 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
989 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
990 asf->packet_timestamp_end = timestamp;
992 asf->packet_nb_payloads++;
996 m_obj_offset += payload_len;
999 if (!asf->multi_payloads_present)
1001 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
1003 else if (asf->packet_nb_payloads == ASF_PAYLOADS_PER_PACKET)
1009 static int update_index(AVFormatContext *s, int start_sec,
1010 uint32_t packet_number, uint16_t packet_count,
1011 uint64_t packet_offset)
1013 ASFContext *asf = s->priv_data;
1015 if (start_sec > asf->next_start_sec) {
1018 if (!asf->next_start_sec) {
1019 asf->next_packet_number = packet_number;
1020 asf->next_packet_count = packet_count;
1021 asf->next_packet_offset = packet_offset;
1024 if (start_sec > asf->nb_index_memory_alloc) {
1026 asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
1027 if ((err = av_reallocp_array(&asf->index_ptr,
1028 asf->nb_index_memory_alloc,
1029 sizeof(*asf->index_ptr))) < 0) {
1030 asf->nb_index_memory_alloc = 0;
1034 for (i = asf->next_start_sec; i < start_sec; i++) {
1035 asf->index_ptr[i].packet_number = asf->next_packet_number;
1036 asf->index_ptr[i].packet_count = asf->next_packet_count;
1037 asf->index_ptr[i].send_time = asf->next_start_sec * INT64_C(10000000);
1038 asf->index_ptr[i].offset = asf->next_packet_offset;
1042 asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
1043 asf->next_packet_number = packet_number;
1044 asf->next_packet_count = packet_count;
1045 asf->next_packet_offset = packet_offset;
1046 asf->next_start_sec = start_sec;
1051 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
1053 ASFContext *asf = s->priv_data;
1054 AVIOContext *pb = s->pb;
1056 AVCodecParameters *par;
1057 uint32_t packet_number;
1060 int flags = pkt->flags;
1062 uint64_t offset = avio_tell(pb);
1064 par = s->streams[pkt->stream_index]->codecpar;
1065 stream = &asf->streams[pkt->stream_index];
1067 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
1068 flags &= ~AV_PKT_FLAG_KEY;
1070 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
1071 av_assert0(pts != AV_NOPTS_VALUE);
1072 if ( pts < - PREROLL_TIME
1073 || pts > (INT_MAX-3)/10000LL * ASF_INDEXED_INTERVAL - PREROLL_TIME) {
1074 av_log(s, AV_LOG_ERROR, "input pts %"PRId64" is invalid\n", pts);
1075 return AVERROR(EINVAL);
1078 asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
1080 packet_number = asf->nb_packets;
1081 put_frame(s, stream, s->streams[pkt->stream_index],
1082 pkt->dts, pkt->data, pkt->size, flags);
1084 start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
1085 / ASF_INDEXED_INTERVAL);
1088 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
1089 uint16_t packet_count = asf->nb_packets - packet_number;
1090 ret = update_index(s, start_sec, packet_number, packet_count, offset);
1094 asf->end_sec = start_sec;
1099 static int asf_write_index(AVFormatContext *s, const ASFIndex *index,
1100 uint16_t max, uint32_t count)
1102 AVIOContext *pb = s->pb;
1105 ff_put_guid(pb, &ff_asf_simple_index_header);
1106 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
1107 ff_put_guid(pb, &ff_asf_my_guid);
1108 avio_wl64(pb, ASF_INDEXED_INTERVAL);
1110 avio_wl32(pb, count);
1111 for (i = 0; i < count; i++) {
1112 avio_wl32(pb, index[i].packet_number);
1113 avio_wl16(pb, index[i].packet_count);
1119 static int asf_write_trailer(AVFormatContext *s)
1121 ASFContext *asf = s->priv_data;
1122 int64_t file_size, data_size;
1125 /* flush the current packet */
1126 if (asf->pb.buf_ptr > asf->pb.buffer)
1130 data_size = avio_tell(s->pb);
1131 if (!asf->is_streamed && asf->next_start_sec) {
1132 if ((ret = update_index(s, asf->end_sec + 1, 0, 0, 0)) < 0)
1134 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->next_start_sec);
1138 if (asf->is_streamed || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1139 put_chunk(s, 0x4524, 0, 0); /* end of stream */
1141 /* rewrite an updated header */
1142 file_size = avio_tell(s->pb);
1143 avio_seek(s->pb, 0, SEEK_SET);
1144 asf_write_header1(s, file_size, data_size - asf->data_offset);
1147 av_freep(&asf->index_ptr);
1151 static const AVOption asf_options[] = {
1152 { "packet_size", "Packet size", offsetof(ASFContext, packet_size), AV_OPT_TYPE_INT, {.i64 = 3200}, PACKET_SIZE_MIN, PACKET_SIZE_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1156 #if CONFIG_ASF_MUXER
1157 static const AVClass asf_muxer_class = {
1158 .class_name = "ASF muxer",
1159 .item_name = av_default_item_name,
1160 .option = asf_options,
1161 .version = LIBAVUTIL_VERSION_INT,
1164 AVOutputFormat ff_asf_muxer = {
1166 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1167 .mime_type = "video/x-ms-asf",
1168 .extensions = "asf,wmv,wma",
1169 .priv_data_size = sizeof(ASFContext),
1170 .audio_codec = AV_CODEC_ID_WMAV2,
1171 .video_codec = AV_CODEC_ID_MSMPEG4V3,
1172 .write_header = asf_write_header,
1173 .write_packet = asf_write_packet,
1174 .write_trailer = asf_write_trailer,
1175 .flags = AVFMT_GLOBALHEADER,
1176 .codec_tag = (const AVCodecTag * const []) {
1177 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
1179 .priv_class = &asf_muxer_class,
1181 #endif /* CONFIG_ASF_MUXER */
1183 #if CONFIG_ASF_STREAM_MUXER
1184 static const AVClass asf_stream_muxer_class = {
1185 .class_name = "ASF stream muxer",
1186 .item_name = av_default_item_name,
1187 .option = asf_options,
1188 .version = LIBAVUTIL_VERSION_INT,
1191 AVOutputFormat ff_asf_stream_muxer = {
1192 .name = "asf_stream",
1193 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1194 .mime_type = "video/x-ms-asf",
1195 .extensions = "asf,wmv,wma",
1196 .priv_data_size = sizeof(ASFContext),
1197 .audio_codec = AV_CODEC_ID_WMAV2,
1198 .video_codec = AV_CODEC_ID_MSMPEG4V3,
1199 .write_header = asf_write_stream_header,
1200 .write_packet = asf_write_packet,
1201 .write_trailer = asf_write_trailer,
1202 .flags = AVFMT_GLOBALHEADER,
1203 .codec_tag = (const AVCodecTag * const []) {
1204 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
1206 .priv_class = &asf_stream_muxer_class,
1208 #endif /* CONFIG_ASF_STREAM_MUXER */