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