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