Statistics
| Branch: | Revision:

ffmpeg / libavformat / asf-enc.c @ 649b918c

History | View | Annotate | Download (25.3 KB)

1
/*
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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20
#include "avi.h"
21
#include "asf.h"
22

    
23
#undef NDEBUG
24
#include <assert.h>
25

    
26
#ifdef CONFIG_ENCODERS
27

    
28
#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
29
#define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
30
                                        ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
31
                                        ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
32
                                        )
33

    
34
#if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
35
#   define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
36
#else
37
#   define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
38
#endif
39

    
40
#define ASF_PPI_PROPERTY_FLAGS (\
41
                        ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
42
                        ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
43
                        ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
44
                        ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
45
                        )
46

    
47
#define ASF_PPI_LENGTH_TYPE_FLAGS 0
48

    
49
#define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
50

    
51
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
52
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 1
53
#endif
54
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
55
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 2
56
#endif
57
#if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
58
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 4
59
#endif
60
#ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
61
#   define ASF_PPI_SEQUENCE_FIELD_SIZE 0
62
#endif
63

    
64

    
65
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
66
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
67
#endif
68
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
69
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
70
#endif
71
#if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
72
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
73
#endif
74
#ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
75
#   define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
76
#endif
77

    
78
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
79
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
80
#endif
81
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
82
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
83
#endif
84
#if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
85
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
86
#endif
87
#ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
88
#   define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
89
#endif
90

    
91
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
92
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
93
#endif
94
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
95
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
96
#endif
97
#if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
98
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
99
#endif
100
#ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
101
#   define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
102
#endif
103

    
104
#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))
105
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
106
#endif
107
#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))
108
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
109
#endif
110
#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))
111
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
112
#endif
113
#ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
114
#   define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
115
#endif
116

    
117
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
118
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
119
#endif
120
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
121
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
122
#endif
123
#if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
124
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
125
#endif
126
#ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
127
#   define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
128
#endif
129

    
130
#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
131
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
132
#endif
133
#if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
134
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
135
#endif
136
#ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
137
#   define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
138
#endif
139

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

    
152

    
153
// Replicated Data shall be at least 8 bytes long.
154
#define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
155

    
156
#define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
157
                        1 + /*Stream Number*/ \
158
                        ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
159
                        ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
160
                        ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
161
                        ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
162
                        )
163
                        
164
#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
165
                        1 + /*Stream Number*/ \
166
                        ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
167
                        ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
168
                        ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
169
                        ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
170
                        ASF_PAYLOAD_LENGTH_FIELD_SIZE \
171
                        )
172

    
173
#define SINGLE_PAYLOAD_DATA_LENGTH (\
174
                        PACKET_SIZE - \
175
                        PACKET_HEADER_MIN_SIZE - \
176
                        PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
177
                        )
178

    
179
#define MULTI_PAYLOAD_CONSTANT (\
180
                        PACKET_SIZE - \
181
                        PACKET_HEADER_MIN_SIZE - \
182
                        1 - /*Payload Flags*/ \
183
                        2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
184
                        )
185

    
186
static int preroll_time = 2000;
187

    
188
static void put_guid(ByteIOContext *s, const GUID *g)
189
{
190
    int i;
191

    
192
    put_le32(s, g->v1);
193
    put_le16(s, g->v2);
194
    put_le16(s, g->v3);
195
    for(i=0;i<8;i++)
196
        put_byte(s, g->v4[i]);
197
}
198

    
199
static void put_str16(ByteIOContext *s, const char *tag)
200
{
201
    int c;
202

    
203
    put_le16(s,strlen(tag) + 1);
204
    for(;;) {
205
        c = (uint8_t)*tag++;
206
        put_le16(s, c);
207
        if (c == '\0')
208
            break;
209
    }
210
}
211

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

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

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

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

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

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

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

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

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

    
266
    t = ti * int64_t_C(10000000);
267
    t += int64_t_C(116444736000000000);
268
    return t;
