Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ 546031ee

History | View | Annotate | Download (22.7 KB)

1
/*
2
 * AVI encoder.
3
 * Copyright (c) 2000 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

    
22
/*
23
 * TODO: 
24
 *  - fill all fields if non streamed (nb_frames for example)
25
 */
26

    
27
typedef struct AVIIentry {
28
    unsigned int flags, pos, len;
29
} AVIIentry;
30

    
31
#define AVI_INDEX_CLUSTER_SIZE 16384
32

    
33
typedef struct AVIIndex {
34
    offset_t    indx_start;
35
    int         entry;
36
    int         ents_allocated;
37
    AVIIentry** cluster;
38
} AVIIndex;
39

    
40
typedef struct {
41
    offset_t riff_start, movi_list, odml_list;
42
    offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
43
    int audio_strm_length[MAX_STREAMS];
44
    int riff_id;
45

    
46
    AVIIndex indexes[MAX_STREAMS];
47
} AVIContext;
48

    
49
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id) 
50
{
51
    int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
52
    int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
53
    return &idx->cluster[cl][id];
54
}
55

    
56
offset_t start_tag(ByteIOContext *pb, const char *tag)
57
{
58
    put_tag(pb, tag);
59
    put_le32(pb, 0);
60
    return url_ftell(pb);
61
}
62

    
63
void end_tag(ByteIOContext *pb, offset_t start)
64
{
65
    offset_t pos;
66

    
67
    pos = url_ftell(pb);
68
    url_fseek(pb, start - 4, SEEK_SET);
69
    put_le32(pb, (uint32_t)(pos - start));
70
    url_fseek(pb, pos, SEEK_SET);
71
}
72

    
73
/* Note: when encoding, the first matching tag is used, so order is
74
   important if multiple tags possible for a given codec. */
