Skip to content
Snippets Groups Projects
avconv.c 85.6 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 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 : "?");
                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->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. */
    
    static int need_output(void)
    {
        int i;
    
        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++)
    
                    output_streams[of->ost_index + j]->finished = 1;
    
    static InputFile *select_input_file(void)
    
        InputFile *ifile = NULL;
    
        int64_t ipts_min = INT64_MAX;
    
    
        for (i = 0; i < nb_input_streams; i++) {
            InputStream *ist = input_streams[i];
            int64_t ipts     = ist->last_dts;
    
    
            if (ist->discard || input_files[ist->file_index]->eagain)
    
                continue;
            if (!input_files[ist->file_index]->eof_reached) {
                if (ipts < ipts_min) {
                    ipts_min = ipts;
    
                    ifile    = input_files[ist->file_index];
    
        return ifile;
    
    #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)
    {
        int i;
    
        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);
        }
        return 0;
    }
    
    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->last_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);
    }
    
    
    static int got_eagain(void)
    {
        int i;
        for (i = 0; i < nb_input_files; i++)
            if (input_files[i]->eagain)
                return 1;
        return 0;
    }
    
    static void reset_eagain(void)
    {
        int i;
        for (i = 0; i < nb_input_files; i++)
            input_files[i]->eagain = 0;
    }
    
    
     * Read one packet from an input file and send it for
     * - decoding -> lavfi (audio/video)
     * - decoding -> encoding -> muxing (subtitles)
     * - muxing (streamcopy)
     *
    
     * - 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(void)
    {
        InputFile *ifile;
        AVFormatContext *is;
        InputStream *ist;
        AVPacket pkt;
        int ret, i, j;
    
        /* select the stream that we must read now */
        ifile = select_input_file();
        /* if none, if is finished */
        if (!ifile) {
            if (got_eagain()) {
                reset_eagain();
                av_usleep(10000);
                return AVERROR(EAGAIN);
            }
            av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from.\n");
            return AVERROR_EOF;
        }
    
        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))
    
            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)
            goto discard_packet;
    
        ist = input_streams[ifile->ist_index + pkt.stream_index];
    
        /* add the stream-global side data to the first packet */
        if (ist->nb_packets == 1)
            for (i = 0; i < ist->st->nb_side_data; i++) {
                AVPacketSideData *src_sd = &ist->st->side_data[i];
                uint8_t *dst_data;
    
                if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
                    continue;
    
                dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
                if (!dst_data)
                    exit_program(1);
    
                memcpy(dst_data, src_sd->data, src_sd->size);
            }
    
    
        if (pkt.dts != AV_NOPTS_VALUE)
            pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
        if (pkt.pts != AV_NOPTS_VALUE)
            pkt.pts += av_rescale_q(ifile->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;
    
        if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != 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_dts;
    
            if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
                ifile->ts_offset -= delta;
                av_log(NULL, AV_LOG_DEBUG,
                       "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
                       delta, ifile->ts_offset);
                pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                if (pkt.pts != AV_NOPTS_VALUE)
                    pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
            }
        }
    
        ret = output_packet(ist, &pkt);
        if (ret < 0) {
            av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
                   ist->file_index, ist->st->index);
            if (exit_on_error)
    
                exit_program(1);
    
    /*
     * The following code is the main loop of the file converter
     */
    
    static int transcode(void)
    
        int ret, i, need_input = 1;
        AVFormatContext *os;
    
        OutputStream *ost;
        InputStream *ist;
        int64_t timer_start;
    
    
        av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
    
        term_init();
    
        timer_start = av_gettime();
    
    
    #if HAVE_PTHREADS
    
        if ((ret = init_input_threads()) < 0)
            goto fail;
    #endif
    
    
    Anton Khirnov's avatar
    Anton Khirnov committed
        while (!received_sigterm) {
    
            /* check if there's any stream where output is still needed */
    
            if (!need_output()) {
                av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
    
            /* read and process one input packet if needed */
            if (need_input) {
                ret = process_input();
                if (ret == AVERROR_EOF)
                    need_input = 0;
    
                if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
    
                av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");
                break;
    
            }
    
            /* dump report by using the output first video and audio streams */
    
            print_report(0, timer_start);
    
    #if HAVE_PTHREADS
    
        free_input_threads();
    #endif
    
    
        /* at the end of stream, we must flush the decoder buffers */
        for (i = 0; i < nb_input_streams; i++) {
    
            if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
    
        poll_filters();
    
    
        term_exit();
    
        /* write the trailer if needed and close file */
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (i = 0; i < nb_output_files; i++) {
    
            av_write_trailer(os);
        }
    
        /* dump report by using the first video and audio streams */
    
        print_report(1, timer_start);
    
        for (i = 0; i < nb_output_streams; i++) {
    
            if (ost->encoding_needed) {
                av_freep(&ost->st->codec->stats_in);
                avcodec_close(ost->st->codec);
            }
        }
    
        /* close each decoder */
        for (i = 0; i < nb_input_streams; i++) {
    
            if (ist->decoding_needed) {
                avcodec_close(ist->st->codec);
    
                if (ist->hwaccel_uninit)
                    ist->hwaccel_uninit(ist->st->codec);
    
    #if HAVE_PTHREADS
    
        free_input_threads();
    #endif
    
        if (output_streams) {
            for (i = 0; i < nb_output_streams; i++) {
    
                    if (ost->stream_copy)
    
                        av_freep(&ost->st->codec->extradata);
                    if (ost->logfile) {
                        fclose(ost->logfile);
                        ost->logfile = NULL;
                    }
                    av_freep(&ost->st->codec->subtitle_header);
                    av_free(ost->forced_kf_pts);
    
                    av_dict_free(&ost->encoder_opts);
    
                    av_dict_free(&ost->resample_opts);
    
    static int64_t getutime(void)
    
    #if HAVE_GETRUSAGE
        struct rusage rusage;
    
        getrusage(RUSAGE_SELF, &rusage);
        return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
    #elif HAVE_GETPROCESSTIMES
        HANDLE proc;
        FILETIME c, e, k, u;
        proc = GetCurrentProcess();
        GetProcessTimes(proc, &c, &e, &k, &u);
        return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
    #else
        return av_gettime();
    #endif
    
    static int64_t getmaxrss(void)
    
    #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
        struct rusage rusage;
        getrusage(RUSAGE_SELF, &rusage);
        return (int64_t)rusage.ru_maxrss * 1024;
    #elif HAVE_GETPROCESSMEMORYINFO
        HANDLE proc;
        PROCESS_MEMORY_COUNTERS memcounters;
        proc = GetCurrentProcess();
        memcounters.cb = sizeof(memcounters);
        GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
        return memcounters.PeakPagefileUsage;
    #else
    
    }
    
    int main(int argc, char **argv)
    {
    
        int ret;
    
        register_exit(avconv_cleanup);
    
        av_log_set_flags(AV_LOG_SKIP_REPEATED);
    
        parse_loglevel(argc, argv, options);
    
    
        avcodec_register_all();
    #if CONFIG_AVDEVICE
        avdevice_register_all();
    #endif
        avfilter_register_all();
        av_register_all();
    
        avformat_network_init();
    
        /* parse options and open all input/output files */
        ret = avconv_parse_options(argc, argv);
        if (ret < 0)
    
            exit_program(1);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (nb_output_files <= 0 && nb_input_files == 0) {
    
            av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
    
            exit_program(1);
    
        }
    
        /* file converter / grab */
        if (nb_output_files <= 0) {
            fprintf(stderr, "At least one output file must be specified\n");
    
            exit_program(1);
    
            exit_program(1);
    
        ti = getutime() - ti;
        if (do_benchmark) {
            int maxrss = getmaxrss() / 1024;
            printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
        }
    
    
        exit_program(0);