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.
24 * - fill all fields if non streamed (nb_frames for example)
27 typedef struct AVIIndex {
29 unsigned int flags, pos, len;
30 struct AVIIndex *next;
34 offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
35 int audio_strm_length[MAX_STREAMS];
36 AVIIndex *first, *last;
39 offset_t start_tag(ByteIOContext *pb, char *tag)
46 void end_tag(ByteIOContext *pb, offset_t start)
51 url_fseek(pb, start - 4, SEEK_SET);
52 put_le32(pb, (UINT32)(pos - start));
53 url_fseek(pb, pos, SEEK_SET);
56 /* Note: when encoding, the first matching tag is used, so order is
57 important if multiple tags possible for a given codec. */
58 CodecTag codec_bmp_tags[] = {
59 { CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
60 { CODEC_ID_H263P, MKTAG('U', '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') },
64 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x') },
65 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
66 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
67 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
68 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
69 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', '4', '1') },
73 unsigned int codec_get_tag(const CodecTag *tags, int id)
75 while (tags->id != 0) {
83 int codec_get_id(const CodecTag *tags, unsigned int tag)
85 while (tags->id != 0) {
93 unsigned int codec_get_bmp_tag(int id)
95 return codec_get_tag(codec_bmp_tags, id);
98 /* BITMAPINFOHEADER header */
99 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags)
101 put_le32(pb, 40); /* size */
102 put_le32(pb, enc->width);
103 put_le32(pb, enc->height);
104 put_le16(pb, 1); /* planes */
105 put_le16(pb, 24); /* depth */
106 /* compression type */
107 put_le32(pb, codec_get_tag(tags, enc->codec_id));
108 put_le32(pb, enc->width * enc->height * 3);
115 void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
117 switch(stream->codec_id) {
118 case CODEC_ID_PCM_S16LE:
119 *au_scale = *au_ssize = 2*stream->channels;
120 *au_byterate = *au_ssize * stream->sample_rate;
122 case CODEC_ID_PCM_U8:
123 case CODEC_ID_PCM_ALAW:
124 case CODEC_ID_PCM_MULAW:
125 *au_scale = *au_ssize = stream->channels;
126 *au_byterate = *au_ssize * stream->sample_rate;
131 *au_byterate = stream->bit_rate / 8;
132 case CODEC_ID_MP3LAME:
135 *au_byterate = stream->bit_rate / 8;
139 *au_byterate = stream->bit_rate / 8;
144 static int avi_write_header(AVFormatContext *s)
147 ByteIOContext *pb = &s->pb;
148 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
149 AVCodecContext *stream, *video_enc;
150 offset_t list1, list2, strh, strf;
152 avi = malloc(sizeof(AVIContext));
155 memset(avi, 0, sizeof(AVIContext));
159 put_le32(pb, 0); /* file length */
163 list1 = start_tag(pb, "LIST");
168 put_le32(pb, 14 * 4);
172 for(n=0;n<s->nb_streams;n++) {
173 stream = &s->streams[n]->codec;
174 bitrate += stream->bit_rate;
175 if (stream->codec_type == CODEC_TYPE_VIDEO)
185 put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
186 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
187 put_le32(pb, 0); /* padding */
188 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
189 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
190 put_le32(pb, nb_frames); /* nb frames, filled later */
191 put_le32(pb, 0); /* initial frame */
192 put_le32(pb, s->nb_streams); /* nb streams */
193 put_le32(pb, 1024 * 1024); /* suggested buffer size */
194 put_le32(pb, video_enc->width);
195 put_le32(pb, video_enc->height);
196 put_le32(pb, 0); /* reserved */
197 put_le32(pb, 0); /* reserved */
198 put_le32(pb, 0); /* reserved */
199 put_le32(pb, 0); /* reserved */
203 list2 = start_tag(pb, "LIST");
206 stream = &s->streams[i]->codec;
208 /* stream generic header */
209 strh = start_tag(pb, "strh");
210 switch(stream->codec_type) {
211 case CODEC_TYPE_VIDEO:
213 put_le32(pb, codec_get_bmp_tag(stream->codec_id));
214 put_le32(pb, 0); /* flags */
215 put_le16(pb, 0); /* priority */
216 put_le16(pb, 0); /* language */
217 put_le32(pb, 0); /* initial frame */
218 put_le32(pb, 1000); /* scale */
219 put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
220 put_le32(pb, 0); /* start */
221 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
222 put_le32(pb, nb_frames); /* length, XXX: fill later */
223 put_le32(pb, 1024 * 1024); /* suggested buffer size */
224 put_le32(pb, -1); /* quality */
225 put_le32(pb, stream->width * stream->height * 3); /* sample size */
228 put_le16(pb, stream->width);
229 put_le16(pb, stream->height);
231 case CODEC_TYPE_AUDIO:
233 put_le32(pb, 1); /* tag */
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 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
239 put_le32(pb, au_scale); /* scale */
240 put_le32(pb, au_byterate); /* rate */
241 put_le32(pb, 0); /* start */
242 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
243 put_le32(pb, 0); /* length, XXX: filled later */
244 put_le32(pb, 12 * 1024); /* suggested buffer size */
245 put_le32(pb, -1); /* quality */
246 put_le32(pb, au_ssize); /* sample size */
253 strf = start_tag(pb, "strf");
254 switch(stream->codec_type) {
255 case CODEC_TYPE_VIDEO:
256 put_bmp_header(pb, stream, codec_bmp_tags);
258 case CODEC_TYPE_AUDIO:
259 if (put_wav_header(pb, stream) < 0) {
271 avi->movi_list = start_tag(pb, "LIST");
276 put_flush_packet(pb);
281 static int avi_write_packet(AVFormatContext *s, int stream_index,
282 UINT8 *buf, int size, int force_pts)
284 AVIContext *avi = s->priv_data;
285 ByteIOContext *pb = &s->pb;
287 unsigned char tag[5];
291 enc = &s->streams[stream_index]->codec;
294 tag[1] = '0' + stream_index;
295 if (enc->codec_type == CODEC_TYPE_VIDEO) {
298 flags = enc->key_frame ? 0x10 : 0x00;
304 if (enc->codec_type == CODEC_TYPE_AUDIO)
305 avi->audio_strm_length[stream_index] += size;
307 if (!url_is_streamed(&s->pb)) {
308 idx = malloc(sizeof(AVIIndex));
309 memcpy(idx->tag, tag, 4);
311 idx->pos = url_ftell(pb) - avi->movi_list;
317 avi->last->next = idx;
321 put_buffer(pb, tag, 4);
323 put_buffer(pb, buf, size);
327 put_flush_packet(pb);
331 static int avi_write_trailer(AVFormatContext *s)
333 ByteIOContext *pb = &s->pb;
334 AVIContext *avi = s->priv_data;
335 offset_t file_size, idx_chunk;
336 int n, nb_frames, au_byterate, au_ssize, au_scale;
337 AVCodecContext *stream;
340 if (!url_is_streamed(&s->pb)) {
341 end_tag(pb, avi->movi_list);
343 idx_chunk = start_tag(pb, "idx1");
345 while (idx != NULL) {
346 put_buffer(pb, idx->tag, 4);
347 put_le32(pb, idx->flags);
348 put_le32(pb, idx->pos);
349 put_le32(pb, idx->len);
352 end_tag(pb, idx_chunk);
354 /* update file size */
355 file_size = url_ftell(pb);
356 url_fseek(pb, 4, SEEK_SET);
357 put_le32(pb, (UINT32)(file_size - 8));
359 /* Fill in frame/sample counters */
361 for(n=0;n<s->nb_streams;n++) {
362 if (avi->frames_hdr_strm[n] != 0) {
363 stream = &s->streams[n]->codec;
364 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
365 if (stream->codec_type == CODEC_TYPE_VIDEO) {
366 put_le32(pb, stream->frame_number);
367 if (nb_frames < stream->frame_number)
368 nb_frames = stream->frame_number;
370 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
371 put_le32(pb, stream->frame_number);
372 nb_frames += stream->frame_number;
374 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
375 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
380 if (avi->frames_hdr_all != 0) {
381 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
382 put_le32(pb, nb_frames);
384 url_fseek(pb, file_size, SEEK_SET);
386 put_flush_packet(pb);
392 AVFormat avi_format = {