Statistics
| Branch: | Revision:

ffmpeg / libavformat / asfenc.c @ 83120e3b

History | View | Annotate | Download (30.3 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
typedef struct {
192
    uint32_t seqno;
193
    int is_streamed;
194
    ASFStream streams[128];              ///< it's max number and it's not that big
195
    /* non streamed additonnal info */
196
    uint64_t nb_packets;                 ///< how many packets are there in the file, invalid if broadcasting
197
    int64_t duration;                    ///< in 100ns units
198
    /* packet filling */
199
    unsigned char multi_payloads_present;
200
    int packet_size_left;
201
    int packet_timestamp_start;
202
    int packet_timestamp_end;
203
    unsigned int packet_nb_payloads;
204
    uint8_t packet_buf[PACKET_SIZE];
205
    ByteIOContext pb;
206
    /* only for reading */
207
    uint64_t data_offset;                ///< beginning of the first data packet
208

    
209
    int64_t last_indexed_pts;
210
    ASFIndex* index_ptr;
211
    uint32_t nb_index_count;
212
    uint32_t nb_index_memory_alloc;
213
    uint16_t maximum_packet;
214
} ASFContext;
215

    
216
static const AVCodecTag codec_asf_bmp_tags[] = {
217
    { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
218
    { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
219
    { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
220
    { CODEC_ID_NONE, 0 },
221
};
222

    
223
#define PREROLL_TIME 3100
224

    
225
static void put_guid(ByteIOContext *s, const ff_asf_guid *g)
226
{
227
    assert(sizeof(*g) == 16);
228
    put_buffer(s, *g, sizeof(*g));
229
}
230

    
231
static void put_str16(ByteIOContext *s, const char *tag)
232
{
233
    int len;
234
    uint8_t *pb;
235
    ByteIOContext *dyn_buf;
236
    if (url_open_dyn_buf(&dyn_buf) < 0)
237
        return;
238

    
239
    avio_put_str16le(dyn_buf, tag);
240
    len = url_close_dyn_buf(dyn_buf, &pb);
241
    put_le16(s, len);
242
    put_buffer(s, pb, len);
243
    av_freep(&pb);
244
}
245

    
246
static int64_t put_header(ByteIOContext *pb, const ff_asf_guid *g)
247
{
248
    int64_t pos;
249

    
250
    pos = url_ftell(pb);
251
    put_guid(pb, g);
252
    put_le64(pb, 24);
253
    return pos;
254
}
255

    
256
/* update header size */
257
static void end_header(ByteIOContext *pb, int64_t pos)
258
{
259
    int64_t pos1;
260

    
261
    pos1 = url_ftell(pb);
262
    url_fseek(pb, pos + 16, SEEK_SET);
263
    put_le64(pb, pos1 - pos);
264
    url_fseek(pb, pos1, SEEK_SET);
265
}
266

    
267
/* write an asf chunk (only used in streaming case) */
268
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
269
{
270
    ASFContext *asf = s->priv_data;
271
    ByteIOContext *pb = s->pb;
272
    int length;
273

    
274
    length = payload_length + 8;
275
    put_le16(pb, type);
276
    put_le16(pb, length);    //size
277
    put_le32(pb, asf->seqno);//sequence number
278
    put_le16(pb, flags); /* unknown bytes */
279
    put_le16(pb, length);    //size_confirm
280
    asf->seqno++;
281
}
282

    
283
/* convert from unix to windows time */
284
static int64_t unix_to_file_time(int ti)
285
{
286
    int64_t t;
287

    
288
    t = ti * INT64_C(10000000);
289
    t += INT64_C(116444736000000000);
290
    return t;
291
}
292

    
293
/* write the header (used two times if non streamed) */
294
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
295
{
296
    ASFContext *asf = s->priv_data;
297
    ByteIOContext *pb = s->pb;
298
    AVMetadataTag *tags[5];
299
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
300
    int has_title;
301
    int metadata_count;
302
    AVCodecContext *enc;
303
    int64_t header_offset, cur_pos, hpos;
304
    int bit_rate;
305
    int64_t duration;
306

    
307
    ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
308

    
309
    tags[0] = av_metadata_get(s->metadata, "title"    , NULL, 0);
310
    tags[1] = av_metadata_get(s->metadata, "author"   , NULL, 0);
311
    tags[2] = av_metadata_get(s->metadata, "copyright", NULL, 0);
312
    tags[3] = av_metadata_get(s->metadata, "comment"  , NULL, 0);
313
    tags[4] = av_metadata_get(s->metadata, "rating"   , NULL, 0);
314

    
315
    duration = asf->duration + PREROLL_TIME * 10000;
316
    has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
317
    metadata_count = s->metadata ? s->metadata->count : 0;
318

    
319
    bit_rate = 0;
320
    for(n=0;n<s->nb_streams;n++) {
321
        enc = s->streams[n]->codec;
322

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

    
325
        bit_rate += enc->bit_rate;
326
    }
327

    
328
    if (asf->is_streamed) {
329
        put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
330
    }
331

    
332
    put_guid(pb, &ff_asf_header);
333
    put_le64(pb, -1); /* header length, will be patched after */
334
    put_le32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
335
    put_byte(pb, 1); /* ??? */
336
    put_byte(pb, 2); /* ??? */
337

    
338
    /* file header */
339
    header_offset = url_ftell(pb);
340
    hpos = put_header(pb, &ff_asf_file_header);
341
    put_guid(pb, &ff_asf_my_guid);
342
    put_le64(pb, file_size);
343
    file_time = 0;
344
    put_le64(pb, unix_to_file_time(file_time));
345
    put_le64(pb, asf->nb_packets); /* number of packets */
346
    put_le64(pb, duration); /* end time stamp (in 100ns units) */
347
    put_le64(pb, asf->duration); /* duration (in 100ns units) */
348
    put_le64(pb, PREROLL_TIME); /* start time stamp */
349
    put_le32(pb, (asf->is_streamed || url_is_streamed(pb)) ? 3 : 2); /* ??? */
350
    put_le32(pb, s->packet_size); /* packet size */
351
    put_le32(pb, s->packet_size); /* packet size */
352
    put_le32(pb, bit_rate); /* Nominal data rate in bps */
353
    end_header(pb, hpos);
354

    
355
    /* unknown headers */
356
    hpos = put_header(pb, &ff_asf_head1_guid);
357
    put_guid(pb, &ff_asf_head2_guid);
358
    put_le32(pb, 6);
359
    put_le16(pb, 0);
360
    end_header(pb, hpos);
361

    
362
    /* title and other infos */
363
    if (has_title) {
364
        int len;
365
        uint8_t *buf;
366
        ByteIOContext *dyn_buf;
367

    
368
        if (url_open_dyn_buf(&dyn_buf) < 0)
369
            return AVERROR(ENOMEM);
370

    
371
        hpos = put_header(pb, &ff_asf_comment_header);
372

    
373
        for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
374
            len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
375
            put_le16(pb, len);
376
        }
377
        len = url_close_dyn_buf(dyn_buf, &buf);
378
        put_buffer(pb, buf, len);
379
        av_freep(&buf);
380
        end_header(pb, hpos);
381
    }
382
    if (metadata_count) {
383
        AVMetadataTag *tag = NULL;
384
        hpos = put_header(pb, &ff_asf_extended_content_header);
385
        put_le16(pb, metadata_count);
386
        while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
387
            put_str16(pb, tag->key);
388
            put_le16(pb, 0);
389
            put_str16(pb, tag->value);
390
        }
391
        end_header(pb, hpos);
392
    }
393

    
394
    /* stream headers */
395
    for(n=0;n<s->nb_streams;n++) {
396
        int64_t es_pos;
397
        //        ASFStream *stream = &asf->streams[n];
398

    
399
        enc = s->streams[n]->codec;
400
        asf->streams[n].num = n + 1;
401
        asf->streams[n].seq = 0;
402

    
403

    
404
        switch(enc->codec_type) {
405
        case AVMEDIA_TYPE_AUDIO:
406
            wav_extra_size = 0;
407
            extra_size = 18 + wav_extra_size;
408
            extra_size2 = 8;
409
            break;
410
        default:
411
        case AVMEDIA_TYPE_VIDEO:
412
            wav_extra_size = enc->extradata_size;
413
            extra_size = 0x33 + wav_extra_size;
414
            extra_size2 = 0;
415
            break;
416
        }
417

    
418
        hpos = put_header(pb, &ff_asf_stream_header);
419
        if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
420
            put_guid(pb, &ff_asf_audio_stream);
421
            put_guid(pb, &ff_asf_audio_conceal_spread);
422
        } else {
423
            put_guid(pb, &ff_asf_video_stream);
424
            put_guid(pb, &ff_asf_video_conceal_none);
425
        }
426
        put_le64(pb, 0); /* ??? */
427
        es_pos = url_ftell(pb);
428
        put_le32(pb, extra_size); /* wav header len */
429
        put_le32(pb, extra_size2); /* additional data len */
430
        put_le16(pb, n + 1); /* stream number */
431
        put_le32(pb, 0); /* ??? */
432

    
433
        if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
434
            /* WAVEFORMATEX header */
435
            int wavsize = ff_put_wav_header(pb, enc);
436
            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)) {
437
                wavsize += 2;
438
                put_le16(pb, 0);
439
            }
440

    
441
            if (wavsize < 0)
442
                return -1;
443
            if (wavsize != extra_size) {
444
                cur_pos = url_ftell(pb);
445
                url_fseek(pb, es_pos, SEEK_SET);
446
                put_le32(pb, wavsize); /* wav header len */
447
                url_fseek(pb, cur_pos, SEEK_SET);
448
            }
449
            /* ERROR Correction */
450
            put_byte(pb, 0x01);
451
            if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
452
                put_le16(pb, 0x0190);
453
                put_le16(pb, 0x0190);
454
            }else{
455
                put_le16(pb, enc->block_align);
456
                put_le16(pb, enc->block_align);
457
            }
