Statistics
| Branch: | Revision:

ffmpeg / libavformat / asfenc.c @ 531d8fa3

History | View | Annotate | Download (29.4 KB)

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 void put_str16_nolen(ByteIOContext *s, const char *tag)
207
{
208
    int c;
209

    
210
    do{
211
        c = (uint8_t)*tag++;
212
        put_le16(s, c);
213
    }while(c);
214
}
215

    
216
static int64_t put_header(ByteIOContext *pb, const ff_asf_guid *g)
217
{
218
    int64_t pos;
219

    
220
    pos = url_ftell(pb);
221
    put_guid(pb, g);
222
    put_le64(pb, 24);
223
    return pos;
224
}
225

    
226
/* update header size */
227
static void end_header(ByteIOContext *pb, int64_t pos)
228
{
229
    int64_t pos1;
230

    
231
    pos1 = url_ftell(pb);
232
    url_fseek(pb, pos + 16, SEEK_SET);
233
    put_le64(pb, pos1 - pos);
234
    url_fseek(pb, pos1, SEEK_SET);
235
}
236

    
237
/* write an asf chunk (only used in streaming case) */
238
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
239
{
240
    ASFContext *asf = s->priv_data;
241
    ByteIOContext *pb = s->pb;
242
    int length;
243

    
244
    length = payload_length + 8;
245
    put_le16(pb, type);
246
    put_le16(pb, length);    //size
247
    put_le32(pb, asf->seqno);//sequence number
248
    put_le16(pb, flags); /* unknown bytes */
249
    put_le16(pb, length);    //size_confirm
250
    asf->seqno++;
251
}
252

    
253
/* convert from unix to windows time */
254
static int64_t unix_to_file_time(int ti)
255
{
256
    int64_t t;
257

    
258
    t = ti * INT64_C(10000000);
259
    t += INT64_C(116444736000000000);
260
    return t;
261
}
262

    
263
/* write the header (used two times if non streamed) */
264
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
265
{
266
    ASFContext *asf = s->priv_data;
267
    ByteIOContext *pb = s->pb;
268
    AVMetadataTag *title, *author, *copyright, *comment;
269
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
270
    int has_title;
271
    int metadata_count;
272
    AVCodecContext *enc;
273
    int64_t header_offset, cur_pos, hpos;
274
    int bit_rate;
275
    int64_t duration;
276

    
277
    title     = av_metadata_get(s->metadata, "title"    , NULL, 0);
278
    author    = av_metadata_get(s->metadata, "author"   , NULL, 0);
279
    copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
280
    comment   = av_metadata_get(s->metadata, "comment"  , NULL, 0);
281

    
282
    duration = asf->duration + PREROLL_TIME * 10000;
283
    has_title = title || author || copyright || comment;
284
    metadata_count = s->metadata ? s->metadata->count : 0;
285

    
286
    bit_rate = 0;
287
    for(n=0;n<s->nb_streams;n++) {
288
        enc = s->streams[n]->codec;
289

    
290
        av_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
291

    
292
        bit_rate += enc->bit_rate;
293
    }
294

    
295
    if (asf->is_streamed) {
296
        put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
297
    }
298

    
299
    put_guid(pb, &ff_asf_header);
300
    put_le64(pb, -1); /* header length, will be patched after */
301
    put_le32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
302
    put_byte(pb, 1); /* ??? */
303
    put_byte(pb, 2); /* ??? */
304

    
305
    /* file header */
306
    header_offset = url_ftell(pb);
307
    hpos = put_header(pb, &ff_asf_file_header);
308
    put_guid(pb, &ff_asf_my_guid);
309
    put_le64(pb, file_size);
310
    file_time = 0;
311
    put_le64(pb, unix_to_file_time(file_time));
312
    put_le64(pb, asf->nb_packets); /* number of packets */
313
    put_le64(pb, duration); /* end time stamp (in 100ns units) */
314
    put_le64(pb, asf->duration); /* duration (in 100ns units) */
315
    put_le64(pb, PREROLL_TIME); /* start time stamp */
316
    put_le32(pb, (asf->is_streamed || url_is_streamed(pb)) ? 3 : 2); /* ??? */
317
    put_le32(pb, s->packet_size); /* packet size */
318
    put_le32(pb, s->packet_size); /* packet size */
319
    put_le32(pb, bit_rate); /* Nominal data rate in bps */
320
    end_header(pb, hpos);
321

    
322
    /* unknown headers */
323
    hpos = put_header(pb, &ff_asf_head1_guid);
324
    put_guid(pb, &ff_asf_head2_guid);
325
    put_le32(pb, 6);
326
    put_le16(pb, 0);
327
    end_header(pb, hpos);
328

    
329
    /* title and other infos */
330
    if (has_title) {
331
        hpos = put_header(pb, &ff_asf_comment_header);
332
        put_le16(pb, title     ? 2 * (strlen(title->value    ) + 1) : 0);
333
        put_le16(pb, author    ? 2 * (strlen(author->value   ) + 1) : 0);
334
        put_le16(pb, copyright ? 2 * (strlen(copyright->value) + 1) : 0);
335
        put_le16(pb, comment   ? 2 * (strlen(comment->value  ) + 1) : 0);
336
        put_le16(pb, 0);
337
        if (title    ) put_str16_nolen(pb, title->value    );
338
        if (author   ) put_str16_nolen(pb, author->value   );
339
        if (copyright) put_str16_nolen(pb, copyright->value);
340
        if (comment  ) put_str16_nolen(pb, comment->value  );
341
        end_header(pb, hpos);
342
    }
343
    if (metadata_count) {
344
        AVMetadataTag *tag = NULL;
345
        hpos = put_header(pb, &ff_asf_extended_content_header);
346
        put_le16(pb, metadata_count);
347
        while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
348
            put_le16(pb, 2*(strlen(tag->key) + 4));
349
            put_le16(pb, 'W');
350
            put_le16(pb, 'M');
351
            put_le16(pb, '/');
352
            put_str16_nolen(pb, tag->key);
353
            put_le16(pb, 0);
354
            put_le16(pb, 2*(strlen(tag->value) + 1));
355
            put_str16_nolen(pb, tag->value);
356
        }
357
        end_header(pb, hpos);
358
    }
