Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 3840147e

History | View | Annotate | Download (28.6 KB)

1 542993b0 Konstantin Andreyev
/*
2
 * Adaptive stream format encoder
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 542993b0 Konstantin Andreyev
 */
19
#include "avformat.h"
20
#include "avi.h"
21
#include "asf.h"
22
23
#undef NDEBUG
24
#include <assert.h>
25
26 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
27 615b92fd Konstantin Andreyev
28 982e53fe Calcium
29 bb270c08 Diego Biurrun
#define ASF_INDEXED_INTERVAL    10000000
30
#define ASF_INDEX_BLOCK         600
31 982e53fe Calcium
32 615b92fd Konstantin Andreyev
#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
33
#define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
34 bb270c08 Diego Biurrun
                ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
35
                ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
36
                )
37 615b92fd Konstantin Andreyev
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 bb270c08 Diego Biurrun
                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 615b92fd Konstantin Andreyev
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 bb270c08 Diego Biurrun
                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 615b92fd Konstantin Andreyev
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 bb270c08 Diego Biurrun
                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 115329f1 Diego Biurrun
168 615b92fd Konstantin Andreyev
#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
169 bb270c08 Diego Biurrun
                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 615b92fd Konstantin Andreyev
177
#define SINGLE_PAYLOAD_DATA_LENGTH (\
178 bb270c08 Diego Biurrun
                PACKET_SIZE - \
179
                PACKET_HEADER_MIN_SIZE - \
180
                PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
181
                )
182 615b92fd Konstantin Andreyev
183
#define MULTI_PAYLOAD_CONSTANT (\
184 bb270c08 Diego Biurrun
                PACKET_SIZE - \
185
                PACKET_HEADER_MIN_SIZE - \
186
                1 - /*Payload Flags*/ \
187
                2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
188
                )
189 615b92fd Konstantin Andreyev
190
static int preroll_time = 2000;
191
192 982e53fe Calcium
static const uint8_t error_spread_ADPCM_G726[] = { 0x01, 0x90, 0x01, 0x90, 0x01, 0x01, 0x00, 0x00 };
193
194 542993b0 Konstantin Andreyev
static void put_guid(ByteIOContext *s, const GUID *g)
195
{
196
    int i;
197
198
    put_le32(s, g->v1);
199
    put_le16(s, g->v2);
200
    put_le16(s, g->v3);
201
    for(i=0;i<8;i++)
202
        put_byte(s, g->v4[i]);
203
}
204
205
static void put_str16(ByteIOContext *s, const char *tag)
206
{
207
    int c;
208
209
    put_le16(s,strlen(tag) + 1);
210
    for(;;) {
211
        c = (uint8_t)*tag++;
212
        put_le16(s, c);
213
        if (c == '\0')
214
            break;
215
    }
216
}
217
218
static void put_str16_nolen(ByteIOContext *s, const char *tag)
219
{
220
    int c;
221
222
    for(;;) {
223
        c = (uint8_t)*tag++;
224
        put_le16(s, c);
225
        if (c == '\0')
226
            break;
227
    }
228
}
229
230
static int64_t put_header(ByteIOContext *pb, const GUID *g)
231
{
232
    int64_t pos;
233
234
    pos = url_ftell(pb);
235
    put_guid(pb, g);
236
    put_le64(pb, 24);
237
    return pos;
238
}
239
240
/* update header size */
241
static void end_header(ByteIOContext *pb, int64_t pos)
242
{
243
    int64_t pos1;
244
245
    pos1 = url_ftell(pb);
246
    url_fseek(pb, pos + 16, SEEK_SET);
247
    put_le64(pb, pos1 - pos);
248
    url_fseek(pb, pos1, SEEK_SET);
249
}
250
251
/* write an asf chunk (only used in streaming case) */
252
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
253
{
254
    ASFContext *asf = s->priv_data;
255
    ByteIOContext *pb = &s->pb;
256
    int length;
257
258
    length = payload_length + 8;
259
    put_le16(pb, type);
260 615b92fd Konstantin Andreyev
    put_le16(pb, length);    //size
261
    put_le32(pb, asf->seqno);//sequence number
262 542993b0 Konstantin Andreyev
    put_le16(pb, flags); /* unknown bytes */
263 615b92fd Konstantin Andreyev
    put_le16(pb, length);    //size_confirm
264 542993b0 Konstantin Andreyev
    asf->seqno++;
265
}
266
267
/* convert from unix to windows time */
268
static int64_t unix_to_file_time(int ti)
269
{
270
    int64_t t;
271
272
    t = ti * int64_t_C(10000000);
273
    t += int64_t_C(116444736000000000);
274
    return t;
275
}
276
277
/* write the header (used two times if non streamed) */
278
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
279
{
280
    ASFContext *asf = s->priv_data;
281
    ByteIOContext *pb = &s->pb;
282
    int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
283
    int has_title;
284
    AVCodecContext *enc;
285
    int64_t header_offset, cur_pos, hpos;
286
    int bit_rate;
287
288
    has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]);
