ffmpeg api-codec-param-test.c源码讲解

try_decode_video_frame

/**
 * 尝试解码视频帧
 *
 * @param codec_ctx 解码器上下文
 * @param pkt 待解码的视频数据包
 * @param decode 是否解码标志,如果为1,则进行解码,如果为0,则不解码
 * @return 返回0表示成功,否则表示出错
 */
static int try_decode_video_frame(AVCodecContext *codec_ctx, AVPacket *pkt, int decode)
{
   
    int ret = 0;
    int got_frame = 0;
    AVFrame *frame = NULL;
    int skip_frame = codec_ctx->skip_frame;

    // 如果解码器未打开,则打开解码器
    if (!avcodec_is_open(codec_ctx)) {
   
        const AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);

        ret = avcodec_open2(codec_ctx, codec, NULL);
        if (ret < 0) {
   
            av_log(codec_ctx, AV_LOG_ERROR, "Failed to open codec\n");
            goto end;
        }
    }

    // 分配一个AVFrame结构体
    frame = av_frame_alloc();
    if (!frame) {
   
        av_log(NULL, AV_LOG_ERROR, "Failed to allocate frame\n");
        goto end;
    }

    // 如果不需要解码,并且解码器支持跳帧填充参数,则将跳帧设置为AVDISCARD_ALL
    if (!decode && avpriv_codec_get_cap_skip_frame_fill_param(codec_ctx->codec)) {
   
        codec_ctx->skip_frame = AVDISCARD_ALL;
    }

    // 循环解码视频帧
    do {
   
        // 解码视频帧
        ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, pkt);
        av_assert0(decode || (!decode && !got_frame));
        if (ret < 0)
            break;
        pkt->data += ret;
        pkt->size -= ret;

        // 如果成功解码到一帧视频,则退出循环
        if (got_frame) {
   
            break;
        }
    } while (pkt->size > 0);

end:
    // 恢复skip_frame的原始值
    codec_ctx->skip_frame = skip_frame;

    // 释放AVFrame结构体
    av_frame_free(&frame);
    return ret;
}

find_video_stream_info

/**
 * 查找视频流信息并尝试解码视频帧
 *
 * @param fmt_ctx AVFormatContext 结构体,表示输入文件的格式上下文
 * @param decode 是否解码标志,如果为 1,则进行解码,如果为 0,则不解码
 * @return 返回 0 表示成功,否则表示出错
 */
static int find_video_stream_info(AVFormatContext *fmt_ctx, int decode)
{
   
    int ret = 0;
    int i, done = 0;
    AVPacket pkt;

    // 初始化 AVPacket 结构体
    av_init_packet(&pkt);

    // 循环读取视频帧数据,直到所有视频流都有数据包
    while (!done) {
   
        AVCodecContext *codec_ctx = NULL;
        AVStream *st;

        // 从输入文件中读取视频帧数据包
        if ((ret = av_read_frame(fmt_ctx, &pkt)) < 0) {
   
            av_log(fmt_ctx, AV_LOG_ERROR, "Failed to read frame\n");
            goto end;
        }

        // 获取视频流的解码器上下文
        st = fmt_ctx->streams[pkt.stream_index];
        codec_ctx = st->codec;

        // 不是视频流或已经解码过一帧视频则跳过
        if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO ||
            st->codec_info_nb_frames++ > 0) {
   
            av_packet_unref(&pkt);
            continue;
        }

        // 尝试解码视频帧
        ret = try_decode_video_frame(codec_ctx, &pkt, decode);
        if (ret < 0) {
   
            av_log(fmt_ctx, AV_LOG_ERROR, "Failed to decode video frame\n");
            goto end;
        }

        // 释放 AVPacket 结构体
        av_packet_unref(&pkt);

        // 检查是否所有视频流都已经解码完毕
        done = 1;
        for (i = 0; i < fmt_ctx->nb_streams; i++) {
   
            st = fmt_ctx->streams[i];
            codec_ctx = st->codec;

            if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO)
                continue;

            done &= st->codec_info_nb_frames > 0;
            /*
            在 FFmpeg 的 AVCodecContext 结构体中,codec_info_nb_frames 是一个用于存储编解码器相关帧数量信息的成员变量。这个变量通常用于在编解码过程中跟踪已经处理的帧数。在 FFmpeg 中,帧是视频编码的基本单位,而 codec_info_nb_frames 则是与当前编解码器相关联的帧的数量。
在代码中,codec_info_nb_frames 被用作检查视频流是否已经解码完成的标志。通过检查 codec_info_nb_frames 是否大于 0,可以判断当前视频流是否已经解码了至少一帧。这在处理视频流时非常有用,因为它可以帮助确定是否还有待处理的帧数据,或者是否已经处理完所有的帧数据。
总之,codec_info_nb_frames 是一个用于存储编解码器相关帧数量信息的成员变量,它在 FFmpeg 中用于跟踪已经处理的帧数,并在视频编解码过程中起到重要的作用。
			*/
        }
    }

