Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ 12f1b1fe

History | View | Annotate | Download (20.7 KB)

1
/*
2
 * AVI muxer
3
 * Copyright (c) 2000 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include "avi.h"
23
#include "riff.h"
24

    
25
/*
26
 * TODO:
27
 *  - fill all fields if non streamed (nb_frames for example)
28
 */
29

    
30
typedef struct AVIIentry {
31
    unsigned int flags, pos, len;
32
} AVIIentry;
33

    
34
#define AVI_INDEX_CLUSTER_SIZE 16384
35

    
36
typedef struct AVIIndex {
37
    int64_t     indx_start;
38
    int         entry;
39
    int         ents_allocated;
40
    AVIIentry** cluster;
41
} AVIIndex;
42

    
43
typedef struct {
44
    int64_t riff_start, movi_list, odml_list;
45
    int64_t frames_hdr_all;
46
    int riff_id;
47
} AVIContext;
48

    
49
typedef struct  {
50
    int64_t frames_hdr_strm;
51
    int audio_strm_length;
52
    int packet_count;
53
    int entry;
54

    
55
    AVIIndex indexes;
56
} AVIStream ;
57

    
58
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
59
{
60
    int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
61
    int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
62
    return &idx->cluster[cl][id];
63
}
64

    
65
static int64_t avi_start_new_riff(AVFormatContext *s, ByteIOContext *pb,
66
                                  const char* riff_tag, const char* list_tag)
