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"
28 #include "libavutil/avassert.h"
32 * - fill all fields if non streamed (nb_frames for example)
35 typedef struct AVIIentry {
36 unsigned int flags, pos, len;
39 #define AVI_INDEX_CLUSTER_SIZE 16384
41 typedef struct AVIIndex {
49 int64_t riff_start, movi_list, odml_list;
50 int64_t frames_hdr_all;
55 int64_t frames_hdr_strm;
56 int audio_strm_length;
63 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
65 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
66 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
67 return &idx->cluster[cl][id];
70 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
71 const char* riff_tag, const char* list_tag)
73 AVIContext *avi= s->priv_data;
78 for (i=0; i<s->nb_streams; i++){
79 AVIStream *avist= s->streams[i]->priv_data;
80 avist->indexes.entry = 0;
83 avi->riff_start = ff_start_tag(pb, "RIFF");
84 ffio_wfourcc(pb, riff_tag);
85 loff = ff_start_tag(pb, "LIST");
86 ffio_wfourcc(pb, list_tag);
90 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
92 tag[0] = '0' + index/10;
93 tag[1] = '0' + index%10;
94 if (type == AVMEDIA_TYPE_VIDEO) {
97 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
98 // note: this is not an official code
109 static int avi_write_counters(AVFormatContext* s, int riff_id)
111 AVIOContext *pb = s->pb;
112 AVIContext *avi = s->priv_data;
113 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
115 AVCodecContext* stream;
117 file_size = avio_tell(pb);
118 for(n = 0; n < s->nb_streams; n++) {
119 AVIStream *avist= s->streams[n]->priv_data;
121 av_assert0(avist->frames_hdr_strm);
122 stream = s->streams[n]->codec;
123 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
124 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
126 avio_wl32(pb, avist->packet_count);
128 avio_wl32(pb, avist->audio_strm_length / au_ssize);
130 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
131 nb_frames = FFMAX(nb_frames, avist->packet_count);
134 av_assert0(avi->frames_hdr_all);
135 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
136 avio_wl32(pb, nb_frames);
138 avio_seek(pb, file_size, SEEK_SET);
143 static int avi_write_header(AVFormatContext *s)
145 AVIContext *avi = s->priv_data;
146 AVIOContext *pb = s->pb;
147 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
148 AVCodecContext *stream, *video_enc;
149 int64_t list1, list2, strh, strf;
150 AVDictionaryEntry *t = NULL;
152 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
153 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
154 AVI_MAX_STREAM_COUNT);
158 for(n=0;n<s->nb_streams;n++) {
159 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
160 if(!s->streams[n]->priv_data)
161 return AVERROR(ENOMEM);
166 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
169 ffio_wfourcc(pb, "avih");
170 avio_wl32(pb, 14 * 4);
174 for(n=0;n<s->nb_streams;n++) {
175 stream = s->streams[n]->codec;
176 bitrate += stream->bit_rate;
177 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
184 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
188 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
189 avio_wl32(pb, 0); /* padding */
191 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
193 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
194 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
195 avio_wl32(pb, nb_frames); /* nb frames, filled later */
196 avio_wl32(pb, 0); /* initial frame */
197 avio_wl32(pb, s->nb_streams); /* nb streams */
198 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
200 avio_wl32(pb, video_enc->width);
201 avio_wl32(pb, video_enc->height);
206 avio_wl32(pb, 0); /* reserved */
207 avio_wl32(pb, 0); /* reserved */
208 avio_wl32(pb, 0); /* reserved */
209 avio_wl32(pb, 0); /* reserved */
213 AVIStream *avist= s->streams[i]->priv_data;
214 list2 = ff_start_tag(pb, "LIST");
215 ffio_wfourcc(pb, "strl");
217 stream = s->streams[i]->codec;
219 /* stream generic header */
220 strh = ff_start_tag(pb, "strh");
221 switch(stream->codec_type) {
222 case AVMEDIA_TYPE_SUBTITLE:
223 // XSUB subtitles behave like video tracks, other subtitles
224 // are not (yet) supported.
225 if (stream->codec_id != AV_CODEC_ID_XSUB) {
226 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
227 return AVERROR_PATCHWELCOME;
229 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
230 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
231 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
232 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
234 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
235 stream->codec_id == AV_CODEC_ID_XSUB)
236 avio_wl32(pb, stream->codec_tag);
239 avio_wl32(pb, 0); /* flags */
240 avio_wl16(pb, 0); /* priority */
241 avio_wl16(pb, 0); /* language */
242 avio_wl32(pb, 0); /* initial frame */
244 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
246 avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
247 if(stream->codec_id == AV_CODEC_ID_XSUB)
248 au_scale = au_byterate = 0;
250 avio_wl32(pb, au_scale); /* scale */
251 avio_wl32(pb, au_byterate); /* rate */
253 avio_wl32(pb, 0); /* start */
254 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
256 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
258 avio_wl32(pb, 0); /* length, XXX: filled later */
260 /* suggested buffer size */ //FIXME set at the end to largest chunk
261 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
262 avio_wl32(pb, 1024 * 1024);
263 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
264 avio_wl32(pb, 12 * 1024);
267 avio_wl32(pb, -1); /* quality */
268 avio_wl32(pb, au_ssize); /* sample size */
270 avio_wl16(pb, stream->width);
271 avio_wl16(pb, stream->height);
272 ff_end_tag(pb, strh);
274 if(stream->codec_type != AVMEDIA_TYPE_DATA){
275 strf = ff_start_tag(pb, "strf");
276 switch(stream->codec_type) {
277 case AVMEDIA_TYPE_SUBTITLE:
278 // XSUB subtitles behave like video tracks, other subtitles
279 // are not (yet) supported.
280 if (stream->codec_id != AV_CODEC_ID_XSUB) break;
281 case AVMEDIA_TYPE_VIDEO:
282 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
284 case AVMEDIA_TYPE_AUDIO:
285 if (ff_put_wav_header(pb, stream) < 0) {
292 ff_end_tag(pb, strf);
293 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
294 ff_riff_write_info_tag(s->pb, "strn", t->value);
300 unsigned char tag[5];
303 /* Starting to lay out AVI OpenDML master index.
304 * We want to make it JUNK entry for now, since we'd
305 * like to get away without making AVI an OpenDML one
306 * for compatibility reasons.
308 avist->indexes.entry = avist->indexes.ents_allocated = 0;
309 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
310 avio_wl16(pb, 4); /* wLongsPerEntry */
311 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
312 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
313 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
314 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
316 avio_wl64(pb, 0); /* dwReserved[3]
317 avio_wl32(pb, 0); Must be 0. */
318 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
320 ff_end_tag(pb, avist->indexes.indx_start);
323 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
324 && s->streams[i]->sample_aspect_ratio.num>0
325 && s->streams[i]->sample_aspect_ratio.den>0){
326 int vprp= ff_start_tag(pb, "vprp");
327 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
328 (AVRational){stream->width, stream->height});
330 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
332 avio_wl32(pb, 0); //video format = unknown
333 avio_wl32(pb, 0); //video standard= unknown
334 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
335 avio_wl32(pb, stream->width );
336 avio_wl32(pb, stream->height);
339 avio_wl32(pb, stream->width );
340 avio_wl32(pb, stream->height);
341 avio_wl32(pb, 1); //progressive FIXME
343 avio_wl32(pb, stream->height);
344 avio_wl32(pb, stream->width );
345 avio_wl32(pb, stream->height);
346 avio_wl32(pb, stream->width );
352 ff_end_tag(pb, vprp);
355 ff_end_tag(pb, list2);
359 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
360 avi->odml_list = ff_start_tag(pb, "JUNK");
361 ffio_wfourcc(pb, "odml");
362 ffio_wfourcc(pb, "dmlh");
364 for (i = 0; i < 248; i+= 4)
366 ff_end_tag(pb, avi->odml_list);
369 ff_end_tag(pb, list1);
371 ff_riff_write_info(s);
373 /* some padding for easier tag editing */
374 list2 = ff_start_tag(pb, "JUNK");
375 for (i = 0; i < 1016; i += 4)
377 ff_end_tag(pb, list2);
379 avi->movi_list = ff_start_tag(pb, "LIST");
380 ffio_wfourcc(pb, "movi");
387 static int avi_write_ix(AVFormatContext *s)
389 AVIOContext *pb = s->pb;
390 AVIContext *avi = s->priv_data;
392 char ix_tag[] = "ix00";
395 av_assert0(pb->seekable);
397 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
400 for (i=0;i<s->nb_streams;i++) {
401 AVIStream *avist= s->streams[i]->priv_data;
404 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
407 /* Writing AVI OpenDML leaf index chunk */
409 ffio_wfourcc(pb, ix_tag); /* ix?? */
410 avio_wl32(pb, avist->indexes.entry * 8 + 24);
412 avio_wl16(pb, 2); /* wLongsPerEntry */
413 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
414 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
415 avio_wl32(pb, avist->indexes.entry);
417 ffio_wfourcc(pb, tag); /* dwChunkId */
418 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
419 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
421 for (j=0; j<avist->indexes.entry; j++) {
422 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
423 avio_wl32(pb, ie->pos + 8);
424 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
425 (ie->flags & 0x10 ? 0 : 0x80000000));
430 /* Updating one entry in the AVI OpenDML master index */
431 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
432 ffio_wfourcc(pb, "indx"); /* enabling this entry */
434 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
435 avio_skip(pb, 16*avi->riff_id);
436 avio_wl64(pb, ix); /* qwOffset */
437 avio_wl32(pb, pos - ix); /* dwSize */
438 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
440 avio_seek(pb, pos, SEEK_SET);
445 static int avi_write_idx1(AVFormatContext *s)
447 AVIOContext *pb = s->pb;
448 AVIContext *avi = s->priv_data;
455 AVIIentry* ie = 0, *tie;
456 int empty, stream_id = -1;
458 idx_chunk = ff_start_tag(pb, "idx1");
459 for(i=0; i<s->nb_streams; i++){
460 avist= s->streams[i]->priv_data;
466 for (i=0; i<s->nb_streams; i++) {
467 avist= s->streams[i]->priv_data;
468 if (avist->indexes.entry <= avist->entry)
471 tie = avi_get_ientry(&avist->indexes, avist->entry);
472 if (empty || tie->pos < ie->pos) {
479 avist= s->streams[stream_id]->priv_data;
480 avi_stream2fourcc(tag, stream_id,
481 s->streams[stream_id]->codec->codec_type);
482 ffio_wfourcc(pb, tag);
483 avio_wl32(pb, ie->flags);
484 avio_wl32(pb, ie->pos);
485 avio_wl32(pb, ie->len);
489 ff_end_tag(pb, idx_chunk);
491 avi_write_counters(s, avi->riff_id);
496 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
498 AVIContext *avi = s->priv_data;
499 AVIOContext *pb = s->pb;
500 unsigned char tag[5];
501 unsigned int flags=0;
502 const int stream_index= pkt->stream_index;
503 AVIStream *avist= s->streams[stream_index]->priv_data;
504 AVCodecContext *enc= s->streams[stream_index]->codec;
507 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
508 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != AV_CODEC_ID_XSUB){
509 AVPacket empty_packet;
511 if(pkt->dts - avist->packet_count > 60000){
512 av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %"PRId64"\n", pkt->dts - avist->packet_count);
513 return AVERROR(EINVAL);
516 av_init_packet(&empty_packet);
517 empty_packet.size= 0;
518 empty_packet.data= NULL;
519 empty_packet.stream_index= stream_index;
520 avi_write_packet(s, &empty_packet);
521 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
523 avist->packet_count++;
525 // Make sure to put an OpenDML chunk when the file size exceeds the limits
527 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
530 ff_end_tag(pb, avi->movi_list);
532 if (avi->riff_id == 1)
535 ff_end_tag(pb, avi->riff_start);
536 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
539 avi_stream2fourcc(tag, stream_index, enc->codec_type);
540 if(pkt->flags&AV_PKT_FLAG_KEY)
542 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
543 avist->audio_strm_length += size;
546 if (s->pb->seekable) {
547 AVIIndex* idx = &avist->indexes;
548 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
549 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
550 if (idx->ents_allocated <= idx->entry) {
551 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
554 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
555 if (!idx->cluster[cl])
557 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
560 idx->cluster[cl][id].flags = flags;
561 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
562 idx->cluster[cl][id].len = size;
566 avio_write(pb, tag, 4);
568 avio_write(pb, pkt->data, size);
576 static int avi_write_trailer(AVFormatContext *s)
578 AVIContext *avi = s->priv_data;
579 AVIOContext *pb = s->pb;
581 int i, j, n, nb_frames;
585 if (avi->riff_id == 1) {
586 ff_end_tag(pb, avi->movi_list);
587 res = avi_write_idx1(s);
588 ff_end_tag(pb, avi->riff_start);
591 ff_end_tag(pb, avi->movi_list);
592 ff_end_tag(pb, avi->riff_start);
594 file_size = avio_tell(pb);
595 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
596 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
599 for (n=nb_frames=0;n<s->nb_streams;n++) {
600 AVCodecContext *stream = s->streams[n]->codec;
601 AVIStream *avist= s->streams[n]->priv_data;
603 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
604 if (nb_frames < avist->packet_count)
605 nb_frames = avist->packet_count;
607 if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
608 nb_frames += avist->packet_count;
612 avio_wl32(pb, nb_frames);
613 avio_seek(pb, file_size, SEEK_SET);
615 avi_write_counters(s, avi->riff_id);
619 for (i=0; i<s->nb_streams; i++) {
620 AVIStream *avist= s->streams[i]->priv_data;
621 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
622 av_free(avist->indexes.cluster[j]);
623 av_freep(&avist->indexes.cluster);
624 avist->indexes.ents_allocated = avist->indexes.entry = 0;
630 AVOutputFormat ff_avi_muxer = {
632 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
633 .mime_type = "video/x-msvideo",
635 .priv_data_size = sizeof(AVIContext),
636 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
637 .video_codec = AV_CODEC_ID_MPEG4,
638 .write_header = avi_write_header,
639 .write_packet = avi_write_packet,
640 .write_trailer = avi_write_trailer,
641 .codec_tag = (const AVCodecTag* const []){
642 ff_codec_bmp_tags, ff_codec_wav_tags, 0
644 .flags = AVFMT_VARIABLE_FPS,