ffmpeg源码分析二:main函数和transcode函数

本文详细解析了FFmpeg的音视频转换流程,从main函数入手,介绍了参数解析、输入输出文件处理、编码器与解码器初始化、音视频转换等核心步骤,并对关键函数进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先从main函数看起,关键解释部分已加注释,该函数在ffmpeg.c文件中。代码如下:

int main(int argc, char **argv)
{
    int ret;
    int64_t ti;
    // 注册清理回调函数
    register_exit(ffmpeg_cleanup);

    setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */

    av_log_set_flags(AV_LOG_SKIP_REPEATED);
    parse_loglevel(argc, argv, options);

    if(argc>1 && !strcmp(argv[1], "-d")){
        run_as_daemon=1;
        av_log_set_callback(log_callback_null);
        argc--;
        argv++;
    }

    // 注册组件们
    avcodec_register_all();
#if CONFIG_AVDEVICE
    avdevice_register_all();
#endif
    avfilter_register_all();
    av_register_all();
    avformat_network_init();

    show_banner(argc, argv, options);

    term_init();

    /* parse options and open all input/output files */
    // 解析参数,并且打开输入输出文件
    ret = ffmpeg_parse_options(argc, argv);
    if (ret < 0)
        exit_program(1);

    if (nb_output_files <= 0 && nb_input_files == 0) {
        show_usage();
        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) {
        av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
        exit_program(1);
    }

//     if (nb_input_files == 0) {
//         av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
//         exit_program(1);
//     }

    current_time = ti = getutime();
    // 音视频转换函数
    if (transcode() < 0)
        exit_program(1);
    ti = getutime() - ti;
    if (do_benchmark) {
        printf("bench: utime=%0.3fs\n", ti / 1000000.0);
    }
    av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
           decode_error_stat[0], decode_error_stat[1]);
    if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
        exit_program(69);

    exit_program(received_nb_signals ? 255 : main_return_code);
    return main_return_code;
}

下面阅读transcode函数:

static int transcode(void)
{
    int ret, i;
    AVFormatContext *os;
    OutputStream *ost;
    InputStream *ist;
    int64_t timer_start;

    // 初始化,打开所有输出流的编码器,打开所有输入流的解码器,写入所有输出文件的文件头。后面详细介绍。
    ret = transcode_init();
    if (ret < 0)
        goto fail;

    if (stdin_interaction) {
        av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
    }

    timer_start = av_gettime();

#if HAVE_PTHREADS
    if ((ret = init_input_threads()) < 0)
        goto fail;
#endif
   
    // 循环,直到收到系统信号才退出 。
    while (!received_sigterm) {
        int64_t cur_time= av_gettime();

        /* if 'q' pressed, exits */
        if (stdin_interaction)
            if (check_keyboard_interaction(cur_time) < 0)
                break;

        /* 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");
            break;
        }

        // 具体的转换工作,后面将详细介绍。
        ret = transcode_step();
        if (ret < 0) {
            if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
                continue;

            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, cur_time);
    }
#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++) {
        ist = input_streams[i];
        if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
            output_packet(ist, NULL);
        }
    }
    flush_encoders();

    term_exit();

    /* write the trailer if needed and close file */
    // 为输出文件写文件尾(有的不需要)。  
    for (i = 0; i < nb_output_files; i++) {
        os = output_files[i]->ctx;
        av_write_trailer(os);
    }

    /* dump report by using the first video and audio streams */
    print_report(1, timer_start, av_gettime());

    // 关闭所有编码器。
    /* close each encoder */
    for (i = 0; i < nb_output_streams; i++) {
        ost = 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++) {
        ist = input_streams[i];
        if (ist->decoding_needed) {
            avcodec_close(ist->st->codec);
            if (ist->hwaccel_uninit)
                ist->hwaccel_uninit(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++) {
            ost = output_streams[i];
            if (ost) {
                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_freep(&ost->forced_kf_pts);
                av_freep(&ost->apad);
                av_dict_free(&ost->opts);
                av_dict_free(&ost->swr_opts);
                av_dict_free(&ost->resample_opts);
            }
        }
    }
    return ret;
}



<think>我们正在处理一个关于FFmpeg4.3版本转码源码的问题。用户要求查找转码相关的源码部分。根据之前的引用内容,特别是引用[3]中提到了转码流程中涉及的关键函数流程,我们可以以此为基础进行扩展。FFmpeg转码的基本流程包括:解封装(demuxing)、解码(decoding)、编码(encoding)封装(muxing)。在FFmpeg源码中,转码过程通常由`ffmpeg.c`这个文件中的代码实现,它包含了整个转码流程的控制逻辑。在FFmpeg4.3版本中,转码的主要源码位于:-`ffmpeg.c`:主程序,控制整个转码流程。-`libavformat`:处理封装解封装。-`libavcodec`:处理编解码。-`libavfilter`:处理滤镜(如缩放、裁剪等)。根据引用[3]的内容,我们还可以知道一些关键函数:-`avformat_open_input()`:打开输入文件。-`avformat_find_stream_info()`:获取流信息。-`avcodec_open2()`:打开编解码器。-`av_read_frame()`:读取一个AVPacket(压缩的数据包)。-对于视频,使用`avcodec_decode_video2()`(在较新版本中可能被`avcodec_send_packet()``avcodec_receive_frame()`替代)进行解码。-对于音频,使用`avcodec_decode_audio4()`(同样,新版本中可能被替代)。-编码部分:使用`avcodec_encode_video2()`(视频编码)`avcodec_encode_audio2()`(音频编码),但在新版本中也被`avcodec_send_frame()``avcodec_receive_packet()`替代。-滤镜处理:通过`libavfilter`进行,使用`av_buffersrc_add_frame()``av_buffersink_get_frame()`等函数。-最后,使用`av_interleaved_write_frame()`将编码后的数据包写入输出文件。注意:FFmpeg4.3版本已经采用了新的编解码API(即`avcodec_send_packet``avcodec_receive_frame`用于解码,`avcodec_send_frame``avcodec_receive_packet`用于编码)。因此,在源码中查找这些函数的使用会更有帮助。具体步骤:1.打开输入文件(解封装):在`ffmpeg.c`中,函数`open_input_file()`负责打开输入文件,并初始化输入流的编解码器。2.打开输出文件(封装):`open_output_file()`函数负责打开输出文件,并初始化输出流的编解码器封装格式。3.初始化滤镜:`init_filters()`函数负责初始化滤镜图。4.主循环:在`transcode()`函数(或类似的主循环)中,不断读取数据包,解码,滤镜处理,编码,然后写入输出。建议用户查看`ffmpeg.c`文件中的`transcode_step()`函数(在FFmpeg4.3中可能是`process_input()`或类似的函数)以及`reap_filters()`函数,这些函数构成了转码的主循环。另外,关于硬件加速转码,如引用[2][4]所示,FFmpeg支持通过特定的编解码器(如h264_cuvid,h264_nvenc)进行硬件编解码。在源码中,硬件加速相关的代码通常位于`libavcodec`目录下的特定编解码器文件中,例如`nvenc.c`、`cuviddec.c`等。因此,总结回答如下:</think>### FFmpeg 4.3 转码源码解析 FFmpeg 4.3 的转码核心流程集中在 `ffmpeg.c` 主模块中,主要涉及以下关键源码文件: #### 1. **转码主流程 (`ffmpeg.c`)** - **入口函数**:`main()` → `ffmpeg_parse_options()` 解析命令行参数 - **核心函数**:`transcode()` 控制整体转码流程: ```c // 伪代码流程 while (读取帧(av_read_frame)) { 解码(avcodec_send_packet + avcodec_receive_frame) 滤镜处理(av_buffersrc_add_frame + av_buffersink_get_frame) 编码(avcodec_send_frame + avcodec_receive_packet) 封装写入(av_interleaved_write_frame) } ``` - **关键数据结构**: - `InputFile`:管理输入文件 - `OutputStream`:管理输出流 - `FilterGraph`:滤镜图结构[^3] #### 2. **编解码层 (`libavcodec`)** - **解码初始化**:`avcodec_open2()` 打开解码器(源码:`libavcodec/utils.c`) - **编码初始化**:`avcodec_open2()` 同理,通过 `AVCodecContext` 配置参数 - **硬件加速**: - NVIDIA 硬解:`h264_cuvid`(源码:`libavcodec/cuviddec.c`) - NVIDIA 硬编:`h264_nvenc`(源码:`libavcodec/nvenc.c`)[^2][^4] - Android 硬解:`h264_mediacodec`(源码:`libavcodec/mediacodecdec.c`)[^4] #### 3. **滤镜处理 (`libavfilter`)** - 初始化:`init_filters()`(源码:`ffmpeg_filter.c`) - 滤镜图构建:`avfilter_graph_parse2()`(源码:`libavfilter/graphparser.c`) - 帧处理:`av_buffersrc_add_frame()` 输入 → `av_buffersink_get_frame()` 输出[^3] #### 4. **封装层 (`libavformat`)** - 输入:`avformat_open_input()`(源码:`libavformat/utils.c`) - 输出:`avformat_alloc_output_context2()` + `avformat_write_header()` - 数据写入:`av_interleaved_write_frame()`[^1][^3] #### 5. **转码示例源码路径 | 模块 | 关键文件 | 功能描述 | |---------------|-----------------------------|----------------------------| | 主控流程 | `ffmpeg.c` | 转码流程调度 | | 硬件编解码 | `libavcodec/nvenc.c` | NVIDIA GPU 编码 | | 滤镜系统 | `libavfilter/vf_scale.c` | 视频缩放滤镜 | | 解封装 | `libavformat/mov.c` | MP4 格式解析 | > 提示:调试时可关注 `ffmpeg.c` 中的 `process_input()` 函数,这是帧处理的核心循环[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值