Skip to content
Snippets Groups Projects
ffmpeg.c 122 KiB
Newer Older
  • Learn to ignore specific revisions
  •     return 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static InputStream *get_input_stream(OutputStream *ost)
    
        if (ost->source_index >= 0)
            return input_streams[ost->source_index];
        return NULL;
    
    static int compare_int64(const void *a, const void *b)
    {
        int64_t va = *(int64_t *)a, vb = *(int64_t *)b;
        return va < vb ? -1 : va > vb ? +1 : 0;
    }
    
    
    static void parse_forced_key_frames(char *kf, OutputStream *ost,
                                        AVCodecContext *avctx)
    
        char *p;
    
        int n = 1, i, size, index = 0;
        int64_t t, *pts;
    
        for (p = kf; *p; p++)
            if (*p == ',')
                n++;
    
        size = n;
        pts = av_malloc(sizeof(*pts) * size);
        if (!pts) {
    
            av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
    
            exit_program(1);
    
        for (i = 0; i < n; i++) {
    
            char *next = strchr(p, ',');
    
            if (!memcmp(p, "chapters", 8)) {
    
                AVFormatContext *avf = output_files[ost->file_index]->ctx;
                int j;
    
                if (avf->nb_chapters > INT_MAX - size ||
                    !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
                                         sizeof(*pts)))) {
                    av_log(NULL, AV_LOG_FATAL,
                           "Could not allocate forced key frames array.\n");
    
                }
                t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
                t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
    
                for (j = 0; j < avf->nb_chapters; j++) {
                    AVChapter *c = avf->chapters[j];
                    av_assert1(index < size);
                    pts[index++] = av_rescale_q(c->start, c->time_base,
                                                avctx->time_base) + t;
                }
    
            } else {
    
                t = parse_time_or_die("force_key_frames", p, 1);
                av_assert1(index < size);
                pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
    
            }
    
    
        av_assert0(index == size);
        qsort(pts, size, sizeof(*pts), compare_int64);
        ost->forced_kf_count = size;
        ost->forced_kf_pts   = pts;
    
    static void report_new_stream(int input_index, AVPacket *pkt)
    
        InputFile *file = input_files[input_index];
        AVStream *st = file->ctx->streams[pkt->stream_index];
    
        if (pkt->stream_index < file->nb_streams_warn)
            return;
        av_log(file->ctx, AV_LOG_WARNING,
               "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
               av_get_media_type_string(st->codec->codec_type),
               input_index, pkt->stream_index,
               pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
        file->nb_streams_warn = pkt->stream_index + 1;
    
    static int transcode_init(void)
    
        AVFormatContext *oc;
    
        AVCodecContext *codec;
    
        InputStream *ist;
        char error[1024];
        int want_sdp = 1;
    
        for (i = 0; i < nb_filtergraphs; i++) {
            FilterGraph *fg = filtergraphs[i];
            for (j = 0; j < fg->nb_outputs; j++) {
                OutputFilter *ofilter = fg->outputs[j];
    
                if (!ofilter->ost || ofilter->ost->source_index >= 0)
    
                    continue;
                if (fg->nb_inputs != 1)
                    continue;
                for (k = nb_input_streams-1; k >= 0 ; k--)
                    if (fg->inputs[0]->ist == input_streams[k])
                        break;
                ofilter->ost->source_index = k;
            }
        }
    
    
        /* 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();
    
        /* output stream init */
    
        for (i = 0; i < nb_output_files; i++) {
    
            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);
    
        /* init complex filtergraphs */
        for (i = 0; i < nb_filtergraphs; i++)
            if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
                return ret;
    
        /* for each output stream, we compute the right encoding parameters */
    
        for (i = 0; i < nb_output_streams; i++) {
    
            AVCodecContext *icodec = NULL;
    
            ost = output_streams[i];
            oc  = output_files[ost->file_index]->ctx;
    
            ist = get_input_stream(ost);
    
            if (ost->attachment_filename)
                continue;
    
            codec  = ost->st->codec;
    
            if (ist) {
                icodec = ist->st->codec;
    
                ost->st->disposition          = ist->st->disposition;
                codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
                codec->chroma_sample_location = icodec->chroma_sample_location;
    
            } else {
                for (j=0; j<oc->nb_streams; j++) {
                    AVStream *st = oc->streams[j];
                    if (st != ost->st && st->codec->codec_type == codec->codec_type)
                        break;
                }
                if (j == oc->nb_streams)
                    if (codec->codec_type == AVMEDIA_TYPE_AUDIO || codec->codec_type == AVMEDIA_TYPE_VIDEO)
                        ost->st->disposition = AV_DISPOSITION_DEFAULT;
    
            if (ost->stream_copy) {
    
                AVRational sar;
    
                av_assert0(ist && !ost->filter);
    
                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) {
    
                    unsigned int codec_tag;
    
                    if (!oc->oformat->codec_tag ||
                         av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
    
                         !av_codec_get_tag2(oc->oformat->codec_tag, icodec->codec_id, &codec_tag))
    
                        codec->codec_tag = icodec->codec_tag;
                }
    
                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->bits_per_coded_sample  = icodec->bits_per_coded_sample;
    
                codec->time_base = ist->st->time_base;
                /*
                 * Avi is a special case here because it supports variable fps but
                 * having the fps and timebase differe significantly adds quite some
                 * overhead
                 */
                if(!strcmp(oc->oformat->name, "avi")) {
                    if ( copy_tb<0 && av_q2d(ist->st->r_frame_rate) >= av_q2d(ist->st->avg_frame_rate)
                                   && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(ist->st->time_base)
                                   && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(icodec->time_base)
                                   && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(icodec->time_base) < 1.0/500
                         || copy_tb==2){
                        codec->time_base.num = ist->st->r_frame_rate.den;
                        codec->time_base.den = 2*ist->st->r_frame_rate.num;
                        codec->ticks_per_frame = 2;
                    } else 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;
                        codec->ticks_per_frame = 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")
    
                          && strcmp(oc->oformat->name, "f4v")
    
                    if(   copy_tb<0 && icodec->time_base.den
                                    && 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;
                    }
                }
    
                if (   codec->codec_tag == AV_RL32("tmcd")
                    && icodec->time_base.num < icodec->time_base.den
                    && icodec->time_base.num > 0
                    && 121LL*icodec->time_base.num > icodec->time_base.den) {
                    codec->time_base = icodec->time_base;
                }
    
                if (ist && !ost->frame_rate.num)
                    ost->frame_rate = ist->framerate;
    
                if(ost->frame_rate.num)
                    codec->time_base = av_inv_q(ost->frame_rate);
    
                av_reduce(&codec->time_base.num, &codec->time_base.den,
                            codec->time_base.num, codec->time_base.den, INT_MAX);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                switch (codec->codec_type) {
    
                case AVMEDIA_TYPE_AUDIO:
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    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;
    
                    if((codec->block_align == 1 || codec->block_align == 1152 || codec->block_align == 576) && codec->codec_id == AV_CODEC_ID_MP3)
    
                        codec->block_align= 0;
                    if(codec->codec_id == AV_CODEC_ID_AC3)
                        codec->block_align= 0;
    
                    break;
                case AVMEDIA_TYPE_VIDEO:
    
                    codec->pix_fmt            = icodec->pix_fmt;
                    codec->width              = icodec->width;
                    codec->height             = icodec->height;
                    codec->has_b_frames       = icodec->has_b_frames;
    
                    if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
    
                            av_mul_q(ost->frame_aspect_ratio,
                                     (AVRational){ codec->height, codec->width });
                        av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
                               "with stream copy may produce invalid files\n");
    
                    else if (ist->st->sample_aspect_ratio.num)
                        sar = ist->st->sample_aspect_ratio;
                    else
                        sar = icodec->sample_aspect_ratio;
                    ost->st->sample_aspect_ratio = codec->sample_aspect_ratio = sar;
    
                    ost->st->avg_frame_rate = ist->st->avg_frame_rate;
    
                    break;
                case AVMEDIA_TYPE_SUBTITLE:
    
                    codec->width  = icodec->width;
    
                    codec->height = icodec->height;
                    break;
                case AVMEDIA_TYPE_DATA:
    
                case AVMEDIA_TYPE_ATTACHMENT:
    
                if (!ost->enc)
                    ost->enc = avcodec_find_encoder(codec->codec_id);
    
                if (!ost->enc) {
                    /* should only happen when a default codec is not present. */
    
                    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;
    
                    ist->decoding_needed++;
    
                ost->encoding_needed = 1;
    
                if (!ost->filter &&
                    (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
                     codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
    
                        FilterGraph *fg;
                        fg = init_simple_filtergraph(ist, ost);
    
                        if (configure_filtergraph(fg)) {
    
                            av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
    
                            exit_program(1);
    
                if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
                    if (ost->filter && !ost->frame_rate.num)
                        ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
    
                    if (ist && !ost->frame_rate.num)
                        ost->frame_rate = ist->framerate;
    
                    if (ist && !ost->frame_rate.num)
                        ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
    //                    ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_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];
                    }
                }
    
                switch (codec->codec_type) {
                case AVMEDIA_TYPE_AUDIO:
                    codec->sample_fmt     = ost->filter->filter->inputs[0]->format;
                    codec->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
                    codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
    
                    codec->channels       = avfilter_link_get_channels(ost->filter->filter->inputs[0]);
    
                    codec->time_base      = (AVRational){ 1, codec->sample_rate };
                    break;
                case AVMEDIA_TYPE_VIDEO:
    
                    codec->time_base = av_inv_q(ost->frame_rate);
                    if (ost->filter && !(codec->time_base.num && codec->time_base.den))
                        codec->time_base = ost->filter->filter->inputs[0]->time_base;
                    if (   av_q2d(codec->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
                       && (video_sync_method == VSYNC_CFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
                        av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
                                                   "Please consider specifying 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);
    
                    codec->width  = ost->filter->filter->inputs[0]->w;
                    codec->height = ost->filter->filter->inputs[0]->h;
                    codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
    
                        ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
                        av_mul_q(ost->frame_aspect_ratio, (AVRational){ codec->height, codec->width }) :
    
                        ost->filter->filter->inputs[0]->sample_aspect_ratio;
    
                    if (!strncmp(ost->enc->name, "libx264", 7) &&
                        codec->pix_fmt == AV_PIX_FMT_NONE &&
                        ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P)
    
                               "No pixel format specified, %s for H.264 encoding chosen.\n"
                               "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
                               av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format));
    
                    if (!strncmp(ost->enc->name, "mpeg2video", 10) &&
                        codec->pix_fmt == AV_PIX_FMT_NONE &&
                        ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P)
                        av_log(NULL, AV_LOG_WARNING,
                               "No pixel format specified, %s for MPEG-2 encoding chosen.\n"
                               "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
                               av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format));
    
                    codec->pix_fmt = ost->filter->filter->inputs[0]->format;
    
                    if (!icodec ||
                        codec->width   != icodec->width  ||
    
                        codec->height  != icodec->height ||
                        codec->pix_fmt != icodec->pix_fmt) {
    
                        codec->bits_per_raw_sample = frame_bits_per_raw_sample;
    
                    if (ost->forced_keyframes) {
                        if (!strncmp(ost->forced_keyframes, "expr:", 5)) {
                            ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5,
                                                forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL);
                            if (ret < 0) {
                                av_log(NULL, AV_LOG_ERROR,
                                       "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5);
                                return ret;
                            }
                            ost->forced_keyframes_expr_const_values[FKF_N] = 0;
                            ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0;
                            ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN;
                            ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN;
                        } else {
                            parse_forced_key_frames(ost->forced_keyframes, ost, ost->st->codec);
                        }
                    }
    
                    break;
                case AVMEDIA_TYPE_SUBTITLE:
    
                    codec->time_base = (AVRational){1, 1000};
    
                    if (!codec->width) {
                        codec->width     = input_streams[ost->source_index]->st->codec->width;
                        codec->height    = input_streams[ost->source_index]->st->codec->height;
                    }
    
                    break;
                default:
                    abort();
                    break;
                }
                /* two pass mode */
    
                if (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
    
                    char logfilename[1024];
                    FILE *f;
    
                    snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
    
                             ost->logfile_prefix ? ost->logfile_prefix :
                                                   DEFAULT_PASS_LOGFILENAME_PREFIX,
    
                    if (!strcmp(ost->enc->name, "libx264")) {
                        av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
                    } else {
    
                        if (codec->flags & CODEC_FLAG_PASS2) {
    
    Anton Khirnov's avatar
    Anton Khirnov committed
                            char  *logbuffer;
                            size_t logbuffer_size;
                            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);
    
    Anton Khirnov's avatar
    Anton Khirnov committed
                            }
                            codec->stats_in = logbuffer;
    
                        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));
    
        /* open each encoder */
    
        for (i = 0; i < nb_output_streams; i++) {
    
            if (ost->encoding_needed) {
    
                AVCodec      *codec = ost->enc;
    
                AVCodecContext *dec = NULL;
    
                if ((ist = get_input_stream(ost)))
                    dec = ist->st->codec;
                if (dec && dec->subtitle_header) {
    
                    /* ASS code assumes this buffer is null terminated so add extra byte. */
                    ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
    
                    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 (!av_dict_get(ost->opts, "threads", NULL, 0))
                    av_dict_set(&ost->opts, "threads", "auto", 0);
    
                if ((ret = avcodec_open2(ost->st->codec, codec, &ost->opts)) < 0) {
                    if (ret == AVERROR_EXPERIMENTAL)
                        abort_codec_experimental(codec, 1);
    
                    snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
    
                            ost->file_index, ost->index);
                    goto dump_format;
    
                if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
                    !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
                    av_buffersink_set_frame_size(ost->filter->filter,
                                                 ost->st->codec->frame_size);
    
                assert_avoptions(ost->opts);
                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;
    
            } else {
                av_opt_set_dict(ost->st->codec, &ost->opts);
    
        /* init input streams */
        for (i = 0; i < nb_input_streams; i++)
    
            if ((ret = init_input_stream(i, error, sizeof(error))) < 0) {
                for (i = 0; i < nb_output_streams; i++) {
                    ost = output_streams[i];
                    avcodec_close(ost->st->codec);
                }
    
        /* 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;
                        break;
                    }
                p->discard = discard;
    
        /* open files and write file headers */
    
            oc->interrupt_callback = int_cb;
    
            if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
                char errbuf[128];
    
                av_strerror(ret, errbuf, sizeof(errbuf));
                snprintf(error, sizeof(error),
                         "Could not write header for output file #%d "
                         "(incorrect codec parameters ?): %s",
                         i, errbuf);
    
                ret = AVERROR(EINVAL);
                goto dump_format;
    
    //         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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    
        /* dump the stream mapping */
    
        av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
    
        for (i = 0; i < nb_input_streams; i++) {
            ist = input_streams[i];
    
            for (j = 0; j < ist->nb_filters; j++) {
                if (ist->filters[j]->graph->graph_desc) {
                    av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",
                           ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
    
                    if (nb_filtergraphs > 1)
                        av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
                    av_log(NULL, AV_LOG_INFO, "\n");
                }
    
        for (i = 0; i < nb_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;
    
            if (ost->filter && ost->filter->graph->graph_desc) {
                /* output from a complex graph */
    
                av_log(NULL, AV_LOG_INFO, "  %s", ost->filter->name);
    
                if (nb_filtergraphs > 1)
                    av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
    
                av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
                       ost->index, ost->enc ? ost->enc->name : "?");
    
            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->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);
    
    /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
    
        for (i = 0; i < nb_output_streams; i++) {
            OutputStream *ost    = output_streams[i];
            OutputFile *of       = output_files[ost->file_index];
            AVFormatContext *os  = output_files[ost->file_index]->ctx;
    
                (os->pb && avio_tell(os->pb) >= of->limit_filesize))
                continue;
    
            if (ost->frame_number >= ost->max_frames) {
    
                int j;
                for (j = 0; j < of->ctx->nb_streams; j++)
    
                    close_output_stream(output_streams[of->ost_index + j]);
    
     * Select the output stream to process.
    
     * @return  selected output stream, or NULL if none available
    
    static OutputStream *choose_output(void)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        int i;
        int64_t opts_min = INT64_MAX;
        OutputStream *ost_min = NULL;
    
        for (i = 0; i < nb_output_streams; i++) {
            OutputStream *ost = output_streams[i];
            int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base,
                                        AV_TIME_BASE_Q);
            if (!ost->unavailable && !ost->finished && opts < opts_min) {
                opts_min = opts;
                ost_min  = ost;
    
    static int check_keyboard_interaction(int64_t cur_time)
    {
        int i, ret, key;
        static int64_t last_time;
        if (received_nb_signals)
            return AVERROR_EXIT;
        /* read_key() returns 0 on EOF */
        if(cur_time - last_time >= 100000 && !run_as_daemon){
            key =  read_key();
            last_time = cur_time;
        }else
            key = -1;
        if (key == 'q')
            return AVERROR_EXIT;
        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 (key == 'c' || key == 'C'){
            char buf[4096], target[64], command[256], arg[256] = {0};
            double time;
            int k, n = 0;
    
            fprintf(stderr, "\nEnter command: <target>|all <time>|-1 <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_filtergraphs; i++) {
                    FilterGraph *fg = filtergraphs[i];
                    if (fg->graph) {
                        if (time < 0) {
                            ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
                                                              key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
    
                            fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
    
                        } else if (key == 'c') {
                            fprintf(stderr, "Queing commands only on filters supporting the specific command is unsupported\n");
                            ret = AVERROR_PATCHWELCOME;
    
                        } else {
                            ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
                        }
                    }
    
            } else {
                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++) {
                OutputStream *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 first matching filter supporting it\n"
                            "C      Send/Que command to all matching filters\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"
            );
        }
        return 0;
    
    #if HAVE_PTHREADS
    
    static void *input_thread(void *arg)
    
        InputFile *f = arg;
        int ret = 0;
    
        while (!transcoding_finished && ret >= 0) {
            AVPacket pkt;
            ret = av_read_frame(f->ctx, &pkt);
    
            if (ret == AVERROR(EAGAIN)) {
    
                av_usleep(10000);
    
                ret = 0;
                continue;
            } else if (ret < 0)
                break;
    
            pthread_mutex_lock(&f->fifo_lock);
            while (!av_fifo_space(f->fifo))
                pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
    
            av_dup_packet(&pkt);
            av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
    
            pthread_mutex_unlock(&f->fifo_lock);
    
        f->finished = 1;
        return NULL;
    
    static void free_input_threads(void)
    
        if (nb_input_files == 1)
            return;
    
        transcoding_finished = 1;
    
        for (i = 0; i < nb_input_files; i++) {
            InputFile *f = input_files[i];
            AVPacket pkt;
    
                continue;
    
            pthread_mutex_lock(&f->fifo_lock);
            while (av_fifo_size(f->fifo)) {
                av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
                av_free_packet(&pkt);
            }
            pthread_cond_signal(&f->fifo_cond);
            pthread_mutex_unlock(&f->fifo_lock);
    
            pthread_join(f->thread, NULL);
            f->joined = 1;
    
            while (av_fifo_size(f->fifo)) {
                av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
                av_free_packet(&pkt);
            }
            av_fifo_free(f->fifo);
    
    static int init_input_threads(void)
    
        int i, ret;
    
        if (nb_input_files == 1)
            return 0;
    
        for (i = 0; i < nb_input_files; i++) {
            InputFile *f = input_files[i];
    
            if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
    
                return AVERROR(ENOMEM);
    
    
            pthread_mutex_init(&f->fifo_lock, NULL);
            pthread_cond_init (&f->fifo_cond, NULL);
    
            if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
                return AVERROR(ret);
    
    static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
    
        int ret = 0;
    
        pthread_mutex_lock(&f->fifo_lock);
    
        if (av_fifo_size(f->fifo)) {
            av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
            pthread_cond_signal(&f->fifo_cond);
        } else {
            if (f->finished)
                ret = AVERROR_EOF;
            else
                ret = AVERROR(EAGAIN);
    
        pthread_mutex_unlock(&f->fifo_lock);
    
        return ret;
    }
    #endif
    
    static int get_input_packet(InputFile *f, AVPacket *pkt)
    {
    
        if (f->rate_emu) {
            int i;
            for (i = 0; i < f->nb_streams; i++) {
                InputStream *ist = input_streams[f->ist_index + i];
    
                int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
    
                int64_t now = av_gettime() - ist->start;
                if (pts > now)
                    return AVERROR(EAGAIN);
            }
        }
    
    
    #if HAVE_PTHREADS
    
        if (nb_input_files > 1)
            return get_input_packet_mt(f, pkt);
    #endif
        return av_read_frame(f->ctx, pkt);
    
        for (i = 0; i < nb_output_streams; i++)
            if (output_streams[i]->unavailable)
    
                return 1;
        return 0;
    }
    
    static void reset_eagain(void)
    {
        int i;
        for (i = 0; i < nb_input_files; i++)
            input_files[i]->eagain = 0;
    
        for (i = 0; i < nb_output_streams; i++)
            output_streams[i]->unavailable = 0;
    }
    
    
     * - 0 -- one packet was read and processed
     * - AVERROR(EAGAIN) -- no packets were available for selected file,
     *   this function should be called again
     * - AVERROR_EOF -- this function should not be called again
     */
    
    static int process_input(int file_index)
    
        InputFile *ifile = input_files[file_index];
    
        AVFormatContext *is;
        InputStream *ist;
        AVPacket pkt;
        int ret, i, j;
    
        is  = ifile->ctx;
        ret = get_input_packet(ifile, &pkt);
    
        if (ret == AVERROR(EAGAIN)) {
            ifile->eagain = 1;
            return ret;
        }
        if (ret < 0) {
            if (ret != AVERROR_EOF) {
                print_error(is->filename, ret);
                if (exit_on_error)
    
                    exit_program(1);
    
            }
            ifile->eof_reached = 1;
    
            for (i = 0; i < ifile->nb_streams; i++) {
                ist = input_streams[ifile->ist_index + i];
                if (ist->decoding_needed)
                    output_packet(ist, NULL);
    
    
                /* mark all outputs that don't go through lavfi as finished */
                for (j = 0; j < nb_output_streams; j++) {
                    OutputStream *ost = output_streams[j];
    
                    if (ost->source_index == ifile->ist_index + i &&
                        (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
                        close_output_stream(ost);
    
            return AVERROR(EAGAIN);
    
        }
    
        reset_eagain();
    
        if (do_pkt_dump) {
            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 >= ifile->nb_streams) {
            report_new_stream(file_index, &pkt);
            goto discard_packet;
        }
    
        ist = input_streams[ifile->ist_index + pkt.stream_index];
        if (ist->discard)
            goto discard_packet;
    
    
        if (debug_ts) {
            av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
    
                   "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
                   ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->st->codec->codec_type),
                   av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
                   av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
                   av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
                   av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
                   av_ts2str(input_files[ist->file_index]->ts_offset),
                   av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
    
        if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
    
            int64_t stime, stime2;
    
            // Correcting starttime based on the enabled streams