75
const CodecTag codec_bmp_tags[] = {
76
    { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
77
    { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
78
    { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
79

    
80
    /* added based on MPlayer */
81
    { CODEC_ID_H263I, MKTAG('i', '2', '6', '3') },
82
    { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
83
    { CODEC_ID_H263P, MKTAG('h', '2', '6', '3') },
84
    { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
85

    
86
    { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
87
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
88
    { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), .invalid_asf = 1 },
89
    { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
90
    { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
91
    { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), .invalid_asf = 1 },
92
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), .invalid_asf = 1 },
93
    { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
94
    { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
95
    { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
96
    { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
97

    
98
    /* added based on MPlayer */
99
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
100
    { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', '1') },
101
    { CODEC_ID_MPEG4, MKTAG('X', 'v', 'i', 'D') },
102
    { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
103
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
104
    { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
105

    
106
    { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
107
    { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '3'), .invalid_asf = 1 },
108
    { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, 
109

    
110
    /* added based on MPlayer */
111
    { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') }, 
112
    { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', 'g', '3') }, 
113
    { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', '4', '3') }, 
114
    { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') }, 
115
    { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '5') }, 
116
    { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') }, 
117
    { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '6') }, 
118
    { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') }, 
119
    { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '4') }, 
120
    { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') }, 
121
    { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') }, 
122
    { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '1') }, 
123
    { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') }, 
124
    { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '0') }, 
125

    
126
    { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') }, 
127

    
128
    /* added based on MPlayer */
129
    { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
130
    { CODEC_ID_MSMPEG4V2, MKTAG('d', 'i', 'v', '2') },
131
    { CODEC_ID_MSMPEG4V2, MKTAG('m', 'p', '4', '2') },
132
 
133
    { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') }, 
134

    
135
    /* added based on MPlayer */
136
    { CODEC_ID_MSMPEG4V1, MKTAG('D', 'I', 'V', '4') }, 
137
    { CODEC_ID_MSMPEG4V1, MKTAG('d', 'i', 'v', '4') }, 
138
    { CODEC_ID_MSMPEG4V1, MKTAG('m', 'p', 'g', '4') }, 
139

    
140
    { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') }, 
141

    
142
    /* added based on MPlayer */
143
    { CODEC_ID_WMV1, MKTAG('w', 'm', 'v', '1') }, 
144

    
145
    { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') }, 
146
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') }, 
147
    { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') }, 
148
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') }, 
149
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') }, 
150
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
151
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') }, 
152
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') }, 
153
    { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') }, 
154
    { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
155
    { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
156
    { CODEC_ID_HUFFYUV, MKTAG('h', 'f', 'y', 'u') },
157
    { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
158
    { CODEC_ID_CYUV, MKTAG('c', 'y', 'u', 'v') },
159
    { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
160
    { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
161
    { CODEC_ID_INDEO3, MKTAG('i', 'v', '3', '1') },
162
    { CODEC_ID_INDEO3, MKTAG('i', 'v', '3', '2') },
163
    { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
164
    { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
165
    { 0, 0 },
166
};
167

    
168
unsigned int codec_get_tag(const CodecTag *tags, int id)
169
{
170
    while (tags->id != 0) {
171
        if (tags->id == id)
172
            return tags->tag;
173
        tags++;
174
    }
175
    return 0;
176
}
177

    
178
static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
179
{
180
    while (tags->id != 0) {
181
        if (!tags->invalid_asf && tags->id == id)
182
            return tags->tag;
183
        tags++;
184
    }
185
    return 0;
186
}
187

    
188
enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag)
189
{
190
    while (tags->id != 0) {
191
        if (tags->tag == tag)
192
            return tags->id;
193
        tags++;
194
    }
195
    return CODEC_ID_NONE;
196
}
197

    
198
unsigned int codec_get_bmp_tag(int id)
199
{
200
    return codec_get_tag(codec_bmp_tags, id);
201
}
202

    
203
/* BITMAPINFOHEADER header */
204
void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
205
{
206
    put_le32(pb, 40 + enc->extradata_size); /* size */
207
    put_le32(pb, enc->width);
208
    put_le32(pb, enc->height);
209
    put_le16(pb, 1); /* planes */
210
    
211
    put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
212
    /* compression type */
213
    put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id));
214
    put_le32(pb, enc->width * enc->height * 3);
215
    put_le32(pb, 0);
216
    put_le32(pb, 0);
217
    put_le32(pb, 0);
218
    put_le32(pb, 0);
219
    
220
    put_buffer(pb, enc->extradata, enc->extradata_size);
221

    
222
    if (enc->extradata_size & 1)
223
        put_byte(pb, 0);
224
}
225

    
226
static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
227
{
228
    switch(stream->codec_id) {
229
    case CODEC_ID_PCM_S16LE:
230
       *au_scale = *au_ssize = 2*stream->channels;
231
       *au_byterate = *au_ssize * stream->sample_rate;
232
        break;
233
    case CODEC_ID_PCM_U8:
234
    case CODEC_ID_PCM_ALAW:
235
    case CODEC_ID_PCM_MULAW:
236
        *au_scale = *au_ssize = stream->channels;
237
        *au_byterate = *au_ssize * stream->sample_rate;
238
        break;
239
    case CODEC_ID_MP2:
240
        *au_ssize = 1;
241
        *au_scale = 1;
242
        *au_byterate = stream->bit_rate / 8;
243
    case CODEC_ID_MP3LAME:
244
        *au_ssize = 1;
245
        *au_scale = 1;
246
        *au_byterate = stream->bit_rate / 8;    
247
    default:
248
        *au_ssize = 1;
249
        *au_scale = 1; 
250
        *au_byterate = stream->bit_rate / 8;
251
        break;
252
    }
253
}
254

    
255
static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb, 
256
                                   const char* riff_tag, const char* list_tag)
257
{
258
    offset_t loff;
259
    int i;
260
    
261
    avi->riff_id++;
262
    for (i=0; i<MAX_STREAMS; i++)
263
         avi->indexes[i].entry = 0;
264
    
265
    avi->riff_start = start_tag(pb, "RIFF");
266
    put_tag(pb, riff_tag);
267
    loff = start_tag(pb, "LIST");
268
    put_tag(pb, list_tag);
269
    return loff;
270
}
271

    
272
static unsigned char* avi_stream2fourcc(unsigned char* tag, int index, 
273
                                        enum CodecType type)
274
{
275
    tag[0] = '0';
276
    tag[1] = '0' + index;
277
    if (type == CODEC_TYPE_VIDEO) {
278
        tag[2] = 'd';
279
        tag[3] = 'c';
280
    } else {
281
        tag[2] = 'w';
282
        tag[3] = 'b';
283
    }
284
    tag[4] = '\0';
285
    return tag;
286
}
287

    
288
static int avi_write_header(AVFormatContext *s)
289
{
290
    AVIContext *avi = s->priv_data;
291
    ByteIOContext *pb = &s->pb;
292
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
293
    AVCodecContext *stream, *video_enc;
294
    offset_t list1, list2, strh, strf;
295

    
296
    /* header list */
297
    avi->riff_id = 0;
298
    list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
299

    
300
    /* avi header */
301
    put_tag(pb, "avih");
302
    put_le32(pb, 14 * 4);
303
    bitrate = 0;
304

    
305
    video_enc = NULL;
306
    for(n=0;n<s->nb_streams;n++) {
307
        stream = &s->streams[n]->codec;
308
        bitrate += stream->bit_rate;
309
        if (stream->codec_type == CODEC_TYPE_VIDEO)
310
            video_enc = stream;
311
    }
312
    
313
    nb_frames = 0;
314

    
315
    if(video_enc){
316
        put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
317
    } else {
318
        put_le32(pb, 0);
319
    }
320
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
321
    put_le32(pb, 0); /* padding */
322
    put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
323
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
324
    put_le32(pb, nb_frames); /* nb frames, filled later */
325
    put_le32(pb, 0); /* initial frame */
326
    put_le32(pb, s->nb_streams); /* nb streams */
327
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
328
    if(video_enc){    
329
    put_le32(pb, video_enc->width);
330
    put_le32(pb, video_enc->height);
331
    } else {
332
        put_le32(pb, 0);
333
        put_le32(pb, 0);
334
    }        
335
    put_le32(pb, 0); /* reserved */
336
    put_le32(pb, 0); /* reserved */
337
    put_le32(pb, 0); /* reserved */
338
    put_le32(pb, 0); /* reserved */
339
    
340
    /* stream list */
341
    for(i=0;i<n;i++) {
342
        list2 = start_tag(pb, "LIST");
343
        put_tag(pb, "strl");
344
    
345
        stream = &s->streams[i]->codec;
346

    
347
        /* FourCC should really be set by the codec itself */
348
        if (! stream->codec_tag) {
349
            stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
350
        }
351

    
352
        /* stream generic header */
353
        strh = start_tag(pb, "strh");
354
        switch(stream->codec_type) {
355
        case CODEC_TYPE_VIDEO:
356
            put_tag(pb, "vids");
357
            put_le32(pb, stream->codec_tag);
358
            put_le32(pb, 0); /* flags */
359
            put_le16(pb, 0); /* priority */
360
            put_le16(pb, 0); /* language */
361
            put_le32(pb, 0); /* initial frame */
362
            
363
            put_le32(pb, stream->frame_rate_base); /* scale */
364
            put_le32(pb, stream->frame_rate); /* rate */
365

    
366
            put_le32(pb, 0); /* start */
367
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
368
            put_le32(pb, nb_frames); /* length, XXX: fill later */
369
            put_le32(pb, 1024 * 1024); /* suggested buffer size */
370
            put_le32(pb, -1); /* quality */
371
            put_le32(pb, stream->width * stream->height * 3); /* sample size */
372
            put_le16(pb, 0);
373
            put_le16(pb, 0);
374
            put_le16(pb, stream->width);
375
            put_le16(pb, stream->height);
376
            break;
377
        case CODEC_TYPE_AUDIO:
378
            put_tag(pb, "auds");
379
            put_le32(pb, 1); /* tag */
380
            put_le32(pb, 0); /* flags */
381
            put_le16(pb, 0); /* priority */
382
            put_le16(pb, 0); /* language */
383
            put_le32(pb, 0); /* initial frame */
384
            parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
385
            put_le32(pb, au_scale); /* scale */
386
            put_le32(pb, au_byterate); /* rate */
387
            put_le32(pb, 0); /* start */
388
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
389
            put_le32(pb, 0); /* length, XXX: filled later */
390
            put_le32(pb, 12 * 1024); /* suggested buffer size */
391
            put_le32(pb, -1); /* quality */
392
            put_le32(pb, au_ssize); /* sample size */
393
            put_le32(pb, 0);
394
            put_le32(pb, 0);
395
            break;
396
        default:
397
            av_abort();
398
        }
399
        end_tag(pb, strh);
400

    
401
        strf = start_tag(pb, "strf");
402
        switch(stream->codec_type) {
403
        case CODEC_TYPE_VIDEO:
404
            put_bmp_header(pb, stream, codec_bmp_tags, 0);
405
            break;
406
        case CODEC_TYPE_AUDIO:
407
            if (put_wav_header(pb, stream) < 0) {
408
                av_free(avi);
409
                return -1;
410
            }
411
            break;
412
        default:
413
            av_abort();
414
        }
415
        end_tag(pb, strf);
416
        
417
        if (!url_is_streamed(pb)) {
418
            unsigned char tag[5];
419
    
420
            /* Starting to lay out AVI OpenDML master index. 
421
             * We want to make it JUNK entry for now, since we'd
422
             * like to get away without making AVI and OpenDML one 
423
             * for compatibility reasons.
424
             */
425
            avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
426
            avi->indexes[i].indx_start = start_tag(pb, "JUNK"); 
427
            put_le16(pb, 4);        /* wLongsPerEntry */
428
            put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
429
            put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
430
            put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
431
            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
432
                                    /* dwChunkId */
433
            put_le64(pb, 0);        /* dwReserved[3]
434
            put_le32(pb, 0);           Must be 0.    */
435
            url_fskip(pb, AVI_MASTER_INDEX_SIZE * 4 * 4);
436
            end_tag(pb, avi->indexes[i].indx_start);
437
        }
438
        
439
        end_tag(pb, list2);
440
    }
441
    
442
    if (!url_is_streamed(pb)) {
443
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
444
        avi->odml_list = start_tag(pb, "JUNK");
445
        put_tag(pb, "odml");
446
        put_tag(pb, "dmlh");
447
        put_le32(pb, 248);
448
        for (i = 0; i < 248; i+= 4)
449
             put_le32(pb, 0);
450
        end_tag(pb, avi->odml_list);
451
    }
452

    
453
    end_tag(pb, list1);
454
    
455
    avi->movi_list = start_tag(pb, "LIST");
456
    put_tag(pb, "movi");
457

    
458
    put_flush_packet(pb);
459

    
460
    return 0;
461
}
462

    
463
static int avi_write_ix(AVFormatContext *s)
464
{
465
    ByteIOContext *pb = &s->pb;
466
    AVIContext *avi = s->priv_data;
467
    unsigned char tag[5];
468
    unsigned char ix_tag[] = "ix00";
469
    int i, j;
470
    
471
    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
472
        return -1;
473
    
474
    for (i=0;i<s->nb_streams;i++) {
475
         offset_t ix, pos;
476
         
477
         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec.codec_type);
478
         ix_tag[3] = '0' + i;
479
         
480
         /* Writing AVI OpenDML leaf index chunk */
481
         ix = url_ftell(pb); 
482
         put_tag(pb, &ix_tag[0]);     /* ix?? */
483
         put_le32(pb, avi->indexes[i].entry * 8 + 24); 
484
                                      /* chunk size */
485
         put_le16(pb, 2);             /* wLongsPerEntry */
486
         put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */ 
487
         put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
488
         put_le32(pb, avi->indexes[i].entry);          
489
                                      /* nEntriesInUse */
490
         put_tag(pb, &tag[0]);        /* dwChunkId */
491
         put_le64(pb, avi->movi_list);/* qwBaseOffset */
492
         put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
493

    
494
         for (j=0; j<avi->indexes[i].entry; j++) {
495
             AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
496
             put_le32(pb, ie->pos + 8);
497
             put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
498
                          (ie->flags & 0x10 ? 0 : 0x80000000));
499
         }
500
         put_flush_packet(pb);
501
         pos = url_ftell(pb);
502
        
503
         /* Updating one entry in the AVI OpenDML master index */
504
         url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
505
         put_tag(pb, "indx");                 /* enabling this entry */
506
         url_fskip(pb, 8);
507
         put_le32(pb, avi->riff_id);          /* nEntriesInUse */
508
         url_fskip(pb, 16*avi->riff_id);
509
         put_le64(pb, ix);                    /* qwOffset */
510
         put_le32(pb, pos - ix);              /* dwSize */
511
         put_le32(pb, avi->indexes[i].entry); /* dwDuration */
512

    
513
         url_fseek(pb, pos, SEEK_SET);
514
    }
515
    return 0;
516
}
517

    
518
static int avi_write_idx1(AVFormatContext *s)
519
{
520
    ByteIOContext *pb = &s->pb;
521
    AVIContext *avi = s->priv_data;
522
    offset_t file_size, idx_chunk;
523
    int i, n, nb_frames, au_byterate, au_ssize, au_scale;
524
    AVCodecContext *stream;
525
    unsigned char tag[5];
526

    
527
    if (!url_is_streamed(pb)) {
528
        AVIIentry* ie = 0, *tie;
529
        int entry[MAX_STREAMS];
530
        int empty, stream_id = -1;
531

    
532
        idx_chunk = start_tag(pb, "idx1");
533
        memset(&entry[0], 0, sizeof(entry));
534
        do {
535
            empty = 1;
536
            for (i=0; i<s->nb_streams; i++) {
537
                 if (avi->indexes[i].entry <= entry[i])
538
                     continue;
539
                 
540
                 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
541
                 if (empty || tie->pos < ie->pos) {
542
                     ie = tie; 
543
                     stream_id = i;
544
                 }
545
                 empty = 0;
546
            }
547
            if (!empty) {
548
                avi_stream2fourcc(&tag[0], stream_id, 
549
                                  s->streams[stream_id]->codec.codec_type); 
550
                put_tag(pb, &tag[0]);
551
                put_le32(pb, ie->flags);
552
                put_le32(pb, ie->pos);
553
                put_le32(pb, ie->len);
554
                entry[stream_id]++;
555
            }
556
        } while (!empty);
557
        end_tag(pb, idx_chunk);
558

    
559
        /* Fill in frame/sample counters */
560
        file_size = url_ftell(pb);
561
        nb_frames = 0;
562
        for(n=0;n<s->nb_streams;n++) {
563
            if (avi->frames_hdr_strm[n] != 0) {
564
                stream = &s->streams[n]->codec;
565
                url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
566
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
567
                    put_le32(pb, stream->frame_number); 
568
                    if (nb_frames < stream->frame_number)
569
                        nb_frames = stream->frame_number;
570
                } else {
571
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
572
                        put_le32(pb, stream->frame_number);
573
                        nb_frames += stream->frame_number;
574
                    } else {
575
                        parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
576
                        put_le32(pb, avi->audio_strm_length[n] / au_ssize);
577
                    }
578
                }
579
            }
580
       }
581
       if (avi->frames_hdr_all != 0) {
582
           url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
583
           put_le32(pb, nb_frames); 
584
       }
585
       url_fseek(pb, file_size, SEEK_SET);
586
    }
587
    return 0;
588
}
589

    
590
static int avi_write_packet(AVFormatContext *s, int stream_index,
591
                            uint8_t *buf, int size, int force_pts)
592
{
593
    AVIContext *avi = s->priv_data;
594
    ByteIOContext *pb = &s->pb;
595
    unsigned char tag[5];
596
    unsigned int flags;
597
    AVCodecContext *enc;
598

    
599
    if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) { 
600
        avi_write_ix(s);
601
        end_tag(pb, avi->movi_list);
602
        
603
        if (avi->riff_id == 1)
604
            avi_write_idx1(s);
605

    
606
        end_tag(pb, avi->riff_start);
607
        avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
608
    }
609
    
610
    enc = &s->streams[stream_index]->codec;
611
    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
612
    if (enc->codec_type == CODEC_TYPE_AUDIO) {
613
       avi->audio_strm_length[stream_index] += size;
614
       flags = 0x10;
615
    } else
616
       flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
617

    
618
    if (!url_is_streamed(&s->pb)) {
619
        AVIIndex* idx = &avi->indexes[stream_index];
620
        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
621
        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
622
        if (idx->ents_allocated <= idx->entry) {
623
            idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*)); 
624
            if (!idx->cluster)
625
                return -1;
626
            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
627
            if (!idx->cluster[cl])
628
                return -1;
629
            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
630
        }
631
         
632
        idx->cluster[cl][id].flags = flags; 
633
        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
634
        idx->cluster[cl][id].len = size;
635
        idx->entry++;
636
    }
637
    
638
    put_buffer(pb, tag, 4);
639
    put_le32(pb, size);
640
    put_buffer(pb, buf, size);
641
    if (size & 1)
642
        put_byte(pb, 0);
643

    
644
    put_flush_packet(pb);
645
    return 0;
646
}
647

    
648
static int avi_write_trailer(AVFormatContext *s)
649
{
650
    AVIContext *avi = s->priv_data;
651
    ByteIOContext *pb = &s->pb;
652
    int res = 0;
653
    int i, j, n, nb_frames;
654
    offset_t file_size;
655

    
656
    if (avi->riff_id == 1) {
657
        end_tag(pb, avi->movi_list);
658
        res = avi_write_idx1(s);
659
        end_tag(pb, avi->riff_start);
660
    } else {
661
        avi_write_ix(s);
662
        end_tag(pb, avi->movi_list);
663
        end_tag(pb, avi->riff_start);
664

    
665
        file_size = url_ftell(pb);
666
        url_fseek(pb, avi->odml_list - 8, SEEK_SET);
667
        put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
668
        url_fskip(pb, 16);
669

    
670
        for (n=nb_frames=0;n<s->nb_streams;n++) {
671
             AVCodecContext *stream = &s->streams[n]->codec;
672
             if (stream->codec_type == CODEC_TYPE_VIDEO) {
673
                 if (nb_frames < stream->frame_number)
674
                     nb_frames = stream->frame_number;
675
             } else {
676
                 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
677
                     nb_frames += stream->frame_number;
678
                }
679
            }
680
        }
681
        put_le32(pb, nb_frames);
682
        url_fseek(pb, file_size, SEEK_SET);
683
    }
684
    put_flush_packet(pb);
685

    
686
    for (i=0; i<MAX_STREAMS; i++) {
687
         for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
688
              av_free(avi->indexes[i].cluster[j]);
689
         av_free(avi->indexes[i].cluster);
690
         avi->indexes[i].cluster = NULL;
691
         avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
692
    }
693
    
694
    return res;
695
}
696

    
697
static AVOutputFormat avi_oformat = {
698
    "avi",
699
    "avi format",
700
    "video/x-msvideo",
701
    "avi",
702
    sizeof(AVIContext),
703
    CODEC_ID_MP2,
704
    CODEC_ID_MPEG4,
705
    avi_write_header,
706
    avi_write_packet,
707
    avi_write_trailer,
708
};
709

    
710
int avienc_init(void)
711
{
712
    av_register_output_format(&avi_oformat);
713
    return 0;
714
}