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