359

    
360
    /* stream headers */
361
    for(n=0;n<s->nb_streams;n++) {
362
        int64_t es_pos;
363
        //        ASFStream *stream = &asf->streams[n];
364

    
365
        enc = s->streams[n]->codec;
366
        asf->streams[n].num = n + 1;
367
        asf->streams[n].seq = 0;
368

    
369

    
370
        switch(enc->codec_type) {
371
        case CODEC_TYPE_AUDIO:
372
            wav_extra_size = 0;
373
            extra_size = 18 + wav_extra_size;
374
            extra_size2 = 8;
375
            break;
376
        default:
377
        case CODEC_TYPE_VIDEO:
378
            wav_extra_size = enc->extradata_size;
379
            extra_size = 0x33 + wav_extra_size;
380
            extra_size2 = 0;
381
            break;
382
        }
383

    
384
        hpos = put_header(pb, &ff_asf_stream_header);
385
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
386
            put_guid(pb, &ff_asf_audio_stream);
387
            put_guid(pb, &ff_asf_audio_conceal_spread);
388
        } else {
389
            put_guid(pb, &ff_asf_video_stream);
390
            put_guid(pb, &ff_asf_video_conceal_none);
391
        }
392
        put_le64(pb, 0); /* ??? */
393
        es_pos = url_ftell(pb);
394
        put_le32(pb, extra_size); /* wav header len */
395
        put_le32(pb, extra_size2); /* additional data len */
396
        put_le16(pb, n + 1); /* stream number */
