asfenc: use codec descriptors instead of AVCodecs to write codec info
[libav.git] / libavformat / asfenc.c
1 /*
2 * ASF muxer
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/dict.h"
23 #include "libavutil/mathematics.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "riff.h"
28 #include "asf.h"
29
30 #undef NDEBUG
31 #include <assert.h>
32
33
34 #define ASF_INDEXED_INTERVAL 10000000
35 #define ASF_INDEX_BLOCK 600
36
37 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
38 #define ASF_PACKET_ERROR_CORRECTION_FLAGS \
39 (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
40 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
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
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)
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
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
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 */
156
157 // Replicated Data shall be at least 8 bytes long.
158 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
159
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)
185
186 #define DATA_HEADER_SIZE 50
187
188 typedef 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];
202 AVIOContext pb;
203 /* only for reading */
204 uint64_t data_offset; ///< beginning of the first data packet
205
206 int64_t last_indexed_pts;
207 ASFIndex *index_ptr;
208 uint32_t nb_index_count;
209 uint32_t nb_index_memory_alloc;
210 uint16_t maximum_packet;
211 } ASFContext;
212
213 static const AVCodecTag codec_asf_bmp_tags[] = {
214 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
215 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
216 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
217 { AV_CODEC_ID_NONE, 0 },
218 };
219
220 #define PREROLL_TIME 3100
221
222 static void put_guid(AVIOContext *s, const ff_asf_guid *g)
223 {
224 assert(sizeof(*g) == 16);
225 avio_write(s, *g, sizeof(*g));
226 }
227
228 static void put_str16(AVIOContext *s, const char *tag)
229 {
230 int len;
231 uint8_t *pb;
232 AVIOContext *dyn_buf;
233 if (avio_open_dyn_buf(&dyn_buf) < 0)
234 return;
235
236 avio_put_str16le(dyn_buf, tag);
237 len = avio_close_dyn_buf(dyn_buf, &pb);
238 avio_wl16(s, len);
239 avio_write(s, pb, len);
240 av_freep(&pb);
241 }
242
243 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
244 {
245 int64_t pos;
246
247 pos = avio_tell(pb);
248 put_guid(pb, g);
249 avio_wl64(pb, 24);
250 return pos;
251 }
252
253 /* update header size */
254 static void end_header(AVIOContext *pb, int64_t pos)
255 {
256 int64_t pos1;
257
258 pos1 = avio_tell(pb);
259 avio_seek(pb, pos + 16, SEEK_SET);
260 avio_wl64(pb, pos1 - pos);
261 avio_seek(pb, pos1, SEEK_SET);
262 }
263
264 /* write an asf chunk (only used in streaming case) */
265 static void put_chunk(AVFormatContext *s, int type,
266 int payload_length, int flags)
267 {
268 ASFContext *asf = s->priv_data;
269 AVIOContext *pb = s->pb;
270 int length;
271
272 length = payload_length + 8;
273 avio_wl16(pb, type);
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
278 asf->seqno++;
279 }
280
281 /* convert from unix to windows time */
282 static int64_t unix_to_file_time(int ti)
283 {
284 int64_t t;
285
286 t = ti * INT64_C(10000000);
287 t += INT64_C(116444736000000000);
288 return t;
289 }
290
291 static 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
306 static 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
349 /* write the header (used two times if non streamed) */
350 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
351 int64_t data_chunk_size)
352 {
353 ASFContext *asf = s->priv_data;
354 AVIOContext *pb = s->pb;
355 AVDictionaryEntry *tags[5];
356 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
357 int has_title;
358 int metadata_count;
359 AVCodecContext *enc;
360 int64_t header_offset, cur_pos, hpos;
361 int bit_rate;
362 int64_t duration;
363
364 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
365
366 tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
367 tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
368 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
369 tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
370 tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
371
372 duration = asf->duration + PREROLL_TIME * 10000;
373 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
374 metadata_count = av_dict_count(s->metadata);
375
376 bit_rate = 0;
377 for (n = 0; n < s->nb_streams; n++) {
378 enc = s->streams[n]->codec;
379
380 avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
381
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
389 put_guid(pb, &ff_asf_header);
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); /* ??? */
394
395 /* file header */
396 header_offset = avio_tell(pb);
397 hpos = put_header(pb, &ff_asf_file_header);
398 put_guid(pb, &ff_asf_my_guid);
399 avio_wl64(pb, file_size);
400 file_time = 0;
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 */
406 avio_wl32(pb, (asf->is_streamed || !pb->seekable) ? 3 : 2); /* ??? */
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 */
410 end_header(pb, hpos);
411
412 /* unknown headers */
413 hpos = put_header(pb, &ff_asf_head1_guid);
414 put_guid(pb, &ff_asf_head2_guid);
415 avio_wl32(pb, 6);
416 avio_wl16(pb, 0);
417 end_header(pb, hpos);
418
419 /* title and other infos */
420 if (has_title) {
421 int len;
422 uint8_t *buf;
423 AVIOContext *dyn_buf;
424
425 if (avio_open_dyn_buf(&dyn_buf) < 0)
426 return AVERROR(ENOMEM);
427
428 hpos = put_header(pb, &ff_asf_comment_header);
429
430 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
431 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
432 avio_wl16(pb, len);
433 }
434 len = avio_close_dyn_buf(dyn_buf, &buf);
435 avio_write(pb, buf, len);
436 av_freep(&buf);
437 end_header(pb, hpos);
438 }
439 if (metadata_count) {
440 AVDictionaryEntry *tag = NULL;
441 hpos = put_header(pb, &ff_asf_extended_content_header);
442 avio_wl16(pb, metadata_count);
443 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
444 put_str16(pb, tag->key);
445 avio_wl16(pb, 0);
446 put_str16(pb, tag->value);
447 }
448 end_header(pb, hpos);
449 }
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 }
456 /* stream headers */
457 for (n = 0; n < s->nb_streams; n++) {
458 int64_t es_pos;
459 // ASFStream *stream = &asf->streams[n];
460
461 enc = s->streams[n]->codec;
462 asf->streams[n].num = n + 1;
463 asf->streams[n].seq = 0;
464
465 switch (enc->codec_type) {
466 case AVMEDIA_TYPE_AUDIO:
467 wav_extra_size = 0;
468 extra_size = 18 + wav_extra_size;
469 extra_size2 = 8;
470 break;
471 default:
472 case AVMEDIA_TYPE_VIDEO:
473 wav_extra_size = enc->extradata_size;
474 extra_size = 0x33 + wav_extra_size;
475 extra_size2 = 0;
476 break;
477 }
478
479 hpos = put_header(pb, &ff_asf_stream_header);
480 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
481 put_guid(pb, &ff_asf_audio_stream);
482 put_guid(pb, &ff_asf_audio_conceal_spread);
483 } else {
484 put_guid(pb, &ff_asf_video_stream);
485 put_guid(pb, &ff_asf_video_conceal_none);
486 }
487 avio_wl64(pb, 0); /* ??? */
488 es_pos = avio_tell(pb);
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); /* ??? */
493
494 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
495 /* WAVEFORMATEX header */
496 int wavsize = ff_put_wav_header(pb, enc);
497
498 if (wavsize < 0)
499 return -1;
500 if (wavsize != extra_size) {
501 cur_pos = avio_tell(pb);
502 avio_seek(pb, es_pos, SEEK_SET);
503 avio_wl32(pb, wavsize); /* wav header len */
504 avio_seek(pb, cur_pos, SEEK_SET);
505 }
506 /* ERROR Correction */
507 avio_w8(pb, 0x01);
508 if (enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align) {
509 avio_wl16(pb, 0x0190);
510 avio_wl16(pb, 0x0190);
511 } else {
512 avio_wl16(pb, enc->block_align);
513 avio_wl16(pb, enc->block_align);
514 }
515 avio_wl16(pb, 0x01);
516 avio_w8(pb, 0x00);
517 } else {
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 */
522
523 /* BITMAPINFOHEADER header */
524 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
525 }
526 end_header(pb, hpos);
527 }
528
529 /* media comments */
530
531 hpos = put_header(pb, &ff_asf_codec_comment_header);
532 put_guid(pb, &ff_asf_codec_comment1_header);
533 avio_wl32(pb, s->nb_streams);
534 for (n = 0; n < s->nb_streams; n++) {
535 const AVCodecDescriptor *codec_desc;
536 const char *desc;
537
538 enc = s->streams[n]->codec;
539 codec_desc = avcodec_descriptor_get(enc->codec_id);
540
541 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
542 avio_wl16(pb, 2);
543 else if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
544 avio_wl16(pb, 1);
545 else
546 avio_wl16(pb, -1);
547
548 if (enc->codec_id == AV_CODEC_ID_WMAV2)
549 desc = "Windows Media Audio V8";
550 else
551 desc = codec_desc ? codec_desc->name : NULL;
552
553 if (desc) {
554 AVIOContext *dyn_buf;
555 uint8_t *buf;
556 int len;
557
558 if (avio_open_dyn_buf(&dyn_buf) < 0)
559 return AVERROR(ENOMEM);
560
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);
569
570 avio_wl16(pb, 0); /* no parameters */
571
572 /* id */
573 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
574 avio_wl16(pb, 2);
575 avio_wl16(pb, enc->codec_tag);
576 } else {
577 avio_wl16(pb, 4);
578 avio_wl32(pb, enc->codec_tag);
579 }
580 if (!enc->codec_tag)
581 return -1;
582 }
583 end_header(pb, hpos);
584
585 /* patch the header size fields */
586
587 cur_pos = avio_tell(pb);
588 header_size = cur_pos - header_offset;
589 if (asf->is_streamed) {
590 header_size += 8 + 30 + DATA_HEADER_SIZE;
591
592 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
593 avio_wl16(pb, header_size);
594 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
595 avio_wl16(pb, header_size);
596
597 header_size -= 8 + 30 + DATA_HEADER_SIZE;
598 }
599 header_size += 24 + 6;
600 avio_seek(pb, header_offset - 14, SEEK_SET);
601 avio_wl64(pb, header_size);
602 avio_seek(pb, cur_pos, SEEK_SET);
603
604 /* movie chunk, followed by packets of packet_size */
605 asf->data_offset = cur_pos;
606 put_guid(pb, &ff_asf_data_header);
607 avio_wl64(pb, data_chunk_size);
608 put_guid(pb, &ff_asf_my_guid);
609 avio_wl64(pb, asf->nb_packets); /* nb packets */
610 avio_w8(pb, 1); /* ??? */
611 avio_w8(pb, 1); /* ??? */
612 return 0;
613 }
614
615 static int asf_write_header(AVFormatContext *s)
616 {
617 ASFContext *asf = s->priv_data;
618
619 s->packet_size = PACKET_SIZE;
620 asf->nb_packets = 0;
621
622 asf->last_indexed_pts = 0;
623 asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
624 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
625 asf->nb_index_count = 0;
626 asf->maximum_packet = 0;
627
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) {
632 //av_free(asf);
633 return -1;
634 }
635
636 avio_flush(s->pb);
637
638 asf->packet_nb_payloads = 0;
639 asf->packet_timestamp_start = -1;
640 asf->packet_timestamp_end = -1;
641 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
642 NULL, NULL, NULL, NULL);
643
644 return 0;
645 }
646
647 static 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
656 static int put_payload_parsing_info(AVFormatContext *s,
657 unsigned sendtime, unsigned duration,
658 int nb_payloads, int padsize)
659 {
660 ASFContext *asf = s->priv_data;
661 AVIOContext *pb = s->pb;
662 int ppi_size, i;
663 int64_t start = avio_tell(pb);
664
665 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
666
667 padsize -= PACKET_HEADER_MIN_SIZE;
668 if (asf->multi_payloads_present)
669 padsize--;
670 assert(padsize >= 0);
671
672 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
673 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
674 avio_w8(pb, 0x0);
675
676 if (asf->multi_payloads_present)
677 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
678
679 if (padsize > 0) {
680 if (padsize < 256)
681 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
682 else
683 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
684 }
685 avio_w8(pb, iLengthTypeFlags);
686
687 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
688
689 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
690 avio_wl16(pb, padsize - 2);
691 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
692 avio_w8(pb, padsize - 1);
693
694 avio_wl32(pb, sendtime);
695 avio_wl16(pb, duration);
696 if (asf->multi_payloads_present)
697 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
698
699 ppi_size = avio_tell(pb) - start;
700
701 return ppi_size;
702 }
703
704 static void flush_packet(AVFormatContext *s)
705 {
706 ASFContext *asf = s->priv_data;
707 int packet_hdr_size, packet_filled_size;
708
709 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
710
711 if (asf->is_streamed)
712 put_chunk(s, 0x4424, s->packet_size, 0);
713
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);
720
721 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
722 assert(packet_hdr_size <= asf->packet_size_left);
723 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
724
725 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
726
727 avio_flush(s->pb);
728 asf->nb_packets++;
729 asf->packet_nb_payloads = 0;
730 asf->packet_timestamp_start = -1;
731 asf->packet_timestamp_end = -1;
732 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
733 NULL, NULL, NULL, NULL);
734 }
735
736 static 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)
739 {
740 ASFContext *asf = s->priv_data;
741 AVIOContext *pb = &asf->pb;
742 int val;
743
744 val = stream->num;
745 if (flags & AV_PKT_FLAG_KEY)
746 val |= ASF_PL_FLAG_KEY_FRAME;
747 avio_w8(pb, val);
748
749 avio_w8(pb, stream->seq); // Media object number
750 avio_wl32(pb, m_obj_offset); // Offset Into Media Object
751
752 // Replicated Data shall be at least 8 bytes long.
753 // The first 4 bytes of data shall contain the
754 // Size of the Media Object that the payload belongs to.
755 // The next 4 bytes of data shall contain the
756 // Presentation Time for the media object that the payload belongs to.
757 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
758
759 avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
760 avio_wl32(pb, presentation_time); // Replicated Data - Presentation Time
761
762 if (asf->multi_payloads_present) {
763 avio_wl16(pb, payload_len); // payload length
764 }
765 }
766
767 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
768 int timestamp, const uint8_t *buf,
769 int m_obj_size, int flags)
770 {
771 ASFContext *asf = s->priv_data;
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);
779
780 asf->packet_size_left = PACKET_SIZE;
781 if (asf->multi_payloads_present) {
782 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
783 } else {
784 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
785 }
786 asf->packet_timestamp_start = timestamp;
787 } else {
788 // multi payloads
789 frag_len1 = asf->packet_size_left -
790 PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS -
791 PACKET_HEADER_MIN_SIZE - 1;
792
793 if (frag_len1 < payload_len &&
794 avst->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
795 flush_packet(s);
796 continue;
797 }
798 }
799 if (frag_len1 > 0) {
800 if (payload_len > frag_len1)
801 payload_len = frag_len1;
802 else if (payload_len == (frag_len1 - 1))
803 payload_len = frag_len1 - 2; // additional byte need to put padding length
804
805 put_payload_header(s, stream, timestamp + PREROLL_TIME,
806 m_obj_size, m_obj_offset, payload_len, flags);
807 avio_write(&asf->pb, buf, payload_len);
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);
813 asf->packet_timestamp_end = timestamp;
814
815 asf->packet_nb_payloads++;
816 } else {
817 payload_len = 0;
818 }
819 m_obj_offset += payload_len;
820 buf += payload_len;
821
822 if (!asf->multi_payloads_present)
823 flush_packet(s);
824 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
825 flush_packet(s);
826 }
827 stream->seq++;
828 }
829
830 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
831 {
832 ASFContext *asf = s->priv_data;
833 AVIOContext *pb = s->pb;
834 ASFStream *stream;
835 int64_t duration;
836 AVCodecContext *codec;
837 int64_t packet_st, pts;
838 int start_sec, i;
839 int flags = pkt->flags;
840 uint64_t offset = avio_tell(pb);
841
842 codec = s->streams[pkt->stream_index]->codec;
843 stream = &asf->streams[pkt->stream_index];
844
845 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
846 flags &= ~AV_PKT_FLAG_KEY;
847
848 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
849 assert(pts != AV_NOPTS_VALUE);
850 duration = pts * 10000;
851 asf->duration = FFMAX(asf->duration, duration + pkt->duration * 10000);
852
853 packet_st = asf->nb_packets;
854 put_frame(s, stream, s->streams[pkt->stream_index],
855 pkt->dts, pkt->data, pkt->size, flags);
856
857 /* check index */
858 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
859 start_sec = (int)(duration / INT64_C(10000000));
860 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
861 for (i = asf->nb_index_count; i < start_sec; i++) {
862 if (i >= asf->nb_index_memory_alloc) {
863 int err;
864 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
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 }
871 }
872 // store
873 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
874 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets - packet_st);
875 asf->index_ptr[i].send_time = start_sec * INT64_C(10000000);
876 asf->index_ptr[i].offset = offset;
877 asf->maximum_packet = FFMAX(asf->maximum_packet,
878 (uint16_t)(asf->nb_packets - packet_st));
879 }
880 asf->nb_index_count = start_sec;
881 asf->last_indexed_pts = duration;
882 }
883 }
884 return 0;
885 }
886
887 static int asf_write_index(AVFormatContext *s, ASFIndex *index,
888 uint16_t max, uint32_t count)
889 {
890 AVIOContext *pb = s->pb;
891 int i;
892
893 put_guid(pb, &ff_asf_simple_index_header);
894 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
895 put_guid(pb, &ff_asf_my_guid);
896 avio_wl64(pb, ASF_INDEXED_INTERVAL);
897 avio_wl32(pb, max);
898 avio_wl32(pb, count);
899 for (i = 0; i < count; i++) {
900 avio_wl32(pb, index[i].packet_number);
901 avio_wl16(pb, index[i].packet_count);
902 }
903
904 return 0;
905 }
906
907 static int asf_write_trailer(AVFormatContext *s)
908 {
909 ASFContext *asf = s->priv_data;
910 int64_t file_size, data_size;
911
912 /* flush the current packet */
913 if (asf->pb.buf_ptr > asf->pb.buffer)
914 flush_packet(s);
915
916 /* write index */
917 data_size = avio_tell(s->pb);
918 if ((!asf->is_streamed) && (asf->nb_index_count != 0))
919 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
920 avio_flush(s->pb);
921
922 if (asf->is_streamed || !s->pb->seekable) {
923 put_chunk(s, 0x4524, 0, 0); /* end of stream */
924 } else {
925 /* rewrite an updated header */
926 file_size = avio_tell(s->pb);
927 avio_seek(s->pb, 0, SEEK_SET);
928 asf_write_header1(s, file_size, data_size - asf->data_offset);
929 }
930
931 av_free(asf->index_ptr);
932 return 0;
933 }
934
935 #if CONFIG_ASF_MUXER
936 AVOutputFormat ff_asf_muxer = {
937 .name = "asf",
938 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
939 .mime_type = "video/x-ms-asf",
940 .extensions = "asf,wmv,wma",
941 .priv_data_size = sizeof(ASFContext),
942 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
943 .video_codec = AV_CODEC_ID_MSMPEG4V3,
944 .write_header = asf_write_header,
945 .write_packet = asf_write_packet,
946 .write_trailer = asf_write_trailer,
947 .flags = AVFMT_GLOBALHEADER,
948 .codec_tag = (const AVCodecTag * const []) {
949 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
950 },
951 };
952 #endif /* CONFIG_ASF_MUXER */
953
954 #if CONFIG_ASF_STREAM_MUXER
955 AVOutputFormat ff_asf_stream_muxer = {
956 .name = "asf_stream",
957 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
958 .mime_type = "video/x-ms-asf",
959 .extensions = "asf,wmv,wma",
960 .priv_data_size = sizeof(ASFContext),
961 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
962 .video_codec = AV_CODEC_ID_MSMPEG4V3,
963 .write_header = asf_write_stream_header,
964 .write_packet = asf_write_packet,
965 .write_trailer = asf_write_trailer,
966 .flags = AVFMT_GLOBALHEADER,
967 .codec_tag = (const AVCodecTag * const []) {
968 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
969 },
970 };
971 #endif /* CONFIG_ASF_STREAM_MUXER */