Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 5c1844ab

History | View | Annotate | Download (28.7 KB)

1
/*
2
 * Adaptive stream format 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 "riff.h"
23
#include "asf.h"
24

    
25
#undef NDEBUG
26
#include <assert.h>
27

    
28

    
29
#define ASF_INDEXED_INTERVAL    10000000
30
#define ASF_INDEX_BLOCK         600
31

    
32
#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
33
#define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
34
                ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
35
                ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
36
                )
37

    
38
#if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
39
#   define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
40
#else
41
#   define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
42
#endif
43

    
44
#define ASF_PPI_PROPERTY_FLAGS (\
45
                ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
46
                ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
47
                ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
48
                ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
49
                )
50

    
51
#define ASF_PPI_LENGTH_TYPE_FLAGS 0
52

    
53
#define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
54

    
55
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
56
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 1
57
#endif
58
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
59
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 2
60
#endif
61
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
62
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 4
63
#endif
64
#ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
65
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 0
66
#endif
67

    
68

    
69
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
70
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
71
#endif
72
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
73
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
74
#endif
75
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
76
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
77
#endif
78
#ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
79
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
80
#endif
81

    
82
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
83
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
84
#endif
85
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
86
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
87
#endif
88
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
89
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
90
#endif
91
#ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
92
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
93
#endif
94

    
95
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
96
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
97
#endif
98
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
99
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
100
#endif
101
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
102
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
103
#endif
104
#ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
105
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
106
#endif
107

    
108
#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))
109
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
110
#endif
111
#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))
112
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
113
#endif
114
#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))
115
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
116
#endif
117
#ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
118
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
119
#endif
120

    
121
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
122
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
123
#endif
124
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
125
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
126
#endif
127
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
128
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
129
#endif
130
#ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
131
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
132
#endif
133

    
134
#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
135
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
136
#endif
137
#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
138
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
139
#endif
140
#ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
141
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
142
#endif
143

    
144
#define PACKET_HEADER_MIN_SIZE (\
145
                ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
146
                ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
147
                1 + /*Length Type Flags*/ \
148
                1 + /*Property Flags*/ \
149
                ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
150
                ASF_PPI_SEQUENCE_FIELD_SIZE + \
151
                ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
152
                4 + /*Send Time Field*/ \
153
                2   /*Duration Field*/ \
154
                )
155

    
156

    
157
// Replicated Data shall be at least 8 bytes long.
158
#define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
159

    
160
#define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
161
                1 + /*Stream Number*/ \
162
                ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
163
                ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
164
                ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
165
                ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
166
                )
167

    
168
#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
169
                1 + /*Stream Number*/ \
170
                ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
171
                ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
172
                ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
173
                ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
174
                ASF_PAYLOAD_LENGTH_FIELD_SIZE \
175
                )
176

    
177
#define SINGLE_PAYLOAD_DATA_LENGTH (\
178
                PACKET_SIZE - \
179
                PACKET_HEADER_MIN_SIZE - \
180
                PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
181
                )
182

    
183
#define MULTI_PAYLOAD_CONSTANT (\
184
                PACKET_SIZE - \
185
                PACKET_HEADER_MIN_SIZE - \
186
                1 - /*Payload Flags*/ \
187
                2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
188
                )
