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