asfenc: use codec descriptors instead of AVCodecs to write codec info
[libav.git] / libavformat / asfenc.c
CommitLineData
542993b0 1/*
f26be477 2 * ASF muxer
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
542993b0 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
542993b0
KA
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
542993b0 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
542993b0
KA
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
542993b0 20 */
48a4ffa7
DB
21
22#include "libavutil/dict.h"
bb461370 23#include "libavutil/mathematics.h"
542993b0 24#include "avformat.h"
48a4ffa7 25#include "avio_internal.h"
c3f9ebf7 26#include "internal.h"
9d9f4119 27#include "riff.h"
542993b0
KA
28#include "asf.h"
29
30#undef NDEBUG
31#include <assert.h>
32
982e53fe 33
bb270c08
DB
34#define ASF_INDEXED_INTERVAL 10000000
35#define ASF_INDEX_BLOCK 600
982e53fe 36
615b92fd 37#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
48a4ffa7
DB
38#define ASF_PACKET_ERROR_CORRECTION_FLAGS \
39 (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
40 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
615b92fd
KA
41
42#if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
43# define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
44#else
45# define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
46#endif
47
48a4ffa7
DB
48#define ASF_PPI_PROPERTY_FLAGS \
49 (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
50 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
51 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
52 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE)
615b92fd
KA
53
54#define ASF_PPI_LENGTH_TYPE_FLAGS 0
55
56#define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
57
58#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
59# define ASF_PPI_SEQUENCE_FIELD_SIZE 1
60#endif
61#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
62# define ASF_PPI_SEQUENCE_FIELD_SIZE 2
63#endif
64#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
65# define ASF_PPI_SEQUENCE_FIELD_SIZE 4
66#endif
67#ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
68# define ASF_PPI_SEQUENCE_FIELD_SIZE 0
69#endif
70
615b92fd
KA
71#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
72# define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
73#endif
74#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
75# define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
76#endif
77#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
78# define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
79#endif
80#ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
81# define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
82#endif
83
84#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
85# define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
86#endif
87#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
88# define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
89#endif
90#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
91# define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
92#endif
93#ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
94# define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
95#endif
96
97#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
98# define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
99#endif
100#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
101# define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
102#endif
103#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
104# define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
105#endif
106#ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
107# define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
108#endif
109
110#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))
111# define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
112#endif
113#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))
114# define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
115#endif
116#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))
117# define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
118#endif
119#ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
120# define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
121#endif
122
123#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
124# define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
125#endif
126#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
127# define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
128#endif
129#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
130# define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
131#endif
132#ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
133# define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
134#endif
135
136#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
137# define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
138#endif
139#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
140# define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
141#endif
142#ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
143# define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
144#endif
145
48a4ffa7
DB
146#define PACKET_HEADER_MIN_SIZE \
147 (ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
148 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
149 1 + /* Length Type Flags */ \
150 1 + /* Property Flags */ \
151 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
152 ASF_PPI_SEQUENCE_FIELD_SIZE + \
153 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
154 4 + /* Send Time Field */ \
155 2) /* Duration Field */
615b92fd
KA
156
157// Replicated Data shall be at least 8 bytes long.
158#define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
159
48a4ffa7
DB
160#define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
161 (1 + /* Stream Number */ \
162 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
163 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
164 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
165 ASF_PAYLOAD_REPLICATED_DATA_LENGTH)
166
167#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
168 (1 + /* Stream Number */ \
169 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
170 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
171 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
172 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
173 ASF_PAYLOAD_LENGTH_FIELD_SIZE)
174
175#define SINGLE_PAYLOAD_DATA_LENGTH \
176 (PACKET_SIZE - \
177 PACKET_HEADER_MIN_SIZE - \
178 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
179
180#define MULTI_PAYLOAD_CONSTANT \
181 (PACKET_SIZE - \
182 PACKET_HEADER_MIN_SIZE - \
183 1 - /* Payload Flags */ \
184 2 * PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS)
615b92fd 185
c53b5dda
VP
186#define DATA_HEADER_SIZE 50
187
4bc328a2
AK
188typedef struct {
189 uint32_t seqno;
190 int is_streamed;
191 ASFStream streams[128]; ///< it's max number and it's not that big
192 /* non streamed additonnal info */
193 uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
194 int64_t duration; ///< in 100ns units
195 /* packet filling */
196 unsigned char multi_payloads_present;
197 int packet_size_left;
198 int packet_timestamp_start;
199 int packet_timestamp_end;
200 unsigned int packet_nb_payloads;
201 uint8_t packet_buf[PACKET_SIZE];
ae628ec1 202 AVIOContext pb;
4bc328a2
AK
203 /* only for reading */
204 uint64_t data_offset; ///< beginning of the first data packet
205
206 int64_t last_indexed_pts;
48a4ffa7 207 ASFIndex *index_ptr;
4bc328a2
AK
208 uint32_t nb_index_count;
209 uint32_t nb_index_memory_alloc;
210 uint16_t maximum_packet;
211} ASFContext;
212
7caf0cc6 213static const AVCodecTag codec_asf_bmp_tags[] = {
48a4ffa7
DB
214 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
215 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
36ef5369 216 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
48a4ffa7 217 { AV_CODEC_ID_NONE, 0 },
7caf0cc6
MN
218};
219
3caa0d93 220#define PREROLL_TIME 3100
615b92fd 221
ae628ec1 222static void put_guid(AVIOContext *s, const ff_asf_guid *g)
542993b0 223{
24c14d6d 224 assert(sizeof(*g) == 16);
77eb5504 225 avio_write(s, *g, sizeof(*g));
542993b0
KA
226}
227
ae628ec1 228static void put_str16(AVIOContext *s, const char *tag)
cba2f6d5
AK
229{
230 int len;
231 uint8_t *pb;
ae628ec1 232 AVIOContext *dyn_buf;
b92c5452 233 if (avio_open_dyn_buf(&dyn_buf) < 0)
cba2f6d5
AK
234 return;
235
dccbd97d 236 avio_put_str16le(dyn_buf, tag);
6dc7d80d 237 len = avio_close_dyn_buf(dyn_buf, &pb);
77eb5504
AK
238 avio_wl16(s, len);
239 avio_write(s, pb, len);
cba2f6d5 240 av_freep(&pb);
542993b0
KA
241}
242
ae628ec1 243static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
542993b0
KA
244{
245 int64_t pos;
246
a2704c97 247 pos = avio_tell(pb);
542993b0 248 put_guid(pb, g);
77eb5504 249 avio_wl64(pb, 24);
542993b0
KA
250 return pos;
251}
252
253/* update header size */
ae628ec1 254static void end_header(AVIOContext *pb, int64_t pos)
542993b0
KA
255{
256 int64_t pos1;
257
a2704c97 258 pos1 = avio_tell(pb);
6b4aa5da 259 avio_seek(pb, pos + 16, SEEK_SET);
77eb5504 260 avio_wl64(pb, pos1 - pos);
6b4aa5da 261 avio_seek(pb, pos1, SEEK_SET);
542993b0
KA
262}
263
264/* write an asf chunk (only used in streaming case) */
48a4ffa7
DB
265static void put_chunk(AVFormatContext *s, int type,
266 int payload_length, int flags)
542993b0
KA
267{
268 ASFContext *asf = s->priv_data;
ae628ec1 269 AVIOContext *pb = s->pb;
542993b0
KA
270 int length;
271
272 length = payload_length + 8;
77eb5504 273 avio_wl16(pb, type);
48a4ffa7
DB
274 avio_wl16(pb, length); // size
275 avio_wl32(pb, asf->seqno); // sequence number
276 avio_wl16(pb, flags); // unknown bytes
277 avio_wl16(pb, length); // size_confirm
542993b0
KA
278 asf->seqno++;
279}
280
281/* convert from unix to windows time */
282static int64_t unix_to_file_time(int ti)
283{
284 int64_t t;
285
48a4ffa7 286 t = ti * INT64_C(10000000);
8da9266c 287 t += INT64_C(116444736000000000);
542993b0
KA
288 return t;
289}
290
bb461370
VP
291static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
292{
293 int i;
294 int32_t send_time = 0;
295 *offset = asf->data_offset + DATA_HEADER_SIZE;
296 for (i = 0; i < asf->nb_index_count; i++) {
297 if (pres_time <= asf->index_ptr[i].send_time)
298 break;
299 send_time = asf->index_ptr[i].send_time;
300 *offset = asf->index_ptr[i].offset;
301 }
302
303 return send_time / 10000;
304}
305
306static int asf_write_markers(AVFormatContext *s)
307{
308 ASFContext *asf = s->priv_data;
309 AVIOContext *pb = s->pb;
310 int i;
311 AVRational scale = {1, 10000000};
312 int64_t hpos = put_header(pb, &ff_asf_marker_header);
313
314 put_guid(pb, &ff_asf_reserved_4); // ASF spec mandates this reserved value
315 avio_wl32(pb, s->nb_chapters); // markers count
316 avio_wl16(pb, 0); // ASF spec mandates 0 for this
317 avio_wl16(pb, 0); // name length 0, no name given
318
319 for (i = 0; i < s->nb_chapters; i++) {
320 AVChapter *c = s->chapters[i];
321 AVDictionaryEntry *t = av_dict_get(c->metadata, "title", NULL, 0);
322 int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
323 uint64_t offset;
324 int32_t send_time = get_send_time(asf, pres_time, &offset);
325 int len = 0;
326 uint8_t *buf;
327 AVIOContext *dyn_buf;
328 if (t) {
329 if (avio_open_dyn_buf(&dyn_buf) < 0)
330 return AVERROR(ENOMEM);
331 avio_put_str16le(dyn_buf, t->value);
332 len = avio_close_dyn_buf(dyn_buf, &buf);
333 }
334 avio_wl64(pb, offset); // offset of the packet with send_time
335 avio_wl64(pb, pres_time + PREROLL_TIME * 10000); // presentation time
336 avio_wl16(pb, 12 + len); // entry length
337 avio_wl32(pb, send_time); // send time
338 avio_wl32(pb, 0); // flags, should be 0
339 avio_wl32(pb, len / 2); // marker desc length in WCHARS!
340 if (t) {
341 avio_write(pb, buf, len); // marker desc
342 av_freep(&buf);
343 }
344 }
345 end_header(pb, hpos);
346 return 0;
347}
348
542993b0 349/* write the header (used two times if non streamed) */
48a4ffa7
DB
350static int asf_write_header1(AVFormatContext *s, int64_t file_size,
351 int64_t data_chunk_size)
542993b0
KA
352{
353 ASFContext *asf = s->priv_data;
ae628ec1 354 AVIOContext *pb = s->pb;
d2d67e42 355 AVDictionaryEntry *tags[5];
542993b0
KA
356 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
357 int has_title;
488227c5 358 int metadata_count;
542993b0
KA
359 AVCodecContext *enc;
360 int64_t header_offset, cur_pos, hpos;
361 int bit_rate;
4336886c 362 int64_t duration;
542993b0 363
ad7768f4 364 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
03700d39 365
48a4ffa7
DB
366 tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
367 tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
d2d67e42 368 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
48a4ffa7
DB
369 tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
370 tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
300f24f3 371
48a4ffa7
DB
372 duration = asf->duration + PREROLL_TIME * 10000;
373 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
987170cb 374 metadata_count = av_dict_count(s->metadata);
542993b0
KA
375
376 bit_rate = 0;
48a4ffa7 377 for (n = 0; n < s->nb_streams; n++) {
01f4895c 378 enc = s->streams[n]->codec;
542993b0 379
c3f9ebf7 380 avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
9ee91c2f 381
542993b0
KA
382 bit_rate += enc->bit_rate;
383 }
384
385 if (asf->is_streamed) {
386 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
387 }
388
17af0525 389 put_guid(pb, &ff_asf_header);
77eb5504
AK
390 avio_wl64(pb, -1); /* header length, will be patched after */
391 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
392 avio_w8(pb, 1); /* ??? */
393 avio_w8(pb, 2); /* ??? */
542993b0
KA
394
395 /* file header */
a2704c97 396 header_offset = avio_tell(pb);
48a4ffa7 397 hpos = put_header(pb, &ff_asf_file_header);
17af0525 398 put_guid(pb, &ff_asf_my_guid);
77eb5504 399 avio_wl64(pb, file_size);
542993b0 400 file_time = 0;
77eb5504
AK
401 avio_wl64(pb, unix_to_file_time(file_time));
402 avio_wl64(pb, asf->nb_packets); /* number of packets */
403 avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
404 avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
405 avio_wl64(pb, PREROLL_TIME); /* start time stamp */
48a4ffa7 406 avio_wl32(pb, (asf->is_streamed || !pb->seekable) ? 3 : 2); /* ??? */
77eb5504
AK
407 avio_wl32(pb, s->packet_size); /* packet size */
408 avio_wl32(pb, s->packet_size); /* packet size */
409 avio_wl32(pb, bit_rate); /* Nominal data rate in bps */
542993b0
KA
410 end_header(pb, hpos);
411
412 /* unknown headers */
17af0525
AJ
413 hpos = put_header(pb, &ff_asf_head1_guid);
414 put_guid(pb, &ff_asf_head2_guid);
77eb5504
AK
415 avio_wl32(pb, 6);
416 avio_wl16(pb, 0);
542993b0
KA
417 end_header(pb, hpos);
418
419 /* title and other infos */
420 if (has_title) {
cba2f6d5
AK
421 int len;
422 uint8_t *buf;
ae628ec1 423 AVIOContext *dyn_buf;
cba2f6d5 424
b92c5452 425 if (avio_open_dyn_buf(&dyn_buf) < 0)
cba2f6d5
AK
426 return AVERROR(ENOMEM);
427
17af0525 428 hpos = put_header(pb, &ff_asf_comment_header);
cba2f6d5
AK
429
430 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
dccbd97d 431 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
77eb5504 432 avio_wl16(pb, len);
cba2f6d5 433 }
6dc7d80d 434 len = avio_close_dyn_buf(dyn_buf, &buf);
77eb5504 435 avio_write(pb, buf, len);
cba2f6d5 436 av_freep(&buf);
542993b0
KA
437 end_header(pb, hpos);
438 }
488227c5 439 if (metadata_count) {
d2d67e42 440 AVDictionaryEntry *tag = NULL;
17af0525 441 hpos = put_header(pb, &ff_asf_extended_content_header);
77eb5504 442 avio_wl16(pb, metadata_count);
d2d67e42 443 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
cba2f6d5 444 put_str16(pb, tag->key);
77eb5504 445 avio_wl16(pb, 0);
cba2f6d5 446 put_str16(pb, tag->value);
488227c5
AJ
447 }
448 end_header(pb, hpos);
449 }
bb461370
VP
450 /* chapters using ASF markers */
451 if (!asf->is_streamed && s->nb_chapters) {
452 int ret;
453 if (ret = asf_write_markers(s))
454 return ret;
455 }
542993b0 456 /* stream headers */
48a4ffa7 457 for (n = 0; n < s->nb_streams; n++) {
542993b0
KA
458 int64_t es_pos;
459 // ASFStream *stream = &asf->streams[n];
460
48a4ffa7 461 enc = s->streams[n]->codec;
542993b0
KA
462 asf->streams[n].num = n + 1;
463 asf->streams[n].seq = 0;
464
48a4ffa7 465 switch (enc->codec_type) {
72415b2a 466 case AVMEDIA_TYPE_AUDIO:
542993b0 467 wav_extra_size = 0;
48a4ffa7
DB
468 extra_size = 18 + wav_extra_size;
469 extra_size2 = 8;
542993b0
KA
470 break;
471 default:
72415b2a 472 case AVMEDIA_TYPE_VIDEO:
982e53fe 473 wav_extra_size = enc->extradata_size;
48a4ffa7
DB
474 extra_size = 0x33 + wav_extra_size;
475 extra_size2 = 0;
542993b0
KA
476 break;
477 }
478
17af0525 479 hpos = put_header(pb, &ff_asf_stream_header);
72415b2a 480 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
17af0525
AJ
481 put_guid(pb, &ff_asf_audio_stream);
482 put_guid(pb, &ff_asf_audio_conceal_spread);
542993b0 483 } else {
17af0525
AJ
484 put_guid(pb, &ff_asf_video_stream);
485 put_guid(pb, &ff_asf_video_conceal_none);
542993b0 486 }
77eb5504 487 avio_wl64(pb, 0); /* ??? */
a2704c97 488 es_pos = avio_tell(pb);
77eb5504
AK
489 avio_wl32(pb, extra_size); /* wav header len */
490 avio_wl32(pb, extra_size2); /* additional data len */
491 avio_wl16(pb, n + 1); /* stream number */
492 avio_wl32(pb, 0); /* ??? */
542993b0 493
72415b2a 494 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
542993b0 495 /* WAVEFORMATEX header */
1a40491e 496 int wavsize = ff_put_wav_header(pb, enc);
542993b0
KA
497
498 if (wavsize < 0)
499 return -1;
500 if (wavsize != extra_size) {
a2704c97 501 cur_pos = avio_tell(pb);
6b4aa5da 502 avio_seek(pb, es_pos, SEEK_SET);
77eb5504 503 avio_wl32(pb, wavsize); /* wav header len */
6b4aa5da 504 avio_seek(pb, cur_pos, SEEK_SET);
542993b0 505 }
982e53fe 506 /* ERROR Correction */
77eb5504 507 avio_w8(pb, 0x01);
48a4ffa7 508 if (enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align) {
77eb5504
AK
509 avio_wl16(pb, 0x0190);
510 avio_wl16(pb, 0x0190);
48a4ffa7 511 } else {
77eb5504
AK
512 avio_wl16(pb, enc->block_align);
513 avio_wl16(pb, enc->block_align);
1d7d9935 514 }
77eb5504
AK
515 avio_wl16(pb, 0x01);
516 avio_w8(pb, 0x00);
542993b0 517 } else {
77eb5504
AK
518 avio_wl32(pb, enc->width);
519 avio_wl32(pb, enc->height);
520 avio_w8(pb, 2); /* ??? */
521 avio_wl16(pb, 40 + enc->extradata_size); /* size */
542993b0
KA
522
523 /* BITMAPINFOHEADER header */
1a40491e 524 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
542993b0
KA
525 }
526 end_header(pb, hpos);
527 }
528
529 /* media comments */
530
17af0525
AJ
531 hpos = put_header(pb, &ff_asf_codec_comment_header);
532 put_guid(pb, &ff_asf_codec_comment1_header);
77eb5504 533 avio_wl32(pb, s->nb_streams);
48a4ffa7 534 for (n = 0; n < s->nb_streams; n++) {
6072184e 535 const AVCodecDescriptor *codec_desc;
531d8fa3 536 const char *desc;
542993b0 537
6072184e
AK
538 enc = s->streams[n]->codec;
539 codec_desc = avcodec_descriptor_get(enc->codec_id);
542993b0 540
48a4ffa7 541 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 542 avio_wl16(pb, 2);
48a4ffa7 543 else if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
77eb5504 544 avio_wl16(pb, 1);
7205395b 545 else
77eb5504 546 avio_wl16(pb, -1);
7205395b 547
48a4ffa7 548 if (enc->codec_id == AV_CODEC_ID_WMAV2)
531d8fa3 549 desc = "Windows Media Audio V8";
7205395b 550 else
6072184e 551 desc = codec_desc ? codec_desc->name : NULL;
cba2f6d5 552
6072184e
AK
553 if (desc) {
554 AVIOContext *dyn_buf;
555 uint8_t *buf;
556 int len;
cba2f6d5 557
6072184e
AK
558 if (avio_open_dyn_buf(&dyn_buf) < 0)
559 return AVERROR(ENOMEM);
cba2f6d5 560
6072184e
AK
561 avio_put_str16le(dyn_buf, desc);
562 len = avio_close_dyn_buf(dyn_buf, &buf);
563 avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
564
565 avio_write(pb, buf, len);
566 av_freep(&buf);
567 } else
568 avio_wl16(pb, 0);
cba2f6d5 569
77eb5504 570 avio_wl16(pb, 0); /* no parameters */
115329f1 571
542993b0 572 /* id */
72415b2a 573 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
77eb5504
AK
574 avio_wl16(pb, 2);
575 avio_wl16(pb, enc->codec_tag);
542993b0 576 } else {
77eb5504
AK
577 avio_wl16(pb, 4);
578 avio_wl32(pb, enc->codec_tag);
542993b0 579 }
48a4ffa7 580 if (!enc->codec_tag)
d65b8230 581 return -1;
542993b0
KA
582 }
583 end_header(pb, hpos);
584
585 /* patch the header size fields */
586
48a4ffa7 587 cur_pos = avio_tell(pb);
542993b0
KA
588 header_size = cur_pos - header_offset;
589 if (asf->is_streamed) {
c53b5dda 590 header_size += 8 + 30 + DATA_HEADER_SIZE;
542993b0 591
6b4aa5da 592 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
77eb5504 593 avio_wl16(pb, header_size);
6b4aa5da 594 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
77eb5504 595 avio_wl16(pb, header_size);
542993b0 596
c53b5dda 597 header_size -= 8 + 30 + DATA_HEADER_SIZE;
542993b0
KA
598 }
599 header_size += 24 + 6;
6b4aa5da 600 avio_seek(pb, header_offset - 14, SEEK_SET);
77eb5504 601 avio_wl64(pb, header_size);
6b4aa5da 602 avio_seek(pb, cur_pos, SEEK_SET);
542993b0
KA
603
604 /* movie chunk, followed by packets of packet_size */
605 asf->data_offset = cur_pos;
17af0525 606 put_guid(pb, &ff_asf_data_header);
77eb5504 607 avio_wl64(pb, data_chunk_size);
17af0525 608 put_guid(pb, &ff_asf_my_guid);
77eb5504
AK
609 avio_wl64(pb, asf->nb_packets); /* nb packets */
610 avio_w8(pb, 1); /* ??? */
611 avio_w8(pb, 1); /* ??? */
542993b0
KA
612 return 0;
613}
614
615static int asf_write_header(AVFormatContext *s)
616{
617 ASFContext *asf = s->priv_data;
618
91d19d47 619 s->packet_size = PACKET_SIZE;
542993b0 620 asf->nb_packets = 0;
115329f1 621
48a4ffa7
DB
622 asf->last_indexed_pts = 0;
623 asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
982e53fe 624 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
48a4ffa7
DB
625 asf->nb_index_count = 0;
626 asf->maximum_packet = 0;
542993b0 627
c53b5dda
VP
628 /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
629 * data_size - asf->data_offset at the moment this function is done.
630 * It is needed to use asf as a streamable format. */
631 if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
542993b0
KA
632 //av_free(asf);
633 return -1;
634 }
635
b7f2fdde 636 avio_flush(s->pb);
542993b0 637
48a4ffa7 638 asf->packet_nb_payloads = 0;
542993b0 639 asf->packet_timestamp_start = -1;
48a4ffa7 640 asf->packet_timestamp_end = -1;
e731b8d8 641 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
48a4ffa7 642 NULL, NULL, NULL, NULL);
542993b0
KA
643
644 return 0;
645}
646
647static int asf_write_stream_header(AVFormatContext *s)
648{
649 ASFContext *asf = s->priv_data;
650
651 asf->is_streamed = 1;
652
653 return asf_write_header(s);
654}
655
48a4ffa7
DB
656static int put_payload_parsing_info(AVFormatContext *s,
657 unsigned sendtime, unsigned duration,
658 int nb_payloads, int padsize)
542993b0
KA
659{
660 ASFContext *asf = s->priv_data;
ae628ec1 661 AVIOContext *pb = s->pb;
615b92fd 662 int ppi_size, i;
48a4ffa7 663 int64_t start = avio_tell(pb);
615b92fd
KA
664
665 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
115329f1 666
2d241e66 667 padsize -= PACKET_HEADER_MIN_SIZE;
48a4ffa7 668 if (asf->multi_payloads_present)
2d241e66 669 padsize--;
48a4ffa7 670 assert(padsize >= 0);
2d241e66 671
77eb5504 672 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
48a4ffa7 673 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
77eb5504 674 avio_w8(pb, 0x0);
542993b0 675
615b92fd
KA
676 if (asf->multi_payloads_present)
677 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
542993b0 678
542993b0
KA
679 if (padsize > 0) {
680 if (padsize < 256)
615b92fd 681 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
542993b0 682 else
615b92fd 683 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
542993b0 684 }
77eb5504 685 avio_w8(pb, iLengthTypeFlags);
615b92fd 686
77eb5504 687 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
615b92fd
KA
688
689 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
77eb5504 690 avio_wl16(pb, padsize - 2);
615b92fd 691 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
77eb5504 692 avio_w8(pb, padsize - 1);
615b92fd 693
77eb5504
AK
694 avio_wl32(pb, sendtime);
695 avio_wl16(pb, duration);
615b92fd 696 if (asf->multi_payloads_present)
77eb5504 697 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
615b92fd 698
a2704c97 699 ppi_size = avio_tell(pb) - start;
542993b0 700
615b92fd 701 return ppi_size;
542993b0
KA
702}
703
704static void flush_packet(AVFormatContext *s)
705{
706 ASFContext *asf = s->priv_data;
615b92fd 707 int packet_hdr_size, packet_filled_size;
542993b0 708
a57c41b2
MN
709 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
710
48a4ffa7 711 if (asf->is_streamed)
91d19d47 712 put_chunk(s, 0x4424, s->packet_size, 0);
615b92fd 713
48a4ffa7
DB
714 packet_hdr_size = put_payload_parsing_info(s,
715 asf->packet_timestamp_start,
716 asf->packet_timestamp_end -
717 asf->packet_timestamp_start,
718 asf->packet_nb_payloads,
719 asf->packet_size_left);
542993b0 720
2d241e66
MN
721 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
722 assert(packet_hdr_size <= asf->packet_size_left);
615b92fd 723 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
542993b0 724
77eb5504 725 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
542993b0 726
b7f2fdde 727 avio_flush(s->pb);
542993b0 728 asf->nb_packets++;
48a4ffa7 729 asf->packet_nb_payloads = 0;
542993b0 730 asf->packet_timestamp_start = -1;
48a4ffa7 731 asf->packet_timestamp_end = -1;
e731b8d8 732 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
48a4ffa7 733 NULL, NULL, NULL, NULL);
542993b0
KA
734}
735
48a4ffa7
DB
736static void put_payload_header(AVFormatContext *s, ASFStream *stream,
737 int presentation_time, int m_obj_size,
738 int m_obj_offset, int payload_len, int flags)
542993b0
KA
739{
740 ASFContext *asf = s->priv_data;
ae628ec1 741 AVIOContext *pb = &asf->pb;
542993b0 742 int val;
115329f1 743
542993b0 744 val = stream->num;
cc947f04 745 if (flags & AV_PKT_FLAG_KEY)
615b92fd 746 val |= ASF_PL_FLAG_KEY_FRAME;
77eb5504 747 avio_w8(pb, val);
115329f1 748
48a4ffa7
DB
749 avio_w8(pb, stream->seq); // Media object number
750 avio_wl32(pb, m_obj_offset); // Offset Into Media Object
115329f1 751
615b92fd 752 // Replicated Data shall be at least 8 bytes long.
115329f1 753 // The first 4 bytes of data shall contain the
615b92fd 754 // Size of the Media Object that the payload belongs to.
115329f1 755 // The next 4 bytes of data shall contain the
615b92fd 756 // Presentation Time for the media object that the payload belongs to.
77eb5504 757 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
615b92fd 758
48a4ffa7
DB
759 avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
760 avio_wl32(pb, presentation_time); // Replicated Data - Presentation Time
115329f1 761
48a4ffa7
DB
762 if (asf->multi_payloads_present) {
763 avio_wl16(pb, payload_len); // payload length
615b92fd 764 }
542993b0
KA
765}
766
48a4ffa7
DB
767static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
768 int timestamp, const uint8_t *buf,
769 int m_obj_size, int flags)
542993b0
KA
770{
771 ASFContext *asf = s->priv_data;
615b92fd
KA
772 int m_obj_offset, payload_len, frag_len1;
773
774 m_obj_offset = 0;
775 while (m_obj_offset < m_obj_size) {
776 payload_len = m_obj_size - m_obj_offset;
777 if (asf->packet_timestamp_start == -1) {
778 asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
115329f1 779
2d241e66 780 asf->packet_size_left = PACKET_SIZE;
48a4ffa7 781 if (asf->multi_payloads_present) {
615b92fd 782 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
48a4ffa7 783 } else {
615b92fd
KA
784 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
785 }
190972a0 786 asf->packet_timestamp_start = timestamp;
48a4ffa7 787 } else {
615b92fd 788 // multi payloads
48a4ffa7
DB
789 frag_len1 = asf->packet_size_left -
790 PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS -
791 PACKET_HEADER_MIN_SIZE - 1;
542993b0 792
48a4ffa7
DB
793 if (frag_len1 < payload_len &&
794 avst->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
75b213ed
MN
795 flush_packet(s);
796 continue;
797 }
615b92fd 798 }
542993b0 799 if (frag_len1 > 0) {
615b92fd
KA
800 if (payload_len > frag_len1)
801 payload_len = frag_len1;
802 else if (payload_len == (frag_len1 - 1))
48a4ffa7 803 payload_len = frag_len1 - 2; // additional byte need to put padding length
115329f1 804
48a4ffa7
DB
805 put_payload_header(s, stream, timestamp + PREROLL_TIME,
806 m_obj_size, m_obj_offset, payload_len, flags);
77eb5504 807 avio_write(&asf->pb, buf, payload_len);
615b92fd
KA
808
809 if (asf->multi_payloads_present)
810 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
811 else
812 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
542993b0 813 asf->packet_timestamp_end = timestamp;
115329f1 814
615b92fd 815 asf->packet_nb_payloads++;
542993b0 816 } else {
615b92fd 817 payload_len = 0;
542993b0 818 }
615b92fd 819 m_obj_offset += payload_len;
48a4ffa7 820 buf += payload_len;
615b92fd
KA
821
822 if (!asf->multi_payloads_present)
823 flush_packet(s);
2d241e66 824 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
542993b0
KA
825 flush_packet(s);
826 }
827 stream->seq++;
828}
829
e928649b 830static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
542993b0
KA
831{
832 ASFContext *asf = s->priv_data;
09f3c937 833 AVIOContext *pb = s->pb;
542993b0
KA
834 ASFStream *stream;
835 int64_t duration;
836 AVCodecContext *codec;
48a4ffa7
DB
837 int64_t packet_st, pts;
838 int start_sec, i;
839 int flags = pkt->flags;
09f3c937 840 uint64_t offset = avio_tell(pb);
542993b0 841
48a4ffa7 842 codec = s->streams[pkt->stream_index]->codec;
e928649b 843 stream = &asf->streams[pkt->stream_index];
542993b0 844
48a4ffa7 845 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
cc947f04 846 flags &= ~AV_PKT_FLAG_KEY;
0d9f8633 847
982e53fe 848 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
9bc93229 849 assert(pts != AV_NOPTS_VALUE);
48a4ffa7
DB
850 duration = pts * 10000;
851 asf->duration = FFMAX(asf->duration, duration + pkt->duration * 10000);
542993b0 852
982e53fe 853 packet_st = asf->nb_packets;
48a4ffa7
DB
854 put_frame(s, stream, s->streams[pkt->stream_index],
855 pkt->dts, pkt->data, pkt->size, flags);
982e53fe
C
856
857 /* check index */
cc947f04 858 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
8da9266c
MR
859 start_sec = (int)(duration / INT64_C(10000000));
860 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
48a4ffa7
DB
861 for (i = asf->nb_index_count; i < start_sec; i++) {
862 if (i >= asf->nb_index_memory_alloc) {
f369b935 863 int err;
982e53fe 864 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
f369b935
AK
865 if ((err = av_reallocp_array(&asf->index_ptr,
866 asf->nb_index_memory_alloc,
867 sizeof(*asf->index_ptr))) < 0) {
868 asf->nb_index_memory_alloc = 0;
869 return err;
870 }
982e53fe
C
871 }
872 // store
873 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
48a4ffa7 874 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets - packet_st);
09f3c937
VP
875 asf->index_ptr[i].send_time = start_sec * INT64_C(10000000);
876 asf->index_ptr[i].offset = offset;
48a4ffa7
DB
877 asf->maximum_packet = FFMAX(asf->maximum_packet,
878 (uint16_t)(asf->nb_packets - packet_st));
982e53fe 879 }
48a4ffa7 880 asf->nb_index_count = start_sec;
982e53fe
C
881 asf->last_indexed_pts = duration;
882 }
883 }
884 return 0;
885}
886
48a4ffa7
DB
887static int asf_write_index(AVFormatContext *s, ASFIndex *index,
888 uint16_t max, uint32_t count)
982e53fe 889{
ae628ec1 890 AVIOContext *pb = s->pb;
982e53fe
C
891 int i;
892
17af0525 893 put_guid(pb, &ff_asf_simple_index_header);
48a4ffa7 894 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
17af0525 895 put_guid(pb, &ff_asf_my_guid);
77eb5504
AK
896 avio_wl64(pb, ASF_INDEXED_INTERVAL);
897 avio_wl32(pb, max);
898 avio_wl32(pb, count);
48a4ffa7 899 for (i = 0; i < count; i++) {
77eb5504
AK
900 avio_wl32(pb, index[i].packet_number);
901 avio_wl16(pb, index[i].packet_count);
982e53fe
C
902 }
903
542993b0
KA
904 return 0;
905}
906
907static int asf_write_trailer(AVFormatContext *s)
908{
909 ASFContext *asf = s->priv_data;
48a4ffa7 910 int64_t file_size, data_size;
542993b0
KA
911
912 /* flush the current packet */
913 if (asf->pb.buf_ptr > asf->pb.buffer)
914 flush_packet(s);
915
982e53fe 916 /* write index */
a2704c97 917 data_size = avio_tell(s->pb);
48a4ffa7 918 if ((!asf->is_streamed) && (asf->nb_index_count != 0))
982e53fe 919 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
b7f2fdde 920 avio_flush(s->pb);
982e53fe 921
8978feda 922 if (asf->is_streamed || !s->pb->seekable) {
542993b0
KA
923 put_chunk(s, 0x4524, 0, 0); /* end of stream */
924 } else {
925 /* rewrite an updated header */
a2704c97 926 file_size = avio_tell(s->pb);
6b4aa5da 927 avio_seek(s->pb, 0, SEEK_SET);
982e53fe 928 asf_write_header1(s, file_size, data_size - asf->data_offset);
542993b0
KA
929 }
930
982e53fe 931 av_free(asf->index_ptr);
542993b0
KA
932 return 0;
933}
934
b250f9c6 935#if CONFIG_ASF_MUXER
c6610a21 936AVOutputFormat ff_asf_muxer = {
dfc2c4d9 937 .name = "asf",
0177b7d2 938 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
dfc2c4d9
AK
939 .mime_type = "video/x-ms-asf",
940 .extensions = "asf,wmv,wma",
941 .priv_data_size = sizeof(ASFContext),
36ef5369
AK
942 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
943 .video_codec = AV_CODEC_ID_MSMPEG4V3,
dfc2c4d9
AK
944 .write_header = asf_write_header,
945 .write_packet = asf_write_packet,
946 .write_trailer = asf_write_trailer,
20234a4b 947 .flags = AVFMT_GLOBALHEADER,
48a4ffa7 948 .codec_tag = (const AVCodecTag * const []) {
20234a4b
MS
949 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
950 },
542993b0 951};
48a4ffa7 952#endif /* CONFIG_ASF_MUXER */
542993b0 953
b250f9c6 954#if CONFIG_ASF_STREAM_MUXER
c6610a21 955AVOutputFormat ff_asf_stream_muxer = {
dfc2c4d9 956 .name = "asf_stream",
0177b7d2 957 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
dfc2c4d9
AK
958 .mime_type = "video/x-ms-asf",
959 .extensions = "asf,wmv,wma",
960 .priv_data_size = sizeof(ASFContext),
36ef5369
AK
961 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
962 .video_codec = AV_CODEC_ID_MSMPEG4V3,
dfc2c4d9
AK
963 .write_header = asf_write_stream_header,
964 .write_packet = asf_write_packet,
965 .write_trailer = asf_write_trailer,
20234a4b 966 .flags = AVFMT_GLOBALHEADER,
48a4ffa7 967 .codec_tag = (const AVCodecTag * const []) {
20234a4b
MS
968 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
969 },
542993b0 970};
48a4ffa7 971#endif /* CONFIG_ASF_STREAM_MUXER */