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
23 #include "avio_internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
30 * - fill all fields if non streamed (nb_frames for example)
33 typedef struct AVIIentry {
34 unsigned int flags, pos, len;
37 #define AVI_INDEX_CLUSTER_SIZE 16384
39 typedef struct AVIIndex {
47 int64_t riff_start, movi_list, odml_list;
48 int64_t frames_hdr_all;
53 int64_t frames_hdr_strm;
54 int audio_strm_length;
61 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
63 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
64 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
65 return &idx->cluster[cl][id];
68 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
69 const char* riff_tag, const char* list_tag)
71 AVIContext *avi= s->priv_data;
76 for (i=0; i<s->nb_streams; i++){
77 AVIStream *avist= s->streams[i]->priv_data;
78 avist->indexes.entry = 0;
81 avi->riff_start = ff_start_tag(pb, "RIFF");
82 ffio_wfourcc(pb, riff_tag);
83 loff = ff_start_tag(pb, "LIST");
84 ffio_wfourcc(pb, list_tag);
88 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
90 tag[0] = '0' + index/10;
91 tag[1] = '0' + index%10;
92 if (type == AVMEDIA_TYPE_VIDEO) {
95 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
96 // note: this is not an official code
107 static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
109 int len = strlen(str);
112 ffio_wfourcc(pb, tag);
114 avio_put_str(pb, str);
120 static int avi_write_counters(AVFormatContext* s, int riff_id)
122 AVIOContext *pb = s->pb;
123 AVIContext *avi = s->priv_data;
124 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
126 AVCodecContext* stream;
128 file_size = avio_tell(pb);
129 for(n = 0; n < s->nb_streams; n++) {
130 AVIStream *avist= s->streams[n]->priv_data;
132 assert(avist->frames_hdr_strm);
133 stream = s->streams[n]->codec;
134 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
135 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
137 avio_wl32(pb, avist->packet_count);
139 avio_wl32(pb, avist->audio_strm_length / au_ssize);
141 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
142 nb_frames = FFMAX(nb_frames, avist->packet_count);
145 assert(avi->frames_hdr_all);
146 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
147 avio_wl32(pb, nb_frames);
149 avio_seek(pb, file_size, SEEK_SET);
154 static int avi_write_header(AVFormatContext *s)
156 AVIContext *avi = s->priv_data;
157 AVIOContext *pb = s->pb;
158 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
159 AVCodecContext *stream, *video_enc;
160 int64_t list1, list2, strh, strf;
161 AVDictionaryEntry *t = NULL;
163 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
164 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
165 AVI_MAX_STREAM_COUNT);
169 for(n=0;n<s->nb_streams;n++) {
170 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
171 if(!s->streams[n]->priv_data)
172 return AVERROR(ENOMEM);
177 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
180 ffio_wfourcc(pb, "avih");
181 avio_wl32(pb, 14 * 4);
185 for(n=0;n<s->nb_streams;n++) {
186 stream = s->streams[n]->codec;
187 bitrate += stream->bit_rate;
188 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
195 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
199 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
200 avio_wl32(pb, 0); /* padding */
202 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
204 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
205 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
206 avio_wl32(pb, nb_frames); /* nb frames, filled later */
207 avio_wl32(pb, 0); /* initial frame */
208 avio_wl32(pb, s->nb_streams); /* nb streams */
209 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
211 avio_wl32(pb, video_enc->width);
212 avio_wl32(pb, video_enc->height);
217 avio_wl32(pb, 0); /* reserved */
218 avio_wl32(pb, 0); /* reserved */
219 avio_wl32(pb, 0); /* reserved */
220 avio_wl32(pb, 0); /* reserved */
224 AVIStream *avist= s->streams[i]->priv_data;
225 list2 = ff_start_tag(pb, "LIST");
226 ffio_wfourcc(pb, "strl");
228 stream = s->streams[i]->codec;
230 /* stream generic header */
231 strh = ff_start_tag(pb, "strh");
232 switch(stream->codec_type) {
233 case AVMEDIA_TYPE_SUBTITLE:
234 // XSUB subtitles behave like video tracks, other subtitles
235 // are not (yet) supported.
236 if (stream->codec_id != CODEC_ID_XSUB) {
237 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
238 return AVERROR_PATCHWELCOME;
240 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
241 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
242 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
243 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
245 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
246 stream->codec_id == CODEC_ID_XSUB)
247 avio_wl32(pb, stream->codec_tag);
250 avio_wl32(pb, 0); /* flags */
251 avio_wl16(pb, 0); /* priority */
252 avio_wl16(pb, 0); /* language */
253 avio_wl32(pb, 0); /* initial frame */
255 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
257 avio_wl32(pb, au_scale); /* scale */
258 avio_wl32(pb, au_byterate); /* rate */
259 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
261 avio_wl32(pb, 0); /* start */
262 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
264 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
266 avio_wl32(pb, 0); /* length, XXX: filled later */
268 /* suggested buffer size */ //FIXME set at the end to largest chunk
269 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
270 avio_wl32(pb, 1024 * 1024);
271 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
272 avio_wl32(pb, 12 * 1024);
275 avio_wl32(pb, -1); /* quality */
276 avio_wl32(pb, au_ssize); /* sample size */
278 avio_wl16(pb, stream->width);
279 avio_wl16(pb, stream->height);
280 ff_end_tag(pb, strh);
282 if(stream->codec_type != AVMEDIA_TYPE_DATA){
283 strf = ff_start_tag(pb, "strf");
284 switch(stream->codec_type) {
285 case AVMEDIA_TYPE_SUBTITLE:
286 // XSUB subtitles behave like video tracks, other subtitles
287 // are not (yet) supported.
288 if (stream->codec_id != CODEC_ID_XSUB) break;
289 case AVMEDIA_TYPE_VIDEO:
290 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
292 case AVMEDIA_TYPE_AUDIO:
293 if (ff_put_wav_header(pb, stream) < 0) {
300 ff_end_tag(pb, strf);
301 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
302 avi_write_info_tag(s->pb, "strn", t->value);
308 unsigned char tag[5];
311 /* Starting to lay out AVI OpenDML master index.
312 * We want to make it JUNK entry for now, since we'd
313 * like to get away without making AVI an OpenDML one
314 * 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, stream->codec_type));
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++)
328 ff_end_tag(pb, avist->indexes.indx_start);
331 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
332 && s->streams[i]->sample_aspect_ratio.num>0
333 && s->streams[i]->sample_aspect_ratio.den>0){
334 int vprp= ff_start_tag(pb, "vprp");
335 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
336 (AVRational){stream->width, stream->height});
338 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
340 avio_wl32(pb, 0); //video format = unknown
341 avio_wl32(pb, 0); //video standard= unknown
342 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
343 avio_wl32(pb, stream->width );
344 avio_wl32(pb, stream->height);
347 avio_wl32(pb, stream->width );
348 avio_wl32(pb, stream->height);
349 avio_wl32(pb, 1); //progressive FIXME
351 avio_wl32(pb, stream->height);
352 avio_wl32(pb, stream->width );
353 avio_wl32(pb, stream->height);
354 avio_wl32(pb, stream->width );
360 ff_end_tag(pb, vprp);
363 ff_end_tag(pb, list2);
367 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
368 avi->odml_list = ff_start_tag(pb, "JUNK");
369 ffio_wfourcc(pb, "odml");
370 ffio_wfourcc(pb, "dmlh");
372 for (i = 0; i < 248; i+= 4)
374 ff_end_tag(pb, avi->odml_list);
377 ff_end_tag(pb, list1);
379 list2 = ff_start_tag(pb, "LIST");
380 ffio_wfourcc(pb, "INFO");
381 ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
382 for (i = 0; *ff_avi_tags[i]; i++) {
383 if ((t = av_dict_get(s->metadata, ff_avi_tags[i], NULL, AV_DICT_MATCH_CASE)))
384 avi_write_info_tag(s->pb, t->key, t->value);
386 ff_end_tag(pb, list2);
388 /* some padding for easier tag editing */
389 list2 = ff_start_tag(pb, "JUNK");
390 for (i = 0; i < 1016; i += 4)
392 ff_end_tag(pb, list2);
394 avi->movi_list = ff_start_tag(pb, "LIST");
395 ffio_wfourcc(pb, "movi");
402 static int avi_write_ix(AVFormatContext *s)
404 AVIOContext *pb = s->pb;
405 AVIContext *avi = s->priv_data;
407 char ix_tag[] = "ix00";
410 assert(pb->seekable);
412 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
415 for (i=0;i<s->nb_streams;i++) {
416 AVIStream *avist= s->streams[i]->priv_data;
419 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
422 /* Writing AVI OpenDML leaf index chunk */
424 ffio_wfourcc(pb, ix_tag); /* ix?? */
425 avio_wl32(pb, avist->indexes.entry * 8 + 24);
427 avio_wl16(pb, 2); /* wLongsPerEntry */
428 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
429 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
430 avio_wl32(pb, avist->indexes.entry);
432 ffio_wfourcc(pb, tag); /* dwChunkId */
433 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
434 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
436 for (j=0; j<avist->indexes.entry; j++) {
437 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
438 avio_wl32(pb, ie->pos + 8);
439 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
440 (ie->flags & 0x10 ? 0 : 0x80000000));
445 /* Updating one entry in the AVI OpenDML master index */
446 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
447 ffio_wfourcc(pb, "indx"); /* enabling this entry */
449 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
450 avio_skip(pb, 16*avi->riff_id);
451 avio_wl64(pb, ix); /* qwOffset */
452 avio_wl32(pb, pos - ix); /* dwSize */
453 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
455 avio_seek(pb, pos, SEEK_SET);
460 static int avi_write_idx1(AVFormatContext *s)
462 AVIOContext *pb = s->pb;
463 AVIContext *avi = s->priv_data;
470 AVIIentry* ie = 0, *tie;
471 int empty, stream_id = -1;
473 idx_chunk = ff_start_tag(pb, "idx1");
474 for(i=0; i<s->nb_streams; i++){
475 avist= s->streams[i]->priv_data;
481 for (i=0; i<s->nb_streams; i++) {
482 avist= s->streams[i]->priv_data;
483 if (avist->indexes.entry <= avist->entry)
486 tie = avi_get_ientry(&avist->indexes, avist->entry);
487 if (empty || tie->pos < ie->pos) {
494 avist= s->streams[stream_id]->priv_data;
495 avi_stream2fourcc(tag, stream_id,
496 s->streams[stream_id]->codec->codec_type);
497 ffio_wfourcc(pb, tag);
498 avio_wl32(pb, ie->flags);
499 avio_wl32(pb, ie->pos);
500 avio_wl32(pb, ie->len);
504 ff_end_tag(pb, idx_chunk);
506 avi_write_counters(s, avi->riff_id);
511 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
513 AVIContext *avi = s->priv_data;
514 AVIOContext *pb = s->pb;
515 unsigned char tag[5];
516 unsigned int flags=0;
517 const int stream_index= pkt->stream_index;
518 AVIStream *avist= s->streams[stream_index]->priv_data;
519 AVCodecContext *enc= s->streams[stream_index]->codec;
522 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
523 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
524 AVPacket empty_packet;
526 if(pkt->dts - avist->packet_count > 60000){
527 av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %Ld\n", pkt->dts - avist->packet_count);
528 return AVERROR(EINVAL);
531 av_init_packet(&empty_packet);
532 empty_packet.size= 0;
533 empty_packet.data= NULL;
534 empty_packet.stream_index= stream_index;
535 avi_write_packet(s, &empty_packet);
536 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
538 avist->packet_count++;
540 // Make sure to put an OpenDML chunk when the file size exceeds the limits
542 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
545 ff_end_tag(pb, avi->movi_list);
547 if (avi->riff_id == 1)
550 ff_end_tag(pb, avi->riff_start);
551 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
554 avi_stream2fourcc(tag, stream_index, enc->codec_type);
555 if(pkt->flags&AV_PKT_FLAG_KEY)
557 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
558 avist->audio_strm_length += size;
561 if (s->pb->seekable) {
562 AVIIndex* idx = &avist->indexes;
563 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
564 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
565 if (idx->ents_allocated <= idx->entry) {
566 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
569 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
570 if (!idx->cluster[cl])
572 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
575 idx->cluster[cl][id].flags = flags;
576 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
577 idx->cluster[cl][id].len = size;
581 avio_write(pb, tag, 4);
583 avio_write(pb, pkt->data, size);
591 static int avi_write_trailer(AVFormatContext *s)
593 AVIContext *avi = s->priv_data;
594 AVIOContext *pb = s->pb;
596 int i, j, n, nb_frames;
600 if (avi->riff_id == 1) {
601 ff_end_tag(pb, avi->movi_list);
602 res = avi_write_idx1(s);
603 ff_end_tag(pb, avi->riff_start);
606 ff_end_tag(pb, avi->movi_list);
607 ff_end_tag(pb, avi->riff_start);
609 file_size = avio_tell(pb);
610 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
611 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
614 for (n=nb_frames=0;n<s->nb_streams;n++) {
615 AVCodecContext *stream = s->streams[n]->codec;
616 AVIStream *avist= s->streams[n]->priv_data;
618 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
619 if (nb_frames < avist->packet_count)
620 nb_frames = avist->packet_count;
622 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
623 nb_frames += avist->packet_count;
627 avio_wl32(pb, nb_frames);
628 avio_seek(pb, file_size, SEEK_SET);
630 avi_write_counters(s, avi->riff_id);
635 for (i=0; i<s->nb_streams; i++) {
636 AVIStream *avist= s->streams[i]->priv_data;
637 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
638 av_free(avist->indexes.cluster[j]);
639 av_freep(&avist->indexes.cluster);
640 avist->indexes.ents_allocated = avist->indexes.entry = 0;
646 AVOutputFormat ff_avi_muxer = {
648 .long_name = NULL_IF_CONFIG_SMALL("AVI format"),
649 .mime_type = "video/x-msvideo",
651 .priv_data_size = sizeof(AVIContext),
652 #if CONFIG_LIBMP3LAME_ENCODER
653 .audio_codec = CODEC_ID_MP3,
655 .audio_codec = CODEC_ID_AC3,
657 .video_codec = CODEC_ID_MPEG4,
658 .write_header = avi_write_header,
659 .write_packet = avi_write_packet,
660 .write_trailer = avi_write_trailer,
661 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
662 .flags= AVFMT_VARIABLE_FPS,