avformat: Remove non-compiling and/or silly commented-out printf/av_log statements
[libav.git] / libavformat / avienc.c
CommitLineData
de6d9b64 1/*
7fbde343 2 * AVI muxer
406792e7 3 * Copyright (c) 2000 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
de6d9b64 21#include "avformat.h"
c3f9ebf7 22#include "internal.h"
de6d9b64 23#include "avi.h"
0abdb293 24#include "avio_internal.h"
9d9f4119 25#include "riff.h"
04d2540c 26#include "libavutil/intreadwrite.h"
d2d67e42 27#include "libavutil/dict.h"
de6d9b64
FB
28
29/*
115329f1 30 * TODO:
de6d9b64
FB
31 * - fill all fields if non streamed (nb_frames for example)
32 */
33
e738cee9 34typedef struct AVIIentry {
de6d9b64 35 unsigned int flags, pos, len;
e738cee9
RS
36} AVIIentry;
37
38#define AVI_INDEX_CLUSTER_SIZE 16384
39
40typedef struct AVIIndex {
bc5c918e 41 int64_t indx_start;
e738cee9
RS
42 int entry;
43 int ents_allocated;
44 AVIIentry** cluster;
de6d9b64
FB
45} AVIIndex;
46
47typedef struct {
bc5c918e 48 int64_t riff_start, movi_list, odml_list;
b4f0e04c 49 int64_t frames_hdr_all;
e738cee9 50 int riff_id;
de6d9b64
FB
51} AVIContext;
52
b4f0e04c
MN
53typedef struct {
54 int64_t frames_hdr_strm;
55 int audio_strm_length;
56 int packet_count;
12f1b1fe 57 int entry;
b4f0e04c
MN
58
59 AVIIndex indexes;
60} AVIStream ;
61
115329f1 62static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
e738cee9
RS
63{
64 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
65 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
66 return &idx->cluster[cl][id];
67}
68
ae628ec1 69static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
bc5c918e 70 const char* riff_tag, const char* list_tag)
ce9fce63 71{
b4f0e04c 72 AVIContext *avi= s->priv_data;
bc5c918e 73 int64_t loff;
e738cee9 74 int i;
115329f1 75
e738cee9 76 avi->riff_id++;
b4f0e04c
MN
77 for (i=0; i<s->nb_streams; i++){
78 AVIStream *avist= s->streams[i]->priv_data;
79 avist->indexes.entry = 0;
80 }
115329f1 81
1a40491e 82 avi->riff_start = ff_start_tag(pb, "RIFF");
0abdb293 83 ffio_wfourcc(pb, riff_tag);
1a40491e 84 loff = ff_start_tag(pb, "LIST");
0abdb293 85 ffio_wfourcc(pb, list_tag);
ce9fce63
RS
86 return loff;
87}
88
72415b2a 89static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
e738cee9 90{
4acc94e9 91 tag[0] = '0' + index/10;
92 tag[1] = '0' + index%10;
72415b2a 93 if (type == AVMEDIA_TYPE_VIDEO) {
e738cee9
RS
94 tag[2] = 'd';
95 tag[3] = 'c';
72415b2a 96 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
2a04d6e7
RD
97 // note: this is not an official code
98 tag[2] = 's';
99 tag[3] = 'b';
e738cee9
RS
100 } else {
101 tag[2] = 'w';
102 tag[3] = 'b';
103 }
104 tag[4] = '\0';
105 return tag;
106}
107
ae628ec1 108static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
57060f89
DC
109{
110 int len = strlen(str);
111 if (len > 0) {
112 len++;
0abdb293 113 ffio_wfourcc(pb, tag);
77eb5504 114 avio_wl32(pb, len);
4efd5cf3 115 avio_put_str(pb, str);
57060f89 116 if (len & 1)
77eb5504 117 avio_w8(pb, 0);
57060f89
DC
118 }
119}
120
5bce834e
BB
121static int avi_write_counters(AVFormatContext* s, int riff_id)
122{
ae628ec1 123 AVIOContext *pb = s->pb;
5bce834e
BB
124 AVIContext *avi = s->priv_data;
125 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
bc5c918e 126 int64_t file_size;
5bce834e
BB
127 AVCodecContext* stream;
128
a2704c97 129 file_size = avio_tell(pb);
5bce834e 130 for(n = 0; n < s->nb_streams; n++) {
b4f0e04c
MN
131 AVIStream *avist= s->streams[n]->priv_data;
132
133 assert(avist->frames_hdr_strm);
5bce834e 134 stream = s->streams[n]->codec;
6b4aa5da 135 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
5bce834e
BB
136 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
137 if(au_ssize == 0) {
77eb5504 138 avio_wl32(pb, avist->packet_count);
5bce834e 139 } else {
77eb5504 140 avio_wl32(pb, avist->audio_strm_length / au_ssize);
5bce834e 141 }
72415b2a 142 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
b4f0e04c 143 nb_frames = FFMAX(nb_frames, avist->packet_count);
5bce834e
BB
144 }
145 if(riff_id == 1) {
146 assert(avi->frames_hdr_all);
6b4aa5da 147 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
77eb5504 148 avio_wl32(pb, nb_frames);
5bce834e 149 }
6b4aa5da 150 avio_seek(pb, file_size, SEEK_SET);
5bce834e
BB
151
152 return 0;
153}
154
de6d9b64
FB
155static int avi_write_header(AVFormatContext *s)
156{
c9a65ca8 157 AVIContext *avi = s->priv_data;
ae628ec1 158 AVIOContext *pb = s->pb;
86bec9a1 159 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
de6d9b64 160 AVCodecContext *stream, *video_enc;
bc5c918e 161 int64_t list1, list2, strh, strf;
d2d67e42 162 AVDictionaryEntry *t = NULL;
de6d9b64 163
4acc94e9 164 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
165 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
166 AVI_MAX_STREAM_COUNT);
167 return -1;
168 }
169
b4f0e04c
MN
170 for(n=0;n<s->nb_streams;n++) {
171 s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
172 if(!s->streams[n]->priv_data)
173 return AVERROR(ENOMEM);
174 }
175
de6d9b64 176 /* header list */
e738cee9 177 avi->riff_id = 0;
b4f0e04c 178 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
de6d9b64
FB
179
180 /* avi header */
0abdb293 181 ffio_wfourcc(pb, "avih");
77eb5504 182 avio_wl32(pb, 14 * 4);
de6d9b64
FB
183 bitrate = 0;
184
185 video_enc = NULL;
186 for(n=0;n<s->nb_streams;n++) {
01f4895c 187 stream = s->streams[n]->codec;
de6d9b64 188 bitrate += stream->bit_rate;
72415b2a 189 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
de6d9b64
FB
190 video_enc = stream;
191 }
115329f1 192
de6d9b64
FB
193 nb_frames = 0;
194
850742d7 195 if(video_enc){
77eb5504 196 avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
850742d7 197 } else {
77eb5504 198 avio_wl32(pb, 0);
850742d7 199 }
77eb5504
AK
200 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
201 avio_wl32(pb, 0); /* padding */
8978feda 202 if (!pb->seekable)
77eb5504 203 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
a2a2668d 204 else
77eb5504 205 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
a2704c97 206 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
77eb5504
AK
207 avio_wl32(pb, nb_frames); /* nb frames, filled later */
208 avio_wl32(pb, 0); /* initial frame */
209 avio_wl32(pb, s->nb_streams); /* nb streams */
210 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
115329f1 211 if(video_enc){
77eb5504
AK
212 avio_wl32(pb, video_enc->width);
213 avio_wl32(pb, video_enc->height);
850742d7 214 } else {
77eb5504
AK
215 avio_wl32(pb, 0);
216 avio_wl32(pb, 0);
115329f1 217 }
77eb5504
AK
218 avio_wl32(pb, 0); /* reserved */
219 avio_wl32(pb, 0); /* reserved */
220 avio_wl32(pb, 0); /* reserved */
221 avio_wl32(pb, 0); /* reserved */
115329f1 222
de6d9b64
FB
223 /* stream list */
224 for(i=0;i<n;i++) {
b4f0e04c 225 AVIStream *avist= s->streams[i]->priv_data;
1a40491e 226 list2 = ff_start_tag(pb, "LIST");
0abdb293 227 ffio_wfourcc(pb, "strl");
115329f1 228
01f4895c 229 stream = s->streams[i]->codec;
de6d9b64
FB
230
231 /* stream generic header */
1a40491e 232 strh = ff_start_tag(pb, "strh");
de6d9b64 233 switch(stream->codec_type) {
72415b2a 234 case AVMEDIA_TYPE_SUBTITLE:
2a04d6e7
RD
235 // XSUB subtitles behave like video tracks, other subtitles
236 // are not (yet) supported.
36ef5369 237 if (stream->codec_id != AV_CODEC_ID_XSUB) {
d6a93490
JM
238 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
239 return AVERROR_PATCHWELCOME;
240 }
0abdb293
AK
241 case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
242 case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
243// case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
244 case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
de6d9b64 245 }
72415b2a 246 if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
36ef5369 247 stream->codec_id == AV_CODEC_ID_XSUB)
77eb5504 248 avio_wl32(pb, stream->codec_tag);
1d92cc2b 249 else
77eb5504
AK
250 avio_wl32(pb, 1);
251 avio_wl32(pb, 0); /* flags */
252 avio_wl16(pb, 0); /* priority */
253 avio_wl16(pb, 0); /* language */
254 avio_wl32(pb, 0); /* initial frame */
1d92cc2b
MN
255
256 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
257
77eb5504
AK
258 avio_wl32(pb, au_scale); /* scale */
259 avio_wl32(pb, au_byterate); /* rate */
c3f9ebf7 260 avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
1d92cc2b 261
77eb5504 262 avio_wl32(pb, 0); /* start */
a2704c97 263 avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
8978feda 264 if (!pb->seekable)
77eb5504 265 avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
bb270c08 266 else
77eb5504 267 avio_wl32(pb, 0); /* length, XXX: filled later */
115329f1 268
1d92cc2b 269 /* suggested buffer size */ //FIXME set at the end to largest chunk
72415b2a 270 if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
77eb5504 271 avio_wl32(pb, 1024 * 1024);
72415b2a 272 else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 273 avio_wl32(pb, 12 * 1024);
1d92cc2b 274 else
77eb5504
AK
275 avio_wl32(pb, 0);
276 avio_wl32(pb, -1); /* quality */
277 avio_wl32(pb, au_ssize); /* sample size */
278 avio_wl32(pb, 0);
279 avio_wl16(pb, stream->width);
280 avio_wl16(pb, stream->height);
1a40491e 281 ff_end_tag(pb, strh);
de6d9b64 282
72415b2a 283 if(stream->codec_type != AVMEDIA_TYPE_DATA){
1a40491e 284 strf = ff_start_tag(pb, "strf");
de6d9b64 285 switch(stream->codec_type) {
72415b2a 286 case AVMEDIA_TYPE_SUBTITLE:
2a04d6e7
RD
287 // XSUB subtitles behave like video tracks, other subtitles
288 // are not (yet) supported.
36ef5369 289 if (stream->codec_id != AV_CODEC_ID_XSUB) break;
72415b2a 290 case AVMEDIA_TYPE_VIDEO:
1a40491e 291 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
de6d9b64 292 break;
72415b2a 293 case AVMEDIA_TYPE_AUDIO:
1a40491e 294 if (ff_put_wav_header(pb, stream) < 0) {
46a3d068
FB
295 return -1;
296 }
de6d9b64 297 break;
53cafac0 298 default:
71c32f19 299 return -1;
de6d9b64 300 }
1a40491e 301 ff_end_tag(pb, strf);
d2d67e42 302 if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
04d2540c
AK
303 avi_write_info_tag(s->pb, "strn", t->value);
304 t = NULL;
305 }
1d92cc2b 306 }
115329f1 307
8978feda 308 if (pb->seekable) {
bb270c08
DB
309 unsigned char tag[5];
310 int j;
115329f1
DB
311
312 /* Starting to lay out AVI OpenDML master index.
bb270c08
DB
313 * We want to make it JUNK entry for now, since we'd
314 * like to get away without making AVI an OpenDML one
315 * for compatibility reasons.
316 */
b4f0e04c
MN
317 avist->indexes.entry = avist->indexes.ents_allocated = 0;
318 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
77eb5504
AK
319 avio_wl16(pb, 4); /* wLongsPerEntry */
320 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
321 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
322 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
99f42c27 323 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
bb270c08 324 /* dwChunkId */
77eb5504
AK
325 avio_wl64(pb, 0); /* dwReserved[3]
326 avio_wl32(pb, 0); Must be 0. */
bb270c08 327 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
77eb5504 328 avio_wl64(pb, 0);
b4f0e04c 329 ff_end_tag(pb, avist->indexes.indx_start);
bb270c08 330 }
115329f1 331
72415b2a 332 if( stream->codec_type == AVMEDIA_TYPE_VIDEO
59729451
AJ
333 && s->streams[i]->sample_aspect_ratio.num>0
334 && s->streams[i]->sample_aspect_ratio.den>0){
1a40491e 335 int vprp= ff_start_tag(pb, "vprp");
59729451 336 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
5b087549
MN
337 (AVRational){stream->width, stream->height});
338 int num, den;
339 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
340
77eb5504
AK
341 avio_wl32(pb, 0); //video format = unknown
342 avio_wl32(pb, 0); //video standard= unknown
343 avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
344 avio_wl32(pb, stream->width );
345 avio_wl32(pb, stream->height);
346 avio_wl16(pb, den);
347 avio_wl16(pb, num);
348 avio_wl32(pb, stream->width );
349 avio_wl32(pb, stream->height);
350 avio_wl32(pb, 1); //progressive FIXME
351
352 avio_wl32(pb, stream->height);
353 avio_wl32(pb, stream->width );
354 avio_wl32(pb, stream->height);
355 avio_wl32(pb, stream->width );
356 avio_wl32(pb, 0);
357 avio_wl32(pb, 0);
358
359 avio_wl32(pb, 0);
360 avio_wl32(pb, 0);
1a40491e 361 ff_end_tag(pb, vprp);
5b087549
MN
362 }
363
1a40491e 364 ff_end_tag(pb, list2);
de6d9b64 365 }
115329f1 366
8978feda 367 if (pb->seekable) {
e738cee9 368 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
1a40491e 369 avi->odml_list = ff_start_tag(pb, "JUNK");
0abdb293
AK
370 ffio_wfourcc(pb, "odml");
371 ffio_wfourcc(pb, "dmlh");
77eb5504 372 avio_wl32(pb, 248);
e738cee9 373 for (i = 0; i < 248; i+= 4)
77eb5504 374 avio_wl32(pb, 0);
1a40491e 375 ff_end_tag(pb, avi->odml_list);
e738cee9 376 }
de6d9b64 377
1a40491e 378 ff_end_tag(pb, list1);
115329f1 379
1a40491e 380 list2 = ff_start_tag(pb, "LIST");
0abdb293 381 ffio_wfourcc(pb, "INFO");
12bc2050
VV
382 ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
383 for (i = 0; *ff_riff_tags[i]; i++) {
384 if ((t = av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
04d2540c
AK
385 avi_write_info_tag(s->pb, t->key, t->value);
386 }
1a40491e 387 ff_end_tag(pb, list2);
57060f89
DC
388
389 /* some padding for easier tag editing */
1a40491e 390 list2 = ff_start_tag(pb, "JUNK");
57060f89 391 for (i = 0; i < 1016; i += 4)
77eb5504 392 avio_wl32(pb, 0);
1a40491e 393 ff_end_tag(pb, list2);
57060f89 394
1a40491e 395 avi->movi_list = ff_start_tag(pb, "LIST");
0abdb293 396 ffio_wfourcc(pb, "movi");
de6d9b64 397
b7f2fdde 398 avio_flush(pb);
de6d9b64
FB
399
400 return 0;
401}
402
e738cee9
RS
403static int avi_write_ix(AVFormatContext *s)
404{
ae628ec1 405 AVIOContext *pb = s->pb;
e738cee9 406 AVIContext *avi = s->priv_data;
191e8ca7
MR
407 char tag[5];
408 char ix_tag[] = "ix00";
e738cee9 409 int i, j;
115329f1 410
8978feda 411 assert(pb->seekable);
a2a2668d 412
e738cee9
RS
413 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
414 return -1;
115329f1 415
e738cee9 416 for (i=0;i<s->nb_streams;i++) {
b4f0e04c 417 AVIStream *avist= s->streams[i]->priv_data;
bc5c918e 418 int64_t ix, pos;
115329f1 419
99f42c27 420 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
bb270c08 421 ix_tag[3] = '0' + i;
115329f1 422
bb270c08 423 /* Writing AVI OpenDML leaf index chunk */
a2704c97 424 ix = avio_tell(pb);
99f42c27 425 ffio_wfourcc(pb, ix_tag); /* ix?? */
77eb5504 426 avio_wl32(pb, avist->indexes.entry * 8 + 24);
bb270c08 427 /* chunk size */
77eb5504
AK
428 avio_wl16(pb, 2); /* wLongsPerEntry */
429 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
430 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
431 avio_wl32(pb, avist->indexes.entry);
bb270c08 432 /* nEntriesInUse */
99f42c27 433 ffio_wfourcc(pb, tag); /* dwChunkId */
77eb5504
AK
434 avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
435 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
e738cee9 436
b4f0e04c
MN
437 for (j=0; j<avist->indexes.entry; j++) {
438 AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
77eb5504
AK
439 avio_wl32(pb, ie->pos + 8);
440 avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
bb270c08 441 (ie->flags & 0x10 ? 0 : 0x80000000));
e738cee9 442 }
b7f2fdde 443 avio_flush(pb);
a2704c97 444 pos = avio_tell(pb);
115329f1 445
bb270c08 446 /* Updating one entry in the AVI OpenDML master index */
6b4aa5da 447 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
0abdb293 448 ffio_wfourcc(pb, "indx"); /* enabling this entry */
45a8a02a 449 avio_skip(pb, 8);
77eb5504 450 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
45a8a02a 451 avio_skip(pb, 16*avi->riff_id);
77eb5504
AK
452 avio_wl64(pb, ix); /* qwOffset */
453 avio_wl32(pb, pos - ix); /* dwSize */
454 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
bb270c08 455
6b4aa5da 456 avio_seek(pb, pos, SEEK_SET);
e738cee9
RS
457 }
458 return 0;
459}
460
461static int avi_write_idx1(AVFormatContext *s)
ce9fce63 462{
ae628ec1 463 AVIOContext *pb = s->pb;
ce9fce63 464 AVIContext *avi = s->priv_data;
bc5c918e 465 int64_t idx_chunk;
5bce834e 466 int i;
191e8ca7 467 char tag[5];
ce9fce63 468
8978feda 469 if (pb->seekable) {
12f1b1fe 470 AVIStream *avist;
bb270c08 471 AVIIentry* ie = 0, *tie;
bb270c08
DB
472 int empty, stream_id = -1;
473
1a40491e 474 idx_chunk = ff_start_tag(pb, "idx1");
12f1b1fe
MN
475 for(i=0; i<s->nb_streams; i++){
476 avist= s->streams[i]->priv_data;
477 avist->entry=0;
478 }
479
bb270c08
DB
480 do {
481 empty = 1;
482 for (i=0; i<s->nb_streams; i++) {
12f1b1fe
MN
483 avist= s->streams[i]->priv_data;
484 if (avist->indexes.entry <= avist->entry)
bb270c08
DB
485 continue;
486
12f1b1fe 487 tie = avi_get_ientry(&avist->indexes, avist->entry);
bb270c08
DB
488 if (empty || tie->pos < ie->pos) {
489 ie = tie;
490 stream_id = i;
491 }
492 empty = 0;
493 }
494 if (!empty) {
12f1b1fe 495 avist= s->streams[stream_id]->priv_data;
99f42c27 496 avi_stream2fourcc(tag, stream_id,
bb270c08 497 s->streams[stream_id]->codec->codec_type);
99f42c27 498 ffio_wfourcc(pb, tag);
77eb5504
AK
499 avio_wl32(pb, ie->flags);
500 avio_wl32(pb, ie->pos);
501 avio_wl32(pb, ie->len);
12f1b1fe 502 avist->entry++;
bb270c08
DB
503 }
504 } while (!empty);
1a40491e 505 ff_end_tag(pb, idx_chunk);
ce9fce63 506
5bce834e 507 avi_write_counters(s, avi->riff_id);
ce9fce63
RS
508 }
509 return 0;
510}
511
e928649b 512static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
513{
514 AVIContext *avi = s->priv_data;
ae628ec1 515 AVIOContext *pb = s->pb;
de6d9b64 516 unsigned char tag[5];
e928649b 517 unsigned int flags=0;
e928649b 518 const int stream_index= pkt->stream_index;
b4f0e04c 519 AVIStream *avist= s->streams[stream_index]->priv_data;
01f4895c 520 AVCodecContext *enc= s->streams[stream_index]->codec;
e928649b 521 int size= pkt->size;
ce9fce63 522
b4f0e04c 523 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
50c3dd32
MN
524 AVPacket empty_packet;
525
526 av_init_packet(&empty_packet);
527 empty_packet.size= 0;
528 empty_packet.data= NULL;
529 empty_packet.stream_index= stream_index;
530 avi_write_packet(s, &empty_packet);
50c3dd32 531 }
b4f0e04c 532 avist->packet_count++;
50c3dd32 533
a2a2668d 534 // Make sure to put an OpenDML chunk when the file size exceeds the limits
8978feda 535 if (pb->seekable &&
a2704c97 536 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
115329f1 537
e738cee9 538 avi_write_ix(s);
1a40491e 539 ff_end_tag(pb, avi->movi_list);
115329f1 540
bb270c08
DB
541 if (avi->riff_id == 1)
542 avi_write_idx1(s);
e738cee9 543
1a40491e 544 ff_end_tag(pb, avi->riff_start);
b4f0e04c 545 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
ce9fce63 546 }
115329f1 547
99f42c27 548 avi_stream2fourcc(tag, stream_index, enc->codec_type);
cc947f04 549 if(pkt->flags&AV_PKT_FLAG_KEY)
e928649b 550 flags = 0x10;
72415b2a 551 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
b4f0e04c 552 avist->audio_strm_length += size;
e928649b 553 }
de6d9b64 554
8978feda 555 if (s->pb->seekable) {
b4f0e04c 556 AVIIndex* idx = &avist->indexes;
bb270c08
DB
557 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
558 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
e738cee9 559 if (idx->ents_allocated <= idx->entry) {
bb270c08
DB
560 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
561 if (!idx->cluster)
562 return -1;
e738cee9 563 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
bb270c08
DB
564 if (!idx->cluster[cl])
565 return -1;
566 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
567 }
115329f1 568
bb270c08 569 idx->cluster[cl][id].flags = flags;
a2704c97 570 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
e738cee9 571 idx->cluster[cl][id].len = size;
bb270c08 572 idx->entry++;
de6d9b64 573 }
115329f1 574
77eb5504
AK
575 avio_write(pb, tag, 4);
576 avio_wl32(pb, size);
577 avio_write(pb, pkt->data, size);
de6d9b64 578 if (size & 1)
77eb5504 579 avio_w8(pb, 0);
de6d9b64 580
b7f2fdde 581 avio_flush(pb);
de6d9b64
FB
582 return 0;
583}
584
585static int avi_write_trailer(AVFormatContext *s)
586{
de6d9b64 587 AVIContext *avi = s->priv_data;
ae628ec1 588 AVIOContext *pb = s->pb;
ce9fce63 589 int res = 0;
e738cee9 590 int i, j, n, nb_frames;
bc5c918e 591 int64_t file_size;
e738cee9 592
8978feda 593 if (pb->seekable){
5c4e1928 594 if (avi->riff_id == 1) {
1a40491e 595 ff_end_tag(pb, avi->movi_list);
5c4e1928 596 res = avi_write_idx1(s);
1a40491e 597 ff_end_tag(pb, avi->riff_start);
5c4e1928
MN
598 } else {
599 avi_write_ix(s);
1a40491e
DV
600 ff_end_tag(pb, avi->movi_list);
601 ff_end_tag(pb, avi->riff_start);
5c4e1928 602
a2704c97 603 file_size = avio_tell(pb);
6b4aa5da 604 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
0abdb293 605 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
45a8a02a 606 avio_skip(pb, 16);
5c4e1928
MN
607
608 for (n=nb_frames=0;n<s->nb_streams;n++) {
609 AVCodecContext *stream = s->streams[n]->codec;
b4f0e04c
MN
610 AVIStream *avist= s->streams[n]->priv_data;
611
72415b2a 612 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
b4f0e04c
MN
613 if (nb_frames < avist->packet_count)
614 nb_frames = avist->packet_count;
5c4e1928 615 } else {
36ef5369 616 if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
b4f0e04c 617 nb_frames += avist->packet_count;
5c4e1928 618 }
86bec9a1
J
619 }
620 }
77eb5504 621 avio_wl32(pb, nb_frames);
6b4aa5da 622 avio_seek(pb, file_size, SEEK_SET);
5bce834e 623
5c4e1928
MN
624 avi_write_counters(s, avi->riff_id);
625 }
a2a2668d 626 }
e738cee9 627
b4f0e04c
MN
628 for (i=0; i<s->nb_streams; i++) {
629 AVIStream *avist= s->streams[i]->priv_data;
630 for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
631 av_free(avist->indexes.cluster[j]);
632 av_freep(&avist->indexes.cluster);
633 avist->indexes.ents_allocated = avist->indexes.entry = 0;
e738cee9 634 }
115329f1 635
ce9fce63 636 return res;
de6d9b64
FB
637}
638
c6610a21 639AVOutputFormat ff_avi_muxer = {
dfc2c4d9 640 .name = "avi",
0177b7d2 641 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
dfc2c4d9
AK
642 .mime_type = "video/x-msvideo",
643 .extensions = "avi",
644 .priv_data_size = sizeof(AVIContext),
36ef5369
AK
645 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
646 .video_codec = AV_CODEC_ID_MPEG4,
dfc2c4d9
AK
647 .write_header = avi_write_header,
648 .write_packet = avi_write_packet,
649 .write_trailer = avi_write_trailer,
20234a4b
MS
650 .codec_tag = (const AVCodecTag* const []){
651 ff_codec_bmp_tags, ff_codec_wav_tags, 0
652 },
653 .flags = AVFMT_VARIABLE_FPS,
de6d9b64 654};