+
+ st = av_new_stream(oc, oc->nb_streams);
+ if (!st) {
+ fprintf(stderr, "Could not alloc stream\n");
+ exit(1);
+ }
+#if defined(HAVE_THREADS)
+ if(thread_count>1)
+ avcodec_thread_init(st->codec, thread_count);
+#endif
+
+ video_enc = st->codec;
+
+ if(video_codec_tag)
+ video_enc->codec_tag= video_codec_tag;
+
+ if( (video_global_header&1)
+ || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
+ video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
+ }
+ if(video_global_header&2){
+ video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
+ avctx_opts->flags2|= CODEC_FLAG2_LOCAL_HEADER;
+ }
+
+ if (video_stream_copy) {
+ st->stream_copy = 1;
+ video_enc->codec_type = CODEC_TYPE_VIDEO;
+ } else {
+ char *p;
+ int i;
+ AVCodec *codec;
+
+ codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
+ if (video_codec_id != CODEC_ID_NONE)
+ codec_id = video_codec_id;
+
+ video_enc->codec_id = codec_id;
+ codec = avcodec_find_encoder(codec_id);
+
+ for(i=0; i<opt_name_count; i++){
+ AVOption *opt;
+ double d= av_get_double(avctx_opts, opt_names[i], &opt);
+ if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
+ av_set_double(video_enc, opt_names[i], d);
+ }
+
+ video_enc->bit_rate = video_bit_rate;
+ video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
+ video_enc->time_base.den = frame_rate;
+ video_enc->time_base.num = frame_rate_base;
+ if(codec && codec->supported_framerates){
+ const AVRational *p= codec->supported_framerates;
+ AVRational req= (AVRational){frame_rate, frame_rate_base};
+ const AVRational *best=NULL;
+ AVRational best_error= (AVRational){INT_MAX, 1};
+ for(; p->den!=0; p++){
+ AVRational error= av_sub_q(req, *p);
+ if(error.num <0) error.num *= -1;
+ if(av_cmp_q(error, best_error) < 0){
+ best_error= error;
+ best= p;
+ }
+ }
+ video_enc->time_base.den= best->num;
+ video_enc->time_base.num= best->den;
+ }
+
+ video_enc->width = frame_width + frame_padright + frame_padleft;
+ video_enc->height = frame_height + frame_padtop + frame_padbottom;
+ video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
+ video_enc->pix_fmt = frame_pix_fmt;
+
+ if(codec && codec->pix_fmts){
+ const enum PixelFormat *p= codec->pix_fmts;
+ for(; *p!=-1; p++){
+ if(*p == video_enc->pix_fmt)
+ break;
+ }
+ if(*p == -1)
+ video_enc->pix_fmt = codec->pix_fmts[0];
+ }
+
+ if (!intra_only)
+ video_enc->gop_size = gop_size;
+ else
+ video_enc->gop_size = 0;
+ if (video_qscale || same_quality) {
+ video_enc->flags |= CODEC_FLAG_QSCALE;
+ video_enc->global_quality=
+ st->quality = FF_QP2LAMBDA * video_qscale;
+ }
+
+ if(intra_matrix)
+ video_enc->intra_matrix = intra_matrix;
+ if(inter_matrix)
+ video_enc->inter_matrix = inter_matrix;
+
+ video_enc->pre_me = pre_me;
+
+ if (b_frames) {
+ video_enc->max_b_frames = b_frames;
+ video_enc->b_quant_factor = 2.0;
+ }
+ video_enc->qmin = video_qmin;
+ video_enc->qmax = video_qmax;
+ video_enc->lmin = video_lmin;
+ video_enc->lmax = video_lmax;
+ video_enc->rc_qsquish = video_qsquish;
+ video_enc->mb_lmin = video_mb_lmin;
+ video_enc->mb_lmax = video_mb_lmax;
+ video_enc->max_qdiff = video_qdiff;
+ video_enc->qblur = video_qblur;
+ video_enc->qcompress = video_qcomp;
+ video_enc->rc_eq = video_rc_eq;
+ video_enc->workaround_bugs = workaround_bugs;
+ video_enc->thread_count = thread_count;
+ p= video_rc_override_string;
+ for(i=0; p; i++){
+ int start, end, q;
+ int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
+ if(e!=3){
+ fprintf(stderr, "error parsing rc_override\n");
+ exit(1);
+ }
+ video_enc->rc_override=
+ av_realloc(video_enc->rc_override,
+ sizeof(RcOverride)*(i+1));
+ video_enc->rc_override[i].start_frame= start;
+ video_enc->rc_override[i].end_frame = end;
+ if(q>0){
+ video_enc->rc_override[i].qscale= q;
+ video_enc->rc_override[i].quality_factor= 1.0;
+ }
+ else{
+ video_enc->rc_override[i].qscale= 0;
+ video_enc->rc_override[i].quality_factor= -q/100.0;
+ }
+ p= strchr(p, '/');
+ if(p) p++;
+ }
+ video_enc->rc_override_count=i;
+
+ video_enc->rc_max_rate = video_rc_max_rate;
+ video_enc->rc_min_rate = video_rc_min_rate;
+ video_enc->rc_buffer_size = video_rc_buffer_size;
+ video_enc->rc_initial_buffer_occupancy = video_rc_buffer_size*3/4;
+ video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
+ video_enc->rc_initial_cplx= video_rc_initial_cplx;
+ video_enc->i_quant_factor = video_i_qfactor;
+ video_enc->b_quant_factor = video_b_qfactor;
+ video_enc->i_quant_offset = video_i_qoffset;
+ video_enc->b_quant_offset = video_b_qoffset;
+ video_enc->intra_quant_bias = video_intra_quant_bias;
+ video_enc->inter_quant_bias = video_inter_quant_bias;
+ video_enc->me_threshold= me_threshold;
+ video_enc->mb_threshold= mb_threshold;
+ video_enc->intra_dc_precision= intra_dc_precision - 8;
+ video_enc->strict_std_compliance = strict;
+ video_enc->error_rate = error_rate;
+ video_enc->scenechange_threshold= sc_threshold;
+ video_enc->me_range = me_range;
+ video_enc->me_penalty_compensation= me_penalty_compensation;
+ video_enc->frame_skip_threshold= frame_skip_threshold;
+ video_enc->frame_skip_factor= frame_skip_factor;
+ video_enc->frame_skip_exp= frame_skip_exp;
+
+ if(packet_size){
+ video_enc->rtp_mode= 1;
+ video_enc->rtp_payload_size= packet_size;
+ }
+
+ if (do_psnr)
+ video_enc->flags|= CODEC_FLAG_PSNR;
+
+ video_enc->me_method = me_method;
+
+ /* two pass mode */
+ if (do_pass) {
+ if (do_pass == 1) {
+ video_enc->flags |= CODEC_FLAG_PASS1;
+ } else {
+ video_enc->flags |= CODEC_FLAG_PASS2;
+ }
+ }
+ }
+
+ /* reset some key parameters */
+ video_disable = 0;
+ video_codec_id = CODEC_ID_NONE;
+ video_stream_copy = 0;
+}
+
+static void new_audio_stream(AVFormatContext *oc)
+{
+ AVStream *st;
+ AVCodecContext *audio_enc;
+ int codec_id, i;
+
+ st = av_new_stream(oc, oc->nb_streams);
+ if (!st) {
+ fprintf(stderr, "Could not alloc stream\n");
+ exit(1);
+ }
+#if defined(HAVE_THREADS)
+ if(thread_count>1)
+ avcodec_thread_init(st->codec, thread_count);
+#endif
+
+ audio_enc = st->codec;
+ audio_enc->codec_type = CODEC_TYPE_AUDIO;
+
+ if(audio_codec_tag)
+ audio_enc->codec_tag= audio_codec_tag;
+
+ if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
+ audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
+ }
+ if (audio_stream_copy) {
+ st->stream_copy = 1;
+ audio_enc->channels = audio_channels;
+ } else {
+ codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_AUDIO);
+
+ for(i=0; i<opt_name_count; i++){
+ AVOption *opt;
+ double d= av_get_double(avctx_opts, opt_names[i], &opt);
+ if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
+ av_set_double(audio_enc, opt_names[i], d);
+ }
+
+ if (audio_codec_id != CODEC_ID_NONE)
+ codec_id = audio_codec_id;
+ audio_enc->codec_id = codec_id;
+
+ audio_enc->bit_rate = audio_bit_rate;
+ if (audio_qscale > QSCALE_NONE) {
+ audio_enc->flags |= CODEC_FLAG_QSCALE;
+ audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
+ }
+ audio_enc->strict_std_compliance = strict;
+ audio_enc->thread_count = thread_count;
+ /* For audio codecs other than AC3 or DTS we limit */
+ /* the number of coded channels to stereo */
+ if (audio_channels > 2 && codec_id != CODEC_ID_AC3
+ && codec_id != CODEC_ID_DTS) {
+ audio_enc->channels = 2;
+ } else
+ audio_enc->channels = audio_channels;
+ }
+ audio_enc->sample_rate = audio_sample_rate;
+ audio_enc->time_base= (AVRational){1, audio_sample_rate};
+ if (audio_language) {
+ pstrcpy(st->language, sizeof(st->language), audio_language);
+ av_free(audio_language);
+ audio_language = NULL;
+ }
+
+ /* reset some key parameters */
+ audio_disable = 0;
+ audio_codec_id = CODEC_ID_NONE;
+ audio_stream_copy = 0;
+}
+
+static void opt_new_subtitle_stream(void)
+{
+ AVFormatContext *oc;
+ AVStream *st;
+ AVCodecContext *subtitle_enc;
+ int i;
+
+ if (nb_output_files <= 0) {
+ fprintf(stderr, "At least one output file must be specified\n");
+ exit(1);
+ }
+ oc = output_files[nb_output_files - 1];
+
+ st = av_new_stream(oc, oc->nb_streams);
+ if (!st) {
+ fprintf(stderr, "Could not alloc stream\n");
+ exit(1);
+ }
+
+ subtitle_enc = st->codec;
+ subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;
+ if (subtitle_stream_copy) {
+ st->stream_copy = 1;
+ } else {
+ for(i=0; i<opt_name_count; i++){
+ AVOption *opt;
+ double d= av_get_double(avctx_opts, opt_names[i], &opt);
+ if(d==d && (opt->flags&AV_OPT_FLAG_SUBTITLE_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
+ av_set_double(subtitle_enc, opt_names[i], d);
+ }
+ subtitle_enc->codec_id = subtitle_codec_id;
+ }
+
+ if (subtitle_language) {
+ pstrcpy(st->language, sizeof(st->language), subtitle_language);
+ av_free(subtitle_language);
+ subtitle_language = NULL;
+ }
+
+ subtitle_codec_id = CODEC_ID_NONE;
+ subtitle_stream_copy = 0;
+}
+
+static void opt_new_audio_stream(void)
+{
+ AVFormatContext *oc;
+ if (nb_output_files <= 0) {
+ fprintf(stderr, "At least one output file must be specified\n");
+ exit(1);
+ }
+ oc = output_files[nb_output_files - 1];
+ new_audio_stream(oc);
+}
+
+static void opt_new_video_stream(void)
+{
+ AVFormatContext *oc;
+ if (nb_output_files <= 0) {
+ fprintf(stderr, "At least one output file must be specified\n");
+ exit(1);
+ }
+ oc = output_files[nb_output_files - 1];
+ new_video_stream(oc);
+}
+
+static void opt_output_file(const char *filename)
+{
+ AVFormatContext *oc;
+ int use_video, use_audio, input_has_video, input_has_audio;