269
}
270

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

    
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, asf->duration); /* end time stamp (in 100ns units) */
312
    put_le64(pb, asf->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
        put_le16(pb, 2 * (strlen(s->title) + 1));
332
        put_le16(pb, 2 * (strlen(s->author) + 1));
333
        put_le16(pb, 2 * (strlen(s->copyright) + 1));
334
        put_le16(pb, 2 * (strlen(s->comment) + 1));
335
        put_le16(pb, 0);
336
        put_str16_nolen(pb, s->title);
337
        put_str16_nolen(pb, s->author);
338
        put_str16_nolen(pb, s->copyright);
339
        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
        switch(enc->codec_type) {
353
        case CODEC_TYPE_AUDIO:
354
            wav_extra_size = 0;
355
            extra_size = 18 + wav_extra_size;
356
            extra_size2 = 0;
357
            break;
358
        default:
359
        case CODEC_TYPE_VIDEO:
360
            wav_extra_size = 0;
361
            extra_size = 0x33;
362
            extra_size2 = 0;
363
            break;
364
        }
365

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

    
381
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
382
            /* WAVEFORMATEX header */
383
            int wavsize = put_wav_header(pb, enc);
384

    
385
            if (wavsize < 0)
386
                return -1;
387
            if (wavsize != extra_size) {
388
                cur_pos = url_ftell(pb);
389
                url_fseek(pb, es_pos, SEEK_SET);
390
                put_le32(pb, wavsize); /* wav header len */
391
                url_fseek(pb, cur_pos, SEEK_SET);
392
            }
393
        } else {
394
            put_le32(pb, enc->width);
395
            put_le32(pb, enc->height);
396
            put_byte(pb, 2); /* ??? */
397
            put_le16(pb, 40); /* size */
398

    
399
            /* BITMAPINFOHEADER header */
400
            put_bmp_header(pb, enc, codec_bmp_tags, 1);
401
        }
402
        end_header(pb, hpos);
403
    }
404

    
405
    /* media comments */
406

    
407
    hpos = put_header(pb, &codec_comment_header);
408
    put_guid(pb, &codec_comment1_header);
409
    put_le32(pb, s->nb_streams);
410
    for(n=0;n<s->nb_streams;n++) {
411
        AVCodec *p;
412

    
413
        enc = &s->streams[n]->codec;
414
        p = avcodec_find_encoder(enc->codec_id);
415

    
416
        put_le16(pb, asf->streams[n].num);
417
        put_str16(pb, p ? p->name : enc->codec_name);
418
        put_le16(pb, 0); /* no parameters */
419
        
420
        
421
        /* id */
422
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
423
            put_le16(pb, 2);
424
            if(!enc->codec_tag)
425
                enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id);
426
            if(!enc->codec_tag)
427
                return -1;
428
            put_le16(pb, enc->codec_tag);
429
        } else {
430
            put_le16(pb, 4);
431
            if(!enc->codec_tag)
432
                enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id);
433
            if(!enc->codec_tag)
434
                return -1;
435
            put_le32(pb, enc->codec_tag);
436
        }
437
    }
438
    end_header(pb, hpos);
439

    
440
    /* patch the header size fields */
441

    
442
    cur_pos = url_ftell(pb);
443
    header_size = cur_pos - header_offset;
444
    if (asf->is_streamed) {
445
        header_size += 8 + 30 + 50;
446

    
447
        url_fseek(pb, header_offset - 10 - 30, SEEK_SET);
448
        put_le16(pb, header_size);
449
        url_fseek(pb, header_offset - 2 - 30, SEEK_SET);
450
        put_le16(pb, header_size);
451

    
452
        header_size -= 8 + 30 + 50;
453
    }
454
    header_size += 24 + 6;
455
    url_fseek(pb, header_offset - 14, SEEK_SET);
456
    put_le64(pb, header_size);
457
    url_fseek(pb, cur_pos, SEEK_SET);
458

    
459
    /* movie chunk, followed by packets of packet_size */
460
    asf->data_offset = cur_pos;
461
    put_guid(pb, &data_header);
462
    put_le64(pb, data_chunk_size);
463
    put_guid(pb, &my_guid);
464
    put_le64(pb, asf->nb_packets); /* nb packets */
465
    put_byte(pb, 1); /* ??? */
466
    put_byte(pb, 1); /* ??? */
467
    return 0;
468
}
469

    
470
static int asf_write_header(AVFormatContext *s)
471
{
472
    ASFContext *asf = s->priv_data;
473

    
474
    asf->packet_size = PACKET_SIZE;
475
    asf->nb_packets = 0;
476

    
477
    if (asf_write_header1(s, 0, 50) < 0) {
478
        //av_free(asf);
479
        return -1;
480
    }
481

    
482
    put_flush_packet(&s->pb);
483

    
484
    asf->packet_nb_payloads = 0;
485
    asf->prev_packet_sent_time = 0;
486
    asf->packet_timestamp_start = -1;
487
    asf->packet_timestamp_end = -1;
488
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
489
                  NULL, NULL, NULL, NULL);
490

    
491
    return 0;
492
}
493

    
494
static int asf_write_stream_header(AVFormatContext *s)
495
{
496
    ASFContext *asf = s->priv_data;
497

    
498
    asf->is_streamed = 1;
499

    
500
    return asf_write_header(s);
501
}
502

    
503
static int put_payload_parsing_info(
504
                                AVFormatContext *s,
505
                                unsigned int    sendtime, 
506
                                unsigned int    duration,
507
                                int             nb_payloads, 
508
                                int             padsize
509
            )