289
290
    bit_rate = 0;
291
    for(n=0;n<s->nb_streams;n++) {
292 01f4895c Michael Niedermayer
        enc = s->streams[n]->codec;
293 542993b0 Konstantin Andreyev
294 9ee91c2f Michael Niedermayer
        av_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
295
296 542993b0 Konstantin Andreyev
        bit_rate += enc->bit_rate;
297
    }
298
299
    if (asf->is_streamed) {
300
        put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
301
    }
302
303
    put_guid(pb, &asf_header);
304
    put_le64(pb, -1); /* header length, will be patched after */
305
    put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
306
    put_byte(pb, 1); /* ??? */
307
    put_byte(pb, 2); /* ??? */
308
309
    /* file header */
310
    header_offset = url_ftell(pb);
311
    hpos = put_header(pb, &file_header);
312
    put_guid(pb, &my_guid);
313
    put_le64(pb, file_size);
314
    file_time = 0;
315
    put_le64(pb, unix_to_file_time(file_time));
316
    put_le64(pb, asf->nb_packets); /* number of packets */
317
    put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
318
    put_le64(pb, asf->duration); /* duration (in 100ns units) */
319 e928649b Michael Niedermayer
    put_le32(pb, preroll_time); /* start time stamp */
320 542993b0 Konstantin Andreyev
    put_le32(pb, 0); /* ??? */
321
    put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
322
    put_le32(pb, asf->packet_size); /* packet size */
323
    put_le32(pb, asf->packet_size); /* packet size */
324
    put_le32(pb, bit_rate); /* Nominal data rate in bps */
325
    end_header(pb, hpos);
326
327
    /* unknown headers */
328
    hpos = put_header(pb, &head1_guid);
329
    put_guid(pb, &head2_guid);
330
    put_le32(pb, 6);
331
    put_le16(pb, 0);
332
    end_header(pb, hpos);
333
334
    /* title and other infos */
335
    if (has_title) {
336
        hpos = put_header(pb, &comment_header);
337 982e53fe Calcium
        if ( s->title[0]     ) { put_le16(pb, 2 * (strlen(s->title    ) + 1)); } else { put_le16(pb, 0); }
338
        if ( s->author[0]    ) { put_le16(pb, 2 * (strlen(s->author   ) + 1)); } else { put_le16(pb, 0); }
339
        if ( s->copyright[0] ) { put_le16(pb, 2 * (strlen(s->copyright) + 1)); } else { put_le16(pb, 0); }
340
        if ( s->comment[0]   ) { put_le16(pb, 2 * (strlen(s->comment  ) + 1)); } else { put_le16(pb, 0); }
341 542993b0 Konstantin Andreyev
        put_le16(pb, 0);
342 982e53fe Calcium
        if ( s->title[0]     ) put_str16_nolen(pb, s->title);
343
        if ( s->author[0]    ) put_str16_nolen(pb, s->author);
344
        if ( s->copyright[0] ) put_str16_nolen(pb, s->copyright);
345
        if ( s->comment[0]   ) put_str16_nolen(pb, s->comment);
346 542993b0 Konstantin Andreyev
        end_header(pb, hpos);
347
    }
348
349
    /* stream headers */
350
    for(n=0;n<s->nb_streams;n++) {
351
        int64_t es_pos;
352 982e53fe Calcium
        uint8_t *er_spr = NULL;
353
        int er_spr_len = 0;
354 542993b0 Konstantin Andreyev
        //        ASFStream *stream = &asf->streams[n];
355
356 01f4895c Michael Niedermayer
        enc = s->streams[n]->codec;
357 542993b0 Konstantin Andreyev
        asf->streams[n].num = n + 1;
358
        asf->streams[n].seq = 0;
359
360 115329f1 Diego Biurrun
361 982e53fe Calcium
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
362
            if (enc->codec_id == CODEC_ID_ADPCM_G726) {
363
                er_spr     = (uint8_t *)error_spread_ADPCM_G726;
364
                er_spr_len = sizeof(error_spread_ADPCM_G726);
365
            }
366
        }
