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