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