Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 98168220

History | View | Annotate | Download (28.3 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
#define PREROLL_TIME 3100
198

    
199
static void put_guid(ByteIOContext *s, const GUID *g)
200
{
201
    assert(sizeof(*g) == 16);
202
    put_buffer(s, g, sizeof(*g));
203
}
204

    
205
static void put_str16_nolen(ByteIOContext *s, const char *tag);
206
static void put_str16(ByteIOContext *s, const char *tag)
207
{
208
    put_le16(s,strlen(tag) + 1);
209
    put_str16_nolen(s, tag);
210
}
211

    
212
static void put_str16_nolen(ByteIOContext *s, const char *tag)
213
{
214
    int c;
215

    
216
    do{
217
        c = (uint8_t)*tag++;
218
        put_le16(s, c);
219
    }while(c);
220
}
221

    
222
static int64_t put_header(ByteIOContext *pb, const GUID *g)
223
{
224
    int64_t pos;
225

    
226
    pos = url_ftell(pb);
227
    put_guid(pb, g);
228
    put_le64(pb, 24);
229
    return pos;
230
}
231

    
232
/* update header size */
233
static void end_header(ByteIOContext *pb, int64_t pos)
234
{
235
    int64_t pos1;
236

    
237
    pos1 = url_ftell(pb);
238
    url_fseek(pb, pos + 16, SEEK_SET);
239
    put_le64(pb, pos1 - pos);
240
    url_fseek(pb, pos1, SEEK_SET);
241
}
242

    
243
/* write an asf chunk (only used in streaming case) */
244
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
245
{
246
    ASFContext *asf = s->priv_data;
247
    ByteIOContext *pb = &s->pb;
248
    int length;
249

    
250
    length = payload_length + 8;
251
    put_le16(pb, type);
252
    put_le16(pb, length);    //size
253
    put_le32(pb, asf->seqno);//sequence number
254
    put_le16(pb, flags); /* unknown bytes */
255
    put_le16(pb, length);    //size_confirm
256
    asf->seqno++;
257
}
258

    
259
/* convert from unix to windows time */
260
static int64_t unix_to_file_time(int ti)
261
{
262
    int64_t t;
263

    
264
    t = ti * INT64_C(10000000);
265
    t += INT64_C(116444736000000000);
266
    return t;
267
}
268

    
269
/* write the header (used two times if non streamed) */
270
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
271
{
272
    ASFContext *asf = s->priv_data;
273
    ByteIOContext *pb = &s->pb;
274
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
275
    int has_title;
276
    AVCodecContext *enc;
277
    int64_t header_offset, cur_pos, hpos;
278
    int bit_rate;
279
    int64_t duration;
280

    
281
    duration = asf->duration + PREROLL_TIME * 10000;
282
    has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
283

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

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

    
290
        bit_rate += enc->bit_rate;
291
    }
292

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

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

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

    
320
    /* unknown headers */
321
    hpos = put_header(pb, &head1_guid);
322
    put_guid(pb, &head2_guid);
323
    put_le32(pb, 6);
324
    put_le16(pb, 0);
325
    end_header(pb, hpos);
326

    
327
    /* title and other infos */
328
    if (has_title) {
329
        hpos = put_header(pb, &comment_header);
330
        if ( s->title[0]     ) { put_le16(pb, 2 * (strlen(s->title    ) + 1)); } else { put_le16(pb, 0); }
331
        if ( s->author[0]    ) { put_le16(pb, 2 * (strlen(s->author   ) + 1)); } else { put_le16(pb, 0); }
332
        if ( s->copyright[0] ) { put_le16(pb, 2 * (strlen(s->copyright) + 1)); } else { put_le16(pb, 0); }
333
        if ( s->comment[0]   ) { put_le16(pb, 2 * (strlen(s->comment  ) + 1)); } else { put_le16(pb, 0); }
334
        put_le16(pb, 0);
335
        if ( s->title[0]     ) put_str16_nolen(pb, s->title);
336
        if ( s->author[0]    ) put_str16_nolen(pb, s->author);
337
        if ( s->copyright[0] ) put_str16_nolen(pb, s->copyright);
338
        if ( s->comment[0]   ) put_str16_nolen(pb, s->comment);
339
        end_header(pb, hpos);
340
    }
341

    
342
    /* stream headers */
343
    for(n=0;n<s->nb_streams;n++) {
344
        int64_t es_pos;
345
        //        ASFStream *stream = &asf->streams[n];
346

    
347
        enc = s->streams[n]->codec;
348
        asf->streams[n].num = n + 1;
349
        asf->streams[n].seq = 0;
350

    
351

    
352
        switch(enc->codec_type) {
353
        case CODEC_TYPE_AUDIO:
354
            wav_extra_size = 0;
355
            extra_size = 18 + wav_extra_size;
356
            extra_size2 = 8;
357
            break;
358
        default:
359
        case CODEC_TYPE_VIDEO:
360
            wav_extra_size = enc->extradata_size;
361
            extra_size = 0x33 + wav_extra_size;
362
            extra_size2 = 0;
363
            break;
364
        }
365

    
366
        hpos = put_header(pb, &stream_header);
367
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
368
            put_guid(pb, &audio_stream);
369
            put_guid(pb, &audio_conceal_spread);
370
        } else {
371
            put_guid(pb, &video_stream);
372
            put_guid(pb, &video_conceal_none);
373
        }
374
        put_le64(pb, 0); /* ??? */
375
        es_pos = url_ftell(pb);
376
        put_le32(pb, extra_size); /* wav header len */
377
        put_le32(pb, extra_size2); /* additional data len */
378
        put_le16(pb, n + 1); /* stream number */
379
        put_le32(pb, 0); /* ??? */
380

    
381
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
382
            /* WAVEFORMATEX header */
383
            int wavsize = put_wav_header(pb, enc);
384
            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)) {
385
                wavsize += 2;
386
                put_le16(pb, 0);
387
            }
