Commit | Line | Data |
---|---|---|
85f07f22 FB |
1 | /* |
2 | * FFmpeg main | |
01310af2 | 3 | * Copyright (c) 2000-2003 Fabrice Bellard |
85f07f22 | 4 | * |
bf5af568 FB |
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. | |
85f07f22 | 9 | * |
bf5af568 | 10 | * This library is distributed in the hope that it will be useful, |
85f07f22 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bf5af568 FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
85f07f22 | 14 | * |
bf5af568 FB |
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 | |
85f07f22 | 18 | */ |
daf8e955 FB |
19 | #define HAVE_AV_CONFIG_H |
20 | #include "avformat.h" | |
10d104e4 | 21 | #include "framehook.h" |
daf8e955 | 22 | |
bdc4796f | 23 | #ifndef CONFIG_WIN32 |
85f07f22 FB |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
26 | #include <sys/ioctl.h> | |
85f07f22 | 27 | #include <sys/time.h> |
85f07f22 | 28 | #include <termios.h> |
5727b222 | 29 | #include <sys/resource.h> |
9680a722 | 30 | #include <signal.h> |
bdc4796f | 31 | #endif |
f3ec2d46 SG |
32 | #ifdef CONFIG_OS2 |
33 | #include <sys/types.h> | |
34 | #include <sys/select.h> | |
35 | #include <stdlib.h> | |
36 | #endif | |
bf5af568 | 37 | #include <time.h> |
85f07f22 | 38 | |
01310af2 FB |
39 | #include "cmdutils.h" |
40 | ||
8aa1e3da MN |
41 | #if !defined(INFINITY) && defined(HUGE_VAL) |
42 | #define INFINITY HUGE_VAL | |
43 | #endif | |
85f07f22 | 44 | |
85f07f22 FB |
45 | /* select an input stream for an output stream */ |
46 | typedef struct AVStreamMap { | |
47 | int file_index; | |
48 | int stream_index; | |
49 | } AVStreamMap; | |
50 | ||
51 | extern const OptionDef options[]; | |
52 | ||
02d504a7 FB |
53 | static void show_help(void); |
54 | static void show_license(void); | |
85f07f22 FB |
55 | |
56 | #define MAX_FILES 20 | |
57 | ||
58 | static AVFormatContext *input_files[MAX_FILES]; | |
59 | static int nb_input_files = 0; | |
60 | ||
61 | static AVFormatContext *output_files[MAX_FILES]; | |
62 | static int nb_output_files = 0; | |
63 | ||
64 | static AVStreamMap stream_maps[MAX_FILES]; | |
65 | static int nb_stream_maps; | |
66 | ||
79fdaa4c FB |
67 | static AVInputFormat *file_iformat; |
68 | static AVOutputFormat *file_oformat; | |
817b23ff | 69 | static AVImageFormat *image_format; |
85f07f22 FB |
70 | static int frame_width = 160; |
71 | static int frame_height = 128; | |
880e8ba7 | 72 | static float frame_aspect_ratio = 0; |
63167088 | 73 | static enum PixelFormat frame_pix_fmt = PIX_FMT_YUV420P; |
ab6d194a MN |
74 | static int frame_topBand = 0; |
75 | static int frame_bottomBand = 0; | |
76 | static int frame_leftBand = 0; | |
77 | static int frame_rightBand = 0; | |
14bea432 MN |
78 | static int frame_rate = 25; |
79 | static int frame_rate_base = 1; | |
3aa102be MN |
80 | static int video_bit_rate = 200*1000; |
81 | static int video_bit_rate_tolerance = 4000*1000; | |
158c7f05 | 82 | static float video_qscale = 0; |
3aa102be MN |
83 | static int video_qmin = 2; |
84 | static int video_qmax = 31; | |
17a70fde MN |
85 | static int video_mb_qmin = 2; |
86 | static int video_mb_qmax = 31; | |
9cdd6a24 MN |
87 | static int video_qdiff = 3; |
88 | static float video_qblur = 0.5; | |
89 | static float video_qcomp = 0.5; | |
84f608f4 VM |
90 | static uint16_t *intra_matrix = NULL; |
91 | static uint16_t *inter_matrix = NULL; | |
ac2830ec | 92 | #if 0 //experimental, (can be removed) |
3aa102be MN |
93 | static float video_rc_qsquish=1.0; |
94 | static float video_rc_qmod_amp=0; | |
95 | static int video_rc_qmod_freq=0; | |
ac2830ec | 96 | #endif |
3aa102be MN |
97 | static char *video_rc_override_string=NULL; |
98 | static char *video_rc_eq="tex^qComp"; | |
99 | static int video_rc_buffer_size=0; | |
100 | static float video_rc_buffer_aggressivity=1.0; | |
101 | static int video_rc_max_rate=0; | |
102 | static int video_rc_min_rate=0; | |
103 | static float video_rc_initial_cplx=0; | |
104 | static float video_b_qfactor = 1.25; | |
105 | static float video_b_qoffset = 1.25; | |
b3a391e8 | 106 | static float video_i_qfactor = -0.8; |
3aa102be | 107 | static float video_i_qoffset = 0.0; |
303e50e6 MN |
108 | static int video_intra_quant_bias= FF_DEFAULT_QUANT_BIAS; |
109 | static int video_inter_quant_bias= FF_DEFAULT_QUANT_BIAS; | |
3bea5386 | 110 | static int me_method = ME_EPZS; |
85f07f22 FB |
111 | static int video_disable = 0; |
112 | static int video_codec_id = CODEC_ID_NONE; | |
113 | static int same_quality = 0; | |
bc6caae2 | 114 | static int b_frames = 0; |
7d1c3fc1 | 115 | static int mb_decision = FF_MB_DECISION_SIMPLE; |
622348f9 | 116 | static int ildct_cmp = FF_CMP_VSAD; |
5d43635e MN |
117 | static int mb_cmp = FF_CMP_SAD; |
118 | static int sub_cmp = FF_CMP_SAD; | |
119 | static int cmp = FF_CMP_SAD; | |
29da453b | 120 | static int use_4mv = 0; |
8e2162f0 | 121 | static int use_obmc = 0; |
21e59552 | 122 | static int use_aic = 0; |
dba019da | 123 | static int use_aiv = 0; |
21e59552 | 124 | static int use_umv = 0; |
bb198e19 | 125 | static int use_alt_scan = 0; |
c0baa56a | 126 | static int use_trell = 0; |
baaf3f46 | 127 | static int use_scan_offset = 0; |
77ea0d4b | 128 | static int qns = 0; |
303e50e6 | 129 | static int closed_gop = 0; |
cfcf0ffd | 130 | static int do_deinterlace = 0; |
bb198e19 MN |
131 | static int do_interlace_dct = 0; |
132 | static int do_interlace_me = 0; | |
4d2858de MN |
133 | static int workaround_bugs = FF_BUG_AUTODETECT; |
134 | static int error_resilience = 2; | |
135 | static int error_concealment = 3; | |
463678ac | 136 | static int dct_algo = 0; |
2ad1516a | 137 | static int idct_algo = 0; |
1dbb6d90 MN |
138 | static int use_part = 0; |
139 | static int packet_size = 0; | |
7ebfc0ea | 140 | static int error_rate = 0; |
f560dd82 | 141 | static int strict = 0; |
bb198e19 MN |
142 | static int top_field_first = -1; |
143 | static int noise_reduction = 0; | |
303e50e6 | 144 | static int sc_threshold = 0; |
59b571c1 | 145 | static int debug = 0; |
0c9bbaec | 146 | static int debug_mv = 0; |
b1b77fe9 | 147 | extern int loop_input; /* currently a hack */ |
85f07f22 FB |
148 | |
149 | static int gop_size = 12; | |
150 | static int intra_only = 0; | |
151 | static int audio_sample_rate = 44100; | |
152 | static int audio_bit_rate = 64000; | |
153 | static int audio_disable = 0; | |
154 | static int audio_channels = 1; | |
155 | static int audio_codec_id = CODEC_ID_NONE; | |
156 | ||
0c1a9eda | 157 | static int64_t recording_time = 0; |
8831db5c | 158 | static int64_t start_time = 0; |
85f07f22 FB |
159 | static int file_overwrite = 0; |
160 | static char *str_title = NULL; | |
161 | static char *str_author = NULL; | |
162 | static char *str_copyright = NULL; | |
163 | static char *str_comment = NULL; | |
5727b222 | 164 | static int do_benchmark = 0; |
a0663ba4 | 165 | static int do_hex_dump = 0; |
254abc2e | 166 | static int do_pkt_dump = 0; |
43f1708f | 167 | static int do_psnr = 0; |
ce7c56c2 | 168 | static int do_vstats = 0; |
5abdb4b1 | 169 | static int do_pass = 0; |
b0368839 | 170 | static int bitexact = 0; |
5abdb4b1 | 171 | static char *pass_logfilename = NULL; |
1629626f FB |
172 | static int audio_stream_copy = 0; |
173 | static int video_stream_copy = 0; | |
5abdb4b1 | 174 | |
bdfcbbed MK |
175 | static int rate_emu = 0; |
176 | ||
8aa3ee32 | 177 | static char *video_grab_format = "video4linux"; |
79a7c268 | 178 | static char *video_device = NULL; |
a5df11ab | 179 | static int video_channel = 0; |
e3ee3283 | 180 | static char *video_standard = "ntsc"; |
79a7c268 | 181 | |
8aa3ee32 | 182 | static char *audio_grab_format = "audio_device"; |
79a7c268 | 183 | static char *audio_device = NULL; |
8aa3ee32 | 184 | |
d9a916e2 | 185 | static int using_stdin = 0; |
bee0d9e5 | 186 | static int using_vhook = 0; |
f068206e | 187 | static int verbose = 1; |
d9a916e2 | 188 | |
5abdb4b1 | 189 | #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass" |
85f07f22 FB |
190 | |
191 | typedef struct AVOutputStream { | |
192 | int file_index; /* file index */ | |
193 | int index; /* stream index in the output file */ | |
194 | int source_index; /* AVInputStream index */ | |
195 | AVStream *st; /* stream in the output file */ | |
ec5517d5 FB |
196 | int encoding_needed; /* true if encoding needed for this stream */ |
197 | int frame_number; | |
198 | /* input pts and corresponding output pts | |
199 | for A/V sync */ | |
200 | double sync_ipts; | |
c11ef252 | 201 | double sync_ipts_offset; |
0c1a9eda | 202 | int64_t sync_opts; |
85f07f22 | 203 | /* video only */ |
34b10a57 D |
204 | int video_resample; /* video_resample and video_crop are mutually exclusive */ |
205 | AVPicture pict_tmp; /* temporary image for resampling */ | |
85f07f22 | 206 | ImgReSampleContext *img_resample_ctx; /* for image resampling */ |
34b10a57 D |
207 | |
208 | int video_crop; /* video_resample and video_crop are mutually exclusive */ | |
209 | int topBand; /* cropping area sizes */ | |
210 | int leftBand; | |
85f07f22 FB |
211 | |
212 | /* audio only */ | |
213 | int audio_resample; | |
214 | ReSampleContext *resample; /* for audio resampling */ | |
215 | FifoBuffer fifo; /* for compression: one audio fifo per codec */ | |
5abdb4b1 | 216 | FILE *logfile; |
85f07f22 FB |
217 | } AVOutputStream; |
218 | ||
219 | typedef struct AVInputStream { | |
220 | int file_index; | |
221 | int index; | |
222 | AVStream *st; | |
223 | int discard; /* true if stream data should be discarded */ | |
224 | int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */ | |
0c1a9eda | 225 | int64_t sample_index; /* current sample */ |
bdfcbbed MK |
226 | |
227 | int64_t start; /* time when read started */ | |
228 | unsigned long frame; /* current frame */ | |
254abc2e FB |
229 | int64_t next_pts; /* synthetic pts for cases where pkt.pts |
230 | is not defined */ | |
e7d0374f | 231 | int64_t pts; /* current pts */ |
85f07f22 FB |
232 | } AVInputStream; |
233 | ||
234 | typedef struct AVInputFile { | |
235 | int eof_reached; /* true if eof reached */ | |
236 | int ist_index; /* index of first stream in ist_table */ | |
237 | int buffer_size; /* current total buffer size */ | |
238 | int buffer_size_max; /* buffer size at which we consider we can stop | |
239 | buffering */ | |
79fdaa4c | 240 | int nb_streams; /* nb streams we are aware of */ |
85f07f22 FB |
241 | } AVInputFile; |
242 | ||
bdc4796f FB |
243 | #ifndef CONFIG_WIN32 |
244 | ||
85f07f22 FB |
245 | /* init terminal so that we can grab keys */ |
246 | static struct termios oldtty; | |
247 | ||
248 | static void term_exit(void) | |
249 | { | |
250 | tcsetattr (0, TCSANOW, &oldtty); | |
251 | } | |
252 | ||
9680a722 RP |
253 | static volatile sig_atomic_t received_sigterm = 0; |
254 | ||
255 | static void | |
256 | sigterm_handler(int sig) | |
257 | { | |
258 | received_sigterm = sig; | |
259 | term_exit(); | |
260 | } | |
261 | ||
85f07f22 FB |
262 | static void term_init(void) |
263 | { | |
264 | struct termios tty; | |
265 | ||
266 | tcgetattr (0, &tty); | |
267 | oldtty = tty; | |
268 | ||
269 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
270 | |INLCR|IGNCR|ICRNL|IXON); | |
271 | tty.c_oflag |= OPOST; | |
272 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
273 | tty.c_cflag &= ~(CSIZE|PARENB); | |
274 | tty.c_cflag |= CS8; | |
275 | tty.c_cc[VMIN] = 1; | |
276 | tty.c_cc[VTIME] = 0; | |
277 | ||
278 | tcsetattr (0, TCSANOW, &tty); | |
279 | ||
9680a722 RP |
280 | signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ |
281 | signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ | |
282 | signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ | |
283 | /* | |
284 | register a function to be called at normal program termination | |
285 | */ | |
85f07f22 | 286 | atexit(term_exit); |
9ddd71fc FR |
287 | #ifdef CONFIG_BEOS_NETSERVER |
288 | fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); | |
289 | #endif | |
85f07f22 FB |
290 | } |
291 | ||
292 | /* read a key without blocking */ | |
293 | static int read_key(void) | |
294 | { | |
9ddd71fc | 295 | int n = 1; |
85f07f22 | 296 | unsigned char ch; |
9ddd71fc FR |
297 | #ifndef CONFIG_BEOS_NETSERVER |
298 | struct timeval tv; | |
85f07f22 FB |
299 | fd_set rfds; |
300 | ||
301 | FD_ZERO(&rfds); | |
302 | FD_SET(0, &rfds); | |
303 | tv.tv_sec = 0; | |
304 | tv.tv_usec = 0; | |
305 | n = select(1, &rfds, NULL, NULL, &tv); | |
9ddd71fc | 306 | #endif |
85f07f22 | 307 | if (n > 0) { |
cb09b2ed PG |
308 | n = read(0, &ch, 1); |
309 | if (n == 1) | |
85f07f22 | 310 | return ch; |
cb09b2ed PG |
311 | |
312 | return n; | |
85f07f22 FB |
313 | } |
314 | return -1; | |
315 | } | |
316 | ||
a38469e1 | 317 | #else |
85f07f22 | 318 | |
bee0d9e5 CY |
319 | static volatile int received_sigterm = 0; |
320 | ||
a38469e1 FB |
321 | /* no interactive support */ |
322 | static void term_exit(void) | |
85f07f22 | 323 | { |
a38469e1 | 324 | } |
85f07f22 | 325 | |
a38469e1 FB |
326 | static void term_init(void) |
327 | { | |
328 | } | |
85f07f22 | 329 | |
a38469e1 FB |
330 | static int read_key(void) |
331 | { | |
cb09b2ed | 332 | return 0; |
85f07f22 FB |
333 | } |
334 | ||
a38469e1 | 335 | #endif |
bdc4796f | 336 | |
b29f97d1 | 337 | static int read_ffserver_streams(AVFormatContext *s, const char *filename) |
85f07f22 | 338 | { |
79fdaa4c | 339 | int i, err; |
85f07f22 FB |
340 | AVFormatContext *ic; |
341 | ||
79fdaa4c FB |
342 | err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL); |
343 | if (err < 0) | |
344 | return err; | |
85f07f22 FB |
345 | /* copy stream format */ |
346 | s->nb_streams = ic->nb_streams; | |
347 | for(i=0;i<ic->nb_streams;i++) { | |
348 | AVStream *st; | |
1e491e29 | 349 | |
e1031171 | 350 | st = av_mallocz(sizeof(AVStream)); |
85f07f22 FB |
351 | memcpy(st, ic->streams[i], sizeof(AVStream)); |
352 | s->streams[i] = st; | |
353 | } | |
354 | ||
355 | av_close_input_file(ic); | |
356 | return 0; | |
357 | } | |
358 | ||
817b23ff | 359 | #define MAX_AUDIO_PACKET_SIZE (128 * 1024) |
85f07f22 FB |
360 | |
361 | static void do_audio_out(AVFormatContext *s, | |
362 | AVOutputStream *ost, | |
363 | AVInputStream *ist, | |
364 | unsigned char *buf, int size) | |
365 | { | |
0c1a9eda | 366 | uint8_t *buftmp; |
d66c7abc SC |
367 | static uint8_t *audio_buf = NULL; |
368 | static uint8_t *audio_out = NULL; | |
558eae03 | 369 | const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE; |
d66c7abc | 370 | |
85f07f22 FB |
371 | int size_out, frame_bytes, ret; |
372 | AVCodecContext *enc; | |
373 | ||
d66c7abc SC |
374 | /* SC: dynamic allocation of buffers */ |
375 | if (!audio_buf) | |
376 | audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE); | |
377 | if (!audio_out) | |
558eae03 | 378 | audio_out = av_malloc(audio_out_size); |
d66c7abc SC |
379 | if (!audio_buf || !audio_out) |
380 | return; /* Should signal an error ! */ | |
381 | ||
382 | ||
85f07f22 FB |
383 | enc = &ost->st->codec; |
384 | ||
385 | if (ost->audio_resample) { | |
386 | buftmp = audio_buf; | |
387 | size_out = audio_resample(ost->resample, | |
388 | (short *)buftmp, (short *)buf, | |
389 | size / (ist->st->codec.channels * 2)); | |
390 | size_out = size_out * enc->channels * 2; | |
391 | } else { | |
392 | buftmp = buf; | |
393 | size_out = size; | |
394 | } | |
395 | ||
396 | /* now encode as many frames as possible */ | |
a0663ba4 | 397 | if (enc->frame_size > 1) { |
85f07f22 FB |
398 | /* output resampled raw samples */ |
399 | fifo_write(&ost->fifo, buftmp, size_out, | |
400 | &ost->fifo.wptr); | |
401 | ||
402 | frame_bytes = enc->frame_size * 2 * enc->channels; | |
403 | ||
404 | while (fifo_read(&ost->fifo, audio_buf, frame_bytes, | |
405 | &ost->fifo.rptr) == 0) { | |
558eae03 | 406 | ret = avcodec_encode_audio(enc, audio_out, audio_out_size, |
a0663ba4 | 407 | (short *)audio_buf); |
ec5517d5 | 408 | av_write_frame(s, ost->index, audio_out, ret); |
85f07f22 FB |
409 | } |
410 | } else { | |
a0663ba4 FB |
411 | /* output a pcm frame */ |
412 | /* XXX: change encoding codec API to avoid this ? */ | |
413 | switch(enc->codec->id) { | |
414 | case CODEC_ID_PCM_S16LE: | |
415 | case CODEC_ID_PCM_S16BE: | |
416 | case CODEC_ID_PCM_U16LE: | |
417 | case CODEC_ID_PCM_U16BE: | |
418 | break; | |
419 | default: | |
420 | size_out = size_out >> 1; | |
421 | break; | |
422 | } | |
423 | ret = avcodec_encode_audio(enc, audio_out, size_out, | |
ff29712a | 424 | (short *)buftmp); |
ec5517d5 | 425 | av_write_frame(s, ost->index, audio_out, ret); |
85f07f22 FB |
426 | } |
427 | } | |
428 | ||
10d104e4 PG |
429 | static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp) |
430 | { | |
431 | AVCodecContext *dec; | |
432 | AVPicture *picture2; | |
433 | AVPicture picture_tmp; | |
0c1a9eda | 434 | uint8_t *buf = 0; |
10d104e4 PG |
435 | |
436 | dec = &ist->st->codec; | |
437 | ||
438 | /* deinterlace : must be done before any resize */ | |
bee0d9e5 | 439 | if (do_deinterlace || using_vhook) { |
10d104e4 PG |
440 | int size; |
441 | ||
442 | /* create temporary picture */ | |
443 | size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height); | |
444 | buf = av_malloc(size); | |
445 | if (!buf) | |
446 | return; | |
447 | ||
448 | picture2 = &picture_tmp; | |
449 | avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height); | |
450 | ||
dafc3856 MN |
451 | if (do_deinterlace){ |
452 | if(avpicture_deinterlace(picture2, picture, | |
453 | dec->pix_fmt, dec->width, dec->height) < 0) { | |
454 | /* if error, do not deinterlace */ | |
455 | av_free(buf); | |
456 | buf = NULL; | |
457 | picture2 = picture; | |
458 | } | |
459 | } else { | |
bee0d9e5 CY |
460 | if (img_convert(picture2, dec->pix_fmt, picture, |
461 | dec->pix_fmt, dec->width, dec->height) < 0) { | |
462 | /* if error, do not copy */ | |
463 | av_free(buf); | |
464 | buf = NULL; | |
465 | picture2 = picture; | |
466 | } | |
467 | } | |
10d104e4 PG |
468 | } else { |
469 | picture2 = picture; | |
470 | } | |
471 | ||
472 | frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height); | |
473 | ||
474 | if (picture != picture2) | |
475 | *picture = *picture2; | |
476 | *bufp = buf; | |
477 | } | |
478 | ||
ec5517d5 FB |
479 | /* we begin to correct av delay at this threshold */ |
480 | #define AV_DELAY_MAX 0.100 | |
85f07f22 FB |
481 | |
482 | static void do_video_out(AVFormatContext *s, | |
483 | AVOutputStream *ost, | |
484 | AVInputStream *ist, | |
7a0f9d7e | 485 | AVFrame *in_picture, |
ec5517d5 | 486 | int *frame_size, AVOutputStream *audio_sync) |
85f07f22 | 487 | { |
ec5517d5 | 488 | int nb_frames, i, ret; |
34b10a57 D |
489 | AVPicture *final_picture, *formatted_picture; |
490 | AVPicture picture_format_temp, picture_crop_temp; | |
303aebf9 | 491 | static uint8_t *video_buffer= NULL; |
0c1a9eda | 492 | uint8_t *buf = NULL, *buf1 = NULL; |
cfcf0ffd | 493 | AVCodecContext *enc, *dec; |
a686caf0 | 494 | enum PixelFormat target_pixfmt; |
85f07f22 | 495 | |
33a1f1a3 | 496 | #define VIDEO_BUFFER_SIZE (1024*1024) |
33a1f1a3 | 497 | |
85f07f22 | 498 | enc = &ost->st->codec; |
cfcf0ffd | 499 | dec = &ist->st->codec; |
85f07f22 | 500 | |
ec5517d5 FB |
501 | /* by default, we output a single frame */ |
502 | nb_frames = 1; | |
503 | ||
204c0f48 PG |
504 | *frame_size = 0; |
505 | ||
ec5517d5 FB |
506 | /* NOTE: the A/V sync is always done by considering the audio is |
507 | the master clock. It is suffisant for transcoding or playing, | |
508 | but not for the general case */ | |
509 | if (audio_sync) { | |
510 | /* compute the A-V delay and duplicate/remove frames if needed */ | |
e7d0374f RS |
511 | double adelta, vdelta, av_delay; |
512 | ||
513 | adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts * | |
514 | s->pts_num / s->pts_den); | |
515 | ||
516 | vdelta = ost->sync_ipts - ((double)ost->sync_opts * | |
517 | s->pts_num / s->pts_den); | |
518 | ||
519 | av_delay = adelta - vdelta; | |
520 | // printf("delay=%f\n", av_delay); | |
521 | if (av_delay < -AV_DELAY_MAX) | |
522 | nb_frames = 2; | |
523 | else if (av_delay > AV_DELAY_MAX) | |
524 | nb_frames = 0; | |
10d104e4 PG |
525 | } else { |
526 | double vdelta; | |
527 | ||
e7d0374f RS |
528 | vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset); |
529 | if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) { | |
530 | if (vdelta < -AV_DELAY_MAX) | |
531 | nb_frames = 2; | |
532 | else if (vdelta > AV_DELAY_MAX) | |
533 | nb_frames = 0; | |
534 | } else { | |
535 | ost->sync_ipts_offset -= vdelta; | |
536 | if (!ost->sync_ipts_offset) | |
537 | ost->sync_ipts_offset = 0.000001; /* one microsecond */ | |
10d104e4 | 538 | } |
ec5517d5 | 539 | } |
445f1b83 RS |
540 | |
541 | #if defined(AVSYNC_DEBUG) | |
254abc2e FB |
542 | { |
543 | static char *action[] = { "drop frame", "copy frame", "dup frame" }; | |
544 | if (audio_sync) | |
545 | fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ", | |
546 | (double) audio_sync->sync_ipts, | |
547 | (double) audio_sync->st->pts.val * s->pts_num / s->pts_den); | |
548 | fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n", | |
549 | (double) ost->sync_ipts, | |
550 | (double) ost->st->pts.val * s->pts_num / s->pts_den, | |
551 | action[nb_frames]); | |
552 | } | |
445f1b83 RS |
553 | #endif |
554 | ||
ec5517d5 | 555 | if (nb_frames <= 0) |
85f07f22 | 556 | return; |
ce7c56c2 | 557 | |
cb09b2ed | 558 | if (!video_buffer) |
ec5517d5 FB |
559 | video_buffer = av_malloc(VIDEO_BUFFER_SIZE); |
560 | if (!video_buffer) | |
561 | return; | |
c04643a2 | 562 | |
cfcf0ffd | 563 | /* convert pixel format if needed */ |
a686caf0 RS |
564 | target_pixfmt = ost->video_resample ? PIX_FMT_YUV420P : enc->pix_fmt; |
565 | if (dec->pix_fmt != target_pixfmt) { | |
cfcf0ffd FB |
566 | int size; |
567 | ||
568 | /* create temporary picture */ | |
39518b49 | 569 | size = avpicture_get_size(target_pixfmt, dec->width, dec->height); |
0f1578af | 570 | buf = av_malloc(size); |
cfcf0ffd FB |
571 | if (!buf) |
572 | return; | |
34b10a57 | 573 | formatted_picture = &picture_format_temp; |
39518b49 | 574 | avpicture_fill(formatted_picture, buf, target_pixfmt, dec->width, dec->height); |
cfcf0ffd | 575 | |
a686caf0 | 576 | if (img_convert(formatted_picture, target_pixfmt, |
7a0f9d7e | 577 | (AVPicture *)in_picture, dec->pix_fmt, |
cfcf0ffd FB |
578 | dec->width, dec->height) < 0) { |
579 | fprintf(stderr, "pixel format conversion not handled\n"); | |
580 | goto the_end; | |
581 | } | |
582 | } else { | |
7a0f9d7e | 583 | formatted_picture = (AVPicture *)in_picture; |
cfcf0ffd FB |
584 | } |
585 | ||
586 | /* XXX: resampling could be done before raw format convertion in | |
587 | some cases to go faster */ | |
588 | /* XXX: only works for YUV420P */ | |
85f07f22 | 589 | if (ost->video_resample) { |
34b10a57 D |
590 | final_picture = &ost->pict_tmp; |
591 | img_resample(ost->img_resample_ctx, final_picture, formatted_picture); | |
a686caf0 RS |
592 | if (enc->pix_fmt != PIX_FMT_YUV420P) { |
593 | int size; | |
594 | ||
595 | av_free(buf); | |
596 | /* create temporary picture */ | |
597 | size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height); | |
598 | buf = av_malloc(size); | |
599 | if (!buf) | |
600 | return; | |
601 | final_picture = &picture_format_temp; | |
602 | avpicture_fill(final_picture, buf, enc->pix_fmt, enc->width, enc->height); | |
603 | ||
604 | if (img_convert(final_picture, enc->pix_fmt, | |
605 | &ost->pict_tmp, PIX_FMT_YUV420P, | |
606 | enc->width, enc->height) < 0) { | |
607 | fprintf(stderr, "pixel format conversion not handled\n"); | |
608 | goto the_end; | |
609 | } | |
610 | } | |
34b10a57 D |
611 | } else if (ost->video_crop) { |
612 | picture_crop_temp.data[0] = formatted_picture->data[0] + | |
613 | (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand; | |
614 | ||
615 | picture_crop_temp.data[1] = formatted_picture->data[1] + | |
616 | ((ost->topBand >> 1) * formatted_picture->linesize[1]) + | |
617 | (ost->leftBand >> 1); | |
618 | ||
619 | picture_crop_temp.data[2] = formatted_picture->data[2] + | |
620 | ((ost->topBand >> 1) * formatted_picture->linesize[2]) + | |
621 | (ost->leftBand >> 1); | |
622 | ||
623 | picture_crop_temp.linesize[0] = formatted_picture->linesize[0]; | |
624 | picture_crop_temp.linesize[1] = formatted_picture->linesize[1]; | |
625 | picture_crop_temp.linesize[2] = formatted_picture->linesize[2]; | |
626 | final_picture = &picture_crop_temp; | |
85f07f22 | 627 | } else { |
34b10a57 | 628 | final_picture = formatted_picture; |
85f07f22 | 629 | } |
85f07f22 FB |
630 | /* duplicates frame if needed */ |
631 | /* XXX: pb because no interleaving */ | |
ec5517d5 | 632 | for(i=0;i<nb_frames;i++) { |
e8750b00 FR |
633 | if (s->oformat->flags & AVFMT_RAWPICTURE) { |
634 | /* raw pictures are written as AVPicture structure to | |
635 | avoid any copies. We support temorarily the older | |
636 | method. */ | |
2744ca9a RS |
637 | AVFrame* old_frame = enc->coded_frame; |
638 | enc->coded_frame = dec->coded_frame; | |
e8750b00 FR |
639 | av_write_frame(s, ost->index, |
640 | (uint8_t *)final_picture, sizeof(AVPicture)); | |
2744ca9a | 641 | enc->coded_frame = old_frame; |
e8750b00 | 642 | } else { |
492cd3a9 | 643 | AVFrame big_picture; |
1e491e29 | 644 | |
492cd3a9 | 645 | memset(&big_picture, 0, sizeof(AVFrame)); |
34b10a57 | 646 | *(AVPicture*)&big_picture= *final_picture; |
7a0f9d7e FB |
647 | /* better than nothing: use input picture interlaced |
648 | settings */ | |
649 | big_picture.interlaced_frame = in_picture->interlaced_frame; | |
bb198e19 MN |
650 | if(do_interlace_me || do_interlace_dct){ |
651 | if(top_field_first == -1) | |
652 | big_picture.top_field_first = in_picture->top_field_first; | |
653 | else | |
654 | big_picture.top_field_first = 1; | |
655 | } | |
7a0f9d7e | 656 | |
85f07f22 FB |
657 | /* handles sameq here. This is not correct because it may |
658 | not be a global option */ | |
659 | if (same_quality) { | |
1e491e29 MN |
660 | big_picture.quality = ist->st->quality; |
661 | }else | |
662 | big_picture.quality = ost->st->quality; | |
cfcf0ffd | 663 | ret = avcodec_encode_video(enc, |
33a1f1a3 | 664 | video_buffer, VIDEO_BUFFER_SIZE, |
1e491e29 | 665 | &big_picture); |
44429457 | 666 | //enc->frame_number = enc->real_pict_num; |
ec5517d5 | 667 | av_write_frame(s, ost->index, video_buffer, ret); |
ce7c56c2 | 668 | *frame_size = ret; |
a5dc85ef | 669 | //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d", |
44429457 | 670 | // enc->frame_number-1, enc->real_pict_num, ret, |
a5dc85ef | 671 | // enc->pict_type); |
5abdb4b1 FB |
672 | /* if two pass, output log */ |
673 | if (ost->logfile && enc->stats_out) { | |
674 | fprintf(ost->logfile, "%s", enc->stats_out); | |
675 | } | |
85f07f22 | 676 | } |
ec5517d5 | 677 | ost->frame_number++; |
85f07f22 | 678 | } |
ec5517d5 | 679 | the_end: |
0f1578af FB |
680 | av_free(buf); |
681 | av_free(buf1); | |
85f07f22 FB |
682 | } |
683 | ||
140cb663 MN |
684 | static double psnr(double d){ |
685 | if(d==0) return INFINITY; | |
b29f97d1 | 686 | return -10.0*log(d)/log(10.0); |
140cb663 MN |
687 | } |
688 | ||
ec5517d5 FB |
689 | static void do_video_stats(AVFormatContext *os, AVOutputStream *ost, |
690 | int frame_size) | |
ce7c56c2 J |
691 | { |
692 | static FILE *fvstats=NULL; | |
0c1a9eda | 693 | static int64_t total_size = 0; |
ce7c56c2 | 694 | char filename[40]; |
bf5af568 FB |
695 | time_t today2; |
696 | struct tm *today; | |
ce7c56c2 J |
697 | AVCodecContext *enc; |
698 | int frame_number; | |
0c1a9eda | 699 | int64_t ti; |
ce7c56c2 J |
700 | double ti1, bitrate, avg_bitrate; |
701 | ||
702 | if (!fvstats) { | |
703 | today2 = time(NULL); | |
704 | today = localtime(&today2); | |
705 | sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour, | |
706 | today->tm_min, | |
707 | today->tm_sec); | |
708 | fvstats = fopen(filename,"w"); | |
709 | if (!fvstats) { | |
710 | perror("fopen"); | |
711 | exit(1); | |
712 | } | |
713 | } | |
714 | ||
715 | ti = MAXINT64; | |
716 | enc = &ost->st->codec; | |
717 | total_size += frame_size; | |
718 | if (enc->codec_type == CODEC_TYPE_VIDEO) { | |
ec5517d5 | 719 | frame_number = ost->frame_number; |
158c7f05 | 720 | fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA); |
140cb663 | 721 | if (enc->flags&CODEC_FLAG_PSNR) |
492cd3a9 | 722 | fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0))); |
ce7c56c2 J |
723 | |
724 | fprintf(fvstats,"f_size= %6d ", frame_size); | |
ec5517d5 FB |
725 | /* compute pts value */ |
726 | ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den; | |
ce7c56c2 J |
727 | if (ti1 < 0.01) |
728 | ti1 = 0.01; | |
729 | ||
14bea432 | 730 | bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0; |
ce7c56c2 J |
731 | avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0; |
732 | fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", | |
733 | (double)total_size / 1024, ti1, bitrate, avg_bitrate); | |
3db320ea | 734 | fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type)); |
ce7c56c2 | 735 | } |
ec5517d5 FB |
736 | } |
737 | ||
b29f97d1 ZK |
738 | static void print_report(AVFormatContext **output_files, |
739 | AVOutputStream **ost_table, int nb_ostreams, | |
740 | int is_last_report) | |
ec5517d5 FB |
741 | { |
742 | char buf[1024]; | |
743 | AVOutputStream *ost; | |
744 | AVFormatContext *oc, *os; | |
0c1a9eda | 745 | int64_t total_size; |
ec5517d5 FB |
746 | AVCodecContext *enc; |
747 | int frame_number, vid, i; | |
748 | double bitrate, ti1, pts; | |
0c1a9eda | 749 | static int64_t last_time = -1; |
ec5517d5 FB |
750 | |
751 | if (!is_last_report) { | |
0c1a9eda | 752 | int64_t cur_time; |
ec5517d5 FB |
753 | /* display the report every 0.5 seconds */ |
754 | cur_time = av_gettime(); | |
755 | if (last_time == -1) { | |
756 | last_time = cur_time; | |
757 | return; | |
758 | } | |
759 | if ((cur_time - last_time) < 500000) | |
760 | return; | |
761 | last_time = cur_time; | |
762 | } | |
763 | ||
ce7c56c2 | 764 | |
ec5517d5 FB |
765 | oc = output_files[0]; |
766 | ||
767 | total_size = url_ftell(&oc->pb); | |
768 | ||
769 | buf[0] = '\0'; | |
770 | ti1 = 1e10; | |
771 | vid = 0; | |
772 | for(i=0;i<nb_ostreams;i++) { | |
773 | ost = ost_table[i]; | |
774 | os = output_files[ost->file_index]; | |
775 | enc = &ost->st->codec; | |
10d104e4 | 776 | if (vid && enc->codec_type == CODEC_TYPE_VIDEO) { |
1e491e29 | 777 | sprintf(buf + strlen(buf), "q=%2.1f ", |
158c7f05 | 778 | enc->coded_frame->quality/(float)FF_QP2LAMBDA); |
10d104e4 | 779 | } |
ec5517d5 FB |
780 | if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) { |
781 | frame_number = ost->frame_number; | |
1e491e29 | 782 | sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ", |
158c7f05 | 783 | frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : 0); |
890972be MN |
784 | if(is_last_report) |
785 | sprintf(buf + strlen(buf), "L"); | |
786 | if (enc->flags&CODEC_FLAG_PSNR){ | |
787 | int j; | |
788 | double error, error_sum=0; | |
789 | double scale, scale_sum=0; | |
790 | char type[3]= {'Y','U','V'}; | |
791 | sprintf(buf + strlen(buf), "PSNR="); | |
792 | for(j=0; j<3; j++){ | |
793 | if(is_last_report){ | |
794 | error= enc->error[j]; | |
795 | scale= enc->width*enc->height*255.0*255.0*frame_number; | |
796 | }else{ | |
797 | error= enc->coded_frame->error[j]; | |
798 | scale= enc->width*enc->height*255.0*255.0; | |
799 | } | |
800 | if(j) scale/=4; | |
801 | error_sum += error; | |
802 | scale_sum += scale; | |
803 | sprintf(buf + strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale)); | |
804 | } | |
805 | sprintf(buf + strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum)); | |
806 | } | |
ec5517d5 FB |
807 | vid = 1; |
808 | } | |
809 | /* compute min output value */ | |
810 | pts = (double)ost->st->pts.val * os->pts_num / os->pts_den; | |
5d9827bc | 811 | if ((pts < ti1) && (pts > 0)) |
ec5517d5 FB |
812 | ti1 = pts; |
813 | } | |
814 | if (ti1 < 0.01) | |
815 | ti1 = 0.01; | |
ec5517d5 | 816 | |
f068206e BE |
817 | if (verbose || is_last_report) { |
818 | bitrate = (double)(total_size * 8) / ti1 / 1000.0; | |
819 | ||
820 | sprintf(buf + strlen(buf), | |
ec5517d5 FB |
821 | "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s", |
822 | (double)total_size / 1024, ti1, bitrate); | |
f068206e BE |
823 | |
824 | fprintf(stderr, "%s \r", buf); | |
ec5517d5 FB |
825 | fflush(stderr); |
826 | } | |
f068206e BE |
827 | |
828 | if (is_last_report) | |
829 | fprintf(stderr, "\n"); | |
ce7c56c2 J |
830 | } |
831 | ||
a700a6ae FB |
832 | /* pkt = NULL means EOF (needed to flush decoder buffers) */ |
833 | static int output_packet(AVInputStream *ist, int ist_index, | |
834 | AVOutputStream **ost_table, int nb_ostreams, | |
835 | AVPacket *pkt) | |
836 | { | |
837 | AVFormatContext *os; | |
838 | AVOutputStream *ost; | |
839 | uint8_t *ptr; | |
840 | int len, ret, i; | |
841 | uint8_t *data_buf; | |
842 | int data_size, got_picture; | |
843 | AVFrame picture; | |
844 | short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2]; | |
845 | void *buffer_to_free; | |
846 | ||
847 | if (pkt && pkt->pts != AV_NOPTS_VALUE) { | |
848 | ist->pts = pkt->pts; | |
849 | } else { | |
850 | ist->pts = ist->next_pts; | |
851 | } | |
852 | ||
853 | if (pkt == NULL) { | |
854 | /* EOF handling */ | |
855 | ptr = NULL; | |
856 | len = 0; | |
857 | goto handle_eof; | |
858 | } | |
859 | ||
860 | len = pkt->size; | |
861 | ptr = pkt->data; | |
862 | while (len > 0) { | |
863 | handle_eof: | |
864 | /* decode the packet if needed */ | |
865 | data_buf = NULL; /* fail safe */ | |
866 | data_size = 0; | |
867 | if (ist->decoding_needed) { | |
868 | switch(ist->st->codec.codec_type) { | |
869 | case CODEC_TYPE_AUDIO: | |
870 | /* XXX: could avoid copy if PCM 16 bits with same | |
871 | endianness as CPU */ | |
872 | ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size, | |
873 | ptr, len); | |
874 | if (ret < 0) | |
875 | goto fail_decode; | |
876 | ptr += ret; | |
877 | len -= ret; | |
878 | /* Some bug in mpeg audio decoder gives */ | |
879 | /* data_size < 0, it seems they are overflows */ | |
880 | if (data_size <= 0) { | |
881 | /* no audio frame */ | |
882 | continue; | |
883 | } | |
884 | data_buf = (uint8_t *)samples; | |
885 | ist->next_pts += ((int64_t)AV_TIME_BASE * data_size) / | |
886 | (2 * ist->st->codec.channels); | |
887 | break; | |
888 | case CODEC_TYPE_VIDEO: | |
889 | data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2; | |
890 | /* XXX: allocate picture correctly */ | |
891 | memset(&picture, 0, sizeof(picture)); | |
892 | ret = avcodec_decode_video(&ist->st->codec, | |
893 | &picture, &got_picture, ptr, len); | |
894 | ist->st->quality= picture.quality; | |
895 | if (ret < 0) | |
896 | goto fail_decode; | |
897 | if (!got_picture) { | |
898 | /* no picture yet */ | |
899 | goto discard_packet; | |
900 | } | |
901 | if (ist->st->codec.frame_rate_base != 0) { | |
902 | ist->next_pts += ((int64_t)AV_TIME_BASE * | |
903 | ist->st->codec.frame_rate_base) / | |
904 | ist->st->codec.frame_rate; | |
905 | } | |
906 | len = 0; | |
907 | break; | |
908 | default: | |
909 | goto fail_decode; | |
910 | } | |
911 | } else { | |
912 | data_buf = ptr; | |
913 | data_size = len; | |
914 | ret = len; | |
915 | len = 0; | |
916 | } | |
917 | ||
918 | buffer_to_free = NULL; | |
919 | if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
920 | pre_process_video_frame(ist, (AVPicture *)&picture, | |
921 | &buffer_to_free); | |
922 | } | |
923 | ||
924 | /* frame rate emulation */ | |
925 | if (ist->st->codec.rate_emu) { | |
926 | int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate); | |
927 | int64_t now = av_gettime() - ist->start; | |
928 | if (pts > now) | |
929 | usleep(pts - now); | |
930 | ||
931 | ist->frame++; | |
932 | } | |
933 | ||
934 | #if 0 | |
935 | /* mpeg PTS deordering : if it is a P or I frame, the PTS | |
936 | is the one of the next displayed one */ | |
937 | /* XXX: add mpeg4 too ? */ | |
938 | if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) { | |
939 | if (ist->st->codec.pict_type != B_TYPE) { | |
940 | int64_t tmp; | |
941 | tmp = ist->last_ip_pts; | |
942 | ist->last_ip_pts = ist->frac_pts.val; | |
943 | ist->frac_pts.val = tmp; | |
944 | } | |
945 | } | |
946 | #endif | |
947 | /* if output time reached then transcode raw format, | |
948 | encode packets and output them */ | |
949 | if (start_time == 0 || ist->pts >= start_time) | |
950 | for(i=0;i<nb_ostreams;i++) { | |
951 | int frame_size; | |
952 | ||
953 | ost = ost_table[i]; | |
954 | if (ost->source_index == ist_index) { | |
955 | os = output_files[ost->file_index]; | |
956 | ||
957 | #if 0 | |
958 | printf("%d: got pts=%0.3f %0.3f\n", i, | |
959 | (double)pkt->pts / AV_TIME_BASE, | |
960 | ((double)ist->pts / AV_TIME_BASE) - | |
961 | ((double)ost->st->pts.val * os->pts_num / os->pts_den)); | |
962 | #endif | |
963 | /* set the input output pts pairs */ | |
964 | ost->sync_ipts = (double)ist->pts / AV_TIME_BASE; | |
965 | /* XXX: take into account the various fifos, | |
966 | in particular for audio */ | |
967 | ost->sync_opts = ost->st->pts.val; | |
968 | //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt->pts=%lld\n", ist->pts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt->pts); | |
969 | ||
970 | if (ost->encoding_needed) { | |
971 | switch(ost->st->codec.codec_type) { | |
972 | case CODEC_TYPE_AUDIO: | |
973 | do_audio_out(os, ost, ist, data_buf, data_size); | |
974 | break; | |
975 | case CODEC_TYPE_VIDEO: | |
976 | /* find an audio stream for synchro */ | |
977 | { | |
978 | int i; | |
979 | AVOutputStream *audio_sync, *ost1; | |
980 | audio_sync = NULL; | |
981 | for(i=0;i<nb_ostreams;i++) { | |
982 | ost1 = ost_table[i]; | |
983 | if (ost1->file_index == ost->file_index && | |
984 | ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) { | |
985 | audio_sync = ost1; | |
986 | break; | |
987 | } | |
988 | } | |
989 | ||
990 | do_video_out(os, ost, ist, &picture, &frame_size, audio_sync); | |
991 | if (do_vstats && frame_size) | |
992 | do_video_stats(os, ost, frame_size); | |
993 | } | |
994 | break; | |
995 | default: | |
996 | av_abort(); | |
997 | } | |
998 | } else { | |
999 | AVFrame avframe; | |
1000 | ||
1001 | /* no reencoding needed : output the packet directly */ | |
1002 | /* force the input stream PTS */ | |
1003 | ||
1004 | memset(&avframe, 0, sizeof(AVFrame)); | |
1005 | ost->st->codec.coded_frame= &avframe; | |
1006 | avframe.key_frame = pkt->flags & PKT_FLAG_KEY; | |
1007 | ||
1008 | av_write_frame(os, ost->index, data_buf, data_size); | |
1009 | ost->st->codec.frame_number++; | |
1010 | ost->frame_number++; | |
1011 | } | |
1012 | } | |
1013 | } | |
1014 | av_free(buffer_to_free); | |
1015 | } | |
1016 | discard_packet: | |
1017 | return 0; | |
1018 | fail_decode: | |
1019 | return -1; | |
1020 | } | |
1021 | ||
1022 | ||
85f07f22 FB |
1023 | /* |
1024 | * The following code is the main loop of the file converter | |
1025 | */ | |
1026 | static int av_encode(AVFormatContext **output_files, | |
1027 | int nb_output_files, | |
1028 | AVFormatContext **input_files, | |
1029 | int nb_input_files, | |
1030 | AVStreamMap *stream_maps, int nb_stream_maps) | |
1031 | { | |
445f1b83 | 1032 | int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0; |
85f07f22 FB |
1033 | AVFormatContext *is, *os; |
1034 | AVCodecContext *codec, *icodec; | |
1035 | AVOutputStream *ost, **ost_table = NULL; | |
1036 | AVInputStream *ist, **ist_table = NULL; | |
bdc4796f | 1037 | AVInputFile *file_table; |
51bd4565 | 1038 | AVFormatContext *stream_no_data; |
cb09b2ed | 1039 | int key; |
bdc4796f | 1040 | |
51bd4565 | 1041 | file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile)); |
bdc4796f FB |
1042 | if (!file_table) |
1043 | goto fail; | |
85f07f22 | 1044 | |
85f07f22 FB |
1045 | /* input stream init */ |
1046 | j = 0; | |
1047 | for(i=0;i<nb_input_files;i++) { | |
1048 | is = input_files[i]; | |
1049 | file_table[i].ist_index = j; | |
79fdaa4c | 1050 | file_table[i].nb_streams = is->nb_streams; |
85f07f22 FB |
1051 | j += is->nb_streams; |
1052 | } | |
1053 | nb_istreams = j; | |
1054 | ||
1055 | ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *)); | |
1056 | if (!ist_table) | |
bdc4796f | 1057 | goto fail; |
85f07f22 FB |
1058 | |
1059 | for(i=0;i<nb_istreams;i++) { | |
1060 | ist = av_mallocz(sizeof(AVInputStream)); | |
1061 | if (!ist) | |
1062 | goto fail; | |
1063 | ist_table[i] = ist; | |
1064 | } | |
1065 | j = 0; | |
1066 | for(i=0;i<nb_input_files;i++) { | |
1067 | is = input_files[i]; | |
1068 | for(k=0;k<is->nb_streams;k++) { | |
1069 | ist = ist_table[j++]; | |
1070 | ist->st = is->streams[k]; | |
1071 | ist->file_index = i; | |
1072 | ist->index = k; | |
1073 | ist->discard = 1; /* the stream is discarded by default | |
1074 | (changed later) */ | |
bdfcbbed MK |
1075 | |
1076 | if (ist->st->codec.rate_emu) { | |
1077 | ist->start = av_gettime(); | |
1078 | ist->frame = 0; | |
1079 | } | |
85f07f22 FB |
1080 | } |
1081 | } | |
1082 | ||
1083 | /* output stream init */ | |
1084 | nb_ostreams = 0; | |
1085 | for(i=0;i<nb_output_files;i++) { | |
1086 | os = output_files[i]; | |
1087 | nb_ostreams += os->nb_streams; | |
1088 | } | |
1089 | if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) { | |
1090 | fprintf(stderr, "Number of stream maps must match number of output streams\n"); | |
1091 | exit(1); | |
1092 | } | |
1093 | ||
bd073980 BF |
1094 | /* Sanity check the mapping args -- do the input files & streams exist? */ |
1095 | for(i=0;i<nb_stream_maps;i++) { | |
1096 | int fi = stream_maps[i].file_index; | |
1097 | int si = stream_maps[i].stream_index; | |
1098 | ||
1099 | if (fi < 0 || fi > nb_input_files - 1 || | |
1100 | si < 0 || si > file_table[fi].nb_streams - 1) { | |
1101 | fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si); | |
1102 | exit(1); | |
1103 | } | |
1104 | } | |
1105 | ||
85f07f22 FB |
1106 | ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams); |
1107 | if (!ost_table) | |
1108 | goto fail; | |
1109 | for(i=0;i<nb_ostreams;i++) { | |
1110 | ost = av_mallocz(sizeof(AVOutputStream)); | |
1111 | if (!ost) | |
1112 | goto fail; | |
1113 | ost_table[i] = ost; | |
1114 | } | |
1115 | ||
1116 | n = 0; | |
1117 | for(k=0;k<nb_output_files;k++) { | |
1118 | os = output_files[k]; | |
1119 | for(i=0;i<os->nb_streams;i++) { | |
1120 | int found; | |
1121 | ost = ost_table[n++]; | |
1122 | ost->file_index = k; | |
1123 | ost->index = i; | |
1124 | ost->st = os->streams[i]; | |
1125 | if (nb_stream_maps > 0) { | |
1126 | ost->source_index = file_table[stream_maps[n-1].file_index].ist_index + | |
1127 | stream_maps[n-1].stream_index; | |
bd073980 BF |
1128 | |
1129 | /* Sanity check that the stream types match */ | |
1130 | if (ist_table[ost->source_index]->st->codec.codec_type != ost->st->codec.codec_type) { | |
1131 | fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n", | |
1132 | stream_maps[n-1].file_index, stream_maps[n-1].stream_index, | |
1133 | ost->file_index, ost->index); | |
1134 | exit(1); | |
1135 | } | |
1136 | ||
85f07f22 FB |
1137 | } else { |
1138 | /* get corresponding input stream index : we select the first one with the right type */ | |
1139 | found = 0; | |
1140 | for(j=0;j<nb_istreams;j++) { | |
1141 | ist = ist_table[j]; | |
1142 | if (ist->discard && | |
1143 | ist->st->codec.codec_type == ost->st->codec.codec_type) { | |
1144 | ost->source_index = j; | |
1145 | found = 1; | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | if (!found) { | |
1150 | /* try again and reuse existing stream */ | |
1151 | for(j=0;j<nb_istreams;j++) { | |
1152 | ist = ist_table[j]; | |
1153 | if (ist->st->codec.codec_type == ost->st->codec.codec_type) { | |
1154 | ost->source_index = j; | |
1155 | found = 1; | |
1156 | } | |
1157 | } | |
1158 | if (!found) { | |
1159 | fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n", | |
1160 | ost->file_index, ost->index); | |
1161 | exit(1); | |
1162 | } | |
1163 | } | |
1164 | } | |
1165 | ist = ist_table[ost->source_index]; | |
1166 | ist->discard = 0; | |
1167 | } | |
1168 | } | |
1169 | ||
85f07f22 FB |
1170 | /* for each output stream, we compute the right encoding parameters */ |
1171 | for(i=0;i<nb_ostreams;i++) { | |
1172 | ost = ost_table[i]; | |
1173 | ist = ist_table[ost->source_index]; | |
1174 | ||
1175 | codec = &ost->st->codec; | |
1176 | icodec = &ist->st->codec; | |
1177 | ||
1629626f FB |
1178 | if (ost->st->stream_copy) { |
1179 | /* if stream_copy is selected, no need to decode or encode */ | |
1180 | codec->codec_id = icodec->codec_id; | |
1181 | codec->codec_type = icodec->codec_type; | |
1182 | codec->codec_tag = icodec->codec_tag; | |
1183 | codec->bit_rate = icodec->bit_rate; | |
1184 | switch(codec->codec_type) { | |
1185 | case CODEC_TYPE_AUDIO: | |
1186 | codec->sample_rate = icodec->sample_rate; | |
1187 | codec->channels = icodec->channels; | |
1188 | break; | |
1189 | case CODEC_TYPE_VIDEO: | |
1190 | codec->frame_rate = icodec->frame_rate; | |
14bea432 | 1191 | codec->frame_rate_base = icodec->frame_rate_base; |
1629626f FB |
1192 | codec->width = icodec->width; |
1193 | codec->height = icodec->height; | |
1194 | break; | |
1195 | default: | |
1196 | av_abort(); | |
1197 | } | |
1198 | } else { | |
1199 | switch(codec->codec_type) { | |
1200 | case CODEC_TYPE_AUDIO: | |
85f07f22 FB |
1201 | if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE)) |
1202 | goto fail; | |
1629626f | 1203 | |
85f07f22 FB |
1204 | if (codec->channels == icodec->channels && |
1205 | codec->sample_rate == icodec->sample_rate) { | |
1206 | ost->audio_resample = 0; | |
1207 | } else { | |
e0d2714a J |
1208 | if (codec->channels != icodec->channels && |
1209 | icodec->codec_id == CODEC_ID_AC3) { | |
1210 | /* Special case for 5:1 AC3 input */ | |
1211 | /* and mono or stereo output */ | |
6dc96cb0 J |
1212 | /* Request specific number of channels */ |
1213 | icodec->channels = codec->channels; | |
1214 | if (codec->sample_rate == icodec->sample_rate) | |
1215 | ost->audio_resample = 0; | |
1216 | else { | |
1217 | ost->audio_resample = 1; | |
1218 | ost->resample = audio_resample_init(codec->channels, icodec->channels, | |
1219 | codec->sample_rate, | |
1220 | icodec->sample_rate); | |
743739d2 MN |
1221 | if(!ost->resample) |
1222 | { | |
1223 | printf("Can't resample. Aborting.\n"); | |
1224 | av_abort(); | |
1225 | } | |
6dc96cb0 | 1226 | } |
e0d2714a J |
1227 | /* Request specific number of channels */ |
1228 | icodec->channels = codec->channels; | |
1229 | } else { | |
1230 | ost->audio_resample = 1; | |
1231 | ost->resample = audio_resample_init(codec->channels, icodec->channels, | |
85f07f22 FB |
1232 | codec->sample_rate, |
1233 | icodec->sample_rate); | |
743739d2 MN |
1234 | if(!ost->resample) |
1235 | { | |
1236 | printf("Can't resample. Aborting.\n"); | |
1237 | av_abort(); | |
1238 | } | |
e0d2714a | 1239 | } |
85f07f22 FB |
1240 | } |
1241 | ist->decoding_needed = 1; | |
1242 | ost->encoding_needed = 1; | |
1629626f FB |
1243 | break; |
1244 | case CODEC_TYPE_VIDEO: | |
85f07f22 | 1245 | if (codec->width == icodec->width && |
34b10a57 D |
1246 | codec->height == icodec->height && |
1247 | frame_topBand == 0 && | |
1248 | frame_bottomBand == 0 && | |
1249 | frame_leftBand == 0 && | |
1250 | frame_rightBand == 0) | |
1251 | { | |
1252 | ost->video_resample = 0; | |
1253 | ost->video_crop = 0; | |
1254 | } else if ((codec->width == icodec->width - | |
1255 | (frame_leftBand + frame_rightBand)) && | |
1256 | (codec->height == icodec->height - | |
1257 | (frame_topBand + frame_bottomBand))) | |
1258 | { | |
85f07f22 | 1259 | ost->video_resample = 0; |
34b10a57 D |
1260 | ost->video_crop = 1; |
1261 | ost->topBand = frame_topBand; | |
1262 | ost->leftBand = frame_leftBand; | |
85f07f22 | 1263 | } else { |
0c1a9eda | 1264 | uint8_t *buf; |
85f07f22 | 1265 | ost->video_resample = 1; |
34b10a57 | 1266 | ost->video_crop = 0; // cropping is handled as part of resample |
0f1578af | 1267 | buf = av_malloc((codec->width * codec->height * 3) / 2); |
85f07f22 FB |
1268 | if (!buf) |
1269 | goto fail; | |
1270 | ost->pict_tmp.data[0] = buf; | |
1271 | ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height); | |
1272 | ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4; | |
1273 | ost->pict_tmp.linesize[0] = codec->width; | |
1274 | ost->pict_tmp.linesize[1] = codec->width / 2; | |
1275 | ost->pict_tmp.linesize[2] = codec->width / 2; | |
1276 | ||
ab6d194a | 1277 | ost->img_resample_ctx = img_resample_full_init( |
85f07f22 | 1278 | ost->st->codec.width, ost->st->codec.height, |
ab6d194a MN |
1279 | ist->st->codec.width, ist->st->codec.height, |
1280 | frame_topBand, frame_bottomBand, | |
1281 | frame_leftBand, frame_rightBand); | |
85f07f22 FB |
1282 | } |
1283 | ost->encoding_needed = 1; | |
1284 | ist->decoding_needed = 1; | |
1629626f FB |
1285 | break; |
1286 | default: | |
1287 | av_abort(); | |
85f07f22 | 1288 | } |
1629626f FB |
1289 | /* two pass mode */ |
1290 | if (ost->encoding_needed && | |
1291 | (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { | |
1292 | char logfilename[1024]; | |
1293 | FILE *f; | |
1294 | int size; | |
1295 | char *logbuffer; | |
1296 | ||
1297 | snprintf(logfilename, sizeof(logfilename), "%s-%d.log", | |
1298 | pass_logfilename ? | |
1299 | pass_logfilename : DEFAULT_PASS_LOGFILENAME, i); | |
1300 | if (codec->flags & CODEC_FLAG_PASS1) { | |
1301 | f = fopen(logfilename, "w"); | |
1302 | if (!f) { | |
1303 | perror(logfilename); | |
1304 | exit(1); | |
1305 | } | |
1306 | ost->logfile = f; | |
1307 | } else { | |
1308 | /* read the log file */ | |
1309 | f = fopen(logfilename, "r"); | |
1310 | if (!f) { | |
1311 | perror(logfilename); | |
1312 | exit(1); | |
1313 | } | |
1314 | fseek(f, 0, SEEK_END); | |
1315 | size = ftell(f); | |
1316 | fseek(f, 0, SEEK_SET); | |
1317 | logbuffer = av_malloc(size + 1); | |
1318 | if (!logbuffer) { | |
1319 | fprintf(stderr, "Could not allocate log buffer\n"); | |
1320 | exit(1); | |
1321 | } | |
1322 | fread(logbuffer, 1, size, f); | |
1323 | fclose(f); | |
1324 | logbuffer[size] = '\0'; | |
1325 | codec->stats_in = logbuffer; | |
5abdb4b1 | 1326 | } |
5abdb4b1 FB |
1327 | } |
1328 | } | |
85f07f22 FB |
1329 | } |
1330 | ||
1629626f FB |
1331 | /* dump the file output parameters - cannot be done before in case |
1332 | of stream copy */ | |
1333 | for(i=0;i<nb_output_files;i++) { | |
1334 | dump_format(output_files[i], i, output_files[i]->filename, 1); | |
1335 | } | |
1336 | ||
1337 | /* dump the stream mapping */ | |
1338 | fprintf(stderr, "Stream mapping:\n"); | |
1339 | for(i=0;i<nb_ostreams;i++) { | |
1340 | ost = ost_table[i]; | |
1341 | fprintf(stderr, " Stream #%d.%d -> #%d.%d\n", | |
1342 | ist_table[ost->source_index]->file_index, | |
1343 | ist_table[ost->source_index]->index, | |
1344 | ost->file_index, | |
1345 | ost->index); | |
1346 | } | |
1347 | ||
85f07f22 FB |
1348 | /* open each encoder */ |
1349 | for(i=0;i<nb_ostreams;i++) { | |
1350 | ost = ost_table[i]; | |
1351 | if (ost->encoding_needed) { | |
1352 | AVCodec *codec; | |
1353 | codec = avcodec_find_encoder(ost->st->codec.codec_id); | |
1354 | if (!codec) { | |
1355 | fprintf(stderr, "Unsupported codec for output stream #%d.%d\n", | |
1356 | ost->file_index, ost->index); | |
1357 | exit(1); | |
1358 | } | |
1359 | if (avcodec_open(&ost->st->codec, codec) < 0) { | |
1360 | fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n", | |
1361 | ost->file_index, ost->index); | |
1362 | exit(1); | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | /* open each decoder */ | |
1368 | for(i=0;i<nb_istreams;i++) { | |
1369 | ist = ist_table[i]; | |
1370 | if (ist->decoding_needed) { | |
1371 | AVCodec *codec; | |
1372 | codec = avcodec_find_decoder(ist->st->codec.codec_id); | |
1373 | if (!codec) { | |
10d104e4 PG |
1374 | fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n", |
1375 | ist->st->codec.codec_id, ist->file_index, ist->index); | |
85f07f22 FB |
1376 | exit(1); |
1377 | } | |
1378 | if (avcodec_open(&ist->st->codec, codec) < 0) { | |
1379 | fprintf(stderr, "Error while opening codec for input stream #%d.%d\n", | |
1380 | ist->file_index, ist->index); | |
1381 | exit(1); | |
1382 | } | |
6dc96cb0 J |
1383 | //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) |
1384 | // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD; | |
85f07f22 FB |
1385 | } |
1386 | } | |
1387 | ||
1388 | /* init pts */ | |
1389 | for(i=0;i<nb_istreams;i++) { | |
1390 | ist = ist_table[i]; | |
445f1b83 | 1391 | is = input_files[ist->file_index]; |
e7d0374f | 1392 | ist->pts = 0; |
254abc2e | 1393 | ist->next_pts = 0; |
85f07f22 FB |
1394 | } |
1395 | ||
1396 | /* compute buffer size max (should use a complete heuristic) */ | |
1397 | for(i=0;i<nb_input_files;i++) { | |
1398 | file_table[i].buffer_size_max = 2048; | |
1399 | } | |
1400 | ||
1401 | /* open files and write file headers */ | |
1402 | for(i=0;i<nb_output_files;i++) { | |
1403 | os = output_files[i]; | |
79fdaa4c | 1404 | if (av_write_header(os) < 0) { |
a38469e1 FB |
1405 | fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i); |
1406 | ret = -EINVAL; | |
1407 | goto fail; | |
1408 | } | |
85f07f22 FB |
1409 | } |
1410 | ||
a38469e1 | 1411 | #ifndef CONFIG_WIN32 |
d9a916e2 CY |
1412 | if ( !using_stdin ) |
1413 | fprintf(stderr, "Press [q] to stop encoding\n"); | |
a38469e1 FB |
1414 | #endif |
1415 | term_init(); | |
1416 | ||
51bd4565 | 1417 | stream_no_data = 0; |
cb09b2ed | 1418 | key = -1; |
51bd4565 | 1419 | |
d9a916e2 | 1420 | for(; received_sigterm == 0;) { |
85f07f22 FB |
1421 | int file_index, ist_index; |
1422 | AVPacket pkt; | |
ec5517d5 FB |
1423 | double pts_min; |
1424 | ||
85f07f22 | 1425 | redo: |
a38469e1 | 1426 | /* if 'q' pressed, exits */ |
d9a916e2 | 1427 | if (!using_stdin) { |
cb09b2ed PG |
1428 | /* read_key() returns 0 on EOF */ |
1429 | key = read_key(); | |
1430 | if (key == 'q') | |
1431 | break; | |
1432 | } | |
a38469e1 | 1433 | |
ec5517d5 FB |
1434 | /* select the stream that we must read now by looking at the |
1435 | smallest output pts */ | |
85f07f22 | 1436 | file_index = -1; |
ec5517d5 FB |
1437 | pts_min = 1e10; |
1438 | for(i=0;i<nb_ostreams;i++) { | |
1439 | double pts; | |
1440 | ost = ost_table[i]; | |
1441 | os = output_files[ost->file_index]; | |
1442 | ist = ist_table[ost->source_index]; | |
1443 | pts = (double)ost->st->pts.val * os->pts_num / os->pts_den; | |
1444 | if (!file_table[ist->file_index].eof_reached && | |
1445 | pts < pts_min) { | |
1446 | pts_min = pts; | |
85f07f22 FB |
1447 | file_index = ist->file_index; |
1448 | } | |
1449 | } | |
1450 | /* if none, if is finished */ | |
51bd4565 | 1451 | if (file_index < 0) { |
85f07f22 | 1452 | break; |
ec5517d5 FB |
1453 | } |
1454 | ||
85f07f22 | 1455 | /* finish if recording time exhausted */ |
ec5517d5 | 1456 | if (recording_time > 0 && pts_min >= (recording_time / 1000000.0)) |
85f07f22 | 1457 | break; |
ec5517d5 | 1458 | |
254abc2e | 1459 | /* read a frame from it and output it in the fifo */ |
85f07f22 | 1460 | is = input_files[file_index]; |
254abc2e | 1461 | if (av_read_frame(is, &pkt) < 0) { |
85f07f22 FB |
1462 | file_table[file_index].eof_reached = 1; |
1463 | continue; | |
1464 | } | |
303e50e6 | 1465 | |
51bd4565 PG |
1466 | if (!pkt.size) { |
1467 | stream_no_data = is; | |
1468 | } else { | |
1469 | stream_no_data = 0; | |
1470 | } | |
254abc2e FB |
1471 | if (do_pkt_dump) { |
1472 | av_pkt_dump(stdout, &pkt, do_hex_dump); | |
817b23ff | 1473 | } |
79fdaa4c FB |
1474 | /* the following test is needed in case new streams appear |
1475 | dynamically in stream : we ignore them */ | |
1476 | if (pkt.stream_index >= file_table[file_index].nb_streams) | |
bf5af568 | 1477 | goto discard_packet; |
85f07f22 FB |
1478 | ist_index = file_table[file_index].ist_index + pkt.stream_index; |
1479 | ist = ist_table[ist_index]; | |
bf5af568 FB |
1480 | if (ist->discard) |
1481 | goto discard_packet; | |
85f07f22 | 1482 | |
8831db5c | 1483 | //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size); |
a700a6ae FB |
1484 | if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) { |
1485 | fprintf(stderr, "Error while decoding stream #%d.%d\n", | |
1486 | ist->file_index, ist->index); | |
1487 | av_free_packet(&pkt); | |
1488 | goto redo; | |
fe08925f | 1489 | } |
a700a6ae | 1490 | |
bf5af568 | 1491 | discard_packet: |
85f07f22 FB |
1492 | av_free_packet(&pkt); |
1493 | ||
ec5517d5 FB |
1494 | /* dump report by using the output first video and audio streams */ |
1495 | print_report(output_files, ost_table, nb_ostreams, 0); | |
85f07f22 | 1496 | } |
a700a6ae FB |
1497 | |
1498 | /* at the end of stream, we must flush the decoder buffers */ | |
1499 | for(i=0;i<nb_istreams;i++) { | |
1500 | ist = ist_table[i]; | |
1501 | if (ist->decoding_needed) { | |
1502 | output_packet(ist, i, ost_table, nb_ostreams, NULL); | |
1503 | } | |
1504 | } | |
1505 | ||
a38469e1 | 1506 | term_exit(); |
85f07f22 FB |
1507 | |
1508 | /* dump report by using the first video and audio streams */ | |
ec5517d5 FB |
1509 | print_report(output_files, ost_table, nb_ostreams, 1); |
1510 | ||
85f07f22 FB |
1511 | /* close each encoder */ |
1512 | for(i=0;i<nb_ostreams;i++) { | |
1513 | ost = ost_table[i]; | |
1514 | if (ost->encoding_needed) { | |
5abdb4b1 | 1515 | av_freep(&ost->st->codec.stats_in); |
85f07f22 FB |
1516 | avcodec_close(&ost->st->codec); |
1517 | } | |
1518 | } | |
1519 | ||
1520 | /* close each decoder */ | |
1521 | for(i=0;i<nb_istreams;i++) { | |
1522 | ist = ist_table[i]; | |
1523 | if (ist->decoding_needed) { | |
1524 | avcodec_close(&ist->st->codec); | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | ||
1529 | /* write the trailer if needed and close file */ | |
1530 | for(i=0;i<nb_output_files;i++) { | |
1531 | os = output_files[i]; | |
79fdaa4c | 1532 | av_write_trailer(os); |
85f07f22 FB |
1533 | } |
1534 | /* finished ! */ | |
1535 | ||
1536 | ret = 0; | |
1537 | fail1: | |
0f1578af | 1538 | av_free(file_table); |
bdc4796f | 1539 | |
85f07f22 FB |
1540 | if (ist_table) { |
1541 | for(i=0;i<nb_istreams;i++) { | |
1542 | ist = ist_table[i]; | |
0f1578af | 1543 | av_free(ist); |
85f07f22 | 1544 | } |
0f1578af | 1545 | av_free(ist_table); |
85f07f22 FB |
1546 | } |
1547 | if (ost_table) { | |
1548 | for(i=0;i<nb_ostreams;i++) { | |
1549 | ost = ost_table[i]; | |
1550 | if (ost) { | |
5abdb4b1 FB |
1551 | if (ost->logfile) { |
1552 | fclose(ost->logfile); | |
1553 | ost->logfile = NULL; | |
1554 | } | |
bf5af568 FB |
1555 | fifo_free(&ost->fifo); /* works even if fifo is not |
1556 | initialized but set to zero */ | |
0f1578af | 1557 | av_free(ost->pict_tmp.data[0]); |
85f07f22 FB |
1558 | if (ost->video_resample) |
1559 | img_resample_close(ost->img_resample_ctx); | |
1560 | if (ost->audio_resample) | |
1561 | audio_resample_close(ost->resample); | |
0f1578af | 1562 | av_free(ost); |
85f07f22 FB |
1563 | } |
1564 | } | |
0f1578af | 1565 | av_free(ost_table); |
85f07f22 FB |
1566 | } |
1567 | return ret; | |
1568 | fail: | |
1569 | ret = -ENOMEM; | |
1570 | goto fail1; | |
1571 | } | |
1572 | ||
1573 | #if 0 | |
1574 | int file_read(const char *filename) | |
1575 | { | |
1576 | URLContext *h; | |
1577 | unsigned char buffer[1024]; | |
1578 | int len, i; | |
1579 | ||
1580 | if (url_open(&h, filename, O_RDONLY) < 0) { | |
1581 | printf("could not open '%s'\n", filename); | |
1582 | return -1; | |
1583 | } | |
1584 | for(;;) { | |
1585 | len = url_read(h, buffer, sizeof(buffer)); | |
1586 | if (len <= 0) | |
1587 | break; | |
1588 | for(i=0;i<len;i++) putchar(buffer[i]); | |
1589 | } | |
1590 | url_close(h); | |
1591 | return 0; | |
1592 | } | |
1593 | #endif | |
1594 | ||
b29f97d1 | 1595 | static void opt_image_format(const char *arg) |
817b23ff FB |
1596 | { |
1597 | AVImageFormat *f; | |
1598 | ||
1599 | for(f = first_image_format; f != NULL; f = f->next) { | |
1600 | if (!strcmp(arg, f->name)) | |
1601 | break; | |
1602 | } | |
1603 | if (!f) { | |
1604 | fprintf(stderr, "Unknown image format: '%s'\n", arg); | |
1605 | exit(1); | |
1606 | } | |
1607 | image_format = f; | |
1608 | } | |
1609 | ||
b29f97d1 | 1610 | static void opt_format(const char *arg) |
85f07f22 | 1611 | { |
817b23ff FB |
1612 | /* compatibility stuff for pgmyuv */ |
1613 | if (!strcmp(arg, "pgmyuv")) { | |
1614 | opt_image_format(arg); | |
1615 | arg = "image"; | |
1616 | } | |
1617 | ||
79fdaa4c FB |
1618 | file_iformat = av_find_input_format(arg); |
1619 | file_oformat = guess_format(arg, NULL, NULL); | |
1620 | if (!file_iformat && !file_oformat) { | |
1621 | fprintf(stderr, "Unknown input or output format: %s\n", arg); | |
85f07f22 FB |
1622 | exit(1); |
1623 | } | |
85f07f22 FB |
1624 | } |
1625 | ||
b29f97d1 | 1626 | static void opt_video_bitrate(const char *arg) |
85f07f22 FB |
1627 | { |
1628 | video_bit_rate = atoi(arg) * 1000; | |
1629 | } | |
1630 | ||
b29f97d1 | 1631 | static void opt_video_bitrate_tolerance(const char *arg) |
9cdd6a24 MN |
1632 | { |
1633 | video_bit_rate_tolerance = atoi(arg) * 1000; | |
1634 | } | |
1635 | ||
b29f97d1 | 1636 | static void opt_video_bitrate_max(const char *arg) |
3aa102be MN |
1637 | { |
1638 | video_rc_max_rate = atoi(arg) * 1000; | |
1639 | } | |
1640 | ||
b29f97d1 | 1641 | static void opt_video_bitrate_min(const char *arg) |
3aa102be MN |
1642 | { |
1643 | video_rc_min_rate = atoi(arg) * 1000; | |
1644 | } | |
1645 | ||
b29f97d1 | 1646 | static void opt_video_buffer_size(const char *arg) |
946c8a12 | 1647 | { |
622348f9 | 1648 | video_rc_buffer_size = atoi(arg) * 8*1024; |
946c8a12 MN |
1649 | } |
1650 | ||
b29f97d1 | 1651 | static void opt_video_rc_eq(char *arg) |
3aa102be MN |
1652 | { |
1653 | video_rc_eq = arg; | |
1654 | } | |
1655 | ||
b29f97d1 | 1656 | static void opt_video_rc_override_string(char *arg) |
ac2830ec MN |
1657 | { |
1658 | video_rc_override_string = arg; | |
1659 | } | |
1660 | ||
3aa102be | 1661 | |
b29f97d1 | 1662 | static void opt_workaround_bugs(const char *arg) |
fe670d09 MN |
1663 | { |
1664 | workaround_bugs = atoi(arg); | |
1665 | } | |
1666 | ||
b29f97d1 | 1667 | static void opt_dct_algo(const char *arg) |
463678ac MN |
1668 | { |
1669 | dct_algo = atoi(arg); | |
1670 | } | |
1671 | ||
b29f97d1 | 1672 | static void opt_idct_algo(const char *arg) |
2ad1516a MN |
1673 | { |
1674 | idct_algo = atoi(arg); | |
1675 | } | |
1676 | ||
1677 | ||
b29f97d1 | 1678 | static void opt_error_resilience(const char *arg) |
8409b8fe MN |
1679 | { |
1680 | error_resilience = atoi(arg); | |
1681 | } | |
1682 | ||
b29f97d1 | 1683 | static void opt_error_concealment(const char *arg) |
4d2858de MN |
1684 | { |
1685 | error_concealment = atoi(arg); | |
1686 | } | |
1687 | ||
b29f97d1 | 1688 | static void opt_debug(const char *arg) |
59b571c1 MN |
1689 | { |
1690 | debug = atoi(arg); | |
1691 | } | |
4d2858de | 1692 | |
0c9bbaec WH |
1693 | static void opt_vismv(const char *arg) |
1694 | { | |
1695 | debug_mv = atoi(arg); | |
1696 | } | |
1697 | ||
f068206e BE |
1698 | static void opt_verbose(const char *arg) |
1699 | { | |
1700 | verbose = atoi(arg); | |
1701 | } | |
1702 | ||
b29f97d1 | 1703 | static void opt_frame_rate(const char *arg) |
85f07f22 | 1704 | { |
445f1b83 RS |
1705 | if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) { |
1706 | fprintf(stderr, "Incorrect frame rate\n"); | |
1707 | exit(1); | |
1708 | } | |
85f07f22 FB |
1709 | } |
1710 | ||
b29f97d1 | 1711 | static void opt_frame_crop_top(const char *arg) |
ab6d194a MN |
1712 | { |
1713 | frame_topBand = atoi(arg); | |
1714 | if (frame_topBand < 0) { | |
1715 | fprintf(stderr, "Incorrect top crop size\n"); | |
1716 | exit(1); | |
1717 | } | |
1718 | if ((frame_topBand % 2) != 0) { | |
1719 | fprintf(stderr, "Top crop size must be a multiple of 2\n"); | |
1720 | exit(1); | |
1721 | } | |
1722 | if ((frame_topBand) >= frame_height){ | |
1723 | fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n"); | |
1724 | exit(1); | |
1725 | } | |
1726 | frame_height -= frame_topBand; | |
1727 | } | |
1728 | ||
b29f97d1 | 1729 | static void opt_frame_crop_bottom(const char *arg) |
ab6d194a MN |
1730 | { |
1731 | frame_bottomBand = atoi(arg); | |
1732 | if (frame_bottomBand < 0) { | |
1733 | fprintf(stderr, "Incorrect bottom crop size\n"); | |
1734 | exit(1); | |
1735 | } | |
1736 | if ((frame_bottomBand % 2) != 0) { | |
1737 | fprintf(stderr, "Bottom crop size must be a multiple of 2\n"); | |
1738 | exit(1); | |
1739 | } | |
1740 | if ((frame_bottomBand) >= frame_height){ | |
1741 | fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n"); | |
1742 | exit(1); | |
1743 | } | |
1744 | frame_height -= frame_bottomBand; | |
1745 | } | |
1746 | ||
b29f97d1 | 1747 | static void opt_frame_crop_left(const char *arg) |
ab6d194a MN |
1748 | { |
1749 | frame_leftBand = atoi(arg); | |
1750 | if (frame_leftBand < 0) { | |
1751 | fprintf(stderr, "Incorrect left crop size\n"); | |
1752 | exit(1); | |
1753 | } | |
1754 | if ((frame_leftBand % 2) != 0) { | |
1755 | fprintf(stderr, "Left crop size must be a multiple of 2\n"); | |
1756 | exit(1); | |
1757 | } | |
1758 | if ((frame_leftBand) >= frame_width){ | |
1759 | fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n"); | |
1760 | exit(1); | |
1761 | } | |
1762 | frame_width -= frame_leftBand; | |
1763 | } | |
1764 | ||
b29f97d1 | 1765 | static void opt_frame_crop_right(const char *arg) |
ab6d194a MN |
1766 | { |
1767 | frame_rightBand = atoi(arg); | |
1768 | if (frame_rightBand < 0) { | |
1769 | fprintf(stderr, "Incorrect right crop size\n"); | |
1770 | exit(1); | |
1771 | } | |
1772 | if ((frame_rightBand % 2) != 0) { | |
1773 | fprintf(stderr, "Right crop size must be a multiple of 2\n"); | |
1774 | exit(1); | |
1775 | } | |
1776 | if ((frame_rightBand) >= frame_width){ | |
1777 | fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n"); | |
1778 | exit(1); | |
1779 | } | |
1780 | frame_width -= frame_rightBand; | |
1781 | } | |
1782 | ||
b29f97d1 | 1783 | static void opt_frame_size(const char *arg) |
85f07f22 | 1784 | { |
445f1b83 | 1785 | if (parse_image_size(&frame_width, &frame_height, arg) < 0) { |
85f07f22 FB |
1786 | fprintf(stderr, "Incorrect frame size\n"); |
1787 | exit(1); | |
1788 | } | |
1789 | if ((frame_width % 2) != 0 || (frame_height % 2) != 0) { | |
1790 | fprintf(stderr, "Frame size must be a multiple of 2\n"); | |
1791 | exit(1); | |
1792 | } | |
1793 | } | |
1794 | ||
63167088 RS |
1795 | static void opt_frame_pix_fmt(const char *arg) |
1796 | { | |
1797 | frame_pix_fmt = avcodec_get_pix_fmt(arg); | |
1798 | } | |
1799 | ||
5fe03e38 RS |
1800 | static void opt_frame_aspect_ratio(const char *arg) |
1801 | { | |
1802 | int x = 0, y = 0; | |
1803 | double ar = 0; | |
1804 | const char *p; | |
1805 | ||
1806 | p = strchr(arg, ':'); | |
1807 | if (p) { | |
1808 | x = strtol(arg, (char **)&arg, 10); | |
1809 | if (arg == p) | |
1810 | y = strtol(arg+1, (char **)&arg, 10); | |
1811 | if (x > 0 && y > 0) | |
1812 | ar = (double)x / (double)y; | |
1813 | } else | |
1814 | ar = strtod(arg, (char **)&arg); | |
1815 | ||
1816 | if (!ar) { | |
1817 | fprintf(stderr, "Incorrect aspect ratio specification.\n"); | |
1818 | exit(1); | |
1819 | } | |
1820 | frame_aspect_ratio = ar; | |
1821 | } | |
1822 | ||
b29f97d1 | 1823 | static void opt_gop_size(const char *arg) |
85f07f22 FB |
1824 | { |
1825 | gop_size = atoi(arg); | |
1826 | } | |
1827 | ||
b29f97d1 | 1828 | static void opt_b_frames(const char *arg) |
bc6caae2 J |
1829 | { |
1830 | b_frames = atoi(arg); | |
1831 | if (b_frames > FF_MAX_B_FRAMES) { | |
1832 | fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES); | |
1833 | exit(1); | |
1834 | } else if (b_frames < 1) { | |
1835 | fprintf(stderr, "\nNumber of B frames must be higher than 0\n"); | |
1836 | exit(1); | |
1837 | } | |
1838 | } | |
1839 | ||
7d1c3fc1 MN |
1840 | static void opt_mb_decision(const char *arg) |
1841 | { | |
1842 | mb_decision = atoi(arg); | |
1843 | } | |
1844 | ||
5d43635e MN |
1845 | static void opt_mb_cmp(const char *arg) |
1846 | { | |
1847 | mb_cmp = atoi(arg); | |
1848 | } | |
1849 | ||
622348f9 MN |
1850 | static void opt_ildct_cmp(const char *arg) |
1851 | { | |
1852 | ildct_cmp = atoi(arg); | |
1853 | } | |
1854 | ||
5d43635e MN |
1855 | static void opt_sub_cmp(const char *arg) |
1856 | { | |
1857 | sub_cmp = atoi(arg); | |
1858 | } | |
1859 | ||
1860 | static void opt_cmp(const char *arg) | |
1861 | { | |
1862 | cmp = atoi(arg); | |
1863 | } | |
1864 | ||
b29f97d1 | 1865 | static void opt_qscale(const char *arg) |
85f07f22 | 1866 | { |
158c7f05 MN |
1867 | video_qscale = atof(arg); |
1868 | if (video_qscale < 0.01 || | |
1869 | video_qscale > 255) { | |
1870 | fprintf(stderr, "qscale must be >= 0.01 and <= 255\n"); | |
85f07f22 FB |
1871 | exit(1); |
1872 | } | |
1873 | } | |
1874 | ||
b29f97d1 | 1875 | static void opt_qmin(const char *arg) |
9cdd6a24 MN |
1876 | { |
1877 | video_qmin = atoi(arg); | |
1878 | if (video_qmin < 0 || | |
1879 | video_qmin > 31) { | |
1880 | fprintf(stderr, "qmin must be >= 1 and <= 31\n"); | |
1881 | exit(1); | |
1882 | } | |
1883 | } | |
1884 | ||
b29f97d1 | 1885 | static void opt_qmax(const char *arg) |
9cdd6a24 MN |
1886 | { |
1887 | video_qmax = atoi(arg); | |
1888 | if (video_qmax < 0 || | |
1889 | video_qmax > 31) { | |
1890 | fprintf(stderr, "qmax must be >= 1 and <= 31\n"); | |
1891 | exit(1); | |
1892 | } | |
1893 | } | |
1894 | ||
b29f97d1 | 1895 | static void opt_mb_qmin(const char *arg) |
17a70fde MN |
1896 | { |
1897 | video_mb_qmin = atoi(arg); | |
1898 | if (video_mb_qmin < 0 || | |
1899 | video_mb_qmin > 31) { | |
1900 | fprintf(stderr, "qmin must be >= 1 and <= 31\n"); | |
1901 | exit(1); | |
1902 | } | |
1903 | } | |
1904 | ||
b29f97d1 | 1905 | static void opt_mb_qmax(const char *arg) |
17a70fde MN |
1906 | { |
1907 | video_mb_qmax = atoi(arg); | |
1908 | if (video_mb_qmax < 0 || | |
1909 | video_mb_qmax > 31) { | |
1910 | fprintf(stderr, "qmax must be >= 1 and <= 31\n"); | |
1911 | exit(1); | |
1912 | } | |
1913 | } | |
1914 | ||
b29f97d1 | 1915 | static void opt_qdiff(const char *arg) |
9cdd6a24 MN |
1916 | { |
1917 | video_qdiff = atoi(arg); | |
1918 | if (video_qdiff < 0 || | |
1919 | video_qdiff > 31) { | |
1920 | fprintf(stderr, "qdiff must be >= 1 and <= 31\n"); | |
1921 | exit(1); | |
1922 | } | |
1923 | } | |
1924 | ||
b29f97d1 | 1925 | static void opt_qblur(const char *arg) |
9cdd6a24 MN |
1926 | { |
1927 | video_qblur = atof(arg); | |
1928 | } | |
1929 | ||
b29f97d1 | 1930 | static void opt_qcomp(const char *arg) |
9cdd6a24 MN |
1931 | { |
1932 | video_qcomp = atof(arg); | |
1933 | } | |
85f07f22 | 1934 | |
b29f97d1 | 1935 | static void opt_rc_initial_cplx(const char *arg) |
ac2830ec MN |
1936 | { |
1937 | video_rc_initial_cplx = atof(arg); | |
1938 | } | |
b29f97d1 | 1939 | static void opt_b_qfactor(const char *arg) |
29700fa6 MN |
1940 | { |
1941 | video_b_qfactor = atof(arg); | |
1942 | } | |
b29f97d1 | 1943 | static void opt_i_qfactor(const char *arg) |
29700fa6 MN |
1944 | { |
1945 | video_i_qfactor = atof(arg); | |
1946 | } | |
b29f97d1 | 1947 | static void opt_b_qoffset(const char *arg) |
29700fa6 MN |
1948 | { |
1949 | video_b_qoffset = atof(arg); | |
1950 | } | |
b29f97d1 | 1951 | static void opt_i_qoffset(const char *arg) |
29700fa6 MN |
1952 | { |
1953 | video_i_qoffset = atof(arg); | |
1954 | } | |
1955 | ||
303e50e6 MN |
1956 | static void opt_ibias(const char *arg) |
1957 | { | |
1958 | video_intra_quant_bias = atoi(arg); | |
1959 | } | |
1960 | static void opt_pbias(const char *arg) | |
1961 | { | |
1962 | video_inter_quant_bias = atoi(arg); | |
1963 | } | |
1964 | ||
b29f97d1 | 1965 | static void opt_packet_size(const char *arg) |
1dbb6d90 MN |
1966 | { |
1967 | packet_size= atoi(arg); | |
1968 | } | |
1969 | ||
7ebfc0ea MN |
1970 | static void opt_error_rate(const char *arg) |
1971 | { | |
1972 | error_rate= atoi(arg); | |
1973 | } | |
1974 | ||
b29f97d1 | 1975 | static void opt_strict(const char *arg) |
f560dd82 MN |
1976 | { |
1977 | strict= atoi(arg); | |
1978 | } | |
1979 | ||
bb198e19 MN |
1980 | static void opt_top_field_first(const char *arg) |
1981 | { | |
1982 | top_field_first= atoi(arg); | |
1983 | } | |
1984 | ||
1985 | static void opt_noise_reduction(const char *arg) | |
1986 | { | |
1987 | noise_reduction= atoi(arg); | |
1988 | } | |
1989 | ||
77ea0d4b MN |
1990 | static void opt_qns(const char *arg) |
1991 | { | |
1992 | qns= atoi(arg); | |
1993 | } | |
1994 | ||
303e50e6 MN |
1995 | static void opt_sc_threshold(const char *arg) |
1996 | { | |
1997 | sc_threshold= atoi(arg); | |
1998 | } | |
1999 | ||
b29f97d1 | 2000 | static void opt_audio_bitrate(const char *arg) |
85f07f22 FB |
2001 | { |
2002 | audio_bit_rate = atoi(arg) * 1000; | |
2003 | } | |
2004 | ||
b29f97d1 | 2005 | static void opt_audio_rate(const char *arg) |
85f07f22 FB |
2006 | { |
2007 | audio_sample_rate = atoi(arg); | |
2008 | } | |
2009 | ||
b29f97d1 | 2010 | static void opt_audio_channels(const char *arg) |
85f07f22 FB |
2011 | { |
2012 | audio_channels = atoi(arg); | |
2013 | } | |
2014 | ||
b29f97d1 | 2015 | static void opt_video_device(const char *arg) |
85f07f22 | 2016 | { |
e9a9e0c2 | 2017 | video_device = av_strdup(arg); |
85f07f22 FB |
2018 | } |
2019 | ||
b29f97d1 | 2020 | static void opt_video_channel(const char *arg) |
a5df11ab FB |
2021 | { |
2022 | video_channel = strtol(arg, NULL, 0); | |
2023 | } | |
2024 | ||
e3ee3283 AB |
2025 | static void opt_video_standard(const char *arg) |
2026 | { | |
2027 | video_standard = av_strdup(arg); | |
2028 | } | |
2029 | ||
b29f97d1 | 2030 | static void opt_audio_device(const char *arg) |
85f07f22 | 2031 | { |
e9a9e0c2 | 2032 | audio_device = av_strdup(arg); |
85f07f22 FB |
2033 | } |
2034 | ||
b29f97d1 | 2035 | static void opt_dv1394(const char *arg) |
8aa3ee32 MK |
2036 | { |
2037 | video_grab_format = "dv1394"; | |
1501987b | 2038 | audio_grab_format = NULL; |
8aa3ee32 MK |
2039 | } |
2040 | ||
b29f97d1 | 2041 | static void opt_audio_codec(const char *arg) |
85f07f22 FB |
2042 | { |
2043 | AVCodec *p; | |
2044 | ||
1629626f FB |
2045 | if (!strcmp(arg, "copy")) { |
2046 | audio_stream_copy = 1; | |
85f07f22 | 2047 | } else { |
1629626f FB |
2048 | p = first_avcodec; |
2049 | while (p) { | |
2050 | if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO) | |
2051 | break; | |
2052 | p = p->next; | |
2053 | } | |
2054 | if (p == NULL) { | |
2055 | fprintf(stderr, "Unknown audio codec '%s'\n", arg); | |
2056 | exit(1); | |
2057 | } else { | |
2058 | audio_codec_id = p->id; | |
2059 | } | |
85f07f22 FB |
2060 | } |
2061 | } | |
2062 | ||
b29f97d1 | 2063 | static void add_frame_hooker(const char *arg) |
10d104e4 PG |
2064 | { |
2065 | int argc = 0; | |
2066 | char *argv[64]; | |
2067 | int i; | |
e9a9e0c2 | 2068 | char *args = av_strdup(arg); |
10d104e4 | 2069 | |
bee0d9e5 CY |
2070 | using_vhook = 1; |
2071 | ||
10d104e4 PG |
2072 | argv[0] = strtok(args, " "); |
2073 | while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) { | |
2074 | } | |
2075 | ||
2076 | i = frame_hook_add(argc, argv); | |
2077 | ||
2078 | if (i != 0) { | |
2079 | fprintf(stderr, "Failed to add video hook function: %s\n", arg); | |
2080 | exit(1); | |
2081 | } | |
2082 | } | |
2083 | ||
85f07f22 FB |
2084 | const char *motion_str[] = { |
2085 | "zero", | |
2086 | "full", | |
2087 | "log", | |
2088 | "phods", | |
7084c149 MN |
2089 | "epzs", |
2090 | "x1", | |
85f07f22 FB |
2091 | NULL, |
2092 | }; | |
2093 | ||
b29f97d1 | 2094 | static void opt_motion_estimation(const char *arg) |
85f07f22 FB |
2095 | { |
2096 | const char **p; | |
2097 | p = motion_str; | |
2098 | for(;;) { | |
2099 | if (!*p) { | |
2100 | fprintf(stderr, "Unknown motion estimation method '%s'\n", arg); | |
2101 | exit(1); | |
2102 | } | |
2103 | if (!strcmp(*p, arg)) | |
2104 | break; | |
2105 | p++; | |
2106 | } | |
101bea5f | 2107 | me_method = (p - motion_str) + 1; |
85f07f22 FB |
2108 | } |
2109 | ||
b29f97d1 | 2110 | static void opt_video_codec(const char *arg) |
85f07f22 FB |
2111 | { |
2112 | AVCodec *p; | |
2113 | ||
1629626f FB |
2114 | if (!strcmp(arg, "copy")) { |
2115 | video_stream_copy = 1; | |
85f07f22 | 2116 | } else { |
1629626f FB |
2117 | p = first_avcodec; |
2118 | while (p) { | |
2119 | if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO) | |
2120 | break; | |
2121 | p = p->next; | |
2122 | } | |
2123 | if (p == NULL) { | |
2124 | fprintf(stderr, "Unknown video codec '%s'\n", arg); | |
2125 | exit(1); | |
2126 | } else { | |
2127 | video_codec_id = p->id; | |
2128 | } | |
85f07f22 FB |
2129 | } |
2130 | } | |
2131 | ||
b29f97d1 | 2132 | static void opt_map(const char *arg) |
85f07f22 FB |
2133 | { |
2134 | AVStreamMap *m; | |
2135 | const char *p; | |
2136 | ||
2137 | p = arg; | |
2138 | m = &stream_maps[nb_stream_maps++]; | |
2139 | ||
2140 | m->file_index = strtol(arg, (char **)&p, 0); | |
2141 | if (*p) | |
2142 | p++; | |
a5dc85ef J |
2143 | |
2144 | m->stream_index = strtol(p, (char **)&p, 0); | |
85f07f22 FB |
2145 | } |
2146 | ||
b29f97d1 | 2147 | static void opt_recording_time(const char *arg) |
85f07f22 FB |
2148 | { |
2149 | recording_time = parse_date(arg, 1); | |
2150 | } | |
2151 | ||
8831db5c MN |
2152 | static void opt_start_time(const char *arg) |
2153 | { | |
2154 | start_time = parse_date(arg, 1); | |
2155 | } | |
2156 | ||
b29f97d1 | 2157 | static void opt_input_file(const char *filename) |
85f07f22 FB |
2158 | { |
2159 | AVFormatContext *ic; | |
2160 | AVFormatParameters params, *ap = ¶ms; | |
14bea432 | 2161 | int err, i, ret, rfps, rfps_base; |
85f07f22 | 2162 | |
b242baa4 FB |
2163 | if (!strcmp(filename, "-")) |
2164 | filename = "pipe:"; | |
2165 | ||
d9a916e2 CY |
2166 | using_stdin |= !strcmp(filename, "pipe:" ) || |
2167 | !strcmp( filename, "/dev/stdin" ); | |
2168 | ||
85f07f22 | 2169 | /* get default parameters from command line */ |
79fdaa4c FB |
2170 | memset(ap, 0, sizeof(*ap)); |
2171 | ap->sample_rate = audio_sample_rate; | |
2172 | ap->channels = audio_channels; | |
2173 | ap->frame_rate = frame_rate; | |
14bea432 | 2174 | ap->frame_rate_base = frame_rate_base; |
79fdaa4c FB |
2175 | ap->width = frame_width; |
2176 | ap->height = frame_height; | |
817b23ff | 2177 | ap->image_format = image_format; |
63167088 | 2178 | ap->pix_fmt = frame_pix_fmt; |
79fdaa4c FB |
2179 | |
2180 | /* open the input file with generic libav function */ | |
2181 | err = av_open_input_file(&ic, filename, file_iformat, 0, ap); | |
85f07f22 | 2182 | if (err < 0) { |
79fdaa4c | 2183 | print_error(filename, err); |
85f07f22 FB |
2184 | exit(1); |
2185 | } | |
2186 | ||
79fdaa4c FB |
2187 | /* If not enough info to get the stream parameters, we decode the |
2188 | first frames to get it. (used in mpeg case for example) */ | |
2189 | ret = av_find_stream_info(ic); | |
85f07f22 FB |
2190 | if (ret < 0) { |
2191 | fprintf(stderr, "%s: could not find codec parameters\n", filename); | |
2192 | exit(1); | |
2193 | } | |
2194 | ||
254abc2e FB |
2195 | /* if seeking requested, we execute it */ |
2196 | if (start_time != 0) { | |
2197 | int64_t timestamp; | |
2198 | ||
2199 | timestamp = start_time; | |
2200 | /* add the stream start time */ | |
2201 | if (ic->start_time != AV_NOPTS_VALUE) | |
2202 | timestamp += ic->start_time; | |
2203 | ret = av_seek_frame(ic, -1, timestamp); | |
2204 | if (ret < 0) { | |
2205 | fprintf(stderr, "%s: could not seek to position %0.3f\n", | |
2206 | filename, (double)timestamp / AV_TIME_BASE); | |
2207 | } | |
2208 | /* reset seek info */ | |
2209 | start_time = 0; | |
2210 | } | |
2211 | ||
85f07f22 FB |
2212 | /* update the current parameters so that they match the one of the input stream */ |
2213 | for(i=0;i<ic->nb_streams;i++) { | |
2214 | AVCodecContext *enc = &ic->streams[i]->codec; | |
2215 | switch(enc->codec_type) { | |
2216 | case CODEC_TYPE_AUDIO: | |
e0d2714a | 2217 | //fprintf(stderr, "\nInput Audio channels: %d", enc->channels); |
85f07f22 FB |
2218 | audio_channels = enc->channels; |
2219 | audio_sample_rate = enc->sample_rate; | |
2220 | break; | |
2221 | case CODEC_TYPE_VIDEO: | |
2222 | frame_height = enc->height; | |
2223 | frame_width = enc->width; | |
5ff85f1d | 2224 | frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height; |
63167088 | 2225 | frame_pix_fmt = enc->pix_fmt; |
14bea432 MN |
2226 | rfps = ic->streams[i]->r_frame_rate; |
2227 | rfps_base = ic->streams[i]->r_frame_rate_base; | |
fe670d09 | 2228 | enc->workaround_bugs = workaround_bugs; |
8409b8fe | 2229 | enc->error_resilience = error_resilience; |
4d2858de | 2230 | enc->error_concealment = error_concealment; |
0c9bbaec WH |
2231 | enc->idct_algo = idct_algo; |
2232 | enc->debug = debug; | |
2233 | enc->debug_mv = debug_mv; | |
b0368839 MN |
2234 | if(bitexact) |
2235 | enc->flags|= CODEC_FLAG_BITEXACT; | |
d7425f59 | 2236 | |
14bea432 MN |
2237 | assert(enc->frame_rate_base == rfps_base); // should be true for now |
2238 | if (enc->frame_rate != rfps) { | |
6dc96cb0 | 2239 | fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n", |
14bea432 MN |
2240 | i, (float)enc->frame_rate / enc->frame_rate_base, |
2241 | (float)rfps / rfps_base); | |
79fdaa4c | 2242 | } |
bf5af568 | 2243 | /* update the current frame rate to match the stream frame rate */ |
14bea432 MN |
2244 | frame_rate = rfps; |
2245 | frame_rate_base = rfps_base; | |
bdfcbbed MK |
2246 | |
2247 | enc->rate_emu = rate_emu; | |
85f07f22 | 2248 | break; |
254abc2e FB |
2249 | case CODEC_TYPE_DATA: |
2250 | break; | |
51bd4565 | 2251 | default: |
c04643a2 | 2252 | av_abort(); |
85f07f22 FB |
2253 | } |
2254 | } | |
2255 | ||
2256 | input_files[nb_input_files] = ic; | |
2257 | /* dump the file content */ | |
2258 | dump_format(ic, nb_input_files, filename, 0); | |
2259 | nb_input_files++; | |
79fdaa4c FB |
2260 | file_iformat = NULL; |
2261 | file_oformat = NULL; | |
817b23ff | 2262 | image_format = NULL; |
bdfcbbed MK |
2263 | |
2264 | rate_emu = 0; | |
85f07f22 FB |
2265 | } |
2266 | ||
b29f97d1 | 2267 | static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr) |
919f448d FB |
2268 | { |
2269 | int has_video, has_audio, i, j; | |
2270 | AVFormatContext *ic; | |
2271 | ||
2272 | has_video = 0; | |
2273 | has_audio = 0; | |
2274 | for(j=0;j<nb_input_files;j++) { | |
2275 | ic = input_files[j]; | |
2276 | for(i=0;i<ic->nb_streams;i++) { | |
2277 | AVCodecContext *enc = &ic->streams[i]->codec; | |
2278 | switch(enc->codec_type) { | |
2279 | case CODEC_TYPE_AUDIO: | |
2280 | has_audio = 1; | |
2281 | break; | |
2282 | case CODEC_TYPE_VIDEO: | |
2283 | has_video = 1; | |
2284 | break; | |
51bd4565 | 2285 | default: |
c04643a2 | 2286 | av_abort(); |
919f448d FB |
2287 | } |
2288 | } | |
2289 | } | |
2290 | *has_video_ptr = has_video; | |
2291 | *has_audio_ptr = has_audio; | |
2292 | } | |
2293 | ||
b29f97d1 | 2294 | static void opt_output_file(const char *filename) |
85f07f22 FB |
2295 | { |
2296 | AVStream *st; | |
2297 | AVFormatContext *oc; | |
919f448d | 2298 | int use_video, use_audio, nb_streams, input_has_video, input_has_audio; |
85f07f22 | 2299 | int codec_id; |
817b23ff | 2300 | AVFormatParameters params, *ap = ¶ms; |
85f07f22 FB |
2301 | |
2302 | if (!strcmp(filename, "-")) | |
2303 | filename = "pipe:"; | |
2304 | ||
2305 | oc = av_mallocz(sizeof(AVFormatContext)); | |
2306 | ||
79fdaa4c FB |
2307 | if (!file_oformat) { |
2308 | file_oformat = guess_format(NULL, filename, NULL); | |
2309 | if (!file_oformat) { | |
2310 | fprintf(stderr, "Unable for find a suitable output format for '%s'\n", | |
2311 | filename); | |
2312 | exit(1); | |
2313 | } | |
85f07f22 FB |
2314 | } |
2315 | ||
79fdaa4c | 2316 | oc->oformat = file_oformat; |
85f07f22 | 2317 | |
79fdaa4c | 2318 | if (!strcmp(file_oformat->name, "ffm") && |
85f07f22 FB |
2319 | strstart(filename, "http:", NULL)) { |
2320 | /* special case for files sent to ffserver: we get the stream | |
2321 | parameters from ffserver */ | |
2322 | if (read_ffserver_streams(oc, filename) < 0) { | |
2323 | fprintf(stderr, "Could not read stream parameters from '%s'\n", filename); | |
2324 | exit(1); | |
2325 | } | |
2326 | } else { | |
79fdaa4c FB |
2327 | use_video = file_oformat->video_codec != CODEC_ID_NONE; |
2328 | use_audio = file_oformat->audio_codec != CODEC_ID_NONE; | |
919f448d | 2329 | |
e30a2846 FB |
2330 | /* disable if no corresponding type found and at least one |
2331 | input file */ | |
2332 | if (nb_input_files > 0) { | |
2333 | check_audio_video_inputs(&input_has_video, &input_has_audio); | |
2334 | if (!input_has_video) | |
2335 | use_video = 0; | |
2336 | if (!input_has_audio) | |
2337 | use_audio = 0; | |
2338 | } | |
919f448d FB |
2339 | |
2340 | /* manual disable */ | |
85f07f22 FB |
2341 | if (audio_disable) { |
2342 | use_audio = 0; | |
2343 | } | |
2344 | if (video_disable) { | |
2345 | use_video = 0; | |
2346 | } | |
2347 | ||
2348 | nb_streams = 0; | |
2349 | if (use_video) { | |
2350 | AVCodecContext *video_enc; | |
2351 | ||
2352 | st = av_mallocz(sizeof(AVStream)); | |
2353 | if (!st) { | |
2354 | fprintf(stderr, "Could not alloc stream\n"); | |
2355 | exit(1); | |
2356 | } | |
1e491e29 | 2357 | avcodec_get_context_defaults(&st->codec); |
85f07f22 | 2358 | |
1629626f | 2359 | video_enc = &st->codec; |
6e6d6dc0 MN |
2360 | |
2361 | if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp")) | |
2362 | video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
1629626f FB |
2363 | if (video_stream_copy) { |
2364 | st->stream_copy = 1; | |
2365 | video_enc->codec_type = CODEC_TYPE_VIDEO; | |
2366 | } else { | |
ac2830ec MN |
2367 | char *p; |
2368 | int i; | |
2369 | ||
1629626f FB |
2370 | codec_id = file_oformat->video_codec; |
2371 | if (video_codec_id != CODEC_ID_NONE) | |
2372 | codec_id = video_codec_id; | |
2373 | ||
2374 | video_enc->codec_id = codec_id; | |
2375 | ||
2376 | video_enc->bit_rate = video_bit_rate; | |
2377 | video_enc->bit_rate_tolerance = video_bit_rate_tolerance; | |
2378 | video_enc->frame_rate = frame_rate; | |
14bea432 | 2379 | video_enc->frame_rate_base = frame_rate_base; |
1629626f FB |
2380 | |
2381 | video_enc->width = frame_width; | |
2382 | video_enc->height = frame_height; | |
1c0dcc39 | 2383 | video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255); |
63167088 | 2384 | video_enc->pix_fmt = frame_pix_fmt; |
1629626f FB |
2385 | |
2386 | if (!intra_only) | |
2387 | video_enc->gop_size = gop_size; | |
2388 | else | |
2389 | video_enc->gop_size = 0; | |
2390 | if (video_qscale || same_quality) { | |
2391 | video_enc->flags |= CODEC_FLAG_QSCALE; | |
158c7f05 | 2392 | st->quality = FF_QP2LAMBDA * video_qscale; |
1629626f | 2393 | } |
84f608f4 VM |
2394 | |
2395 | if(intra_matrix) | |
2396 | video_enc->intra_matrix = intra_matrix; | |
2397 | if(inter_matrix) | |
2398 | video_enc->inter_matrix = inter_matrix; | |
2399 | ||
b0368839 MN |
2400 | if(bitexact) |
2401 | video_enc->flags |= CODEC_FLAG_BITEXACT; | |
2402 | ||
7d1c3fc1 | 2403 | video_enc->mb_decision = mb_decision; |
5d43635e | 2404 | video_enc->mb_cmp = mb_cmp; |
622348f9 | 2405 | video_enc->ildct_cmp = ildct_cmp; |
5d43635e MN |
2406 | video_enc->me_sub_cmp = sub_cmp; |
2407 | video_enc->me_cmp = cmp; | |
77ea0d4b | 2408 | video_enc->quantizer_noise_shaping= qns; |
7d1c3fc1 | 2409 | |
21e59552 MN |
2410 | if (use_umv) { |
2411 | video_enc->flags |= CODEC_FLAG_H263P_UMV; | |
2412 | } | |
2413 | if (use_aic) { | |
2414 | video_enc->flags |= CODEC_FLAG_H263P_AIC; | |
2415 | } | |
dba019da MN |
2416 | if (use_aiv) { |
2417 | video_enc->flags |= CODEC_FLAG_H263P_AIV; | |
2418 | } | |
1629626f | 2419 | if (use_4mv) { |
1629626f FB |
2420 | video_enc->flags |= CODEC_FLAG_4MV; |
2421 | } | |
8e2162f0 MN |
2422 | if (use_obmc) { |
2423 | video_enc->flags |= CODEC_FLAG_OBMC; | |
2424 | } | |
2425 | ||
e56d417b | 2426 | if(use_part) { |
1629626f | 2427 | video_enc->flags |= CODEC_FLAG_PART; |
e56d417b | 2428 | } |
bb198e19 MN |
2429 | if (use_alt_scan) { |
2430 | video_enc->flags |= CODEC_FLAG_ALT_SCAN; | |
2431 | } | |
c0baa56a MN |
2432 | if (use_trell) { |
2433 | video_enc->flags |= CODEC_FLAG_TRELLIS_QUANT; | |
2434 | } | |
baaf3f46 MN |
2435 | if (use_scan_offset) { |
2436 | video_enc->flags |= CODEC_FLAG_SVCD_SCAN_OFFSET; | |
2437 | } | |
303e50e6 MN |
2438 | if (closed_gop) { |
2439 | video_enc->flags |= CODEC_FLAG_CLOSED_GOP; | |
2440 | } | |
1629626f | 2441 | if (b_frames) { |
1629626f FB |
2442 | video_enc->max_b_frames = b_frames; |
2443 | video_enc->b_frame_strategy = 0; | |
2444 | video_enc->b_quant_factor = 2.0; | |
bc6caae2 | 2445 | } |
bb198e19 | 2446 | if (do_interlace_dct) { |
e56d417b FB |
2447 | video_enc->flags |= CODEC_FLAG_INTERLACED_DCT; |
2448 | } | |
bb198e19 MN |
2449 | if (do_interlace_me) { |
2450 | video_enc->flags |= CODEC_FLAG_INTERLACED_ME; | |
2451 | } | |
1629626f FB |
2452 | video_enc->qmin = video_qmin; |
2453 | video_enc->qmax = video_qmax; | |
17a70fde MN |
2454 | video_enc->mb_qmin = video_mb_qmin; |
2455 | video_enc->mb_qmax = video_mb_qmax; | |
1629626f FB |
2456 | video_enc->max_qdiff = video_qdiff; |
2457 | video_enc->qblur = video_qblur; | |
2458 | video_enc->qcompress = video_qcomp; | |
2459 | video_enc->rc_eq = video_rc_eq; | |
0c9bbaec WH |
2460 | video_enc->debug = debug; |
2461 | video_enc->debug_mv = debug_mv; | |
ac2830ec MN |
2462 | p= video_rc_override_string; |
2463 | for(i=0; p; i++){ | |
2464 | int start, end, q; | |
2465 | int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
2466 | if(e!=3){ | |
2467 | fprintf(stderr, "error parsing rc_override\n"); | |
2468 | exit(1); | |
2469 | } | |
2470 | video_enc->rc_override= | |
47e2a6e6 FB |
2471 | av_realloc(video_enc->rc_override, |
2472 | sizeof(RcOverride)*(i+1)); | |
ac2830ec MN |
2473 | video_enc->rc_override[i].start_frame= start; |
2474 | video_enc->rc_override[i].end_frame = end; | |
2475 | if(q>0){ | |
2476 | video_enc->rc_override[i].qscale= q; | |
2477 | video_enc->rc_override[i].quality_factor= 1.0; | |
2478 | } | |
2479 | else{ | |
2480 | video_enc->rc_override[i].qscale= 0; | |
2481 | video_enc->rc_override[i].quality_factor= -q/100.0; | |
2482 | } | |
2483 | p= strchr(p, '/'); | |
2484 | if(p) p++; | |
2485 | } | |
2486 | video_enc->rc_override_count=i; | |
2487 | ||
1629626f FB |
2488 | video_enc->rc_max_rate = video_rc_max_rate; |
2489 | video_enc->rc_min_rate = video_rc_min_rate; | |
2490 | video_enc->rc_buffer_size = video_rc_buffer_size; | |
2491 | video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity; | |
ac2830ec | 2492 | video_enc->rc_initial_cplx= video_rc_initial_cplx; |
1629626f FB |
2493 | video_enc->i_quant_factor = video_i_qfactor; |
2494 | video_enc->b_quant_factor = video_b_qfactor; | |
2495 | video_enc->i_quant_offset = video_i_qoffset; | |
2496 | video_enc->b_quant_offset = video_b_qoffset; | |
303e50e6 MN |
2497 | video_enc->intra_quant_bias = video_intra_quant_bias; |
2498 | video_enc->inter_quant_bias = video_inter_quant_bias; | |
1629626f FB |
2499 | video_enc->dct_algo = dct_algo; |
2500 | video_enc->idct_algo = idct_algo; | |
f560dd82 | 2501 | video_enc->strict_std_compliance = strict; |
7ebfc0ea | 2502 | video_enc->error_rate = error_rate; |
bb198e19 | 2503 | video_enc->noise_reduction= noise_reduction; |
303e50e6 | 2504 | video_enc->scenechange_threshold= sc_threshold; |
1629626f FB |
2505 | if(packet_size){ |
2506 | video_enc->rtp_mode= 1; | |
2507 | video_enc->rtp_payload_size= packet_size; | |
2508 | } | |
9cdd6a24 | 2509 | |
1629626f | 2510 | if (do_psnr) |
140cb663 | 2511 | video_enc->flags|= CODEC_FLAG_PSNR; |
e4986da9 | 2512 | |
1629626f | 2513 | video_enc->me_method = me_method; |
5abdb4b1 | 2514 | |
1629626f FB |
2515 | /* two pass mode */ |
2516 | if (do_pass) { | |
2517 | if (do_pass == 1) { | |
2518 | video_enc->flags |= CODEC_FLAG_PASS1; | |
2519 | } else { | |
2520 | video_enc->flags |= CODEC_FLAG_PASS2; | |
2521 | } | |
5abdb4b1 | 2522 | } |
cfcf0ffd | 2523 | } |
85f07f22 FB |
2524 | oc->streams[nb_streams] = st; |
2525 | nb_streams++; | |
2526 | } | |
2527 | ||
2528 | if (use_audio) { | |
2529 | AVCodecContext *audio_enc; | |
2530 | ||
2531 | st = av_mallocz(sizeof(AVStream)); | |
2532 | if (!st) { | |
2533 | fprintf(stderr, "Could not alloc stream\n"); | |
2534 | exit(1); | |
2535 | } | |
1e491e29 | 2536 | avcodec_get_context_defaults(&st->codec); |
1629626f | 2537 | |
85f07f22 | 2538 | audio_enc = &st->codec; |
85f07f22 | 2539 | audio_enc->codec_type = CODEC_TYPE_AUDIO; |
7f96ed5b MN |
2540 | |
2541 | if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp")) | |
2542 | audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
1629626f FB |
2543 | if (audio_stream_copy) { |
2544 | st->stream_copy = 1; | |
2545 | } else { | |
2546 | codec_id = file_oformat->audio_codec; | |
2547 | if (audio_codec_id != CODEC_ID_NONE) | |
2548 | codec_id = audio_codec_id; | |
2549 | audio_enc->codec_id = codec_id; | |
2550 | ||
2551 | audio_enc->bit_rate = audio_bit_rate; | |
2552 | audio_enc->sample_rate = audio_sample_rate; | |
2553 | /* For audio codecs other than AC3 we limit */ | |
2554 | /* the number of coded channels to stereo */ | |
2555 | if (audio_channels > 2 && codec_id != CODEC_ID_AC3) { | |
2556 | audio_enc->channels = 2; | |
2557 | } else | |
2558 | audio_enc->channels = audio_channels; | |
2559 | } | |
85f07f22 FB |
2560 | oc->streams[nb_streams] = st; |
2561 | nb_streams++; | |
2562 | } | |
2563 | ||
2564 | oc->nb_streams = nb_streams; | |
2565 | ||
2566 | if (!nb_streams) { | |
919f448d | 2567 | fprintf(stderr, "No audio or video streams available\n"); |
85f07f22 FB |
2568 | exit(1); |
2569 | } | |
2570 | ||
2571 | if (str_title) | |
79fdaa4c | 2572 | pstrcpy(oc->title, sizeof(oc->title), str_title); |
85f07f22 | 2573 | if (str_author) |
79fdaa4c | 2574 | pstrcpy(oc->author, sizeof(oc->author), str_author); |
85f07f22 | 2575 | if (str_copyright) |
79fdaa4c | 2576 | pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright); |
85f07f22 | 2577 | if (str_comment) |
79fdaa4c | 2578 | pstrcpy(oc->comment, sizeof(oc->comment), str_comment); |
85f07f22 FB |
2579 | } |
2580 | ||
1629626f | 2581 | output_files[nb_output_files++] = oc; |
85f07f22 FB |
2582 | |
2583 | strcpy(oc->filename, filename); | |
919f448d FB |
2584 | |
2585 | /* check filename in case of an image number is expected */ | |
79fdaa4c FB |
2586 | if (oc->oformat->flags & AVFMT_NEEDNUMBER) { |
2587 | if (filename_number_test(oc->filename) < 0) { | |
2588 | print_error(oc->filename, AVERROR_NUMEXPECTED); | |
919f448d | 2589 | exit(1); |
79fdaa4c | 2590 | } |
919f448d FB |
2591 | } |
2592 | ||
79fdaa4c | 2593 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { |
85f07f22 FB |
2594 | /* test if it already exists to avoid loosing precious files */ |
2595 | if (!file_overwrite && | |
2596 | (strchr(filename, ':') == NULL || | |
2597 | strstart(filename, "file:", NULL))) { | |
2598 | if (url_exist(filename)) { | |
2599 | int c; | |
2600 | ||
d9a916e2 CY |
2601 | if ( !using_stdin ) { |
2602 | fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename); | |
2603 | fflush(stderr); | |
2604 | c = getchar(); | |
2605 | if (toupper(c) != 'Y') { | |
2606 | fprintf(stderr, "Not overwriting - exiting\n"); | |
2607 | exit(1); | |
2608 | } | |
2609 | } | |
2610 | else { | |
2611 | fprintf(stderr,"File '%s' already exists. Exiting.\n", filename); | |
85f07f22 | 2612 | exit(1); |
d9a916e2 | 2613 | } |
85f07f22 FB |
2614 | } |
2615 | } | |
2616 | ||
2617 | /* open the file */ | |
2618 | if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) { | |
2619 | fprintf(stderr, "Could not open '%s'\n", filename); | |
2620 | exit(1); | |
2621 | } | |
2622 | } | |
2623 | ||
817b23ff FB |
2624 | memset(ap, 0, sizeof(*ap)); |
2625 | ap->image_format = image_format; | |
2626 | if (av_set_parameters(oc, ap) < 0) { | |
2627 | fprintf(stderr, "%s: Invalid encoding parameters\n", | |
2628 | oc->filename); | |
2629 | exit(1); | |
2630 | } | |
2631 | ||
85f07f22 | 2632 | /* reset some options */ |
79fdaa4c FB |
2633 | file_oformat = NULL; |
2634 | file_iformat = NULL; | |
817b23ff | 2635 | image_format = NULL; |
85f07f22 FB |
2636 | audio_disable = 0; |
2637 | video_disable = 0; | |
2638 | audio_codec_id = CODEC_ID_NONE; | |
2639 | video_codec_id = CODEC_ID_NONE; | |
1629626f FB |
2640 | audio_stream_copy = 0; |
2641 | video_stream_copy = 0; | |
85f07f22 FB |
2642 | } |
2643 | ||
a38469e1 | 2644 | /* prepare dummy protocols for grab */ |
b29f97d1 | 2645 | static void prepare_grab(void) |
a38469e1 FB |
2646 | { |
2647 | int has_video, has_audio, i, j; | |
2648 | AVFormatContext *oc; | |
2649 | AVFormatContext *ic; | |
79a7c268 | 2650 | AVFormatParameters vp1, *vp = &vp1; |
a38469e1 | 2651 | AVFormatParameters ap1, *ap = &ap1; |
79a7c268 | 2652 | |
a38469e1 FB |
2653 | /* see if audio/video inputs are needed */ |
2654 | has_video = 0; | |
2655 | has_audio = 0; | |
2656 | memset(ap, 0, sizeof(*ap)); | |
79a7c268 | 2657 | memset(vp, 0, sizeof(*vp)); |
a38469e1 FB |
2658 | for(j=0;j<nb_output_files;j++) { |
2659 | oc = output_files[j]; | |
2660 | for(i=0;i<oc->nb_streams;i++) { | |
2661 | AVCodecContext *enc = &oc->streams[i]->codec; | |
2662 | switch(enc->codec_type) { | |
2663 | case CODEC_TYPE_AUDIO: | |
2664 | if (enc->sample_rate > ap->sample_rate) | |
2665 | ap->sample_rate = enc->sample_rate; | |
2666 | if (enc->channels > ap->channels) | |
2667 | ap->channels = enc->channels; | |
2668 | has_audio = 1; | |
2669 | break; | |
2670 | case CODEC_TYPE_VIDEO: | |
79a7c268 FB |
2671 | if (enc->width > vp->width) |
2672 | vp->width = enc->width; | |
2673 | if (enc->height > vp->height) | |
2674 | vp->height = enc->height; | |
14bea432 MN |
2675 | |
2676 | assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE); | |
2677 | if (enc->frame_rate > vp->frame_rate){ | |
2678 | vp->frame_rate = enc->frame_rate; | |
2679 | vp->frame_rate_base = enc->frame_rate_base; | |
2680 | } | |
a38469e1 FB |
2681 | has_video = 1; |
2682 | break; | |
51bd4565 | 2683 | default: |
c04643a2 | 2684 | av_abort(); |
a38469e1 FB |
2685 | } |
2686 | } | |
2687 | } | |
2688 | ||
2689 | if (has_video == 0 && has_audio == 0) { | |
2690 | fprintf(stderr, "Output file must have at least one audio or video stream\n"); | |
2691 | exit(1); | |
2692 | } | |
2693 | ||
2694 | if (has_video) { | |
79fdaa4c | 2695 | AVInputFormat *fmt1; |
8aa3ee32 | 2696 | fmt1 = av_find_input_format(video_grab_format); |
a5df11ab FB |
2697 | vp->device = video_device; |
2698 | vp->channel = video_channel; | |
e3ee3283 | 2699 | vp->standard = video_standard; |
79a7c268 | 2700 | if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) { |
bf5af568 | 2701 | fprintf(stderr, "Could not find video grab device\n"); |
a38469e1 FB |
2702 | exit(1); |
2703 | } | |
ddaae6a9 RS |
2704 | /* If not enough info to get the stream parameters, we decode the |
2705 | first frames to get it. */ | |
2706 | if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && av_find_stream_info(ic) < 0) { | |
2707 | fprintf(stderr, "Could not find video grab parameters\n"); | |
2708 | exit(1); | |
2709 | } | |
79fdaa4c | 2710 | /* by now video grab has one stream */ |
14bea432 MN |
2711 | ic->streams[0]->r_frame_rate = vp->frame_rate; |
2712 | ic->streams[0]->r_frame_rate_base = vp->frame_rate_base; | |
a38469e1 | 2713 | input_files[nb_input_files] = ic; |
79a7c268 | 2714 | dump_format(ic, nb_input_files, "", 0); |
a38469e1 FB |
2715 | nb_input_files++; |
2716 | } | |
1501987b | 2717 | if (has_audio && audio_grab_format) { |
79fdaa4c | 2718 | AVInputFormat *fmt1; |
8aa3ee32 | 2719 | fmt1 = av_find_input_format(audio_grab_format); |
79a7c268 | 2720 | ap->device = audio_device; |
79fdaa4c | 2721 | if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) { |
bf5af568 | 2722 | fprintf(stderr, "Could not find audio grab device\n"); |
a38469e1 FB |
2723 | exit(1); |
2724 | } | |
2725 | input_files[nb_input_files] = ic; | |
79a7c268 | 2726 | dump_format(ic, nb_input_files, "", 0); |
a38469e1 FB |
2727 | nb_input_files++; |
2728 | } | |
2729 | } | |
2730 | ||
5abdb4b1 | 2731 | /* same option as mencoder */ |
b29f97d1 | 2732 | static void opt_pass(const char *pass_str) |
5abdb4b1 FB |
2733 | { |
2734 | int pass; | |
2735 | pass = atoi(pass_str); | |
2736 | if (pass != 1 && pass != 2) { | |
2737 | fprintf(stderr, "pass number can be only 1 or 2\n"); | |
2738 | exit(1); | |
2739 | } | |
2740 | do_pass = pass; | |
2741 | } | |
a38469e1 | 2742 | |
f3ec2d46 | 2743 | #if defined(CONFIG_WIN32) || defined(CONFIG_OS2) |
0c1a9eda | 2744 | static int64_t getutime(void) |
5727b222 | 2745 | { |
f3ec2d46 | 2746 | return av_gettime(); |
5727b222 | 2747 | } |
bdc4796f | 2748 | #else |
0c1a9eda | 2749 | static int64_t getutime(void) |
bdc4796f | 2750 | { |
f3ec2d46 SG |
2751 | struct rusage rusage; |
2752 | ||
2753 | getrusage(RUSAGE_SELF, &rusage); | |
2754 | return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec; | |
bdc4796f FB |
2755 | } |
2756 | #endif | |
5727b222 | 2757 | |
79fdaa4c FB |
2758 | extern int ffm_nopts; |
2759 | ||
b29f97d1 | 2760 | static void opt_bitexact(void) |
79fdaa4c | 2761 | { |
b0368839 | 2762 | bitexact=1; |
79fdaa4c FB |
2763 | /* disable generate of real time pts in ffm (need to be supressed anyway) */ |
2764 | ffm_nopts = 1; | |
2765 | } | |
2766 | ||
b29f97d1 | 2767 | static void show_formats(void) |
85f07f22 | 2768 | { |
79fdaa4c FB |
2769 | AVInputFormat *ifmt; |
2770 | AVOutputFormat *ofmt; | |
817b23ff | 2771 | AVImageFormat *image_fmt; |
85f07f22 FB |
2772 | URLProtocol *up; |
2773 | AVCodec *p; | |
2774 | const char **pp; | |
2775 | ||
817b23ff | 2776 | printf("Output audio/video file formats:"); |
79fdaa4c FB |
2777 | for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) { |
2778 | printf(" %s", ofmt->name); | |
85f07f22 FB |
2779 | } |
2780 | printf("\n"); | |
817b23ff FB |
2781 | |
2782 | printf("Input audio/video file formats:"); | |
79fdaa4c FB |
2783 | for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) { |
2784 | printf(" %s", ifmt->name); | |
85f07f22 FB |
2785 | } |
2786 | printf("\n"); | |
2787 | ||
817b23ff FB |
2788 | printf("Output image formats:"); |
2789 | for(image_fmt = first_image_format; image_fmt != NULL; | |
2790 | image_fmt = image_fmt->next) { | |
2791 | if (image_fmt->img_write) | |
2792 | printf(" %s", image_fmt->name); | |
2793 | } | |
2794 | printf("\n"); | |
2795 | ||
2796 | printf("Input image formats:"); | |
2797 | for(image_fmt = first_image_format; image_fmt != NULL; | |
2798 | image_fmt = image_fmt->next) { | |
2799 | if (image_fmt->img_read) | |
2800 | printf(" %s", image_fmt->name); | |
2801 | } | |
2802 | printf("\n"); | |
2803 | ||
85f07f22 FB |
2804 | printf("Codecs:\n"); |
2805 | printf(" Encoders:"); | |
2806 | for(p = first_avcodec; p != NULL; p = p->next) { | |
2807 | if (p->encode) | |
2808 | printf(" %s", p->name); | |
2809 | } | |
2810 | printf("\n"); | |
2811 | ||
2812 | printf(" Decoders:"); | |
2813 | for(p = first_avcodec; p != NULL; p = p->next) { | |
2814 | if (p->decode) | |
2815 | printf(" %s", p->name); | |
2816 | } | |
2817 | printf("\n"); | |
2818 | ||
2819 | printf("Supported file protocols:"); | |
2820 | for(up = first_protocol; up != NULL; up = up->next) | |
2821 | printf(" %s:", up->name); | |
2822 | printf("\n"); | |
2823 | ||
ba2a8cb4 | 2824 | printf("Frame size, frame rate abbreviations: ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n"); |
85f07f22 FB |
2825 | printf("Motion estimation methods:"); |
2826 | pp = motion_str; | |
2827 | while (*pp) { | |
2828 | printf(" %s", *pp); | |
101bea5f | 2829 | if ((pp - motion_str + 1) == ME_ZERO) |
85f07f22 | 2830 | printf("(fastest)"); |
101bea5f | 2831 | else if ((pp - motion_str + 1) == ME_FULL) |
85f07f22 | 2832 | printf("(slowest)"); |
101bea5f | 2833 | else if ((pp - motion_str + 1) == ME_EPZS) |
85f07f22 FB |
2834 | printf("(default)"); |
2835 | pp++; | |
2836 | } | |
2837 | printf("\n"); | |
2838 | exit(1); | |
2839 | } | |
2840 | ||
84f608f4 VM |
2841 | void parse_matrix_coeffs(uint16_t *dest, const char *str) |
2842 | { | |
2843 | int i; | |
2844 | const char *p = str; | |
2845 | for(i = 0;; i++) { | |
2846 | dest[i] = atoi(p); | |
2847 | if(i == 63) | |
2848 | break; | |
2849 | p = strchr(p, ','); | |
2850 | if(!p) { | |
2851 | fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i); | |
2852 | exit(1); | |
2853 | } | |
2854 | p++; | |
2855 | } | |
2856 | } | |
2857 | ||
2858 | void opt_inter_matrix(const char *arg) | |
2859 | { | |
2860 | inter_matrix = av_mallocz(sizeof(uint16_t) * 64); | |
2861 | parse_matrix_coeffs(inter_matrix, arg); | |
2862 | } | |
2863 | ||
2864 | void opt_intra_matrix(const char *arg) | |
2865 | { | |
2866 | intra_matrix = av_mallocz(sizeof(uint16_t) * 64); | |
2867 | parse_matrix_coeffs(intra_matrix, arg); | |
2868 | } | |
2869 | ||
e0595741 VM |
2870 | static void opt_target(const char *arg) |
2871 | { | |
2872 | int norm = -1; | |
2873 | ||
2874 | if(!strncmp(arg, "pal-", 4)) { | |
2875 | norm = 0; | |
2876 | arg += 4; | |
2877 | } else if(!strncmp(arg, "ntsc-", 5)) { | |
2878 | norm = 1; | |
2879 | arg += 5; | |
2880 | } else { | |
2881 | int fr; | |
2882 | /* Calculate FR via float to avoid int overflow */ | |
2883 | fr = (int)(frame_rate * 1000.0 / frame_rate_base); | |
2884 | if(fr == 25000) { | |
2885 | norm = 0; | |
2886 | } else if((fr == 29970) || (fr == 23976)) { | |
2887 | norm = 1; | |
2888 | } else { | |
2889 | /* Try to determine PAL/NTSC by peeking in the input files */ | |
2890 | if(nb_input_files) { | |
2891 | int i, j; | |
2892 | for(j = 0; j < nb_input_files; j++) { | |
2893 | for(i = 0; i < input_files[j]->nb_streams; i++) { | |
2894 | AVCodecContext *c = &input_files[j]->streams[i]->codec; | |
2895 | if(c->codec_type != CODEC_TYPE_VIDEO) | |
2896 | continue; | |
2897 | fr = c->frame_rate * 1000 / c->frame_rate_base; | |
2898 | if(fr == 25000) { | |
2899 | norm = 0; | |
2900 | break; | |
2901 | } else if((fr == 29970) || (fr == 23976)) { | |
2902 | norm = 1; | |
2903 | break; | |
2904 | } | |
2905 | } | |
2906 | if(norm >= 0) | |
2907 | break; | |
2908 | } | |
2909 | } | |
2910 | } | |
2911 | if(verbose && norm >= 0) | |
2912 | printf("Assuming %s for target.\n", norm ? "NTSC" : "PAL"); | |
2913 | } | |
2914 | ||
2915 | if(norm < 0) { | |
2916 | fprintf(stderr, "Could not determine norm (PAL/NTSC) for target.\n"); | |
2917 | fprintf(stderr, "Please prefix target with \"pal-\" or \"ntsc-\",\n"); | |
2918 | fprintf(stderr, "or set a framerate with \"-r xxx\".\n"); | |
2919 | exit(1); | |
2920 | } | |
2921 | ||
2922 | if(!strcmp(arg, "vcd")) { | |
2923 | ||
2924 | opt_video_codec("mpeg1video"); | |
2925 | opt_audio_codec("mp2"); | |
2926 | opt_format("vcd"); | |
2927 | ||
2928 | opt_frame_size(norm ? "352x240" : "352x288"); | |
2929 | ||
2930 | video_bit_rate = 1150000; | |
2931 | video_rc_max_rate = 1150000; | |
2932 | video_rc_min_rate = 1150000; | |
fb066639 | 2933 | video_rc_buffer_size = 40*1024*8; |
e0595741 VM |
2934 | |
2935 | audio_bit_rate = 224000; | |
2936 | audio_sample_rate = 44100; | |
2937 | ||
2938 | } else if(!strcmp(arg, "svcd")) { | |
2939 | ||
2940 | opt_video_codec("mpeg2video"); | |
2941 | opt_audio_codec("mp2"); | |
2942 | opt_format("vcd"); | |
2943 | ||
2944 | opt_frame_size(norm ? "480x480" : "480x576"); | |
2945 | opt_gop_size(norm ? "18" : "15"); | |
2946 | ||
2947 | video_bit_rate = 2040000; | |
2948 | video_rc_max_rate = 2516000; | |
55bbad6f | 2949 | video_rc_min_rate = 0; //1145000; |
fb066639 | 2950 | video_rc_buffer_size = 224*1024*8; |
baaf3f46 | 2951 | use_scan_offset = 1; |
e0595741 VM |
2952 | |
2953 | audio_bit_rate = 224000; | |
2954 | audio_sample_rate = 44100; | |
2955 | ||
2956 | } else if(!strcmp(arg, "dvd")) { | |
2957 | ||
2958 | opt_video_codec("mpeg2video"); | |
2959 | opt_audio_codec("ac3"); | |
2960 | opt_format("vob"); | |
2961 | ||
2962 | opt_frame_size(norm ? "720x480" : "720x576"); | |
2963 | opt_gop_size(norm ? "18" : "15"); | |
2964 | ||
2965 | video_bit_rate = 6000000; | |
2966 | video_rc_max_rate = 9000000; | |
55bbad6f | 2967 | video_rc_min_rate = 0; //1500000; |
fb066639 | 2968 | video_rc_buffer_size = 224*1024*8; |
e0595741 VM |
2969 | |
2970 | audio_bit_rate = 448000; | |
2971 | audio_sample_rate = 48000; | |
2972 | ||
2973 | } else { | |
2974 | fprintf(stderr, "Unknown target: %s\n", arg); | |
2975 | exit(1); | |
2976 | } | |
2977 | } | |
2978 | ||
85f07f22 | 2979 | const OptionDef options[] = { |
02d504a7 FB |
2980 | /* main options */ |
2981 | { "L", 0, {(void*)show_license}, "show license" }, | |
bdc4796f FB |
2982 | { "h", 0, {(void*)show_help}, "show help" }, |
2983 | { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." }, | |
2984 | { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" }, | |
817b23ff | 2985 | { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" }, |
bdc4796f FB |
2986 | { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" }, |
2987 | { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" }, | |
2988 | { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" }, | |
2989 | { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" }, | |
254abc2e | 2990 | { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" }, |
bdc4796f FB |
2991 | { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" }, |
2992 | { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" }, | |
2993 | { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" }, | |
2994 | { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" }, | |
1d366fce | 2995 | { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" }, |
0c9bbaec | 2996 | { "vismv", HAS_ARG | OPT_EXPERT, {(void*)opt_vismv}, "visualize motion vectors", "" }, |
bdc4796f | 2997 | { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark}, |
5727b222 | 2998 | "add timings for benchmarking" }, |
254abc2e | 2999 | { "dump", OPT_BOOL | OPT_EXPERT, {(void*)&do_pkt_dump}, |
a0663ba4 | 3000 | "dump each input packet" }, |
254abc2e FB |
3001 | { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump}, |
3002 | "when dumping packets, also dump the payload" }, | |
79fdaa4c | 3003 | { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" }, |
b1b77fe9 FB |
3004 | { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" }, |
3005 | { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" }, | |
f068206e | 3006 | { "v", HAS_ARG, {(void*)opt_verbose}, "control amount of logging", "verbose" }, |
e0595741 | 3007 | { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\" or \"dvd\")", "type" }, |
02d504a7 FB |
3008 | |
3009 | /* video options */ | |
3010 | { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" }, | |
3011 | { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" }, | |
02d504a7 FB |
3012 | { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" }, |
3013 | { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" }, | |
3014 | { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" }, | |
3015 | { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" }, | |
3016 | { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" }, | |
3017 | { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" }, | |
3018 | { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" }, | |
3019 | { "g", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" }, | |
3020 | { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"}, | |
3021 | { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" }, | |
3022 | { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" }, | |
3023 | { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" }, | |
3024 | { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" }, | |
3025 | { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" }, | |
3026 | { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" }, | |
3027 | { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" }, | |
3028 | { "qblur", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" }, | |
3029 | { "qcomp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" }, | |
3030 | { "rc_init_cplx", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" }, | |
3031 | { "b_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" }, | |
3032 | { "i_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" }, | |
3033 | { "b_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" }, | |
3034 | { "i_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" }, | |
303e50e6 MN |
3035 | { "ibias", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_ibias}, "intra quant bias", "bias" }, |
3036 | { "pbias", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_pbias}, "inter quant bias", "bias" }, | |
02d504a7 FB |
3037 | // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" }, |
3038 | { "rc_eq", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_eq}, "set rate control equation", "equation" }, | |
d95ac2c5 | 3039 | { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" }, |
02d504a7 FB |
3040 | { "bt", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" }, |
3041 | { "maxrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" }, | |
3042 | { "minrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" }, | |
622348f9 | 3043 | { "bufsize", HAS_ARG | OPT_VIDEO, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kByte)", "size" }, |
02d504a7 FB |
3044 | { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" }, |
3045 | { "me", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_motion_estimation}, "set motion estimation method", | |
3046 | "method" }, | |
3047 | { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo", "algo" }, | |
3048 | { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo", "algo" }, | |
3049 | { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience", "n" }, | |
d95ac2c5 | 3050 | { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment", "bit_mask" }, |
f42eda74 | 3051 | { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames", "frames" }, |
02d504a7 FB |
3052 | { "hq", OPT_BOOL, {(void*)&mb_decision}, "activate high quality settings" }, |
3053 | { "mbd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_decision}, "macroblock decision", "mode" }, | |
5d43635e | 3054 | { "mbcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_cmp}, "macroblock compare function", "cmp function" }, |
622348f9 | 3055 | { "ildctcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_ildct_cmp}, "ildct compare function", "cmp function" }, |
5d43635e MN |
3056 | { "subcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sub_cmp}, "subpel compare function", "cmp function" }, |
3057 | { "cmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_cmp}, "fullpel compare function", "cmp function" }, | |
e56d417b | 3058 | { "4mv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_4mv}, "use four motion vector by macroblock (MPEG4)" }, |
8e2162f0 | 3059 | { "obmc", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_obmc}, "use overlapped block motion compensation (h263+)" }, |
e56d417b | 3060 | { "part", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_part}, "use data partitioning (MPEG4)" }, |
02d504a7 FB |
3061 | { "bug", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" }, |
3062 | { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "set packet size in bits", "size" }, | |
7ebfc0ea | 3063 | { "error", HAS_ARG | OPT_EXPERT, {(void*)opt_error_rate}, "error rate", "rate" }, |
02d504a7 FB |
3064 | { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standarts", "strictness" }, |
3065 | { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality}, | |
3066 | "use same video quality as source (implies VBR)" }, | |
3067 | { "pass", HAS_ARG | OPT_VIDEO, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" }, | |
3068 | { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename}, "select two pass log file name", "file" }, | |
3069 | { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace}, | |
3070 | "deinterlace pictures" }, | |
bb198e19 MN |
3071 | { "ildct", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_interlace_dct}, |
3072 | "force interlaced dct support in encoder (MPEG2/MPEG4)" }, | |
3073 | { "ilme", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_interlace_me}, | |
3074 | "force interlacied me support in encoder MPEG2" }, | |
02d504a7 FB |
3075 | { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" }, |
3076 | { "vstats", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_vstats}, "dump video coding statistics to file" }, | |
3077 | { "vhook", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)add_frame_hooker}, "insert video processing module", "module" }, | |
02d504a7 | 3078 | { "aic", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" }, |
dba019da | 3079 | { "aiv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aiv}, "enable Alternative inter vlc (h263+)" }, |
02d504a7 | 3080 | { "umv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" }, |
bb198e19 | 3081 | { "alt", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_alt_scan}, "enable alternate scantable (mpeg2)" }, |
c0baa56a | 3082 | { "trell", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_trell}, "enable trellis quantization" }, |
303e50e6 | 3083 | { "cgop", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&closed_gop}, "closed gop" }, |
baaf3f46 | 3084 | { "scan_offset", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_scan_offset}, "enable SVCD Scan Offset placeholder" }, |
84f608f4 VM |
3085 | { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_intra_matrix}, "specify intra matrix coeffs", "matrix" }, |
3086 | { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_inter_matrix}, "specify inter matrix coeffs", "matrix" }, | |
bb198e19 MN |
3087 | { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_top_field_first}, "top=1/bottom=0/auto=-1 field first", "" }, |
3088 | { "nr", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_noise_reduction}, "noise reduction", "" }, | |
77ea0d4b | 3089 | { "qns", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qns}, "quantization noise shaping", "" }, |
303e50e6 | 3090 | { "sc_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sc_threshold}, "scene change threshold", "threshold" }, |
02d504a7 FB |
3091 | |
3092 | /* audio options */ | |
3093 | { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", }, | |
3094 | { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" }, | |
3095 | { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" }, | |
3096 | { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" }, | |
3097 | { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" }, | |
3098 | ||
3099 | /* grab options */ | |
3100 | { "vd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_device}, "set video grab device", "device" }, | |
3101 | { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" }, | |
3102 | { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" }, | |
3103 | { "dv1394", OPT_EXPERT | OPT_GRAB, {(void*)opt_dv1394}, "set DV1394 grab", "" }, | |
3104 | { "ad", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_GRAB, {(void*)opt_audio_device}, "set audio device", "device" }, | |
85f07f22 FB |
3105 | { NULL, }, |
3106 | }; | |
3107 | ||
02d504a7 FB |
3108 | static void show_banner(void) |
3109 | { | |
3110 | printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2003 Fabrice Bellard\n"); | |
3111 | } | |
3112 | ||
3113 | static void show_license(void) | |
3114 | { | |
3115 | show_banner(); | |
3116 | printf( | |
3117 | "This library is free software; you can redistribute it and/or\n" | |
3118 | "modify it under the terms of the GNU Lesser General Public\n" | |
3119 | "License as published by the Free Software Foundation; either\n" | |
3120 | "version 2 of the License, or (at your option) any later version.\n" | |
3121 | "\n" | |
3122 | "This library is distributed in the hope that it will be useful,\n" | |
3123 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | |
3124 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" | |
3125 | "Lesser General Public License for more details.\n" | |
3126 | "\n" | |
3127 | "You should have received a copy of the GNU Lesser General Public\n" | |
3128 | "License along with this library; if not, write to the Free Software\n" | |
3129 | "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n" | |
3130 | ); | |
3131 | exit(1); | |
3132 | } | |
3133 | ||
3134 | static void show_help(void) | |
01310af2 | 3135 | { |
02d504a7 | 3136 | show_banner(); |
01310af2 FB |
3137 | printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n" |
3138 | "Hyper fast Audio and Video encoder\n"); | |
3139 | printf("\n"); | |
02d504a7 FB |
3140 | show_help_options(options, "Main options:\n", |
3141 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO, 0); | |
3142 | show_help_options(options, "\nVideo options:\n", | |
3143 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3144 | OPT_VIDEO); | |
3145 | show_help_options(options, "\nAdvanced Video options:\n", | |
3146 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3147 | OPT_VIDEO | OPT_EXPERT); | |
3148 | show_help_options(options, "\nAudio options:\n", | |
3149 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3150 | OPT_AUDIO); | |
3151 | show_help_options(options, "\nAdvanced Audio options:\n", | |
3152 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3153 | OPT_AUDIO | OPT_EXPERT); | |
3154 | show_help_options(options, "\nAudio/Video grab options:\n", | |
3155 | OPT_GRAB, | |
3156 | OPT_GRAB); | |
3157 | show_help_options(options, "\nAdvanced options:\n", | |
3158 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3159 | OPT_EXPERT); | |
01310af2 FB |
3160 | exit(1); |
3161 | } | |
3162 | ||
3163 | void parse_arg_file(const char *filename) | |
3164 | { | |
3165 | opt_output_file(filename); | |
3166 | } | |
3167 | ||
85f07f22 FB |
3168 | int main(int argc, char **argv) |
3169 | { | |
01310af2 | 3170 | int i; |
0c1a9eda | 3171 | int64_t ti; |
63b15e55 | 3172 | |
2c4ae653 | 3173 | av_register_all(); |
85f07f22 FB |
3174 | |
3175 | if (argc <= 1) | |
3176 | show_help(); | |
a38469e1 FB |
3177 | |
3178 | /* parse options */ | |
01310af2 | 3179 | parse_options(argc, argv, options); |
85f07f22 | 3180 | |
01310af2 FB |
3181 | /* file converter / grab */ |
3182 | if (nb_output_files <= 0) { | |
3183 | fprintf(stderr, "Must supply at least one output file\n"); | |
3184 | exit(1); | |
3185 | } | |
3186 | ||
3187 | if (nb_input_files == 0) { | |
3188 | prepare_grab(); | |
a38469e1 FB |
3189 | } |
3190 | ||
3191 | ti = getutime(); | |
3192 | av_encode(output_files, nb_output_files, input_files, nb_input_files, | |
3193 | stream_maps, nb_stream_maps); | |
3194 | ti = getutime() - ti; | |
3195 | if (do_benchmark) { | |
3196 | printf("bench: utime=%0.3fs\n", ti / 1000000.0); | |
85f07f22 FB |
3197 | } |
3198 | ||
3199 | /* close files */ | |
3200 | for(i=0;i<nb_output_files;i++) { | |
4fca59f2 ZK |
3201 | /* maybe av_close_output_file ??? */ |
3202 | AVFormatContext *s = output_files[i]; | |
3203 | int j; | |
3204 | if (!(s->oformat->flags & AVFMT_NOFILE)) | |
3205 | url_fclose(&s->pb); | |
3206 | for(j=0;j<s->nb_streams;j++) | |
3207 | av_free(s->streams[j]); | |
3208 | av_free(s); | |
85f07f22 | 3209 | } |
79fdaa4c FB |
3210 | for(i=0;i<nb_input_files;i++) |
3211 | av_close_input_file(input_files[i]); | |
85f07f22 | 3212 | |
855ea723 | 3213 | av_free_static(); |
db40a39a | 3214 | |
84f608f4 VM |
3215 | if(intra_matrix) |
3216 | av_free(intra_matrix); | |
3217 | if(inter_matrix) | |
3218 | av_free(inter_matrix); | |
db40a39a | 3219 | |
e45a2872 | 3220 | #ifdef POWERPC_PERFORMANCE_REPORT |
35e5fb06 RD |
3221 | extern void powerpc_display_perf_report(void); |
3222 | powerpc_display_perf_report(); | |
e45a2872 | 3223 | #endif /* POWERPC_PERFORMANCE_REPORT */ |
db40a39a | 3224 | |
9680a722 RP |
3225 | #ifndef CONFIG_WIN32 |
3226 | if (received_sigterm) { | |
3227 | fprintf(stderr, | |
3228 | "Received signal %d: terminating.\n", | |
3229 | (int) received_sigterm); | |
3230 | exit (255); | |
3231 | } | |
3232 | #endif | |
3233 | exit(0); /* not all OS-es handle main() return value */ | |
85f07f22 FB |
3234 | return 0; |
3235 | } |