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