more debug output
[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 "framehook.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 #if !defined(INFINITY) && defined(HUGE_VAL)
35 #define INFINITY HUGE_VAL
36 #endif
37
38 #define MAXINT64 INT64_C(0x7fffffffffffffff)
39
40 typedef struct {
41 const char *name;
42 int flags;
43 #define HAS_ARG 0x0001
44 #define OPT_BOOL 0x0002
45 #define OPT_EXPERT 0x0004
46 #define OPT_STRING 0x0008
47 union {
48 void (*func_arg)();
49 int *int_arg;
50 char **str_arg;
51 } u;
52 const char *help;
53 const char *argname;
54 } OptionDef;
55
56 /* select an input stream for an output stream */
57 typedef struct AVStreamMap {
58 int file_index;
59 int stream_index;
60 } AVStreamMap;
61
62 extern const OptionDef options[];
63
64 void show_help(void);
65
66 #define MAX_FILES 20
67
68 static AVFormatContext *input_files[MAX_FILES];
69 static int nb_input_files = 0;
70
71 static AVFormatContext *output_files[MAX_FILES];
72 static int nb_output_files = 0;
73
74 static AVStreamMap stream_maps[MAX_FILES];
75 static int nb_stream_maps;
76
77 static AVInputFormat *file_iformat;
78 static AVOutputFormat *file_oformat;
79 static int frame_width = 160;
80 static int frame_height = 128;
81 static int frame_topBand = 0;
82 static int frame_bottomBand = 0;
83 static int frame_leftBand = 0;
84 static int frame_rightBand = 0;
85 static int frame_rate = 25 * FRAME_RATE_BASE;
86 extern int emulate_frame_rate;
87 static int video_bit_rate = 200*1000;
88 static int video_bit_rate_tolerance = 4000*1000;
89 static int video_qscale = 0;
90 static int video_qmin = 2;
91 static int video_qmax = 31;
92 static int video_qdiff = 3;
93 static float video_qblur = 0.5;
94 static float video_qcomp = 0.5;
95 #if 0 //experimental, (can be removed)
96 static float video_rc_qsquish=1.0;
97 static float video_rc_qmod_amp=0;
98 static int video_rc_qmod_freq=0;
99 #endif
100 static char *video_rc_override_string=NULL;
101 static char *video_rc_eq="tex^qComp";
102 static int video_rc_buffer_size=0;
103 static float video_rc_buffer_aggressivity=1.0;
104 static int video_rc_max_rate=0;
105 static int video_rc_min_rate=0;
106 static float video_rc_initial_cplx=0;
107 static float video_b_qfactor = 1.25;
108 static float video_b_qoffset = 1.25;
109 static float video_i_qfactor = -0.8;
110 static float video_i_qoffset = 0.0;
111 static int me_method = 0;
112 static int video_disable = 0;
113 static int video_codec_id = CODEC_ID_NONE;
114 static int same_quality = 0;
115 static int b_frames = 0;
116 static int use_hq = 0;
117 static int use_4mv = 0;
118 static int do_deinterlace = 0;
119 static int workaround_bugs = FF_BUG_AUTODETECT;
120 static int error_resilience = 2;
121 static int error_concealment = 3;
122 static int dct_algo = 0;
123 static int idct_algo = 0;
124 static int use_part = 0;
125 static int packet_size = 0;
126 static int debug = 0;
127
128 static int gop_size = 12;
129 static int intra_only = 0;
130 static int audio_sample_rate = 44100;
131 static int audio_bit_rate = 64000;
132 static int audio_disable = 0;
133 static int audio_channels = 1;
134 static int audio_codec_id = CODEC_ID_NONE;
135
136 static INT64 recording_time = 0;
137 static int file_overwrite = 0;
138 static char *str_title = NULL;
139 static char *str_author = NULL;
140 static char *str_copyright = NULL;
141 static char *str_comment = NULL;
142 static int do_benchmark = 0;
143 static int do_hex_dump = 0;
144 static int do_play = 0;
145 static int do_psnr = 0;
146 static int do_vstats = 0;
147 static int do_pass = 0;
148 static char *pass_logfilename = NULL;
149 static int audio_stream_copy = 0;
150 static int video_stream_copy = 0;
151
152 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
153
154 #if !defined(CONFIG_AUDIO_OSS) && !defined(CONFIG_AUDIO_BEOS)
155 const char *audio_device = "none";
156 #endif
157 #ifndef CONFIG_VIDEO4LINUX
158 const char *v4l_device = "none";
159 #endif
160
161 typedef struct AVOutputStream {
162 int file_index; /* file index */
163 int index; /* stream index in the output file */
164 int source_index; /* AVInputStream index */
165 AVStream *st; /* stream in the output file */
166 int encoding_needed; /* true if encoding needed for this stream */
167 int frame_number;
168 /* input pts and corresponding output pts
169 for A/V sync */
170 double sync_ipts;
171 double sync_ipts_offset;
172 INT64 sync_opts;
173 /* video only */
174 int video_resample; /* video_resample and video_crop are mutually exclusive */
175 AVPicture pict_tmp; /* temporary image for resampling */
176 ImgReSampleContext *img_resample_ctx; /* for image resampling */
177
178 int video_crop; /* video_resample and video_crop are mutually exclusive */
179 int topBand; /* cropping area sizes */
180 int leftBand;
181
182 /* audio only */
183 int audio_resample;
184 ReSampleContext *resample; /* for audio resampling */
185 FifoBuffer fifo; /* for compression: one audio fifo per codec */
186 FILE *logfile;
187 } AVOutputStream;
188
189 typedef struct AVInputStream {
190 int file_index;
191 int index;
192 AVStream *st;
193 int discard; /* true if stream data should be discarded */
194 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
195 INT64 sample_index; /* current sample */
196 int frame_decoded; /* true if a video or audio frame has been decoded */
197 } AVInputStream;
198
199 typedef struct AVInputFile {
200 int eof_reached; /* true if eof reached */
201 int ist_index; /* index of first stream in ist_table */
202 int buffer_size; /* current total buffer size */
203 int buffer_size_max; /* buffer size at which we consider we can stop
204 buffering */
205 int nb_streams; /* nb streams we are aware of */
206 } AVInputFile;
207
208 #ifndef CONFIG_WIN32
209
210 /* init terminal so that we can grab keys */
211 static struct termios oldtty;
212
213 static void term_exit(void)
214 {
215 tcsetattr (0, TCSANOW, &oldtty);
216 }
217
218 static void term_init(void)
219 {
220 struct termios tty;
221
222 tcgetattr (0, &tty);
223 oldtty = tty;
224
225 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
226 |INLCR|IGNCR|ICRNL|IXON);
227 tty.c_oflag |= OPOST;
228 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
229 tty.c_cflag &= ~(CSIZE|PARENB);
230 tty.c_cflag |= CS8;
231 tty.c_cc[VMIN] = 1;
232 tty.c_cc[VTIME] = 0;
233
234 tcsetattr (0, TCSANOW, &tty);
235
236 atexit(term_exit);
237 #ifdef CONFIG_BEOS_NETSERVER
238 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
239 #endif
240 }
241
242 /* read a key without blocking */
243 static int read_key(void)
244 {
245 int n = 1;
246 unsigned char ch;
247 #ifndef CONFIG_BEOS_NETSERVER
248 struct timeval tv;
249 fd_set rfds;
250
251 FD_ZERO(&rfds);
252 FD_SET(0, &rfds);
253 tv.tv_sec = 0;
254 tv.tv_usec = 0;
255 n = select(1, &rfds, NULL, NULL, &tv);
256 #endif
257 if (n > 0) {
258 n = read(0, &ch, 1);
259 if (n == 1)
260 return ch;
261
262 return n;
263 }
264 return -1;
265 }
266
267 #else
268
269 /* no interactive support */
270 static void term_exit(void)
271 {
272 }
273
274 static void term_init(void)
275 {
276 }
277
278 static int read_key(void)
279 {
280 return 0;
281 }
282
283 #endif
284
285 int read_ffserver_streams(AVFormatContext *s, const char *filename)
286 {
287 int i, err;
288 AVFormatContext *ic;
289
290 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
291 if (err < 0)
292 return err;
293 /* copy stream format */
294 s->nb_streams = ic->nb_streams;
295 for(i=0;i<ic->nb_streams;i++) {
296 AVStream *st;
297
298 st = av_mallocz(sizeof(AVFormatContext));
299 memcpy(st, ic->streams[i], sizeof(AVStream));
300 s->streams[i] = st;
301 }
302
303 av_close_input_file(ic);
304 return 0;
305 }
306
307 #define MAX_AUDIO_PACKET_SIZE 16384
308
309 static void do_audio_out(AVFormatContext *s,
310 AVOutputStream *ost,
311 AVInputStream *ist,
312 unsigned char *buf, int size)
313 {
314 UINT8 *buftmp;
315 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
316 UINT8 audio_out[4*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it - yep really WMA */
317 int size_out, frame_bytes, ret;
318 AVCodecContext *enc;
319
320 enc = &ost->st->codec;
321
322 if (ost->audio_resample) {
323 buftmp = audio_buf;
324 size_out = audio_resample(ost->resample,
325 (short *)buftmp, (short *)buf,
326 size / (ist->st->codec.channels * 2));
327 size_out = size_out * enc->channels * 2;
328 } else {
329 buftmp = buf;
330 size_out = size;
331 }
332
333 /* now encode as many frames as possible */
334 if (enc->frame_size > 1) {
335 /* output resampled raw samples */
336 fifo_write(&ost->fifo, buftmp, size_out,
337 &ost->fifo.wptr);
338
339 frame_bytes = enc->frame_size * 2 * enc->channels;
340
341 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
342 &ost->fifo.rptr) == 0) {
343 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
344 (short *)audio_buf);
345 av_write_frame(s, ost->index, audio_out, ret);
346 }
347 } else {
348 /* output a pcm frame */
349 /* XXX: change encoding codec API to avoid this ? */
350 switch(enc->codec->id) {
351 case CODEC_ID_PCM_S16LE:
352 case CODEC_ID_PCM_S16BE:
353 case CODEC_ID_PCM_U16LE:
354 case CODEC_ID_PCM_U16BE:
355 break;
356 default:
357 size_out = size_out >> 1;
358 break;
359 }
360 ret = avcodec_encode_audio(enc, audio_out, size_out,
361 (short *)buftmp);
362 av_write_frame(s, ost->index, audio_out, ret);
363 }
364 }
365
366 /* write a picture to a raw mux */
367 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
368 int pix_fmt, int w, int h)
369 {
370 UINT8 *buf, *src, *dest;
371 int size, j, i;
372
373 /* XXX: not efficient, should add test if we can take
374 directly the AVPicture */
375 switch(pix_fmt) {
376 case PIX_FMT_YUV420P:
377 size = avpicture_get_size(pix_fmt, w, h);
378 buf = av_malloc(size);
379 if (!buf)
380 return;
381 dest = buf;
382 for(i=0;i<3;i++) {
383 if (i == 1) {
384 w >>= 1;
385 h >>= 1;
386 }
387 src = picture->data[i];
388 for(j=0;j<h;j++) {
389 memcpy(dest, src, w);
390 dest += w;
391 src += picture->linesize[i];
392 }
393 }
394 break;
395 case PIX_FMT_YUV422P:
396 size = (w * h) * 2;
397 buf = av_malloc(size);
398 if (!buf)
399 return;
400 dest = buf;
401 for(i=0;i<3;i++) {
402 if (i == 1) {
403 w >>= 1;
404 }
405 src = picture->data[i];
406 for(j=0;j<h;j++) {
407 memcpy(dest, src, w);
408 dest += w;
409 src += picture->linesize[i];
410 }
411 }
412 break;
413 case PIX_FMT_YUV444P:
414 size = (w * h) * 3;
415 buf = av_malloc(size);
416 if (!buf)
417 return;
418 dest = buf;
419 for(i=0;i<3;i++) {
420 src = picture->data[i];
421 for(j=0;j<h;j++) {
422 memcpy(dest, src, w);
423 dest += w;
424 src += picture->linesize[i];
425 }
426 }
427 break;
428 case PIX_FMT_YUV422:
429 size = (w * h) * 2;
430 buf = av_malloc(size);
431 if (!buf)
432 return;
433 dest = buf;
434 src = picture->data[0];
435 for(j=0;j<h;j++) {
436 memcpy(dest, src, w * 2);
437 dest += w * 2;
438 src += picture->linesize[0];
439 }
440 break;
441 case PIX_FMT_RGB24:
442 case PIX_FMT_BGR24:
443 size = (w * h) * 3;
444 buf = av_malloc(size);
445 if (!buf)
446 return;
447 dest = buf;
448 src = picture->data[0];
449 for(j=0;j<h;j++) {
450 memcpy(dest, src, w * 3);
451 dest += w * 3;
452 src += picture->linesize[0];
453 }
454 break;
455 default:
456 return;
457 }
458 av_write_frame(s, index, buf, size);
459 av_free(buf);
460 }
461
462 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
463 {
464 AVCodecContext *dec;
465 AVPicture *picture2;
466 AVPicture picture_tmp;
467 UINT8 *buf = 0;
468
469 dec = &ist->st->codec;
470
471 /* deinterlace : must be done before any resize */
472 if (do_deinterlace) {
473 int size;
474
475 /* create temporary picture */
476 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
477 buf = av_malloc(size);
478 if (!buf)
479 return;
480
481 picture2 = &picture_tmp;
482 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
483
484 if (avpicture_deinterlace(picture2, picture,
485 dec->pix_fmt, dec->width, dec->height) < 0) {
486 /* if error, do not deinterlace */
487 av_free(buf);
488 buf = NULL;
489 picture2 = picture;
490 }
491 } else {
492 picture2 = picture;
493 }
494
495 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
496
497 if (picture != picture2)
498 *picture = *picture2;
499 *bufp = buf;
500 }
501
502 /* we begin to correct av delay at this threshold */
503 #define AV_DELAY_MAX 0.100
504
505 static void do_video_out(AVFormatContext *s,
506 AVOutputStream *ost,
507 AVInputStream *ist,
508 AVPicture *in_picture,
509 int *frame_size, AVOutputStream *audio_sync)
510 {
511 int nb_frames, i, ret;
512 AVPicture *final_picture, *formatted_picture;
513 AVPicture picture_format_temp, picture_crop_temp;
514 static UINT8 *video_buffer;
515 UINT8 *buf = NULL, *buf1 = NULL;
516 AVCodecContext *enc, *dec;
517
518 #define VIDEO_BUFFER_SIZE (1024*1024)
519
520 enc = &ost->st->codec;
521 dec = &ist->st->codec;
522
523 /* by default, we output a single frame */
524 nb_frames = 1;
525
526 /* NOTE: the A/V sync is always done by considering the audio is
527 the master clock. It is suffisant for transcoding or playing,
528 but not for the general case */
529 if (audio_sync) {
530 /* compute the A-V delay and duplicate/remove frames if needed */
531 double adelta, vdelta, apts, vpts, av_delay;
532
533 if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
534 ost->sync_ipts != AV_NOPTS_VALUE) {
535
536 adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
537 s->pts_num / s->pts_den;
538 apts = audio_sync->sync_ipts + adelta;
539
540 vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
541 s->pts_num / s->pts_den;
542 vpts = ost->sync_ipts + vdelta;
543
544 av_delay = apts - vpts;
545 // printf("delay=%f\n", av_delay);
546 if (av_delay < -AV_DELAY_MAX)
547 nb_frames = 2;
548 else if (av_delay > AV_DELAY_MAX)
549 nb_frames = 0;
550 }
551 } else {
552 double vdelta;
553
554 if (ost->sync_ipts != AV_NOPTS_VALUE) {
555 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
556 if (vdelta < 100 && vdelta > -100) {
557 if (vdelta < -AV_DELAY_MAX)
558 nb_frames = 2;
559 else if (vdelta > AV_DELAY_MAX)
560 nb_frames = 0;
561 } else {
562 ost->sync_ipts_offset -= vdelta;
563 }
564
565 #if 0
566 {
567 static char *action[] = { "drop frame", "copy frame", "dup frame" };
568 printf("Input PTS %12.6f, output PTS %12.6f: %s\n",
569 (double) ost->sync_ipts, (double) ost->st->pts.val * s->pts_num / s->pts_den,
570 action[nb_frames]);
571 }
572 #endif
573 }
574 }
575 if (nb_frames <= 0)
576 return;
577
578 if (!video_buffer)
579 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
580 if (!video_buffer)
581 return;
582
583 /* convert pixel format if needed */
584 if (enc->pix_fmt != dec->pix_fmt) {
585 int size;
586
587 /* create temporary picture */
588 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
589 buf = av_malloc(size);
590 if (!buf)
591 return;
592 formatted_picture = &picture_format_temp;
593 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
594
595 if (img_convert(formatted_picture, enc->pix_fmt,
596 in_picture, dec->pix_fmt,
597 dec->width, dec->height) < 0) {
598 fprintf(stderr, "pixel format conversion not handled\n");
599 goto the_end;
600 }
601 } else {
602 formatted_picture = in_picture;
603 }
604
605 /* XXX: resampling could be done before raw format convertion in
606 some cases to go faster */
607 /* XXX: only works for YUV420P */
608 if (ost->video_resample) {
609 final_picture = &ost->pict_tmp;
610 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
611 } else if (ost->video_crop) {
612 picture_crop_temp.data[0] = formatted_picture->data[0] +
613 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
614
615 picture_crop_temp.data[1] = formatted_picture->data[1] +
616 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
617 (ost->leftBand >> 1);
618
619 picture_crop_temp.data[2] = formatted_picture->data[2] +
620 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
621 (ost->leftBand >> 1);
622
623 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
624 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
625 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
626 final_picture = &picture_crop_temp;
627 } else {
628 final_picture = formatted_picture;
629 }
630 /* duplicates frame if needed */
631 /* XXX: pb because no interleaving */
632 for(i=0;i<nb_frames;i++) {
633 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
634 AVFrame big_picture;
635
636 memset(&big_picture, 0, sizeof(AVFrame));
637 *(AVPicture*)&big_picture= *final_picture;
638
639 /* handles sameq here. This is not correct because it may
640 not be a global option */
641 if (same_quality) {
642 big_picture.quality = ist->st->quality;
643 }else
644 big_picture.quality = ost->st->quality;
645
646 ret = avcodec_encode_video(enc,
647 video_buffer, VIDEO_BUFFER_SIZE,
648 &big_picture);
649 //enc->frame_number = enc->real_pict_num;
650 av_write_frame(s, ost->index, video_buffer, ret);
651 *frame_size = ret;
652 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
653 // enc->frame_number-1, enc->real_pict_num, ret,
654 // enc->pict_type);
655 /* if two pass, output log */
656 if (ost->logfile && enc->stats_out) {
657 fprintf(ost->logfile, "%s", enc->stats_out);
658 }
659 } else {
660 if (s->oformat->flags & AVFMT_RAWPICTURE) {
661 /* raw pictures are written as AVPicture structure to
662 avoid any copies. We support temorarily the older
663 method. */
664 av_write_frame(s, ost->index,
665 (UINT8 *)final_picture, sizeof(AVPicture));
666 } else {
667 write_picture(s, ost->index, final_picture, enc->pix_fmt,
668 enc->width, enc->height);
669 }
670 }
671 ost->frame_number++;
672 }
673 the_end:
674 av_free(buf);
675 av_free(buf1);
676 }
677
678 static double psnr(double d){
679 if(d==0) return INFINITY;
680 return -10.0*log(d)/log(10);
681 }
682
683 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
684 int frame_size)
685 {
686 static FILE *fvstats=NULL;
687 static INT64 total_size = 0;
688 char filename[40];
689 time_t today2;
690 struct tm *today;
691 AVCodecContext *enc;
692 int frame_number;
693 INT64 ti;
694 double ti1, bitrate, avg_bitrate;
695
696 if (!fvstats) {
697 today2 = time(NULL);
698 today = localtime(&today2);
699 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
700 today->tm_min,
701 today->tm_sec);
702 fvstats = fopen(filename,"w");
703 if (!fvstats) {
704 perror("fopen");
705 exit(1);
706 }
707 }
708
709 ti = MAXINT64;
710 enc = &ost->st->codec;
711 total_size += frame_size;
712 if (enc->codec_type == CODEC_TYPE_VIDEO) {
713 frame_number = ost->frame_number;
714 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
715 if (enc->flags&CODEC_FLAG_PSNR)
716 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
717
718 fprintf(fvstats,"f_size= %6d ", frame_size);
719 /* compute pts value */
720 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
721 if (ti1 < 0.01)
722 ti1 = 0.01;
723
724 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
725 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
726 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
727 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
728 fprintf(fvstats,"type= %s\n", enc->coded_frame->key_frame == 1 ? "I" : "P");
729 }
730 }
731
732 void print_report(AVFormatContext **output_files,
733 AVOutputStream **ost_table, int nb_ostreams,
734 int is_last_report)
735 {
736 char buf[1024];
737 AVOutputStream *ost;
738 AVFormatContext *oc, *os;
739 INT64 total_size;
740 AVCodecContext *enc;
741 int frame_number, vid, i;
742 double bitrate, ti1, pts;
743 static INT64 last_time = -1;
744
745 if (!is_last_report) {
746 INT64 cur_time;
747 /* display the report every 0.5 seconds */
748 cur_time = av_gettime();
749 if (last_time == -1) {
750 last_time = cur_time;
751 return;
752 }
753 if ((cur_time - last_time) < 500000)
754 return;
755 last_time = cur_time;
756 }
757
758
759 oc = output_files[0];
760
761 total_size = url_ftell(&oc->pb);
762
763 buf[0] = '\0';
764 ti1 = 1e10;
765 vid = 0;
766 for(i=0;i<nb_ostreams;i++) {
767 ost = ost_table[i];
768 os = output_files[ost->file_index];
769 enc = &ost->st->codec;
770 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
771 sprintf(buf + strlen(buf), "q=%2.1f ",
772 enc->coded_frame->quality);
773 }
774 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
775 frame_number = ost->frame_number;
776 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
777 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
778 if (enc->flags&CODEC_FLAG_PSNR)
779 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
780 vid = 1;
781 }
782 /* compute min output value */
783 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
784 if ((pts < ti1) && (pts > 0))
785 ti1 = pts;
786 }
787 if (ti1 < 0.01)
788 ti1 = 0.01;
789 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
790
791 sprintf(buf + strlen(buf),
792 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
793 (double)total_size / 1024, ti1, bitrate);
794
795 fprintf(stderr, "%s ", buf);
796
797 if (is_last_report) {
798 fprintf(stderr, "\n");
799 } else {
800 fprintf(stderr, "\r");
801 fflush(stderr);
802 }
803 }
804
805 /*
806 * The following code is the main loop of the file converter
807 */
808 static int av_encode(AVFormatContext **output_files,
809 int nb_output_files,
810 AVFormatContext **input_files,
811 int nb_input_files,
812 AVStreamMap *stream_maps, int nb_stream_maps)
813 {
814 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
815 AVFormatContext *is, *os;
816 AVCodecContext *codec, *icodec;
817 AVOutputStream *ost, **ost_table = NULL;
818 AVInputStream *ist, **ist_table = NULL;
819 AVInputFile *file_table;
820 AVFormatContext *stream_no_data;
821 int key;
822
823 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
824 if (!file_table)
825 goto fail;
826
827 /* input stream init */
828 j = 0;
829 for(i=0;i<nb_input_files;i++) {
830 is = input_files[i];
831 file_table[i].ist_index = j;
832 file_table[i].nb_streams = is->nb_streams;
833 j += is->nb_streams;
834 }
835 nb_istreams = j;
836
837 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
838 if (!ist_table)
839 goto fail;
840
841 for(i=0;i<nb_istreams;i++) {
842 ist = av_mallocz(sizeof(AVInputStream));
843 if (!ist)
844 goto fail;
845 ist_table[i] = ist;
846 }
847 j = 0;
848 for(i=0;i<nb_input_files;i++) {
849 is = input_files[i];
850 for(k=0;k<is->nb_streams;k++) {
851 ist = ist_table[j++];
852 ist->st = is->streams[k];
853 ist->file_index = i;
854 ist->index = k;
855 ist->discard = 1; /* the stream is discarded by default
856 (changed later) */
857 }
858 }
859
860 /* output stream init */
861 nb_ostreams = 0;
862 for(i=0;i<nb_output_files;i++) {
863 os = output_files[i];
864 nb_ostreams += os->nb_streams;
865 }
866 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
867 fprintf(stderr, "Number of stream maps must match number of output streams\n");
868 exit(1);
869 }
870
871 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
872 if (!ost_table)
873 goto fail;
874 for(i=0;i<nb_ostreams;i++) {
875 ost = av_mallocz(sizeof(AVOutputStream));
876 if (!ost)
877 goto fail;
878 ost_table[i] = ost;
879 }
880
881 n = 0;
882 for(k=0;k<nb_output_files;k++) {
883 os = output_files[k];
884 for(i=0;i<os->nb_streams;i++) {
885 int found;
886 ost = ost_table[n++];
887 ost->file_index = k;
888 ost->index = i;
889 ost->st = os->streams[i];
890 if (nb_stream_maps > 0) {
891 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
892 stream_maps[n-1].stream_index;
893 } else {
894 /* get corresponding input stream index : we select the first one with the right type */
895 found = 0;
896 for(j=0;j<nb_istreams;j++) {
897 ist = ist_table[j];
898 if (ist->discard &&
899 ist->st->codec.codec_type == ost->st->codec.codec_type) {
900 ost->source_index = j;
901 found = 1;
902 }
903 }
904
905 if (!found) {
906 /* try again and reuse existing stream */
907 for(j=0;j<nb_istreams;j++) {
908 ist = ist_table[j];
909 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
910 ost->source_index = j;
911 found = 1;
912 }
913 }
914 if (!found) {
915 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
916 ost->file_index, ost->index);
917 exit(1);
918 }
919 }
920 }
921 ist = ist_table[ost->source_index];
922 ist->discard = 0;
923 }
924 }
925
926 /* for each output stream, we compute the right encoding parameters */
927 for(i=0;i<nb_ostreams;i++) {
928 ost = ost_table[i];
929 ist = ist_table[ost->source_index];
930
931 codec = &ost->st->codec;
932 icodec = &ist->st->codec;
933
934 if (ost->st->stream_copy) {
935 /* if stream_copy is selected, no need to decode or encode */
936 codec->codec_id = icodec->codec_id;
937 codec->codec_type = icodec->codec_type;
938 codec->codec_tag = icodec->codec_tag;
939 codec->bit_rate = icodec->bit_rate;
940 switch(codec->codec_type) {
941 case CODEC_TYPE_AUDIO:
942 codec->sample_rate = icodec->sample_rate;
943 codec->channels = icodec->channels;
944 break;
945 case CODEC_TYPE_VIDEO:
946 codec->frame_rate = icodec->frame_rate;
947 codec->width = icodec->width;
948 codec->height = icodec->height;
949 break;
950 default:
951 av_abort();
952 }
953 } else {
954 switch(codec->codec_type) {
955 case CODEC_TYPE_AUDIO:
956 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
957 goto fail;
958
959 if (codec->channels == icodec->channels &&
960 codec->sample_rate == icodec->sample_rate) {
961 ost->audio_resample = 0;
962 } else {
963 if (codec->channels != icodec->channels &&
964 icodec->codec_id == CODEC_ID_AC3) {
965 /* Special case for 5:1 AC3 input */
966 /* and mono or stereo output */
967 /* Request specific number of channels */
968 icodec->channels = codec->channels;
969 if (codec->sample_rate == icodec->sample_rate)
970 ost->audio_resample = 0;
971 else {
972 ost->audio_resample = 1;
973 ost->resample = audio_resample_init(codec->channels, icodec->channels,
974 codec->sample_rate,
975 icodec->sample_rate);
976 }
977 /* Request specific number of channels */
978 icodec->channels = codec->channels;
979 } else {
980 ost->audio_resample = 1;
981 ost->resample = audio_resample_init(codec->channels, icodec->channels,
982 codec->sample_rate,
983 icodec->sample_rate);
984 }
985 }
986 ist->decoding_needed = 1;
987 ost->encoding_needed = 1;
988 break;
989 case CODEC_TYPE_VIDEO:
990 if (codec->width == icodec->width &&
991 codec->height == icodec->height &&
992 frame_topBand == 0 &&
993 frame_bottomBand == 0 &&
994 frame_leftBand == 0 &&
995 frame_rightBand == 0)
996 {
997 ost->video_resample = 0;
998 ost->video_crop = 0;
999 } else if ((codec->width == icodec->width -
1000 (frame_leftBand + frame_rightBand)) &&
1001 (codec->height == icodec->height -
1002 (frame_topBand + frame_bottomBand)))
1003 {
1004 ost->video_resample = 0;
1005 ost->video_crop = 1;
1006 ost->topBand = frame_topBand;
1007 ost->leftBand = frame_leftBand;
1008 } else {
1009 UINT8 *buf;
1010 ost->video_resample = 1;
1011 ost->video_crop = 0; // cropping is handled as part of resample
1012 buf = av_malloc((codec->width * codec->height * 3) / 2);
1013 if (!buf)
1014 goto fail;
1015 ost->pict_tmp.data[0] = buf;
1016 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1017 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1018 ost->pict_tmp.linesize[0] = codec->width;
1019 ost->pict_tmp.linesize[1] = codec->width / 2;
1020 ost->pict_tmp.linesize[2] = codec->width / 2;
1021
1022 ost->img_resample_ctx = img_resample_full_init(
1023 ost->st->codec.width, ost->st->codec.height,
1024 ist->st->codec.width, ist->st->codec.height,
1025 frame_topBand, frame_bottomBand,
1026 frame_leftBand, frame_rightBand);
1027 }
1028 ost->encoding_needed = 1;
1029 ist->decoding_needed = 1;
1030 break;
1031 default:
1032 av_abort();
1033 }
1034 /* two pass mode */
1035 if (ost->encoding_needed &&
1036 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1037 char logfilename[1024];
1038 FILE *f;
1039 int size;
1040 char *logbuffer;
1041
1042 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1043 pass_logfilename ?
1044 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1045 if (codec->flags & CODEC_FLAG_PASS1) {
1046 f = fopen(logfilename, "w");
1047 if (!f) {
1048 perror(logfilename);
1049 exit(1);
1050 }
1051 ost->logfile = f;
1052 } else {
1053 /* read the log file */
1054 f = fopen(logfilename, "r");
1055 if (!f) {
1056 perror(logfilename);
1057 exit(1);
1058 }
1059 fseek(f, 0, SEEK_END);
1060 size = ftell(f);
1061 fseek(f, 0, SEEK_SET);
1062 logbuffer = av_malloc(size + 1);
1063 if (!logbuffer) {
1064 fprintf(stderr, "Could not allocate log buffer\n");
1065 exit(1);
1066 }
1067 fread(logbuffer, 1, size, f);
1068 fclose(f);
1069 logbuffer[size] = '\0';
1070 codec->stats_in = logbuffer;
1071 }
1072 }
1073 }
1074 }
1075
1076 /* dump the file output parameters - cannot be done before in case
1077 of stream copy */
1078 for(i=0;i<nb_output_files;i++) {
1079 dump_format(output_files[i], i, output_files[i]->filename, 1);
1080 }
1081
1082 /* dump the stream mapping */
1083 fprintf(stderr, "Stream mapping:\n");
1084 for(i=0;i<nb_ostreams;i++) {
1085 ost = ost_table[i];
1086 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1087 ist_table[ost->source_index]->file_index,
1088 ist_table[ost->source_index]->index,
1089 ost->file_index,
1090 ost->index);
1091 }
1092
1093 /* open each encoder */
1094 for(i=0;i<nb_ostreams;i++) {
1095 ost = ost_table[i];
1096 if (ost->encoding_needed) {
1097 AVCodec *codec;
1098 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1099 if (!codec) {
1100 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1101 ost->file_index, ost->index);
1102 exit(1);
1103 }
1104 if (avcodec_open(&ost->st->codec, codec) < 0) {
1105 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1106 ost->file_index, ost->index);
1107 exit(1);
1108 }
1109 }
1110 }
1111
1112 /* open each decoder */
1113 for(i=0;i<nb_istreams;i++) {
1114 ist = ist_table[i];
1115 if (ist->decoding_needed) {
1116 AVCodec *codec;
1117 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1118 if (!codec) {
1119 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1120 ist->st->codec.codec_id, ist->file_index, ist->index);
1121 exit(1);
1122 }
1123 if (avcodec_open(&ist->st->codec, codec) < 0) {
1124 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1125 ist->file_index, ist->index);
1126 exit(1);
1127 }
1128 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1129 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1130 ist->frame_decoded = 1;
1131 }
1132 }
1133
1134 /* init pts */
1135 for(i=0;i<nb_istreams;i++) {
1136 ist = ist_table[i];
1137 }
1138
1139 /* compute buffer size max (should use a complete heuristic) */
1140 for(i=0;i<nb_input_files;i++) {
1141 file_table[i].buffer_size_max = 2048;
1142 }
1143
1144 /* open files and write file headers */
1145 for(i=0;i<nb_output_files;i++) {
1146 os = output_files[i];
1147 if (av_write_header(os) < 0) {
1148 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1149 ret = -EINVAL;
1150 goto fail;
1151 }
1152 }
1153
1154 #ifndef CONFIG_WIN32
1155 if (!do_play) {
1156 fprintf(stderr, "Press [q] to stop encoding\n");
1157 } else {
1158 fprintf(stderr, "Press [q] to stop playing\n");
1159 }
1160 #endif
1161 term_init();
1162
1163 stream_no_data = 0;
1164 key = -1;
1165
1166 for(;;) {
1167 int file_index, ist_index;
1168 AVPacket pkt;
1169 UINT8 *ptr;
1170 int len;
1171 UINT8 *data_buf;
1172 int data_size, got_picture;
1173 AVPicture picture;
1174 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1175 void *buffer_to_free;
1176 double pts_min;
1177
1178 redo:
1179 /* if 'q' pressed, exits */
1180 if (key) {
1181 /* read_key() returns 0 on EOF */
1182 key = read_key();
1183 if (key == 'q')
1184 break;
1185 }
1186
1187 /* select the stream that we must read now by looking at the
1188 smallest output pts */
1189 file_index = -1;
1190 pts_min = 1e10;
1191 for(i=0;i<nb_ostreams;i++) {
1192 double pts;
1193 ost = ost_table[i];
1194 os = output_files[ost->file_index];
1195 ist = ist_table[ost->source_index];
1196 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1197 if (!file_table[ist->file_index].eof_reached &&
1198 pts < pts_min) {
1199 pts_min = pts;
1200 file_index = ist->file_index;
1201 }
1202 }
1203 /* if none, if is finished */
1204 if (file_index < 0) {
1205 break;
1206 }
1207
1208 /* finish if recording time exhausted */
1209 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1210 break;
1211
1212 /* read a packet from it and output it in the fifo */
1213 is = input_files[file_index];
1214 if (av_read_packet(is, &pkt) < 0) {
1215 file_table[file_index].eof_reached = 1;
1216 continue;
1217 }
1218 if (!pkt.size) {
1219 stream_no_data = is;
1220 } else {
1221 stream_no_data = 0;
1222 }
1223 /* the following test is needed in case new streams appear
1224 dynamically in stream : we ignore them */
1225 if (pkt.stream_index >= file_table[file_index].nb_streams)
1226 goto discard_packet;
1227 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1228 ist = ist_table[ist_index];
1229 if (ist->discard)
1230 goto discard_packet;
1231
1232 if (do_hex_dump) {
1233 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1234 av_hex_dump(pkt.data, pkt.size);
1235 }
1236
1237 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1238
1239 len = pkt.size;
1240 ptr = pkt.data;
1241 pts_set = 0;
1242 while (len > 0) {
1243 INT64 ipts;
1244
1245 ipts = AV_NOPTS_VALUE;
1246
1247 /* decode the packet if needed */
1248 data_buf = NULL; /* fail safe */
1249 data_size = 0;
1250 if (ist->decoding_needed) {
1251 /* NOTE1: we only take into account the PTS if a new
1252 frame has begun (MPEG semantics) */
1253 /* NOTE2: even if the fraction is not initialized,
1254 av_frac_set can be used to set the integer part */
1255 if (ist->frame_decoded &&
1256 pkt.pts != AV_NOPTS_VALUE &&
1257 !pts_set) {
1258 ipts = pkt.pts;
1259 ist->frame_decoded = 0;
1260 pts_set = 1;
1261 }
1262
1263 switch(ist->st->codec.codec_type) {
1264 case CODEC_TYPE_AUDIO:
1265 /* XXX: could avoid copy if PCM 16 bits with same
1266 endianness as CPU */
1267 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1268 ptr, len);
1269 if (ret < 0)
1270 goto fail_decode;
1271 /* Some bug in mpeg audio decoder gives */
1272 /* data_size < 0, it seems they are overflows */
1273 if (data_size <= 0) {
1274 /* no audio frame */
1275 ptr += ret;
1276 len -= ret;
1277 continue;
1278 }
1279 data_buf = (UINT8 *)samples;
1280 break;
1281 case CODEC_TYPE_VIDEO:
1282 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1283 int size;
1284
1285 size = (ist->st->codec.width * ist->st->codec.height);
1286 avpicture_fill(&picture, ptr,
1287 ist->st->codec.pix_fmt,
1288 ist->st->codec.width,
1289 ist->st->codec.height);
1290 ret = len;
1291 } else {
1292 AVFrame big_picture;
1293
1294 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1295 ret = avcodec_decode_video(&ist->st->codec,
1296 &big_picture, &got_picture, ptr, len);
1297 picture= *(AVPicture*)&big_picture;
1298 ist->st->quality= big_picture.quality;
1299 if (ret < 0) {
1300 fail_decode:
1301 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1302 ist->file_index, ist->index);
1303 av_free_packet(&pkt);
1304 goto redo;
1305 }
1306 if (!got_picture) {
1307 /* no picture yet */
1308 ptr += ret;
1309 len -= ret;
1310 continue;
1311 }
1312
1313 }
1314 break;
1315 default:
1316 goto fail_decode;
1317 }
1318 } else {
1319 data_buf = ptr;
1320 data_size = len;
1321 ret = len;
1322 }
1323 ptr += ret;
1324 len -= ret;
1325
1326 buffer_to_free = 0;
1327 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1328 pre_process_video_frame(ist, &picture, &buffer_to_free);
1329 }
1330
1331 ist->frame_decoded = 1;
1332
1333 #if 0
1334 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1335 is the one of the next displayed one */
1336 /* XXX: add mpeg4 too ? */
1337 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1338 if (ist->st->codec.pict_type != B_TYPE) {
1339 INT64 tmp;
1340 tmp = ist->last_ip_pts;
1341 ist->last_ip_pts = ist->frac_pts.val;
1342 ist->frac_pts.val = tmp;
1343 }
1344 }
1345 #endif
1346 /* transcode raw format, encode packets and output them */
1347
1348 for(i=0;i<nb_ostreams;i++) {
1349 int frame_size;
1350
1351 ost = ost_table[i];
1352 if (ost->source_index == ist_index) {
1353 os = output_files[ost->file_index];
1354
1355 if (ipts != AV_NOPTS_VALUE) {
1356 #if 0
1357 printf("%d: got pts=%f %f\n",
1358 i, pkt.pts / 90000.0,
1359 (ipts - ost->st->pts.val) / 90000.0);
1360 #endif
1361 /* set the input output pts pairs */
1362 ost->sync_ipts = (double)ipts * is->pts_num /
1363 is->pts_den;
1364 /* XXX: take into account the various fifos,
1365 in particular for audio */
1366 ost->sync_opts = ost->st->pts.val;
1367 //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt.pts=%lld\n", ipts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt.pts);
1368 } else {
1369 //printf("pts.val=%lld\n", ost->st->pts.val);
1370 }
1371
1372 if (ost->encoding_needed) {
1373 switch(ost->st->codec.codec_type) {
1374 case CODEC_TYPE_AUDIO:
1375 do_audio_out(os, ost, ist, data_buf, data_size);
1376 break;
1377 case CODEC_TYPE_VIDEO:
1378 /* find an audio stream for synchro */
1379 {
1380 int i;
1381 AVOutputStream *audio_sync, *ost1;
1382 audio_sync = NULL;
1383 for(i=0;i<nb_ostreams;i++) {
1384 ost1 = ost_table[i];
1385 if (ost1->file_index == ost->file_index &&
1386 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1387 audio_sync = ost1;
1388 break;
1389 }
1390 }
1391
1392 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1393 if (do_vstats)
1394 do_video_stats(os, ost, frame_size);
1395 }
1396 break;
1397 default:
1398 av_abort();
1399 }
1400 } else {
1401 /* no reencoding needed : output the packet directly */
1402 /* force the input stream PTS */
1403 av_write_frame(os, ost->index, data_buf, data_size);
1404 ost->st->codec.frame_number++;
1405 ost->frame_number++;
1406 }
1407 }
1408 }
1409 av_free(buffer_to_free);
1410 ipts = AV_NOPTS_VALUE;
1411 }
1412 discard_packet:
1413 av_free_packet(&pkt);
1414
1415 /* dump report by using the output first video and audio streams */
1416 print_report(output_files, ost_table, nb_ostreams, 0);
1417 }
1418 term_exit();
1419
1420 /* dump report by using the first video and audio streams */
1421 print_report(output_files, ost_table, nb_ostreams, 1);
1422
1423 /* close each encoder */
1424 for(i=0;i<nb_ostreams;i++) {
1425 ost = ost_table[i];
1426 if (ost->encoding_needed) {
1427 av_freep(&ost->st->codec.stats_in);
1428 avcodec_close(&ost->st->codec);
1429 }
1430 }
1431
1432 /* close each decoder */
1433 for(i=0;i<nb_istreams;i++) {
1434 ist = ist_table[i];
1435 if (ist->decoding_needed) {
1436 avcodec_close(&ist->st->codec);
1437 }
1438 }
1439
1440
1441 /* write the trailer if needed and close file */
1442 for(i=0;i<nb_output_files;i++) {
1443 os = output_files[i];
1444 av_write_trailer(os);
1445 }
1446 /* finished ! */
1447
1448 ret = 0;
1449 fail1:
1450 av_free(file_table);
1451
1452 if (ist_table) {
1453 for(i=0;i<nb_istreams;i++) {
1454 ist = ist_table[i];
1455 av_free(ist);
1456 }
1457 av_free(ist_table);
1458 }
1459 if (ost_table) {
1460 for(i=0;i<nb_ostreams;i++) {
1461 ost = ost_table[i];
1462 if (ost) {
1463 if (ost->logfile) {
1464 fclose(ost->logfile);
1465 ost->logfile = NULL;
1466 }
1467 fifo_free(&ost->fifo); /* works even if fifo is not
1468 initialized but set to zero */
1469 av_free(ost->pict_tmp.data[0]);
1470 if (ost->video_resample)
1471 img_resample_close(ost->img_resample_ctx);
1472 if (ost->audio_resample)
1473 audio_resample_close(ost->resample);
1474 av_free(ost);
1475 }
1476 }
1477 av_free(ost_table);
1478 }
1479 return ret;
1480 fail:
1481 ret = -ENOMEM;
1482 goto fail1;
1483 }
1484
1485 #if 0
1486 int file_read(const char *filename)
1487 {
1488 URLContext *h;
1489 unsigned char buffer[1024];
1490 int len, i;
1491
1492 if (url_open(&h, filename, O_RDONLY) < 0) {
1493 printf("could not open '%s'\n", filename);
1494 return -1;
1495 }
1496 for(;;) {
1497 len = url_read(h, buffer, sizeof(buffer));
1498 if (len <= 0)
1499 break;
1500 for(i=0;i<len;i++) putchar(buffer[i]);
1501 }
1502 url_close(h);
1503 return 0;
1504 }
1505 #endif
1506
1507 void show_licence(void)
1508 {
1509 printf(
1510 "ffmpeg version " FFMPEG_VERSION "\n"
1511 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1512 "This library is free software; you can redistribute it and/or\n"
1513 "modify it under the terms of the GNU Lesser General Public\n"
1514 "License as published by the Free Software Foundation; either\n"
1515 "version 2 of the License, or (at your option) any later version.\n"
1516 "\n"
1517 "This library is distributed in the hope that it will be useful,\n"
1518 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1519 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1520 "Lesser General Public License for more details.\n"
1521 "\n"
1522 "You should have received a copy of the GNU Lesser General Public\n"
1523 "License along with this library; if not, write to the Free Software\n"
1524 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1525 );
1526 exit(1);
1527 }
1528
1529 void opt_format(const char *arg)
1530 {
1531 file_iformat = av_find_input_format(arg);
1532 file_oformat = guess_format(arg, NULL, NULL);
1533 if (!file_iformat && !file_oformat) {
1534 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1535 exit(1);
1536 }
1537 }
1538
1539 void opt_video_bitrate(const char *arg)
1540 {
1541 video_bit_rate = atoi(arg) * 1000;
1542 }
1543
1544 void opt_video_bitrate_tolerance(const char *arg)
1545 {
1546 video_bit_rate_tolerance = atoi(arg) * 1000;
1547 }
1548
1549 void opt_video_bitrate_max(const char *arg)
1550 {
1551 video_rc_max_rate = atoi(arg) * 1000;
1552 }
1553
1554 void opt_video_bitrate_min(const char *arg)
1555 {
1556 video_rc_min_rate = atoi(arg) * 1000;
1557 }
1558
1559 void opt_video_buffer_size(const char *arg)
1560 {
1561 video_rc_buffer_size = atoi(arg) * 1000;
1562 }
1563
1564 void opt_video_rc_eq(char *arg)
1565 {
1566 video_rc_eq = arg;
1567 }
1568
1569 void opt_video_rc_override_string(char *arg)
1570 {
1571 video_rc_override_string = arg;
1572 }
1573
1574
1575 void opt_workaround_bugs(const char *arg)
1576 {
1577 workaround_bugs = atoi(arg);
1578 }
1579
1580 void opt_dct_algo(const char *arg)
1581 {
1582 dct_algo = atoi(arg);
1583 }
1584
1585 void opt_idct_algo(const char *arg)
1586 {
1587 idct_algo = atoi(arg);
1588 }
1589
1590
1591 void opt_error_resilience(const char *arg)
1592 {
1593 error_resilience = atoi(arg);
1594 }
1595
1596 void opt_error_concealment(const char *arg)
1597 {
1598 error_concealment = atoi(arg);
1599 }
1600
1601 void opt_debug(const char *arg)
1602 {
1603 debug = atoi(arg);
1604 }
1605
1606 void opt_frame_rate(const char *arg)
1607 {
1608 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1609 }
1610
1611
1612 void opt_frame_crop_top(const char *arg)
1613 {
1614 frame_topBand = atoi(arg);
1615 if (frame_topBand < 0) {
1616 fprintf(stderr, "Incorrect top crop size\n");
1617 exit(1);
1618 }
1619 if ((frame_topBand % 2) != 0) {
1620 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1621 exit(1);
1622 }
1623 if ((frame_topBand) >= frame_height){
1624 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1625 exit(1);
1626 }
1627 frame_height -= frame_topBand;
1628 }
1629
1630 void opt_frame_crop_bottom(const char *arg)
1631 {
1632 frame_bottomBand = atoi(arg);
1633 if (frame_bottomBand < 0) {
1634 fprintf(stderr, "Incorrect bottom crop size\n");
1635 exit(1);
1636 }
1637 if ((frame_bottomBand % 2) != 0) {
1638 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1639 exit(1);
1640 }
1641 if ((frame_bottomBand) >= frame_height){
1642 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1643 exit(1);
1644 }
1645 frame_height -= frame_bottomBand;
1646 }
1647
1648 void opt_frame_crop_left(const char *arg)
1649 {
1650 frame_leftBand = atoi(arg);
1651 if (frame_leftBand < 0) {
1652 fprintf(stderr, "Incorrect left crop size\n");
1653 exit(1);
1654 }
1655 if ((frame_leftBand % 2) != 0) {
1656 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1657 exit(1);
1658 }
1659 if ((frame_leftBand) >= frame_width){
1660 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1661 exit(1);
1662 }
1663 frame_width -= frame_leftBand;
1664 }
1665
1666 void opt_frame_crop_right(const char *arg)
1667 {
1668 frame_rightBand = atoi(arg);
1669 if (frame_rightBand < 0) {
1670 fprintf(stderr, "Incorrect right crop size\n");
1671 exit(1);
1672 }
1673 if ((frame_rightBand % 2) != 0) {
1674 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1675 exit(1);
1676 }
1677 if ((frame_rightBand) >= frame_width){
1678 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1679 exit(1);
1680 }
1681 frame_width -= frame_rightBand;
1682 }
1683
1684 void opt_frame_size(const char *arg)
1685 {
1686 parse_image_size(&frame_width, &frame_height, arg);
1687 if (frame_width <= 0 || frame_height <= 0) {
1688 fprintf(stderr, "Incorrect frame size\n");
1689 exit(1);
1690 }
1691 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1692 fprintf(stderr, "Frame size must be a multiple of 2\n");
1693 exit(1);
1694 }
1695 }
1696
1697 void opt_gop_size(const char *arg)
1698 {
1699 gop_size = atoi(arg);
1700 }
1701
1702 void opt_b_frames(const char *arg)
1703 {
1704 b_frames = atoi(arg);
1705 if (b_frames > FF_MAX_B_FRAMES) {
1706 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1707 exit(1);
1708 } else if (b_frames < 1) {
1709 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1710 exit(1);
1711 }
1712 }
1713
1714 void opt_qscale(const char *arg)
1715 {
1716 video_qscale = atoi(arg);
1717 if (video_qscale < 0 ||
1718 video_qscale > 31) {
1719 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1720 exit(1);
1721 }
1722 }
1723
1724 void opt_qmin(const char *arg)
1725 {
1726 video_qmin = atoi(arg);
1727 if (video_qmin < 0 ||
1728 video_qmin > 31) {
1729 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1730 exit(1);
1731 }
1732 }
1733
1734 void opt_qmax(const char *arg)
1735 {
1736 video_qmax = atoi(arg);
1737 if (video_qmax < 0 ||
1738 video_qmax > 31) {
1739 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1740 exit(1);
1741 }
1742 }
1743
1744 void opt_qdiff(const char *arg)
1745 {
1746 video_qdiff = atoi(arg);
1747 if (video_qdiff < 0 ||
1748 video_qdiff > 31) {
1749 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1750 exit(1);
1751 }
1752 }
1753
1754 void opt_qblur(const char *arg)
1755 {
1756 video_qblur = atof(arg);
1757 }
1758
1759 void opt_qcomp(const char *arg)
1760 {
1761 video_qcomp = atof(arg);
1762 }
1763
1764 void opt_rc_initial_cplx(const char *arg)
1765 {
1766 video_rc_initial_cplx = atof(arg);
1767 }
1768 void opt_b_qfactor(const char *arg)
1769 {
1770 video_b_qfactor = atof(arg);
1771 }
1772 void opt_i_qfactor(const char *arg)
1773 {
1774 video_i_qfactor = atof(arg);
1775 }
1776 void opt_b_qoffset(const char *arg)
1777 {
1778 video_b_qoffset = atof(arg);
1779 }
1780 void opt_i_qoffset(const char *arg)
1781 {
1782 video_i_qoffset = atof(arg);
1783 }
1784
1785 void opt_packet_size(const char *arg)
1786 {
1787 packet_size= atoi(arg);
1788 }
1789
1790 void opt_audio_bitrate(const char *arg)
1791 {
1792 audio_bit_rate = atoi(arg) * 1000;
1793 }
1794
1795 void opt_audio_rate(const char *arg)
1796 {
1797 audio_sample_rate = atoi(arg);
1798 }
1799
1800 void opt_audio_channels(const char *arg)
1801 {
1802 audio_channels = atoi(arg);
1803 }
1804
1805 void opt_video_device(const char *arg)
1806 {
1807 v4l_device = strdup(arg);
1808 }
1809
1810 void opt_audio_device(const char *arg)
1811 {
1812 audio_device = strdup(arg);
1813 }
1814
1815 void opt_audio_codec(const char *arg)
1816 {
1817 AVCodec *p;
1818
1819 if (!strcmp(arg, "copy")) {
1820 audio_stream_copy = 1;
1821 } else {
1822 p = first_avcodec;
1823 while (p) {
1824 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1825 break;
1826 p = p->next;
1827 }
1828 if (p == NULL) {
1829 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1830 exit(1);
1831 } else {
1832 audio_codec_id = p->id;
1833 }
1834 }
1835 }
1836
1837 void add_frame_hooker(const char *arg)
1838 {
1839 int argc = 0;
1840 char *argv[64];
1841 int i;
1842 char *args = strdup(arg);
1843
1844 argv[0] = strtok(args, " ");
1845 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1846 }
1847
1848 i = frame_hook_add(argc, argv);
1849
1850 if (i != 0) {
1851 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1852 exit(1);
1853 }
1854 }
1855
1856 const char *motion_str[] = {
1857 "zero",
1858 "full",
1859 "log",
1860 "phods",
1861 "epzs",
1862 "x1",
1863 NULL,
1864 };
1865
1866 void opt_motion_estimation(const char *arg)
1867 {
1868 const char **p;
1869 p = motion_str;
1870 for(;;) {
1871 if (!*p) {
1872 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1873 exit(1);
1874 }
1875 if (!strcmp(*p, arg))
1876 break;
1877 p++;
1878 }
1879 me_method = (p - motion_str) + 1;
1880 }
1881
1882 void opt_video_codec(const char *arg)
1883 {
1884 AVCodec *p;
1885
1886 if (!strcmp(arg, "copy")) {
1887 video_stream_copy = 1;
1888 } else {
1889 p = first_avcodec;
1890 while (p) {
1891 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1892 break;
1893 p = p->next;
1894 }
1895 if (p == NULL) {
1896 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1897 exit(1);
1898 } else {
1899 video_codec_id = p->id;
1900 }
1901 }
1902 }
1903
1904 void opt_map(const char *arg)
1905 {
1906 AVStreamMap *m;
1907 const char *p;
1908
1909 p = arg;
1910 m = &stream_maps[nb_stream_maps++];
1911
1912 m->file_index = strtol(arg, (char **)&p, 0);
1913 if (*p)
1914 p++;
1915
1916 m->stream_index = strtol(p, (char **)&p, 0);
1917 }
1918
1919 void opt_recording_time(const char *arg)
1920 {
1921 recording_time = parse_date(arg, 1);
1922 }
1923
1924 void print_error(const char *filename, int err)
1925 {
1926 switch(err) {
1927 case AVERROR_NUMEXPECTED:
1928 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1929 "Use '%%d' to specify the image number:\n"
1930 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1931 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1932 filename);
1933 break;
1934 case AVERROR_INVALIDDATA:
1935 fprintf(stderr, "%s: Error while parsing header\n", filename);
1936 break;
1937 case AVERROR_NOFMT:
1938 fprintf(stderr, "%s: Unknown format\n", filename);
1939 break;
1940 default:
1941 fprintf(stderr, "%s: Error while opening file\n", filename);
1942 break;
1943 }
1944 }
1945
1946 void opt_input_file(const char *filename)
1947 {
1948 AVFormatContext *ic;
1949 AVFormatParameters params, *ap = &params;
1950 int err, i, ret, rfps;
1951
1952 if (!strcmp(filename, "-"))
1953 filename = "pipe:";
1954
1955 /* get default parameters from command line */
1956 memset(ap, 0, sizeof(*ap));
1957 ap->sample_rate = audio_sample_rate;
1958 ap->channels = audio_channels;
1959 ap->frame_rate = frame_rate;
1960 ap->width = frame_width;
1961 ap->height = frame_height;
1962
1963 /* open the input file with generic libav function */
1964 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
1965 if (err < 0) {
1966 print_error(filename, err);
1967 exit(1);
1968 }
1969
1970 /* If not enough info to get the stream parameters, we decode the
1971 first frames to get it. (used in mpeg case for example) */
1972 ret = av_find_stream_info(ic);
1973 if (ret < 0) {
1974 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1975 exit(1);
1976 }
1977
1978 /* update the current parameters so that they match the one of the input stream */
1979 for(i=0;i<ic->nb_streams;i++) {
1980 AVCodecContext *enc = &ic->streams[i]->codec;
1981 switch(enc->codec_type) {
1982 case CODEC_TYPE_AUDIO:
1983 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1984 audio_channels = enc->channels;
1985 audio_sample_rate = enc->sample_rate;
1986 break;
1987 case CODEC_TYPE_VIDEO:
1988 frame_height = enc->height;
1989 frame_width = enc->width;
1990 rfps = ic->streams[i]->r_frame_rate;
1991 enc->workaround_bugs = workaround_bugs;
1992 enc->error_resilience = error_resilience;
1993 enc->error_concealment = error_concealment;
1994 enc->idct_algo= idct_algo;
1995 enc->debug= debug;
1996 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
1997 enc->flags|= CODEC_FLAG_TRUNCATED; */
1998 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO)
1999 enc->flags|= CODEC_FLAG_TRUNCATED;
2000
2001 if (enc->frame_rate != rfps) {
2002 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2003 i, (float)enc->frame_rate / FRAME_RATE_BASE,
2004 (float)rfps / FRAME_RATE_BASE);
2005 }
2006 /* update the current frame rate to match the stream frame rate */
2007 frame_rate = rfps;
2008 break;
2009 default:
2010 av_abort();
2011 }
2012 }
2013
2014 input_files[nb_input_files] = ic;
2015 /* dump the file content */
2016 dump_format(ic, nb_input_files, filename, 0);
2017 nb_input_files++;
2018 file_iformat = NULL;
2019 file_oformat = NULL;
2020 }
2021
2022 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2023 {
2024 int has_video, has_audio, i, j;
2025 AVFormatContext *ic;
2026
2027 has_video = 0;
2028 has_audio = 0;
2029 for(j=0;j<nb_input_files;j++) {
2030 ic = input_files[j];
2031 for(i=0;i<ic->nb_streams;i++) {
2032 AVCodecContext *enc = &ic->streams[i]->codec;
2033 switch(enc->codec_type) {
2034 case CODEC_TYPE_AUDIO:
2035 has_audio = 1;
2036 break;
2037 case CODEC_TYPE_VIDEO:
2038 has_video = 1;
2039 break;
2040 default:
2041 av_abort();
2042 }
2043 }
2044 }
2045 *has_video_ptr = has_video;
2046 *has_audio_ptr = has_audio;
2047 }
2048
2049 void opt_output_file(const char *filename)
2050 {
2051 AVStream *st;
2052 AVFormatContext *oc;
2053 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2054 int codec_id;
2055
2056 if (!strcmp(filename, "-"))
2057 filename = "pipe:";
2058
2059 oc = av_mallocz(sizeof(AVFormatContext));
2060
2061 if (!file_oformat) {
2062 file_oformat = guess_format(NULL, filename, NULL);
2063 if (!file_oformat) {
2064 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2065 filename);
2066 exit(1);
2067 }
2068 }
2069
2070 oc->oformat = file_oformat;
2071
2072 if (!strcmp(file_oformat->name, "ffm") &&
2073 strstart(filename, "http:", NULL)) {
2074 /* special case for files sent to ffserver: we get the stream
2075 parameters from ffserver */
2076 if (read_ffserver_streams(oc, filename) < 0) {
2077 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2078 exit(1);
2079 }
2080 } else {
2081 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2082 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2083
2084 /* disable if no corresponding type found and at least one
2085 input file */
2086 if (nb_input_files > 0) {
2087 check_audio_video_inputs(&input_has_video, &input_has_audio);
2088 if (!input_has_video)
2089 use_video = 0;
2090 if (!input_has_audio)
2091 use_audio = 0;
2092 }
2093
2094 /* manual disable */
2095 if (audio_disable) {
2096 use_audio = 0;
2097 }
2098 if (video_disable) {
2099 use_video = 0;
2100 }
2101
2102 nb_streams = 0;
2103 if (use_video) {
2104 AVCodecContext *video_enc;
2105
2106 st = av_mallocz(sizeof(AVStream));
2107 if (!st) {
2108 fprintf(stderr, "Could not alloc stream\n");
2109 exit(1);
2110 }
2111 avcodec_get_context_defaults(&st->codec);
2112
2113 video_enc = &st->codec;
2114 if (video_stream_copy) {
2115 st->stream_copy = 1;
2116 video_enc->codec_type = CODEC_TYPE_VIDEO;
2117 } else {
2118 char *p;
2119 int i;
2120
2121 codec_id = file_oformat->video_codec;
2122 if (video_codec_id != CODEC_ID_NONE)
2123 codec_id = video_codec_id;
2124
2125 video_enc->codec_id = codec_id;
2126
2127 video_enc->bit_rate = video_bit_rate;
2128 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2129 video_enc->frame_rate = frame_rate;
2130
2131 video_enc->width = frame_width;
2132 video_enc->height = frame_height;
2133
2134 if (!intra_only)
2135 video_enc->gop_size = gop_size;
2136 else
2137 video_enc->gop_size = 0;
2138 if (video_qscale || same_quality) {
2139 video_enc->flags |= CODEC_FLAG_QSCALE;
2140 st->quality = video_qscale;
2141 }
2142
2143 if (use_hq) {
2144 video_enc->flags |= CODEC_FLAG_HQ;
2145 }
2146
2147 if (use_4mv) {
2148 video_enc->flags |= CODEC_FLAG_HQ;
2149 video_enc->flags |= CODEC_FLAG_4MV;
2150 }
2151
2152 if(use_part)
2153 video_enc->flags |= CODEC_FLAG_PART;
2154
2155
2156 if (b_frames) {
2157 if (codec_id != CODEC_ID_MPEG4) {
2158 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
2159 exit(1);
2160 }
2161 video_enc->max_b_frames = b_frames;
2162 video_enc->b_frame_strategy = 0;
2163 video_enc->b_quant_factor = 2.0;
2164 }
2165
2166 video_enc->qmin = video_qmin;
2167 video_enc->qmax = video_qmax;
2168 video_enc->max_qdiff = video_qdiff;
2169 video_enc->qblur = video_qblur;
2170 video_enc->qcompress = video_qcomp;
2171 video_enc->rc_eq = video_rc_eq;
2172 video_enc->debug= debug;
2173
2174 p= video_rc_override_string;
2175 for(i=0; p; i++){
2176 int start, end, q;
2177 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2178 if(e!=3){
2179 fprintf(stderr, "error parsing rc_override\n");
2180 exit(1);
2181 }
2182 video_enc->rc_override=
2183 realloc(video_enc->rc_override, sizeof(RcOverride)*(i+1));
2184 video_enc->rc_override[i].start_frame= start;
2185 video_enc->rc_override[i].end_frame = end;
2186 if(q>0){
2187 video_enc->rc_override[i].qscale= q;
2188 video_enc->rc_override[i].quality_factor= 1.0;
2189 }
2190 else{
2191 video_enc->rc_override[i].qscale= 0;
2192 video_enc->rc_override[i].quality_factor= -q/100.0;
2193 }
2194 p= strchr(p, '/');
2195 if(p) p++;
2196 }
2197 video_enc->rc_override_count=i;
2198
2199 video_enc->rc_max_rate = video_rc_max_rate;
2200 video_enc->rc_min_rate = video_rc_min_rate;
2201 video_enc->rc_buffer_size = video_rc_buffer_size;
2202 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2203 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2204 video_enc->i_quant_factor = video_i_qfactor;
2205 video_enc->b_quant_factor = video_b_qfactor;
2206 video_enc->i_quant_offset = video_i_qoffset;
2207 video_enc->b_quant_offset = video_b_qoffset;
2208 video_enc->dct_algo = dct_algo;
2209 video_enc->idct_algo = idct_algo;
2210 if(packet_size){
2211 video_enc->rtp_mode= 1;
2212 video_enc->rtp_payload_size= packet_size;
2213 }
2214
2215 if (do_psnr)
2216 video_enc->flags|= CODEC_FLAG_PSNR;
2217
2218 video_enc->me_method = me_method;
2219
2220 /* two pass mode */
2221 if (do_pass) {
2222 if (do_pass == 1) {
2223 video_enc->flags |= CODEC_FLAG_PASS1;
2224 } else {
2225 video_enc->flags |= CODEC_FLAG_PASS2;
2226 }
2227 }
2228
2229 /* XXX: need to find a way to set codec parameters */
2230 if (oc->oformat->flags & AVFMT_RGB24) {
2231 video_enc->pix_fmt = PIX_FMT_RGB24;
2232 }
2233 }
2234 oc->streams[nb_streams] = st;
2235 nb_streams++;
2236 }
2237
2238 if (use_audio) {
2239 AVCodecContext *audio_enc;
2240
2241 st = av_mallocz(sizeof(AVStream));
2242 if (!st) {
2243 fprintf(stderr, "Could not alloc stream\n");
2244 exit(1);
2245 }
2246 avcodec_get_context_defaults(&st->codec);
2247
2248 audio_enc = &st->codec;
2249 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2250 if (audio_stream_copy) {
2251 st->stream_copy = 1;
2252 } else {
2253 codec_id = file_oformat->audio_codec;
2254 if (audio_codec_id != CODEC_ID_NONE)
2255 codec_id = audio_codec_id;
2256 audio_enc->codec_id = codec_id;
2257
2258 audio_enc->bit_rate = audio_bit_rate;
2259 audio_enc->sample_rate = audio_sample_rate;
2260 /* For audio codecs other than AC3 we limit */
2261 /* the number of coded channels to stereo */
2262 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2263 audio_enc->channels = 2;
2264 } else
2265 audio_enc->channels = audio_channels;
2266 }
2267 oc->streams[nb_streams] = st;
2268 nb_streams++;
2269 }
2270
2271 oc->nb_streams = nb_streams;
2272
2273 if (!nb_streams) {
2274 fprintf(stderr, "No audio or video streams available\n");
2275 exit(1);
2276 }
2277
2278 if (str_title)
2279 pstrcpy(oc->title, sizeof(oc->title), str_title);
2280 if (str_author)
2281 pstrcpy(oc->author, sizeof(oc->author), str_author);
2282 if (str_copyright)
2283 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2284 if (str_comment)
2285 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2286 }
2287
2288 output_files[nb_output_files++] = oc;
2289
2290 strcpy(oc->filename, filename);
2291
2292 /* check filename in case of an image number is expected */
2293 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2294 if (filename_number_test(oc->filename) < 0) {
2295 print_error(oc->filename, AVERROR_NUMEXPECTED);
2296 exit(1);
2297 }
2298 }
2299
2300 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2301 /* test if it already exists to avoid loosing precious files */
2302 if (!file_overwrite &&
2303 (strchr(filename, ':') == NULL ||
2304 strstart(filename, "file:", NULL))) {
2305 if (url_exist(filename)) {
2306 int c;
2307
2308 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2309 fflush(stdout);
2310 c = getchar();
2311 if (toupper(c) != 'Y') {
2312 fprintf(stderr, "Not overwriting - exiting\n");
2313 exit(1);
2314 }
2315 }
2316 }
2317
2318 /* open the file */
2319 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2320 fprintf(stderr, "Could not open '%s'\n", filename);
2321 exit(1);
2322 }
2323 }
2324
2325 /* reset some options */
2326 file_oformat = NULL;
2327 file_iformat = NULL;
2328 audio_disable = 0;
2329 video_disable = 0;
2330 audio_codec_id = CODEC_ID_NONE;
2331 video_codec_id = CODEC_ID_NONE;
2332 audio_stream_copy = 0;
2333 video_stream_copy = 0;
2334 }
2335
2336 /* prepare dummy protocols for grab */
2337 void prepare_grab(void)
2338 {
2339 int has_video, has_audio, i, j;
2340 AVFormatContext *oc;
2341 AVFormatContext *ic;
2342 AVFormatParameters ap1, *ap = &ap1;
2343
2344 /* see if audio/video inputs are needed */
2345 has_video = 0;
2346 has_audio = 0;
2347 memset(ap, 0, sizeof(*ap));
2348 for(j=0;j<nb_output_files;j++) {
2349 oc = output_files[j];
2350 for(i=0;i<oc->nb_streams;i++) {
2351 AVCodecContext *enc = &oc->streams[i]->codec;
2352 switch(enc->codec_type) {
2353 case CODEC_TYPE_AUDIO:
2354 if (enc->sample_rate > ap->sample_rate)
2355 ap->sample_rate = enc->sample_rate;
2356 if (enc->channels > ap->channels)
2357 ap->channels = enc->channels;
2358 has_audio = 1;
2359 break;
2360 case CODEC_TYPE_VIDEO:
2361 if (enc->width > ap->width)
2362 ap->width = enc->width;
2363 if (enc->height > ap->height)
2364 ap->height = enc->height;
2365 if (enc->frame_rate > ap->frame_rate)
2366 ap->frame_rate = enc->frame_rate;
2367 has_video = 1;
2368 break;
2369 default:
2370 av_abort();
2371 }
2372 }
2373 }
2374
2375 if (has_video == 0 && has_audio == 0) {
2376 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2377 exit(1);
2378 }
2379
2380 if (has_video) {
2381 AVInputFormat *fmt1;
2382 fmt1 = av_find_input_format("video_grab_device");
2383 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2384 fprintf(stderr, "Could not find video grab device\n");
2385 exit(1);
2386 }
2387 /* by now video grab has one stream */
2388 ic->streams[0]->r_frame_rate = ap->frame_rate;
2389 input_files[nb_input_files] = ic;
2390 dump_format(ic, nb_input_files, v4l_device, 0);
2391 nb_input_files++;
2392 }
2393 if (has_audio) {
2394 AVInputFormat *fmt1;
2395 fmt1 = av_find_input_format("audio_device");
2396 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2397 fprintf(stderr, "Could not find audio grab device\n");
2398 exit(1);
2399 }
2400 input_files[nb_input_files] = ic;
2401 dump_format(ic, nb_input_files, audio_device, 0);
2402 nb_input_files++;
2403 }
2404 }
2405
2406 /* open the necessary output devices for playing */
2407 void prepare_play(void)
2408 {
2409 file_iformat = NULL;
2410 file_oformat = guess_format("audio_device", NULL, NULL);
2411 if (!file_oformat) {
2412 fprintf(stderr, "Could not find audio device\n");
2413 exit(1);
2414 }
2415
2416 opt_output_file(audio_device);
2417 }
2418
2419 /* same option as mencoder */
2420 void opt_pass(const char *pass_str)
2421 {
2422 int pass;
2423 pass = atoi(pass_str);
2424 if (pass != 1 && pass != 2) {
2425 fprintf(stderr, "pass number can be only 1 or 2\n");
2426 exit(1);
2427 }
2428 do_pass = pass;
2429 }
2430
2431 #ifndef CONFIG_WIN32
2432 INT64 getutime(void)
2433 {
2434 struct rusage rusage;
2435
2436 getrusage(RUSAGE_SELF, &rusage);
2437 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2438 }
2439 #else
2440 INT64 getutime(void)
2441 {
2442 return av_gettime();
2443 }
2444 #endif
2445
2446 extern int ffm_nopts;
2447
2448 void opt_bitexact(void)
2449 {
2450 avcodec_set_bit_exact();
2451 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2452 ffm_nopts = 1;
2453 }
2454
2455 void show_formats(void)
2456 {
2457 AVInputFormat *ifmt;
2458 AVOutputFormat *ofmt;
2459 URLProtocol *up;
2460 AVCodec *p;
2461 const char **pp;
2462
2463 printf("File formats:\n");
2464 printf(" Encoding:");
2465 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2466 printf(" %s", ofmt->name);
2467 }
2468 printf("\n");
2469 printf(" Decoding:");
2470 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2471 printf(" %s", ifmt->name);
2472 }
2473 printf("\n");
2474
2475 printf("Codecs:\n");
2476 printf(" Encoders:");
2477 for(p = first_avcodec; p != NULL; p = p->next) {
2478 if (p->encode)
2479 printf(" %s", p->name);
2480 }
2481 printf("\n");
2482
2483 printf(" Decoders:");
2484 for(p = first_avcodec; p != NULL; p = p->next) {
2485 if (p->decode)
2486 printf(" %s", p->name);
2487 }
2488 printf("\n");
2489
2490 printf("Supported file protocols:");
2491 for(up = first_protocol; up != NULL; up = up->next)
2492 printf(" %s:", up->name);
2493 printf("\n");
2494
2495 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2496 printf("Motion estimation methods:");
2497 pp = motion_str;
2498 while (*pp) {
2499 printf(" %s", *pp);
2500 if ((pp - motion_str + 1) == ME_ZERO)
2501 printf("(fastest)");
2502 else if ((pp - motion_str + 1) == ME_FULL)
2503 printf("(slowest)");
2504 else if ((pp - motion_str + 1) == ME_EPZS)
2505 printf("(default)");
2506 pp++;
2507 }
2508 printf("\n");
2509 exit(1);
2510 }
2511
2512 void show_help(void)
2513 {
2514 const char *prog;
2515 const OptionDef *po;
2516 int i, expert;
2517
2518 prog = do_play ? "ffplay" : "ffmpeg";
2519
2520 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2521 prog);
2522
2523 if (!do_play) {
2524 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2525 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2526 } else {
2527 printf("usage: ffplay [options] input_file...\n"
2528 "Simple audio player\n");
2529 }
2530
2531 printf("\n"
2532 "Main options are:\n");
2533 for(i=0;i<2;i++) {
2534 if (i == 1)
2535 printf("\nAdvanced options are:\n");
2536 for(po = options; po->name != NULL; po++) {
2537 char buf[64];
2538 expert = (po->flags & OPT_EXPERT) != 0;
2539 if (expert == i) {
2540 strcpy(buf, po->name);
2541 if (po->flags & HAS_ARG) {
2542 strcat(buf, " ");
2543 strcat(buf, po->argname);
2544 }
2545 printf("-%-17s %s\n", buf, po->help);
2546 }
2547 }
2548 }
2549
2550 exit(1);
2551 }
2552
2553 const OptionDef options[] = {
2554 { "L", 0, {(void*)show_licence}, "show license" },
2555 { "h", 0, {(void*)show_help}, "show help" },
2556 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2557 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2558 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2559 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2560 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2561 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2562 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2563 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2564 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2565 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2566 { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2567 { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2568 /* video options */
2569 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2570 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2571 { "em_rate", OPT_BOOL|OPT_EXPERT, {(void*)&emulate_frame_rate}, "makes img reading happen at nominal frame rate" },
2572 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2573 { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2574 { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2575 { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2576 { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2577 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2578 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2579 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2580 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2581 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2582 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2583 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2584 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2585 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2586 { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2587 { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2588 { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2589 { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2590 { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2591 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2592 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2593 { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
2594 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2595 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2596 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2597 { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2598 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2599 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2600 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2601 "method" },
2602 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2603 { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2604 { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
2605 { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_concealment}, "set error concealment", "" },
2606 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2607 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2608 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2609 { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2610 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2611 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
2612 { "sameq", OPT_BOOL, {(void*)&same_quality},
2613 "use same video quality as source (implies VBR)" },
2614 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info" },
2615 /* audio options */
2616 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2617 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2618 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2619 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2620 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2621 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2622 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2623 "deinterlace pictures" },
2624 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2625 "add timings for benchmarking" },
2626 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2627 "dump each input packet" },
2628 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2629 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2630 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2631 { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
2632 { NULL, },
2633 };
2634
2635 int main(int argc, char **argv)
2636 {
2637 int optindex, i;
2638 const char *opt, *arg;
2639 const OptionDef *po;
2640 INT64 ti;
2641
2642 av_register_all();
2643
2644 /* detect if invoked as player */
2645 i = strlen(argv[0]);
2646 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2647 do_play = 1;
2648
2649 if (argc <= 1)
2650 show_help();
2651
2652 /* parse options */
2653 optindex = 1;
2654 while (optindex < argc) {
2655 opt = argv[optindex++];
2656
2657 if (opt[0] == '-' && opt[1] != '\0') {
2658 po = options;
2659 while (po->name != NULL) {
2660 if (!strcmp(opt + 1, po->name))
2661 break;
2662 po++;
2663 }
2664 if (!po->name) {
2665 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2666 exit(1);
2667 }
2668 arg = NULL;
2669 if (po->flags & HAS_ARG) {
2670 arg = argv[optindex++];
2671 if (!arg) {
2672 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
2673 exit(1);
2674 }
2675 }
2676 if (po->flags & OPT_STRING) {
2677 char *str;
2678 str = strdup(arg);
2679 *po->u.str_arg = str;
2680 } else if (po->flags & OPT_BOOL) {
2681 *po->u.int_arg = 1;
2682 } else {
2683 po->u.func_arg(arg);
2684 }
2685 } else {
2686 if (!do_play) {
2687 opt_output_file(opt);
2688 } else {
2689 opt_input_file(opt);
2690 }
2691 }
2692 }
2693
2694
2695 if (!do_play) {
2696 /* file converter / grab */
2697 if (nb_output_files <= 0) {
2698 fprintf(stderr, "Must supply at least one output file\n");
2699 exit(1);
2700 }
2701
2702 if (nb_input_files == 0) {
2703 prepare_grab();
2704 }
2705 } else {
2706 /* player */
2707 if (nb_input_files <= 0) {
2708 fprintf(stderr, "Must supply at least one input file\n");
2709 exit(1);
2710 }
2711 prepare_play();
2712 }
2713
2714 ti = getutime();
2715 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2716 stream_maps, nb_stream_maps);
2717 ti = getutime() - ti;
2718 if (do_benchmark) {
2719 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2720 }
2721
2722 /* close files */
2723 for(i=0;i<nb_output_files;i++) {
2724 /* maybe av_close_output_file ??? */
2725 AVFormatContext *s = output_files[i];
2726 int j;
2727 if (!(s->oformat->flags & AVFMT_NOFILE))
2728 url_fclose(&s->pb);
2729 for(j=0;j<s->nb_streams;j++)
2730 av_free(s->streams[j]);
2731 av_free(s);
2732 }
2733 for(i=0;i<nb_input_files;i++)
2734 av_close_input_file(input_files[i]);
2735
2736 av_free_static();
2737 return 0;
2738 }