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