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