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