397
        put_le32(pb, 0); /* ??? */
398

    
399
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
400
            /* WAVEFORMATEX header */
401
            int wavsize = ff_put_wav_header(pb, enc);
402
            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)) {
403
                wavsize += 2;
404
                put_le16(pb, 0);
405
            }
406

    
407
            if (wavsize < 0)
408
                return -1;
409
            if (wavsize != extra_size) {
410
                cur_pos = url_ftell(pb);
411
                url_fseek(pb, es_pos, SEEK_SET);
412
                put_le32(pb, wavsize); /* wav header len */
413
                url_fseek(pb, cur_pos, SEEK_SET);
414
            }
415
            /* ERROR Correction */
416
            put_byte(pb, 0x01);
417
            if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
418
                put_le16(pb, 0x0190);
419
                put_le16(pb, 0x0190);
420
            }else{
421
                put_le16(pb, enc->block_align);
422
                put_le16(pb, enc->block_align);
423
            }
424
            put_le16(pb, 0x01);
425
            put_byte(pb, 0x00);
426
        } else {
427
            put_le32(pb, enc->width);
428
            put_le32(pb, enc->height);
429
            put_byte(pb, 2); /* ??? */
430
            put_le16(pb, 40 + enc->extradata_size); /* size */
431

    
432
            /* BITMAPINFOHEADER header */
433
            ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
434
        }
435
        end_header(pb, hpos);
436
    }
437

    
438
    /* media comments */
439

    
440
    hpos = put_header(pb, &ff_asf_codec_comment_header);
441
    put_guid(pb, &ff_asf_codec_comment1_header);
442
    put_le32(pb, s->nb_streams);
443
    for(n=0;n<s->nb_streams;n++) {
444
        AVCodec *p;
445
        const char *desc;
446

    
447
        enc = s->streams[n]->codec;
448
        p = avcodec_find_encoder(enc->codec_id);
449

    
450
        if(enc->codec_type == CODEC_TYPE_AUDIO)
451
            put_le16(pb, 2);
452
        else if(enc->codec_type == CODEC_TYPE_VIDEO)
453
            put_le16(pb, 1);
454
        else
455
            put_le16(pb, -1);
456

    
457
        if(enc->codec_id == CODEC_ID_WMAV2)
458
            desc = "Windows Media Audio V8";
459
        else
460
            desc = p ? p->name : enc->codec_name;
461
        put_le16(pb, strlen(desc) + 1); // "number of characters" = length in bytes / 2
462
        put_str16_nolen(pb, desc);
463
        put_le16(pb, 0); /* no parameters */
464

    
465

    
466
        /* id */
467
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
468
            put_le16(pb, 2);
469
            put_le16(pb, enc->codec_tag);
470
        } else {
471
            put_le16(pb, 4);
472
            put_le32(pb, enc->codec_tag);
473
        }
474
        if(!enc->codec_tag)
475
            return -1;
476
    }
477
    end_header(pb, hpos);
478

    
479
    /* patch the header size fields */
480

    
481
    cur_pos = url_ftell(pb);
482
    header_size = cur_pos - header_offset;
483
    if (asf->is_streamed) {
484
        header_size += 8 + 30 + 50;
485

    
486
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
487
        put_le16(pb, header_size);
488
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
489
        put_le16(pb, header_size);
490

    
491
        header_size -= 8 + 30 + 50;
492
    }
493
    header_size += 24 + 6;
494
    url_fseek(pb, header_offset - 14, SEEK_SET);
495
    put_le64(pb, header_size);
496
    url_fseek(pb, cur_pos, SEEK_SET);
497

    
498
    /* movie chunk, followed by packets of packet_size */
499
    asf->data_offset = cur_pos;
500
    put_guid(pb, &ff_asf_data_header);
501
    put_le64(pb, data_chunk_size);
502
    put_guid(pb, &ff_asf_my_guid);
