]> git.sesse.net Git - ffmpeg/blob - libavformat/avienc.c
Merge commit '2b8dd371e4d276ca0d342e82b8b4cc281be0630a'
[ffmpeg] / libavformat / avienc.c
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 "internal.h"
23 #include "avi.h"
24 #include "avio_internal.h"
25 #include "riff.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/avassert.h"
29
30 /*
31  * TODO:
32  *  - fill all fields if non streamed (nb_frames for example)
33  */
34
35 typedef struct AVIIentry {
36     unsigned int flags, pos, len;
37 } AVIIentry;
38
39 #define AVI_INDEX_CLUSTER_SIZE 16384
40
41 typedef struct AVIIndex {
42     int64_t     indx_start;
43     int         entry;
44     int         ents_allocated;
45     AVIIentry** cluster;
46 } AVIIndex;
47
48 typedef struct {
49     int64_t riff_start, movi_list, odml_list;
50     int64_t frames_hdr_all;
51     int riff_id;
52 } AVIContext;
53
54 typedef struct  {
55     int64_t frames_hdr_strm;
56     int audio_strm_length;
57     int packet_count;
58     int entry;
59
60     AVIIndex indexes;
61 } AVIStream ;
62
63 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
64 {
65     int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
66     int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
67     return &idx->cluster[cl][id];
68 }
69
70 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
71                                   const char* riff_tag, const char* list_tag)
72 {
73     AVIContext *avi= s->priv_data;
74     int64_t loff;
75     int i;
76
77     avi->riff_id++;
78     for (i=0; i<s->nb_streams; i++){
79         AVIStream *avist= s->streams[i]->priv_data;
80         avist->indexes.entry = 0;
81     }
82
83     avi->riff_start = ff_start_tag(pb, "RIFF");
84     ffio_wfourcc(pb, riff_tag);
85     loff = ff_start_tag(pb, "LIST");
86     ffio_wfourcc(pb, list_tag);
87     return loff;
88 }
89
90 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
91 {
92     tag[0] = '0' + index/10;
93     tag[1] = '0' + index%10;
94     if (type == AVMEDIA_TYPE_VIDEO) {
95         tag[2] = 'd';
96         tag[3] = 'c';
97     } else if (type == AVMEDIA_TYPE_SUBTITLE) {
98         // note: this is not an official code
99         tag[2] = 's';
100         tag[3] = 'b';
101     } else {
102         tag[2] = 'w';
103         tag[3] = 'b';
104     }
105     tag[4] = '\0';
106     return tag;
107 }
108
109 static int avi_write_counters(AVFormatContext* s, int riff_id)
110 {
111     AVIOContext *pb = s->pb;
112     AVIContext *avi = s->priv_data;
113     int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
114     int64_t file_size;
115     AVCodecContext* stream;
116
117     file_size = avio_tell(pb);
118     for(n = 0; n < s->nb_streams; n++) {
119         AVIStream *avist= s->streams[n]->priv_data;
120
121         av_assert0(avist->frames_hdr_strm);
122         stream = s->streams[n]->codec;
123         avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
124         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
125         if(au_ssize == 0) {
126             avio_wl32(pb, avist->packet_count);
127         } else {
128             avio_wl32(pb, avist->audio_strm_length / au_ssize);
129         }
130         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
131             nb_frames = FFMAX(nb_frames, avist->packet_count);
132     }
133     if(riff_id == 1) {
134         av_assert0(avi->frames_hdr_all);
135         avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
136         avio_wl32(pb, nb_frames);
137     }
138     avio_seek(pb, file_size, SEEK_SET);
139
140     return 0;
141 }
142
143 static int avi_write_header(AVFormatContext *s)
144 {
145     AVIContext *avi = s->priv_data;
146     AVIOContext *pb = s->pb;
147     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
148     AVCodecContext *stream, *video_enc;
149     int64_t list1, list2, strh, strf;
150     AVDictionaryEntry *t = NULL;
151
152     if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
153         av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
154                AVI_MAX_STREAM_COUNT);
155         return -1;
156     }
157
158     for(n=0;n<s->nb_streams;n++) {
159         s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
160         if(!s->streams[n]->priv_data)
161             return AVERROR(ENOMEM);
162     }
163
164     /* header list */
165     avi->riff_id = 0;
166     list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
167
168     /* avi header */
169     ffio_wfourcc(pb, "avih");
170     avio_wl32(pb, 14 * 4);
171     bitrate = 0;
172
173     video_enc = NULL;
174     for(n=0;n<s->nb_streams;n++) {
175         stream = s->streams[n]->codec;
176         bitrate += stream->bit_rate;
177         if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
178             video_enc = stream;
179     }
180
181     nb_frames = 0;
182
183     if(video_enc){
184         avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
185     } else {
186         avio_wl32(pb, 0);
187     }
188     avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
189     avio_wl32(pb, 0); /* padding */
190     if (!pb->seekable)
191         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
192     else
193         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
194     avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
195     avio_wl32(pb, nb_frames); /* nb frames, filled later */
196     avio_wl32(pb, 0); /* initial frame */
197     avio_wl32(pb, s->nb_streams); /* nb streams */
198     avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
199     if(video_enc){
200         avio_wl32(pb, video_enc->width);
201         avio_wl32(pb, video_enc->height);
202     } else {
203         avio_wl32(pb, 0);
204         avio_wl32(pb, 0);
205     }
206     avio_wl32(pb, 0); /* reserved */
207     avio_wl32(pb, 0); /* reserved */
208     avio_wl32(pb, 0); /* reserved */
209     avio_wl32(pb, 0); /* reserved */
210
211     /* stream list */
212     for(i=0;i<n;i++) {
213         AVIStream *avist= s->streams[i]->priv_data;
214         list2 = ff_start_tag(pb, "LIST");
215         ffio_wfourcc(pb, "strl");
216
217         stream = s->streams[i]->codec;
218
219         /* stream generic header */
220         strh = ff_start_tag(pb, "strh");
221         switch(stream->codec_type) {
222         case AVMEDIA_TYPE_SUBTITLE:
223             // XSUB subtitles behave like video tracks, other subtitles
224             // are not (yet) supported.
225             if (stream->codec_id != AV_CODEC_ID_XSUB) {
226                 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
227                 return AVERROR_PATCHWELCOME;
228             }
229         case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
230         case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
231 //      case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
232         case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
233         }
234         if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
235            stream->codec_id == AV_CODEC_ID_XSUB)
236             avio_wl32(pb, stream->codec_tag);
237         else
238             avio_wl32(pb, 1);
239         avio_wl32(pb, 0); /* flags */
240         avio_wl16(pb, 0); /* priority */
241         avio_wl16(pb, 0); /* language */
242         avio_wl32(pb, 0); /* initial frame */
243
244         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
245
246         avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
247         if(stream->codec_id == AV_CODEC_ID_XSUB)
248             au_scale = au_byterate = 0;
249
250         avio_wl32(pb, au_scale); /* scale */
251         avio_wl32(pb, au_byterate); /* rate */
252
253         avio_wl32(pb, 0); /* start */
254         avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
255         if (!pb->seekable)
256             avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
257         else
258             avio_wl32(pb, 0); /* length, XXX: filled later */
259
260         /* suggested buffer size */ //FIXME set at the end to largest chunk
261         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
262             avio_wl32(pb, 1024 * 1024);
263         else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
264             avio_wl32(pb, 12 * 1024);
265         else
266             avio_wl32(pb, 0);
267         avio_wl32(pb, -1); /* quality */
268         avio_wl32(pb, au_ssize); /* sample size */
269         avio_wl32(pb, 0);
270         avio_wl16(pb, stream->width);
271         avio_wl16(pb, stream->height);
272         ff_end_tag(pb, strh);
273
274       if(stream->codec_type != AVMEDIA_TYPE_DATA){
275         strf = ff_start_tag(pb, "strf");
276         switch(stream->codec_type) {
277         case AVMEDIA_TYPE_SUBTITLE:
278             // XSUB subtitles behave like video tracks, other subtitles
279             // are not (yet) supported.
280             if (stream->codec_id != AV_CODEC_ID_XSUB) break;
281         case AVMEDIA_TYPE_VIDEO:
282             ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
283             break;
284         case AVMEDIA_TYPE_AUDIO:
285             if (ff_put_wav_header(pb, stream) < 0) {
286                 return -1;
287             }
288             break;
289         default:
290             return -1;
291         }
292         ff_end_tag(pb, strf);
293         if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
294             ff_riff_write_info_tag(s->pb, "strn", t->value);
295             t = NULL;
296         }
297       }
298
299         if (pb->seekable) {
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             avio_wl16(pb, 4);        /* wLongsPerEntry */
311             avio_w8(pb, 0);          /* bIndexSubType (0 == frame index) */
312             avio_w8(pb, 0);          /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
313             avio_wl32(pb, 0);        /* nEntriesInUse (will fill out later on) */
314             ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
315                                     /* dwChunkId */
316             avio_wl64(pb, 0);        /* dwReserved[3]
317             avio_wl32(pb, 0);           Must be 0.    */
318             for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
319                  avio_wl64(pb, 0);
320             ff_end_tag(pb, avist->indexes.indx_start);
321         }
322
323         if(   stream->codec_type == AVMEDIA_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             avio_wl32(pb, 0); //video format  = unknown
333             avio_wl32(pb, 0); //video standard= unknown
334             avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
335             avio_wl32(pb, stream->width );
336             avio_wl32(pb, stream->height);
337             avio_wl16(pb, den);
338             avio_wl16(pb, num);
339             avio_wl32(pb, stream->width );
340             avio_wl32(pb, stream->height);
341             avio_wl32(pb, 1); //progressive FIXME
342
343             avio_wl32(pb, stream->height);
344             avio_wl32(pb, stream->width );
345             avio_wl32(pb, stream->height);
346             avio_wl32(pb, stream->width );
347             avio_wl32(pb, 0);
348             avio_wl32(pb, 0);
349
350             avio_wl32(pb, 0);
351             avio_wl32(pb, 0);
352             ff_end_tag(pb, vprp);
353         }
354
355         ff_end_tag(pb, list2);
356     }
357
358     if (pb->seekable) {
359         /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
360         avi->odml_list = ff_start_tag(pb, "JUNK");
361         ffio_wfourcc(pb, "odml");
362         ffio_wfourcc(pb, "dmlh");
363         avio_wl32(pb, 248);
364         for (i = 0; i < 248; i+= 4)
365              avio_wl32(pb, 0);
366         ff_end_tag(pb, avi->odml_list);
367     }
368
369     ff_end_tag(pb, list1);
370
371     ff_riff_write_info(s);
372
373     /* some padding for easier tag editing */
374     list2 = ff_start_tag(pb, "JUNK");
375     for (i = 0; i < 1016; i += 4)
376         avio_wl32(pb, 0);
377     ff_end_tag(pb, list2);
378
379     avi->movi_list = ff_start_tag(pb, "LIST");
380     ffio_wfourcc(pb, "movi");
381
382     avio_flush(pb);
383
384     return 0;
385 }
386
387 static int avi_write_ix(AVFormatContext *s)
388 {
389     AVIOContext *pb = s->pb;
390     AVIContext *avi = s->priv_data;
391     char tag[5];
392     char ix_tag[] = "ix00";
393     int i, j;
394
395     av_assert0(pb->seekable);
396
397     if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
398         return -1;
399
400     for (i=0;i<s->nb_streams;i++) {
401         AVIStream *avist= s->streams[i]->priv_data;
402          int64_t ix, pos;
403
404          avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
405          ix_tag[3] = '0' + i;
406
407          /* Writing AVI OpenDML leaf index chunk */
408          ix = avio_tell(pb);
409          ffio_wfourcc(pb, ix_tag);     /* ix?? */
410          avio_wl32(pb, avist->indexes.entry * 8 + 24);
411                                       /* chunk size */
412          avio_wl16(pb, 2);             /* wLongsPerEntry */
413          avio_w8(pb, 0);             /* bIndexSubType (0 == frame index) */
414          avio_w8(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
415          avio_wl32(pb, avist->indexes.entry);
416                                       /* nEntriesInUse */
417          ffio_wfourcc(pb, tag);        /* dwChunkId */
418          avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
419          avio_wl32(pb, 0);             /* dwReserved_3 (must be 0) */
420
421          for (j=0; j<avist->indexes.entry; j++) {
422              AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
423              avio_wl32(pb, ie->pos + 8);
424              avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
425                           (ie->flags & 0x10 ? 0 : 0x80000000));
426          }
427          avio_flush(pb);
428          pos = avio_tell(pb);
429
430          /* Updating one entry in the AVI OpenDML master index */
431          avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
432          ffio_wfourcc(pb, "indx");            /* enabling this entry */
433          avio_skip(pb, 8);
434          avio_wl32(pb, avi->riff_id);         /* nEntriesInUse */
435          avio_skip(pb, 16*avi->riff_id);
436          avio_wl64(pb, ix);                   /* qwOffset */
437          avio_wl32(pb, pos - ix);             /* dwSize */
438          avio_wl32(pb, avist->indexes.entry); /* dwDuration */
439
440          avio_seek(pb, pos, SEEK_SET);
441     }
442     return 0;
443 }
444
445 static int avi_write_idx1(AVFormatContext *s)
446 {
447     AVIOContext *pb = s->pb;
448     AVIContext *avi = s->priv_data;
449     int64_t idx_chunk;
450     int i;
451     char tag[5];
452
453     if (pb->seekable) {
454         AVIStream *avist;
455         AVIIentry* ie = 0, *tie;
456         int empty, stream_id = -1;
457
458         idx_chunk = ff_start_tag(pb, "idx1");
459         for(i=0; i<s->nb_streams; i++){
460             avist= s->streams[i]->priv_data;
461             avist->entry=0;
462         }
463
464         do {
465             empty = 1;
466             for (i=0; i<s->nb_streams; i++) {
467                 avist= s->streams[i]->priv_data;
468                  if (avist->indexes.entry <= avist->entry)
469                      continue;
470
471                  tie = avi_get_ientry(&avist->indexes, avist->entry);
472                  if (empty || tie->pos < ie->pos) {
473                      ie = tie;
474                      stream_id = i;
475                  }
476                  empty = 0;
477             }
478             if (!empty) {
479                 avist= s->streams[stream_id]->priv_data;
480                 avi_stream2fourcc(tag, stream_id,
481                                   s->streams[stream_id]->codec->codec_type);
482                 ffio_wfourcc(pb, tag);
483                 avio_wl32(pb, ie->flags);
484                 avio_wl32(pb, ie->pos);
485                 avio_wl32(pb, ie->len);
486                 avist->entry++;
487             }
488         } while (!empty);
489         ff_end_tag(pb, idx_chunk);
490
491         avi_write_counters(s, avi->riff_id);
492     }
493     return 0;
494 }
495
496 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
497 {
498     AVIContext *avi = s->priv_data;
499     AVIOContext *pb = s->pb;
500     unsigned char tag[5];
501     unsigned int flags=0;
502     const int stream_index= pkt->stream_index;
503     AVIStream *avist= s->streams[stream_index]->priv_data;
504     AVCodecContext *enc= s->streams[stream_index]->codec;
505     int size= pkt->size;
506
507 //    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
508     while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB){
509         AVPacket empty_packet;
510
511         if(pkt->dts - avist->packet_count > 60000){
512             av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %"PRId64"\n", pkt->dts - avist->packet_count);
513             return AVERROR(EINVAL);
514         }
515
516         av_init_packet(&empty_packet);
517         empty_packet.size= 0;
518         empty_packet.data= NULL;
519         empty_packet.stream_index= stream_index;
520         avi_write_packet(s, &empty_packet);
521 //        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
522     }
523     avist->packet_count++;
524
525     // Make sure to put an OpenDML chunk when the file size exceeds the limits
526     if (pb->seekable &&
527         (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
528
529         avi_write_ix(s);
530         ff_end_tag(pb, avi->movi_list);
531
532         if (avi->riff_id == 1)
533             avi_write_idx1(s);
534
535         ff_end_tag(pb, avi->riff_start);
536         avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
537     }
538
539     avi_stream2fourcc(tag, stream_index, enc->codec_type);
540     if(pkt->flags&AV_PKT_FLAG_KEY)
541         flags = 0x10;
542     if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
543        avist->audio_strm_length += size;
544     }
545
546     if (s->pb->seekable) {
547         AVIIndex* idx = &avist->indexes;
548         int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
549         int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
550         if (idx->ents_allocated <= idx->entry) {
551             idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
552             if (!idx->cluster)
553                 return -1;
554             idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
555             if (!idx->cluster[cl])
556                 return -1;
557             idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
558         }
559
560         idx->cluster[cl][id].flags = flags;
561         idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
562         idx->cluster[cl][id].len = size;
563         idx->entry++;
564     }
565
566     avio_write(pb, tag, 4);
567     avio_wl32(pb, size);
568     avio_write(pb, pkt->data, size);
569     if (size & 1)
570         avio_w8(pb, 0);
571
572     avio_flush(pb);
573     return 0;
574 }
575
576 static int avi_write_trailer(AVFormatContext *s)
577 {
578     AVIContext *avi = s->priv_data;
579     AVIOContext *pb = s->pb;
580     int res = 0;
581     int i, j, n, nb_frames;
582     int64_t file_size;
583
584     if (pb->seekable){
585         if (avi->riff_id == 1) {
586             ff_end_tag(pb, avi->movi_list);
587             res = avi_write_idx1(s);
588             ff_end_tag(pb, avi->riff_start);
589         } else {
590             avi_write_ix(s);
591             ff_end_tag(pb, avi->movi_list);
592             ff_end_tag(pb, avi->riff_start);
593
594             file_size = avio_tell(pb);
595             avio_seek(pb, avi->odml_list - 8, SEEK_SET);
596             ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
597             avio_skip(pb, 16);
598
599             for (n=nb_frames=0;n<s->nb_streams;n++) {
600                 AVCodecContext *stream = s->streams[n]->codec;
601                 AVIStream *avist= s->streams[n]->priv_data;
602
603                 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
604                     if (nb_frames < avist->packet_count)
605                         nb_frames = avist->packet_count;
606                 } else {
607                     if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
608                         nb_frames += avist->packet_count;
609                     }
610                 }
611             }
612             avio_wl32(pb, nb_frames);
613             avio_seek(pb, file_size, SEEK_SET);
614
615             avi_write_counters(s, avi->riff_id);
616         }
617     }
618
619     for (i=0; i<s->nb_streams; i++) {
620          AVIStream *avist= s->streams[i]->priv_data;
621          for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
622               av_free(avist->indexes.cluster[j]);
623          av_freep(&avist->indexes.cluster);
624          avist->indexes.ents_allocated = avist->indexes.entry = 0;
625     }
626
627     return res;
628 }
629
630 AVOutputFormat ff_avi_muxer = {
631     .name              = "avi",
632     .long_name         = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
633     .mime_type         = "video/x-msvideo",
634     .extensions        = "avi",
635     .priv_data_size    = sizeof(AVIContext),
636     .audio_codec       = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
637     .video_codec       = AV_CODEC_ID_MPEG4,
638     .write_header      = avi_write_header,
639     .write_packet      = avi_write_packet,
640     .write_trailer     = avi_write_trailer,
641     .codec_tag         = (const AVCodecTag* const []){
642         ff_codec_bmp_tags, ff_codec_wav_tags, 0
643     },
644     .flags             = AVFMT_VARIABLE_FPS,
645 };