3 * Copyright (c) 2000 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 int avi_write_counters(AVFormatContext* s, int riff_id)
110 AVIOContext *pb = s->pb;
111 AVIContext *avi = s->priv_data;
112 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
114 AVCodecContext* stream;
116 file_size = avio_tell(pb);
117 for(n = 0; n < s->nb_streams; n++) {
118 AVIStream *avist= s->streams[n]->priv_data;
120 assert(avist->frames_hdr_strm);
121 stream = s->streams[n]->codec;
122 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
123 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
125 avio_wl32(pb, avist->packet_count);
127 avio_wl32(pb, avist->audio_strm_length / au_ssize);
129 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
130 nb_frames = FFMAX(nb_frames, avist->packet_count);
133 assert(avi->frames_hdr_all);
134 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
135 avio_wl32(pb, nb_frames);
137 avio_seek(pb, file_size, SEEK_SET);
142 static int avi_write_header(AVFormatContext *s)
144 AVIContext *avi = s->priv_data;
145 AVIOContext *pb = s->pb;
146 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
147 AVCodecContext *stream, *video_enc;
148 int64_t list1, list2, strh, strf;
149 AVDictionaryEntry *t = NULL;
151 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
152 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
153 AVI_MAX_STREAM_COUNT);
157 for(n=0;n<s->nb_streams;n++) {
158 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
159 if(!s->streams[n]->priv_data)
160 return AVERROR(ENOMEM);
165 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
168 ffio_wfourcc(pb, "avih");
169 avio_wl32(pb, 14 * 4);
173 for(n=0;n<s->nb_streams;n++) {
174 stream = s->streams[n]->codec;
175 bitrate += stream->bit_rate;
176 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
183 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
187 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
188 avio_wl32(pb, 0); /* padding */
190 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
192 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
193 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
194 avio_wl32(pb, nb_frames); /* nb frames, filled later */
195 avio_wl32(pb, 0); /* initial frame */
196 avio_wl32(pb, s->nb_streams); /* nb streams */
197 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
199 avio_wl32(pb, video_enc->width);
200 avio_wl32(pb, video_enc->height);
205 avio_wl32(pb, 0); /* reserved */
206 avio_wl32(pb, 0); /* reserved */
207 avio_wl32(pb, 0); /* reserved */
208 avio_wl32(pb, 0); /* reserved */
212 AVIStream *avist= s->streams[i]->priv_data;
213 list2 = ff_start_tag(pb, "LIST");
214 ffio_wfourcc(pb, "strl");
216 stream = s->streams[i]->codec;
218 /* stream generic header */
219 strh = ff_start_tag(pb, "strh");
220 switch(stream->codec_type) {
221 case AVMEDIA_TYPE_SUBTITLE:
222 // XSUB subtitles behave like video tracks, other subtitles
223 // are not (yet) supported.
224 if (stream->codec_id != AV_CODEC_ID_XSUB) {
225 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
226 return AVERROR_PATCHWELCOME;
228 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
229 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
230 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
231 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
233 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
234 stream->codec_id == AV_CODEC_ID_XSUB)
235 avio_wl32(pb, stream->codec_tag);
238 avio_wl32(pb, 0); /* flags */
239 avio_wl16(pb, 0); /* priority */
240 avio_wl16(pb, 0); /* language */
241 avio_wl32(pb, 0); /* initial frame */
243 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
245 avio_wl32(pb, au_scale); /* scale */
246 avio_wl32(pb, au_byterate); /* rate */
247 avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
249 avio_wl32(pb, 0); /* start */
250 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
252 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
254 avio_wl32(pb, 0); /* length, XXX: filled later */
256 /* suggested buffer size */ //FIXME set at the end to largest chunk
257 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
258 avio_wl32(pb, 1024 * 1024);
259 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
260 avio_wl32(pb, 12 * 1024);
263 avio_wl32(pb, -1); /* quality */
264 avio_wl32(pb, au_ssize); /* sample size */
266 avio_wl16(pb, stream->width);
267 avio_wl16(pb, stream->height);
268 ff_end_tag(pb, strh);
270 if(stream->codec_type != AVMEDIA_TYPE_DATA){
271 strf = ff_start_tag(pb, "strf");
272 switch(stream->codec_type) {
273 case AVMEDIA_TYPE_SUBTITLE:
274 // XSUB subtitles behave like video tracks, other subtitles
275 // are not (yet) supported.
276 if (stream->codec_id != AV_CODEC_ID_XSUB) break;
277 case AVMEDIA_TYPE_VIDEO:
278 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
280 case AVMEDIA_TYPE_AUDIO:
281 if (ff_put_wav_header(pb, stream) < 0) {
288 ff_end_tag(pb, strf);
289 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
290 ff_riff_write_info_tag(s->pb, "strn", t->value);
296 unsigned char tag[5];
299 /* Starting to lay out AVI OpenDML master index.
300 * We want to make it JUNK entry for now, since we'd
301 * like to get away without making AVI an OpenDML one
302 * for compatibility reasons.
304 avist->indexes.entry = avist->indexes.ents_allocated = 0;
305 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
306 avio_wl16(pb, 4); /* wLongsPerEntry */
307 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
308 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
309 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
310 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
312 avio_wl64(pb, 0); /* dwReserved[3]
313 avio_wl32(pb, 0); Must be 0. */
314 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
316 ff_end_tag(pb, avist->indexes.indx_start);
319 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
320 && s->streams[i]->sample_aspect_ratio.num>0
321 && s->streams[i]->sample_aspect_ratio.den>0){
322 int vprp= ff_start_tag(pb, "vprp");
323 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
324 (AVRational){stream->width, stream->height});
326 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
328 avio_wl32(pb, 0); //video format = unknown
329 avio_wl32(pb, 0); //video standard= unknown
330 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
331 avio_wl32(pb, stream->width );
332 avio_wl32(pb, stream->height);
335 avio_wl32(pb, stream->width );
336 avio_wl32(pb, stream->height);
337 avio_wl32(pb, 1); //progressive FIXME
339 avio_wl32(pb, stream->height);
340 avio_wl32(pb, stream->width );
341 avio_wl32(pb, stream->height);
342 avio_wl32(pb, stream->width );
348 ff_end_tag(pb, vprp);
351 ff_end_tag(pb, list2);
355 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
356 avi->odml_list = ff_start_tag(pb, "JUNK");
357 ffio_wfourcc(pb, "odml");
358 ffio_wfourcc(pb, "dmlh");
360 for (i = 0; i < 248; i+= 4)
362 ff_end_tag(pb, avi->odml_list);
365 ff_end_tag(pb, list1);
367 ff_riff_write_info(s);
369 /* some padding for easier tag editing */
370 list2 = ff_start_tag(pb, "JUNK");
371 for (i = 0; i < 1016; i += 4)
373 ff_end_tag(pb, list2);
375 avi->movi_list = ff_start_tag(pb, "LIST");
376 ffio_wfourcc(pb, "movi");
383 static int avi_write_ix(AVFormatContext *s)
385 AVIOContext *pb = s->pb;
386 AVIContext *avi = s->priv_data;
388 char ix_tag[] = "ix00";
391 assert(pb->seekable);
393 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
396 for (i=0;i<s->nb_streams;i++) {
397 AVIStream *avist= s->streams[i]->priv_data;
400 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
403 /* Writing AVI OpenDML leaf index chunk */
405 ffio_wfourcc(pb, ix_tag); /* ix?? */
406 avio_wl32(pb, avist->indexes.entry * 8 + 24);
408 avio_wl16(pb, 2); /* wLongsPerEntry */
409 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
410 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
411 avio_wl32(pb, avist->indexes.entry);
413 ffio_wfourcc(pb, tag); /* dwChunkId */
414 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
415 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
417 for (j=0; j<avist->indexes.entry; j++) {
418 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
419 avio_wl32(pb, ie->pos + 8);
420 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
421 (ie->flags & 0x10 ? 0 : 0x80000000));
426 /* Updating one entry in the AVI OpenDML master index */
427 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
428 ffio_wfourcc(pb, "indx"); /* enabling this entry */
430 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
431 avio_skip(pb, 16*avi->riff_id);
432 avio_wl64(pb, ix); /* qwOffset */
433 avio_wl32(pb, pos - ix); /* dwSize */
434 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
436 avio_seek(pb, pos, SEEK_SET);
441 static int avi_write_idx1(AVFormatContext *s)
443 AVIOContext *pb = s->pb;
444 AVIContext *avi = s->priv_data;
451 AVIIentry* ie = 0, *tie;
452 int empty, stream_id = -1;
454 idx_chunk = ff_start_tag(pb, "idx1");
455 for(i=0; i<s->nb_streams; i++){
456 avist= s->streams[i]->priv_data;
462 for (i=0; i<s->nb_streams; i++) {
463 avist= s->streams[i]->priv_data;
464 if (avist->indexes.entry <= avist->entry)
467 tie = avi_get_ientry(&avist->indexes, avist->entry);
468 if (empty || tie->pos < ie->pos) {
475 avist= s->streams[stream_id]->priv_data;
476 avi_stream2fourcc(tag, stream_id,
477 s->streams[stream_id]->codec->codec_type);
478 ffio_wfourcc(pb, tag);
479 avio_wl32(pb, ie->flags);
480 avio_wl32(pb, ie->pos);
481 avio_wl32(pb, ie->len);
485 ff_end_tag(pb, idx_chunk);
487 avi_write_counters(s, avi->riff_id);
492 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
494 AVIContext *avi = s->priv_data;
495 AVIOContext *pb = s->pb;
496 unsigned char tag[5];
497 unsigned int flags=0;
498 const int stream_index= pkt->stream_index;
499 AVIStream *avist= s->streams[stream_index]->priv_data;
500 AVCodecContext *enc= s->streams[stream_index]->codec;
503 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
504 AVPacket empty_packet;
506 av_init_packet(&empty_packet);
507 empty_packet.size= 0;
508 empty_packet.data= NULL;
509 empty_packet.stream_index= stream_index;
510 avi_write_packet(s, &empty_packet);
512 avist->packet_count++;
514 // Make sure to put an OpenDML chunk when the file size exceeds the limits
516 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
519 ff_end_tag(pb, avi->movi_list);
521 if (avi->riff_id == 1)
524 ff_end_tag(pb, avi->riff_start);
525 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
528 avi_stream2fourcc(tag, stream_index, enc->codec_type);
529 if(pkt->flags&AV_PKT_FLAG_KEY)
531 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
532 avist->audio_strm_length += size;
535 if (s->pb->seekable) {
536 AVIIndex* idx = &avist->indexes;
537 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
538 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
539 if (idx->ents_allocated <= idx->entry) {
540 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
543 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
544 if (!idx->cluster[cl])
546 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
549 idx->cluster[cl][id].flags = flags;
550 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
551 idx->cluster[cl][id].len = size;
555 avio_write(pb, tag, 4);
557 avio_write(pb, pkt->data, size);
565 static int avi_write_trailer(AVFormatContext *s)
567 AVIContext *avi = s->priv_data;
568 AVIOContext *pb = s->pb;
570 int i, j, n, nb_frames;
574 if (avi->riff_id == 1) {
575 ff_end_tag(pb, avi->movi_list);
576 res = avi_write_idx1(s);
577 ff_end_tag(pb, avi->riff_start);
580 ff_end_tag(pb, avi->movi_list);
581 ff_end_tag(pb, avi->riff_start);
583 file_size = avio_tell(pb);
584 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
585 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
588 for (n=nb_frames=0;n<s->nb_streams;n++) {
589 AVCodecContext *stream = s->streams[n]->codec;
590 AVIStream *avist= s->streams[n]->priv_data;
592 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
593 if (nb_frames < avist->packet_count)
594 nb_frames = avist->packet_count;
596 if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
597 nb_frames += avist->packet_count;
601 avio_wl32(pb, nb_frames);
602 avio_seek(pb, file_size, SEEK_SET);
604 avi_write_counters(s, avi->riff_id);
608 for (i=0; i<s->nb_streams; i++) {
609 AVIStream *avist= s->streams[i]->priv_data;
610 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
611 av_free(avist->indexes.cluster[j]);
612 av_freep(&avist->indexes.cluster);
613 avist->indexes.ents_allocated = avist->indexes.entry = 0;
619 AVOutputFormat ff_avi_muxer = {
621 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
622 .mime_type = "video/x-msvideo",
624 .priv_data_size = sizeof(AVIContext),
625 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
626 .video_codec = AV_CODEC_ID_MPEG4,
627 .write_header = avi_write_header,
628 .write_packet = avi_write_packet,
629 .write_trailer = avi_write_trailer,
630 .codec_tag = (const AVCodecTag* const []){
631 ff_codec_bmp_tags, ff_codec_wav_tags, 0
633 .flags = AVFMT_VARIABLE_FPS,