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