Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ ffdf0d48

History | View | Annotate | Download (21.1 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, AVStream *st, const char *fourcc, const char *key1, const char *key2)
118
{
119
    AVMetadataTag *tag;
120
    if(st){
121
        tag= av_metadata_get(st->metadata, key1, NULL, 0);
122
        if(!tag && key2)
123
            tag= av_metadata_get(st->metadata, key2, NULL, 0);
124
    }else{
125
        tag= av_metadata_get(s->metadata, key1, NULL, 0);
126
    if(!tag && key2)
127
        tag= av_metadata_get(s->metadata, key2, NULL, 0);
128
    }
129
    if(tag)
130
        avi_write_info_tag(s->pb, fourcc, tag->value);
131
}
132

    
133
static int avi_write_counters(AVFormatContext* s, int riff_id)
134
{
135
    ByteIOContext *pb = s->pb;
136
    AVIContext *avi = s->priv_data;
137
    int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
138
    int64_t file_size;
139
    AVCodecContext* stream;
140

    
141
    file_size = url_ftell(pb);
142
    for(n = 0; n < s->nb_streams; n++) {
143
        AVIStream *avist= s->streams[n]->priv_data;
144

    
145
        assert(avist->frames_hdr_strm);
146
        stream = s->streams[n]->codec;
147
        url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
148
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
149
        if(au_ssize == 0) {
150
            put_le32(pb, avist->packet_count);
151
        } else {
152
            put_le32(pb, avist->audio_strm_length / au_ssize);
153
        }
154
        if(stream->codec_type == CODEC_TYPE_VIDEO)
155
            nb_frames = FFMAX(nb_frames, avist->packet_count);
156
    }
157
    if(riff_id == 1) {
158
        assert(avi->frames_hdr_all);
159
        url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
160
        put_le32(pb, nb_frames);
161
    }
162
    url_fseek(pb, file_size, SEEK_SET);
163

    
164
    return 0;
165
}
166

    
167
static int avi_write_header(AVFormatContext *s)
168
{
169
    AVIContext *avi = s->priv_data;
170
    ByteIOContext *pb = s->pb;
171
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
172
    AVCodecContext *stream, *video_enc;
173
    int64_t list1, list2, strh, strf;
174

    
175
    for(n=0;n<s->nb_streams;n++) {
176
        s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
177
        if(!s->streams[n]->priv_data)
178
            return AVERROR(ENOMEM);
179
    }
180

    
181
    /* header list */
182
    avi->riff_id = 0;
183
    list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
184

    
185
    /* avi header */
186
    put_tag(pb, "avih");
187
    put_le32(pb, 14 * 4);
188
    bitrate = 0;
189

    
190
    video_enc = NULL;
191
    for(n=0;n<s->nb_streams;n++) {
192
        stream = s->streams[n]->codec;
193
        bitrate += stream->bit_rate;
194
        if (stream->codec_type == CODEC_TYPE_VIDEO)
195
            video_enc = stream;
196
    }
197

    
198
    nb_frames = 0;
199

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

    
228
    /* stream list */
229
    for(i=0;i<n;i++) {
230
        AVIStream *avist= s->streams[i]->priv_data;
231
        list2 = ff_start_tag(pb, "LIST");
232
        put_tag(pb, "strl");
233

    
234
        stream = s->streams[i]->codec;
235

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

    
258
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
259

    
260
        put_le32(pb, au_scale); /* scale */
261
        put_le32(pb, au_byterate); /* rate */
262
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
263

    
264
        put_le32(pb, 0); /* start */
265
        avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
266
        if (url_is_streamed(pb))
267
            put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
268
        else
269
            put_le32(pb, 0); /* length, XXX: filled later */
270

    
271
        /* suggested buffer size */ //FIXME set at the end to largest chunk
272
        if(stream->codec_type == CODEC_TYPE_VIDEO)
273
            put_le32(pb, 1024 * 1024);
274
        else if(stream->codec_type == CODEC_TYPE_AUDIO)
275
            put_le32(pb, 12 * 1024);
276
        else
277
            put_le32(pb, 0);
278
        put_le32(pb, -1); /* quality */
279
        put_le32(pb, au_ssize); /* sample size */
280
        put_le32(pb, 0);
281
        put_le16(pb, stream->width);
282
        put_le16(pb, stream->height);
283
        ff_end_tag(pb, strh);
284

    
285
      if(stream->codec_type != CODEC_TYPE_DATA){
286
        strf = ff_start_tag(pb, "strf");
287
        switch(stream->codec_type) {
288
        case CODEC_TYPE_SUBTITLE:
289
            // XSUB subtitles behave like video tracks, other subtitles
290
            // are not (yet) supported.
291
            if (stream->codec_id != CODEC_ID_XSUB) break;
292
        case CODEC_TYPE_VIDEO:
293
            ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
294
            break;
295
        case CODEC_TYPE_AUDIO:
296
            if (ff_put_wav_header(pb, stream) < 0) {
297
                return -1;
298
            }
299
            break;
300
        default:
301
            return -1;
302
        }
303
        ff_end_tag(pb, strf);
304
        avi_write_info_tag2(s, s->streams[i], "strn", "Title", "Description");
305
      }
306

    
307
        if (!url_is_streamed(pb)) {
308
            unsigned char tag[5];
309
            int j;
310

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

    
331
        if(   stream->codec_type == CODEC_TYPE_VIDEO
332
           && s->streams[i]->sample_aspect_ratio.num>0
333
           && s->streams[i]->sample_aspect_ratio.den>0){
334
            int vprp= ff_start_tag(pb, "vprp");
335
            AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
336
                                      (AVRational){stream->width, stream->height});
337
            int num, den;
338
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
339

    
340
            put_le32(pb, 0); //video format  = unknown
341
            put_le32(pb, 0); //video standard= unknown
342
            put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
343
            put_le32(pb, stream->width );
344
            put_le32(pb, stream->height);
345
            put_le16(pb, den);
346
            put_le16(pb, num);
347
            put_le32(pb, stream->width );
348
            put_le32(pb, stream->height);
349
            put_le32(pb, 1); //progressive FIXME
350

    
351
            put_le32(pb, stream->height);
352
            put_le32(pb, stream->width );
353
            put_le32(pb, stream->height);
354
            put_le32(pb, stream->width );
355
            put_le32(pb, 0);
356
            put_le32(pb, 0);
357

    
358
            put_le32(pb, 0);
359
            put_le32(pb, 0);
360
            ff_end_tag(pb, vprp);
361
        }
362

    
363
        ff_end_tag(pb, list2);
364
    }
365

    
366
    if (!url_is_streamed(pb)) {
367
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
368
        avi->odml_list = ff_start_tag(pb, "JUNK");
369
        put_tag(pb, "odml");
370
        put_tag(pb, "dmlh");
371
        put_le32(pb, 248);
372
        for (i = 0; i < 248; i+= 4)
373
             put_le32(pb, 0);
374
        ff_end_tag(pb, avi->odml_list);
375
    }
376

    
377
    ff_end_tag(pb, list1);
378

    
379
    list2 = ff_start_tag(pb, "LIST");
380
    put_tag(pb, "INFO");
381
    avi_write_info_tag2(s, NULL, "INAM", "Title", NULL);
382
    avi_write_info_tag2(s, NULL, "IART", "Artist", "Author");
383
    avi_write_info_tag2(s, NULL, "ICOP", "Copyright", NULL);
384
    avi_write_info_tag2(s, NULL, "ICMT", "Comment", NULL);
385
    avi_write_info_tag2(s, NULL, "IPRD", "Album", NULL);
386
    avi_write_info_tag2(s, NULL, "IGNR", "Genre", NULL);
387
    avi_write_info_tag2(s, NULL, "IPRT", "Track", NULL);
388
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
389
        avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
390
    ff_end_tag(pb, list2);
391

    
392
    /* some padding for easier tag editing */
393
    list2 = ff_start_tag(pb, "JUNK");
394
    for (i = 0; i < 1016; i += 4)
395
        put_le32(pb, 0);
396
    ff_end_tag(pb, list2);
397

    
398
    avi->movi_list = ff_start_tag(pb, "LIST");
399
    put_tag(pb, "movi");
400

    
401
    put_flush_packet(pb);
402

    
403
    return 0;
404
}
405

    
406
static int avi_write_ix(AVFormatContext *s)
407
{
408
    ByteIOContext *pb = s->pb;
409
    AVIContext *avi = s->priv_data;
410
    char tag[5];
411
    char ix_tag[] = "ix00";
412
    int i, j;
413

    
414
    assert(!url_is_streamed(pb));
415

    
416
    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
417
        return -1;
418

    
419
    for (i=0;i<s->nb_streams;i++) {
420
        AVIStream *avist= s->streams[i]->priv_data;
421
         int64_t ix, pos;
422

    
423
         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
424
         ix_tag[3] = '0' + i;
425

    
426
         /* Writing AVI OpenDML leaf index chunk */
427
         ix = url_ftell(pb);
428
         put_tag(pb, &ix_tag[0]);     /* ix?? */
429
         put_le32(pb, avist->indexes.entry * 8 + 24);
430
                                      /* chunk size */
431
         put_le16(pb, 2);             /* wLongsPerEntry */
432
         put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */
433
         put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
434
         put_le32(pb, avist->indexes.entry);
435
                                      /* nEntriesInUse */
436
         put_tag(pb, &tag[0]);        /* dwChunkId */
437
         put_le64(pb, avi->movi_list);/* qwBaseOffset */
438
         put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
439

    
440
         for (j=0; j<avist->indexes.entry; j++) {
441
             AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
442
             put_le32(pb, ie->pos + 8);
443
             put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
444
                          (ie->flags & 0x10 ? 0 : 0x80000000));
445
         }
446
         put_flush_packet(pb);
447
         pos = url_ftell(pb);
448

    
449
         /* Updating one entry in the AVI OpenDML master index */
450
         url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
451
         put_tag(pb, "indx");                 /* enabling this entry */
452
         url_fskip(pb, 8);
453
         put_le32(pb, avi->riff_id);          /* nEntriesInUse */
454
         url_fskip(pb, 16*avi->riff_id);
455
         put_le64(pb, ix);                    /* qwOffset */
456
         put_le32(pb, pos - ix);              /* dwSize */
457
         put_le32(pb, avist->indexes.entry); /* dwDuration */
458

    
459
         url_fseek(pb, pos, SEEK_SET);
460
    }
