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
23 #include "avio_internal.h"
25 #include "libavutil/intreadwrite.h"
29 * - fill all fields if non streamed (nb_frames for example)
32 typedef struct AVIIentry {
33 unsigned int flags, pos, len;
36 #define AVI_INDEX_CLUSTER_SIZE 16384
38 typedef struct AVIIndex {
46 int64_t riff_start, movi_list, odml_list;
47 int64_t frames_hdr_all;
52 int64_t frames_hdr_strm;
53 int audio_strm_length;
60 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
62 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
63 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
64 return &idx->cluster[cl][id];
67 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
68 const char* riff_tag, const char* list_tag)
70 AVIContext *avi= s->priv_data;
75 for (i=0; i<s->nb_streams; i++){
76 AVIStream *avist= s->streams[i]->priv_data;
77 avist->indexes.entry = 0;
80 avi->riff_start = ff_start_tag(pb, "RIFF");
81 ffio_wfourcc(pb, riff_tag);
82 loff = ff_start_tag(pb, "LIST");
83 ffio_wfourcc(pb, list_tag);
87 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
89 tag[0] = '0' + index/10;
90 tag[1] = '0' + index%10;
91 if (type == AVMEDIA_TYPE_VIDEO) {
94 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
95 // note: this is not an official code
106 static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
108 int len = strlen(str);
111 ffio_wfourcc(pb, tag);
113 avio_put_str(pb, str);
119 static int avi_write_counters(AVFormatContext* s, int riff_id)
121 AVIOContext *pb = s->pb;
122 AVIContext *avi = s->priv_data;
123 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
125 AVCodecContext* stream;
127 file_size = avio_tell(pb);
128 for(n = 0; n < s->nb_streams; n++) {
129 AVIStream *avist= s->streams[n]->priv_data;
131 assert(avist->frames_hdr_strm);
132 stream = s->streams[n]->codec;
133 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
134 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
136 avio_wl32(pb, avist->packet_count);
138 avio_wl32(pb, avist->audio_strm_length / au_ssize);
140 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
141 nb_frames = FFMAX(nb_frames, avist->packet_count);
144 assert(avi->frames_hdr_all);
145 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
146 avio_wl32(pb, nb_frames);
148 avio_seek(pb, file_size, SEEK_SET);
153 static int avi_write_header(AVFormatContext *s)
155 AVIContext *avi = s->priv_data;
156 AVIOContext *pb = s->pb;
157 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
158 AVCodecContext *stream, *video_enc;
159 int64_t list1, list2, strh, strf;
160 AVMetadataTag *t = NULL;
162 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
163 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
164 AVI_MAX_STREAM_COUNT);
168 for(n=0;n<s->nb_streams;n++) {
169 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
170 if(!s->streams[n]->priv_data)
171 return AVERROR(ENOMEM);
176 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
179 ffio_wfourcc(pb, "avih");
180 avio_wl32(pb, 14 * 4);
184 for(n=0;n<s->nb_streams;n++) {
185 stream = s->streams[n]->codec;
186 bitrate += stream->bit_rate;
187 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
194 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
198 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
199 avio_wl32(pb, 0); /* padding */
200 if (url_is_streamed(pb))
201 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
203 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
204 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
205 avio_wl32(pb, nb_frames); /* nb frames, filled later */
206 avio_wl32(pb, 0); /* initial frame */
207 avio_wl32(pb, s->nb_streams); /* nb streams */
208 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
210 avio_wl32(pb, video_enc->width);
211 avio_wl32(pb, video_enc->height);
216 avio_wl32(pb, 0); /* reserved */
217 avio_wl32(pb, 0); /* reserved */
218 avio_wl32(pb, 0); /* reserved */
219 avio_wl32(pb, 0); /* reserved */
223 AVIStream *avist= s->streams[i]->priv_data;
224 list2 = ff_start_tag(pb, "LIST");
225 ffio_wfourcc(pb, "strl");
227 stream = s->streams[i]->codec;
229 /* stream generic header */
230 strh = ff_start_tag(pb, "strh");
231 switch(stream->codec_type) {
232 case AVMEDIA_TYPE_SUBTITLE:
233 // XSUB subtitles behave like video tracks, other subtitles
234 // are not (yet) supported.
235 if (stream->codec_id != CODEC_ID_XSUB) {
236 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
237 return AVERROR_PATCHWELCOME;
239 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
240 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
241 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
242 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
244 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
245 stream->codec_id == CODEC_ID_XSUB)
246 avio_wl32(pb, stream->codec_tag);
249 avio_wl32(pb, 0); /* flags */
250 avio_wl16(pb, 0); /* priority */
251 avio_wl16(pb, 0); /* language */
252 avio_wl32(pb, 0); /* initial frame */
254 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
256 avio_wl32(pb, au_scale); /* scale */
257 avio_wl32(pb, au_byterate); /* rate */
258 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
260 avio_wl32(pb, 0); /* start */
261 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
262 if (url_is_streamed(pb))
263 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
265 avio_wl32(pb, 0); /* length, XXX: filled later */
267 /* suggested buffer size */ //FIXME set at the end to largest chunk
268 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
269 avio_wl32(pb, 1024 * 1024);
270 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
271 avio_wl32(pb, 12 * 1024);
274 avio_wl32(pb, -1); /* quality */
275 avio_wl32(pb, au_ssize); /* sample size */
277 avio_wl16(pb, stream->width);
278 avio_wl16(pb, stream->height);
279 ff_end_tag(pb, strh);
281 if(stream->codec_type != AVMEDIA_TYPE_DATA){
282 strf = ff_start_tag(pb, "strf");
283 switch(stream->codec_type) {
284 case AVMEDIA_TYPE_SUBTITLE:
285 // XSUB subtitles behave like video tracks, other subtitles
286 // are not (yet) supported.
287 if (stream->codec_id != CODEC_ID_XSUB) break;
288 case AVMEDIA_TYPE_VIDEO:
289 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
291 case AVMEDIA_TYPE_AUDIO:
292 if (ff_put_wav_header(pb, stream) < 0) {
299 ff_end_tag(pb, strf);
300 if ((t = av_metadata_get(s->streams[i]->metadata, "title", NULL, 0))) {
301 avi_write_info_tag(s->pb, "strn", t->value);
306 if (!url_is_streamed(pb)) {
307 unsigned char tag[5];
310 /* Starting to lay out AVI OpenDML master index.
311 * We want to make it JUNK entry for now, since we'd
312 * like to get away without making AVI an OpenDML one
313 * for compatibility reasons.
315 avist->indexes.entry = avist->indexes.ents_allocated = 0;
316 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
317 avio_wl16(pb, 4); /* wLongsPerEntry */
318 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
319 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
320 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
321 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
323 avio_wl64(pb, 0); /* dwReserved[3]
324 avio_wl32(pb, 0); Must be 0. */
325 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
327 ff_end_tag(pb, avist->indexes.indx_start);
330 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
331 && s->streams[i]->sample_aspect_ratio.num>0
332 && s->streams[i]->sample_aspect_ratio.den>0){
333 int vprp= ff_start_tag(pb, "vprp");
334 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
335 (AVRational){stream->width, stream->height});
337 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
339 avio_wl32(pb, 0); //video format = unknown
340 avio_wl32(pb, 0); //video standard= unknown
341 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
342 avio_wl32(pb, stream->width );
343 avio_wl32(pb, stream->height);
346 avio_wl32(pb, stream->width );
347 avio_wl32(pb, stream->height);
348 avio_wl32(pb, 1); //progressive FIXME
350 avio_wl32(pb, stream->height);
351 avio_wl32(pb, stream->width );
352 avio_wl32(pb, stream->height);
353 avio_wl32(pb, stream->width );
359 ff_end_tag(pb, vprp);
362 ff_end_tag(pb, list2);
365 if (!url_is_streamed(pb)) {
366 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
367 avi->odml_list = ff_start_tag(pb, "JUNK");
368 ffio_wfourcc(pb, "odml");
369 ffio_wfourcc(pb, "dmlh");
371 for (i = 0; i < 248; i+= 4)
373 ff_end_tag(pb, avi->odml_list);
376 ff_end_tag(pb, list1);
378 list2 = ff_start_tag(pb, "LIST");
379 ffio_wfourcc(pb, "INFO");
380 ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
381 for (i = 0; *ff_avi_tags[i]; i++) {
382 if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
383 avi_write_info_tag(s->pb, t->key, t->value);
385 ff_end_tag(pb, list2);
387 /* some padding for easier tag editing */
388 list2 = ff_start_tag(pb, "JUNK");
389 for (i = 0; i < 1016; i += 4)
391 ff_end_tag(pb, list2);
393 avi->movi_list = ff_start_tag(pb, "LIST");
394 ffio_wfourcc(pb, "movi");
401 static int avi_write_ix(AVFormatContext *s)
403 AVIOContext *pb = s->pb;
404 AVIContext *avi = s->priv_data;
406 char ix_tag[] = "ix00";
409 assert(!url_is_streamed(pb));
411 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
414 for (i=0;i<s->nb_streams;i++) {
415 AVIStream *avist= s->streams[i]->priv_data;
418 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
421 /* Writing AVI OpenDML leaf index chunk */
423 ffio_wfourcc(pb, ix_tag); /* ix?? */
424 avio_wl32(pb, avist->indexes.entry * 8 + 24);
426 avio_wl16(pb, 2); /* wLongsPerEntry */
427 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
428 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
429 avio_wl32(pb, avist->indexes.entry);
431 ffio_wfourcc(pb, tag); /* dwChunkId */
432 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
433 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
435 for (j=0; j<avist->indexes.entry; j++) {
436 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
437 avio_wl32(pb, ie->pos + 8);
438 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
439 (ie->flags & 0x10 ? 0 : 0x80000000));
444 /* Updating one entry in the AVI OpenDML master index */
445 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
446 ffio_wfourcc(pb, "indx"); /* enabling this entry */
448 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
449 avio_skip(pb, 16*avi->riff_id);
450 avio_wl64(pb, ix); /* qwOffset */
451 avio_wl32(pb, pos - ix); /* dwSize */
452 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
454 avio_seek(pb, pos, SEEK_SET);
459 static int avi_write_idx1(AVFormatContext *s)
461 AVIOContext *pb = s->pb;
462 AVIContext *avi = s->priv_data;
467 if (!url_is_streamed(pb)) {
469 AVIIentry* ie = 0, *tie;
470 int empty, stream_id = -1;
472 idx_chunk = ff_start_tag(pb, "idx1");
473 for(i=0; i<s->nb_streams; i++){
474 avist= s->streams[i]->priv_data;
480 for (i=0; i<s->nb_streams; i++) {
481 avist= s->streams[i]->priv_data;
482 if (avist->indexes.entry <= avist->entry)
485 tie = avi_get_ientry(&avist->indexes, avist->entry);
486 if (empty || tie->pos < ie->pos) {
493 avist= s->streams[stream_id]->priv_data;
494 avi_stream2fourcc(tag, stream_id,
495 s->streams[stream_id]->codec->codec_type);
496 ffio_wfourcc(pb, tag);
497 avio_wl32(pb, ie->flags);
498 avio_wl32(pb, ie->pos);
499 avio_wl32(pb, ie->len);
503 ff_end_tag(pb, idx_chunk);
505 avi_write_counters(s, avi->riff_id);
510 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
512 AVIContext *avi = s->priv_data;
513 AVIOContext *pb = s->pb;
514 unsigned char tag[5];
515 unsigned int flags=0;
516 const int stream_index= pkt->stream_index;
517 AVIStream *avist= s->streams[stream_index]->priv_data;
518 AVCodecContext *enc= s->streams[stream_index]->codec;
521 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
522 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
523 AVPacket empty_packet;
525 av_init_packet(&empty_packet);
526 empty_packet.size= 0;
527 empty_packet.data= NULL;
528 empty_packet.stream_index= stream_index;
529 avi_write_packet(s, &empty_packet);
530 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
532 avist->packet_count++;
534 // Make sure to put an OpenDML chunk when the file size exceeds the limits
535 if (!url_is_streamed(pb) &&
536 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
539 ff_end_tag(pb, avi->movi_list);
541 if (avi->riff_id == 1)
544 ff_end_tag(pb, avi->riff_start);
545 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
548 avi_stream2fourcc(tag, stream_index, enc->codec_type);
549 if(pkt->flags&AV_PKT_FLAG_KEY)
551 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
552 avist->audio_strm_length += size;
555 if (!url_is_streamed(s->pb)) {
556 AVIIndex* idx = &avist->indexes;
557 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
558 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
559 if (idx->ents_allocated <= idx->entry) {
560 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
563 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
564 if (!idx->cluster[cl])
566 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
569 idx->cluster[cl][id].flags = flags;
570 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
571 idx->cluster[cl][id].len = size;
575 avio_write(pb, tag, 4);
577 avio_write(pb, pkt->data, size);
585 static int avi_write_trailer(AVFormatContext *s)
587 AVIContext *avi = s->priv_data;
588 AVIOContext *pb = s->pb;
590 int i, j, n, nb_frames;
593 if (!url_is_streamed(pb)){
594 if (avi->riff_id == 1) {
595 ff_end_tag(pb, avi->movi_list);
596 res = avi_write_idx1(s);
597 ff_end_tag(pb, avi->riff_start);
600 ff_end_tag(pb, avi->movi_list);
601 ff_end_tag(pb, avi->riff_start);
603 file_size = avio_tell(pb);
604 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
605 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
608 for (n=nb_frames=0;n<s->nb_streams;n++) {
609 AVCodecContext *stream = s->streams[n]->codec;
610 AVIStream *avist= s->streams[n]->priv_data;
612 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
613 if (nb_frames < avist->packet_count)
614 nb_frames = avist->packet_count;
616 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
617 nb_frames += avist->packet_count;
621 avio_wl32(pb, nb_frames);
622 avio_seek(pb, file_size, SEEK_SET);
624 avi_write_counters(s, avi->riff_id);
629 for (i=0; i<s->nb_streams; i++) {
630 AVIStream *avist= s->streams[i]->priv_data;
631 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
632 av_free(avist->indexes.cluster[j]);
633 av_freep(&avist->indexes.cluster);
634 avist->indexes.ents_allocated = avist->indexes.entry = 0;
640 AVOutputFormat ff_avi_muxer = {
642 NULL_IF_CONFIG_SMALL("AVI format"),
651 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
652 .flags= AVFMT_VARIABLE_FPS,