riffenc: take an AVStream instead of an AVCodecContext
[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;
3407172b 60} AVIStream;
b4f0e04c 61
3407172b 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,
3407172b 70 const char *riff_tag, const char *list_tag)
ce9fce63 71{
3407172b 72 AVIContext *avi = s->priv_data;
bc5c918e 73 int64_t loff;
e738cee9 74 int i;
115329f1 75
e738cee9 76 avi->riff_id++;
3407172b
KC
77 for (i = 0; i < s->nb_streams; i++) {
78 AVIStream *avist = s->streams[i]->priv_data;
b4f0e04c
MN
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
3407172b 89static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
e738cee9 90{
3407172b
KC
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
3407172b 108static int avi_write_counters(AVFormatContext *s, int riff_id)
5bce834e 109{
ae628ec1 110 AVIOContext *pb = s->pb;
5bce834e
BB
111 AVIContext *avi = s->priv_data;
112 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
bc5c918e 113 int64_t file_size;
3407172b 114 AVCodecContext *stream;
5bce834e 115
a2704c97 116 file_size = avio_tell(pb);
3407172b
KC
117 for (n = 0; n < s->nb_streams; n++) {
118 AVIStream *avist = s->streams[n]->priv_data;
b4f0e04c
MN
119
120 assert(avist->frames_hdr_strm);
5bce834e 121 stream = s->streams[n]->codec;
6b4aa5da 122 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
d754ed41 123 ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
3407172b 124 if (au_ssize == 0)
77eb5504 125 avio_wl32(pb, avist->packet_count);
3407172b 126 else
77eb5504 127 avio_wl32(pb, avist->audio_strm_length / au_ssize);
3407172b 128 if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
b4f0e04c 129 nb_frames = FFMAX(nb_frames, avist->packet_count);
5bce834e 130 }
3407172b 131 if (riff_id == 1) {
5bce834e 132 assert(avi->frames_hdr_all);
6b4aa5da 133 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
77eb5504 134 avio_wl32(pb, nb_frames);
5bce834e 135 }
6b4aa5da 136 avio_seek(pb, file_size, SEEK_SET);
5bce834e
BB
137
138 return 0;
139}
140
de6d9b64
FB
141static int avi_write_header(AVFormatContext *s)
142{
c9a65ca8 143 AVIContext *avi = s->priv_data;
ae628ec1 144 AVIOContext *pb = s->pb;
86bec9a1 145 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
c3311d47 146 AVCodecContext *video_enc;
bc5c918e 147 int64_t list1, list2, strh, strf;
d2d67e42 148 AVDictionaryEntry *t = NULL;
de6d9b64 149
4acc94e9 150 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
151 av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
152 AVI_MAX_STREAM_COUNT);
153 return -1;
154 }
155
3407172b
KC
156 for (n = 0; n < s->nb_streams; n++) {
157 s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
158 if (!s->streams[n]->priv_data)
b4f0e04c
MN
159 return AVERROR(ENOMEM);
160 }
161
de6d9b64 162 /* header list */
e738cee9 163 avi->riff_id = 0;
b4f0e04c 164 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
de6d9b64
FB
165
166 /* avi header */
0abdb293 167 ffio_wfourcc(pb, "avih");
77eb5504 168 avio_wl32(pb, 14 * 4);
de6d9b64
FB
169 bitrate = 0;
170
171 video_enc = NULL;
3407172b 172 for (n = 0; n < s->nb_streams; n++) {
c3311d47
AK
173 AVCodecContext *codec = s->streams[n]->codec;
174 bitrate += codec->bit_rate;
175 if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
176 video_enc = codec;
de6d9b64 177 }
115329f1 178
de6d9b64
FB
179 nb_frames = 0;
180
3407172b
KC
181 if (video_enc)
182 avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_enc->time_base.num /
183 video_enc->time_base.den));
184 else
77eb5504 185 avio_wl32(pb, 0);
77eb5504
AK
186 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
187 avio_wl32(pb, 0); /* padding */
8978feda 188 if (!pb->seekable)
3407172b 189 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
a2a2668d 190 else
3407172b 191 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
a2704c97 192 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
77eb5504
AK
193 avio_wl32(pb, nb_frames); /* nb frames, filled later */
194 avio_wl32(pb, 0); /* initial frame */
195 avio_wl32(pb, s->nb_streams); /* nb streams */
196 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
3407172b 197 if (video_enc) {
77eb5504
AK
198 avio_wl32(pb, video_enc->width);
199 avio_wl32(pb, video_enc->height);
850742d7 200 } else {
77eb5504
AK
201 avio_wl32(pb, 0);
202 avio_wl32(pb, 0);
115329f1 203 }
77eb5504
AK
204 avio_wl32(pb, 0); /* reserved */
205 avio_wl32(pb, 0); /* reserved */
206 avio_wl32(pb, 0); /* reserved */
207 avio_wl32(pb, 0); /* reserved */
115329f1 208
de6d9b64 209 /* stream list */
3407172b 210 for (i = 0; i < n; i++) {
c3311d47
AK
211 AVStream *st = s->streams[i];
212 AVCodecContext *enc = st->codec;
213 AVIStream *avist = st->priv_data;
1a40491e 214 list2 = ff_start_tag(pb, "LIST");
0abdb293 215 ffio_wfourcc(pb, "strl");
115329f1 216
de6d9b64 217 /* stream generic header */
1a40491e 218 strh = ff_start_tag(pb, "strh");
c3311d47 219 switch (enc->codec_type) {
72415b2a 220 case AVMEDIA_TYPE_SUBTITLE:
2a04d6e7
RD
221 // XSUB subtitles behave like video tracks, other subtitles
222 // are not (yet) supported.
c3311d47 223 if (enc->codec_id != AV_CODEC_ID_XSUB) {
3407172b
KC
224 av_log(s, AV_LOG_ERROR,
225 "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
d6a93490
JM
226 return AVERROR_PATCHWELCOME;
227 }
3407172b
KC
228 case AVMEDIA_TYPE_VIDEO:
229 ffio_wfourcc(pb, "vids");
230 break;
231 case AVMEDIA_TYPE_AUDIO:
232 ffio_wfourcc(pb, "auds");
233 break;
234// case AVMEDIA_TYPE_TEXT:
235// ffio_wfourcc(pb, "txts");
236// break;
237 case AVMEDIA_TYPE_DATA:
238 ffio_wfourcc(pb, "dats");
239 break;
de6d9b64 240 }
c3311d47
AK
241 if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
242 enc->codec_id == AV_CODEC_ID_XSUB)
243 avio_wl32(pb, enc->codec_tag);
1d92cc2b 244 else
77eb5504
AK
245 avio_wl32(pb, 1);
246 avio_wl32(pb, 0); /* flags */
247 avio_wl16(pb, 0); /* priority */
248 avio_wl16(pb, 0); /* language */
249 avio_wl32(pb, 0); /* initial frame */
1d92cc2b 250
d754ed41 251 ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
1d92cc2b 252
77eb5504
AK
253 avio_wl32(pb, au_scale); /* scale */
254 avio_wl32(pb, au_byterate); /* rate */
c3311d47 255 avpriv_set_pts_info(st, 64, au_scale, au_byterate);
1d92cc2b 256
77eb5504 257 avio_wl32(pb, 0); /* start */
3407172b
KC
258 /* remember this offset to fill later */
259 avist->frames_hdr_strm = avio_tell(pb);
8978feda 260 if (!pb->seekable)
3407172b
KC
261 /* FIXME: this may be broken, but who cares */
262 avio_wl32(pb, AVI_MAX_RIFF_SIZE);
bb270c08 263 else
3407172b 264 avio_wl32(pb, 0); /* length, XXX: filled later */
115329f1 265
1d92cc2b 266 /* suggested buffer size */ //FIXME set at the end to largest chunk
c3311d47 267 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
77eb5504 268 avio_wl32(pb, 1024 * 1024);
c3311d47 269 else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 270 avio_wl32(pb, 12 * 1024);
1d92cc2b 271 else
77eb5504
AK
272 avio_wl32(pb, 0);
273 avio_wl32(pb, -1); /* quality */
274 avio_wl32(pb, au_ssize); /* sample size */
275 avio_wl32(pb, 0);
c3311d47
AK
276 avio_wl16(pb, enc->width);
277 avio_wl16(pb, enc->height);
1a40491e 278 ff_end_tag(pb, strh);
de6d9b64 279
c3311d47 280 if (enc->codec_type != AVMEDIA_TYPE_DATA) {
3407172b 281 strf = ff_start_tag(pb, "strf");
c3311d47 282 switch (enc->codec_type) {
3407172b
KC
283 case AVMEDIA_TYPE_SUBTITLE:
284 /* XSUB subtitles behave like video tracks, other subtitles
285 * are not (yet) supported. */
c3311d47 286 if (enc->codec_id != AV_CODEC_ID_XSUB)
3407172b
KC
287 break;
288 case AVMEDIA_TYPE_VIDEO:
c3311d47 289 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0);
3407172b
KC
290 break;
291 case AVMEDIA_TYPE_AUDIO:
c3311d47 292 if (ff_put_wav_header(pb, enc) < 0)
3407172b
KC
293 return -1;
294 break;
295 default:
46a3d068
FB
296 return -1;
297 }
3407172b 298 ff_end_tag(pb, strf);
c3311d47 299 if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
3407172b
KC
300 ff_riff_write_info_tag(s->pb, "strn", t->value);
301 t = NULL;
302 }
04d2540c 303 }
115329f1 304
8978feda 305 if (pb->seekable) {
bb270c08
DB
306 unsigned char tag[5];
307 int j;
115329f1
DB
308
309 /* Starting to lay out AVI OpenDML master index.
bb270c08
DB
310 * We want to make it JUNK entry for now, since we'd
311 * like to get away without making AVI an OpenDML one
3407172b
KC
312 * for compatibility reasons. */
313 avist->indexes.entry = avist->indexes.ents_allocated = 0;
b4f0e04c 314 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
3407172b
KC
315 avio_wl16(pb, 4); /* wLongsPerEntry */
316 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
317 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
318 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
c3311d47 319 ffio_wfourcc(pb, avi_stream2fourcc(tag, i, enc->codec_type));
3407172b
KC
320 /* dwChunkId */
321 avio_wl64(pb, 0); /* dwReserved[3] */
322 // avio_wl32(pb, 0); /* Must be 0. */
323 for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
324 avio_wl64(pb, 0);
b4f0e04c 325 ff_end_tag(pb, avist->indexes.indx_start);
bb270c08 326 }
115329f1 327
c3311d47
AK
328 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
329 st->sample_aspect_ratio.num > 0 &&
330 st->sample_aspect_ratio.den > 0) {
3407172b 331 int vprp = ff_start_tag(pb, "vprp");
c3311d47
AK
332 AVRational dar = av_mul_q(st->sample_aspect_ratio,
333 (AVRational) { enc->width,
334 enc->height });
5b087549
MN
335 int num, den;
336 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
337
3407172b
KC
338 avio_wl32(pb, 0); // video format = unknown
339 avio_wl32(pb, 0); // video standard = unknown
c3311d47
AK
340 avio_wl32(pb, lrintf(1.0 / av_q2d(enc->time_base)));
341 avio_wl32(pb, enc->width);
342 avio_wl32(pb, enc->height);
77eb5504
AK
343 avio_wl16(pb, den);
344 avio_wl16(pb, num);
c3311d47
AK
345 avio_wl32(pb, enc->width);
346 avio_wl32(pb, enc->height);
3407172b 347 avio_wl32(pb, 1); // progressive FIXME
77eb5504 348
c3311d47
AK
349 avio_wl32(pb, enc->height);
350 avio_wl32(pb, enc->width);
351 avio_wl32(pb, enc->height);
352 avio_wl32(pb, enc->width);
77eb5504
AK
353 avio_wl32(pb, 0);
354 avio_wl32(pb, 0);
355
356 avio_wl32(pb, 0);
357 avio_wl32(pb, 0);
1a40491e 358 ff_end_tag(pb, vprp);
5b087549
MN
359 }
360
1a40491e 361 ff_end_tag(pb, list2);
de6d9b64 362 }
115329f1 363
8978feda 364 if (pb->seekable) {
e738cee9 365 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
1a40491e 366 avi->odml_list = ff_start_tag(pb, "JUNK");
0abdb293
AK
367 ffio_wfourcc(pb, "odml");
368 ffio_wfourcc(pb, "dmlh");
77eb5504 369 avio_wl32(pb, 248);
3407172b
KC
370 for (i = 0; i < 248; i += 4)
371 avio_wl32(pb, 0);
1a40491e 372 ff_end_tag(pb, avi->odml_list);
e738cee9 373 }
de6d9b64 374
1a40491e 375 ff_end_tag(pb, list1);
115329f1 376
71e92414 377 ff_riff_write_info(s);
57060f89
DC
378
379 /* some padding for easier tag editing */
1a40491e 380 list2 = ff_start_tag(pb, "JUNK");
57060f89 381 for (i = 0; i < 1016; i += 4)
77eb5504 382 avio_wl32(pb, 0);
1a40491e 383 ff_end_tag(pb, list2);
57060f89 384
1a40491e 385 avi->movi_list = ff_start_tag(pb, "LIST");
0abdb293 386 ffio_wfourcc(pb, "movi");
de6d9b64 387
b7f2fdde 388 avio_flush(pb);
de6d9b64
FB
389
390 return 0;
391}
392
e738cee9
RS
393static int avi_write_ix(AVFormatContext *s)
394{
ae628ec1 395 AVIOContext *pb = s->pb;
e738cee9 396 AVIContext *avi = s->priv_data;
191e8ca7
MR
397 char tag[5];
398 char ix_tag[] = "ix00";
e738cee9 399 int i, j;
115329f1 400
8978feda 401 assert(pb->seekable);
a2a2668d 402
e738cee9
RS
403 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
404 return -1;
115329f1 405
3407172b
KC
406 for (i = 0; i < s->nb_streams; i++) {
407 AVIStream *avist = s->streams[i]->priv_data;
408 int64_t ix, pos;
409
410 avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
411 ix_tag[3] = '0' + i;
412
413 /* Writing AVI OpenDML leaf index chunk */
414 ix = avio_tell(pb);
415 ffio_wfourcc(pb, ix_tag); /* ix?? */
416 avio_wl32(pb, avist->indexes.entry * 8 + 24);
417 /* chunk size */
418 avio_wl16(pb, 2); /* wLongsPerEntry */
419 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
420 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
421 avio_wl32(pb, avist->indexes.entry);
422 /* nEntriesInUse */
423 ffio_wfourcc(pb, tag); /* dwChunkId */
424 avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
425 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
426
427 for (j = 0; j < avist->indexes.entry; j++) {
428 AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
429 avio_wl32(pb, ie->pos + 8);
430 avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
bb270c08 431 (ie->flags & 0x10 ? 0 : 0x80000000));
3407172b
KC
432 }
433 avio_flush(pb);
434 pos = avio_tell(pb);
435
436 /* Updating one entry in the AVI OpenDML master index */
437 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
438 ffio_wfourcc(pb, "indx"); /* enabling this entry */
439 avio_skip(pb, 8);
440 avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
441 avio_skip(pb, 16 * avi->riff_id);
442 avio_wl64(pb, ix); /* qwOffset */
443 avio_wl32(pb, pos - ix); /* dwSize */
444 avio_wl32(pb, avist->indexes.entry); /* dwDuration */
445
446 avio_seek(pb, pos, SEEK_SET);
e738cee9
RS
447 }
448 return 0;
449}
450
451static int avi_write_idx1(AVFormatContext *s)
ce9fce63 452{
ae628ec1 453 AVIOContext *pb = s->pb;
ce9fce63 454 AVIContext *avi = s->priv_data;
bc5c918e 455 int64_t idx_chunk;
5bce834e 456 int i;
191e8ca7 457 char tag[5];
ce9fce63 458
8978feda 459 if (pb->seekable) {
12f1b1fe 460 AVIStream *avist;
3407172b 461 AVIIentry *ie = 0, *tie;
bb270c08
DB
462 int empty, stream_id = -1;
463
1a40491e 464 idx_chunk = ff_start_tag(pb, "idx1");
3407172b
KC
465 for (i = 0; i < s->nb_streams; i++) {
466 avist = s->streams[i]->priv_data;
467 avist->entry = 0;
12f1b1fe
MN
468 }
469
bb270c08
DB
470 do {
471 empty = 1;
3407172b
KC
472 for (i = 0; i < s->nb_streams; i++) {
473 avist = s->streams[i]->priv_data;
474 if (avist->indexes.entry <= avist->entry)
475 continue;
476
477 tie = avi_get_ientry(&avist->indexes, avist->entry);
478 if (empty || tie->pos < ie->pos) {
479 ie = tie;
480 stream_id = i;
481 }
482 empty = 0;
bb270c08
DB
483 }
484 if (!empty) {
3407172b 485 avist = s->streams[stream_id]->priv_data;
99f42c27 486 avi_stream2fourcc(tag, stream_id,
bb270c08 487 s->streams[stream_id]->codec->codec_type);
99f42c27 488 ffio_wfourcc(pb, tag);
77eb5504
AK
489 avio_wl32(pb, ie->flags);
490 avio_wl32(pb, ie->pos);
491 avio_wl32(pb, ie->len);
12f1b1fe 492 avist->entry++;
bb270c08
DB
493 }
494 } while (!empty);
1a40491e 495 ff_end_tag(pb, idx_chunk);
ce9fce63 496
5bce834e 497 avi_write_counters(s, avi->riff_id);
ce9fce63
RS
498 }
499 return 0;
500}
501
e928649b 502static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 503{
de6d9b64 504 unsigned char tag[5];
3407172b
KC
505 unsigned int flags = 0;
506 const int stream_index = pkt->stream_index;
507 int size = pkt->size;
508 AVIContext *avi = s->priv_data;
509 AVIOContext *pb = s->pb;
510 AVIStream *avist = s->streams[stream_index]->priv_data;
511 AVCodecContext *enc = s->streams[stream_index]->codec;
512
513 while (enc->block_align == 0 && pkt->dts != AV_NOPTS_VALUE &&
514 pkt->dts > avist->packet_count) {
50c3dd32
MN
515 AVPacket empty_packet;
516
517 av_init_packet(&empty_packet);
3407172b
KC
518 empty_packet.size = 0;
519 empty_packet.data = NULL;
520 empty_packet.stream_index = stream_index;
50c3dd32 521 avi_write_packet(s, &empty_packet);
50c3dd32 522 }
b4f0e04c 523 avist->packet_count++;
50c3dd32 524
a2a2668d 525 // Make sure to put an OpenDML chunk when the file size exceeds the limits
8978feda 526 if (pb->seekable &&
a2704c97 527 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
e738cee9 528 avi_write_ix(s);
1a40491e 529 ff_end_tag(pb, avi->movi_list);
115329f1 530
bb270c08
DB
531 if (avi->riff_id == 1)
532 avi_write_idx1(s);
e738cee9 533
1a40491e 534 ff_end_tag(pb, avi->riff_start);
b4f0e04c 535 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
ce9fce63 536 }
115329f1 537
99f42c27 538 avi_stream2fourcc(tag, stream_index, enc->codec_type);
3407172b 539 if (pkt->flags & AV_PKT_FLAG_KEY)
e928649b 540 flags = 0x10;
3407172b
KC
541 if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
542 avist->audio_strm_length += size;
de6d9b64 543
8978feda 544 if (s->pb->seekable) {
5626f994 545 int err;
3407172b 546 AVIIndex *idx = &avist->indexes;
bb270c08
DB
547 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
548 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
e738cee9 549 if (idx->ents_allocated <= idx->entry) {
3407172b
KC
550 if ((err = av_reallocp(&idx->cluster,
551 (cl + 1) * sizeof(*idx->cluster))) < 0) {
d872fb0f 552 idx->ents_allocated = 0;
3407172b 553 idx->entry = 0;
5626f994 554 return err;
d872fb0f 555 }
3407172b
KC
556 idx->cluster[cl] =
557 av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
bb270c08
DB
558 if (!idx->cluster[cl])
559 return -1;
560 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
561 }
115329f1 562
bb270c08 563 idx->cluster[cl][id].flags = flags;
3407172b
KC
564 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
565 idx->cluster[cl][id].len = size;
bb270c08 566 idx->entry++;
de6d9b64 567 }
115329f1 568
77eb5504
AK
569 avio_write(pb, tag, 4);
570 avio_wl32(pb, size);
571 avio_write(pb, pkt->data, size);
de6d9b64 572 if (size & 1)
77eb5504 573 avio_w8(pb, 0);
de6d9b64 574
de6d9b64
FB
575 return 0;
576}
577
578static int avi_write_trailer(AVFormatContext *s)
579{
de6d9b64 580 AVIContext *avi = s->priv_data;
ae628ec1 581 AVIOContext *pb = s->pb;
ce9fce63 582 int res = 0;
e738cee9 583 int i, j, n, nb_frames;
bc5c918e 584 int64_t file_size;
e738cee9 585
3407172b 586 if (pb->seekable) {
5c4e1928 587 if (avi->riff_id == 1) {
1a40491e 588 ff_end_tag(pb, avi->movi_list);
5c4e1928 589 res = avi_write_idx1(s);
1a40491e 590 ff_end_tag(pb, avi->riff_start);
5c4e1928
MN
591 } else {
592 avi_write_ix(s);
1a40491e
DV
593 ff_end_tag(pb, avi->movi_list);
594 ff_end_tag(pb, avi->riff_start);
5c4e1928 595
a2704c97 596 file_size = avio_tell(pb);
6b4aa5da 597 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
0abdb293 598 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
45a8a02a 599 avio_skip(pb, 16);
5c4e1928 600
3407172b 601 for (n = nb_frames = 0; n < s->nb_streams; n++) {
5c4e1928 602 AVCodecContext *stream = s->streams[n]->codec;
3407172b 603 AVIStream *avist = s->streams[n]->priv_data;
b4f0e04c 604
72415b2a 605 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
b4f0e04c
MN
606 if (nb_frames < avist->packet_count)
607 nb_frames = avist->packet_count;
5c4e1928 608 } else {
3407172b
KC
609 if (stream->codec_id == AV_CODEC_ID_MP2 ||
610 stream->codec_id == AV_CODEC_ID_MP3)
b4f0e04c 611 nb_frames += avist->packet_count;
86bec9a1
J
612 }
613 }
77eb5504 614 avio_wl32(pb, nb_frames);
6b4aa5da 615 avio_seek(pb, file_size, SEEK_SET);
5bce834e 616
5c4e1928
MN
617 avi_write_counters(s, avi->riff_id);
618 }
a2a2668d 619 }
e738cee9 620
3407172b
KC
621 for (i = 0; i < s->nb_streams; i++) {
622 AVIStream *avist = s->streams[i]->priv_data;
623 for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
624 av_free(avist->indexes.cluster[j]);
625 av_freep(&avist->indexes.cluster);
626 avist->indexes.ents_allocated = avist->indexes.entry = 0;
e738cee9 627 }
115329f1 628
ce9fce63 629 return res;
de6d9b64
FB
630}
631
c6610a21 632AVOutputFormat ff_avi_muxer = {
3407172b
KC
633 .name = "avi",
634 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
635 .mime_type = "video/x-msvideo",
636 .extensions = "avi",
637 .priv_data_size = sizeof(AVIContext),
638 .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
639 .video_codec = AV_CODEC_ID_MPEG4,
640 .write_header = avi_write_header,
641 .write_packet = avi_write_packet,
642 .write_trailer = avi_write_trailer,
643 .codec_tag = (const AVCodecTag * const []) {
20234a4b
MS
644 ff_codec_bmp_tags, ff_codec_wav_tags, 0
645 },
de6d9b64 646};