Added VideoBitRateRange and VideoBitRAteTolerance settings
[libav.git] / libav / 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
27typedef struct AVIIndex {
28 unsigned char tag[4];
29 unsigned int flags, pos, len;
30 struct AVIIndex *next;
31} AVIIndex;
32
33typedef struct {
86bec9a1
J
34 offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
35 int audio_strm_length[MAX_STREAMS];
de6d9b64
FB
36 AVIIndex *first, *last;
37} AVIContext;
38
39offset_t start_tag(ByteIOContext *pb, char *tag)
40{
41 put_tag(pb, tag);
42 put_le32(pb, 0);
43 return url_ftell(pb);
44}
45
46void end_tag(ByteIOContext *pb, offset_t start)
47{
48 offset_t pos;
49
50 pos = url_ftell(pb);
51 url_fseek(pb, start - 4, SEEK_SET);
8be1c656 52 put_le32(pb, (UINT32)(pos - start));
de6d9b64
FB
53 url_fseek(pb, pos, SEEK_SET);
54}
55
56/* Note: when encoding, the first matching tag is used, so order is
57 important if multiple tags possible for a given codec. */
58CodecTag codec_bmp_tags[] = {
95c79a24
J
59 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
60 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
de6d9b64
FB
61 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
62 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
58f26ba9
FB
63 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
64 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x') },
390dffc5
MN
65 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0') },
66 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
67 { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd') },
68 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's') },
69 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
70 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
71 { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
58f26ba9 72 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
bb3debab
GV
73 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
74 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
75 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
9b030d9d
MN
76 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
77 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
020fcc94
FB
78 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
79 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
f20dca40 80 { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') },
020fcc94 81 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
1cbc2890
FB
82 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
83 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
84 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
bb15f7fd 85 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
de6d9b64
FB
86 { 0, 0 },
87};
88
a266644f 89unsigned int codec_get_tag(const CodecTag *tags, int id)
de6d9b64
FB
90{
91 while (tags->id != 0) {
92 if (tags->id == id)
93 return tags->tag;
94 tags++;
95 }
96 return 0;
97}
98
a266644f 99int codec_get_id(const CodecTag *tags, unsigned int tag)
de6d9b64
FB
100{
101 while (tags->id != 0) {
102 if (tags->tag == tag)
103 return tags->id;
104 tags++;
105 }
106 return 0;
107}
108
109unsigned int codec_get_bmp_tag(int id)
110{
111 return codec_get_tag(codec_bmp_tags, id);
112}
113
114/* BITMAPINFOHEADER header */
502d105f 115void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags)
de6d9b64
FB
116{
117 put_le32(pb, 40); /* size */
118 put_le32(pb, enc->width);
119 put_le32(pb, enc->height);
120 put_le16(pb, 1); /* planes */
121 put_le16(pb, 24); /* depth */
122 /* compression type */
502d105f 123 put_le32(pb, codec_get_tag(tags, enc->codec_id));
de6d9b64
FB
124 put_le32(pb, enc->width * enc->height * 3);
125 put_le32(pb, 0);
126 put_le32(pb, 0);
127 put_le32(pb, 0);
128 put_le32(pb, 0);
129}
130
86bec9a1
J
131void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
132{
133 switch(stream->codec_id) {
134 case CODEC_ID_PCM_S16LE:
135 *au_scale = *au_ssize = 2*stream->channels;
136 *au_byterate = *au_ssize * stream->sample_rate;
137 break;
138 case CODEC_ID_PCM_U8:
139 case CODEC_ID_PCM_ALAW:
140 case CODEC_ID_PCM_MULAW:
141 *au_scale = *au_ssize = stream->channels;
142 *au_byterate = *au_ssize * stream->sample_rate;
143 break;
144 case CODEC_ID_MP2:
145 *au_ssize = 1;
146 *au_scale = 1;
147 *au_byterate = stream->bit_rate / 8;
5798368b
J
148 case CODEC_ID_MP3LAME:
149 *au_ssize = 1;
150 *au_scale = 1;
151 *au_byterate = stream->bit_rate / 8;
86bec9a1
J
152 default:
153 *au_ssize = 1;
154 *au_scale = 1;
155 *au_byterate = stream->bit_rate / 8;
156 break;
157 }
158}
159
de6d9b64
FB
160static int avi_write_header(AVFormatContext *s)
161{
c9a65ca8 162 AVIContext *avi = s->priv_data;
de6d9b64 163 ByteIOContext *pb = &s->pb;
86bec9a1 164 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
de6d9b64
FB
165 AVCodecContext *stream, *video_enc;
166 offset_t list1, list2, strh, strf;
167
de6d9b64
FB
168 put_tag(pb, "RIFF");
169 put_le32(pb, 0); /* file length */
170 put_tag(pb, "AVI ");
171
172 /* header list */
173 list1 = start_tag(pb, "LIST");
174 put_tag(pb, "hdrl");
175
176 /* avi header */
177 put_tag(pb, "avih");
178 put_le32(pb, 14 * 4);
179 bitrate = 0;
180
181 video_enc = NULL;
182 for(n=0;n<s->nb_streams;n++) {
183 stream = &s->streams[n]->codec;
184 bitrate += stream->bit_rate;
185 if (stream->codec_type == CODEC_TYPE_VIDEO)
186 video_enc = stream;
187 }
188
189 if (!video_enc) {
1ea4f593 190 av_free(avi);
de6d9b64
FB
191 return -1;
192 }
193 nb_frames = 0;
194
8be1c656 195 put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
de6d9b64
FB
196 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
197 put_le32(pb, 0); /* padding */
198 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
86bec9a1 199 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
200 put_le32(pb, nb_frames); /* nb frames, filled later */
201 put_le32(pb, 0); /* initial frame */
202 put_le32(pb, s->nb_streams); /* nb streams */
203 put_le32(pb, 1024 * 1024); /* suggested buffer size */
204 put_le32(pb, video_enc->width);
205 put_le32(pb, video_enc->height);
206 put_le32(pb, 0); /* reserved */
207 put_le32(pb, 0); /* reserved */
208 put_le32(pb, 0); /* reserved */
209 put_le32(pb, 0); /* reserved */
210
211 /* stream list */
212 for(i=0;i<n;i++) {
213 list2 = start_tag(pb, "LIST");
214 put_tag(pb, "strl");
215
216 stream = &s->streams[i]->codec;
217
218 /* stream generic header */
219 strh = start_tag(pb, "strh");
220 switch(stream->codec_type) {
221 case CODEC_TYPE_VIDEO:
222 put_tag(pb, "vids");
223 put_le32(pb, codec_get_bmp_tag(stream->codec_id));
224 put_le32(pb, 0); /* flags */
225 put_le16(pb, 0); /* priority */
226 put_le16(pb, 0); /* language */
227 put_le32(pb, 0); /* initial frame */
228 put_le32(pb, 1000); /* scale */
229 put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
230 put_le32(pb, 0); /* start */
86bec9a1 231 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
232 put_le32(pb, nb_frames); /* length, XXX: fill later */
233 put_le32(pb, 1024 * 1024); /* suggested buffer size */
86bec9a1 234 put_le32(pb, -1); /* quality */
de6d9b64
FB
235 put_le32(pb, stream->width * stream->height * 3); /* sample size */
236 put_le16(pb, 0);
237 put_le16(pb, 0);
238 put_le16(pb, stream->width);
239 put_le16(pb, stream->height);
240 break;
241 case CODEC_TYPE_AUDIO:
242 put_tag(pb, "auds");
86bec9a1 243 put_le32(pb, 1); /* tag */
de6d9b64
FB
244 put_le32(pb, 0); /* flags */
245 put_le16(pb, 0); /* priority */
246 put_le16(pb, 0); /* language */
247 put_le32(pb, 0); /* initial frame */
86bec9a1
J
248 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
249 put_le32(pb, au_scale); /* scale */
250 put_le32(pb, au_byterate); /* rate */
de6d9b64 251 put_le32(pb, 0); /* start */
86bec9a1 252 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
253 put_le32(pb, 0); /* length, XXX: filled later */
254 put_le32(pb, 12 * 1024); /* suggested buffer size */
255 put_le32(pb, -1); /* quality */
86bec9a1 256 put_le32(pb, au_ssize); /* sample size */
de6d9b64
FB
257 put_le32(pb, 0);
258 put_le32(pb, 0);
259 break;
53cafac0 260 default:
42343f7e 261 av_abort();
de6d9b64
FB
262 }
263 end_tag(pb, strh);
264
265 strf = start_tag(pb, "strf");
266 switch(stream->codec_type) {
267 case CODEC_TYPE_VIDEO:
502d105f 268 put_bmp_header(pb, stream, codec_bmp_tags);
de6d9b64
FB
269 break;
270 case CODEC_TYPE_AUDIO:
46a3d068 271 if (put_wav_header(pb, stream) < 0) {
1ea4f593 272 av_free(avi);
46a3d068
FB
273 return -1;
274 }
de6d9b64 275 break;
53cafac0 276 default:
42343f7e 277 av_abort();
de6d9b64
FB
278 }
279 end_tag(pb, strf);
280 end_tag(pb, list2);
281 }
282
283 end_tag(pb, list1);
284
285 avi->movi_list = start_tag(pb, "LIST");
286 avi->first = NULL;
287 avi->last = NULL;
288 put_tag(pb, "movi");
289
290 put_flush_packet(pb);
291
292 return 0;
293}
294
295static int avi_write_packet(AVFormatContext *s, int stream_index,
10bb7023 296 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
297{
298 AVIContext *avi = s->priv_data;
299 ByteIOContext *pb = &s->pb;
300 AVIIndex *idx;
301 unsigned char tag[5];
302 unsigned int flags;
303 AVCodecContext *enc;
304
305 enc = &s->streams[stream_index]->codec;
306
307 tag[0] = '0';
308 tag[1] = '0' + stream_index;
309 if (enc->codec_type == CODEC_TYPE_VIDEO) {
310 tag[2] = 'd';
311 tag[3] = 'c';
312 flags = enc->key_frame ? 0x10 : 0x00;
313 } else {
314 tag[2] = 'w';
315 tag[3] = 'b';
316 flags = 0x10;
317 }
86bec9a1
J
318 if (enc->codec_type == CODEC_TYPE_AUDIO)
319 avi->audio_strm_length[stream_index] += size;
de6d9b64
FB
320
321 if (!url_is_streamed(&s->pb)) {
1ea4f593 322 idx = av_malloc(sizeof(AVIIndex));
de6d9b64
FB
323 memcpy(idx->tag, tag, 4);
324 idx->flags = flags;
325 idx->pos = url_ftell(pb) - avi->movi_list;
326 idx->len = size;
327 idx->next = NULL;
328 if (!avi->last)
329 avi->first = idx;
330 else
331 avi->last->next = idx;
332 avi->last = idx;
333 }
334
335 put_buffer(pb, tag, 4);
336 put_le32(pb, size);
337 put_buffer(pb, buf, size);
338 if (size & 1)
339 put_byte(pb, 0);
340
341 put_flush_packet(pb);
342 return 0;
343}
344
345static int avi_write_trailer(AVFormatContext *s)
346{
347 ByteIOContext *pb = &s->pb;
348 AVIContext *avi = s->priv_data;
349 offset_t file_size, idx_chunk;
86bec9a1
J
350 int n, nb_frames, au_byterate, au_ssize, au_scale;
351 AVCodecContext *stream;
de6d9b64
FB
352 AVIIndex *idx;
353
354 if (!url_is_streamed(&s->pb)) {
355 end_tag(pb, avi->movi_list);
356
357 idx_chunk = start_tag(pb, "idx1");
358 idx = avi->first;
359 while (idx != NULL) {
360 put_buffer(pb, idx->tag, 4);
361 put_le32(pb, idx->flags);
362 put_le32(pb, idx->pos);
363 put_le32(pb, idx->len);
364 idx = idx->next;
365 }
366 end_tag(pb, idx_chunk);
367
368 /* update file size */
369 file_size = url_ftell(pb);
370 url_fseek(pb, 4, SEEK_SET);
8be1c656 371 put_le32(pb, (UINT32)(file_size - 8));
86bec9a1
J
372
373 /* Fill in frame/sample counters */
374 nb_frames = 0;
375 for(n=0;n<s->nb_streams;n++) {
376 if (avi->frames_hdr_strm[n] != 0) {
377 stream = &s->streams[n]->codec;
378 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
379 if (stream->codec_type == CODEC_TYPE_VIDEO) {
380 put_le32(pb, stream->frame_number);
381 if (nb_frames < stream->frame_number)
382 nb_frames = stream->frame_number;
383 } else {
5798368b 384 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
86bec9a1
J
385 put_le32(pb, stream->frame_number);
386 nb_frames += stream->frame_number;
387 } else {
388 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
389 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
390 }
391 }
392 }
393 }
394 if (avi->frames_hdr_all != 0) {
395 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
396 put_le32(pb, nb_frames);
397 }
de6d9b64
FB
398 url_fseek(pb, file_size, SEEK_SET);
399 }
400 put_flush_packet(pb);
de6d9b64
FB
401 return 0;
402}
403
c9a65ca8 404static AVOutputFormat avi_oformat = {
de6d9b64
FB
405 "avi",
406 "avi format",
407 "video/x-msvideo",
408 "avi",
c9a65ca8 409 sizeof(AVIContext),
de6d9b64
FB
410 CODEC_ID_MP2,
411 CODEC_ID_MSMPEG4,
412 avi_write_header,
413 avi_write_packet,
414 avi_write_trailer,
de6d9b64 415};
c9a65ca8
FB
416
417int avienc_init(void)
418{
419 av_register_output_format(&avi_oformat);
420 return 0;
421}