189

    
190
static const AVCodecTag codec_asf_bmp_tags[] = {
191
    { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
192
    { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
193
    { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
194
    { CODEC_ID_NONE, 0 },
195
};
196

    
197
static int preroll_time = 2000;
198

    
199
static const uint8_t error_spread_ADPCM_G726[] = { 0x01, 0x90, 0x01, 0x90, 0x01, 0x01, 0x00, 0x00 };
200

    
201
static void put_guid(ByteIOContext *s, const GUID *g)
202
{
203
    int i;
204

    
205
    put_le32(s, g->v1);
206
    put_le16(s, g->v2);
207
    put_le16(s, g->v3);
208
    for(i=0;i<8;i++)
209
        put_byte(s, g->v4[i]);
210
}
211

    
212
static void put_str16_nolen(ByteIOContext *s, const char *tag);
213
static void put_str16(ByteIOContext *s, const char *tag)
214
{
215
    put_le16(s,strlen(tag) + 1);
216
    put_str16_nolen(s, tag);
217
}
218

    
219
static void put_str16_nolen(ByteIOContext *s, const char *tag)
220
{
221
    int c;
222

    
223
    do{
224
        c = (uint8_t)*tag++;
225
        put_le16(s, c);
226
    }while(c);
227
}
228

    
229
static int64_t put_header(ByteIOContext *pb, const GUID *g)
230
{
231
    int64_t pos;
232

    
233
    pos = url_ftell(pb);
234
    put_guid(pb, g);
235
    put_le64(pb, 24);
236
    return pos;
237
}
238

    
239
/* update header size */
240
static void end_header(ByteIOContext *pb, int64_t pos)
241
{
242
    int64_t pos1;
243

    
244
    pos1 = url_ftell(pb);
245
    url_fseek(pb, pos + 16, SEEK_SET);
246
    put_le64(pb, pos1 - pos);
247
    url_fseek(pb, pos1, SEEK_SET);
248
}
249

    
250
/* write an asf chunk (only used in streaming case) */
251
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
252
{
253
    ASFContext *asf = s->priv_data;
254
    ByteIOContext *pb = &s->pb;
255
    int length;
256

    
257
    length = payload_length + 8;
258
    put_le16(pb, type);
259
    put_le16(pb, length);    //size
260
    put_le32(pb, asf->seqno);//sequence number
261
    put_le16(pb, flags); /* unknown bytes */
262
    put_le16(pb, length);    //size_confirm
263
    asf->seqno++;
264
}
265

    
266
/* convert from unix to windows time */
267
static int64_t unix_to_file_time(int ti)
268
{
269
    int64_t t;
270

    
271
    t = ti * INT64_C(10000000);
272
    t += INT64_C(116444736000000000);
273
    return t;
274
}
275

    
276
/* write the header (used two times if non streamed) */
277
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
278
{
279
    ASFContext *asf = s->priv_data;
280
    ByteIOContext *pb = &s->pb;
281
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
282
    int has_title;
283
    AVCodecContext *enc;
284
    int64_t header_offset, cur_pos, hpos;
285
    int bit_rate;
286
    int64_t duration;
287

    
288
    duration = asf->duration + preroll_time * 10000;
289
    has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
290

    
291
    bit_rate = 0;
292
    for(n=0;n<s->nb_streams;n++) {
293
        enc = s->streams[n]->codec;
294

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

    
297
        bit_rate += enc->bit_rate;
298
    }
299

    
300
    if (asf->is_streamed) {
301
        put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
302
    }
303

    
304
    put_guid(pb, &asf_header);
305
    put_le64(pb, -1); /* header length, will be patched after */
306
    put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
307
    put_byte(pb, 1); /* ??? */
308
    put_byte(pb, 2); /* ??? */
309

    
310
    /* file header */
311
    header_offset = url_ftell(pb);
312
    hpos = put_header(pb, &file_header);
313
    put_guid(pb, &my_guid);
314
    put_le64(pb, file_size);
315
    file_time = 0;
316
    put_le64(pb, unix_to_file_time(file_time));
317
    put_le64(pb, asf->nb_packets); /* number of packets */
318
    put_le64(pb, duration); /* end time stamp (in 100ns units) */
319
    put_le64(pb, duration); /* duration (in 100ns units) */
320
    put_le32(pb, preroll_time); /* start time stamp */
321
    put_le32(pb, 0); /* ??? */
322
    put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
323
    put_le32(pb, asf->packet_size); /* packet size */
324
    put_le32(pb, asf->packet_size); /* packet size */
325
    put_le32(pb, bit_rate); /* Nominal data rate in bps */
326
    end_header(pb, hpos);
327

    
328
    /* unknown headers */
329
    hpos = put_header(pb, &head1_guid);
330
    put_guid(pb, &head2_guid);
331
    put_le32(pb, 6);
332
    put_le16(pb, 0);
333
    end_header(pb, hpos);
334

    
335
    /* title and other infos */
336
    if (has_title) {
337
        hpos = put_header(pb, &comment_header);
338
        if ( s->title[0]     ) { put_le16(pb, 2 * (strlen(s->title    ) + 1)); } else { put_le16(pb, 0); }
339
        if ( s->author[0]    ) { put_le16(pb, 2 * (strlen(s->author   ) + 1)); } else { put_le16(pb, 0); }
340
        if ( s->copyright[0] ) { put_le16(pb, 2 * (strlen(s->copyright) + 1)); } else { put_le16(pb, 0); }
341
        if ( s->comment[0]   ) { put_le16(pb, 2 * (strlen(s->comment  ) + 1)); } else { put_le16(pb, 0); }
342
        put_le16(pb, 0);
343
        if ( s->title[0]     ) put_str16_nolen(pb, s->title);
344
        if ( s->author[0]    ) put_str16_nolen(pb, s->author);
345
        if ( s->copyright[0] ) put_str16_nolen(pb, s->copyright);
346
        if ( s->comment[0]   ) put_str16_nolen(pb, s->comment);
347
        end_header(pb, hpos);
348
    }
349

    
350
    /* stream headers */
351
    for(n=0;n<s->nb_streams;n++) {
352
        int64_t es_pos;
353
        const uint8_t *er_spr = NULL;
354
        int er_spr_len = 0;
355
        //        ASFStream *stream = &asf->streams[n];
356

    
357
        enc = s->streams[n]->codec;
358
        asf->streams[n].num = n + 1;
359
        asf->streams[n].seq = 0;
360

    
361

    
362
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
363
            if (enc->codec_id == CODEC_ID_ADPCM_G726) {
364
                er_spr     = error_spread_ADPCM_G726;
365
                er_spr_len = sizeof(error_spread_ADPCM_G726);
366
            }
367
        }
368

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

    
383
        hpos = put_header(pb, &stream_header);
384
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
385
            put_guid(pb, &audio_stream);
386
            if ((er_spr != NULL) && (er_spr_len != 0)) {
387
                put_guid(pb, &audio_conceal_spread);
388
            } else {
389
                put_guid(pb, &video_conceal_none);
390
            }
391
        } else {
392
            put_guid(pb, &video_stream);
393
            put_guid(pb, &video_conceal_none);
394
        }
395
        put_le64(pb, 0); /* ??? */
396
        es_pos = url_ftell(pb);
397
        put_le32(pb, extra_size); /* wav header len */
398
        put_le32(pb, extra_size2); /* additional data len */
399
        put_le16(pb, n + 1); /* stream number */
400
        put_le32(pb, 0); /* ??? */
401

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

    
410
            if (wavsize < 0)
411
                return -1;
412
            if (wavsize != extra_size) {
413
                cur_pos = url_ftell(pb);
414
                url_fseek(pb, es_pos, SEEK_SET);
415
                put_le32(pb, wavsize); /* wav header len */
416
                url_fseek(pb, cur_pos, SEEK_SET);
417
            }
418
            /* ERROR Correction */
419
            if ((er_spr != NULL) && (er_spr_len != 0))
420
                put_buffer(pb, er_spr, er_spr_len);
421
        } else {
422
            put_le32(pb, enc->width);
423
            put_le32(pb, enc->height);
424
            put_byte(pb, 2); /* ??? */
425
            put_le16(pb, 40 + enc->extradata_size); /* size */
426

    
427
            /* BITMAPINFOHEADER header */
428
            put_bmp_header(pb, enc, codec_bmp_tags, 1);
429
        }
430
        end_header(pb, hpos);
431
    }
432

    
433
    /* media comments */
434

    
435
    hpos = put_header(pb, &codec_comment_header);
436
    put_guid(pb, &codec_comment1_header);
437
    put_le32(pb, s->nb_streams);
438
    for(n=0;n<s->nb_streams;n++) {
439
        AVCodec *p;
440

    
441
        enc = s->streams[n]->codec;
442
        p = avcodec_find_encoder(enc->codec_id);
443

    
444
        put_le16(pb, asf->streams[n].num);
445
        put_str16(pb, p ? p->name : enc->codec_name);
446
        put_le16(pb, 0); /* no parameters */
447

    
448

    
449
        /* id */
450
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
451
            put_le16(pb, 2);
452
            put_le16(pb, enc->codec_tag);
453
        } else {
454
            put_le16(pb, 4);
455
            put_le32(pb, enc->codec_tag);
456
        }
457
        if(!enc->codec_tag)
458
            return -1;
459
    }