367
368 542993b0 Konstantin Andreyev
        switch(enc->codec_type) {
369
        case CODEC_TYPE_AUDIO:
370
            wav_extra_size = 0;
371
            extra_size = 18 + wav_extra_size;
372 982e53fe Calcium
            extra_size2 = er_spr_len;
373 542993b0 Konstantin Andreyev
            break;
374
        default:
375
        case CODEC_TYPE_VIDEO:
376 982e53fe Calcium
            wav_extra_size = enc->extradata_size;
377
            extra_size = 0x33 + wav_extra_size;
378 542993b0 Konstantin Andreyev
            extra_size2 = 0;
379
            break;
380
        }
381
382
        hpos = put_header(pb, &stream_header);
383
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
384
            put_guid(pb, &audio_stream);
385 982e53fe Calcium
            if ((er_spr != NULL) && (er_spr_len != 0)) {
386
                put_guid(pb, &audio_conceal_spread);
387
            } else {
388
                put_guid(pb, &video_conceal_none);
389
            }
390 542993b0 Konstantin Andreyev
        } else {
391
            put_guid(pb, &video_stream);
392
            put_guid(pb, &video_conceal_none);
393
        }
394
        put_le64(pb, 0); /* ??? */
395
        es_pos = url_ftell(pb);
396
        put_le32(pb, extra_size); /* wav header len */
397
        put_le32(pb, extra_size2); /* additional data len */
398
        put_le16(pb, n + 1); /* stream number */
399
        put_le32(pb, 0); /* ??? */
400
401
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
402
            /* WAVEFORMATEX header */
403
            int wavsize = put_wav_header(pb, enc);
404 982e53fe Calcium
            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)) {
405
                wavsize += 2;
406
                put_le16(pb, 0);
407
            }
408 542993b0 Konstantin Andreyev
409
            if (wavsize < 0)
410
                return -1;
411
            if (wavsize != extra_size) {
412
                cur_pos = url_ftell(pb);
413
                url_fseek(pb, es_pos, SEEK_SET);
414
                put_le32(pb, wavsize); /* wav header len */
415
                url_fseek(pb, cur_pos, SEEK_SET);
416
            }
417 982e53fe Calcium
            /* ERROR Correction */
418
            if ((er_spr != NULL) && (er_spr_len != 0))
419
                put_buffer(pb, er_spr, er_spr_len);
420 542993b0 Konstantin Andreyev
        } else {
421
            put_le32(pb, enc->width);
422
            put_le32(pb, enc->height);
423
            put_byte(pb, 2); /* ??? */
424 982e53fe Calcium
            put_le16(pb, 40 + enc->extradata_size); /* size */
425 542993b0 Konstantin Andreyev
426
            /* BITMAPINFOHEADER header */
427
            put_bmp_header(pb, enc, codec_bmp_tags, 1);
428
        }
429
        end_header(pb, hpos);
430
    }
431
432
    /* media comments */
433
434
    hpos = put_header(pb, &codec_comment_header);
435
    put_guid(pb, &codec_comment1_header);
436
    put_le32(pb, s->nb_streams);
437
    for(n=0;n<s->nb_streams;n++) {
438
        AVCodec *p;
439
440 01f4895c Michael Niedermayer
        enc = s->streams[n]->codec;
441 542993b0 Konstantin Andreyev
        p = avcodec_find_encoder(enc->codec_id);
442
443
        put_le16(pb, asf->streams[n].num);
444
        put_str16(pb, p ? p->name : enc->codec_name);
445
        put_le16(pb, 0); /* no parameters */
446 115329f1 Diego Biurrun
447
448 542993b0 Konstantin Andreyev
        /* id */
449
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
450
            put_le16(pb, 2);
451
            if(!enc->codec_tag)
452
                enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
453
            if(!enc->codec_tag)
454
                return -1;
455
            put_le16(pb, enc->codec_tag);
456
        } else {
457
            put_le16(pb, 4);
458
            if(!enc->codec_tag)
459
                enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id);