510
{
511
    ASFContext *asf = s->priv_data;
512
    ByteIOContext *pb = &s->pb;
513
    int ppi_size, i;
514
    unsigned char *start_ppi_ptr = pb->buf_ptr;
515

    
516
    int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
517
  
518
    put_byte(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
519
    for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
520
        put_byte(pb, 0x0);
521
    }
522

    
523
    if (asf->multi_payloads_present)
524
        iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
525

    
526
    if (padsize > 0) {
527
        if (padsize < 256)
528
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
529
        else
530
            iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
531
    }
532
    put_byte(pb, iLengthTypeFlags);
533

    
534
    put_byte(pb, ASF_PPI_PROPERTY_FLAGS);
535

    
536
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
537
        put_le16(pb, padsize - 2);
538
    if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
539
        put_byte(pb, padsize - 1);
540

    
541
    put_le32(pb, sendtime);
542
    put_le16(pb, duration);
543
    if (asf->multi_payloads_present)
544
        put_byte(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
545

    
546
    ppi_size = pb->buf_ptr - start_ppi_ptr;
547

    
548
    return ppi_size;
549
}
550

    
551
static void flush_packet(AVFormatContext *s)
552
{
553
    ASFContext *asf = s->priv_data;
554
    int packet_hdr_size, packet_filled_size;
555

    
556
    if (asf->is_streamed) {
557
        put_chunk(s, 0x4424, asf->packet_size, 0);
558
    }
559

    
560
    packet_hdr_size = put_payload_parsing_info(
561
                            s,
562
                            asf->packet_timestamp_start,
563
                            asf->packet_timestamp_end - asf->packet_timestamp_start,
564
                            asf->packet_nb_payloads,
565
                            asf->packet_size_left
566
                        );
567

    
568
    packet_filled_size = PACKET_SIZE - packet_hdr_size - asf->packet_size_left;
569
    memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
570

    
571
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - packet_hdr_size);
572

    
573
    put_flush_packet(&s->pb);
574
    asf->nb_packets++;
575
    asf->packet_nb_payloads = 0;
576
    asf->prev_packet_sent_time = asf->packet_timestamp_start;
577
    asf->packet_timestamp_start = -1;
578
    asf->packet_timestamp_end = -1;
579
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1,
580
                  NULL, NULL, NULL, NULL);
581
}
582

    
583
static void put_payload_header(
584
                                AVFormatContext *s,
585
                                ASFStream       *stream,
586
                                int             presentation_time,
587
                                int             m_obj_size,
588
                                int             m_obj_offset,
589
                                int             payload_len,
590
                                int             flags
591
            )
592
{
593
    ASFContext *asf = s->priv_data;
594
    ByteIOContext *pb = &asf->pb;
595
    int val;
596
    
597
    val = stream->num;
598
    if (flags & PKT_FLAG_KEY)
599
        val |= ASF_PL_FLAG_KEY_FRAME;
600
    put_byte(pb, val);
601
        
602
    put_byte(pb, stream->seq);  //Media object number
603
    put_le32(pb, m_obj_offset); //Offset Into Media Object
604
         
605
    // Replicated Data shall be at least 8 bytes long.
606
    // The first 4 bytes of data shall contain the 
607
    // Size of the Media Object that the payload belongs to.
608
    // The next 4 bytes of data shall contain the 
609
    // Presentation Time for the media object that the payload belongs to.
610
    put_byte(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
611

    
612
    put_le32(pb, m_obj_size);       //Replicated Data - Media Object Size
613
    put_le32(pb, presentation_time);//Replicated Data - Presentation Time
614
    
615
    if (asf->multi_payloads_present){
616
        put_le16(pb, payload_len);   //payload length
617
    }
618
}
619

    
620
static void put_frame(
621
                    AVFormatContext *s,
622
                    ASFStream       *stream,
623
                    int             timestamp,
624
                    const uint8_t   *buf,
625
                    int             m_obj_size,
626
                    int             flags
627
                )
628
{
629
    ASFContext *asf = s->priv_data;
630
    int m_obj_offset, payload_len, frag_len1;
631

    
632
    m_obj_offset = 0;
633
    while (m_obj_offset < m_obj_size) {
634
        payload_len = m_obj_size - m_obj_offset;
635
        if (asf->packet_timestamp_start == -1) {
636
            asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
637
            
638
            if (asf->multi_payloads_present){
639
                asf->packet_size_left = PACKET_SIZE; //For debug
640
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE - 1;
641
                frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
642
            }
643
            else {
644
                asf->packet_size_left = PACKET_SIZE - PACKET_HEADER_MIN_SIZE;
645
                frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
646
            }
647
            if (asf->prev_packet_sent_time > timestamp)
648
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
649
            else
650
                asf->packet_timestamp_start = timestamp;
651
        }
652
        else {
653
            // multi payloads
654
            frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS;
655

    
656
            if (asf->prev_packet_sent_time > timestamp)
657
                asf->packet_timestamp_start = asf->prev_packet_sent_time;
658
            else if (asf->packet_timestamp_start >= timestamp)
659
                asf->packet_timestamp_start = timestamp;
660
        }
661
        if (frag_len1 > 0) {
662
            if (payload_len > frag_len1)
663
                payload_len = frag_len1;
664
            else if (payload_len == (frag_len1 - 1))
665
                payload_len = frag_len1 - 2;  //additional byte need to put padding length
666
            
667
            put_payload_header(s, stream, timestamp+preroll_time, m_obj_size, m_obj_offset, payload_len, flags);
668
            put_buffer(&asf->pb, buf, payload_len);
669

    
670
            if (asf->multi_payloads_present)
671
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
672
            else
673
                asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
674
            asf->packet_timestamp_end = timestamp;
675
            
676
            asf->packet_nb_payloads++;
677
        } else {
678
            payload_len = 0;
679
        }
680
        m_obj_offset += payload_len;
681
        buf += payload_len;
682

    
683
        if (!asf->multi_payloads_present)
684
            flush_packet(s);
685
        else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + 1))
686
            flush_packet(s);
687
    }