460
    end_header(pb, hpos);
461

    
462
    /* patch the header size fields */
463

    
464
    cur_pos = url_ftell(pb);
465
    header_size = cur_pos - header_offset;
466
    if (asf->is_streamed) {
467
        header_size += 8 + 30 + 50;
468

    
469
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
470
        put_le16(pb, header_size);
471
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
472
        put_le16(pb, header_size);
473

    
474
        header_size -= 8 + 30 + 50;
475
    }
476
    header_size += 24 + 6;
477
    url_fseek(pb, header_offset - 14, SEEK_SET);
478
    put_le64(pb, header_size);
479
    url_fseek(pb, cur_pos, SEEK_SET);
480

    
481
    /* movie chunk, followed by packets of packet_size */
482
    asf->data_offset = cur_pos;
483
    put_guid(pb, &data_header);
484
    put_le64(pb, data_chunk_size);
485
    put_guid(pb, &my_guid);
486
    put_le64(pb, asf->nb_packets); /* nb packets */
487
    put_byte(pb, 1); /* ??? */
488
    put_byte(pb, 1); /* ??? */
489
    return 0;
490
}
491

    
492
static int asf_write_header(AVFormatContext *s)
493
{
494
    ASFContext *asf = s->priv_data;
495

    
496
    asf->packet_size = PACKET_SIZE;
497
    asf->nb_packets = 0;
498

    
499
    asf->last_indexed_pts = 0;
500
    asf->index_ptr = (ASFIndex*)av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
501
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
502
    asf->nb_index_count = 0;
503
    asf->maximum_packet = 0;
504

    
505
    if (asf_write_header1(s, 0, 50) < 0) {
506
        //av_free(asf);
507
        return -1;
508
    }
509

    
510
    put_flush_packet(&s->pb);
511

    
512
    asf->packet_nb_payloads = 0;
513
    asf->prev_packet_sent_time = 0;
514
    asf->packet_timestamp_start = -1;
515
    asf->packet_timestamp_end = -1;
516
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
517
                  NULL, NULL, NULL, NULL);
