Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 7caf0cc6

History | View | Annotate | Download (29.1 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(ByteIOContext *s, const char *tag)
213
{
214
    int c;
215

    
216
    put_le16(s,strlen(tag) + 1);
217
    for(;;) {
218
        c = (uint8_t)*tag++;
219
        put_le16(s, c);
220
        if (c == '\0')
221
            break;
222
    }
223
}
224

    
225
static void put_str16_nolen(ByteIOContext *s, const char *tag)
226
{
227
    int c;
228

    
229
    for(;;) {
230
        c = (uint8_t)*tag++;
231
        put_le16(s, c);
232
        if (c == '\0')
233
            break;
234
    }
235
}
236

    
237
static int64_t put_header(ByteIOContext *pb, const GUID *g)
238
{
239
    int64_t pos;
240

    
241
    pos = url_ftell(pb);
242
    put_guid(pb, g);
243
    put_le64(pb, 24);
244
    return pos;
245
}
246

    
247
/* update header size */
248
static void end_header(ByteIOContext *pb, int64_t pos)
249
{
250
    int64_t pos1;
251

    
252
    pos1 = url_ftell(pb);
253
    url_fseek(pb, pos + 16, SEEK_SET);
254
    put_le64(pb, pos1 - pos);
255
    url_fseek(pb, pos1, SEEK_SET);
256
}
257

    
258
/* write an asf chunk (only used in streaming case) */
259
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
260
{
261
    ASFContext *asf = s->priv_data;
262
    ByteIOContext *pb = &s->pb;
263
    int length;
264

    
265
    length = payload_length + 8;
266
    put_le16(pb, type);
267
    put_le16(pb, length);    //size
268
    put_le32(pb, asf->seqno);//sequence number
269
    put_le16(pb, flags); /* unknown bytes */
270
    put_le16(pb, length);    //size_confirm
271
    asf->seqno++;
272
}
273

    
274
/* convert from unix to windows time */
275
static int64_t unix_to_file_time(int ti)
276
{
277
    int64_t t;
278

    
279
    t = ti * INT64_C(10000000);
280
    t += INT64_C(116444736000000000);
281
    return t;
282
}
283

    
284
/* write the header (used two times if non streamed) */
285
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
286
{
287
    ASFContext *asf = s->priv_data;
288
    ByteIOContext *pb = &s->pb;
289
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
290
    int has_title;
291
    AVCodecContext *enc;
292
    int64_t header_offset, cur_pos, hpos;
293
    int bit_rate;
294
    int64_t duration;
295

    
296
    duration = asf->duration + preroll_time * 10000;
297
    has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
298

    
299
    bit_rate = 0;
300
    for(n=0;n<s->nb_streams;n++) {
301
        enc = s->streams[n]->codec;
302

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

    
305
        bit_rate += enc->bit_rate;
306
    }
307

    
308
    if (asf->is_streamed) {
309
        put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
310
    }
311

    
312
    put_guid(pb, &asf_header);
313
    put_le64(pb, -1); /* header length, will be patched after */
314
    put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
315
    put_byte(pb, 1); /* ??? */
316
    put_byte(pb, 2); /* ??? */
317

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

    
336
    /* unknown headers */
337
    hpos = put_header(pb, &head1_guid);
338
    put_guid(pb, &head2_guid);
339
    put_le32(pb, 6);
340
    put_le16(pb, 0);
341
    end_header(pb, hpos);
342

    
343
    /* title and other infos */
344
    if (has_title) {
345
        hpos = put_header(pb, &comment_header);
346
        if ( s->title[0]     ) { put_le16(pb, 2 * (strlen(s->title    ) + 1)); } else { put_le16(pb, 0); }
347
        if ( s->author[0]    ) { put_le16(pb, 2 * (strlen(s->author   ) + 1)); } else { put_le16(pb, 0); }
348
        if ( s->copyright[0] ) { put_le16(pb, 2 * (strlen(s->copyright) + 1)); } else { put_le16(pb, 0); }
349
        if ( s->comment[0]   ) { put_le16(pb, 2 * (strlen(s->comment  ) + 1)); } else { put_le16(pb, 0); }
350
        put_le16(pb, 0);
351
        if ( s->title[0]     ) put_str16_nolen(pb, s->title);
352
        if ( s->author[0]    ) put_str16_nolen(pb, s->author);
353
        if ( s->copyright[0] ) put_str16_nolen(pb, s->copyright);
354
        if ( s->comment[0]   ) put_str16_nolen(pb, s->comment);
355
        end_header(pb, hpos);
356
    }
357

    
358
    /* stream headers */
359
    for(n=0;n<s->nb_streams;n++) {
360
        int64_t es_pos;
361
        const uint8_t *er_spr = NULL;
362
        int er_spr_len = 0;
363
        //        ASFStream *stream = &asf->streams[n];
364

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

    
369

    
370
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
371
            if (enc->codec_id == CODEC_ID_ADPCM_G726) {
372
                er_spr     = error_spread_ADPCM_G726;
373
                er_spr_len = sizeof(error_spread_ADPCM_G726);
374
            }
375
        }
376

    
377
        switch(enc->codec_type) {
378
        case CODEC_TYPE_AUDIO:
379
            wav_extra_size = 0;
380
            extra_size = 18 + wav_extra_size;
381
            extra_size2 = er_spr_len;
382
            break;
383
        default:
384
        case CODEC_TYPE_VIDEO:
385
            wav_extra_size = enc->extradata_size;
386
            extra_size = 0x33 + wav_extra_size;
387
            extra_size2 = 0;
388
            break;
389
        }
390

    
391
        hpos = put_header(pb, &stream_header);
392
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
393
            put_guid(pb, &audio_stream);
394
            if ((er_spr != NULL) && (er_spr_len != 0)) {
395
                put_guid(pb, &audio_conceal_spread);
396
            } else {
397
                put_guid(pb, &video_conceal_none);
398
            }
399
        } else {
400
            put_guid(pb, &video_stream);
401
            put_guid(pb, &video_conceal_none);
402
        }
403
        put_le64(pb, 0); /* ??? */
404
        es_pos = url_ftell(pb);
405
        put_le32(pb, extra_size); /* wav header len */
406
        put_le32(pb, extra_size2); /* additional data len */
407
        put_le16(pb, n + 1); /* stream number */
408
        put_le32(pb, 0); /* ??? */
409

    
410
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
411
            /* WAVEFORMATEX header */
412
            int wavsize = put_wav_header(pb, enc);
413
            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)) {
414
                wavsize += 2;
415
                put_le16(pb, 0);
416
            }
