美文网首页
QT开发 YUV编码h.264文件

QT开发 YUV编码h.264文件

作者: 我有一只小毛驴_从来都不骑 | 来源:发表于2021-08-13 17:54 被阅读0次
// 检查像素格式
static int check_pix_fmt(const AVCodec *codec,
                         enum AVPixelFormat pixFmt) {
    const enum AVPixelFormat *p = codec->pix_fmts;
    while (*p != AV_PIX_FMT_NONE) {
        if (*p == pixFmt) return 1;
        p++;
    }
    return 0;
}
typedef struct {
    const char *filename;
    int width;
    int height;
    AVPixelFormat pixFmt;
    int fps;
} VideoEncodeSpec;

// 返回负数:中途出现了错误
// 返回0:编码操作正常完成
static int encode(AVCodecContext *ctx,
                  AVFrame *frame,
                  AVPacket *pkt,
                  QFile &outFile) {
    // 发送数据到编码器
    int ret = avcodec_send_frame(ctx, frame);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "avcodec_send_frame error" << errbuf;
        return ret;
    }

    // 不断从编码器中取出编码后的数据
    while (true) {
        ret = avcodec_receive_packet(ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            // 继续读取数据到frame,然后送到编码器
            return 0;
        } else if (ret < 0) { // 其他错误
            return ret;
        }

        // 成功从编码器拿到编码后的数据
        // 将编码后的数据写入文件
        outFile.write((char *) pkt->data, pkt->size);

        // 释放pkt内部的资源
        av_packet_unref(pkt);
    }
}

void FFmpegs::h264Encode(VideoEncodeSpec &in,
                         const char *outFilename) {
    // 文件
    QFile inFile(in.filename);
    QFile outFile(outFilename);

    // 一帧图片的大小
    int imgSize = av_image_get_buffer_size(in.pixFmt, in.width, in.height, 1);

    // 返回结果
    int ret = 0;

    // 编码器
    AVCodec *codec = nullptr;

    // 编码上下文
    AVCodecContext *ctx = nullptr;

    // 存放编码前的数据(yuv)
    AVFrame *frame = nullptr;

    // 存放编码后的数据(h264)
    AVPacket *pkt = nullptr;
    // 获取编码器
    codec = avcodec_find_encoder_by_name("libx264");
    if (!codec) {
        qDebug() << "encoder not found";
        return;
    }

    // 检查输入数据的采样格式
    if (!check_pix_fmt(codec, in.pixFmt)) {
        qDebug() << "unsupported pixel format"
                 << av_get_pix_fmt_name(in.pixFmt);
        return;
    }

    // 创建编码上下文
    ctx = avcodec_alloc_context3(codec);
    if (!ctx) {
        qDebug() << "avcodec_alloc_context3 error";
        return;
    }

    // 设置yuv参数
    ctx->width = in.width;
    ctx->height = in.height;
    ctx->pix_fmt = in.pixFmt;
    // 设置帧率(1秒钟显示的帧数是in.fps)
    ctx->time_base = {1, in.fps};

    // 打开编码器
    ret = avcodec_open2(ctx, codec, nullptr);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "avcodec_open2 error" << errbuf;
        goto end;
    }

    // 创建AVFrame
    frame = av_frame_alloc();
    if (!frame) {
        qDebug() << "av_frame_alloc error";
        goto end;
    }

    frame->width = ctx->width;
    frame->height = ctx->height;
    frame->format = ctx->pix_fmt;
    frame->pts = 0;

    // 利用width、height、format创建缓冲区
    ret = av_image_alloc(frame->data, frame->linesize,
                         in.width, in.height, in.pixFmt, 1);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "av_frame_get_buffer error" << errbuf;
        goto end;
    }

    // 创建输入缓冲区(方法2)
//    buf = (uint8_t *) av_malloc(imgSize);
//    ret = av_image_fill_arrays(frame->data, frame->linesize,
//                               buf,
//                               in.pixFmt, in.width, in.height, 1);
//    if (ret < 0) {
//        ERROR_BUF(ret);
//        qDebug() << "av_image_fill_arrays error" << errbuf;
//        goto end;
//    }
//    qDebug() << buf << frame->data[0];

    // 创建输入缓冲区(方法3)
//    ret = av_frame_get_buffer(frame, 0);
//    if (ret < 0) {
//        ERROR_BUF(ret);
//        qDebug() << "av_frame_get_buffer error" << errbuf;
//        goto end;
//    }

    // 创建AVPacket
    pkt = av_packet_alloc();
    if (!pkt) {
        qDebug() << "av_packet_alloc error";
        goto end;
    }

    // 打开文件
    if (!inFile.open(QFile::ReadOnly)) {
        qDebug() << "file open error" << in.filename;
        goto end;
    }
    if (!outFile.open(QFile::WriteOnly)) {
        qDebug() << "file open error" << outFilename;
        goto end;
    }

    // 读取数据到frame中
    while ((ret = inFile.read((char *) frame->data[0],
                              imgSize)) > 0) {
        // 进行编码
        if (encode(ctx, frame, pkt, outFile) < 0) {
            goto end;
        }
    // 设置帧的序号
        frame->pts++;
    }

    // 刷新缓冲区
    encode(ctx, nullptr, pkt, outFile);

end:
    // 关闭文件
    inFile.close();
    outFile.close();
 // 释放资源
    if (frame) {
        av_freep(&frame->data[0]);
        av_frame_free(&frame);
    }
    av_packet_free(&pkt);
    avcodec_free_context(&ctx);

    qDebug() << "线程正常结束";
}

相关文章

  • QT开发 YUV编码h.264文件

  • FFmpeg编码yuv转H264

    紧接上一章内容,将视频文件添加一个红色方框后文件转成了YUV数据,这一节就再处理下YUV数据,编码成H.264文件...

  • 26_H.264编码实战

    本文的主要内容:使用H.264编码对YUV视频进行压缩。 使用FFmpeg命令进行H.264编码 如果是命令行的操...

  • FFmpeg4-视频编码

    一、视频编码?->YUV编码为H.264 分析视频编码原理->流程 第一步:注册组件->编码器、解码器等等…av_...

  • 视频编解码五:FFmpeg视频编码流程

    视频编码->YUV420p编码为H.264 第一点:分析视频编码原理?->流程? 第一步:注册组件->编码器、解码...

  • FFmpeg转码(1)

    FFmpeg软编码(H.264) 查看libx264编码信息 x264编码参数 H.264编码举例: 1. 编码器...

  • 基于FFmpeg的iOS播放器(一)

    1.使用FFmpeg、x264、libxvidcore解码h.264格式和MPEG-4文件。 2.YUV420P格...

  • H264码流分析导读

    导读 H.264码流结构解析 H.264编码格式 H.264的功能分为两层:视频编码层(VCL, Video Co...

  • 使用AudioToolbox编码AAC

    前言 使用VideoToolbox硬编码H.264使用VideoToolbox硬解码H.264这次在编码H.264...

  • ffmpeg将yuv文件编码为mp4

    上一遍文件是将mp4的视频流数据解码,并且写入yuv的数据文件中,这篇文章是一个逆向操作,既将yuv数据文件编码为...

网友评论

      本文标题:QT开发 YUV编码h.264文件

      本文链接:https://www.haomeiwen.com/subject/ururbltx.html