fixing warning
[libav.git] / libav / avienc.c
CommitLineData
de6d9b64
FB
1/*
2 * AVI encoder.
3 * Copyright (c) 2000 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 {
34 offset_t movi_list;
35 AVIIndex *first, *last;
36} AVIContext;
37
38offset_t start_tag(ByteIOContext *pb, char *tag)
39{
40 put_tag(pb, tag);
41 put_le32(pb, 0);
42 return url_ftell(pb);
43}
44
45void end_tag(ByteIOContext *pb, offset_t start)
46{
47 offset_t pos;
48
49 pos = url_ftell(pb);
50 url_fseek(pb, start - 4, SEEK_SET);
8be1c656 51 put_le32(pb, (UINT32)(pos - start));
de6d9b64
FB
52 url_fseek(pb, pos, SEEK_SET);
53}
54
55/* Note: when encoding, the first matching tag is used, so order is
56 important if multiple tags possible for a given codec. */
57CodecTag codec_bmp_tags[] = {
58 { CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
6bbc662f 59 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
de6d9b64
FB
60 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
61 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
58f26ba9
FB
62 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
63 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x') },
64 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
de6d9b64
FB
65 { CODEC_ID_MSMPEG4, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
66 { CODEC_ID_MSMPEG4, MKTAG('M', 'P', '4', '3') },
67 { 0, 0 },
68};
69
de6d9b64
FB
70unsigned int codec_get_tag(CodecTag *tags, int id)
71{
72 while (tags->id != 0) {
73 if (tags->id == id)
74 return tags->tag;
75 tags++;
76 }
77 return 0;
78}
79
80int codec_get_id(CodecTag *tags, unsigned int tag)
81{
82 while (tags->id != 0) {
83 if (tags->tag == tag)
84 return tags->id;
85 tags++;
86 }
87 return 0;
88}
89
90unsigned int codec_get_bmp_tag(int id)
91{
92 return codec_get_tag(codec_bmp_tags, id);
93}
94
95/* BITMAPINFOHEADER header */
96void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc)
97{
98 put_le32(pb, 40); /* size */
99 put_le32(pb, enc->width);
100 put_le32(pb, enc->height);
101 put_le16(pb, 1); /* planes */
102 put_le16(pb, 24); /* depth */
103 /* compression type */
104 put_le32(pb, codec_get_bmp_tag(enc->codec_id));
105 put_le32(pb, enc->width * enc->height * 3);
106 put_le32(pb, 0);
107 put_le32(pb, 0);
108 put_le32(pb, 0);
109 put_le32(pb, 0);
110}
111
de6d9b64
FB
112static int avi_write_header(AVFormatContext *s)
113{
114 AVIContext *avi;
115 ByteIOContext *pb = &s->pb;
116 int bitrate, n, i, nb_frames;
117 AVCodecContext *stream, *video_enc;
118 offset_t list1, list2, strh, strf;
119
120 avi = malloc(sizeof(AVIContext));
121 if (!avi)
122 return -1;
123 memset(avi, 0, sizeof(AVIContext));
124 s->priv_data = avi;
125
126 put_tag(pb, "RIFF");
127 put_le32(pb, 0); /* file length */
128 put_tag(pb, "AVI ");
129
130 /* header list */
131 list1 = start_tag(pb, "LIST");
132 put_tag(pb, "hdrl");
133
134 /* avi header */
135 put_tag(pb, "avih");
136 put_le32(pb, 14 * 4);
137 bitrate = 0;
138
139 video_enc = NULL;
140 for(n=0;n<s->nb_streams;n++) {
141 stream = &s->streams[n]->codec;
142 bitrate += stream->bit_rate;
143 if (stream->codec_type == CODEC_TYPE_VIDEO)
144 video_enc = stream;
145 }
146
147 if (!video_enc) {
148 free(avi);
149 return -1;
150 }
151 nb_frames = 0;
152
8be1c656 153 put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
de6d9b64
FB
154 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
155 put_le32(pb, 0); /* padding */
156 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
157 put_le32(pb, nb_frames); /* nb frames, filled later */
158 put_le32(pb, 0); /* initial frame */
159 put_le32(pb, s->nb_streams); /* nb streams */
160 put_le32(pb, 1024 * 1024); /* suggested buffer size */
161 put_le32(pb, video_enc->width);
162 put_le32(pb, video_enc->height);
163 put_le32(pb, 0); /* reserved */
164 put_le32(pb, 0); /* reserved */
165 put_le32(pb, 0); /* reserved */
166 put_le32(pb, 0); /* reserved */
167
168 /* stream list */
169 for(i=0;i<n;i++) {
170 list2 = start_tag(pb, "LIST");
171 put_tag(pb, "strl");
172
173 stream = &s->streams[i]->codec;
174
175 /* stream generic header */
176 strh = start_tag(pb, "strh");
177 switch(stream->codec_type) {
178 case CODEC_TYPE_VIDEO:
179 put_tag(pb, "vids");
180 put_le32(pb, codec_get_bmp_tag(stream->codec_id));
181 put_le32(pb, 0); /* flags */
182 put_le16(pb, 0); /* priority */
183 put_le16(pb, 0); /* language */
184 put_le32(pb, 0); /* initial frame */
185 put_le32(pb, 1000); /* scale */
186 put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
187 put_le32(pb, 0); /* start */
188 put_le32(pb, nb_frames); /* length, XXX: fill later */
189 put_le32(pb, 1024 * 1024); /* suggested buffer size */
190 put_le32(pb, 10000); /* quality */
191 put_le32(pb, stream->width * stream->height * 3); /* sample size */
192 put_le16(pb, 0);
193 put_le16(pb, 0);
194 put_le16(pb, stream->width);
195 put_le16(pb, stream->height);
196 break;
197 case CODEC_TYPE_AUDIO:
198 put_tag(pb, "auds");
199 put_le32(pb, 0);
200 put_le32(pb, 0); /* flags */
201 put_le16(pb, 0); /* priority */
202 put_le16(pb, 0); /* language */
203 put_le32(pb, 0); /* initial frame */
204 put_le32(pb, 1); /* scale */
205 put_le32(pb, stream->bit_rate / 8); /* rate */
206 put_le32(pb, 0); /* start */
207 put_le32(pb, 0); /* length, XXX: filled later */
208 put_le32(pb, 12 * 1024); /* suggested buffer size */
209 put_le32(pb, -1); /* quality */
210 put_le32(pb, 1); /* sample size */
211 put_le32(pb, 0);
212 put_le32(pb, 0);
213 break;
214 }
215 end_tag(pb, strh);
216
217 strf = start_tag(pb, "strf");
218 switch(stream->codec_type) {
219 case CODEC_TYPE_VIDEO:
220 put_bmp_header(pb, stream);
221 break;
222 case CODEC_TYPE_AUDIO:
46a3d068
FB
223 if (put_wav_header(pb, stream) < 0) {
224 free(avi);
225 return -1;
226 }
de6d9b64
FB
227 break;
228 }
229 end_tag(pb, strf);
230 end_tag(pb, list2);
231 }
232
233 end_tag(pb, list1);
234
235 avi->movi_list = start_tag(pb, "LIST");
236 avi->first = NULL;
237 avi->last = NULL;
238 put_tag(pb, "movi");
239
240 put_flush_packet(pb);
241
242 return 0;
243}
244
245static int avi_write_packet(AVFormatContext *s, int stream_index,
246 UINT8 *buf, int size)
247{
248 AVIContext *avi = s->priv_data;
249 ByteIOContext *pb = &s->pb;
250 AVIIndex *idx;
251 unsigned char tag[5];
252 unsigned int flags;
253 AVCodecContext *enc;
254
255 enc = &s->streams[stream_index]->codec;
256
257 tag[0] = '0';
258 tag[1] = '0' + stream_index;
259 if (enc->codec_type == CODEC_TYPE_VIDEO) {
260 tag[2] = 'd';
261 tag[3] = 'c';
262 flags = enc->key_frame ? 0x10 : 0x00;
263 } else {
264 tag[2] = 'w';
265 tag[3] = 'b';
266 flags = 0x10;
267 }
268
269 if (!url_is_streamed(&s->pb)) {
270 idx = malloc(sizeof(AVIIndex));
271 memcpy(idx->tag, tag, 4);
272 idx->flags = flags;
273 idx->pos = url_ftell(pb) - avi->movi_list;
274 idx->len = size;
275 idx->next = NULL;
276 if (!avi->last)
277 avi->first = idx;
278 else
279 avi->last->next = idx;
280 avi->last = idx;
281 }
282
283 put_buffer(pb, tag, 4);
284 put_le32(pb, size);
285 put_buffer(pb, buf, size);
286 if (size & 1)
287 put_byte(pb, 0);
288
289 put_flush_packet(pb);
290 return 0;
291}
292
293static int avi_write_trailer(AVFormatContext *s)
294{
295 ByteIOContext *pb = &s->pb;
296 AVIContext *avi = s->priv_data;
297 offset_t file_size, idx_chunk;
298 AVIIndex *idx;
299
300 if (!url_is_streamed(&s->pb)) {
301 end_tag(pb, avi->movi_list);
302
303 idx_chunk = start_tag(pb, "idx1");
304 idx = avi->first;
305 while (idx != NULL) {
306 put_buffer(pb, idx->tag, 4);
307 put_le32(pb, idx->flags);
308 put_le32(pb, idx->pos);
309 put_le32(pb, idx->len);
310 idx = idx->next;
311 }
312 end_tag(pb, idx_chunk);
313
314 /* update file size */
315 file_size = url_ftell(pb);
316 url_fseek(pb, 4, SEEK_SET);
8be1c656 317 put_le32(pb, (UINT32)(file_size - 8));
de6d9b64
FB
318 url_fseek(pb, file_size, SEEK_SET);
319 }
320 put_flush_packet(pb);
321
322 free(avi);
323 return 0;
324}
325
326AVFormat avi_format = {
327 "avi",
328 "avi format",
329 "video/x-msvideo",
330 "avi",
331 CODEC_ID_MP2,
332 CODEC_ID_MSMPEG4,
333 avi_write_header,
334 avi_write_packet,
335 avi_write_trailer,
336
337 avi_read_header,
338 avi_read_packet,
339 avi_read_close,
340};