3 * Copyright (c) 2000 Gerard Lantau.
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.
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.
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.
21 #include <netinet/in.h>
29 * - fill all fields if non streamed (nb_frames for example)
32 typedef struct AVIIndex {
34 unsigned int flags, pos, len;
35 struct AVIIndex *next;
40 AVIIndex *first, *last;
43 offset_t start_tag(ByteIOContext *pb, char *tag)
50 void end_tag(ByteIOContext *pb, offset_t start)
55 url_fseek(pb, start - 4, SEEK_SET);
56 put_le32(pb, pos - start);
57 url_fseek(pb, pos, SEEK_SET);
60 /* Note: when encoding, the first matching tag is used, so order is
61 important if multiple tags possible for a given codec. */
62 CodecTag codec_bmp_tags[] = {
63 { CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
64 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
65 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
66 { CODEC_ID_OPENDIVX, MKTAG('D', 'I', 'V', 'X') },
67 { CODEC_ID_OPENDIVX, MKTAG('d', 'i', 'v', 'x') },
68 { CODEC_ID_OPENDIVX, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
69 { CODEC_ID_MSMPEG4, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
70 { CODEC_ID_MSMPEG4, MKTAG('M', 'P', '4', '3') },
74 CodecTag codec_wav_tags[] = {
75 { CODEC_ID_MP2, 0x55 },
76 { CODEC_ID_MP2, 0x50 },
77 { CODEC_ID_AC3, 0x2000 },
78 { CODEC_ID_PCM, 0x01 },
83 unsigned int codec_get_tag(CodecTag *tags, int id)
85 while (tags->id != 0) {
93 int codec_get_id(CodecTag *tags, unsigned int tag)
95 while (tags->id != 0) {
103 unsigned int codec_get_bmp_tag(int id)
105 return codec_get_tag(codec_bmp_tags, id);
108 /* BITMAPINFOHEADER header */
109 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc)
111 put_le32(pb, 40); /* size */
112 put_le32(pb, enc->width);
113 put_le32(pb, enc->height);
114 put_le16(pb, 1); /* planes */
115 put_le16(pb, 24); /* depth */
116 /* compression type */
117 put_le32(pb, codec_get_bmp_tag(enc->codec_id));
118 put_le32(pb, enc->width * enc->height * 3);
125 /* WAVEFORMATEX header */
126 void put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
130 tag = codec_get_tag(codec_wav_tags, enc->codec_id);
133 put_le16(pb, enc->channels);
134 put_le32(pb, enc->sample_rate);
135 put_le32(pb, enc->bit_rate / 8);
136 put_le16(pb, 1); /* block align */
137 put_le16(pb, 16); /* bits per sample */
138 put_le16(pb, 0); /* wav_extra_size */
141 static int avi_write_header(AVFormatContext *s)
144 ByteIOContext *pb = &s->pb;
145 int bitrate, n, i, nb_frames;
146 AVCodecContext *stream, *video_enc;
147 offset_t list1, list2, strh, strf;
149 avi = malloc(sizeof(AVIContext));
152 memset(avi, 0, sizeof(AVIContext));
156 put_le32(pb, 0); /* file length */
160 list1 = start_tag(pb, "LIST");
165 put_le32(pb, 14 * 4);
169 for(n=0;n<s->nb_streams;n++) {
170 stream = &s->streams[n]->codec;
171 bitrate += stream->bit_rate;
172 if (stream->codec_type == CODEC_TYPE_VIDEO)
182 put_le32(pb, 1000000LL * FRAME_RATE_BASE / video_enc->frame_rate);
183 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
184 put_le32(pb, 0); /* padding */
185 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
186 put_le32(pb, nb_frames); /* nb frames, filled later */
187 put_le32(pb, 0); /* initial frame */
188 put_le32(pb, s->nb_streams); /* nb streams */
189 put_le32(pb, 1024 * 1024); /* suggested buffer size */
190 put_le32(pb, video_enc->width);
191 put_le32(pb, video_enc->height);
192 put_le32(pb, 0); /* reserved */
193 put_le32(pb, 0); /* reserved */
194 put_le32(pb, 0); /* reserved */
195 put_le32(pb, 0); /* reserved */
199 list2 = start_tag(pb, "LIST");
202 stream = &s->streams[i]->codec;
204 /* stream generic header */
205 strh = start_tag(pb, "strh");
206 switch(stream->codec_type) {
207 case CODEC_TYPE_VIDEO:
209 put_le32(pb, codec_get_bmp_tag(stream->codec_id));
210 put_le32(pb, 0); /* flags */
211 put_le16(pb, 0); /* priority */
212 put_le16(pb, 0); /* language */
213 put_le32(pb, 0); /* initial frame */
214 put_le32(pb, 1000); /* scale */
215 put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
216 put_le32(pb, 0); /* start */
217 put_le32(pb, nb_frames); /* length, XXX: fill later */
218 put_le32(pb, 1024 * 1024); /* suggested buffer size */
219 put_le32(pb, 10000); /* quality */
220 put_le32(pb, stream->width * stream->height * 3); /* sample size */
223 put_le16(pb, stream->width);
224 put_le16(pb, stream->height);
226 case CODEC_TYPE_AUDIO:
229 put_le32(pb, 0); /* flags */
230 put_le16(pb, 0); /* priority */
231 put_le16(pb, 0); /* language */
232 put_le32(pb, 0); /* initial frame */
233 put_le32(pb, 1); /* scale */
234 put_le32(pb, stream->bit_rate / 8); /* rate */
235 put_le32(pb, 0); /* start */
236 put_le32(pb, 0); /* length, XXX: filled later */
237 put_le32(pb, 12 * 1024); /* suggested buffer size */
238 put_le32(pb, -1); /* quality */
239 put_le32(pb, 1); /* sample size */
246 strf = start_tag(pb, "strf");
247 switch(stream->codec_type) {
248 case CODEC_TYPE_VIDEO:
249 put_bmp_header(pb, stream);
251 case CODEC_TYPE_AUDIO:
252 put_wav_header(pb, stream);
261 avi->movi_list = start_tag(pb, "LIST");
266 put_flush_packet(pb);
271 static int avi_write_packet(AVFormatContext *s, int stream_index,
272 UINT8 *buf, int size)
274 AVIContext *avi = s->priv_data;
275 ByteIOContext *pb = &s->pb;
277 unsigned char tag[5];
281 enc = &s->streams[stream_index]->codec;
284 tag[1] = '0' + stream_index;
285 if (enc->codec_type == CODEC_TYPE_VIDEO) {
288 flags = enc->key_frame ? 0x10 : 0x00;
295 if (!url_is_streamed(&s->pb)) {
296 idx = malloc(sizeof(AVIIndex));
297 memcpy(idx->tag, tag, 4);
299 idx->pos = url_ftell(pb) - avi->movi_list;
305 avi->last->next = idx;
309 put_buffer(pb, tag, 4);
311 put_buffer(pb, buf, size);
315 put_flush_packet(pb);
319 static int avi_write_trailer(AVFormatContext *s)
321 ByteIOContext *pb = &s->pb;
322 AVIContext *avi = s->priv_data;
323 offset_t file_size, idx_chunk;
326 if (!url_is_streamed(&s->pb)) {
327 end_tag(pb, avi->movi_list);
329 idx_chunk = start_tag(pb, "idx1");
331 while (idx != NULL) {
332 put_buffer(pb, idx->tag, 4);
333 put_le32(pb, idx->flags);
334 put_le32(pb, idx->pos);
335 put_le32(pb, idx->len);
338 end_tag(pb, idx_chunk);
340 /* update file size */
341 file_size = url_ftell(pb);
342 url_fseek(pb, 4, SEEK_SET);
343 put_le32(pb, file_size);
344 url_fseek(pb, file_size, SEEK_SET);
346 put_flush_packet(pb);
352 AVFormat avi_format = {