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 #ifdef CONFIG_AVI_MUXER
31 typedef struct AVIIentry
{
32 unsigned int flags
, pos
, len
;
35 #define AVI_INDEX_CLUSTER_SIZE 16384
37 typedef struct AVIIndex
{
45 int64_t riff_start
, movi_list
, odml_list
;
46 int64_t frames_hdr_all
, frames_hdr_strm
[MAX_STREAMS
];
47 int audio_strm_length
[MAX_STREAMS
];
49 int packet_count
[MAX_STREAMS
];
51 AVIIndex indexes
[MAX_STREAMS
];
54 static inline AVIIentry
* avi_get_ientry(AVIIndex
* idx
, int ent_id
)
56 int cl
= ent_id
/ AVI_INDEX_CLUSTER_SIZE
;
57 int id
= ent_id
% AVI_INDEX_CLUSTER_SIZE
;
58 return &idx
->cluster
[cl
][id
];
61 static int64_t avi_start_new_riff(AVIContext
*avi
, ByteIOContext
*pb
,
62 const char* riff_tag
, const char* list_tag
)
68 for (i
=0; i
<MAX_STREAMS
; i
++)
69 avi
->indexes
[i
].entry
= 0;
71 avi
->riff_start
= start_tag(pb
, "RIFF");
72 put_tag(pb
, riff_tag
);
73 loff
= start_tag(pb
, "LIST");
74 put_tag(pb
, list_tag
);
78 static char* avi_stream2fourcc(char* tag
, int index
, enum CodecType type
)
82 if (type
== CODEC_TYPE_VIDEO
) {
93 static void avi_write_info_tag(ByteIOContext
*pb
, const char *tag
, const char *str
)
95 int len
= strlen(str
);
106 static void avi_write_info_tag2(AVFormatContext
*s
, const char *fourcc
, const char *key1
, const char *key2
)
108 AVMetadataTag
*tag
= av_metadata_get(s
->metadata
, key1
, NULL
, AV_METADATA_IGNORE_CASE
);
110 tag
= av_metadata_get(s
->metadata
, key2
, NULL
, AV_METADATA_IGNORE_CASE
);
112 avi_write_info_tag(s
->pb
, fourcc
, tag
->value
);
115 static int avi_write_counters(AVFormatContext
* s
, int riff_id
)
117 ByteIOContext
*pb
= s
->pb
;
118 AVIContext
*avi
= s
->priv_data
;
119 int n
, au_byterate
, au_ssize
, au_scale
, nb_frames
= 0;
121 AVCodecContext
* stream
;
123 file_size
= url_ftell(pb
);
124 for(n
= 0; n
< s
->nb_streams
; n
++) {
125 assert(avi
->frames_hdr_strm
[n
]);
126 stream
= s
->streams
[n
]->codec
;
127 url_fseek(pb
, avi
->frames_hdr_strm
[n
], SEEK_SET
);
128 ff_parse_specific_params(stream
, &au_byterate
, &au_ssize
, &au_scale
);
130 put_le32(pb
, avi
->packet_count
[n
]);
132 put_le32(pb
, avi
->audio_strm_length
[n
] / au_ssize
);
134 if(stream
->codec_type
== CODEC_TYPE_VIDEO
)
135 nb_frames
= FFMAX(nb_frames
, avi
->packet_count
[n
]);
138 assert(avi
->frames_hdr_all
);
139 url_fseek(pb
, avi
->frames_hdr_all
, SEEK_SET
);
140 put_le32(pb
, nb_frames
);
142 url_fseek(pb
, file_size
, SEEK_SET
);
147 static int avi_write_header(AVFormatContext
*s
)
149 AVIContext
*avi
= s
->priv_data
;
150 ByteIOContext
*pb
= s
->pb
;
151 int bitrate
, n
, i
, nb_frames
, au_byterate
, au_ssize
, au_scale
;
152 AVCodecContext
*stream
, *video_enc
;
153 int64_t list1
, list2
, strh
, strf
;
157 list1
= avi_start_new_riff(avi
, pb
, "AVI ", "hdrl");
161 put_le32(pb
, 14 * 4);
165 for(n
=0;n
<s
->nb_streams
;n
++) {
166 stream
= s
->streams
[n
]->codec
;
167 bitrate
+= stream
->bit_rate
;
168 if (stream
->codec_type
== CODEC_TYPE_VIDEO
)
175 put_le32(pb
, (uint32_t)(INT64_C(1000000) * video_enc
->time_base
.num
/ video_enc
->time_base
.den
));
179 put_le32(pb
, bitrate
/ 8); /* XXX: not quite exact */
180 put_le32(pb
, 0); /* padding */
181 if (url_is_streamed(pb
))
182 put_le32(pb
, AVIF_TRUSTCKTYPE
| AVIF_ISINTERLEAVED
); /* flags */
184 put_le32(pb
, AVIF_TRUSTCKTYPE
| AVIF_HASINDEX
| AVIF_ISINTERLEAVED
); /* flags */
185 avi
->frames_hdr_all
= url_ftell(pb
); /* remember this offset to fill later */
186 put_le32(pb
, nb_frames
); /* nb frames, filled later */
187 put_le32(pb
, 0); /* initial frame */
188 put_le32(pb
, s
->nb_streams
); /* nb streams */
189 put_le32(pb
, 1024 * 1024); /* suggested buffer size */
191 put_le32(pb
, video_enc
->width
);
192 put_le32(pb
, video_enc
->height
);
197 put_le32(pb
, 0); /* reserved */
198 put_le32(pb
, 0); /* reserved */
199 put_le32(pb
, 0); /* reserved */
200 put_le32(pb
, 0); /* reserved */
204 list2
= start_tag(pb
, "LIST");
207 stream
= s
->streams
[i
]->codec
;
209 /* stream generic header */
210 strh
= start_tag(pb
, "strh");
211 switch(stream
->codec_type
) {
212 case CODEC_TYPE_VIDEO
: put_tag(pb
, "vids"); break;
213 case CODEC_TYPE_AUDIO
: put_tag(pb
, "auds"); break;
214 // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
215 case CODEC_TYPE_DATA
: put_tag(pb
, "dats"); break;
217 if(stream
->codec_type
== CODEC_TYPE_VIDEO
)
218 put_le32(pb
, stream
->codec_tag
);
221 put_le32(pb
, 0); /* flags */
222 put_le16(pb
, 0); /* priority */
223 put_le16(pb
, 0); /* language */
224 put_le32(pb
, 0); /* initial frame */
226 ff_parse_specific_params(stream
, &au_byterate
, &au_ssize
, &au_scale
);
228 put_le32(pb
, au_scale
); /* scale */
229 put_le32(pb
, au_byterate
); /* rate */
230 av_set_pts_info(s
->streams
[i
], 64, au_scale
, au_byterate
);
232 put_le32(pb
, 0); /* start */
233 avi
->frames_hdr_strm
[i
] = url_ftell(pb
); /* remember this offset to fill later */
234 if (url_is_streamed(pb
))
235 put_le32(pb
, AVI_MAX_RIFF_SIZE
); /* FIXME: this may be broken, but who cares */
237 put_le32(pb
, 0); /* length, XXX: filled later */
239 /* suggested buffer size */ //FIXME set at the end to largest chunk
240 if(stream
->codec_type
== CODEC_TYPE_VIDEO
)
241 put_le32(pb
, 1024 * 1024);
242 else if(stream
->codec_type
== CODEC_TYPE_AUDIO
)
243 put_le32(pb
, 12 * 1024);
246 put_le32(pb
, -1); /* quality */
247 put_le32(pb
, au_ssize
); /* sample size */
249 put_le16(pb
, stream
->width
);
250 put_le16(pb
, stream
->height
);
253 if(stream
->codec_type
!= CODEC_TYPE_DATA
){
254 strf
= start_tag(pb
, "strf");
255 switch(stream
->codec_type
) {
256 case CODEC_TYPE_VIDEO
:
257 put_bmp_header(pb
, stream
, codec_bmp_tags
, 0);
259 case CODEC_TYPE_AUDIO
:
260 if (put_wav_header(pb
, stream
) < 0) {
270 if (!url_is_streamed(pb
)) {
271 unsigned char tag
[5];
274 /* Starting to lay out AVI OpenDML master index.
275 * We want to make it JUNK entry for now, since we'd
276 * like to get away without making AVI an OpenDML one
277 * for compatibility reasons.
279 avi
->indexes
[i
].entry
= avi
->indexes
[i
].ents_allocated
= 0;
280 avi
->indexes
[i
].indx_start
= start_tag(pb
, "JUNK");
281 put_le16(pb
, 4); /* wLongsPerEntry */
282 put_byte(pb
, 0); /* bIndexSubType (0 == frame index) */
283 put_byte(pb
, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
284 put_le32(pb
, 0); /* nEntriesInUse (will fill out later on) */
285 put_tag(pb
, avi_stream2fourcc(&tag
[0], i
, stream
->codec_type
));
287 put_le64(pb
, 0); /* dwReserved[3]
288 put_le32(pb, 0); Must be 0. */
289 for (j
=0; j
< AVI_MASTER_INDEX_SIZE
* 2; j
++)
291 end_tag(pb
, avi
->indexes
[i
].indx_start
);
294 if( stream
->codec_type
== CODEC_TYPE_VIDEO
295 && s
->streams
[i
]->sample_aspect_ratio
.num
>0
296 && s
->streams
[i
]->sample_aspect_ratio
.den
>0){
297 int vprp
= start_tag(pb
, "vprp");
298 AVRational dar
= av_mul_q(s
->streams
[i
]->sample_aspect_ratio
,
299 (AVRational
){stream
->width
, stream
->height
});
301 av_reduce(&num
, &den
, dar
.num
, dar
.den
, 0xFFFF);
303 put_le32(pb
, 0); //video format = unknown
304 put_le32(pb
, 0); //video standard= unknown
305 put_le32(pb
, lrintf(1.0/av_q2d(stream
->time_base
)));
306 put_le32(pb
, stream
->width
);
307 put_le32(pb
, stream
->height
);
310 put_le32(pb
, stream
->width
);
311 put_le32(pb
, stream
->height
);
312 put_le32(pb
, 1); //progressive FIXME
314 put_le32(pb
, stream
->height
);
315 put_le32(pb
, stream
->width
);
316 put_le32(pb
, stream
->height
);
317 put_le32(pb
, stream
->width
);
329 if (!url_is_streamed(pb
)) {
330 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
331 avi
->odml_list
= start_tag(pb
, "JUNK");
335 for (i
= 0; i
< 248; i
+= 4)
337 end_tag(pb
, avi
->odml_list
);
342 list2
= start_tag(pb
, "LIST");
344 avi_write_info_tag2(s
, "INAM", "Title", NULL
);
345 avi_write_info_tag2(s
, "IART", "Artist", "Author");
346 avi_write_info_tag2(s
, "ICOP", "Copyright", NULL
);
347 avi_write_info_tag2(s
, "ICMT", "Comment", NULL
);
348 avi_write_info_tag2(s
, "IPRD", "Album", NULL
);
349 avi_write_info_tag2(s
, "IGNR", "Genre", NULL
);
350 avi_write_info_tag2(s
, "IPRT", "Track", NULL
);
351 if(!(s
->streams
[0]->codec
->flags
& CODEC_FLAG_BITEXACT
))
352 avi_write_info_tag(pb
, "ISFT", LIBAVFORMAT_IDENT
);
355 /* some padding for easier tag editing */
356 list2
= start_tag(pb
, "JUNK");
357 for (i
= 0; i
< 1016; i
+= 4)
361 avi
->movi_list
= start_tag(pb
, "LIST");
364 put_flush_packet(pb
);
369 static int avi_write_ix(AVFormatContext
*s
)
371 ByteIOContext
*pb
= s
->pb
;
372 AVIContext
*avi
= s
->priv_data
;
374 char ix_tag
[] = "ix00";
377 assert(!url_is_streamed(pb
));
379 if (avi
->riff_id
> AVI_MASTER_INDEX_SIZE
)
382 for (i
=0;i
<s
->nb_streams
;i
++) {
385 avi_stream2fourcc(&tag
[0], i
, s
->streams
[i
]->codec
->codec_type
);
388 /* Writing AVI OpenDML leaf index chunk */
390 put_tag(pb
, &ix_tag
[0]); /* ix?? */
391 put_le32(pb
, avi
->indexes
[i
].entry
* 8 + 24);
393 put_le16(pb
, 2); /* wLongsPerEntry */
394 put_byte(pb
, 0); /* bIndexSubType (0 == frame index) */
395 put_byte(pb
, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
396 put_le32(pb
, avi
->indexes
[i
].entry
);
398 put_tag(pb
, &tag
[0]); /* dwChunkId */
399 put_le64(pb
, avi
->movi_list
);/* qwBaseOffset */
400 put_le32(pb
, 0); /* dwReserved_3 (must be 0) */
402 for (j
=0; j
<avi
->indexes
[i
].entry
; j
++) {
403 AVIIentry
* ie
= avi_get_ientry(&avi
->indexes
[i
], j
);
404 put_le32(pb
, ie
->pos
+ 8);
405 put_le32(pb
, ((uint32_t)ie
->len
& ~0x80000000) |
406 (ie
->flags
& 0x10 ? 0 : 0x80000000));
408 put_flush_packet(pb
);
411 /* Updating one entry in the AVI OpenDML master index */
412 url_fseek(pb
, avi
->indexes
[i
].indx_start
- 8, SEEK_SET
);
413 put_tag(pb
, "indx"); /* enabling this entry */
415 put_le32(pb
, avi
->riff_id
); /* nEntriesInUse */
416 url_fskip(pb
, 16*avi
->riff_id
);
417 put_le64(pb
, ix
); /* qwOffset */
418 put_le32(pb
, pos
- ix
); /* dwSize */
419 put_le32(pb
, avi
->indexes
[i
].entry
); /* dwDuration */
421 url_fseek(pb
, pos
, SEEK_SET
);
426 static int avi_write_idx1(AVFormatContext
*s
)
428 ByteIOContext
*pb
= s
->pb
;
429 AVIContext
*avi
= s
->priv_data
;
434 if (!url_is_streamed(pb
)) {
435 AVIIentry
* ie
= 0, *tie
;
436 int entry
[MAX_STREAMS
];
437 int empty
, stream_id
= -1;
439 idx_chunk
= start_tag(pb
, "idx1");
440 memset(&entry
[0], 0, sizeof(entry
));
443 for (i
=0; i
<s
->nb_streams
; i
++) {
444 if (avi
->indexes
[i
].entry
<= entry
[i
])
447 tie
= avi_get_ientry(&avi
->indexes
[i
], entry
[i
]);
448 if (empty
|| tie
->pos
< ie
->pos
) {
455 avi_stream2fourcc(&tag
[0], stream_id
,
456 s
->streams
[stream_id
]->codec
->codec_type
);
457 put_tag(pb
, &tag
[0]);
458 put_le32(pb
, ie
->flags
);
459 put_le32(pb
, ie
->pos
);
460 put_le32(pb
, ie
->len
);
464 end_tag(pb
, idx_chunk
);
466 avi_write_counters(s
, avi
->riff_id
);
471 static int avi_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
473 AVIContext
*avi
= s
->priv_data
;
474 ByteIOContext
*pb
= s
->pb
;
475 unsigned char tag
[5];
476 unsigned int flags
=0;
477 const int stream_index
= pkt
->stream_index
;
478 AVCodecContext
*enc
= s
->streams
[stream_index
]->codec
;
481 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
482 while(enc
->block_align
==0 && pkt
->dts
!= AV_NOPTS_VALUE
&& pkt
->dts
> avi
->packet_count
[stream_index
]){
483 AVPacket empty_packet
;
485 av_init_packet(&empty_packet
);
486 empty_packet
.size
= 0;
487 empty_packet
.data
= NULL
;
488 empty_packet
.stream_index
= stream_index
;
489 avi_write_packet(s
, &empty_packet
);
490 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
492 avi
->packet_count
[stream_index
]++;
494 // Make sure to put an OpenDML chunk when the file size exceeds the limits
495 if (!url_is_streamed(pb
) &&
496 (url_ftell(pb
) - avi
->riff_start
> AVI_MAX_RIFF_SIZE
)) {
499 end_tag(pb
, avi
->movi_list
);
501 if (avi
->riff_id
== 1)
504 end_tag(pb
, avi
->riff_start
);
505 avi
->movi_list
= avi_start_new_riff(avi
, pb
, "AVIX", "movi");
508 avi_stream2fourcc(&tag
[0], stream_index
, enc
->codec_type
);
509 if(pkt
->flags
&PKT_FLAG_KEY
)
511 if (enc
->codec_type
== CODEC_TYPE_AUDIO
) {
512 avi
->audio_strm_length
[stream_index
] += size
;
515 if (!url_is_streamed(s
->pb
)) {
516 AVIIndex
* idx
= &avi
->indexes
[stream_index
];
517 int cl
= idx
->entry
/ AVI_INDEX_CLUSTER_SIZE
;
518 int id
= idx
->entry
% AVI_INDEX_CLUSTER_SIZE
;
519 if (idx
->ents_allocated
<= idx
->entry
) {
520 idx
->cluster
= av_realloc(idx
->cluster
, (cl
+1)*sizeof(void*));
523 idx
->cluster
[cl
] = av_malloc(AVI_INDEX_CLUSTER_SIZE
*sizeof(AVIIentry
));
524 if (!idx
->cluster
[cl
])
526 idx
->ents_allocated
+= AVI_INDEX_CLUSTER_SIZE
;
529 idx
->cluster
[cl
][id
].flags
= flags
;
530 idx
->cluster
[cl
][id
].pos
= url_ftell(pb
) - avi
->movi_list
;
531 idx
->cluster
[cl
][id
].len
= size
;
535 put_buffer(pb
, tag
, 4);
537 put_buffer(pb
, pkt
->data
, size
);
541 put_flush_packet(pb
);
545 static int avi_write_trailer(AVFormatContext
*s
)
547 AVIContext
*avi
= s
->priv_data
;
548 ByteIOContext
*pb
= s
->pb
;
550 int i
, j
, n
, nb_frames
;
553 if (!url_is_streamed(pb
)){
554 if (avi
->riff_id
== 1) {
555 end_tag(pb
, avi
->movi_list
);
556 res
= avi_write_idx1(s
);
557 end_tag(pb
, avi
->riff_start
);
560 end_tag(pb
, avi
->movi_list
);
561 end_tag(pb
, avi
->riff_start
);
563 file_size
= url_ftell(pb
);
564 url_fseek(pb
, avi
->odml_list
- 8, SEEK_SET
);
565 put_tag(pb
, "LIST"); /* Making this AVI OpenDML one */
568 for (n
=nb_frames
=0;n
<s
->nb_streams
;n
++) {
569 AVCodecContext
*stream
= s
->streams
[n
]->codec
;
570 if (stream
->codec_type
== CODEC_TYPE_VIDEO
) {
571 if (nb_frames
< avi
->packet_count
[n
])
572 nb_frames
= avi
->packet_count
[n
];
574 if (stream
->codec_id
== CODEC_ID_MP2
|| stream
->codec_id
== CODEC_ID_MP3
) {
575 nb_frames
+= avi
->packet_count
[n
];
579 put_le32(pb
, nb_frames
);
580 url_fseek(pb
, file_size
, SEEK_SET
);
582 avi_write_counters(s
, avi
->riff_id
);
585 put_flush_packet(pb
);
587 for (i
=0; i
<MAX_STREAMS
; i
++) {
588 for (j
=0; j
<avi
->indexes
[i
].ents_allocated
/AVI_INDEX_CLUSTER_SIZE
; j
++)
589 av_free(avi
->indexes
[i
].cluster
[j
]);
590 av_free(avi
->indexes
[i
].cluster
);
591 avi
->indexes
[i
].cluster
= NULL
;
592 avi
->indexes
[i
].ents_allocated
= avi
->indexes
[i
].entry
= 0;
598 AVOutputFormat avi_muxer
= {
600 NULL_IF_CONFIG_SMALL("AVI format"),
609 .codec_tag
= (const AVCodecTag
* const []){codec_bmp_tags
, codec_wav_tags
, 0},
611 #endif //CONFIG_AVI_MUXER