Skip to content
Snippets Groups Projects
ffmpeg.c 134 KiB
Newer Older
  • Learn to ignore specific revisions
  •     for (i = 0; i < nb_output_streams; i++) {
    
            OutputStream *ost = output_streams[i];
    
            if (!check_output_constraints(ist, ost) || !ost->encoding_needed
                || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
    
            do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
    
        avsubtitle_free(&subtitle);
    
    /* pkt = NULL means EOF (needed to flush decoder buffers) */
    
    static int output_packet(InputStream *ist, const AVPacket *pkt)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
        if (!ist->saw_first_ts) {
    
            ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
    
            ist->pts = 0;
            if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
                ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
                ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
    
        if (ist->next_dts == AV_NOPTS_VALUE)
    
            ist->next_dts = ist->dts;
        if (ist->next_pts == AV_NOPTS_VALUE)
            ist->next_pts = ist->pts;
    
        if (pkt == NULL) {
            /* EOF handling */
            av_init_packet(&avpkt);
            avpkt.data = NULL;
            avpkt.size = 0;
            goto handle_eof;
        } else {
            avpkt = *pkt;
        }
    
        if (pkt->dts != AV_NOPTS_VALUE) {
            ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
    
            if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
    
                ist->next_pts = ist->pts = ist->dts;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        // 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;
            ist->dts = ist->next_dts;
    
            if (avpkt.size && avpkt.size != pkt->size &&
                !(ist->dec->capabilities & CODEC_CAP_SUBFRAMES)) {
    
                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;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            }
    
            switch (ist->dec_ctx->codec_type) {
    
                ret = decode_audio    (ist, &avpkt, &got_output);
    
                break;
            case AVMEDIA_TYPE_VIDEO:
    
                ret = decode_video    (ist, &avpkt, &got_output);
    
                if (avpkt.duration) {
                    duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
    
                } else if(ist->dec_ctx->time_base.num != 0 && ist->dec_ctx->time_base.den != 0) {
                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
    
                    duration = ((int64_t)AV_TIME_BASE *
    
                                    ist->dec_ctx->time_base.num * ticks) /
                                    ist->dec_ctx->time_base.den;
    
                if(ist->dts != AV_NOPTS_VALUE && duration) {
                    ist->next_dts += duration;
                }else
                    ist->next_dts = AV_NOPTS_VALUE;
    
                if (got_output)
                    ist->next_pts += duration; //FIXME the duration is not correct in some cases
    
                break;
            case AVMEDIA_TYPE_SUBTITLE:
                ret = transcode_subtitles(ist, &avpkt, &got_output);
                break;
    
    Anton Khirnov's avatar
    Anton Khirnov committed
            default:
                return -1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            avpkt.dts=
            avpkt.pts= AV_NOPTS_VALUE;
    
            // touch data and size only if not EOF
            if (pkt) {
    
                if(ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO)
    
                avpkt.data += ret;
                avpkt.size -= ret;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
        }
    
    
        if (!ist->decoding_needed) {
    
            ist->dts = ist->next_dts;
    
            switch (ist->dec_ctx->codec_type) {
    
            case AVMEDIA_TYPE_AUDIO:
    
                ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
                                 ist->dec_ctx->sample_rate;
    
                break;
            case AVMEDIA_TYPE_VIDEO:
    
                    // TODO: Remove work-around for c99-to-c89 issue 7
                    AVRational time_base_q = AV_TIME_BASE_Q;
                    int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
                    ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
    
                    ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
    
                } else if(ist->dec_ctx->time_base.num != 0) {
                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
    
                    ist->next_dts += ((int64_t)AV_TIME_BASE *
    
                                      ist->dec_ctx->time_base.num * ticks) /
                                      ist->dec_ctx->time_base.den;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            ist->pts = ist->dts;
            ist->next_pts = ist->next_dts;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        for (i = 0; pkt && i < nb_output_streams; i++) {
    
            OutputStream *ost = output_streams[i];
    
            if (!check_output_constraints(ist, ost) || ost->encoding_needed)
                continue;
    
    static void print_sdp(void)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
        AVFormatContext **avc = av_malloc_array(nb_output_files, sizeof(*avc));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            exit_program(1);
    
        for (i = 0; i < nb_output_files; i++)
    
        av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
    
        printf("SDP:\n%s\n", sdp);
        fflush(stdout);
    
    static const HWAccel *get_hwaccel(enum AVPixelFormat pix_fmt)
    {
        int i;
        for (i = 0; hwaccels[i].name; i++)
            if (hwaccels[i].pix_fmt == pix_fmt)
                return &hwaccels[i];
        return NULL;
    }
    
    static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
    {
        InputStream *ist = s->opaque;
        const enum AVPixelFormat *p;
        int ret;
    
        for (p = pix_fmts; *p != -1; p++) {
            const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
            const HWAccel *hwaccel;
    
            if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
                break;
    
            hwaccel = get_hwaccel(*p);
            if (!hwaccel ||
                (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
                (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
                continue;
    
            ret = hwaccel->init(s);
            if (ret < 0) {
                if (ist->hwaccel_id == hwaccel->id) {
                    av_log(NULL, AV_LOG_FATAL,
                           "%s hwaccel requested for input stream #%d:%d, "
                           "but cannot be initialized.\n", hwaccel->name,
                           ist->file_index, ist->st->index);
                    exit_program(1);
                }
                continue;
            }
            ist->active_hwaccel_id = hwaccel->id;
            ist->hwaccel_pix_fmt   = *p;
            break;
        }
    
        return *p;
    }
    
    static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
    {
        InputStream *ist = s->opaque;
    
        if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
            return ist->hwaccel_get_buffer(s, frame, flags);
    
        return avcodec_default_get_buffer2(s, frame, flags);
    }
    
    
    static int init_input_stream(int ist_index, 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->dec_ctx->codec_id), ist->file_index, ist->st->index);
    
                return AVERROR(EINVAL);
    
            ist->dec_ctx->opaque                = ist;
            ist->dec_ctx->get_format            = get_format;
            ist->dec_ctx->get_buffer2           = get_buffer;
            ist->dec_ctx->thread_safe_callbacks = 1;
    
            av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
    
            if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
                av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
    
            if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
    
                if (ret == AVERROR_EXPERIMENTAL)
                    abort_codec_experimental(codec, 0);
    
    
                snprintf(error, error_len,
                         "Error while opening decoder for input stream "
                         "#%d:%d : %s",
    
                         ist->file_index, ist->st->index, av_err2str(ret));
    
            assert_avoptions(ist->decoder_opts);
    
        ist->next_pts = AV_NOPTS_VALUE;
    
        ist->next_dts = AV_NOPTS_VALUE;
    
        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++;
    
        pts = av_malloc_array(size, sizeof(*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 void set_encoder_id(OutputFile *of, OutputStream *ost)
    {
        AVDictionaryEntry *e;
    
        uint8_t *encoder_string;
        int encoder_string_len;
        int format_flags = 0;
    
        if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
            return;
    
    
        e = av_dict_get(of->opts, "fflags", NULL, 0);
        if (e) {
            const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
            if (!o)
                return;
            av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
        }
    
        e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
        if (e) {
            const AVOption *o = av_opt_find(ost->st->codec, "flags", NULL, 0, 0);
            if (!o)
                return;
            av_opt_eval_flags(ost->st->codec, o, e->value, &codec_flags);
        }
    
    
        encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
        encoder_string     = av_mallocz(encoder_string_len);
        if (!encoder_string)
            exit_program(1);
    
    
        if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & CODEC_FLAG_BITEXACT))
    
            av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
    
        else
            av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
    
        av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
        av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
                    AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
    }
    
    
    static int transcode_init(void)
    
        AVFormatContext *oc;
    
        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_relative();
    
        /* 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 *enc_ctx;
    
            AVCodecContext *dec_ctx = NULL;
    
            ost = output_streams[i];
            oc  = output_files[ost->file_index]->ctx;
    
            ist = get_input_stream(ost);
    
            if (ost->attachment_filename)
                continue;
    
                ost->st->disposition          = ist->st->disposition;
    
                enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
                enc_ctx->chroma_sample_location = dec_ctx->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 == enc_ctx->codec_type)
    
                    if (enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO || enc_ctx->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)dec_ctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
    
                if (extra_size > INT_MAX) {
    
                /* if stream_copy is selected, no need to decode or encode */
    
                enc_ctx->codec_id   = dec_ctx->codec_id;
                enc_ctx->codec_type = dec_ctx->codec_type;
    
                if (!enc_ctx->codec_tag) {
    
                    unsigned int codec_tag;
    
                    if (!oc->oformat->codec_tag ||
    
                         av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
    
                         !av_codec_get_tag2(oc->oformat->codec_tag, dec_ctx->codec_id, &codec_tag))
    
                        enc_ctx->codec_tag = dec_ctx->codec_tag;
    
                enc_ctx->bit_rate       = dec_ctx->bit_rate;
                enc_ctx->rc_max_rate    = dec_ctx->rc_max_rate;
                enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
                enc_ctx->field_order    = dec_ctx->field_order;
                enc_ctx->extradata      = av_mallocz(extra_size);
                if (!enc_ctx->extradata) {
    
                memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
    
                enc_ctx->extradata_size= dec_ctx->extradata_size;
                enc_ctx->bits_per_coded_sample  = dec_ctx->bits_per_coded_sample;
    
                enc_ctx->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(dec_ctx->time_base)
                                   && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
    
                        enc_ctx->time_base.num = ist->st->r_frame_rate.den;
                        enc_ctx->time_base.den = 2*ist->st->r_frame_rate.num;
                        enc_ctx->ticks_per_frame = 2;
    
                    } else if (   copy_tb<0 && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->st->time_base)
    
                                     && av_q2d(ist->st->time_base) < 1.0/500
                        || copy_tb==0){
    
                        enc_ctx->time_base = dec_ctx->time_base;
                        enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
                        enc_ctx->time_base.den *= 2;
                        enc_ctx->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 && dec_ctx->time_base.den
                                    && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->st->time_base)
    
                                    && av_q2d(ist->st->time_base) < 1.0/500
                       || copy_tb==0){
    
                        enc_ctx->time_base = dec_ctx->time_base;
                        enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
    
                if (   enc_ctx->codec_tag == AV_RL32("tmcd")
    
                    && dec_ctx->time_base.num < dec_ctx->time_base.den
                    && dec_ctx->time_base.num > 0
                    && 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
    
                    enc_ctx->time_base = dec_ctx->time_base;
    
                if (ist && !ost->frame_rate.num)
                    ost->frame_rate = ist->framerate;
    
                if(ost->frame_rate.num)
    
                    enc_ctx->time_base = av_inv_q(ost->frame_rate);
    
                av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
                            enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
    
                ost->parser = av_parser_init(enc_ctx->codec_id);
    
                switch (enc_ctx->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);
    
                    enc_ctx->channel_layout     = dec_ctx->channel_layout;
                    enc_ctx->sample_rate        = dec_ctx->sample_rate;
                    enc_ctx->channels           = dec_ctx->channels;
                    enc_ctx->frame_size         = dec_ctx->frame_size;
                    enc_ctx->audio_service_type = dec_ctx->audio_service_type;
                    enc_ctx->block_align        = dec_ctx->block_align;
    
                    enc_ctx->delay              = dec_ctx->delay;
                    if((enc_ctx->block_align == 1 || enc_ctx->block_align == 1152 || enc_ctx->block_align == 576) && enc_ctx->codec_id == AV_CODEC_ID_MP3)
                        enc_ctx->block_align= 0;
                    if(enc_ctx->codec_id == AV_CODEC_ID_AC3)
                        enc_ctx->block_align= 0;
    
                    break;
                case AVMEDIA_TYPE_VIDEO:
    
                    enc_ctx->pix_fmt            = dec_ctx->pix_fmt;
                    enc_ctx->width              = dec_ctx->width;
                    enc_ctx->height             = dec_ctx->height;
                    enc_ctx->has_b_frames       = dec_ctx->has_b_frames;
    
                    if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
    
                            av_mul_q(ost->frame_aspect_ratio,
    
                                     (AVRational){ enc_ctx->height, enc_ctx->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 = dec_ctx->sample_aspect_ratio;
    
                    ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
    
                    ost->st->avg_frame_rate = ist->st->avg_frame_rate;
    
                    break;
                case AVMEDIA_TYPE_SUBTITLE:
    
                    enc_ctx->width  = dec_ctx->width;
                    enc_ctx->height = dec_ctx->height;
    
                    break;
                case AVMEDIA_TYPE_DATA:
    
                case AVMEDIA_TYPE_ATTACHMENT:
    
                    ost->enc = avcodec_find_encoder(enc_ctx->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;
    
                set_encoder_id(output_files[ost->file_index], ost);
    
    
                if (!ost->filter &&
    
                    (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
                     enc_ctx->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 (enc_ctx->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;
                    if (ist && !ost->frame_rate.num) {
                        ost->frame_rate = (AVRational){25, 1};
                        av_log(NULL, AV_LOG_WARNING,
                               "No information "
                               "about the input framerate is available. Falling "
                               "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
                               "if you want a different framerate.\n",
                               ost->file_index, ost->index);
                    }
    
    //                    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];
                    }
    
                    if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
    
                        av_reduce(&ost->frame_rate.num, &ost->frame_rate.den,
                                  ost->frame_rate.num, ost->frame_rate.den, 65535);
                    }
    
                switch (enc_ctx->codec_type) {
    
                case AVMEDIA_TYPE_AUDIO:
    
                    enc_ctx->sample_fmt     = ost->filter->filter->inputs[0]->format;
                    enc_ctx->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
                    enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
    
                    enc_ctx->channels       = avfilter_link_get_channels(ost->filter->filter->inputs[0]);
    
                    enc_ctx->time_base      = (AVRational){ 1, enc_ctx->sample_rate };
    
                    break;
                case AVMEDIA_TYPE_VIDEO:
    
                    enc_ctx->time_base = av_inv_q(ost->frame_rate);
                    if (ost->filter && !(enc_ctx->time_base.num && enc_ctx->time_base.den))
                        enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
                    if (   av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
    
                       && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (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,
    
                    enc_ctx->width  = ost->filter->filter->inputs[0]->w;
                    enc_ctx->height = ost->filter->filter->inputs[0]->h;
                    enc_ctx->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){ enc_ctx->height, enc_ctx->width }) :
    
                        ost->filter->filter->inputs[0]->sample_aspect_ratio;
    
                    if (!strncmp(ost->enc->name, "libx264", 7) &&
    
                        enc_ctx->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) &&
    
                        enc_ctx->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));
    
                    enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
    
                    ost->st->avg_frame_rate = ost->frame_rate;
    
    
                        enc_ctx->width   != dec_ctx->width  ||
                        enc_ctx->height  != dec_ctx->height ||
                        enc_ctx->pix_fmt != dec_ctx->pix_fmt) {
                        enc_ctx->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->enc_ctx);
    
                    break;
                case AVMEDIA_TYPE_SUBTITLE:
    
                    enc_ctx->time_base = (AVRational){1, 1000};
    
                    if (!enc_ctx->width) {
                        enc_ctx->width     = input_streams[ost->source_index]->st->codec->width;
                        enc_ctx->height    = input_streams[ost->source_index]->st->codec->height;
    
                    break;
                default:
                    abort();
                    break;
                }
                /* two pass mode */
    
                if (enc_ctx->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->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
    
                        if (enc_ctx->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
                            }
    
                            enc_ctx->stats_in = logbuffer;
    
                        if (enc_ctx->flags & CODEC_FLAG_PASS1) {
    
                            f = av_fopen_utf8(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)))
    
                if (dec && dec->subtitle_header) {
    
                    /* ASS code assumes this buffer is null terminated so add extra byte. */
    
                    ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
                    if (!ost->enc_ctx->subtitle_header) {
    
                        ret = AVERROR(ENOMEM);
                        goto dump_format;
    
                    memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
                    ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
    
                if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
                    av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
    
                av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
    
                if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_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,
    
                assert_avoptions(ost->encoder_opts);
    
                if (ost->enc_ctx->bit_rate && ost->enc_ctx->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");
    
                av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
            }
    
            ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
            if (ret < 0) {
                av_log(NULL, AV_LOG_FATAL,
                       "Error initializing the output stream codec context.\n");
                exit_program(1);
    
        /* 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->enc_ctx);
    
        /* 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) {
    
                snprintf(error, sizeof(error),
                         "Could not write header for output file #%d "
                         "(incorrect codec parameters ?): %s",
    
                         i, av_err2str(ret));
    
                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;