458
            put_le16(pb, 0x01);
459
            put_byte(pb, 0x00);
460
        } else {
461
            put_le32(pb, enc->width);
462
            put_le32(pb, enc->height);
463
            put_byte(pb, 2); /* ??? */
464
            put_le16(pb, 40 + enc->extradata_size); /* size */
465

    
466
            /* BITMAPINFOHEADER header */
467
            ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
468
        }
469
        end_header(pb, hpos);
470
    }
471

    
472
    /* media comments */
473

    
474
    hpos = put_header(pb, &ff_asf_codec_comment_header);
475
    put_guid(pb, &ff_asf_codec_comment1_header);
476
    put_le32(pb, s->nb_streams);
477
    for(n=0;n<s->nb_streams;n++) {
478
        AVCodec *p;
479
        const char *desc;
480
        int len;
481
        uint8_t *buf;
482
        ByteIOContext *dyn_buf;
483

    
484
        enc = s->streams[n]->codec;
485
        p = avcodec_find_encoder(enc->codec_id);
486

    
487
        if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
488
            put_le16(pb, 2);
489
        else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
490
            put_le16(pb, 1);
491
        else
492
            put_le16(pb, -1);
493

    
494
        if(enc->codec_id == CODEC_ID_WMAV2)
495
            desc = "Windows Media Audio V8";
496
        else
497
            desc = p ? p->name : enc->codec_name;
498

    
499
        if ( url_open_dyn_buf(&dyn_buf) < 0)
500
            return AVERROR(ENOMEM);
501

    
502
        avio_put_str16le(dyn_buf, desc);
503
        len = url_close_dyn_buf(dyn_buf, &buf);
504
        put_le16(pb, len / 2); // "number of characters" = length in bytes / 2
505

    
506
        put_buffer(pb, buf, len);
507
        av_freep(&buf);
508

    
509
        put_le16(pb, 0); /* no parameters */
510

    
511

    
512
        /* id */
513
        if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
514
            put_le16(pb, 2);
515
            put_le16(pb, enc->codec_tag);
516
        } else {
517
            put_le16(pb, 4);
518
            put_le32(pb, enc->codec_tag);
519
        }
