bump version numbers to 0.4.8
[libav.git] / libavformat / avienc.c
CommitLineData
de6d9b64
FB
1/*
2 * AVI encoder.
19720f15 3 * Copyright (c) 2000 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20#include "avi.h"
21
22/*
23 * TODO:
24 * - fill all fields if non streamed (nb_frames for example)
25 */
26
e738cee9 27typedef struct AVIIentry {
de6d9b64 28 unsigned int flags, pos, len;
e738cee9
RS
29} AVIIentry;
30
31#define AVI_INDEX_CLUSTER_SIZE 16384
32
33typedef struct AVIIndex {
34 offset_t indx_start;
35 int entry;
36 int ents_allocated;
37 AVIIentry** cluster;
de6d9b64
FB
38} AVIIndex;
39
40typedef struct {
ce9fce63
RS
41 offset_t riff_start, movi_list, odml_list;
42 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
86bec9a1 43 int audio_strm_length[MAX_STREAMS];
e738cee9
RS
44 int riff_id;
45
46 AVIIndex indexes[MAX_STREAMS];
de6d9b64
FB
47} AVIContext;
48
e738cee9
RS
49static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
50{
51 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
52 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
53 return &idx->cluster[cl][id];
54}
55
0570bf06 56offset_t start_tag(ByteIOContext *pb, const char *tag)
de6d9b64
FB
57{
58 put_tag(pb, tag);
59 put_le32(pb, 0);
60 return url_ftell(pb);
61}
62
63void end_tag(ByteIOContext *pb, offset_t start)
64{
65 offset_t pos;
66
67 pos = url_ftell(pb);
68 url_fseek(pb, start - 4, SEEK_SET);
0c1a9eda 69 put_le32(pb, (uint32_t)(pos - start));
de6d9b64
FB
70 url_fseek(pb, pos, SEEK_SET);
71}
72
73/* Note: when encoding, the first matching tag is used, so order is
74 important if multiple tags possible for a given codec. */
0570bf06 75const CodecTag codec_bmp_tags[] = {
95c79a24
J
76 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
77 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
de6d9b64 78 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
a05c8d71
MN
79
80 /* added based on MPlayer */
a05c8d71 81 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
a05c8d71
MN
82 { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
83
b1d89f82 84 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
b1d89f82
MR
85 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
86 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
390dffc5
MN
87 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
88 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
58f26ba9 89 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
a05c8d71
MN
90
91 /* added based on MPlayer */
92 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
a05c8d71
MN
93 { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
94 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
95 { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
96
b1d89f82 97 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
bb3debab 98 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
a05c8d71
MN
99
100 /* added based on MPlayer */
101 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
a05c8d71 102 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
a05c8d71 103 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
a05c8d71 104 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
a05c8d71
MN
105 { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
106 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
a05c8d71 107 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
a05c8d71 108
bb3debab 109 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
a05c8d71
MN
110
111 /* added based on MPlayer */
112 { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
a05c8d71 113
9b030d9d 114 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
a05c8d71
MN
115
116 /* added based on MPlayer */
117 { CODEC_ID_MSMPEG4V1, MKTAG('D', 'I', 'V', '4') },
a05c8d71 118
9b030d9d 119 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
a05c8d71
MN
120
121 /* added based on MPlayer */
e23d5712 122 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
020fcc94
FB
123 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
124 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
ce9fce63
RS
125 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
126 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
1cbc2890
FB
127 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
128 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
129 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
6b17c988 130 { CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
bb15f7fd 131 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
ccafe7b1 132 { CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
5b9d235f 133 { CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
ccafe7b1 134 { CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
f560dd82 135 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
1e711bd6 136 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
e8750b00 137 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
e8b478c1 138 { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
deabd4fd
MM
139 { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
140 { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
d86053a4 141 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
9ca9fce1 142 { CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
8d876a43 143 { CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
be3564ed 144 { CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
7408ad05 145 { CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
de6d9b64
FB
146 { 0, 0 },
147};
148
a266644f 149unsigned int codec_get_tag(const CodecTag *tags, int id)
de6d9b64
FB
150{
151 while (tags->id != 0) {
152 if (tags->id == id)
153 return tags->tag;
154 tags++;
155 }
156 return 0;
157}
158
2727c35e
PG
159static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
160{
161 while (tags->id != 0) {
162 if (!tags->invalid_asf && tags->id == id)
163 return tags->tag;
164 tags++;
165 }
166 return 0;
167}
168
5d234974 169enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag)
de6d9b64
FB
170{
171 while (tags->id != 0) {
77efa159
MN
172 if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF)
173 && toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF)
174 && toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF)
175 && toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF))
de6d9b64
FB
176 return tags->id;
177 tags++;
178 }
5d234974 179 return CODEC_ID_NONE;
de6d9b64
FB
180}
181
182unsigned int codec_get_bmp_tag(int id)
183{
184 return codec_get_tag(codec_bmp_tags, id);
185}
186
187/* BITMAPINFOHEADER header */
0570bf06 188void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
de6d9b64 189{
952c69c4 190 put_le32(pb, 40 + enc->extradata_size); /* size */
de6d9b64
FB
191 put_le32(pb, enc->width);
192 put_le32(pb, enc->height);
193 put_le16(pb, 1); /* planes */
952c69c4
MN
194
195 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
de6d9b64 196 /* compression type */
63167088 197 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : enc->codec_tag);
de6d9b64
FB
198 put_le32(pb, enc->width * enc->height * 3);
199 put_le32(pb, 0);
200 put_le32(pb, 0);
201 put_le32(pb, 0);
202 put_le32(pb, 0);
952c69c4
MN
203
204 put_buffer(pb, enc->extradata, enc->extradata_size);
205
206 if (enc->extradata_size & 1)
207 put_byte(pb, 0);
de6d9b64
FB
208}
209
0570bf06 210static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
86bec9a1
J
211{
212 switch(stream->codec_id) {
213 case CODEC_ID_PCM_S16LE:
214 *au_scale = *au_ssize = 2*stream->channels;
215 *au_byterate = *au_ssize * stream->sample_rate;
216 break;
217 case CODEC_ID_PCM_U8:
218 case CODEC_ID_PCM_ALAW:
219 case CODEC_ID_PCM_MULAW:
220 *au_scale = *au_ssize = stream->channels;
221 *au_byterate = *au_ssize * stream->sample_rate;
222 break;
223 case CODEC_ID_MP2:
224 *au_ssize = 1;
225 *au_scale = 1;
226 *au_byterate = stream->bit_rate / 8;
5798368b
J
227 case CODEC_ID_MP3LAME:
228 *au_ssize = 1;
229 *au_scale = 1;
230 *au_byterate = stream->bit_rate / 8;
86bec9a1
J
231 default:
232 *au_ssize = 1;
233 *au_scale = 1;
234 *au_byterate = stream->bit_rate / 8;
235 break;
236 }
237}
238
ce9fce63
RS
239static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
240 const char* riff_tag, const char* list_tag)
241{
242 offset_t loff;
e738cee9
RS
243 int i;
244
245 avi->riff_id++;
246 for (i=0; i<MAX_STREAMS; i++)
247 avi->indexes[i].entry = 0;
248
ce9fce63
RS
249 avi->riff_start = start_tag(pb, "RIFF");
250 put_tag(pb, riff_tag);
251 loff = start_tag(pb, "LIST");
252 put_tag(pb, list_tag);
253 return loff;
254}
255
e738cee9
RS
256static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
257 enum CodecType type)
258{
259 tag[0] = '0';
260 tag[1] = '0' + index;
261 if (type == CODEC_TYPE_VIDEO) {
262 tag[2] = 'd';
263 tag[3] = 'c';
264 } else {
265 tag[2] = 'w';
266 tag[3] = 'b';
267 }
268 tag[4] = '\0';
269 return tag;
270}
271
de6d9b64
FB
272static int avi_write_header(AVFormatContext *s)
273{
c9a65ca8 274 AVIContext *avi = s->priv_data;
de6d9b64 275 ByteIOContext *pb = &s->pb;
86bec9a1 276 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
de6d9b64
FB
277 AVCodecContext *stream, *video_enc;
278 offset_t list1, list2, strh, strf;
279
de6d9b64 280 /* header list */
e738cee9 281 avi->riff_id = 0;
ce9fce63 282 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
de6d9b64
FB
283
284 /* avi header */
285 put_tag(pb, "avih");
286 put_le32(pb, 14 * 4);
287 bitrate = 0;
288
289 video_enc = NULL;
290 for(n=0;n<s->nb_streams;n++) {
291 stream = &s->streams[n]->codec;
292 bitrate += stream->bit_rate;
293 if (stream->codec_type == CODEC_TYPE_VIDEO)
294 video_enc = stream;
295 }
296
de6d9b64
FB
297 nb_frames = 0;
298
850742d7 299 if(video_enc){
14bea432 300 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
850742d7
AR
301 } else {
302 put_le32(pb, 0);
303 }
de6d9b64
FB
304 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
305 put_le32(pb, 0); /* padding */
306 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
86bec9a1 307 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
308 put_le32(pb, nb_frames); /* nb frames, filled later */
309 put_le32(pb, 0); /* initial frame */
310 put_le32(pb, s->nb_streams); /* nb streams */
311 put_le32(pb, 1024 * 1024); /* suggested buffer size */
850742d7 312 if(video_enc){
de6d9b64
FB
313 put_le32(pb, video_enc->width);
314 put_le32(pb, video_enc->height);
850742d7
AR
315 } else {
316 put_le32(pb, 0);
317 put_le32(pb, 0);
318 }
de6d9b64
FB
319 put_le32(pb, 0); /* reserved */
320 put_le32(pb, 0); /* reserved */
321 put_le32(pb, 0); /* reserved */
322 put_le32(pb, 0); /* reserved */
323
324 /* stream list */
325 for(i=0;i<n;i++) {
326 list2 = start_tag(pb, "LIST");
327 put_tag(pb, "strl");
328
329 stream = &s->streams[i]->codec;
330
e8750b00
FR
331 /* FourCC should really be set by the codec itself */
332 if (! stream->codec_tag) {
333 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
334 }
335
de6d9b64
FB
336 /* stream generic header */
337 strh = start_tag(pb, "strh");
338 switch(stream->codec_type) {
339 case CODEC_TYPE_VIDEO:
340 put_tag(pb, "vids");
e8750b00 341 put_le32(pb, stream->codec_tag);
de6d9b64
FB
342 put_le32(pb, 0); /* flags */
343 put_le16(pb, 0); /* priority */
344 put_le16(pb, 0); /* language */
345 put_le32(pb, 0); /* initial frame */
b559b29b 346
14bea432
MN
347 put_le32(pb, stream->frame_rate_base); /* scale */
348 put_le32(pb, stream->frame_rate); /* rate */
b559b29b 349
de6d9b64 350 put_le32(pb, 0); /* start */
86bec9a1 351 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
352 put_le32(pb, nb_frames); /* length, XXX: fill later */
353 put_le32(pb, 1024 * 1024); /* suggested buffer size */
86bec9a1 354 put_le32(pb, -1); /* quality */
de6d9b64
FB
355 put_le32(pb, stream->width * stream->height * 3); /* sample size */
356 put_le16(pb, 0);
357 put_le16(pb, 0);
358 put_le16(pb, stream->width);
359 put_le16(pb, stream->height);
360 break;
361 case CODEC_TYPE_AUDIO:
362 put_tag(pb, "auds");
86bec9a1 363 put_le32(pb, 1); /* tag */
de6d9b64
FB
364 put_le32(pb, 0); /* flags */
365 put_le16(pb, 0); /* priority */
366 put_le16(pb, 0); /* language */
367 put_le32(pb, 0); /* initial frame */
86bec9a1
J
368 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
369 put_le32(pb, au_scale); /* scale */
370 put_le32(pb, au_byterate); /* rate */
de6d9b64 371 put_le32(pb, 0); /* start */
86bec9a1 372 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
373 put_le32(pb, 0); /* length, XXX: filled later */
374 put_le32(pb, 12 * 1024); /* suggested buffer size */
375 put_le32(pb, -1); /* quality */
86bec9a1 376 put_le32(pb, au_ssize); /* sample size */
de6d9b64
FB
377 put_le32(pb, 0);
378 put_le32(pb, 0);
379 break;
53cafac0 380 default:
42343f7e 381 av_abort();
de6d9b64
FB
382 }
383 end_tag(pb, strh);
384
385 strf = start_tag(pb, "strf");
386 switch(stream->codec_type) {
387 case CODEC_TYPE_VIDEO:
2727c35e 388 put_bmp_header(pb, stream, codec_bmp_tags, 0);
de6d9b64
FB
389 break;
390 case CODEC_TYPE_AUDIO:
46a3d068 391 if (put_wav_header(pb, stream) < 0) {
1ea4f593 392 av_free(avi);
46a3d068
FB
393 return -1;
394 }
de6d9b64 395 break;
53cafac0 396 default:
42343f7e 397 av_abort();
de6d9b64
FB
398 }
399 end_tag(pb, strf);
e738cee9
RS
400
401 if (!url_is_streamed(pb)) {
402 unsigned char tag[5];
8c0ff5d5 403 int j;
e738cee9
RS
404
405 /* Starting to lay out AVI OpenDML master index.
406 * We want to make it JUNK entry for now, since we'd
8c0ff5d5 407 * like to get away without making AVI an OpenDML one
e738cee9
RS
408 * for compatibility reasons.
409 */
410 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
411 avi->indexes[i].indx_start = start_tag(pb, "JUNK");
412 put_le16(pb, 4); /* wLongsPerEntry */
413 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
414 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
415 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
416 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
417 /* dwChunkId */
418 put_le64(pb, 0); /* dwReserved[3]
419 put_le32(pb, 0); Must be 0. */
8c0ff5d5
RS
420 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
421 put_le64(pb, 0);
e738cee9
RS
422 end_tag(pb, avi->indexes[i].indx_start);
423 }
424
de6d9b64
FB
425 end_tag(pb, list2);
426 }
ce9fce63 427
e738cee9
RS
428 if (!url_is_streamed(pb)) {
429 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
430 avi->odml_list = start_tag(pb, "JUNK");
431 put_tag(pb, "odml");
432 put_tag(pb, "dmlh");
433 put_le32(pb, 248);
434 for (i = 0; i < 248; i+= 4)
435 put_le32(pb, 0);
436 end_tag(pb, avi->odml_list);
437 }
de6d9b64
FB
438
439 end_tag(pb, list1);
440
441 avi->movi_list = start_tag(pb, "LIST");
de6d9b64
FB
442 put_tag(pb, "movi");
443
444 put_flush_packet(pb);
445
446 return 0;
447}
448
e738cee9
RS
449static int avi_write_ix(AVFormatContext *s)
450{
451 ByteIOContext *pb = &s->pb;
452 AVIContext *avi = s->priv_data;
453 unsigned char tag[5];
454 unsigned char ix_tag[] = "ix00";
455 int i, j;
456
457 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
458 return -1;
459
460 for (i=0;i<s->nb_streams;i++) {
461 offset_t ix, pos;
462
463 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec.codec_type);
464 ix_tag[3] = '0' + i;
465
466 /* Writing AVI OpenDML leaf index chunk */
467 ix = url_ftell(pb);
468 put_tag(pb, &ix_tag[0]); /* ix?? */
469 put_le32(pb, avi->indexes[i].entry * 8 + 24);
470 /* chunk size */
471 put_le16(pb, 2); /* wLongsPerEntry */
472 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
473 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
474 put_le32(pb, avi->indexes[i].entry);
475 /* nEntriesInUse */
476 put_tag(pb, &tag[0]); /* dwChunkId */
477 put_le64(pb, avi->movi_list);/* qwBaseOffset */
478 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
479
480 for (j=0; j<avi->indexes[i].entry; j++) {
481 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
482 put_le32(pb, ie->pos + 8);
483 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
484 (ie->flags & 0x10 ? 0 : 0x80000000));
485 }
486 put_flush_packet(pb);
487 pos = url_ftell(pb);
488
489 /* Updating one entry in the AVI OpenDML master index */
490 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
491 put_tag(pb, "indx"); /* enabling this entry */
492 url_fskip(pb, 8);
493 put_le32(pb, avi->riff_id); /* nEntriesInUse */
494 url_fskip(pb, 16*avi->riff_id);
495 put_le64(pb, ix); /* qwOffset */
496 put_le32(pb, pos - ix); /* dwSize */
497 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
498
499 url_fseek(pb, pos, SEEK_SET);
500 }
501 return 0;
502}
503
504static int avi_write_idx1(AVFormatContext *s)
ce9fce63
RS
505{
506 ByteIOContext *pb = &s->pb;
507 AVIContext *avi = s->priv_data;
508 offset_t file_size, idx_chunk;
e738cee9 509 int i, n, nb_frames, au_byterate, au_ssize, au_scale;
ce9fce63 510 AVCodecContext *stream;
e738cee9 511 unsigned char tag[5];
ce9fce63
RS
512
513 if (!url_is_streamed(pb)) {
546031ee 514 AVIIentry* ie = 0, *tie;
e738cee9 515 int entry[MAX_STREAMS];
546031ee 516 int empty, stream_id = -1;
e738cee9
RS
517
518 idx_chunk = start_tag(pb, "idx1");
519 memset(&entry[0], 0, sizeof(entry));
520 do {
521 empty = 1;
522 for (i=0; i<s->nb_streams; i++) {
523 if (avi->indexes[i].entry <= entry[i])
524 continue;
525
526 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
527 if (empty || tie->pos < ie->pos) {
528 ie = tie;
529 stream_id = i;
530 }
531 empty = 0;
532 }
533 if (!empty) {
534 avi_stream2fourcc(&tag[0], stream_id,
535 s->streams[stream_id]->codec.codec_type);
536 put_tag(pb, &tag[0]);
537 put_le32(pb, ie->flags);
538 put_le32(pb, ie->pos);
539 put_le32(pb, ie->len);
540 entry[stream_id]++;
541 }
542 } while (!empty);
543 end_tag(pb, idx_chunk);
ce9fce63
RS
544
545 /* Fill in frame/sample counters */
e738cee9 546 file_size = url_ftell(pb);
ce9fce63
RS
547 nb_frames = 0;
548 for(n=0;n<s->nb_streams;n++) {
549 if (avi->frames_hdr_strm[n] != 0) {
550 stream = &s->streams[n]->codec;
551 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
552 if (stream->codec_type == CODEC_TYPE_VIDEO) {
553 put_le32(pb, stream->frame_number);
554 if (nb_frames < stream->frame_number)
555 nb_frames = stream->frame_number;
556 } else {
557 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
558 put_le32(pb, stream->frame_number);
559 nb_frames += stream->frame_number;
560 } else {
561 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
562 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
563 }
564 }
565 }
566 }
567 if (avi->frames_hdr_all != 0) {
568 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
569 put_le32(pb, nb_frames);
570 }
571 url_fseek(pb, file_size, SEEK_SET);
572 }
573 return 0;
574}
575
de6d9b64 576static int avi_write_packet(AVFormatContext *s, int stream_index,
0c1a9eda 577 uint8_t *buf, int size, int force_pts)
de6d9b64
FB
578{
579 AVIContext *avi = s->priv_data;
580 ByteIOContext *pb = &s->pb;
de6d9b64
FB
581 unsigned char tag[5];
582 unsigned int flags;
583 AVCodecContext *enc;
ce9fce63
RS
584
585 if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) {
e738cee9
RS
586 avi_write_ix(s);
587 end_tag(pb, avi->movi_list);
588
589 if (avi->riff_id == 1)
590 avi_write_idx1(s);
591
592 end_tag(pb, avi->riff_start);
ce9fce63
RS
593 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
594 }
de6d9b64
FB
595
596 enc = &s->streams[stream_index]->codec;
e738cee9
RS
597 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
598 if (enc->codec_type == CODEC_TYPE_AUDIO) {
86bec9a1 599 avi->audio_strm_length[stream_index] += size;
e738cee9
RS
600 flags = 0x10;
601 } else
602 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
de6d9b64
FB
603
604 if (!url_is_streamed(&s->pb)) {
e738cee9
RS
605 AVIIndex* idx = &avi->indexes[stream_index];
606 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
607 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
608 if (idx->ents_allocated <= idx->entry) {
609 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
610 if (!idx->cluster)
611 return -1;
612 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
613 if (!idx->cluster[cl])
614 return -1;
615 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
616 }
617
618 idx->cluster[cl][id].flags = flags;
619 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
620 idx->cluster[cl][id].len = size;
621 idx->entry++;
de6d9b64
FB
622 }
623
624 put_buffer(pb, tag, 4);
625 put_le32(pb, size);
626 put_buffer(pb, buf, size);
627 if (size & 1)
628 put_byte(pb, 0);
629
630 put_flush_packet(pb);
631 return 0;
632}
633
634static int avi_write_trailer(AVFormatContext *s)
635{
de6d9b64 636 AVIContext *avi = s->priv_data;
ce9fce63
RS
637 ByteIOContext *pb = &s->pb;
638 int res = 0;
e738cee9
RS
639 int i, j, n, nb_frames;
640 offset_t file_size;
641
642 if (avi->riff_id == 1) {
643 end_tag(pb, avi->movi_list);
644 res = avi_write_idx1(s);
645 end_tag(pb, avi->riff_start);
646 } else {
647 avi_write_ix(s);
648 end_tag(pb, avi->movi_list);
649 end_tag(pb, avi->riff_start);
de6d9b64 650
de6d9b64 651 file_size = url_ftell(pb);
ce9fce63
RS
652 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
653 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
654 url_fskip(pb, 16);
655
656 for (n=nb_frames=0;n<s->nb_streams;n++) {
657 AVCodecContext *stream = &s->streams[n]->codec;
658 if (stream->codec_type == CODEC_TYPE_VIDEO) {
659 if (nb_frames < stream->frame_number)
660 nb_frames = stream->frame_number;
661 } else {
662 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
663 nb_frames += stream->frame_number;
86bec9a1
J
664 }
665 }
ce9fce63
RS
666 }
667 put_le32(pb, nb_frames);
ce9fce63 668 url_fseek(pb, file_size, SEEK_SET);
e738cee9 669 }
de6d9b64 670 put_flush_packet(pb);
e738cee9
RS
671
672 for (i=0; i<MAX_STREAMS; i++) {
673 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
674 av_free(avi->indexes[i].cluster[j]);
675 av_free(avi->indexes[i].cluster);
676 avi->indexes[i].cluster = NULL;
677 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
678 }
679
ce9fce63 680 return res;
de6d9b64
FB
681}
682
c9a65ca8 683static AVOutputFormat avi_oformat = {
de6d9b64
FB
684 "avi",
685 "avi format",
686 "video/x-msvideo",
687 "avi",
c9a65ca8 688 sizeof(AVIContext),
de6d9b64 689 CODEC_ID_MP2,
22cf0455 690 CODEC_ID_MPEG4,
de6d9b64
FB
691 avi_write_header,
692 avi_write_packet,
693 avi_write_trailer,
de6d9b64 694};
c9a65ca8
FB
695
696int avienc_init(void)
697{
698 av_register_output_format(&avi_oformat);
699 return 0;
700}