FFmpeg编码02——测试h264和h265并写入文件

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <iostream>
#include <fstream>

#undef main
#include "SDL.h"

extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    #include <libavdevice/avdevice.h>
    #include <libavformat/version.h>
    #include <libavutil/time.h>
    #include <libavutil/mathematics.h>
    #include <libavutil/imgutils.h>
}

int main(int argc, char *argv[])
{
#if 0
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    qDebug() << avcodec_configuration();
    return a.exec();
#endif

    std::string filename = "400_300_25";
    AVCodecID codec_id = AV_CODEC_ID_H264;
//    AVCodecID codec_id = AV_CODEC_ID_HEVC;

    if(codec_id == AV_CODEC_ID_H264) {
        filename += ".h264";
    } else if(codec_id == AV_CODEC_ID_HEVC) {
        filename += ".h265";
    }

    std::ofstream ofs;
    ofs.open(filename, std::ios::binary);

    // 1. 寻找编码器
    auto codec = avcodec_find_encoder(codec_id);
    if(!codec) {
        qDebug() << "codec not find!";
        return -1;
    }

    // 2. 编码上下文
    auto c = avcodec_alloc_context3(codec);
    if(!c) {
        qDebug() << "avcodec alloc failed!";
        return -1;
    }

    // 3. 设定上下文参数
    c->width = 400;
    c->height = 300;

    // 帧时间戳的时间单位, pts *time_base = 播放时间(秒)
    c->time_base = {1, 25};                 // 分子,分母, 1s中的时间单元数;
    c->pix_fmt = AV_PIX_FMT_YUV420P;        // 元数据像素格式
    c->thread_count = 16;                   // 编码线程数

    // 4. 打开编码上下文
    int re = avcodec_open2(c, codec, NULL);
    if(re != 0) {
        qDebug() << "avcodec open error!";
        return -1;
    }

    // 创建AVFrame空间
    auto frame = av_frame_alloc();              // 未压缩数据
    frame->width = c->width;
    frame->height = c->height;
    frame->format = c->pix_fmt;
    re = av_frame_get_buffer(frame, 0);
    if(re != 0) {
        qDebug() << "av_frame_get_buffer failed!";
        return -1;
    }

    // 测试,10s视频
    auto pkt = av_packet_alloc();               // 每一帧内部空间不确定
    for(int i = 0; i < 250; i++) {
        // 生成AVFrame数据, 每一帧数据不同
        // Y
        for(int y = 0; y < c->height; y++) {
            for(int x = 0; x < c->width; x++) {
                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
            }
        }

        // U, V
        for(int y = 0; y < c->height / 2; y++) {
            for(int x = 0; x < c->width / 2; x++) {
                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
            }
        }

        frame->pts = i;

        // 发送未压缩帧到线程中压缩
        re = avcodec_send_frame(c, frame);
        if(re != 0) {
            break;
        }

        // 表示有数据返回
        while(re >= 0) {
            // 接收压缩帧,一般前几次调用返回空(缓存,立刻返回),编码会重新创建线程
            // 每次调用,都会重新分配pkt中的空间
            re = avcodec_receive_packet(c, pkt);
            if(re == AVERROR(EAGAIN) || re == AVERROR_EOF) {
                break;
            }

            if(re < 0)
                break;

            ofs.write((char*)pkt->data, pkt->size);         // 写入测试文件
            av_packet_unref(pkt);
        }
    }

    ofs.close();
    av_packet_free(&pkt);
    av_frame_free(&frame);
    avcodec_free_context(&c);           // 释放编码器上下文
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值