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