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