Skip to content
Snippets Groups Projects
ffmpeg.c 189 KiB
Newer Older
  • Learn to ignore specific revisions
  •             av_log(NULL, AV_LOG_FATAL,
                       "Audio volume adjustment on sample format %s is not supported.\n",
                       av_get_sample_fmt_name(ist->st->codec->sample_fmt));
                exit_program(1);
            }
        }
    
        rate_emu_sleep(ist);
    
        for (i = 0; i < nb_output_streams; i++) {
            OutputStream *ost = &output_streams[i];
    
            if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
                continue;
    
            do_audio_out(output_files[ost->file_index].ctx, ost, ist, decoded_frame);
    
    }
    
    static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *pkt_pts, int64_t *pkt_dts)
    {
        AVFrame *decoded_frame, *filtered_frame = NULL;
        void *buffer_to_free = NULL;
        int i, ret = 0;
        float quality = 0;
    
    #if CONFIG_AVFILTER
    
        int frame_available = 1;
    
        int64_t *best_effort_timestamp;
        AVRational *frame_sample_aspect;
    
        if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
    
            return AVERROR(ENOMEM);
    
        else
            avcodec_get_frame_defaults(ist->decoded_frame);
        decoded_frame = ist->decoded_frame;
    
        pkt->pts  = *pkt_pts;
        pkt->dts  = *pkt_dts;
        *pkt_pts  = AV_NOPTS_VALUE;
    
    
        if (pkt->duration) {
            duration = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
        } else if(ist->st->codec->time_base.num != 0) {
    
            int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
    
            duration = ((int64_t)AV_TIME_BASE *
    
                              ist->st->codec->time_base.num * ticks) /
    
                              ist->st->codec->time_base.den;
        }
    
        if(*pkt_dts != AV_NOPTS_VALUE && duration) {
            *pkt_dts += duration;
    
        }else
            *pkt_dts = AV_NOPTS_VALUE;
    
        ret = avcodec_decode_video2(ist->st->codec,
                                    decoded_frame, got_output, pkt);
        if (ret < 0)
    
        quality = same_quant ? decoded_frame->quality : 0;
        if (!*got_output) {
            /* no picture yet */
    
        best_effort_timestamp= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "best_effort_timestamp");
        if(*best_effort_timestamp != AV_NOPTS_VALUE)
            ist->next_pts = ist->pts = *best_effort_timestamp;
    
        pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
    
        frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
    
        for(i=0;i<nb_output_streams;i++) {
    
            OutputStream *ost = ost = &output_streams[i];
    
            if(check_output_constraints(ist, ost) && ost->encoding_needed){
    
                if (!frame_sample_aspect->num)
                    *frame_sample_aspect = ist->st->sample_aspect_ratio;
    
                decoded_frame->pts = ist->pts;
    
                if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER) {
    
                    FrameBuffer      *buf = decoded_frame->opaque;
                    AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
                                                decoded_frame->data, decoded_frame->linesize,
                                                AV_PERM_READ | AV_PERM_PRESERVE,
                                                ist->st->codec->width, ist->st->codec->height,
                                                ist->st->codec->pix_fmt);
    
                    avfilter_copy_frame_props(fb, decoded_frame);
                    fb->pts                 = ist->pts;
                    fb->buf->priv           = buf;
                    fb->buf->free           = filter_release_buffer;
    
                    buf->refcount++;
                    av_buffersrc_buffer(ost->input_video_filter, fb);
                } else
    
                if((av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE)) < 0){
                    av_log(0, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
                    exit_program(1);
                }
    
        rate_emu_sleep(ist);
    
        for (i = 0; i < nb_output_streams; i++) {
            OutputStream *ost = &output_streams[i];
            int frame_size;
    
            if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
                continue;
    
    #if CONFIG_AVFILTER
            if (ost->input_video_filter) {
    
                frame_available = av_buffersink_poll_frame(ost->output_video_filter);
    
            }
            while (frame_available) {
                if (ost->output_video_filter) {
                    AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
    
                    if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0){
                        av_log(0, AV_LOG_WARNING, "AV Filter told us it has a frame available but failed to output one\n");
    
                    if (!ist->filtered_frame && !(ist->filtered_frame = avcodec_alloc_frame())) {
                        av_free(buffer_to_free);
                        return AVERROR(ENOMEM);
                    } else
                        avcodec_get_frame_defaults(ist->filtered_frame);
                    filtered_frame = ist->filtered_frame;
    
                    *filtered_frame= *decoded_frame; //for me_threshold
                    if (ost->picref) {
                        avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
                        ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
    
                if (ost->picref->video && !ost->frame_aspect_ratio)
                    ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio;
    #else
                filtered_frame = decoded_frame;
    #endif
    
                do_video_out(output_files[ost->file_index].ctx, ost, ist, filtered_frame, &frame_size,
                             same_quant ? quality : ost->st->codec->global_quality);
                if (vstats_filename && frame_size)
                    do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);
    #if CONFIG_AVFILTER
                cont:
    
                frame_available = ost->output_video_filter && av_buffersink_poll_frame(ost->output_video_filter);
    
                avfilter_unref_buffer(ost->picref);
    
        av_free(buffer_to_free);
        return ret;
    
    static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
    
        AVSubtitle subtitle;
        int i, ret = avcodec_decode_subtitle2(ist->st->codec,
                                              &subtitle, got_output, pkt);
        if (ret < 0)
            return ret;
        if (!*got_output)
    
    
        rate_emu_sleep(ist);
    
        for (i = 0; i < nb_output_streams; i++) {
            OutputStream *ost = &output_streams[i];
    
            if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
                continue;
    
            do_subtitle_out(output_files[ost->file_index].ctx, ost, ist, &subtitle, pkt->pts);
        }
    
        avsubtitle_free(&subtitle);
    
    
    /* pkt = NULL means EOF (needed to flush decoder buffers) */
    
    static int output_packet(InputStream *ist,
    
                             OutputStream *ost_table, int nb_ostreams,
    
        int64_t pkt_dts = AV_NOPTS_VALUE;
    
        int64_t pkt_pts = AV_NOPTS_VALUE;
    
        AVPacket avpkt;
    
    
        if (ist->next_pts == AV_NOPTS_VALUE)
            ist->next_pts = ist->pts;
    
            av_init_packet(&avpkt);
    
            avpkt.data = NULL;
            avpkt.size = 0;
    
        if (pkt->dts != AV_NOPTS_VALUE) {
            if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
    
                ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
            pkt_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
        }
    
        if(pkt->pts != AV_NOPTS_VALUE)
            pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
    
        // while we have more to decode or while the decoder did output something on EOF
    
        while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
    
            ist->pts = ist->next_pts;
    
            if (avpkt.size && avpkt.size != pkt->size) {
    
                av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
                       "Multiple frames in a packet from stream %d\n", pkt->stream_index);
    
                ist->showed_multi_packet_warning = 1;
            }
    
            switch (ist->st->codec->codec_type) {
    
            case AVMEDIA_TYPE_AUDIO:
                ret = transcode_audio    (ist, &avpkt, &got_output);
                break;
            case AVMEDIA_TYPE_VIDEO:
                ret = transcode_video    (ist, &avpkt, &got_output, &pkt_pts, &pkt_dts);
                break;
            case AVMEDIA_TYPE_SUBTITLE:
                ret = transcode_subtitles(ist, &avpkt, &got_output);
    
            // touch data and size only if not EOF
            if (pkt) {
                if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
                    ret = avpkt.size;
                avpkt.data += ret;
                avpkt.size -= ret;
            }
    
        /* handle stream copy */
        if (!ist->decoding_needed) {
            rate_emu_sleep(ist);
    
            ist->pts = ist->next_pts;
    
            switch (ist->st->codec->codec_type) {
            case AVMEDIA_TYPE_AUDIO:
                ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
                                 ist->st->codec->sample_rate;
                break;
            case AVMEDIA_TYPE_VIDEO:
    
                if (pkt->duration) {
                    ist->next_pts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
                } else if(ist->st->codec->time_base.num != 0) {
    
                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
    
                    ist->next_pts += ((int64_t)AV_TIME_BASE *
                                      ist->st->codec->time_base.num * ticks) /
                                      ist->st->codec->time_base.den;
    
        }
        for (i = 0; pkt && i < nb_ostreams; i++) {
    
            OutputStream *ost = &ost_table[i];
    
            if (!check_output_constraints(ist, ost) || ost->encoding_needed)
                continue;
    
            do_streamcopy(ist, ost, pkt);
    
    static void print_sdp(OutputFile *output_files, int n)
    
        AVFormatContext **avc = av_malloc(sizeof(*avc) * n);
    
    
        if (!avc)
            exit_program(1);
        for (i = 0; i < n; i++)
            avc[i] = output_files[i].ctx;
    
        av_sdp_create(avc, n, sdp, sizeof(sdp));
    
    Luca Barbato's avatar
    Luca Barbato committed
        fflush(stdout);
    
    static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
                                 char *error, int error_len)
    {
        InputStream *ist = &input_streams[ist_index];
        if (ist->decoding_needed) {
            AVCodec *codec = ist->dec;
            if (!codec) {
    
                snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
    
                        avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index);
                return AVERROR(EINVAL);
            }
    
            ist->dr1 = codec->capabilities & CODEC_CAP_DR1;
            if (codec->type == AVMEDIA_TYPE_VIDEO && ist->dr1) {
    
                ist->st->codec->get_buffer     = codec_get_buffer;
                ist->st->codec->release_buffer = codec_release_buffer;
                ist->st->codec->opaque         = ist;
            }
    
    
            if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
    
                snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
    
                        ist->file_index, ist->st->index);
                return AVERROR(EINVAL);
            }
            assert_codec_experimental(ist->st->codec, 0);
            assert_avoptions(ist->opts);
        }
    
    
        ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
    
        ist->next_pts = AV_NOPTS_VALUE;
        ist->is_start = 1;
    
        return 0;
    }
    
    
    static int transcode_init(OutputFile *output_files, int nb_output_files,
                              InputFile  *input_files,  int nb_input_files)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        int ret = 0, i, j, k;
    
        AVFormatContext *oc;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVCodecContext *codec, *icodec;
    
        InputStream *ist;
    
        /* init framerate emulation */
        for (i = 0; i < nb_input_files; i++) {
            InputFile *ifile = &input_files[i];
            if (ifile->rate_emu)
                for (j = 0; j < ifile->nb_streams; j++)
                    input_streams[j + ifile->ist_index].start = av_gettime();
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* output stream init */
    
        for (i = 0; i < nb_output_files; i++) {
            oc = output_files[i].ctx;
            if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
                av_dump_format(oc, i, oc->filename, 1);
    
                av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* for each output stream, we compute the right encoding parameters */
    
        for (i = 0; i < nb_output_streams; i++) {
            ost = &output_streams[i];
    
            oc  = output_files[ost->file_index].ctx;
    
            ist = &input_streams[ost->source_index];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            if (ost->attachment_filename)
                continue;
    
    
            codec  = ost->st->codec;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            ost->st->disposition          = ist->st->disposition;
            codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
    
            codec->chroma_sample_location = icodec->chroma_sample_location;
    
            if (ost->stream_copy) {
    
                uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
    
    
                if (extra_size > INT_MAX) {
    
                /* if stream_copy is selected, no need to decode or encode */
    
                codec->codec_id   = icodec->codec_id;
    
                codec->codec_type = icodec->codec_type;
    
                if (!codec->codec_tag) {
                    if (!oc->oformat->codec_tag ||
                         av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
                         av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
    
                codec->bit_rate       = icodec->bit_rate;
    
                codec->rc_max_rate    = icodec->rc_max_rate;
                codec->rc_buffer_size = icodec->rc_buffer_size;
    
                codec->field_order    = icodec->field_order;
    
                codec->extradata      = av_mallocz(extra_size);
    
                if (!codec->extradata) {
    
                memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
    
                codec->extradata_size= icodec->extradata_size;
    
    
                codec->time_base = ist->st->time_base;
    
                if(!strcmp(oc->oformat->name, "avi")) {
    
                    if (   copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base)
                                     && av_q2d(ist->st->time_base) < 1.0/500
                        || copy_tb==0){
    
                        codec->time_base = icodec->time_base;
                        codec->time_base.num *= icodec->ticks_per_frame;
                        codec->time_base.den *= 2;
                    }
    
                } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
                          && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
                          && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
                ) {
    
                    if(   copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base)
                                    && av_q2d(ist->st->time_base) < 1.0/500
                       || copy_tb==0){
    
                        codec->time_base = icodec->time_base;
                        codec->time_base.num *= icodec->ticks_per_frame;
                    }
                }
                av_reduce(&codec->time_base.num, &codec->time_base.den,
                            codec->time_base.num, codec->time_base.den, INT_MAX);
    
    
                switch (codec->codec_type) {
    
                    if (audio_volume != 256) {
    
                        av_log(NULL, AV_LOG_FATAL, "-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;
    
                    codec->pix_fmt            = icodec->pix_fmt;
                    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};
                    }
    
                    ost->st->avg_frame_rate = ist->st->avg_frame_rate;
    
                    codec->width  = icodec->width;
    
                case AVMEDIA_TYPE_DATA:
    
                case AVMEDIA_TYPE_ATTACHMENT:
    
                if (!ost->enc)
                    ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
    
                ist->decoding_needed = 1;
                ost->encoding_needed = 1;
    
                switch (codec->codec_type) {
    
                    ost->fifo = av_fifo_alloc(1024);
    
                    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);
    
                    if (ost->audio_channels_mapped) {
                        /* the requested output channel is set to the number of
                         * -map_channel only if no -ac are specified */
                        if (!codec->channels) {
                            codec->channels       = ost->audio_channels_mapped;
                            codec->channel_layout = av_get_default_channel_layout(codec->channels);
                            if (!codec->channel_layout) {
                                av_log(NULL, AV_LOG_FATAL, "Unable to find an appropriate channel layout for requested number of channel\n");
                                exit_program(1);
                            }
                        }
                        /* fill unused channel mapping with -1 (which means a muted
                         * channel in case the number of output channels is bigger
                         * than the number of mapped channel) */
                        for (j = ost->audio_channels_mapped; j < FF_ARRAY_ELEMS(ost->audio_channels_map); j++)
                            ost->audio_channels_map[j] = -1;
                    } else if (!codec->channels) {
    
                        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;
                    ost->audio_resample      |=    codec->sample_fmt     != icodec->sample_fmt
                                                || codec->channel_layout != icodec->channel_layout;
                    icodec->request_channels  = codec->channels;
    
                    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) {
    
                        av_log(NULL, AV_LOG_FATAL, "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
                    }
    
                    ost->resample_height  = icodec->height;
                    ost->resample_width   = icodec->width;
                    ost->resample_pix_fmt = icodec->pix_fmt;
    
                        ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational) { 25, 1 };
    
                    if (ost->enc && ost->enc->supported_framerates && !ost->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 && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
                        av_log(oc, 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");
                    }
    
                    for (j = 0; j < ost->forced_kf_count; j++)
                        ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j],
                                                             AV_TIME_BASE_Q,
                                                             codec->time_base);
    
    #if CONFIG_AVFILTER
    
                    if (configure_video_filters(ist, ost)) {
    
                        av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (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_PASS2) {
    
                        if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
    
                            av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
                                   logfilename);
    
                            exit_program(1);
    
                    if (codec->flags & CODEC_FLAG_PASS1) {
                        f = fopen(logfilename, "wb");
                        if (!f) {
                            av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
                                   logfilename, strerror(errno));
                            exit_program(1);
                        }
                        ost->logfile = f;
                    }
    
            if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
    
                /* maximum video buffer size is 8-bytes per pixel, plus DPX header size (1664)*/
    
                int size        = codec->width * codec->height;
    
                bit_buffer_size = FFMAX(bit_buffer_size, 9*size + 10000);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        if (!bit_buffer)
            bit_buffer = av_malloc(bit_buffer_size);
    
            av_log(NULL, AV_LOG_ERROR, "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) {
    
                AVCodec      *codec = ost->enc;
    
                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;
    
    
                if (ost->st->codec->me_threshold)
                    input_streams[ost->source_index].st->codec->debug |= FF_DEBUG_MV;
    
    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)
    
        /* discard unused programs */
        for (i = 0; i < nb_input_files; i++) {
            InputFile *ifile = &input_files[i];
            for (j = 0; j < ifile->ctx->nb_programs; j++) {
                AVProgram *p = ifile->ctx->programs[j];
                int discard  = AVDISCARD_ALL;
    
                for (k = 0; k < p->nb_stream_indexes; k++)
                    if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) {
                        discard = AVDISCARD_DEFAULT;
    
                p->discard = discard;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
        /* open files and write file headers */
    
            oc = output_files[i].ctx;
            oc->interrupt_callback = int_cb;
            if (avformat_write_header(oc, &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(oc->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);
    
        av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
        for (i = 0; i < nb_output_streams; i++) {
            ost = &output_streams[i];
    
    
            if (ost->attachment_filename) {
                /* an attached file */
                av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
                       ost->attachment_filename, ost->file_index, ost->index);
                continue;
    
            av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
    
                   input_streams[ost->source_index].file_index,
                   input_streams[ost->source_index].st->index,
                   ost->file_index,
                   ost->index);
    
            if (ost->audio_channels_mapped) {
                av_log(NULL, AV_LOG_INFO, " [ch:");
                for (j = 0; j < ost->audio_channels_mapped; j++)
                    if (ost->audio_channels_map[j] == -1)
                        av_log(NULL, AV_LOG_INFO, " M");
                    else
                        av_log(NULL, AV_LOG_INFO, " %d", ost->audio_channels_map[j]);
                av_log(NULL, AV_LOG_INFO, "]");
            }
    
            if (ost->sync_ist != &input_streams[ost->source_index])
    
                av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
    
                       ost->sync_ist->file_index,
                       ost->sync_ist->st->index);
    
            if (ost->stream_copy)
    
                av_log(NULL, AV_LOG_INFO, " (copy)");
            else
                av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ?
                       input_streams[ost->source_index].dec->name : "?",
                       ost->enc ? ost->enc->name : "?");
            av_log(NULL, AV_LOG_INFO, "\n");
    
            av_log(NULL, AV_LOG_ERROR, "%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) {
    
            av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
    
        timer_start = av_gettime();
    
        for (; received_sigterm == 0;) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            int file_index, ist_index;
            AVPacket pkt;
    
            ipts_min = INT64_MAX;
            opts_min = 1e100;
    
                if(cur_time - last_time >= 100000 && !run_as_daemon){
                    key =  read_key();
                    last_time = cur_time;
                }else
                    key = -1;
    
                if (key == '+') av_log_set_level(av_log_get_level()+10);
                if (key == '-') av_log_set_level(av_log_get_level()-10);
    
                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 buf[4096], target[64], command[256], arg[256] = {0};
                    double time;
    
                    fprintf(stderr, "\nEnter command: <target> <time> <command>[ <argument>]\n");
    
                    i = 0;
                    while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
                        if (k > 0)
                            buf[i++] = k;
                    buf[i] = 0;
    
                    if (k > 0 &&
                        (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
                        av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
                               target, time, command, arg);
    
                        for (i = 0; i < nb_output_streams; i++) {
    
                            if (ost->graph) {
                                if (time < 0) {
                                    ret = avfilter_graph_send_command(ost->graph, target, command, arg, buf, sizeof(buf),
                                                                      key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
                                    fprintf(stderr, "Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
                                } else {
                                    ret = avfilter_graph_queue_command(ost->graph, target, command, arg, 0, time);
    
                        av_log(NULL, AV_LOG_ERROR,
                               "Parse error, at least 3 arguments were expected, "
                               "only %d given in string '%s'\n", n, buf);
    
                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
    
                        if(scanf("%d", &debug)!=1)
                            fprintf(stderr,"error parsing debug value\n");
    
                    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) {
                    if (ipts < ipts_min) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        ipts_min = ipts;
    
                        if (input_sync)
                            file_index = ist->file_index;
    
                    if (opts < opts_min) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        opts_min = opts;
    
                        if (!input_sync) file_index = ist->file_index;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
                if (ost->frame_number >= ost->max_frames) {
                    int j;
    
                    for (j = 0; j < of->ctx->nb_streams; j++)
                        output_streams[of->ost_index + j].is_past_recording_time = 1;
    
    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);
            if (ret == AVERROR(EAGAIN)) {
                no_packet[file_index] = 1;
    
                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
            }
    
            no_packet_count = 0;
    
            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;
    
                    av_log(NULL, AV_LOG_DEBUG,