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
27 * - fill all fields if non streamed (nb_frames for example)
30 typedef struct AVIIentry {
31 unsigned int flags, pos, len;
34 #define AVI_INDEX_CLUSTER_SIZE 16384
36 typedef struct AVIIndex {
44 int64_t riff_start, movi_list, odml_list;
45 int64_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
46 int audio_strm_length[MAX_STREAMS];
48 int packet_count[MAX_STREAMS];
50 AVIIndex indexes[MAX_STREAMS];
53 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
55 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
56 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
57 return &idx->cluster[cl][id];
60 static int64_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
61 const char* riff_tag, const char* list_tag)
67 for (i=0; i<MAX_STREAMS; i++)
68 avi->indexes[i].entry = 0;
70 avi->riff_start = ff_start_tag(pb, "RIFF");
71 put_tag(pb, riff_tag);
72 loff = ff_start_tag(pb, "LIST");
73 put_tag(pb, list_tag);
77 static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
81 if (type == CODEC_TYPE_VIDEO) {
84 } else if (type == CODEC_TYPE_SUBTITLE) {
85 // note: this is not an official code
96 static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
98 int len = strlen(str);
109 static void avi_write_info_tag2(AVFormatContext *s, const char *fourcc, const char *key1, const char *key2)
111 AVMetadataTag *tag= av_metadata_get(s->metadata, key1, NULL, 0);
113 tag= av_metadata_get(s->metadata, key2, NULL, 0);
115 avi_write_info_tag(s->pb, fourcc, tag->value);
118 static int avi_write_counters(AVFormatContext* s, int riff_id)
120 ByteIOContext *pb = s->pb;
121 AVIContext *avi = s->priv_data;
122 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
124 AVCodecContext* stream;
126 file_size = url_ftell(pb);
127 for(n = 0; n < s->nb_streams; n++) {
128 assert(avi->frames_hdr_strm[n]);
129 stream = s->streams[n]->codec;
130 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
131 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
133 put_le32(pb, avi->packet_count[n]);
135 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
137 if(stream->codec_type == CODEC_TYPE_VIDEO)
138 nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
141 assert(avi->frames_hdr_all);
142 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
143 put_le32(pb, nb_frames);
145 url_fseek(pb, file_size, SEEK_SET);
150 static int avi_write_header(AVFormatContext *s)
152 AVIContext *avi = s->priv_data;
153 ByteIOContext *pb = s->pb;
154 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
155 AVCodecContext *stream, *video_enc;
156 int64_t list1, list2, strh, strf;
160 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
164 put_le32(pb, 14 * 4);
168 for(n=0;n<s->nb_streams;n++) {
169 stream = s->streams[n]->codec;
170 bitrate += stream->bit_rate;
171 if (stream->codec_type == CODEC_TYPE_VIDEO)
178 put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
182 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
183 put_le32(pb, 0); /* padding */
184 if (url_is_streamed(pb))
185 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
187 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
188 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
189 put_le32(pb, nb_frames); /* nb frames, filled later */
190 put_le32(pb, 0); /* initial frame */
191 put_le32(pb, s->nb_streams); /* nb streams */
192 put_le32(pb, 1024 * 1024); /* suggested buffer size */
194 put_le32(pb, video_enc->width);
195 put_le32(pb, video_enc->height);
200 put_le32(pb, 0); /* reserved */
201 put_le32(pb, 0); /* reserved */
202 put_le32(pb, 0); /* reserved */
203 put_le32(pb, 0); /* reserved */
207 list2 = ff_start_tag(pb, "LIST");
210 stream = s->streams[i]->codec;
212 /* stream generic header */
213 strh = ff_start_tag(pb, "strh");
214 switch(stream->codec_type) {
215 case CODEC_TYPE_SUBTITLE:
216 // XSUB subtitles behave like video tracks, other subtitles
217 // are not (yet) supported.
218 if (stream->codec_id != CODEC_ID_XSUB) break;
219 case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
220 case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
221 // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
222 case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
224 if(stream->codec_type == CODEC_TYPE_VIDEO ||
225 stream->codec_id == CODEC_ID_XSUB)
226 put_le32(pb, stream->codec_tag);
229 put_le32(pb, 0); /* flags */
230 put_le16(pb, 0); /* priority */
231 put_le16(pb, 0); /* language */
232 put_le32(pb, 0); /* initial frame */
234 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
236 put_le32(pb, au_scale); /* scale */
237 put_le32(pb, au_byterate); /* rate */
238 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
240 put_le32(pb, 0); /* start */
241 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
242 if (url_is_streamed(pb))
243 put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
245 put_le32(pb, 0); /* length, XXX: filled later */
247 /* suggested buffer size */ //FIXME set at the end to largest chunk
248 if(stream->codec_type == CODEC_TYPE_VIDEO)
249 put_le32(pb, 1024 * 1024);
250 else if(stream->codec_type == CODEC_TYPE_AUDIO)
251 put_le32(pb, 12 * 1024);
254 put_le32(pb, -1); /* quality */
255 put_le32(pb, au_ssize); /* sample size */
257 put_le16(pb, stream->width);
258 put_le16(pb, stream->height);
259 ff_end_tag(pb, strh);
261 if(stream->codec_type != CODEC_TYPE_DATA){
262 strf = ff_start_tag(pb, "strf");
263 switch(stream->codec_type) {
264 case CODEC_TYPE_SUBTITLE:
265 // XSUB subtitles behave like video tracks, other subtitles
266 // are not (yet) supported.
267 if (stream->codec_id != CODEC_ID_XSUB) break;
268 case CODEC_TYPE_VIDEO:
269 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
271 case CODEC_TYPE_AUDIO:
272 if (ff_put_wav_header(pb, stream) < 0) {
279 ff_end_tag(pb, strf);
282 if (!url_is_streamed(pb)) {
283 unsigned char tag[5];
286 /* Starting to lay out AVI OpenDML master index.
287 * We want to make it JUNK entry for now, since we'd
288 * like to get away without making AVI an OpenDML one
289 * for compatibility reasons.
291 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
292 avi->indexes[i].indx_start = ff_start_tag(pb, "JUNK");
293 put_le16(pb, 4); /* wLongsPerEntry */
294 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
295 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
296 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
297 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
299 put_le64(pb, 0); /* dwReserved[3]
300 put_le32(pb, 0); Must be 0. */
301 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
303 ff_end_tag(pb, avi->indexes[i].indx_start);
306 if( stream->codec_type == CODEC_TYPE_VIDEO
307 && s->streams[i]->sample_aspect_ratio.num>0
308 && s->streams[i]->sample_aspect_ratio.den>0){
309 int vprp= ff_start_tag(pb, "vprp");
310 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
311 (AVRational){stream->width, stream->height});
313 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
315 put_le32(pb, 0); //video format = unknown
316 put_le32(pb, 0); //video standard= unknown
317 put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
318 put_le32(pb, stream->width );
319 put_le32(pb, stream->height);
322 put_le32(pb, stream->width );
323 put_le32(pb, stream->height);
324 put_le32(pb, 1); //progressive FIXME
326 put_le32(pb, stream->height);
327 put_le32(pb, stream->width );
328 put_le32(pb, stream->height);
329 put_le32(pb, stream->width );
335 ff_end_tag(pb, vprp);
338 ff_end_tag(pb, list2);
341 if (!url_is_streamed(pb)) {
342 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
343 avi->odml_list = ff_start_tag(pb, "JUNK");
347 for (i = 0; i < 248; i+= 4)
349 ff_end_tag(pb, avi->odml_list);
352 ff_end_tag(pb, list1);
354 list2 = ff_start_tag(pb, "LIST");
356 avi_write_info_tag2(s, "INAM", "Title", NULL);
357 avi_write_info_tag2(s, "IART", "Artist", "Author");
358 avi_write_info_tag2(s, "ICOP", "Copyright", NULL);
359 avi_write_info_tag2(s, "ICMT", "Comment", NULL);
360 avi_write_info_tag2(s, "IPRD", "Album", NULL);
361 avi_write_info_tag2(s, "IGNR", "Genre", NULL);
362 avi_write_info_tag2(s, "IPRT", "Track", NULL);
363 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
364 avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
365 ff_end_tag(pb, list2);
367 /* some padding for easier tag editing */
368 list2 = ff_start_tag(pb, "JUNK");
369 for (i = 0; i < 1016; i += 4)
371 ff_end_tag(pb, list2);
373 avi->movi_list = ff_start_tag(pb, "LIST");
376 put_flush_packet(pb);
381 static int avi_write_ix(AVFormatContext *s)
383 ByteIOContext *pb = s->pb;
384 AVIContext *avi = s->priv_data;
386 char ix_tag[] = "ix00";
389 assert(!url_is_streamed(pb));
391 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
394 for (i=0;i<s->nb_streams;i++) {
397 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
400 /* Writing AVI OpenDML leaf index chunk */
402 put_tag(pb, &ix_tag[0]); /* ix?? */
403 put_le32(pb, avi->indexes[i].entry * 8 + 24);
405 put_le16(pb, 2); /* wLongsPerEntry */
406 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
407 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
408 put_le32(pb, avi->indexes[i].entry);
410 put_tag(pb, &tag[0]); /* dwChunkId */
411 put_le64(pb, avi->movi_list);/* qwBaseOffset */
412 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
414 for (j=0; j<avi->indexes[i].entry; j++) {
415 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
416 put_le32(pb, ie->pos + 8);
417 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
418 (ie->flags & 0x10 ? 0 : 0x80000000));
420 put_flush_packet(pb);
423 /* Updating one entry in the AVI OpenDML master index */
424 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
425 put_tag(pb, "indx"); /* enabling this entry */
427 put_le32(pb, avi->riff_id); /* nEntriesInUse */
428 url_fskip(pb, 16*avi->riff_id);
429 put_le64(pb, ix); /* qwOffset */
430 put_le32(pb, pos - ix); /* dwSize */
431 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
433 url_fseek(pb, pos, SEEK_SET);
438 static int avi_write_idx1(AVFormatContext *s)
440 ByteIOContext *pb = s->pb;
441 AVIContext *avi = s->priv_data;
446 if (!url_is_streamed(pb)) {
447 AVIIentry* ie = 0, *tie;
448 int entry[MAX_STREAMS];
449 int empty, stream_id = -1;
451 idx_chunk = ff_start_tag(pb, "idx1");
452 memset(&entry[0], 0, sizeof(entry));
455 for (i=0; i<s->nb_streams; i++) {
456 if (avi->indexes[i].entry <= entry[i])
459 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
460 if (empty || tie->pos < ie->pos) {
467 avi_stream2fourcc(&tag[0], stream_id,
468 s->streams[stream_id]->codec->codec_type);
469 put_tag(pb, &tag[0]);
470 put_le32(pb, ie->flags);
471 put_le32(pb, ie->pos);
472 put_le32(pb, ie->len);
476 ff_end_tag(pb, idx_chunk);
478 avi_write_counters(s, avi->riff_id);
483 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
485 AVIContext *avi = s->priv_data;
486 ByteIOContext *pb = s->pb;
487 unsigned char tag[5];
488 unsigned int flags=0;
489 const int stream_index= pkt->stream_index;
490 AVCodecContext *enc= s->streams[stream_index]->codec;
493 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
494 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
495 AVPacket empty_packet;
497 av_init_packet(&empty_packet);
498 empty_packet.size= 0;
499 empty_packet.data= NULL;
500 empty_packet.stream_index= stream_index;
501 avi_write_packet(s, &empty_packet);
502 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
504 avi->packet_count[stream_index]++;
506 // Make sure to put an OpenDML chunk when the file size exceeds the limits
507 if (!url_is_streamed(pb) &&
508 (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
511 ff_end_tag(pb, avi->movi_list);
513 if (avi->riff_id == 1)
516 ff_end_tag(pb, avi->riff_start);
517 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
520 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
521 if(pkt->flags&PKT_FLAG_KEY)
523 if (enc->codec_type == CODEC_TYPE_AUDIO) {
524 avi->audio_strm_length[stream_index] += size;
527 if (!url_is_streamed(s->pb)) {
528 AVIIndex* idx = &avi->indexes[stream_index];
529 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
530 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
531 if (idx->ents_allocated <= idx->entry) {
532 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
535 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
536 if (!idx->cluster[cl])
538 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
541 idx->cluster[cl][id].flags = flags;
542 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
543 idx->cluster[cl][id].len = size;
547 put_buffer(pb, tag, 4);
549 put_buffer(pb, pkt->data, size);
553 put_flush_packet(pb);
557 static int avi_write_trailer(AVFormatContext *s)
559 AVIContext *avi = s->priv_data;
560 ByteIOContext *pb = s->pb;
562 int i, j, n, nb_frames;
565 if (!url_is_streamed(pb)){
566 if (avi->riff_id == 1) {
567 ff_end_tag(pb, avi->movi_list);
568 res = avi_write_idx1(s);
569 ff_end_tag(pb, avi->riff_start);
572 ff_end_tag(pb, avi->movi_list);
573 ff_end_tag(pb, avi->riff_start);
575 file_size = url_ftell(pb);
576 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
577 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
580 for (n=nb_frames=0;n<s->nb_streams;n++) {
581 AVCodecContext *stream = s->streams[n]->codec;
582 if (stream->codec_type == CODEC_TYPE_VIDEO) {
583 if (nb_frames < avi->packet_count[n])
584 nb_frames = avi->packet_count[n];
586 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
587 nb_frames += avi->packet_count[n];
591 put_le32(pb, nb_frames);
592 url_fseek(pb, file_size, SEEK_SET);
594 avi_write_counters(s, avi->riff_id);
597 put_flush_packet(pb);
599 for (i=0; i<MAX_STREAMS; i++) {
600 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
601 av_free(avi->indexes[i].cluster[j]);
602 av_freep(&avi->indexes[i].cluster);
603 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
609 AVOutputFormat avi_muxer = {
611 NULL_IF_CONFIG_SMALL("AVI format"),
620 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
621 .flags= AVFMT_VARIABLE_FPS,