67
{
68
    AVIContext *avi= s->priv_data;
69
    int64_t loff;
70
    int i;
71

    
72
    avi->riff_id++;
73
    for (i=0; i<s->nb_streams; i++){
74
        AVIStream *avist= s->streams[i]->priv_data;
75
        avist->indexes.entry = 0;
76
    }
77

    
78
    avi->riff_start = ff_start_tag(pb, "RIFF");
79
    put_tag(pb, riff_tag);
80
    loff = ff_start_tag(pb, "LIST");
81
    put_tag(pb, list_tag);
82
    return loff;
83
}
84

    
85
static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
86
{
87
    tag[0] = '0';
88
    tag[1] = '0' + index;
89
    if (type == CODEC_TYPE_VIDEO) {
90
        tag[2] = 'd';
91
        tag[3] = 'c';
92
    } else if (type == CODEC_TYPE_SUBTITLE) {
93
        // note: this is not an official code
94
        tag[2] = 's';
95
        tag[3] = 'b';
96
    } else {
97
        tag[2] = 'w';
98
        tag[3] = 'b';
99
    }
100
    tag[4] = '\0';
101
    return tag;
102
}
103

    
104
static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
105
{
106
    int len = strlen(str);
107
    if (len > 0) {
108
        len++;
109
        put_tag(pb, tag);
110
        put_le32(pb, len);
111
        put_strz(pb, str);
112
        if (len & 1)
113
            put_byte(pb, 0);
114
    }
115
}
116

    
117
static void avi_write_info_tag2(AVFormatContext *s, const char *fourcc, const char *key1, const char *key2)
118
{
119
    AVMetadataTag *tag= av_metadata_get(s->metadata, key1, NULL, 0);
120
    if(!tag && key2)
121
        tag= av_metadata_get(s->metadata, key2, NULL, 0);
122
    if(tag)
123
        avi_write_info_tag(s->pb, fourcc, tag->value);
124
}
125

    
126
static int avi_write_counters(AVFormatContext* s, int riff_id)
127
{
128
    ByteIOContext *pb = s->pb;
129
    AVIContext *avi = s->priv_data;
130
    int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
131
    int64_t file_size;
132
    AVCodecContext* stream;
133

    
134
    file_size = url_ftell(pb);
135
    for(n = 0; n < s->nb_streams; n++) {
136
        AVIStream *avist= s->streams[n]->priv_data;
137

    
138
        assert(avist->frames_hdr_strm);
139
        stream = s->streams[n]->codec;
140
        url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
141
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
142
        if(au_ssize == 0) {
143
            put_le32(pb, avist->packet_count);
144
        } else {
145
            put_le32(pb, avist->audio_strm_length / au_ssize);
146
        }
147
        if(stream->codec_type == CODEC_TYPE_VIDEO)
148
            nb_frames = FFMAX(nb_frames, avist->packet_count);
149
    }
150
    if(riff_id == 1) {
151
        assert(avi->frames_hdr_all);
152
        url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
153
        put_le32(pb, nb_frames);
154
    }
155
    url_fseek(pb, file_size, SEEK_SET);
156

    
157
    return 0;
158
}
159

    
160
static int avi_write_header(AVFormatContext *s)
161
{
162
    AVIContext *avi = s->priv_data;
163
    ByteIOContext *pb = s->pb;
164
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
165
    AVCodecContext *stream, *video_enc;
166
    int64_t list1, list2, strh, strf;
167

    
168
    for(n=0;n<s->nb_streams;n++) {
169
        s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
170
        if(!s->streams[n]->priv_data)
171
            return AVERROR(ENOMEM);
172
    }
173

    
174
    /* header list */
175
    avi->riff_id = 0;
176
    list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
177

    
178
    /* avi header */
179
    put_tag(pb, "avih");
180
    put_le32(pb, 14 * 4);
181
    bitrate = 0;
182

    
183
    video_enc = NULL;
184
    for(n=0;n<s->nb_streams;n++) {
185
        stream = s->streams[n]->codec;
186
        bitrate += stream->bit_rate;
187
        if (stream->codec_type == CODEC_TYPE_VIDEO)
188
            video_enc = stream;
189
    }
190

    
191
    nb_frames = 0;
192

    
193
    if(video_enc){
194
        put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
195
    } else {
196
        put_le32(pb, 0);
197
    }
198
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
199
    put_le32(pb, 0); /* padding */
200
    if (url_is_streamed(pb))
201
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
202
    else
203
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
204
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
205
    put_le32(pb, nb_frames); /* nb frames, filled later */
206
    put_le32(pb, 0); /* initial frame */
207
    put_le32(pb, s->nb_streams); /* nb streams */
208
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
209
    if(video_enc){
210
        put_le32(pb, video_enc->width);
211
        put_le32(pb, video_enc->height);
212
    } else {
213
        put_le32(pb, 0);
214
        put_le32(pb, 0);
215
    }
216
    put_le32(pb, 0); /* reserved */
217
    put_le32(pb, 0); /* reserved */
218
    put_le32(pb, 0); /* reserved */
219
    put_le32(pb, 0); /* reserved */
220

    
221
    /* stream list */
222
    for(i=0;i<n;i++) {
223
        AVIStream *avist= s->streams[i]->priv_data;
224
        list2 = ff_start_tag(pb, "LIST");
225
        put_tag(pb, "strl");
226

    
227
        stream = s->streams[i]->codec;
228

    
229
        /* stream generic header */
230
        strh = ff_start_tag(pb, "strh");
231
        switch(stream->codec_type) {
232
        case CODEC_TYPE_SUBTITLE:
233
            // XSUB subtitles behave like video tracks, other subtitles
234
            // are not (yet) supported.
235
            if (stream->codec_id != CODEC_ID_XSUB) break;
236
        case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
237
        case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
238
//        case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
239
        case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
240
        }
241
        if(stream->codec_type == CODEC_TYPE_VIDEO ||
242
           stream->codec_id == CODEC_ID_XSUB)
243
            put_le32(pb, stream->codec_tag);
244
        else
245
            put_le32(pb, 1);
246
        put_le32(pb, 0); /* flags */
247
        put_le16(pb, 0); /* priority */
248
        put_le16(pb, 0); /* language */
249
        put_le32(pb, 0); /* initial frame */
250

    
251
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
252

    
253
        put_le32(pb, au_scale); /* scale */
254
        put_le32(pb, au_byterate); /* rate */
255
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
256

    
257
        put_le32(pb, 0); /* start */
258
        avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
259
        if (url_is_streamed(pb))
260
            put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
261
        else
262
            put_le32(pb, 0); /* length, XXX: filled later */
263

    
264
        /* suggested buffer size */ //FIXME set at the end to largest chunk
265
        if(stream->codec_type == CODEC_TYPE_VIDEO)
266
            put_le32(pb, 1024 * 1024);
267
        else if(stream->codec_type == CODEC_TYPE_AUDIO)
268
            put_le32(pb, 12 * 1024);
269
        else
270
            put_le32(pb, 0);
271
        put_le32(pb, -1); /* quality */
272
        put_le32(pb, au_ssize); /* sample size */
273
        put_le32(pb, 0);
274
        put_le16(pb, stream->width);
275
        put_le16(pb, stream->height);
276
        ff_end_tag(pb, strh);
277

    
278
      if(stream->codec_type != CODEC_TYPE_DATA){
279
        strf = ff_start_tag(pb, "strf");
280
        switch(stream->codec_type) {
281
        case CODEC_TYPE_SUBTITLE:
282
            // XSUB subtitles behave like video tracks, other subtitles
283
            // are not (yet) supported.
284
            if (stream->codec_id != CODEC_ID_XSUB) break;
285
        case CODEC_TYPE_VIDEO:
286
            ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
287
            break;
288
        case CODEC_TYPE_AUDIO:
289
            if (ff_put_wav_header(pb, stream) < 0) {
290
                return -1;
291
            }
292
            break;
293
        default:
294
            return -1;
295
        }
296
        ff_end_tag(pb, strf);
297
      }
298

    
299
        if (!url_is_streamed(pb)) {
300
            unsigned char tag[5];
301
            int j;
302

    
303
            /* Starting to lay out AVI OpenDML master index.
304
             * We want to make it JUNK entry for now, since we'd
305
             * like to get away without making AVI an OpenDML one
306
             * for compatibility reasons.
307
             */
308
            avist->indexes.entry = avist->indexes.ents_allocated = 0;
309
            avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
310
            put_le16(pb, 4);        /* wLongsPerEntry */
311
            put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
312
            put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
313
            put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
314
            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
315
                                    /* dwChunkId */
316
            put_le64(pb, 0);        /* dwReserved[3]
317
            put_le32(pb, 0);           Must be 0.    */
318
            for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
319
                 put_le64(pb, 0);
320
            ff_end_tag(pb, avist->indexes.indx_start);
321
        }
322

    
323
        if(   stream->codec_type == CODEC_TYPE_VIDEO
324
           && s->streams[i]->sample_aspect_ratio.num>0
325
           && s->streams[i]->sample_aspect_ratio.den>0){
326
            int vprp= ff_start_tag(pb, "vprp");
327
            AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
328
                                      (AVRational){stream->width, stream->height});
329
            int num, den;
330
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
331

    
332
            put_le32(pb, 0); //video format  = unknown
333
            put_le32(pb, 0); //video standard= unknown
334
            put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
335
            put_le32(pb, stream->width );
336
            put_le32(pb, stream->height);
337
            put_le16(pb, den);
338
            put_le16(pb, num);
339
            put_le32(pb, stream->width );
340
            put_le32(pb, stream->height);
341
            put_le32(pb, 1); //progressive FIXME
342

    
343
            put_le32(pb, stream->height);
344
            put_le32(pb, stream->width );
345
            put_le32(pb, stream->height);
346
            put_le32(pb, stream->width );
347
            put_le32(pb, 0);
348
            put_le32(pb, 0);
349

    
350
            put_le32(pb, 0);
351
            put_le32(pb, 0);
352
            ff_end_tag(pb, vprp);
353
        }
354

    
355
        ff_end_tag(pb, list2);
356
    }
