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