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