357

    
358
    if (!url_is_streamed(pb)) {
359
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
360
        avi->odml_list = ff_start_tag(pb, "JUNK");
361
        put_tag(pb, "odml");
362
        put_tag(pb, "dmlh");
363
        put_le32(pb, 248);
364
        for (i = 0; i < 248; i+= 4)
365
             put_le32(pb, 0);
366
        ff_end_tag(pb, avi->odml_list);
367
    }
368

    
369
    ff_end_tag(pb, list1);
370

    
371
    list2 = ff_start_tag(pb, "LIST");
372
    put_tag(pb, "INFO");
373
    avi_write_info_tag2(s, "INAM", "Title", NULL);
374
    avi_write_info_tag2(s, "IART", "Artist", "Author");
375
    avi_write_info_tag2(s, "ICOP", "Copyright", NULL);
376
    avi_write_info_tag2(s, "ICMT", "Comment", NULL);
377
    avi_write_info_tag2(s, "IPRD", "Album", NULL);
378
    avi_write_info_tag2(s, "IGNR", "Genre", NULL);
379
    avi_write_info_tag2(s, "IPRT", "Track", NULL);
380
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
381
        avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
382
    ff_end_tag(pb, list2);
383

    
384
    /* some padding for easier tag editing */
385
    list2 = ff_start_tag(pb, "JUNK");
386
    for (i = 0; i < 1016; i += 4)
387
        put_le32(pb, 0);
388
    ff_end_tag(pb, list2);
389

    
390
    avi->movi_list = ff_start_tag(pb, "LIST");
391
    put_tag(pb, "movi");
392

    
393
    put_flush_packet(pb);
