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