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