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