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