503
    put_le64(pb, asf->nb_packets); /* nb packets */
504
    put_byte(pb, 1); /* ??? */
505
    put_byte(pb, 1); /* ??? */
506
    return 0;
507
}
508

    
509
static int asf_write_header(AVFormatContext *s)
510
{
511
    ASFContext *asf = s->priv_data;
512

    
513
    s->packet_size  = PACKET_SIZE;
514
    asf->nb_packets = 0;
515

    
516
    asf->last_indexed_pts = 0;
517
    asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
518
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
519
    asf->nb_index_count = 0;
520
    asf->maximum_packet = 0;
521

    
522
    /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
523
     *  at the moment this function is done. It is needed to use asf as
524
     *  streamable format. */
525
    if (asf_write_header1(s, 0, 50) < 0) {
526
        //av_free(asf);
527
        return -1;
528
    }
529

    
530
    put_flush_packet(s->pb);
531

    
532
    asf->packet_nb_payloads = 0;
533
    asf->packet_timestamp_start = -1;
534
    asf->packet_timestamp_end = -1;
535
    init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
536
                  NULL, NULL, NULL, NULL);
537

    
538
    return 0;
539
}
540

    
541
static int asf_write_stream_header(AVFormatContext *s)
542
{
543
    ASFContext *asf = s->priv_data;
544

    
545
    asf->is_streamed = 1;
546

    
547
    return asf_write_header(s);
548
}
549

    
550
static int put_payload_parsing_info(
551
                                AVFormatContext *s,
552
                                unsigned int    sendtime,
553
                                unsigned int    duration,
554
                                int             nb_payloads,
555
                                int             padsize
556
            )
557
{
558
    ASFContext *asf = s->priv_data;
559
    ByteIOContext *pb = s->pb;
560
    int ppi_size, i;
561
    int64_t start= url_ftell(pb);
562

    
563
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
564

    
565
    padsize -= PACKET_HEADER_MIN_SIZE;
566
    if(asf->multi_payloads_present)
567
        padsize--;
568
    assert(padsize>=0);
569

    
570
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
571
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
572
        put_byte(pb, 0x0);
573
    }
574

    
575
    if (asf->multi_payloads_present)
576
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
577

    
578
    if (padsize > 0) {
579
        if (padsize < 256)
580
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
581
        else
582
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
583
    }
584
    put_byte(pb, iLengthTypeFlags);
585

    
586
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
587

    
588
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
589
        put_le16(pb, padsize - 2);
590
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
591
        put_byte(pb, padsize - 1);
592

    
593
    put_le32(pb, sendtime);
594
    put_le16(pb, duration);
595
    if (asf->multi_payloads_present)
596
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
597

    
598
    ppi_size = url_ftell(pb) - start;
599

    
600
    return ppi_size;
601
}
602

    
603
static void flush_packet(AVFormatContext *s)
604
{
605
    ASFContext *asf = s->priv_data;
606
    int packet_hdr_size, packet_filled_size;
607

    
608
    assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
609

    
610
    if (asf->is_streamed) {
611
        put_chunk(s, 0x4424, s->packet_size, 0);
612
    }
613

    
614
    packet_hdr_size = put_payload_parsing_info(
615
                            s,
616
                            asf->packet_timestamp_start,
617
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
618
                            asf->packet_nb_payloads,
619
                            asf->packet_size_left
620
                        );
621

    
622
    packet_filled_size = PACKET_SIZE - asf->packet_size_left;
623
    assert(packet_hdr_size <= asf->packet_size_left);
624
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
625

    
626
    put_buffer(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
627

    
628
    put_flush_packet(s->pb);
629
    asf->nb_packets++;
630
    asf->packet_nb_payloads = 0;
631
    asf->packet_timestamp_start = -1;
632
    asf->packet_timestamp_end = -1;
633
    init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
634
                  NULL, NULL, NULL, NULL);
635
}
636

    
637
static void put_payload_header(
638
                                AVFormatContext *s,
639
                                ASFStream       *stream,
640
                                int             presentation_time,
641
                                int             m_obj_size,
642
                                int             m_obj_offset,
643
                                int             payload_len,
644
                                int             flags
645
            )
