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