77dccac0a43fbe97d279f0264e1e2942691c56bc
[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 riff_start, movi_list, odml_list;
35 offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
36 int audio_strm_length[MAX_STREAMS];
37 AVIIndex *first, *last;
38 } AVIContext;
39
40 offset_t start_tag(ByteIOContext *pb, const char *tag)
41 {
42 put_tag(pb, tag);
43 put_le32(pb, 0);
44 return url_ftell(pb);
45 }
46
47 void end_tag(ByteIOContext *pb, offset_t start)
48 {
49 offset_t pos;
50
51 pos = url_ftell(pb);
52 url_fseek(pb, start - 4, SEEK_SET);
53 put_le32(pb, (uint32_t)(pos - start));
54 url_fseek(pb, pos, SEEK_SET);
55 }
56
57 /* Note: when encoding, the first matching tag is used, so order is
58 important if multiple tags possible for a given codec. */
59 const CodecTag codec_bmp_tags[] = {
60 { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
61 { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
62 { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
63
64 /* added based on MPlayer */
65 { CODEC_ID_H263I, MKTAG('i', '2', '6', '3') },
66 { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
67 { CODEC_ID_H263P, MKTAG('h', '2', '6', '3') },
68 { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
69
70 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
71 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
72 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), .invalid_asf = 1 },
73 { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
74 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
75 { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), .invalid_asf = 1 },
76 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), .invalid_asf = 1 },
77 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
78 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
79 { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
80 { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
81
82 /* added based on MPlayer */
83 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
84 { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', '1') },
85 { CODEC_ID_MPEG4, MKTAG('X', 'v', 'i', 'D') },
86 { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
87 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
88 { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
89
90 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
91 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '3'), .invalid_asf = 1 },
92 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
93
94 /* added based on MPlayer */
95 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
96 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', 'g', '3') },
97 { CODEC_ID_MSMPEG4V3, MKTAG('m', 'p', '4', '3') },
98 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
99 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '5') },
100 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
101 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '6') },
102 { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
103 { CODEC_ID_MSMPEG4V3, MKTAG('d', 'i', 'v', '4') },
104 { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
105 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
106 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '1') },
107 { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
108 { CODEC_ID_MSMPEG4V3, MKTAG('c', 'o', 'l', '0') },
109
110 { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
111
112 /* added based on MPlayer */
113 { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
114 { CODEC_ID_MSMPEG4V2, MKTAG('d', 'i', 'v', '2') },
115 { CODEC_ID_MSMPEG4V2, MKTAG('m', 'p', '4', '2') },
116
117 { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
118
119 /* added based on MPlayer */
120 { CODEC_ID_MSMPEG4V1, MKTAG('D', 'I', 'V', '4') },
121 { CODEC_ID_MSMPEG4V1, MKTAG('d', 'i', 'v', '4') },
122 { CODEC_ID_MSMPEG4V1, MKTAG('m', 'p', 'g', '4') },
123
124 { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
125
126 /* added based on MPlayer */
127 { CODEC_ID_WMV1, MKTAG('w', 'm', 'v', '1') },
128
129 { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
130 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
131 { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') },
132 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
133 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
134 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
135 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
136 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
137 { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
138 { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
139 { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
140 { CODEC_ID_HUFFYUV, MKTAG('h', 'f', 'y', 'u') },
141 { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
142 { CODEC_ID_CYUV, MKTAG('c', 'y', 'u', 'v') },
143 { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
144 { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
145 { 0, 0 },
146 };
147
148 unsigned int codec_get_tag(const CodecTag *tags, int id)
149 {
150 while (tags->id != 0) {
151 if (tags->id == id)
152 return tags->tag;
153 tags++;
154 }
155 return 0;
156 }
157
158 static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
159 {
160 while (tags->id != 0) {
161 if (!tags->invalid_asf && tags->id == id)
162 return tags->tag;
163 tags++;
164 }
165 return 0;
166 }
167
168 int codec_get_id(const CodecTag *tags, unsigned int tag)
169 {
170 while (tags->id != 0) {
171 if (tags->tag == tag)
172 return tags->id;
173 tags++;
174 }
175 return 0;
176 }
177
178 unsigned int codec_get_bmp_tag(int id)
179 {
180 return codec_get_tag(codec_bmp_tags, id);
181 }
182
183 /* BITMAPINFOHEADER header */
184 void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
185 {
186 put_le32(pb, 40 + enc->extradata_size); /* size */
187 put_le32(pb, enc->width);
188 put_le32(pb, enc->height);
189 put_le16(pb, 1); /* planes */
190
191 put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
192 /* compression type */
193 put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id));
194 put_le32(pb, enc->width * enc->height * 3);
195 put_le32(pb, 0);
196 put_le32(pb, 0);
197 put_le32(pb, 0);
198 put_le32(pb, 0);
199
200 put_buffer(pb, enc->extradata, enc->extradata_size);
201
202 if (enc->extradata_size & 1)
203 put_byte(pb, 0);
204 }
205
206 static void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
207 {
208 switch(stream->codec_id) {
209 case CODEC_ID_PCM_S16LE:
210 *au_scale = *au_ssize = 2*stream->channels;
211 *au_byterate = *au_ssize * stream->sample_rate;
212 break;
213 case CODEC_ID_PCM_U8:
214 case CODEC_ID_PCM_ALAW:
215 case CODEC_ID_PCM_MULAW:
216 *au_scale = *au_ssize = stream->channels;
217 *au_byterate = *au_ssize * stream->sample_rate;
218 break;
219 case CODEC_ID_MP2:
220 *au_ssize = 1;
221 *au_scale = 1;
222 *au_byterate = stream->bit_rate / 8;
223 case CODEC_ID_MP3LAME:
224 *au_ssize = 1;
225 *au_scale = 1;
226 *au_byterate = stream->bit_rate / 8;
227 default:
228 *au_ssize = 1;
229 *au_scale = 1;
230 *au_byterate = stream->bit_rate / 8;
231 break;
232 }
233 }
234
235 static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
236 const char* riff_tag, const char* list_tag)
237 {
238 offset_t loff;
239 avi->riff_start = start_tag(pb, "RIFF");
240 put_tag(pb, riff_tag);
241 loff = start_tag(pb, "LIST");
242 put_tag(pb, list_tag);
243 return loff;
244 }
245
246 static int avi_write_header(AVFormatContext *s)
247 {
248 AVIContext *avi = s->priv_data;
249 ByteIOContext *pb = &s->pb;
250 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
251 AVCodecContext *stream, *video_enc;
252 offset_t list1, list2, strh, strf;
253
254 /* header list */
255 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
256
257 /* avi header */
258 put_tag(pb, "avih");
259 put_le32(pb, 14 * 4);
260 bitrate = 0;
261
262 video_enc = NULL;
263 for(n=0;n<s->nb_streams;n++) {
264 stream = &s->streams[n]->codec;
265 bitrate += stream->bit_rate;
266 if (stream->codec_type == CODEC_TYPE_VIDEO)
267 video_enc = stream;
268 }
269
270 nb_frames = 0;
271
272 if(video_enc){
273 put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->frame_rate_base / video_enc->frame_rate));
274 } else {
275 put_le32(pb, 0);
276 }
277 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
278 put_le32(pb, 0); /* padding */
279 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
280 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
281 put_le32(pb, nb_frames); /* nb frames, filled later */
282 put_le32(pb, 0); /* initial frame */
283 put_le32(pb, s->nb_streams); /* nb streams */
284 put_le32(pb, 1024 * 1024); /* suggested buffer size */
285 if(video_enc){
286 put_le32(pb, video_enc->width);
287 put_le32(pb, video_enc->height);
288 } else {
289 put_le32(pb, 0);
290 put_le32(pb, 0);
291 }
292 put_le32(pb, 0); /* reserved */
293 put_le32(pb, 0); /* reserved */
294 put_le32(pb, 0); /* reserved */
295 put_le32(pb, 0); /* reserved */
296
297 /* stream list */
298 for(i=0;i<n;i++) {
299 list2 = start_tag(pb, "LIST");
300 put_tag(pb, "strl");
301
302 stream = &s->streams[i]->codec;
303
304 /* FourCC should really be set by the codec itself */
305 if (! stream->codec_tag) {
306 stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
307 }
308
309 /* stream generic header */
310 strh = start_tag(pb, "strh");
311 switch(stream->codec_type) {
312 case CODEC_TYPE_VIDEO:
313 put_tag(pb, "vids");
314 put_le32(pb, stream->codec_tag);
315 put_le32(pb, 0); /* flags */
316 put_le16(pb, 0); /* priority */
317 put_le16(pb, 0); /* language */
318 put_le32(pb, 0); /* initial frame */
319
320 put_le32(pb, stream->frame_rate_base); /* scale */
321 put_le32(pb, stream->frame_rate); /* rate */
322
323 put_le32(pb, 0); /* start */
324 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
325 put_le32(pb, nb_frames); /* length, XXX: fill later */
326 put_le32(pb, 1024 * 1024); /* suggested buffer size */
327 put_le32(pb, -1); /* quality */
328 put_le32(pb, stream->width * stream->height * 3); /* sample size */
329 put_le16(pb, 0);
330 put_le16(pb, 0);
331 put_le16(pb, stream->width);
332 put_le16(pb, stream->height);
333 break;
334 case CODEC_TYPE_AUDIO:
335 put_tag(pb, "auds");
336 put_le32(pb, 1); /* tag */
337 put_le32(pb, 0); /* flags */
338 put_le16(pb, 0); /* priority */
339 put_le16(pb, 0); /* language */
340 put_le32(pb, 0); /* initial frame */
341 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
342 put_le32(pb, au_scale); /* scale */
343 put_le32(pb, au_byterate); /* rate */
344 put_le32(pb, 0); /* start */
345 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
346 put_le32(pb, 0); /* length, XXX: filled later */
347 put_le32(pb, 12 * 1024); /* suggested buffer size */
348 put_le32(pb, -1); /* quality */
349 put_le32(pb, au_ssize); /* sample size */
350 put_le32(pb, 0);
351 put_le32(pb, 0);
352 break;
353 default:
354 av_abort();
355 }
356 end_tag(pb, strh);
357
358 strf = start_tag(pb, "strf");
359 switch(stream->codec_type) {
360 case CODEC_TYPE_VIDEO:
361 put_bmp_header(pb, stream, codec_bmp_tags, 0);
362 break;
363 case CODEC_TYPE_AUDIO:
364 if (put_wav_header(pb, stream) < 0) {
365 av_free(avi);
366 return -1;
367 }
368 break;
369 default:
370 av_abort();
371 }
372 end_tag(pb, strf);
373 end_tag(pb, list2);
374 }
375
376 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
377 avi->odml_list = start_tag(pb, "JUNK");
378 put_tag(pb, "odml");
379 put_tag(pb, "dmlh");
380 put_le32(pb, 248);
381 for (i = 0; i < 248; i+= 4)
382 put_le32(pb, 0);
383 end_tag(pb, avi->odml_list);
384
385 end_tag(pb, list1);
386
387 avi->movi_list = start_tag(pb, "LIST");
388 avi->first = NULL;
389 avi->last = NULL;
390 put_tag(pb, "movi");
391
392 put_flush_packet(pb);
393
394 return 0;
395 }
396
397 static int avi_finish_riff1(AVFormatContext *s)
398 {
399 ByteIOContext *pb = &s->pb;
400 AVIContext *avi = s->priv_data;
401 offset_t file_size, idx_chunk;
402 int n, nb_frames, au_byterate, au_ssize, au_scale;
403 AVCodecContext *stream;
404 AVIIndex *idx;
405
406 if (!url_is_streamed(pb)) {
407 end_tag(pb, avi->movi_list);
408
409 idx_chunk = start_tag(pb, "idx1");
410 idx = avi->first;
411 while (idx != NULL) {
412 put_buffer(pb, idx->tag, 4);
413 put_le32(pb, idx->flags);
414 put_le32(pb, idx->pos);
415 put_le32(pb, idx->len);
416 idx = idx->next;
417 }
418 end_tag(pb, idx_chunk);
419
420 /* update file size */
421 file_size = url_ftell(pb);
422 end_tag(pb, avi->riff_start);
423
424 /* Fill in frame/sample counters */
425 nb_frames = 0;
426 for(n=0;n<s->nb_streams;n++) {
427 if (avi->frames_hdr_strm[n] != 0) {
428 stream = &s->streams[n]->codec;
429 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
430 if (stream->codec_type == CODEC_TYPE_VIDEO) {
431 put_le32(pb, stream->frame_number);
432 if (nb_frames < stream->frame_number)
433 nb_frames = stream->frame_number;
434 } else {
435 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
436 put_le32(pb, stream->frame_number);
437 nb_frames += stream->frame_number;
438 } else {
439 parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
440 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
441 }
442 }
443 }
444 }
445 if (avi->frames_hdr_all != 0) {
446 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
447 put_le32(pb, nb_frames);
448 }
449 url_fseek(pb, file_size, SEEK_SET);
450 }
451 return 0;
452 }
453
454 static int avi_write_packet(AVFormatContext *s, int stream_index,
455 uint8_t *buf, int size, int force_pts)
456 {
457 AVIContext *avi = s->priv_data;
458 ByteIOContext *pb = &s->pb;
459 AVIIndex *idx;
460 unsigned char tag[5];
461 unsigned int flags;
462 AVCodecContext *enc;
463
464 if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) {
465 if (avi->riff_start != 8) {
466 end_tag(pb, avi->movi_list);
467 end_tag(pb, avi->riff_start);
468 } else
469 avi_finish_riff1(s);
470
471 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
472 }
473
474 enc = &s->streams[stream_index]->codec;
475
476 tag[0] = '0';
477 tag[1] = '0' + stream_index;
478 if (enc->codec_type == CODEC_TYPE_VIDEO) {
479 tag[2] = 'd';
480 tag[3] = 'c';
481 flags = enc->coded_frame->key_frame ? 0x10 : 0x00;
482 } else {
483 tag[2] = 'w';
484 tag[3] = 'b';
485 flags = 0x10;
486 }
487 if (enc->codec_type == CODEC_TYPE_AUDIO)
488 avi->audio_strm_length[stream_index] += size;
489
490 if (!url_is_streamed(&s->pb)) {
491 idx = av_malloc(sizeof(AVIIndex));
492 memcpy(idx->tag, tag, 4);
493 idx->flags = flags;
494 idx->pos = url_ftell(pb) - avi->movi_list;
495 idx->len = size;
496 idx->next = NULL;
497 if (!avi->last)
498 avi->first = idx;
499 else
500 avi->last->next = idx;
501 avi->last = idx;
502 }
503
504 put_buffer(pb, tag, 4);
505 put_le32(pb, size);
506 put_buffer(pb, buf, size);
507 if (size & 1)
508 put_byte(pb, 0);
509
510 put_flush_packet(pb);
511 return 0;
512 }
513
514 static int avi_write_trailer(AVFormatContext *s)
515 {
516 AVIContext *avi = s->priv_data;
517 ByteIOContext *pb = &s->pb;
518 int res = 0;
519
520 if (avi->riff_start != 8) {
521 int n, nb_frames;
522 offset_t file_size;
523
524 file_size = url_ftell(pb);
525 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
526 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
527 url_fskip(pb, 16);
528
529 for (n=nb_frames=0;n<s->nb_streams;n++) {
530 AVCodecContext *stream = &s->streams[n]->codec;
531 if (stream->codec_type == CODEC_TYPE_VIDEO) {
532 if (nb_frames < stream->frame_number)
533 nb_frames = stream->frame_number;
534 } else {
535 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
536 nb_frames += stream->frame_number;
537 }
538 }
539 }
540 put_le32(pb, nb_frames);
541
542 end_tag(pb, avi->movi_list);
543 end_tag(pb, avi->riff_start);
544 url_fseek(pb, file_size, SEEK_SET);
545 } else
546 res = avi_finish_riff1(s);
547
548 put_flush_packet(pb);
549 return res;
550 }
551
552 static AVOutputFormat avi_oformat = {
553 "avi",
554 "avi format",
555 "video/x-msvideo",
556 "avi",
557 sizeof(AVIContext),
558 CODEC_ID_MP2,
559 CODEC_ID_MSMPEG4V3,
560 avi_write_header,
561 avi_write_packet,
562 avi_write_trailer,
563 };
564
565 int avienc_init(void)
566 {
567 av_register_output_format(&avi_oformat);
568 return 0;
569 }