460
            if(!enc->codec_tag)
461
                return -1;
462
            put_le32(pb, enc->codec_tag);
463
        }
464
    }
465
    end_header(pb, hpos);
466
467
    /* patch the header size fields */
468
469
    cur_pos = url_ftell(pb);
470
    header_size = cur_pos - header_offset;
471
    if (asf->is_streamed) {
472
        header_size += 8 + 30 + 50;
473
474
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
475
        put_le16(pb, header_size);
476
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
477
        put_le16(pb, header_size);
478
479
        header_size -= 8 + 30 + 50;
480
    }
481
    header_size += 24 + 6;
482
    url_fseek(pb, header_offset - 14, SEEK_SET);
483
    put_le64(pb, header_size);
484
    url_fseek(pb, cur_pos, SEEK_SET);
485
486
    /* movie chunk, followed by packets of packet_size */
487
    asf->data_offset = cur_pos;
488
    put_guid(pb, &data_header);
489
    put_le64(pb, data_chunk_size);
490
    put_guid(pb, &my_guid);
491
    put_le64(pb, asf->nb_packets); /* nb packets */
492
    put_byte(pb, 1); /* ??? */
493
    put_byte(pb, 1); /* ??? */
494
    return 0;
495
}
496
497
static int asf_write_header(AVFormatContext *s)
498
{
499
    ASFContext *asf = s->priv_data;
500
501
    asf->packet_size = PACKET_SIZE;
502
    asf->nb_packets = 0;
503 115329f1 Diego Biurrun
504 982e53fe Calcium
    asf->last_indexed_pts = 0;
505
    asf->index_ptr = (ASFIndex*)av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
506
    asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
507
    asf->nb_index_count = 0;
508
    asf->maximum_packet = 0;
509 542993b0 Konstantin Andreyev
510
    if (asf_write_header1(s, 0, 50) < 0) {
511
        //av_free(asf);
512
        return -1;
513
    }
514
515
    put_flush_packet(&s->pb);
516
517 615b92fd Konstantin Andreyev
    asf->packet_nb_payloads = 0;
518
    asf->prev_packet_sent_time = 0;
519 542993b0 Konstantin Andreyev
    asf->packet_timestamp_start = -1;
520
    asf->packet_timestamp_end = -1;
521
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
522
                  NULL, NULL, NULL, NULL);
523
524
    return 0;
525
}
526
527
static int asf_write_stream_header(AVFormatContext *s)
528
{
529
    ASFContext *asf = s->priv_data;
530
531
    asf->is_streamed = 1;
532
533
    return asf_write_header(s);
534
}
535
536 615b92fd Konstantin Andreyev
static int put_payload_parsing_info(
537
                                AVFormatContext *s,
538 115329f1 Diego Biurrun
                                unsigned int    sendtime,
539 615b92fd Konstantin Andreyev
                                unsigned int    duration,
540 115329f1 Diego Biurrun
                                int             nb_payloads,
541 615b92fd Konstantin Andreyev
                                int             padsize
542
            )
543 542993b0 Konstantin Andreyev
{
544
    ASFContext *asf = s->priv_data;
545
    ByteIOContext *pb = &s->pb;
546 615b92fd Konstantin Andreyev
    int ppi_size, i;
547
    unsigned char *start_ppi_ptr = pb->buf_ptr;
548
549
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
550 115329f1 Diego Biurrun
551 615b92fd Konstantin Andreyev
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
552
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
553
        put_byte(pb, 0x0);
554 542993b0 Konstantin Andreyev
    }
555
556 615b92fd Konstantin Andreyev
    if (asf->multi_payloads_present)
557
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
558 542993b0 Konstantin Andreyev
559
    if (padsize > 0) {
560
        if (padsize < 256)
561 615b92fd Konstantin Andreyev
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
562 542993b0 Konstantin Andreyev
        else
563 615b92fd Konstantin Andreyev
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
564 542993b0 Konstantin Andreyev
    }
565 615b92fd Konstantin Andreyev
    put_byte(pb, iLengthTypeFlags);
566
567
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
568
569
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
570 542993b0 Konstantin Andreyev
        put_le16(pb, padsize - 2);
571 615b92fd Konstantin Andreyev
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
572 542993b0 Konstantin Andreyev
        put_byte(pb, padsize - 1);
