Skip to content
Snippets Groups Projects
ffmpeg.c 78.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 if (do_pass == 1) {
                        video_enc->flags |= CODEC_FLAG_PASS1;
                    } else {
                        video_enc->flags |= CODEC_FLAG_PASS2;
                    }
                }
    
                /* XXX: need to find a way to set codec parameters */
    
                if (oc->oformat->flags & AVFMT_RGB24) {
    
                    video_enc->pix_fmt = PIX_FMT_RGB24;
                }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
                oc->streams[nb_streams] = st;
                nb_streams++;
            }
        
            if (use_audio) {
                AVCodecContext *audio_enc;
    
                st = av_mallocz(sizeof(AVStream));
                if (!st) {
                    fprintf(stderr, "Could not alloc stream\n");
                    exit(1);
                }
                audio_enc = &st->codec;
    
                codec_id = file_oformat->audio_codec;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (audio_codec_id != CODEC_ID_NONE)
                    codec_id = audio_codec_id;
                audio_enc->codec_id = codec_id;
                audio_enc->codec_type = CODEC_TYPE_AUDIO;
                
                audio_enc->bit_rate = audio_bit_rate;
                audio_enc->sample_rate = audio_sample_rate;
    
                /* For audio codecs other than AC3 we limit */
                /* the number of coded channels to stereo   */
                if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
                    audio_enc->channels = 2;
                } else
                    audio_enc->channels = audio_channels;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                oc->streams[nb_streams] = st;
                nb_streams++;
            }
    
            oc->nb_streams = nb_streams;
    
            if (!nb_streams) {
    
                fprintf(stderr, "No audio or video streams available\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                exit(1);
            }
    
            if (str_title)
    
                pstrcpy(oc->title, sizeof(oc->title), str_title);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (str_author)
    
                pstrcpy(oc->author, sizeof(oc->author), str_author);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (str_copyright)
    
                pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (str_comment)
    
                pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        output_files[nb_output_files] = oc;
        /* dump the file content */
        dump_format(oc, nb_output_files, filename, 1);
        nb_output_files++;
    
        strcpy(oc->filename, filename);
    
    
        /* check filename in case of an image number is expected */
    
        if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
            if (filename_number_test(oc->filename) < 0) {
                print_error(oc->filename, AVERROR_NUMEXPECTED);
    
        if (!(oc->oformat->flags & AVFMT_NOFILE)) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* test if it already exists to avoid loosing precious files */
            if (!file_overwrite && 
                (strchr(filename, ':') == NULL ||
                 strstart(filename, "file:", NULL))) {
                if (url_exist(filename)) {
                    int c;
                    
                    printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
                    fflush(stdout);
                    c = getchar();
                    if (toupper(c) != 'Y') {
                        fprintf(stderr, "Not overwriting - exiting\n");
                        exit(1);
                    }
                }
            }
            
            /* open the file */
            if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
                fprintf(stderr, "Could not open '%s'\n", filename);
                exit(1);
            }
        }
    
        /* reset some options */
    
        file_oformat = NULL;
        file_iformat = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        audio_disable = 0;
        video_disable = 0;
        audio_codec_id = CODEC_ID_NONE;
        video_codec_id = CODEC_ID_NONE;
    }
    
    
    /* prepare dummy protocols for grab */
    void prepare_grab(void)
    {
        int has_video, has_audio, i, j;
        AVFormatContext *oc;
        AVFormatContext *ic;
        AVFormatParameters ap1, *ap = &ap1;
    
        /* see if audio/video inputs are needed */
        has_video = 0;
        has_audio = 0;
        memset(ap, 0, sizeof(*ap));
        for(j=0;j<nb_output_files;j++) {
            oc = output_files[j];
            for(i=0;i<oc->nb_streams;i++) {
                AVCodecContext *enc = &oc->streams[i]->codec;
                switch(enc->codec_type) {
                case CODEC_TYPE_AUDIO:
                    if (enc->sample_rate > ap->sample_rate)
                        ap->sample_rate = enc->sample_rate;
                    if (enc->channels > ap->channels)
                        ap->channels = enc->channels;
                    has_audio = 1;
                    break;
                case CODEC_TYPE_VIDEO:
                    if (enc->width > ap->width)
                        ap->width = enc->width;
                    if (enc->height > ap->height)
                        ap->height = enc->height;
                    if (enc->frame_rate > ap->frame_rate)
                        ap->frame_rate = enc->frame_rate;
                    has_video = 1;
                    break;
    
                }
            }
        }
        
        if (has_video == 0 && has_audio == 0) {
            fprintf(stderr, "Output file must have at least one audio or video stream\n");
            exit(1);
        }
        
        if (has_video) {
    
            AVInputFormat *fmt1;
            fmt1 = av_find_input_format("video_grab_device");
            if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
    
                fprintf(stderr, "Could not find video grab device\n");
    
            /* by now video grab has one stream */
            ic->streams[0]->r_frame_rate = ap->frame_rate;
    
            input_files[nb_input_files] = ic;
            dump_format(ic, nb_input_files, v4l_device, 0);
            nb_input_files++;
        }
        if (has_audio) {
    
            AVInputFormat *fmt1;
            fmt1 = av_find_input_format("audio_device");
            if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
    
                fprintf(stderr, "Could not find audio grab device\n");
    
                exit(1);
            }
            input_files[nb_input_files] = ic;
            dump_format(ic, nb_input_files, audio_device, 0);
            nb_input_files++;
        }
    }
    
    /* open the necessary output devices for playing */
    void prepare_play(void)
    {
    
        file_iformat = NULL;
        file_oformat = guess_format("audio_device", NULL, NULL);
        if (!file_oformat) {
    
            fprintf(stderr, "Could not find audio device\n");
            exit(1);
        }
        
        opt_output_file(audio_device);
    }
    
    
    /* same option as mencoder */
    void opt_pass(const char *pass_str)
    {
        int pass;
        pass = atoi(pass_str);
        if (pass != 1 && pass != 2) {
            fprintf(stderr, "pass number can be only 1 or 2\n");
            exit(1);
        }
        do_pass = pass;
    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #ifndef CONFIG_WIN32
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    INT64 getutime(void)
    {
        struct rusage rusage;
    
        getrusage(RUSAGE_SELF, &rusage);
        return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #else
    INT64 getutime(void)
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    #endif
    
    extern int ffm_nopts;
    
    void opt_bitexact(void)
    {
        avcodec_set_bit_exact();
        /* disable generate of real time pts in ffm (need to be supressed anyway) */
        ffm_nopts = 1;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    void show_formats(void)
    {
    
        AVInputFormat *ifmt;
        AVOutputFormat *ofmt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        URLProtocol *up;
        AVCodec *p;
        const char **pp;
    
        printf("File formats:\n");
        printf("  Encoding:");
    
        for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
            printf(" %s", ofmt->name);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        printf("\n");
        printf("  Decoding:");
    
        for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
            printf(" %s", ifmt->name);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        printf("\n");
    
        printf("Codecs:\n");
        printf("  Encoders:");
        for(p = first_avcodec; p != NULL; p = p->next) {
            if (p->encode)
                printf(" %s", p->name);
        }
        printf("\n");
    
        printf("  Decoders:");
        for(p = first_avcodec; p != NULL; p = p->next) {
            if (p->decode)
                printf(" %s", p->name);
        }
        printf("\n");
    
        printf("Supported file protocols:");
        for(up = first_protocol; up != NULL; up = up->next)
            printf(" %s:", up->name);
        printf("\n");
        
        printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
        printf("Motion estimation methods:");
        pp = motion_str;
        while (*pp) {
            printf(" %s", *pp);
    
            if ((pp - motion_str + 1) == ME_ZERO) 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                printf("(fastest)");
    
            else if ((pp - motion_str + 1) == ME_FULL) 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                printf("(slowest)");
    
            else if ((pp - motion_str + 1) == ME_EPZS) 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                printf("(default)");
            pp++;
        }
        printf("\n");
        exit(1);
    }
    
    void show_help(void)
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        const OptionDef *po;
        int i, expert;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
    
               prog);
        
        if (!do_play) {
            printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
                   "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
        } else {
            printf("usage: ffplay [options] input_file...\n"
                   "Simple audio player\n");
        }
               
        printf("\n"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
               "Main options are:\n");
        for(i=0;i<2;i++) {
            if (i == 1)
                printf("\nAdvanced options are:\n");
            for(po = options; po->name != NULL; po++) {
                char buf[64];
                expert = (po->flags & OPT_EXPERT) != 0;
                if (expert == i) {
                    strcpy(buf, po->name);
                    if (po->flags & HAS_ARG) {
                        strcat(buf, " ");
                        strcat(buf, po->argname);
                    }
                    printf("-%-17s  %s\n", buf, po->help);
                }
            }
        }
    
        exit(1);
    }
    
    const OptionDef options[] = {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "L", 0, {(void*)show_licence}, "show license" },
        { "h", 0, {(void*)show_help}, "show help" },
        { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
        { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
    
    Juanjo's avatar
    Juanjo committed
        { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
        { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
        { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
        { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
        { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
        { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
        { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
        { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
    
        { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
        { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* video options */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
        { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
        { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
    
        { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
        { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
        { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
        { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
        { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
        { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
        { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
    
        { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
        { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
        { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
        { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
        { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
    
        { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
        { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
        { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
        { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
    
        { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
    
        { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
    
        { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
        { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
    
        { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
        { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method", 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
          "method" },
    
        { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo",  "algo" },
    
        { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo",  "algo" },
    
        { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience",  "" },
    
        { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
    
        { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
    
        { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
    
        { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
    
        { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
    
        { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "sameq", OPT_BOOL, {(void*)&same_quality}, 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
          "use same video quality as source (implies VBR)" },
        /* audio options */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
        { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
        { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
        { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
        { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
        { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
        { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace}, 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark}, 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
          "add timings for benchmarking" },
    
        { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump}, 
          "dump each input packet" },
    
        { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
        { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" }, 
    
        { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" }, 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        { NULL, },
    };
    
    int main(int argc, char **argv)
    {
        int optindex, i;
        const char *opt, *arg;
        const OptionDef *po;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        /* detect if invoked as player */
        i = strlen(argv[0]);
        if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
            do_play = 1;
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (argc <= 1)
            show_help();
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        optindex = 1;
        while (optindex < argc) {
            opt = argv[optindex++];
            
            if (opt[0] == '-' && opt[1] != '\0') {
                po = options;
                while (po->name != NULL) {
                    if (!strcmp(opt + 1, po->name))
                        break;
                    po++;
                }
                if (!po->name) {
                    fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
                    exit(1);
                }
                arg = NULL;
                if (po->flags & HAS_ARG)
                    arg = argv[optindex++];
                if (po->flags & OPT_STRING) {
                    char *str;
                    str = strdup(arg);
                    *po->u.str_arg = str;
                } else if (po->flags & OPT_BOOL) {
                    *po->u.int_arg = 1;
                } else {
                    po->u.func_arg(arg);
                }
            } else {
    
                if (!do_play) {
                    opt_output_file(opt);
                } else {
                    opt_input_file(opt);
                }
    
        if (!do_play) {
            /* file converter / grab */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (nb_output_files <= 0) {
                fprintf(stderr, "Must supply at least one output file\n");
                exit(1);
            }
    
        } else {
            /* player */
            if (nb_input_files <= 0) {
                fprintf(stderr, "Must supply at least one input file\n");
                exit(1);
            }
            prepare_play();
        }
    
        ti = getutime();
        av_encode(output_files, nb_output_files, input_files, nb_input_files, 
                  stream_maps, nb_stream_maps);
        ti = getutime() - ti;
        if (do_benchmark) {
            printf("bench: utime=%0.3fs\n", ti / 1000000.0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* close files */
        for(i=0;i<nb_output_files;i++) {
    
            if (!(output_files[i]->oformat->flags & AVFMT_NOFILE)) 
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                url_fclose(&output_files[i]->pb);
        }
    
        for(i=0;i<nb_input_files;i++)
            av_close_input_file(input_files[i]);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        return 0;
    }