Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 1d7d9935

History | View | Annotate | Download (27.6 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 = 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, duration); /* duration (in 100ns units) */
313
    put_le32(pb, preroll_time); /* start time stamp */
314
    put_le32(pb, 0); /* ??? */
315
    put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
316
    put_le32(pb, asf->packet_size); /* packet size */
317
    put_le32(pb, asf->packet_size); /* packet size */
318
    put_le32(pb, bit_rate); /* Nominal data rate in bps */
319
    end_header(pb, hpos);
320

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

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

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

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

    
352

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

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

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

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

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

    
421
    /* media comments */
422

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

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

    
432
        put_le16(pb, asf->streams[n].num);
433
        put_str16(pb, p ? p->name : enc->codec_name);
434
        put_le16(pb, 0); /* no parameters */
435

    
436

    
437
        /* id */
438
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
439
            put_le16(pb, 2);
440
            put_le16(pb, enc->codec_tag);
441
        } else {
442
            put_le16(pb, 4);
443
            put_le32(pb, enc->codec_tag);
444
        }
445
        if(!enc->codec_tag)
446
            return -1;
447
    }
448
    end_header(pb, hpos);
449

    
450
    /* patch the header size fields */
451

    
452
    cur_pos = url_ftell(pb);
453
    header_size = cur_pos - header_offset;
454
    if (asf->is_streamed) {
455
        header_size += 8 + 30 + 50;
456

    
457
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
458
        put_le16(pb, header_size);
459
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
460
        put_le16(pb, header_size);
461

    
462
        header_size -= 8 + 30 + 50;
463
    }
464
    header_size += 24 + 6;
465
    url_fseek(pb, header_offset - 14, SEEK_SET);
466
    put_le64(pb, header_size);
467
    url_fseek(pb, cur_pos, SEEK_SET);
468

    
469
    /* movie chunk, followed by packets of packet_size */
470
    asf->data_offset = cur_pos;
471
    put_guid(pb, &data_header);
472
    put_le64(pb, data_chunk_size);
473
    put_guid(pb, &my_guid);
474
    put_le64(pb, asf->nb_packets); /* nb packets */
475
    put_byte(pb, 1); /* ??? */
476
    put_byte(pb, 1); /* ??? */
477
    return 0;
478
}
479

    
480
static int asf_write_header(AVFormatContext *s)
481
{
482
    ASFContext *asf = s->priv_data;
483

    
484
    asf->packet_size = PACKET_SIZE;
485
    asf->nb_packets = 0;
486

    
487
    asf->last_indexed_pts = 0;
488
    asf->index_ptr = (ASFIndex*)av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
489
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
490
    asf->nb_index_count = 0;
491
    asf->maximum_packet = 0;
492

    
493
    if (asf_write_header1(s, 0, 50) < 0) {
494
        //av_free(asf);
495
        return -1;
496
    }
497

    
498
    put_flush_packet(&s->pb);
499

    
500
    asf->packet_nb_payloads = 0;
501
    asf->packet_timestamp_start = -1;
502
    asf->packet_timestamp_end = -1;
503
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
504
                  NULL, NULL, NULL, NULL);
505

    
506
    return 0;
507
}
508

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

    
513
    asf->is_streamed = 1;
514

    
515
    return asf_write_header(s);
516
}
517

    
518
static int put_payload_parsing_info(
519
                                AVFormatContext *s,
520
                                unsigned int    sendtime,
521
                                unsigned int    duration,
522
                                int             nb_payloads,
523
                                int             padsize
524
            )
525
{
526
    ASFContext *asf = s->priv_data;
527
    ByteIOContext *pb = &s->pb;
528
    int ppi_size, i;
529
    int64_t start= url_ftell(pb);
530

    
531
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
532

    
533
    padsize -= PACKET_HEADER_MIN_SIZE;
534
    if(asf->multi_payloads_present)
535
        padsize--;
536
    assert(padsize>=0);
537

    
538
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
539
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
540
        put_byte(pb, 0x0);
541
    }
542

    
543
    if (asf->multi_payloads_present)
544
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
545

    
546
    if (padsize > 0) {
547
        if (padsize < 256)
548
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
549
        else
550
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
551
    }
552
    put_byte(pb, iLengthTypeFlags);
