Skip to content
Snippets Groups Projects
avconv.c 80.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •             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 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)
    
            }
            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];
        if (ist->discard)
            goto discard_packet;
    
        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)
    
    /*
     * 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);
            }
        }
    
        /* finished ! */
        ret = 0;
    
     fail:
    
    #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->opts);
                }
            }
        }
        return ret;
    }
    
    
    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
    
    static void parse_cpuflags(int argc, char **argv, const OptionDef *options)
    
        int idx = locate_option(argc, argv, options, "cpuflags");
        if (idx && argv[idx + 1])
    
            opt_cpuflags(NULL, "cpuflags", argv[idx + 1]);
    
    int main(int argc, char **argv)
    {
    
        OptionsContext o = { 0 };
    
        reset_options(&o);
    
    
        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_cpuflags(argc, argv, options);
    
    
        parse_options(&o, argc, argv, options, opt_output_file);
    
    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);
    
        }
    
        /* file converter / grab */
        if (nb_output_files <= 0) {
            fprintf(stderr, "At least one output file must be specified\n");
    
        ti = getutime() - ti;
        if (do_benchmark) {
            int maxrss = getmaxrss() / 1024;
            printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
        }