461
    return 0;
462
}
463

    
464
static int avi_write_idx1(AVFormatContext *s)
465
{
466
    ByteIOContext *pb = s->pb;
467
    AVIContext *avi = s->priv_data;
468
    int64_t idx_chunk;
469
    int i;
470
    char tag[5];
471

    
472
    if (!url_is_streamed(pb)) {
473
        AVIStream *avist;
474
        AVIIentry* ie = 0, *tie;
475
        int empty, stream_id = -1;
476

    
477
        idx_chunk = ff_start_tag(pb, "idx1");
478
        for(i=0; i<s->nb_streams; i++){
479
            avist= s->streams[i]->priv_data;
480
            avist->entry=0;
481
        }
482

    
483
        do {
484
            empty = 1;
485
            for (i=0; i<s->nb_streams; i++) {
486
                avist= s->streams[i]->priv_data;
487
                 if (avist->indexes.entry <= avist->entry)
488
                     continue;
489

    
490
                 tie = avi_get_ientry(&avist->indexes, avist->entry);
491
                 if (empty || tie->pos < ie->pos) {
492
                     ie = tie;
493
                     stream_id = i;
494
                 }
495
                 empty = 0;
496
            }
497
            if (!empty) {
498
                avist= s->streams[stream_id]->priv_data;
499
                avi_stream2fourcc(&tag[0], stream_id,
500
                                  s->streams[stream_id]->codec->codec_type);
501
                put_tag(pb, &tag[0]);
502
                put_le32(pb, ie->flags);
503
                put_le32(pb, ie->pos);
504
                put_le32(pb, ie->len);
505
                avist->entry++;
506
            }
507
        } while (!empty);
508
        ff_end_tag(pb, idx_chunk);
509

    
510
        avi_write_counters(s, avi->riff_id);
511
    }
512
    return 0;
513
}
514

    
515
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
516
{
517
    AVIContext *avi = s->priv_data;
518
    ByteIOContext *pb = s->pb;
519
    unsigned char tag[5];
520
    unsigned int flags=0;
521
    const int stream_index= pkt->stream_index;
522
    AVIStream *avist= s->streams[stream_index]->priv_data;
523
    AVCodecContext *enc= s->streams[stream_index]->codec;
524
    int size= pkt->size;
525

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

    
530
        av_init_packet(&empty_packet);
531
        empty_packet.size= 0;
532
        empty_packet.data= NULL;
533
        empty_packet.stream_index= stream_index;
534
        avi_write_packet(s, &empty_packet);
535
//        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
536
    }