388

    
389
            if (wavsize < 0)
390
                return -1;
391
            if (wavsize != extra_size) {
392
                cur_pos = url_ftell(pb);
393
                url_fseek(pb, es_pos, SEEK_SET);
394
                put_le32(pb, wavsize); /* wav header len */
395
                url_fseek(pb, cur_pos, SEEK_SET);
396
            }
397
            /* ERROR Correction */
398
            put_byte(pb, 0x01);
399
            if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
400
                put_le16(pb, 0x0190);
401
                put_le16(pb, 0x0190);
402
            }else{
403
                put_le16(pb, enc->block_align);
404
                put_le16(pb, enc->block_align);
405
            }
406
            put_le16(pb, 0x01);
407
            put_byte(pb, 0x00);
408
        } else {
409
            put_le32(pb, enc->width);
410
            put_le32(pb, enc->height);
411
            put_byte(pb, 2); /* ??? */
412
            put_le16(pb, 40 + enc->extradata_size); /* size */
413

    
414
            /* BITMAPINFOHEADER header */
415
            put_bmp_header(pb, enc, codec_bmp_tags, 1);
416
        }
417
        end_header(pb, hpos);
418
    }
419

    
420
    /* media comments */
421

    
422
    hpos = put_header(pb, &codec_comment_header);
423
    put_guid(pb, &codec_comment1_header);
424
    put_le32(pb, s->nb_streams);
425
    for(n=0;n<s->nb_streams;n++) {
426
        AVCodec *p;
427

    
428
        enc = s->streams[n]->codec;
429
        p = avcodec_find_encoder(enc->codec_id);
430

    
431
        if(enc->codec_type == CODEC_TYPE_AUDIO)
432
            put_le16(pb, 2);
433
        else if(enc->codec_type == CODEC_TYPE_VIDEO)
434
            put_le16(pb, 1);
435
        else
436
            put_le16(pb, -1);
437

    
438
        if(enc->codec_id == CODEC_ID_WMAV2)
439
            put_str16(pb, "Windows Media Audio V8");
440
        else
441
            put_str16(pb, p ? p->name : enc->codec_name);
442
        put_le16(pb, 0); /* no parameters */
443

    
444

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

    
458
    /* patch the header size fields */
459

    
460
    cur_pos = url_ftell(pb);
461
    header_size = cur_pos - header_offset;
462
    if (asf->is_streamed) {
463
        header_size += 8 + 30 + 50;
464

    
465
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
466
        put_le16(pb, header_size);
467
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
468
        put_le16(pb, header_size);
469

    
470
        header_size -= 8 + 30 + 50;
471
    }
472
    header_size += 24 + 6;
473
    url_fseek(pb, header_offset - 14, SEEK_SET);
474
    put_le64(pb, header_size);
475
    url_fseek(pb, cur_pos, SEEK_SET);
476

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

    
488
static int asf_write_header(AVFormatContext *s)
489
{
490
    ASFContext *asf = s->priv_data;
491

    
492
    asf->packet_size = PACKET_SIZE;
493
    asf->nb_packets = 0;
494

    
495
    asf->last_indexed_pts = 0;
496
    asf->index_ptr = (ASFIndex*)av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
497
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
498
    asf->nb_index_count = 0;
499
    asf->maximum_packet = 0;
500

    
501
    /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
502
     *  at the moment this function is done. It is needed to use asf as
503
     *  streamable format. */
504
    if (asf_write_header1(s, 0, 50) < 0) {
505
        //av_free(asf);
506
        return -1;
507
    }
508

    
509
    put_flush_packet(&s->pb);
510

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

    
517
    return 0;
518
}
519

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

    
524
    asf->is_streamed = 1;
525

    
526
    return asf_write_header(s);
527
}
528

    
529
static int put_payload_parsing_info(
530
                                AVFormatContext *s,
531
                                unsigned int    sendtime,
532
                                unsigned int    duration,
533
                                int             nb_payloads,
534
                                int             padsize
535
            )
