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