added new ffplay program
[libav.git] / ffmpeg.c
CommitLineData
85f07f22
FB
1/*
2 * FFmpeg main
3 * Copyright (c) 2000,2001 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 */
daf8e955
FB
19#define HAVE_AV_CONFIG_H
20#include "avformat.h"
21
bdc4796f 22#ifndef CONFIG_WIN32
85f07f22
FB
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
85f07f22 26#include <sys/time.h>
85f07f22 27#include <termios.h>
5727b222
FB
28#include <sys/time.h>
29#include <sys/resource.h>
85f07f22 30#include <ctype.h>
bdc4796f 31#endif
85f07f22 32
85f07f22 33
bdc4796f
FB
34#define MAXINT64 INT64_C(0x7fffffffffffffff)
35
85f07f22
FB
36typedef struct {
37 const char *name;
38 int flags;
39#define HAS_ARG 0x0001
40#define OPT_BOOL 0x0002
41#define OPT_EXPERT 0x0004
42#define OPT_STRING 0x0008
43 union {
44 void (*func_arg)();
45 int *int_arg;
46 char **str_arg;
47 } u;
48 const char *help;
49 const char *argname;
50} OptionDef;
51
52/* select an input stream for an output stream */
53typedef struct AVStreamMap {
54 int file_index;
55 int stream_index;
56} AVStreamMap;
57
58extern const OptionDef options[];
59
60void show_help(void);
61
62#define MAX_FILES 20
63
64static AVFormatContext *input_files[MAX_FILES];
65static int nb_input_files = 0;
66
67static AVFormatContext *output_files[MAX_FILES];
68static int nb_output_files = 0;
69
70static AVStreamMap stream_maps[MAX_FILES];
71static int nb_stream_maps;
72
73static AVFormat *file_format;
74static int frame_width = 160;
75static int frame_height = 128;
76static int frame_rate = 25 * FRAME_RATE_BASE;
77static int video_bit_rate = 200000;
78static int video_qscale = 0;
79static int video_disable = 0;
80static int video_codec_id = CODEC_ID_NONE;
81static int same_quality = 0;
cfcf0ffd 82static int do_deinterlace = 0;
85f07f22
FB
83
84static int gop_size = 12;
85static int intra_only = 0;
86static int audio_sample_rate = 44100;
87static int audio_bit_rate = 64000;
88static int audio_disable = 0;
89static int audio_channels = 1;
90static int audio_codec_id = CODEC_ID_NONE;
91
92static INT64 recording_time = 0;
93static int file_overwrite = 0;
94static char *str_title = NULL;
95static char *str_author = NULL;
96static char *str_copyright = NULL;
97static char *str_comment = NULL;
5727b222 98static int do_benchmark = 0;
a0663ba4 99static int do_hex_dump = 0;
85f07f22
FB
100
101typedef struct AVOutputStream {
102 int file_index; /* file index */
103 int index; /* stream index in the output file */
104 int source_index; /* AVInputStream index */
105 AVStream *st; /* stream in the output file */
106 int encoding_needed; /* true if encoding needed for this stream */
107
108 int fifo_packet_rptr; /* read index in the corresponding
109 avinputstream packet fifo */
110 /* video only */
111 AVPicture pict_tmp; /* temporary image for resizing */
112 int video_resample;
113 ImgReSampleContext *img_resample_ctx; /* for image resampling */
114
115 /* audio only */
116 int audio_resample;
117 ReSampleContext *resample; /* for audio resampling */
118 FifoBuffer fifo; /* for compression: one audio fifo per codec */
119} AVOutputStream;
120
121typedef struct AVInputStream {
122 int file_index;
123 int index;
124 AVStream *st;
125 int discard; /* true if stream data should be discarded */
126 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
127 INT64 pts; /* current pts */
128 int frame_number; /* current frame */
129 INT64 sample_index; /* current sample */
130} AVInputStream;
131
132typedef struct AVInputFile {
133 int eof_reached; /* true if eof reached */
134 int ist_index; /* index of first stream in ist_table */
135 int buffer_size; /* current total buffer size */
136 int buffer_size_max; /* buffer size at which we consider we can stop
137 buffering */
138} AVInputFile;
139
bdc4796f
FB
140#ifndef CONFIG_WIN32
141
85f07f22
FB
142/* init terminal so that we can grab keys */
143static struct termios oldtty;
144
145static void term_exit(void)
146{
147 tcsetattr (0, TCSANOW, &oldtty);
148}
149
150static void term_init(void)
151{
152 struct termios tty;
153
154 tcgetattr (0, &tty);
155 oldtty = tty;
156
157 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
158 |INLCR|IGNCR|ICRNL|IXON);
159 tty.c_oflag |= OPOST;
160 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
161 tty.c_cflag &= ~(CSIZE|PARENB);
162 tty.c_cflag |= CS8;
163 tty.c_cc[VMIN] = 1;
164 tty.c_cc[VTIME] = 0;
165
166 tcsetattr (0, TCSANOW, &tty);
167
168 atexit(term_exit);
169}
170
171/* read a key without blocking */
172static int read_key(void)
173{
174 struct timeval tv;
175 int n;
176 unsigned char ch;
177 fd_set rfds;
178
179 FD_ZERO(&rfds);
180 FD_SET(0, &rfds);
181 tv.tv_sec = 0;
182 tv.tv_usec = 0;
183 n = select(1, &rfds, NULL, NULL, &tv);
184 if (n > 0) {
185 if (read(0, &ch, 1) == 1)
186 return ch;
187 }
188 return -1;
189}
190
191#define AUDIO_FIFO_SIZE 8192
192
193/* main loop for grabbing */
194int av_grab(AVFormatContext *s)
195{
1d7840aa
FB
196 UINT8 audio_buf[AUDIO_FIFO_SIZE];
197 UINT8 audio_buf1[AUDIO_FIFO_SIZE];
198 UINT8 audio_out[AUDIO_FIFO_SIZE];
544286b3 199 UINT8 video_buffer[1024*1024];
85f07f22
FB
200 char buf[256];
201 short *samples;
202 URLContext *audio_handle = NULL, *video_handle = NULL;
203 int ret;
204 AVCodecContext *enc, *first_video_enc = NULL;
205 int frame_size, frame_bytes;
206 int use_audio, use_video;
207 int frame_rate, sample_rate, channels;
208 int width, height, frame_number, i, pix_fmt = 0;
209 AVOutputStream *ost_table[s->nb_streams], *ost;
210 UINT8 *picture_in_buf = NULL, *picture_420p = NULL;
211 int audio_fifo_size = 0, picture_size = 0;
212 INT64 time_start;
213
214 /* init output stream info */
215 for(i=0;i<s->nb_streams;i++)
216 ost_table[i] = NULL;
217
218 /* output stream init */
219 for(i=0;i<s->nb_streams;i++) {
220 ost = av_mallocz(sizeof(AVOutputStream));
221 if (!ost)
222 goto fail;
223 ost->index = i;
224 ost->st = s->streams[i];
225 ost_table[i] = ost;
226 }
227
228 use_audio = 0;
229 use_video = 0;
230 frame_rate = 0;
231 sample_rate = 0;
232 frame_size = 0;
233 channels = 1;
234 width = 0;
235 height = 0;
236 frame_number = 0;
237
238 for(i=0;i<s->nb_streams;i++) {
239 AVCodec *codec;
240
241 ost = ost_table[i];
242 enc = &ost->st->codec;
243 codec = avcodec_find_encoder(enc->codec_id);
244 if (!codec) {
245 fprintf(stderr, "Unknown codec\n");
246 return -1;
247 }
248 if (avcodec_open(enc, codec) < 0) {
249 fprintf(stderr, "Incorrect encode parameters\n");
250 return -1;
251 }
252 switch(enc->codec_type) {
253 case CODEC_TYPE_AUDIO:
254 use_audio = 1;
255 if (enc->sample_rate > sample_rate)
256 sample_rate = enc->sample_rate;
257 if (enc->frame_size > frame_size)
258 frame_size = enc->frame_size;
259 if (enc->channels > channels)
260 channels = enc->channels;
261 break;
262 case CODEC_TYPE_VIDEO:
263 if (!first_video_enc)
264 first_video_enc = enc;
265 use_video = 1;
266 if (enc->frame_rate > frame_rate)
267 frame_rate = enc->frame_rate;
268 if (enc->width > width)
269 width = enc->width;
270 if (enc->height > height)
271 height = enc->height;
272 break;
273 }
274 }
275
276 /* audio */
277 samples = NULL;
278 if (use_audio) {
279 snprintf(buf, sizeof(buf), "audio:%d,%d", sample_rate, channels);
280 ret = url_open(&audio_handle, buf, URL_RDONLY);
281 if (ret < 0) {
282 fprintf(stderr, "Could not open audio device: disabling audio capture\n");
283 use_audio = 0;
284 } else {
285 URLFormat f;
286 /* read back exact grab parameters */
287 if (url_getformat(audio_handle, &f) < 0) {
288 fprintf(stderr, "could not read back video grab parameters\n");
289 goto fail;
290 }
291 sample_rate = f.sample_rate;
292 channels = f.channels;
293 audio_fifo_size = ((AUDIO_FIFO_SIZE / 2) / audio_handle->packet_size) *
294 audio_handle->packet_size;
295 fprintf(stderr, "Audio sampling: %d Hz, %s\n",
296 sample_rate, channels == 2 ? "stereo" : "mono");
297 }
298 }
299
300 /* video */
301 if (use_video) {
302 snprintf(buf, sizeof(buf), "video:%d,%d,%f",
303 width, height, (float)frame_rate / FRAME_RATE_BASE);
304
305 ret = url_open(&video_handle, buf, URL_RDONLY);
306 if (ret < 0) {
307 fprintf(stderr,"Could not init video 4 linux capture: disabling video capture\n");
308 use_video = 0;
309 } else {
310 URLFormat f;
311 const char *pix_fmt_str;
312 /* read back exact grab parameters */
313 if (url_getformat(video_handle, &f) < 0) {
314 fprintf(stderr, "could not read back video grab parameters\n");
315 goto fail;
316 }
317 width = f.width;
318 height = f.height;
319 pix_fmt = f.pix_fmt;
320 switch(pix_fmt) {
321 case PIX_FMT_YUV420P:
322 pix_fmt_str = "420P";
323 break;
324 case PIX_FMT_YUV422:
325 pix_fmt_str = "422";
326 break;
327 case PIX_FMT_RGB24:
328 pix_fmt_str = "RGB24";
329 break;
330 case PIX_FMT_BGR24:
331 pix_fmt_str = "BGR24";
332 break;
333 default:
334 pix_fmt_str = "???";
335 break;
336 }
337 picture_size = video_handle->packet_size;
338 picture_in_buf = malloc(picture_size);
339 if (!picture_in_buf)
340 goto fail;
341 /* allocate a temporary picture if not grabbing in 420P format */
342 if (pix_fmt != PIX_FMT_YUV420P) {
343 picture_420p = malloc((width * height * 3) / 2);
344 }
345 fprintf(stderr, "Video sampling: %dx%d, %s format, %0.2f fps\n",
346 width, height, pix_fmt_str, (float)frame_rate / FRAME_RATE_BASE);
347 }
348 }
349
350 if (!use_video && !use_audio) {
351 fprintf(stderr,"Could not open grab devices : exiting\n");
352 exit(1);
353 }
354
355 /* init built in conversion functions */
356 for(i=0;i<s->nb_streams;i++) {
357 ost = ost_table[i];
358 enc = &ost->st->codec;
359 switch(enc->codec_type) {
360 case CODEC_TYPE_AUDIO:
361 ost->audio_resample = 0;
362 if ((enc->channels != channels ||
363 enc->sample_rate != sample_rate)) {
364 ost->audio_resample = 1;
365 ost->resample = audio_resample_init(enc->channels, channels,
366 enc->sample_rate, sample_rate);
367 }
1d7840aa
FB
368 if (fifo_init(&ost->fifo, (2 * audio_fifo_size * enc->sample_rate *
369 enc->channels) / sample_rate))
85f07f22
FB
370 goto fail;
371 break;
372 case CODEC_TYPE_VIDEO:
373 ost->video_resample = 0;
374 if (enc->width != width ||
375 enc->height != height) {
376 UINT8 *buf;
377 ost->video_resample = 1;
378 buf = malloc((enc->width * enc->height * 3) / 2);
379 if (!buf)
380 goto fail;
381 ost->pict_tmp.data[0] = buf;
382 ost->pict_tmp.data[1] = buf + enc->width * height;
383 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (enc->width * height) / 4;
384 ost->pict_tmp.linesize[0] = enc->width;
385 ost->pict_tmp.linesize[1] = enc->width / 2;
386 ost->pict_tmp.linesize[2] = enc->width / 2;
387 ost->img_resample_ctx = img_resample_init(
388 ost->st->codec.width, ost->st->codec.height,
389 width, height);
390 }
391 }
392 }
393
394 fprintf(stderr, "Press [q] to stop encoding\n");
395
396 s->format->write_header(s);
397 time_start = gettime();
398 term_init();
399
400 for(;;) {
401 /* if 'q' pressed, exits */
402 if (read_key() == 'q')
403 break;
404
405 /* read & compress audio frames */
406 if (use_audio) {
407 int ret, nb_samples, nb_samples_out;
408 UINT8 *buftmp;
409
410 for(;;) {
411 ret = url_read(audio_handle, audio_buf, audio_fifo_size);
412 if (ret <= 0)
413 break;
414 /* fill each codec fifo by doing the right sample
415 rate conversion. This is not optimal because we
416 do too much work, but it is easy to do */
417 nb_samples = ret / (channels * 2);
418 for(i=0;i<s->nb_streams;i++) {
419 ost = ost_table[i];
420 enc = &ost->st->codec;
421 if (enc->codec_type == CODEC_TYPE_AUDIO) {
422 /* rate & stereo convertion */
423 if (!ost->audio_resample) {
424 buftmp = audio_buf;
425 nb_samples_out = nb_samples;
426 } else {
427 buftmp = audio_buf1;
428 nb_samples_out = audio_resample(ost->resample,
429 (short *)buftmp, (short *)audio_buf,
430 nb_samples);
431 }
432 fifo_write(&ost->fifo, buftmp, nb_samples_out * enc->channels * 2,
433 &ost->fifo.wptr);
434 }
435 }
436
437 /* compress as many frame as possible with each audio codec */
438 for(i=0;i<s->nb_streams;i++) {
439 ost = ost_table[i];
440 enc = &ost->st->codec;
441 if (enc->codec_type == CODEC_TYPE_AUDIO) {
442 frame_bytes = enc->frame_size * 2 * enc->channels;
443
444 while (fifo_read(&ost->fifo, audio_buf,
445 frame_bytes, &ost->fifo.rptr) == 0) {
446 ret = avcodec_encode_audio(enc,
447 audio_out, sizeof(audio_out),
448 (short *)audio_buf);
449 s->format->write_packet(s, ost->index, audio_out, ret);
450 }
451 }
452 }
453 }
454 }
455
456 if (use_video) {
cfcf0ffd
FB
457 AVPicture *picture1, *picture2, *picture;
458 AVPicture picture_tmp0, picture_tmp1;
85f07f22
FB
459
460 ret = url_read(video_handle, picture_in_buf, picture_size);
461 if (ret < 0)
462 break;
cfcf0ffd
FB
463
464 picture2 = &picture_tmp0;
465 avpicture_fill(picture2, picture_in_buf, pix_fmt, width, height);
466
85f07f22 467 if (pix_fmt != PIX_FMT_YUV420P) {
cfcf0ffd 468 picture = &picture_tmp1;
919f448d
FB
469 avpicture_fill(picture, picture_420p,
470 PIX_FMT_YUV420P, width, height);
cfcf0ffd
FB
471 img_convert(picture, PIX_FMT_YUV420P,
472 picture2, pix_fmt,
473 width, height);
85f07f22 474 } else {
cfcf0ffd 475 picture = picture2;
85f07f22 476 }
85f07f22
FB
477
478 for(i=0;i<s->nb_streams;i++) {
479 ost = ost_table[i];
480 enc = &ost->st->codec;
481 if (enc->codec_type == CODEC_TYPE_VIDEO) {
482 int n1, n2, nb;
483
484 /* feed each codec with its requested frame rate */
485 n1 = ((INT64)frame_number * enc->frame_rate) / frame_rate;
486 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / frame_rate;
487 nb = n2 - n1;
488 if (nb > 0) {
489 /* resize the picture if needed */
490 if (ost->video_resample) {
491 picture1 = &ost->pict_tmp;
492 img_resample(ost->img_resample_ctx,
cfcf0ffd 493 picture1, picture);
85f07f22 494 } else {
cfcf0ffd 495 picture1 = picture;
85f07f22
FB
496 }
497 ret = avcodec_encode_video(enc, video_buffer,
498 sizeof(video_buffer),
499 picture1);
500 s->format->write_packet(s, ost->index, video_buffer, ret);
501 }
502 }
503 }
504 frame_number++;
505 }
506
507 /* write report */
508 {
509 char buf[1024];
510 INT64 total_size;
511 float ti, bitrate;
512 static float last_ti;
513 INT64 ti1;
514
515 total_size = url_ftell(&s->pb);
516 ti1 = gettime() - time_start;
517 /* check elapsed time */
518 if (recording_time && ti1 >= recording_time)
519 break;
520
521 ti = ti1 / 1000000.0;
522 if (ti < 0.1)
523 ti = 0.1;
524 /* dispaly twice per second */
525 if ((ti - last_ti) >= 0.5) {
526 last_ti = ti;
527 bitrate = (int)((total_size * 8) / ti / 1000.0);
528
529 buf[0] = '\0';
530 if (use_video) {
531 sprintf(buf + strlen(buf), "frame=%5d fps=%4.1f q=%2d ",
532 frame_number, (float)frame_number / ti, first_video_enc->quality);
533 }
534
535 sprintf(buf + strlen(buf), "size=%8LdkB time=%0.1f bitrate=%6.1fkbits/s",
536 total_size / 1024, ti, bitrate);
537 fprintf(stderr, "%s \r", buf);
538 fflush(stderr);
539 }
540 }
541 }
542 term_exit();
543
544 for(i=0;i<s->nb_streams;i++) {
545 ost = ost_table[i];
546 enc = &ost->st->codec;
547 avcodec_close(enc);
548 }
549 s->format->write_trailer(s);
550
551 if (audio_handle)
552 url_close(audio_handle);
553
554 if (video_handle)
555 url_close(video_handle);
556
557 /* write report */
558 {
559 float ti, bitrate;
560 INT64 total_size;
561
562 total_size = url_ftell(&s->pb);
563
564 ti = (gettime() - time_start) / 1000000.0;
565 if (ti < 0.1)
566 ti = 0.1;
567 bitrate = (int)((total_size * 8) / ti / 1000.0);
568
569 fprintf(stderr, "\033[K\nTotal time = %0.1f s, %Ld KBytes, %0.1f kbits/s\n",
570 ti, total_size / 1024, bitrate);
571 if (use_video) {
572 fprintf(stderr, "Total frames = %d\n", frame_number);
573 }
574 }
575
576 ret = 0;
577 fail1:
578 if (picture_in_buf)
579 free(picture_in_buf);
580 if (picture_420p)
581 free(picture_420p);
582 for(i=0;i<s->nb_streams;i++) {
583 ost = ost_table[i];
584 if (ost) {
585 if (ost->fifo.buffer)
586 fifo_free(&ost->fifo);
587 if (ost->pict_tmp.data[0])
588 free(ost->pict_tmp.data[0]);
589 if (ost->video_resample)
590 img_resample_close(ost->img_resample_ctx);
591 if (ost->audio_resample)
592 audio_resample_close(ost->resample);
593 free(ost);
594 }
595 }
596 return ret;
597 fail:
598 ret = -ENOMEM;
599 goto fail1;
600}
601
bdc4796f
FB
602#endif /* CONFIG_WIN32 */
603
85f07f22
FB
604int read_ffserver_streams(AVFormatContext *s, const char *filename)
605{
606 int i;
607 AVFormatContext *ic;
608
609 ic = av_open_input_file(filename, FFM_PACKET_SIZE);
610 if (!ic)
611 return -EIO;
612 /* copy stream format */
613 s->nb_streams = ic->nb_streams;
614 for(i=0;i<ic->nb_streams;i++) {
615 AVStream *st;
616 st = av_mallocz(sizeof(AVFormatContext));
617 memcpy(st, ic->streams[i], sizeof(AVStream));
618 s->streams[i] = st;
619 }
620
621 av_close_input_file(ic);
622 return 0;
623}
624
625#define MAX_AUDIO_PACKET_SIZE 16384
626
627static void do_audio_out(AVFormatContext *s,
628 AVOutputStream *ost,
629 AVInputStream *ist,
630 unsigned char *buf, int size)
631{
632 UINT8 *buftmp;
633 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
634 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
635 int size_out, frame_bytes, ret;
636 AVCodecContext *enc;
637
638 enc = &ost->st->codec;
639
640 if (ost->audio_resample) {
641 buftmp = audio_buf;
642 size_out = audio_resample(ost->resample,
643 (short *)buftmp, (short *)buf,
644 size / (ist->st->codec.channels * 2));
645 size_out = size_out * enc->channels * 2;
646 } else {
647 buftmp = buf;
648 size_out = size;
649 }
650
651 /* now encode as many frames as possible */
a0663ba4 652 if (enc->frame_size > 1) {
85f07f22
FB
653 /* output resampled raw samples */
654 fifo_write(&ost->fifo, buftmp, size_out,
655 &ost->fifo.wptr);
656
657 frame_bytes = enc->frame_size * 2 * enc->channels;
658
659 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
660 &ost->fifo.rptr) == 0) {
a0663ba4
FB
661 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
662 (short *)audio_buf);
85f07f22
FB
663 s->format->write_packet(s, ost->index, audio_out, ret);
664 }
665 } else {
a0663ba4
FB
666 /* output a pcm frame */
667 /* XXX: change encoding codec API to avoid this ? */
668 switch(enc->codec->id) {
669 case CODEC_ID_PCM_S16LE:
670 case CODEC_ID_PCM_S16BE:
671 case CODEC_ID_PCM_U16LE:
672 case CODEC_ID_PCM_U16BE:
673 break;
674 default:
675 size_out = size_out >> 1;
676 break;
677 }
678 ret = avcodec_encode_audio(enc, audio_out, size_out,
679 (short *)buftmp);
680 s->format->write_packet(s, ost->index, audio_out, ret);
85f07f22
FB
681 }
682}
683
684/* write a picture to a raw mux */
cfcf0ffd
FB
685static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
686 int pix_fmt, int w, int h)
85f07f22
FB
687{
688 UINT8 *buf, *src, *dest;
689 int size, j, i;
cfcf0ffd
FB
690
691 size = avpicture_get_size(pix_fmt, w, h);
692 buf = malloc(size);
693 if (!buf)
694 return;
695
85f07f22
FB
696 /* XXX: not efficient, should add test if we can take
697 directly the AVPicture */
cfcf0ffd
FB
698 switch(pix_fmt) {
699 case PIX_FMT_YUV420P:
700 dest = buf;
701 for(i=0;i<3;i++) {
702 if (i == 1) {
703 w >>= 1;
704 h >>= 1;
705 }
706 src = picture->data[i];
707 for(j=0;j<h;j++) {
708 memcpy(dest, src, w);
709 dest += w;
710 src += picture->linesize[i];
711 }
712 }
713 break;
714 case PIX_FMT_YUV422P:
715 size = (w * h) * 2;
716 buf = malloc(size);
717 dest = buf;
718 for(i=0;i<3;i++) {
719 if (i == 1) {
720 w >>= 1;
721 }
722 src = picture->data[i];
723 for(j=0;j<h;j++) {
724 memcpy(dest, src, w);
725 dest += w;
726 src += picture->linesize[i];
727 }
728 }
729 break;
730 case PIX_FMT_YUV444P:
731 size = (w * h) * 3;
732 buf = malloc(size);
733 dest = buf;
734 for(i=0;i<3;i++) {
735 src = picture->data[i];
736 for(j=0;j<h;j++) {
737 memcpy(dest, src, w);
738 dest += w;
739 src += picture->linesize[i];
740 }
741 }
742 break;
743 case PIX_FMT_YUV422:
744 size = (w * h) * 2;
745 buf = malloc(size);
746 dest = buf;
747 src = picture->data[0];
748 for(j=0;j<h;j++) {
749 memcpy(dest, src, w * 2);
750 dest += w * 2;
751 src += picture->linesize[0];
85f07f22 752 }
cfcf0ffd
FB
753 break;
754 case PIX_FMT_RGB24:
755 case PIX_FMT_BGR24:
756 size = (w * h) * 3;
757 buf = malloc(size);
758 dest = buf;
759 src = picture->data[0];
85f07f22 760 for(j=0;j<h;j++) {
cfcf0ffd
FB
761 memcpy(dest, src, w * 3);
762 dest += w * 3;
763 src += picture->linesize[0];
85f07f22 764 }
cfcf0ffd
FB
765 break;
766 default:
767 return;
85f07f22
FB
768 }
769 s->format->write_packet(s, index, buf, size);
770 free(buf);
771}
772
773
774static void do_video_out(AVFormatContext *s,
775 AVOutputStream *ost,
776 AVInputStream *ist,
cfcf0ffd 777 AVPicture *picture1)
85f07f22
FB
778{
779 int n1, n2, nb, i, ret, frame_number;
cfcf0ffd
FB
780 AVPicture *picture, *picture2, *pict;
781 AVPicture picture_tmp1, picture_tmp2;
544286b3 782 UINT8 video_buffer[1024*1024];
cfcf0ffd
FB
783 UINT8 *buf = NULL, *buf1 = NULL;
784 AVCodecContext *enc, *dec;
85f07f22
FB
785
786 enc = &ost->st->codec;
cfcf0ffd 787 dec = &ist->st->codec;
85f07f22
FB
788
789 frame_number = ist->frame_number;
790 /* first drop frame if needed */
cfcf0ffd
FB
791 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
792 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
85f07f22
FB
793 nb = n2 - n1;
794 if (nb <= 0)
795 return;
796
cfcf0ffd
FB
797 /* deinterlace : must be done before any resize */
798 if (do_deinterlace) {
799 int size;
800
801 /* create temporary picture */
802 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
803 buf1 = malloc(size);
804 if (!buf1)
805 return;
806
807 picture2 = &picture_tmp2;
808 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
809
810 if (avpicture_deinterlace(picture2, picture1,
811 dec->pix_fmt, dec->width, dec->height) < 0) {
812 /* if error, do not deinterlace */
813 free(buf1);
814 buf1 = NULL;
815 picture2 = picture1;
816 }
817 } else {
818 picture2 = picture1;
819 }
820
821 /* convert pixel format if needed */
822 if (enc->pix_fmt != dec->pix_fmt) {
823 int size;
824
825 /* create temporary picture */
826 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
827 buf = malloc(size);
828 if (!buf)
829 return;
830 pict = &picture_tmp1;
831 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
832
833 if (img_convert(pict, enc->pix_fmt,
834 picture2, dec->pix_fmt,
835 dec->width, dec->height) < 0) {
836 fprintf(stderr, "pixel format conversion not handled\n");
837 goto the_end;
838 }
839 } else {
840 pict = picture2;
841 }
842
843 /* XXX: resampling could be done before raw format convertion in
844 some cases to go faster */
845 /* XXX: only works for YUV420P */
85f07f22
FB
846 if (ost->video_resample) {
847 picture = &ost->pict_tmp;
848 img_resample(ost->img_resample_ctx, picture, pict);
849 } else {
850 picture = pict;
851 }
852
853 /* duplicates frame if needed */
854 /* XXX: pb because no interleaving */
855 for(i=0;i<nb;i++) {
856 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
857 /* handles sameq here. This is not correct because it may
858 not be a global option */
859 if (same_quality) {
cfcf0ffd 860 enc->quality = dec->quality;
85f07f22 861 }
cfcf0ffd 862 ret = avcodec_encode_video(enc,
85f07f22
FB
863 video_buffer, sizeof(video_buffer),
864 picture);
865 s->format->write_packet(s, ost->index, video_buffer, ret);
866 } else {
cfcf0ffd 867 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
85f07f22
FB
868 }
869 }
cfcf0ffd
FB
870 the_end:
871 if (buf)
872 free(buf);
873 if (buf1)
874 free(buf1);
85f07f22
FB
875}
876
85f07f22
FB
877static void hex_dump(UINT8 *buf, int size)
878{
879 int len, i, j, c;
880
881 for(i=0;i<size;i+=16) {
882 len = size - i;
883 if (len > 16)
884 len = 16;
885 printf("%08x ", i);
886 for(j=0;j<16;j++) {
887 if (j < len)
888 printf(" %02x", buf[i+j]);
889 else
890 printf(" ");
891 }
892 printf(" ");
893 for(j=0;j<len;j++) {
894 c = buf[i+j];
895 if (c < ' ' || c > '~')
896 c = '.';
897 printf("%c", c);
898 }
899 printf("\n");
900 }
901}
85f07f22
FB
902
903/*
904 * The following code is the main loop of the file converter
905 */
906static int av_encode(AVFormatContext **output_files,
907 int nb_output_files,
908 AVFormatContext **input_files,
909 int nb_input_files,
910 AVStreamMap *stream_maps, int nb_stream_maps)
911{
bdc4796f 912 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
85f07f22
FB
913 AVFormatContext *is, *os;
914 AVCodecContext *codec, *icodec;
915 AVOutputStream *ost, **ost_table = NULL;
916 AVInputStream *ist, **ist_table = NULL;
917 INT64 min_pts, start_time;
bdc4796f
FB
918 AVInputFile *file_table;
919
920 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
921 if (!file_table)
922 goto fail;
85f07f22
FB
923
924 memset(file_table, 0, sizeof(file_table));
925
926 /* input stream init */
927 j = 0;
928 for(i=0;i<nb_input_files;i++) {
929 is = input_files[i];
930 file_table[i].ist_index = j;
931 j += is->nb_streams;
932 }
933 nb_istreams = j;
934
935 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
936 if (!ist_table)
bdc4796f 937 goto fail;
85f07f22
FB
938
939 for(i=0;i<nb_istreams;i++) {
940 ist = av_mallocz(sizeof(AVInputStream));
941 if (!ist)
942 goto fail;
943 ist_table[i] = ist;
944 }
945 j = 0;
946 for(i=0;i<nb_input_files;i++) {
947 is = input_files[i];
948 for(k=0;k<is->nb_streams;k++) {
949 ist = ist_table[j++];
950 ist->st = is->streams[k];
951 ist->file_index = i;
952 ist->index = k;
953 ist->discard = 1; /* the stream is discarded by default
954 (changed later) */
955 }
956 }
957
958 /* output stream init */
959 nb_ostreams = 0;
960 for(i=0;i<nb_output_files;i++) {
961 os = output_files[i];
962 nb_ostreams += os->nb_streams;
963 }
964 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
965 fprintf(stderr, "Number of stream maps must match number of output streams\n");
966 exit(1);
967 }
968
969 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
970 if (!ost_table)
971 goto fail;
972 for(i=0;i<nb_ostreams;i++) {
973 ost = av_mallocz(sizeof(AVOutputStream));
974 if (!ost)
975 goto fail;
976 ost_table[i] = ost;
977 }
978
979 n = 0;
980 for(k=0;k<nb_output_files;k++) {
981 os = output_files[k];
982 for(i=0;i<os->nb_streams;i++) {
983 int found;
984 ost = ost_table[n++];
985 ost->file_index = k;
986 ost->index = i;
987 ost->st = os->streams[i];
988 if (nb_stream_maps > 0) {
989 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
990 stream_maps[n-1].stream_index;
991 } else {
992 /* get corresponding input stream index : we select the first one with the right type */
993 found = 0;
994 for(j=0;j<nb_istreams;j++) {
995 ist = ist_table[j];
996 if (ist->discard &&
997 ist->st->codec.codec_type == ost->st->codec.codec_type) {
998 ost->source_index = j;
999 found = 1;
1000 }
1001 }
1002
1003 if (!found) {
1004 /* try again and reuse existing stream */
1005 for(j=0;j<nb_istreams;j++) {
1006 ist = ist_table[j];
1007 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
1008 ost->source_index = j;
1009 found = 1;
1010 }
1011 }
1012 if (!found) {
1013 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1014 ost->file_index, ost->index);
1015 exit(1);
1016 }
1017 }
1018 }
1019 ist = ist_table[ost->source_index];
1020 ist->discard = 0;
1021 }
1022 }
1023
1024 /* dump the stream mapping */
1025 fprintf(stderr, "Stream mapping:\n");
1026 for(i=0;i<nb_ostreams;i++) {
1027 ost = ost_table[i];
1028 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1029 ist_table[ost->source_index]->file_index,
1030 ist_table[ost->source_index]->index,
1031 ost->file_index,
1032 ost->index);
1033 }
1034
1035 /* for each output stream, we compute the right encoding parameters */
1036 for(i=0;i<nb_ostreams;i++) {
1037 ost = ost_table[i];
1038 ist = ist_table[ost->source_index];
1039
1040 codec = &ost->st->codec;
1041 icodec = &ist->st->codec;
1042
1043 switch(codec->codec_type) {
1044 case CODEC_TYPE_AUDIO:
1045 /* check if same codec with same parameters. If so, no
1046 reencoding is needed */
1047 if (codec->codec_id == icodec->codec_id &&
1048 codec->bit_rate == icodec->bit_rate &&
1049 codec->sample_rate == icodec->sample_rate &&
1050 codec->channels == icodec->channels) {
1051 /* no reencoding */
1052 } else {
1053 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1054 goto fail;
1055
1056 if (codec->channels == icodec->channels &&
1057 codec->sample_rate == icodec->sample_rate) {
1058 ost->audio_resample = 0;
1059 } else {
1060 ost->audio_resample = 1;
1061 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1062 codec->sample_rate,
1063 icodec->sample_rate);
1064 }
1065 ist->decoding_needed = 1;
1066 ost->encoding_needed = 1;
1067 }
1068 break;
1069 case CODEC_TYPE_VIDEO:
1070 /* check if same codec with same parameters. If so, no
1071 reencoding is needed */
1072 if (codec->codec_id == icodec->codec_id &&
1073 codec->bit_rate == icodec->bit_rate &&
1074 codec->frame_rate == icodec->frame_rate &&
1075 codec->width == icodec->width &&
1076 codec->height == icodec->height) {
1077 /* no reencoding */
1078 } else {
1079 if (codec->width == icodec->width &&
1080 codec->height == icodec->height) {
1081 ost->video_resample = 0;
1082 } else {
1083 UINT8 *buf;
1084 ost->video_resample = 1;
1085 buf = malloc((codec->width * codec->height * 3) / 2);
1086 if (!buf)
1087 goto fail;
1088 ost->pict_tmp.data[0] = buf;
1089 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1090 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1091 ost->pict_tmp.linesize[0] = codec->width;
1092 ost->pict_tmp.linesize[1] = codec->width / 2;
1093 ost->pict_tmp.linesize[2] = codec->width / 2;
1094
1095 ost->img_resample_ctx = img_resample_init(
1096 ost->st->codec.width, ost->st->codec.height,
1097 ist->st->codec.width, ist->st->codec.height);
1098 }
1099 ost->encoding_needed = 1;
1100 ist->decoding_needed = 1;
1101 }
1102 break;
1103 }
1104 }
1105
1106 /* open each encoder */
1107 for(i=0;i<nb_ostreams;i++) {
1108 ost = ost_table[i];
1109 if (ost->encoding_needed) {
1110 AVCodec *codec;
1111 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1112 if (!codec) {
1113 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1114 ost->file_index, ost->index);
1115 exit(1);
1116 }
1117 if (avcodec_open(&ost->st->codec, codec) < 0) {
1118 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1119 ost->file_index, ost->index);
1120 exit(1);
1121 }
1122 }
1123 }
1124
1125 /* open each decoder */
1126 for(i=0;i<nb_istreams;i++) {
1127 ist = ist_table[i];
1128 if (ist->decoding_needed) {
1129 AVCodec *codec;
1130 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1131 if (!codec) {
1132 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
1133 ist->file_index, ist->index);
1134 exit(1);
1135 }
1136 if (avcodec_open(&ist->st->codec, codec) < 0) {
1137 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1138 ist->file_index, ist->index);
1139 exit(1);
1140 }
1141 }
1142 }
1143
1144 /* init pts */
1145 for(i=0;i<nb_istreams;i++) {
1146 ist = ist_table[i];
1147 ist->pts = 0;
1148 ist->frame_number = 0;
1149 }
1150
1151 /* compute buffer size max (should use a complete heuristic) */
1152 for(i=0;i<nb_input_files;i++) {
1153 file_table[i].buffer_size_max = 2048;
1154 }
1155
1156 /* open files and write file headers */
1157 for(i=0;i<nb_output_files;i++) {
1158 os = output_files[i];
1159 os->format->write_header(os);
1160 }
1161
1162 start_time = gettime();
1163 min_pts = 0;
1164 for(;;) {
1165 int file_index, ist_index;
1166 AVPacket pkt;
1167 UINT8 *ptr;
1168 int len;
1169 UINT8 *data_buf;
1170 int data_size, got_picture;
1171 AVPicture picture;
1172 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1173
1174 /* select the input file with the smallest pts */
1175 redo:
1176 file_index = -1;
bdc4796f 1177 min_pts = MAXINT64;
85f07f22
FB
1178 for(i=0;i<nb_istreams;i++) {
1179 ist = ist_table[i];
1180 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
1181 min_pts = ist->pts;
1182 file_index = ist->file_index;
1183 }
1184 }
1185 /* if none, if is finished */
1186 if (file_index < 0)
1187 break;
1188 /* finish if recording time exhausted */
1189 if (recording_time > 0 && min_pts >= recording_time)
1190 break;
1191 /* read a packet from it and output it in the fifo */
1192
1193 is = input_files[file_index];
1194 if (av_read_packet(is, &pkt) < 0) {
1195 file_table[file_index].eof_reached = 1;
1196 continue;
1197 }
1198 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1199 ist = ist_table[ist_index];
1200 if (ist->discard) {
1201 continue;
1202 }
1203
a0663ba4
FB
1204 if (do_hex_dump) {
1205 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1206 hex_dump(pkt.data, pkt.size);
1207 }
85f07f22
FB
1208
1209 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1210
1211 len = pkt.size;
1212 ptr = pkt.data;
1213 while (len > 0) {
1214
1215 /* decode the packet if needed */
1216 data_buf = NULL; /* fail safe */
1217 data_size = 0;
1218 if (ist->decoding_needed) {
1219 switch(ist->st->codec.codec_type) {
1220 case CODEC_TYPE_AUDIO:
a0663ba4
FB
1221 /* XXX: could avoid copy if PCM 16 bits with same
1222 endianness as CPU */
1223 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1224 ptr, len);
1225 if (ret < 0)
1226 goto fail_decode;
1227 if (data_size == 0) {
1228 /* no audio frame */
1229 ptr += ret;
1230 len -= ret;
1231 continue;
85f07f22 1232 }
a0663ba4 1233 data_buf = (UINT8 *)samples;
85f07f22
FB
1234 break;
1235 case CODEC_TYPE_VIDEO:
1236 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1237 int size;
1238 size = (ist->st->codec.width * ist->st->codec.height);
cfcf0ffd
FB
1239 avpicture_fill(&picture, ptr,
1240 ist->st->codec.pix_fmt,
1241 ist->st->codec.width,
1242 ist->st->codec.height);
85f07f22
FB
1243 ret = len;
1244 } else {
1245 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1246 ret = avcodec_decode_video(&ist->st->codec,
1247 &picture, &got_picture, ptr, len);
1248 if (ret < 0) {
1249 fail_decode:
1250 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1251 ist->file_index, ist->index);
1252 av_free_packet(&pkt);
1253 goto redo;
1254 }
1255 if (!got_picture) {
1256 /* no picture yet */
1257 ptr += ret;
1258 len -= ret;
1259 continue;
1260 }
1261 }
1262 break;
1263 default:
1264 goto fail_decode;
1265 }
1266 } else {
1267 data_buf = ptr;
1268 data_size = len;
1269 ret = len;
1270 }
1271 /* update pts */
1272 switch(ist->st->codec.codec_type) {
1273 case CODEC_TYPE_AUDIO:
1274 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1275 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1276 break;
1277 case CODEC_TYPE_VIDEO:
1278 ist->frame_number++;
1279 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1280 ist->st->codec.frame_rate;
1281 break;
1282 }
1283 ptr += ret;
1284 len -= ret;
1285
1286 /* transcode raw format, encode packets and output them */
1287
1288 for(i=0;i<nb_ostreams;i++) {
1289 ost = ost_table[i];
1290 if (ost->source_index == ist_index) {
1291 os = output_files[ost->file_index];
1292
1293 if (ost->encoding_needed) {
1294 switch(ost->st->codec.codec_type) {
1295 case CODEC_TYPE_AUDIO:
1296 do_audio_out(os, ost, ist, data_buf, data_size);
1297 break;
1298 case CODEC_TYPE_VIDEO:
1299 do_video_out(os, ost, ist, &picture);
1300 break;
1301 }
1302 } else {
1303 /* no reencoding needed : output the packet directly */
1304 os->format->write_packet(os, ost->index, data_buf, data_size);
1305 }
1306 }
1307 }
1308 }
1309 av_free_packet(&pkt);
1310
1311 /* dump report by using the first video and audio streams */
1312 {
1313 char buf[1024];
1314 AVFormatContext *oc;
1315 INT64 total_size, ti;
1316 AVCodecContext *enc;
1317 int frame_number, vid;
1318 double bitrate, ti1;
1319 static INT64 last_time;
1320
1321 if ((min_pts - last_time) >= 500000) {
1322 last_time = min_pts;
1323
1324 oc = output_files[0];
1325
1326 total_size = url_ftell(&oc->pb);
1327
1328 buf[0] = '\0';
bdc4796f 1329 ti = MAXINT64;
85f07f22
FB
1330 vid = 0;
1331 for(i=0;i<nb_ostreams;i++) {
1332 ost = ost_table[i];
1333 enc = &ost->st->codec;
1334 ist = ist_table[ost->source_index];
1335 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1336 frame_number = ist->frame_number;
1337 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1338 frame_number, enc->quality);
1339 vid = 1;
1340 }
1341 /* compute min pts value */
1342 if (!ist->discard && ist->pts < ti) {
1343 ti = ist->pts;
1344 }
1345 }
1346
bdc4796f 1347 ti1 = (double)ti / 1000000.0;
85f07f22
FB
1348 if (ti1 < 0.1)
1349 ti1 = 0.1;
1350 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1351
bdc4796f
FB
1352 sprintf(buf + strlen(buf),
1353 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1354 (double)total_size / 1024, ti1, bitrate);
85f07f22
FB
1355
1356 fprintf(stderr, "%s \r", buf);
1357 fflush(stderr);
1358 }
1359 }
1360 }
1361
1362 /* dump report by using the first video and audio streams */
1363 {
1364 char buf[1024];
1365 AVFormatContext *oc;
1366 INT64 total_size, ti;
1367 AVCodecContext *enc;
1368 int frame_number, vid;
1369 double bitrate, ti1;
1370
1371 oc = output_files[0];
1372
1373 total_size = url_ftell(&oc->pb);
1374
1375 buf[0] = '\0';
bdc4796f 1376 ti = MAXINT64;
85f07f22
FB
1377 vid = 0;
1378 for(i=0;i<nb_ostreams;i++) {
1379 ost = ost_table[i];
1380 enc = &ost->st->codec;
1381 ist = ist_table[ost->source_index];
1382 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1383 frame_number = ist->frame_number;
1384 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1385 frame_number, enc->quality);
1386 vid = 1;
1387 }
1388 /* compute min pts value */
1389 if (!ist->discard && ist->pts < ti) {
1390 ti = ist->pts;
1391 }
1392 }
1393
1394 ti1 = ti / 1000000.0;
1395 if (ti1 < 0.1)
1396 ti1 = 0.1;
1397 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1398
bdc4796f
FB
1399 sprintf(buf + strlen(buf),
1400 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1401 (double)total_size / 1024, ti1, bitrate);
85f07f22
FB
1402
1403 fprintf(stderr, "%s \n", buf);
1404 }
1405 /* close each encoder */
1406 for(i=0;i<nb_ostreams;i++) {
1407 ost = ost_table[i];
1408 if (ost->encoding_needed) {
1409 avcodec_close(&ost->st->codec);
1410 }
1411 }
1412
1413 /* close each decoder */
1414 for(i=0;i<nb_istreams;i++) {
1415 ist = ist_table[i];
1416 if (ist->decoding_needed) {
1417 avcodec_close(&ist->st->codec);
1418 }
1419 }
1420
1421
1422 /* write the trailer if needed and close file */
1423 for(i=0;i<nb_output_files;i++) {
1424 os = output_files[i];
1425 os->format->write_trailer(os);
1426 }
1427 /* finished ! */
1428
1429 ret = 0;
1430 fail1:
bdc4796f
FB
1431 free(file_table);
1432
85f07f22
FB
1433 if (ist_table) {
1434 for(i=0;i<nb_istreams;i++) {
1435 ist = ist_table[i];
1436 if (ist) {
1437 free(ist);
1438 }
1439 }
1440 free(ist_table);
1441 }
1442 if (ost_table) {
1443 for(i=0;i<nb_ostreams;i++) {
1444 ost = ost_table[i];
1445 if (ost) {
1446 if (ost->pict_tmp.data[0])
1447 free(ost->pict_tmp.data[0]);
1448 if (ost->video_resample)
1449 img_resample_close(ost->img_resample_ctx);
1450 if (ost->audio_resample)
1451 audio_resample_close(ost->resample);
1452 free(ost);
1453 }
1454 }
1455 free(ost_table);
1456 }
1457 return ret;
1458 fail:
1459 ret = -ENOMEM;
1460 goto fail1;
1461}
1462
1463#if 0
1464int file_read(const char *filename)
1465{
1466 URLContext *h;
1467 unsigned char buffer[1024];
1468 int len, i;
1469
1470 if (url_open(&h, filename, O_RDONLY) < 0) {
1471 printf("could not open '%s'\n", filename);
1472 return -1;
1473 }
1474 for(;;) {
1475 len = url_read(h, buffer, sizeof(buffer));
1476 if (len <= 0)
1477 break;
1478 for(i=0;i<len;i++) putchar(buffer[i]);
1479 }
1480 url_close(h);
1481 return 0;
1482}
1483#endif
1484
1485void show_licence(void)
1486{
1487 printf(
1488 "ffmpeg version " FFMPEG_VERSION "\n"
1489 "Copyright (c) 2000,2001 Gerard Lantau\n"
1490 "This program is free software; you can redistribute it and/or modify\n"
1491 "it under the terms of the GNU General Public License as published by\n"
1492 "the Free Software Foundation; either version 2 of the License, or\n"
1493 "(at your option) any later version.\n"
1494 "\n"
1495 "This program is distributed in the hope that it will be useful,\n"
1496 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1497 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1498 "GNU General Public License for more details.\n"
1499 "\n"
1500 "You should have received a copy of the GNU General Public License\n"
1501 "along with this program; if not, write to the Free Software\n"
1502 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1503 );
1504 exit(1);
1505}
1506
1507void opt_format(const char *arg)
1508{
1509 AVFormat *f;
1510 f = first_format;
1511 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1512 if (f == NULL) {
1513 fprintf(stderr, "Invalid format: %s\n", arg);
1514 exit(1);
1515 }
1516 file_format = f;
1517}
1518
1519void opt_video_bitrate(const char *arg)
1520{
1521 video_bit_rate = atoi(arg) * 1000;
1522}
1523
1524void opt_frame_rate(const char *arg)
1525{
1526 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1527}
1528
1529void opt_frame_size(const char *arg)
1530{
1531 parse_image_size(&frame_width, &frame_height, arg);
1532 if (frame_width <= 0 || frame_height <= 0) {
1533 fprintf(stderr, "Incorrect frame size\n");
1534 exit(1);
1535 }
1536 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1537 fprintf(stderr, "Frame size must be a multiple of 2\n");
1538 exit(1);
1539 }
1540}
1541
1542void opt_gop_size(const char *arg)
1543{
1544 gop_size = atoi(arg);
1545}
1546
1547void opt_qscale(const char *arg)
1548{
1549 video_qscale = atoi(arg);
1550 if (video_qscale < 0 ||
1551 video_qscale > 31) {
1552 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1553 exit(1);
1554 }
1555}
1556
1557
1558void opt_audio_bitrate(const char *arg)
1559{
1560 audio_bit_rate = atoi(arg) * 1000;
1561}
1562
1563void opt_audio_rate(const char *arg)
1564{
1565 audio_sample_rate = atoi(arg);
1566}
1567
1568void opt_audio_channels(const char *arg)
1569{
1570 audio_channels = atoi(arg);
1571}
1572
bdc4796f 1573#ifdef CONFIG_GRAB
85f07f22
FB
1574void opt_video_device(const char *arg)
1575{
1576 v4l_device = strdup(arg);
1577}
1578
1579void opt_audio_device(const char *arg)
1580{
1581 audio_device = strdup(arg);
1582}
bdc4796f 1583#endif
85f07f22
FB
1584
1585void opt_audio_codec(const char *arg)
1586{
1587 AVCodec *p;
1588
1589 p = first_avcodec;
1590 while (p) {
1591 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1592 break;
1593 p = p->next;
1594 }
1595 if (p == NULL) {
1596 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1597 exit(1);
1598 } else {
1599 audio_codec_id = p->id;
1600 }
1601}
1602
1603const char *motion_str[] = {
1604 "zero",
1605 "full",
1606 "log",
1607 "phods",
1608 NULL,
1609};
1610
1611void opt_motion_estimation(const char *arg)
1612{
1613 const char **p;
1614 p = motion_str;
1615 for(;;) {
1616 if (!*p) {
1617 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1618 exit(1);
1619 }
1620 if (!strcmp(*p, arg))
1621 break;
1622 p++;
1623 }
1624 motion_estimation_method = p - motion_str;
1625}
1626
1627void opt_video_codec(const char *arg)
1628{
1629 AVCodec *p;
1630
1631 p = first_avcodec;
1632 while (p) {
1633 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1634 break;
1635 p = p->next;
1636 }
1637 if (p == NULL) {
1638 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1639 exit(1);
1640 } else {
1641 video_codec_id = p->id;
1642 }
1643}
1644
1645void opt_map(const char *arg)
1646{
1647 AVStreamMap *m;
1648 const char *p;
1649
1650 p = arg;
1651 m = &stream_maps[nb_stream_maps++];
1652
1653 m->file_index = strtol(arg, (char **)&p, 0);
1654 if (*p)
1655 p++;
1656 m->stream_index = strtol(arg, (char **)&p, 0);
1657}
1658
1659void opt_recording_time(const char *arg)
1660{
1661 recording_time = parse_date(arg, 1);
1662}
1663
1664/* return the number of packet read to find the codec parameters */
1665int find_codec_parameters(AVFormatContext *ic)
1666{
1667 int val, i, count, ret, got_picture, size;
1668 AVCodec *codec;
1669 AVCodecContext *enc;
1670 AVStream *st;
1671 AVPacket *pkt;
1672 AVPicture picture;
1673 AVPacketList *pktl, **ppktl;
1674 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1675 UINT8 *ptr;
1676
1677 count = 0;
1678 ppktl = &ic->packet_buffer;
1679 for(;;) {
1680 for(i=0;i<ic->nb_streams;i++) {
1681 enc = &ic->streams[i]->codec;
1682
1683 switch(enc->codec_type) {
1684 case CODEC_TYPE_AUDIO:
1685 val = enc->sample_rate;
1686 break;
1687 case CODEC_TYPE_VIDEO:
1688 val = enc->width;
1689 break;
1690 default:
1691 val = 1;
1692 break;
1693 }
1694 /* if no parameters supplied, then we should read it from
1695 the stream */
1696 if (val == 0)
1697 break;
1698 }
1699 if (i == ic->nb_streams) {
1700 ret = count;
1701 break;
1702 }
1703
1704 if (count == 0) {
1705 /* open each codec */
1706 for(i=0;i<ic->nb_streams;i++) {
1707 st = ic->streams[i];
1708 codec = avcodec_find_decoder(st->codec.codec_id);
1709 if (codec == NULL) {
1710 ret = -1;
1711 goto the_end;
1712 }
1713 avcodec_open(&st->codec, codec);
1714 }
1715 }
1716 pktl = av_mallocz(sizeof(AVPacketList));
1717 if (!pktl) {
1718 ret = -1;
1719 break;
1720 }
1721
1722 /* add the packet in the buffered packet list */
1723 *ppktl = pktl;
1724 ppktl = &pktl->next;
1725
1726 pkt = &pktl->pkt;
1727 if (ic->format->read_packet(ic, pkt) < 0) {
1728 ret = -1;
1729 break;
1730 }
1731 st = ic->streams[pkt->stream_index];
1732
1733 /* decode the data and update codec parameters */
1734 ptr = pkt->data;
1735 size = pkt->size;
1736 while (size > 0) {
1737 switch(st->codec.codec_type) {
1738 case CODEC_TYPE_VIDEO:
1739 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1740 break;
1741 case CODEC_TYPE_AUDIO:
1742 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1743 break;
1744 default:
1745 ret = -1;
1746 break;
1747 }
1748 if (ret < 0) {
1749 ret = -1;
1750 goto the_end;
1751 }
1752 if (got_picture)
1753 break;
1754 ptr += ret;
1755 size -= ret;
1756 }
1757
1758 count++;
1759 }
1760 the_end:
1761 if (count > 0) {
1762 /* close each codec */
1763 for(i=0;i<ic->nb_streams;i++) {
1764 st = ic->streams[i];
1765 avcodec_close(&st->codec);
1766 }
1767 }
1768 return ret;
1769}
1770
919f448d
FB
1771int filename_number_test(const char *filename)
1772{
1773 char buf[1024];
1774
1775 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1776 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1777 "Use '%%d' to specify the image number:\n"
1778 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1779 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1780 filename);
1781 return -1;
1782 } else {
1783 return 0;
1784 }
1785}
85f07f22
FB
1786
1787void opt_input_file(const char *filename)
1788{
1789 AVFormatContext *ic;
1790 AVFormatParameters params, *ap = &params;
1791 URLFormat url_format;
1792 AVFormat *fmt;
1793 int err, i, ret;
1794
1795 ic = av_mallocz(sizeof(AVFormatContext));
1796 strcpy(ic->filename, filename);
1797 /* first format guess to know if we must open file */
1798 fmt = file_format;
1799 if (!fmt)
1800 fmt = guess_format(NULL, filename, NULL);
1801
1802 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1803 /* open file */
1804 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1805 fprintf(stderr, "Could not open '%s'\n", filename);
1806 exit(1);
1807 }
1808
1809 /* find format and set default parameters */
1810 fmt = file_format;
1811 err = url_getformat(url_fileno(&ic->pb), &url_format);
1812 if (err >= 0) {
1813 if (!fmt)
1814 fmt = guess_format(url_format.format_name, NULL, NULL);
1815 ap->sample_rate = url_format.sample_rate;
1816 ap->frame_rate = url_format.frame_rate;
1817 ap->channels = url_format.channels;
1818 ap->width = url_format.width;
1819 ap->height = url_format.height;
1820 ap->pix_fmt = url_format.pix_fmt;
1821 } else {
1822 if (!fmt)
1823 fmt = guess_format(NULL, filename, NULL);
1824 memset(ap, 0, sizeof(*ap));
1825 }
1826 } else {
1827 memset(ap, 0, sizeof(*ap));
1828 }
1829
1830 if (!fmt || !fmt->read_header) {
1831 fprintf(stderr, "%s: Unknown file format\n", filename);
1832 exit(1);
1833 }
1834 ic->format = fmt;
1835
1836 /* get default parameters from command line */
1837 if (!ap->sample_rate)
1838 ap->sample_rate = audio_sample_rate;
1839 if (!ap->channels)
1840 ap->channels = audio_channels;
1841
1842 if (!ap->frame_rate)
1843 ap->frame_rate = frame_rate;
1844 if (!ap->width)
1845 ap->width = frame_width;
1846 if (!ap->height)
1847 ap->height = frame_height;
919f448d
FB
1848
1849 /* check filename in case of an image number is expected */
1850 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1851 if (filename_number_test(ic->filename) < 0)
1852 exit(1);
1853 }
85f07f22
FB
1854
1855 err = ic->format->read_header(ic, ap);
1856 if (err < 0) {
1857 fprintf(stderr, "%s: Error while parsing header\n", filename);
1858 exit(1);
1859 }
1860
1861 /* If not enough info for the codecs, we decode the first frames
1862 to get it. (used in mpeg case for example) */
1863 ret = find_codec_parameters(ic);
1864 if (ret < 0) {
1865 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1866 exit(1);
1867 }
1868
1869 /* update the current parameters so that they match the one of the input stream */
1870 for(i=0;i<ic->nb_streams;i++) {
1871 AVCodecContext *enc = &ic->streams[i]->codec;
1872 switch(enc->codec_type) {
1873 case CODEC_TYPE_AUDIO:
1874 audio_channels = enc->channels;
1875 audio_sample_rate = enc->sample_rate;
1876 break;
1877 case CODEC_TYPE_VIDEO:
1878 frame_height = enc->height;
1879 frame_width = enc->width;
1880 frame_rate = enc->frame_rate;
1881 break;
1882 }
1883 }
1884
1885 input_files[nb_input_files] = ic;
1886 /* dump the file content */
1887 dump_format(ic, nb_input_files, filename, 0);
1888 nb_input_files++;
1889 file_format = NULL;
1890}
1891
919f448d
FB
1892void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1893{
1894 int has_video, has_audio, i, j;
1895 AVFormatContext *ic;
1896
1897 has_video = 0;
1898 has_audio = 0;
1899 for(j=0;j<nb_input_files;j++) {
1900 ic = input_files[j];
1901 for(i=0;i<ic->nb_streams;i++) {
1902 AVCodecContext *enc = &ic->streams[i]->codec;
1903 switch(enc->codec_type) {
1904 case CODEC_TYPE_AUDIO:
1905 has_audio = 1;
1906 break;
1907 case CODEC_TYPE_VIDEO:
1908 has_video = 1;
1909 break;
1910 }
1911 }
1912 }
1913 *has_video_ptr = has_video;
1914 *has_audio_ptr = has_audio;
1915}
1916
85f07f22
FB
1917void opt_output_file(const char *filename)
1918{
1919 AVStream *st;
1920 AVFormatContext *oc;
919f448d 1921 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
85f07f22
FB
1922 int codec_id;
1923
1924 if (!strcmp(filename, "-"))
1925 filename = "pipe:";
1926
1927 oc = av_mallocz(sizeof(AVFormatContext));
1928
1929 if (!file_format) {
1930 file_format = guess_format(NULL, filename, NULL);
1931 if (!file_format)
1932 file_format = &mpeg_mux_format;
1933 }
1934
1935 oc->format = file_format;
1936
1937 if (!strcmp(file_format->name, "ffm") &&
1938 strstart(filename, "http:", NULL)) {
1939 /* special case for files sent to ffserver: we get the stream
1940 parameters from ffserver */
1941 if (read_ffserver_streams(oc, filename) < 0) {
1942 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1943 exit(1);
1944 }
1945 } else {
1946 use_video = file_format->video_codec != CODEC_ID_NONE;
1947 use_audio = file_format->audio_codec != CODEC_ID_NONE;
919f448d 1948
e30a2846
FB
1949 /* disable if no corresponding type found and at least one
1950 input file */
1951 if (nb_input_files > 0) {
1952 check_audio_video_inputs(&input_has_video, &input_has_audio);
1953 if (!input_has_video)
1954 use_video = 0;
1955 if (!input_has_audio)
1956 use_audio = 0;
1957 }
919f448d
FB
1958
1959 /* manual disable */
85f07f22
FB
1960 if (audio_disable) {
1961 use_audio = 0;
1962 }
1963 if (video_disable) {
1964 use_video = 0;
1965 }
1966
1967 nb_streams = 0;
1968 if (use_video) {
1969 AVCodecContext *video_enc;
1970
1971 st = av_mallocz(sizeof(AVStream));
1972 if (!st) {
1973 fprintf(stderr, "Could not alloc stream\n");
1974 exit(1);
1975 }
1976 video_enc = &st->codec;
1977
1978 codec_id = file_format->video_codec;
1979 if (video_codec_id != CODEC_ID_NONE)
1980 codec_id = video_codec_id;
1981
1982 video_enc->codec_id = codec_id;
1983 video_enc->codec_type = CODEC_TYPE_VIDEO;
1984
1985 video_enc->bit_rate = video_bit_rate;
1986 video_enc->frame_rate = frame_rate;
1987
1988 video_enc->width = frame_width;
1989 video_enc->height = frame_height;
1990 if (!intra_only)
1991 video_enc->gop_size = gop_size;
1992 else
1993 video_enc->gop_size = 0;
1994 if (video_qscale || same_quality) {
1995 video_enc->flags |= CODEC_FLAG_QSCALE;
1996 video_enc->quality = video_qscale;
1997 }
cfcf0ffd
FB
1998 /* XXX: need to find a way to set codec parameters */
1999 if (oc->format == &ppm_format ||
2000 oc->format == &ppmpipe_format) {
2001 video_enc->pix_fmt = PIX_FMT_RGB24;
2002 }
85f07f22
FB
2003
2004 oc->streams[nb_streams] = st;
2005 nb_streams++;
2006 }
2007
2008 if (use_audio) {
2009 AVCodecContext *audio_enc;
2010
2011 st = av_mallocz(sizeof(AVStream));
2012 if (!st) {
2013 fprintf(stderr, "Could not alloc stream\n");
2014 exit(1);
2015 }
2016 audio_enc = &st->codec;
2017 codec_id = file_format->audio_codec;
2018 if (audio_codec_id != CODEC_ID_NONE)
2019 codec_id = audio_codec_id;
2020 audio_enc->codec_id = codec_id;
2021 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2022
2023 audio_enc->bit_rate = audio_bit_rate;
2024 audio_enc->sample_rate = audio_sample_rate;
2025 audio_enc->channels = audio_channels;
2026 oc->streams[nb_streams] = st;
2027 nb_streams++;
2028 }
2029
2030 oc->nb_streams = nb_streams;
2031
2032 if (!nb_streams) {
919f448d 2033 fprintf(stderr, "No audio or video streams available\n");
85f07f22
FB
2034 exit(1);
2035 }
2036
2037 if (str_title)
2038 nstrcpy(oc->title, sizeof(oc->title), str_title);
2039 if (str_author)
2040 nstrcpy(oc->author, sizeof(oc->author), str_author);
2041 if (str_copyright)
2042 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2043 if (str_comment)
2044 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2045 }
2046
2047 output_files[nb_output_files] = oc;
2048 /* dump the file content */
2049 dump_format(oc, nb_output_files, filename, 1);
2050 nb_output_files++;
2051
2052 strcpy(oc->filename, filename);
919f448d
FB
2053
2054 /* check filename in case of an image number is expected */
2055 if (oc->format->flags & AVFMT_NEEDNUMBER) {
2056 if (filename_number_test(oc->filename) < 0)
2057 exit(1);
2058 }
2059
85f07f22
FB
2060 if (!(oc->format->flags & AVFMT_NOFILE)) {
2061 /* test if it already exists to avoid loosing precious files */
2062 if (!file_overwrite &&
2063 (strchr(filename, ':') == NULL ||
2064 strstart(filename, "file:", NULL))) {
2065 if (url_exist(filename)) {
2066 int c;
2067
2068 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2069 fflush(stdout);
2070 c = getchar();
2071 if (toupper(c) != 'Y') {
2072 fprintf(stderr, "Not overwriting - exiting\n");
2073 exit(1);
2074 }
2075 }
2076 }
2077
2078 /* open the file */
2079 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2080 fprintf(stderr, "Could not open '%s'\n", filename);
2081 exit(1);
2082 }
2083 }
2084
2085 /* reset some options */
2086 file_format = NULL;
2087 audio_disable = 0;
2088 video_disable = 0;
2089 audio_codec_id = CODEC_ID_NONE;
2090 video_codec_id = CODEC_ID_NONE;
2091}
2092
bdc4796f 2093#ifndef CONFIG_WIN32
5727b222
FB
2094INT64 getutime(void)
2095{
2096 struct rusage rusage;
2097
2098 getrusage(RUSAGE_SELF, &rusage);
2099 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2100}
bdc4796f
FB
2101#else
2102INT64 getutime(void)
2103{
2104 return gettime();
2105}
2106#endif
5727b222 2107
85f07f22
FB
2108void show_formats(void)
2109{
2110 AVFormat *f;
2111 URLProtocol *up;
2112 AVCodec *p;
2113 const char **pp;
2114
2115 printf("File formats:\n");
2116 printf(" Encoding:");
2117 for(f = first_format; f != NULL; f = f->next) {
2118 if (f->write_header)
2119 printf(" %s", f->name);
2120 }
2121 printf("\n");
2122 printf(" Decoding:");
2123 for(f = first_format; f != NULL; f = f->next) {
2124 if (f->read_header)
2125 printf(" %s", f->name);
2126 }
2127 printf("\n");
2128
2129 printf("Codecs:\n");
2130 printf(" Encoders:");
2131 for(p = first_avcodec; p != NULL; p = p->next) {
2132 if (p->encode)
2133 printf(" %s", p->name);
2134 }
2135 printf("\n");
2136
2137 printf(" Decoders:");
2138 for(p = first_avcodec; p != NULL; p = p->next) {
2139 if (p->decode)
2140 printf(" %s", p->name);
2141 }
2142 printf("\n");
2143
2144 printf("Supported file protocols:");
2145 for(up = first_protocol; up != NULL; up = up->next)
2146 printf(" %s:", up->name);
2147 printf("\n");
2148
2149 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2150 printf("Motion estimation methods:");
2151 pp = motion_str;
2152 while (*pp) {
2153 printf(" %s", *pp);
2154 if ((pp - motion_str) == ME_ZERO)
2155 printf("(fastest)");
2156 else if ((pp - motion_str) == ME_FULL)
2157 printf("(slowest)");
2158 else if ((pp - motion_str) == ME_LOG)
2159 printf("(default)");
2160 pp++;
2161 }
2162 printf("\n");
2163 exit(1);
2164}
2165
2166void show_help(void)
2167{
2168 const OptionDef *po;
2169 int i, expert;
2170
2171 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantau\n"
2172 "usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2173 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n"
2174 "\n"
2175 "Main options are:\n");
2176 for(i=0;i<2;i++) {
2177 if (i == 1)
2178 printf("\nAdvanced options are:\n");
2179 for(po = options; po->name != NULL; po++) {
2180 char buf[64];
2181 expert = (po->flags & OPT_EXPERT) != 0;
2182 if (expert == i) {
2183 strcpy(buf, po->name);
2184 if (po->flags & HAS_ARG) {
2185 strcat(buf, " ");
2186 strcat(buf, po->argname);
2187 }
2188 printf("-%-17s %s\n", buf, po->help);
2189 }
2190 }
2191 }
2192
2193 exit(1);
2194}
2195
2196const OptionDef options[] = {
bdc4796f
FB
2197 { "L", 0, {(void*)show_licence}, "show license" },
2198 { "h", 0, {(void*)show_help}, "show help" },
2199 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2200 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2201 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2202 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2203 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2204 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2205 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2206 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2207 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2208 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
85f07f22 2209 /* video options */
bdc4796f
FB
2210 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2211 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2212 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2213 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2214 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2215 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2216 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2217#ifdef CONFIG_GRAB
2218 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2219#endif
2220 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2221 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
85f07f22 2222 "method" },
bdc4796f 2223 { "sameq", OPT_BOOL, {(void*)&same_quality},
85f07f22
FB
2224 "use same video quality as source (implies VBR)" },
2225 /* audio options */
bdc4796f
FB
2226 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2227 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2228 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2229 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2230#ifdef CONFIG_GRAB
2231 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2232#endif
2233 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2234 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
cfcf0ffd 2235 "deinterlace pictures" },
bdc4796f 2236 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
5727b222 2237 "add timings for benchmarking" },
a0663ba4
FB
2238 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2239 "dump each input packet" },
85f07f22
FB
2240
2241 { NULL, },
2242};
2243
2244int main(int argc, char **argv)
2245{
2246 int optindex, i;
2247 const char *opt, *arg;
2248 const OptionDef *po;
2249
2250 register_all();
2251
2252 if (argc <= 1)
2253 show_help();
2254
2255 optindex = 1;
2256 while (optindex < argc) {
2257 opt = argv[optindex++];
2258
2259 if (opt[0] == '-' && opt[1] != '\0') {
2260 po = options;
2261 while (po->name != NULL) {
2262 if (!strcmp(opt + 1, po->name))
2263 break;
2264 po++;
2265 }
2266 if (!po->name) {
2267 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2268 exit(1);
2269 }
2270 arg = NULL;
2271 if (po->flags & HAS_ARG)
2272 arg = argv[optindex++];
2273 if (po->flags & OPT_STRING) {
2274 char *str;
2275 str = strdup(arg);
2276 *po->u.str_arg = str;
2277 } else if (po->flags & OPT_BOOL) {
2278 *po->u.int_arg = 1;
2279 } else {
2280 po->u.func_arg(arg);
2281 }
2282 } else {
2283 opt_output_file(opt);
2284 }
2285 }
2286
2287
2288 if (nb_input_files == 0) {
bdc4796f 2289#ifdef CONFIG_GRAB
85f07f22
FB
2290 if (nb_output_files != 1) {
2291 fprintf(stderr, "Only one output file supported when grabbing\n");
2292 exit(1);
2293 }
2294 av_grab(output_files[0]);
bdc4796f
FB
2295#else
2296 fprintf(stderr, "Must supply at least one input file\n");
2297#endif
85f07f22 2298 } else {
5727b222
FB
2299 INT64 ti;
2300
85f07f22
FB
2301 if (nb_output_files <= 0) {
2302 fprintf(stderr, "Must supply at least one output file\n");
2303 exit(1);
2304 }
5727b222 2305 ti = getutime();
85f07f22
FB
2306 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2307 stream_maps, nb_stream_maps);
5727b222
FB
2308 ti = getutime() - ti;
2309 if (do_benchmark) {
2310 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2311 }
85f07f22
FB
2312 }
2313
2314 /* close files */
2315 for(i=0;i<nb_output_files;i++) {
2316 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2317 url_fclose(&output_files[i]->pb);
2318 }
2319 for(i=0;i<nb_input_files;i++) {
2320 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2321 url_fclose(&input_files[i]->pb);
2322 }
2323
2324 return 0;
2325}