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