ffmpeg.c

Go to the documentation of this file.
00001 /*
00002  * FFmpeg main
00003  * Copyright (c) 2000-2003 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "config.h"
00023 #include <ctype.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include <stdlib.h>
00027 #include <errno.h>
00028 #include <signal.h>
00029 #include <limits.h>
00030 #include "libavformat/avformat.h"
00031 #include "libavdevice/avdevice.h"
00032 #include "libswscale/swscale.h"
00033 #include "libavformat/framehook.h"
00034 #include "libavcodec/opt.h"
00035 #include "libavutil/fifo.h"
00036 #include "libavutil/avstring.h"
00037 #include "libavformat/os_support.h"
00038 
00039 #ifdef HAVE_SYS_RESOURCE_H
00040 #include <sys/types.h>
00041 #include <sys/resource.h>
00042 #elif defined(HAVE_GETPROCESSTIMES)
00043 #include <windows.h>
00044 #endif
00045 
00046 #if defined(HAVE_TERMIOS_H)
00047 #include <unistd.h>
00048 #include <fcntl.h>
00049 #include <sys/ioctl.h>
00050 #include <sys/time.h>
00051 #include <termios.h>
00052 #elif defined(HAVE_CONIO_H)
00053 #include <conio.h>
00054 #endif
00055 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
00056 #include <time.h>
00057 
00058 #include "cmdutils.h"
00059 
00060 #undef NDEBUG
00061 #include <assert.h>
00062 
00063 #undef exit
00064 
00065 const char program_name[] = "FFmpeg";
00066 const int program_birth_year = 2000;
00067 
00068 /* select an input stream for an output stream */
00069 typedef struct AVStreamMap {
00070     int file_index;
00071     int stream_index;
00072     int sync_file_index;
00073     int sync_stream_index;
00074 } AVStreamMap;
00075 
00076 /** select an input file for an output file */
00077 typedef struct AVMetaDataMap {
00078     int out_file;
00079     int in_file;
00080 } AVMetaDataMap;
00081 
00082 static const OptionDef options[];
00083 
00084 #define MAX_FILES 20
00085 
00086 static AVFormatContext *input_files[MAX_FILES];
00087 static int64_t input_files_ts_offset[MAX_FILES];
00088 static double input_files_ts_scale[MAX_FILES][MAX_STREAMS];
00089 static int nb_input_files = 0;
00090 
00091 static AVFormatContext *output_files[MAX_FILES];
00092 static int nb_output_files = 0;
00093 
00094 static AVStreamMap stream_maps[MAX_FILES];
00095 static int nb_stream_maps;
00096 
00097 static AVMetaDataMap meta_data_maps[MAX_FILES];
00098 static int nb_meta_data_maps;
00099 
00100 static AVInputFormat *file_iformat;
00101 static AVOutputFormat *file_oformat;
00102 static int frame_width  = 0;
00103 static int frame_height = 0;
00104 static float frame_aspect_ratio = 0;
00105 static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
00106 static int frame_padtop  = 0;
00107 static int frame_padbottom = 0;
00108 static int frame_padleft  = 0;
00109 static int frame_padright = 0;
00110 static int padcolor[3] = {16,128,128}; /* default to black */
00111 static int frame_topBand  = 0;
00112 static int frame_bottomBand = 0;
00113 static int frame_leftBand  = 0;
00114 static int frame_rightBand = 0;
00115 static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
00116 static AVRational frame_rate;
00117 static float video_qscale = 0;
00118 static uint16_t *intra_matrix = NULL;
00119 static uint16_t *inter_matrix = NULL;
00120 #if 0 //experimental, (can be removed)
00121 static float video_rc_qsquish=1.0;
00122 static float video_rc_qmod_amp=0;
00123 static int video_rc_qmod_freq=0;
00124 #endif
00125 static const char *video_rc_override_string=NULL;
00126 static int video_disable = 0;
00127 static int video_discard = 0;
00128 static char *video_codec_name = NULL;
00129 static int video_codec_tag = 0;
00130 static int same_quality = 0;
00131 static int do_deinterlace = 0;
00132 static int top_field_first = -1;
00133 static int me_threshold = 0;
00134 static int intra_dc_precision = 8;
00135 static int loop_input = 0;
00136 static int loop_output = AVFMT_NOOUTPUTLOOP;
00137 static int qp_hist = 0;
00138 
00139 static int intra_only = 0;
00140 static int audio_sample_rate = 44100;
00141 #define QSCALE_NONE -99999
00142 static float audio_qscale = QSCALE_NONE;
00143 static int audio_disable = 0;
00144 static int audio_channels = 1;
00145 static char  *audio_codec_name = NULL;
00146 static int audio_codec_tag = 0;
00147 static char *audio_language = NULL;
00148 
00149 static int subtitle_disable = 0;
00150 static char *subtitle_codec_name = NULL;
00151 static char *subtitle_language = NULL;
00152 
00153 static float mux_preload= 0.5;
00154 static float mux_max_delay= 0.7;
00155 
00156 static int64_t recording_time = INT64_MAX;
00157 static int64_t start_time = 0;
00158 static int64_t rec_timestamp = 0;
00159 static int64_t input_ts_offset = 0;
00160 static int file_overwrite = 0;
00161 static char *str_title = NULL;
00162 static char *str_author = NULL;
00163 static char *str_copyright = NULL;
00164 static char *str_comment = NULL;
00165 static char *str_genre = NULL;
00166 static char *str_album = NULL;
00167 static int do_benchmark = 0;
00168 static int do_hex_dump = 0;
00169 static int do_pkt_dump = 0;
00170 static int do_psnr = 0;
00171 static int do_pass = 0;
00172 static char *pass_logfilename = NULL;
00173 static int audio_stream_copy = 0;
00174 static int video_stream_copy = 0;
00175 static int subtitle_stream_copy = 0;
00176 static int video_sync_method= -1;
00177 static int audio_sync_method= 0;
00178 static float audio_drift_threshold= 0.1;
00179 static int copy_ts= 0;
00180 static int opt_shortest = 0; //
00181 static int video_global_header = 0;
00182 static char *vstats_filename;
00183 static FILE *vstats_file;
00184 static int opt_programid = 0;
00185 
00186 static int rate_emu = 0;
00187 
00188 static int  video_channel = 0;
00189 static char *video_standard;
00190 
00191 static int audio_volume = 256;
00192 
00193 static int using_stdin = 0;
00194 static int using_vhook = 0;
00195 static int verbose = 1;
00196 static int thread_count= 1;
00197 static int q_pressed = 0;
00198 static int64_t video_size = 0;
00199 static int64_t audio_size = 0;
00200 static int64_t extra_size = 0;
00201 static int nb_frames_dup = 0;
00202 static int nb_frames_drop = 0;
00203 static int input_sync;
00204 static uint64_t limit_filesize = 0; //
00205 
00206 static int pgmyuv_compatibility_hack=0;
00207 static float dts_delta_threshold = 10;
00208 
00209 static unsigned int sws_flags = SWS_BICUBIC;
00210 
00211 static const char **opt_names;
00212 static int opt_name_count;
00213 static AVCodecContext *avctx_opts[CODEC_TYPE_NB];
00214 static AVFormatContext *avformat_opts;
00215 static struct SwsContext *sws_opts;
00216 static int64_t timer_start;
00217 
00218 static AVBitStreamFilterContext *video_bitstream_filters=NULL;
00219 static AVBitStreamFilterContext *audio_bitstream_filters=NULL;
00220 static AVBitStreamFilterContext *subtitle_bitstream_filters=NULL;
00221 static AVBitStreamFilterContext *bitstream_filters[MAX_FILES][MAX_STREAMS];
00222 
00223 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
00224 
00225 struct AVInputStream;
00226 
00227 typedef struct AVOutputStream {
00228     int file_index;          /* file index */
00229     int index;               /* stream index in the output file */
00230     int source_index;        /* AVInputStream index */
00231     AVStream *st;            /* stream in the output file */
00232     int encoding_needed;     /* true if encoding needed for this stream */
00233     int frame_number;
00234     /* input pts and corresponding output pts
00235        for A/V sync */
00236     //double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
00237     struct AVInputStream *sync_ist; /* input stream to sync against */
00238     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
00239     /* video only */
00240     int video_resample;
00241     AVFrame pict_tmp;      /* temporary image for resampling */
00242     struct SwsContext *img_resample_ctx; /* for image resampling */
00243     int resample_height;
00244 
00245     int video_crop;
00246     int topBand;             /* cropping area sizes */
00247     int leftBand;
00248 
00249     int video_pad;
00250     int padtop;              /* padding area sizes */
00251     int padbottom;
00252     int padleft;
00253     int padright;
00254 
00255     /* audio only */
00256     int audio_resample;
00257     ReSampleContext *resample; /* for audio resampling */
00258     AVFifoBuffer fifo;     /* for compression: one audio fifo per codec */
00259     FILE *logfile;
00260 } AVOutputStream;
00261 
00262 typedef struct AVInputStream {
00263     int file_index;
00264     int index;
00265     AVStream *st;
00266     int discard;             /* true if stream data should be discarded */
00267     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
00268     int64_t sample_index;      /* current sample */
00269 
00270     int64_t       start;     /* time when read started */
00271     unsigned long frame;     /* current frame */
00272     int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
00273                                 is not defined */
00274     int64_t       pts;       /* current pts */
00275     int is_start;            /* is 1 at the start and after a discontinuity */
00276 } AVInputStream;
00277 
00278 typedef struct AVInputFile {
00279     int eof_reached;      /* true if eof reached */
00280     int ist_index;        /* index of first stream in ist_table */
00281     int buffer_size;      /* current total buffer size */
00282     int nb_streams;       /* nb streams we are aware of */
00283 } AVInputFile;
00284 
00285 #ifdef HAVE_TERMIOS_H
00286 
00287 /* init terminal so that we can grab keys */
00288 static struct termios oldtty;
00289 #endif
00290 
00291 static void term_exit(void)
00292 {
00293 #ifdef HAVE_TERMIOS_H
00294     tcsetattr (0, TCSANOW, &oldtty);
00295 #endif
00296 }
00297 
00298 static volatile sig_atomic_t received_sigterm = 0;
00299 
00300 static void
00301 sigterm_handler(int sig)
00302 {
00303     received_sigterm = sig;
00304     term_exit();
00305 }
00306 
00307 static void term_init(void)
00308 {
00309 #ifdef HAVE_TERMIOS_H
00310     struct termios tty;
00311 
00312     tcgetattr (0, &tty);
00313     oldtty = tty;
00314 
00315     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
00316                           |INLCR|IGNCR|ICRNL|IXON);
00317     tty.c_oflag |= OPOST;
00318     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
00319     tty.c_cflag &= ~(CSIZE|PARENB);
00320     tty.c_cflag |= CS8;
00321     tty.c_cc[VMIN] = 1;
00322     tty.c_cc[VTIME] = 0;
00323 
00324     tcsetattr (0, TCSANOW, &tty);
00325     signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
00326 #endif
00327 
00328     signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).  */
00329     signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
00330     /*
00331     register a function to be called at normal program termination
00332     */
00333     atexit(term_exit);
00334 #ifdef CONFIG_BEOS_NETSERVER
00335     fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
00336 #endif
00337 }
00338 
00339 /* read a key without blocking */
00340 static int read_key(void)
00341 {
00342 #if defined(HAVE_TERMIOS_H)
00343     int n = 1;
00344     unsigned char ch;
00345 #ifndef CONFIG_BEOS_NETSERVER
00346     struct timeval tv;
00347     fd_set rfds;
00348 
00349     FD_ZERO(&rfds);
00350     FD_SET(0, &rfds);
00351     tv.tv_sec = 0;
00352     tv.tv_usec = 0;
00353     n = select(1, &rfds, NULL, NULL, &tv);
00354 #endif
00355     if (n > 0) {
00356         n = read(0, &ch, 1);
00357         if (n == 1)
00358             return ch;
00359 
00360         return n;
00361     }
00362 #elif defined(HAVE_CONIO_H)
00363     if(kbhit())
00364         return(getch());
00365 #endif
00366     return -1;
00367 }
00368 
00369 static int decode_interrupt_cb(void)
00370 {
00371     return q_pressed || (q_pressed = read_key() == 'q');
00372 }
00373 
00374 static int av_exit(int ret)
00375 {
00376     int i;
00377 
00378     /* close files */
00379     for(i=0;i<nb_output_files;i++) {
00380         /* maybe av_close_output_file ??? */
00381         AVFormatContext *s = output_files[i];
00382         int j;
00383         if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
00384             url_fclose(s->pb);
00385         for(j=0;j<s->nb_streams;j++) {
00386             av_free(s->streams[j]->codec);
00387             av_free(s->streams[j]);
00388         }
00389         av_free(s);
00390     }
00391     for(i=0;i<nb_input_files;i++)
00392         av_close_input_file(input_files[i]);
00393 
00394     av_free(intra_matrix);
00395     av_free(inter_matrix);
00396 
00397     if (vstats_file)
00398         fclose(vstats_file);
00399     av_free(vstats_filename);
00400 
00401     av_free(opt_names);
00402 
00403     av_free(video_codec_name);
00404     av_free(audio_codec_name);
00405     av_free(subtitle_codec_name);
00406 
00407     av_free(video_standard);
00408 
00409 #ifdef CONFIG_POWERPC_PERF
00410     extern void powerpc_display_perf_report(void);
00411     powerpc_display_perf_report();
00412 #endif /* CONFIG_POWERPC_PERF */
00413 
00414     if (received_sigterm) {
00415         fprintf(stderr,
00416             "Received signal %d: terminating.\n",
00417             (int) received_sigterm);
00418         exit (255);
00419     }
00420 
00421     exit(ret); /* not all OS-es handle main() return value */
00422     return ret;
00423 }
00424 
00425 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
00426 {
00427     int i, err;
00428     AVFormatContext *ic;
00429     int nopts = 0;
00430 
00431     err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
00432     if (err < 0)
00433         return err;
00434     /* copy stream format */
00435     s->nb_streams = ic->nb_streams;
00436     for(i=0;i<ic->nb_streams;i++) {
00437         AVStream *st;
00438 
00439         // FIXME: a more elegant solution is needed
00440         st = av_mallocz(sizeof(AVStream));
00441         memcpy(st, ic->streams[i], sizeof(AVStream));
00442         st->codec = avcodec_alloc_context();
00443         memcpy(st->codec, ic->streams[i]->codec, sizeof(AVCodecContext));
00444         s->streams[i] = st;
00445 
00446         if (st->codec->codec_type == CODEC_TYPE_AUDIO && audio_stream_copy)
00447             st->stream_copy = 1;
00448         else if (st->codec->codec_type == CODEC_TYPE_VIDEO && video_stream_copy)
00449             st->stream_copy = 1;
00450 
00451         if(!st->codec->thread_count)
00452             st->codec->thread_count = 1;
00453         if(st->codec->thread_count>1)
00454             avcodec_thread_init(st->codec, st->codec->thread_count);
00455 
00456         if(st->codec->flags & CODEC_FLAG_BITEXACT)
00457             nopts = 1;
00458     }
00459 
00460     if (!nopts)
00461         s->timestamp = av_gettime();
00462 
00463     av_close_input_file(ic);
00464     return 0;
00465 }
00466 
00467 static double
00468 get_sync_ipts(const AVOutputStream *ost)
00469 {
00470     const AVInputStream *ist = ost->sync_ist;
00471     return (double)(ist->pts - start_time)/AV_TIME_BASE;
00472 }
00473 
00474 static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
00475     int ret;
00476 
00477     while(bsfc){
00478         AVPacket new_pkt= *pkt;
00479         int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
00480                                           &new_pkt.data, &new_pkt.size,
00481                                           pkt->data, pkt->size,
00482                                           pkt->flags & PKT_FLAG_KEY);
00483         if(a>0){
00484             av_free_packet(pkt);
00485             new_pkt.destruct= av_destruct_packet;
00486         } else if(a<0){
00487             fprintf(stderr, "%s failed for stream %d, codec %s",
00488                     bsfc->filter->name, pkt->stream_index,
00489                     avctx->codec ? avctx->codec->name : "copy");
00490             print_error("", a);
00491         }
00492         *pkt= new_pkt;
00493 
00494         bsfc= bsfc->next;
00495     }
00496 
00497     ret= av_interleaved_write_frame(s, pkt);
00498     if(ret < 0){
00499         print_error("av_interleaved_write_frame()", ret);
00500         av_exit(1);
00501     }
00502 }
00503 
00504 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
00505 
00506 static void do_audio_out(AVFormatContext *s,
00507                          AVOutputStream *ost,
00508                          AVInputStream *ist,
00509                          unsigned char *buf, int size)
00510 {
00511     uint8_t *buftmp;
00512     static uint8_t *audio_buf = NULL;
00513     static uint8_t *audio_out = NULL;
00514     const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
00515 
00516     int size_out, frame_bytes, ret;
00517     AVCodecContext *enc= ost->st->codec;
00518     AVCodecContext *dec= ist->st->codec;
00519 
00520     /* SC: dynamic allocation of buffers */
00521     if (!audio_buf)
00522         audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
00523     if (!audio_out)
00524         audio_out = av_malloc(audio_out_size);
00525     if (!audio_buf || !audio_out)
00526         return;               /* Should signal an error ! */
00527 
00528     if (enc->channels != dec->channels)
00529         ost->audio_resample = 1;
00530 
00531     if (ost->audio_resample && !ost->resample) {
00532         ost->resample = audio_resample_init(enc->channels,    dec->channels,
00533                                             enc->sample_rate, dec->sample_rate);
00534         if (!ost->resample) {
00535             fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
00536                     dec->channels, dec->sample_rate,
00537                     enc->channels, enc->sample_rate);
00538             av_exit(1);
00539         }
00540     }
00541 
00542     if(audio_sync_method){
00543         double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
00544                 - av_fifo_size(&ost->fifo)/(ost->st->codec->channels * 2);
00545         double idelta= delta*ist->st->codec->sample_rate / enc->sample_rate;
00546         int byte_delta= ((int)idelta)*2*ist->st->codec->channels;
00547 
00548         //FIXME resample delay
00549         if(fabs(delta) > 50){
00550             if(ist->is_start || fabs(delta) > audio_drift_threshold*enc->sample_rate){
00551                 if(byte_delta < 0){
00552                     byte_delta= FFMAX(byte_delta, -size);
00553                     size += byte_delta;
00554                     buf  -= byte_delta;
00555                     if(verbose > 2)
00556                         fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
00557                     if(!size)
00558                         return;
00559                     ist->is_start=0;
00560                 }else{
00561                     static uint8_t *input_tmp= NULL;
00562                     input_tmp= av_realloc(input_tmp, byte_delta + size);
00563 
00564                     if(byte_delta + size <= MAX_AUDIO_PACKET_SIZE)
00565                         ist->is_start=0;
00566                     else
00567                         byte_delta= MAX_AUDIO_PACKET_SIZE - size;
00568 
00569                     memset(input_tmp, 0, byte_delta);
00570                     memcpy(input_tmp + byte_delta, buf, size);
00571                     buf= input_tmp;
00572                     size += byte_delta;
00573                     if(verbose > 2)
00574                         fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
00575                 }
00576             }else if(audio_sync_method>1){
00577                 int comp= av_clip(delta, -audio_sync_method, audio_sync_method);
00578                 assert(ost->audio_resample);
00579                 if(verbose > 2)
00580                     fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
00581 //                fprintf(stderr, "drift:%f len:%d opts:%"PRId64" ipts:%"PRId64" fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), av_fifo_size(&ost->fifo)/(ost->st->codec->channels * 2));
00582                 av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
00583             }
00584         }
00585     }else
00586         ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
00587                         - av_fifo_size(&ost->fifo)/(ost->st->codec->channels * 2); //FIXME wrong
00588 
00589     if (ost->audio_resample) {
00590         buftmp = audio_buf;
00591         size_out = audio_resample(ost->resample,
00592                                   (short *)buftmp, (short *)buf,
00593                                   size / (ist->st->codec->channels * 2));
00594         size_out = size_out * enc->channels * 2;
00595     } else {
00596         buftmp = buf;
00597         size_out = size;
00598     }
00599 
00600     /* now encode as many frames as possible */
00601     if (enc->frame_size > 1) {
00602         /* output resampled raw samples */
00603         av_fifo_realloc(&ost->fifo, av_fifo_size(&ost->fifo) + size_out);
00604         av_fifo_generic_write(&ost->fifo, buftmp, size_out, NULL);
00605 
00606         frame_bytes = enc->frame_size * 2 * enc->channels;
00607 
00608         while (av_fifo_size(&ost->fifo) >= frame_bytes) {
00609             AVPacket pkt;
00610             av_init_packet(&pkt);
00611 
00612             av_fifo_read(&ost->fifo, audio_buf, frame_bytes);
00613 
00614             //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
00615 
00616             ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
00617                                        (short *)audio_buf);
00618             audio_size += ret;
00619             pkt.stream_index= ost->index;
00620             pkt.data= audio_out;
00621             pkt.size= ret;
00622             if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
00623                 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
00624             pkt.flags |= PKT_FLAG_KEY;
00625             write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
00626 
00627             ost->sync_opts += enc->frame_size;
00628         }
00629     } else {
00630         AVPacket pkt;
00631         av_init_packet(&pkt);
00632 
00633         ost->sync_opts += size_out / (2 * enc->channels);
00634 
00635         /* output a pcm frame */
00636         /* XXX: change encoding codec API to avoid this ? */
00637         switch(enc->codec->id) {
00638         case CODEC_ID_PCM_S32LE:
00639         case CODEC_ID_PCM_S32BE:
00640         case CODEC_ID_PCM_U32LE:
00641         case CODEC_ID_PCM_U32BE:
00642             size_out = size_out << 1;
00643             break;
00644         case CODEC_ID_PCM_S24LE:
00645         case CODEC_ID_PCM_S24BE:
00646         case CODEC_ID_PCM_U24LE:
00647         case CODEC_ID_PCM_U24BE:
00648         case CODEC_ID_PCM_S24DAUD:
00649             size_out = size_out / 2 * 3;
00650             break;
00651         case CODEC_ID_PCM_S16LE:
00652         case CODEC_ID_PCM_S16BE:
00653         case CODEC_ID_PCM_U16LE:
00654         case CODEC_ID_PCM_U16BE:
00655             break;
00656         default:
00657             size_out = size_out >> 1;
00658             break;
00659         }
00660         //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
00661         ret = avcodec_encode_audio(enc, audio_out, size_out,
00662                                    (short *)buftmp);
00663         audio_size += ret;
00664         pkt.stream_index= ost->index;
00665         pkt.data= audio_out;
00666         pkt.size= ret;
00667         if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
00668             pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
00669         pkt.flags |= PKT_FLAG_KEY;
00670         write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
00671     }
00672 }
00673 
00674 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
00675 {
00676     AVCodecContext *dec;
00677     AVPicture *picture2;
00678     AVPicture picture_tmp;
00679     uint8_t *buf = 0;
00680 
00681     dec = ist->st->codec;
00682 
00683     /* deinterlace : must be done before any resize */
00684     if (do_deinterlace || using_vhook) {
00685         int size;
00686 
00687         /* create temporary picture */
00688         size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
00689         buf = av_malloc(size);
00690         if (!buf)
00691             return;
00692 
00693         picture2 = &picture_tmp;
00694         avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
00695 
00696         if (do_deinterlace){
00697             if(avpicture_deinterlace(picture2, picture,
00698                                      dec->pix_fmt, dec->width, dec->height) < 0) {
00699                 /* if error, do not deinterlace */
00700                 av_free(buf);
00701                 buf = NULL;
00702                 picture2 = picture;
00703             }
00704         } else {
00705             av_picture_copy(picture2, picture, dec->pix_fmt, dec->width, dec->height);
00706         }
00707     } else {
00708         picture2 = picture;
00709     }
00710 
00711     if (ENABLE_VHOOK)
00712         frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height,
00713                            1000000 * ist->pts / AV_TIME_BASE);
00714 
00715     if (picture != picture2)
00716         *picture = *picture2;
00717     *bufp = buf;
00718 }
00719 
00720 /* we begin to correct av delay at this threshold */
00721 #define AV_DELAY_MAX 0.100
00722 
00723 static void do_subtitle_out(AVFormatContext *s,
00724                             AVOutputStream *ost,
00725                             AVInputStream *ist,
00726                             AVSubtitle *sub,
00727                             int64_t pts)
00728 {
00729     static uint8_t *subtitle_out = NULL;
00730     int subtitle_out_max_size = 65536;
00731     int subtitle_out_size, nb, i;
00732     AVCodecContext *enc;
00733     AVPacket pkt;
00734 
00735     if (pts == AV_NOPTS_VALUE) {
00736         fprintf(stderr, "Subtitle packets must have a pts\n");
00737         return;
00738     }
00739 
00740     enc = ost->st->codec;
00741 
00742     if (!subtitle_out) {
00743         subtitle_out = av_malloc(subtitle_out_max_size);
00744     }
00745 
00746     /* Note: DVB subtitle need one packet to draw them and one other
00747        packet to clear them */
00748     /* XXX: signal it in the codec context ? */
00749     if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
00750         nb = 2;
00751     else
00752         nb = 1;
00753 
00754     for(i = 0; i < nb; i++) {
00755         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
00756                                                     subtitle_out_max_size, sub);
00757 
00758         av_init_packet(&pkt);
00759         pkt.stream_index = ost->index;
00760         pkt.data = subtitle_out;
00761         pkt.size = subtitle_out_size;
00762         pkt.pts = av_rescale_q(pts, ist->st->time_base, ost->st->time_base);
00763         if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
00764             /* XXX: the pts correction is handled here. Maybe handling
00765                it in the codec would be better */
00766             if (i == 0)
00767                 pkt.pts += 90 * sub->start_display_time;
00768             else
00769                 pkt.pts += 90 * sub->end_display_time;
00770         }
00771         write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
00772     }
00773 }
00774 
00775 static int bit_buffer_size= 1024*256;
00776 static uint8_t *bit_buffer= NULL;
00777 
00778 static void do_video_out(AVFormatContext *s,
00779                          AVOutputStream *ost,
00780                          AVInputStream *ist,
00781                          AVFrame *in_picture,
00782                          int *frame_size)
00783 {
00784     int nb_frames, i, ret;
00785     AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
00786     AVFrame picture_crop_temp, picture_pad_temp;
00787     AVCodecContext *enc, *dec;
00788 
00789     avcodec_get_frame_defaults(&picture_crop_temp);
00790     avcodec_get_frame_defaults(&picture_pad_temp);
00791 
00792     enc = ost->st->codec;
00793     dec = ist->st->codec;
00794