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