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