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