553

    
554
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
555

    
556
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
557
        put_le16(pb, padsize - 2);
558
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
559
        put_byte(pb, padsize - 1);
560

    
561
    put_le32(pb, sendtime);
562
    put_le16(pb, duration);
563
    if (asf->multi_payloads_present)
564
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
565

    
566
    ppi_size = url_ftell(pb) - start;
567

    
568
    return ppi_size;
569
}
570

    
571
static void flush_packet(AVFormatContext *s)
572
{
573
    ASFContext *asf = s->priv_data;
574
    int packet_hdr_size, packet_filled_size;
575

    
576
    if (asf->is_streamed) {
577
        put_chunk(s, 0x4424, asf->packet_size, 0);
578
    }
579

    
580
    packet_hdr_size = put_payload_parsing_info(
581
                            s,
582
                            asf->packet_timestamp_start,
583
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
584
                            asf->packet_nb_payloads,
585
                            asf->packet_size_left
586
                        );
587

    
588
    packet_filled_size = PACKET_SIZE - asf->packet_size_left;
589
    assert(packet_hdr_size <= asf->packet_size_left);
590
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
591

    
592
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
593

    
594
    put_flush_packet(&s->pb);
595
    asf->nb_packets++;
596
    asf->packet_nb_payloads = 0;
597
    asf->packet_timestamp_start = -1;
598
    asf->packet_timestamp_end = -1;
599
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
600
                  NULL, NULL, NULL, NULL);
601
}
602

    
603
static void put_payload_header(
604
                                AVFormatContext *s,
605
                                ASFStream       *stream,
606
                                int             presentation_time,
607
                                int             m_obj_size,
608
                                int             m_obj_offset,
609
                                int             payload_len,
610
                                int             flags
611
            )
612
{
613
    ASFContext *asf = s->priv_data;
614
    ByteIOContext *pb = &asf->pb;
615
    int val;
616

    
617
    val = stream->num;
618
    if (flags & PKT_FLAG_KEY)
619
        val |= ASF_PL_FLAG_KEY_FRAME;
620
    put_byte(pb, val);
621

    
622
    put_byte(pb, stream->seq);  //Media object number
623
    put_le32(pb, m_obj_offset); //Offset Into Media Object
624

    
625
    // Replicated Data shall be at least 8 bytes long.
626
    // The first 4 bytes of data shall contain the
627
    // Size of the Media Object that the payload belongs to.
628
    // The next 4 bytes of data shall contain the
629
    // Presentation Time for the media object that the payload belongs to.
630
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
631

    
632
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
633
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
634

    
635
    if (asf->multi_payloads_present){
636
        put_le16(pb, payload_len);   //payload length
637
    }
638
}
639

    
640
static void put_frame(
641
                    AVFormatContext *s,
642
                    ASFStream       *stream,
643
                    int             timestamp,
644
                    const uint8_t   *buf,
645
                    int             m_obj_size,
646
                    int             flags
647
                )