518

    
519
    return 0;
520
}
521

    
522
static int asf_write_stream_header(AVFormatContext *s)
523
{
524
    ASFContext *asf = s->priv_data;
525

    
526
    asf->is_streamed = 1;
527

    
528
    return asf_write_header(s);
529
}
530

    
531
static int put_payload_parsing_info(
532
                                AVFormatContext *s,
533
                                unsigned int    sendtime,
534
                                unsigned int    duration,
535
                                int             nb_payloads,
536
                                int             padsize
537
            )
538
{
539
    ASFContext *asf = s->priv_data;
540
    ByteIOContext *pb = &s->pb;
541
    int ppi_size, i;
542
    unsigned char *start_ppi_ptr = pb->buf_ptr;
543

    
544
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
545

    
546
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
547
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
548
        put_byte(pb, 0x0);
549
    }
550

    
551
    if (asf->multi_payloads_present)
552
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
553

    
554
    if (padsize > 0) {
555
        if (padsize < 256)
556
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
557
        else
558
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
559
    }
560
    put_byte(pb, iLengthTypeFlags);
561

    
562
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
563

    
564
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
565
        put_le16(pb, padsize - 2);
566
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
567
        put_byte(pb, padsize - 1);
568

    
569
    put_le32(pb, sendtime);
570
    put_le16(pb, duration);
571
    if (asf->multi_payloads_present)
572
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
573

    
574
    ppi_size = pb->buf_ptr - start_ppi_ptr;
575

    
576
    return ppi_size;
577
}
578

    
579
static void flush_packet(AVFormatContext *s)
580
{
581
    ASFContext *asf = s->priv_data;
582
    int packet_hdr_size, packet_filled_size;
583

    
584
    if (asf->is_streamed) {
585
        put_chunk(s, 0x4424, asf->packet_size, 0);
586
    }
587

    
588
    packet_hdr_size = put_payload_parsing_info(
589
                            s,
590
                            asf->packet_timestamp_start,
591
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
592
                            asf->packet_nb_payloads,
593
                            asf->packet_size_left
594
                        );
595

    
596
    packet_filled_size = PACKET_SIZE - packet_hdr_size - asf->packet_size_left;
597
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
598

    
599
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
600

    
601
    put_flush_packet(&s->pb);
602
    asf->nb_packets++;
603
    asf->packet_nb_payloads = 0;
604
    asf->prev_packet_sent_time = asf->packet_timestamp_start;
605
    asf->packet_timestamp_start = -1;
606
    asf->packet_timestamp_end = -1;
607
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
608
                  NULL, NULL, NULL, NULL);
