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 avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
259 if(stream->codec_id == CODEC_ID_XSUB)
260 au_scale = au_byterate = 0;
262 avio_wl32(pb, au_scale); /* scale */
263 avio_wl32(pb, au_byterate); /* rate */
265 avio_wl32(pb, 0); /* start */
266 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
268 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
270 avio_wl32(pb, 0); /* length, XXX: filled later */
272 /* suggested buffer size */ //FIXME set at the end to largest chunk
273 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
274 avio_wl32(pb, 1024 * 1024);
275 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
276 avio_wl32(pb, 12 * 1024);
279 avio_wl32(pb, -1); /* quality */
280 avio_wl32(pb, au_ssize); /* sample size */
282 avio_wl16(pb, stream->width);
283 avio_wl16(pb, stream->height);
284 ff_end_tag(pb, strh);
286 if(stream->codec_type != AVMEDIA_TYPE_DATA){
287 strf = ff_start_tag(pb, "strf");
288 switch(stream->codec_type) {
289 case AVMEDIA_TYPE_SUBTITLE:
290 // XSUB subtitles behave like video tracks, other subtitles
291 // are not (yet) supported.
292 if (stream->codec_id != CODEC_ID_XSUB) break;
293 case AVMEDIA_TYPE_VIDEO:
294 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
296 case AVMEDIA_TYPE_AUDIO:
297 if (ff_put_wav_header(pb, stream) < 0) {
304 ff_end_tag(pb, strf);
305 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
306 avi_write_info_tag(s->pb, "strn", t->value);
312 unsigned char tag[5];
315 /* Starting to lay out AVI OpenDML master index.
316 * We want to make it JUNK entry for now, since we'd
317 * like to get away without making AVI an OpenDML one
318 * for compatibility reasons.
320 avist->indexes.entry = avist->indexes.ents_allocated = 0;
321 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
322 avio_wl16(pb, 4); /* wLongsPerEntry */
323 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
324 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
325 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
326 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
328 avio_wl64(pb, 0); /* dwReserved[3]
329 avio_wl32(pb, 0); Must be 0. */
330 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
332 ff_end_tag(pb, avist->indexes.indx_start);
335 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
336 && s->streams[i]->sample_aspect_ratio.num>0
337 && s->streams[i]->sample_aspect_ratio.den>0){
338 int vprp= ff_start_tag(pb, "vprp");
339 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
340 (AVRational){stream->width, stream->height});
342 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
344 avio_wl32(pb, 0); //video format = unknown
345 avio_wl32(pb, 0); //video standard= unknown
346 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
347 avio_wl32(pb, stream->width );
348 avio_wl32(pb, stream->height);
351 avio_wl32(pb, stream->width );
352 avio_wl32(pb, stream->height);
353 avio_wl32(pb, 1); //progressive FIXME
355 avio_wl32(pb, stream->height);
356 avio_wl32(pb, stream->width );
357 avio_wl32(pb, stream->height);
358 avio_wl32(pb, stream->width );
364 ff_end_tag(pb, vprp);
367 ff_end_tag(pb, list2);
371 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
372 avi->odml_list = ff_start_tag(pb, "JUNK");
373 ffio_wfourcc(pb, "odml");
374 ffio_wfourcc(pb, "dmlh");
376 for (i = 0; i < 248; i+= 4)
378 ff_end_tag(pb, avi->odml_list);
381 ff_end_tag(pb, list1);
383 list2 = ff_start_tag(pb, "LIST");
384 ffio_wfourcc(pb, "INFO");
385 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
386 for (i = 0; *ff_riff_tags[i]; i++) {
387 if ((t = av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
388 avi_write_info_tag(s->pb, t->key, t->value);
390 ff_end_tag(pb, list2);
392 /* some padding for easier tag editing */
393 list2 = ff_start_tag(pb, "JUNK");
394 for (i = 0; i < 1016; i += 4)
396 ff_end_tag(pb, list2);
398 avi->movi_list = ff_start_tag(pb, "LIST");
399 ffio_wfourcc(pb, "movi");
406 static int avi_write_ix(AVFormatContext *s)
408 AVIOContext *pb = s->pb;
409 AVIContext *avi = s->priv_data;
411 char ix_tag[] = "ix00";
414 assert(pb->seekable);
416 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
419 for (i=0;i<s->nb_streams;i++) {
420 AVIStream *avist= s->streams[i]->priv_data;
423 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
426 /* Writing AVI OpenDML leaf index chunk */
428 ffio_wfourcc(pb, ix_tag); /* ix?? */
429 avio_wl32(pb, avist->indexes.entry * 8 + 24);
431 avio_wl16(pb, 2); /* wLongsPerEntry */
432 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
433 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
434 avio_wl32(pb, avist->indexes.entry);
436 ffio_wfourcc(pb, tag); /* dwChunkId */
437 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
438 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
440 for (j=0; j<avist->indexes.entry; j++) {
441 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
442 avio_wl32(pb, ie->pos + 8);
443 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
444 (ie->flags & 0x10 ? 0 : 0x80000000));
449 /* Updating one entry in the AVI OpenDML master index */
450 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
451 ffio_wfourcc(pb, "indx"); /* enabling this entry */
453 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
454 avio_skip(pb, 16*avi->riff_id);
455 avio_wl64(pb, ix); /* qwOffset */
456 avio_wl32(pb, pos - ix); /* dwSize */
457 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
459 avio_seek(pb, pos, SEEK_SET);
464 static int avi_write_idx1(AVFormatContext *s)
466 AVIOContext *pb = s->pb;
467 AVIContext *avi = s->priv_data;
474 AVIIentry* ie = 0, *tie;
475 int empty, stream_id = -1;
477 idx_chunk = ff_start_tag(pb, "idx1");
478 for(i=0; i<s->nb_streams; i++){
479 avist= s->streams[i]->priv_data;
485 for (i=0; i<s->nb_streams; i++) {
486 avist= s->streams[i]->priv_data;
487 if (avist->indexes.entry <= avist->entry)
490 tie = avi_get_ientry(&avist->indexes, avist->entry);
491 if (empty || tie->pos < ie->pos) {
498 avist= s->streams[stream_id]->priv_data;
499 avi_stream2fourcc(tag, stream_id,
500 s->streams[stream_id]->codec->codec_type);
501 ffio_wfourcc(pb, tag);
502 avio_wl32(pb, ie->flags);
503 avio_wl32(pb, ie->pos);
504 avio_wl32(pb, ie->len);
508 ff_end_tag(pb, idx_chunk);
510 avi_write_counters(s, avi->riff_id);
515 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
517 AVIContext *avi = s->priv_data;
518 AVIOContext *pb = s->pb;
519 unsigned char tag[5];
520 unsigned int flags=0;
521 const int stream_index= pkt->stream_index;
522 AVIStream *avist= s->streams[stream_index]->priv_data;
523 AVCodecContext *enc= s->streams[stream_index]->codec;
526 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
527 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count && enc->codec_id != CODEC_ID_XSUB){
528 AVPacket empty_packet;
530 if(pkt->dts - avist->packet_count > 60000){
531 av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %"PRId64"\n", pkt->dts - avist->packet_count);
532 return AVERROR(EINVAL);
535 av_init_packet(&empty_packet);
536 empty_packet.size= 0;
537 empty_packet.data= NULL;
538 empty_packet.stream_index= stream_index;
539 avi_write_packet(s, &empty_packet);
540 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
542 avist->packet_count++;
544 // Make sure to put an OpenDML chunk when the file size exceeds the limits
546 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
549 ff_end_tag(pb, avi->movi_list);
551 if (avi->riff_id == 1)
554 ff_end_tag(pb, avi->riff_start);
555 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
558 avi_stream2fourcc(tag, stream_index, enc->codec_type);
559 if(pkt->flags&AV_PKT_FLAG_KEY)
561 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
562 avist->audio_strm_length += size;
565 if (s->pb->seekable) {
566 AVIIndex* idx = &avist->indexes;
567 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
568 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
569 if (idx->ents_allocated <= idx->entry) {
570 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
573 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
574 if (!idx->cluster[cl])
576 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
579 idx->cluster[cl][id].flags = flags;
580 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
581 idx->cluster[cl][id].len = size;
585 avio_write(pb, tag, 4);
587 avio_write(pb, pkt->data, size);
595 static int avi_write_trailer(AVFormatContext *s)
597 AVIContext *avi = s->priv_data;
598 AVIOContext *pb = s->pb;
600 int i, j, n, nb_frames;
604 if (avi->riff_id == 1) {
605 ff_end_tag(pb, avi->movi_list);
606 res = avi_write_idx1(s);
607 ff_end_tag(pb, avi->riff_start);
610 ff_end_tag(pb, avi->movi_list);
611 ff_end_tag(pb, avi->riff_start);
613 file_size = avio_tell(pb);
614 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
615 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
618 for (n=nb_frames=0;n<s->nb_streams;n++) {
619 AVCodecContext *stream = s->streams[n]->codec;
620 AVIStream *avist= s->streams[n]->priv_data;
622 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
623 if (nb_frames < avist->packet_count)
624 nb_frames = avist->packet_count;
626 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
627 nb_frames += avist->packet_count;
631 avio_wl32(pb, nb_frames);
632 avio_seek(pb, file_size, SEEK_SET);
634 avi_write_counters(s, avi->riff_id);
639 for (i=0; i<s->nb_streams; i++) {
640 AVIStream *avist= s->streams[i]->priv_data;
641 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
642 av_free(avist->indexes.cluster[j]);
643 av_freep(&avist->indexes.cluster);
644 avist->indexes.ents_allocated = avist->indexes.entry = 0;
650 AVOutputFormat ff_avi_muxer = {
652 .long_name = NULL_IF_CONFIG_SMALL("AVI format"),
653 .mime_type = "video/x-msvideo",
655 .priv_data_size = sizeof(AVIContext),
656 #if CONFIG_LIBMP3LAME_ENCODER
657 .audio_codec = CODEC_ID_MP3,
659 .audio_codec = CODEC_ID_AC3,
661 .video_codec = CODEC_ID_MPEG4,
662 .write_header = avi_write_header,
663 .write_packet = avi_write_packet,
664 .write_trailer = avi_write_trailer,
665 .codec_tag = (const AVCodecTag* const []){
666 ff_codec_bmp_tags, ff_codec_wav_tags, 0
668 .flags = AVFMT_VARIABLE_FPS,