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