Skip to content
Snippets Groups Projects
ffmpeg.c 144 KiB
Newer Older
  • Learn to ignore specific revisions
  •             int size= codec->width * codec->height;
    
                bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
    
    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_ostreams;i++) {
            ost = ost_table[i];
            if (ost->encoding_needed) {
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                AVCodec *codec = output_codecs[i];
                if (!codec)
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                    codec = avcodec_find_encoder(ost->st->codec->codec_id);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (!codec) {
    
                    snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d",
                             ost->st->codec->codec_id, ost->file_index, ost->index);
    
                    ret = AVERROR(EINVAL);
                    goto dump_format;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (avcodec_open(ost->st->codec, codec) < 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
                }
    
                extra_size += ost->st->codec->extradata_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
        /* open each decoder */
        for(i=0;i<nb_istreams;i++) {
            ist = ist_table[i];
            if (ist->decoding_needed) {
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                AVCodec *codec = input_codecs[i];
                if (!codec)
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                    codec = avcodec_find_decoder(ist->st->codec->codec_id);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (!codec) {
    
                    snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
    
                            ist->st->codec->codec_id, ist->file_index, ist->index);
    
                    ret = AVERROR(EINVAL);
                    goto dump_format;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (avcodec_open(ist->st->codec, codec) < 0) {
    
                    snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                            ist->file_index, ist->index);
    
                    ret = AVERROR(EINVAL);
                    goto dump_format;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                //if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO)
                //    ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
        /* init pts */
        for(i=0;i<nb_istreams;i++) {
            ist = ist_table[i];
    
            ist->pts = 0;
    
            ist->is_start = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* set meta data information from input file if required */
        for (i=0;i<nb_meta_data_maps;i++) {
            AVFormatContext *out_file;
            AVFormatContext *in_file;
    
            AVMetadataTag *mtag;
    
    
            int out_file_index = meta_data_maps[i].out_file;
            int in_file_index = meta_data_maps[i].in_file;
    
            if (out_file_index < 0 || out_file_index >= nb_output_files) {
    
                snprintf(error, sizeof(error), "Invalid output file index %d map_meta_data(%d,%d)",
                         out_file_index, out_file_index, in_file_index);
    
            if (in_file_index < 0 || in_file_index >= nb_input_files) {
    
                snprintf(error, sizeof(error), "Invalid input file index %d map_meta_data(%d,%d)",
                         in_file_index, out_file_index, in_file_index);
    
            out_file = output_files[out_file_index];
            in_file = input_files[in_file_index];
    
    
    
            mtag=NULL;
            while((mtag=av_metadata_get(in_file->metadata, "", mtag, AV_METADATA_IGNORE_SUFFIX)))
                av_metadata_set(&out_file->metadata, mtag->key, mtag->value);
            av_metadata_conv(out_file, out_file->oformat->metadata_conv,
                                        in_file->iformat->metadata_conv);
    
    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];
    
            if (av_write_header(os) < 0) {
    
                snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
    
            if (strcmp(output_files[i]->oformat->name, "rtp")) {
                want_sdp = 0;
            }
        }
    
    
     dump_format:
        /* dump the file output parameters - cannot be done before in case
           of stream copy */
        for(i=0;i<nb_output_files;i++) {
            dump_format(output_files[i], i, output_files[i]->filename, 1);
        }
    
        /* dump the stream mapping */
        if (verbose >= 0) {
            fprintf(stderr, "Stream mapping:\n");
            for(i=0;i<nb_ostreams;i++) {
                ost = ost_table[i];
                fprintf(stderr, "  Stream #%d.%d -> #%d.%d",
                        ist_table[ost->source_index]->file_index,
                        ist_table[ost->source_index]->index,
                        ost->file_index,
                        ost->index);
                if (ost->sync_ist != ist_table[ost->source_index])
                    fprintf(stderr, " [sync #%d.%d]",
                            ost->sync_ist->file_index,
                            ost->sync_ist->index);
                fprintf(stderr, "\n");
            }
        }
    
        if (ret) {
            fprintf(stderr, "%s\n", error);
            goto fail;
        }
    
    
        if (want_sdp) {
            print_sdp(output_files, nb_output_files);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        if (!using_stdin && verbose >= 0) {
    
            fprintf(stderr, "Press [q] to stop encoding\n");
    
            url_set_interrupt_cb(decode_interrupt_cb);
        }
    
        timer_start = av_gettime();
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            int file_index, ist_index;
            AVPacket pkt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        redo:
    
                /* read_key() returns 0 on EOF */
                key = read_key();
                if (key == 'q')
                    break;
            }
    
            /* 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;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                double ipts, opts;
    
                ost = ost_table[i];
                os = output_files[ost->file_index];
                ist = ist_table[ost->source_index];
    
                if(no_packet[ist->file_index])
                    continue;
    
                if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO)
                    opts = ost->sync_opts * av_q2d(ost->st->codec->time_base);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                else
    
                    opts = ost->st->pts.val * av_q2d(ost->st->time_base);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                ipts = (double)ist->pts;
                if (!file_table[ist->file_index].eof_reached){
                    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, sizeof(no_packet));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* finish if recording time exhausted */
    
            if (opts_min >= (recording_time / 1000000.0))
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
            /* finish if limit size exhausted */
    
            if (limit_filesize != 0 && limit_filesize < url_ftell(output_files[0]->pb))
    
            /* read a frame from it and output it in the fifo */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is = input_files[file_index];
    
            ret= av_read_frame(is, &pkt);
    
                no_packet[file_index]=1;
                no_packet_count++;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                file_table[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
            }
    
            no_packet_count=0;
            memset(no_packet, 0, sizeof(no_packet));
    
    
                av_pkt_dump_log(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump);
    
            /* the following test is needed in case new streams appear
               dynamically in stream : we ignore them */
            if (pkt.stream_index >= file_table[file_index].nb_streams)
    
                goto discard_packet;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            ist_index = file_table[file_index].ist_index + pkt.stream_index;
            ist = ist_table[ist_index];
    
            if (ist->discard)
                goto discard_packet;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            if (pkt.dts != AV_NOPTS_VALUE)
                pkt.dts += av_rescale_q(input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ist->st->time_base);
            if (pkt.pts != AV_NOPTS_VALUE)
                pkt.pts += av_rescale_q(input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ist->st->time_base);
    
    
            if(input_files_ts_scale[file_index][pkt.stream_index]){
                if(pkt.pts != AV_NOPTS_VALUE)
                    pkt.pts *= input_files_ts_scale[file_index][pkt.stream_index];
                if(pkt.dts != AV_NOPTS_VALUE)
                    pkt.dts *= input_files_ts_scale[file_index][pkt.stream_index];
            }
    
    
    //        fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files_ts_offset[ist->file_index], 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((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){
    
                    input_files_ts_offset[ist->file_index]-= delta;
                    if (verbose > 2)
    
                        fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
    
                    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->index, pkt.size);
    
            if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
    
    
                if (verbose >= 0)
                    fprintf(stderr, "Error while decoding stream #%d.%d\n",
                            ist->file_index, ist->index);
    
    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, ost_table, nb_ostreams, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        /* at the end of stream, we must flush the decoder buffers */
        for(i=0;i<nb_istreams;i++) {
            ist = ist_table[i];
            if (ist->decoding_needed) {
                output_packet(ist, i, ost_table, nb_ostreams, NULL);
            }
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        /* write the trailer if needed and close file */
        for(i=0;i<nb_output_files;i++) {
            os = output_files[i];
            av_write_trailer(os);
        }
    
    
        /* dump report by using the first video and audio streams */
        print_report(output_files, ost_table, nb_ostreams, 1);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* close each encoder */
        for(i=0;i<nb_ostreams;i++) {
            ost = ost_table[i];
            if (ost->encoding_needed) {
    
                av_freep(&ost->st->codec->stats_in);
                avcodec_close(ost->st->codec);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* close each decoder */
        for(i=0;i<nb_istreams;i++) {
            ist = ist_table[i];
            if (ist->decoding_needed) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
        /* finished ! */
    
        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;
                    }
    
                    av_fifo_free(ost->fifo); /* works even if fifo is not
    
                                                 initialized but set to zero */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    av_free(ost->pict_tmp.data[0]);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    if (ost->video_resample)
    
                        sws_freeContext(ost->img_resample_ctx);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        audio_resample_close(ost->resample);
    
                    if (ost->reformat_ctx)
                        av_audio_convert_free(ost->reformat_ctx);
    
    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;
    }
    
    
    static void opt_format(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        /* compatibility stuff for pgmyuv */
        if (!strcmp(arg, "pgmyuv")) {
    
            pgmyuv_compatibility_hack=1;
    
            fprintf(stderr, "pgmyuv format is deprecated, use image2\n");
    
        file_iformat = av_find_input_format(arg);
        file_oformat = guess_format(arg, NULL, NULL);
        if (!file_iformat && !file_oformat) {
            fprintf(stderr, "Unknown input or output format: %s\n", arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    static void opt_video_rc_override_string(const char *arg)
    
    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_frame_rate(&frame_rate, arg) < 0) {
    
            fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int opt_bitrate(const char *opt, const char *arg)
    
    {
        int codec_type = opt[0]=='a' ? CODEC_TYPE_AUDIO : CODEC_TYPE_VIDEO;
    
        opt_default(opt, arg);
    
    
        if (av_get_int(avcodec_opts[codec_type], "b", NULL) < 1000)
    
            fprintf(stderr, "WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s\n");
    
    static void opt_frame_crop_top(const char *arg)
    
        frame_topBand = atoi(arg);
    
        if (frame_topBand < 0) {
            fprintf(stderr, "Incorrect top crop size\n");
    
            fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_bottom(const char *arg)
    
    {
        frame_bottomBand = atoi(arg);
        if (frame_bottomBand < 0) {
            fprintf(stderr, "Incorrect bottom crop size\n");
    
            fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_left(const char *arg)
    
    {
        frame_leftBand = atoi(arg);
        if (frame_leftBand < 0) {
            fprintf(stderr, "Incorrect left crop size\n");
    
            fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_crop_right(const char *arg)
    
    {
        frame_rightBand = atoi(arg);
        if (frame_rightBand < 0) {
            fprintf(stderr, "Incorrect right crop size\n");
    
            fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
    
    static void opt_frame_size(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        if (av_parse_video_frame_size(&frame_width, &frame_height, arg) < 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            fprintf(stderr, "Incorrect frame size\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
            fprintf(stderr, "Frame size must be a multiple of 2\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    static void opt_pad_color(const char *arg) {
        /* Input is expected to be six hex digits similar to
           how colors are expressed in html tags (but without the #) */
        int rgb = strtol(arg, NULL, 16);
        int r,g,b;
    
        g = ((rgb >> 8) & 255);
        b = (rgb & 255);
    
        padcolor[0] = RGB_TO_Y(r,g,b);
        padcolor[1] = RGB_TO_U(r,g,b,0);
        padcolor[2] = RGB_TO_V(r,g,b,0);
    }
    
    static void opt_frame_pad_top(const char *arg)
    {
    
        frame_padtop = atoi(arg);
    
        if (frame_padtop < 0) {
            fprintf(stderr, "Incorrect top pad size\n");
    
        frame_padbottom = atoi(arg);
    
        if (frame_padbottom < 0) {
            fprintf(stderr, "Incorrect bottom pad size\n");
    
        frame_padleft = atoi(arg);
    
        if (frame_padleft < 0) {
            fprintf(stderr, "Incorrect left pad size\n");
    
        frame_padright = atoi(arg);
    
        if (frame_padright < 0) {
            fprintf(stderr, "Incorrect right pad size\n");
    
    static void list_fmts(void (*get_fmt_string)(char *buf, int buf_size, int fmt), int nb_fmts)
    
        char fmt_str[128];
        for (i=-1; i < nb_fmts; i++) {
            get_fmt_string (fmt_str, sizeof(fmt_str), i);
            fprintf(stdout, "%s\n", fmt_str);
    
    static void opt_frame_pix_fmt(const char *arg)
    {
    
            frame_pix_fmt = avcodec_get_pix_fmt(arg);
    
            if (frame_pix_fmt == PIX_FMT_NONE) {
                fprintf(stderr, "Unknown pixel format requested: %s\n", arg);
                av_exit(1);
            }
        } else {
    
            list_fmts(avcodec_pix_fmt_string, PIX_FMT_NB);
    
    static void opt_frame_aspect_ratio(const char *arg)
    {
        int x = 0, y = 0;
        double ar = 0;
        const char *p;
    
            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");
    
    static int opt_metadata(const char *opt, const char *arg)
    {
        char *mid= strchr(arg, '=');
    
        if(!mid){
            fprintf(stderr, "Missing =\n");
            av_exit(1);
        }
        *mid++= 0;
    
        metadata_count++;
        metadata= av_realloc(metadata, sizeof(*metadata)*metadata_count);
        metadata[metadata_count-1].key  = av_strdup(arg);
        metadata[metadata_count-1].value= av_strdup(mid);
    
        return 0;
    }
    
    
    static void opt_qscale(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Loren Merritt's avatar
    Loren Merritt committed
        if (video_qscale <= 0 ||
    
    Loren Merritt's avatar
    Loren Merritt committed
            fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    static void opt_top_field_first(const char *arg)
    {
        top_field_first= atoi(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 void opt_audio_sample_fmt(const char *arg)
    {
        if (strcmp(arg, "list"))
            audio_sample_fmt = avcodec_get_sample_fmt(arg);
        else {
            list_fmts(avcodec_sample_fmt_string, SAMPLE_FMT_NB);
            av_exit(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 void opt_video_channel(const char *arg)
    
    static void opt_video_standard(const char *arg)
    {
        video_standard = av_strdup(arg);
    }
    
    
    static void opt_codec(int *pstream_copy, char **pcodec_name,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    
    static void opt_audio_codec(const char *arg)
    {
    
        opt_codec(&audio_stream_copy, &audio_codec_name, CODEC_TYPE_AUDIO, arg);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    static void opt_audio_tag(const char *arg)
    {
        char *tail;
        audio_codec_tag= strtol(arg, &tail, 0);
    
        if(!tail || *tail)
            audio_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
    }
    
    static void opt_video_tag(const char *arg)
    {
        char *tail;
        video_codec_tag= strtol(arg, &tail, 0);
    
        if(!tail || *tail)
            video_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
    }
    
    
    static void opt_video_codec(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        opt_codec(&video_stream_copy, &video_codec_name, CODEC_TYPE_VIDEO, arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    static void opt_subtitle_codec(const char *arg)
    {
    
        opt_codec(&subtitle_stream_copy, &subtitle_codec_name, CODEC_TYPE_SUBTITLE, arg);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_subtitle_tag(const char *arg)
    {
        char *tail;
        subtitle_codec_tag= strtol(arg, &tail, 0);
    
        if(!tail || *tail)
            subtitle_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
    }
    
    
    static void opt_map(const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVStreamMap *m;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        m = &stream_maps[nb_stream_maps++];
    
    
        m->file_index = strtol(arg, &p, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (*p)
            p++;
    
        m->stream_index = strtol(p, &p, 0);
    
            m->sync_file_index = strtol(p, &p, 0);
    
            m->sync_stream_index = strtol(p, &p, 0);
    
        } else {
            m->sync_file_index = m->file_index;
            m->sync_stream_index = m->stream_index;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void opt_map_meta_data(const char *arg)
    {
        AVMetaDataMap *m;
    
        m->out_file = strtol(arg, &p, 0);
    
        m->in_file = strtol(p, &p, 0);
    
    static void opt_input_ts_scale(const char *arg)
    {
        unsigned int stream;
        double scale;
        char *p;
    
        stream = strtol(arg, &p, 0);
        if (*p)
            p++;
        scale= strtod(p, &p);
    
        if(stream >= MAX_STREAMS)
            av_exit(1);
    
        input_files_ts_scale[nb_input_files][stream]= scale;
    }
    
    
    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_rec_timestamp(const char *opt, const char *arg)
    
        rec_timestamp = parse_time_or_die(opt, arg, 0) / 1000000;
        return 0;
    
    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, int type, int encoder)
    {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        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) {
    
            fprintf(stderr, "Unknown %s '%s'\n", codec_string, name);
    
            fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
    
    static void opt_input_file(const char *filename)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        AVFormatContext *ic;
        AVFormatParameters params, *ap = &params;
    
        int64_t timestamp;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (!strcmp(filename, "-"))
            filename = "pipe:";
    
    
        using_stdin |= !strncmp(filename, "pipe:", 5) ||
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* get default parameters from command line */
    
        if (!ic) {
            print_error(filename, AVERROR(ENOMEM));
            av_exit(1);
        }
    
        ap->prealloced_context = 1;
    
        ap->sample_rate = audio_sample_rate;
        ap->channels = audio_channels;
    
        ap->time_base.den = frame_rate.num;
        ap->time_base.num = frame_rate.den;
    
        ap->width = frame_width + frame_padleft + frame_padright;
        ap->height = frame_height + frame_padtop + frame_padbottom;
    
        ap->pix_fmt = frame_pix_fmt;
    
       // ap->sample_fmt = audio_sample_fmt; //FIXME:not implemented in libavformat
    
        ap->channel = video_channel;
        ap->standard = video_standard;
    
        ap->video_codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 0);
        ap->audio_codec_id = find_codec_or_die(audio_codec_name, CODEC_TYPE_AUDIO, 0);
    
        if(pgmyuv_compatibility_hack)
            ap->video_codec_id= CODEC_ID_PGMYUV;
    
        set_context_opts(ic, avformat_opts, AV_OPT_FLAG_DECODING_PARAM);
    
    
        ic->video_codec_id   = find_codec_or_die(video_codec_name   , CODEC_TYPE_VIDEO   , 0);
        ic->audio_codec_id   = find_codec_or_die(audio_codec_name   , CODEC_TYPE_AUDIO   , 0);
        ic->subtitle_codec_id= find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 0);
    
        ic->flags |= AVFMT_FLAG_NONBLOCK;
    
        /* 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
        }
    
        if(opt_programid) {
            int i;
            for(i=0; i<ic->nb_programs; i++)
                if(ic->programs[i]->id != opt_programid)
                    ic->programs[i]->discard = AVDISCARD_ALL;
        }
    
        /* 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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        timestamp = start_time;
        /* add the stream start time */
        if (ic->start_time != AV_NOPTS_VALUE)
            timestamp += ic->start_time;
    
    
        /* if seeking requested, we execute it */
        if (start_time != 0) {
    
            ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
    
                fprintf(stderr, "%s: could not seek to position %0.3f\n",
    
                        filename, (double)timestamp / AV_TIME_BASE);
            }
            /* reset seek info */
            start_time = 0;
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* update the current parameters so that they match the one of the input stream */
        for(i=0;i<ic->nb_streams;i++) {
    
            AVCodecContext *enc = ic->streams[i]->codec;
    
            if(thread_count>1)
                avcodec_thread_init(enc, thread_count);
            enc->thread_count= thread_count;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            switch(enc->codec_type) {
            case CODEC_TYPE_AUDIO:
    
                set_context_opts(enc, avcodec_opts[CODEC_TYPE_AUDIO], AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM);
    
                //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
    
                channel_layout = enc->channel_layout;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                audio_channels = enc->channels;
                audio_sample_rate = enc->sample_rate;
    
                audio_sample_fmt = enc->sample_fmt;
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                input_codecs[nb_icodecs++] = avcodec_find_decoder_by_name(audio_codec_name);
    
                    ic->streams[i]->discard= AVDISCARD_ALL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
            case CODEC_TYPE_VIDEO:
    
                set_context_opts(enc, avcodec_opts[CODEC_TYPE_VIDEO], AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                frame_height = enc->height;
                frame_width = enc->width;
    
                if(ic->streams[i]->sample_aspect_ratio.num)
                    frame_aspect_ratio=av_q2d(ic->streams[i]->sample_aspect_ratio);
                else
                    frame_aspect_ratio=av_q2d(enc->sample_aspect_ratio);
                frame_aspect_ratio *= (float) enc->width / enc->height;
    
                frame_pix_fmt = enc->pix_fmt;
    
                rfps      = ic->streams[i]->r_frame_rate.num;
                rfps_base = ic->streams[i]->r_frame_rate.den;
    
                if(enc->lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
    
                if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
    
                        fprintf(stderr,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
    
                                i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
    
                        (float)rfps / rfps_base, rfps, rfps_base);
    
                /* update the current frame rate to match the stream frame rate */
    
                frame_rate.num = rfps;
                frame_rate.den = rfps_base;
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                input_codecs[nb_icodecs++] = avcodec_find_decoder_by_name(video_codec_name);
    
                    ic->streams[i]->discard= AVDISCARD_ALL;
                else if(video_discard)
                    ic->streams[i]->discard= video_discard;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                input_codecs[nb_icodecs++] = avcodec_find_decoder_by_name(subtitle_codec_name);
    
                if(subtitle_disable)
                    ic->streams[i]->discard = AVDISCARD_ALL;
    
            case CODEC_TYPE_ATTACHMENT:
    
    Aurelien Jacobs's avatar
    Aurelien Jacobs committed
                nb_icodecs++;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        input_files[nb_input_files] = ic;
    
        input_files_ts_offset[nb_input_files] = input_ts_offset - (copy_ts ? 0 : timestamp);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* dump the file content */
    
        if (verbose >= 0)
            dump_format(ic, nb_input_files, filename, 0);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        nb_input_files++;
    
        file_iformat = NULL;
        file_oformat = NULL;
    
        video_channel = 0;
    
        av_freep(&video_codec_name);
        av_freep(&audio_codec_name);
        av_freep(&subtitle_codec_name);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static void check_audio_video_sub_inputs(int *has_video_ptr, int *has_audio_ptr,
                                             int *has_subtitle_ptr)