520
        if(!enc->codec_tag)
521
            return -1;
522
    }
523
    end_header(pb, hpos);
524

    
525
    /* patch the header size fields */
526

    
527
    cur_pos = url_ftell(pb);
528
    header_size = cur_pos - header_offset;
529
    if (asf->is_streamed) {
530
        header_size += 8 + 30 + 50;
531

    
532
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
533
        put_le16(pb, header_size);
534
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
535
        put_le16(pb, header_size);
536

    
537
        header_size -= 8 + 30 + 50;
538
    }
539
    header_size += 24 + 6;
540
    url_fseek(pb, header_offset - 14, SEEK_SET);
541
    put_le64(pb, header_size);
542
    url_fseek(pb, cur_pos, SEEK_SET);
543

    
544
    /* movie chunk, followed by packets of packet_size */
545
    asf->data_offset = cur_pos;
546
    put_guid(pb, &ff_asf_data_header);
547
    put_le64(pb, data_chunk_size);
548
    put_guid(pb, &ff_asf_my_guid);
549
    put_le64(pb, asf->nb_packets); /* nb packets */
550
    put_byte(pb, 1); /* ??? */
551
    put_byte(pb, 1); /* ??? */
552
    return 0;
553
}
554

    
555
static int asf_write_header(AVFormatContext *s)
556
{
557
    ASFContext *asf = s->priv_data;
558

    
559
    s->packet_size  = PACKET_SIZE;
560
    asf->nb_packets = 0;
561

    
562
    asf->last_indexed_pts = 0;
563
    asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
564
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
565
    asf->nb_index_count = 0;
566
    asf->maximum_packet = 0;
567

    
568
    /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
569
     *  at the moment this function is done. It is needed to use asf as
570
     *  streamable format. */
571
    if (asf_write_header1(s, 0, 50) < 0) {
572
        //av_free(asf);
573
        return -1;
574
    }
575

    
576
    put_flush_packet(s->pb);
577

    
578
    asf->packet_nb_payloads = 0;
579
    asf->packet_timestamp_start = -1;
580
    asf->packet_timestamp_end = -1;
581
    init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
582
                  NULL, NULL, NULL, NULL);
