qmin/qmax cleanup
[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
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
0570bf06 39offset_t start_tag(ByteIOContext *pb, const char *tag)
de6d9b64
FB
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);
0c1a9eda 52 put_le32(pb, (uint32_t)(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. */
0570bf06 58const CodecTag 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') },
b1d89f82
MR
63 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
64 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), .invalid_asf = 1 },
65 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
66 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
67 { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), .invalid_asf = 1 },
68 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), .invalid_asf = 1 },
390dffc5
MN
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 */
b1d89f82 73 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
8a555875 74 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '3'), .invalid_asf = 1 },
bb3debab
GV
75 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
76 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
9b030d9d
MN
77 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
78 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
e23d5712 79 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
020fcc94
FB
80 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
81 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
f20dca40 82 { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') },
020fcc94 83 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
1cbc2890
FB
84 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
85 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
86 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
bb15f7fd 87 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
f560dd82
MN
88 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
89 { CODEC_ID_HUFFYUV, MKTAG('h', 'f', 'y', 'u') },
1e711bd6
MM
90 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
91 { CODEC_ID_CYUV, MKTAG('c', 'y', 'u', 'v') },
e8750b00 92 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
de6d9b64
FB
93 { 0, 0 },
94};
95
a266644f 96unsigned int codec_get_tag(const CodecTag *tags, int id)
de6d9b64
FB
97{
98 while (tags->id != 0) {
99 if (tags->id == id)
100 return tags->tag;
101 tags++;
102 }
103 return 0;
104}
105
2727c35e
PG
106static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
107{
108 while (tags->id != 0) {
109 if (!tags->invalid_asf && tags->id == id)
110 return tags->tag;
111 tags++;
112 }
113 return 0;
114}
115
a266644f 116int codec_get_id(const CodecTag *tags, unsigned int tag)
de6d9b64
FB
117{
118 while (tags->id != 0) {
119 if (tags->tag == tag)
120 return tags->id;
121 tags++;
122 }
123 return 0;
124}
125
126unsigned int codec_get_bmp_tag(int id)
127{
128 return codec_get_tag(codec_bmp_tags, id);
129}
130
131/* BITMAPINFOHEADER header */
0570bf06 132void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
de6d9b64 133{
952c69c4 134 put_le32(pb, 40 + enc->extradata_size); /* size */
de6d9b64
FB
135 put_le32(pb, enc->width);
136 put_le32(pb, enc->height);
137 put_le16(pb, 1); /* planes */
952c69c4
MN
138
139 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
de6d9b64 140 /* compression type */
2727c35e 141 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id));
de6d9b64
FB
142 put_le32(pb, enc->width * enc->height * 3);
143 put_le32(pb, 0);
144 put_le32(pb, 0);
145 put_le32(pb, 0);
146 put_le32(pb, 0);
952c69c4
MN
147
148 put_buffer(pb, enc->extradata, enc->extradata_size);
149
150 if (enc->extradata_size & 1)
151 put_byte(pb, 0);
de6d9b64
FB
152}
153
0570bf06 154static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
86bec9a1
J
155{
156 switch(stream->codec_id) {
157 case CODEC_ID_PCM_S16LE:
158 *au_scale = *au_ssize = 2*stream->channels;
159 *au_byterate = *au_ssize * stream->sample_rate;
160 break;
161 case CODEC_ID_PCM_U8:
162 case CODEC_ID_PCM_ALAW:
163 case CODEC_ID_PCM_MULAW:
164 *au_scale = *au_ssize = stream->channels;
165 *au_byterate = *au_ssize * stream->sample_rate;
166 break;
167 case CODEC_ID_MP2:
168 *au_ssize = 1;
169 *au_scale = 1;
170 *au_byterate = stream->bit_rate / 8;
5798368b
J
171 case CODEC_ID_MP3LAME:
172 *au_ssize = 1;
173 *au_scale = 1;
174 *au_byterate = stream->bit_rate / 8;
86bec9a1
J
175 default:
176 *au_ssize = 1;
177 *au_scale = 1;
178 *au_byterate = stream->bit_rate / 8;
179 break;
180 }
181}
182
de6d9b64
FB
183static int avi_write_header(AVFormatContext *s)
184{
c9a65ca8 185 AVIContext *avi = s->priv_data;
de6d9b64 186 ByteIOContext *pb = &s->pb;
86bec9a1 187 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
de6d9b64
FB
188 AVCodecContext *stream, *video_enc;
189 offset_t list1, list2, strh, strf;
190
de6d9b64
FB
191 put_tag(pb, "RIFF");
192 put_le32(pb, 0); /* file length */
193 put_tag(pb, "AVI ");
194
195 /* header list */
196 list1 = start_tag(pb, "LIST");
197 put_tag(pb, "hdrl");
198
199 /* avi header */
200 put_tag(pb, "avih");
201 put_le32(pb, 14 * 4);
202 bitrate = 0;
203
204 video_enc = NULL;
205 for(n=0;n<s->nb_streams;n++) {
206 stream = &s->streams[n]->codec;
207 bitrate += stream->bit_rate;
208 if (stream->codec_type == CODEC_TYPE_VIDEO)
209 video_enc = stream;
210 }
211
850742d7
AR
212/* allowing audio-only AVI file
213
de6d9b64 214 if (!video_enc) {
1ea4f593 215 av_free(avi);
de6d9b64
FB
216 return -1;
217 }
850742d7 218*/
de6d9b64
FB
219 nb_frames = 0;
220
850742d7 221 if(video_enc){
14bea432 222 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
850742d7
AR
223 } else {
224 put_le32(pb, 0);
225 }
de6d9b64
FB
226 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
227 put_le32(pb, 0); /* padding */
228 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
86bec9a1 229 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
230 put_le32(pb, nb_frames); /* nb frames, filled later */
231 put_le32(pb, 0); /* initial frame */
232 put_le32(pb, s->nb_streams); /* nb streams */
233 put_le32(pb, 1024 * 1024); /* suggested buffer size */
850742d7 234 if(video_enc){
de6d9b64
FB
235 put_le32(pb, video_enc->width);
236 put_le32(pb, video_enc->height);
850742d7
AR
237 } else {
238 put_le32(pb, 0);
239 put_le32(pb, 0);
240 }
de6d9b64
FB
241 put_le32(pb, 0); /* reserved */
242 put_le32(pb, 0); /* reserved */
243 put_le32(pb, 0); /* reserved */
244 put_le32(pb, 0); /* reserved */
245
246 /* stream list */
247 for(i=0;i<n;i++) {
248 list2 = start_tag(pb, "LIST");
249 put_tag(pb, "strl");
250
251 stream = &s->streams[i]->codec;
252
e8750b00
FR
253 /* FourCC should really be set by the codec itself */
254 if (! stream->codec_tag) {
255 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
256 }
257
de6d9b64
FB
258 /* stream generic header */
259 strh = start_tag(pb, "strh");
260 switch(stream->codec_type) {
261 case CODEC_TYPE_VIDEO:
262 put_tag(pb, "vids");
e8750b00 263 put_le32(pb, stream->codec_tag);
de6d9b64
FB
264 put_le32(pb, 0); /* flags */
265 put_le16(pb, 0); /* priority */
266 put_le16(pb, 0); /* language */
267 put_le32(pb, 0); /* initial frame */
b559b29b 268
14bea432
MN
269 put_le32(pb, stream->frame_rate_base); /* scale */
270 put_le32(pb, stream->frame_rate); /* rate */
b559b29b 271
de6d9b64 272 put_le32(pb, 0); /* start */
86bec9a1 273 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
274 put_le32(pb, nb_frames); /* length, XXX: fill later */
275 put_le32(pb, 1024 * 1024); /* suggested buffer size */
86bec9a1 276 put_le32(pb, -1); /* quality */
de6d9b64
FB
277 put_le32(pb, stream->width * stream->height * 3); /* sample size */
278 put_le16(pb, 0);
279 put_le16(pb, 0);
280 put_le16(pb, stream->width);
281 put_le16(pb, stream->height);
282 break;
283 case CODEC_TYPE_AUDIO:
284 put_tag(pb, "auds");
86bec9a1 285 put_le32(pb, 1); /* tag */
de6d9b64
FB
286 put_le32(pb, 0); /* flags */
287 put_le16(pb, 0); /* priority */
288 put_le16(pb, 0); /* language */
289 put_le32(pb, 0); /* initial frame */
86bec9a1
J
290 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
291 put_le32(pb, au_scale); /* scale */
292 put_le32(pb, au_byterate); /* rate */
de6d9b64 293 put_le32(pb, 0); /* start */
86bec9a1 294 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
de6d9b64
FB
295 put_le32(pb, 0); /* length, XXX: filled later */
296 put_le32(pb, 12 * 1024); /* suggested buffer size */
297 put_le32(pb, -1); /* quality */
86bec9a1 298 put_le32(pb, au_ssize); /* sample size */
de6d9b64
FB
299 put_le32(pb, 0);
300 put_le32(pb, 0);
301 break;
53cafac0 302 default:
42343f7e 303 av_abort();
de6d9b64
FB
304 }
305 end_tag(pb, strh);
306
307 strf = start_tag(pb, "strf");
308 switch(stream->codec_type) {
309 case CODEC_TYPE_VIDEO:
2727c35e 310 put_bmp_header(pb, stream, codec_bmp_tags, 0);
de6d9b64
FB
311 break;
312 case CODEC_TYPE_AUDIO:
46a3d068 313 if (put_wav_header(pb, stream) < 0) {
1ea4f593 314 av_free(avi);
46a3d068
FB
315 return -1;
316 }
de6d9b64 317 break;
53cafac0 318 default:
42343f7e 319 av_abort();
de6d9b64
FB
320 }
321 end_tag(pb, strf);
322 end_tag(pb, list2);
323 }
324
325 end_tag(pb, list1);
326
327 avi->movi_list = start_tag(pb, "LIST");
328 avi->first = NULL;
329 avi->last = NULL;
330 put_tag(pb, "movi");
331
332 put_flush_packet(pb);
333
334 return 0;
335}
336
337static int avi_write_packet(AVFormatContext *s, int stream_index,
0c1a9eda 338 uint8_t *buf, int size, int force_pts)
de6d9b64
FB
339{
340 AVIContext *avi = s->priv_data;
341 ByteIOContext *pb = &s->pb;
342 AVIIndex *idx;
343 unsigned char tag[5];
344 unsigned int flags;
345 AVCodecContext *enc;
346
347 enc = &s->streams[stream_index]->codec;
348
349 tag[0] = '0';
350 tag[1] = '0' + stream_index;
351 if (enc->codec_type == CODEC_TYPE_VIDEO) {
352 tag[2] = 'd';
353 tag[3] = 'c';
492cd3a9 354 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
de6d9b64
FB
355 } else {
356 tag[2] = 'w';
357 tag[3] = 'b';
358 flags = 0x10;
359 }
86bec9a1
J
360 if (enc->codec_type == CODEC_TYPE_AUDIO)
361 avi->audio_strm_length[stream_index] += size;
de6d9b64
FB
362
363 if (!url_is_streamed(&s->pb)) {
1ea4f593 364 idx = av_malloc(sizeof(AVIIndex));
de6d9b64
FB
365 memcpy(idx->tag, tag, 4);
366 idx->flags = flags;
367 idx->pos = url_ftell(pb) - avi->movi_list;
368 idx->len = size;
369 idx->next = NULL;
370 if (!avi->last)
371 avi->first = idx;
372 else
373 avi->last->next = idx;
374 avi->last = idx;
375 }
376
377 put_buffer(pb, tag, 4);
378 put_le32(pb, size);
379 put_buffer(pb, buf, size);
380 if (size & 1)
381 put_byte(pb, 0);
382
383 put_flush_packet(pb);
384 return 0;
385}
386
387static int avi_write_trailer(AVFormatContext *s)
388{
389 ByteIOContext *pb = &s->pb;
390 AVIContext *avi = s->priv_data;
391 offset_t file_size, idx_chunk;
86bec9a1
J
392 int n, nb_frames, au_byterate, au_ssize, au_scale;
393 AVCodecContext *stream;
de6d9b64
FB
394 AVIIndex *idx;
395
396 if (!url_is_streamed(&s->pb)) {
397 end_tag(pb, avi->movi_list);
398
399 idx_chunk = start_tag(pb, "idx1");
400 idx = avi->first;
401 while (idx != NULL) {
402 put_buffer(pb, idx->tag, 4);
403 put_le32(pb, idx->flags);
404 put_le32(pb, idx->pos);
405 put_le32(pb, idx->len);
406 idx = idx->next;
407 }
408 end_tag(pb, idx_chunk);
409
410 /* update file size */
411 file_size = url_ftell(pb);
412 url_fseek(pb, 4, SEEK_SET);
0c1a9eda 413 put_le32(pb, (uint32_t)(file_size - 8));
86bec9a1
J
414
415 /* Fill in frame/sample counters */
416 nb_frames = 0;
417 for(n=0;n<s->nb_streams;n++) {
418 if (avi->frames_hdr_strm[n] != 0) {
419 stream = &s->streams[n]->codec;
420 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
421 if (stream->codec_type == CODEC_TYPE_VIDEO) {
422 put_le32(pb, stream->frame_number);
423 if (nb_frames < stream->frame_number)
424 nb_frames = stream->frame_number;
425 } else {
5798368b 426 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
86bec9a1
J
427 put_le32(pb, stream->frame_number);
428 nb_frames += stream->frame_number;
429 } else {
430 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
431 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
432 }
433 }
434 }
435 }
436 if (avi->frames_hdr_all != 0) {
437 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
438 put_le32(pb, nb_frames);
439 }
de6d9b64
FB
440 url_fseek(pb, file_size, SEEK_SET);
441 }
442 put_flush_packet(pb);
de6d9b64
FB
443 return 0;
444}
445
c9a65ca8 446static AVOutputFormat avi_oformat = {
de6d9b64
FB
447 "avi",
448 "avi format",
449 "video/x-msvideo",
450 "avi",
c9a65ca8 451 sizeof(AVIContext),
de6d9b64 452 CODEC_ID_MP2,
d7425f59 453 CODEC_ID_MSMPEG4V3,
de6d9b64
FB
454 avi_write_header,
455 avi_write_packet,
456 avi_write_trailer,
de6d9b64 457};
c9a65ca8
FB
458
459int avienc_init(void)
460{
461 av_register_output_format(&avi_oformat);
462 return 0;
463}