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