417

    
418
            if (wavsize < 0)
419
                return -1;
420
            if (wavsize != extra_size) {
421
                cur_pos = url_ftell(pb);
422
                url_fseek(pb, es_pos, SEEK_SET);
423
                put_le32(pb, wavsize); /* wav header len */
424
                url_fseek(pb, cur_pos, SEEK_SET);
425
            }
426
            /* ERROR Correction */
427
            if ((er_spr != NULL) && (er_spr_len != 0))
428
                put_buffer(pb, er_spr, er_spr_len);
429
        } else {
430
            put_le32(pb, enc->width);
431
            put_le32(pb, enc->height);
432
            put_byte(pb, 2); /* ??? */
433
            put_le16(pb, 40 + enc->extradata_size); /* size */
434

    
435
            /* BITMAPINFOHEADER header */
436
            put_bmp_header(pb, enc, codec_bmp_tags, 1);
437
        }
438
        end_header(pb, hpos);
439
    }
440

    
441
    /* media comments */
442

    
443
    hpos = put_header(pb, &codec_comment_header);
444
    put_guid(pb, &codec_comment1_header);
445
    put_le32(pb, s->nb_streams);
446
    for(n=0;n<s->nb_streams;n++) {
447
        AVCodec *p;
448

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

    
452
        put_le16(pb, asf->streams[n].num);
453
        put_str16(pb, p ? p->name : enc->codec_name);
454
        put_le16(pb, 0); /* no parameters */
455

    
456

    
457
        /* id */
458
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
459
            put_le16(pb, 2);
460
            if(!enc->codec_tag)
461
                enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
462
            if(!enc->codec_tag)
463
                return -1;
464
            put_le16(pb, enc->codec_tag);
465
        } else {
466
            put_le16(pb, 4);
467
            if(!enc->codec_tag)
468
                enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id);
469
            if(!enc->codec_tag)
470
                return -1;
471
            put_le32(pb, enc->codec_tag);
472
        }
473
    }
474
    end_header(pb, hpos);
475

    
476
    /* patch the header size fields */
477

    
478
    cur_pos = url_ftell(pb);
479
    header_size = cur_pos - header_offset;
