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