573 615b92fd Konstantin Andreyev
574
    put_le32(pb, sendtime);
575 542993b0 Konstantin Andreyev
    put_le16(pb, duration);
576 615b92fd Konstantin Andreyev
    if (asf->multi_payloads_present)
577
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
578
579
    ppi_size = pb->buf_ptr - start_ppi_ptr;
580 542993b0 Konstantin Andreyev
581 615b92fd Konstantin Andreyev
    return ppi_size;
582 542993b0 Konstantin Andreyev
}
583
584
static void flush_packet(AVFormatContext *s)
585
{
586
    ASFContext *asf = s->priv_data;
587 615b92fd Konstantin Andreyev
    int packet_hdr_size, packet_filled_size;
588 542993b0 Konstantin Andreyev
589 615b92fd Konstantin Andreyev
    if (asf->is_streamed) {
590
        put_chunk(s, 0x4424, asf->packet_size, 0);
591
    }
592
593
    packet_hdr_size = put_payload_parsing_info(
594
                            s,
595
                            asf->packet_timestamp_start,
596
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
597
                            asf->packet_nb_payloads,
598
                            asf->packet_size_left
599
                        );
600 542993b0 Konstantin Andreyev
601 615b92fd Konstantin Andreyev
    packet_filled_size = PACKET_SIZE - packet_hdr_size - asf->packet_size_left;
602
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
603 542993b0 Konstantin Andreyev
604 615b92fd Konstantin Andreyev
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
605 542993b0 Konstantin Andreyev
606
    put_flush_packet(&s->pb);
607
    asf->nb_packets++;
608 615b92fd Konstantin Andreyev
    asf->packet_nb_payloads = 0;
609
    asf->prev_packet_sent_time = asf->packet_timestamp_start;
610 542993b0 Konstantin Andreyev
    asf->packet_timestamp_start = -1;
611
    asf->packet_timestamp_end = -1;
612
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
613
                  NULL, NULL, NULL, NULL);
614
}
615
616 615b92fd Konstantin Andreyev
static void put_payload_header(
617
                                AVFormatContext *s,
618
                                ASFStream       *stream,
619
                                int             presentation_time,
620
                                int             m_obj_size,
621
                                int             m_obj_offset,
622 3c895fc0 Michael Niedermayer
                                int             payload_len,
623
                                int             flags
624 615b92fd Konstantin Andreyev
            )
625 542993b0 Konstantin Andreyev
{
626
    ASFContext *asf = s->priv_data;
627
    ByteIOContext *pb = &asf->pb;
628
    int val;
629 115329f1 Diego Biurrun
630 542993b0 Konstantin Andreyev
    val = stream->num;
631 3c895fc0 Michael Niedermayer
    if (flags & PKT_FLAG_KEY)
632 615b92fd Konstantin Andreyev
        val |= ASF_PL_FLAG_KEY_FRAME;
633 542993b0 Konstantin Andreyev
    put_byte(pb, val);
634 115329f1 Diego Biurrun
635 615b92fd Konstantin Andreyev
    put_byte(pb, stream->seq);  //Media object number
636
    put_le32(pb, m_obj_offset); //Offset Into Media Object
637 115329f1 Diego Biurrun
638 615b92fd Konstantin Andreyev
    // Replicated Data shall be at least 8 bytes long.
639 115329f1 Diego Biurrun
    // The first 4 bytes of data shall contain the
640 615b92fd Konstantin Andreyev
    // Size of the Media Object that the payload belongs to.
641 115329f1 Diego Biurrun
    // The next 4 bytes of data shall contain the
642 615b92fd Konstantin Andreyev
    // Presentation Time for the media object that the payload belongs to.
643
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
644
645
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
646
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
647 115329f1 Diego Biurrun
648 615b92fd Konstantin Andreyev
    if (asf->multi_payloads_present){
649
        put_le16(pb, payload_len);   //payload length
650
    }
651 542993b0 Konstantin Andreyev
}
652
653 615b92fd Konstantin Andreyev
static void put_frame(
654
                    AVFormatContext *s,
655
                    ASFStream       *stream,
656 bb270c08 Diego Biurrun
                    int             timestamp,
657 615b92fd Konstantin Andreyev
                    const uint8_t   *buf,
658 bb270c08 Diego Biurrun
                    int             m_obj_size,
659 3c895fc0 Michael Niedermayer
                    int             flags
660 bb270c08 Diego Biurrun
                )
