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