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