688
    stream->seq++;
689
}
690

    
691
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
692
{
693
    ASFContext *asf = s->priv_data;
694
    ASFStream *stream;
695
    int64_t duration;
696
    AVCodecContext *codec;
697

    
698
    codec = &s->streams[pkt->stream_index]->codec;
699
    stream = &asf->streams[pkt->stream_index];
700

    
701
    //XXX /FIXME use duration from AVPacket
702
    if (codec->codec_type == CODEC_TYPE_AUDIO) {
703
        duration = (codec->frame_number * (int64_t)codec->frame_size * int64_t_C(10000000)) /
704
            codec->sample_rate;
705
    } else {
706
        duration = av_rescale(codec->frame_number * (int64_t)codec->frame_rate_base, 10000000, codec->frame_rate);
707
    }
708
    if (duration > asf->duration)
709
        asf->duration = duration;
710

    
711
    put_frame(s, stream, pkt->pts, pkt->data, pkt->size, pkt->flags);
712
    return 0;
713
}
714

    
715
static int asf_write_trailer(AVFormatContext *s)
716
{
717
    ASFContext *asf = s->priv_data;
718
    int64_t file_size;
719

    
720
    /* flush the current packet */
721
    if (asf->pb.buf_ptr > asf->pb.buffer)
722
        flush_packet(s);
723

    
724
    if (asf->is_streamed) {
725
        put_chunk(s, 0x4524, 0, 0); /* end of stream */
726
    } else {
727
        /* rewrite an updated header */
728
        file_size = url_ftell(&s->pb);
729
        url_fseek(&s->pb, 0, SEEK_SET);
730
        asf_write_header1(s, file_size, file_size - asf->data_offset);
731
    }
732

    
733
    put_flush_packet(&s->pb);
734
    return 0;
735
}
736

    
737
AVOutputFormat asf_oformat = {
738
    "asf",
739
    "asf format",
740
    "video/x-ms-asf",
741
    "asf,wmv,wma",
742
    sizeof(ASFContext),
743
#ifdef CONFIG_MP3LAME
744
    CODEC_ID_MP3,
745
#else
746
    CODEC_ID_MP2,
747
#endif
748
    CODEC_ID_MSMPEG4V3,
749
    asf_write_header,
750
    asf_write_packet,
751
    asf_write_trailer,
752
};
753

    
754
AVOutputFormat asf_stream_oformat = {
755
    "asf_stream",
756
    "asf format",
757
    "video/x-ms-asf",
758
    "asf,wmv,wma",
759
    sizeof(ASFContext),
760
#ifdef CONFIG_MP3LAME
761
    CODEC_ID_MP3,
762
#else
763
    CODEC_ID_MP2,
764
#endif
765
    CODEC_ID_MSMPEG4V3,
766
    asf_write_stream_header,
767
    asf_write_packet,
768
    asf_write_trailer,
769
};
770
#endif //CONFIG_ENCODERS