trying to fix qscale==0
[libav.git] / ffmpeg.c
CommitLineData
85f07f22
FB
1/*
2 * FFmpeg main
3 * Copyright (c) 2000,2001 Gerard Lantau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
daf8e955
FB
19#define HAVE_AV_CONFIG_H
20#include "avformat.h"
21
bdc4796f 22#ifndef CONFIG_WIN32
85f07f22
FB
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
85f07f22 26#include <sys/time.h>
85f07f22 27#include <termios.h>
5727b222
FB
28#include <sys/time.h>
29#include <sys/resource.h>
85f07f22 30#include <ctype.h>
bdc4796f 31#endif
85f07f22 32
85f07f22 33
bdc4796f
FB
34#define MAXINT64 INT64_C(0x7fffffffffffffff)
35
85f07f22
FB
36typedef struct {
37 const char *name;
38 int flags;
39#define HAS_ARG 0x0001
40#define OPT_BOOL 0x0002
41#define OPT_EXPERT 0x0004
42#define OPT_STRING 0x0008
43 union {
44 void (*func_arg)();
45 int *int_arg;
46 char **str_arg;
47 } u;
48 const char *help;
49 const char *argname;
50} OptionDef;
51
52/* select an input stream for an output stream */
53typedef struct AVStreamMap {
54 int file_index;
55 int stream_index;
56} AVStreamMap;
57
58extern const OptionDef options[];
59
60void show_help(void);
61
62#define MAX_FILES 20
63
64static AVFormatContext *input_files[MAX_FILES];
65static int nb_input_files = 0;
66
67static AVFormatContext *output_files[MAX_FILES];
68static int nb_output_files = 0;
69
70static AVStreamMap stream_maps[MAX_FILES];
71static int nb_stream_maps;
72
73static AVFormat *file_format;
74static int frame_width = 160;
75static int frame_height = 128;
76static int frame_rate = 25 * FRAME_RATE_BASE;
77static int video_bit_rate = 200000;
9cdd6a24 78static int video_bit_rate_tolerance = 200000;
85f07f22 79static int video_qscale = 0;
9cdd6a24
MN
80static int video_qmin = 3;
81static int video_qmax = 15;
82static int video_qdiff = 3;
83static float video_qblur = 0.5;
84static float video_qcomp = 0.5;
85f07f22
FB
85static int video_disable = 0;
86static int video_codec_id = CODEC_ID_NONE;
87static int same_quality = 0;
cfcf0ffd 88static int do_deinterlace = 0;
85f07f22
FB
89
90static int gop_size = 12;
91static int intra_only = 0;
92static int audio_sample_rate = 44100;
93static int audio_bit_rate = 64000;
94static int audio_disable = 0;
95static int audio_channels = 1;
96static int audio_codec_id = CODEC_ID_NONE;
97
98static INT64 recording_time = 0;
99static int file_overwrite = 0;
100static char *str_title = NULL;
101static char *str_author = NULL;
102static char *str_copyright = NULL;
103static char *str_comment = NULL;
5727b222 104static int do_benchmark = 0;
a0663ba4 105static int do_hex_dump = 0;
a38469e1 106static int do_play = 0;
43f1708f 107static int do_psnr = 0;
85f07f22
FB
108
109typedef struct AVOutputStream {
110 int file_index; /* file index */
111 int index; /* stream index in the output file */
112 int source_index; /* AVInputStream index */
113 AVStream *st; /* stream in the output file */
114 int encoding_needed; /* true if encoding needed for this stream */
115
116 int fifo_packet_rptr; /* read index in the corresponding
117 avinputstream packet fifo */
118 /* video only */
119 AVPicture pict_tmp; /* temporary image for resizing */
120 int video_resample;
121 ImgReSampleContext *img_resample_ctx; /* for image resampling */
122
123 /* audio only */
124 int audio_resample;
125 ReSampleContext *resample; /* for audio resampling */
126 FifoBuffer fifo; /* for compression: one audio fifo per codec */
127} AVOutputStream;
128
129typedef struct AVInputStream {
130 int file_index;
131 int index;
132 AVStream *st;
133 int discard; /* true if stream data should be discarded */
134 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
135 INT64 pts; /* current pts */
136 int frame_number; /* current frame */
137 INT64 sample_index; /* current sample */
138} AVInputStream;
139
140typedef struct AVInputFile {
141 int eof_reached; /* true if eof reached */
142 int ist_index; /* index of first stream in ist_table */
143 int buffer_size; /* current total buffer size */
144 int buffer_size_max; /* buffer size at which we consider we can stop
145 buffering */
146} AVInputFile;
147
bdc4796f
FB
148#ifndef CONFIG_WIN32
149
85f07f22
FB
150/* init terminal so that we can grab keys */
151static struct termios oldtty;
152
153static void term_exit(void)
154{
155 tcsetattr (0, TCSANOW, &oldtty);
156}
157
158static void term_init(void)
159{
160 struct termios tty;
161
162 tcgetattr (0, &tty);
163 oldtty = tty;
164
165 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
166 |INLCR|IGNCR|ICRNL|IXON);
167 tty.c_oflag |= OPOST;
168 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
169 tty.c_cflag &= ~(CSIZE|PARENB);
170 tty.c_cflag |= CS8;
171 tty.c_cc[VMIN] = 1;
172 tty.c_cc[VTIME] = 0;
173
174 tcsetattr (0, TCSANOW, &tty);
175
176 atexit(term_exit);
177}
178
179/* read a key without blocking */
180static int read_key(void)
181{
182 struct timeval tv;
183 int n;
184 unsigned char ch;
185 fd_set rfds;
186
187 FD_ZERO(&rfds);
188 FD_SET(0, &rfds);
189 tv.tv_sec = 0;
190 tv.tv_usec = 0;
191 n = select(1, &rfds, NULL, NULL, &tv);
192 if (n > 0) {
193 if (read(0, &ch, 1) == 1)
194 return ch;
195 }
196 return -1;
197}
198
a38469e1 199#else
85f07f22 200
a38469e1
FB
201/* no interactive support */
202static void term_exit(void)
85f07f22 203{
a38469e1 204}
85f07f22 205
a38469e1
FB
206static void term_init(void)
207{
208}
85f07f22 209
a38469e1
FB
210static int read_key(void)
211{
212 return -1;
85f07f22
FB
213}
214
a38469e1 215#endif
bdc4796f 216
85f07f22
FB
217int read_ffserver_streams(AVFormatContext *s, const char *filename)
218{
219 int i;
220 AVFormatContext *ic;
221
a38469e1 222 ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
85f07f22
FB
223 if (!ic)
224 return -EIO;
225 /* copy stream format */
226 s->nb_streams = ic->nb_streams;
227 for(i=0;i<ic->nb_streams;i++) {
228 AVStream *st;
229 st = av_mallocz(sizeof(AVFormatContext));
230 memcpy(st, ic->streams[i], sizeof(AVStream));
231 s->streams[i] = st;
232 }
233
234 av_close_input_file(ic);
235 return 0;
236}
237
238#define MAX_AUDIO_PACKET_SIZE 16384
239
240static void do_audio_out(AVFormatContext *s,
241 AVOutputStream *ost,
242 AVInputStream *ist,
243 unsigned char *buf, int size)
244{
245 UINT8 *buftmp;
246 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
247 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
248 int size_out, frame_bytes, ret;
249 AVCodecContext *enc;
250
251 enc = &ost->st->codec;
252
253 if (ost->audio_resample) {
254 buftmp = audio_buf;
255 size_out = audio_resample(ost->resample,
256 (short *)buftmp, (short *)buf,
257 size / (ist->st->codec.channels * 2));
258 size_out = size_out * enc->channels * 2;
259 } else {
260 buftmp = buf;
261 size_out = size;
262 }
263
264 /* now encode as many frames as possible */
a0663ba4 265 if (enc->frame_size > 1) {
85f07f22
FB
266 /* output resampled raw samples */
267 fifo_write(&ost->fifo, buftmp, size_out,
268 &ost->fifo.wptr);
269
270 frame_bytes = enc->frame_size * 2 * enc->channels;
271
272 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
273 &ost->fifo.rptr) == 0) {
a0663ba4
FB
274 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
275 (short *)audio_buf);
85f07f22
FB
276 s->format->write_packet(s, ost->index, audio_out, ret);
277 }
278 } else {
a0663ba4
FB
279 /* output a pcm frame */
280 /* XXX: change encoding codec API to avoid this ? */
281 switch(enc->codec->id) {
282 case CODEC_ID_PCM_S16LE:
283 case CODEC_ID_PCM_S16BE:
284 case CODEC_ID_PCM_U16LE:
285 case CODEC_ID_PCM_U16BE:
286 break;
287 default:
288 size_out = size_out >> 1;
289 break;
290 }
291 ret = avcodec_encode_audio(enc, audio_out, size_out,
292 (short *)buftmp);
293 s->format->write_packet(s, ost->index, audio_out, ret);
85f07f22
FB
294 }
295}
296
297/* write a picture to a raw mux */
cfcf0ffd
FB
298static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
299 int pix_fmt, int w, int h)
85f07f22
FB
300{
301 UINT8 *buf, *src, *dest;
302 int size, j, i;
cfcf0ffd
FB
303
304 size = avpicture_get_size(pix_fmt, w, h);
305 buf = malloc(size);
306 if (!buf)
307 return;
308
85f07f22
FB
309 /* XXX: not efficient, should add test if we can take
310 directly the AVPicture */
cfcf0ffd
FB
311 switch(pix_fmt) {
312 case PIX_FMT_YUV420P:
313 dest = buf;
314 for(i=0;i<3;i++) {
315 if (i == 1) {
316 w >>= 1;
317 h >>= 1;
318 }
319 src = picture->data[i];
320 for(j=0;j<h;j++) {
321 memcpy(dest, src, w);
322 dest += w;
323 src += picture->linesize[i];
324 }
325 }
326 break;
327 case PIX_FMT_YUV422P:
328 size = (w * h) * 2;
329 buf = malloc(size);
330 dest = buf;
331 for(i=0;i<3;i++) {
332 if (i == 1) {
333 w >>= 1;
334 }
335 src = picture->data[i];
336 for(j=0;j<h;j++) {
337 memcpy(dest, src, w);
338 dest += w;
339 src += picture->linesize[i];
340 }
341 }
342 break;
343 case PIX_FMT_YUV444P:
344 size = (w * h) * 3;
345 buf = malloc(size);
346 dest = buf;
347 for(i=0;i<3;i++) {
348 src = picture->data[i];
349 for(j=0;j<h;j++) {
350 memcpy(dest, src, w);
351 dest += w;
352 src += picture->linesize[i];
353 }
354 }
355 break;
356 case PIX_FMT_YUV422:
357 size = (w * h) * 2;
358 buf = malloc(size);
359 dest = buf;
360 src = picture->data[0];
361 for(j=0;j<h;j++) {
362 memcpy(dest, src, w * 2);
363 dest += w * 2;
364 src += picture->linesize[0];
85f07f22 365 }
cfcf0ffd
FB
366 break;
367 case PIX_FMT_RGB24:
368 case PIX_FMT_BGR24:
369 size = (w * h) * 3;
370 buf = malloc(size);
371 dest = buf;
372 src = picture->data[0];
85f07f22 373 for(j=0;j<h;j++) {
cfcf0ffd
FB
374 memcpy(dest, src, w * 3);
375 dest += w * 3;
376 src += picture->linesize[0];
85f07f22 377 }
cfcf0ffd
FB
378 break;
379 default:
380 return;
85f07f22
FB
381 }
382 s->format->write_packet(s, index, buf, size);
383 free(buf);
384}
385
386
387static void do_video_out(AVFormatContext *s,
388 AVOutputStream *ost,
389 AVInputStream *ist,
cfcf0ffd 390 AVPicture *picture1)
85f07f22
FB
391{
392 int n1, n2, nb, i, ret, frame_number;
cfcf0ffd
FB
393 AVPicture *picture, *picture2, *pict;
394 AVPicture picture_tmp1, picture_tmp2;
544286b3 395 UINT8 video_buffer[1024*1024];
cfcf0ffd
FB
396 UINT8 *buf = NULL, *buf1 = NULL;
397 AVCodecContext *enc, *dec;
85f07f22
FB
398
399 enc = &ost->st->codec;
cfcf0ffd 400 dec = &ist->st->codec;
85f07f22
FB
401
402 frame_number = ist->frame_number;
403 /* first drop frame if needed */
cfcf0ffd
FB
404 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
405 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
85f07f22
FB
406 nb = n2 - n1;
407 if (nb <= 0)
408 return;
409
cfcf0ffd
FB
410 /* deinterlace : must be done before any resize */
411 if (do_deinterlace) {
412 int size;
413
414 /* create temporary picture */
415 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
416 buf1 = malloc(size);
417 if (!buf1)
418 return;
419
420 picture2 = &picture_tmp2;
421 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
422
423 if (avpicture_deinterlace(picture2, picture1,
424 dec->pix_fmt, dec->width, dec->height) < 0) {
425 /* if error, do not deinterlace */
426 free(buf1);
427 buf1 = NULL;
428 picture2 = picture1;
429 }
430 } else {
431 picture2 = picture1;
432 }
433
434 /* convert pixel format if needed */
435 if (enc->pix_fmt != dec->pix_fmt) {
436 int size;
437
438 /* create temporary picture */
439 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
440 buf = malloc(size);
441 if (!buf)
442 return;
443 pict = &picture_tmp1;
444 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
445
446 if (img_convert(pict, enc->pix_fmt,
447 picture2, dec->pix_fmt,
448 dec->width, dec->height) < 0) {
449 fprintf(stderr, "pixel format conversion not handled\n");
450 goto the_end;
451 }
452 } else {
453 pict = picture2;
454 }
455
456 /* XXX: resampling could be done before raw format convertion in
457 some cases to go faster */
458 /* XXX: only works for YUV420P */
85f07f22
FB
459 if (ost->video_resample) {
460 picture = &ost->pict_tmp;
461 img_resample(ost->img_resample_ctx, picture, pict);
462 } else {
463 picture = pict;
464 }
465
466 /* duplicates frame if needed */
467 /* XXX: pb because no interleaving */
468 for(i=0;i<nb;i++) {
469 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
470 /* handles sameq here. This is not correct because it may
471 not be a global option */
472 if (same_quality) {
cfcf0ffd 473 enc->quality = dec->quality;
85f07f22 474 }
cfcf0ffd 475 ret = avcodec_encode_video(enc,
85f07f22
FB
476 video_buffer, sizeof(video_buffer),
477 picture);
478 s->format->write_packet(s, ost->index, video_buffer, ret);
479 } else {
cfcf0ffd 480 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
85f07f22
FB
481 }
482 }
cfcf0ffd
FB
483 the_end:
484 if (buf)
485 free(buf);
486 if (buf1)
487 free(buf1);
85f07f22
FB
488}
489
85f07f22
FB
490static void hex_dump(UINT8 *buf, int size)
491{
492 int len, i, j, c;
493
494 for(i=0;i<size;i+=16) {
495 len = size - i;
496 if (len > 16)
497 len = 16;
498 printf("%08x ", i);
499 for(j=0;j<16;j++) {
500 if (j < len)
501 printf(" %02x", buf[i+j]);
502 else
503 printf(" ");
504 }
505 printf(" ");
506 for(j=0;j<len;j++) {
507 c = buf[i+j];
508 if (c < ' ' || c > '~')
509 c = '.';
510 printf("%c", c);
511 }
512 printf("\n");
513 }
514}
85f07f22
FB
515
516/*
517 * The following code is the main loop of the file converter
518 */
519static int av_encode(AVFormatContext **output_files,
520 int nb_output_files,
521 AVFormatContext **input_files,
522 int nb_input_files,
523 AVStreamMap *stream_maps, int nb_stream_maps)
524{
bdc4796f 525 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
85f07f22
FB
526 AVFormatContext *is, *os;
527 AVCodecContext *codec, *icodec;
528 AVOutputStream *ost, **ost_table = NULL;
529 AVInputStream *ist, **ist_table = NULL;
530 INT64 min_pts, start_time;
bdc4796f
FB
531 AVInputFile *file_table;
532
533 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
534 if (!file_table)
535 goto fail;
85f07f22
FB
536
537 memset(file_table, 0, sizeof(file_table));
538
539 /* input stream init */
540 j = 0;
541 for(i=0;i<nb_input_files;i++) {
542 is = input_files[i];
543 file_table[i].ist_index = j;
544 j += is->nb_streams;
545 }
546 nb_istreams = j;
547
548 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
549 if (!ist_table)
bdc4796f 550 goto fail;
85f07f22
FB
551
552 for(i=0;i<nb_istreams;i++) {
553 ist = av_mallocz(sizeof(AVInputStream));
554 if (!ist)
555 goto fail;
556 ist_table[i] = ist;
557 }
558 j = 0;
559 for(i=0;i<nb_input_files;i++) {
560 is = input_files[i];
561 for(k=0;k<is->nb_streams;k++) {
562 ist = ist_table[j++];
563 ist->st = is->streams[k];
564 ist->file_index = i;
565 ist->index = k;
566 ist->discard = 1; /* the stream is discarded by default
567 (changed later) */
568 }
569 }
570
571 /* output stream init */
572 nb_ostreams = 0;
573 for(i=0;i<nb_output_files;i++) {
574 os = output_files[i];
575 nb_ostreams += os->nb_streams;
576 }
577 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
578 fprintf(stderr, "Number of stream maps must match number of output streams\n");
579 exit(1);
580 }
581
582 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
583 if (!ost_table)
584 goto fail;
585 for(i=0;i<nb_ostreams;i++) {
586 ost = av_mallocz(sizeof(AVOutputStream));
587 if (!ost)
588 goto fail;
589 ost_table[i] = ost;
590 }
591
592 n = 0;
593 for(k=0;k<nb_output_files;k++) {
594 os = output_files[k];
595 for(i=0;i<os->nb_streams;i++) {
596 int found;
597 ost = ost_table[n++];
598 ost->file_index = k;
599 ost->index = i;
600 ost->st = os->streams[i];
601 if (nb_stream_maps > 0) {
602 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
603 stream_maps[n-1].stream_index;
604 } else {
605 /* get corresponding input stream index : we select the first one with the right type */
606 found = 0;
607 for(j=0;j<nb_istreams;j++) {
608 ist = ist_table[j];
609 if (ist->discard &&
610 ist->st->codec.codec_type == ost->st->codec.codec_type) {
611 ost->source_index = j;
612 found = 1;
613 }
614 }
615
616 if (!found) {
617 /* try again and reuse existing stream */
618 for(j=0;j<nb_istreams;j++) {
619 ist = ist_table[j];
620 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
621 ost->source_index = j;
622 found = 1;
623 }
624 }
625 if (!found) {
626 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
627 ost->file_index, ost->index);
628 exit(1);
629 }
630 }
631 }
632 ist = ist_table[ost->source_index];
633 ist->discard = 0;
634 }
635 }
636
637 /* dump the stream mapping */
638 fprintf(stderr, "Stream mapping:\n");
639 for(i=0;i<nb_ostreams;i++) {
640 ost = ost_table[i];
641 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
642 ist_table[ost->source_index]->file_index,
643 ist_table[ost->source_index]->index,
644 ost->file_index,
645 ost->index);
646 }
647
648 /* for each output stream, we compute the right encoding parameters */
649 for(i=0;i<nb_ostreams;i++) {
650 ost = ost_table[i];
651 ist = ist_table[ost->source_index];
652
653 codec = &ost->st->codec;
654 icodec = &ist->st->codec;
655
656 switch(codec->codec_type) {
657 case CODEC_TYPE_AUDIO:
658 /* check if same codec with same parameters. If so, no
659 reencoding is needed */
660 if (codec->codec_id == icodec->codec_id &&
661 codec->bit_rate == icodec->bit_rate &&
662 codec->sample_rate == icodec->sample_rate &&
663 codec->channels == icodec->channels) {
664 /* no reencoding */
665 } else {
666 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
667 goto fail;
668
669 if (codec->channels == icodec->channels &&
670 codec->sample_rate == icodec->sample_rate) {
671 ost->audio_resample = 0;
672 } else {
673 ost->audio_resample = 1;
674 ost->resample = audio_resample_init(codec->channels, icodec->channels,
675 codec->sample_rate,
676 icodec->sample_rate);
677 }
678 ist->decoding_needed = 1;
679 ost->encoding_needed = 1;
680 }
681 break;
682 case CODEC_TYPE_VIDEO:
683 /* check if same codec with same parameters. If so, no
684 reencoding is needed */
685 if (codec->codec_id == icodec->codec_id &&
686 codec->bit_rate == icodec->bit_rate &&
687 codec->frame_rate == icodec->frame_rate &&
688 codec->width == icodec->width &&
689 codec->height == icodec->height) {
690 /* no reencoding */
691 } else {
692 if (codec->width == icodec->width &&
693 codec->height == icodec->height) {
694 ost->video_resample = 0;
695 } else {
696 UINT8 *buf;
697 ost->video_resample = 1;
698 buf = malloc((codec->width * codec->height * 3) / 2);
699 if (!buf)
700 goto fail;
701 ost->pict_tmp.data[0] = buf;
702 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
703 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
704 ost->pict_tmp.linesize[0] = codec->width;
705 ost->pict_tmp.linesize[1] = codec->width / 2;
706 ost->pict_tmp.linesize[2] = codec->width / 2;
707
708 ost->img_resample_ctx = img_resample_init(
709 ost->st->codec.width, ost->st->codec.height,
710 ist->st->codec.width, ist->st->codec.height);
711 }
712 ost->encoding_needed = 1;
713 ist->decoding_needed = 1;
714 }
715 break;
716 }
717 }
718
719 /* open each encoder */
720 for(i=0;i<nb_ostreams;i++) {
721 ost = ost_table[i];
722 if (ost->encoding_needed) {
723 AVCodec *codec;
724 codec = avcodec_find_encoder(ost->st->codec.codec_id);
725 if (!codec) {
726 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
727 ost->file_index, ost->index);
728 exit(1);
729 }
730 if (avcodec_open(&ost->st->codec, codec) < 0) {
731 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
732 ost->file_index, ost->index);
733 exit(1);
734 }
735 }
736 }
737
738 /* open each decoder */
739 for(i=0;i<nb_istreams;i++) {
740 ist = ist_table[i];
741 if (ist->decoding_needed) {
742 AVCodec *codec;
743 codec = avcodec_find_decoder(ist->st->codec.codec_id);
744 if (!codec) {
745 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
746 ist->file_index, ist->index);
747 exit(1);
748 }
749 if (avcodec_open(&ist->st->codec, codec) < 0) {
750 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
751 ist->file_index, ist->index);
752 exit(1);
753 }
754 }
755 }
756
757 /* init pts */
758 for(i=0;i<nb_istreams;i++) {
759 ist = ist_table[i];
760 ist->pts = 0;
761 ist->frame_number = 0;
762 }
763
764 /* compute buffer size max (should use a complete heuristic) */
765 for(i=0;i<nb_input_files;i++) {
766 file_table[i].buffer_size_max = 2048;
767 }
768
769 /* open files and write file headers */
770 for(i=0;i<nb_output_files;i++) {
771 os = output_files[i];
a38469e1
FB
772 if (os->format->write_header(os) < 0) {
773 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
774 ret = -EINVAL;
775 goto fail;
776 }
85f07f22
FB
777 }
778
a38469e1
FB
779#ifndef CONFIG_WIN32
780 if (!do_play) {
781 fprintf(stderr, "Press [q] to stop encoding\n");
782 } else {
783 fprintf(stderr, "Press [q] to stop playing\n");
784 }
785#endif
786 term_init();
787
85f07f22
FB
788 start_time = gettime();
789 min_pts = 0;
790 for(;;) {
791 int file_index, ist_index;
792 AVPacket pkt;
793 UINT8 *ptr;
794 int len;
795 UINT8 *data_buf;
796 int data_size, got_picture;
797 AVPicture picture;
798 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
799
85f07f22 800 redo:
a38469e1
FB
801 /* if 'q' pressed, exits */
802 if (read_key() == 'q')
803 break;
804
805 /* select the input file with the smallest pts */
85f07f22 806 file_index = -1;
bdc4796f 807 min_pts = MAXINT64;
85f07f22
FB
808 for(i=0;i<nb_istreams;i++) {
809 ist = ist_table[i];
810 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
811 min_pts = ist->pts;
812 file_index = ist->file_index;
813 }
814 }
815 /* if none, if is finished */
816 if (file_index < 0)
817 break;
818 /* finish if recording time exhausted */
819 if (recording_time > 0 && min_pts >= recording_time)
820 break;
821 /* read a packet from it and output it in the fifo */
85f07f22
FB
822 is = input_files[file_index];
823 if (av_read_packet(is, &pkt) < 0) {
824 file_table[file_index].eof_reached = 1;
825 continue;
826 }
827 ist_index = file_table[file_index].ist_index + pkt.stream_index;
828 ist = ist_table[ist_index];
829 if (ist->discard) {
830 continue;
831 }
832
a0663ba4
FB
833 if (do_hex_dump) {
834 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
835 hex_dump(pkt.data, pkt.size);
836 }
85f07f22
FB
837
838 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
839
840 len = pkt.size;
841 ptr = pkt.data;
842 while (len > 0) {
843
844 /* decode the packet if needed */
845 data_buf = NULL; /* fail safe */
846 data_size = 0;
847 if (ist->decoding_needed) {
848 switch(ist->st->codec.codec_type) {
849 case CODEC_TYPE_AUDIO:
a0663ba4
FB
850 /* XXX: could avoid copy if PCM 16 bits with same
851 endianness as CPU */
852 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
853 ptr, len);
854 if (ret < 0)
855 goto fail_decode;
856 if (data_size == 0) {
857 /* no audio frame */
858 ptr += ret;
859 len -= ret;
860 continue;
85f07f22 861 }
a0663ba4 862 data_buf = (UINT8 *)samples;
85f07f22
FB
863 break;
864 case CODEC_TYPE_VIDEO:
865 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
866 int size;
867 size = (ist->st->codec.width * ist->st->codec.height);
cfcf0ffd
FB
868 avpicture_fill(&picture, ptr,
869 ist->st->codec.pix_fmt,
870 ist->st->codec.width,
871 ist->st->codec.height);
85f07f22
FB
872 ret = len;
873 } else {
874 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
875 ret = avcodec_decode_video(&ist->st->codec,
876 &picture, &got_picture, ptr, len);
877 if (ret < 0) {
878 fail_decode:
879 fprintf(stderr, "Error while decoding stream #%d.%d\n",
880 ist->file_index, ist->index);
881 av_free_packet(&pkt);
882 goto redo;
883 }
884 if (!got_picture) {
885 /* no picture yet */
886 ptr += ret;
887 len -= ret;
888 continue;
889 }
890 }
891 break;
892 default:
893 goto fail_decode;
894 }
895 } else {
896 data_buf = ptr;
897 data_size = len;
898 ret = len;
899 }
900 /* update pts */
901 switch(ist->st->codec.codec_type) {
902 case CODEC_TYPE_AUDIO:
903 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
904 ist->sample_index += data_size / (2 * ist->st->codec.channels);
905 break;
906 case CODEC_TYPE_VIDEO:
907 ist->frame_number++;
908 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
909 ist->st->codec.frame_rate;
910 break;
911 }
912 ptr += ret;
913 len -= ret;
914
915 /* transcode raw format, encode packets and output them */
916
917 for(i=0;i<nb_ostreams;i++) {
918 ost = ost_table[i];
919 if (ost->source_index == ist_index) {
920 os = output_files[ost->file_index];
921
922 if (ost->encoding_needed) {
923 switch(ost->st->codec.codec_type) {
924 case CODEC_TYPE_AUDIO:
925 do_audio_out(os, ost, ist, data_buf, data_size);
926 break;
927 case CODEC_TYPE_VIDEO:
928 do_video_out(os, ost, ist, &picture);
929 break;
930 }
931 } else {
932 /* no reencoding needed : output the packet directly */
933 os->format->write_packet(os, ost->index, data_buf, data_size);
934 }
935 }
936 }
937 }
938 av_free_packet(&pkt);
939
940 /* dump report by using the first video and audio streams */
941 {
942 char buf[1024];
943 AVFormatContext *oc;
944 INT64 total_size, ti;
945 AVCodecContext *enc;
946 int frame_number, vid;
947 double bitrate, ti1;
948 static INT64 last_time;
949
950 if ((min_pts - last_time) >= 500000) {
951 last_time = min_pts;
952
953 oc = output_files[0];
954
955 total_size = url_ftell(&oc->pb);
956
957 buf[0] = '\0';
bdc4796f 958 ti = MAXINT64;
85f07f22
FB
959 vid = 0;
960 for(i=0;i<nb_ostreams;i++) {
961 ost = ost_table[i];
962 enc = &ost->st->codec;
963 ist = ist_table[ost->source_index];
964 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
965 frame_number = ist->frame_number;
966 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
967 frame_number, enc->quality);
43f1708f
J
968 if (do_psnr)
969 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
85f07f22
FB
970 vid = 1;
971 }
972 /* compute min pts value */
973 if (!ist->discard && ist->pts < ti) {
974 ti = ist->pts;
975 }
976 }
977
bdc4796f 978 ti1 = (double)ti / 1000000.0;
85f07f22
FB
979 if (ti1 < 0.1)
980 ti1 = 0.1;
981 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
982
bdc4796f
FB
983 sprintf(buf + strlen(buf),
984 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
985 (double)total_size / 1024, ti1, bitrate);
85f07f22
FB
986
987 fprintf(stderr, "%s \r", buf);
988 fflush(stderr);
989 }
990 }
991 }
a38469e1 992 term_exit();
85f07f22
FB
993
994 /* dump report by using the first video and audio streams */
995 {
996 char buf[1024];
997 AVFormatContext *oc;
998 INT64 total_size, ti;
999 AVCodecContext *enc;
1000 int frame_number, vid;
1001 double bitrate, ti1;
1002
1003 oc = output_files[0];
1004
1005 total_size = url_ftell(&oc->pb);
1006
1007 buf[0] = '\0';
bdc4796f 1008 ti = MAXINT64;
85f07f22
FB
1009 vid = 0;
1010 for(i=0;i<nb_ostreams;i++) {
1011 ost = ost_table[i];
1012 enc = &ost->st->codec;
1013 ist = ist_table[ost->source_index];
1014 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1015 frame_number = ist->frame_number;
1016 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1017 frame_number, enc->quality);
43f1708f
J
1018 if (do_psnr)
1019 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
85f07f22
FB
1020 vid = 1;
1021 }
1022 /* compute min pts value */
1023 if (!ist->discard && ist->pts < ti) {
1024 ti = ist->pts;
1025 }
1026 }
1027
1028 ti1 = ti / 1000000.0;
1029 if (ti1 < 0.1)
1030 ti1 = 0.1;
1031 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1032
bdc4796f
FB
1033 sprintf(buf + strlen(buf),
1034 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1035 (double)total_size / 1024, ti1, bitrate);
85f07f22
FB
1036
1037 fprintf(stderr, "%s \n", buf);
1038 }
1039 /* close each encoder */
1040 for(i=0;i<nb_ostreams;i++) {
1041 ost = ost_table[i];
1042 if (ost->encoding_needed) {
1043 avcodec_close(&ost->st->codec);
1044 }
1045 }
1046
1047 /* close each decoder */
1048 for(i=0;i<nb_istreams;i++) {
1049 ist = ist_table[i];
1050 if (ist->decoding_needed) {
1051 avcodec_close(&ist->st->codec);
1052 }
1053 }
1054
1055
1056 /* write the trailer if needed and close file */
1057 for(i=0;i<nb_output_files;i++) {
1058 os = output_files[i];
1059 os->format->write_trailer(os);
1060 }
1061 /* finished ! */
1062
1063 ret = 0;
1064 fail1:
bdc4796f
FB
1065 free(file_table);
1066
85f07f22
FB
1067 if (ist_table) {
1068 for(i=0;i<nb_istreams;i++) {
1069 ist = ist_table[i];
1070 if (ist) {
1071 free(ist);
1072 }
1073 }
1074 free(ist_table);
1075 }
1076 if (ost_table) {
1077 for(i=0;i<nb_ostreams;i++) {
1078 ost = ost_table[i];
1079 if (ost) {
1080 if (ost->pict_tmp.data[0])
1081 free(ost->pict_tmp.data[0]);
1082 if (ost->video_resample)
1083 img_resample_close(ost->img_resample_ctx);
1084 if (ost->audio_resample)
1085 audio_resample_close(ost->resample);
1086 free(ost);
1087 }
1088 }
1089 free(ost_table);
1090 }
1091 return ret;
1092 fail:
1093 ret = -ENOMEM;
1094 goto fail1;
1095}
1096
1097#if 0
1098int file_read(const char *filename)
1099{
1100 URLContext *h;
1101 unsigned char buffer[1024];
1102 int len, i;
1103
1104 if (url_open(&h, filename, O_RDONLY) < 0) {
1105 printf("could not open '%s'\n", filename);
1106 return -1;
1107 }
1108 for(;;) {
1109 len = url_read(h, buffer, sizeof(buffer));
1110 if (len <= 0)
1111 break;
1112 for(i=0;i<len;i++) putchar(buffer[i]);
1113 }
1114 url_close(h);
1115 return 0;
1116}
1117#endif
1118
1119void show_licence(void)
1120{
1121 printf(
1122 "ffmpeg version " FFMPEG_VERSION "\n"
1123 "Copyright (c) 2000,2001 Gerard Lantau\n"
1124 "This program is free software; you can redistribute it and/or modify\n"
1125 "it under the terms of the GNU General Public License as published by\n"
1126 "the Free Software Foundation; either version 2 of the License, or\n"
1127 "(at your option) any later version.\n"
1128 "\n"
1129 "This program is distributed in the hope that it will be useful,\n"
1130 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1131 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1132 "GNU General Public License for more details.\n"
1133 "\n"
1134 "You should have received a copy of the GNU General Public License\n"
1135 "along with this program; if not, write to the Free Software\n"
1136 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1137 );
1138 exit(1);
1139}
1140
1141void opt_format(const char *arg)
1142{
1143 AVFormat *f;
1144 f = first_format;
1145 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1146 if (f == NULL) {
1147 fprintf(stderr, "Invalid format: %s\n", arg);
1148 exit(1);
1149 }
1150 file_format = f;
1151}
1152
1153void opt_video_bitrate(const char *arg)
1154{
1155 video_bit_rate = atoi(arg) * 1000;
1156}
1157
9cdd6a24
MN
1158void opt_video_bitrate_tolerance(const char *arg)
1159{
1160 video_bit_rate_tolerance = atoi(arg) * 1000;
1161}
1162
85f07f22
FB
1163void opt_frame_rate(const char *arg)
1164{
1165 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1166}
1167
1168void opt_frame_size(const char *arg)
1169{
1170 parse_image_size(&frame_width, &frame_height, arg);
1171 if (frame_width <= 0 || frame_height <= 0) {
1172 fprintf(stderr, "Incorrect frame size\n");
1173 exit(1);
1174 }
1175 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1176 fprintf(stderr, "Frame size must be a multiple of 2\n");
1177 exit(1);
1178 }
1179}
1180
1181void opt_gop_size(const char *arg)
1182{
1183 gop_size = atoi(arg);
1184}
1185
1186void opt_qscale(const char *arg)
1187{
1188 video_qscale = atoi(arg);
1189 if (video_qscale < 0 ||
1190 video_qscale > 31) {
1191 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1192 exit(1);
1193 }
1194}
1195
9cdd6a24
MN
1196void opt_qmin(const char *arg)
1197{
1198 video_qmin = atoi(arg);
1199 if (video_qmin < 0 ||
1200 video_qmin > 31) {
1201 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1202 exit(1);
1203 }
1204}
1205
1206void opt_qmax(const char *arg)
1207{
1208 video_qmax = atoi(arg);
1209 if (video_qmax < 0 ||
1210 video_qmax > 31) {
1211 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1212 exit(1);
1213 }
1214}
1215
1216void opt_qdiff(const char *arg)
1217{
1218 video_qdiff = atoi(arg);
1219 if (video_qdiff < 0 ||
1220 video_qdiff > 31) {
1221 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1222 exit(1);
1223 }
1224}
1225
1226void opt_qblur(const char *arg)
1227{
1228 video_qblur = atof(arg);
1229}
1230
1231void opt_qcomp(const char *arg)
1232{
1233 video_qcomp = atof(arg);
1234}
85f07f22
FB
1235
1236void opt_audio_bitrate(const char *arg)
1237{
1238 audio_bit_rate = atoi(arg) * 1000;
1239}
1240
1241void opt_audio_rate(const char *arg)
1242{
1243 audio_sample_rate = atoi(arg);
1244}
1245
1246void opt_audio_channels(const char *arg)
1247{
1248 audio_channels = atoi(arg);
1249}
1250
bdc4796f 1251#ifdef CONFIG_GRAB
85f07f22
FB
1252void opt_video_device(const char *arg)
1253{
1254 v4l_device = strdup(arg);
1255}
1256
1257void opt_audio_device(const char *arg)
1258{
1259 audio_device = strdup(arg);
1260}
bdc4796f 1261#endif
85f07f22
FB
1262
1263void opt_audio_codec(const char *arg)
1264{
1265 AVCodec *p;
1266
1267 p = first_avcodec;
1268 while (p) {
1269 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1270 break;
1271 p = p->next;
1272 }
1273 if (p == NULL) {
1274 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1275 exit(1);
1276 } else {
1277 audio_codec_id = p->id;
1278 }
1279}
1280
1281const char *motion_str[] = {
1282 "zero",
1283 "full",
1284 "log",
1285 "phods",
1286 NULL,
1287};
1288
1289void opt_motion_estimation(const char *arg)
1290{
1291 const char **p;
1292 p = motion_str;
1293 for(;;) {
1294 if (!*p) {
1295 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1296 exit(1);
1297 }
1298 if (!strcmp(*p, arg))
1299 break;
1300 p++;
1301 }
1302 motion_estimation_method = p - motion_str;
1303}
1304
1305void opt_video_codec(const char *arg)
1306{
1307 AVCodec *p;
1308
1309 p = first_avcodec;
1310 while (p) {
1311 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1312 break;
1313 p = p->next;
1314 }
1315 if (p == NULL) {
1316 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1317 exit(1);
1318 } else {
1319 video_codec_id = p->id;
1320 }
1321}
1322
1323void opt_map(const char *arg)
1324{
1325 AVStreamMap *m;
1326 const char *p;
1327
1328 p = arg;
1329 m = &stream_maps[nb_stream_maps++];
1330
1331 m->file_index = strtol(arg, (char **)&p, 0);
1332 if (*p)
1333 p++;
1334 m->stream_index = strtol(arg, (char **)&p, 0);
1335}
1336
1337void opt_recording_time(const char *arg)
1338{
1339 recording_time = parse_date(arg, 1);
1340}
1341
1342/* return the number of packet read to find the codec parameters */
1343int find_codec_parameters(AVFormatContext *ic)
1344{
1345 int val, i, count, ret, got_picture, size;
1346 AVCodec *codec;
1347 AVCodecContext *enc;
1348 AVStream *st;
1349 AVPacket *pkt;
1350 AVPicture picture;
1351 AVPacketList *pktl, **ppktl;
1352 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1353 UINT8 *ptr;
1354
1355 count = 0;
1356 ppktl = &ic->packet_buffer;
1357 for(;;) {
1358 for(i=0;i<ic->nb_streams;i++) {
1359 enc = &ic->streams[i]->codec;
1360
1361 switch(enc->codec_type) {
1362 case CODEC_TYPE_AUDIO:
1363 val = enc->sample_rate;
1364 break;
1365 case CODEC_TYPE_VIDEO:
1366 val = enc->width;
1367 break;
1368 default:
1369 val = 1;
1370 break;
1371 }
1372 /* if no parameters supplied, then we should read it from
1373 the stream */
1374 if (val == 0)
1375 break;
1376 }
1377 if (i == ic->nb_streams) {
1378 ret = count;
1379 break;
1380 }
1381
1382 if (count == 0) {
1383 /* open each codec */
1384 for(i=0;i<ic->nb_streams;i++) {
1385 st = ic->streams[i];
1386 codec = avcodec_find_decoder(st->codec.codec_id);
1387 if (codec == NULL) {
1388 ret = -1;
1389 goto the_end;
1390 }
1391 avcodec_open(&st->codec, codec);
1392 }
1393 }
1394 pktl = av_mallocz(sizeof(AVPacketList));
1395 if (!pktl) {
1396 ret = -1;
1397 break;
1398 }
1399
1400 /* add the packet in the buffered packet list */
1401 *ppktl = pktl;
1402 ppktl = &pktl->next;
1403
1404 pkt = &pktl->pkt;
1405 if (ic->format->read_packet(ic, pkt) < 0) {
1406 ret = -1;
1407 break;
1408 }
1409 st = ic->streams[pkt->stream_index];
1410
1411 /* decode the data and update codec parameters */
1412 ptr = pkt->data;
1413 size = pkt->size;
1414 while (size > 0) {
1415 switch(st->codec.codec_type) {
1416 case CODEC_TYPE_VIDEO:
1417 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1418 break;
1419 case CODEC_TYPE_AUDIO:
1420 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1421 break;
1422 default:
1423 ret = -1;
1424 break;
1425 }
1426 if (ret < 0) {
1427 ret = -1;
1428 goto the_end;
1429 }
1430 if (got_picture)
1431 break;
1432 ptr += ret;
1433 size -= ret;
1434 }
1435
1436 count++;
1437 }
1438 the_end:
1439 if (count > 0) {
1440 /* close each codec */
1441 for(i=0;i<ic->nb_streams;i++) {
1442 st = ic->streams[i];
1443 avcodec_close(&st->codec);
1444 }
1445 }
1446 return ret;
1447}
1448
919f448d
FB
1449int filename_number_test(const char *filename)
1450{
1451 char buf[1024];
1452
1453 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1454 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1455 "Use '%%d' to specify the image number:\n"
1456 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1457 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1458 filename);
1459 return -1;
1460 } else {
1461 return 0;
1462 }
1463}
85f07f22
FB
1464
1465void opt_input_file(const char *filename)
1466{
1467 AVFormatContext *ic;
1468 AVFormatParameters params, *ap = &params;
1469 URLFormat url_format;
1470 AVFormat *fmt;
1471 int err, i, ret;
1472
1473 ic = av_mallocz(sizeof(AVFormatContext));
1474 strcpy(ic->filename, filename);
1475 /* first format guess to know if we must open file */
1476 fmt = file_format;
1477 if (!fmt)
1478 fmt = guess_format(NULL, filename, NULL);
1479
1480 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1481 /* open file */
1482 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1483 fprintf(stderr, "Could not open '%s'\n", filename);
1484 exit(1);
1485 }
1486
1487 /* find format and set default parameters */
1488 fmt = file_format;
1489 err = url_getformat(url_fileno(&ic->pb), &url_format);
1490 if (err >= 0) {
1491 if (!fmt)
1492 fmt = guess_format(url_format.format_name, NULL, NULL);
1493 ap->sample_rate = url_format.sample_rate;
1494 ap->frame_rate = url_format.frame_rate;
1495 ap->channels = url_format.channels;
1496 ap->width = url_format.width;
1497 ap->height = url_format.height;
1498 ap->pix_fmt = url_format.pix_fmt;
1499 } else {
1500 if (!fmt)
1501 fmt = guess_format(NULL, filename, NULL);
1502 memset(ap, 0, sizeof(*ap));
1503 }
1504 } else {
1505 memset(ap, 0, sizeof(*ap));
1506 }
1507
1508 if (!fmt || !fmt->read_header) {
1509 fprintf(stderr, "%s: Unknown file format\n", filename);
1510 exit(1);
1511 }
1512 ic->format = fmt;
1513
1514 /* get default parameters from command line */
1515 if (!ap->sample_rate)
1516 ap->sample_rate = audio_sample_rate;
1517 if (!ap->channels)
1518 ap->channels = audio_channels;
1519
1520 if (!ap->frame_rate)
1521 ap->frame_rate = frame_rate;
1522 if (!ap->width)
1523 ap->width = frame_width;
1524 if (!ap->height)
1525 ap->height = frame_height;
919f448d
FB
1526
1527 /* check filename in case of an image number is expected */
1528 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1529 if (filename_number_test(ic->filename) < 0)
1530 exit(1);
1531 }
85f07f22
FB
1532
1533 err = ic->format->read_header(ic, ap);
1534 if (err < 0) {
1535 fprintf(stderr, "%s: Error while parsing header\n", filename);
1536 exit(1);
1537 }
1538
1539 /* If not enough info for the codecs, we decode the first frames
1540 to get it. (used in mpeg case for example) */
1541 ret = find_codec_parameters(ic);
1542 if (ret < 0) {
1543 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1544 exit(1);
1545 }
1546
1547 /* update the current parameters so that they match the one of the input stream */
1548 for(i=0;i<ic->nb_streams;i++) {
1549 AVCodecContext *enc = &ic->streams[i]->codec;
1550 switch(enc->codec_type) {
1551 case CODEC_TYPE_AUDIO:
1552 audio_channels = enc->channels;
1553 audio_sample_rate = enc->sample_rate;
1554 break;
1555 case CODEC_TYPE_VIDEO:
1556 frame_height = enc->height;
1557 frame_width = enc->width;
1558 frame_rate = enc->frame_rate;
1559 break;
1560 }
1561 }
1562
1563 input_files[nb_input_files] = ic;
1564 /* dump the file content */
1565 dump_format(ic, nb_input_files, filename, 0);
1566 nb_input_files++;
1567 file_format = NULL;
1568}
1569
919f448d
FB
1570void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1571{
1572 int has_video, has_audio, i, j;
1573 AVFormatContext *ic;
1574
1575 has_video = 0;
1576 has_audio = 0;
1577 for(j=0;j<nb_input_files;j++) {
1578 ic = input_files[j];
1579 for(i=0;i<ic->nb_streams;i++) {
1580 AVCodecContext *enc = &ic->streams[i]->codec;
1581 switch(enc->codec_type) {
1582 case CODEC_TYPE_AUDIO:
1583 has_audio = 1;
1584 break;
1585 case CODEC_TYPE_VIDEO:
1586 has_video = 1;
1587 break;
1588 }
1589 }
1590 }
1591 *has_video_ptr = has_video;
1592 *has_audio_ptr = has_audio;
1593}
1594
85f07f22
FB
1595void opt_output_file(const char *filename)
1596{
1597 AVStream *st;
1598 AVFormatContext *oc;
919f448d 1599 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
85f07f22
FB
1600 int codec_id;
1601
1602 if (!strcmp(filename, "-"))
1603 filename = "pipe:";
1604
1605 oc = av_mallocz(sizeof(AVFormatContext));
1606
1607 if (!file_format) {
1608 file_format = guess_format(NULL, filename, NULL);
1609 if (!file_format)
1610 file_format = &mpeg_mux_format;
1611 }
1612
1613 oc->format = file_format;
1614
1615 if (!strcmp(file_format->name, "ffm") &&
1616 strstart(filename, "http:", NULL)) {
1617 /* special case for files sent to ffserver: we get the stream
1618 parameters from ffserver */
1619 if (read_ffserver_streams(oc, filename) < 0) {
1620 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1621 exit(1);
1622 }
1623 } else {
1624 use_video = file_format->video_codec != CODEC_ID_NONE;
1625 use_audio = file_format->audio_codec != CODEC_ID_NONE;
919f448d 1626
e30a2846
FB
1627 /* disable if no corresponding type found and at least one
1628 input file */
1629 if (nb_input_files > 0) {
1630 check_audio_video_inputs(&input_has_video, &input_has_audio);
1631 if (!input_has_video)
1632 use_video = 0;
1633 if (!input_has_audio)
1634 use_audio = 0;
1635 }
919f448d
FB
1636
1637 /* manual disable */
85f07f22
FB
1638 if (audio_disable) {
1639 use_audio = 0;
1640 }
1641 if (video_disable) {
1642 use_video = 0;
1643 }
1644
1645 nb_streams = 0;
1646 if (use_video) {
1647 AVCodecContext *video_enc;
1648
1649 st = av_mallocz(sizeof(AVStream));
1650 if (!st) {
1651 fprintf(stderr, "Could not alloc stream\n");
1652 exit(1);
1653 }
1654 video_enc = &st->codec;
1655
1656 codec_id = file_format->video_codec;
1657 if (video_codec_id != CODEC_ID_NONE)
1658 codec_id = video_codec_id;
1659
1660 video_enc->codec_id = codec_id;
1661 video_enc->codec_type = CODEC_TYPE_VIDEO;
1662
1663 video_enc->bit_rate = video_bit_rate;
9cdd6a24 1664 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
85f07f22
FB
1665 video_enc->frame_rate = frame_rate;
1666
1667 video_enc->width = frame_width;
1668 video_enc->height = frame_height;
1669 if (!intra_only)
1670 video_enc->gop_size = gop_size;
1671 else
1672 video_enc->gop_size = 0;
1673 if (video_qscale || same_quality) {
1674 video_enc->flags |= CODEC_FLAG_QSCALE;
1675 video_enc->quality = video_qscale;
1676 }
9cdd6a24
MN
1677
1678 video_enc->qmin= video_qmin;
1679 video_enc->qmax= video_qmax;
1680 video_enc->max_qdiff= video_qdiff;
1681 video_enc->qblur= video_qblur;
1682 video_enc->qcompress= video_qcomp;
1683
43f1708f
J
1684 if (do_psnr)
1685 video_enc->get_psnr = 1;
1686 else
1687 video_enc->get_psnr = 0;
cfcf0ffd
FB
1688 /* XXX: need to find a way to set codec parameters */
1689 if (oc->format == &ppm_format ||
1690 oc->format == &ppmpipe_format) {
1691 video_enc->pix_fmt = PIX_FMT_RGB24;
1692 }
85f07f22
FB
1693
1694 oc->streams[nb_streams] = st;
1695 nb_streams++;
1696 }
1697
1698 if (use_audio) {
1699 AVCodecContext *audio_enc;
1700
1701 st = av_mallocz(sizeof(AVStream));
1702 if (!st) {
1703 fprintf(stderr, "Could not alloc stream\n");
1704 exit(1);
1705 }
1706 audio_enc = &st->codec;
1707 codec_id = file_format->audio_codec;
1708 if (audio_codec_id != CODEC_ID_NONE)
1709 codec_id = audio_codec_id;
1710 audio_enc->codec_id = codec_id;
1711 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1712
1713 audio_enc->bit_rate = audio_bit_rate;
1714 audio_enc->sample_rate = audio_sample_rate;
1715 audio_enc->channels = audio_channels;
1716 oc->streams[nb_streams] = st;
1717 nb_streams++;
1718 }
1719
1720 oc->nb_streams = nb_streams;
1721
1722 if (!nb_streams) {
919f448d 1723 fprintf(stderr, "No audio or video streams available\n");
85f07f22
FB
1724 exit(1);
1725 }
1726
1727 if (str_title)
1728 nstrcpy(oc->title, sizeof(oc->title), str_title);
1729 if (str_author)
1730 nstrcpy(oc->author, sizeof(oc->author), str_author);
1731 if (str_copyright)
1732 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1733 if (str_comment)
1734 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1735 }
1736
1737 output_files[nb_output_files] = oc;
1738 /* dump the file content */
1739 dump_format(oc, nb_output_files, filename, 1);
1740 nb_output_files++;
1741
1742 strcpy(oc->filename, filename);
919f448d
FB
1743
1744 /* check filename in case of an image number is expected */
1745 if (oc->format->flags & AVFMT_NEEDNUMBER) {
1746 if (filename_number_test(oc->filename) < 0)
1747 exit(1);
1748 }
1749
85f07f22
FB
1750 if (!(oc->format->flags & AVFMT_NOFILE)) {
1751 /* test if it already exists to avoid loosing precious files */
1752 if (!file_overwrite &&
1753 (strchr(filename, ':') == NULL ||
1754 strstart(filename, "file:", NULL))) {
1755 if (url_exist(filename)) {
1756 int c;
1757
1758 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1759 fflush(stdout);
1760 c = getchar();
1761 if (toupper(c) != 'Y') {
1762 fprintf(stderr, "Not overwriting - exiting\n");
1763 exit(1);
1764 }
1765 }
1766 }
1767
1768 /* open the file */
1769 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1770 fprintf(stderr, "Could not open '%s'\n", filename);
1771 exit(1);
1772 }
1773 }
1774
1775 /* reset some options */
1776 file_format = NULL;
1777 audio_disable = 0;
1778 video_disable = 0;
1779 audio_codec_id = CODEC_ID_NONE;
1780 video_codec_id = CODEC_ID_NONE;
1781}
1782
a38469e1
FB
1783#ifdef CONFIG_GRAB
1784
1785/* prepare dummy protocols for grab */
1786void prepare_grab(void)
1787{
1788 int has_video, has_audio, i, j;
1789 AVFormatContext *oc;
1790 AVFormatContext *ic;
1791 AVFormatParameters ap1, *ap = &ap1;
1792
1793 /* see if audio/video inputs are needed */
1794 has_video = 0;
1795 has_audio = 0;
1796 memset(ap, 0, sizeof(*ap));
1797 for(j=0;j<nb_output_files;j++) {
1798 oc = output_files[j];
1799 for(i=0;i<oc->nb_streams;i++) {
1800 AVCodecContext *enc = &oc->streams[i]->codec;
1801 switch(enc->codec_type) {
1802 case CODEC_TYPE_AUDIO:
1803 if (enc->sample_rate > ap->sample_rate)
1804 ap->sample_rate = enc->sample_rate;
1805 if (enc->channels > ap->channels)
1806 ap->channels = enc->channels;
1807 has_audio = 1;
1808 break;
1809 case CODEC_TYPE_VIDEO:
1810 if (enc->width > ap->width)
1811 ap->width = enc->width;
1812 if (enc->height > ap->height)
1813 ap->height = enc->height;
1814 if (enc->frame_rate > ap->frame_rate)
1815 ap->frame_rate = enc->frame_rate;
1816 has_video = 1;
1817 break;
1818 }
1819 }
1820 }
1821
1822 if (has_video == 0 && has_audio == 0) {
1823 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1824 exit(1);
1825 }
1826
1827 if (has_video) {
1828 ic = av_open_input_file("", "video_grab_device", 0, ap);
1829 if (!ic) {
1830 fprintf(stderr, "Could not open video grab device\n");
1831 exit(1);
1832 }
1833 input_files[nb_input_files] = ic;
1834 dump_format(ic, nb_input_files, v4l_device, 0);
1835 nb_input_files++;
1836 }
1837 if (has_audio) {
1838 ic = av_open_input_file("", "audio_device", 0, ap);
1839 if (!ic) {
1840 fprintf(stderr, "Could not open audio grab device\n");
1841 exit(1);
1842 }
1843 input_files[nb_input_files] = ic;
1844 dump_format(ic, nb_input_files, audio_device, 0);
1845 nb_input_files++;
1846 }
1847}
1848
1849#else
1850
1851void prepare_grab(void)
1852{
1853 fprintf(stderr, "Must supply at least one input file\n");
1854 exit(1);
1855}
1856
1857#endif
1858
1859/* open the necessary output devices for playing */
1860void prepare_play(void)
1861{
ef0bc4c9 1862#ifndef __BEOS__
a38469e1
FB
1863 file_format = guess_format("audio_device", NULL, NULL);
1864 if (!file_format) {
1865 fprintf(stderr, "Could not find audio device\n");
1866 exit(1);
1867 }
1868
1869 opt_output_file(audio_device);
ef0bc4c9 1870#endif
a38469e1
FB
1871}
1872
1873
bdc4796f 1874#ifndef CONFIG_WIN32
5727b222
FB
1875INT64 getutime(void)
1876{
1877 struct rusage rusage;
1878
1879 getrusage(RUSAGE_SELF, &rusage);
1880 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1881}
bdc4796f
FB
1882#else
1883INT64 getutime(void)
1884{
1885 return gettime();
1886}
1887#endif
5727b222 1888
85f07f22
FB
1889void show_formats(void)
1890{
1891 AVFormat *f;
1892 URLProtocol *up;
1893 AVCodec *p;
1894 const char **pp;
1895
1896 printf("File formats:\n");
1897 printf(" Encoding:");
1898 for(f = first_format; f != NULL; f = f->next) {
1899 if (f->write_header)
1900 printf(" %s", f->name);
1901 }
1902 printf("\n");
1903 printf(" Decoding:");
1904 for(f = first_format; f != NULL; f = f->next) {
1905 if (f->read_header)
1906 printf(" %s", f->name);
1907 }
1908 printf("\n");
1909
1910 printf("Codecs:\n");
1911 printf(" Encoders:");
1912 for(p = first_avcodec; p != NULL; p = p->next) {
1913 if (p->encode)
1914 printf(" %s", p->name);
1915 }
1916 printf("\n");
1917
1918 printf(" Decoders:");
1919 for(p = first_avcodec; p != NULL; p = p->next) {
1920 if (p->decode)
1921 printf(" %s", p->name);
1922 }
1923 printf("\n");
1924
1925 printf("Supported file protocols:");
1926 for(up = first_protocol; up != NULL; up = up->next)
1927 printf(" %s:", up->name);
1928 printf("\n");
1929
1930 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
1931 printf("Motion estimation methods:");
1932 pp = motion_str;
1933 while (*pp) {
1934 printf(" %s", *pp);
1935 if ((pp - motion_str) == ME_ZERO)
1936 printf("(fastest)");
1937 else if ((pp - motion_str) == ME_FULL)
1938 printf("(slowest)");
1939 else if ((pp - motion_str) == ME_LOG)
1940 printf("(default)");
1941 pp++;
1942 }
1943 printf("\n");
1944 exit(1);
1945}
1946
1947void show_help(void)
1948{
a38469e1 1949 const char *prog;
85f07f22
FB
1950 const OptionDef *po;
1951 int i, expert;
a38469e1
FB
1952
1953 prog = do_play ? "ffplay" : "ffmpeg";
85f07f22 1954
a38469e1
FB
1955 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
1956 prog);
1957
1958 if (!do_play) {
1959 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
1960 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
1961 } else {
1962 printf("usage: ffplay [options] input_file...\n"
1963 "Simple audio player\n");
1964 }
1965
1966 printf("\n"
85f07f22
FB
1967 "Main options are:\n");
1968 for(i=0;i<2;i++) {
1969 if (i == 1)
1970 printf("\nAdvanced options are:\n");
1971 for(po = options; po->name != NULL; po++) {
1972 char buf[64];
1973 expert = (po->flags & OPT_EXPERT) != 0;
1974 if (expert == i) {
1975 strcpy(buf, po->name);
1976 if (po->flags & HAS_ARG) {
1977 strcat(buf, " ");
1978 strcat(buf, po->argname);
1979 }
1980 printf("-%-17s %s\n", buf, po->help);
1981 }
1982 }
1983 }
1984
1985 exit(1);
1986}
1987
1988const OptionDef options[] = {
bdc4796f
FB
1989 { "L", 0, {(void*)show_licence}, "show license" },
1990 { "h", 0, {(void*)show_help}, "show help" },
1991 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
1992 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1993 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
1994 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
1995 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
1996 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
1997 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
1998 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
1999 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2000 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
85f07f22 2001 /* video options */
bdc4796f
FB
2002 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2003 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2004 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2005 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2006 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2007 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2008 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
9cdd6a24
MN
2009 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2010 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2011 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2012 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2013 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2014 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
bdc4796f
FB
2015#ifdef CONFIG_GRAB
2016 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2017#endif
2018 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2019 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
85f07f22 2020 "method" },
bdc4796f 2021 { "sameq", OPT_BOOL, {(void*)&same_quality},
85f07f22
FB
2022 "use same video quality as source (implies VBR)" },
2023 /* audio options */
bdc4796f
FB
2024 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2025 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2026 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2027 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2028#ifdef CONFIG_GRAB
2029 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2030#endif
2031 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2032 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
cfcf0ffd 2033 "deinterlace pictures" },
bdc4796f 2034 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
5727b222 2035 "add timings for benchmarking" },
a0663ba4
FB
2036 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2037 "dump each input packet" },
43f1708f 2038 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
85f07f22
FB
2039
2040 { NULL, },
2041};
2042
2043int main(int argc, char **argv)
2044{
2045 int optindex, i;
2046 const char *opt, *arg;
2047 const OptionDef *po;
a38469e1 2048 INT64 ti;
85f07f22
FB
2049
2050 register_all();
2051
a38469e1
FB
2052 /* detect if invoked as player */
2053 i = strlen(argv[0]);
2054 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2055 do_play = 1;
2056
85f07f22
FB
2057 if (argc <= 1)
2058 show_help();
a38469e1
FB
2059
2060 /* parse options */
85f07f22
FB
2061 optindex = 1;
2062 while (optindex < argc) {
2063 opt = argv[optindex++];
2064
2065 if (opt[0] == '-' && opt[1] != '\0') {
2066 po = options;
2067 while (po->name != NULL) {
2068 if (!strcmp(opt + 1, po->name))
2069 break;
2070 po++;
2071 }
2072 if (!po->name) {
2073 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2074 exit(1);
2075 }
2076 arg = NULL;
2077 if (po->flags & HAS_ARG)
2078 arg = argv[optindex++];
2079 if (po->flags & OPT_STRING) {
2080 char *str;
2081 str = strdup(arg);
2082 *po->u.str_arg = str;
2083 } else if (po->flags & OPT_BOOL) {
2084 *po->u.int_arg = 1;
2085 } else {
2086 po->u.func_arg(arg);
2087 }
2088 } else {
a38469e1
FB
2089 if (!do_play) {
2090 opt_output_file(opt);
2091 } else {
2092 opt_input_file(opt);
2093 }
85f07f22
FB
2094 }
2095 }
2096
2097
a38469e1
FB
2098 if (!do_play) {
2099 /* file converter / grab */
85f07f22
FB
2100 if (nb_output_files <= 0) {
2101 fprintf(stderr, "Must supply at least one output file\n");
2102 exit(1);
2103 }
a38469e1
FB
2104
2105 if (nb_input_files == 0) {
2106 prepare_grab();
5727b222 2107 }
a38469e1
FB
2108 } else {
2109 /* player */
2110 if (nb_input_files <= 0) {
2111 fprintf(stderr, "Must supply at least one input file\n");
2112 exit(1);
2113 }
2114 prepare_play();
2115 }
2116
2117 ti = getutime();
2118 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2119 stream_maps, nb_stream_maps);
2120 ti = getutime() - ti;
2121 if (do_benchmark) {
2122 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
85f07f22
FB
2123 }
2124
2125 /* close files */
2126 for(i=0;i<nb_output_files;i++) {
2127 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2128 url_fclose(&output_files[i]->pb);
2129 }
2130 for(i=0;i<nb_input_files;i++) {
2131 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2132 url_fclose(&input_files[i]->pb);
2133 }
2134
2135 return 0;
2136}