3 * Copyright (c) 2000 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "avio_internal.h"
30 #include "libavformat/avlanguage.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/timestamp.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavcodec/raw.h"
41 * - fill all fields if non streamed (nb_frames for example)
44 typedef struct AVIIentry {
45 unsigned int flags, pos, len;
48 #define AVI_INDEX_CLUSTER_SIZE 16384
50 typedef struct AVIIndex {
52 int64_t audio_strm_offset;
58 typedef struct AVIContext {
59 int64_t riff_start, movi_list, odml_list;
60 int64_t frames_hdr_all;
64 typedef struct AVIStream {
65 int64_t frames_hdr_strm;
66 int64_t audio_strm_length;
77 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt);
79 static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
81 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
82 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
83 return &idx->cluster[cl][id];
86 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
87 const char *riff_tag, const char *list_tag)
89 AVIContext *avi = s->priv_data;
94 for (i = 0; i < s->nb_streams; i++) {
95 AVIStream *avist = s->streams[i]->priv_data;
96 avist->indexes.audio_strm_offset = avist->audio_strm_length;
97 avist->indexes.entry = 0;
100 avi->riff_start = ff_start_tag(pb, "RIFF");
101 ffio_wfourcc(pb, riff_tag);
102 loff = ff_start_tag(pb, "LIST");
103 ffio_wfourcc(pb, list_tag);
107 static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
109 tag[0] = '0' + index / 10;
110 tag[1] = '0' + index % 10;
111 if (type == AVMEDIA_TYPE_VIDEO) {
114 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
115 // note: this is not an official code
126 static int avi_write_counters(AVFormatContext *s, int riff_id)
128 AVIOContext *pb = s->pb;
129 AVIContext *avi = s->priv_data;
130 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
132 AVCodecContext *stream;
134 file_size = avio_tell(pb);
135 for (n = 0; n < s->nb_streams; n++) {
136 AVIStream *avist = s->streams[n]->priv_data;
138 av_assert0(avist->frames_hdr_strm);
139 stream = s->streams[n]->codec;
140 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
141 ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
143 avio_wl32(pb, avist->packet_count);
145 avio_wl32(pb, avist->audio_strm_length / au_ssize);
146 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
147 nb_frames = FFMAX(nb_frames, avist->packet_count);
150 av_assert0(avi->frames_hdr_all);
151 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
152 avio_wl32(pb, nb_frames);
154 avio_seek(pb, file_size, SEEK_SET);
159 static int avi_write_header(AVFormatContext *s)
161 AVIContext *avi = s->priv_data;
162 AVIOContext *pb = s->pb;
163 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
164 AVCodecContext *video_enc;
165 AVStream *video_st = NULL;
166 int64_t list1, list2, strh, strf;
167 AVDictionaryEntry *t = NULL;
170 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
171 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
172 AVI_MAX_STREAM_COUNT);
173 return AVERROR(EINVAL);
176 for (n = 0; n < s->nb_streams; n++) {
177 s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
178 if (!s->streams[n]->priv_data)
179 return AVERROR(ENOMEM);
184 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
187 ffio_wfourcc(pb, "avih");
188 avio_wl32(pb, 14 * 4);
192 for (n = 0; n < s->nb_streams; n++) {
193 AVCodecContext *codec = s->streams[n]->codec;
194 bitrate += codec->bit_rate;
195 if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
197 video_st = s->streams[n];
203 // TODO: should be avg_frame_rate
205 avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
206 video_st->time_base.den));
209 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
210 avio_wl32(pb, 0); /* padding */
212 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
214 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
215 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
216 avio_wl32(pb, nb_frames); /* nb frames, filled later */
217 avio_wl32(pb, 0); /* initial frame */
218 avio_wl32(pb, s->nb_streams); /* nb streams */
219 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
221 avio_wl32(pb, video_enc->width);
222 avio_wl32(pb, video_enc->height);
227 avio_wl32(pb, 0); /* reserved */
228 avio_wl32(pb, 0); /* reserved */
229 avio_wl32(pb, 0); /* reserved */
230 avio_wl32(pb, 0); /* reserved */
233 for (i = 0; i < n; i++) {
234 AVStream *st = s->streams[i];
235 AVCodecContext *enc = st->codec;
236 AVIStream *avist = st->priv_data;
237 list2 = ff_start_tag(pb, "LIST");
238 ffio_wfourcc(pb, "strl");
240 /* stream generic header */
241 strh = ff_start_tag(pb, "strh");
242 switch (enc->codec_type) {
243 case AVMEDIA_TYPE_SUBTITLE:
244 // XSUB subtitles behave like video tracks, other subtitles
245 // are not (yet) supported.
246 if (enc->codec_id != AV_CODEC_ID_XSUB) {
247 av_log(s, AV_LOG_ERROR,
248 "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
249 return AVERROR_PATCHWELCOME;
251 case AVMEDIA_TYPE_VIDEO:
252 ffio_wfourcc(pb, "vids");
254 case AVMEDIA_TYPE_AUDIO:
255 ffio_wfourcc(pb, "auds");
257 // case AVMEDIA_TYPE_TEXT:
258 // ffio_wfourcc(pb, "txts");
260 case AVMEDIA_TYPE_DATA:
261 ffio_wfourcc(pb, "dats");
264 if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
265 enc->codec_id == AV_CODEC_ID_XSUB)
266 avio_wl32(pb, enc->codec_tag);
269 avio_wl32(pb, 0); /* flags */
270 avio_wl16(pb, 0); /* priority */
271 avio_wl16(pb, 0); /* language */
272 avio_wl32(pb, 0); /* initial frame */
274 ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
276 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
277 && enc->codec_id != AV_CODEC_ID_XSUB
278 && au_byterate > 1000LL*au_scale) {
282 avpriv_set_pts_info(st, 64, au_scale, au_byterate);
283 if (enc->codec_id == AV_CODEC_ID_XSUB)
284 au_scale = au_byterate = 0;
286 avio_wl32(pb, au_scale); /* scale */
287 avio_wl32(pb, au_byterate); /* rate */
289 avio_wl32(pb, 0); /* start */
290 /* remember this offset to fill later */
291 avist->frames_hdr_strm = avio_tell(pb);
293 /* FIXME: this may be broken, but who cares */
294 avio_wl32(pb, AVI_MAX_RIFF_SIZE);
296 avio_wl32(pb, 0); /* length, XXX: filled later */
298 /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
299 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
300 avio_wl32(pb, 1024 * 1024);
301 else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
302 avio_wl32(pb, 12 * 1024);
305 avio_wl32(pb, -1); /* quality */
306 avio_wl32(pb, au_ssize); /* sample size */
308 avio_wl16(pb, enc->width);
309 avio_wl16(pb, enc->height);
310 ff_end_tag(pb, strh);
312 if (enc->codec_type != AVMEDIA_TYPE_DATA) {
314 enum AVPixelFormat pix_fmt;
316 strf = ff_start_tag(pb, "strf");
317 switch (enc->codec_type) {
318 case AVMEDIA_TYPE_SUBTITLE:
319 /* XSUB subtitles behave like video tracks, other subtitles
320 * are not (yet) supported. */
321 if (enc->codec_id != AV_CODEC_ID_XSUB)
323 case AVMEDIA_TYPE_VIDEO:
324 /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
326 && enc->codec_id == AV_CODEC_ID_RAWVIDEO
327 && enc->pix_fmt == AV_PIX_FMT_RGB555LE
328 && enc->bits_per_coded_sample == 15)
329 enc->bits_per_coded_sample = 16;
330 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0, 0);
331 pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
332 enc->bits_per_coded_sample);
334 && enc->codec_id == AV_CODEC_ID_RAWVIDEO
335 && enc->pix_fmt != pix_fmt
336 && enc->pix_fmt != AV_PIX_FMT_NONE)
337 av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
338 av_get_pix_fmt_name(enc->pix_fmt));
340 case AVMEDIA_TYPE_AUDIO:
341 if ((ret = ff_put_wav_header(pb, enc, 0)) < 0)
345 av_log(s, AV_LOG_ERROR,
346 "Invalid or not supported codec type '%s' found in the input\n",
347 (char *)av_x_if_null(av_get_media_type_string(enc->codec_type), "?"));
348 return AVERROR(EINVAL);
350 ff_end_tag(pb, strf);
351 if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
352 ff_riff_write_info_tag(s->pb, "strn", t->value);
355 if (enc->codec_id == AV_CODEC_ID_XSUB
356 && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
357 const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
360 char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
361 ff_riff_write_info_tag(s->pb, "strn", str);
368 unsigned char tag[5];
371 /* Starting to lay out AVI OpenDML master index.
372 * We want to make it JUNK entry for now, since we'd
373 * like to get away without making AVI an OpenDML one
374 * for compatibility reasons. */
375 avist->indexes.entry = avist->indexes.ents_allocated = 0;
376 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
377 avio_wl16(pb, 4); /* wLongsPerEntry */
378 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
379 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
380 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
381 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, enc->codec_type));
383 avio_wl64(pb, 0); /* dwReserved[3] */
384 // avio_wl32(pb, 0); /* Must be 0. */
385 for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
387 ff_end_tag(pb, avist->indexes.indx_start);
390 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
391 st->sample_aspect_ratio.num > 0 &&
392 st->sample_aspect_ratio.den > 0) {
393 int vprp = ff_start_tag(pb, "vprp");
394 AVRational dar = av_mul_q(st->sample_aspect_ratio,
395 (AVRational) { enc->width,
398 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
400 avio_wl32(pb, 0); // video format = unknown
401 avio_wl32(pb, 0); // video standard = unknown
402 // TODO: should be avg_frame_rate
403 avio_wl32(pb, lrintf(1.0 / av_q2d(st->time_base)));
404 avio_wl32(pb, enc->width);
405 avio_wl32(pb, enc->height);
408 avio_wl32(pb, enc->width);
409 avio_wl32(pb, enc->height);
410 avio_wl32(pb, 1); // progressive FIXME
412 avio_wl32(pb, enc->height);
413 avio_wl32(pb, enc->width);
414 avio_wl32(pb, enc->height);
415 avio_wl32(pb, enc->width);
421 ff_end_tag(pb, vprp);
424 ff_end_tag(pb, list2);
428 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
429 avi->odml_list = ff_start_tag(pb, "JUNK");
430 ffio_wfourcc(pb, "odml");
431 ffio_wfourcc(pb, "dmlh");
433 for (i = 0; i < 248; i += 4)
435 ff_end_tag(pb, avi->odml_list);
438 ff_end_tag(pb, list1);
440 ff_riff_write_info(s);
443 padding = s->metadata_header_padding;
447 /* some padding for easier tag editing */
449 list2 = ff_start_tag(pb, "JUNK");
450 for (i = padding; i > 0; i -= 4)
452 ff_end_tag(pb, list2);
455 avi->movi_list = ff_start_tag(pb, "LIST");
456 ffio_wfourcc(pb, "movi");
463 static int avi_write_ix(AVFormatContext *s)
465 AVIOContext *pb = s->pb;
466 AVIContext *avi = s->priv_data;
468 char ix_tag[] = "ix00";
471 av_assert0(pb->seekable);
473 if (avi->riff_id > AVI_MASTER_INDEX_SIZE) {
474 av_log(s, AV_LOG_ERROR, "Invalid riff index %d > %d\n",
475 avi->riff_id, AVI_MASTER_INDEX_SIZE);
476 return AVERROR(EINVAL);
479 for (i = 0; i < s->nb_streams; i++) {
480 AVIStream *avist = s->streams[i]->priv_data;
482 int au_byterate, au_ssize, au_scale;
484 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
487 /* Writing AVI OpenDML leaf index chunk */
489 ffio_wfourcc(pb, ix_tag); /* ix?? */
490 avio_wl32(pb, avist->indexes.entry * 8 + 24);
492 avio_wl16(pb, 2); /* wLongsPerEntry */
493 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
494 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
495 avio_wl32(pb, avist->indexes.entry);
497 ffio_wfourcc(pb, tag); /* dwChunkId */
498 avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
499 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
501 for (j = 0; j < avist->indexes.entry; j++) {
502 AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
503 avio_wl32(pb, ie->pos + 8);
504 avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
505 (ie->flags & 0x10 ? 0 : 0x80000000));
510 /* Updating one entry in the AVI OpenDML master index */
511 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
512 ffio_wfourcc(pb, "indx"); /* enabling this entry */
514 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
515 avio_skip(pb, 16 * avi->riff_id);
516 avio_wl64(pb, ix); /* qwOffset */
517 avio_wl32(pb, pos - ix); /* dwSize */
518 ff_parse_specific_params(s->streams[i], &au_byterate, &au_ssize, &au_scale);
519 if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
520 uint32_t audio_segm_size = (avist->audio_strm_length - avist->indexes.audio_strm_offset);
521 if ((audio_segm_size % au_ssize > 0) && !avist->sample_requested) {
522 avpriv_request_sample(s, "OpenDML index duration for audio packets with partial frames");
523 avist->sample_requested = 1;
525 avio_wl32(pb, audio_segm_size / au_ssize); /* dwDuration (sample count) */
527 avio_wl32(pb, avist->indexes.entry); /* dwDuration (packet count) */
529 avio_seek(pb, pos, SEEK_SET);
534 static int avi_write_idx1(AVFormatContext *s)
536 AVIOContext *pb = s->pb;
537 AVIContext *avi = s->priv_data;
544 AVIIentry *ie = 0, *tie;
545 int empty, stream_id = -1;
547 idx_chunk = ff_start_tag(pb, "idx1");
548 for (i = 0; i < s->nb_streams; i++) {
549 avist = s->streams[i]->priv_data;
555 for (i = 0; i < s->nb_streams; i++) {
556 avist = s->streams[i]->priv_data;
557 if (avist->indexes.entry <= avist->entry)
560 tie = avi_get_ientry(&avist->indexes, avist->entry);
561 if (empty || tie->pos < ie->pos) {
568 avist = s->streams[stream_id]->priv_data;
569 avi_stream2fourcc(tag, stream_id,
570 s->streams[stream_id]->codec->codec_type);
571 ffio_wfourcc(pb, tag);
572 avio_wl32(pb, ie->flags);
573 avio_wl32(pb, ie->pos);
574 avio_wl32(pb, ie->len);
578 ff_end_tag(pb, idx_chunk);
580 avi_write_counters(s, avi->riff_id);
585 static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
587 AVIStream *avist = s->streams[stream_index]->priv_data;
588 AVCodecContext *enc = s->streams[stream_index]->codec;
590 av_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
591 while (enc->block_align == 0 && dts != AV_NOPTS_VALUE &&
592 dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
593 AVPacket empty_packet;
595 if (dts - avist->packet_count > 60000) {
596 av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
597 return AVERROR(EINVAL);
600 av_init_packet(&empty_packet);
601 empty_packet.size = 0;
602 empty_packet.data = NULL;
603 empty_packet.stream_index = stream_index;
604 avi_write_packet(s, &empty_packet);
605 av_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
611 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
613 unsigned char tag[5];
614 unsigned int flags = 0;
615 const int stream_index = pkt->stream_index;
616 int size = pkt->size;
617 AVIContext *avi = s->priv_data;
618 AVIOContext *pb = s->pb;
619 AVIStream *avist = s->streams[stream_index]->priv_data;
620 AVCodecContext *enc = s->streams[stream_index]->codec;
623 if (enc->codec_id == AV_CODEC_ID_H264 && enc->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
624 ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
629 if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
632 if (pkt->dts != AV_NOPTS_VALUE)
633 avist->last_dts = pkt->dts + pkt->duration;
635 avist->packet_count++;
637 // Make sure to put an OpenDML chunk when the file size exceeds the limits
639 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
641 ff_end_tag(pb, avi->movi_list);
643 if (avi->riff_id == 1)
646 ff_end_tag(pb, avi->riff_start);
647 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
650 avi_stream2fourcc(tag, stream_index, enc->codec_type);
651 if (pkt->flags & AV_PKT_FLAG_KEY)
653 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
654 avist->audio_strm_length += size;
656 if (s->pb->seekable) {
657 AVIIndex *idx = &avist->indexes;
658 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
659 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
660 if (idx->ents_allocated <= idx->entry) {
661 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
663 idx->ents_allocated = 0;
665 return AVERROR(ENOMEM);
668 av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
669 if (!idx->cluster[cl])
670 return AVERROR(ENOMEM);
671 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
674 idx->cluster[cl][id].flags = flags;
675 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
676 idx->cluster[cl][id].len = size;
677 avist->max_size = FFMAX(avist->max_size, size);
681 avio_write(pb, tag, 4);
683 avio_write(pb, pkt->data, size);
690 static int avi_write_trailer(AVFormatContext *s)
692 AVIContext *avi = s->priv_data;
693 AVIOContext *pb = s->pb;
695 int i, j, n, nb_frames;
698 for (i = 0; i < s->nb_streams; i++) {
699 AVIStream *avist = s->streams[i]->priv_data;
700 write_skip_frames(s, i, avist->last_dts);
704 if (avi->riff_id == 1) {
705 ff_end_tag(pb, avi->movi_list);
706 res = avi_write_idx1(s);
707 ff_end_tag(pb, avi->riff_start);
710 ff_end_tag(pb, avi->movi_list);
711 ff_end_tag(pb, avi->riff_start);
713 file_size = avio_tell(pb);
714 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
715 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
718 for (n = nb_frames = 0; n < s->nb_streams; n++) {
719 AVCodecContext *stream = s->streams[n]->codec;
720 AVIStream *avist = s->streams[n]->priv_data;
722 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
723 if (nb_frames < avist->packet_count)
724 nb_frames = avist->packet_count;
726 if (stream->codec_id == AV_CODEC_ID_MP2 ||
727 stream->codec_id == AV_CODEC_ID_MP3)
728 nb_frames += avist->packet_count;
731 avio_wl32(pb, nb_frames);
732 avio_seek(pb, file_size, SEEK_SET);
734 avi_write_counters(s, avi->riff_id);
738 for (i = 0; i < s->nb_streams; i++) {
739 AVIStream *avist = s->streams[i]->priv_data;
740 for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
741 av_freep(&avist->indexes.cluster[j]);
742 av_freep(&avist->indexes.cluster);
743 avist->indexes.ents_allocated = avist->indexes.entry = 0;
745 avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
746 avio_wl32(pb, avist->max_size);
753 AVOutputFormat ff_avi_muxer = {
755 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
756 .mime_type = "video/x-msvideo",
758 .priv_data_size = sizeof(AVIContext),
759 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
760 .video_codec = AV_CODEC_ID_MPEG4,
761 .write_header = avi_write_header,
762 .write_packet = avi_write_packet,
763 .write_trailer = avi_write_trailer,
764 .codec_tag = (const AVCodecTag * const []) {
765 ff_codec_bmp_tags, ff_codec_wav_tags, 0