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