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