480
    if (asf->is_streamed) {
481
        header_size += 8 + 30 + 50;
482

    
483
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
484
        put_le16(pb, header_size);
485
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
486
        put_le16(pb, header_size);
487

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

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

    
506
static int asf_write_header(AVFormatContext *s)
507
{
508
    ASFContext *asf = s->priv_data;
509

    
510
    asf->packet_size = PACKET_SIZE;
511
    asf->nb_packets = 0;
512

    
513
    asf->last_indexed_pts = 0;
514
    asf->index_ptr = (ASFIndex*)av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
515
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
516
    asf->nb_index_count = 0;
517
    asf->maximum_packet = 0;
518

    
519
    if (asf_write_header1(s, 0, 50) < 0) {
520
        //av_free(asf);
521
        return -1;
522
    }
523

    
524
    put_flush_packet(&s->pb);
525

    
526
    asf->packet_nb_payloads = 0;
527
    asf->prev_packet_sent_time = 0;
528
    asf->packet_timestamp_start = -1;
529
    asf->packet_timestamp_end = -1;
530
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
531
                  NULL, NULL, NULL, NULL);
532

    
533
    return 0;
534
}
535

    
536
static int asf_write_stream_header(AVFormatContext *s)
537
{
538
    ASFContext *asf = s->priv_data;
539

    
540
    asf->is_streamed = 1;
541

    
542
    return asf_write_header(s);
543
}
544

    
545
static int put_payload_parsing_info(
546
                                AVFormatContext *s,
547
                                unsigned int    sendtime,
548
                                unsigned int    duration,
549
                                int             nb_payloads,
550
                                int             padsize
551
            )
552
{
553
    ASFContext *asf = s->priv_data;
554
    ByteIOContext *pb = &s->pb;
555
    int ppi_size, i;
556
    unsigned char *start_ppi_ptr = pb->buf_ptr;
557

    
558
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
559

    
560
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
561
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
562
        put_byte(pb, 0x0);
563
    }
564

    
565
    if (asf->multi_payloads_present)
566
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
567

    
568
    if (padsize > 0) {
569
        if (padsize < 256)
570
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
571
        else
572
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
573
    }
574
    put_byte(pb, iLengthTypeFlags);
575

    
576
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
577

    
578
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
579
        put_le16(pb, padsize - 2);
580
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
581
        put_byte(pb, padsize - 1);
582

    
583
    put_le32(pb, sendtime);
584
    put_le16(pb, duration);
585
    if (asf->multi_payloads_present)
586
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
587

    
588
    ppi_size = pb->buf_ptr - start_ppi_ptr;
589

    
590
    return ppi_size;
591
}
592

    
593
static void flush_packet(AVFormatContext *s)
594
{
595
    ASFContext *asf = s->priv_data;
596
    int packet_hdr_size, packet_filled_size;
597

    
598
    if (asf->is_streamed) {
599
        put_chunk(s, 0x4424, asf->packet_size, 0);
600
    }
601

    
602
    packet_hdr_size = put_payload_parsing_info(
603
                            s,
604
                            asf->packet_timestamp_start,
605
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
606
                            asf->packet_nb_payloads,
607
                            asf->packet_size_left
608
                        );
609

    
610
    packet_filled_size = PACKET_SIZE - packet_hdr_size - asf->packet_size_left;
611
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
612

    
613
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
614

    
615
    put_flush_packet(&s->pb);
616
    asf->nb_packets++;
617
    asf->packet_nb_payloads = 0;
618
    asf->prev_packet_sent_time = asf->packet_timestamp_start;
619
    asf->packet_timestamp_start = -1;
620
    asf->packet_timestamp_end = -1;
621
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
622
                  NULL, NULL, NULL, NULL);
623
}
624

    
625
static void put_payload_header(
626
                                AVFormatContext *s,
627
                                ASFStream       *stream,
628
                                int             presentation_time,
629
                                int             m_obj_size,
630
                                int             m_obj_offset,
631
                                int             payload_len,
632
                                int             flags
633
            )
634
{
635
    ASFContext *asf = s->priv_data;
636
    ByteIOContext *pb = &asf->pb;
637
    int val;
638

    
639
    val = stream->num;
640
    if (flags & PKT_FLAG_KEY)
641
        val |= ASF_PL_FLAG_KEY_FRAME;
642
    put_byte(pb, val);
643

    
644
    put_byte(pb, stream->seq);  //Media object number
645
    put_le32(pb, m_obj_offset); //Offset Into Media Object
646

    
647
    // Replicated Data shall be at least 8 bytes long.
648
    // The first 4 bytes of data shall contain the
649
    // Size of the Media Object that the payload belongs to.
650
    // The next 4 bytes of data shall contain the
651
    // Presentation Time for the media object that the payload belongs to.
652
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
653

    
654
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
655
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
656

    
657
    if (asf->multi_payloads_present){
658
        put_le16(pb, payload_len);   //payload length
659
    }
660
}
661

    
662
static void put_frame(
663
                    AVFormatContext *s,
664
                    ASFStream       *stream,
665
                    int             timestamp,
666
                    const uint8_t   *buf,
667
                    int             m_obj_size,
668
                    int             flags
669
                )