394

    
395
    return 0;
396
}
397

    
398
static int avi_write_ix(AVFormatContext *s)
399
{
400
    ByteIOContext *pb = s->pb;
401
    AVIContext *avi = s->priv_data;
402
    char tag[5];
403
    char ix_tag[] = "ix00";
404
    int i, j;
405

    
406
    assert(!url_is_streamed(pb));
407

    
408
    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
409
        return -1;
410

    
411
    for (i=0;i<s->nb_streams;i++) {
412
        AVIStream *avist= s->streams[i]->priv_data;
413
         int64_t ix, pos;
414

    
415
         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
416
         ix_tag[3] = '0' + i;
417

    
418
         /* Writing AVI OpenDML leaf index chunk */
419
         ix = url_ftell(pb);
420
         put_tag(pb, &ix_tag[0]);     /* ix?? */
421
         put_le32(pb, avist->indexes.entry * 8 + 24);
422
                                      /* chunk size */
423
         put_le16(pb, 2);             /* wLongsPerEntry */
424
         put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */
425
         put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
426
         put_le32(pb, avist->indexes.entry);
427
                                      /* nEntriesInUse */
428
         put_tag(pb, &tag[0]);        /* dwChunkId */
429
         put_le64(pb, avi->movi_list);/* qwBaseOffset */
430
         put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
431

    
432
         for (j=0; j<avist->indexes.entry; j++) {
433
             AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
434
             put_le32(pb, ie->pos + 8);
435
             put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
436
                          (ie->flags & 0x10 ? 0 : 0x80000000));
437
         }
438
         put_flush_packet(pb);
439
         pos = url_ftell(pb);
440

    
441
         /* Updating one entry in the AVI OpenDML master index */
442
         url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
443
         put_tag(pb, "indx");                 /* enabling this entry */
444
         url_fskip(pb, 8);
445
         put_le32(pb, avi->riff_id);          /* nEntriesInUse */
446
         url_fskip(pb, 16*avi->riff_id);
447
         put_le64(pb, ix);                    /* qwOffset */
448
         put_le32(pb, pos - ix);              /* dwSize */
449
         put_le32(pb, avist->indexes.entry); /* dwDuration */
450

    
451
         url_fseek(pb, pos, SEEK_SET);
452
    }
453
    return 0;
454
}
455

    
456
static int avi_write_idx1(AVFormatContext *s)
457
{
458
    ByteIOContext *pb = s->pb;
459
    AVIContext *avi = s->priv_data;
460
    int64_t idx_chunk;
461
    int i;
462
    char tag[5];
463

    
464
    if (!url_is_streamed(pb)) {
465
        AVIStream *avist;
466
        AVIIentry* ie = 0, *tie;
467
        int empty, stream_id = -1;
468

    
469
        idx_chunk = ff_start_tag(pb, "idx1");
470
        for(i=0; i<s->nb_streams; i++){
471
            avist= s->streams[i]->priv_data;
472
            avist->entry=0;
473
        }
474

    
475
        do {
476
            empty = 1;
477
            for (i=0; i<s->nb_streams; i++) {
478
                avist= s->streams[i]->priv_data;
479
                 if (avist->indexes.entry <= avist->entry)
480
                     continue;
481

    
482
                 tie = avi_get_ientry(&avist->indexes, avist->entry);
483
                 if (empty || tie->pos < ie->pos) {
484
                     ie = tie;
485
                     stream_id = i;
486
                 }
487
                 empty = 0;
488
            }
489
            if (!empty) {
490
                avist= s->streams[stream_id]->priv_data;
491
                avi_stream2fourcc(&tag[0], stream_id,
492
                                  s->streams[stream_id]->codec->codec_type);
493
                put_tag(pb, &tag[0]);
494
                put_le32(pb, ie->flags);
495
                put_le32(pb, ie->pos);
496
                put_le32(pb, ie->len);
497
                avist->entry++;
498
            }
499
        } while (!empty);
500
        ff_end_tag(pb, idx_chunk);
501

    
502
        avi_write_counters(s, avi->riff_id);
503
    }
504
    return 0;
505
}
506

    
507
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
508
{
509
    AVIContext *avi = s->priv_data;
510
    ByteIOContext *pb = s->pb;
511
    unsigned char tag[5];
512
    unsigned int flags=0;
513
    const int stream_index= pkt->stream_index;
514
    AVIStream *avist= s->streams[stream_index]->priv_data;
515
    AVCodecContext *enc= s->streams[stream_index]->codec;
516
    int size= pkt->size;
517

    
518
//    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
519
    while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
520
        AVPacket empty_packet;
521

    
522
        av_init_packet(&empty_packet);
523
        empty_packet.size= 0;
524
        empty_packet.data= NULL;
525
        empty_packet.stream_index= stream_index;
526
        avi_write_packet(s, &empty_packet);
527
//        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
528
    }