609
}
610

    
611
static void put_payload_header(
612
                                AVFormatContext *s,
613
                                ASFStream       *stream,
614
                                int             presentation_time,
615
                                int             m_obj_size,
616
                                int             m_obj_offset,
617
                                int             payload_len,
618
                                int             flags
619
            )
620
{
621
    ASFContext *asf = s->priv_data;
622
    ByteIOContext *pb = &asf->pb;
623
    int val;
624

    
625
    val = stream->num;
626
    if (flags & PKT_FLAG_KEY)
627
        val |= ASF_PL_FLAG_KEY_FRAME;
628
    put_byte(pb, val);
629

    
630
    put_byte(pb, stream->seq);  //Media object number
631
    put_le32(pb, m_obj_offset); //Offset Into Media Object
632

    
633
    // Replicated Data shall be at least 8 bytes long.
634
    // The first 4 bytes of data shall contain the
635
    // Size of the Media Object that the payload belongs to.
636
    // The next 4 bytes of data shall contain the
637
    // Presentation Time for the media object that the payload belongs to.
638
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
639

    
640
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
641
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
642

    
643
    if (asf->multi_payloads_present){
644
        put_le16(pb, payload_len);   //payload length
645
    }
646
}
647

    
648
static void put_frame(
649
                    AVFormatContext *s,
650
                    ASFStream       *stream,
651
                    int             timestamp,
652
                    const uint8_t   *buf,
653
                    int             m_obj_size,
654
                    int             flags
655
                )
656
{
657
    ASFContext *asf = s->priv_data;
658
    int m_obj_offset, payload_len, frag_len1;
659

    
660
    m_obj_offset = 0;
661
    while (m_obj_offset < m_obj_size) {
662
        payload_len = m_obj_size - m_obj_offset;
663
        if (asf->packet_timestamp_start == -1) {
664
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
665

    
666
            if (asf->multi_payloads_present){
667
                asf->packet_size_left = PACKET_SIZE; //For debug
668
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE - 1;
669
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
670
            }
671
            else {
672
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE;
673
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
674
            }
675
            if (asf->prev_packet_sent_time > timestamp)
676
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
677
            else
678
                asf->packet_timestamp_start = timestamp;
679
        }
680
        else {
681
            // multi payloads
682
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS;
683

    
684
            if (asf->prev_packet_sent_time > timestamp)
685
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
686
            else if (asf->packet_timestamp_start >= timestamp)
687
                asf->packet_timestamp_start = timestamp;
688
        }
689
        if (frag_len1 > 0) {
690
            if (payload_len > frag_len1)
691
                payload_len = frag_len1;
692
            else if (payload_len == (frag_len1 - 1))
693
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
694

    
695
            put_payload_header(s, stream, timestamp+preroll_time, m_obj_size, m_obj_offset, payload_len, flags);
696
            put_buffer(&asf->pb, buf, payload_len);
697

    
698
            if (asf->multi_payloads_present)
699
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
700
            else
701
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
702
            asf->packet_timestamp_end = timestamp;
703

    
704
            asf->packet_nb_payloads++;
705
        } else {
706
            payload_len = 0;
707
        }
708
        m_obj_offset += payload_len;
709
        buf += payload_len;
710

    
711
        if (!asf->multi_payloads_present)
712
            flush_packet(s);
713
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + 1))
714
            flush_packet(s);
715
    }
716
    stream->seq++;