646
{
647
    ASFContext *asf = s->priv_data;
648
    ByteIOContext *pb = &asf->pb;
649
    int val;
650

    
651
    val = stream->num;
652
    if (flags & PKT_FLAG_KEY)
653
        val |= ASF_PL_FLAG_KEY_FRAME;
654
    put_byte(pb, val);
655

    
656
    put_byte(pb, stream->seq);  //Media object number
657
    put_le32(pb, m_obj_offset); //Offset Into Media Object
658

    
659
    // Replicated Data shall be at least 8 bytes long.
660
    // The first 4 bytes of data shall contain the
661
    // Size of the Media Object that the payload belongs to.
662
    // The next 4 bytes of data shall contain the
663
    // Presentation Time for the media object that the payload belongs to.
664
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
665

    
666
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
667
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
668

    
669
    if (asf->multi_payloads_present){
670
        put_le16(pb, payload_len);   //payload length
671
    }
672
}
673

    
674
static void put_frame(
675
                    AVFormatContext *s,
676
                    ASFStream       *stream,
677
                    AVStream        *avst,
678
                    int             timestamp,
679
                    const uint8_t   *buf,
680
                    int             m_obj_size,
681
                    int             flags
682
                )
683
{
684
    ASFContext *asf = s->priv_data;
685
    int m_obj_offset, payload_len, frag_len1;
686

    
687
    m_obj_offset = 0;
688
    while (m_obj_offset < m_obj_size) {
689
        payload_len = m_obj_size - m_obj_offset;
690
        if (asf->packet_timestamp_start == -1) {
691
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
692

    
693
            asf->packet_size_left = PACKET_SIZE;
694
            if (asf->multi_payloads_present){
695
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
696
            }
697
            else {
698
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
699
            }
700
            asf->packet_timestamp_start = timestamp;
701
        }
702
        else {
703
            // multi payloads
704
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
705

    
706
            if(frag_len1 < payload_len && avst->codec->codec_type == CODEC_TYPE_AUDIO){
707
                flush_packet(s);
708
                continue;
709
            }
710
        }
711
        if (frag_len1 > 0) {
712
            if (payload_len > frag_len1)
713
                payload_len = frag_len1;
714
            else if (payload_len == (frag_len1 - 1))
715
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
716

    
717
            put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
718
            put_buffer(&asf->pb, buf, payload_len);
719

    
720
            if (asf->multi_payloads_present)
721
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
722
            else
723
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
724
            asf->packet_timestamp_end = timestamp;
725

    
726
            asf->packet_nb_payloads++;
727
        } else {
728
            payload_len = 0;
729
        }
730
        m_obj_offset += payload_len;
731
        buf += payload_len;
732

    
733
        if (!asf->multi_payloads_present)
734
            flush_packet(s);
735
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
736
            flush_packet(s);
737
    }
738
    stream->seq++;
739
}
740

    
741
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
742
{
743
    ASFContext *asf = s->priv_data;
744
    ASFStream *stream;
745
    int64_t duration;
746
    AVCodecContext *codec;
747
    int64_t packet_st,pts;
748
    int start_sec,i;
749
    int flags= pkt->flags;
750

    
751
    codec = s->streams[pkt->stream_index]->codec;
752
    stream = &asf->streams[pkt->stream_index];
753

    
754
    if(codec->codec_type == CODEC_TYPE_AUDIO)
755
        flags &= ~PKT_FLAG_KEY;
756

    
757
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
758
    assert(pts != AV_NOPTS_VALUE);
759
    duration = pts * 10000;
760
    asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
761

    
762
    packet_st = asf->nb_packets;
763
    put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
764

    
765
    /* check index */
766
    if ((!asf->is_streamed) && (flags & PKT_FLAG_KEY)) {
767
        start_sec = (int)(duration / INT64_C(10000000));
768
        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
769
            for(i=asf->nb_index_count;i<start_sec;i++) {
770
                if (i>=asf->nb_index_memory_alloc) {
771
                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
772
                    asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
773
                }
774
                // store
775
                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
776
                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets-packet_st);
777
                asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
778
            }
779
            asf->nb_index_count = start_sec;
780
            asf->last_indexed_pts = duration;
781
        }
