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