3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define HAVE_AV_CONFIG_H
26 #include <sys/ioctl.h>
29 #include <sys/resource.h>
39 #define MAXINT64 INT64_C(0x7fffffffffffffff)
44 #define HAS_ARG 0x0001
45 #define OPT_BOOL 0x0002
46 #define OPT_EXPERT 0x0004
47 #define OPT_STRING 0x0008
57 /* select an input stream for an output stream */
58 typedef struct AVStreamMap
{
63 extern const OptionDef options
[];
69 static AVFormatContext
*input_files
[MAX_FILES
];
70 static int nb_input_files
= 0;
72 static AVFormatContext
*output_files
[MAX_FILES
];
73 static int nb_output_files
= 0;
75 static AVStreamMap stream_maps
[MAX_FILES
];
76 static int nb_stream_maps
;
78 static AVInputFormat
*file_iformat
;
79 static AVOutputFormat
*file_oformat
;
80 static int frame_width
= 160;
81 static int frame_height
= 128;
82 static int frame_topBand
= 0;
83 static int frame_bottomBand
= 0;
84 static int frame_leftBand
= 0;
85 static int frame_rightBand
= 0;
86 static int frame_rate
= 25 * FRAME_RATE_BASE
;
87 static int video_bit_rate
= 200*1000;
88 static int video_bit_rate_tolerance
= 4000*1000;
89 static int video_qscale
= 0;
90 static int video_qmin
= 2;
91 static int video_qmax
= 31;
92 static int video_qdiff
= 3;
93 static float video_qblur
= 0.5;
94 static float video_qcomp
= 0.5;
95 static float video_rc_qsquish
=1.0;
96 static float video_rc_qmod_amp
=0;
97 static int video_rc_qmod_freq
=0;
98 static char *video_rc_override_string
=NULL
;
99 static char *video_rc_eq
="tex^qComp";
100 static int video_rc_buffer_size
=0;
101 static float video_rc_buffer_aggressivity
=1.0;
102 static int video_rc_max_rate
=0;
103 static int video_rc_min_rate
=0;
104 static float video_rc_initial_cplx
=0;
105 static float video_b_qfactor
= 1.25;
106 static float video_b_qoffset
= 1.25;
107 static float video_i_qfactor
= -0.8;
108 static float video_i_qoffset
= 0.0;
109 static int me_method
= 0;
110 static int video_disable
= 0;
111 static int video_codec_id
= CODEC_ID_NONE
;
112 static int same_quality
= 0;
113 static int b_frames
= 0;
114 static int use_hq
= 0;
115 static int use_4mv
= 0;
116 static int do_deinterlace
= 0;
117 static int workaround_bugs
= 0;
118 static int error_resilience
= 0;
119 static int dct_algo
= 0;
120 static int idct_algo
= 0;
121 static int use_part
= 0;
122 static int packet_size
= 0;
124 static int gop_size
= 12;
125 static int intra_only
= 0;
126 static int audio_sample_rate
= 44100;
127 static int audio_bit_rate
= 64000;
128 static int audio_disable
= 0;
129 static int audio_channels
= 1;
130 static int audio_codec_id
= CODEC_ID_NONE
;
132 static INT64 recording_time
= 0;
133 static int file_overwrite
= 0;
134 static char *str_title
= NULL
;
135 static char *str_author
= NULL
;
136 static char *str_copyright
= NULL
;
137 static char *str_comment
= NULL
;
138 static int do_benchmark
= 0;
139 static int do_hex_dump
= 0;
140 static int do_play
= 0;
141 static int do_psnr
= 0;
142 static int do_vstats
= 0;
143 static int mpeg_vcd
= 0;
145 #ifndef CONFIG_AUDIO_OSS
146 const char *audio_device
= "none";
148 #ifndef CONFIG_VIDEO4LINUX
149 const char *v4l_device
= "none";
152 typedef struct AVOutputStream
{
153 int file_index
; /* file index */
154 int index
; /* stream index in the output file */
155 int source_index
; /* AVInputStream index */
156 AVStream
*st
; /* stream in the output file */
157 int encoding_needed
; /* true if encoding needed for this stream */
160 AVPicture pict_tmp
; /* temporary image for resizing */
162 ImgReSampleContext
*img_resample_ctx
; /* for image resampling */
166 ReSampleContext
*resample
; /* for audio resampling */
167 FifoBuffer fifo
; /* for compression: one audio fifo per codec */
170 typedef struct AVInputStream
{
174 int discard
; /* true if stream data should be discarded */
175 int decoding_needed
; /* true if the packets must be decoded in 'raw_fifo' */
176 Ticker pts_ticker
; /* Ticker for PTS calculation */
177 int ticker_inited
; /* to signal if the ticker was initialized */
178 INT64 pts
; /* current pts */
179 int pts_increment
; /* expected pts increment for next packet */
180 int frame_number
; /* current frame */
181 INT64 sample_index
; /* current sample */
184 typedef struct AVInputFile
{
185 int eof_reached
; /* true if eof reached */
186 int ist_index
; /* index of first stream in ist_table */
187 int buffer_size
; /* current total buffer size */
188 int buffer_size_max
; /* buffer size at which we consider we can stop
190 int nb_streams
; /* nb streams we are aware of */
195 /* init terminal so that we can grab keys */
196 static struct termios oldtty
;
198 static void term_exit(void)
200 tcsetattr (0, TCSANOW
, &oldtty
);
203 static void term_init(void)
210 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
211 |INLCR
|IGNCR
|ICRNL
|IXON
);
212 tty
.c_oflag
|= OPOST
;
213 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
214 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
219 tcsetattr (0, TCSANOW
, &tty
);
224 /* read a key without blocking */
225 static int read_key(void)
236 n
= select(1, &rfds
, NULL
, NULL
, &tv
);
249 /* no interactive support */
250 static void term_exit(void)
254 static void term_init(void)
258 static int read_key(void)
265 int read_ffserver_streams(AVFormatContext
*s
, const char *filename
)
270 err
= av_open_input_file(&ic
, filename
, NULL
, FFM_PACKET_SIZE
, NULL
);
273 /* copy stream format */
274 s
->nb_streams
= ic
->nb_streams
;
275 for(i
=0;i
<ic
->nb_streams
;i
++) {
277 st
= av_mallocz(sizeof(AVFormatContext
));
278 memcpy(st
, ic
->streams
[i
], sizeof(AVStream
));
282 av_close_input_file(ic
);
286 #define MAX_AUDIO_PACKET_SIZE 16384
288 static void do_audio_out(AVFormatContext
*s
,
291 unsigned char *buf
, int size
)
294 UINT8 audio_buf
[2*MAX_AUDIO_PACKET_SIZE
]; /* XXX: allocate it */
295 UINT8 audio_out
[MAX_AUDIO_PACKET_SIZE
]; /* XXX: allocate it */
296 int size_out
, frame_bytes
, ret
;
299 enc
= &ost
->st
->codec
;
301 if (ost
->audio_resample
) {
303 size_out
= audio_resample(ost
->resample
,
304 (short *)buftmp
, (short *)buf
,
305 size
/ (ist
->st
->codec
.channels
* 2));
306 size_out
= size_out
* enc
->channels
* 2;
312 /* now encode as many frames as possible */
313 if (enc
->frame_size
> 1) {
314 /* output resampled raw samples */
315 fifo_write(&ost
->fifo
, buftmp
, size_out
,
318 frame_bytes
= enc
->frame_size
* 2 * enc
->channels
;
320 while (fifo_read(&ost
->fifo
, audio_buf
, frame_bytes
,
321 &ost
->fifo
.rptr
) == 0) {
322 ret
= avcodec_encode_audio(enc
, audio_out
, sizeof(audio_out
),
324 s
->oformat
->write_packet(s
, ost
->index
, audio_out
, ret
, 0);
327 /* output a pcm frame */
328 /* XXX: change encoding codec API to avoid this ? */
329 switch(enc
->codec
->id
) {
330 case CODEC_ID_PCM_S16LE
:
331 case CODEC_ID_PCM_S16BE
:
332 case CODEC_ID_PCM_U16LE
:
333 case CODEC_ID_PCM_U16BE
:
336 size_out
= size_out
>> 1;
339 ret
= avcodec_encode_audio(enc
, audio_out
, size_out
,
341 s
->oformat
->write_packet(s
, ost
->index
, audio_out
, ret
, 0);
345 /* write a picture to a raw mux */
346 static void write_picture(AVFormatContext
*s
, int index
, AVPicture
*picture
,
347 int pix_fmt
, int w
, int h
)
349 UINT8
*buf
, *src
, *dest
;
352 /* XXX: not efficient, should add test if we can take
353 directly the AVPicture */
355 case PIX_FMT_YUV420P
:
356 size
= avpicture_get_size(pix_fmt
, w
, h
);
357 buf
= av_malloc(size
);
366 src
= picture
->data
[i
];
368 memcpy(dest
, src
, w
);
370 src
+= picture
->linesize
[i
];
374 case PIX_FMT_YUV422P
:
376 buf
= av_malloc(size
);
384 src
= picture
->data
[i
];
386 memcpy(dest
, src
, w
);
388 src
+= picture
->linesize
[i
];
392 case PIX_FMT_YUV444P
:
394 buf
= av_malloc(size
);
399 src
= picture
->data
[i
];
401 memcpy(dest
, src
, w
);
403 src
+= picture
->linesize
[i
];
409 buf
= av_malloc(size
);
413 src
= picture
->data
[0];
415 memcpy(dest
, src
, w
* 2);
417 src
+= picture
->linesize
[0];
423 buf
= av_malloc(size
);
427 src
= picture
->data
[0];
429 memcpy(dest
, src
, w
* 3);
431 src
+= picture
->linesize
[0];
437 s
->oformat
->write_packet(s
, index
, buf
, size
, 0);
442 static void do_video_out(AVFormatContext
*s
,
448 int n1
, n2
, nb
, i
, ret
, frame_number
, dec_frame_rate
;
449 AVPicture
*picture
, *picture2
, *pict
;
450 AVPicture picture_tmp1
, picture_tmp2
;
451 static UINT8
*video_buffer
;
452 UINT8
*buf
= NULL
, *buf1
= NULL
;
453 AVCodecContext
*enc
, *dec
;
455 #define VIDEO_BUFFER_SIZE (1024*1024)
457 enc
= &ost
->st
->codec
;
458 dec
= &ist
->st
->codec
;
460 frame_number
= ist
->frame_number
;
461 dec_frame_rate
= ist
->st
->r_frame_rate
;
462 // fprintf(stderr, "\n%d", dec_frame_rate);
463 /* first drop frame if needed */
464 n1
= ((INT64
)frame_number
* enc
->frame_rate
) / dec_frame_rate
;
465 n2
= (((INT64
)frame_number
+ 1) * enc
->frame_rate
) / dec_frame_rate
;
471 video_buffer
= av_malloc(VIDEO_BUFFER_SIZE
);
472 if(!video_buffer
) return;
474 /* deinterlace : must be done before any resize */
475 if (do_deinterlace
) {
478 /* create temporary picture */
479 size
= avpicture_get_size(dec
->pix_fmt
, dec
->width
, dec
->height
);
480 buf1
= av_malloc(size
);
484 picture2
= &picture_tmp2
;
485 avpicture_fill(picture2
, buf1
, dec
->pix_fmt
, dec
->width
, dec
->height
);
487 if (avpicture_deinterlace(picture2
, picture1
,
488 dec
->pix_fmt
, dec
->width
, dec
->height
) < 0) {
489 /* if error, do not deinterlace */
498 /* convert pixel format if needed */
499 if (enc
->pix_fmt
!= dec
->pix_fmt
) {
502 /* create temporary picture */
503 size
= avpicture_get_size(enc
->pix_fmt
, dec
->width
, dec
->height
);
504 buf
= av_malloc(size
);
507 pict
= &picture_tmp1
;
508 avpicture_fill(pict
, buf
, enc
->pix_fmt
, dec
->width
, dec
->height
);
510 if (img_convert(pict
, enc
->pix_fmt
,
511 picture2
, dec
->pix_fmt
,
512 dec
->width
, dec
->height
) < 0) {
513 fprintf(stderr
, "pixel format conversion not handled\n");
520 /* XXX: resampling could be done before raw format convertion in
521 some cases to go faster */
522 /* XXX: only works for YUV420P */
523 if (ost
->video_resample
) {
524 picture
= &ost
->pict_tmp
;
525 img_resample(ost
->img_resample_ctx
, picture
, pict
);
530 /* duplicates frame if needed */
531 /* XXX: pb because no interleaving */
533 if (enc
->codec_id
!= CODEC_ID_RAWVIDEO
) {
534 /* handles sameq here. This is not correct because it may
535 not be a global option */
537 enc
->quality
= dec
->quality
;
540 ret
= avcodec_encode_video(enc
,
541 video_buffer
, VIDEO_BUFFER_SIZE
,
543 //enc->frame_number = enc->real_pict_num;
544 s
->oformat
->write_packet(s
, ost
->index
, video_buffer
, ret
, 0);
546 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
547 // enc->frame_number-1, enc->real_pict_num, ret,
550 write_picture(s
, ost
->index
, picture
, enc
->pix_fmt
, enc
->width
, enc
->height
);
558 static void do_video_stats(AVOutputStream
*ost
,
562 static FILE *fvstats
=NULL
;
563 static INT64 total_size
= 0;
570 double ti1
, bitrate
, avg_bitrate
;
574 today
= localtime(&today2
);
575 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
578 fvstats
= fopen(filename
,"w");
586 enc
= &ost
->st
->codec
;
587 total_size
+= frame_size
;
588 if (enc
->codec_type
== CODEC_TYPE_VIDEO
) {
589 frame_number
= ist
->frame_number
;
590 fprintf(fvstats
, "frame= %5d q= %2d ", frame_number
, enc
->quality
);
592 fprintf(fvstats
, "PSNR= %6.2f ", enc
->psnr_y
);
594 fprintf(fvstats
,"f_size= %6d ", frame_size
);
595 /* compute min pts value */
596 if (!ist
->discard
&& ist
->pts
< ti
) {
599 ti1
= (double)ti
/ 1000000.0;
603 bitrate
= (double)(frame_size
* 8) * enc
->frame_rate
/ FRAME_RATE_BASE
/ 1000.0;
604 avg_bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
605 fprintf(fvstats
, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
606 (double)total_size
/ 1024, ti1
, bitrate
, avg_bitrate
);
607 fprintf(fvstats
,"type= %s\n", enc
->key_frame
== 1 ?
"I" : "P");
615 * The following code is the main loop of the file converter
617 static int av_encode(AVFormatContext
**output_files
,
619 AVFormatContext
**input_files
,
621 AVStreamMap
*stream_maps
, int nb_stream_maps
)
623 int ret
, i
, j
, k
, n
, nb_istreams
= 0, nb_ostreams
= 0;
624 AVFormatContext
*is
, *os
;
625 AVCodecContext
*codec
, *icodec
;
626 AVOutputStream
*ost
, **ost_table
= NULL
;
627 AVInputStream
*ist
, **ist_table
= NULL
;
628 INT64 min_pts
, start_time
;
629 AVInputFile
*file_table
;
630 AVFormatContext
*stream_no_data
;
633 file_table
= (AVInputFile
*) av_mallocz(nb_input_files
* sizeof(AVInputFile
));
637 /* input stream init */
639 for(i
=0;i
<nb_input_files
;i
++) {
641 file_table
[i
].ist_index
= j
;
642 file_table
[i
].nb_streams
= is
->nb_streams
;
647 ist_table
= av_mallocz(nb_istreams
* sizeof(AVInputStream
*));
651 for(i
=0;i
<nb_istreams
;i
++) {
652 ist
= av_mallocz(sizeof(AVInputStream
));
658 for(i
=0;i
<nb_input_files
;i
++) {
660 for(k
=0;k
<is
->nb_streams
;k
++) {
661 ist
= ist_table
[j
++];
662 ist
->st
= is
->streams
[k
];
665 ist
->discard
= 1; /* the stream is discarded by default
670 /* output stream init */
672 for(i
=0;i
<nb_output_files
;i
++) {
673 os
= output_files
[i
];
674 nb_ostreams
+= os
->nb_streams
;
676 os
->flags
|= AVF_FLAG_VCD
;
678 if (nb_stream_maps
> 0 && nb_stream_maps
!= nb_ostreams
) {
679 fprintf(stderr
, "Number of stream maps must match number of output streams\n");
683 ost_table
= av_mallocz(sizeof(AVOutputStream
*) * nb_ostreams
);
686 for(i
=0;i
<nb_ostreams
;i
++) {
687 ost
= av_mallocz(sizeof(AVOutputStream
));
694 for(k
=0;k
<nb_output_files
;k
++) {
695 os
= output_files
[k
];
696 for(i
=0;i
<os
->nb_streams
;i
++) {
698 ost
= ost_table
[n
++];
701 ost
->st
= os
->streams
[i
];
702 if (nb_stream_maps
> 0) {
703 ost
->source_index
= file_table
[stream_maps
[n
-1].file_index
].ist_index
+
704 stream_maps
[n
-1].stream_index
;
706 /* get corresponding input stream index : we select the first one with the right type */
708 for(j
=0;j
<nb_istreams
;j
++) {
711 ist
->st
->codec
.codec_type
== ost
->st
->codec
.codec_type
) {
712 ost
->source_index
= j
;
718 /* try again and reuse existing stream */
719 for(j
=0;j
<nb_istreams
;j
++) {
721 if (ist
->st
->codec
.codec_type
== ost
->st
->codec
.codec_type
) {
722 ost
->source_index
= j
;
727 fprintf(stderr
, "Could not find input stream matching output stream #%d.%d\n",
728 ost
->file_index
, ost
->index
);
733 ist
= ist_table
[ost
->source_index
];
738 /* dump the stream mapping */
739 fprintf(stderr
, "Stream mapping:\n");
740 for(i
=0;i
<nb_ostreams
;i
++) {
742 fprintf(stderr
, " Stream #%d.%d -> #%d.%d\n",
743 ist_table
[ost
->source_index
]->file_index
,
744 ist_table
[ost
->source_index
]->index
,
749 /* for each output stream, we compute the right encoding parameters */
750 for(i
=0;i
<nb_ostreams
;i
++) {
752 ist
= ist_table
[ost
->source_index
];
754 codec
= &ost
->st
->codec
;
755 icodec
= &ist
->st
->codec
;
757 switch(codec
->codec_type
) {
758 case CODEC_TYPE_AUDIO
:
759 /* check if same codec with same parameters. If so, no
760 reencoding is needed */
761 if (codec
->codec_id
== icodec
->codec_id
&&
762 codec
->bit_rate
== icodec
->bit_rate
&&
763 codec
->sample_rate
== icodec
->sample_rate
&&
764 codec
->channels
== icodec
->channels
) {
766 /* use the same frame size */
767 codec
->frame_size
= icodec
->frame_size
;
768 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
770 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
772 if (fifo_init(&ost
->fifo
, 2 * MAX_AUDIO_PACKET_SIZE
))
775 if (codec
->channels
== icodec
->channels
&&
776 codec
->sample_rate
== icodec
->sample_rate
) {
777 ost
->audio_resample
= 0;
779 if (codec
->channels
!= icodec
->channels
&&
780 icodec
->codec_id
== CODEC_ID_AC3
) {
781 /* Special case for 5:1 AC3 input */
782 /* and mono or stereo output */
783 /* Request specific number of channels */
784 icodec
->channels
= codec
->channels
;
785 if (codec
->sample_rate
== icodec
->sample_rate
)
786 ost
->audio_resample
= 0;
788 ost
->audio_resample
= 1;
789 ost
->resample
= audio_resample_init(codec
->channels
, icodec
->channels
,
791 icodec
->sample_rate
);
793 /* Request specific number of channels */
794 icodec
->channels
= codec
->channels
;
796 ost
->audio_resample
= 1;
797 ost
->resample
= audio_resample_init(codec
->channels
, icodec
->channels
,
799 icodec
->sample_rate
);
802 ist
->decoding_needed
= 1;
803 ost
->encoding_needed
= 1;
806 case CODEC_TYPE_VIDEO
:
807 /* check if same codec with same parameters. If so, no
808 reencoding is needed */
809 if (codec
->codec_id
== icodec
->codec_id
&&
810 codec
->bit_rate
== icodec
->bit_rate
&&
811 codec
->frame_rate
== icodec
->frame_rate
&&
812 codec
->width
== icodec
->width
&&
813 codec
->height
== icodec
->height
) {
816 if (codec
->width
== icodec
->width
&&
817 codec
->height
== icodec
->height
) {
818 ost
->video_resample
= 0;
821 ost
->video_resample
= 1;
822 buf
= av_malloc((codec
->width
* codec
->height
* 3) / 2);
825 ost
->pict_tmp
.data
[0] = buf
;
826 ost
->pict_tmp
.data
[1] = ost
->pict_tmp
.data
[0] + (codec
->width
* codec
->height
);
827 ost
->pict_tmp
.data
[2] = ost
->pict_tmp
.data
[1] + (codec
->width
* codec
->height
) / 4;
828 ost
->pict_tmp
.linesize
[0] = codec
->width
;
829 ost
->pict_tmp
.linesize
[1] = codec
->width
/ 2;
830 ost
->pict_tmp
.linesize
[2] = codec
->width
/ 2;
832 ost
->img_resample_ctx
= img_resample_full_init(
833 ost
->st
->codec
.width
, ost
->st
->codec
.height
,
834 ist
->st
->codec
.width
, ist
->st
->codec
.height
,
835 frame_topBand
, frame_bottomBand
,
836 frame_leftBand
, frame_rightBand
);
838 ost
->encoding_needed
= 1;
839 ist
->decoding_needed
= 1;
847 /* open each encoder */
848 for(i
=0;i
<nb_ostreams
;i
++) {
850 if (ost
->encoding_needed
) {
852 codec
= avcodec_find_encoder(ost
->st
->codec
.codec_id
);
854 fprintf(stderr
, "Unsupported codec for output stream #%d.%d\n",
855 ost
->file_index
, ost
->index
);
858 if (avcodec_open(&ost
->st
->codec
, codec
) < 0) {
859 fprintf(stderr
, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
860 ost
->file_index
, ost
->index
);
866 /* open each decoder */
867 for(i
=0;i
<nb_istreams
;i
++) {
869 if (ist
->decoding_needed
) {
871 codec
= avcodec_find_decoder(ist
->st
->codec
.codec_id
);
873 fprintf(stderr
, "Unsupported codec for input stream #%d.%d\n",
874 ist
->file_index
, ist
->index
);
877 if (avcodec_open(&ist
->st
->codec
, codec
) < 0) {
878 fprintf(stderr
, "Error while opening codec for input stream #%d.%d\n",
879 ist
->file_index
, ist
->index
);
882 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
883 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
888 for(i
=0;i
<nb_istreams
;i
++) {
891 ist
->frame_number
= 0;
894 /* compute buffer size max (should use a complete heuristic) */
895 for(i
=0;i
<nb_input_files
;i
++) {
896 file_table
[i
].buffer_size_max
= 2048;
899 /* open files and write file headers */
900 for(i
=0;i
<nb_output_files
;i
++) {
901 os
= output_files
[i
];
902 if (av_write_header(os
) < 0) {
903 fprintf(stderr
, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i
);
911 fprintf(stderr
, "Press [q] to stop encoding\n");
913 fprintf(stderr
, "Press [q] to stop playing\n");
918 start_time
= av_gettime();
924 int file_index
, ist_index
;
929 int data_size
, got_picture
;
931 short samples
[AVCODEC_MAX_AUDIO_FRAME_SIZE
/ 2];
934 /* if 'q' pressed, exits */
936 /* read_key() returns 0 on EOF */
942 /* select the input file with the smallest pts */
945 for(i
=0;i
<nb_istreams
;i
++) {
947 /* For some reason, the pts_increment code breaks q estimation?!? */
948 if (!ist
->discard
&& !file_table
[ist
->file_index
].eof_reached
&&
949 ist
->pts
/* + ist->pts_increment */ < min_pts
&& input_files
[ist
->file_index
] != stream_no_data
) {
950 min_pts
= ist
->pts
/* + ist->pts_increment */;
951 file_index
= ist
->file_index
;
954 /* if none, if is finished */
955 if (file_index
< 0) {
956 if (stream_no_data
) {
957 #ifndef CONFIG_WIN32 /* no usleep in VisualC ? */
959 snooze(10 * 1000); /* mmu_man */ /* in microsec */
960 #elif defined(__CYGWIN__)
966 ts
.tv_nsec
= 1000 * 1000 * 10;
975 /* finish if recording time exhausted */
976 if (recording_time
> 0 && min_pts
>= recording_time
)
978 /* read a packet from it and output it in the fifo */
979 is
= input_files
[file_index
];
980 if (av_read_packet(is
, &pkt
) < 0) {
981 file_table
[file_index
].eof_reached
= 1;
989 /* the following test is needed in case new streams appear
990 dynamically in stream : we ignore them */
991 if (pkt
.stream_index
>= file_table
[file_index
].nb_streams
)
993 ist_index
= file_table
[file_index
].ist_index
+ pkt
.stream_index
;
994 ist
= ist_table
[ist_index
];
998 if (pkt
.flags
& PKT_FLAG_DROPPED_FRAME
)
1002 printf("stream #%d, size=%d:\n", pkt
.stream_index
, pkt
.size
);
1003 av_hex_dump(pkt
.data
, pkt
.size
);
1006 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1012 /* decode the packet if needed */
1013 data_buf
= NULL
; /* fail safe */
1015 if (ist
->decoding_needed
) {
1016 switch(ist
->st
->codec
.codec_type
) {
1017 case CODEC_TYPE_AUDIO
:
1018 /* XXX: could avoid copy if PCM 16 bits with same
1019 endianness as CPU */
1020 ret
= avcodec_decode_audio(&ist
->st
->codec
, samples
, &data_size
,
1024 /* Some bug in mpeg audio decoder gives */
1025 /* data_size < 0, it seems they are overflows */
1026 if (data_size
<= 0) {
1027 /* no audio frame */
1032 data_buf
= (UINT8
*)samples
;
1034 case CODEC_TYPE_VIDEO
:
1035 if (ist
->st
->codec
.codec_id
== CODEC_ID_RAWVIDEO
) {
1038 size
= (ist
->st
->codec
.width
* ist
->st
->codec
.height
);
1039 avpicture_fill(&picture
, ptr
,
1040 ist
->st
->codec
.pix_fmt
,
1041 ist
->st
->codec
.width
,
1042 ist
->st
->codec
.height
);
1045 data_size
= (ist
->st
->codec
.width
* ist
->st
->codec
.height
* 3) / 2;
1046 ret
= avcodec_decode_video(&ist
->st
->codec
,
1047 &picture
, &got_picture
, ptr
, len
);
1050 fprintf(stderr
, "Error while decoding stream #%d.%d\n",
1051 ist
->file_index
, ist
->index
);
1052 av_free_packet(&pkt
);
1056 /* no picture yet */
1073 if (!ist
->ticker_inited
) {
1074 switch (ist
->st
->codec
.codec_type
) {
1075 case CODEC_TYPE_AUDIO
:
1076 ticker_init(&ist
->pts_ticker
,
1077 (INT64
)ist
->st
->codec
.sample_rate
,
1079 ist
->ticker_inited
= 1;
1081 case CODEC_TYPE_VIDEO
:
1082 ticker_init(&ist
->pts_ticker
,
1083 (INT64
)ist
->st
->r_frame_rate
,
1084 ((INT64
)1000000 * FRAME_RATE_BASE
));
1085 ist
->ticker_inited
= 1;
1092 switch(ist
->st
->codec
.codec_type
) {
1093 case CODEC_TYPE_AUDIO
:
1094 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1095 ist
->pts
= ticker_abs(&ist
->pts_ticker
, ist
->sample_index
);
1096 ist
->sample_index
+= data_size
/ (2 * ist
->st
->codec
.channels
);
1097 ist
->pts_increment
= (INT64
) (data_size
/ (2 * ist
->st
->codec
.channels
)) * 1000000 / ist
->st
->codec
.sample_rate
;
1099 case CODEC_TYPE_VIDEO
:
1100 ist
->frame_number
++;
1101 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1102 // ist->st->codec.frame_rate;
1103 ist
->pts
= ticker_abs(&ist
->pts_ticker
, ist
->frame_number
);
1104 ist
->pts_increment
= ((INT64
) 1000000 * FRAME_RATE_BASE
) /
1105 ist
->st
->codec
.frame_rate
;
1113 /* transcode raw format, encode packets and output them */
1115 for(i
=0;i
<nb_ostreams
;i
++) {
1118 if (ost
->source_index
== ist_index
) {
1119 os
= output_files
[ost
->file_index
];
1121 if (ost
->encoding_needed
) {
1122 switch(ost
->st
->codec
.codec_type
) {
1123 case CODEC_TYPE_AUDIO
:
1124 do_audio_out(os
, ost
, ist
, data_buf
, data_size
);
1126 case CODEC_TYPE_VIDEO
:
1127 do_video_out(os
, ost
, ist
, &picture
, &frame_size
);
1129 do_video_stats(ost
, ist
, frame_size
);
1135 /* no reencoding needed : output the packet directly */
1136 /* force the input stream PTS */
1137 os
->oformat
->write_packet(os
, ost
->index
, data_buf
, data_size
, pkt
.pts
);
1143 av_free_packet(&pkt
);
1145 /* dump report by using the first video and audio streams */
1148 AVFormatContext
*oc
;
1149 INT64 total_size
, ti
;
1150 AVCodecContext
*enc
;
1151 int frame_number
, vid
;
1152 double bitrate
, ti1
;
1153 static INT64 last_time
;
1155 if ((min_pts
- last_time
) >= 500000) {
1156 last_time
= min_pts
;
1158 oc
= output_files
[0];
1160 total_size
= url_ftell(&oc
->pb
);
1165 for(i
=0;i
<nb_ostreams
;i
++) {
1167 enc
= &ost
->st
->codec
;
1168 ist
= ist_table
[ost
->source_index
];
1169 if (!vid
&& enc
->codec_type
== CODEC_TYPE_VIDEO
) {
1170 frame_number
= ist
->frame_number
;
1171 sprintf(buf
+ strlen(buf
), "frame=%5d q=%2d ",
1172 frame_number
, enc
->quality
);
1174 sprintf(buf
+ strlen(buf
), "PSNR=%6.2f ", enc
->psnr_y
);
1177 /* compute min pts value */
1178 if (!ist
->discard
&& ist
->pts
< ti
) {
1183 ti1
= (double)ti
/ 1000000.0;
1186 bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
1188 sprintf(buf
+ strlen(buf
),
1189 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1190 (double)total_size
/ 1024, ti1
, bitrate
);
1192 fprintf(stderr
, "%s \r", buf
);
1199 /* dump report by using the first video and audio streams */
1202 AVFormatContext
*oc
;
1203 INT64 total_size
, ti
;
1204 AVCodecContext
*enc
;
1205 int frame_number
, vid
;
1206 double bitrate
, ti1
;
1208 oc
= output_files
[0];
1210 total_size
= url_ftell(&oc
->pb
);
1215 for(i
=0;i
<nb_ostreams
;i
++) {
1217 enc
= &ost
->st
->codec
;
1218 ist
= ist_table
[ost
->source_index
];
1219 if (!vid
&& enc
->codec_type
== CODEC_TYPE_VIDEO
) {
1220 frame_number
= ist
->frame_number
;
1221 sprintf(buf
+ strlen(buf
), "frame=%5d q=%2d ",
1222 frame_number
, enc
->quality
);
1224 sprintf(buf
+ strlen(buf
), "PSNR=%6.2f ", enc
->psnr_y
);
1227 /* compute min pts value */
1228 if (!ist
->discard
&& ist
->pts
< ti
) {
1233 ti1
= ti
/ 1000000.0;
1236 bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
1238 sprintf(buf
+ strlen(buf
),
1239 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1240 (double)total_size
/ 1024, ti1
, bitrate
);
1242 fprintf(stderr
, "%s \n", buf
);
1244 /* close each encoder */
1245 for(i
=0;i
<nb_ostreams
;i
++) {
1247 if (ost
->encoding_needed
) {
1248 avcodec_close(&ost
->st
->codec
);
1252 /* close each decoder */
1253 for(i
=0;i
<nb_istreams
;i
++) {
1255 if (ist
->decoding_needed
) {
1256 avcodec_close(&ist
->st
->codec
);
1261 /* write the trailer if needed and close file */
1262 for(i
=0;i
<nb_output_files
;i
++) {
1263 os
= output_files
[i
];
1264 av_write_trailer(os
);
1270 av_free(file_table
);
1273 for(i
=0;i
<nb_istreams
;i
++) {
1280 for(i
=0;i
<nb_ostreams
;i
++) {
1283 fifo_free(&ost
->fifo
); /* works even if fifo is not
1284 initialized but set to zero */
1285 av_free(ost
->pict_tmp
.data
[0]);
1286 if (ost
->video_resample
)
1287 img_resample_close(ost
->img_resample_ctx
);
1288 if (ost
->audio_resample
)
1289 audio_resample_close(ost
->resample
);
1302 int file_read(const char *filename
)
1305 unsigned char buffer
[1024];
1308 if (url_open(&h
, filename
, O_RDONLY
) < 0) {
1309 printf("could not open '%s'\n", filename
);
1313 len
= url_read(h
, buffer
, sizeof(buffer
));
1316 for(i
=0;i
<len
;i
++) putchar(buffer
[i
]);
1323 void show_licence(void)
1326 "ffmpeg version " FFMPEG_VERSION
"\n"
1327 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1328 "This library is free software; you can redistribute it and/or\n"
1329 "modify it under the terms of the GNU Lesser General Public\n"
1330 "License as published by the Free Software Foundation; either\n"
1331 "version 2 of the License, or (at your option) any later version.\n"
1333 "This library is distributed in the hope that it will be useful,\n"
1334 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1335 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1336 "Lesser General Public License for more details.\n"
1338 "You should have received a copy of the GNU Lesser General Public\n"
1339 "License along with this library; if not, write to the Free Software\n"
1340 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1345 void opt_format(const char *arg
)
1347 file_iformat
= av_find_input_format(arg
);
1348 file_oformat
= guess_format(arg
, NULL
, NULL
);
1349 if (!file_iformat
&& !file_oformat
) {
1350 fprintf(stderr
, "Unknown input or output format: %s\n", arg
);
1355 void opt_video_bitrate(const char *arg
)
1357 video_bit_rate
= atoi(arg
) * 1000;
1360 void opt_video_bitrate_tolerance(const char *arg
)
1362 video_bit_rate_tolerance
= atoi(arg
) * 1000;
1365 void opt_video_bitrate_max(const char *arg
)
1367 video_rc_max_rate
= atoi(arg
) * 1000;
1370 void opt_video_bitrate_min(const char *arg
)
1372 video_rc_min_rate
= atoi(arg
) * 1000;
1375 void opt_video_buffer_size(const char *arg
)
1377 video_rc_buffer_size
= atoi(arg
) * 1000;
1380 void opt_video_rc_eq(char *arg
)
1386 void opt_workaround_bugs(const char *arg
)
1388 workaround_bugs
= atoi(arg
);
1391 void opt_dct_algo(const char *arg
)
1393 dct_algo
= atoi(arg
);
1396 void opt_idct_algo(const char *arg
)
1398 idct_algo
= atoi(arg
);
1402 void opt_error_resilience(const char *arg
)
1404 error_resilience
= atoi(arg
);
1407 void opt_frame_rate(const char *arg
)
1409 frame_rate
= (int)(strtod(arg
, 0) * FRAME_RATE_BASE
);
1413 void opt_frame_crop_top(const char *arg
)
1415 frame_topBand
= atoi(arg
);
1416 if (frame_topBand
< 0) {
1417 fprintf(stderr
, "Incorrect top crop size\n");
1420 if ((frame_topBand
% 2) != 0) {
1421 fprintf(stderr
, "Top crop size must be a multiple of 2\n");
1424 if ((frame_topBand
) >= frame_height
){
1425 fprintf(stderr
, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1428 frame_height
-= frame_topBand
;
1431 void opt_frame_crop_bottom(const char *arg
)
1433 frame_bottomBand
= atoi(arg
);
1434 if (frame_bottomBand
< 0) {
1435 fprintf(stderr
, "Incorrect bottom crop size\n");
1438 if ((frame_bottomBand
% 2) != 0) {
1439 fprintf(stderr
, "Bottom crop size must be a multiple of 2\n");
1442 if ((frame_bottomBand
) >= frame_height
){
1443 fprintf(stderr
, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1446 frame_height
-= frame_bottomBand
;
1449 void opt_frame_crop_left(const char *arg
)
1451 frame_leftBand
= atoi(arg
);
1452 if (frame_leftBand
< 0) {
1453 fprintf(stderr
, "Incorrect left crop size\n");
1456 if ((frame_leftBand
% 2) != 0) {
1457 fprintf(stderr
, "Left crop size must be a multiple of 2\n");
1460 if ((frame_leftBand
) >= frame_width
){
1461 fprintf(stderr
, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1464 frame_width
-= frame_leftBand
;
1467 void opt_frame_crop_right(const char *arg
)
1469 frame_rightBand
= atoi(arg
);
1470 if (frame_rightBand
< 0) {
1471 fprintf(stderr
, "Incorrect right crop size\n");
1474 if ((frame_rightBand
% 2) != 0) {
1475 fprintf(stderr
, "Right crop size must be a multiple of 2\n");
1478 if ((frame_rightBand
) >= frame_width
){
1479 fprintf(stderr
, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1482 frame_width
-= frame_rightBand
;
1485 void opt_frame_size(const char *arg
)
1487 parse_image_size(&frame_width
, &frame_height
, arg
);
1488 if (frame_width
<= 0 || frame_height
<= 0) {
1489 fprintf(stderr
, "Incorrect frame size\n");
1492 if ((frame_width
% 2) != 0 || (frame_height
% 2) != 0) {
1493 fprintf(stderr
, "Frame size must be a multiple of 2\n");
1498 void opt_gop_size(const char *arg
)
1500 gop_size
= atoi(arg
);
1503 void opt_b_frames(const char *arg
)
1505 b_frames
= atoi(arg
);
1506 if (b_frames
> FF_MAX_B_FRAMES
) {
1507 fprintf(stderr
, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES
);
1509 } else if (b_frames
< 1) {
1510 fprintf(stderr
, "\nNumber of B frames must be higher than 0\n");
1515 void opt_qscale(const char *arg
)
1517 video_qscale
= atoi(arg
);
1518 if (video_qscale
< 0 ||
1519 video_qscale
> 31) {
1520 fprintf(stderr
, "qscale must be >= 1 and <= 31\n");
1525 void opt_qmin(const char *arg
)
1527 video_qmin
= atoi(arg
);
1528 if (video_qmin
< 0 ||
1530 fprintf(stderr
, "qmin must be >= 1 and <= 31\n");
1535 void opt_qmax(const char *arg
)
1537 video_qmax
= atoi(arg
);
1538 if (video_qmax
< 0 ||
1540 fprintf(stderr
, "qmax must be >= 1 and <= 31\n");
1545 void opt_qdiff(const char *arg
)
1547 video_qdiff
= atoi(arg
);
1548 if (video_qdiff
< 0 ||
1550 fprintf(stderr
, "qdiff must be >= 1 and <= 31\n");
1555 void opt_qblur(const char *arg
)
1557 video_qblur
= atof(arg
);
1560 void opt_qcomp(const char *arg
)
1562 video_qcomp
= atof(arg
);
1565 void opt_b_qfactor(const char *arg
)
1567 video_b_qfactor
= atof(arg
);
1569 void opt_i_qfactor(const char *arg
)
1571 video_i_qfactor
= atof(arg
);
1573 void opt_b_qoffset(const char *arg
)
1575 video_b_qoffset
= atof(arg
);
1577 void opt_i_qoffset(const char *arg
)
1579 video_i_qoffset
= atof(arg
);
1582 void opt_packet_size(const char *arg
)
1584 packet_size
= atoi(arg
);
1587 void opt_audio_bitrate(const char *arg
)
1589 audio_bit_rate
= atoi(arg
) * 1000;
1592 void opt_audio_rate(const char *arg
)
1594 audio_sample_rate
= atoi(arg
);
1597 void opt_audio_channels(const char *arg
)
1599 audio_channels
= atoi(arg
);
1602 void opt_video_device(const char *arg
)
1604 v4l_device
= strdup(arg
);
1607 void opt_audio_device(const char *arg
)
1609 audio_device
= strdup(arg
);
1612 void opt_audio_codec(const char *arg
)
1618 if (!strcmp(p
->name
, arg
) && p
->type
== CODEC_TYPE_AUDIO
)
1623 fprintf(stderr
, "Unknown audio codec '%s'\n", arg
);
1626 audio_codec_id
= p
->id
;
1630 const char *motion_str
[] = {
1640 void opt_motion_estimation(const char *arg
)
1646 fprintf(stderr
, "Unknown motion estimation method '%s'\n", arg
);
1649 if (!strcmp(*p
, arg
))
1653 me_method
= (p
- motion_str
) + 1;
1656 void opt_video_codec(const char *arg
)
1662 if (!strcmp(p
->name
, arg
) && p
->type
== CODEC_TYPE_VIDEO
)
1667 fprintf(stderr
, "Unknown video codec '%s'\n", arg
);
1670 video_codec_id
= p
->id
;
1674 void opt_map(const char *arg
)
1680 m
= &stream_maps
[nb_stream_maps
++];
1682 m
->file_index
= strtol(arg
, (char **)&p
, 0);
1686 m
->stream_index
= strtol(p
, (char **)&p
, 0);
1689 void opt_recording_time(const char *arg
)
1691 recording_time
= parse_date(arg
, 1);
1694 void print_error(const char *filename
, int err
)
1697 case AVERROR_NUMEXPECTED
:
1698 fprintf(stderr
, "%s: Incorrect image filename syntax.\n"
1699 "Use '%%d' to specify the image number:\n"
1700 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1701 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1704 case AVERROR_INVALIDDATA
:
1705 fprintf(stderr
, "%s: Error while parsing header\n", filename
);
1708 fprintf(stderr
, "%s: Unknown format\n", filename
);
1711 fprintf(stderr
, "%s: Error while opening file\n", filename
);
1716 void opt_input_file(const char *filename
)
1718 AVFormatContext
*ic
;
1719 AVFormatParameters params
, *ap
= ¶ms
;
1720 int err
, i
, ret
, rfps
;
1722 /* get default parameters from command line */
1723 memset(ap
, 0, sizeof(*ap
));
1724 ap
->sample_rate
= audio_sample_rate
;
1725 ap
->channels
= audio_channels
;
1726 ap
->frame_rate
= frame_rate
;
1727 ap
->width
= frame_width
;
1728 ap
->height
= frame_height
;
1730 /* open the input file with generic libav function */
1731 err
= av_open_input_file(&ic
, filename
, file_iformat
, 0, ap
);
1733 print_error(filename
, err
);
1737 /* If not enough info to get the stream parameters, we decode the
1738 first frames to get it. (used in mpeg case for example) */
1739 ret
= av_find_stream_info(ic
);
1741 fprintf(stderr
, "%s: could not find codec parameters\n", filename
);
1745 /* update the current parameters so that they match the one of the input stream */
1746 for(i
=0;i
<ic
->nb_streams
;i
++) {
1747 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
1748 switch(enc
->codec_type
) {
1749 case CODEC_TYPE_AUDIO
:
1750 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1751 audio_channels
= enc
->channels
;
1752 audio_sample_rate
= enc
->sample_rate
;
1754 case CODEC_TYPE_VIDEO
:
1755 frame_height
= enc
->height
;
1756 frame_width
= enc
->width
;
1757 rfps
= ic
->streams
[i
]->r_frame_rate
;
1758 enc
->workaround_bugs
= workaround_bugs
;
1759 enc
->error_resilience
= error_resilience
;
1760 enc
->idct_algo
= idct_algo
;
1761 if (enc
->frame_rate
!= rfps
) {
1762 fprintf(stderr
,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
1763 i
, (float)enc
->frame_rate
/ FRAME_RATE_BASE
,
1764 (float)rfps
/ FRAME_RATE_BASE
);
1766 /* update the current frame rate to match the stream frame rate */
1774 input_files
[nb_input_files
] = ic
;
1775 /* dump the file content */
1776 dump_format(ic
, nb_input_files
, filename
, 0);
1778 file_iformat
= NULL
;
1779 file_oformat
= NULL
;
1782 void check_audio_video_inputs(int *has_video_ptr
, int *has_audio_ptr
)
1784 int has_video
, has_audio
, i
, j
;
1785 AVFormatContext
*ic
;
1789 for(j
=0;j
<nb_input_files
;j
++) {
1790 ic
= input_files
[j
];
1791 for(i
=0;i
<ic
->nb_streams
;i
++) {
1792 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
1793 switch(enc
->codec_type
) {
1794 case CODEC_TYPE_AUDIO
:
1797 case CODEC_TYPE_VIDEO
:
1805 *has_video_ptr
= has_video
;
1806 *has_audio_ptr
= has_audio
;
1809 void opt_output_file(const char *filename
)
1812 AVFormatContext
*oc
;
1813 int use_video
, use_audio
, nb_streams
, input_has_video
, input_has_audio
;
1816 if (!strcmp(filename
, "-"))
1819 oc
= av_mallocz(sizeof(AVFormatContext
));
1821 if (!file_oformat
) {
1822 file_oformat
= guess_format(NULL
, filename
, NULL
);
1823 if (!file_oformat
) {
1824 fprintf(stderr
, "Unable for find a suitable output format for '%s'\n",
1830 oc
->oformat
= file_oformat
;
1832 if (!strcmp(file_oformat
->name
, "ffm") &&
1833 strstart(filename
, "http:", NULL
)) {
1834 /* special case for files sent to ffserver: we get the stream
1835 parameters from ffserver */
1836 if (read_ffserver_streams(oc
, filename
) < 0) {
1837 fprintf(stderr
, "Could not read stream parameters from '%s'\n", filename
);
1841 use_video
= file_oformat
->video_codec
!= CODEC_ID_NONE
;
1842 use_audio
= file_oformat
->audio_codec
!= CODEC_ID_NONE
;
1844 /* disable if no corresponding type found and at least one
1846 if (nb_input_files
> 0) {
1847 check_audio_video_inputs(&input_has_video
, &input_has_audio
);
1848 if (!input_has_video
)
1850 if (!input_has_audio
)
1854 /* manual disable */
1855 if (audio_disable
) {
1858 if (video_disable
) {
1864 AVCodecContext
*video_enc
;
1866 st
= av_mallocz(sizeof(AVStream
));
1868 fprintf(stderr
, "Could not alloc stream\n");
1871 video_enc
= &st
->codec
;
1873 codec_id
= file_oformat
->video_codec
;
1874 if (video_codec_id
!= CODEC_ID_NONE
)
1875 codec_id
= video_codec_id
;
1877 video_enc
->codec_id
= codec_id
;
1878 video_enc
->codec_type
= CODEC_TYPE_VIDEO
;
1880 video_enc
->bit_rate
= video_bit_rate
;
1881 video_enc
->bit_rate_tolerance
= video_bit_rate_tolerance
;
1882 video_enc
->frame_rate
= frame_rate
;
1884 video_enc
->width
= frame_width
;
1885 video_enc
->height
= frame_height
;
1887 video_enc
->gop_size
= gop_size
;
1889 video_enc
->gop_size
= 0;
1890 if (video_qscale
|| same_quality
) {
1891 video_enc
->flags
|= CODEC_FLAG_QSCALE
;
1892 video_enc
->quality
= video_qscale
;
1896 video_enc
->flags
|= CODEC_FLAG_HQ
;
1900 video_enc
->flags
|= CODEC_FLAG_HQ
;
1901 video_enc
->flags
|= CODEC_FLAG_4MV
;
1905 video_enc
->flags
|= CODEC_FLAG_PART
;
1909 if (codec_id
!= CODEC_ID_MPEG4
) {
1910 fprintf(stderr
, "\nB frames encoding only supported by MPEG-4.\n");
1913 video_enc
->max_b_frames
= b_frames
;
1914 video_enc
->b_frame_strategy
= 0;
1915 video_enc
->b_quant_factor
= 2.0;
1918 video_enc
->qmin
= video_qmin
;
1919 video_enc
->qmax
= video_qmax
;
1920 video_enc
->max_qdiff
= video_qdiff
;
1921 video_enc
->qblur
= video_qblur
;
1922 video_enc
->qcompress
= video_qcomp
;
1923 video_enc
->rc_eq
= video_rc_eq
;
1924 video_enc
->rc_max_rate
= video_rc_max_rate
;
1925 video_enc
->rc_min_rate
= video_rc_min_rate
;
1926 video_enc
->rc_buffer_size
= video_rc_buffer_size
;
1927 video_enc
->rc_buffer_aggressivity
= video_rc_buffer_aggressivity
;
1928 video_enc
->i_quant_factor
= video_i_qfactor
;
1929 video_enc
->b_quant_factor
= video_b_qfactor
;
1930 video_enc
->i_quant_offset
= video_i_qoffset
;
1931 video_enc
->b_quant_offset
= video_b_qoffset
;
1932 video_enc
->dct_algo
= dct_algo
;
1933 video_enc
->idct_algo
= idct_algo
;
1935 video_enc
->rtp_mode
= 1;
1936 video_enc
->rtp_payload_size
= packet_size
;
1940 video_enc
->get_psnr
= 1;
1942 video_enc
->get_psnr
= 0;
1944 video_enc
->me_method
= me_method
;
1946 /* XXX: need to find a way to set codec parameters */
1947 if (oc
->oformat
->flags
& AVFMT_RGB24
) {
1948 video_enc
->pix_fmt
= PIX_FMT_RGB24
;
1951 oc
->streams
[nb_streams
] = st
;
1956 AVCodecContext
*audio_enc
;
1958 st
= av_mallocz(sizeof(AVStream
));
1960 fprintf(stderr
, "Could not alloc stream\n");
1963 audio_enc
= &st
->codec
;
1964 codec_id
= file_oformat
->audio_codec
;
1965 if (audio_codec_id
!= CODEC_ID_NONE
)
1966 codec_id
= audio_codec_id
;
1967 audio_enc
->codec_id
= codec_id
;
1968 audio_enc
->codec_type
= CODEC_TYPE_AUDIO
;
1970 audio_enc
->bit_rate
= audio_bit_rate
;
1971 audio_enc
->sample_rate
= audio_sample_rate
;
1972 /* For audio codecs other than AC3 we limit */
1973 /* the number of coded channels to stereo */
1974 if (audio_channels
> 2 && codec_id
!= CODEC_ID_AC3
) {
1975 audio_enc
->channels
= 2;
1977 audio_enc
->channels
= audio_channels
;
1978 oc
->streams
[nb_streams
] = st
;
1982 oc
->nb_streams
= nb_streams
;
1985 fprintf(stderr
, "No audio or video streams available\n");
1990 pstrcpy(oc
->title
, sizeof(oc
->title
), str_title
);
1992 pstrcpy(oc
->author
, sizeof(oc
->author
), str_author
);
1994 pstrcpy(oc
->copyright
, sizeof(oc
->copyright
), str_copyright
);
1996 pstrcpy(oc
->comment
, sizeof(oc
->comment
), str_comment
);
1999 output_files
[nb_output_files
] = oc
;
2000 /* dump the file content */
2001 dump_format(oc
, nb_output_files
, filename
, 1);
2004 strcpy(oc
->filename
, filename
);
2006 /* check filename in case of an image number is expected */
2007 if (oc
->oformat
->flags
& AVFMT_NEEDNUMBER
) {
2008 if (filename_number_test(oc
->filename
) < 0) {
2009 print_error(oc
->filename
, AVERROR_NUMEXPECTED
);
2014 if (!(oc
->oformat
->flags
& AVFMT_NOFILE
)) {
2015 /* test if it already exists to avoid loosing precious files */
2016 if (!file_overwrite
&&
2017 (strchr(filename
, ':') == NULL
||
2018 strstart(filename
, "file:", NULL
))) {
2019 if (url_exist(filename
)) {
2022 printf("File '%s' already exists. Overwrite ? [y/N] ", filename
);
2025 if (toupper(c
) != 'Y') {
2026 fprintf(stderr
, "Not overwriting - exiting\n");
2033 if (url_fopen(&oc
->pb
, filename
, URL_WRONLY
) < 0) {
2034 fprintf(stderr
, "Could not open '%s'\n", filename
);
2039 /* reset some options */
2040 file_oformat
= NULL
;
2041 file_iformat
= NULL
;
2044 audio_codec_id
= CODEC_ID_NONE
;
2045 video_codec_id
= CODEC_ID_NONE
;
2048 /* prepare dummy protocols for grab */
2049 void prepare_grab(void)
2051 int has_video
, has_audio
, i
, j
;
2052 AVFormatContext
*oc
;
2053 AVFormatContext
*ic
;
2054 AVFormatParameters ap1
, *ap
= &ap1
;
2056 /* see if audio/video inputs are needed */
2059 memset(ap
, 0, sizeof(*ap
));
2060 for(j
=0;j
<nb_output_files
;j
++) {
2061 oc
= output_files
[j
];
2062 for(i
=0;i
<oc
->nb_streams
;i
++) {
2063 AVCodecContext
*enc
= &oc
->streams
[i
]->codec
;
2064 switch(enc
->codec_type
) {
2065 case CODEC_TYPE_AUDIO
:
2066 if (enc
->sample_rate
> ap
->sample_rate
)
2067 ap
->sample_rate
= enc
->sample_rate
;
2068 if (enc
->channels
> ap
->channels
)
2069 ap
->channels
= enc
->channels
;
2072 case CODEC_TYPE_VIDEO
:
2073 if (enc
->width
> ap
->width
)
2074 ap
->width
= enc
->width
;
2075 if (enc
->height
> ap
->height
)
2076 ap
->height
= enc
->height
;
2077 if (enc
->frame_rate
> ap
->frame_rate
)
2078 ap
->frame_rate
= enc
->frame_rate
;
2087 if (has_video
== 0 && has_audio
== 0) {
2088 fprintf(stderr
, "Output file must have at least one audio or video stream\n");
2093 AVInputFormat
*fmt1
;
2094 fmt1
= av_find_input_format("video_grab_device");
2095 if (av_open_input_file(&ic
, "", fmt1
, 0, ap
) < 0) {
2096 fprintf(stderr
, "Could not find video grab device\n");
2099 /* by now video grab has one stream */
2100 ic
->streams
[0]->r_frame_rate
= ap
->frame_rate
;
2101 input_files
[nb_input_files
] = ic
;
2102 dump_format(ic
, nb_input_files
, v4l_device
, 0);
2106 AVInputFormat
*fmt1
;
2107 fmt1
= av_find_input_format("audio_device");
2108 if (av_open_input_file(&ic
, "", fmt1
, 0, ap
) < 0) {
2109 fprintf(stderr
, "Could not find audio grab device\n");
2112 input_files
[nb_input_files
] = ic
;
2113 dump_format(ic
, nb_input_files
, audio_device
, 0);
2118 /* open the necessary output devices for playing */
2119 void prepare_play(void)
2121 file_iformat
= NULL
;
2122 file_oformat
= guess_format("audio_device", NULL
, NULL
);
2123 if (!file_oformat
) {
2124 fprintf(stderr
, "Could not find audio device\n");
2128 opt_output_file(audio_device
);
2132 #ifndef CONFIG_WIN32
2133 INT64
getutime(void)
2135 struct rusage rusage
;
2137 getrusage(RUSAGE_SELF
, &rusage
);
2138 return (rusage
.ru_utime
.tv_sec
* 1000000LL) + rusage
.ru_utime
.tv_usec
;
2141 INT64
getutime(void)
2143 return av_gettime();
2147 extern int ffm_nopts
;
2149 void opt_bitexact(void)
2151 avcodec_set_bit_exact();
2152 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2156 void show_formats(void)
2158 AVInputFormat
*ifmt
;
2159 AVOutputFormat
*ofmt
;
2164 printf("File formats:\n");
2165 printf(" Encoding:");
2166 for(ofmt
= first_oformat
; ofmt
!= NULL
; ofmt
= ofmt
->next
) {
2167 printf(" %s", ofmt
->name
);
2170 printf(" Decoding:");
2171 for(ifmt
= first_iformat
; ifmt
!= NULL
; ifmt
= ifmt
->next
) {
2172 printf(" %s", ifmt
->name
);
2176 printf("Codecs:\n");
2177 printf(" Encoders:");
2178 for(p
= first_avcodec
; p
!= NULL
; p
= p
->next
) {
2180 printf(" %s", p
->name
);
2184 printf(" Decoders:");
2185 for(p
= first_avcodec
; p
!= NULL
; p
= p
->next
) {
2187 printf(" %s", p
->name
);
2191 printf("Supported file protocols:");
2192 for(up
= first_protocol
; up
!= NULL
; up
= up
->next
)
2193 printf(" %s:", up
->name
);
2196 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2197 printf("Motion estimation methods:");
2201 if ((pp
- motion_str
+ 1) == ME_ZERO
)
2202 printf("(fastest)");
2203 else if ((pp
- motion_str
+ 1) == ME_FULL
)
2204 printf("(slowest)");
2205 else if ((pp
- motion_str
+ 1) == ME_EPZS
)
2206 printf("(default)");
2213 void show_help(void)
2216 const OptionDef
*po
;
2219 prog
= do_play ?
"ffplay" : "ffmpeg";
2221 printf("%s version " FFMPEG_VERSION
", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2225 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2226 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2228 printf("usage: ffplay [options] input_file...\n"
2229 "Simple audio player\n");
2233 "Main options are:\n");
2236 printf("\nAdvanced options are:\n");
2237 for(po
= options
; po
->name
!= NULL
; po
++) {
2239 expert
= (po
->flags
& OPT_EXPERT
) != 0;
2241 strcpy(buf
, po
->name
);
2242 if (po
->flags
& HAS_ARG
) {
2244 strcat(buf
, po
->argname
);
2246 printf("-%-17s %s\n", buf
, po
->help
);
2254 const OptionDef options
[] = {
2255 { "L", 0, {(void*)show_licence
}, "show license" },
2256 { "h", 0, {(void*)show_help
}, "show help" },
2257 { "formats", 0, {(void*)show_formats
}, "show available formats, codecs, protocols, ..." },
2258 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
2259 { "vcd", OPT_BOOL
, {(void*)&mpeg_vcd
}, "output Video CD MPEG-PS compliant file" },
2260 { "i", HAS_ARG
, {(void*)opt_input_file
}, "input file name", "filename" },
2261 { "y", OPT_BOOL
, {(void*)&file_overwrite
}, "overwrite output files" },
2262 { "map", HAS_ARG
| OPT_EXPERT
, {(void*)opt_map
}, "set input stream mapping", "file:stream" },
2263 { "t", HAS_ARG
, {(void*)opt_recording_time
}, "set the recording time", "duration" },
2264 { "title", HAS_ARG
| OPT_STRING
, {(void*)&str_title
}, "set the title", "string" },
2265 { "author", HAS_ARG
| OPT_STRING
, {(void*)&str_author
}, "set the author", "string" },
2266 { "copyright", HAS_ARG
| OPT_STRING
, {(void*)&str_copyright
}, "set the copyright", "string" },
2267 { "comment", HAS_ARG
| OPT_STRING
, {(void*)&str_comment
}, "set the comment", "string" },
2269 { "b", HAS_ARG
, {(void*)opt_video_bitrate
}, "set video bitrate (in kbit/s)", "bitrate" },
2270 { "r", HAS_ARG
, {(void*)opt_frame_rate
}, "set frame rate (in Hz)", "rate" },
2271 { "s", HAS_ARG
, {(void*)opt_frame_size
}, "set frame size (WxH or abbreviation)", "size" },
2272 { "croptop", HAS_ARG
, {(void*)opt_frame_crop_top
}, "set top crop band size (in pixels)", "size" },
2273 { "cropbottom", HAS_ARG
, {(void*)opt_frame_crop_bottom
}, "set bottom crop band size (in pixels)", "size" },
2274 { "cropleft", HAS_ARG
, {(void*)opt_frame_crop_left
}, "set left crop band size (in pixels)", "size" },
2275 { "cropright", HAS_ARG
, {(void*)opt_frame_crop_right
}, "set right crop band size (in pixels)", "size" },
2276 { "g", HAS_ARG
| OPT_EXPERT
, {(void*)opt_gop_size
}, "set the group of picture size", "gop_size" },
2277 { "intra", OPT_BOOL
| OPT_EXPERT
, {(void*)&intra_only
}, "use only intra frames"},
2278 { "vn", OPT_BOOL
, {(void*)&video_disable
}, "disable video" },
2279 { "qscale", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qscale
}, "use fixed video quantiser scale (VBR)", "q" },
2280 { "qmin", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qmin
}, "min video quantiser scale (VBR)", "q" },
2281 { "qmax", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qmax
}, "max video quantiser scale (VBR)", "q" },
2282 { "qdiff", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qdiff
}, "max difference between the quantiser scale (VBR)", "q" },
2283 { "qblur", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qblur
}, "video quantiser scale blur (VBR)", "blur" },
2284 { "qcomp", HAS_ARG
| OPT_EXPERT
, {(void*)opt_qcomp
}, "video quantiser scale compression (VBR)", "compression" },
2285 { "b_qfactor", HAS_ARG
| OPT_EXPERT
, {(void*)opt_b_qfactor
}, "qp factor between p and b frames", "factor" },
2286 { "i_qfactor", HAS_ARG
| OPT_EXPERT
, {(void*)opt_i_qfactor
}, "qp factor between p and i frames", "factor" },
2287 { "b_qoffset", HAS_ARG
| OPT_EXPERT
, {(void*)opt_b_qoffset
}, "qp offset between p and b frames", "offset" },
2288 { "i_qoffset", HAS_ARG
| OPT_EXPERT
, {(void*)opt_i_qoffset
}, "qp offset between p and i frames", "offset" },
2289 { "rc_eq", HAS_ARG
| OPT_EXPERT
, {(void*)opt_video_rc_eq
}, "", "equation" },
2290 { "bt", HAS_ARG
, {(void*)opt_video_bitrate_tolerance
}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2291 { "maxrate", HAS_ARG
, {(void*)opt_video_bitrate_max
}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2292 { "minrate", HAS_ARG
, {(void*)opt_video_bitrate_min
}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2293 { "bufsize", HAS_ARG
, {(void*)opt_video_buffer_size
}, "set ratecontrol buffere size (in kbit)", "size" },
2294 { "vd", HAS_ARG
| OPT_EXPERT
, {(void*)opt_video_device
}, "set video grab device", "device" },
2295 { "vcodec", HAS_ARG
| OPT_EXPERT
, {(void*)opt_video_codec
}, "force video codec", "codec" },
2296 { "me", HAS_ARG
| OPT_EXPERT
, {(void*)opt_motion_estimation
}, "set motion estimation method",
2298 { "dct_algo", HAS_ARG
| OPT_EXPERT
, {(void*)opt_dct_algo
}, "set dct algo", "algo" },
2299 { "idct_algo", HAS_ARG
| OPT_EXPERT
, {(void*)opt_idct_algo
}, "set idct algo", "algo" },
2300 { "er", HAS_ARG
| OPT_EXPERT
, {(void*)opt_error_resilience
}, "set error resilience", "" },
2301 { "bf", HAS_ARG
| OPT_EXPERT
, {(void*)opt_b_frames
}, "use 'frames' B frames (only MPEG-4)", "frames" },
2302 { "hq", OPT_BOOL
| OPT_EXPERT
, {(void*)&use_hq
}, "activate high quality settings" },
2303 { "4mv", OPT_BOOL
| OPT_EXPERT
, {(void*)&use_4mv
}, "use four motion vector by macroblock (only MPEG-4)" },
2304 { "part", OPT_BOOL
| OPT_EXPERT
, {(void*)&use_part
}, "use data partitioning (only MPEG-4)" },
2305 { "bug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_workaround_bugs
}, "workaround not auto detected encoder bugs", "param" },
2306 { "ps", HAS_ARG
| OPT_EXPERT
, {(void*)opt_packet_size
}, "packet size", "size in bits" },
2307 { "sameq", OPT_BOOL
, {(void*)&same_quality
},
2308 "use same video quality as source (implies VBR)" },
2310 { "ab", HAS_ARG
, {(void*)opt_audio_bitrate
}, "set audio bitrate (in kbit/s)", "bitrate", },
2311 { "ar", HAS_ARG
, {(void*)opt_audio_rate
}, "set audio sampling rate (in Hz)", "rate" },
2312 { "ac", HAS_ARG
, {(void*)opt_audio_channels
}, "set number of audio channels", "channels" },
2313 { "an", OPT_BOOL
, {(void*)&audio_disable
}, "disable audio" },
2314 { "ad", HAS_ARG
| OPT_EXPERT
, {(void*)opt_audio_device
}, "set audio device", "device" },
2315 { "acodec", HAS_ARG
| OPT_EXPERT
, {(void*)opt_audio_codec
}, "force audio codec", "codec" },
2316 { "deinterlace", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_deinterlace
},
2317 "deinterlace pictures" },
2318 { "benchmark", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_benchmark
},
2319 "add timings for benchmarking" },
2320 { "hex", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_hex_dump
},
2321 "dump each input packet" },
2322 { "psnr", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_psnr
}, "calculate PSNR of compressed frames" },
2323 { "vstats", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_vstats
}, "dump video coding statistics to file" },
2324 { "bitexact", OPT_EXPERT
, {(void*)opt_bitexact
}, "only use bit exact algorithms (for codec testing)" },
2328 int main(int argc
, char **argv
)
2331 const char *opt
, *arg
;
2332 const OptionDef
*po
;
2337 /* detect if invoked as player */
2338 i
= strlen(argv
[0]);
2339 if (i
>= 6 && !strcmp(argv
[0] + i
- 6, "ffplay"))
2347 while (optindex
< argc
) {
2348 opt
= argv
[optindex
++];
2350 if (opt
[0] == '-' && opt
[1] != '\0') {
2352 while (po
->name
!= NULL
) {
2353 if (!strcmp(opt
+ 1, po
->name
))
2358 fprintf(stderr
, "%s: unrecognized option '%s'\n", argv
[0], opt
);
2362 if (po
->flags
& HAS_ARG
)
2363 arg
= argv
[optindex
++];
2364 if (po
->flags
& OPT_STRING
) {
2367 *po
->u
.str_arg
= str
;
2368 } else if (po
->flags
& OPT_BOOL
) {
2371 po
->u
.func_arg(arg
);
2375 opt_output_file(opt
);
2377 opt_input_file(opt
);
2384 /* file converter / grab */
2385 if (nb_output_files
<= 0) {
2386 fprintf(stderr
, "Must supply at least one output file\n");
2390 if (nb_input_files
== 0) {
2395 if (nb_input_files
<= 0) {
2396 fprintf(stderr
, "Must supply at least one input file\n");
2403 av_encode(output_files
, nb_output_files
, input_files
, nb_input_files
,
2404 stream_maps
, nb_stream_maps
);
2405 ti
= getutime() - ti
;
2407 printf("bench: utime=%0.3fs\n", ti
/ 1000000.0);
2411 for(i
=0;i
<nb_output_files
;i
++) {
2412 if (!(output_files
[i
]->oformat
->flags
& AVFMT_NOFILE
))
2413 url_fclose(&output_files
[i
]->pb
);
2415 for(i
=0;i
<nb_input_files
;i
++)
2416 av_close_input_file(input_files
[i
]);