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