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