asfenc: write tags in proper UTF-16.
[libav.git] / libavformat / asfenc.c
1 /*
2 * ASF muxer
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21 #include "avformat.h"
22 #include "metadata.h"
23 #include "riff.h"
24 #include "asf.h"
25
26 #undef NDEBUG
27 #include <assert.h>
28
29
30 #define ASF_INDEXED_INTERVAL 10000000
31 #define ASF_INDEX_BLOCK 600
32
33 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
34 #define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
35 ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
36 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
37 )
38
39 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
40 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
41 #else
42 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
43 #endif
44
45 #define ASF_PPI_PROPERTY_FLAGS (\
46 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
47 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
48 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
49 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
50 )
51
52 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
53
54 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
55
56 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
57 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
58 #endif
59 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
60 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
61 #endif
62 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
63 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
64 #endif
65 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
66 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
67 #endif
68
69
70 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
71 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
72 #endif
73 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
74 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
75 #endif
76 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
77 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
78 #endif
79 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
80 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
81 #endif
82
83 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
84 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
85 #endif
86 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
87 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
88 #endif
89 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
90 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
91 #endif
92 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
93 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
94 #endif
95
96 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
97 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
98 #endif
99 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
100 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
101 #endif
102 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
103 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
104 #endif
105 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
106 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
107 #endif
108
109 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
110 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
111 #endif
112 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
113 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
114 #endif
115 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
116 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
117 #endif
118 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
119 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
120 #endif
121
122 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
123 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
124 #endif
125 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
126 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
127 #endif
128 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
129 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
130 #endif
131 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
132 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
133 #endif
134
135 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
136 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
137 #endif
138 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
139 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
140 #endif
141 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
142 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
143 #endif
144
145 #define PACKET_HEADER_MIN_SIZE (\
146 ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
147 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
148 1 + /*Length Type Flags*/ \
149 1 + /*Property Flags*/ \
150 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
151 ASF_PPI_SEQUENCE_FIELD_SIZE + \
152 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
153 4 + /*Send Time Field*/ \
154 2 /*Duration Field*/ \
155 )
156
157
158 // Replicated Data shall be at least 8 bytes long.
159 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
160
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 \
167 )
168
169 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
170 1 + /*Stream Number*/ \
171 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
172 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
173 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
174 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
175 ASF_PAYLOAD_LENGTH_FIELD_SIZE \
176 )
177
178 #define SINGLE_PAYLOAD_DATA_LENGTH (\
179 PACKET_SIZE - \
180 PACKET_HEADER_MIN_SIZE - \
181 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
182 )
183
184 #define MULTI_PAYLOAD_CONSTANT (\
185 PACKET_SIZE - \
186 PACKET_HEADER_MIN_SIZE - \
187 1 - /*Payload Flags*/ \
188 2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
189 )
190
191 static const AVCodecTag codec_asf_bmp_tags[] = {
192 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
193 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
194 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
195 { CODEC_ID_NONE, 0 },
196 };
197
198 #define PREROLL_TIME 3100
199
200 static void put_guid(ByteIOContext *s, const ff_asf_guid *g)
201 {
202 assert(sizeof(*g) == 16);
203 put_buffer(s, *g, sizeof(*g));
204 }
205
206 static int put_str16_nolen(ByteIOContext *s, const char *tag)
207 {
208 const uint8_t *q = tag;
209 int ret = 0;
210
211 while (*q) {
212 uint32_t ch;
213 uint16_t tmp;
214
215 GET_UTF8(ch, *q++, break;)
216 PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
217 }
218 put_le16(s, 0);
219 ret += 2;
220 return ret;
221 }
222
223 static void put_str16(ByteIOContext *s, const char *tag)
224 {
225 int len;
226 uint8_t *pb;
227 ByteIOContext *dyn_buf;
228 if (url_open_dyn_buf(&dyn_buf) < 0)
229 return;
230
231 put_str16_nolen(dyn_buf, tag);
232 len = url_close_dyn_buf(dyn_buf, &pb);
233 put_le16(s, len);
234 put_buffer(s, pb, len);
235 av_freep(&pb);
236 }
237
238 static int64_t put_header(ByteIOContext *pb, const ff_asf_guid *g)
239 {
240 int64_t pos;
241
242 pos = url_ftell(pb);
243 put_guid(pb, g);
244 put_le64(pb, 24);
245 return pos;
246 }
247
248 /* update header size */
249 static void end_header(ByteIOContext *pb, int64_t pos)
250 {
251 int64_t pos1;
252
253 pos1 = url_ftell(pb);
254 url_fseek(pb, pos + 16, SEEK_SET);
255 put_le64(pb, pos1 - pos);
256 url_fseek(pb, pos1, SEEK_SET);
257 }
258
259 /* write an asf chunk (only used in streaming case) */
260 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
261 {
262 ASFContext *asf = s->priv_data;
263 ByteIOContext *pb = s->pb;
264 int length;
265
266 length = payload_length + 8;
267 put_le16(pb, type);
268 put_le16(pb, length); //size
269 put_le32(pb, asf->seqno);//sequence number
270 put_le16(pb, flags); /* unknown bytes */
271 put_le16(pb, length); //size_confirm
272 asf->seqno++;
273 }
274
275 /* convert from unix to windows time */
276 static int64_t unix_to_file_time(int ti)
277 {
278 int64_t t;
279
280 t = ti * INT64_C(10000000);
281 t += INT64_C(116444736000000000);
282 return t;
283 }
284
285 /* write the header (used two times if non streamed) */
286 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
287 {
288 ASFContext *asf = s->priv_data;
289 ByteIOContext *pb = s->pb;
290 AVMetadataTag *tags[5];
291 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
292 int has_title;
293 int metadata_count;
294 AVCodecContext *enc;
295 int64_t header_offset, cur_pos, hpos;
296 int bit_rate;
297 int64_t duration;
298
299 tags[0] = av_metadata_get(s->metadata, "title" , NULL, 0);
300 tags[1] = av_metadata_get(s->metadata, "author" , NULL, 0);
301 tags[2] = av_metadata_get(s->metadata, "copyright", NULL, 0);
302 tags[3] = av_metadata_get(s->metadata, "comment" , NULL, 0);
303 tags[4] = av_metadata_get(s->metadata, "rating" , NULL, 0);
304
305 duration = asf->duration + PREROLL_TIME * 10000;
306 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
307 metadata_count = s->metadata ? s->metadata->count : 0;
308
309 bit_rate = 0;
310 for(n=0;n<s->nb_streams;n++) {
311 enc = s->streams[n]->codec;
312
313 av_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
314
315 bit_rate += enc->bit_rate;
316 }
317
318 if (asf->is_streamed) {
319 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
320 }
321
322 put_guid(pb, &ff_asf_header);
323 put_le64(pb, -1); /* header length, will be patched after */
324 put_le32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
325 put_byte(pb, 1); /* ??? */
326 put_byte(pb, 2); /* ??? */
327
328 /* file header */
329 header_offset = url_ftell(pb);
330 hpos = put_header(pb, &ff_asf_file_header);
331 put_guid(pb, &ff_asf_my_guid);
332 put_le64(pb, file_size);
333 file_time = 0;
334 put_le64(pb, unix_to_file_time(file_time));
335 put_le64(pb, asf->nb_packets); /* number of packets */
336 put_le64(pb, duration); /* end time stamp (in 100ns units) */
337 put_le64(pb, asf->duration); /* duration (in 100ns units) */
338 put_le64(pb, PREROLL_TIME); /* start time stamp */
339 put_le32(pb, (asf->is_streamed || url_is_streamed(pb)) ? 3 : 2); /* ??? */
340 put_le32(pb, s->packet_size); /* packet size */
341 put_le32(pb, s->packet_size); /* packet size */
342 put_le32(pb, bit_rate); /* Nominal data rate in bps */
343 end_header(pb, hpos);
344
345 /* unknown headers */
346 hpos = put_header(pb, &ff_asf_head1_guid);
347 put_guid(pb, &ff_asf_head2_guid);
348 put_le32(pb, 6);
349 put_le16(pb, 0);
350 end_header(pb, hpos);
351
352 /* title and other infos */
353 if (has_title) {
354 int len;
355 uint8_t *buf;
356 ByteIOContext *dyn_buf;
357
358 if (url_open_dyn_buf(&dyn_buf) < 0)
359 return AVERROR(ENOMEM);
360
361 hpos = put_header(pb, &ff_asf_comment_header);
362
363 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
364 len = tags[n] ? put_str16_nolen(dyn_buf, tags[n]->value) : 0;
365 put_le16(pb, len);
366 }
367 len = url_close_dyn_buf(dyn_buf, &buf);
368 put_buffer(pb, buf, len);
369 av_freep(&buf);
370 end_header(pb, hpos);
371 }
372 if (metadata_count) {
373 AVMetadataTag *tag = NULL;
374 hpos = put_header(pb, &ff_asf_extended_content_header);
375 put_le16(pb, metadata_count);
376 while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
377 put_str16(pb, tag->key);
378 put_le16(pb, 0);
379 put_str16(pb, tag->value);
380 }
381 end_header(pb, hpos);
382 }
383
384 /* stream headers */
385 for(n=0;n<s->nb_streams;n++) {
386 int64_t es_pos;
387 // ASFStream *stream = &asf->streams[n];
388
389 enc = s->streams[n]->codec;
390 asf->streams[n].num = n + 1;
391 asf->streams[n].seq = 0;
392
393
394 switch(enc->codec_type) {
395 case CODEC_TYPE_AUDIO:
396 wav_extra_size = 0;
397 extra_size = 18 + wav_extra_size;
398 extra_size2 = 8;
399 break;
400 default:
401 case CODEC_TYPE_VIDEO:
402 wav_extra_size = enc->extradata_size;
403 extra_size = 0x33 + wav_extra_size;
404 extra_size2 = 0;
405 break;
406 }
407
408 hpos = put_header(pb, &ff_asf_stream_header);
409 if (enc->codec_type == CODEC_TYPE_AUDIO) {
410 put_guid(pb, &ff_asf_audio_stream);
411 put_guid(pb, &ff_asf_audio_conceal_spread);
412 } else {
413 put_guid(pb, &ff_asf_video_stream);
414 put_guid(pb, &ff_asf_video_conceal_none);
415 }
416 put_le64(pb, 0); /* ??? */
417 es_pos = url_ftell(pb);
418 put_le32(pb, extra_size); /* wav header len */
419 put_le32(pb, extra_size2); /* additional data len */
420 put_le16(pb, n + 1); /* stream number */
421 put_le32(pb, 0); /* ??? */
422
423 if (enc->codec_type == CODEC_TYPE_AUDIO) {
424 /* WAVEFORMATEX header */
425 int wavsize = ff_put_wav_header(pb, enc);
426 if ((enc->codec_id != CODEC_ID_MP3) && (enc->codec_id != CODEC_ID_MP2) && (enc->codec_id != CODEC_ID_ADPCM_IMA_WAV) && (enc->extradata_size==0)) {
427 wavsize += 2;
428 put_le16(pb, 0);
429 }
430
431 if (wavsize < 0)
432 return -1;
433 if (wavsize != extra_size) {
434 cur_pos = url_ftell(pb);
435 url_fseek(pb, es_pos, SEEK_SET);
436 put_le32(pb, wavsize); /* wav header len */
437 url_fseek(pb, cur_pos, SEEK_SET);
438 }
439 /* ERROR Correction */
440 put_byte(pb, 0x01);
441 if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
442 put_le16(pb, 0x0190);
443 put_le16(pb, 0x0190);
444 }else{
445 put_le16(pb, enc->block_align);
446 put_le16(pb, enc->block_align);
447 }
448 put_le16(pb, 0x01);
449 put_byte(pb, 0x00);
450 } else {
451 put_le32(pb, enc->width);
452 put_le32(pb, enc->height);
453 put_byte(pb, 2); /* ??? */
454 put_le16(pb, 40 + enc->extradata_size); /* size */
455
456 /* BITMAPINFOHEADER header */
457 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
458 }
459 end_header(pb, hpos);
460 }
461
462 /* media comments */
463
464 hpos = put_header(pb, &ff_asf_codec_comment_header);
465 put_guid(pb, &ff_asf_codec_comment1_header);
466 put_le32(pb, s->nb_streams);
467 for(n=0;n<s->nb_streams;n++) {
468 AVCodec *p;
469 const char *desc;
470 int len;
471 uint8_t *buf;
472 ByteIOContext *dyn_buf;
473
474 enc = s->streams[n]->codec;
475 p = avcodec_find_encoder(enc->codec_id);
476
477 if(enc->codec_type == CODEC_TYPE_AUDIO)
478 put_le16(pb, 2);
479 else if(enc->codec_type == CODEC_TYPE_VIDEO)
480 put_le16(pb, 1);
481 else
482 put_le16(pb, -1);
483
484 if(enc->codec_id == CODEC_ID_WMAV2)
485 desc = "Windows Media Audio V8";
486 else
487 desc = p ? p->name : enc->codec_name;
488
489 if ( url_open_dyn_buf(&dyn_buf) < 0)
490 return AVERROR(ENOMEM);
491
492 put_str16_nolen(dyn_buf, desc);
493 len = url_close_dyn_buf(dyn_buf, &buf);
494 put_le16(pb, len / 2); // "number of characters" = length in bytes / 2
495
496 put_buffer(pb, buf, len);
497 av_freep(&buf);
498
499 put_le16(pb, 0); /* no parameters */
500
501
502 /* id */
503 if (enc->codec_type == CODEC_TYPE_AUDIO) {
504 put_le16(pb, 2);
505 put_le16(pb, enc->codec_tag);
506 } else {
507 put_le16(pb, 4);
508 put_le32(pb, enc->codec_tag);
509 }
510 if(!enc->codec_tag)
511 return -1;
512 }
513 end_header(pb, hpos);
514
515 /* patch the header size fields */
516
517 cur_pos = url_ftell(pb);
518 header_size = cur_pos - header_offset;
519 if (asf->is_streamed) {
520 header_size += 8 + 30 + 50;
521
522 url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
523 put_le16(pb, header_size);
524 url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
525 put_le16(pb, header_size);
526
527 header_size -= 8 + 30 + 50;
528 }
529 header_size += 24 + 6;
530 url_fseek(pb, header_offset - 14, SEEK_SET);
531 put_le64(pb, header_size);
532 url_fseek(pb, cur_pos, SEEK_SET);
533
534 /* movie chunk, followed by packets of packet_size */
535 asf->data_offset = cur_pos;
536 put_guid(pb, &ff_asf_data_header);
537 put_le64(pb, data_chunk_size);
538 put_guid(pb, &ff_asf_my_guid);
539 put_le64(pb, asf->nb_packets); /* nb packets */
540 put_byte(pb, 1); /* ??? */
541 put_byte(pb, 1); /* ??? */
542 return 0;
543 }
544
545 static int asf_write_header(AVFormatContext *s)
546 {
547 ASFContext *asf = s->priv_data;
548
549 s->packet_size = PACKET_SIZE;
550 asf->nb_packets = 0;
551
552 asf->last_indexed_pts = 0;
553 asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
554 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
555 asf->nb_index_count = 0;
556 asf->maximum_packet = 0;
557
558 /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
559 * at the moment this function is done. It is needed to use asf as
560 * streamable format. */
561 if (asf_write_header1(s, 0, 50) < 0) {
562 //av_free(asf);
563 return -1;
564 }
565
566 put_flush_packet(s->pb);
567
568 asf->packet_nb_payloads = 0;
569 asf->packet_timestamp_start = -1;
570 asf->packet_timestamp_end = -1;
571 init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
572 NULL, NULL, NULL, NULL);
573
574 return 0;
575 }
576
577 static int asf_write_stream_header(AVFormatContext *s)
578 {
579 ASFContext *asf = s->priv_data;
580
581 asf->is_streamed = 1;
582
583 return asf_write_header(s);
584 }
585
586 static int put_payload_parsing_info(
587 AVFormatContext *s,
588 unsigned int sendtime,
589 unsigned int duration,
590 int nb_payloads,
591 int padsize
592 )
593 {
594 ASFContext *asf = s->priv_data;
595 ByteIOContext *pb = s->pb;
596 int ppi_size, i;
597 int64_t start= url_ftell(pb);
598
599 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
600
601 padsize -= PACKET_HEADER_MIN_SIZE;
602 if(asf->multi_payloads_present)
603 padsize--;
604 assert(padsize>=0);
605
606 put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
607 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
608 put_byte(pb, 0x0);
609 }
610
611 if (asf->multi_payloads_present)
612 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
613
614 if (padsize > 0) {
615 if (padsize < 256)
616 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
617 else
618 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
619 }
620 put_byte(pb, iLengthTypeFlags);
621
622 put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
623
624 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
625 put_le16(pb, padsize - 2);
626 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
627 put_byte(pb, padsize - 1);
628
629 put_le32(pb, sendtime);
630 put_le16(pb, duration);
631 if (asf->multi_payloads_present)
632 put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
633
634 ppi_size = url_ftell(pb) - start;
635
636 return ppi_size;
637 }
638
639 static void flush_packet(AVFormatContext *s)
640 {
641 ASFContext *asf = s->priv_data;
642 int packet_hdr_size, packet_filled_size;
643
644 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
645
646 if (asf->is_streamed) {
647 put_chunk(s, 0x4424, s->packet_size, 0);
648 }
649
650 packet_hdr_size = put_payload_parsing_info(
651 s,
652 asf->packet_timestamp_start,
653 asf->packet_timestamp_end - asf->packet_timestamp_start,
654 asf->packet_nb_payloads,
655 asf->packet_size_left
656 );
657
658 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
659 assert(packet_hdr_size <= asf->packet_size_left);
660 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
661
662 put_buffer(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
663
664 put_flush_packet(s->pb);
665 asf->nb_packets++;
666 asf->packet_nb_payloads = 0;
667 asf->packet_timestamp_start = -1;
668 asf->packet_timestamp_end = -1;
669 init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
670 NULL, NULL, NULL, NULL);
671 }
672
673 static void put_payload_header(
674 AVFormatContext *s,
675 ASFStream *stream,
676 int presentation_time,
677 int m_obj_size,
678 int m_obj_offset,
679 int payload_len,
680 int flags
681 )
682 {
683 ASFContext *asf = s->priv_data;
684 ByteIOContext *pb = &asf->pb;
685 int val;
686
687 val = stream->num;
688 if (flags & PKT_FLAG_KEY)
689 val |= ASF_PL_FLAG_KEY_FRAME;
690 put_byte(pb, val);
691
692 put_byte(pb, stream->seq); //Media object number
693 put_le32(pb, m_obj_offset); //Offset Into Media Object
694
695 // Replicated Data shall be at least 8 bytes long.
696 // The first 4 bytes of data shall contain the
697 // Size of the Media Object that the payload belongs to.
698 // The next 4 bytes of data shall contain the
699 // Presentation Time for the media object that the payload belongs to.
700 put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
701
702 put_le32(pb, m_obj_size); //Replicated Data - Media Object Size
703 put_le32(pb, presentation_time);//Replicated Data - Presentation Time
704
705 if (asf->multi_payloads_present){
706 put_le16(pb, payload_len); //payload length
707 }
708 }
709
710 static void put_frame(
711 AVFormatContext *s,
712 ASFStream *stream,
713 AVStream *avst,
714 int timestamp,
715 const uint8_t *buf,
716 int m_obj_size,
717 int flags
718 )
719 {
720 ASFContext *asf = s->priv_data;
721 int m_obj_offset, payload_len, frag_len1;
722
723 m_obj_offset = 0;
724 while (m_obj_offset < m_obj_size) {
725 payload_len = m_obj_size - m_obj_offset;
726 if (asf->packet_timestamp_start == -1) {
727 asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
728
729 asf->packet_size_left = PACKET_SIZE;
730 if (asf->multi_payloads_present){
731 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
732 }
733 else {
734 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
735 }
736 asf->packet_timestamp_start = timestamp;
737 }
738 else {
739 // multi payloads
740 frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
741
742 if(frag_len1 < payload_len && avst->codec->codec_type == CODEC_TYPE_AUDIO){
743 flush_packet(s);
744 continue;
745 }
746 }
747 if (frag_len1 > 0) {
748 if (payload_len > frag_len1)
749 payload_len = frag_len1;
750 else if (payload_len == (frag_len1 - 1))
751 payload_len = frag_len1 - 2; //additional byte need to put padding length
752
753 put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
754 put_buffer(&asf->pb, buf, payload_len);
755
756 if (asf->multi_payloads_present)
757 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
758 else
759 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
760 asf->packet_timestamp_end = timestamp;
761
762 asf->packet_nb_payloads++;
763 } else {
764 payload_len = 0;
765 }
766 m_obj_offset += payload_len;
767 buf += payload_len;
768
769 if (!asf->multi_payloads_present)
770 flush_packet(s);
771 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
772 flush_packet(s);
773 }
774 stream->seq++;
775 }
776
777 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
778 {
779 ASFContext *asf = s->priv_data;
780 ASFStream *stream;
781 int64_t duration;
782 AVCodecContext *codec;
783 int64_t packet_st,pts;
784 int start_sec,i;
785 int flags= pkt->flags;
786
787 codec = s->streams[pkt->stream_index]->codec;
788 stream = &asf->streams[pkt->stream_index];
789
790 if(codec->codec_type == CODEC_TYPE_AUDIO)
791 flags &= ~PKT_FLAG_KEY;
792
793 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
794 assert(pts != AV_NOPTS_VALUE);
795 duration = pts * 10000;
796 asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
797
798 packet_st = asf->nb_packets;
799 put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
800
801 /* check index */
802 if ((!asf->is_streamed) && (flags & PKT_FLAG_KEY)) {
803 start_sec = (int)(duration / INT64_C(10000000));
804 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
805 for(i=asf->nb_index_count;i<start_sec;i++) {
806 if (i>=asf->nb_index_memory_alloc) {
807 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
808 asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
809 }
810 // store
811 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
812 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets-packet_st);
813 asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
814 }
815 asf->nb_index_count = start_sec;
816 asf->last_indexed_pts = duration;
817 }
818 }
819 return 0;
820 }
821
822 //
823 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
824 {
825 ByteIOContext *pb = s->pb;
826 int i;
827
828 put_guid(pb, &ff_asf_simple_index_header);
829 put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
830 put_guid(pb, &ff_asf_my_guid);
831 put_le64(pb, ASF_INDEXED_INTERVAL);
832 put_le32(pb, max);
833 put_le32(pb, count);
834 for(i=0; i<count; i++) {
835 put_le32(pb, index[i].packet_number);
836 put_le16(pb, index[i].packet_count);
837 }
838
839 return 0;
840 }
841
842 static int asf_write_trailer(AVFormatContext *s)
843 {
844 ASFContext *asf = s->priv_data;
845 int64_t file_size,data_size;
846
847 /* flush the current packet */
848 if (asf->pb.buf_ptr > asf->pb.buffer)
849 flush_packet(s);
850
851 /* write index */
852 data_size = url_ftell(s->pb);
853 if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
854 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
855 }
856 put_flush_packet(s->pb);
857
858 if (asf->is_streamed || url_is_streamed(s->pb)) {
859 put_chunk(s, 0x4524, 0, 0); /* end of stream */
860 } else {
861 /* rewrite an updated header */
862 file_size = url_ftell(s->pb);
863 url_fseek(s->pb, 0, SEEK_SET);
864 asf_write_header1(s, file_size, data_size - asf->data_offset);
865 }
866
867 put_flush_packet(s->pb);
868 av_free(asf->index_ptr);
869 return 0;
870 }
871
872 #if CONFIG_ASF_MUXER
873 AVOutputFormat asf_muxer = {
874 "asf",
875 NULL_IF_CONFIG_SMALL("ASF format"),
876 "video/x-ms-asf",
877 "asf,wmv,wma",
878 sizeof(ASFContext),
879 #if CONFIG_LIBMP3LAME
880 CODEC_ID_MP3,
881 #else
882 CODEC_ID_MP2,
883 #endif
884 CODEC_ID_MSMPEG4V3,
885 asf_write_header,
886 asf_write_packet,
887 asf_write_trailer,
888 .flags = AVFMT_GLOBALHEADER,
889 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
890 .metadata_conv = ff_asf_metadata_conv,
891 };
892 #endif
893
894 #if CONFIG_ASF_STREAM_MUXER
895 AVOutputFormat asf_stream_muxer = {
896 "asf_stream",
897 NULL_IF_CONFIG_SMALL("ASF format"),
898 "video/x-ms-asf",
899 "asf,wmv,wma",
900 sizeof(ASFContext),
901 #if CONFIG_LIBMP3LAME
902 CODEC_ID_MP3,
903 #else
904 CODEC_ID_MP2,
905 #endif
906 CODEC_ID_MSMPEG4V3,
907 asf_write_stream_header,
908 asf_write_packet,
909 asf_write_trailer,
910 .flags = AVFMT_GLOBALHEADER,
911 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
912 .metadata_conv = ff_asf_metadata_conv,
913 };
914 #endif //CONFIG_ASF_STREAM_MUXER