536
{
537
    ASFContext *asf = s->priv_data;
538
    ByteIOContext *pb = &s->pb;
539
    int ppi_size, i;
540
    int64_t start= url_ftell(pb);
541

    
542
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
543

    
544
    padsize -= PACKET_HEADER_MIN_SIZE;
545
    if(asf->multi_payloads_present)
546
        padsize--;
547
    assert(padsize>=0);
548

    
549
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
550
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
551
        put_byte(pb, 0x0);
552
    }
553

    
554
    if (asf->multi_payloads_present)
555
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
556

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

    
565
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
566

    
567
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
568
        put_le16(pb, padsize - 2);
569
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
570
        put_byte(pb, padsize - 1);
571

    
572
    put_le32(pb, sendtime);
573
    put_le16(pb, duration);
574
    if (asf->multi_payloads_present)
575
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
576

    
577
    ppi_size = url_ftell(pb) - start;
578

    
579
    return ppi_size;
580
}
581

    
582
static void flush_packet(AVFormatContext *s)
583
{
584
    ASFContext *asf = s->priv_data;
585
    int packet_hdr_size, packet_filled_size;
586

    
587
    if (asf->is_streamed) {
588
        put_chunk(s, 0x4424, asf->packet_size, 0);
589
    }
590

    
591
    packet_hdr_size = put_payload_parsing_info(
592
                            s,
593
                            asf->packet_timestamp_start,
594
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
595
                            asf->packet_nb_payloads,
596
                            asf->packet_size_left
597
                        );
598

    
599
    packet_filled_size = PACKET_SIZE - asf->packet_size_left;
600
    assert(packet_hdr_size <= asf->packet_size_left);
601
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
602

    
603
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
604

    
605
    put_flush_packet(&s->pb);
606
    asf->nb_packets++;
607
    asf->packet_nb_payloads = 0;
608
    asf->packet_timestamp_start = -1;
609
    asf->packet_timestamp_end = -1;
610
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
611
                  NULL, NULL, NULL, NULL);
612
}
613

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

    
628
    val = stream->num;
629
    if (flags & PKT_FLAG_KEY)
630
        val |= ASF_PL_FLAG_KEY_FRAME;
631
    put_byte(pb, val);
632

    
633
    put_byte(pb, stream->seq);  //Media object number
634
    put_le32(pb, m_obj_offset); //Offset Into Media Object
635

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

    
643
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
644
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
645

    
646
    if (asf->multi_payloads_present){
647
        put_le16(pb, payload_len);   //payload length
648
    }
649
}
650

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

    
664
    m_obj_offset = 0;
665
    while (m_obj_offset < m_obj_size) {
666
        payload_len = m_obj_size - m_obj_offset;
667
        if (asf->packet_timestamp_start == -1) {
668
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
669

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

    
683
            asf->packet_timestamp_start = timestamp;
684

    
685
            if(frag_len1 < payload_len && avst->codec->codec_type == CODEC_TYPE_AUDIO){
686
                flush_packet(s);
687
                continue;
688
            }
689
        }
690
        if (frag_len1 > 0) {
691
            if (payload_len > frag_len1)
692
                payload_len = frag_len1;
693
            else if (payload_len == (frag_len1 - 1))
694
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
695

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

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

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

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

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

    
730
    codec = s->streams[pkt->stream_index]->codec;
731
    stream = &asf->streams[pkt->stream_index];
732

    
733
    if(codec->codec_type == CODEC_TYPE_AUDIO)
734
        flags &= ~PKT_FLAG_KEY;
735

    
736
    //XXX /FIXME use duration from AVPacket (quick hack by)
737
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
738
    assert(pts != AV_NOPTS_VALUE);
739
    duration = pts * 10000;
740
    asf->duration= FFMAX(asf->duration, duration);
741

    
742
    packet_st = asf->nb_packets;
743
    put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
744

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

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

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

    
783
    return 0;
784
}
785

    
786
static int asf_write_trailer(AVFormatContext *s)
787
{
788
    ASFContext *asf = s->priv_data;
789
    int64_t file_size,data_size;
790

    
791
    /* flush the current packet */
792
    if (asf->pb.buf_ptr > asf->pb.buffer)
793
        flush_packet(s);
794

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

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

    
811
    put_flush_packet(&s->pb);
812
    av_free(asf->index_ptr);
813
    return 0;
814
}
815

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

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