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