checksums for the bitexact simple idct
[libav.git] / ffmpeg.c
CommitLineData
85f07f22
FB
1/*
2 * FFmpeg main
bf5af568 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
85f07f22 4 *
bf5af568
FB
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.
85f07f22 9 *
bf5af568 10 * This library is distributed in the hope that it will be useful,
85f07f22 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bf5af568
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
85f07f22 14 *
bf5af568
FB
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
85f07f22 18 */
daf8e955
FB
19#define HAVE_AV_CONFIG_H
20#include "avformat.h"
6dc96cb0 21#include "tick.h"
daf8e955 22
bdc4796f 23#ifndef CONFIG_WIN32
85f07f22
FB
24#include <unistd.h>
25#include <fcntl.h>
26#include <sys/ioctl.h>
85f07f22 27#include <sys/time.h>
85f07f22 28#include <termios.h>
5727b222 29#include <sys/resource.h>
bdc4796f 30#endif
27a5e8b8
FR
31#ifdef __BEOS__
32/* for snooze() */
33#include <OS.h>
34#endif
bf5af568
FB
35#include <time.h>
36#include <ctype.h>
85f07f22 37
85f07f22 38
bdc4796f
FB
39#define MAXINT64 INT64_C(0x7fffffffffffffff)
40
85f07f22
FB
41typedef struct {
42 const char *name;
43 int flags;
44#define HAS_ARG 0x0001
45#define OPT_BOOL 0x0002
46#define OPT_EXPERT 0x0004
47#define OPT_STRING 0x0008
48 union {
49 void (*func_arg)();
50 int *int_arg;
51 char **str_arg;
52 } u;
53 const char *help;
54 const char *argname;
55} OptionDef;
56
57/* select an input stream for an output stream */
58typedef struct AVStreamMap {
59 int file_index;
60 int stream_index;
61} AVStreamMap;
62
63extern const OptionDef options[];
64
65void show_help(void);
66
67#define MAX_FILES 20
68
69static AVFormatContext *input_files[MAX_FILES];
70static int nb_input_files = 0;
71
72static AVFormatContext *output_files[MAX_FILES];
73static int nb_output_files = 0;
74
75static AVStreamMap stream_maps[MAX_FILES];
76static int nb_stream_maps;
77
79fdaa4c
FB
78static AVInputFormat *file_iformat;
79static AVOutputFormat *file_oformat;
85f07f22
FB
80static int frame_width = 160;
81static int frame_height = 128;
82static int frame_rate = 25 * FRAME_RATE_BASE;
3aa102be
MN
83static int video_bit_rate = 200*1000;
84static int video_bit_rate_tolerance = 4000*1000;
85f07f22 85static int video_qscale = 0;
3aa102be
MN
86static int video_qmin = 2;
87static int video_qmax = 31;
9cdd6a24
MN
88static int video_qdiff = 3;
89static float video_qblur = 0.5;
90static float video_qcomp = 0.5;
3aa102be
MN
91static float video_rc_qsquish=1.0;
92static float video_rc_qmod_amp=0;
93static int video_rc_qmod_freq=0;
94static char *video_rc_override_string=NULL;
95static char *video_rc_eq="tex^qComp";
96static int video_rc_buffer_size=0;
97static float video_rc_buffer_aggressivity=1.0;
98static int video_rc_max_rate=0;
99static int video_rc_min_rate=0;
100static float video_rc_initial_cplx=0;
101static float video_b_qfactor = 1.25;
102static float video_b_qoffset = 1.25;
103static float video_i_qfactor = 0.8;
104static float video_i_qoffset = 0.0;
101bea5f 105static int me_method = 0;
85f07f22
FB
106static int video_disable = 0;
107static int video_codec_id = CODEC_ID_NONE;
108static int same_quality = 0;
bc6caae2 109static int b_frames = 0;
e4986da9 110static int use_hq = 0;
29da453b 111static int use_4mv = 0;
cfcf0ffd 112static int do_deinterlace = 0;
fe670d09 113static int workaround_bugs = 0;
463678ac 114static int dct_algo = 0;
85f07f22
FB
115
116static int gop_size = 12;
117static int intra_only = 0;
118static int audio_sample_rate = 44100;
119static int audio_bit_rate = 64000;
120static int audio_disable = 0;
121static int audio_channels = 1;
122static int audio_codec_id = CODEC_ID_NONE;
123
124static INT64 recording_time = 0;
125static int file_overwrite = 0;
126static char *str_title = NULL;
127static char *str_author = NULL;
128static char *str_copyright = NULL;
129static char *str_comment = NULL;
5727b222 130static int do_benchmark = 0;
a0663ba4 131static int do_hex_dump = 0;
a38469e1 132static int do_play = 0;
43f1708f 133static int do_psnr = 0;
ce7c56c2 134static int do_vstats = 0;
0b97443a 135static int mpeg_vcd = 0;
85f07f22 136
bf5af568
FB
137#ifndef CONFIG_AUDIO_OSS
138const char *audio_device = "none";
139#endif
140#ifndef CONFIG_VIDEO4LINUX
141const char *v4l_device = "none";
142#endif
143
85f07f22
FB
144typedef struct AVOutputStream {
145 int file_index; /* file index */
146 int index; /* stream index in the output file */
147 int source_index; /* AVInputStream index */
148 AVStream *st; /* stream in the output file */
149 int encoding_needed; /* true if encoding needed for this stream */
150
85f07f22
FB
151 /* video only */
152 AVPicture pict_tmp; /* temporary image for resizing */
153 int video_resample;
154 ImgReSampleContext *img_resample_ctx; /* for image resampling */
155
156 /* audio only */
157 int audio_resample;
158 ReSampleContext *resample; /* for audio resampling */
159 FifoBuffer fifo; /* for compression: one audio fifo per codec */
160} AVOutputStream;
161
162typedef struct AVInputStream {
163 int file_index;
164 int index;
165 AVStream *st;
166 int discard; /* true if stream data should be discarded */
167 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
6dc96cb0
J
168 Ticker pts_ticker; /* Ticker for PTS calculation */
169 int ticker_inited; /* to signal if the ticker was initialized */
85f07f22 170 INT64 pts; /* current pts */
51bd4565 171 int pts_increment; /* expected pts increment for next packet */
85f07f22
FB
172 int frame_number; /* current frame */
173 INT64 sample_index; /* current sample */
174} AVInputStream;
175
176typedef struct AVInputFile {
177 int eof_reached; /* true if eof reached */
178 int ist_index; /* index of first stream in ist_table */
179 int buffer_size; /* current total buffer size */
180 int buffer_size_max; /* buffer size at which we consider we can stop
181 buffering */
79fdaa4c 182 int nb_streams; /* nb streams we are aware of */
85f07f22
FB
183} AVInputFile;
184
bdc4796f
FB
185#ifndef CONFIG_WIN32
186
85f07f22
FB
187/* init terminal so that we can grab keys */
188static struct termios oldtty;
189
190static void term_exit(void)
191{
192 tcsetattr (0, TCSANOW, &oldtty);
193}
194
195static void term_init(void)
196{
197 struct termios tty;
198
199 tcgetattr (0, &tty);
200 oldtty = tty;
201
202 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
203 |INLCR|IGNCR|ICRNL|IXON);
204 tty.c_oflag |= OPOST;
205 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
206 tty.c_cflag &= ~(CSIZE|PARENB);
207 tty.c_cflag |= CS8;
208 tty.c_cc[VMIN] = 1;
209 tty.c_cc[VTIME] = 0;
210
211 tcsetattr (0, TCSANOW, &tty);
212
213 atexit(term_exit);
214}
215
216/* read a key without blocking */
217static int read_key(void)
218{
219 struct timeval tv;
220 int n;
221 unsigned char ch;
222 fd_set rfds;
223
224 FD_ZERO(&rfds);
225 FD_SET(0, &rfds);
226 tv.tv_sec = 0;
227 tv.tv_usec = 0;
228 n = select(1, &rfds, NULL, NULL, &tv);
229 if (n > 0) {
cb09b2ed
PG
230 n = read(0, &ch, 1);
231 if (n == 1)
85f07f22 232 return ch;
cb09b2ed
PG
233
234 return n;
85f07f22
FB
235 }
236 return -1;
237}
238
a38469e1 239#else
85f07f22 240
a38469e1
FB
241/* no interactive support */
242static void term_exit(void)
85f07f22 243{
a38469e1 244}
85f07f22 245
a38469e1
FB
246static void term_init(void)
247{
248}
85f07f22 249
a38469e1
FB
250static int read_key(void)
251{
cb09b2ed 252 return 0;
85f07f22
FB
253}
254
a38469e1 255#endif
bdc4796f 256
85f07f22
FB
257int read_ffserver_streams(AVFormatContext *s, const char *filename)
258{
79fdaa4c 259 int i, err;
85f07f22
FB
260 AVFormatContext *ic;
261
79fdaa4c
FB
262 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
263 if (err < 0)
264 return err;
85f07f22
FB
265 /* copy stream format */
266 s->nb_streams = ic->nb_streams;
267 for(i=0;i<ic->nb_streams;i++) {
268 AVStream *st;
269 st = av_mallocz(sizeof(AVFormatContext));
270 memcpy(st, ic->streams[i], sizeof(AVStream));
271 s->streams[i] = st;
272 }
273
274 av_close_input_file(ic);
275 return 0;
276}
277
278#define MAX_AUDIO_PACKET_SIZE 16384
279
280static void do_audio_out(AVFormatContext *s,
281 AVOutputStream *ost,
282 AVInputStream *ist,
283 unsigned char *buf, int size)
284{
285 UINT8 *buftmp;
286 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
287 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
288 int size_out, frame_bytes, ret;
289 AVCodecContext *enc;
290
291 enc = &ost->st->codec;
292
293 if (ost->audio_resample) {
294 buftmp = audio_buf;
295 size_out = audio_resample(ost->resample,
296 (short *)buftmp, (short *)buf,
297 size / (ist->st->codec.channels * 2));
298 size_out = size_out * enc->channels * 2;
299 } else {
300 buftmp = buf;
301 size_out = size;
302 }
303
304 /* now encode as many frames as possible */
a0663ba4 305 if (enc->frame_size > 1) {
85f07f22
FB
306 /* output resampled raw samples */
307 fifo_write(&ost->fifo, buftmp, size_out,
308 &ost->fifo.wptr);
309
310 frame_bytes = enc->frame_size * 2 * enc->channels;
311
312 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
313 &ost->fifo.rptr) == 0) {
a0663ba4
FB
314 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
315 (short *)audio_buf);
79fdaa4c 316 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
85f07f22
FB
317 }
318 } else {
a0663ba4
FB
319 /* output a pcm frame */
320 /* XXX: change encoding codec API to avoid this ? */
321 switch(enc->codec->id) {
322 case CODEC_ID_PCM_S16LE:
323 case CODEC_ID_PCM_S16BE:
324 case CODEC_ID_PCM_U16LE:
325 case CODEC_ID_PCM_U16BE:
326 break;
327 default:
328 size_out = size_out >> 1;
329 break;
330 }
331 ret = avcodec_encode_audio(enc, audio_out, size_out,
332 (short *)buftmp);
79fdaa4c 333 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
85f07f22
FB
334 }
335}
336
337/* write a picture to a raw mux */
cfcf0ffd
FB
338static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
339 int pix_fmt, int w, int h)
85f07f22
FB
340{
341 UINT8 *buf, *src, *dest;
342 int size, j, i;
cfcf0ffd 343
85f07f22
FB
344 /* XXX: not efficient, should add test if we can take
345 directly the AVPicture */
cfcf0ffd
FB
346 switch(pix_fmt) {
347 case PIX_FMT_YUV420P:
5b0ad91b 348 size = avpicture_get_size(pix_fmt, w, h);
0f1578af 349 buf = av_malloc(size);
5b0ad91b
J
350 if (!buf)
351 return;
cfcf0ffd
FB
352 dest = buf;
353 for(i=0;i<3;i++) {
354 if (i == 1) {
355 w >>= 1;
356 h >>= 1;
357 }
358 src = picture->data[i];
359 for(j=0;j<h;j++) {
360 memcpy(dest, src, w);
361 dest += w;
362 src += picture->linesize[i];
363 }
364 }
365 break;
366 case PIX_FMT_YUV422P:
367 size = (w * h) * 2;
0f1578af 368 buf = av_malloc(size);
5b0ad91b
J
369 if (!buf)
370 return;
cfcf0ffd
FB
371 dest = buf;
372 for(i=0;i<3;i++) {
373 if (i == 1) {
374 w >>= 1;
375 }
376 src = picture->data[i];
377 for(j=0;j<h;j++) {
378 memcpy(dest, src, w);
379 dest += w;
380 src += picture->linesize[i];
381 }
382 }
383 break;
384 case PIX_FMT_YUV444P:
385 size = (w * h) * 3;
0f1578af 386 buf = av_malloc(size);
5b0ad91b
J
387 if (!buf)
388 return;
cfcf0ffd
FB
389 dest = buf;
390 for(i=0;i<3;i++) {
391 src = picture->data[i];
392 for(j=0;j<h;j++) {
393 memcpy(dest, src, w);
394 dest += w;
395 src += picture->linesize[i];
396 }
397 }
398 break;
399 case PIX_FMT_YUV422:
400 size = (w * h) * 2;
0f1578af 401 buf = av_malloc(size);
5b0ad91b
J
402 if (!buf)
403 return;
cfcf0ffd
FB
404 dest = buf;
405 src = picture->data[0];
406 for(j=0;j<h;j++) {
407 memcpy(dest, src, w * 2);
408 dest += w * 2;
409 src += picture->linesize[0];
85f07f22 410 }
cfcf0ffd
FB
411 break;
412 case PIX_FMT_RGB24:
413 case PIX_FMT_BGR24:
414 size = (w * h) * 3;
0f1578af 415 buf = av_malloc(size);
5b0ad91b
J
416 if (!buf)
417 return;
cfcf0ffd
FB
418 dest = buf;
419 src = picture->data[0];
85f07f22 420 for(j=0;j<h;j++) {
cfcf0ffd
FB
421 memcpy(dest, src, w * 3);
422 dest += w * 3;
423 src += picture->linesize[0];
85f07f22 424 }
cfcf0ffd
FB
425 break;
426 default:
427 return;
85f07f22 428 }
79fdaa4c 429 s->oformat->write_packet(s, index, buf, size, 0);
0f1578af 430 av_free(buf);
85f07f22
FB
431}
432
433
434static void do_video_out(AVFormatContext *s,
435 AVOutputStream *ost,
436 AVInputStream *ist,
ce7c56c2
J
437 AVPicture *picture1,
438 int *frame_size)
85f07f22 439{
6dc96cb0 440 int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
cfcf0ffd
FB
441 AVPicture *picture, *picture2, *pict;
442 AVPicture picture_tmp1, picture_tmp2;
cb09b2ed 443 static UINT8 *video_buffer;
cfcf0ffd
FB
444 UINT8 *buf = NULL, *buf1 = NULL;
445 AVCodecContext *enc, *dec;
85f07f22 446
33a1f1a3 447#define VIDEO_BUFFER_SIZE (1024*1024)
33a1f1a3 448
85f07f22 449 enc = &ost->st->codec;
cfcf0ffd 450 dec = &ist->st->codec;
85f07f22
FB
451
452 frame_number = ist->frame_number;
6dc96cb0 453 dec_frame_rate = ist->st->r_frame_rate;
79fdaa4c 454 // fprintf(stderr, "\n%d", dec_frame_rate);
85f07f22 455 /* first drop frame if needed */
6dc96cb0
J
456 n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
457 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
85f07f22 458 nb = n2 - n1;
ce7c56c2 459 if (nb <= 0)
85f07f22 460 return;
ce7c56c2 461
cb09b2ed
PG
462 if (!video_buffer)
463 video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
c04643a2
PG
464 if(!video_buffer) return;
465
cfcf0ffd
FB
466 /* deinterlace : must be done before any resize */
467 if (do_deinterlace) {
468 int size;
469
470 /* create temporary picture */
471 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
0f1578af 472 buf1 = av_malloc(size);
cfcf0ffd
FB
473 if (!buf1)
474 return;
475
476 picture2 = &picture_tmp2;
477 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
478
479 if (avpicture_deinterlace(picture2, picture1,
480 dec->pix_fmt, dec->width, dec->height) < 0) {
481 /* if error, do not deinterlace */
0f1578af 482 av_free(buf1);
cfcf0ffd
FB
483 buf1 = NULL;
484 picture2 = picture1;
485 }
486 } else {
487 picture2 = picture1;
488 }
489
490 /* convert pixel format if needed */
491 if (enc->pix_fmt != dec->pix_fmt) {
492 int size;
493
494 /* create temporary picture */
495 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
0f1578af 496 buf = av_malloc(size);
cfcf0ffd
FB
497 if (!buf)
498 return;
499 pict = &picture_tmp1;
500 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
501
502 if (img_convert(pict, enc->pix_fmt,
503 picture2, dec->pix_fmt,
504 dec->width, dec->height) < 0) {
505 fprintf(stderr, "pixel format conversion not handled\n");
506 goto the_end;
507 }
508 } else {
509 pict = picture2;
510 }
511
512 /* XXX: resampling could be done before raw format convertion in
513 some cases to go faster */
514 /* XXX: only works for YUV420P */
85f07f22
FB
515 if (ost->video_resample) {
516 picture = &ost->pict_tmp;
517 img_resample(ost->img_resample_ctx, picture, pict);
518 } else {
519 picture = pict;
520 }
6dc96cb0 521 nb=1;
85f07f22
FB
522 /* duplicates frame if needed */
523 /* XXX: pb because no interleaving */
524 for(i=0;i<nb;i++) {
525 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
526 /* handles sameq here. This is not correct because it may
527 not be a global option */
528 if (same_quality) {
cfcf0ffd 529 enc->quality = dec->quality;
85f07f22 530 }
6dc96cb0 531
cfcf0ffd 532 ret = avcodec_encode_video(enc,
33a1f1a3 533 video_buffer, VIDEO_BUFFER_SIZE,
85f07f22 534 picture);
44429457 535 //enc->frame_number = enc->real_pict_num;
79fdaa4c 536 s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
ce7c56c2 537 *frame_size = ret;
a5dc85ef 538 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
44429457 539 // enc->frame_number-1, enc->real_pict_num, ret,
a5dc85ef 540 // enc->pict_type);
85f07f22 541 } else {
cfcf0ffd 542 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
85f07f22
FB
543 }
544 }
cfcf0ffd 545 the_end:
0f1578af
FB
546 av_free(buf);
547 av_free(buf1);
85f07f22
FB
548}
549
ce7c56c2
J
550static void do_video_stats(AVOutputStream *ost,
551 AVInputStream *ist,
552 int frame_size)
553{
554 static FILE *fvstats=NULL;
555 static INT64 total_size = 0;
ce7c56c2 556 char filename[40];
bf5af568
FB
557 time_t today2;
558 struct tm *today;
ce7c56c2
J
559 AVCodecContext *enc;
560 int frame_number;
561 INT64 ti;
562 double ti1, bitrate, avg_bitrate;
563
564 if (!fvstats) {
565 today2 = time(NULL);
566 today = localtime(&today2);
567 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
568 today->tm_min,
569 today->tm_sec);
570 fvstats = fopen(filename,"w");
571 if (!fvstats) {
572 perror("fopen");
573 exit(1);
574 }
575 }
576
577 ti = MAXINT64;
578 enc = &ost->st->codec;
579 total_size += frame_size;
580 if (enc->codec_type == CODEC_TYPE_VIDEO) {
581 frame_number = ist->frame_number;
582 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
583 if (do_psnr)
584 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
585
586 fprintf(fvstats,"f_size= %6d ", frame_size);
587 /* compute min pts value */
588 if (!ist->discard && ist->pts < ti) {
589 ti = ist->pts;
590 }
591 ti1 = (double)ti / 1000000.0;
592 if (ti1 < 0.01)
593 ti1 = 0.01;
594
595 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
596 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
597 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
598 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
599 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
600 }
601
602
603
604}
605
85f07f22
FB
606/*
607 * The following code is the main loop of the file converter
608 */
609static int av_encode(AVFormatContext **output_files,
610 int nb_output_files,
611 AVFormatContext **input_files,
612 int nb_input_files,
613 AVStreamMap *stream_maps, int nb_stream_maps)
614{
bdc4796f 615 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
85f07f22
FB
616 AVFormatContext *is, *os;
617 AVCodecContext *codec, *icodec;
618 AVOutputStream *ost, **ost_table = NULL;
619 AVInputStream *ist, **ist_table = NULL;
620 INT64 min_pts, start_time;
bdc4796f 621 AVInputFile *file_table;
51bd4565 622 AVFormatContext *stream_no_data;
cb09b2ed 623 int key;
bdc4796f 624
51bd4565 625 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
bdc4796f
FB
626 if (!file_table)
627 goto fail;
85f07f22 628
85f07f22
FB
629 /* input stream init */
630 j = 0;
631 for(i=0;i<nb_input_files;i++) {
632 is = input_files[i];
633 file_table[i].ist_index = j;
79fdaa4c 634 file_table[i].nb_streams = is->nb_streams;
85f07f22
FB
635 j += is->nb_streams;
636 }
637 nb_istreams = j;
638
639 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
640 if (!ist_table)
bdc4796f 641 goto fail;
85f07f22
FB
642
643 for(i=0;i<nb_istreams;i++) {
644 ist = av_mallocz(sizeof(AVInputStream));
645 if (!ist)
646 goto fail;
647 ist_table[i] = ist;
648 }
649 j = 0;
650 for(i=0;i<nb_input_files;i++) {
651 is = input_files[i];
652 for(k=0;k<is->nb_streams;k++) {
653 ist = ist_table[j++];
654 ist->st = is->streams[k];
655 ist->file_index = i;
656 ist->index = k;
657 ist->discard = 1; /* the stream is discarded by default
658 (changed later) */
659 }
660 }
661
662 /* output stream init */
663 nb_ostreams = 0;
664 for(i=0;i<nb_output_files;i++) {
665 os = output_files[i];
666 nb_ostreams += os->nb_streams;
0b97443a
J
667 if (mpeg_vcd)
668 os->flags |= AVF_FLAG_VCD;
85f07f22
FB
669 }
670 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
671 fprintf(stderr, "Number of stream maps must match number of output streams\n");
672 exit(1);
673 }
674
675 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
676 if (!ost_table)
677 goto fail;
678 for(i=0;i<nb_ostreams;i++) {
679 ost = av_mallocz(sizeof(AVOutputStream));
680 if (!ost)
681 goto fail;
682 ost_table[i] = ost;
683 }
684
685 n = 0;
686 for(k=0;k<nb_output_files;k++) {
687 os = output_files[k];
688 for(i=0;i<os->nb_streams;i++) {
689 int found;
690 ost = ost_table[n++];
691 ost->file_index = k;
692 ost->index = i;
693 ost->st = os->streams[i];
694 if (nb_stream_maps > 0) {
695 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
696 stream_maps[n-1].stream_index;
697 } else {
698 /* get corresponding input stream index : we select the first one with the right type */
699 found = 0;
700 for(j=0;j<nb_istreams;j++) {
701 ist = ist_table[j];
702 if (ist->discard &&
703 ist->st->codec.codec_type == ost->st->codec.codec_type) {
704 ost->source_index = j;
705 found = 1;
706 }
707 }
708
709 if (!found) {
710 /* try again and reuse existing stream */
711 for(j=0;j<nb_istreams;j++) {
712 ist = ist_table[j];
713 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
714 ost->source_index = j;
715 found = 1;
716 }
717 }
718 if (!found) {
719 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
720 ost->file_index, ost->index);
721 exit(1);
722 }
723 }
724 }
725 ist = ist_table[ost->source_index];
726 ist->discard = 0;
727 }
728 }
729
730 /* dump the stream mapping */
731 fprintf(stderr, "Stream mapping:\n");
732 for(i=0;i<nb_ostreams;i++) {
733 ost = ost_table[i];
734 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
735 ist_table[ost->source_index]->file_index,
736 ist_table[ost->source_index]->index,
737 ost->file_index,
738 ost->index);
739 }
740
741 /* for each output stream, we compute the right encoding parameters */
742 for(i=0;i<nb_ostreams;i++) {
743 ost = ost_table[i];
744 ist = ist_table[ost->source_index];
745
746 codec = &ost->st->codec;
747 icodec = &ist->st->codec;
748
749 switch(codec->codec_type) {
750 case CODEC_TYPE_AUDIO:
751 /* check if same codec with same parameters. If so, no
752 reencoding is needed */
753 if (codec->codec_id == icodec->codec_id &&
754 codec->bit_rate == icodec->bit_rate &&
755 codec->sample_rate == icodec->sample_rate &&
756 codec->channels == icodec->channels) {
757 /* no reencoding */
10bb7023
J
758 /* use the same frame size */
759 codec->frame_size = icodec->frame_size;
760 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
761 // icodec->bit_rate;
762 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
85f07f22
FB
763 } else {
764 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
765 goto fail;
766
767 if (codec->channels == icodec->channels &&
768 codec->sample_rate == icodec->sample_rate) {
769 ost->audio_resample = 0;
770 } else {
e0d2714a
J
771 if (codec->channels != icodec->channels &&
772 icodec->codec_id == CODEC_ID_AC3) {
773 /* Special case for 5:1 AC3 input */
774 /* and mono or stereo output */
6dc96cb0
J
775 /* Request specific number of channels */
776 icodec->channels = codec->channels;
777 if (codec->sample_rate == icodec->sample_rate)
778 ost->audio_resample = 0;
779 else {
780 ost->audio_resample = 1;
781 ost->resample = audio_resample_init(codec->channels, icodec->channels,
782 codec->sample_rate,
783 icodec->sample_rate);
784 }
e0d2714a
J
785 /* Request specific number of channels */
786 icodec->channels = codec->channels;
787 } else {
788 ost->audio_resample = 1;
789 ost->resample = audio_resample_init(codec->channels, icodec->channels,
85f07f22
FB
790 codec->sample_rate,
791 icodec->sample_rate);
e0d2714a 792 }
85f07f22
FB
793 }
794 ist->decoding_needed = 1;
795 ost->encoding_needed = 1;
796 }
797 break;
798 case CODEC_TYPE_VIDEO:
799 /* check if same codec with same parameters. If so, no
800 reencoding is needed */
801 if (codec->codec_id == icodec->codec_id &&
802 codec->bit_rate == icodec->bit_rate &&
803 codec->frame_rate == icodec->frame_rate &&
804 codec->width == icodec->width &&
805 codec->height == icodec->height) {
806 /* no reencoding */
807 } else {
808 if (codec->width == icodec->width &&
809 codec->height == icodec->height) {
810 ost->video_resample = 0;
811 } else {
812 UINT8 *buf;
813 ost->video_resample = 1;
0f1578af 814 buf = av_malloc((codec->width * codec->height * 3) / 2);
85f07f22
FB
815 if (!buf)
816 goto fail;
817 ost->pict_tmp.data[0] = buf;
818 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
819 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
820 ost->pict_tmp.linesize[0] = codec->width;
821 ost->pict_tmp.linesize[1] = codec->width / 2;
822 ost->pict_tmp.linesize[2] = codec->width / 2;
823
824 ost->img_resample_ctx = img_resample_init(
825 ost->st->codec.width, ost->st->codec.height,
826 ist->st->codec.width, ist->st->codec.height);
827 }
828 ost->encoding_needed = 1;
829 ist->decoding_needed = 1;
830 }
831 break;
51bd4565 832 default:
c04643a2 833 av_abort();
85f07f22
FB
834 }
835 }
836
837 /* open each encoder */
838 for(i=0;i<nb_ostreams;i++) {
839 ost = ost_table[i];
840 if (ost->encoding_needed) {
841 AVCodec *codec;
842 codec = avcodec_find_encoder(ost->st->codec.codec_id);
843 if (!codec) {
844 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
845 ost->file_index, ost->index);
846 exit(1);
847 }
848 if (avcodec_open(&ost->st->codec, codec) < 0) {
849 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
850 ost->file_index, ost->index);
851 exit(1);
852 }
853 }
854 }
855
856 /* open each decoder */
857 for(i=0;i<nb_istreams;i++) {
858 ist = ist_table[i];
859 if (ist->decoding_needed) {
860 AVCodec *codec;
861 codec = avcodec_find_decoder(ist->st->codec.codec_id);
862 if (!codec) {
863 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
864 ist->file_index, ist->index);
865 exit(1);
866 }
867 if (avcodec_open(&ist->st->codec, codec) < 0) {
868 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
869 ist->file_index, ist->index);
870 exit(1);
871 }
6dc96cb0
J
872 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
873 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
85f07f22
FB
874 }
875 }
876
877 /* init pts */
878 for(i=0;i<nb_istreams;i++) {
879 ist = ist_table[i];
880 ist->pts = 0;
881 ist->frame_number = 0;
882 }
883
884 /* compute buffer size max (should use a complete heuristic) */
885 for(i=0;i<nb_input_files;i++) {
886 file_table[i].buffer_size_max = 2048;
887 }
888
889 /* open files and write file headers */
890 for(i=0;i<nb_output_files;i++) {
891 os = output_files[i];
79fdaa4c 892 if (av_write_header(os) < 0) {
a38469e1
FB
893 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
894 ret = -EINVAL;
895 goto fail;
896 }
85f07f22
FB
897 }
898
a38469e1
FB
899#ifndef CONFIG_WIN32
900 if (!do_play) {
901 fprintf(stderr, "Press [q] to stop encoding\n");
902 } else {
903 fprintf(stderr, "Press [q] to stop playing\n");
904 }
905#endif
906 term_init();
907
2c4ae653 908 start_time = av_gettime();
85f07f22 909 min_pts = 0;
51bd4565 910 stream_no_data = 0;
cb09b2ed 911 key = -1;
51bd4565 912
85f07f22
FB
913 for(;;) {
914 int file_index, ist_index;
915 AVPacket pkt;
916 UINT8 *ptr;
917 int len;
918 UINT8 *data_buf;
919 int data_size, got_picture;
920 AVPicture picture;
921 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
922
85f07f22 923 redo:
a38469e1 924 /* if 'q' pressed, exits */
cb09b2ed
PG
925 if (key) {
926 /* read_key() returns 0 on EOF */
927 key = read_key();
928 if (key == 'q')
929 break;
930 }
a38469e1
FB
931
932 /* select the input file with the smallest pts */
85f07f22 933 file_index = -1;
bdc4796f 934 min_pts = MAXINT64;
85f07f22
FB
935 for(i=0;i<nb_istreams;i++) {
936 ist = ist_table[i];
51bd4565
PG
937 /* For some reason, the pts_increment code breaks q estimation?!? */
938 if (!ist->discard && !file_table[ist->file_index].eof_reached &&
939 ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
940 min_pts = ist->pts /* + ist->pts_increment */;
85f07f22
FB
941 file_index = ist->file_index;
942 }
943 }
944 /* if none, if is finished */
51bd4565
PG
945 if (file_index < 0) {
946 if (stream_no_data) {
3f027ca7
FB
947#ifndef CONFIG_WIN32 /* no usleep in VisualC ? */
948#ifdef __BEOS__
949 snooze(10 * 1000); /* mmu_man */ /* in microsec */
950#elif defined(__CYGWIN__)
951 usleep(10 * 1000);
952#else
51bd4565
PG
953 struct timespec ts;
954
955 ts.tv_sec = 0;
956 ts.tv_nsec = 1000 * 1000 * 10;
957 nanosleep(&ts, 0);
27a5e8b8 958#endif
bf5af568 959#endif
51bd4565
PG
960 stream_no_data = 0;
961 continue;
962 }
85f07f22 963 break;
51bd4565 964 }
85f07f22
FB
965 /* finish if recording time exhausted */
966 if (recording_time > 0 && min_pts >= recording_time)
967 break;
968 /* read a packet from it and output it in the fifo */
85f07f22
FB
969 is = input_files[file_index];
970 if (av_read_packet(is, &pkt) < 0) {
971 file_table[file_index].eof_reached = 1;
972 continue;
973 }
51bd4565
PG
974 if (!pkt.size) {
975 stream_no_data = is;
976 } else {
977 stream_no_data = 0;
978 }
79fdaa4c
FB
979 /* the following test is needed in case new streams appear
980 dynamically in stream : we ignore them */
981 if (pkt.stream_index >= file_table[file_index].nb_streams)
bf5af568 982 goto discard_packet;
85f07f22
FB
983 ist_index = file_table[file_index].ist_index + pkt.stream_index;
984 ist = ist_table[ist_index];
bf5af568
FB
985 if (ist->discard)
986 goto discard_packet;
85f07f22 987
51bd4565
PG
988 if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
989 ist->frame_number++;
990
a0663ba4
FB
991 if (do_hex_dump) {
992 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
79fdaa4c 993 av_hex_dump(pkt.data, pkt.size);
a0663ba4 994 }
85f07f22
FB
995
996 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
997
998 len = pkt.size;
999 ptr = pkt.data;
1000 while (len > 0) {
1001
1002 /* decode the packet if needed */
1003 data_buf = NULL; /* fail safe */
1004 data_size = 0;
1005 if (ist->decoding_needed) {
1006 switch(ist->st->codec.codec_type) {
1007 case CODEC_TYPE_AUDIO:
a0663ba4
FB
1008 /* XXX: could avoid copy if PCM 16 bits with same
1009 endianness as CPU */
1010 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1011 ptr, len);
1012 if (ret < 0)
1013 goto fail_decode;
afc80f59
J
1014 /* Some bug in mpeg audio decoder gives */
1015 /* data_size < 0, it seems they are overflows */
1016 if (data_size <= 0) {
a0663ba4
FB
1017 /* no audio frame */
1018 ptr += ret;
1019 len -= ret;
1020 continue;
85f07f22 1021 }
a0663ba4 1022 data_buf = (UINT8 *)samples;
85f07f22
FB
1023 break;
1024 case CODEC_TYPE_VIDEO:
1025 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1026 int size;
1027 size = (ist->st->codec.width * ist->st->codec.height);
cfcf0ffd
FB
1028 avpicture_fill(&picture, ptr,
1029 ist->st->codec.pix_fmt,
1030 ist->st->codec.width,
1031 ist->st->codec.height);
85f07f22
FB
1032 ret = len;
1033 } else {
1034 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1035 ret = avcodec_decode_video(&ist->st->codec,
1036 &picture, &got_picture, ptr, len);
1037 if (ret < 0) {
1038 fail_decode:
1039 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1040 ist->file_index, ist->index);
1041 av_free_packet(&pkt);
1042 goto redo;
1043 }
1044 if (!got_picture) {
1045 /* no picture yet */
1046 ptr += ret;
1047 len -= ret;
1048 continue;
1049 }
6dc96cb0 1050
85f07f22
FB
1051 }
1052 break;
1053 default:
1054 goto fail_decode;
1055 }
1056 } else {
1057 data_buf = ptr;
1058 data_size = len;
1059 ret = len;
1060 }
6dc96cb0
J
1061 /* init tickers */
1062 if (!ist->ticker_inited) {
1063 switch (ist->st->codec.codec_type) {
1064 case CODEC_TYPE_AUDIO:
1065 ticker_init(&ist->pts_ticker,
1066 (INT64)ist->st->codec.sample_rate,
1067 (INT64)(1000000));
1068 ist->ticker_inited = 1;
1069 break;
1070 case CODEC_TYPE_VIDEO:
1071 ticker_init(&ist->pts_ticker,
1072 (INT64)ist->st->r_frame_rate,
1073 ((INT64)1000000 * FRAME_RATE_BASE));
1074 ist->ticker_inited = 1;
1075 break;
1076 default:
c04643a2 1077 av_abort();
6dc96cb0
J
1078 }
1079 }
85f07f22
FB
1080 /* update pts */
1081 switch(ist->st->codec.codec_type) {
1082 case CODEC_TYPE_AUDIO:
6dc96cb0 1083 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
cbadbf19 1084 ist->pts = ticker_abs(&ist->pts_ticker, ist->sample_index);
85f07f22 1085 ist->sample_index += data_size / (2 * ist->st->codec.channels);
51bd4565 1086 ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
85f07f22
FB
1087 break;
1088 case CODEC_TYPE_VIDEO:
1089 ist->frame_number++;
6dc96cb0
J
1090 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1091 // ist->st->codec.frame_rate;
cbadbf19 1092 ist->pts = ticker_abs(&ist->pts_ticker, ist->frame_number);
51bd4565
PG
1093 ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
1094 ist->st->codec.frame_rate;
85f07f22 1095 break;
51bd4565 1096 default:
c04643a2 1097 av_abort();
85f07f22
FB
1098 }
1099 ptr += ret;
1100 len -= ret;
1101
1102 /* transcode raw format, encode packets and output them */
1103
1104 for(i=0;i<nb_ostreams;i++) {
ce7c56c2 1105 int frame_size;
85f07f22
FB
1106 ost = ost_table[i];
1107 if (ost->source_index == ist_index) {
1108 os = output_files[ost->file_index];
1109
1110 if (ost->encoding_needed) {
1111 switch(ost->st->codec.codec_type) {
1112 case CODEC_TYPE_AUDIO:
1113 do_audio_out(os, ost, ist, data_buf, data_size);
1114 break;
1115 case CODEC_TYPE_VIDEO:
ce7c56c2
J
1116 do_video_out(os, ost, ist, &picture, &frame_size);
1117 if (do_vstats)
1118 do_video_stats(ost, ist, frame_size);
85f07f22 1119 break;
51bd4565 1120 default:
c04643a2 1121 av_abort();
85f07f22
FB
1122 }
1123 } else {
1124 /* no reencoding needed : output the packet directly */
10bb7023 1125 /* force the input stream PTS */
79fdaa4c 1126 os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
85f07f22
FB
1127 }
1128 }
1129 }
1130 }
bf5af568 1131 discard_packet:
85f07f22
FB
1132 av_free_packet(&pkt);
1133
1134 /* dump report by using the first video and audio streams */
1135 {
1136 char buf[1024];
1137 AVFormatContext *oc;
1138 INT64 total_size, ti;
1139 AVCodecContext *enc;
1140 int frame_number, vid;
1141 double bitrate, ti1;
1142 static INT64 last_time;
1143
1144 if ((min_pts - last_time) >= 500000) {
1145 last_time = min_pts;
1146
1147 oc = output_files[0];
1148
1149 total_size = url_ftell(&oc->pb);
1150
1151 buf[0] = '\0';
bdc4796f 1152 ti = MAXINT64;
85f07f22
FB
1153 vid = 0;
1154 for(i=0;i<nb_ostreams;i++) {
1155 ost = ost_table[i];
1156 enc = &ost->st->codec;
1157 ist = ist_table[ost->source_index];
1158 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1159 frame_number = ist->frame_number;
1160 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1161 frame_number, enc->quality);
43f1708f
J
1162 if (do_psnr)
1163 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
85f07f22
FB
1164 vid = 1;
1165 }
1166 /* compute min pts value */
1167 if (!ist->discard && ist->pts < ti) {
1168 ti = ist->pts;
1169 }
1170 }
1171
bdc4796f 1172 ti1 = (double)ti / 1000000.0;
ce7c56c2
J
1173 if (ti1 < 0.01)
1174 ti1 = 0.01;
85f07f22
FB
1175 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1176
bdc4796f
FB
1177 sprintf(buf + strlen(buf),
1178 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1179 (double)total_size / 1024, ti1, bitrate);
85f07f22 1180
ce7c56c2 1181 fprintf(stderr, "%s \r", buf);
85f07f22
FB
1182 fflush(stderr);
1183 }
1184 }
1185 }
a38469e1 1186 term_exit();
85f07f22
FB
1187
1188 /* dump report by using the first video and audio streams */
1189 {
1190 char buf[1024];
1191 AVFormatContext *oc;
1192 INT64 total_size, ti;
1193 AVCodecContext *enc;
1194 int frame_number, vid;
1195 double bitrate, ti1;
1196
1197 oc = output_files[0];
1198
1199 total_size = url_ftell(&oc->pb);
1200
1201 buf[0] = '\0';
bdc4796f 1202 ti = MAXINT64;
85f07f22
FB
1203 vid = 0;
1204 for(i=0;i<nb_ostreams;i++) {
1205 ost = ost_table[i];
1206 enc = &ost->st->codec;
1207 ist = ist_table[ost->source_index];
1208 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1209 frame_number = ist->frame_number;
1210 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1211 frame_number, enc->quality);
43f1708f
J
1212 if (do_psnr)
1213 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
85f07f22
FB
1214 vid = 1;
1215 }
1216 /* compute min pts value */
1217 if (!ist->discard && ist->pts < ti) {
1218 ti = ist->pts;
1219 }
1220 }
1221
1222 ti1 = ti / 1000000.0;
ce7c56c2
J
1223 if (ti1 < 0.01)
1224 ti1 = 0.01;
85f07f22
FB
1225 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1226
bdc4796f
FB
1227 sprintf(buf + strlen(buf),
1228 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1229 (double)total_size / 1024, ti1, bitrate);
85f07f22 1230
ce7c56c2 1231 fprintf(stderr, "%s \n", buf);
85f07f22
FB
1232 }
1233 /* close each encoder */
1234 for(i=0;i<nb_ostreams;i++) {
1235 ost = ost_table[i];
1236 if (ost->encoding_needed) {
1237 avcodec_close(&ost->st->codec);
1238 }
1239 }
1240
1241 /* close each decoder */
1242 for(i=0;i<nb_istreams;i++) {
1243 ist = ist_table[i];
1244 if (ist->decoding_needed) {
1245 avcodec_close(&ist->st->codec);
1246 }
1247 }
1248
1249
1250 /* write the trailer if needed and close file */
1251 for(i=0;i<nb_output_files;i++) {
1252 os = output_files[i];
79fdaa4c 1253 av_write_trailer(os);
85f07f22
FB
1254 }
1255 /* finished ! */
1256
1257 ret = 0;
1258 fail1:
0f1578af 1259 av_free(file_table);
bdc4796f 1260
85f07f22
FB
1261 if (ist_table) {
1262 for(i=0;i<nb_istreams;i++) {
1263 ist = ist_table[i];
0f1578af 1264 av_free(ist);
85f07f22 1265 }
0f1578af 1266 av_free(ist_table);
85f07f22
FB
1267 }
1268 if (ost_table) {
1269 for(i=0;i<nb_ostreams;i++) {
1270 ost = ost_table[i];
1271 if (ost) {
bf5af568
FB
1272 fifo_free(&ost->fifo); /* works even if fifo is not
1273 initialized but set to zero */
0f1578af 1274 av_free(ost->pict_tmp.data[0]);
85f07f22
FB
1275 if (ost->video_resample)
1276 img_resample_close(ost->img_resample_ctx);
1277 if (ost->audio_resample)
1278 audio_resample_close(ost->resample);
0f1578af 1279 av_free(ost);
85f07f22
FB
1280 }
1281 }
0f1578af 1282 av_free(ost_table);
85f07f22
FB
1283 }
1284 return ret;
1285 fail:
1286 ret = -ENOMEM;
1287 goto fail1;
1288}
1289
1290#if 0
1291int file_read(const char *filename)
1292{
1293 URLContext *h;
1294 unsigned char buffer[1024];
1295 int len, i;
1296
1297 if (url_open(&h, filename, O_RDONLY) < 0) {
1298 printf("could not open '%s'\n", filename);
1299 return -1;
1300 }
1301 for(;;) {
1302 len = url_read(h, buffer, sizeof(buffer));
1303 if (len <= 0)
1304 break;
1305 for(i=0;i<len;i++) putchar(buffer[i]);
1306 }
1307 url_close(h);
1308 return 0;
1309}
1310#endif
1311
1312void show_licence(void)
1313{
1314 printf(
1315 "ffmpeg version " FFMPEG_VERSION "\n"
bf5af568
FB
1316 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1317 "This library is free software; you can redistribute it and/or\n"
1318 "modify it under the terms of the GNU Lesser General Public\n"
1319 "License as published by the Free Software Foundation; either\n"
1320 "version 2 of the License, or (at your option) any later version.\n"
85f07f22 1321 "\n"
bf5af568 1322 "This library is distributed in the hope that it will be useful,\n"
85f07f22 1323 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
bf5af568
FB
1324 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1325 "Lesser General Public License for more details.\n"
85f07f22 1326 "\n"
bf5af568
FB
1327 "You should have received a copy of the GNU Lesser General Public\n"
1328 "License along with this library; if not, write to the Free Software\n"
1329 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
85f07f22
FB
1330 );
1331 exit(1);
1332}
1333
1334void opt_format(const char *arg)
1335{
79fdaa4c
FB
1336 file_iformat = av_find_input_format(arg);
1337 file_oformat = guess_format(arg, NULL, NULL);
1338 if (!file_iformat && !file_oformat) {
1339 fprintf(stderr, "Unknown input or output format: %s\n", arg);
85f07f22
FB
1340 exit(1);
1341 }
85f07f22
FB
1342}
1343
1344void opt_video_bitrate(const char *arg)
1345{
1346 video_bit_rate = atoi(arg) * 1000;
1347}
1348
9cdd6a24
MN
1349void opt_video_bitrate_tolerance(const char *arg)
1350{
1351 video_bit_rate_tolerance = atoi(arg) * 1000;
1352}
1353
3aa102be
MN
1354void opt_video_bitrate_max(const char *arg)
1355{
1356 video_rc_max_rate = atoi(arg) * 1000;
1357}
1358
1359void opt_video_bitrate_min(const char *arg)
1360{
1361 video_rc_min_rate = atoi(arg) * 1000;
1362}
1363
1364void opt_video_rc_eq(char *arg)
1365{
1366 video_rc_eq = arg;
1367}
1368
1369
fe670d09
MN
1370void opt_workaround_bugs(const char *arg)
1371{
1372 workaround_bugs = atoi(arg);
1373}
1374
463678ac
MN
1375void opt_dct_algo(const char *arg)
1376{
1377 dct_algo = atoi(arg);
1378}
1379
85f07f22
FB
1380void opt_frame_rate(const char *arg)
1381{
1382 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1383}
1384
1385void opt_frame_size(const char *arg)
1386{
1387 parse_image_size(&frame_width, &frame_height, arg);
1388 if (frame_width <= 0 || frame_height <= 0) {
1389 fprintf(stderr, "Incorrect frame size\n");
1390 exit(1);
1391 }
1392 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1393 fprintf(stderr, "Frame size must be a multiple of 2\n");
1394 exit(1);
1395 }
1396}
1397
1398void opt_gop_size(const char *arg)
1399{
1400 gop_size = atoi(arg);
1401}
1402
bc6caae2
J
1403void opt_b_frames(const char *arg)
1404{
1405 b_frames = atoi(arg);
1406 if (b_frames > FF_MAX_B_FRAMES) {
1407 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1408 exit(1);
1409 } else if (b_frames < 1) {
1410 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1411 exit(1);
1412 }
1413}
1414
85f07f22
FB
1415void opt_qscale(const char *arg)
1416{
1417 video_qscale = atoi(arg);
1418 if (video_qscale < 0 ||
1419 video_qscale > 31) {
1420 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1421 exit(1);
1422 }
1423}
1424
9cdd6a24
MN
1425void opt_qmin(const char *arg)
1426{
1427 video_qmin = atoi(arg);
1428 if (video_qmin < 0 ||
1429 video_qmin > 31) {
1430 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1431 exit(1);
1432 }
1433}
1434
1435void opt_qmax(const char *arg)
1436{
1437 video_qmax = atoi(arg);
1438 if (video_qmax < 0 ||
1439 video_qmax > 31) {
1440 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1441 exit(1);
1442 }
1443}
1444
1445void opt_qdiff(const char *arg)
1446{
1447 video_qdiff = atoi(arg);
1448 if (video_qdiff < 0 ||
1449 video_qdiff > 31) {
1450 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1451 exit(1);
1452 }
1453}
1454
1455void opt_qblur(const char *arg)
1456{
1457 video_qblur = atof(arg);
1458}
1459
1460void opt_qcomp(const char *arg)
1461{
1462 video_qcomp = atof(arg);
1463}
85f07f22
FB
1464
1465void opt_audio_bitrate(const char *arg)
1466{
1467 audio_bit_rate = atoi(arg) * 1000;
1468}
1469
1470void opt_audio_rate(const char *arg)
1471{
1472 audio_sample_rate = atoi(arg);
1473}
1474
1475void opt_audio_channels(const char *arg)
1476{
1477 audio_channels = atoi(arg);
1478}
1479
1480void opt_video_device(const char *arg)
1481{
1482 v4l_device = strdup(arg);
1483}
1484
1485void opt_audio_device(const char *arg)
1486{
1487 audio_device = strdup(arg);
1488}
1489
1490void opt_audio_codec(const char *arg)
1491{
1492 AVCodec *p;
1493
1494 p = first_avcodec;
1495 while (p) {
1496 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1497 break;
1498 p = p->next;
1499 }
1500 if (p == NULL) {
1501 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1502 exit(1);
1503 } else {
1504 audio_codec_id = p->id;
1505 }
1506}
1507
1508const char *motion_str[] = {
1509 "zero",
1510 "full",
1511 "log",
1512 "phods",
7084c149
MN
1513 "epzs",
1514 "x1",
85f07f22
FB
1515 NULL,
1516};
1517
1518void opt_motion_estimation(const char *arg)
1519{
1520 const char **p;
1521 p = motion_str;
1522 for(;;) {
1523 if (!*p) {
1524 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1525 exit(1);
1526 }
1527 if (!strcmp(*p, arg))
1528 break;
1529 p++;
1530 }
101bea5f 1531 me_method = (p - motion_str) + 1;
85f07f22
FB
1532}
1533
1534void opt_video_codec(const char *arg)
1535{
1536 AVCodec *p;
1537
1538 p = first_avcodec;
1539 while (p) {
1540 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1541 break;
1542 p = p->next;
1543 }
1544 if (p == NULL) {
1545 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1546 exit(1);
1547 } else {
1548 video_codec_id = p->id;
1549 }
1550}
1551
1552void opt_map(const char *arg)
1553{
1554 AVStreamMap *m;
1555 const char *p;
1556
1557 p = arg;
1558 m = &stream_maps[nb_stream_maps++];
1559
1560 m->file_index = strtol(arg, (char **)&p, 0);
1561 if (*p)
1562 p++;
a5dc85ef
J
1563
1564 m->stream_index = strtol(p, (char **)&p, 0);
85f07f22
FB
1565}
1566
1567void opt_recording_time(const char *arg)
1568{
1569 recording_time = parse_date(arg, 1);
1570}
1571
79fdaa4c 1572void print_error(const char *filename, int err)
919f448d 1573{
79fdaa4c
FB
1574 switch(err) {
1575 case AVERROR_NUMEXPECTED:
919f448d
FB
1576 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1577 "Use '%%d' to specify the image number:\n"
1578 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1579 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1580 filename);
79fdaa4c
FB
1581 break;
1582 case AVERROR_INVALIDDATA:
1583 fprintf(stderr, "%s: Error while parsing header\n", filename);
1584 break;
1585 case AVERROR_NOFMT:
1586 fprintf(stderr, "%s: Unknown format\n", filename);
1587 break;
1588 default:
1589 fprintf(stderr, "%s: Error while opening file\n", filename);
1590 break;
919f448d
FB
1591 }
1592}
85f07f22
FB
1593
1594void opt_input_file(const char *filename)
1595{
1596 AVFormatContext *ic;
1597 AVFormatParameters params, *ap = &params;
6dc96cb0 1598 int err, i, ret, rfps;
85f07f22 1599
85f07f22 1600 /* get default parameters from command line */
79fdaa4c
FB
1601 memset(ap, 0, sizeof(*ap));
1602 ap->sample_rate = audio_sample_rate;
1603 ap->channels = audio_channels;
1604 ap->frame_rate = frame_rate;
1605 ap->width = frame_width;
1606 ap->height = frame_height;
1607
1608 /* open the input file with generic libav function */
1609 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
85f07f22 1610 if (err < 0) {
79fdaa4c 1611 print_error(filename, err);
85f07f22
FB
1612 exit(1);
1613 }
1614
79fdaa4c
FB
1615 /* If not enough info to get the stream parameters, we decode the
1616 first frames to get it. (used in mpeg case for example) */
1617 ret = av_find_stream_info(ic);
85f07f22
FB
1618 if (ret < 0) {
1619 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1620 exit(1);
1621 }
1622
1623 /* update the current parameters so that they match the one of the input stream */
1624 for(i=0;i<ic->nb_streams;i++) {
1625 AVCodecContext *enc = &ic->streams[i]->codec;
1626 switch(enc->codec_type) {
1627 case CODEC_TYPE_AUDIO:
e0d2714a 1628 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
85f07f22
FB
1629 audio_channels = enc->channels;
1630 audio_sample_rate = enc->sample_rate;
1631 break;
1632 case CODEC_TYPE_VIDEO:
1633 frame_height = enc->height;
1634 frame_width = enc->width;
79fdaa4c 1635 rfps = ic->streams[i]->r_frame_rate;
fe670d09 1636 enc->workaround_bugs = workaround_bugs;
79fdaa4c 1637 if (enc->frame_rate != rfps) {
6dc96cb0
J
1638 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
1639 i, (float)enc->frame_rate / FRAME_RATE_BASE,
1640 (float)rfps / FRAME_RATE_BASE);
79fdaa4c 1641 }
bf5af568
FB
1642 /* update the current frame rate to match the stream frame rate */
1643 frame_rate = rfps;
85f07f22 1644 break;
51bd4565 1645 default:
c04643a2 1646 av_abort();
85f07f22
FB
1647 }
1648 }
1649
1650 input_files[nb_input_files] = ic;
1651 /* dump the file content */
1652 dump_format(ic, nb_input_files, filename, 0);
1653 nb_input_files++;
79fdaa4c
FB
1654 file_iformat = NULL;
1655 file_oformat = NULL;
85f07f22
FB
1656}
1657
919f448d
FB
1658void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1659{
1660 int has_video, has_audio, i, j;
1661 AVFormatContext *ic;
1662
1663 has_video = 0;
1664 has_audio = 0;
1665 for(j=0;j<nb_input_files;j++) {
1666 ic = input_files[j];
1667 for(i=0;i<ic->nb_streams;i++) {
1668 AVCodecContext *enc = &ic->streams[i]->codec;
1669 switch(enc->codec_type) {
1670 case CODEC_TYPE_AUDIO:
1671 has_audio = 1;
1672 break;
1673 case CODEC_TYPE_VIDEO:
1674 has_video = 1;
1675 break;
51bd4565 1676 default:
c04643a2 1677 av_abort();
919f448d
FB
1678 }
1679 }
1680 }
1681 *has_video_ptr = has_video;
1682 *has_audio_ptr = has_audio;
1683}
1684
85f07f22
FB
1685void opt_output_file(const char *filename)
1686{
1687 AVStream *st;
1688 AVFormatContext *oc;
919f448d 1689 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
85f07f22
FB
1690 int codec_id;
1691
1692 if (!strcmp(filename, "-"))
1693 filename = "pipe:";
1694
1695 oc = av_mallocz(sizeof(AVFormatContext));
1696
79fdaa4c
FB
1697 if (!file_oformat) {
1698 file_oformat = guess_format(NULL, filename, NULL);
1699 if (!file_oformat) {
1700 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
1701 filename);
1702 exit(1);
1703 }
85f07f22
FB
1704 }
1705
79fdaa4c 1706 oc->oformat = file_oformat;
85f07f22 1707
79fdaa4c 1708 if (!strcmp(file_oformat->name, "ffm") &&
85f07f22
FB
1709 strstart(filename, "http:", NULL)) {
1710 /* special case for files sent to ffserver: we get the stream
1711 parameters from ffserver */
1712 if (read_ffserver_streams(oc, filename) < 0) {
1713 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1714 exit(1);
1715 }
1716 } else {
79fdaa4c
FB
1717 use_video = file_oformat->video_codec != CODEC_ID_NONE;
1718 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
919f448d 1719
e30a2846
FB
1720 /* disable if no corresponding type found and at least one
1721 input file */
1722 if (nb_input_files > 0) {
1723 check_audio_video_inputs(&input_has_video, &input_has_audio);
1724 if (!input_has_video)
1725 use_video = 0;
1726 if (!input_has_audio)
1727 use_audio = 0;
1728 }
919f448d
FB
1729
1730 /* manual disable */
85f07f22
FB
1731 if (audio_disable) {
1732 use_audio = 0;
1733 }
1734 if (video_disable) {
1735 use_video = 0;
1736 }
1737
1738 nb_streams = 0;
1739 if (use_video) {
1740 AVCodecContext *video_enc;
1741
1742 st = av_mallocz(sizeof(AVStream));
1743 if (!st) {
1744 fprintf(stderr, "Could not alloc stream\n");
1745 exit(1);
1746 }
1747 video_enc = &st->codec;
1748
79fdaa4c 1749 codec_id = file_oformat->video_codec;
85f07f22
FB
1750 if (video_codec_id != CODEC_ID_NONE)
1751 codec_id = video_codec_id;
1752
1753 video_enc->codec_id = codec_id;
1754 video_enc->codec_type = CODEC_TYPE_VIDEO;
1755
1756 video_enc->bit_rate = video_bit_rate;
9cdd6a24 1757 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
85f07f22
FB
1758 video_enc->frame_rate = frame_rate;
1759
1760 video_enc->width = frame_width;
1761 video_enc->height = frame_height;
1762 if (!intra_only)
1763 video_enc->gop_size = gop_size;
1764 else
1765 video_enc->gop_size = 0;
1766 if (video_qscale || same_quality) {
1767 video_enc->flags |= CODEC_FLAG_QSCALE;
1768 video_enc->quality = video_qscale;
1769 }
9cdd6a24 1770
e4986da9
J
1771 if (use_hq) {
1772 video_enc->flags |= CODEC_FLAG_HQ;
1773 }
1774
29da453b
J
1775 if (use_4mv) {
1776 video_enc->flags |= CODEC_FLAG_HQ;
1777 video_enc->flags |= CODEC_FLAG_4MV;
1778 }
bc6caae2
J
1779
1780 if (b_frames) {
1781 if (codec_id != CODEC_ID_MPEG4) {
1782 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
1783 exit(1);
1784 }
1785 video_enc->max_b_frames = b_frames;
1786 video_enc->b_frame_strategy = 0;
1787 video_enc->b_quant_factor = 2.0;
1788 }
1789
1790 video_enc->qmin = video_qmin;
1791 video_enc->qmax = video_qmax;
1792 video_enc->max_qdiff = video_qdiff;
1793 video_enc->qblur = video_qblur;
1794 video_enc->qcompress = video_qcomp;
3aa102be
MN
1795 video_enc->rc_eq = video_rc_eq;
1796 video_enc->rc_max_rate = video_rc_max_rate;
1797 video_enc->rc_min_rate = video_rc_min_rate;
1798 video_enc->rc_buffer_size = video_rc_buffer_size;
1799 video_enc->i_quant_factor = video_i_qfactor;
1800 video_enc->b_quant_factor = video_b_qfactor;
1801 video_enc->i_quant_offset = video_i_qoffset;
1802 video_enc->b_quant_offset = video_b_qoffset;
463678ac 1803 video_enc->dct_algo = dct_algo;
9cdd6a24 1804
43f1708f
J
1805 if (do_psnr)
1806 video_enc->get_psnr = 1;
1807 else
1808 video_enc->get_psnr = 0;
e4986da9 1809
101bea5f 1810 video_enc->me_method = me_method;
e4986da9 1811
cfcf0ffd 1812 /* XXX: need to find a way to set codec parameters */
79fdaa4c 1813 if (oc->oformat->flags & AVFMT_RGB24) {
cfcf0ffd
FB
1814 video_enc->pix_fmt = PIX_FMT_RGB24;
1815 }
85f07f22
FB
1816
1817 oc->streams[nb_streams] = st;
1818 nb_streams++;
1819 }
1820
1821 if (use_audio) {
1822 AVCodecContext *audio_enc;
1823
1824 st = av_mallocz(sizeof(AVStream));
1825 if (!st) {
1826 fprintf(stderr, "Could not alloc stream\n");
1827 exit(1);
1828 }
1829 audio_enc = &st->codec;
79fdaa4c 1830 codec_id = file_oformat->audio_codec;
85f07f22
FB
1831 if (audio_codec_id != CODEC_ID_NONE)
1832 codec_id = audio_codec_id;
1833 audio_enc->codec_id = codec_id;
1834 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1835
1836 audio_enc->bit_rate = audio_bit_rate;
1837 audio_enc->sample_rate = audio_sample_rate;
e0d2714a
J
1838 /* For audio codecs other than AC3 we limit */
1839 /* the number of coded channels to stereo */
1840 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1841 audio_enc->channels = 2;
1842 } else
1843 audio_enc->channels = audio_channels;
85f07f22
FB
1844 oc->streams[nb_streams] = st;
1845 nb_streams++;
1846 }
1847
1848 oc->nb_streams = nb_streams;
1849
1850 if (!nb_streams) {
919f448d 1851 fprintf(stderr, "No audio or video streams available\n");
85f07f22
FB
1852 exit(1);
1853 }
1854
1855 if (str_title)
79fdaa4c 1856 pstrcpy(oc->title, sizeof(oc->title), str_title);
85f07f22 1857 if (str_author)
79fdaa4c 1858 pstrcpy(oc->author, sizeof(oc->author), str_author);
85f07f22 1859 if (str_copyright)
79fdaa4c 1860 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
85f07f22 1861 if (str_comment)
79fdaa4c 1862 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
85f07f22
FB
1863 }
1864
1865 output_files[nb_output_files] = oc;
1866 /* dump the file content */
1867 dump_format(oc, nb_output_files, filename, 1);
1868 nb_output_files++;
1869
1870 strcpy(oc->filename, filename);
919f448d
FB
1871
1872 /* check filename in case of an image number is expected */
79fdaa4c
FB
1873 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
1874 if (filename_number_test(oc->filename) < 0) {
1875 print_error(oc->filename, AVERROR_NUMEXPECTED);
919f448d 1876 exit(1);
79fdaa4c 1877 }
919f448d
FB
1878 }
1879
79fdaa4c 1880 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
85f07f22
FB
1881 /* test if it already exists to avoid loosing precious files */
1882 if (!file_overwrite &&
1883 (strchr(filename, ':') == NULL ||
1884 strstart(filename, "file:", NULL))) {
1885 if (url_exist(filename)) {
1886 int c;
1887
1888 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1889 fflush(stdout);
1890 c = getchar();
1891 if (toupper(c) != 'Y') {
1892 fprintf(stderr, "Not overwriting - exiting\n");
1893 exit(1);
1894 }
1895 }
1896 }
1897
1898 /* open the file */
1899 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1900 fprintf(stderr, "Could not open '%s'\n", filename);
1901 exit(1);
1902 }
1903 }
1904
1905 /* reset some options */
79fdaa4c
FB
1906 file_oformat = NULL;
1907 file_iformat = NULL;
85f07f22
FB
1908 audio_disable = 0;
1909 video_disable = 0;
1910 audio_codec_id = CODEC_ID_NONE;
1911 video_codec_id = CODEC_ID_NONE;
1912}
1913
a38469e1
FB
1914/* prepare dummy protocols for grab */
1915void prepare_grab(void)
1916{
1917 int has_video, has_audio, i, j;
1918 AVFormatContext *oc;
1919 AVFormatContext *ic;
1920 AVFormatParameters ap1, *ap = &ap1;
1921
1922 /* see if audio/video inputs are needed */
1923 has_video = 0;
1924 has_audio = 0;
1925 memset(ap, 0, sizeof(*ap));
1926 for(j=0;j<nb_output_files;j++) {
1927 oc = output_files[j];
1928 for(i=0;i<oc->nb_streams;i++) {
1929 AVCodecContext *enc = &oc->streams[i]->codec;
1930 switch(enc->codec_type) {
1931 case CODEC_TYPE_AUDIO:
1932 if (enc->sample_rate > ap->sample_rate)
1933 ap->sample_rate = enc->sample_rate;
1934 if (enc->channels > ap->channels)
1935 ap->channels = enc->channels;
1936 has_audio = 1;
1937 break;
1938 case CODEC_TYPE_VIDEO:
1939 if (enc->width > ap->width)
1940 ap->width = enc->width;
1941 if (enc->height > ap->height)
1942 ap->height = enc->height;
1943 if (enc->frame_rate > ap->frame_rate)
1944 ap->frame_rate = enc->frame_rate;
1945 has_video = 1;
1946 break;
51bd4565 1947 default:
c04643a2 1948 av_abort();
a38469e1
FB
1949 }
1950 }
1951 }
1952
1953 if (has_video == 0 && has_audio == 0) {
1954 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1955 exit(1);
1956 }
1957
1958 if (has_video) {
79fdaa4c
FB
1959 AVInputFormat *fmt1;
1960 fmt1 = av_find_input_format("video_grab_device");
1961 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
bf5af568 1962 fprintf(stderr, "Could not find video grab device\n");
a38469e1
FB
1963 exit(1);
1964 }
79fdaa4c
FB
1965 /* by now video grab has one stream */
1966 ic->streams[0]->r_frame_rate = ap->frame_rate;
a38469e1
FB
1967 input_files[nb_input_files] = ic;
1968 dump_format(ic, nb_input_files, v4l_device, 0);
1969 nb_input_files++;
1970 }
1971 if (has_audio) {
79fdaa4c
FB
1972 AVInputFormat *fmt1;
1973 fmt1 = av_find_input_format("audio_device");
1974 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
bf5af568 1975 fprintf(stderr, "Could not find audio grab device\n");
a38469e1
FB
1976 exit(1);
1977 }
1978 input_files[nb_input_files] = ic;
1979 dump_format(ic, nb_input_files, audio_device, 0);
1980 nb_input_files++;
1981 }
1982}
1983
a38469e1
FB
1984/* open the necessary output devices for playing */
1985void prepare_play(void)
1986{
bf5af568
FB
1987 file_iformat = NULL;
1988 file_oformat = guess_format("audio_device", NULL, NULL);
1989 if (!file_oformat) {
a38469e1
FB
1990 fprintf(stderr, "Could not find audio device\n");
1991 exit(1);
1992 }
1993
1994 opt_output_file(audio_device);
1995}
1996
1997
bdc4796f 1998#ifndef CONFIG_WIN32
5727b222
FB
1999INT64 getutime(void)
2000{
2001 struct rusage rusage;
2002
2003 getrusage(RUSAGE_SELF, &rusage);
2004 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2005}
bdc4796f
FB
2006#else
2007INT64 getutime(void)
2008{
2c4ae653 2009 return av_gettime();
bdc4796f
FB
2010}
2011#endif
5727b222 2012
79fdaa4c
FB
2013extern int ffm_nopts;
2014
2015void opt_bitexact(void)
2016{
2017 avcodec_set_bit_exact();
2018 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2019 ffm_nopts = 1;
2020}
2021
85f07f22
FB
2022void show_formats(void)
2023{
79fdaa4c
FB
2024 AVInputFormat *ifmt;
2025 AVOutputFormat *ofmt;
85f07f22
FB
2026 URLProtocol *up;
2027 AVCodec *p;
2028 const char **pp;
2029
2030 printf("File formats:\n");
2031 printf(" Encoding:");
79fdaa4c
FB
2032 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2033 printf(" %s", ofmt->name);
85f07f22
FB
2034 }
2035 printf("\n");
2036 printf(" Decoding:");
79fdaa4c
FB
2037 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2038 printf(" %s", ifmt->name);
85f07f22
FB
2039 }
2040 printf("\n");
2041
2042 printf("Codecs:\n");
2043 printf(" Encoders:");
2044 for(p = first_avcodec; p != NULL; p = p->next) {
2045 if (p->encode)
2046 printf(" %s", p->name);
2047 }
2048 printf("\n");
2049
2050 printf(" Decoders:");
2051 for(p = first_avcodec; p != NULL; p = p->next) {
2052 if (p->decode)
2053 printf(" %s", p->name);
2054 }
2055 printf("\n");
2056
2057 printf("Supported file protocols:");
2058 for(up = first_protocol; up != NULL; up = up->next)
2059 printf(" %s:", up->name);
2060 printf("\n");
2061
2062 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2063 printf("Motion estimation methods:");
2064 pp = motion_str;
2065 while (*pp) {
2066 printf(" %s", *pp);
101bea5f 2067 if ((pp - motion_str + 1) == ME_ZERO)
85f07f22 2068 printf("(fastest)");
101bea5f 2069 else if ((pp - motion_str + 1) == ME_FULL)
85f07f22 2070 printf("(slowest)");
101bea5f 2071 else if ((pp - motion_str + 1) == ME_EPZS)
85f07f22
FB
2072 printf("(default)");
2073 pp++;
2074 }
2075 printf("\n");
2076 exit(1);
2077}
2078
2079void show_help(void)
2080{
a38469e1 2081 const char *prog;
85f07f22
FB
2082 const OptionDef *po;
2083 int i, expert;
a38469e1
FB
2084
2085 prog = do_play ? "ffplay" : "ffmpeg";
85f07f22 2086
bf5af568 2087 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
a38469e1
FB
2088 prog);
2089
2090 if (!do_play) {
2091 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2092 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2093 } else {
2094 printf("usage: ffplay [options] input_file...\n"
2095 "Simple audio player\n");
2096 }
2097
2098 printf("\n"
85f07f22
FB
2099 "Main options are:\n");
2100 for(i=0;i<2;i++) {
2101 if (i == 1)
2102 printf("\nAdvanced options are:\n");
2103 for(po = options; po->name != NULL; po++) {
2104 char buf[64];
2105 expert = (po->flags & OPT_EXPERT) != 0;
2106 if (expert == i) {
2107 strcpy(buf, po->name);
2108 if (po->flags & HAS_ARG) {
2109 strcat(buf, " ");
2110 strcat(buf, po->argname);
2111 }
2112 printf("-%-17s %s\n", buf, po->help);
2113 }
2114 }
2115 }
2116
2117 exit(1);
2118}
2119
2120const OptionDef options[] = {
bdc4796f
FB
2121 { "L", 0, {(void*)show_licence}, "show license" },
2122 { "h", 0, {(void*)show_help}, "show help" },
2123 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2124 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
0b97443a 2125 { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
bdc4796f
FB
2126 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2127 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2128 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2129 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2130 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2131 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2132 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2133 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
85f07f22 2134 /* video options */
bdc4796f
FB
2135 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2136 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2137 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2138 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2139 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2140 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2141 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
9cdd6a24
MN
2142 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2143 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2144 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2145 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2146 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
3aa102be 2147 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
9cdd6a24 2148 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
3aa102be
MN
2149 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2150 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
bf5af568 2151 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
bdc4796f
FB
2152 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2153 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
85f07f22 2154 "method" },
463678ac 2155 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
bc6caae2 2156 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
e4986da9 2157 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
29da453b 2158 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
fe670d09 2159 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
bdc4796f 2160 { "sameq", OPT_BOOL, {(void*)&same_quality},
85f07f22
FB
2161 "use same video quality as source (implies VBR)" },
2162 /* audio options */
bdc4796f
FB
2163 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2164 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2165 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2166 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
bdc4796f 2167 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
bdc4796f
FB
2168 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2169 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
cfcf0ffd 2170 "deinterlace pictures" },
bdc4796f 2171 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
5727b222 2172 "add timings for benchmarking" },
a0663ba4
FB
2173 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2174 "dump each input packet" },
ce7c56c2
J
2175 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2176 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
79fdaa4c 2177 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
85f07f22
FB
2178 { NULL, },
2179};
2180
2181int main(int argc, char **argv)
2182{
2183 int optindex, i;
2184 const char *opt, *arg;
2185 const OptionDef *po;
a38469e1 2186 INT64 ti;
85f07f22 2187
2c4ae653 2188 av_register_all();
85f07f22 2189
a38469e1
FB
2190 /* detect if invoked as player */
2191 i = strlen(argv[0]);
2192 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2193 do_play = 1;
2194
85f07f22
FB
2195 if (argc <= 1)
2196 show_help();
a38469e1
FB
2197
2198 /* parse options */
85f07f22
FB
2199 optindex = 1;
2200 while (optindex < argc) {
2201 opt = argv[optindex++];
2202
2203 if (opt[0] == '-' && opt[1] != '\0') {
2204 po = options;
2205 while (po->name != NULL) {
2206 if (!strcmp(opt + 1, po->name))
2207 break;
2208 po++;
2209 }
2210 if (!po->name) {
2211 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2212 exit(1);
2213 }
2214 arg = NULL;
2215 if (po->flags & HAS_ARG)
2216 arg = argv[optindex++];
2217 if (po->flags & OPT_STRING) {
2218 char *str;
2219 str = strdup(arg);
2220 *po->u.str_arg = str;
2221 } else if (po->flags & OPT_BOOL) {
2222 *po->u.int_arg = 1;
2223 } else {
2224 po->u.func_arg(arg);
2225 }
2226 } else {
a38469e1
FB
2227 if (!do_play) {
2228 opt_output_file(opt);
2229 } else {
2230 opt_input_file(opt);
2231 }
85f07f22
FB
2232 }
2233 }
2234
2235
a38469e1
FB
2236 if (!do_play) {
2237 /* file converter / grab */
85f07f22
FB
2238 if (nb_output_files <= 0) {
2239 fprintf(stderr, "Must supply at least one output file\n");
2240 exit(1);
2241 }
a38469e1
FB
2242
2243 if (nb_input_files == 0) {
2244 prepare_grab();
5727b222 2245 }
a38469e1
FB
2246 } else {
2247 /* player */
2248 if (nb_input_files <= 0) {
2249 fprintf(stderr, "Must supply at least one input file\n");
2250 exit(1);
2251 }
2252 prepare_play();
2253 }
2254
2255 ti = getutime();
2256 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2257 stream_maps, nb_stream_maps);
2258 ti = getutime() - ti;
2259 if (do_benchmark) {
2260 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
85f07f22
FB
2261 }
2262
2263 /* close files */
2264 for(i=0;i<nb_output_files;i++) {
79fdaa4c 2265 if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
85f07f22
FB
2266 url_fclose(&output_files[i]->pb);
2267 }
79fdaa4c
FB
2268 for(i=0;i<nb_input_files;i++)
2269 av_close_input_file(input_files[i]);
85f07f22
FB
2270
2271 return 0;
2272}