782
    }
783
    return 0;
784
}
785

    
786
//
787
static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
788
{
789
    ByteIOContext *pb = s->pb;
790
    int i;
791

    
792
    put_guid(pb, &ff_asf_simple_index_header);
793
    put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
794
    put_guid(pb, &ff_asf_my_guid);
795
    put_le64(pb, ASF_INDEXED_INTERVAL);
796
    put_le32(pb, max);
797
    put_le32(pb, count);
798
    for(i=0; i<count; i++) {
799
        put_le32(pb, index[i].packet_number);
800
        put_le16(pb, index[i].packet_count);
801
    }
802

    
803
    return 0;
804
}
805

    
806
static int asf_write_trailer(AVFormatContext *s)
807
{
808
    ASFContext *asf = s->priv_data;
809
    int64_t file_size,data_size;
810

    
811
    /* flush the current packet */
812
    if (asf->pb.buf_ptr > asf->pb.buffer)
813
        flush_packet(s);
814

    
815
    /* write index */
816
    data_size = url_ftell(s->pb);
817
    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
818
        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
819
    }
820
    put_flush_packet(s->pb);
821

    
822
    if (asf->is_streamed || url_is_streamed(s->pb)) {
823
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
824
    } else {
825
        /* rewrite an updated header */
826
        file_size = url_ftell(s->pb);
827
        url_fseek(s->pb, 0, SEEK_SET);
828
        asf_write_header1(s, file_size, data_size - asf->data_offset);
829
    }
830

    
831
    put_flush_packet(s->pb);
832
    av_free(asf->index_ptr);
833
    return 0;
834
}
835

    
836
#if CONFIG_ASF_MUXER
837
AVOutputFormat asf_muxer = {
838
    "asf",
839
    NULL_IF_CONFIG_SMALL("ASF format"),
840
    "video/x-ms-asf",
841
    "asf,wmv,wma",
842
    sizeof(ASFContext),
843
#if CONFIG_LIBMP3LAME
844
    CODEC_ID_MP3,
845
#else
846
    CODEC_ID_MP2,
847
#endif
848
    CODEC_ID_MSMPEG4V3,
849
    asf_write_header,
850
    asf_write_packet,
851
    asf_write_trailer,
852
    .flags = AVFMT_GLOBALHEADER,
853
    .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
854
    .metadata_conv = ff_asf_metadata_conv,
855
};
856
#endif
857

    
858
#if CONFIG_ASF_STREAM_MUXER
859
AVOutputFormat asf_stream_muxer = {
860
    "asf_stream",
861
    NULL_IF_CONFIG_SMALL("ASF format"),
862
    "video/x-ms-asf",
863
    "asf,wmv,wma",
864
    sizeof(ASFContext),
865
#if CONFIG_LIBMP3LAME
866
    CODEC_ID_MP3,
867
#else
868
    CODEC_ID_MP2,
869
#endif
870
    CODEC_ID_MSMPEG4V3,
871
    asf_write_stream_header,
872
    asf_write_packet,
873
    asf_write_trailer,
874
    .flags = AVFMT_GLOBALHEADER,
875
    .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
876
    .metadata_conv = ff_asf_metadata_conv,
877
};
878
#endif //CONFIG_ASF_STREAM_MUXER