583

    
584
    return 0;
585
}
586

    
587
static int asf_write_stream_header(AVFormatContext *s)
588
{
589
    ASFContext *asf = s->priv_data;
590

    
591
    asf->is_streamed = 1;
592

    
593
    return asf_write_header(s);
594
}
595

    
596
static int put_payload_parsing_info(
597
                                AVFormatContext *s,
598
                                unsigned int    sendtime,
599
                                unsigned int    duration,
600
                                int             nb_payloads,
601
                                int             padsize
602
            )
603
{
604
    ASFContext *asf = s->priv_data;
605
    ByteIOContext *pb = s->pb;
606
    int ppi_size, i;
607
    int64_t start= url_ftell(pb);
608

    
609
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
610

    
611
    padsize -= PACKET_HEADER_MIN_SIZE;
612
    if(asf->multi_payloads_present)
613
        padsize--;
614
    assert(padsize>=0);
615

    
616
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
617
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
618
        put_byte(pb, 0x0);
619
    }
620

    
621
    if (asf->multi_payloads_present)
622
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
623

    
624
    if (padsize > 0) {
625
        if (padsize < 256)
626
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
627
        else
628
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
629
    }
630
    put_byte(pb, iLengthTypeFlags);
631

    
632
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
633

    
634
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
635
        put_le16(pb, padsize - 2);
636
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
637
        put_byte(pb, padsize - 1);
638

    
639
    put_le32(pb, sendtime);
640
    put_le16(pb, duration);
641
    if (asf->multi_payloads_present)
642
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
643

    
644
    ppi_size = url_ftell(pb) - start;
645

    
646
    return ppi_size;
