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