670
{
671
    ASFContext *asf = s->priv_data;
672
    int m_obj_offset, payload_len, frag_len1;
673

    
674
    m_obj_offset = 0;
675
    while (m_obj_offset < m_obj_size) {
676
        payload_len = m_obj_size - m_obj_offset;
677
        if (asf->packet_timestamp_start == -1) {
678
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
679

    
680
            if (asf->multi_payloads_present){
681
                asf->packet_size_left = PACKET_SIZE; //For debug
682
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE - 1;
683
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
684
            }
685
            else {
686
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE;
687
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
688
            }
689
            if (asf->prev_packet_sent_time > timestamp)
690
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
691
            else
692
                asf->packet_timestamp_start = timestamp;
693
        }
694
        else {
695
            // multi payloads
696
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS;
697

    
698
            if (asf->prev_packet_sent_time > timestamp)
699
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
700
            else if (asf->packet_timestamp_start >= timestamp)
701
                asf->packet_timestamp_start = timestamp;
702
        }
703
        if (frag_len1 > 0) {
704
            if (payload_len > frag_len1)
705
                payload_len = frag_len1;
706
            else if (payload_len == (frag_len1 - 1))
707
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
708

    
709
            put_payload_header(s, stream, timestamp+preroll_time, m_obj_size, m_obj_offset, payload_len, flags);
710
            put_buffer(&asf->pb, buf, payload_len);
711

    
712
            if (asf->multi_payloads_present)
713
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
714
            else
715
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
716
            asf->packet_timestamp_end = timestamp;
717

    
718
            asf->packet_nb_payloads++;
719
        } else {
720
            payload_len = 0;
721
        }
722
        m_obj_offset += payload_len;
723
        buf += payload_len;
724

    
725
        if (!asf->multi_payloads_present)
726
            flush_packet(s);
727
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + 1))
728
            flush_packet(s);
729
    }
730
    stream->seq++;
731
}
732

    
733
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
734
{
735
    ASFContext *asf = s->priv_data;
736
    ASFStream *stream;
737
    int64_t duration;
738
    AVCodecContext *codec;
739
    int64_t packet_st,pts;
740
    int start_sec,i;
741

    
742
    codec = s->streams[pkt->stream_index]->codec;
743
    stream = &asf->streams[pkt->stream_index];
744

    
745
    //XXX /FIXME use duration from AVPacket (quick hack by)
746
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
747
    if (pts == AV_NOPTS_VALUE) {
748
        if (codec->codec_type == CODEC_TYPE_AUDIO) {
749
            duration = (codec->frame_number * (int64_t)codec->frame_size * INT64_C(10000000)) /
750
                codec->sample_rate;
751
        } else {
752
            duration = av_rescale(codec->frame_number * (int64_t)codec->time_base.num, 10000000, codec->time_base.den);
753
        }
754
    } else {
755
        duration = pts * 10000;
756
    }
757
    if (duration > asf->duration)
758
        asf->duration = duration;
759

    
760
    packet_st = asf->nb_packets;
761
    put_frame(s, stream, pkt->pts, pkt->data, pkt->size, pkt->flags);
762

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

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

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

    
802
    return 0;
803
}
804

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

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

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

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

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

    
835
#ifdef CONFIG_ASF_MUXER
836
AVOutputFormat asf_muxer = {
837
    "asf",
838
    "asf format",
839
    "video/x-ms-asf",
840
    "asf,wmv,wma",
841
    sizeof(ASFContext),
842
#ifdef CONFIG_LIBMP3LAME
843
    CODEC_ID_MP3,
844
#else
845
    CODEC_ID_MP2,
846
#endif
847
    CODEC_ID_MSMPEG4V3,
848
    asf_write_header,
849
    asf_write_packet,
850
    asf_write_trailer,
851
    .flags = AVFMT_GLOBALHEADER,
852
    .codec_tag= {codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags},
853
};
854
#endif
855

    
856
#ifdef CONFIG_ASF_STREAM_MUXER
857
AVOutputFormat asf_stream_muxer = {
858
    "asf_stream",
859
    "asf format",
860
    "video/x-ms-asf",
861
    "asf,wmv,wma",
862
    sizeof(ASFContext),
863
#ifdef CONFIG_LIBMP3LAME
864
    CODEC_ID_MP3,
865
#else
866
    CODEC_ID_MP2,
867
#endif
868
    CODEC_ID_MSMPEG4V3,
869
    asf_write_stream_header,
870
    asf_write_packet,
871
    asf_write_trailer,
872
    .flags = AVFMT_GLOBALHEADER,
873
    .codec_tag= {codec_asf_bmp_tags, codec_bmp_tags, codec_wav_tags},
874
};
875
#endif //CONFIG_ASF_STREAM_MUXER