661 542993b0 Konstantin Andreyev
{
662
    ASFContext *asf = s->priv_data;
663 615b92fd Konstantin Andreyev
    int m_obj_offset, payload_len, frag_len1;
664
665
    m_obj_offset = 0;
666
    while (m_obj_offset < m_obj_size) {
667
        payload_len = m_obj_size - m_obj_offset;
668
        if (asf->packet_timestamp_start == -1) {
669
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
670 115329f1 Diego Biurrun
671 615b92fd Konstantin Andreyev
            if (asf->multi_payloads_present){
672
                asf->packet_size_left = PACKET_SIZE; //For debug
673
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE - 1;
674
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
675
            }
676
            else {
677
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE;
678
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
679
            }
680
            if (asf->prev_packet_sent_time > timestamp)
681
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
682
            else
683
                asf->packet_timestamp_start = timestamp;
684
        }
685
        else {
686
            // multi payloads
687
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS;
688 542993b0 Konstantin Andreyev
689 615b92fd Konstantin Andreyev
            if (asf->prev_packet_sent_time > timestamp)
690
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
691
            else if (asf->packet_timestamp_start >= timestamp)
692
                asf->packet_timestamp_start = timestamp;
693
        }
694 542993b0 Konstantin Andreyev
        if (frag_len1 > 0) {
695 615b92fd Konstantin Andreyev
            if (payload_len > frag_len1)
696
                payload_len = frag_len1;
697
            else if (payload_len == (frag_len1 - 1))
698
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
699 115329f1 Diego Biurrun
700 3c895fc0 Michael Niedermayer
            put_payload_header(s, stream, timestamp+preroll_time, m_obj_size, m_obj_offset, payload_len, flags);
701 615b92fd Konstantin Andreyev
            put_buffer(&asf->pb, buf, payload_len);
702
703
            if (asf->multi_payloads_present)
704
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
705
            else
706
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
707 542993b0 Konstantin Andreyev
            asf->packet_timestamp_end = timestamp;
708 115329f1 Diego Biurrun
709 615b92fd Konstantin Andreyev
            asf->packet_nb_payloads++;
710 542993b0 Konstantin Andreyev
        } else {
711 615b92fd Konstantin Andreyev
            payload_len = 0;
712 542993b0 Konstantin Andreyev
        }
713 615b92fd Konstantin Andreyev
        m_obj_offset += payload_len;
714
        buf += payload_len;
715
716
        if (!asf->multi_payloads_present)
717
            flush_packet(s);
718
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + 1))
719 542993b0 Konstantin Andreyev
            flush_packet(s);
720
    }
721
    stream->seq++;
722
}
723
724 e928649b Michael Niedermayer
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
725 542993b0 Konstantin Andreyev
{
726
    ASFContext *asf = s->priv_data;
727
    ASFStream *stream;
728
    int64_t duration;
729
    AVCodecContext *codec;
730 115329f1 Diego Biurrun
    int64_t packet_st,pts;
731 982e53fe Calcium
    int start_sec,i;
732 542993b0 Konstantin Andreyev
733 01f4895c Michael Niedermayer
    codec = s->streams[pkt->stream_index]->codec;
734 e928649b Michael Niedermayer
    stream = &asf->streams[pkt->stream_index];
735 542993b0 Konstantin Andreyev
736 982e53fe Calcium
    //XXX /FIXME use duration from AVPacket (quick hack by)
737
    pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
738
    if (pts == AV_NOPTS_VALUE) {
739
        if (codec->codec_type == CODEC_TYPE_AUDIO) {
740
            duration = (codec->frame_number * (int64_t)codec->frame_size * int64_t_C(10000000)) /
741
                codec->sample_rate;
742
        } else {
743 c0df9d75 Michael Niedermayer
            duration = av_rescale(codec->frame_number * (int64_t)codec->time_base.num, 10000000, codec->time_base.den);
744 982e53fe Calcium
        }
745 542993b0 Konstantin Andreyev
    } else {
746 982e53fe Calcium
        duration = pts * 10000;
747 542993b0 Konstantin Andreyev
    }
748
    if (duration > asf->duration)
749
        asf->duration = duration;
750
751 982e53fe Calcium
    packet_st = asf->nb_packets;
752 3c895fc0 Michael Niedermayer
    put_frame(s, stream, pkt->pts, pkt->data, pkt->size, pkt->flags);
753 982e53fe Calcium
754
    /* check index */
755
    if ((!asf->is_streamed) && (codec->codec_type == CODEC_TYPE_VIDEO) && (pkt->flags & PKT_FLAG_KEY)) {
756
        start_sec = (int)(duration / int64_t_C(10000000));
757
        if (start_sec != (int)(asf->last_indexed_pts / int64_t_C(10000000))) {
758
            for(i=asf->nb_index_count;i<start_sec;i++) {
759
                if (i>=asf->nb_index_memory_alloc) {
760
                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
761
                    asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
762
                }
763
                // store
764
                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
765
                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets-packet_st);
766
                if (asf->maximum_packet < (uint16_t)(asf->nb_packets-packet_st))
767
                    asf->maximum_packet = (uint16_t)(asf->nb_packets-packet_st);
768
            }
769
            asf->nb_index_count = start_sec;
770
            asf->last_indexed_pts = duration;
771
        }
772
    }