end:
    // 释放 AVPacket 结构体
    av_packet_unref(&pkt);

    // 关闭所有在 try_decode_video_frame 中打开的解码器
    for (i = 0; i < fmt_ctx->nb_streams; i++) {
   
        AVStream *st = fmt_ctx->streams[i];
        avcodec_close(st->codec);
    }

    // 返回解码结果
    return ret < 0;
}

/**
 * 打印视频流信息
 *
 * @param fmt_ctx AVFormatContext 结构体,表示输入文件的格式上下文
 * @param decode 是否解码标志,如果为 1,则进行解码,如果为 0,则不解码
 */
static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
{
   
    int i;

    // 遍历所有视频流
    for (i = 0; i < fmt_ctx->nb_streams; i++) {
   
        const AVOption *opt = NULL;
        const AVStream *st = fmt_ctx->streams[i];
        AVCodecContext *codec_ctx = st->codec;

        // 打印视频流的序号和解码标志
        printf("stream=%d, decode=%d\n", i, decode);

        // 遍历视频流的所有选项
        while (opt = av_opt_next(codec_ctx, opt)) {
   
            uint8_t *str;

            // 跳过常量选项
            if (opt->type == AV_OPT_TYPE_CONST)
                continue;

            // 跳过帧数选项
            if (!strcmp(opt->name, "frame_number"))
                continue;

            // 获取选项的值并打印
            if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
   
                printf("    %s=%s\n", opt->name, str);
                av_free(str);
            }
        }
    }
}

/**
 * 打开输入文件并尝试解码视频流信息
 *
 * @param fmt_ctx AVFormatContext 结构体指针的指针,用于存储打开的输入文件的格式上下文
 * @param filename 输入文件名
 * @param decode 是否解码标志,如果为 1,则进行解码,如果为 0,则不解码
 * @return 返回 0 表示成功,否则表示出错
 */
static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
{
   
    int ret = 0;

    // 打开输入文件并读取格式
    ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
    if (ret < 0) {
   
        av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
        goto end;
    }

    // 获取视频流信息并尝试解码
    ret = find_video_stream_info(*fmt_ctx, decode);
    if (ret < 0) {
   
        goto end;
    }

    // 打印视频流信息
    dump_video_streams(*fmt_ctx, decode);

end:
    return ret;
}

/**
 * 检查两个格式上下文中的视频流是否相同
 *
 * @param fmt_ctx1 第一个输入文件的格式上下文
 * @param fmt_ctx2 第二个输入文件的格式上下文
 * @return 返回 0 表示视频流相同,否则表示不同
 */
static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
{
   
    int i;
    int ret = 0;

    // 断言两个格式上下文中的视频流数量相同
    av_assert0(fmt_ctx1->nb_streams == fmt_ctx2->nb_streams);

    // 遍历每个视频流
    for (i = 0; i < fmt_ctx1->nb_streams; i++) {
   
        const AVOption *opt = NULL;
        const AVStream *st1 = fmt_ctx1->streams[i];
        const AVStream *st2 = fmt_ctx2->streams[i];
        AVCodecContext *codec_ctx1 = st1->codec;
        AVCodecContext *codec_ctx2 = st2->codec;

        // 如果当前流不是视频流,则跳过
        if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
            continue;

        // 遍历视频流的所有选项
        while (opt = av_opt_next(codec_ctx1, opt)) {
   
            uint8_t *str1 = NULL, *str2 = NULL;

            // 跳过常量选项
            if (opt->type == AV_OPT_TYPE_CONST)
                continue;

            // 跳过帧数选项
            if (!strcmp(opt->name, "frame_number"))
                continue;

            // 获取第一个格式上下文中选项的值
            av_assert0(av_opt_get(codec_ctx1, opt->name, 0, &str1) >= 0);

            // 获取第二个格式上下文中选项的值
            av_assert0(av_opt_get(codec_ctx2, opt->name, 0, &str2) >= 0);

            // 比较两个值是否相同,如果不同则打印错误信息
            if (strcmp(str1, str2)) {
   
                av_log(NULL, AV_LOG_ERROR, "Field %s differs: %s %s", opt->name, str1, str2);
                ret = AVERROR(EINVAL);  // 设置返回值表示不同
            }

            // 释放内存
            av_free(str1);
            av_free(str2);
        }
    }

    // 返回比较结果
    return ret;
}

相关推荐

  1. ffmpeg api-codec-param-test.c讲解

    2024-01-09 07:00:01       32 阅读
  2. obs video-scaler-ffmpeg.c 讲解

    2024-01-09 07:00:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 07:00:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 07:00:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 07:00:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 07:00:01       18 阅读

热门阅读

  1. 数据结构与算法Python版:计数排序

    2024-01-09 07:00:01       36 阅读
  2. 收到的字符串写入xml并且将这个xml写入.zip文件中

    2024-01-09 07:00:01       37 阅读
  3. Android-设计模式

    2024-01-09 07:00:01       29 阅读
  4. Spring Boot日志配置

    2024-01-09 07:00:01       35 阅读
  5. 【Spring Boot 3】【数据源】自定义JDBC多数据源

    2024-01-09 07:00:01       43 阅读