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