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
24 #include "avio_internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/dict.h"
31 * - fill all fields if non streamed (nb_frames for example)
34 typedef struct AVIIentry {
35 unsigned int flags, pos, len;
38 #define AVI_INDEX_CLUSTER_SIZE 16384
40 typedef struct AVIIndex {
48 int64_t riff_start, movi_list, odml_list;
49 int64_t frames_hdr_all;
54 int64_t frames_hdr_strm;
55 int audio_strm_length;
62 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
64 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
65 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
66 return &idx->cluster[cl][id];
69 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
70 const char* riff_tag, const char* list_tag)
72 AVIContext *avi= s->priv_data;
77 for (i=0; i<s->nb_streams; i++){
78 AVIStream *avist= s->streams[i]->priv_data;
79 avist->indexes.entry = 0;
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);
89 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
91 tag[0] = '0' + index/10;
92 tag[1] = '0' + index%10;
93 if (type == AVMEDIA_TYPE_VIDEO) {
96 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
97 // note: this is not an official code
108 static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
110 int len = strlen(str);
113 ffio_wfourcc(pb, tag);
115 avio_put_str(pb, str);
121 static int avi_write_counters(AVFormatContext* s, int riff_id)
123 AVIOContext *pb = s->pb;
124 AVIContext *avi = s->priv_data;
125 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
127 AVCodecContext* stream;
129 file_size = avio_tell(pb);
130 for(n = 0; n < s->nb_streams; n++) {
131 AVIStream *avist= s->streams[n]->priv_data;
133 assert(avist->frames_hdr_strm);
134 stream = s->streams[n]->codec;
135 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
136 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
138 avio_wl32(pb, avist->packet_count);
140 avio_wl32(pb, avist->audio_strm_length / au_ssize);
142 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
143 nb_frames = FFMAX(nb_frames, avist->packet_count);
146 assert(avi->frames_hdr_all);
147 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
148 avio_wl32(pb, nb_frames);
150 avio_seek(pb, file_size, SEEK_SET);
155 static int avi_write_header(AVFormatContext *s)
157 AVIContext *avi = s->priv_data;
158 AVIOContext *pb = s->pb;
159 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
160 AVCodecContext *stream, *video_enc;
161 int64_t list1, list2, strh, strf;
162 AVDictionaryEntry *t = NULL;
164 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
165 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
166 AVI_MAX_STREAM_COUNT);
170 for(n=0;n<s->nb_streams;n++) {
171 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
172 if(!s->streams[n]->priv_data)
173 return AVERROR(ENOMEM);
178 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
181 ffio_wfourcc(pb, "avih");
182 avio_wl32(pb, 14 * 4);
186 for(n=0;n<s->nb_streams;n++) {
187 stream = s->streams[n]->codec;
188 bitrate += stream->bit_rate;
189 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
196 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
200 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
201 avio_wl32(pb, 0); /* padding */
203 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
205 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
206 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
207 avio_wl32(pb, nb_frames); /* nb frames, filled later */
208 avio_wl32(pb, 0); /* initial frame */
209 avio_wl32(pb, s->nb_streams); /* nb streams */
210 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
212 avio_wl32(pb, video_enc->width);
213 avio_wl32(pb, video_enc->height);
218 avio_wl32(pb, 0); /* reserved */
219 avio_wl32(pb, 0); /* reserved */
220 avio_wl32(pb, 0); /* reserved */
221 avio_wl32(pb, 0); /* reserved */
225 AVIStream *avist= s->streams[i]->priv_data;
226 list2 = ff_start_tag(pb, "LIST");
227 ffio_wfourcc(pb, "strl");
229 stream = s->streams[i]->codec;
231 /* stream generic header */
232 strh = ff_start_tag(pb, "strh");
233 switch(stream->codec_type) {
234 case AVMEDIA_TYPE_SUBTITLE:
235 // XSUB subtitles behave like video tracks, other subtitles
236 // are not (yet) supported.
237 if (stream->codec_id != CODEC_ID_XSUB) {
238 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
239 return AVERROR_PATCHWELCOME;
241 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
242 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
243 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
244 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
246 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
247 stream->codec_id == CODEC_ID_XSUB)
248 avio_wl32(pb, stream->codec_tag);
251 avio_wl32(pb, 0); /* flags */
252 avio_wl16(pb, 0); /* priority */
253 avio_wl16(pb, 0); /* language */
254 avio_wl32(pb, 0); /* initial frame */
256 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
258 avio_wl32(pb, au_scale); /* scale */
259 avio_wl32(pb, au_byterate); /* rate */
260 avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
262 avio_wl32(pb, 0); /* start */
263 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
265 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
267 avio_wl32(pb, 0); /* length, XXX: filled later */
269 /* suggested buffer size */ //FIXME set at the end to largest chunk
270 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
271 avio_wl32(pb, 1024 * 1024);
272 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
273 avio_wl32(pb, 12 * 1024);
276 avio_wl32(pb, -1); /* quality */
277 avio_wl32(pb, au_ssize); /* sample size */
279 avio_wl16(pb, stream->width);
280 avio_wl16(pb, stream->height);
281 ff_end_tag(pb, strh);
283 if(stream->codec_type != AVMEDIA_TYPE_DATA){
284 strf = ff_start_tag(pb, "strf");
285 switch(stream->codec_type) {
286 case AVMEDIA_TYPE_SUBTITLE:
287 // XSUB subtitles behave like video tracks, other subtitles
288 // are not (yet) supported.
289 if (stream->codec_id != CODEC_ID_XSUB) break;
290 case AVMEDIA_TYPE_VIDEO:
291 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
293 case AVMEDIA_TYPE_AUDIO:
294 if (ff_put_wav_header(pb, stream) < 0) {
301 ff_end_tag(pb, strf);
302 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
303 avi_write_info_tag(s->pb, "strn", t->value);
309 unsigned char tag[5];
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.
317 avist->indexes.entry = avist->indexes.ents_allocated = 0;
318 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
319 avio_wl16(pb, 4); /* wLongsPerEntry */
320 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
321 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
322 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
323 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
325 avio_wl64(pb, 0); /* dwReserved[3]
326 avio_wl32(pb, 0); Must be 0. */
327 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
329 ff_end_tag(pb, avist->indexes.indx_start);
332 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
333 && s->streams[i]->sample_aspect_ratio.num>0
334 && s->streams[i]->sample_aspect_ratio.den>0){
335 int vprp= ff_start_tag(pb, "vprp");
336 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
337 (AVRational){stream->width, stream->height});
339 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
341 avio_wl32(pb, 0); //video format = unknown
342 avio_wl32(pb, 0); //video standard= unknown
343 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
344 avio_wl32(pb, stream->width );
345 avio_wl32(pb, stream->height);
348 avio_wl32(pb, stream->width );
349 avio_wl32(pb, stream->height);
350 avio_wl32(pb, 1); //progressive FIXME
352 avio_wl32(pb, stream->height);
353 avio_wl32(pb, stream->width );
354 avio_wl32(pb, stream->height);
355 avio_wl32(pb, stream->width );
361 ff_end_tag(pb, vprp);
364 ff_end_tag(pb, list2);
368 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
369 avi->odml_list = ff_start_tag(pb, "JUNK");
370 ffio_wfourcc(pb, "odml");
371 ffio_wfourcc(pb, "dmlh");
373 for (i = 0; i < 248; i+= 4)
375 ff_end_tag(pb, avi->odml_list);
378 ff_end_tag(pb, list1);
380 list2 = ff_start_tag(pb, "LIST");
381 ffio_wfourcc(pb, "INFO");
382 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
383 for (i = 0; *ff_riff_tags[i]; i++) {
384 if ((t = av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
385 avi_write_info_tag(s->pb, t->key, t->value);
387 ff_end_tag(pb, list2);
389 /* some padding for easier tag editing */
390 list2 = ff_start_tag(pb, "JUNK");
391 for (i = 0; i < 1016; i += 4)
393 ff_end_tag(pb, list2);
395 avi->movi_list = ff_start_tag(pb, "LIST");
396 ffio_wfourcc(pb, "movi");
403 static int avi_write_ix(AVFormatContext *s)
405 AVIOContext *pb = s->pb;
406 AVIContext *avi = s->priv_data;
408 char ix_tag[] = "ix00";
411 assert(pb->seekable);
413 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
416 for (i=0;i<s->nb_streams;i++) {
417 AVIStream *avist= s->streams[i]->priv_data;
420 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
423 /* Writing AVI OpenDML leaf index chunk */
425 ffio_wfourcc(pb, ix_tag); /* ix?? */
426 avio_wl32(pb, avist->indexes.entry * 8 + 24);
428 avio_wl16(pb, 2); /* wLongsPerEntry */
429 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
430 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
431 avio_wl32(pb, avist->indexes.entry);
433 ffio_wfourcc(pb, tag); /* dwChunkId */
434 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
435 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
437 for (j=0; j<avist->indexes.entry; j++) {
438 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
439 avio_wl32(pb, ie->pos + 8);
440 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
441 (ie->flags & 0x10 ? 0 : 0x80000000));
446 /* Updating one entry in the AVI OpenDML master index */
447 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
448 ffio_wfourcc(pb, "indx"); /* enabling this entry */
450 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
451 avio_skip(pb, 16*avi->riff_id);
452 avio_wl64(pb, ix); /* qwOffset */
453 avio_wl32(pb, pos - ix); /* dwSize */
454 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
456 avio_seek(pb, pos, SEEK_SET);
461 static int avi_write_idx1(AVFormatContext *s)
463 AVIOContext *pb = s->pb;
464 AVIContext *avi = s->priv_data;
471 AVIIentry* ie = 0, *tie;
472 int empty, stream_id = -1;
474 idx_chunk = ff_start_tag(pb, "idx1");
475 for(i=0; i<s->nb_streams; i++){
476 avist= s->streams[i]->priv_data;
482 for (i=0; i<s->nb_streams; i++) {
483 avist= s->streams[i]->priv_data;
484 if (avist->indexes.entry <= avist->entry)
487 tie = avi_get_ientry(&avist->indexes, avist->entry);
488 if (empty || tie->pos < ie->pos) {
495 avist= s->streams[stream_id]->priv_data;
496 avi_stream2fourcc(tag, stream_id,
497 s->streams[stream_id]->codec->codec_type);
498 ffio_wfourcc(pb, tag);
499 avio_wl32(pb, ie->flags);
500 avio_wl32(pb, ie->pos);
501 avio_wl32(pb, ie->len);
505 ff_end_tag(pb, idx_chunk);
507 avi_write_counters(s, avi->riff_id);
512 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
514 AVIContext *avi = s->priv_data;
515 AVIOContext *pb = s->pb;
516 unsigned char tag[5];
517 unsigned int flags=0;
518 const int stream_index= pkt->stream_index;
519 AVIStream *avist= s->streams[stream_index]->priv_data;
520 AVCodecContext *enc= s->streams[stream_index]->codec;
523 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
524 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
525 AVPacket empty_packet;
527 if(pkt->dts - avist->packet_count > 60000){
528 av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %"PRId64"\n", pkt->dts - avist->packet_count);
529 return AVERROR(EINVAL);
532 av_init_packet(&empty_packet);
533 empty_packet.size= 0;
534 empty_packet.data= NULL;
535 empty_packet.stream_index= stream_index;
536 avi_write_packet(s, &empty_packet);
537 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
539 avist->packet_count++;
541 // Make sure to put an OpenDML chunk when the file size exceeds the limits
543 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
546 ff_end_tag(pb, avi->movi_list);
548 if (avi->riff_id == 1)
551 ff_end_tag(pb, avi->riff_start);
552 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
555 avi_stream2fourcc(tag, stream_index, enc->codec_type);
556 if(pkt->flags&AV_PKT_FLAG_KEY)
558 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
559 avist->audio_strm_length += size;
562 if (s->pb->seekable) {
563 AVIIndex* idx = &avist->indexes;
564 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
565 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
566 if (idx->ents_allocated <= idx->entry) {
567 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
570 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
571 if (!idx->cluster[cl])
573 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
576 idx->cluster[cl][id].flags = flags;
577 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
578 idx->cluster[cl][id].len = size;
582 avio_write(pb, tag, 4);
584 avio_write(pb, pkt->data, size);
592 static int avi_write_trailer(AVFormatContext *s)
594 AVIContext *avi = s->priv_data;
595 AVIOContext *pb = s->pb;
597 int i, j, n, nb_frames;
601 if (avi->riff_id == 1) {
602 ff_end_tag(pb, avi->movi_list);
603 res = avi_write_idx1(s);
604 ff_end_tag(pb, avi->riff_start);
607 ff_end_tag(pb, avi->movi_list);
608 ff_end_tag(pb, avi->riff_start);
610 file_size = avio_tell(pb);
611 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
612 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
615 for (n=nb_frames=0;n<s->nb_streams;n++) {
616 AVCodecContext *stream = s->streams[n]->codec;
617 AVIStream *avist= s->streams[n]->priv_data;
619 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
620 if (nb_frames < avist->packet_count)
621 nb_frames = avist->packet_count;
623 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
624 nb_frames += avist->packet_count;
628 avio_wl32(pb, nb_frames);
629 avio_seek(pb, file_size, SEEK_SET);
631 avi_write_counters(s, avi->riff_id);
636 for (i=0; i<s->nb_streams; i++) {
637 AVIStream *avist= s->streams[i]->priv_data;
638 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
639 av_free(avist->indexes.cluster[j]);
640 av_freep(&avist->indexes.cluster);
641 avist->indexes.ents_allocated = avist->indexes.entry = 0;
647 AVOutputFormat ff_avi_muxer = {
649 .long_name = NULL_IF_CONFIG_SMALL("AVI format"),
650 .mime_type = "video/x-msvideo",
652 .priv_data_size = sizeof(AVIContext),
653 #if CONFIG_LIBMP3LAME_ENCODER
654 .audio_codec = CODEC_ID_MP3,
656 .audio_codec = CODEC_ID_AC3,
658 .video_codec = 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 []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
663 .flags= AVFMT_VARIABLE_FPS,