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