773
    return 0;
774
}
775
776
//
777
static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
778
{
779
    ByteIOContext *pb = &s->pb;
780
    int i;
781
782
    put_guid(pb, &simple_index_header);
783
    put_le64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
784
    put_guid(pb, &my_guid);
785
    put_le64(pb, ASF_INDEXED_INTERVAL);
786
    put_le32(pb, max);
787
    put_le32(pb, count);
788
    for(i=0; i<count; i++) {
789
        put_le32(pb, index[i].packet_number);
790
        put_le16(pb, index[i].packet_count);
791
    }
792
793 542993b0 Konstantin Andreyev
    return 0;
794
}
795
796
static int asf_write_trailer(AVFormatContext *s)
797
{
798
    ASFContext *asf = s->priv_data;
799 982e53fe Calcium
    int64_t file_size,data_size;
800 542993b0 Konstantin Andreyev
801
    /* flush the current packet */
802
    if (asf->pb.buf_ptr > asf->pb.buffer)
803
        flush_packet(s);
804
805 982e53fe Calcium
    /* write index */
806
    data_size = url_ftell(&s->pb);
807
    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
808
        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
809
    }
810
    put_flush_packet(&s->pb);
811
812 542993b0 Konstantin Andreyev
    if (asf->is_streamed) {
813
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
814
    } else {
815
        /* rewrite an updated header */
816
        file_size = url_ftell(&s->pb);
817
        url_fseek(&s->pb, 0, SEEK_SET);
818 982e53fe Calcium
        asf_write_header1(s, file_size, data_size - asf->data_offset);
819 542993b0 Konstantin Andreyev
    }
820
821
    put_flush_packet(&s->pb);
822 982e53fe Calcium
    av_free(asf->index_ptr);
823 542993b0 Konstantin Andreyev
    return 0;
824
}
825
826
AVOutputFormat asf_oformat = {
827
    "asf",
828
    "asf format",
829
    "video/x-ms-asf",
830 87094371 Michael Niedermayer
    "asf,wmv,wma",
831 542993b0 Konstantin Andreyev
    sizeof(ASFContext),
832
#ifdef CONFIG_MP3LAME
833
    CODEC_ID_MP3,
834
#else
835
    CODEC_ID_MP2,
836
#endif
837
    CODEC_ID_MSMPEG4V3,
838
    asf_write_header,
839
    asf_write_packet,
840
    asf_write_trailer,
841 982e53fe Calcium
    .flags = AVFMT_GLOBALHEADER,
842 542993b0 Konstantin Andreyev
};
843
844
AVOutputFormat asf_stream_oformat = {
845
    "asf_stream",
846
    "asf format",
847
    "video/x-ms-asf",
848 87094371 Michael Niedermayer
    "asf,wmv,wma",
849 542993b0 Konstantin Andreyev
    sizeof(ASFContext),
850
#ifdef CONFIG_MP3LAME
851
    CODEC_ID_MP3,
852
#else
853
    CODEC_ID_MP2,
854
#endif
855
    CODEC_ID_MSMPEG4V3,
856
    asf_write_stream_header,
857
    asf_write_packet,
858
    asf_write_trailer,
859 982e53fe Calcium
    .flags = AVFMT_GLOBALHEADER,
860 542993b0 Konstantin Andreyev
};
861 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS