ffmpeg使用png编码器把rgb24编码为png图像

version

#define LIBAVUTIL_VERSION_MAJOR 58

#define LIBAVUTIL_VERSION_MINOR 12

#define LIBAVUTIL_VERSION_MICRO 100

note

不使用AVOutputFormat

code

复制代码
void CFfmpegOps::EncodeRGB24ToPNG(const char *infile, const char *width_str, const char *height_str, const char *outfile)
{
    if ((!infile) || (!width_str) || (!height_str) || (!outfile))
    {
        return;
    }

    int width = 0;
    int height = 0;
    AVFrame *rgb24frame = nullptr;
    int rgb24frame_bytes = 0;
    AVPixelFormat rgb24_fmt = AV_PIX_FMT_RGB24;
    int ret = -1;
    FILE *in_fp = nullptr;
    size_t n = 0;
    AVPacket *pkt = nullptr;
    const AVCodec *png_encoder = nullptr;
    AVCodecContext *png_encoder_ctx = nullptr;
    FILE *out_fp = nullptr;

    try
    {
        width = std::stoi(width_str);
        height = std::stoi(height_str);
    }
    catch (const std::exception &e)
    {
        return;
    }

    rgb24frame = av_frame_alloc();
    if (!rgb24frame)
    {
        printf("av_frame_alloc error\n");
        goto end;
    }
    rgb24frame->format = rgb24_fmt;
    rgb24frame->width = width;
    rgb24frame->height = height;

    rgb24frame_bytes = av_image_get_buffer_size(rgb24_fmt, width, height, 1);

    ret = av_frame_get_buffer(rgb24frame, 0);
    if (ret < 0)
    {
        printf("av_frame_get_buffer error(%s)\n", GetFfmpegERR(ret));
        goto end;
    }

    in_fp = fopen(infile, "rb");
    if (!in_fp)
    {
        printf("fopen error\n");
        goto end;
    }

    n = fread(rgb24frame->data[0], sizeof(uint8_t), width * height * 3, in_fp);
    if ((int)n != (width * height * 3))
    {
        printf("n != (width * height * 3)\n");
        goto end;
    }

    pkt = av_packet_alloc();
    if (!pkt)
    {
        printf("av_packet_alloc error\n");
        goto end;
    }

    png_encoder = avcodec_find_encoder(AV_CODEC_ID_PNG);
    if (!png_encoder)
    {
        printf("avcodec_find_encoder error\n");
        goto end;
    }

    png_encoder_ctx = avcodec_alloc_context3(png_encoder);
    if (!png_encoder_ctx)
    {
        printf("avcodec_alloc_context3 error\n");
        goto end;
    }
    png_encoder_ctx->pix_fmt = rgb24_fmt;
    png_encoder_ctx->width = width;
    png_encoder_ctx->height = height;
    png_encoder_ctx->time_base.num = 1;
    png_encoder_ctx->time_base.den = 25;
    png_encoder_ctx->framerate.num = 25;
    png_encoder_ctx->framerate.den = 1;
    png_encoder_ctx->bit_rate = rgb24frame_bytes * png_encoder_ctx->framerate.num * 8;

    ret = avcodec_open2(png_encoder_ctx, png_encoder, nullptr);
    if (ret < 0)
    {
        printf("avcodec_open2 error(%s)\n", GetFfmpegERR(ret));
        goto end;
    }

    out_fp = fopen(outfile, "wb");
    if (!out_fp)
    {
        printf("fopen error\n");
        goto end;
    }

    ret = avcodec_send_frame(png_encoder_ctx, rgb24frame);
    if (ret != 0)
    {
        printf("avcodec_send_frame error(%s)\n", GetFfmpegERR(ret));
        goto end;
    }

    while (1)
    {
        ret = avcodec_receive_packet(png_encoder_ctx, pkt);
        if (ret != 0)
        {
            if (ret == AVERROR(EAGAIN))
            {
                continue;
            }

            printf("avcodec_receive_packet error(%s)\n", GetFfmpegERR(ret));
            break;
        }

        n = fwrite(pkt->data, pkt->size, sizeof(uint8_t), out_fp);

        av_packet_unref(pkt);

        break;
    }

end:
    if (out_fp)
    {
        fclose(out_fp);
        out_fp = nullptr;
    }

    if (png_encoder_ctx)
    {
        avcodec_free_context(&png_encoder_ctx);
        png_encoder_ctx = nullptr;
    }

    if (pkt)
    {
        av_packet_free(&pkt);
        pkt = nullptr;
    }

    if (in_fp)
    {
        fclose(in_fp);
        in_fp = nullptr;
    }

    if (rgb24frame)
    {
        av_frame_free(&rgb24frame);
        rgb24frame = nullptr;
    }
}

performance

相关推荐
喜欢吃燃面40 分钟前
C++算法竞赛:位运算
开发语言·c++·学习·算法
草莓熊Lotso43 分钟前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
开发语言·c++·经验分享·笔记·其他
困鲲鲲43 分钟前
CPP多线程2:多线程竞争与死锁问题
c++·多线程·死锁
快乐的划水a9 小时前
组合模式及优化
c++·设计模式·组合模式
星星火柴93611 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑12 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
Cx330❀13 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
阿巴~阿巴~14 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
..过云雨14 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
胡耀超14 小时前
DataOceanAI Dolphin(ffmpeg音频转化教程) 多语言(中国方言)语音识别系统部署与应用指南
python·深度学习·ffmpeg·音视频·语音识别·多模态·asr