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