Skip to content
Snippets Groups Projects
ffmpeg.c 163 KiB
Newer Older
  • Learn to ignore specific revisions
  •                         codec->time_base.num, codec->time_base.den, INT_MAX);
    
    
                    if(audio_volume != 256) {
                        fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n");
    
                        exit_program(1);
    
                    codec->channel_layout = icodec->channel_layout;
    
                    codec->sample_rate = icodec->sample_rate;
                    codec->channels = icodec->channels;
    
                    codec->frame_size = icodec->frame_size;
    
                    codec->audio_service_type = icodec->audio_service_type;
    
                    codec->block_align= icodec->block_align;
    
                    if(codec->block_align == 1 && codec->codec_id == CODEC_ID_MP3)
                        codec->block_align= 0;
    
                    if(codec->codec_id == CODEC_ID_AC3)
                        codec->block_align= 0;
    
                    codec->width = icodec->width;
                    codec->height = icodec->height;
    
                    codec->has_b_frames = icodec->has_b_frames;
    
                    if (!codec->sample_aspect_ratio.num) {
                        codec->sample_aspect_ratio =
                        ost->st->sample_aspect_ratio =
                            ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
                            ist->st->codec->sample_aspect_ratio.num ?
                            ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
                    }
    
                    codec->width = icodec->width;
                    codec->height = icodec->height;
    
                case AVMEDIA_TYPE_DATA:
                    break;
    
                if (!ost->enc)
                    ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
    
                    ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);
    
                    if (!codec->sample_rate) {
                        codec->sample_rate = icodec->sample_rate;
                    }
    
                    choose_sample_rate(ost->st, ost->enc);
    
                    codec->time_base = (AVRational){1, codec->sample_rate};
    
                    if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
                        codec->sample_fmt = icodec->sample_fmt;
                    choose_sample_fmt(ost->st, ost->enc);
    
                        codec->channels = icodec->channels;
    
                        codec->channel_layout = icodec->channel_layout;
                    }
    
                    if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
                        codec->channel_layout = 0;
    
                    ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
                    icodec->request_channels = codec->channels;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    ist->decoding_needed = 1;
                    ost->encoding_needed = 1;
    
                    ost->resample_sample_fmt  = icodec->sample_fmt;
                    ost->resample_sample_rate = icodec->sample_rate;
                    ost->resample_channels    = icodec->channels;
    
                    if (codec->pix_fmt == PIX_FMT_NONE)
                        codec->pix_fmt = icodec->pix_fmt;
                    choose_pixel_fmt(ost->st, ost->enc);
    
    
                    if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
    
                        fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n");
    
                        exit_program(1);
    
    
                    if (!codec->width || !codec->height) {
                        codec->width  = icodec->width;
                        codec->height = icodec->height;
                    }
    
    
                    ost->video_resample = codec->width   != icodec->width  ||
                                          codec->height  != icodec->height ||
                                          codec->pix_fmt != icodec->pix_fmt;
    
                        codec->bits_per_raw_sample= frame_bits_per_raw_sample;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    }
    
    Stefano Sabatini's avatar
    Stefano Sabatini committed
                    ost->resample_height = icodec->height;
                    ost->resample_width  = icodec->width;
    
                    ost->resample_pix_fmt= icodec->pix_fmt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    ost->encoding_needed = 1;
                    ist->decoding_needed = 1;
    
                    if (!ost->frame_rate.num)
                        ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1};
    
                    if (ost->enc && ost->enc->supported_framerates && !force_fps) {
                        int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
                        ost->frame_rate = ost->enc->supported_framerates[idx];
    
                    }
                    codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
    
                    if(   av_q2d(codec->time_base) < 0.001 && video_sync_method
                       && (video_sync_method==1 || (video_sync_method<0 && !(os->oformat->flags & AVFMT_VARIABLE_FPS)))){
                        av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n"
                                                   "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n");
                    }
    
    #if CONFIG_AVFILTER
    
                    if (configure_video_filters(ist, ost)) {
    
                        fprintf(stderr, "Error opening filters!\n");
                        exit(1);
                    }
    #endif
    
                    ost->encoding_needed = 1;
                    ist->decoding_needed = 1;
                    break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (ost->encoding_needed && codec->codec_id != CODEC_ID_H264 &&
    
                    (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
                    char logfilename[1024];
                    FILE *f;
    
    
                    snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
    
                             pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
                             i);
    
                    if (codec->flags & CODEC_FLAG_PASS1) {
    
                        f = fopen(logfilename, "wb");
    
                            fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno));
    
                            exit_program(1);
    
                        char  *logbuffer;
                        size_t logbuffer_size;
                        if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
                            fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
    
                            exit_program(1);
    
            if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
    
                /* maximum video buffer size is 6-bytes per pixel, plus DPX header size */
    
                int size= codec->width * codec->height;
    
                bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 1664);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        if (!bit_buffer)
            bit_buffer = av_malloc(bit_buffer_size);
    
            fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
                    bit_buffer_size);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* open each encoder */
    
        for (i = 0; i < nb_output_streams; i++) {
            ost = &output_streams[i];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ost->encoding_needed) {
    
                AVCodecContext *dec = input_streams[ost->source_index].st->codec;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (!codec) {
    
                    snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d.%d",
                             avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
    
                    ret = AVERROR(EINVAL);
                    goto dump_format;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (dec->subtitle_header) {
                    ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
                    if (!ost->st->codec->subtitle_header) {
                        ret = AVERROR(ENOMEM);
                        goto dump_format;
                    }
                    memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
                    ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
                }
    
                if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
    
                    snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                            ost->file_index, ost->index);
    
                    ret = AVERROR(EINVAL);
                    goto dump_format;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                assert_codec_experimental(ost->st->codec, 1);
    
                if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
                    av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
                                                 "It takes bits/s as argument, not kbits/s\n");
    
                extra_size += ost->st->codec->extradata_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
    
        /* init input streams */
        for (i = 0; i < nb_input_streams; i++)
    
            if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* open files and write file headers */
    
        for (i = 0; i < nb_output_files; i++) {
            os = output_files[i].ctx;
            if (avformat_write_header(os, &output_files[i].opts) < 0) {
    
                snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
    
    //        assert_avoptions(output_files[i].opts);
            if (strcmp(os->oformat->name, "rtp")) {
    
    
     dump_format:
        /* dump the file output parameters - cannot be done before in case
           of stream copy */
        for(i=0;i<nb_output_files;i++) {
    
            av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
    
        }
    
        /* dump the stream mapping */
        if (verbose >= 0) {
            fprintf(stderr, "Stream mapping:\n");
    
            for (i = 0; i < nb_output_streams;i ++) {
                ost = &output_streams[i];
    
                fprintf(stderr, "  Stream #%d.%d -> #%d.%d",
    
                        input_streams[ost->source_index].file_index,
                        input_streams[ost->source_index].st->index,
    
                if (ost->sync_ist != &input_streams[ost->source_index])
    
                    fprintf(stderr, " [sync #%d.%d]",
                            ost->sync_ist->file_index,
    
                            ost->sync_ist->st->index);
    
                if (ost->st->stream_copy)
                    fprintf(stderr, " (copy)");
    
                    fprintf(stderr, " (%s -> %s)", input_streams[ost->source_index].dec ?
                            input_streams[ost->source_index].dec->name : "?",
                            ost->enc ? ost->enc->name : "?");
    
                fprintf(stderr, "\n");
            }
        }
    
        if (ret) {
            fprintf(stderr, "%s\n", error);
    
        if (want_sdp) {
            print_sdp(output_files, nb_output_files);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        return 0;
    }
    
    /*
     * The following code is the main loop of the file converter
     */
    static int transcode(OutputFile *output_files,
                         int nb_output_files,
                         InputFile *input_files,
                         int nb_input_files)
    {
        int ret, i;
        AVFormatContext *is, *os;
        OutputStream *ost;
        InputStream *ist;
        uint8_t *no_packet;
        int no_packet_count=0;
        int64_t timer_start;
        int key;
    
        if (!(no_packet = av_mallocz(nb_input_files)))
            exit_program(1);
    
        ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files);
        if (ret < 0)
            goto fail;
    
    
        if (!using_stdin) {
            if(verbose >= 0)
    
                fprintf(stderr, "Press [q] to stop, [?] for help\n");
    
            avio_set_interrupt_cb(decode_interrupt_cb);
    
        timer_start = av_gettime();
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            int file_index, ist_index;
            AVPacket pkt;
    
                /* read_key() returns 0 on EOF */
                key = read_key();
                if (key == 'q')
                    break;
    
                if (key == '+') verbose++;
                if (key == '-') verbose--;
                if (key == 's') qp_hist     ^= 1;
                if (key == 'h'){
                    if (do_hex_dump){
                        do_hex_dump = do_pkt_dump = 0;
                    } else if(do_pkt_dump){
                        do_hex_dump = 1;
                    } else
                        do_pkt_dump = 1;
                    av_log_set_level(AV_LOG_DEBUG);
                }
    
    #if CONFIG_AVFILTER
    
                if (key == 'c' || key == 'C'){
                    char ret[4096], target[64], cmd[256], arg[256]={0};
    
                    fprintf(stderr, "\nEnter command: <target> <time> <command>[ <argument>]\n");
    
                    while((k=read_key()) !='\n' && k!='\r' && i<sizeof(ret)-1)
                        if(k>0) ret[i++]= k;
    
                    ret[i]= 0;
                    if(k>0 && sscanf(ret, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &ts, cmd, arg) >= 3){
    
                                if(ts<0){
                                    r= avfilter_graph_send_command(ost->graph, target, cmd, arg, ret, sizeof(ret), key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
                                    fprintf(stderr, "Command reply for %d: %d, %s\n", i, r, ret);
                                }else{
                                    r= avfilter_graph_queue_command(ost->graph, target, cmd, arg, 0, ts);
                                }
    
                if (key == 'd' || key == 'D'){
                    int debug=0;
                    if(key == 'D') {
    
                        debug = input_streams[0].st->codec->debug<<1;
    
                        if(!debug) debug = 1;
                        while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash
                            debug += debug;
                    }else
                        scanf("%d", &debug);
    
                    for(i=0;i<nb_input_streams;i++) {
                        input_streams[i].st->codec->debug = debug;
    
                    for(i=0;i<nb_output_streams;i++) {
                        ost = &output_streams[i];
    
                        ost->st->codec->debug = debug;
                    }
                    if(debug) av_log_set_level(AV_LOG_DEBUG);
                    fprintf(stderr,"debug=%d\n", debug);
                }
    
                if (key == '?'){
                    fprintf(stderr, "key    function\n"
                                    "?      show this help\n"
                                    "+      increase verbosity\n"
                                    "-      decrease verbosity\n"
    
                                    "c      Send command to filtergraph\n"
    
                                    "D      cycle through available debug modes\n"
    
                                    "h      dump packets/hex press to cycle through the 3 states\n"
                                    "q      quit\n"
                                    "s      Show QP histogram\n"
                    );
                }
    
            /* select the stream that we must read now by looking at the
               smallest output pts */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            file_index = -1;
    
            for (i = 0; i < nb_output_streams; i++) {
    
                of = &output_files[ost->file_index];
    
                ist = &input_streams[ost->source_index];
    
                if (ost->is_past_recording_time || no_packet[ist->file_index] ||
                    (os->pb && avio_tell(os->pb) >= of->limit_filesize))
    
                opts = ost->st->pts.val * av_q2d(ost->st->time_base);
    
                if (!input_files[ist->file_index].eof_reached){
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                    if(ipts < ipts_min) {
                        ipts_min = ipts;
                        if(input_sync ) file_index = ist->file_index;
                    }
                    if(opts < opts_min) {
                        opts_min = opts;
                        if(!input_sync) file_index = ist->file_index;
                    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
    
                    file_index= -1;
                    break;
                }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
            /* if none, if is finished */
    
                if(no_packet_count){
                    no_packet_count=0;
    
                    memset(no_packet, 0, nb_input_files);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
            /* read a frame from it and output it in the fifo */
    
            is = input_files[file_index].ctx;
    
            ret= av_read_frame(is, &pkt);
    
                no_packet[file_index]=1;
                no_packet_count++;
    
                input_files[file_index].eof_reached = 1;
    
    Alex Beregszaszi's avatar
    Alex Beregszaszi committed
                if (opt_shortest)
                    break;
                else
                    continue;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            memset(no_packet, 0, nb_input_files);
    
                av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
                                 is->streams[pkt.stream_index]);
    
            /* the following test is needed in case new streams appear
               dynamically in stream : we ignore them */
    
            if (pkt.stream_index >= input_files[file_index].nb_streams)
    
                goto discard_packet;
    
            ist_index = input_files[file_index].ist_index + pkt.stream_index;
            ist = &input_streams[ist_index];
    
            if (ist->discard)
                goto discard_packet;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
                pkt.dts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
    
                pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
    
                if(pkt.pts != AV_NOPTS_VALUE)
    
                    pkt.pts *= ist->ts_scale;
    
                if(pkt.dts != AV_NOPTS_VALUE)
    
                    pkt.dts *= ist->ts_scale;
    
    //        fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files[ist->file_index].ts_offset, ist->st->codec->codec_type);
    
            if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
                && (is->iformat->flags & AVFMT_TS_DISCONT)) {
    
                int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
                int64_t delta= pkt_dts - ist->next_pts;
    
                if((delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
                    (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
                     ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
                    pkt_dts+1<ist->pts)&& !copy_ts){
    
                    input_files[ist->file_index].ts_offset -= delta;
    
                        fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
                                delta, input_files[ist->file_index].ts_offset);
    
                    pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                    if(pkt.pts != AV_NOPTS_VALUE)
                        pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
    
            //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
    
            if (output_packet(ist, ist_index, output_streams, nb_output_streams, &pkt) < 0) {
    
    
                if (verbose >= 0)
                    fprintf(stderr, "Error while decoding stream #%d.%d\n",
    
                            ist->file_index, ist->st->index);
    
                    exit_program(1);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            av_free_packet(&pkt);
    
            /* dump report by using the output first video and audio streams */
    
            print_report(output_files, output_streams, nb_output_streams, 0, timer_start);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        /* at the end of stream, we must flush the decoder buffers */
    
        for (i = 0; i < nb_input_streams; i++) {
            ist = &input_streams[i];
    
                output_packet(ist, i, output_streams, nb_output_streams, NULL);
    
        flush_encoders(output_streams, nb_output_streams);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        /* write the trailer if needed and close file */
        for(i=0;i<nb_output_files;i++) {
    
        /* dump report by using the first video and audio streams */
    
        print_report(output_files, output_streams, nb_output_streams, 1, timer_start);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* close each encoder */
    
        for (i = 0; i < nb_output_streams; i++) {
            ost = &output_streams[i];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ost->encoding_needed) {
    
                av_freep(&ost->st->codec->stats_in);
                avcodec_close(ost->st->codec);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
    #if CONFIG_AVFILTER
            avfilter_graph_free(&ost->graph);
    #endif
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* close each decoder */
    
        for (i = 0; i < nb_input_streams; i++) {
            ist = &input_streams[i];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ist->decoding_needed) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
        /* finished ! */
    
        av_freep(&bit_buffer);
    
        av_freep(&no_packet);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (output_streams) {
            for (i = 0; i < nb_output_streams; i++) {
                ost = &output_streams[i];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (ost) {
    
                    if (ost->st->stream_copy)
                        av_freep(&ost->st->codec->extradata);
    
                    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 */
    
                    av_freep(&ost->st->codec->subtitle_header);
    
                    av_free(ost->resample_frame.data[0]);
    
                    av_free(ost->forced_kf_pts);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    if (ost->video_resample)
    
                        sws_freeContext(ost->img_resample_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        audio_resample_close(ost->resample);
    
                    if (ost->reformat_ctx)
                        av_audio_convert_free(ost->reformat_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
            }
        }
        return ret;
    }
    
    
    static int opt_format(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        last_asked_format = arg;
    
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_video_rc_override_string(const char *opt, const char *arg)
    
    {
        video_rc_override_string = arg;
    
        return 0;
    
    static int opt_me_threshold(const char *opt, const char *arg)
    
        me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
        return 0;
    
    static int opt_verbose(const char *opt, const char *arg)
    
        verbose = parse_number_or_die(opt, arg, OPT_INT64, -10, 10);
    
    static int opt_frame_rate(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (av_parse_video_rate(&frame_rate, arg) < 0) {
    
            fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg);
    
            exit_program(1);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    Stefano Sabatini's avatar
    Stefano Sabatini committed
    static int opt_frame_crop(const char *opt, const char *arg)
    
    Stefano Sabatini's avatar
    Stefano Sabatini committed
        fprintf(stderr, "Option '%s' has been removed, use the crop filter instead\n", opt);
        return AVERROR(EINVAL);
    
    static int opt_frame_size(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fprintf(stderr, "Incorrect frame size\n");
    
            return AVERROR(EINVAL);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_pad(const char *opt, const char *arg) {
        fprintf(stderr, "Option '%s' has been removed, use the pad filter instead\n", opt);
        return -1;
    
    static int opt_frame_pix_fmt(const char *opt, const char *arg)
    
            frame_pix_fmt = av_get_pix_fmt(arg);
    
            if (frame_pix_fmt == PIX_FMT_NONE) {
                fprintf(stderr, "Unknown pixel format requested: %s\n", arg);
    
                return AVERROR(EINVAL);
    
            exit_program(0);
    
        return 0;
    
    static int opt_frame_aspect_ratio(const char *opt, const char *arg)
    
            x = strtol(arg, &end, 10);
            if (end == p)
                y = strtol(end+1, &end, 10);
    
            if (x > 0 && y > 0)
                ar = (double)x / (double)y;
    
            ar = strtod(arg, NULL);
    
    
        if (!ar) {
            fprintf(stderr, "Incorrect aspect ratio specification.\n");
    
            return AVERROR(EINVAL);
    
        return 0;
    
    static int opt_metadata(const char *opt, const char *arg)
    {
        char *mid= strchr(arg, '=');
    
        if(!mid){
            fprintf(stderr, "Missing =\n");
    
            exit_program(1);
    
        av_dict_set(&metadata, arg, mid, 0);
    
    static int opt_qscale(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        video_qscale = parse_number_or_die(opt, arg, OPT_FLOAT, 0, 255);
        if (video_qscale <= 0 || video_qscale > 255) {
    
    Loren Merritt's avatar
    Loren Merritt committed
            fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_top_field_first(const char *opt, const char *arg)
    
        top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1);
    
        return opt_default(opt, arg);
    
    static int opt_thread_count(const char *opt, const char *arg)
    
        thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
    
        if (verbose >= 0)
            fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
    
    static int opt_audio_sample_fmt(const char *opt, const char *arg)
    
            audio_sample_fmt = av_get_sample_fmt(arg);
    
            if (audio_sample_fmt == AV_SAMPLE_FMT_NONE) {
                av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
    
                return AVERROR(EINVAL);
    
            int i;
            char fmt_str[128];
            for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
                printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
    
            exit_program(0);
    
        return 0;
    
    static int opt_audio_rate(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_audio_channels(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_video_channel(const char *opt, const char *arg)
    
        av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -channel.\n");
    
        return opt_default("channel", arg);
    
    static int opt_video_standard(const char *opt, const char *arg)
    
        av_log(NULL, AV_LOG_WARNING, "This option is deprecated, use -standard.\n");
    
        return opt_default("standard", arg);
    
    static int opt_codec(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        switch(opt[strlen(opt)-1]){
        case 'a':    audio_codec_name = arg; break;
        case 'v':    video_codec_name = arg; break;
        case 's': subtitle_codec_name = arg; break;
        }
    
    
        return av_dict_set(&codec_names, opt, arg, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_audio_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:a", arg);
    
    }
    
    static int opt_video_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:v", arg);
    
    }
    
    static int opt_subtitle_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:s", arg);
    
    }
    
    static int opt_data_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:d", arg);
    
    static int opt_codec_tag(const char *opt, const char *arg)
    
        uint32_t *codec_tag;
    
        codec_tag = !strcmp(opt, "atag") ? &audio_codec_tag :
                    !strcmp(opt, "vtag") ? &video_codec_tag :
                    !strcmp(opt, "stag") ? &subtitle_codec_tag : NULL;
    
        if (!codec_tag)
            return -1;
    
        *codec_tag = strtol(arg, &tail, 0);
        if (!tail || *tail)
    
            *codec_tag = AV_RL32(arg);
    
    static int opt_map(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        StreamMap *m = NULL;
        int i, negative = 0, file_idx;
        int sync_file_idx = -1, sync_stream_idx;
        char *p, *sync;
        char *map;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (*arg == '-') {
            negative = 1;
            arg++;
        }
        map = av_strdup(arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        /* parse sync stream first, just pick first matching stream */
        if (sync = strchr(map, ',')) {
            *sync = 0;
            sync_file_idx = strtol(sync + 1, &sync, 0);
            if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
                av_log(NULL, AV_LOG_ERROR, "Invalid sync file index: %d.\n", sync_file_idx);
                exit_program(1);
            }
            if (*sync)
                sync++;
    
            for (i = 0; i < input_files[sync_file_idx].nb_streams; i++)
    
                if (check_stream_specifier(input_files[sync_file_idx].ctx,
                                           input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
                    sync_stream_idx = i;
                    break;
                }
    
            if (i == input_files[sync_file_idx].nb_streams) {
    
                av_log(NULL, AV_LOG_ERROR, "Sync stream specification in map %s does not "
                                           "match any streams.\n", arg);
                exit_program(1);
            }
        }
    
    
        file_idx = strtol(map, &p, 0);
        if (file_idx >= nb_input_files || file_idx < 0) {
            av_log(NULL, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx);
            exit_program(1);
    
        if (negative)
            /* disable some already defined maps */
            for (i = 0; i < nb_stream_maps; i++) {
                m = &stream_maps[i];
                if (check_stream_specifier(input_files[m->file_index].ctx,
                                           input_files[m->file_index].ctx->streams[m->stream_index],
                                           *p == ':' ? p + 1 : p) > 0)
                    m->disabled = 1;
            }
        else
    
            for (i = 0; i < input_files[file_idx].nb_streams; i++) {
    
                if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
                            *p == ':' ? p + 1 : p) <= 0)
                    continue;
                stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1);
                m = &stream_maps[nb_stream_maps - 1];
    
                m->file_index   = file_idx;
                m->stream_index = i;
    
                if (sync_file_idx >= 0) {
                    m->sync_file_index   = sync_file_idx;
                    m->sync_stream_index = sync_stream_idx;
                } else {
                    m->sync_file_index   = file_idx;
                    m->sync_stream_index = i;
                }
            }
    
        if (!m) {
            av_log(NULL, AV_LOG_ERROR, "Stream map '%s' matches no streams.\n", arg);
            exit_program(1);
        }
    
        av_freep(&map);
    
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void parse_meta_type(char *arg, char *type, int *index)
    
            *type = *(++arg);
            switch (*arg) {
            case 'g':
                break;
            case 's':
            case 'c':
            case 'p':
    
                if (*(++arg) == ':')
                    *index = strtol(++arg, NULL, 0);
    
                break;
            default:
                fprintf(stderr, "Invalid metadata type %c.\n", *arg);
    
                exit_program(1);
    
    static int opt_map_metadata(const char *opt, const char *arg)
    
        MetadataMap *m, *m1;
    
        meta_data_maps = grow_array(meta_data_maps, sizeof(*meta_data_maps),
                                    &nb_meta_data_maps, nb_meta_data_maps + 1);
    
        m = &meta_data_maps[nb_meta_data_maps - 1][1];
    
        m->file = strtol(arg, &p, 0);
    
        parse_meta_type(p, &m->type, &m->index);
    
        m1 = &meta_data_maps[nb_meta_data_maps - 1][0];
        if (p = strchr(opt, ':'))
            parse_meta_type(p, &m1->type, &m1->index);
        else
            m1->type = 'g';
    
        if (m->type == 'g' || m1->type == 'g')
            metadata_global_autocopy = 0;
    
        if (m->type == 's' || m1->type == 's')
            metadata_streams_autocopy = 0;
        if (m->type == 'c' || m1->type == 'c')
            metadata_chapters_autocopy = 0;
    
    static int opt_map_meta_data(const char *opt, const char *arg)
    
    {
        fprintf(stderr, "-map_meta_data is deprecated and will be removed soon. "
                        "Use -map_metadata instead.\n");
    
        return opt_map_metadata(opt, arg);
    
    static int opt_input_ts_scale(const char *opt, const char *arg)
    
        return av_dict_set(&ts_scale, opt, arg, 0);
    
    static int opt_recording_time(const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        recording_time = parse_time_or_die(opt, arg, 1);
        return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_start_time(const char *opt, const char *arg)
    
        start_time = parse_time_or_die(opt, arg, 1);
        return 0;
    
    static int opt_recording_timestamp(const char *opt, const char *arg)
    
        char buf[128];
        int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
        struct tm time = *gmtime((time_t*)&recording_timestamp);
        strftime(buf, sizeof(buf), "creation_time=%FT%T%z", &time);
        opt_metadata("metadata", buf);
    
        av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
                                     "tag instead.\n", opt);
    
    static int opt_input_ts_offset(const char *opt, const char *arg)
    
        input_ts_offset = parse_time_or_die(opt, arg, 1);
        return 0;
    
    static enum CodecID find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
    {
        const char *codec_string = encoder ? "encoder" : "decoder";
        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 %s '%s'\n", codec_string, name);