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