537
    avist->packet_count++;
538

    
539
    // Make sure to put an OpenDML chunk when the file size exceeds the limits
540
    if (!url_is_streamed(pb) &&
541
        (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
542

    
543
        avi_write_ix(s);
544
        ff_end_tag(pb, avi->movi_list);
545

    
546
        if (avi->riff_id == 1)
547
            avi_write_idx1(s);
548

    
549
        ff_end_tag(pb, avi->riff_start);
550
        avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
551
    }
552

    
553
    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
554
    if(pkt->flags&PKT_FLAG_KEY)
555
        flags = 0x10;
556
    if (enc->codec_type == CODEC_TYPE_AUDIO) {
557
       avist->audio_strm_length += size;
558
    }
559

    
560
    if (!url_is_streamed(s->pb)) {
561
        AVIIndex* idx = &avist->indexes;
562
        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
563
        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
564
        if (idx->ents_allocated <= idx->entry) {
565
            idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
566
            if (!idx->cluster)
567
                return -1;
568
            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
569
            if (!idx->cluster[cl])
570
                return -1;
571
            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
572
        }
573

    
574
        idx->cluster[cl][id].flags = flags;
575
        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
576
        idx->cluster[cl][id].len = size;
577
        idx->entry++;
578
    }
579

    
580
    put_buffer(pb, tag, 4);
581
    put_le32(pb, size);
582
    put_buffer(pb, pkt->data, size);
583
    if (size & 1)
584
        put_byte(pb, 0);
585

    
586
    put_flush_packet(pb);
587
    return 0;
588
}
589

    
590
static int avi_write_trailer(AVFormatContext *s)
591
{
592
    AVIContext *avi = s->priv_data;
593
    ByteIOContext *pb = s->pb;
594
    int res = 0;
595
    int i, j, n, nb_frames;
596
    int64_t file_size;
597

    
598
    if (!url_is_streamed(pb)){
599
        if (avi->riff_id == 1) {
600
            ff_end_tag(pb, avi->movi_list);
601
            res = avi_write_idx1(s);
602
            ff_end_tag(pb, avi->riff_start);
603
        } else {
604
            avi_write_ix(s);
605
            ff_end_tag(pb, avi->movi_list);
606
            ff_end_tag(pb, avi->riff_start);
607

    
608
            file_size = url_ftell(pb);
609
            url_fseek(pb, avi->odml_list - 8, SEEK_SET);
610
            put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
611
            url_fskip(pb, 16);
612

    
613
            for (n=nb_frames=0;n<s->nb_streams;n++) {
614
                AVCodecContext *stream = s->streams[n]->codec;
615
                AVIStream *avist= s->streams[n]->priv_data;
616

    
617
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
618
                    if (nb_frames < avist->packet_count)
619
                        nb_frames = avist->packet_count;
620
                } else {
621
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
622
                        nb_frames += avist->packet_count;
623
                    }
624
                }
625
            }
626
            put_le32(pb, nb_frames);
627
            url_fseek(pb, file_size, SEEK_SET);
628

    
629
            avi_write_counters(s, avi->riff_id);
630
        }
631
    }
632
    put_flush_packet(pb);
633

    
634
    for (i=0; i<s->nb_streams; i++) {
635
         AVIStream *avist= s->streams[i]->priv_data;
636
         for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
637
              av_free(avist->indexes.cluster[j]);
638
         av_freep(&avist->indexes.cluster);
639
         avist->indexes.ents_allocated = avist->indexes.entry = 0;
640
    }
641

    
642
    return res;
643
}
644

    
645
AVOutputFormat avi_muxer = {
646
    "avi",
647
    NULL_IF_CONFIG_SMALL("AVI format"),
648
    "video/x-msvideo",
649
    "avi",
650
    sizeof(AVIContext),
651
    CODEC_ID_MP2,
652
    CODEC_ID_MPEG4,
653
    avi_write_header,
654
    avi_write_packet,
655
    avi_write_trailer,
656
    .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
657
    .flags= AVFMT_VARIABLE_FPS,
658
};