Skip to content
Snippets Groups Projects
ffmpeg.c 149 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* finished ! */
        
        ret = 0;
     fail1:
    
        av_freep(&bit_buffer);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        av_free(file_table);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (ist_table) {
            for(i=0;i<nb_istreams;i++) {
                ist = ist_table[i];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                av_free(ist);
    
    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;
                    }
    
                    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)
                        img_resample_close(ost->img_resample_ctx);
                    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:
        ret = -ENOMEM;
        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_image_format(const char *arg)
    
    {
        AVImageFormat *f;
        
        for(f = first_image_format; f != NULL; f = f->next) {
            if (!strcmp(arg, f->name))
                break;
        }
        if (!f) {
            fprintf(stderr, "Unknown image format: '%s'\n", arg);
            exit(1);
        }
        image_format = f;
    }
    
    
    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;
    
        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_bitrate(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        video_bit_rate = atoi(arg) * 1000;
    }
    
    
    static void opt_video_bitrate_tolerance(const char *arg)
    
    {
        video_bit_rate_tolerance = atoi(arg) * 1000;
    }
    
    
    static void opt_video_bitrate_max(const char *arg)
    
    {
        video_rc_max_rate = atoi(arg) * 1000;
    }
    
    
    static void opt_video_bitrate_min(const char *arg)
    
    {
        video_rc_min_rate = atoi(arg) * 1000;
    }
    
    
    static void opt_video_buffer_size(const char *arg)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    {
    
        video_rc_buffer_size = atoi(arg) * 8*1024;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    }
    
    
    static void opt_video_rc_eq(char *arg)
    
    {
        video_rc_eq = arg;
    }
    
    
    static void opt_video_rc_override_string(char *arg)
    
    static void opt_workaround_bugs(const char *arg)
    
    static void opt_dct_algo(const char *arg)
    
    static void opt_idct_algo(const char *arg)
    
    static void opt_me_threshold(const char *arg)
    {
        me_threshold = atoi(arg);
    }
    
    
    static void opt_mb_threshold(const char *arg)
    {
        mb_threshold = atoi(arg);
    }
    
    static void opt_error_resilience(const char *arg)
    
    {
        error_resilience = atoi(arg);
    }
    
    
    static void opt_error_concealment(const char *arg)
    
    static void opt_debug(const char *arg)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    {
        debug = atoi(arg);
    }
    
    static void opt_vismv(const char *arg)
    {
        debug_mv = atoi(arg);
    }
        
    
    static void opt_verbose(const char *arg)
    {
        verbose = atoi(arg);
    
        av_log_set_level(atoi(arg));
    
    static void opt_frame_rate(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
            fprintf(stderr, "Incorrect frame rate\n");
    	exit(1);
        }
    
    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");
            exit(1);
        }
        frame_height -= frame_topBand;
    }
    
    
    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");
            exit(1);        
        }
        if ((frame_bottomBand) >= frame_height){
        	fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
            exit(1);
        }
        frame_height -= frame_bottomBand;
    }
    
    
    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");
            exit(1);
        }
        frame_width -= frame_leftBand;
    }
    
    
    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");
            exit(1);        
        }
        if ((frame_rightBand) >= frame_width){
        	fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
            exit(1);
        }
        frame_width -= frame_rightBand;
    }
    
    
    static void opt_frame_size(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (parse_image_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;
        
        r = (rgb >> 16); 
        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);
        }
    }
    
    
    
    static void opt_frame_pix_fmt(const char *arg)
    {
        frame_pix_fmt = avcodec_get_pix_fmt(arg);
    }
    
    
    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");
    	exit(1);
        }
        frame_aspect_ratio = ar;
    }
    
    
    static void opt_gop_size(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        gop_size = atoi(arg);
    }
    
    
    static void opt_b_frames(const char *arg)
    
    {
        b_frames = atoi(arg);
        if (b_frames > FF_MAX_B_FRAMES) {
            fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
            exit(1);
        } else if (b_frames < 1) {
            fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
            exit(1);
        }
    }
    
    
    static void opt_mb_decision(const char *arg)
    {
        mb_decision = atoi(arg);
    }
    
    
    static void opt_mb_cmp(const char *arg)
    {
        mb_cmp = atoi(arg);
    }
    
    
    static void opt_ildct_cmp(const char *arg)
    {
        ildct_cmp = atoi(arg);
    }
    
    
    static void opt_sub_cmp(const char *arg)
    {
        sub_cmp = atoi(arg);
    }
    
    static void opt_cmp(const char *arg)
    {
        cmp = atoi(arg);
    }
    
    
    static void opt_pre_cmp(const char *arg)
    {
        pre_cmp = atoi(arg);
    }
    
    static void opt_pre_me(const char *arg)
    {
        pre_me = atoi(arg);
    }
    
    static void opt_lumi_mask(const char *arg)
    {
        lumi_mask = atof(arg);
    }
    
    static void opt_dark_mask(const char *arg)
    {
        dark_mask = atof(arg);
    }
    
    static void opt_scplx_mask(const char *arg)
    {
        scplx_mask = atof(arg);
    }
    
    static void opt_tcplx_mask(const char *arg)
    {
        tcplx_mask = atof(arg);
    }
    
    static void opt_p_mask(const char *arg)
    {
        p_mask = atof(arg);
    }
    
    
    static void opt_qscale(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        video_qscale = atof(arg);
        if (video_qscale < 0.01 ||
            video_qscale > 255) {
            fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            exit(1);
        }
    }
    
    
    static void opt_qsquish(const char *arg)
    {
        video_qsquish = atof(arg);
        if (video_qsquish < 0.0 ||
            video_qsquish > 99.0) {
            fprintf(stderr, "qsquish must be >= 0.0 and <= 99.0\n");
            exit(1);
        }
    }
    
    static void opt_lelim(const char *arg)
    {
        video_lelim = atoi(arg);
        if (video_lelim < -99 ||
            video_lelim > 99) {
            fprintf(stderr, "lelim must be >= -99 and <= 99\n");
            exit(1);
        }
    }
    
    static void opt_celim(const char *arg)
    {
        video_celim = atoi(arg);
        if (video_celim < -99 ||
            video_celim > 99) {
            fprintf(stderr, "celim must be >= -99 and <= 99\n");
            exit(1);
        }
    }
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    static void opt_lmax(const char *arg)
    {
        video_lmax = atof(arg)*FF_QP2LAMBDA;
    }
    
    static void opt_lmin(const char *arg)
    {
        video_lmin = atof(arg)*FF_QP2LAMBDA;
    }
    
    
    static void opt_qmin(const char *arg)
    
    {
        video_qmin = atoi(arg);
    
            video_qmin > 31) {
            fprintf(stderr, "qmin must be >= 1 and <= 31\n");
            exit(1);
        }
    }
    
    
    static void opt_qmax(const char *arg)
    
    {
        video_qmax = atoi(arg);
    
            video_qmax > 31) {
            fprintf(stderr, "qmax must be >= 1 and <= 31\n");
            exit(1);
        }
    }
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    {
    
        video_mb_lmin = atof(arg)*FF_QP2LAMBDA;
        if (video_mb_lmin < 1 ||
            video_mb_lmin > FF_LAMBDA_MAX) {
            fprintf(stderr, "mblmin must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    {
    
        video_mb_lmax = atof(arg)*FF_QP2LAMBDA;
        if (video_mb_lmax < 1 ||
            video_mb_lmax > FF_LAMBDA_MAX) {
            fprintf(stderr, "mblmax must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
    
    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_qblur(const char *arg)
    
    {
        video_qblur = atof(arg);
    }
    
    
    static void opt_qcomp(const char *arg)
    
    {
        video_qcomp = atof(arg);
    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    static void opt_rc_initial_cplx(const char *arg)
    
    {
        video_rc_initial_cplx = atof(arg);
    }
    
    static void opt_b_qfactor(const char *arg)
    
    static void opt_i_qfactor(const char *arg)
    
    static void opt_b_qoffset(const char *arg)
    
    static void opt_i_qoffset(const char *arg)
    
    static void opt_ibias(const char *arg)
    {
        video_intra_quant_bias = atoi(arg);
    }
    static void opt_pbias(const char *arg)
    {
        video_inter_quant_bias = atoi(arg);
    }
    
    
    static void opt_packet_size(const char *arg)
    
    static void opt_error_rate(const char *arg)
    {
        error_rate= atoi(arg);
    }
    
    
    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_noise_reduction(const char *arg)
    {
        noise_reduction= atoi(arg);
    }
    
    
    static void opt_qns(const char *arg)
    {
        qns= atoi(arg);
    }
    
    
    static void opt_sc_threshold(const char *arg)
    {
        sc_threshold= atoi(arg);
    }
    
    
    static void opt_me_range(const char *arg)
    {
        me_range = 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_bitrate(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        audio_bit_rate = atoi(arg) * 1000;
    }
    
    
    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_device(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        video_device = av_strdup(arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_grab_device(const char *arg)
    {
        grab_device = av_strdup(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_audio_device(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        audio_device = av_strdup(arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_audio_codec(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVCodec *p;
    
    
        if (!strcmp(arg, "copy")) {
            audio_stream_copy = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            p = first_avcodec;
            while (p) {
                if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
                    break;
                p = p->next;
            }
            if (p == NULL) {
                fprintf(stderr, "Unknown audio codec '%s'\n", arg);
                exit(1);
            } else {
                audio_codec_id = p->id;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    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);
        }
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    const char *motion_str[] = {
        "zero",
        "full",
        "log",
        "phods",
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        "epzs",
        "x1",
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        NULL,
    };
    
    
    static void opt_motion_estimation(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        const char **p;
        p = motion_str;
        for(;;) {
            if (!*p) {
                fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
                exit(1);
            }
            if (!strcmp(*p, arg))
                break;
            p++;
        }
    
        me_method = (p - motion_str) + 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_video_codec(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVCodec *p;
    
    
        if (!strcmp(arg, "copy")) {
            video_stream_copy = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            p = first_avcodec;
            while (p) {
                if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
                    break;
                p = p->next;
            }
            if (p == NULL) {
                fprintf(stderr, "Unknown video codec '%s'\n", arg);
                exit(1);
            } else {
                video_codec_id = p->id;
            }
    
    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);
    
    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 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:";
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        using_stdin |= !strncmp(filename, "pipe:", 5) || 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* get default parameters from command line */
    
        memset(ap, 0, sizeof(*ap));
        ap->sample_rate = audio_sample_rate;
        ap->channels = audio_channels;
    
        ap->time_base.den = frame_rate;
        ap->time_base.num = frame_rate_base;
    
        ap->width = frame_width + frame_padleft + frame_padright;
        ap->height = frame_height + frame_padtop + frame_padbottom;
    
        ap->pix_fmt = frame_pix_fmt;
    
        ap->device  = grab_device;
        ap->channel = video_channel;
        ap->standard = video_standard;
    
        ap->video_codec_id = video_codec_id;
        ap->audio_codec_id = audio_codec_id;
        if(pgmyuv_compatibility_hack)
            ap->video_codec_id= CODEC_ID_PGMYUV;
    
    
        /* 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);
    
            if (ret < 0) {
                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);
    #endif
            enc->thread_count= thread_count;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            switch(enc->codec_type) {
            case CODEC_TYPE_AUDIO:
    
                //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:
                frame_height = enc->height;
                frame_width = enc->width;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    	    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;
    
                enc->error_resilience = error_resilience; 
    
                enc->error_concealment = error_concealment; 
    
                enc->idct_algo = idct_algo;
                enc->debug = debug;
                enc->debug_mv = debug_mv;            
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                enc->lowres= lowres;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                if(lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
    
                if(bitexact)
                    enc->flags|= CODEC_FLAG_BITEXACT;
    
                if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) { 
    
                        fprintf(stderr,"\nSeems that stream %d comes from film source: %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 */
    
                    ic->streams[i]->discard= AVDISCARD_ALL;
                else if(video_discard)
                    ic->streams[i]->discard= video_discard;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
    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;
    
        grab_device = NULL;
        video_channel = 0;
        
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_grab(const char *arg)
    {
        file_iformat = av_find_input_format(arg);
        opt_input_file("");
    }