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