Skip to content
Snippets Groups Projects
ffmpeg.c 135 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            av_free(ist_table);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        if (ost_table) {
            for(i=0;i<nb_ostreams;i++) {
                ost = ost_table[i];
                if (ost) {
    
                    if (ost->logfile) {
                        fclose(ost->logfile);
                        ost->logfile = NULL;
                    }
    
                    av_fifo_free(&ost->fifo); /* works even if fifo is not
                                                 initialized but set to zero */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    av_free(ost->pict_tmp.data[0]);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    if (ost->video_resample)
    
                        sws_freeContext(ost->img_resample_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    if (ost->audio_resample)
                        audio_resample_close(ost->resample);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    av_free(ost);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            av_free(ost_table);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        return ret;
     fail:
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        goto fail1;
    }
    
    #if 0
    int file_read(const char *filename)
    {
        URLContext *h;
        unsigned char buffer[1024];
        int len, i;
    
        if (url_open(&h, filename, O_RDONLY) < 0) {
            printf("could not open '%s'\n", filename);
            return -1;
        }
        for(;;) {
            len = url_read(h, buffer, sizeof(buffer));
            if (len <= 0)
                break;
            for(i=0;i<len;i++) putchar(buffer[i]);
        }
        url_close(h);
        return 0;
    }
    #endif
    
    
    static void opt_format(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        /* compatibility stuff for pgmyuv */
        if (!strcmp(arg, "pgmyuv")) {
    
            pgmyuv_compatibility_hack=1;
    
            fprintf(stderr, "pgmyuv format is deprecated, use image2\n");
    
        file_iformat = av_find_input_format(arg);
        file_oformat = guess_format(arg, NULL, NULL);
        if (!file_iformat && !file_oformat) {
            fprintf(stderr, "Unknown input or output format: %s\n", arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            exit(1);
        }
    }
    
    
    static void opt_video_rc_eq(char *arg)
    
    {
        video_rc_eq = arg;
    }
    
    
    static void opt_video_rc_override_string(char *arg)
    
    static void opt_me_threshold(const char *arg)
    {
        me_threshold = atoi(arg);
    }
    
    
    static void opt_verbose(const char *arg)
    {
        verbose = atoi(arg);
    
    static void opt_frame_rate(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
    
            fprintf(stderr, "Incorrect frame rate\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_frame_crop_top(const char *arg)
    
        frame_topBand = atoi(arg);
    
        if (frame_topBand < 0) {
            fprintf(stderr, "Incorrect top crop size\n");
            exit(1);
        }
        if ((frame_topBand % 2) != 0) {
            fprintf(stderr, "Top crop size must be a multiple of 2\n");
            exit(1);
        }
        if ((frame_topBand) >= frame_height){
    
            fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_bottom(const char *arg)
    
    {
        frame_bottomBand = atoi(arg);
        if (frame_bottomBand < 0) {
            fprintf(stderr, "Incorrect bottom crop size\n");
            exit(1);
        }
        if ((frame_bottomBand % 2) != 0) {
            fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
    
            fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_left(const char *arg)
    
    {
        frame_leftBand = atoi(arg);
        if (frame_leftBand < 0) {
            fprintf(stderr, "Incorrect left crop size\n");
            exit(1);
        }
        if ((frame_leftBand % 2) != 0) {
            fprintf(stderr, "Left crop size must be a multiple of 2\n");
            exit(1);
        }
        if ((frame_leftBand) >= frame_width){
    
            fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_right(const char *arg)
    
    {
        frame_rightBand = atoi(arg);
        if (frame_rightBand < 0) {
            fprintf(stderr, "Incorrect right crop size\n");
            exit(1);
        }
        if ((frame_rightBand % 2) != 0) {
            fprintf(stderr, "Right crop size must be a multiple of 2\n");
    
            fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_size(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (av_parse_video_frame_size(&frame_width, &frame_height, arg) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fprintf(stderr, "Incorrect frame size\n");
            exit(1);
        }
        if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
            fprintf(stderr, "Frame size must be a multiple of 2\n");
            exit(1);
        }
    }
    
    
    
    #define SCALEBITS 10
    #define ONE_HALF  (1 << (SCALEBITS - 1))
    
    #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
    
    
    #define RGB_TO_Y(r, g, b) \
    ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
      FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
    
    #define RGB_TO_U(r1, g1, b1, shift)\
    (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
         FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
    
    #define RGB_TO_V(r1, g1, b1, shift)\
    (((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
       FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
    
    static void opt_pad_color(const char *arg) {
        /* Input is expected to be six hex digits similar to
           how colors are expressed in html tags (but without the #) */
        int rgb = strtol(arg, NULL, 16);
        int r,g,b;
    
        g = ((rgb >> 8) & 255);
        b = (rgb & 255);
    
        padcolor[0] = RGB_TO_Y(r,g,b);
        padcolor[1] = RGB_TO_U(r,g,b,0);
        padcolor[2] = RGB_TO_V(r,g,b,0);
    }
    
    static void opt_frame_pad_top(const char *arg)
    {
    
        frame_padtop = atoi(arg);
    
        if (frame_padtop < 0) {
            fprintf(stderr, "Incorrect top pad size\n");
            exit(1);
        }
        if ((frame_padtop % 2) != 0) {
            fprintf(stderr, "Top pad size must be a multiple of 2\n");
            exit(1);
        }
    }
    
    static void opt_frame_pad_bottom(const char *arg)
    {
    
        frame_padbottom = atoi(arg);
    
        if (frame_padbottom < 0) {
            fprintf(stderr, "Incorrect bottom pad size\n");
            exit(1);
        }
        if ((frame_padbottom % 2) != 0) {
            fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
            exit(1);
        }
    }
    
    
    static void opt_frame_pad_left(const char *arg)
    {
    
        frame_padleft = atoi(arg);
    
        if (frame_padleft < 0) {
            fprintf(stderr, "Incorrect left pad size\n");
            exit(1);
        }
        if ((frame_padleft % 2) != 0) {
            fprintf(stderr, "Left pad size must be a multiple of 2\n");
            exit(1);
        }
    }
    
    
    static void opt_frame_pad_right(const char *arg)
    {
    
        frame_padright = atoi(arg);
    
        if (frame_padright < 0) {
            fprintf(stderr, "Incorrect right pad size\n");
            exit(1);
        }
        if ((frame_padright % 2) != 0) {
            fprintf(stderr, "Right pad size must be a multiple of 2\n");
            exit(1);
        }
    }
    
    
    void list_pix_fmts(void)
    {
        int i;
        char pix_fmt_str[128];
        for (i=-1; i < PIX_FMT_NB; i++) {
            avcodec_pix_fmt_string (pix_fmt_str, sizeof(pix_fmt_str), i);
            fprintf(stdout, "%s\n", pix_fmt_str);
        }
    }
    
    static void opt_frame_pix_fmt(const char *arg)
    {
    
        if (strcmp(arg, "list"))
            frame_pix_fmt = avcodec_get_pix_fmt(arg);
        else {
            list_pix_fmts();
            exit(0);
        }
    
    static void opt_frame_aspect_ratio(const char *arg)
    {
        int x = 0, y = 0;
        double ar = 0;
        const char *p;
    
        p = strchr(arg, ':');
        if (p) {
            x = strtol(arg, (char **)&arg, 10);
    
            if (arg == p)
                y = strtol(arg+1, (char **)&arg, 10);
            if (x > 0 && y > 0)
                ar = (double)x / (double)y;
    
        } else
            ar = strtod(arg, (char **)&arg);
    
        if (!ar) {
            fprintf(stderr, "Incorrect aspect ratio specification.\n");
    
    static void opt_qscale(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Loren Merritt's avatar
    Loren Merritt committed
        if (video_qscale <= 0 ||
    
    Loren Merritt's avatar
    Loren Merritt committed
            fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            exit(1);
        }
    }
    
    
    static void opt_qdiff(const char *arg)
    
    {
        video_qdiff = atoi(arg);
        if (video_qdiff < 0 ||
            video_qdiff > 31) {
            fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
            exit(1);
        }
    }
    
    
    static void opt_strict(const char *arg)
    
    {
        strict= atoi(arg);
    }
    
    
    static void opt_top_field_first(const char *arg)
    {
        top_field_first= atoi(arg);
    }
    
    
    static void opt_thread_count(const char *arg)
    {
        thread_count= atoi(arg);
    
        if (verbose >= 0)
            fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
    
    static void opt_audio_rate(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        audio_sample_rate = atoi(arg);
    }
    
    
    static void opt_audio_channels(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        audio_channels = atoi(arg);
    }
    
    
    static void opt_video_channel(const char *arg)
    
    static void opt_video_standard(const char *arg)
    {
        video_standard = av_strdup(arg);
    }
    
    
    static void opt_codec(int *pstream_copy, char **pcodec_name,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    static void opt_audio_codec(const char *arg)
    {
    
        opt_codec(&audio_stream_copy, &audio_codec_name, CODEC_TYPE_AUDIO, arg);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    static void opt_audio_tag(const char *arg)
    {
        char *tail;
        audio_codec_tag= strtol(arg, &tail, 0);
    
        if(!tail || *tail)
            audio_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
    }
    
    static void opt_video_tag(const char *arg)
    {
        char *tail;
        video_codec_tag= strtol(arg, &tail, 0);
    
        if(!tail || *tail)
            video_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
    }
    
    
    static void add_frame_hooker(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        char *args = av_strdup(arg);
    
        argv[0] = strtok(args, " ");
        while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
        }
    
        i = frame_hook_add(argc, argv);
    
        if (i != 0) {
            fprintf(stderr, "Failed to add video hook function: %s\n", arg);
            exit(1);
        }
    }
    
    static void opt_video_codec(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        opt_codec(&video_stream_copy, &video_codec_name, CODEC_TYPE_VIDEO, arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    static void opt_subtitle_codec(const char *arg)
    {
    
        opt_codec(&subtitle_stream_copy, &subtitle_codec_name, CODEC_TYPE_SUBTITLE, arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_map(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVStreamMap *m;
        const char *p;
    
        p = arg;
        m = &stream_maps[nb_stream_maps++];
    
        m->file_index = strtol(arg, (char **)&p, 0);
        if (*p)
            p++;
    
    Juanjo's avatar
    Juanjo committed
    
        m->stream_index = strtol(p, (char **)&p, 0);
    
        if (*p) {
            p++;
            m->sync_file_index = strtol(p, (char **)&p, 0);
            if (*p)
                p++;
            m->sync_stream_index = strtol(p, (char **)&p, 0);
        } else {
            m->sync_file_index = m->file_index;
            m->sync_stream_index = m->stream_index;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_map_meta_data(const char *arg)
    {
        AVMetaDataMap *m;
        const char *p;
    
        p = arg;
        m = &meta_data_maps[nb_meta_data_maps++];
    
        m->out_file = strtol(arg, (char **)&p, 0);
        if (*p)
            p++;
    
        m->in_file = strtol(p, (char **)&p, 0);
    }
    
    
    static void opt_recording_time(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        recording_time = parse_date(arg, 1);
    }
    
    
    static void opt_start_time(const char *arg)
    {
        start_time = parse_date(arg, 1);
    }
    
    
    static void opt_rec_timestamp(const char *arg)
    {
        rec_timestamp = parse_date(arg, 0) / 1000000;
    }
    
    
    static void opt_input_ts_offset(const char *arg)
    {
        input_ts_offset = parse_date(arg, 1);
    }
    
    
    static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
    {
        AVCodec *codec;
    
        if(!name)
            return CODEC_ID_NONE;
        codec = encoder ?
            avcodec_find_encoder_by_name(name) :
            avcodec_find_decoder_by_name(name);
        if(!codec) {
            av_log(NULL, AV_LOG_ERROR, "Unknown codec '%s'\n", name);
            exit(1);
        }
        if(codec->type != type) {
            av_log(NULL, AV_LOG_ERROR, "Invalid codec type '%s'\n", name);
            exit(1);
        }
        return codec->id;
    }
    
    
    static void opt_input_file(const char *filename)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVFormatContext *ic;
        AVFormatParameters params, *ap = &params;
    
        int64_t timestamp;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (!strcmp(filename, "-"))
            filename = "pipe:";
    
    
        using_stdin |= !strncmp(filename, "pipe:", 5) ||
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* get default parameters from command line */
    
        ic = av_alloc_format_context();
    
    
        ap->prealloced_context = 1;
    
        ap->sample_rate = audio_sample_rate;
        ap->channels = audio_channels;
    
        ap->time_base.den = frame_rate.num;
        ap->time_base.num = frame_rate.den;
    
        ap->width = frame_width + frame_padleft + frame_padright;
        ap->height = frame_height + frame_padtop + frame_padbottom;
    
        ap->pix_fmt = frame_pix_fmt;
    
        ap->channel = video_channel;
        ap->standard = video_standard;
    
        ap->video_codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 0);
        ap->audio_codec_id = find_codec_or_die(audio_codec_name, CODEC_TYPE_AUDIO, 0);
    
        if(pgmyuv_compatibility_hack)
            ap->video_codec_id= CODEC_ID_PGMYUV;
    
        for(i=0; i<opt_name_count; i++){
    
            double d= av_get_double(avformat_opts, opt_names[i], &opt);
            if(d==d && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
                av_set_double(ic, opt_names[i], d);
        }
    
        /* open the input file with generic libav function */
        err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (err < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            exit(1);
        }
    
        /* If not enough info to get the stream parameters, we decode the
           first frames to get it. (used in mpeg case for example) */
        ret = av_find_stream_info(ic);
    
        if (ret < 0 && verbose >= 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fprintf(stderr, "%s: could not find codec parameters\n", filename);
            exit(1);
        }
    
    
        timestamp = start_time;
        /* add the stream start time */
        if (ic->start_time != AV_NOPTS_VALUE)
            timestamp += ic->start_time;
    
    
        /* if seeking requested, we execute it */
        if (start_time != 0) {
    
            ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
    
                fprintf(stderr, "%s: could not seek to position %0.3f\n",
    
                        filename, (double)timestamp / AV_TIME_BASE);
            }
            /* reset seek info */
            start_time = 0;
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* update the current parameters so that they match the one of the input stream */
        for(i=0;i<ic->nb_streams;i++) {
    
            AVCodecContext *enc = ic->streams[i]->codec;
    
            if(thread_count>1)
                avcodec_thread_init(enc, thread_count);
            enc->thread_count= thread_count;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            switch(enc->codec_type) {
            case CODEC_TYPE_AUDIO:
    
                for(j=0; j<opt_name_count; j++){
    
                    double d= av_get_double(avctx_opts[CODEC_TYPE_AUDIO], opt_names[j], &opt);
    
                    if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
                        av_set_double(enc, opt_names[j], d);
                }
    
                //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                audio_channels = enc->channels;
                audio_sample_rate = enc->sample_rate;
    
                    ic->streams[i]->discard= AVDISCARD_ALL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
            case CODEC_TYPE_VIDEO:
    
                for(j=0; j<opt_name_count; j++){
    
                    double d= av_get_double(avctx_opts[CODEC_TYPE_VIDEO], opt_names[j], &opt);
    
                    if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
                        av_set_double(enc, opt_names[j], d);
                }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                frame_height = enc->height;
                frame_width = enc->width;
    
                frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
                frame_pix_fmt = enc->pix_fmt;
    
                rfps      = ic->streams[i]->r_frame_rate.num;
                rfps_base = ic->streams[i]->r_frame_rate.den;
    
                if(enc->lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
    
                if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
    
                        fprintf(stderr,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
    
                                i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
    
                        (float)rfps / rfps_base, rfps, rfps_base);
    
                /* update the current frame rate to match the stream frame rate */
    
                frame_rate.num = rfps;
                frame_rate.den = rfps_base;
    
                    ic->streams[i]->discard= AVDISCARD_ALL;
                else if(video_discard)
                    ic->streams[i]->discard= video_discard;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
                if(subtitle_disable)
                    ic->streams[i]->discard = AVDISCARD_ALL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        input_files[nb_input_files] = ic;
    
        input_files_ts_offset[nb_input_files] = input_ts_offset - (copy_ts ? 0 : timestamp);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* dump the file content */
    
        if (verbose >= 0)
            dump_format(ic, nb_input_files, filename, 0);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        nb_input_files++;
    
        file_iformat = NULL;
        file_oformat = NULL;
    
        video_channel = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void check_audio_video_sub_inputs(int *has_video_ptr, int *has_audio_ptr,
                                             int *has_subtitle_ptr)
    
        int has_video, has_audio, has_subtitle, i, j;
    
        for(j=0;j<nb_input_files;j++) {
            ic = input_files[j];
            for(i=0;i<ic->nb_streams;i++) {
    
                AVCodecContext *enc = ic->streams[i]->codec;
    
                switch(enc->codec_type) {
                case CODEC_TYPE_AUDIO:
                    has_audio = 1;
                    break;
                case CODEC_TYPE_VIDEO:
                    has_video = 1;
                    break;
    
                case CODEC_TYPE_SUBTITLE:
                    has_subtitle = 1;
                    break;
    
                }
            }
        }
        *has_video_ptr = has_video;
        *has_audio_ptr = has_audio;
    
        *has_subtitle_ptr = has_subtitle;
    
    static void new_video_stream(AVFormatContext *oc)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVStream *st;
    
        st = av_new_stream(oc, oc->nb_streams);
        if (!st) {
            fprintf(stderr, "Could not alloc stream\n");
            exit(1);
        }
    
        avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);
    
        bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;
        video_bitstream_filters= NULL;
    
    
            avcodec_thread_init(st->codec, thread_count);
    
        if(video_codec_tag)
            video_enc->codec_tag= video_codec_tag;
    
           || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
    
            video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
    
            avctx_opts[CODEC_TYPE_VIDEO]->flags|= CODEC_FLAG_GLOBAL_HEADER;
    
        }
        if(video_global_header&2){
    
            video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
    
            avctx_opts[CODEC_TYPE_VIDEO]->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_name)
                codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 1);
    
            video_enc->codec_id = codec_id;
            codec = avcodec_find_encoder(codec_id);
    
            for(i=0; i<opt_name_count; i++){
    
                 double d= av_get_double(avctx_opts[CODEC_TYPE_VIDEO], 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->time_base.den = frame_rate.num;
            video_enc->time_base.num = frame_rate.den;
    
            if(codec && codec->supported_framerates){
                const AVRational *p= codec->supported_framerates;
    
                AVRational req= (AVRational){frame_rate.num, frame_rate.den};
    
                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*video_enc->height/video_enc->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];
            }
    
    
                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->max_qdiff = video_qdiff;
            video_enc->rc_eq = video_rc_eq;
            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;
    
            if (!video_enc->rc_initial_buffer_occupancy)
                video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;
    
            video_enc->me_threshold= me_threshold;
            video_enc->intra_dc_precision= intra_dc_precision - 8;
            video_enc->strict_std_compliance = strict;
    
            if (do_psnr)
                video_enc->flags|= CODEC_FLAG_PSNR;
    
            /* 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_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);
        }
    
        avcodec_get_context_defaults2(st->codec, CODEC_TYPE_AUDIO);
    
    
        bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters;
        audio_bitstream_filters= NULL;
    
    
            avcodec_thread_init(st->codec, thread_count);
    
        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[CODEC_TYPE_AUDIO]->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++){
    
                double d= av_get_double(avctx_opts[CODEC_TYPE_AUDIO], 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_name)
                codec_id = find_codec_or_die(audio_codec_name, CODEC_TYPE_AUDIO, 1);
    
            if (audio_qscale > QSCALE_NONE) {
                audio_enc->flags |= CODEC_FLAG_QSCALE;
                audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
            }
    
            audio_enc->thread_count = thread_count;
    
            audio_enc->channels = audio_channels;
    
        }
        audio_enc->sample_rate = audio_sample_rate;
    
        audio_enc->time_base= (AVRational){1, audio_sample_rate};
    
            av_strlcpy(st->language, audio_language, sizeof(st->language));
    
            av_free(audio_language);
            audio_language = NULL;
        }
    
        /* reset some key parameters */
        audio_disable = 0;
    
    static void new_subtitle_stream(AVFormatContext *oc)
    
        st = av_new_stream(oc, oc->nb_streams);
        if (!st) {
            fprintf(stderr, "Could not alloc stream\n");
            exit(1);
        }
    
        avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE);
    
        subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;
        if (subtitle_stream_copy) {
            st->stream_copy = 1;
        } else {
    
            for(i=0; i<opt_name_count; i++){
    
                 double d= av_get_double(avctx_opts[CODEC_TYPE_SUBTITLE], 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 = find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 1);
    
            av_strlcpy(st->language, subtitle_language, sizeof(st->language));
    
            av_free(subtitle_language);
            subtitle_language = NULL;
        }
    
    
        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_new_subtitle_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_subtitle_stream(oc);