3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * - fill all fields if non streamed (nb_frames for example)
28 #ifdef CONFIG_AVI_MUXER
29 typedef struct AVIIentry {
30 unsigned int flags, pos, len;
33 #define AVI_INDEX_CLUSTER_SIZE 16384
35 typedef struct AVIIndex {
43 offset_t riff_start, movi_list, odml_list;
44 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
45 int audio_strm_length[MAX_STREAMS];
47 int packet_count[MAX_STREAMS];
49 AVIIndex indexes[MAX_STREAMS];
52 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
54 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
55 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
56 return &idx->cluster[cl][id];
59 static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
60 const char* riff_tag, const char* list_tag)
66 for (i=0; i<MAX_STREAMS; i++)
67 avi->indexes[i].entry = 0;
69 avi->riff_start = start_tag(pb, "RIFF");
70 put_tag(pb, riff_tag);
71 loff = start_tag(pb, "LIST");
72 put_tag(pb, list_tag);
76 static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
81 if (type == CODEC_TYPE_VIDEO) {
92 static int avi_write_header(AVFormatContext *s)
94 AVIContext *avi = s->priv_data;
95 ByteIOContext *pb = &s->pb;
96 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
97 AVCodecContext *stream, *video_enc;
98 offset_t list1, list2, strh, strf;
102 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
106 put_le32(pb, 14 * 4);
110 for(n=0;n<s->nb_streams;n++) {
111 stream = s->streams[n]->codec;
112 bitrate += stream->bit_rate;
113 if (stream->codec_type == CODEC_TYPE_VIDEO)
120 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
124 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
125 put_le32(pb, 0); /* padding */
126 if (url_is_streamed(pb))
127 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
129 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
130 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
131 put_le32(pb, nb_frames); /* nb frames, filled later */
132 put_le32(pb, 0); /* initial frame */
133 put_le32(pb, s->nb_streams); /* nb streams */
134 put_le32(pb, 1024 * 1024); /* suggested buffer size */
136 put_le32(pb, video_enc->width);
137 put_le32(pb, video_enc->height);
142 put_le32(pb, 0); /* reserved */
143 put_le32(pb, 0); /* reserved */
144 put_le32(pb, 0); /* reserved */
145 put_le32(pb, 0); /* reserved */
149 list2 = start_tag(pb, "LIST");
152 stream = s->streams[i]->codec;
154 /* FourCC should really be set by the codec itself */
155 if (! stream->codec_tag) {
156 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
159 /* stream generic header */
160 strh = start_tag(pb, "strh");
161 switch(stream->codec_type) {
162 case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
163 case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
164 // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
165 case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
167 if(stream->codec_type == CODEC_TYPE_VIDEO)
168 put_le32(pb, stream->codec_tag);
171 put_le32(pb, 0); /* flags */
172 put_le16(pb, 0); /* priority */
173 put_le16(pb, 0); /* language */
174 put_le32(pb, 0); /* initial frame */
176 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
178 put_le32(pb, au_scale); /* scale */
179 put_le32(pb, au_byterate); /* rate */
180 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
182 put_le32(pb, 0); /* start */
183 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
184 if (url_is_streamed(pb))
185 put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
187 put_le32(pb, 0); /* length, XXX: filled later */
189 /* suggested buffer size */ //FIXME set at the end to largest chunk
190 if(stream->codec_type == CODEC_TYPE_VIDEO)
191 put_le32(pb, 1024 * 1024);
192 else if(stream->codec_type == CODEC_TYPE_AUDIO)
193 put_le32(pb, 12 * 1024);
196 put_le32(pb, -1); /* quality */
197 put_le32(pb, au_ssize); /* sample size */
199 put_le16(pb, stream->width);
200 put_le16(pb, stream->height);
203 if(stream->codec_type != CODEC_TYPE_DATA){
204 strf = start_tag(pb, "strf");
205 switch(stream->codec_type) {
206 case CODEC_TYPE_VIDEO:
207 put_bmp_header(pb, stream, codec_bmp_tags, 0);
209 case CODEC_TYPE_AUDIO:
210 if (put_wav_header(pb, stream) < 0) {
221 if (!url_is_streamed(pb)) {
222 unsigned char tag[5];
225 /* Starting to lay out AVI OpenDML master index.
226 * We want to make it JUNK entry for now, since we'd
227 * like to get away without making AVI an OpenDML one
228 * for compatibility reasons.
230 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
231 avi->indexes[i].indx_start = start_tag(pb, "JUNK");
232 put_le16(pb, 4); /* wLongsPerEntry */
233 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
234 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
235 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
236 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
238 put_le64(pb, 0); /* dwReserved[3]
239 put_le32(pb, 0); Must be 0. */
240 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
242 end_tag(pb, avi->indexes[i].indx_start);
248 if (!url_is_streamed(pb)) {
249 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
250 avi->odml_list = start_tag(pb, "JUNK");
254 for (i = 0; i < 248; i+= 4)
256 end_tag(pb, avi->odml_list);
261 avi->movi_list = start_tag(pb, "LIST");
264 put_flush_packet(pb);
269 static int avi_write_ix(AVFormatContext *s)
271 ByteIOContext *pb = &s->pb;
272 AVIContext *avi = s->priv_data;
273 unsigned char tag[5];
274 unsigned char ix_tag[] = "ix00";
277 assert(!url_is_streamed(pb));
279 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
282 for (i=0;i<s->nb_streams;i++) {
285 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
288 /* Writing AVI OpenDML leaf index chunk */
290 put_tag(pb, &ix_tag[0]); /* ix?? */
291 put_le32(pb, avi->indexes[i].entry * 8 + 24);
293 put_le16(pb, 2); /* wLongsPerEntry */
294 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
295 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
296 put_le32(pb, avi->indexes[i].entry);
298 put_tag(pb, &tag[0]); /* dwChunkId */
299 put_le64(pb, avi->movi_list);/* qwBaseOffset */
300 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
302 for (j=0; j<avi->indexes[i].entry; j++) {
303 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
304 put_le32(pb, ie->pos + 8);
305 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
306 (ie->flags & 0x10 ? 0 : 0x80000000));
308 put_flush_packet(pb);
311 /* Updating one entry in the AVI OpenDML master index */
312 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
313 put_tag(pb, "indx"); /* enabling this entry */
315 put_le32(pb, avi->riff_id); /* nEntriesInUse */
316 url_fskip(pb, 16*avi->riff_id);
317 put_le64(pb, ix); /* qwOffset */
318 put_le32(pb, pos - ix); /* dwSize */
319 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
321 url_fseek(pb, pos, SEEK_SET);
326 static int avi_write_idx1(AVFormatContext *s)
328 ByteIOContext *pb = &s->pb;
329 AVIContext *avi = s->priv_data;
330 offset_t file_size, idx_chunk;
331 int i, n, nb_frames, au_byterate, au_ssize, au_scale;
332 AVCodecContext *stream;
333 unsigned char tag[5];
335 if (!url_is_streamed(pb)) {
336 AVIIentry* ie = 0, *tie;
337 int entry[MAX_STREAMS];
338 int empty, stream_id = -1;
340 idx_chunk = start_tag(pb, "idx1");
341 memset(&entry[0], 0, sizeof(entry));
344 for (i=0; i<s->nb_streams; i++) {
345 if (avi->indexes[i].entry <= entry[i])
348 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
349 if (empty || tie->pos < ie->pos) {
356 avi_stream2fourcc(&tag[0], stream_id,
357 s->streams[stream_id]->codec->codec_type);
358 put_tag(pb, &tag[0]);
359 put_le32(pb, ie->flags);
360 put_le32(pb, ie->pos);
361 put_le32(pb, ie->len);
365 end_tag(pb, idx_chunk);
367 /* Fill in frame/sample counters */
368 file_size = url_ftell(pb);
370 for(n=0;n<s->nb_streams;n++) {
371 assert(avi->frames_hdr_strm[n]);
372 stream = s->streams[n]->codec;
373 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
374 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
376 put_le32(pb, avi->packet_count[n]);
378 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
380 if(stream->codec_type == CODEC_TYPE_VIDEO)
381 nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
383 assert(avi->frames_hdr_all);
384 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
385 put_le32(pb, nb_frames);
386 url_fseek(pb, file_size, SEEK_SET);
391 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
393 AVIContext *avi = s->priv_data;
394 ByteIOContext *pb = &s->pb;
395 unsigned char tag[5];
396 unsigned int flags=0;
397 const int stream_index= pkt->stream_index;
398 AVCodecContext *enc= s->streams[stream_index]->codec;
401 // av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
402 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
403 AVPacket empty_packet;
405 av_init_packet(&empty_packet);
406 empty_packet.size= 0;
407 empty_packet.data= NULL;
408 empty_packet.stream_index= stream_index;
409 avi_write_packet(s, &empty_packet);
410 // av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
412 avi->packet_count[stream_index]++;
414 // Make sure to put an OpenDML chunk when the file size exceeds the limits
415 if (!url_is_streamed(pb) &&
416 (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
419 end_tag(pb, avi->movi_list);
421 if (avi->riff_id == 1)
424 end_tag(pb, avi->riff_start);
425 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
428 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
429 if(pkt->flags&PKT_FLAG_KEY)
431 if (enc->codec_type == CODEC_TYPE_AUDIO) {
432 avi->audio_strm_length[stream_index] += size;
435 if (!url_is_streamed(&s->pb)) {
436 AVIIndex* idx = &avi->indexes[stream_index];
437 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
438 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
439 if (idx->ents_allocated <= idx->entry) {
440 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
443 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
444 if (!idx->cluster[cl])
446 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
449 idx->cluster[cl][id].flags = flags;
450 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
451 idx->cluster[cl][id].len = size;
455 put_buffer(pb, tag, 4);
457 put_buffer(pb, pkt->data, size);
461 put_flush_packet(pb);
465 static int avi_write_trailer(AVFormatContext *s)
467 AVIContext *avi = s->priv_data;
468 ByteIOContext *pb = &s->pb;
470 int i, j, n, nb_frames;
473 if (!url_is_streamed(pb))
475 if (avi->riff_id == 1) {
476 end_tag(pb, avi->movi_list);
477 res = avi_write_idx1(s);
478 end_tag(pb, avi->riff_start);
481 end_tag(pb, avi->movi_list);
482 end_tag(pb, avi->riff_start);
484 file_size = url_ftell(pb);
485 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
486 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
489 for (n=nb_frames=0;n<s->nb_streams;n++) {
490 AVCodecContext *stream = s->streams[n]->codec;
491 if (stream->codec_type == CODEC_TYPE_VIDEO) {
492 if (nb_frames < avi->packet_count[n])
493 nb_frames = avi->packet_count[n];
495 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
496 nb_frames += avi->packet_count[n];
500 put_le32(pb, nb_frames);
501 url_fseek(pb, file_size, SEEK_SET);
504 put_flush_packet(pb);
506 for (i=0; i<MAX_STREAMS; i++) {
507 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
508 av_free(avi->indexes[i].cluster[j]);
509 av_free(avi->indexes[i].cluster);
510 avi->indexes[i].cluster = NULL;
511 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
517 AVOutputFormat avi_muxer = {
529 #endif //CONFIG_AVI_MUXER