717
}
718

    
719
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
720
{
721
    ASFContext *asf = s->priv_data;
722
    ASFStream *stream;
723
    int64_t duration;
724
    AVCodecContext *codec;
725
    int64_t packet_st,pts;
726
    int start_sec,i;
727

    
728
    codec = s->streams[pkt->stream_index]->codec;
729
    stream = &asf->streams[pkt->stream_index];
730

    
731
    //XXX /FIXME use duration from AVPacket (quick hack by)
732
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
733
    if (pts == AV_NOPTS_VALUE) {
734
        if (codec->codec_type == CODEC_TYPE_AUDIO) {
735
            duration = (codec->frame_number * (int64_t)codec->frame_size * INT64_C(10000000)) /
736
                codec->sample_rate;
737
        } else {
738
            duration = av_rescale(codec->frame_number * (int64_t)codec->time_base.num, 10000000, codec->time_base.den);
739
        }
740
    } else {
741
        duration = pts * 10000;
742
    }
743
    asf->duration= FFMAX(asf->duration, duration);
744

    
745
    packet_st = asf->nb_packets;
746
    put_frame(s, stream, pkt->pts, pkt->data, pkt->size, pkt->flags);
747

    
748
    /* check index */
749
    if ((!asf->is_streamed) && (codec->codec_type == CODEC_TYPE_VIDEO) && (pkt->flags & PKT_FLAG_KEY)) {
750
        start_sec = (int)(duration / INT64_C(10000000));
751
        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
752
            for(i=asf->nb_index_count;i<start_sec;i++) {
753
                if (i>=asf->nb_index_memory_alloc) {
754
                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
755
                    asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
756
                }
757
                // store
758
                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
759
                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets-packet_st);
760
                asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
761
            }
762
            asf->nb_index_count = start_sec;
763
            asf->last_indexed_pts = duration;
764
        }
765
    }
766
    return 0;
767
}
768

    
769
//
770
static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
771
{
772
    ByteIOContext *pb = &s->pb;
773
    int i;
774

    
775
    put_guid(pb, &simple_index_header);
776
    put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
777
    put_guid(pb, &my_guid);
778
    put_le64(pb, ASF_INDEXED_INTERVAL);
779
    put_le32(pb, max);
780
    put_le32(pb, count);
781
    for(i=0; i<count; i++) {
782
        put_le32(pb, index[i].packet_number);
783
        put_le16(pb, index[i].packet_count);
784
    }
785

    
786
    return 0;
787
}
788

    
789
static int asf_write_trailer(AVFormatContext *s)
790
{
791
    ASFContext *asf = s->priv_data;
792
    int64_t file_size,data_size;
793

    
794
    /* flush the current packet */
795
    if (asf->pb.buf_ptr > asf->pb.buffer)
796
        flush_packet(s);
797

    
798
    /* write index */
799
    data_size = url_ftell(&s->pb);
800
    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
801
        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
802
    }
803
    put_flush_packet(&s->pb);
804

    
805
    if (asf->is_streamed) {
806
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
807
    } else {
808
        /* rewrite an updated header */
809
        file_size = url_ftell(&s->pb);
810
        url_fseek(&s->pb, 0, SEEK_SET);
811
        asf_write_header1(s, file_size, data_size - asf->data_offset);
812
    }
813

    
814
    put_flush_packet(&s->pb);
815
    av_free(asf->index_ptr);
816
    return 0;
817
}
818

    
819
#ifdef CONFIG_ASF_MUXER
820
AVOutputFormat asf_muxer = {
821
    "asf",
822
    "asf format",
823
    "video/x-ms-asf",
824
    "asf,wmv,wma",
825
    sizeof(ASFContext),
826
#ifdef CONFIG_LIBMP3LAME
827
    CODEC_ID_MP3,
828
#else
829
    CODEC_ID_MP2,
830
#endif
831
    CODEC_ID_MSMPEG4V3,
832
    asf_write_header,
833
    asf_write_packet,
834
    asf_write_trailer,
835
    .flags = AVFMT_GLOBALHEADER,
836
    .codec_tag= (const AVCodecTag*[]){codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags, 0},
837
};
838
#endif
839

    
840
#ifdef CONFIG_ASF_STREAM_MUXER
841
AVOutputFormat asf_stream_muxer = {
842
    "asf_stream",
843
    "asf format",
844
    "video/x-ms-asf",
845
    "asf,wmv,wma",
846
    sizeof(ASFContext),
847
#ifdef CONFIG_LIBMP3LAME
848
    CODEC_ID_MP3,
849
#else
850
    CODEC_ID_MP2,
851
#endif
852
    CODEC_ID_MSMPEG4V3,
853
    asf_write_stream_header,
854
    asf_write_packet,
855
    asf_write_trailer,
856
    .flags = AVFMT_GLOBALHEADER,
857
    .codec_tag= (const AVCodecTag*[]){codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags, 0},
858
};
859
#endif //CONFIG_ASF_STREAM_MUXER