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