647
}
648

    
649
static void flush_packet(AVFormatContext *s)
650
{
651
    ASFContext *asf = s->priv_data;
652
    int packet_hdr_size, packet_filled_size;
653

    
654
    assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
655

    
656
    if (asf->is_streamed) {
657
        put_chunk(s, 0x4424, s->packet_size, 0);
658
    }
659

    
660
    packet_hdr_size = put_payload_parsing_info(
661
                            s,
662
                            asf->packet_timestamp_start,
663
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
664
                            asf->packet_nb_payloads,
665
                            asf->packet_size_left
666
                        );
667

    
668
    packet_filled_size = PACKET_SIZE - asf->packet_size_left;
669
    assert(packet_hdr_size <= asf->packet_size_left);
670
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
671

    
672
    put_buffer(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
673

    
674
    put_flush_packet(s->pb);
675
    asf->nb_packets++;
676
    asf->packet_nb_payloads = 0;
677
    asf->packet_timestamp_start = -1;
678
    asf->packet_timestamp_end = -1;
679
    init_put_byte(&asf->pb, asf->packet_buf, s->packet_size, 1,
680
                  NULL, NULL, NULL, NULL);
681
}
682

    
683
static void put_payload_header(
684
                                AVFormatContext *s,
685
                                ASFStream       *stream,
686
                                int             presentation_time,
687
                                int             m_obj_size,
688
                                int             m_obj_offset,
689
                                int             payload_len,
690
                                int             flags
691
            )
692
{
693
    ASFContext *asf = s->priv_data;
694
    ByteIOContext *pb = &asf->pb;
695
    int val;
696

    
697
    val = stream->num;
698
    if (flags & AV_PKT_FLAG_KEY)
699
        val |= ASF_PL_FLAG_KEY_FRAME;
700
    put_byte(pb, val);
701

    
702
    put_byte(pb, stream->seq);  //Media object number
703
    put_le32(pb, m_obj_offset); //Offset Into Media Object
704

    
705
    // Replicated Data shall be at least 8 bytes long.
706
    // The first 4 bytes of data shall contain the
707
    // Size of the Media Object that the payload belongs to.
708
    // The next 4 bytes of data shall contain the
709
    // Presentation Time for the media object that the payload belongs to.
710
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
711

    
712
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
713
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
714

    
715
    if (asf->multi_payloads_present){
716
        put_le16(pb, payload_len);   //payload length
717
    }
718
}
719

    
720
static void put_frame(
721
                    AVFormatContext *s,
722
                    ASFStream       *stream,
723
                    AVStream        *avst,
724
                    int             timestamp,
725
                    const uint8_t   *buf,
726
                    int             m_obj_size,
727
                    int             flags
728
                )
729
{
730
    ASFContext *asf = s->priv_data;
731
    int m_obj_offset, payload_len, frag_len1;
732

    
733
    m_obj_offset = 0;
734
    while (m_obj_offset < m_obj_size) {
735
        payload_len = m_obj_size - m_obj_offset;
736
        if (asf->packet_timestamp_start == -1) {
737
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
738

    
739
            asf->packet_size_left = PACKET_SIZE;
740
            if (asf->multi_payloads_present){
741
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
742
            }
743
            else {
744
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
745
            }
746
            asf->packet_timestamp_start = timestamp;
747
        }
748
        else {
749
            // multi payloads
750
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
751

    
752
            if(frag_len1 < payload_len && avst->codec->codec_type == AVMEDIA_TYPE_AUDIO){
753
                flush_packet(s);
754
                continue;
755
            }
756
        }
757
        if (frag_len1 > 0) {
758
            if (payload_len > frag_len1)
759
                payload_len = frag_len1;
760
            else if (payload_len == (frag_len1 - 1))
761
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
762

    
763
            put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
764
            put_buffer(&asf->pb, buf, payload_len);
765

    
766
            if (asf->multi_payloads_present)
767
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
768
            else
769
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
770
            asf->packet_timestamp_end = timestamp;
771

    
772
            asf->packet_nb_payloads++;
773
        } else {
774
            payload_len = 0;
775
        }
776
        m_obj_offset += payload_len;
777
        buf += payload_len;
778

    
779
        if (!asf->multi_payloads_present)
780
            flush_packet(s);
781
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
782
            flush_packet(s);
783
    }
784
    stream->seq++;
785
}
786

    
787
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
788
{
789
    ASFContext *asf = s->priv_data;
790
    ASFStream *stream;
791
    int64_t duration;
792
    AVCodecContext *codec;
793
    int64_t packet_st,pts;
794
    int start_sec,i;
795
    int flags= pkt->flags;
796

    
797
    codec = s->streams[pkt->stream_index]->codec;
798
    stream = &asf->streams[pkt->stream_index];
799

    
800
    if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
801
        flags &= ~AV_PKT_FLAG_KEY;
802

    
803
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
804
    assert(pts != AV_NOPTS_VALUE);
805
    duration = pts * 10000;
806
    asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
807

    
808
    packet_st = asf->nb_packets;
809
    put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
810

    
811
    /* check index */
812
    if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
813
        start_sec = (int)(duration / INT64_C(10000000));
814
        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
815
            for(i=asf->nb_index_count;i<start_sec;i++) {
816
                if (i>=asf->nb_index_memory_alloc) {
817
                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
818
                    asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
819
                }
820
                // store
821
                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
822
                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets-packet_st);
823
                asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
824
            }
825
            asf->nb_index_count = start_sec;
826
            asf->last_indexed_pts = duration;
827
        }
