Reportedly FFmpeg fails to compile on Cygwin with vhook enabled, but FFserver
[libav.git] / ffmpeg.c
CommitLineData
85f07f22 1/*
115329f1 2 * FFmpeg main
01310af2 3 * Copyright (c) 2000-2003 Fabrice Bellard
85f07f22 4 *
bf5af568
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
85f07f22 9 *
bf5af568 10 * This library is distributed in the hope that it will be useful,
85f07f22 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bf5af568
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
85f07f22 14 *
bf5af568
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
85f07f22 18 */
daf8e955 19#define HAVE_AV_CONFIG_H
22f7a060 20#include <limits.h>
daf8e955 21#include "avformat.h"
10d104e4 22#include "framehook.h"
1ac31760 23#include "dsputil.h"
6fd46414 24#include "opt.h"
daf8e955 25
bdc4796f 26#ifndef CONFIG_WIN32
85f07f22
FB
27#include <unistd.h>
28#include <fcntl.h>
29#include <sys/ioctl.h>
85f07f22 30#include <sys/time.h>
85f07f22 31#include <termios.h>
5727b222 32#include <sys/resource.h>
9680a722 33#include <signal.h>
bdc4796f 34#endif
f3ec2d46
SG
35#ifdef CONFIG_OS2
36#include <sys/types.h>
37#include <sys/select.h>
38#include <stdlib.h>
39#endif
64c020a8 40#undef time //needed because HAVE_AV_CONFIG_H is defined on top
bf5af568 41#include <time.h>
85f07f22 42
01310af2
FB
43#include "cmdutils.h"
44
2b18dcd0
MN
45#undef NDEBUG
46#include <assert.h>
47
8aa1e3da
MN
48#if !defined(INFINITY) && defined(HUGE_VAL)
49#define INFINITY HUGE_VAL
50#endif
85f07f22 51
85f07f22
FB
52/* select an input stream for an output stream */
53typedef struct AVStreamMap {
54 int file_index;
55 int stream_index;
b4a3389e
WG
56 int sync_file_index;
57 int sync_stream_index;
85f07f22
FB
58} AVStreamMap;
59
0a38bafd
PB
60/** select an input file for an output file */
61typedef struct AVMetaDataMap {
62 int out_file;
63 int in_file;
64} AVMetaDataMap;
65
85f07f22
FB
66extern const OptionDef options[];
67
02d504a7
FB
68static void show_help(void);
69static void show_license(void);
637b5326 70static int opt_default(const char *opt, const char *arg);
85f07f22
FB
71
72#define MAX_FILES 20
73
74static AVFormatContext *input_files[MAX_FILES];
a6a92a9a 75static int64_t input_files_ts_offset[MAX_FILES];
85f07f22
FB
76static int nb_input_files = 0;
77
78static AVFormatContext *output_files[MAX_FILES];
79static int nb_output_files = 0;
80
81static AVStreamMap stream_maps[MAX_FILES];
82static int nb_stream_maps;
83
0a38bafd
PB
84static AVMetaDataMap meta_data_maps[MAX_FILES];
85static int nb_meta_data_maps;
86
79fdaa4c
FB
87static AVInputFormat *file_iformat;
88static AVOutputFormat *file_oformat;
817b23ff 89static AVImageFormat *image_format;
55cf1959
MN
90static int frame_width = 0;
91static int frame_height = 0;
880e8ba7 92static float frame_aspect_ratio = 0;
644a9262 93static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
1ff93ffc
TK
94static int frame_padtop = 0;
95static int frame_padbottom = 0;
96static int frame_padleft = 0;
97static int frame_padright = 0;
98static int padcolor[3] = {16,128,128}; /* default to black */
ab6d194a
MN
99static int frame_topBand = 0;
100static int frame_bottomBand = 0;
101static int frame_leftBand = 0;
102static int frame_rightBand = 0;
cf7fc795 103static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
14bea432
MN
104static int frame_rate = 25;
105static int frame_rate_base = 1;
3aa102be
MN
106static int video_bit_rate = 200*1000;
107static int video_bit_rate_tolerance = 4000*1000;
158c7f05 108static float video_qscale = 0;
3aa102be
MN
109static int video_qmin = 2;
110static int video_qmax = 31;
4ea4b274
MN
111static int video_lmin = 2*FF_QP2LAMBDA;
112static int video_lmax = 31*FF_QP2LAMBDA;
6e0d8c06
MN
113static int video_mb_lmin = 2*FF_QP2LAMBDA;
114static int video_mb_lmax = 31*FF_QP2LAMBDA;
9cdd6a24
MN
115static int video_qdiff = 3;
116static float video_qblur = 0.5;
6a5d4395 117static float video_qsquish = 0.0;
9cdd6a24 118static float video_qcomp = 0.5;
84f608f4
VM
119static uint16_t *intra_matrix = NULL;
120static uint16_t *inter_matrix = NULL;
ac2830ec 121#if 0 //experimental, (can be removed)
3aa102be
MN
122static float video_rc_qsquish=1.0;
123static float video_rc_qmod_amp=0;
124static int video_rc_qmod_freq=0;
ac2830ec 125#endif
3aa102be
MN
126static char *video_rc_override_string=NULL;
127static char *video_rc_eq="tex^qComp";
128static int video_rc_buffer_size=0;
129static float video_rc_buffer_aggressivity=1.0;
130static int video_rc_max_rate=0;
131static int video_rc_min_rate=0;
132static float video_rc_initial_cplx=0;
133static float video_b_qfactor = 1.25;
134static float video_b_qoffset = 1.25;
b3a391e8 135static float video_i_qfactor = -0.8;
3aa102be 136static float video_i_qoffset = 0.0;
303e50e6
MN
137static int video_intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
138static int video_inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
3bea5386 139static int me_method = ME_EPZS;
85f07f22 140static int video_disable = 0;
f3356e9c 141static int video_discard = 0;
85f07f22 142static int video_codec_id = CODEC_ID_NONE;
b2a2197e 143static int video_codec_tag = 0;
85f07f22 144static int same_quality = 0;
bc6caae2 145static int b_frames = 0;
68fc9c27 146static int b_strategy = 0;
9c3d33d6 147static int pre_me = 0;
cfcf0ffd 148static int do_deinterlace = 0;
4d2858de 149static int workaround_bugs = FF_BUG_AUTODETECT;
1dbb6d90 150static int packet_size = 0;
7ebfc0ea 151static int error_rate = 0;
f560dd82 152static int strict = 0;
bb198e19 153static int top_field_first = -1;
303e50e6 154static int sc_threshold = 0;
f4f3223f 155static int me_threshold = 0;
f20f8a8b 156static int mb_threshold = 0;
1a11cbcc 157static int intra_dc_precision = 8;
09c3e44e 158static int me_penalty_compensation= 256;
bbf18b21
MN
159static int frame_skip_threshold= 0;
160static int frame_skip_factor= 0;
0fd6aea1 161static int frame_skip_exp= 0;
b1b77fe9 162extern int loop_input; /* currently a hack */
8108551a 163static int loop_output = AVFMT_NOOUTPUTLOOP;
30bc6613 164static int genpts = 0;
0888fd22 165static int qp_hist = 0;
85f07f22
FB
166
167static int gop_size = 12;
168static int intra_only = 0;
169static int audio_sample_rate = 44100;
170static int audio_bit_rate = 64000;
c57c770d
JR
171#define QSCALE_NONE -99999
172static float audio_qscale = QSCALE_NONE;
85f07f22
FB
173static int audio_disable = 0;
174static int audio_channels = 1;
175static int audio_codec_id = CODEC_ID_NONE;
b2a2197e 176static int audio_codec_tag = 0;
cf7fc795
FB
177static char *audio_language = NULL;
178
179static int subtitle_codec_id = CODEC_ID_NONE;
180static char *subtitle_language = NULL;
85f07f22 181
2db3c638
MN
182static int mux_rate= 0;
183static int mux_packet_size= 0;
17c88cb0
MN
184static float mux_preload= 0.5;
185static float mux_max_delay= 0.7;
2db3c638 186
0c1a9eda 187static int64_t recording_time = 0;
8831db5c 188static int64_t start_time = 0;
4568325a 189static int64_t rec_timestamp = 0;
a6a92a9a 190static int64_t input_ts_offset = 0;
85f07f22
FB
191static int file_overwrite = 0;
192static char *str_title = NULL;
193static char *str_author = NULL;
194static char *str_copyright = NULL;
195static char *str_comment = NULL;
5727b222 196static int do_benchmark = 0;
a0663ba4 197static int do_hex_dump = 0;
254abc2e 198static int do_pkt_dump = 0;
43f1708f 199static int do_psnr = 0;
ce7c56c2 200static int do_vstats = 0;
5abdb4b1
FB
201static int do_pass = 0;
202static char *pass_logfilename = NULL;
1629626f
FB
203static int audio_stream_copy = 0;
204static int video_stream_copy = 0;
cf7fc795 205static int subtitle_stream_copy = 0;
986ebcdb
MN
206static int video_sync_method= 1;
207static int audio_sync_method= 0;
72bd8100 208static int copy_ts= 0;
ca80810b 209static int opt_shortest = 0; //
90ad92b3 210static int video_global_header = 0;
5abdb4b1 211
bdfcbbed
MK
212static int rate_emu = 0;
213
6beefa40
MN
214#ifdef CONFIG_BKTR
215static char *video_grab_format = "bktr";
216#else
8aa3ee32 217static char *video_grab_format = "video4linux";
6beefa40 218#endif
79a7c268 219static char *video_device = NULL;
2639c651 220static char *grab_device = NULL;
a5df11ab 221static int video_channel = 0;
e3ee3283 222static char *video_standard = "ntsc";
79a7c268 223
8aa3ee32 224static char *audio_grab_format = "audio_device";
79a7c268 225static char *audio_device = NULL;
a9aa3467 226static int audio_volume = 256;
8aa3ee32 227
d9a916e2 228static int using_stdin = 0;
bee0d9e5 229static int using_vhook = 0;
f068206e 230static int verbose = 1;
9c3d33d6 231static int thread_count= 1;
b51469a0 232static int q_pressed = 0;
2f0472ff 233static int me_range = 0;
1008ceb3
MN
234static int64_t video_size = 0;
235static int64_t audio_size = 0;
236static int64_t extra_size = 0;
a6a92a9a
WG
237static int nb_frames_dup = 0;
238static int nb_frames_drop = 0;
6e454c38 239static int input_sync;
b6e16b86 240static int limit_filesize = 0; //
d9a916e2 241
5b6d5596
MN
242static int pgmyuv_compatibility_hack=0;
243
8bbf6db9
MN
244const char **opt_names=NULL;
245int opt_name_count=0;
246AVCodecContext *avctx_opts;
247
5b6d5596 248
5abdb4b1 249#define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
85f07f22 250
b4a3389e
WG
251struct AVInputStream;
252
85f07f22
FB
253typedef struct AVOutputStream {
254 int file_index; /* file index */
255 int index; /* stream index in the output file */
256 int source_index; /* AVInputStream index */
257 AVStream *st; /* stream in the output file */
ec5517d5
FB
258 int encoding_needed; /* true if encoding needed for this stream */
259 int frame_number;
260 /* input pts and corresponding output pts
261 for A/V sync */
b4a3389e
WG
262 //double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
263 struct AVInputStream *sync_ist; /* input stream to sync against */
e928649b 264 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
85f07f22 265 /* video only */
34b10a57 266 int video_resample; /* video_resample and video_crop are mutually exclusive */
a4d36c11 267 AVFrame pict_tmp; /* temporary image for resampling */
85f07f22 268 ImgReSampleContext *img_resample_ctx; /* for image resampling */
34b10a57
D
269
270 int video_crop; /* video_resample and video_crop are mutually exclusive */
271 int topBand; /* cropping area sizes */
272 int leftBand;
115329f1 273
1ff93ffc
TK
274 int video_pad; /* video_resample and video_pad are mutually exclusive */
275 int padtop; /* padding area sizes */
276 int padbottom;
277 int padleft;
278 int padright;
115329f1 279
85f07f22
FB
280 /* audio only */
281 int audio_resample;
282 ReSampleContext *resample; /* for audio resampling */
283 FifoBuffer fifo; /* for compression: one audio fifo per codec */
5abdb4b1 284 FILE *logfile;
85f07f22
FB
285} AVOutputStream;
286
287typedef struct AVInputStream {
288 int file_index;
289 int index;
290 AVStream *st;
291 int discard; /* true if stream data should be discarded */
292 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
0c1a9eda 293 int64_t sample_index; /* current sample */
bdfcbbed
MK
294
295 int64_t start; /* time when read started */
296 unsigned long frame; /* current frame */
254abc2e
FB
297 int64_t next_pts; /* synthetic pts for cases where pkt.pts
298 is not defined */
e7d0374f 299 int64_t pts; /* current pts */
ff4905a5 300 int is_start; /* is 1 at the start and after a discontinuity */
85f07f22
FB
301} AVInputStream;
302
303typedef struct AVInputFile {
304 int eof_reached; /* true if eof reached */
305 int ist_index; /* index of first stream in ist_table */
306 int buffer_size; /* current total buffer size */
307 int buffer_size_max; /* buffer size at which we consider we can stop
308 buffering */
79fdaa4c 309 int nb_streams; /* nb streams we are aware of */
85f07f22
FB
310} AVInputFile;
311
bdc4796f
FB
312#ifndef CONFIG_WIN32
313
85f07f22
FB
314/* init terminal so that we can grab keys */
315static struct termios oldtty;
316
317static void term_exit(void)
318{
319 tcsetattr (0, TCSANOW, &oldtty);
320}
321
9680a722
RP
322static volatile sig_atomic_t received_sigterm = 0;
323
324static void
325sigterm_handler(int sig)
326{
327 received_sigterm = sig;
328 term_exit();
329}
330
85f07f22
FB
331static void term_init(void)
332{
333 struct termios tty;
334
335 tcgetattr (0, &tty);
336 oldtty = tty;
337
338 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
339 |INLCR|IGNCR|ICRNL|IXON);
340 tty.c_oflag |= OPOST;
341 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
342 tty.c_cflag &= ~(CSIZE|PARENB);
343 tty.c_cflag |= CS8;
344 tty.c_cc[VMIN] = 1;
345 tty.c_cc[VTIME] = 0;
115329f1 346
85f07f22
FB
347 tcsetattr (0, TCSANOW, &tty);
348
9680a722
RP
349 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
350 signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
351 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
352 /*
353 register a function to be called at normal program termination
354 */
85f07f22 355 atexit(term_exit);
9ddd71fc
FR
356#ifdef CONFIG_BEOS_NETSERVER
357 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
358#endif
85f07f22
FB
359}
360
361/* read a key without blocking */
362static int read_key(void)
363{
9ddd71fc 364 int n = 1;
85f07f22 365 unsigned char ch;
9ddd71fc
FR
366#ifndef CONFIG_BEOS_NETSERVER
367 struct timeval tv;
85f07f22
FB
368 fd_set rfds;
369
370 FD_ZERO(&rfds);
371 FD_SET(0, &rfds);
372 tv.tv_sec = 0;
373 tv.tv_usec = 0;
374 n = select(1, &rfds, NULL, NULL, &tv);
9ddd71fc 375#endif
85f07f22 376 if (n > 0) {
cb09b2ed
PG
377 n = read(0, &ch, 1);
378 if (n == 1)
85f07f22 379 return ch;
cb09b2ed
PG
380
381 return n;
85f07f22
FB
382 }
383 return -1;
384}
385
b51469a0
LS
386static int decode_interrupt_cb(void)
387{
388 return q_pressed || (q_pressed = read_key() == 'q');
389}
390
a38469e1 391#else
85f07f22 392
bee0d9e5
CY
393static volatile int received_sigterm = 0;
394
a38469e1
FB
395/* no interactive support */
396static void term_exit(void)
85f07f22 397{
a38469e1 398}
85f07f22 399
a38469e1
FB
400static void term_init(void)
401{
402}
85f07f22 403
a38469e1
FB
404static int read_key(void)
405{
cb09b2ed 406 return 0;
85f07f22
FB
407}
408
a38469e1 409#endif
bdc4796f 410
b29f97d1 411static int read_ffserver_streams(AVFormatContext *s, const char *filename)
85f07f22 412{
79fdaa4c 413 int i, err;
85f07f22
FB
414 AVFormatContext *ic;
415
79fdaa4c
FB
416 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
417 if (err < 0)
418 return err;
85f07f22
FB
419 /* copy stream format */
420 s->nb_streams = ic->nb_streams;
421 for(i=0;i<ic->nb_streams;i++) {
422 AVStream *st;
1e491e29 423
f37f8d4c 424 // FIXME: a more elegant solution is needed
e1031171 425 st = av_mallocz(sizeof(AVStream));
85f07f22 426 memcpy(st, ic->streams[i], sizeof(AVStream));
f37f8d4c
AB
427 st->codec = avcodec_alloc_context();
428 memcpy(st->codec, ic->streams[i]->codec, sizeof(AVCodecContext));
85f07f22
FB
429 s->streams[i] = st;
430 }
431
432 av_close_input_file(ic);
433 return 0;
434}
435
b4a3389e
WG
436static double
437get_sync_ipts(const AVOutputStream *ost)
438{
439 const AVInputStream *ist = ost->sync_ist;
440 return (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/AV_TIME_BASE;
441}
442
817b23ff 443#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
85f07f22 444
115329f1
DB
445static void do_audio_out(AVFormatContext *s,
446 AVOutputStream *ost,
85f07f22
FB
447 AVInputStream *ist,
448 unsigned char *buf, int size)
449{
0c1a9eda 450 uint8_t *buftmp;
d66c7abc
SC
451 static uint8_t *audio_buf = NULL;
452 static uint8_t *audio_out = NULL;
558eae03 453 const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
d66c7abc 454
85f07f22 455 int size_out, frame_bytes, ret;
01f4895c 456 AVCodecContext *enc= ost->st->codec;
85f07f22 457
d66c7abc
SC
458 /* SC: dynamic allocation of buffers */
459 if (!audio_buf)
460 audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
461 if (!audio_out)
558eae03 462 audio_out = av_malloc(audio_out_size);
d66c7abc
SC
463 if (!audio_buf || !audio_out)
464 return; /* Should signal an error ! */
465
986ebcdb 466 if(audio_sync_method){
115329f1 467 double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
01f4895c
MN
468 - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec->channels * 2);
469 double idelta= delta*ist->st->codec->sample_rate / enc->sample_rate;
470 int byte_delta= ((int)idelta)*2*ist->st->codec->channels;
ff4905a5 471
986ebcdb
MN
472 //FIXME resample delay
473 if(fabs(delta) > 50){
ff4905a5
MN
474 if(ist->is_start){
475 if(byte_delta < 0){
f41dd5aa 476 byte_delta= FFMAX(byte_delta, -size);
ff4905a5
MN
477 size += byte_delta;
478 buf -= byte_delta;
479 if(verbose > 2)
480 fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
481 if(!size)
482 return;
483 ist->is_start=0;
484 }else{
485 static uint8_t *input_tmp= NULL;
486 input_tmp= av_realloc(input_tmp, byte_delta + size);
487
488 if(byte_delta + size <= MAX_AUDIO_PACKET_SIZE)
489 ist->is_start=0;
490 else
491 byte_delta= MAX_AUDIO_PACKET_SIZE - size;
492
493 memset(input_tmp, 0, byte_delta);
494 memcpy(input_tmp + byte_delta, buf, size);
495 buf= input_tmp;
496 size += byte_delta;
497 if(verbose > 2)
498 fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
499 }
500 }else if(audio_sync_method>1){
501 int comp= clip(delta, -audio_sync_method, audio_sync_method);
502 assert(ost->audio_resample);
503 if(verbose > 2)
504 fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
01f4895c 505// fprintf(stderr, "drift:%f len:%d opts:%lld ipts:%lld fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec->channels * 2));
ff4905a5
MN
506 av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
507 }
115329f1 508 }
986ebcdb 509 }else
b4a3389e 510 ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
01f4895c 511 - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec->channels * 2); //FIXME wrong
85f07f22
FB
512
513 if (ost->audio_resample) {
514 buftmp = audio_buf;
115329f1 515 size_out = audio_resample(ost->resample,
85f07f22 516 (short *)buftmp, (short *)buf,
01f4895c 517 size / (ist->st->codec->channels * 2));
85f07f22
FB
518 size_out = size_out * enc->channels * 2;
519 } else {
520 buftmp = buf;
521 size_out = size;
522 }
523
524 /* now encode as many frames as possible */
a0663ba4 525 if (enc->frame_size > 1) {
85f07f22 526 /* output resampled raw samples */
115329f1 527 fifo_write(&ost->fifo, buftmp, size_out,
85f07f22
FB
528 &ost->fifo.wptr);
529
530 frame_bytes = enc->frame_size * 2 * enc->channels;
115329f1
DB
531
532 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
85f07f22 533 &ost->fifo.rptr) == 0) {
e928649b
MN
534 AVPacket pkt;
535 av_init_packet(&pkt);
536
115329f1 537 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
a0663ba4 538 (short *)audio_buf);
1008ceb3 539 audio_size += ret;
e928649b
MN
540 pkt.stream_index= ost->index;
541 pkt.data= audio_out;
542 pkt.size= ret;
e7902f20 543 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 544 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b 545 pkt.flags |= PKT_FLAG_KEY;
3c895fc0 546 av_interleaved_write_frame(s, &pkt);
115329f1 547
986ebcdb 548 ost->sync_opts += enc->frame_size;
85f07f22
FB
549 }
550 } else {
e928649b
MN
551 AVPacket pkt;
552 av_init_packet(&pkt);
986ebcdb 553
c43042a7 554 ost->sync_opts += size_out / (2 * enc->channels);
986ebcdb 555
a0663ba4
FB
556 /* output a pcm frame */
557 /* XXX: change encoding codec API to avoid this ? */
558 switch(enc->codec->id) {
b461b3bc
RD
559 case CODEC_ID_PCM_S32LE:
560 case CODEC_ID_PCM_S32BE:
561 case CODEC_ID_PCM_U32LE:
562 case CODEC_ID_PCM_U32BE:
563 size_out = size_out << 1;
564 break;
565 case CODEC_ID_PCM_S24LE:
566 case CODEC_ID_PCM_S24BE:
567 case CODEC_ID_PCM_U24LE:
568 case CODEC_ID_PCM_U24BE:
569 case CODEC_ID_PCM_S24DAUD:
570 size_out = size_out / 2 * 3;
571 break;
a0663ba4
FB
572 case CODEC_ID_PCM_S16LE:
573 case CODEC_ID_PCM_S16BE:
574 case CODEC_ID_PCM_U16LE:
575 case CODEC_ID_PCM_U16BE:
576 break;
577 default:
578 size_out = size_out >> 1;
579 break;
580 }
115329f1 581 ret = avcodec_encode_audio(enc, audio_out, size_out,
ff29712a 582 (short *)buftmp);
1008ceb3 583 audio_size += ret;
e928649b
MN
584 pkt.stream_index= ost->index;
585 pkt.data= audio_out;
586 pkt.size= ret;
e7902f20 587 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 588 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b 589 pkt.flags |= PKT_FLAG_KEY;
3c895fc0 590 av_interleaved_write_frame(s, &pkt);
85f07f22
FB
591 }
592}
593
10d104e4
PG
594static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
595{
596 AVCodecContext *dec;
597 AVPicture *picture2;
598 AVPicture picture_tmp;
0c1a9eda 599 uint8_t *buf = 0;
10d104e4 600
01f4895c 601 dec = ist->st->codec;
10d104e4
PG
602
603 /* deinterlace : must be done before any resize */
bee0d9e5 604 if (do_deinterlace || using_vhook) {
10d104e4
PG
605 int size;
606
607 /* create temporary picture */
608 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
609 buf = av_malloc(size);
610 if (!buf)
611 return;
115329f1 612
10d104e4
PG
613 picture2 = &picture_tmp;
614 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
615
dafc3856 616 if (do_deinterlace){
115329f1 617 if(avpicture_deinterlace(picture2, picture,
dafc3856
MN
618 dec->pix_fmt, dec->width, dec->height) < 0) {
619 /* if error, do not deinterlace */
620 av_free(buf);
621 buf = NULL;
622 picture2 = picture;
623 }
624 } else {
115329f1 625 if (img_convert(picture2, dec->pix_fmt, picture,
bee0d9e5
CY
626 dec->pix_fmt, dec->width, dec->height) < 0) {
627 /* if error, do not copy */
628 av_free(buf);
629 buf = NULL;
630 picture2 = picture;
631 }
632 }
10d104e4
PG
633 } else {
634 picture2 = picture;
635 }
636
637 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
638
639 if (picture != picture2)
640 *picture = *picture2;
641 *bufp = buf;
642}
643
ec5517d5
FB
644/* we begin to correct av delay at this threshold */
645#define AV_DELAY_MAX 0.100
85f07f22 646
1ff93ffc
TK
647
648/* Expects img to be yuv420 */
649static void fill_pad_region(AVPicture* img, int height, int width,
650 int padtop, int padbottom, int padleft, int padright, int *color) {
115329f1 651
1ff93ffc
TK
652 int i, y, shift;
653 uint8_t *optr;
115329f1 654
1ff93ffc
TK
655 for (i = 0; i < 3; i++) {
656 shift = (i == 0) ? 0 : 1;
115329f1 657
1ff93ffc 658 if (padtop || padleft) {
115329f1 659 memset(img->data[i], color[i], (((img->linesize[i] * padtop) +
1ff93ffc
TK
660 padleft) >> shift));
661 }
662
663 if (padleft || padright) {
664 optr = img->data[i] + (img->linesize[i] * (padtop >> shift)) +
665 (img->linesize[i] - (padright >> shift));
666
da723fd2 667 for (y = 0; y < ((height - (padtop + padbottom) - 1) >> shift); y++) {
1ff93ffc
TK
668 memset(optr, color[i], (padleft + padright) >> shift);
669 optr += img->linesize[i];
670 }
671 }
115329f1 672
da723fd2
C
673 if (padbottom || padright) {
674 optr = img->data[i] + (((img->linesize[i] * (height - padbottom)) - padright) >> shift);
675 memset(optr, color[i], (((img->linesize[i] * padbottom) + padright) >> shift));
1ff93ffc
TK
676 }
677 }
678}
679
115329f1
DB
680static void do_subtitle_out(AVFormatContext *s,
681 AVOutputStream *ost,
cf7fc795
FB
682 AVInputStream *ist,
683 AVSubtitle *sub,
684 int64_t pts)
685{
686 static uint8_t *subtitle_out = NULL;
687 int subtitle_out_max_size = 65536;
688 int subtitle_out_size, nb, i;
689 AVCodecContext *enc;
690 AVPacket pkt;
691
692 if (pts == AV_NOPTS_VALUE) {
693 fprintf(stderr, "Subtitle packets must have a pts\n");
694 return;
695 }
696
01f4895c 697 enc = ost->st->codec;
cf7fc795
FB
698
699 if (!subtitle_out) {
700 subtitle_out = av_malloc(subtitle_out_max_size);
701 }
702
703 /* Note: DVB subtitle need one packet to draw them and one other
704 packet to clear them */
705 /* XXX: signal it in the codec context ? */
706 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
707 nb = 2;
708 else
709 nb = 1;
710
711 for(i = 0; i < nb; i++) {
115329f1 712 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
cf7fc795 713 subtitle_out_max_size, sub);
115329f1 714
cf7fc795
FB
715 av_init_packet(&pkt);
716 pkt.stream_index = ost->index;
717 pkt.data = subtitle_out;
718 pkt.size = subtitle_out_size;
719 pkt.pts = av_rescale_q(av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q) + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ost->st->time_base);
720 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
721 /* XXX: the pts correction is handled here. Maybe handling
722 it in the codec would be better */
723 if (i == 0)
724 pkt.pts += 90 * sub->start_display_time;
725 else
726 pkt.pts += 90 * sub->end_display_time;
727 }
728 av_interleaved_write_frame(s, &pkt);
729 }
730}
731
8a6cb114 732static int bit_buffer_size= 1024*256;
27537106 733static uint8_t *bit_buffer= NULL;
1ff93ffc 734
115329f1
DB
735static void do_video_out(AVFormatContext *s,
736 AVOutputStream *ost,
85f07f22 737 AVInputStream *ist,
7a0f9d7e 738 AVFrame *in_picture,
986ebcdb 739 int *frame_size)
85f07f22 740{
ec5517d5 741 int nb_frames, i, ret;
a4d36c11
MN
742 AVFrame *final_picture, *formatted_picture;
743 AVFrame picture_format_temp, picture_crop_temp;
0c1a9eda 744 uint8_t *buf = NULL, *buf1 = NULL;
cfcf0ffd 745 AVCodecContext *enc, *dec;
a686caf0 746 enum PixelFormat target_pixfmt;
115329f1 747
a4d36c11
MN
748 avcodec_get_frame_defaults(&picture_format_temp);
749 avcodec_get_frame_defaults(&picture_crop_temp);
750
01f4895c
MN
751 enc = ost->st->codec;
752 dec = ist->st->codec;
85f07f22 753
ec5517d5
FB
754 /* by default, we output a single frame */
755 nb_frames = 1;
756
204c0f48
PG
757 *frame_size = 0;
758
986ebcdb 759 if(video_sync_method){
10d104e4 760 double vdelta;
b4a3389e 761 vdelta = get_sync_ipts(ost) / av_q2d(enc->time_base) - ost->sync_opts;
e928649b
MN
762 //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
763 if (vdelta < -1.1)
764 nb_frames = 0;
765 else if (vdelta > 1.1)
70122f29 766 nb_frames = lrintf(vdelta);
72bd8100 767//fprintf(stderr, "vdelta:%f, ost->sync_opts:%lld, ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, ost->sync_ipts, nb_frames);
50c3dd32
MN
768 if (nb_frames == 0){
769 ++nb_frames_drop;
770 if (verbose>2)
771 fprintf(stderr, "*** drop!\n");
8300609b
MN
772 }else if (nb_frames > 1) {
773 nb_frames_dup += nb_frames;
50c3dd32 774 if (verbose>2)
8300609b 775 fprintf(stderr, "*** %d dup!\n", nb_frames-1);
50c3dd32
MN
776 }
777 }else
b4a3389e 778 ost->sync_opts= lrintf(get_sync_ipts(ost) / av_q2d(enc->time_base));
445f1b83 779
7ca60994 780 nb_frames= FFMIN(nb_frames, max_frames[CODEC_TYPE_VIDEO] - ost->frame_number);
115329f1 781 if (nb_frames <= 0)
85f07f22 782 return;
ce7c56c2 783
cfcf0ffd 784 /* convert pixel format if needed */
1ff93ffc
TK
785 target_pixfmt = ost->video_resample || ost->video_pad
786 ? PIX_FMT_YUV420P : enc->pix_fmt;
a686caf0 787 if (dec->pix_fmt != target_pixfmt) {
cfcf0ffd
FB
788 int size;
789
790 /* create temporary picture */
39518b49 791 size = avpicture_get_size(target_pixfmt, dec->width, dec->height);
0f1578af 792 buf = av_malloc(size);
cfcf0ffd
FB
793 if (!buf)
794 return;
34b10a57 795 formatted_picture = &picture_format_temp;
a4d36c11 796 avpicture_fill((AVPicture*)formatted_picture, buf, target_pixfmt, dec->width, dec->height);
115329f1
DB
797
798 if (img_convert((AVPicture*)formatted_picture, target_pixfmt,
799 (AVPicture *)in_picture, dec->pix_fmt,
cfcf0ffd 800 dec->width, dec->height) < 0) {
d8019eb5
AD
801
802 if (verbose >= 0)
803 fprintf(stderr, "pixel format conversion not handled\n");
804
cfcf0ffd
FB
805 goto the_end;
806 }
807 } else {
a4d36c11 808 formatted_picture = in_picture;
cfcf0ffd
FB
809 }
810
1ff93ffc 811 /* XXX: resampling could be done before raw format conversion in
cfcf0ffd
FB
812 some cases to go faster */
813 /* XXX: only works for YUV420P */
85f07f22 814 if (ost->video_resample) {
34b10a57 815 final_picture = &ost->pict_tmp;
a4d36c11 816 img_resample(ost->img_resample_ctx, (AVPicture*)final_picture, (AVPicture*)formatted_picture);
115329f1 817
1ff93ffc 818 if (ost->padtop || ost->padbottom || ost->padleft || ost->padright) {
a4d36c11 819 fill_pad_region((AVPicture*)final_picture, enc->height, enc->width,
1ff93ffc
TK
820 ost->padtop, ost->padbottom, ost->padleft, ost->padright,
821 padcolor);
822 }
115329f1 823
a686caf0
RS
824 if (enc->pix_fmt != PIX_FMT_YUV420P) {
825 int size;
115329f1 826
a686caf0
RS
827 av_free(buf);
828 /* create temporary picture */
829 size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
830 buf = av_malloc(size);
831 if (!buf)
832 return;
833 final_picture = &picture_format_temp;
a4d36c11 834 avpicture_fill((AVPicture*)final_picture, buf, enc->pix_fmt, enc->width, enc->height);
115329f1
DB
835
836 if (img_convert((AVPicture*)final_picture, enc->pix_fmt,
837 (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
a686caf0 838 enc->width, enc->height) < 0) {
d8019eb5
AD
839
840 if (verbose >= 0)
841 fprintf(stderr, "pixel format conversion not handled\n");
842
a686caf0
RS
843 goto the_end;
844 }
845 }
34b10a57
D
846 } else if (ost->video_crop) {
847 picture_crop_temp.data[0] = formatted_picture->data[0] +
848 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
849
850 picture_crop_temp.data[1] = formatted_picture->data[1] +
851 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
852 (ost->leftBand >> 1);
853
854 picture_crop_temp.data[2] = formatted_picture->data[2] +
855 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
856 (ost->leftBand >> 1);
857
858 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
859 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
860 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
861 final_picture = &picture_crop_temp;
1ff93ffc
TK
862 } else if (ost->video_pad) {
863 final_picture = &ost->pict_tmp;
864
865 for (i = 0; i < 3; i++) {
866 uint8_t *optr, *iptr;
867 int shift = (i == 0) ? 0 : 1;
868 int y, yheight;
115329f1 869
1ff93ffc 870 /* set offset to start writing image into */
115329f1 871 optr = final_picture->data[i] + (((final_picture->linesize[i] *
1ff93ffc
TK
872 ost->padtop) + ost->padleft) >> shift);
873 iptr = formatted_picture->data[i];
874
875 yheight = (enc->height - ost->padtop - ost->padbottom) >> shift;
876 for (y = 0; y < yheight; y++) {
877 /* copy unpadded image row into padded image row */
878 memcpy(optr, iptr, formatted_picture->linesize[i]);
879 optr += final_picture->linesize[i];
880 iptr += formatted_picture->linesize[i];
881 }
882 }
883
a4d36c11 884 fill_pad_region((AVPicture*)final_picture, enc->height, enc->width,
1ff93ffc
TK
885 ost->padtop, ost->padbottom, ost->padleft, ost->padright,
886 padcolor);
115329f1 887
1ff93ffc
TK
888 if (enc->pix_fmt != PIX_FMT_YUV420P) {
889 int size;
890
891 av_free(buf);
892 /* create temporary picture */
893 size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
894 buf = av_malloc(size);
895 if (!buf)
896 return;
897 final_picture = &picture_format_temp;
a4d36c11 898 avpicture_fill((AVPicture*)final_picture, buf, enc->pix_fmt, enc->width, enc->height);
1ff93ffc 899
115329f1
DB
900 if (img_convert((AVPicture*)final_picture, enc->pix_fmt,
901 (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
1ff93ffc 902 enc->width, enc->height) < 0) {
d8019eb5
AD
903
904 if (verbose >= 0)
905 fprintf(stderr, "pixel format conversion not handled\n");
906
1ff93ffc
TK
907 goto the_end;
908 }
909 }
85f07f22 910 } else {
34b10a57 911 final_picture = formatted_picture;
85f07f22 912 }
85f07f22 913 /* duplicates frame if needed */
ec5517d5 914 for(i=0;i<nb_frames;i++) {
e928649b
MN
915 AVPacket pkt;
916 av_init_packet(&pkt);
917 pkt.stream_index= ost->index;
918
e8750b00
FR
919 if (s->oformat->flags & AVFMT_RAWPICTURE) {
920 /* raw pictures are written as AVPicture structure to
921 avoid any copies. We support temorarily the older
922 method. */
2744ca9a 923 AVFrame* old_frame = enc->coded_frame;
e928649b
MN
924 enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack
925 pkt.data= (uint8_t *)final_picture;
926 pkt.size= sizeof(AVPicture);
e7902f20 927 if(dec->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 928 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b
MN
929 if(dec->coded_frame && dec->coded_frame->key_frame)
930 pkt.flags |= PKT_FLAG_KEY;
931
3c895fc0 932 av_interleaved_write_frame(s, &pkt);
2744ca9a 933 enc->coded_frame = old_frame;
e8750b00 934 } else {
492cd3a9 935 AVFrame big_picture;
a4d36c11
MN
936
937 big_picture= *final_picture;
7a0f9d7e
FB
938 /* better than nothing: use input picture interlaced
939 settings */
940 big_picture.interlaced_frame = in_picture->interlaced_frame;
637b5326 941 if(avctx_opts->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)){
bb198e19
MN
942 if(top_field_first == -1)
943 big_picture.top_field_first = in_picture->top_field_first;
944 else
2a8edc5d 945 big_picture.top_field_first = top_field_first;
bb198e19 946 }
7a0f9d7e 947
85f07f22
FB
948 /* handles sameq here. This is not correct because it may
949 not be a global option */
950 if (same_quality) {
1e491e29
MN
951 big_picture.quality = ist->st->quality;
952 }else
953 big_picture.quality = ost->st->quality;
f4f3223f
MN
954 if(!me_threshold)
955 big_picture.pict_type = 0;
50c3dd32 956// big_picture.pts = AV_NOPTS_VALUE;
c0df9d75
MN
957 big_picture.pts= ost->sync_opts;
958// big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->time_base.num, enc->time_base.den);
50c3dd32 959//av_log(NULL, AV_LOG_DEBUG, "%lld -> encoder\n", ost->sync_opts);
115329f1 960 ret = avcodec_encode_video(enc,
8a6cb114 961 bit_buffer, bit_buffer_size,
1e491e29 962 &big_picture);
44429457 963 //enc->frame_number = enc->real_pict_num;
4bbc6260 964 if(ret>0){
27537106 965 pkt.data= bit_buffer;
e928649b 966 pkt.size= ret;
e7902f20 967 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 968 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
115329f1 969/*av_log(NULL, AV_LOG_DEBUG, "encoder -> %lld/%lld\n",
c0df9d75
MN
970 pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1,
971 pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1);*/
50c3dd32 972
e928649b
MN
973 if(enc->coded_frame && enc->coded_frame->key_frame)
974 pkt.flags |= PKT_FLAG_KEY;
3c895fc0 975 av_interleaved_write_frame(s, &pkt);
e928649b
MN
976 *frame_size = ret;
977 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
978 // enc->frame_number-1, enc->real_pict_num, ret,
979 // enc->pict_type);
980 /* if two pass, output log */
981 if (ost->logfile && enc->stats_out) {
982 fprintf(ost->logfile, "%s", enc->stats_out);
983 }
5abdb4b1 984 }
85f07f22 985 }
50c3dd32 986 ost->sync_opts++;
ec5517d5 987 ost->frame_number++;
85f07f22 988 }
ec5517d5 989 the_end:
0f1578af
FB
990 av_free(buf);
991 av_free(buf1);
85f07f22
FB
992}
993
140cb663
MN
994static double psnr(double d){
995 if(d==0) return INFINITY;
b29f97d1 996 return -10.0*log(d)/log(10.0);
140cb663
MN
997}
998
115329f1 999static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
ec5517d5 1000 int frame_size)
ce7c56c2
J
1001{
1002 static FILE *fvstats=NULL;
ce7c56c2 1003 char filename[40];
bf5af568
FB
1004 time_t today2;
1005 struct tm *today;
ce7c56c2
J
1006 AVCodecContext *enc;
1007 int frame_number;
0c1a9eda 1008 int64_t ti;
ce7c56c2 1009 double ti1, bitrate, avg_bitrate;
115329f1 1010
ce7c56c2
J
1011 if (!fvstats) {
1012 today2 = time(NULL);
1013 today = localtime(&today2);
2fc8ea24 1014 snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour,
ce7c56c2
J
1015 today->tm_min,
1016 today->tm_sec);
1017 fvstats = fopen(filename,"w");
1018 if (!fvstats) {
1019 perror("fopen");
1020 exit(1);
1021 }
1022 }
115329f1 1023
ce7c56c2 1024 ti = MAXINT64;
01f4895c 1025 enc = ost->st->codec;
ce7c56c2 1026 if (enc->codec_type == CODEC_TYPE_VIDEO) {
ec5517d5 1027 frame_number = ost->frame_number;
158c7f05 1028 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
140cb663 1029 if (enc->flags&CODEC_FLAG_PSNR)
492cd3a9 1030 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
115329f1 1031
ce7c56c2 1032 fprintf(fvstats,"f_size= %6d ", frame_size);
ec5517d5 1033 /* compute pts value */
c0df9d75 1034 ti1 = ost->sync_opts * av_q2d(enc->time_base);
ce7c56c2
J
1035 if (ti1 < 0.01)
1036 ti1 = 0.01;
115329f1 1037
c0df9d75 1038 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1008ceb3 1039 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
ce7c56c2 1040 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1008ceb3 1041 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
115329f1 1042 fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
ce7c56c2 1043 }
ec5517d5
FB
1044}
1045
b29f97d1
ZK
1046static void print_report(AVFormatContext **output_files,
1047 AVOutputStream **ost_table, int nb_ostreams,
1048 int is_last_report)
ec5517d5
FB
1049{
1050 char buf[1024];
1051 AVOutputStream *ost;
1052 AVFormatContext *oc, *os;
0c1a9eda 1053 int64_t total_size;
ec5517d5
FB
1054 AVCodecContext *enc;
1055 int frame_number, vid, i;
1056 double bitrate, ti1, pts;
0c1a9eda 1057 static int64_t last_time = -1;
0888fd22 1058 static int qp_histogram[52];
115329f1 1059
ec5517d5 1060 if (!is_last_report) {
0c1a9eda 1061 int64_t cur_time;
ec5517d5
FB
1062 /* display the report every 0.5 seconds */
1063 cur_time = av_gettime();
1064 if (last_time == -1) {
1065 last_time = cur_time;
1066 return;
115329f1 1067 }
ec5517d5
FB
1068 if ((cur_time - last_time) < 500000)
1069 return;
1070 last_time = cur_time;
1071 }
1072
ce7c56c2 1073
ec5517d5
FB
1074 oc = output_files[0];
1075
1076 total_size = url_ftell(&oc->pb);
115329f1 1077
ec5517d5
FB
1078 buf[0] = '\0';
1079 ti1 = 1e10;
1080 vid = 0;
1081 for(i=0;i<nb_ostreams;i++) {
1082 ost = ost_table[i];
1083 os = output_files[ost->file_index];
01f4895c 1084 enc = ost->st->codec;
10d104e4 1085 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
0ecca7a4 1086 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ",
158c7f05 1087 enc->coded_frame->quality/(float)FF_QP2LAMBDA);
10d104e4 1088 }
ec5517d5
FB
1089 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1090 frame_number = ost->frame_number;
0888fd22
MN
1091 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d q=%3.1f ",
1092 frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : -1);
890972be 1093 if(is_last_report)
0ecca7a4 1094 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
0888fd22
MN
1095 if(qp_hist && enc->coded_frame){
1096 int j;
1097 int qp= lrintf(enc->coded_frame->quality/(float)FF_QP2LAMBDA);
1098 if(qp>=0 && qp<sizeof(qp_histogram)/sizeof(int))
1099 qp_histogram[qp]++;
1100 for(j=0; j<32; j++)
1101 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2)));
1102 }
890972be
MN
1103 if (enc->flags&CODEC_FLAG_PSNR){
1104 int j;
1105 double error, error_sum=0;
1106 double scale, scale_sum=0;
1107 char type[3]= {'Y','U','V'};
0ecca7a4 1108 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
890972be
MN
1109 for(j=0; j<3; j++){
1110 if(is_last_report){
1111 error= enc->error[j];
1112 scale= enc->width*enc->height*255.0*255.0*frame_number;
1113 }else{
1114 error= enc->coded_frame->error[j];
1115 scale= enc->width*enc->height*255.0*255.0;
1116 }
1117 if(j) scale/=4;
1118 error_sum += error;
1119 scale_sum += scale;
0ecca7a4 1120 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
890972be 1121 }
0ecca7a4 1122 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
890972be 1123 }
ec5517d5
FB
1124 vid = 1;
1125 }
1126 /* compute min output value */
9ee91c2f 1127 pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
5d9827bc 1128 if ((pts < ti1) && (pts > 0))
ec5517d5
FB
1129 ti1 = pts;
1130 }
1131 if (ti1 < 0.01)
1132 ti1 = 0.01;
115329f1 1133
f068206e
BE
1134 if (verbose || is_last_report) {
1135 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
115329f1
DB
1136
1137 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
ec5517d5
FB
1138 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1139 (double)total_size / 1024, ti1, bitrate);
a6a92a9a
WG
1140
1141 if (verbose > 1)
0ecca7a4 1142 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
a6a92a9a 1143 nb_frames_dup, nb_frames_drop);
115329f1 1144
d8019eb5
AD
1145 if (verbose >= 0)
1146 fprintf(stderr, "%s \r", buf);
1147
ec5517d5
FB
1148 fflush(stderr);
1149 }
115329f1 1150
1008ceb3
MN
1151 if (is_last_report && verbose >= 0){
1152 int64_t raw= audio_size + video_size + extra_size;
f068206e 1153 fprintf(stderr, "\n");
1008ceb3
MN
1154 fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1155 video_size/1024.0,
1156 audio_size/1024.0,
1157 extra_size/1024.0,
1158 100.0*(total_size - raw)/raw
1159 );
1160 }
ce7c56c2
J
1161}
1162
a700a6ae
FB
1163/* pkt = NULL means EOF (needed to flush decoder buffers) */
1164static int output_packet(AVInputStream *ist, int ist_index,
1165 AVOutputStream **ost_table, int nb_ostreams,
4b85a28f 1166 const AVPacket *pkt)
a700a6ae
FB
1167{
1168 AVFormatContext *os;
1169 AVOutputStream *ost;
1170 uint8_t *ptr;
1171 int len, ret, i;
1172 uint8_t *data_buf;
1173 int data_size, got_picture;
1174 AVFrame picture;
a700a6ae 1175 void *buffer_to_free;
78953e62
MN
1176 static int samples_size= 0;
1177 static short *samples= NULL;
cf7fc795
FB
1178 AVSubtitle subtitle, *subtitle_to_free;
1179 int got_subtitle;
115329f1 1180
2b18dcd0
MN
1181 if(!pkt){
1182 ist->pts= ist->next_pts; // needed for last packet if vsync=0
1183 } else if (pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
c0df9d75 1184 ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
a700a6ae 1185 } else {
a603bf8d 1186// assert(ist->pts == ist->next_pts);
a700a6ae 1187 }
115329f1 1188
a700a6ae
FB
1189 if (pkt == NULL) {
1190 /* EOF handling */
1191 ptr = NULL;
1192 len = 0;
1193 goto handle_eof;
1194 }
1195
1196 len = pkt->size;
1197 ptr = pkt->data;
1198 while (len > 0) {
1199 handle_eof:
1200 /* decode the packet if needed */
1201 data_buf = NULL; /* fail safe */
1202 data_size = 0;
cf7fc795 1203 subtitle_to_free = NULL;
a700a6ae 1204 if (ist->decoding_needed) {
01f4895c 1205 switch(ist->st->codec->codec_type) {
df84ac2e 1206 case CODEC_TYPE_AUDIO:{
115329f1 1207 if(pkt)
78953e62 1208 samples= av_fast_realloc(samples, &samples_size, FFMAX(pkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE));
a700a6ae
FB
1209 /* XXX: could avoid copy if PCM 16 bits with same
1210 endianness as CPU */
01f4895c 1211 ret = avcodec_decode_audio(ist->st->codec, samples, &data_size,
a700a6ae
FB
1212 ptr, len);
1213 if (ret < 0)
1214 goto fail_decode;
1215 ptr += ret;
1216 len -= ret;
1217 /* Some bug in mpeg audio decoder gives */
1218 /* data_size < 0, it seems they are overflows */
1219 if (data_size <= 0) {
1220 /* no audio frame */
1221 continue;
1222 }
1223 data_buf = (uint8_t *)samples;
115329f1 1224 ist->next_pts += ((int64_t)AV_TIME_BASE/2 * data_size) /
01f4895c 1225 (ist->st->codec->sample_rate * ist->st->codec->channels);
df84ac2e 1226 break;}
a700a6ae 1227 case CODEC_TYPE_VIDEO:
01f4895c 1228 data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
a700a6ae 1229 /* XXX: allocate picture correctly */
9740beff
MN
1230 avcodec_get_frame_defaults(&picture);
1231
115329f1 1232 ret = avcodec_decode_video(ist->st->codec,
a700a6ae
FB
1233 &picture, &got_picture, ptr, len);
1234 ist->st->quality= picture.quality;
115329f1 1235 if (ret < 0)
a700a6ae
FB
1236 goto fail_decode;
1237 if (!got_picture) {
1238 /* no picture yet */
1239 goto discard_packet;
1240 }
01f4895c 1241 if (ist->st->codec->time_base.num != 0) {
115329f1 1242 ist->next_pts += ((int64_t)AV_TIME_BASE *
01f4895c
MN
1243 ist->st->codec->time_base.num) /
1244 ist->st->codec->time_base.den;
a700a6ae
FB
1245 }
1246 len = 0;
1247 break;
cf7fc795 1248 case CODEC_TYPE_SUBTITLE:
115329f1 1249 ret = avcodec_decode_subtitle(ist->st->codec,
cf7fc795
FB
1250 &subtitle, &got_subtitle, ptr, len);
1251 if (ret < 0)
a700a6ae 1252 goto fail_decode;
cf7fc795
FB
1253 if (!got_subtitle) {
1254 goto discard_packet;
a700a6ae 1255 }
cf7fc795
FB
1256 subtitle_to_free = &subtitle;
1257 len = 0;
1258 break;
1259 default:
1260 goto fail_decode;
1261 }
1262 } else {
01f4895c 1263 switch(ist->st->codec->codec_type) {
2fef0bdf 1264 case CODEC_TYPE_AUDIO:
115329f1 1265 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
01f4895c 1266 (ist->st->codec->sample_rate * ist->st->codec->channels);
2fef0bdf
MN
1267 break;
1268 case CODEC_TYPE_VIDEO:
01f4895c 1269 if (ist->st->codec->time_base.num != 0) {
115329f1 1270 ist->next_pts += ((int64_t)AV_TIME_BASE *
01f4895c
MN
1271 ist->st->codec->time_base.num) /
1272 ist->st->codec->time_base.den;
2fef0bdf
MN
1273 }
1274 break;
1275 }
a700a6ae
FB
1276 data_buf = ptr;
1277 data_size = len;
1278 ret = len;
1279 len = 0;
1280 }
1281
1282 buffer_to_free = NULL;
01f4895c 1283 if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO) {
115329f1 1284 pre_process_video_frame(ist, (AVPicture *)&picture,
a700a6ae
FB
1285 &buffer_to_free);
1286 }
1287
7e987c33 1288 // preprocess audio (volume)
01f4895c 1289 if (ist->st->codec->codec_type == CODEC_TYPE_AUDIO) {
a9aa3467 1290 if (audio_volume != 256) {
7e987c33 1291 short *volp;
7e987c33
C
1292 volp = samples;
1293 for(i=0;i<(data_size / sizeof(short));i++) {
a9aa3467 1294 int v = ((*volp) * audio_volume + 128) >> 8;
7e987c33
C
1295 if (v < -32768) v = -32768;
1296 if (v > 32767) v = 32767;
1297 *volp++ = v;
1298 }
1299 }
1300 }
1301
a700a6ae 1302 /* frame rate emulation */
01f4895c
MN
1303 if (ist->st->codec->rate_emu) {
1304 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec->time_base.num, 1000000, ist->st->codec->time_base.den);
a700a6ae
FB
1305 int64_t now = av_gettime() - ist->start;
1306 if (pts > now)
1307 usleep(pts - now);
1308
1309 ist->frame++;
1310 }
1311
1312#if 0
1313 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1314 is the one of the next displayed one */
1315 /* XXX: add mpeg4 too ? */
01f4895c
MN
1316 if (ist->st->codec->codec_id == CODEC_ID_MPEG1VIDEO) {
1317 if (ist->st->codec->pict_type != B_TYPE) {
a700a6ae
FB
1318 int64_t tmp;
1319 tmp = ist->last_ip_pts;
1320 ist->last_ip_pts = ist->frac_pts.val;
1321 ist->frac_pts.val = tmp;
1322 }
1323 }
1324#endif
115329f1 1325 /* if output time reached then transcode raw format,
a700a6ae
FB
1326 encode packets and output them */
1327 if (start_time == 0 || ist->pts >= start_time)
1328 for(i=0;i<nb_ostreams;i++) {
1329 int frame_size;
1330
1331 ost = ost_table[i];
1332 if (ost->source_index == ist_index) {
1333 os = output_files[ost->file_index];
1334
1335#if 0
115329f1
DB
1336 printf("%d: got pts=%0.3f %0.3f\n", i,
1337 (double)pkt->pts / AV_TIME_BASE,
1338 ((double)ist->pts / AV_TIME_BASE) -
986ebcdb 1339 ((double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den));
a700a6ae
FB
1340#endif
1341 /* set the input output pts pairs */
b4a3389e 1342 //ost->sync_ipts = (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/ AV_TIME_BASE;
a700a6ae
FB
1343
1344 if (ost->encoding_needed) {
01f4895c 1345 switch(ost->st->codec->codec_type) {
a700a6ae
FB
1346 case CODEC_TYPE_AUDIO:
1347 do_audio_out(os, ost, ist, data_buf, data_size);
1348 break;
1349 case CODEC_TYPE_VIDEO:
986ebcdb 1350 do_video_out(os, ost, ist, &picture, &frame_size);
1008ceb3 1351 video_size += frame_size;
a700a6ae
FB
1352 if (do_vstats && frame_size)
1353 do_video_stats(os, ost, frame_size);
a700a6ae 1354 break;
cf7fc795
FB
1355 case CODEC_TYPE_SUBTITLE:
1356 do_subtitle_out(os, ost, ist, &subtitle,
1357 pkt->pts);
1358 break;
a700a6ae
FB
1359 default:
1360 av_abort();
1361 }
1362 } else {
e928649b
MN
1363 AVFrame avframe; //FIXME/XXX remove this
1364 AVPacket opkt;
1365 av_init_packet(&opkt);
1366
a700a6ae
FB
1367 /* no reencoding needed : output the packet directly */
1368 /* force the input stream PTS */
115329f1 1369
9740beff 1370 avcodec_get_frame_defaults(&avframe);
01f4895c 1371 ost->st->codec->coded_frame= &avframe;
115329f1 1372 avframe.key_frame = pkt->flags & PKT_FLAG_KEY;
bf17ff61 1373
01f4895c 1374 if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO)
c7baf14b 1375 audio_size += data_size;
01f4895c 1376 else if (ost->st->codec->codec_type == CODEC_TYPE_VIDEO) {
c7baf14b 1377 video_size += data_size;
daed32f7
WG
1378 ost->sync_opts++;
1379 }
c7baf14b 1380
e928649b 1381 opkt.stream_index= ost->index;
6c55b27f 1382 if(pkt->pts != AV_NOPTS_VALUE)
c0df9d75 1383 opkt.pts= av_rescale_q(av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q) + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ost->st->time_base);
6c55b27f
MN
1384 else
1385 opkt.pts= AV_NOPTS_VALUE;
cf7fc795
FB
1386
1387 {
1388 int64_t dts;
1389 if (pkt->dts == AV_NOPTS_VALUE)
1390 dts = ist->next_pts;
1391 else
1392 dts= av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1393 opkt.dts= av_rescale_q(dts + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ost->st->time_base);
1394 }
e928649b 1395 opkt.flags= pkt->flags;
01f4895c 1396 if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & PKT_FLAG_KEY))
90ad92b3 1397 opkt.destruct= av_destruct_packet;
3c895fc0 1398 av_interleaved_write_frame(os, &opkt);
01f4895c 1399 ost->st->codec->frame_number++;
a700a6ae 1400 ost->frame_number++;
90ad92b3 1401 av_free_packet(&opkt);
a700a6ae
FB
1402 }
1403 }
1404 }
1405 av_free(buffer_to_free);
cf7fc795
FB
1406 /* XXX: allocate the subtitles in the codec ? */
1407 if (subtitle_to_free) {
c6ec28b1
MN
1408 if (subtitle_to_free->rects != NULL) {
1409 for (i = 0; i < subtitle_to_free->num_rects; i++) {
1410 av_free(subtitle_to_free->rects[i].bitmap);
1411 av_free(subtitle_to_free->rects[i].rgba_palette);
1412 }
1413 av_freep(&subtitle_to_free->rects);
1414 }
1415 subtitle_to_free->num_rects = 0;
cf7fc795
FB
1416 subtitle_to_free = NULL;
1417 }
a700a6ae
FB
1418 }
1419 discard_packet:
6f824977
MN
1420 if (pkt == NULL) {
1421 /* EOF handling */
115329f1 1422
6f824977
MN
1423 for(i=0;i<nb_ostreams;i++) {
1424 ost = ost_table[i];
1425 if (ost->source_index == ist_index) {
01f4895c 1426 AVCodecContext *enc= ost->st->codec;
6f824977 1427 os = output_files[ost->file_index];
115329f1 1428
01f4895c 1429 if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO && enc->frame_size <=1)
6f824977 1430 continue;
01f4895c 1431 if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
6f824977
MN
1432 continue;
1433
1434 if (ost->encoding_needed) {
1435 for(;;) {
1436 AVPacket pkt;
1437 av_init_packet(&pkt);
1438 pkt.stream_index= ost->index;
115329f1 1439
01f4895c 1440 switch(ost->st->codec->codec_type) {
115329f1 1441 case CODEC_TYPE_AUDIO:
8a6cb114 1442 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
6f824977
MN
1443 audio_size += ret;
1444 pkt.flags |= PKT_FLAG_KEY;
1445 break;
1446 case CODEC_TYPE_VIDEO:
8a6cb114 1447 ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
6f824977
MN
1448 video_size += ret;
1449 if(enc->coded_frame && enc->coded_frame->key_frame)
1450 pkt.flags |= PKT_FLAG_KEY;
1451 if (ost->logfile && enc->stats_out) {
1452 fprintf(ost->logfile, "%s", enc->stats_out);
1453 }
1454 break;
1455 default:
1456 ret=-1;
1457 }
115329f1 1458
6f824977
MN
1459 if(ret<=0)
1460 break;
27537106 1461 pkt.data= bit_buffer;
6f824977 1462 pkt.size= ret;
e7902f20 1463 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 1464 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
6f824977
MN
1465 av_interleaved_write_frame(os, &pkt);
1466 }
1467 }
1468 }
1469 }
1470 }
115329f1 1471
a700a6ae
FB
1472 return 0;
1473 fail_decode:
1474 return -1;
1475}
1476
1477
85f07f22
FB
1478/*
1479 * The following code is the main loop of the file converter
1480 */
1481static int av_encode(AVFormatContext **output_files,
1482 int nb_output_files,
1483 AVFormatContext **input_files,
1484 int nb_input_files,
1485 AVStreamMap *stream_maps, int nb_stream_maps)
1486{
445f1b83 1487 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
85f07f22
FB
1488 AVFormatContext *is, *os;
1489 AVCodecContext *codec, *icodec;
1490 AVOutputStream *ost, **ost_table = NULL;
1491 AVInputStream *ist, **ist_table = NULL;
bdc4796f 1492 AVInputFile *file_table;
51bd4565 1493 AVFormatContext *stream_no_data;
cb09b2ed 1494 int key;
bdc4796f 1495
51bd4565 1496 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
bdc4796f
FB
1497 if (!file_table)
1498 goto fail;
115329f1 1499
85f07f22
FB
1500 /* input stream init */
1501 j = 0;
1502 for(i=0;i<nb_input_files;i++) {
1503 is = input_files[i];
1504 file_table[i].ist_index = j;
79fdaa4c 1505 file_table[i].nb_streams = is->nb_streams;
85f07f22
FB
1506 j += is->nb_streams;
1507 }
1508 nb_istreams = j;
1509
1510 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
1511 if (!ist_table)
bdc4796f 1512 goto fail;
115329f1 1513
85f07f22
FB
1514 for(i=0;i<nb_istreams;i++) {
1515 ist = av_mallocz(sizeof(AVInputStream));
1516 if (!ist)
1517 goto fail;
1518 ist_table[i] = ist;
1519 }
1520 j = 0;
1521 for(i=0;i<nb_input_files;i++) {
1522 is = input_files[i];
1523 for(k=0;k<is->nb_streams;k++) {
1524 ist = ist_table[j++];
1525 ist->st = is->streams[k];
1526 ist->file_index = i;
1527 ist->index = k;
1528 ist->discard = 1; /* the stream is discarded by default
1529 (changed later) */
bdfcbbed 1530
01f4895c 1531 if (ist->st->codec->rate_emu) {
bdfcbbed
MK
1532 ist->start = av_gettime();
1533 ist->frame = 0;
1534 }
85f07f22
FB
1535 }
1536 }
1537
1538 /* output stream init */
1539 nb_ostreams = 0;
1540 for(i=0;i<nb_output_files;i++) {
1541 os = output_files[i];
1542 nb_ostreams += os->nb_streams;
1543 }
1544 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
1545 fprintf(stderr, "Number of stream maps must match number of output streams\n");
1546 exit(1);
1547 }
1548
bd073980
BF
1549 /* Sanity check the mapping args -- do the input files & streams exist? */
1550 for(i=0;i<nb_stream_maps;i++) {
1551 int fi = stream_maps[i].file_index;
1552 int si = stream_maps[i].stream_index;
115329f1 1553
bd073980
BF
1554 if (fi < 0 || fi > nb_input_files - 1 ||
1555 si < 0 || si > file_table[fi].nb_streams - 1) {
1556 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
1557 exit(1);
1558 }
b4a3389e
WG
1559 fi = stream_maps[i].sync_file_index;
1560 si = stream_maps[i].sync_stream_index;
1561 if (fi < 0 || fi > nb_input_files - 1 ||
1562 si < 0 || si > file_table[fi].nb_streams - 1) {
1563 fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
1564 exit(1);
1565 }
bd073980 1566 }
115329f1 1567
85f07f22
FB
1568 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
1569 if (!ost_table)
1570 goto fail;
1571 for(i=0;i<nb_ostreams;i++) {
1572 ost = av_mallocz(sizeof(AVOutputStream));
1573 if (!ost)
1574 goto fail;
1575 ost_table[i] = ost;
1576 }
115329f1 1577
85f07f22
FB
1578 n = 0;
1579 for(k=0;k<nb_output_files;k++) {
1580 os = output_files[k];
1581 for(i=0;i<os->nb_streams;i++) {
1582 int found;
1583 ost = ost_table[n++];
1584 ost->file_index = k;
1585 ost->index = i;
1586 ost->st = os->streams[i];
1587 if (nb_stream_maps > 0) {
115329f1 1588 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
85f07f22 1589 stream_maps[n-1].stream_index;
115329f1 1590
bd073980 1591 /* Sanity check that the stream types match */
01f4895c 1592 if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
bd073980
BF
1593 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
1594 stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
1595 ost->file_index, ost->index);
1596 exit(1);
1597 }
115329f1 1598
85f07f22
FB
1599 } else {
1600 /* get corresponding input stream index : we select the first one with the right type */
1601 found = 0;
1602 for(j=0;j<nb_istreams;j++) {
1603 ist = ist_table[j];
115329f1 1604 if (ist->discard &&
01f4895c 1605 ist->st->codec->codec_type == ost->st->codec->codec_type) {
85f07f22
FB
1606 ost->source_index = j;
1607 found = 1;
c19e76ce 1608 break;
85f07f22
FB
1609 }
1610 }
115329f1 1611
85f07f22
FB
1612 if (!found) {
1613 /* try again and reuse existing stream */
1614 for(j=0;j<nb_istreams;j++) {
1615 ist = ist_table[j];
01f4895c 1616 if (ist->st->codec->codec_type == ost->st->codec->codec_type) {
85f07f22
FB
1617 ost->source_index = j;
1618 found = 1;
1619 }
1620 }
1621 if (!found) {
1622 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1623 ost->file_index, ost->index);
1624 exit(1);
1625 }
1626 }
1627 }
1628 ist = ist_table[ost->source_index];
1629 ist->discard = 0;
b4a3389e
WG
1630 ost->sync_ist = (nb_stream_maps > 0) ?
1631 ist_table[file_table[stream_maps[n-1].sync_file_index].ist_index +
1632 stream_maps[n-1].sync_stream_index] : ist;
85f07f22
FB
1633 }
1634 }
1635
85f07f22
FB
1636 /* for each output stream, we compute the right encoding parameters */
1637 for(i=0;i<nb_ostreams;i++) {
1638 ost = ost_table[i];
1639 ist = ist_table[ost->source_index];
1640
01f4895c
MN
1641 codec = ost->st->codec;
1642 icodec = ist->st->codec;
85f07f22 1643
1629626f
FB
1644 if (ost->st->stream_copy) {
1645 /* if stream_copy is selected, no need to decode or encode */
1646 codec->codec_id = icodec->codec_id;
1647 codec->codec_type = icodec->codec_type;
b2a2197e 1648 if(!codec->codec_tag) codec->codec_tag = icodec->codec_tag;
1629626f 1649 codec->bit_rate = icodec->bit_rate;
dffbfd06
MN
1650 codec->extradata= icodec->extradata;
1651 codec->extradata_size= icodec->extradata_size;
78e03516 1652 codec->time_base = icodec->time_base;
1629626f
FB
1653 switch(codec->codec_type) {
1654 case CODEC_TYPE_AUDIO:
1655 codec->sample_rate = icodec->sample_rate;
1656 codec->channels = icodec->channels;
0a3b0447 1657 codec->frame_size = icodec->frame_size;
c1344911 1658 codec->block_align= icodec->block_align;
1629626f
FB
1659 break;
1660 case CODEC_TYPE_VIDEO:
1629626f
FB
1661 codec->width = icodec->width;
1662 codec->height = icodec->height;
ff8cc24b 1663 codec->has_b_frames = icodec->has_b_frames;
1629626f 1664 break;
cf7fc795
FB
1665 case CODEC_TYPE_SUBTITLE:
1666 break;
1629626f
FB
1667 default:
1668 av_abort();
1669 }
1670 } else {
1671 switch(codec->codec_type) {
1672 case CODEC_TYPE_AUDIO:
85f07f22
FB
1673 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1674 goto fail;
115329f1 1675
85f07f22
FB
1676 if (codec->channels == icodec->channels &&
1677 codec->sample_rate == icodec->sample_rate) {
1678 ost->audio_resample = 0;
1679 } else {
e0d2714a 1680 if (codec->channels != icodec->channels &&
23c99253
MN
1681 (icodec->codec_id == CODEC_ID_AC3 ||
1682 icodec->codec_id == CODEC_ID_DTS)) {
1683 /* Special case for 5:1 AC3 and DTS input */
e0d2714a 1684 /* and mono or stereo output */
6dc96cb0
J
1685 /* Request specific number of channels */
1686 icodec->channels = codec->channels;
1687 if (codec->sample_rate == icodec->sample_rate)
1688 ost->audio_resample = 0;
1689 else {
1690 ost->audio_resample = 1;
6dc96cb0 1691 }
e0d2714a 1692 } else {
115329f1 1693 ost->audio_resample = 1;
986ebcdb
MN
1694 }
1695 }
ff4905a5 1696 if(audio_sync_method>1)
986ebcdb
MN
1697 ost->audio_resample = 1;
1698
1699 if(ost->audio_resample){
1700 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1701 codec->sample_rate, icodec->sample_rate);
1702 if(!ost->resample){
1703 printf("Can't resample. Aborting.\n");
1704 av_abort();
e0d2714a 1705 }
85f07f22
FB
1706 }
1707 ist->decoding_needed = 1;
1708 ost->encoding_needed = 1;
1629626f
FB
1709 break;
1710 case CODEC_TYPE_VIDEO:
85f07f22 1711 if (codec->width == icodec->width &&
34b10a57
D
1712 codec->height == icodec->height &&
1713 frame_topBand == 0 &&
1714 frame_bottomBand == 0 &&
1715 frame_leftBand == 0 &&
115329f1 1716 frame_rightBand == 0 &&
1ff93ffc
TK
1717 frame_padtop == 0 &&
1718 frame_padbottom == 0 &&
1719 frame_padleft == 0 &&
1720 frame_padright == 0)
34b10a57
D
1721 {
1722 ost->video_resample = 0;
1723 ost->video_crop = 0;
1ff93ffc 1724 ost->video_pad = 0;
34b10a57
D
1725 } else if ((codec->width == icodec->width -
1726 (frame_leftBand + frame_rightBand)) &&
1727 (codec->height == icodec->height -
1728 (frame_topBand + frame_bottomBand)))
1729 {
85f07f22 1730 ost->video_resample = 0;
34b10a57
D
1731 ost->video_crop = 1;
1732 ost->topBand = frame_topBand;
1733 ost->leftBand = frame_leftBand;
115329f1 1734 } else if ((codec->width == icodec->width +
1ff93ffc
TK
1735 (frame_padleft + frame_padright)) &&
1736 (codec->height == icodec->height +
1737 (frame_padtop + frame_padbottom))) {
1738 ost->video_resample = 0;
1739 ost->video_crop = 0;
1740 ost->video_pad = 1;
1741 ost->padtop = frame_padtop;
1742 ost->padleft = frame_padleft;
1743 ost->padbottom = frame_padbottom;
1744 ost->padright = frame_padright;
2de28abb 1745 avcodec_get_frame_defaults(&ost->pict_tmp);
a4d36c11 1746 if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
1ff93ffc
TK
1747 codec->width, codec->height ) )
1748 goto fail;
85f07f22 1749 } else {
85f07f22 1750 ost->video_resample = 1;
34b10a57 1751 ost->video_crop = 0; // cropping is handled as part of resample
2de28abb 1752 avcodec_get_frame_defaults(&ost->pict_tmp);
a4d36c11 1753 if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
2caa92d9 1754 codec->width, codec->height ) )
85f07f22 1755 goto fail;
85f07f22 1756
115329f1 1757 ost->img_resample_ctx = img_resample_full_init(
f37f8d4c
AB
1758 codec->width, codec->height,
1759 icodec->width, icodec->height,
ab6d194a 1760 frame_topBand, frame_bottomBand,
115329f1
DB
1761 frame_leftBand, frame_rightBand,
1762 frame_padtop, frame_padbottom,
1ff93ffc 1763 frame_padleft, frame_padright);
115329f1 1764
1ff93ffc
TK
1765 ost->padtop = frame_padtop;
1766 ost->padleft = frame_padleft;
1767 ost->padbottom = frame_padbottom;
1768 ost->padright = frame_padright;
115329f1 1769
85f07f22
FB
1770 }
1771 ost->encoding_needed = 1;
1772 ist->decoding_needed = 1;
1629626f 1773 break;
cf7fc795
FB
1774 case CODEC_TYPE_SUBTITLE:
1775 ost->encoding_needed = 1;
1776 ist->decoding_needed = 1;
1777 break;
1629626f
FB
1778 default:
1779 av_abort();
cf7fc795 1780 break;
85f07f22 1781 }
1629626f 1782 /* two pass mode */
115329f1 1783 if (ost->encoding_needed &&
1629626f
FB
1784 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1785 char logfilename[1024];
1786 FILE *f;
1787 int size;
1788 char *logbuffer;
115329f1
DB
1789
1790 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1791 pass_logfilename ?
1629626f
FB
1792 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1793 if (codec->flags & CODEC_FLAG_PASS1) {
1794 f = fopen(logfilename, "w");
1795 if (!f) {
1796 perror(logfilename);
1797 exit(1);
1798 }
1799 ost->logfile = f;
1800 } else {
1801 /* read the log file */
1802 f = fopen(logfilename, "r");
1803 if (!f) {
1804 perror(logfilename);
1805 exit(1);
1806 }
1807 fseek(f, 0, SEEK_END);
1808 size = ftell(f);
1809 fseek(f, 0, SEEK_SET);
1810 logbuffer = av_malloc(size + 1);
1811 if (!logbuffer) {
1812 fprintf(stderr, "Could not allocate log buffer\n");
1813 exit(1);
1814 }
4776fa92 1815 size = fread(logbuffer, 1, size, f);
1629626f
FB
1816 fclose(f);
1817 logbuffer[size] = '\0';
1818 codec->stats_in = logbuffer;
5abdb4b1 1819 }
5abdb4b1
FB
1820 }
1821 }
8a6cb114
MN
1822 if(codec->codec_type == CODEC_TYPE_VIDEO){
1823 int size= codec->width * codec->height;
1824 bit_buffer_size= FFMAX(bit_buffer_size, 4*size);
1825 }
85f07f22
FB
1826 }
1827
8a6cb114
MN
1828 if (!bit_buffer)
1829 bit_buffer = av_malloc(bit_buffer_size);
1830 if (!bit_buffer)
1831 goto fail;
1832
1629626f
FB
1833 /* dump the file output parameters - cannot be done before in case
1834 of stream copy */
1835 for(i=0;i<nb_output_files;i++) {
1836 dump_format(output_files[i], i, output_files[i]->filename, 1);
1837 }
1838
1839 /* dump the stream mapping */
d8019eb5
AD
1840 if (verbose >= 0) {
1841 fprintf(stderr, "Stream mapping:\n");
1842 for(i=0;i<nb_ostreams;i++) {
1843 ost = ost_table[i];
b4a3389e 1844 fprintf(stderr, " Stream #%d.%d -> #%d.%d",
d8019eb5
AD
1845 ist_table[ost->source_index]->file_index,
1846 ist_table[ost->source_index]->index,
115329f1 1847 ost->file_index,
d8019eb5 1848 ost->index);
b4a3389e
WG
1849 if (ost->sync_ist != ist_table[ost->source_index])
1850 fprintf(stderr, " [sync #%d.%d]",
1851 ost->sync_ist->file_index,
1852 ost->sync_ist->index);
1853 fprintf(stderr, "\n");
d8019eb5 1854 }
1629626f
FB
1855 }
1856
85f07f22
FB
1857 /* open each encoder */
1858 for(i=0;i<nb_ostreams;i++) {
1859 ost = ost_table[i];
1860 if (ost->encoding_needed) {
1861 AVCodec *codec;
01f4895c 1862 codec = avcodec_find_encoder(ost->st->codec->codec_id);
85f07f22 1863 if (!codec) {
115329f1 1864 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
85f07f22
FB
1865 ost->file_index, ost->index);
1866 exit(1);
1867 }
01f4895c 1868 if (avcodec_open(ost->st->codec, codec) < 0) {
115329f1 1869 fprintf(stderr, "Error while opening codec for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
85f07f22
FB
1870 ost->file_index, ost->index);
1871 exit(1);
1872 }
01f4895c 1873 extra_size += ost->st->codec->extradata_size;
85f07f22
FB
1874 }
1875 }
1876
1877 /* open each decoder */
1878 for(i=0;i<nb_istreams;i++) {
1879 ist = ist_table[i];
1880 if (ist->decoding_needed) {
1881 AVCodec *codec;
01f4895c 1882 codec = avcodec_find_decoder(ist->st->codec->codec_id);
85f07f22 1883 if (!codec) {
115329f1 1884 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
01f4895c 1885 ist->st->codec->codec_id, ist->file_index, ist->index);
85f07f22
FB
1886 exit(1);
1887 }
01f4895c 1888 if (avcodec_open(ist->st->codec, codec) < 0) {
115329f1 1889 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
85f07f22
FB
1890 ist->file_index, ist->index);
1891 exit(1);
1892 }
01f4895c
MN
1893 //if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO)
1894 // ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
85f07f22
FB
1895 }
1896 }
1897
1898 /* init pts */
1899 for(i=0;i<nb_istreams;i++) {
1900 ist = ist_table[i];
445f1b83 1901 is = input_files[ist->file_index];
e7d0374f 1902 ist->pts = 0;
c0df9d75 1903 ist->next_pts = av_rescale_q(ist->st->start_time, ist->st->time_base, AV_TIME_BASE_Q);
115329f1 1904 if(ist->st->start_time == AV_NOPTS_VALUE)
bd1b79a1 1905 ist->next_pts=0;
fdb86eb1
MN
1906 if(input_files_ts_offset[ist->file_index])
1907 ist->next_pts= AV_NOPTS_VALUE;
ff4905a5 1908 ist->is_start = 1;
85f07f22 1909 }
bd1b79a1 1910
85f07f22
FB
1911 /* compute buffer size max (should use a complete heuristic) */
1912 for(i=0;i<nb_input_files;i++) {
1913 file_table[i].buffer_size_max = 2048;
1914 }
1915
0a38bafd
PB
1916 /* set meta data information from input file if required */
1917 for (i=0;i<nb_meta_data_maps;i++) {
1918 AVFormatContext *out_file;
1919 AVFormatContext *in_file;
1920
1921 int out_file_index = meta_data_maps[i].out_file;
1922 int in_file_index = meta_data_maps[i].in_file;
1923 if ( out_file_index < 0 || out_file_index >= nb_output_files ) {
1924 fprintf(stderr, "Invalid output file index %d map_meta_data(%d,%d)\n", out_file_index, out_file_index, in_file_index);
1925 ret = -EINVAL;
1926 goto fail;
1927 }
1928 if ( in_file_index < 0 || in_file_index >= nb_input_files ) {
1929 fprintf(stderr, "Invalid input file index %d map_meta_data(%d,%d)\n", in_file_index, out_file_index, in_file_index);
1930 ret = -EINVAL;
1931 goto fail;
115329f1
DB
1932 }
1933
0a38bafd
PB
1934 out_file = output_files[out_file_index];
1935 in_file = input_files[in_file_index];
1936
1937 strcpy(out_file->title, in_file->title);
1938 strcpy(out_file->author, in_file->author);
1939 strcpy(out_file->copyright, in_file->copyright);
1940 strcpy(out_file->comment, in_file->comment);
1941 strcpy(out_file->album, in_file->album);
1942 out_file->year = in_file->year;
1943 out_file->track = in_file->track;
1944 strcpy(out_file->genre, in_file->genre);
1945 }
115329f1 1946
85f07f22
FB
1947 /* open files and write file headers */
1948 for(i=0;i<nb_output_files;i++) {
1949 os = output_files[i];
79fdaa4c 1950 if (av_write_header(os) < 0) {
65bf3c53 1951 fprintf(stderr, "Could not write header for output file #%d (incorrect codec parameters ?)\n", i);
a38469e1
FB
1952 ret = -EINVAL;
1953 goto fail;
1954 }
85f07f22
FB
1955 }
1956
a38469e1 1957#ifndef CONFIG_WIN32
d8019eb5 1958 if ( !using_stdin && verbose >= 0) {
d9a916e2 1959 fprintf(stderr, "Press [q] to stop encoding\n");
b51469a0
LS
1960 url_set_interrupt_cb(decode_interrupt_cb);
1961 }
a38469e1
FB
1962#endif
1963 term_init();
1964
51bd4565 1965 stream_no_data = 0;
cb09b2ed 1966 key = -1;
51bd4565 1967
d9a916e2 1968 for(; received_sigterm == 0;) {
85f07f22
FB
1969 int file_index, ist_index;
1970 AVPacket pkt;
a9aeda81
PB
1971 double ipts_min;
1972 double opts_min;
23ffe323 1973
85f07f22 1974 redo:
a9aeda81
PB
1975 ipts_min= 1e100;
1976 opts_min= 1e100;
a38469e1 1977 /* if 'q' pressed, exits */
d9a916e2 1978 if (!using_stdin) {
b51469a0
LS
1979 if (q_pressed)
1980 break;
cb09b2ed
PG
1981 /* read_key() returns 0 on EOF */
1982 key = read_key();
1983 if (key == 'q')
1984 break;
1985 }
a38469e1 1986
ec5517d5
FB
1987 /* select the stream that we must read now by looking at the
1988 smallest output pts */
85f07f22 1989 file_index = -1;
ec5517d5 1990 for(i=0;i<nb_ostreams;i++) {
23ffe323 1991 double ipts, opts;
ec5517d5
FB
1992 ost = ost_table[i];
1993 os = output_files[ost->file_index];
1994 ist = ist_table[ost->source_index];
01f4895c
MN
1995 if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO)
1996 opts = ost->sync_opts * av_q2d(ost->st->codec->time_base);
23ffe323 1997 else
c0df9d75 1998 opts = ost->st->pts.val * av_q2d(ost->st->time_base);
23ffe323
MN
1999 ipts = (double)ist->pts;
2000 if (!file_table[ist->file_index].eof_reached){
2001 if(ipts < ipts_min) {
2002 ipts_min = ipts;
2003 if(input_sync ) file_index = ist->file_index;
2004 }
2005 if(opts < opts_min) {
2006 opts_min = opts;
2007 if(!input_sync) file_index = ist->file_index;
2008 }
85f07f22 2009 }
01f4895c 2010 if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
7ca60994
MN
2011 file_index= -1;
2012 break;
2013 }
85f07f22
FB
2014 }
2015 /* if none, if is finished */
51bd4565 2016 if (file_index < 0) {
85f07f22 2017 break;
ec5517d5
FB
2018 }
2019
85f07f22 2020 /* finish if recording time exhausted */
23ffe323 2021 if (recording_time > 0 && opts_min >= (recording_time / 1000000.0))
85f07f22 2022 break;
ec5517d5 2023
b6e16b86
C
2024 /* finish if limit size exhausted */
2025 if (limit_filesize != 0 && (limit_filesize * 1024) < url_ftell(&output_files[0]->pb))
2026 break;
2027
254abc2e 2028 /* read a frame from it and output it in the fifo */
85f07f22 2029 is = input_files[file_index];
254abc2e 2030 if (av_read_frame(is, &pkt) < 0) {
85f07f22 2031 file_table[file_index].eof_reached = 1;
ca80810b 2032 if (opt_shortest) break; else continue; //
85f07f22 2033 }
303e50e6 2034
51bd4565
PG
2035 if (!pkt.size) {
2036 stream_no_data = is;
2037 } else {
2038 stream_no_data = 0;
2039 }
254abc2e
FB
2040 if (do_pkt_dump) {
2041 av_pkt_dump(stdout, &pkt, do_hex_dump);
817b23ff 2042 }
79fdaa4c
FB
2043 /* the following test is needed in case new streams appear
2044 dynamically in stream : we ignore them */
2045 if (pkt.stream_index >= file_table[file_index].nb_streams)
bf5af568 2046 goto discard_packet;
85f07f22
FB
2047 ist_index = file_table[file_index].ist_index + pkt.stream_index;
2048 ist = ist_table[ist_index];
bf5af568
FB
2049 if (ist->discard)
2050 goto discard_packet;
85f07f22 2051
01f4895c 2052// fprintf(stderr, "next:%lld dts:%lld off:%lld %d\n", ist->next_pts, pkt.dts, input_files_ts_offset[ist->file_index], ist->st->codec->codec_type);
fdb86eb1 2053 if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE) {
c0df9d75 2054 int64_t delta= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q) - ist->next_pts;
72bd8100
MN
2055 if(ABS(delta) > 10LL*AV_TIME_BASE && !copy_ts){
2056 input_files_ts_offset[ist->file_index]-= delta;
2057 if (verbose > 2)
4733abcb 2058 fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
72bd8100
MN
2059 for(i=0; i<file_table[file_index].nb_streams; i++){
2060 int index= file_table[file_index].ist_index + i;
2061 ist_table[index]->next_pts += delta;
ff4905a5 2062 ist_table[index]->is_start=1;
72bd8100
MN
2063 }
2064 }
2065 }
2066
8831db5c 2067 //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
a700a6ae 2068 if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
d8019eb5
AD
2069
2070 if (verbose >= 0)
2071 fprintf(stderr, "Error while decoding stream #%d.%d\n",
2072 ist->file_index, ist->index);
2073
a700a6ae
FB
2074 av_free_packet(&pkt);
2075 goto redo;
fe08925f 2076 }
cf7fc795 2077
bf5af568 2078 discard_packet:
85f07f22 2079 av_free_packet(&pkt);
115329f1 2080
ec5517d5
FB
2081 /* dump report by using the output first video and audio streams */
2082 print_report(output_files, ost_table, nb_ostreams, 0);
85f07f22 2083 }
a700a6ae
FB
2084
2085 /* at the end of stream, we must flush the decoder buffers */
2086 for(i=0;i<nb_istreams;i++) {
2087 ist = ist_table[i];
2088 if (ist->decoding_needed) {
2089 output_packet(ist, i, ost_table, nb_ostreams, NULL);
2090 }
2091 }
2092
a38469e1 2093 term_exit();
85f07f22 2094
c4e37247
MN
2095 /* write the trailer if needed and close file */
2096 for(i=0;i<nb_output_files;i++) {
2097 os = output_files[i];
2098 av_write_trailer(os);
2099 }
2100
37f5cd5a
MN
2101 /* dump report by using the first video and audio streams */
2102 print_report(output_files, ost_table, nb_ostreams, 1);
2103
85f07f22
FB
2104 /* close each encoder */
2105 for(i=0;i<nb_ostreams;i++) {
2106 ost = ost_table[i];
2107 if (ost->encoding_needed) {
01f4895c
MN
2108 av_freep(&ost->st->codec->stats_in);
2109 avcodec_close(ost->st->codec);
85f07f22
FB
2110 }
2111 }
115329f1 2112
85f07f22
FB
2113 /* close each decoder */
2114 for(i=0;i<nb_istreams;i++) {
2115 ist = ist_table[i];
2116 if (ist->decoding_needed) {
01f4895c 2117 avcodec_close(ist->st->codec);
85f07f22
FB
2118 }
2119 }
85f07f22 2120
85f07f22 2121 /* finished ! */
115329f1 2122
85f07f22
FB
2123 ret = 0;
2124 fail1:
83b07470 2125 av_freep(&bit_buffer);
0f1578af 2126 av_free(file_table);
bdc4796f 2127
85f07f22
FB
2128 if (ist_table) {
2129 for(i=0;i<nb_istreams;i++) {
2130 ist = ist_table[i];
0f1578af 2131 av_free(ist);
85f07f22 2132 }
0f1578af 2133 av_free(ist_table);
85f07f22
FB
2134 }
2135 if (ost_table) {
2136 for(i=0;i<nb_ostreams;i++) {
2137 ost = ost_table[i];
2138 if (ost) {
5abdb4b1
FB
2139 if (ost->logfile) {
2140 fclose(ost->logfile);
2141 ost->logfile = NULL;
2142 }
bf5af568
FB
2143 fifo_free(&ost->fifo); /* works even if fifo is not
2144 initialized but set to zero */
0f1578af 2145 av_free(ost->pict_tmp.data[0]);
85f07f22
FB
2146 if (ost->video_resample)
2147 img_resample_close(ost->img_resample_ctx);
2148 if (ost->audio_resample)
2149 audio_resample_close(ost->resample);
0f1578af 2150 av_free(ost);
85f07f22
FB
2151 }
2152 }
0f1578af 2153 av_free(ost_table);
85f07f22
FB
2154 }
2155 return ret;
2156 fail:
2157 ret = -ENOMEM;
2158 goto fail1;
2159}
2160
2161#if 0
2162int file_read(const char *filename)
2163{
2164 URLContext *h;
2165 unsigned char buffer[1024];
2166 int len, i;
2167
2168 if (url_open(&h, filename, O_RDONLY) < 0) {
2169 printf("could not open '%s'\n", filename);
2170 return -1;
2171 }
2172 for(;;) {
2173 len = url_read(h, buffer, sizeof(buffer));
2174 if (len <= 0)
2175 break;
2176 for(i=0;i<len;i++) putchar(buffer[i]);
2177 }
2178 url_close(h);
2179 return 0;
2180}
2181#endif
2182
b29f97d1 2183static void opt_image_format(const char *arg)
817b23ff
FB
2184{
2185 AVImageFormat *f;
115329f1 2186
817b23ff
FB
2187 for(f = first_image_format; f != NULL; f = f->next) {
2188 if (!strcmp(arg, f->name))
2189 break;
2190 }
2191 if (!f) {
2192 fprintf(stderr, "Unknown image format: '%s'\n", arg);
2193 exit(1);
2194 }
2195 image_format = f;
2196}
2197
b29f97d1 2198static void opt_format(const char *arg)
85f07f22 2199{
817b23ff
FB
2200 /* compatibility stuff for pgmyuv */
2201 if (!strcmp(arg, "pgmyuv")) {
5b6d5596 2202 pgmyuv_compatibility_hack=1;
9286699a 2203// opt_image_format(arg);
5b6d5596 2204 arg = "image2";
817b23ff
FB
2205 }
2206
79fdaa4c
FB
2207 file_iformat = av_find_input_format(arg);
2208 file_oformat = guess_format(arg, NULL, NULL);
2209 if (!file_iformat && !file_oformat) {
2210 fprintf(stderr, "Unknown input or output format: %s\n", arg);
85f07f22
FB
2211 exit(1);
2212 }
85f07f22
FB
2213}
2214
b29f97d1 2215static void opt_video_bitrate(const char *arg)
85f07f22
FB
2216{
2217 video_bit_rate = atoi(arg) * 1000;
2218}
2219
b29f97d1 2220static void opt_video_bitrate_tolerance(const char *arg)
9cdd6a24
MN
2221{
2222 video_bit_rate_tolerance = atoi(arg) * 1000;
2223}
2224
b29f97d1 2225static void opt_video_bitrate_max(const char *arg)
3aa102be
MN
2226{
2227 video_rc_max_rate = atoi(arg) * 1000;
2228}
2229
b29f97d1 2230static void opt_video_bitrate_min(const char *arg)
3aa102be
MN
2231{
2232 video_rc_min_rate = atoi(arg) * 1000;
2233}
2234
b29f97d1 2235static void opt_video_buffer_size(const char *arg)
946c8a12 2236{
622348f9 2237 video_rc_buffer_size = atoi(arg) * 8*1024;
946c8a12
MN
2238}
2239
b29f97d1 2240static void opt_video_rc_eq(char *arg)
3aa102be
MN
2241{
2242 video_rc_eq = arg;
2243}
2244
b29f97d1 2245static void opt_video_rc_override_string(char *arg)
ac2830ec
MN
2246{
2247 video_rc_override_string = arg;
2248}
2249
3aa102be 2250
b29f97d1 2251static void opt_workaround_bugs(const char *arg)
fe670d09
MN
2252{
2253 workaround_bugs = atoi(arg);
2254}
2255
f4f3223f
MN
2256static void opt_me_threshold(const char *arg)
2257{
2258 me_threshold = atoi(arg);
2259}
2260
f20f8a8b
MN
2261static void opt_mb_threshold(const char *arg)
2262{
2263 mb_threshold = atoi(arg);
2264}
2ad1516a 2265
f068206e
BE
2266static void opt_verbose(const char *arg)
2267{
2268 verbose = atoi(arg);
d8019eb5 2269 av_log_set_level(atoi(arg));
f068206e
BE
2270}
2271
b29f97d1 2272static void opt_frame_rate(const char *arg)
85f07f22 2273{
445f1b83
RS
2274 if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
2275 fprintf(stderr, "Incorrect frame rate\n");
2276 exit(1);
2277 }
85f07f22
FB
2278}
2279
b29f97d1 2280static void opt_frame_crop_top(const char *arg)
ab6d194a 2281{
115329f1 2282 frame_topBand = atoi(arg);
ab6d194a
MN
2283 if (frame_topBand < 0) {
2284 fprintf(stderr, "Incorrect top crop size\n");
2285 exit(1);
2286 }
2287 if ((frame_topBand % 2) != 0) {
2288 fprintf(stderr, "Top crop size must be a multiple of 2\n");
2289 exit(1);
2290 }
2291 if ((frame_topBand) >= frame_height){
2292 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2293 exit(1);
2294 }
2295 frame_height -= frame_topBand;
2296}
2297
b29f97d1 2298static void opt_frame_crop_bottom(const char *arg)
ab6d194a
MN
2299{
2300 frame_bottomBand = atoi(arg);
2301 if (frame_bottomBand < 0) {
2302 fprintf(stderr, "Incorrect bottom crop size\n");
2303 exit(1);
2304 }
2305 if ((frame_bottomBand % 2) != 0) {
2306 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
115329f1 2307 exit(1);
ab6d194a
MN
2308 }
2309 if ((frame_bottomBand) >= frame_height){
2310 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2311 exit(1);
2312 }
2313 frame_height -= frame_bottomBand;
2314}
2315
b29f97d1 2316static void opt_frame_crop_left(const char *arg)
ab6d194a
MN
2317{
2318 frame_leftBand = atoi(arg);
2319 if (frame_leftBand < 0) {
2320 fprintf(stderr, "Incorrect left crop size\n");
2321 exit(1);
2322 }
2323 if ((frame_leftBand % 2) != 0) {
2324 fprintf(stderr, "Left crop size must be a multiple of 2\n");
2325 exit(1);
2326 }
2327 if ((frame_leftBand) >= frame_width){
2328 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2329 exit(1);
2330 }
2331 frame_width -= frame_leftBand;
2332}
2333
b29f97d1 2334static void opt_frame_crop_right(const char *arg)
ab6d194a
MN
2335{
2336 frame_rightBand = atoi(arg);
2337 if (frame_rightBand < 0) {
2338 fprintf(stderr, "Incorrect right crop size\n");
2339 exit(1);
2340 }
2341 if ((frame_rightBand % 2) != 0) {
2342 fprintf(stderr, "Right crop size must be a multiple of 2\n");
115329f1 2343 exit(1);
ab6d194a
MN
2344 }
2345 if ((frame_rightBand) >= frame_width){
2346 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2347 exit(1);
2348 }
2349 frame_width -= frame_rightBand;
2350}
2351
b29f97d1 2352static void opt_frame_size(const char *arg)
85f07f22 2353{
445f1b83 2354 if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
85f07f22
FB
2355 fprintf(stderr, "Incorrect frame size\n");
2356 exit(1);
2357 }
2358 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
2359 fprintf(stderr, "Frame size must be a multiple of 2\n");
2360 exit(1);
2361 }
2362}
2363
1ff93ffc
TK
2364
2365#define SCALEBITS 10
2366#define ONE_HALF (1 << (SCALEBITS - 1))
2367#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
2368
2369#define RGB_TO_Y(r, g, b) \
2370((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
2371 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
2372
2373#define RGB_TO_U(r1, g1, b1, shift)\
2374(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
2375 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2376
2377#define RGB_TO_V(r1, g1, b1, shift)\
2378(((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
2379 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2380
2381static void opt_pad_color(const char *arg) {
2382 /* Input is expected to be six hex digits similar to
2383 how colors are expressed in html tags (but without the #) */
2384 int rgb = strtol(arg, NULL, 16);
2385 int r,g,b;
115329f1
DB
2386
2387 r = (rgb >> 16);
1ff93ffc
TK
2388 g = ((rgb >> 8) & 255);
2389 b = (rgb & 255);
2390
2391 padcolor[0] = RGB_TO_Y(r,g,b);
2392 padcolor[1] = RGB_TO_U(r,g,b,0);
2393 padcolor[2] = RGB_TO_V(r,g,b,0);
2394}
2395
2396static void opt_frame_pad_top(const char *arg)
2397{
115329f1 2398 frame_padtop = atoi(arg);
1ff93ffc
TK
2399 if (frame_padtop < 0) {
2400 fprintf(stderr, "Incorrect top pad size\n");
2401 exit(1);
2402 }
2403 if ((frame_padtop % 2) != 0) {
2404 fprintf(stderr, "Top pad size must be a multiple of 2\n");
2405 exit(1);
2406 }
2407}
2408
2409static void opt_frame_pad_bottom(const char *arg)
2410{
115329f1 2411 frame_padbottom = atoi(arg);
1ff93ffc
TK
2412 if (frame_padbottom < 0) {
2413 fprintf(stderr, "Incorrect bottom pad size\n");
2414 exit(1);
2415 }
2416 if ((frame_padbottom % 2) != 0) {
2417 fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2418 exit(1);
2419 }
2420}
2421
2422
2423static void opt_frame_pad_left(const char *arg)
2424{
115329f1 2425 frame_padleft = atoi(arg);
1ff93ffc
TK
2426 if (frame_padleft < 0) {
2427 fprintf(stderr, "Incorrect left pad size\n");
2428 exit(1);
2429 }
2430 if ((frame_padleft % 2) != 0) {
2431 fprintf(stderr, "Left pad size must be a multiple of 2\n");
2432 exit(1);
2433 }
2434}
2435
2436
2437static void opt_frame_pad_right(const char *arg)
2438{
115329f1 2439 frame_padright = atoi(arg);
1ff93ffc
TK
2440 if (frame_padright < 0) {
2441 fprintf(stderr, "Incorrect right pad size\n");
2442 exit(1);
2443 }
2444 if ((frame_padright % 2) != 0) {
2445 fprintf(stderr, "Right pad size must be a multiple of 2\n");
2446 exit(1);
2447 }
2448}
2449
2450
63167088
RS
2451static void opt_frame_pix_fmt(const char *arg)
2452{
2453 frame_pix_fmt = avcodec_get_pix_fmt(arg);
2454}
2455
5fe03e38
RS
2456static void opt_frame_aspect_ratio(const char *arg)
2457{
2458 int x = 0, y = 0;
2459 double ar = 0;
2460 const char *p;
115329f1 2461
5fe03e38
RS
2462 p = strchr(arg, ':');
2463 if (p) {
2464 x = strtol(arg, (char **)&arg, 10);
2465 if (arg == p)
2466 y = strtol(arg+1, (char **)&arg, 10);
2467 if (x > 0 && y > 0)
2468 ar = (double)x / (double)y;
2469 } else
2470 ar = strtod(arg, (char **)&arg);
2471
2472 if (!ar) {
2473 fprintf(stderr, "Incorrect aspect ratio specification.\n");
2474 exit(1);
2475 }
2476 frame_aspect_ratio = ar;
2477}
2478
b29f97d1 2479static void opt_gop_size(const char *arg)
85f07f22
FB
2480{
2481 gop_size = atoi(arg);
2482}
2483
b29f97d1 2484static void opt_b_frames(const char *arg)
bc6caae2
J
2485{
2486 b_frames = atoi(arg);
2487 if (b_frames > FF_MAX_B_FRAMES) {
2488 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
2489 exit(1);
2490 } else if (b_frames < 1) {
2491 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
2492 exit(1);
2493 }
2494}
2495
9c3d33d6
MN
2496static void opt_pre_me(const char *arg)
2497{
2498 pre_me = atoi(arg);
2499}
2500
b29f97d1 2501static void opt_qscale(const char *arg)
85f07f22 2502{
158c7f05
MN
2503 video_qscale = atof(arg);
2504 if (video_qscale < 0.01 ||
2505 video_qscale > 255) {
2506 fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
85f07f22
FB
2507 exit(1);
2508 }
2509}
2510
6a5d4395
MD
2511static void opt_qsquish(const char *arg)
2512{
2513 video_qsquish = atof(arg);
2514 if (video_qsquish < 0.0 ||
2515 video_qsquish > 99.0) {
2516 fprintf(stderr, "qsquish must be >= 0.0 and <= 99.0\n");
2517 exit(1);
2518 }
2519}
2520
4ea4b274
MN
2521static void opt_lmax(const char *arg)
2522{
2523 video_lmax = atof(arg)*FF_QP2LAMBDA;
2524}
2525
2526static void opt_lmin(const char *arg)
2527{
2528 video_lmin = atof(arg)*FF_QP2LAMBDA;
2529}
2530
b29f97d1 2531static void opt_qmin(const char *arg)
9cdd6a24
MN
2532{
2533 video_qmin = atoi(arg);
6e0d8c06 2534 if (video_qmin < 1 ||
9cdd6a24
MN
2535 video_qmin > 31) {
2536 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
2537 exit(1);
2538 }
2539}
2540
b29f97d1 2541static void opt_qmax(const char *arg)
9cdd6a24
MN
2542{
2543 video_qmax = atoi(arg);
6e0d8c06 2544 if (video_qmax < 1 ||
9cdd6a24
MN
2545 video_qmax > 31) {
2546 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
2547 exit(1);
2548 }
2549}
2550
6e0d8c06 2551static void opt_mb_lmin(const char *arg)
17a70fde 2552{
6e0d8c06
MN
2553 video_mb_lmin = atof(arg)*FF_QP2LAMBDA;
2554 if (video_mb_lmin < 1 ||
2555 video_mb_lmin > FF_LAMBDA_MAX) {
2556 fprintf(stderr, "mblmin must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
17a70fde
MN
2557 exit(1);
2558 }
2559}
2560
6e0d8c06 2561static void opt_mb_lmax(const char *arg)
17a70fde 2562{
6e0d8c06
MN
2563 video_mb_lmax = atof(arg)*FF_QP2LAMBDA;
2564 if (video_mb_lmax < 1 ||
2565 video_mb_lmax > FF_LAMBDA_MAX) {
2566 fprintf(stderr, "mblmax must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
17a70fde
MN
2567 exit(1);
2568 }
2569}
2570
b29f97d1 2571static void opt_qdiff(const char *arg)
9cdd6a24
MN
2572{
2573 video_qdiff = atoi(arg);
2574 if (video_qdiff < 0 ||
2575 video_qdiff > 31) {
2576 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2577 exit(1);
2578 }
2579}
2580
b29f97d1 2581static void opt_qblur(const char *arg)
9cdd6a24
MN
2582{
2583 video_qblur = atof(arg);
2584}
2585
b29f97d1 2586static void opt_qcomp(const char *arg)
9cdd6a24
MN
2587{
2588 video_qcomp = atof(arg);
2589}
85f07f22 2590
b29f97d1 2591static void opt_rc_initial_cplx(const char *arg)
ac2830ec
MN
2592{
2593 video_rc_initial_cplx = atof(arg);
2594}
b29f97d1 2595static void opt_b_qfactor(const char *arg)
29700fa6
MN
2596{
2597 video_b_qfactor = atof(arg);
2598}
b29f97d1 2599static void opt_i_qfactor(const char *arg)
29700fa6
MN
2600{
2601 video_i_qfactor = atof(arg);
2602}
b29f97d1 2603static void opt_b_qoffset(const char *arg)
29700fa6
MN
2604{
2605 video_b_qoffset = atof(arg);
2606}
b29f97d1 2607static void opt_i_qoffset(const char *arg)
29700fa6
MN
2608{
2609 video_i_qoffset = atof(arg);
2610}
2611
303e50e6
MN
2612static void opt_ibias(const char *arg)
2613{
2614 video_intra_quant_bias = atoi(arg);
2615}
2616static void opt_pbias(const char *arg)
2617{
2618 video_inter_quant_bias = atoi(arg);
2619}
2620
b29f97d1 2621static void opt_packet_size(const char *arg)
1dbb6d90
MN
2622{
2623 packet_size= atoi(arg);
2624}
2625
7ebfc0ea
MN
2626static void opt_error_rate(const char *arg)
2627{
2628 error_rate= atoi(arg);
2629}
2630
b29f97d1 2631static void opt_strict(const char *arg)
f560dd82
MN
2632{
2633 strict= atoi(arg);
2634}
2635
bb198e19
MN
2636static void opt_top_field_first(const char *arg)
2637{
2638 top_field_first= atoi(arg);
2639}
2640
303e50e6
MN
2641static void opt_sc_threshold(const char *arg)
2642{
2643 sc_threshold= atoi(arg);
2644}
2645
2f0472ff
TB
2646static void opt_me_range(const char *arg)
2647{
2648 me_range = atoi(arg);
2649}
2650
9c3d33d6
MN
2651static void opt_thread_count(const char *arg)
2652{
2653 thread_count= atoi(arg);
2450cff2 2654#if !defined(HAVE_THREADS)
d8019eb5
AD
2655 if (verbose >= 0)
2656 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
842b556a 2657#endif
9c3d33d6
MN
2658}
2659
b29f97d1 2660static void opt_audio_bitrate(const char *arg)
85f07f22
FB
2661{
2662 audio_bit_rate = atoi(arg) * 1000;
2663}
2664
b29f97d1 2665static void opt_audio_rate(const char *arg)
85f07f22
FB
2666{
2667 audio_sample_rate = atoi(arg);
2668}
2669
b29f97d1 2670static void opt_audio_channels(const char *arg)
85f07f22
FB
2671{
2672 audio_channels = atoi(arg);
2673}
2674
b29f97d1 2675static void opt_video_device(const char *arg)
85f07f22 2676{
e9a9e0c2 2677 video_device = av_strdup(arg);
85f07f22
FB
2678}
2679
2639c651
RS
2680static void opt_grab_device(const char *arg)
2681{
2682 grab_device = av_strdup(arg);
2683}
2684
b29f97d1 2685static void opt_video_channel(const char *arg)
a5df11ab
FB
2686{
2687 video_channel = strtol(arg, NULL, 0);
2688}
2689
e3ee3283
AB
2690static void opt_video_standard(const char *arg)
2691{
2692 video_standard = av_strdup(arg);
2693}
2694
b29f97d1 2695static void opt_audio_device(const char *arg)
85f07f22 2696{
e9a9e0c2 2697 audio_device = av_strdup(arg);
85f07f22
FB
2698}
2699
cf7fc795
FB
2700static void opt_codec(int *pstream_copy, int *pcodec_id,
2701 int codec_type, const char *arg)
85f07f22
FB
2702{
2703 AVCodec *p;
2704
1629626f 2705 if (!strcmp(arg, "copy")) {
cf7fc795 2706 *pstream_copy = 1;
85f07f22 2707 } else {
1629626f
FB
2708 p = first_avcodec;
2709 while (p) {
cf7fc795 2710 if (!strcmp(p->name, arg) && p->type == codec_type)
1629626f
FB
2711 break;
2712 p = p->next;
2713 }
2714 if (p == NULL) {
cf7fc795 2715 fprintf(stderr, "Unknown codec '%s'\n", arg);
1629626f
FB
2716 exit(1);
2717 } else {
cf7fc795 2718 *pcodec_id = p->id;
1629626f 2719 }
85f07f22
FB
2720 }
2721}
2722
cf7fc795
FB
2723static void opt_audio_codec(const char *arg)
2724{
2725 opt_codec(&audio_stream_copy, &audio_codec_id, CODEC_TYPE_AUDIO, arg);
2726}
2727
b2a2197e
MN
2728static void opt_audio_tag(const char *arg)
2729{
2730 char *tail;
2731 audio_codec_tag= strtol(arg, &tail, 0);
2732
2733 if(!tail || *tail)
2734 audio_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2735}
2736
2737static void opt_video_tag(const char *arg)
2738{
2739 char *tail;
2740 video_codec_tag= strtol(arg, &tail, 0);
2741
2742 if(!tail || *tail)
2743 video_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2744}
2745
b29f97d1 2746static void add_frame_hooker(const char *arg)
10d104e4
PG
2747{
2748 int argc = 0;
2749 char *argv[64];
2750 int i;
e9a9e0c2 2751 char *args = av_strdup(arg);
10d104e4 2752
bee0d9e5
CY
2753 using_vhook = 1;
2754
10d104e4
PG
2755 argv[0] = strtok(args, " ");
2756 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
2757 }
2758
2759 i = frame_hook_add(argc, argv);
2760
2761 if (i != 0) {
2762 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
2763 exit(1);
2764 }
2765}
2766
85f07f22
FB
2767const char *motion_str[] = {
2768 "zero",
2769 "full",
2770 "log",
2771 "phods",
7084c149
MN
2772 "epzs",
2773 "x1",
85f07f22
FB
2774 NULL,
2775};
2776
b29f97d1 2777static void opt_motion_estimation(const char *arg)
85f07f22
FB
2778{
2779 const char **p;
2780 p = motion_str;
2781 for(;;) {
2782 if (!*p) {
2783 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
2784 exit(1);
2785 }
2786 if (!strcmp(*p, arg))
2787 break;
2788 p++;
2789 }
101bea5f 2790 me_method = (p - motion_str) + 1;
85f07f22
FB
2791}
2792
b29f97d1 2793static void opt_video_codec(const char *arg)
85f07f22 2794{
cf7fc795
FB
2795 opt_codec(&video_stream_copy, &video_codec_id, CODEC_TYPE_VIDEO, arg);
2796}
85f07f22 2797
cf7fc795
FB
2798static void opt_subtitle_codec(const char *arg)
2799{
2800 opt_codec(&subtitle_stream_copy, &subtitle_codec_id, CODEC_TYPE_SUBTITLE, arg);
85f07f22
FB
2801}
2802
b29f97d1 2803static void opt_map(const char *arg)
85f07f22
FB
2804{
2805 AVStreamMap *m;
2806 const char *p;
2807
2808 p = arg;
2809 m = &stream_maps[nb_stream_maps++];
2810
2811 m->file_index = strtol(arg, (char **)&p, 0);
2812 if (*p)
2813 p++;
a5dc85ef
J
2814
2815 m->stream_index = strtol(p, (char **)&p, 0);
b4a3389e
WG
2816 if (*p) {
2817 p++;
2818 m->sync_file_index = strtol(p, (char **)&p, 0);
2819 if (*p)
2820 p++;
2821 m->sync_stream_index = strtol(p, (char **)&p, 0);
2822 } else {
2823 m->sync_file_index = m->file_index;
2824 m->sync_stream_index = m->stream_index;
2825 }
85f07f22
FB
2826}
2827
0a38bafd
PB
2828static void opt_map_meta_data(const char *arg)
2829{
2830 AVMetaDataMap *m;
2831 const char *p;
115329f1 2832
0a38bafd
PB
2833 p = arg;
2834 m = &meta_data_maps[nb_meta_data_maps++];
2835
2836 m->out_file = strtol(arg, (char **)&p, 0);
2837 if (*p)
2838 p++;
2839
2840 m->in_file = strtol(p, (char **)&p, 0);
2841}
2842
b29f97d1 2843static void opt_recording_time(const char *arg)
85f07f22
FB
2844{
2845 recording_time = parse_date(arg, 1);
2846}
2847
8831db5c
MN
2848static void opt_start_time(const char *arg)
2849{
2850 start_time = parse_date(arg, 1);
2851}
2852
4568325a
RS
2853static void opt_rec_timestamp(const char *arg)
2854{
2855 rec_timestamp = parse_date(arg, 0) / 1000000;
2856}
2857
a6a92a9a
WG
2858static void opt_input_ts_offset(const char *arg)
2859{
2860 input_ts_offset = parse_date(arg, 1);
2861}
2862
b29f97d1 2863static void opt_input_file(const char *filename)
85f07f22
FB
2864{
2865 AVFormatContext *ic;
2866 AVFormatParameters params, *ap = &params;
14bea432 2867 int err, i, ret, rfps, rfps_base;
bd1b79a1 2868 int64_t timestamp;
85f07f22 2869
b242baa4
FB
2870 if (!strcmp(filename, "-"))
2871 filename = "pipe:";
2872
115329f1 2873 using_stdin |= !strncmp(filename, "pipe:", 5) ||
d9a916e2
CY
2874 !strcmp( filename, "/dev/stdin" );
2875
85f07f22 2876 /* get default parameters from command line */
79fdaa4c
FB
2877 memset(ap, 0, sizeof(*ap));
2878 ap->sample_rate = audio_sample_rate;
2879 ap->channels = audio_channels;
c0df9d75
MN
2880 ap->time_base.den = frame_rate;
2881 ap->time_base.num = frame_rate_base;
1ff93ffc
TK
2882 ap->width = frame_width + frame_padleft + frame_padright;
2883 ap->height = frame_height + frame_padtop + frame_padbottom;
817b23ff 2884 ap->image_format = image_format;
63167088 2885 ap->pix_fmt = frame_pix_fmt;
2639c651
RS
2886 ap->device = grab_device;
2887 ap->channel = video_channel;
2888 ap->standard = video_standard;
5b6d5596
MN
2889 ap->video_codec_id = video_codec_id;
2890 ap->audio_codec_id = audio_codec_id;
2891 if(pgmyuv_compatibility_hack)
2892 ap->video_codec_id= CODEC_ID_PGMYUV;
79fdaa4c
FB
2893
2894 /* open the input file with generic libav function */
2895 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
85f07f22 2896 if (err < 0) {
79fdaa4c 2897 print_error(filename, err);
85f07f22
FB
2898 exit(1);
2899 }
30bc6613
MN
2900
2901 if(genpts)
2902 ic->flags|= AVFMT_FLAG_GENPTS;
115329f1 2903
79fdaa4c
FB
2904 /* If not enough info to get the stream parameters, we decode the
2905 first frames to get it. (used in mpeg case for example) */
2906 ret = av_find_stream_info(ic);
d8019eb5 2907 if (ret < 0 && verbose >= 0) {
85f07f22
FB
2908 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2909 exit(1);
2910 }
2911
bd1b79a1
MN
2912 timestamp = start_time;
2913 /* add the stream start time */
2914 if (ic->start_time != AV_NOPTS_VALUE)
2915 timestamp += ic->start_time;
2916
254abc2e
FB
2917 /* if seeking requested, we execute it */
2918 if (start_time != 0) {
3ba1438d 2919 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
254abc2e 2920 if (ret < 0) {
115329f1 2921 fprintf(stderr, "%s: could not seek to position %0.3f\n",
254abc2e
FB
2922 filename, (double)timestamp / AV_TIME_BASE);
2923 }
2924 /* reset seek info */
2925 start_time = 0;
2926 }
2927
85f07f22
FB
2928 /* update the current parameters so that they match the one of the input stream */
2929 for(i=0;i<ic->nb_streams;i++) {
637b5326 2930 int j;
01f4895c 2931 AVCodecContext *enc = ic->streams[i]->codec;
2450cff2 2932#if defined(HAVE_THREADS)
c62c07d3
MN
2933 if(thread_count>1)
2934 avcodec_thread_init(enc, thread_count);
2935#endif
2936 enc->thread_count= thread_count;
85f07f22
FB
2937 switch(enc->codec_type) {
2938 case CODEC_TYPE_AUDIO:
637b5326
MN
2939 for(j=0; j<opt_name_count; j++){
2940 AVOption *opt;
2941 double d= av_get_double(avctx_opts, opt_names[j], &opt);
2942 if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2943 av_set_double(enc, opt_names[j], d);
2944 }
e0d2714a 2945 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
85f07f22
FB
2946 audio_channels = enc->channels;
2947 audio_sample_rate = enc->sample_rate;
b4aea108 2948 if(audio_disable)
f3356e9c 2949 ic->streams[i]->discard= AVDISCARD_ALL;
85f07f22
FB
2950 break;
2951 case CODEC_TYPE_VIDEO:
637b5326
MN
2952 for(j=0; j<opt_name_count; j++){
2953 AVOption *opt;
2954 double d= av_get_double(avctx_opts, opt_names[j], &opt);
2955 if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2956 av_set_double(enc, opt_names[j], d);
2957 }
85f07f22
FB
2958 frame_height = enc->height;
2959 frame_width = enc->width;
5ff85f1d 2960 frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
63167088 2961 frame_pix_fmt = enc->pix_fmt;
c0df9d75
MN
2962 rfps = ic->streams[i]->r_frame_rate.num;
2963 rfps_base = ic->streams[i]->r_frame_rate.den;
fe670d09 2964 enc->workaround_bugs = workaround_bugs;
637b5326 2965 if(enc->lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
f4f3223f
MN
2966 if(me_threshold)
2967 enc->debug |= FF_DEBUG_MV;
d7425f59 2968
115329f1 2969 if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
d8019eb5
AD
2970
2971 if (verbose >= 0)
15bc38e5 2972 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
c0df9d75 2973 i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
d8019eb5 2974
15bc38e5 2975 (float)rfps / rfps_base, rfps, rfps_base);
79fdaa4c 2976 }
bf5af568 2977 /* update the current frame rate to match the stream frame rate */
14bea432
MN
2978 frame_rate = rfps;
2979 frame_rate_base = rfps_base;
bdfcbbed
MK
2980
2981 enc->rate_emu = rate_emu;
b4aea108 2982 if(video_disable)
f3356e9c
MN
2983 ic->streams[i]->discard= AVDISCARD_ALL;
2984 else if(video_discard)
2985 ic->streams[i]->discard= video_discard;
85f07f22 2986 break;
254abc2e
FB
2987 case CODEC_TYPE_DATA:
2988 break;
cf7fc795
FB
2989 case CODEC_TYPE_SUBTITLE:
2990 break;
ae38261e
MB
2991 case CODEC_TYPE_UNKNOWN:
2992 break;
51bd4565 2993 default:
c04643a2 2994 av_abort();
85f07f22
FB
2995 }
2996 }
115329f1 2997
85f07f22 2998 input_files[nb_input_files] = ic;
bd1b79a1 2999 input_files_ts_offset[nb_input_files] = input_ts_offset - (copy_ts ? 0 : timestamp);
85f07f22 3000 /* dump the file content */
d8019eb5
AD
3001 if (verbose >= 0)
3002 dump_format(ic, nb_input_files, filename, 0);
3003
85f07f22 3004 nb_input_files++;
79fdaa4c
FB
3005 file_iformat = NULL;
3006 file_oformat = NULL;
817b23ff 3007 image_format = NULL;
bdfcbbed 3008
2639c651
RS
3009 grab_device = NULL;
3010 video_channel = 0;
115329f1 3011
bdfcbbed 3012 rate_emu = 0;
85f07f22
FB
3013}
3014
2639c651
RS
3015static void opt_grab(const char *arg)
3016{
3017 file_iformat = av_find_input_format(arg);
3018 opt_input_file("");
3019}
3020
b29f97d1 3021static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
919f448d
FB
3022{
3023 int has_video, has_audio, i, j;
3024 AVFormatContext *ic;
3025
3026 has_video = 0;
3027 has_audio = 0;
3028 for(j=0;j<nb_input_files;j++) {
3029 ic = input_files[j];
3030 for(i=0;i<ic->nb_streams;i++) {
01f4895c 3031 AVCodecContext *enc = ic->streams[i]->codec;
919f448d
FB
3032 switch(enc->codec_type) {
3033 case CODEC_TYPE_AUDIO:
3034 has_audio = 1;
3035 break;
3036 case CODEC_TYPE_VIDEO:
3037 has_video = 1;
3038 break;
6fb316d5 3039 case CODEC_TYPE_DATA:
ae38261e 3040 case CODEC_TYPE_UNKNOWN:
cf7fc795 3041 case CODEC_TYPE_SUBTITLE:
6fb316d5 3042 break;
51bd4565 3043 default:
c04643a2 3044 av_abort();
919f448d
FB
3045 }
3046 }
3047 }
3048 *has_video_ptr = has_video;
3049 *has_audio_ptr = has_audio;
3050}
3051
cf7fc795 3052static void new_video_stream(AVFormatContext *oc)
85f07f22
FB
3053{
3054 AVStream *st;
cf7fc795
FB
3055 AVCodecContext *video_enc;
3056 int codec_id;
115329f1 3057
cf7fc795
FB
3058 st = av_new_stream(oc, oc->nb_streams);
3059 if (!st) {
3060 fprintf(stderr, "Could not alloc stream\n");
3061 exit(1);
3062 }
3063#if defined(HAVE_THREADS)
3064 if(thread_count>1)
01f4895c 3065 avcodec_thread_init(st->codec, thread_count);
cf7fc795 3066#endif
115329f1 3067
01f4895c 3068 video_enc = st->codec;
115329f1 3069
cf7fc795
FB
3070 if(video_codec_tag)
3071 video_enc->codec_tag= video_codec_tag;
115329f1 3072
90ad92b3 3073 if( (video_global_header&1)
637b5326 3074 || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
cf7fc795 3075 video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
637b5326
MN
3076 avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
3077 }
3078 if(video_global_header&2){
90ad92b3 3079 video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
637b5326
MN
3080 avctx_opts->flags2|= CODEC_FLAG2_LOCAL_HEADER;
3081 }
90ad92b3 3082
cf7fc795
FB
3083 if (video_stream_copy) {
3084 st->stream_copy = 1;
3085 video_enc->codec_type = CODEC_TYPE_VIDEO;
3086 } else {
3087 char *p;
3088 int i;
3089 AVCodec *codec;
115329f1 3090
cf7fc795
FB
3091 codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
3092 if (video_codec_id != CODEC_ID_NONE)
3093 codec_id = video_codec_id;
115329f1 3094
cf7fc795
FB
3095 video_enc->codec_id = codec_id;
3096 codec = avcodec_find_encoder(codec_id);
115329f1 3097
8bbf6db9
MN
3098 for(i=0; i<opt_name_count; i++){
3099 AVOption *opt;
3100 double d= av_get_double(avctx_opts, opt_names[i], &opt);
3101 if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3102 av_set_double(video_enc, opt_names[i], d);
3103 }
115329f1 3104
cf7fc795
FB
3105 video_enc->bit_rate = video_bit_rate;
3106 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
115329f1
DB
3107 video_enc->time_base.den = frame_rate;
3108 video_enc->time_base.num = frame_rate_base;
cf7fc795
FB
3109 if(codec && codec->supported_framerates){
3110 const AVRational *p= codec->supported_framerates;
3111 AVRational req= (AVRational){frame_rate, frame_rate_base};
3112 const AVRational *best=NULL;
3113 AVRational best_error= (AVRational){INT_MAX, 1};
3114 for(; p->den!=0; p++){
3115 AVRational error= av_sub_q(req, *p);
3116 if(error.num <0) error.num *= -1;
3117 if(av_cmp_q(error, best_error) < 0){
3118 best_error= error;
3119 best= p;
3120 }
3121 }
3122 video_enc->time_base.den= best->num;
3123 video_enc->time_base.num= best->den;
3124 }
115329f1 3125
cf7fc795
FB
3126 video_enc->width = frame_width + frame_padright + frame_padleft;
3127 video_enc->height = frame_height + frame_padtop + frame_padbottom;
3128 video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
3129 video_enc->pix_fmt = frame_pix_fmt;
3130
3131 if(codec && codec->pix_fmts){
3132 const enum PixelFormat *p= codec->pix_fmts;
3133 for(; *p!=-1; p++){
3134 if(*p == video_enc->pix_fmt)
3135 break;
3136 }
3137 if(*p == -1)
3138 video_enc->pix_fmt = codec->pix_fmts[0];
3139 }
3140
3141 if (!intra_only)
3142 video_enc->gop_size = gop_size;
3143 else
3144 video_enc->gop_size = 0;
3145 if (video_qscale || same_quality) {
3146 video_enc->flags |= CODEC_FLAG_QSCALE;
115329f1 3147 video_enc->global_quality=
cf7fc795
FB
3148 st->quality = FF_QP2LAMBDA * video_qscale;
3149 }
3150
3151 if(intra_matrix)
3152 video_enc->intra_matrix = intra_matrix;
3153 if(inter_matrix)
3154 video_enc->inter_matrix = inter_matrix;
3155
cf7fc795 3156 video_enc->pre_me = pre_me;
115329f1 3157
cf7fc795
FB
3158 if (b_frames) {
3159 video_enc->max_b_frames = b_frames;
3160 video_enc->b_frame_strategy = b_strategy;
3161 video_enc->b_quant_factor = 2.0;
3162 }
cf7fc795
FB
3163 video_enc->qmin = video_qmin;
3164 video_enc->qmax = video_qmax;
3165 video_enc->lmin = video_lmin;
3166 video_enc->lmax = video_lmax;
3167 video_enc->rc_qsquish = video_qsquish;
cf7fc795
FB
3168 video_enc->mb_lmin = video_mb_lmin;
3169 video_enc->mb_lmax = video_mb_lmax;
3170 video_enc->max_qdiff = video_qdiff;
3171 video_enc->qblur = video_qblur;
3172 video_enc->qcompress = video_qcomp;
3173 video_enc->rc_eq = video_rc_eq;
cf7fc795
FB
3174 video_enc->workaround_bugs = workaround_bugs;
3175 video_enc->thread_count = thread_count;
3176 p= video_rc_override_string;
3177 for(i=0; p; i++){
3178 int start, end, q;
3179 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
3180 if(e!=3){
3181 fprintf(stderr, "error parsing rc_override\n");
3182 exit(1);
3183 }
115329f1
DB
3184 video_enc->rc_override=
3185 av_realloc(video_enc->rc_override,
cf7fc795
FB
3186 sizeof(RcOverride)*(i+1));
3187 video_enc->rc_override[i].start_frame= start;
3188 video_enc->rc_override[i].end_frame = end;
3189 if(q>0){
3190 video_enc->rc_override[i].qscale= q;
3191 video_enc->rc_override[i].quality_factor= 1.0;
3192 }
3193 else{
3194 video_enc->rc_override[i].qscale= 0;
3195 video_enc->rc_override[i].quality_factor= -q/100.0;
3196 }
3197 p= strchr(p, '/');
3198 if(p) p++;
3199 }
3200 video_enc->rc_override_count=i;
3201
3202 video_enc->rc_max_rate = video_rc_max_rate;
3203 video_enc->rc_min_rate = video_rc_min_rate;
3204 video_enc->rc_buffer_size = video_rc_buffer_size;
3205 video_enc->rc_initial_buffer_occupancy = video_rc_buffer_size*3/4;
3206 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
3207 video_enc->rc_initial_cplx= video_rc_initial_cplx;
3208 video_enc->i_quant_factor = video_i_qfactor;
3209 video_enc->b_quant_factor = video_b_qfactor;
3210 video_enc->i_quant_offset = video_i_qoffset;
3211 video_enc->b_quant_offset = video_b_qoffset;
3212 video_enc->intra_quant_bias = video_intra_quant_bias;
3213 video_enc->inter_quant_bias = video_inter_quant_bias;
cf7fc795
FB
3214 video_enc->me_threshold= me_threshold;
3215 video_enc->mb_threshold= mb_threshold;
3216 video_enc->intra_dc_precision= intra_dc_precision - 8;
3217 video_enc->strict_std_compliance = strict;
3218 video_enc->error_rate = error_rate;
cf7fc795
FB
3219 video_enc->scenechange_threshold= sc_threshold;
3220 video_enc->me_range = me_range;
cf7fc795
FB
3221 video_enc->me_penalty_compensation= me_penalty_compensation;
3222 video_enc->frame_skip_threshold= frame_skip_threshold;
3223 video_enc->frame_skip_factor= frame_skip_factor;
3224 video_enc->frame_skip_exp= frame_skip_exp;
cf7fc795
FB
3225
3226 if(packet_size){
3227 video_enc->rtp_mode= 1;
3228 video_enc->rtp_payload_size= packet_size;
3229 }
115329f1 3230
cf7fc795
FB
3231 if (do_psnr)
3232 video_enc->flags|= CODEC_FLAG_PSNR;
115329f1 3233
cf7fc795
FB
3234 video_enc->me_method = me_method;
3235
3236 /* two pass mode */
3237 if (do_pass) {
3238 if (do_pass == 1) {
3239 video_enc->flags |= CODEC_FLAG_PASS1;
3240 } else {
3241 video_enc->flags |= CODEC_FLAG_PASS2;
3242 }
3243 }
3244 }
3245
3246 /* reset some key parameters */
3247 video_disable = 0;
3248 video_codec_id = CODEC_ID_NONE;
3249 video_stream_copy = 0;
3250}
3251
3252static void new_audio_stream(AVFormatContext *oc)
3253{
3254 AVStream *st;
3255 AVCodecContext *audio_enc;
8bbf6db9 3256 int codec_id, i;
115329f1 3257
cf7fc795
FB
3258 st = av_new_stream(oc, oc->nb_streams);
3259 if (!st) {
3260 fprintf(stderr, "Could not alloc stream\n");
3261 exit(1);
3262 }
3263#if defined(HAVE_THREADS)
3264 if(thread_count>1)
01f4895c 3265 avcodec_thread_init(st->codec, thread_count);
cf7fc795 3266#endif
115329f1 3267
01f4895c 3268 audio_enc = st->codec;
cf7fc795 3269 audio_enc->codec_type = CODEC_TYPE_AUDIO;
115329f1 3270
cf7fc795
FB
3271 if(audio_codec_tag)
3272 audio_enc->codec_tag= audio_codec_tag;
115329f1 3273
637b5326 3274 if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
cf7fc795 3275 audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
637b5326
MN
3276 avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
3277 }
cf7fc795
FB
3278 if (audio_stream_copy) {
3279 st->stream_copy = 1;
3280 audio_enc->channels = audio_channels;
3281 } else {
3282 codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_AUDIO);
115329f1 3283
8bbf6db9
MN
3284 for(i=0; i<opt_name_count; i++){
3285 AVOption *opt;
3286 double d= av_get_double(avctx_opts, opt_names[i], &opt);
3287 if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3288 av_set_double(audio_enc, opt_names[i], d);
3289 }
115329f1 3290
cf7fc795
FB
3291 if (audio_codec_id != CODEC_ID_NONE)
3292 codec_id = audio_codec_id;
3293 audio_enc->codec_id = codec_id;
115329f1 3294
cf7fc795 3295 audio_enc->bit_rate = audio_bit_rate;
c57c770d
JR
3296 if (audio_qscale > QSCALE_NONE) {
3297 audio_enc->flags |= CODEC_FLAG_QSCALE;
3298 audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
3299 }
cf7fc795
FB
3300 audio_enc->strict_std_compliance = strict;
3301 audio_enc->thread_count = thread_count;
3302 /* For audio codecs other than AC3 or DTS we limit */
3303 /* the number of coded channels to stereo */
3304 if (audio_channels > 2 && codec_id != CODEC_ID_AC3
3305 && codec_id != CODEC_ID_DTS) {
3306 audio_enc->channels = 2;
3307 } else
3308 audio_enc->channels = audio_channels;
3309 }
3310 audio_enc->sample_rate = audio_sample_rate;
78e03516 3311 audio_enc->time_base= (AVRational){1, audio_sample_rate};
cf7fc795
FB
3312 if (audio_language) {
3313 pstrcpy(st->language, sizeof(st->language), audio_language);
3314 av_free(audio_language);
3315 audio_language = NULL;
3316 }
3317
3318 /* reset some key parameters */
3319 audio_disable = 0;
3320 audio_codec_id = CODEC_ID_NONE;
3321 audio_stream_copy = 0;
3322}
3323
3324static void opt_new_subtitle_stream(void)
3325{
3326 AVFormatContext *oc;
3327 AVStream *st;
3328 AVCodecContext *subtitle_enc;
8bbf6db9 3329 int i;
115329f1 3330
cf7fc795
FB
3331 if (nb_output_files <= 0) {
3332 fprintf(stderr, "At least one output file must be specified\n");
3333 exit(1);
3334 }
3335 oc = output_files[nb_output_files - 1];
3336
3337 st = av_new_stream(oc, oc->nb_streams);
3338 if (!st) {
3339 fprintf(stderr, "Could not alloc stream\n");
3340 exit(1);
3341 }
3342
01f4895c 3343 subtitle_enc = st->codec;
cf7fc795
FB
3344 subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;
3345 if (subtitle_stream_copy) {
3346 st->stream_copy = 1;
3347 } else {
8bbf6db9
MN
3348 for(i=0; i<opt_name_count; i++){
3349 AVOption *opt;
3350 double d= av_get_double(avctx_opts, opt_names[i], &opt);
3351 if(d==d && (opt->flags&AV_OPT_FLAG_SUBTITLE_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3352 av_set_double(subtitle_enc, opt_names[i], d);
3353 }
cf7fc795
FB
3354 subtitle_enc->codec_id = subtitle_codec_id;
3355 }
3356
3357 if (subtitle_language) {
3358 pstrcpy(st->language, sizeof(st->language), subtitle_language);
3359 av_free(subtitle_language);
3360 subtitle_language = NULL;
3361 }
3362
3363 subtitle_codec_id = CODEC_ID_NONE;
3364 subtitle_stream_copy = 0;
3365}
3366
3367static void opt_new_audio_stream(void)
3368{
3369 AVFormatContext *oc;
3370 if (nb_output_files <= 0) {
3371 fprintf(stderr, "At least one output file must be specified\n");
3372 exit(1);
3373 }
3374 oc = output_files[nb_output_files - 1];
3375 new_audio_stream(oc);
3376}
3377
3378static void opt_new_video_stream(void)
3379{
3380 AVFormatContext *oc;
3381 if (nb_output_files <= 0) {
3382 fprintf(stderr, "At least one output file must be specified\n");
3383 exit(1);
3384 }
3385 oc = output_files[nb_output_files - 1];
3386 new_video_stream(oc);
3387}
3388
3389static void opt_output_file(const char *filename)
3390{
3391 AVFormatContext *oc;
3392 int use_video, use_audio, input_has_video, input_has_audio;
817b23ff 3393 AVFormatParameters params, *ap = &params;
85f07f22
FB
3394
3395 if (!strcmp(filename, "-"))
3396 filename = "pipe:";
3397
bc874dae 3398 oc = av_alloc_format_context();
85f07f22 3399
79fdaa4c
FB
3400 if (!file_oformat) {
3401 file_oformat = guess_format(NULL, filename, NULL);
3402 if (!file_oformat) {
3403 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
3404 filename);
3405 exit(1);
3406 }
85f07f22 3407 }
115329f1 3408
79fdaa4c 3409 oc->oformat = file_oformat;
cf7fc795 3410 pstrcpy(oc->filename, sizeof(oc->filename), filename);
85f07f22 3411
115329f1 3412 if (!strcmp(file_oformat->name, "ffm") &&
85f07f22
FB
3413 strstart(filename, "http:", NULL)) {
3414 /* special case for files sent to ffserver: we get the stream
3415 parameters from ffserver */
3416 if (read_ffserver_streams(oc, filename) < 0) {
3417 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
3418 exit(1);
3419 }
3420 } else {
f2d9a9f5
LA
3421 use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_id != CODEC_ID_NONE;
3422 use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_id != CODEC_ID_NONE;
919f448d 3423