]> git.sesse.net Git - ffmpeg/blob - libav/avienc.c
* patch by Philip Gladstone
[ffmpeg] / libav / avienc.c
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  */
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, 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 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_MSMPEG4, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
67     { CODEC_ID_MSMPEG4, MKTAG('M', 'P', '4', '3') }, 
68     { 0, 0 },
69 };
70
71 unsigned int codec_get_tag(const CodecTag *tags, int id)
72 {
73     while (tags->id != 0) {
74         if (tags->id == id)
75             return tags->tag;
76         tags++;
77     }
78     return 0;
79 }
80
81 int codec_get_id(const CodecTag *tags, unsigned int tag)
82 {
83     while (tags->id != 0) {
84         if (tags->tag == tag)
85             return tags->id;
86         tags++;
87     }
88     return 0;
89 }
90
91 unsigned int codec_get_bmp_tag(int id)
92 {
93     return codec_get_tag(codec_bmp_tags, id);
94 }
95
96 /* BITMAPINFOHEADER header */
97 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags)
98 {
99     put_le32(pb, 40); /* size */
100     put_le32(pb, enc->width);
101     put_le32(pb, enc->height);
102     put_le16(pb, 1); /* planes */
103     put_le16(pb, 24); /* depth */
104     /* compression type */
105     put_le32(pb, codec_get_tag(tags, enc->codec_id));
106     put_le32(pb, enc->width * enc->height * 3);
107     put_le32(pb, 0);
108     put_le32(pb, 0);
109     put_le32(pb, 0);
110     put_le32(pb, 0);
111 }
112
113 void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
114 {
115     switch(stream->codec_id) {
116     case CODEC_ID_PCM_S16LE:
117        *au_scale = *au_ssize = 2*stream->channels;
118        *au_byterate = *au_ssize * stream->sample_rate;
119         break;
120     case CODEC_ID_PCM_U8:
121     case CODEC_ID_PCM_ALAW:
122     case CODEC_ID_PCM_MULAW:
123         *au_scale = *au_ssize = stream->channels;
124         *au_byterate = *au_ssize * stream->sample_rate;
125         break;
126     case CODEC_ID_MP2:
127         *au_ssize = 1;
128         *au_scale = 1;
129         *au_byterate = stream->bit_rate / 8;
130     case CODEC_ID_MP3LAME:
131         *au_ssize = 1;
132         *au_scale = 1;
133         *au_byterate = stream->bit_rate / 8;    
134     default:
135         *au_ssize = 1;
136         *au_scale = 1; 
137         *au_byterate = stream->bit_rate / 8;
138         break;
139     }
140 }
141
142 static int avi_write_header(AVFormatContext *s)
143 {
144     AVIContext *avi;
145     ByteIOContext *pb = &s->pb;
146     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
147     AVCodecContext *stream, *video_enc;
148     offset_t list1, list2, strh, strf;
149
150     avi = malloc(sizeof(AVIContext));
151     if (!avi)
152         return -1;
153     memset(avi, 0, sizeof(AVIContext));
154     s->priv_data = avi;
155
156     put_tag(pb, "RIFF");
157     put_le32(pb, 0); /* file length */
158     put_tag(pb, "AVI ");
159
160     /* header list */
161     list1 = start_tag(pb, "LIST");
162     put_tag(pb, "hdrl");
163
164     /* avi header */
165     put_tag(pb, "avih");
166     put_le32(pb, 14 * 4);
167     bitrate = 0;
168
169     video_enc = NULL;
170     for(n=0;n<s->nb_streams;n++) {
171         stream = &s->streams[n]->codec;
172         bitrate += stream->bit_rate;
173         if (stream->codec_type == CODEC_TYPE_VIDEO)
174             video_enc = stream;
175     }
176     
177     if (!video_enc) {
178         free(avi);
179         return -1;
180     }
181     nb_frames = 0;
182
183     put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
184     put_le32(pb, bitrate / 8); /* XXX: not quite exact */
185     put_le32(pb, 0); /* padding */
186     put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
187     avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
188     put_le32(pb, nb_frames); /* nb frames, filled later */
189     put_le32(pb, 0); /* initial frame */
190     put_le32(pb, s->nb_streams); /* nb streams */
191     put_le32(pb, 1024 * 1024); /* suggested buffer size */
192     put_le32(pb, video_enc->width);
193     put_le32(pb, video_enc->height);
194     put_le32(pb, 0); /* reserved */
195     put_le32(pb, 0); /* reserved */
196     put_le32(pb, 0); /* reserved */
197     put_le32(pb, 0); /* reserved */
198     
199     /* stream list */
200     for(i=0;i<n;i++) {
201         list2 = start_tag(pb, "LIST");
202         put_tag(pb, "strl");
203     
204         stream = &s->streams[i]->codec;
205
206         /* stream generic header */
207         strh = start_tag(pb, "strh");
208         switch(stream->codec_type) {
209         case CODEC_TYPE_VIDEO:
210             put_tag(pb, "vids");
211             put_le32(pb, codec_get_bmp_tag(stream->codec_id));
212             put_le32(pb, 0); /* flags */
213             put_le16(pb, 0); /* priority */
214             put_le16(pb, 0); /* language */
215             put_le32(pb, 0); /* initial frame */
216             put_le32(pb, 1000); /* scale */
217             put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
218             put_le32(pb, 0); /* start */
219             avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
220             put_le32(pb, nb_frames); /* length, XXX: fill later */
221             put_le32(pb, 1024 * 1024); /* suggested buffer size */
222             put_le32(pb, -1); /* quality */
223             put_le32(pb, stream->width * stream->height * 3); /* sample size */
224             put_le16(pb, 0);
225             put_le16(pb, 0);
226             put_le16(pb, stream->width);
227             put_le16(pb, stream->height);
228             break;
229         case CODEC_TYPE_AUDIO:
230             put_tag(pb, "auds");
231             put_le32(pb, 1); /* tag */
232             put_le32(pb, 0); /* flags */
233             put_le16(pb, 0); /* priority */
234             put_le16(pb, 0); /* language */
235             put_le32(pb, 0); /* initial frame */
236             parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
237             put_le32(pb, au_scale); /* scale */
238             put_le32(pb, au_byterate); /* rate */
239             put_le32(pb, 0); /* start */
240             avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
241             put_le32(pb, 0); /* length, XXX: filled later */
242             put_le32(pb, 12 * 1024); /* suggested buffer size */
243             put_le32(pb, -1); /* quality */
244             put_le32(pb, au_ssize); /* sample size */
245             put_le32(pb, 0);
246             put_le32(pb, 0);
247             break;
248         }
249         end_tag(pb, strh);
250
251         strf = start_tag(pb, "strf");
252         switch(stream->codec_type) {
253         case CODEC_TYPE_VIDEO:
254             put_bmp_header(pb, stream, codec_bmp_tags);
255             break;
256         case CODEC_TYPE_AUDIO:
257             if (put_wav_header(pb, stream) < 0) {
258                 free(avi);
259                 return -1;
260             }
261             break;
262         }
263         end_tag(pb, strf);
264         end_tag(pb, list2);
265     }
266
267     end_tag(pb, list1);
268     
269     avi->movi_list = start_tag(pb, "LIST");
270     avi->first = NULL;
271     avi->last = NULL;
272     put_tag(pb, "movi");
273
274     put_flush_packet(pb);
275
276     return 0;
277 }
278
279 static int avi_write_packet(AVFormatContext *s, int stream_index,
280                             UINT8 *buf, int size, int force_pts)
281 {
282     AVIContext *avi = s->priv_data;
283     ByteIOContext *pb = &s->pb;
284     AVIIndex *idx;
285     unsigned char tag[5];
286     unsigned int flags;
287     AVCodecContext *enc;
288     
289     enc = &s->streams[stream_index]->codec;
290
291     tag[0] = '0';
292     tag[1] = '0' + stream_index;
293     if (enc->codec_type == CODEC_TYPE_VIDEO) {
294         tag[2] = 'd';
295         tag[3] = 'c';
296         flags = enc->key_frame ? 0x10 : 0x00;
297     } else {
298         tag[2] = 'w';
299         tag[3] = 'b';
300         flags = 0x10;
301     }
302     if (enc->codec_type == CODEC_TYPE_AUDIO) 
303        avi->audio_strm_length[stream_index] += size;
304
305     if (!url_is_streamed(&s->pb)) {
306         idx = malloc(sizeof(AVIIndex));
307         memcpy(idx->tag, tag, 4);
308         idx->flags = flags;
309         idx->pos = url_ftell(pb) - avi->movi_list;
310         idx->len = size;
311         idx->next = NULL;
312         if (!avi->last)
313             avi->first = idx;
314         else
315             avi->last->next = idx;
316         avi->last = idx;
317     }
318     
319     put_buffer(pb, tag, 4);
320     put_le32(pb, size);
321     put_buffer(pb, buf, size);
322     if (size & 1)
323         put_byte(pb, 0);
324
325     put_flush_packet(pb);
326     return 0;
327 }
328
329 static int avi_write_trailer(AVFormatContext *s)
330 {
331     ByteIOContext *pb = &s->pb;
332     AVIContext *avi = s->priv_data;
333     offset_t file_size, idx_chunk;
334     int n, nb_frames, au_byterate, au_ssize, au_scale;
335     AVCodecContext *stream;
336     AVIIndex *idx;
337
338     if (!url_is_streamed(&s->pb)) {
339         end_tag(pb, avi->movi_list);
340
341         idx_chunk = start_tag(pb, "idx1");
342         idx = avi->first;
343         while (idx != NULL) {
344             put_buffer(pb, idx->tag, 4);
345             put_le32(pb, idx->flags);
346             put_le32(pb, idx->pos);
347             put_le32(pb, idx->len);
348             idx = idx->next;
349         }
350         end_tag(pb, idx_chunk);
351         
352         /* update file size */
353         file_size = url_ftell(pb);
354         url_fseek(pb, 4, SEEK_SET);
355         put_le32(pb, (UINT32)(file_size - 8));
356
357         /* Fill in frame/sample counters */
358         nb_frames = 0;
359         for(n=0;n<s->nb_streams;n++) {
360             if (avi->frames_hdr_strm[n] != 0) {
361                 stream = &s->streams[n]->codec;
362                 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
363                 if (stream->codec_type == CODEC_TYPE_VIDEO) {
364                     put_le32(pb, stream->frame_number); 
365                     if (nb_frames < stream->frame_number)
366                         nb_frames = stream->frame_number;
367                 } else {
368                     if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
369                         put_le32(pb, stream->frame_number);
370                         nb_frames += stream->frame_number;
371                     } else {
372                         parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
373                         put_le32(pb, avi->audio_strm_length[n] / au_ssize);
374                     }
375                 }
376             }
377        }
378        if (avi->frames_hdr_all != 0) {
379            url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
380            put_le32(pb, nb_frames); 
381        }
382         url_fseek(pb, file_size, SEEK_SET);
383     }
384     put_flush_packet(pb);
385
386     free(avi);
387     return 0;
388 }
389
390 AVFormat avi_format = {
391     "avi",
392     "avi format",
393     "video/x-msvideo",
394     "avi",
395     CODEC_ID_MP2,
396     CODEC_ID_MSMPEG4,
397     avi_write_header,
398     avi_write_packet,
399     avi_write_trailer,
400
401     avi_read_header,
402     avi_read_packet,
403     avi_read_close,
404 };