529
    avist->packet_count++;
530

    
531
    // Make sure to put an OpenDML chunk when the file size exceeds the limits
532
    if (!url_is_streamed(pb) &&
533
        (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
534

    
535
        avi_write_ix(s);
536
        ff_end_tag(pb, avi->movi_list);
537

    
538
        if (avi->riff_id == 1)
539
            avi_write_idx1(s);
540

    
541
        ff_end_tag(pb, avi->riff_start);
542
        avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
543
    }
544

    
545
    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
546
    if(pkt->flags&PKT_FLAG_KEY)
547
        flags = 0x10;
548
    if (enc->codec_type == CODEC_TYPE_AUDIO) {
549
       avist->audio_strm_length += size;
550
    }
551

    
552
    if (!url_is_streamed(s->pb)) {
553
        AVIIndex* idx = &avist->indexes;
554
        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
555
        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
556
        if (idx->ents_allocated <= idx->entry) {
557
            idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
558
            if (!idx->cluster)
559
                return -1;
560
            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
561
            if (!idx->cluster[cl])
562
                return -1;
563
            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
564
        }
565

    
566
        idx->cluster[cl][id].flags = flags;
567
        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
568
        idx->cluster[cl][id].len = size;
569
        idx->entry++;
570
    }
571

    
572
    put_buffer(pb, tag, 4);
573
    put_le32(pb, size);
574
    put_buffer(pb, pkt->data, size);
575
    if (size & 1)
576
        put_byte(pb, 0);
577

    
578
    put_flush_packet(pb);
579
    return 0;
580
}
581

    
582
static int avi_write_trailer(AVFormatContext *s)
583
{
584
    AVIContext *avi = s->priv_data;
585
    ByteIOContext *pb = s->pb;
586
    int res = 0;
587
    int i, j, n, nb_frames;
588
    int64_t file_size;
589

    
590
    if (!url_is_streamed(pb)){
591
        if (avi->riff_id == 1) {
592
            ff_end_tag(pb, avi->movi_list);
593
            res = avi_write_idx1(s);
594
            ff_end_tag(pb, avi->riff_start);
595
        } else {
596
            avi_write_ix(s);
597
            ff_end_tag(pb, avi->movi_list);
598
            ff_end_tag(pb, avi->riff_start);
599

    
600
            file_size = url_ftell(pb);
601
            url_fseek(pb, avi->odml_list - 8, SEEK_SET);
602
            put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
603
            url_fskip(pb, 16);
604

    
605
            for (n=nb_frames=0;n<s->nb_streams;n++) {
606
                AVCodecContext *stream = s->streams[n]->codec;
607
                AVIStream *avist= s->streams[n]->priv_data;
608

    
609
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
610
                    if (nb_frames < avist->packet_count)
611
                        nb_frames = avist->packet_count;
612
                } else {
613
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
614
                        nb_frames += avist->packet_count;
615
                    }
616
                }
617
            }
618
            put_le32(pb, nb_frames);
619
            url_fseek(pb, file_size, SEEK_SET);
620

    
621
            avi_write_counters(s, avi->riff_id);
622
        }
623
    }
624
    put_flush_packet(pb);
625

    
626
    for (i=0; i<s->nb_streams; i++) {
627
         AVIStream *avist= s->streams[i]->priv_data;
628
         for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
629
              av_free(avist->indexes.cluster[j]);
630
         av_freep(&avist->indexes.cluster);
631
         avist->indexes.ents_allocated = avist->indexes.entry = 0;
632
    }
633

    
634
    return res;
635
}
636

    
637
AVOutputFormat avi_muxer = {
638
    "avi",
639
    NULL_IF_CONFIG_SMALL("AVI format"),
640
    "video/x-msvideo",
641
    "avi",
642
    sizeof(AVIContext),
643
    CODEC_ID_MP2,
644
    CODEC_ID_MPEG4,
645
    avi_write_header,
646
    avi_write_packet,
647
    avi_write_trailer,
648
    .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
649
    .flags= AVFMT_VARIABLE_FPS,
650
};