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