828
    }
829
    return 0;
830
}
831

    
832
//
833
static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
834
{
835
    ByteIOContext *pb = s->pb;
836
    int i;
837

    
838
    put_guid(pb, &ff_asf_simple_index_header);
839
    put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
840
    put_guid(pb, &ff_asf_my_guid);
841
    put_le64(pb, ASF_INDEXED_INTERVAL);
842
    put_le32(pb, max);
843
    put_le32(pb, count);
844
    for(i=0; i<count; i++) {
845
        put_le32(pb, index[i].packet_number);
846
        put_le16(pb, index[i].packet_count);
847
    }
848

    
849
    return 0;
850
}
851

    
852
static int asf_write_trailer(AVFormatContext *s)
853
{
854
    ASFContext *asf = s->priv_data;
855
    int64_t file_size,data_size;
856

    
857
    /* flush the current packet */
858
    if (asf->pb.buf_ptr > asf->pb.buffer)
859
        flush_packet(s);
860

    
861
    /* write index */
862
    data_size = url_ftell(s->pb);
863
    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
864
        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
865
    }
866
    put_flush_packet(s->pb);
867

    
868
    if (asf->is_streamed || url_is_streamed(s->pb)) {
869
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
870
    } else {
871
        /* rewrite an updated header */
872
        file_size = url_ftell(s->pb);
873
        url_fseek(s->pb, 0, SEEK_SET);
874
        asf_write_header1(s, file_size, data_size - asf->data_offset);
875
    }
876

    
877
    put_flush_packet(s->pb);
878
    av_free(asf->index_ptr);
879
    return 0;
880
}
881

    
882
#if CONFIG_ASF_MUXER
883
AVOutputFormat ff_asf_muxer = {
884
    "asf",
885
    NULL_IF_CONFIG_SMALL("ASF format"),
886
    "video/x-ms-asf",
887
    "asf,wmv,wma",
888
    sizeof(ASFContext),
889
#if CONFIG_LIBMP3LAME
890
    CODEC_ID_MP3,
891
#else
892
    CODEC_ID_MP2,
893
#endif
894
    CODEC_ID_MSMPEG4V3,
895
    asf_write_header,
896
    asf_write_packet,
897
    asf_write_trailer,
898
    .flags = AVFMT_GLOBALHEADER,
899
    .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
900
};
901
#endif
902

    
903
#if CONFIG_ASF_STREAM_MUXER
904
AVOutputFormat ff_asf_stream_muxer = {
905
    "asf_stream",
906
    NULL_IF_CONFIG_SMALL("ASF format"),
907
    "video/x-ms-asf",
908
    "asf,wmv,wma",
909
    sizeof(ASFContext),
910
#if CONFIG_LIBMP3LAME
911
    CODEC_ID_MP3,
912
#else
913
    CODEC_ID_MP2,
914
#endif
915
    CODEC_ID_MSMPEG4V3,
916
    asf_write_stream_header,
917
    asf_write_packet,
918
    asf_write_trailer,
919
    .flags = AVFMT_GLOBALHEADER,
920
    .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
921
};
922
#endif //CONFIG_ASF_STREAM_MUXER