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