648
{
649
    ASFContext *asf = s->priv_data;
650
    int m_obj_offset, payload_len, frag_len1;
651

    
652
    m_obj_offset = 0;
653
    while (m_obj_offset < m_obj_size) {
654
        payload_len = m_obj_size - m_obj_offset;
655
        if (asf->packet_timestamp_start == -1) {
656
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
657

    
658
            asf->packet_size_left = PACKET_SIZE;
659
            if (asf->multi_payloads_present){
660
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
661
            }
662
            else {
663
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
664
            }
665
            asf->packet_timestamp_start = timestamp;
666
        }
667
        else {
668
            // multi payloads
669
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
670

    
671
            asf->packet_timestamp_start = timestamp;
672
        }
673
        if (frag_len1 > 0) {
674
            if (payload_len > frag_len1)
675
                payload_len = frag_len1;
676
            else if (payload_len == (frag_len1 - 1))
677
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
678

    
679
            put_payload_header(s, stream, timestamp+preroll_time, m_obj_size, m_obj_offset, payload_len, flags);
680
            put_buffer(&asf->pb, buf, payload_len);
681

    
682
            if (asf->multi_payloads_present)
683
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
684
            else
685
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
686
            asf->packet_timestamp_end = timestamp;
687

    
688
            asf->packet_nb_payloads++;
689
        } else {
690
            payload_len = 0;
691
        }
692
        m_obj_offset += payload_len;
693
        buf += payload_len;
694

    
695
        if (!asf->multi_payloads_present)
696
            flush_packet(s);
697
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
698
            flush_packet(s);
699
    }
700
    stream->seq++;
701
}
702

    
703
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
704
{
705
    ASFContext *asf = s->priv_data;
706
    ASFStream *stream;
707
    int64_t duration;
708
    AVCodecContext *codec;
709
    int64_t packet_st,pts;
710
    int start_sec,i;
711
    int flags= pkt->flags;
712

    
713
    codec = s->streams[pkt->stream_index]->codec;
714
    stream = &asf->streams[pkt->stream_index];
715

    
716
    if(codec->codec_type == CODEC_TYPE_AUDIO)
717
        flags &= ~PKT_FLAG_KEY;
718

    
719
    //XXX /FIXME use duration from AVPacket (quick hack by)
720
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
721
    assert(pts != AV_NOPTS_VALUE);
722
    duration = pts * 10000;
723
    asf->duration= FFMAX(asf->duration, duration);
724

    
725
    packet_st = asf->nb_packets;
726
    put_frame(s, stream, pkt->dts, pkt->data, pkt->size, flags);
727

    
728
    /* check index */
729
    if ((!asf->is_streamed) && (flags & PKT_FLAG_KEY)) {
730
        start_sec = (int)(duration / INT64_C(10000000));
731
        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
732
            for(i=asf->nb_index_count;i<start_sec;i++) {
733
                if (i>=asf->nb_index_memory_alloc) {
734
                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
735
                    asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
736
                }
737
                // store
738
                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
739
                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets-packet_st);
740
                asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
741
            }
742
            asf->nb_index_count = start_sec;
743
            asf->last_indexed_pts = duration;
744
        }
745
    }
746
    return 0;
747
}
748

    
749
//
750
static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
751
{
752
    ByteIOContext *pb = &s->pb;
753
    int i;
754

    
755
    put_guid(pb, &simple_index_header);
756
    put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
757
    put_guid(pb, &my_guid);
758
    put_le64(pb, ASF_INDEXED_INTERVAL);
759
    put_le32(pb, max);
760
    put_le32(pb, count);
761
    for(i=0; i<count; i++) {
762
        put_le32(pb, index[i].packet_number);
763
        put_le16(pb, index[i].packet_count);
764
    }
765

    
766
    return 0;
767
}
768

    
769
static int asf_write_trailer(AVFormatContext *s)
770
{
771
    ASFContext *asf = s->priv_data;
772
    int64_t file_size,data_size;
773

    
774
    /* flush the current packet */
775
    if (asf->pb.buf_ptr > asf->pb.buffer)
776
        flush_packet(s);
777

    
778
    /* write index */
779
    data_size = url_ftell(&s->pb);
780
    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
781
        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
782
    }
783
    put_flush_packet(&s->pb);
784

    
785
    if (asf->is_streamed) {
786
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
787
    } else {
788
        /* rewrite an updated header */
789
        file_size = url_ftell(&s->pb);
790
        url_fseek(&s->pb, 0, SEEK_SET);
791
        asf_write_header1(s, file_size, data_size - asf->data_offset);
792
    }
793

    
794
    put_flush_packet(&s->pb);
795
    av_free(asf->index_ptr);
796
    return 0;
797
}
798

    
799
#ifdef CONFIG_ASF_MUXER
800
AVOutputFormat asf_muxer = {
801
    "asf",
802
    "asf format",
803
    "video/x-ms-asf",
804
    "asf,wmv,wma",
805
    sizeof(ASFContext),
806
#ifdef CONFIG_LIBMP3LAME
807
    CODEC_ID_MP3,
808
#else
809
    CODEC_ID_MP2,
810
#endif
811
    CODEC_ID_MSMPEG4V3,
812
    asf_write_header,
813
    asf_write_packet,
814
    asf_write_trailer,
815
    .flags = AVFMT_GLOBALHEADER,
816
    .codec_tag= (const AVCodecTag*[]){codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags, 0},
817
};
818
#endif
819

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