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