FFmpeg进行图像格式转换(cv::Mat --->>YUV---->>AVFrame)

一. cv::Mat到YUV420的转换(opencv)

RGB24使用24位来表示一个像素,RGB分量都用8位表示,取值范围为0-255。在一个2*2的像素区域,RRG暂用的字节数为2*2*3=12字节。那么用yuv表示,占用的字节数为4(Y)+1(u)+1(v)=6字节,其中Y占用4个字节,U和V各占用1字节,比例为4:1:1

所以在一个宽高为w*h的设备上,使用rgb表示编码占用的字节数为w*h*3,使用yuv表示暂用的内存为w*h*+w*h/4+w*h/4 = w*h*3/2.

opencv提供了rgb到yuv420的格式转换函数;

下面给出基本用法

1.  读取mp4格式的视频文件,转换成Yuv42

rgbtoyuv.cpp


#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
  
    cv::VideoCapture vc;  
    bool flag = vc.open("test.mp4");  
    if (!flag)  
    {  
        printf("avi file open error ");  
        system("pause");  
        exit(-1);  
    }  
  
    int frmCount = vc.get(CV_CAP_PROP_FRAME_COUNT);  
    frmCount -= 5;  
    printf("frmCount: %d ", frmCount);  
  
    int w = vc.get(CV_CAP_PROP_FRAME_WIDTH);  
    int h = vc.get(CV_CAP_PROP_FRAME_HEIGHT);  
    int bufLen = w*h*3/2;  
    unsigned char* pYuvBuf = new unsigned char[bufLen];  
    FILE* pFileOut = fopen("result.yuv", "w+");  
    if (!pFileOut)  
    {  
        printf("pFileOut open error ");  
        system("pause");  
        exit(-1);  
    }  
    printf("pFileOut open ok ");  
      
    for (int i=0; i<frmCount; i++)  
    {  
        printf("%d/%d ", i+1, frmCount);  
  
        cv::Mat srcImg;  
        vc>>srcImg;  
  
        cv::imshow("img", srcImg);  
        cv::waitKey(1);  
  
        cv::Mat yuvImg;  
        cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420);  
        memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));  
  
        fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);  
    }  
  
    fclose(pFileOut);  
    delete[] pYuvBuf;  
}  

编译以及运行:

g++ rgbtoyuv.cpp -o rgbtoyuv `pkg-config --cflags --libs opencv` -std=c++11

 2. 读取yuv420格式的文件,转换成cv::Mat格式,并予以显示:

yuvtorgb.cpp

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
  
    int w =960;  
    int h =544;  
    printf("yuv file w: %d, h: %d ", w, h);  
  
    FILE* pFileIn = fopen("result.yuv", "rb+");  
    int bufLen = w*h*3/2;  
    unsigned char* pYuvBuf = new unsigned char[bufLen];  
    int iCount = 0;  
  
  
    for(int i=0; i<780; i++)  
    {  
        fread(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileIn);  
  
        cv::Mat yuvImg;  
        yuvImg.create(h*3/2, w, CV_8UC1);   
        memcpy(yuvImg.data, pYuvBuf, bufLen*sizeof(unsigned char));  
        cv::Mat rgbImg;  
        cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);  
  
        cv::imshow("img", yuvImg);  
        cv::waitKey(1);  
  
        printf("%d ", iCount++);  
    }  
  
    delete[] pYuvBuf;  
  
  
    fclose(pFileIn);  
}  

编译和运行

 g++ yuvtorgb.cpp -o yuvtorgb `pkg-config --cflags --libs opencv` -std=c++11

二. AVFrame的简介和转换

参考链接

1. AVFrame的简介

  AVFrame,这个结构体应该是保存视频帧的信息的。像一帧图像也是可以保存在AVFrame结构中。事实上,我们可以直接从一个YUV文件中,把一张YUV图像数据读到AVFrame中。


    typedef struct AVFrame  
    {  
    #define AV_NUM_DATA_POINTERS 8  
        uint8_t *   data [AV_NUM_DATA_POINTERS]; //指向图像数据  
      
        int linesize [AV_NUM_DATA_POINTERS]; //行的长度  
      
        int width; //图像的宽  
        int height; //图像的高  
        int format;  //图像格式  
         ……  
    }AVFrame;  

        注意到data成员是一个指针数组。其指向的内容就是图像的实际数据。

        可以用av_frame_alloc(void)函数来分配一个AVFrame结构体。这个函数只是分配AVFrame结构体,但data指向的内存并没有分配,需要我们指定,这个内存的大小就是一张特定格式图像所需的大小。

// AVframe的初始化
    
    AVFrame* frame = av_frame_alloc();  
      
    //这里FFmpeg会帮我们计算这个格式的图片,需要多少字节来存储  
    //相当于前一篇博文例子中的width * height * 2  
    int bytes_num = avpicture_get_size(AV_PIX_FMT_YUV420P, width, height); //AV_PIX_FMT_YUV420P是FFmpeg定义的标明YUV420P图像格式的宏定义  
      
    //申请空间来存放图片数据。包含源数据和目标数据  
    uint8_t* buff = (uint8_t*)av_malloc(bytes_num);  
      
    //前面的av_frame_alloc函数,只是为这个AVFrame结构体分配了内存,  
    //而该类型的指针指向的内存还没分配。这里把av_malloc得到的内存和AVFrame关联起来。  
    //当然,其还会设置AVFrame的其他成员  
    avpicture_fill((AVPicture*)frame, buff, AV_PIX_FMT_ YUV420P,width, height); 

2. YUV转AVFrame

       cv::Mat yuvImg;
       cv::cvtColor(srcImg, yuvImg, cv::COLOR_BGR2YUV_I420);
       memcpy(picture_buf, yuvImg.data, bufLen*sizeof(unsigned char));

       pFrame->data[0] = picture_buf;              // Y
       pFrame->data[1] = picture_buf+ y_size;      // U
       pFrame->data[2] = picture_buf+ y_size*5/4;  // V

3. CV::Mat转AVFrame

 struct SwsContext *sws_ctx_bgr_yuv = NULL;
    sws_ctx_bgr_yuv = sws_getContext(pCodecCtx->width,
                                     pCodecCtx->height,
                                     AV_PIX_FMT_BGR24,
                                     pCodecCtx->width,
                                     pCodecCtx->height,
                                     pCodecCtx->pix_fmt //AV_PIX_FMT_YUV420p
                                     ,0,0,NULL,NULL);


    if( sws_ctx_bgr_yuv == NULL)
    {
        printf("sws_getContext fail ");
        return ;
    }


    const int kStide[] = { (int)srcImg.step[0] };
    sws_scale(sws_ctx_bgr_yuv, &srcImg.data, kStide, 0, srcImg.rows, pFrame->data, pFrame->linesize);

4.  cv::Mat ---->>AVFrame  ----->> H265

main.cpp


#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>

#include "encoder.h"


int main()
{
      encoder *ed = new encoder();

      cv::VideoCapture cap(cv::String("/home/zhy/Documents/remote_driving/encoder_YUV_H264-h265/test.mp4"));
       if (!cap.isOpened())
       {
           return -1;
       }
      int i=0;
      while(true){
       cv::Mat srcImg;
       cap>>srcImg;       
       std::cout<<1222<<std::endl;

       ed->encode(srcImg,i);
       i++;
       }

      //ed.endencoder();
}

encoder.h


#ifndef ENCODER_H
#define ENCODER_H
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>

extern "C"
{
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
}


class encoder
{
private:
    AVFormatContext* pFormatCtx;
    AVOutputFormat* fmt;
    AVStream* video_st;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;
    AVPacket pkt;
    uint8_t* picture_buf;
    AVFrame* pFrame;

    int picture_size;
    int y_size;
    int framecnt;
    int w;
    int h;
    int bufLen;
    const char* out_file;


public:
     encoder();
     ~encoder();
    void encode(cv::Mat img, int img_sequence);
    int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index);

};

#endif // ENCODER_H
encoder.cpp

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include "encoder.h"

encoder::encoder()
{
    w=960;
    h=544;
    bufLen=w * h * 3/2;
    out_file = "/home/zhy/Documents/remote_driving/encoder_YUV_H264-h265/untitled/src01.hevc";
    av_register_all();//注册FFmpeg所有编解码器。
    //Method1.
    pFormatCtx = avformat_alloc_context(); //初始化输出码流的AVFormatContext。
    //Guess Format
    fmt = av_guess_format(NULL, out_file, NULL);
    pFormatCtx->oformat = fmt;

    //Open output URL
    //打开文件的缓冲区输入输出,flags 标识为  AVIO_FLAG_READ_WRITE ,可读写;将输出文件中的数据读入到程序的 buffer 当中,方便之后的数据写入fwrite
    if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){  //打开输出文件
        printf("Failed to open output file! \n");
        return ;
    }

    video_st = avformat_new_stream(pFormatCtx, 0);//创建输出码流的AVStream。
        // 设置 码率25 帧每秒(fps=25)
    //video_st->time_base.num = 1;
    //video_st->time_base.den = 25;

    if (video_st==NULL){
        return ;
    }
        //为输出文件设置编码的参数和格式
    //Param that must set
    pCodecCtx = video_st->codec; // 从媒体流中获取到编码结构体,一个 AVStream 对应一个  AVCodecContext
    pCodecCtx->codec_id =AV_CODEC_ID_HEVC;// 设置编码器的 id,例如 h265 的编码 id 就是 AV_CODEC_ID_H265
    //pCodecCtx->codec_id = fmt->video_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;//编码器视频编码的类型
    pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;//设置像素格式为 yuv 格式
    pCodecCtx->width =w;  //设置视频的宽高
    pCodecCtx->height = h;
    pCodecCtx->bit_rate = 400000;   //采样的码率;采样码率越大,视频大小越大
    pCodecCtx->gop_size=250;  //每250帧插入1个I帧,I帧越少,视频越小

    pCodecCtx->time_base.num = 1;
    pCodecCtx->time_base.den = 25;

    //H264
    //pCodecCtx->me_range = 16;
    //pCodecCtx->max_qdiff = 4;
    //pCodecCtx->qcompress = 0.6;
    pCodecCtx->qmin = 10; //最大和最小量化系数
    pCodecCtx->qmax = 51;

    //Optional Param
    pCodecCtx->max_b_frames=3; // 设置 B 帧最大的数量,B帧为视频图片空间的前后预测帧, B 帧相对于 I、P 帧来说,压缩率比较大,采用多编码 B 帧提高清晰度

    // Set Option //设置编码速度
    AVDictionary *param = 0;
        //preset的参数调节编码速度和质量的平衡。
    //tune的参数值指定片子的类型,是和视觉优化的参数,
    //zerolatency: 零延迟,用在需要非常低的延迟的情况下,比如电视电话会议的编码
    //H.264
    if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
        av_dict_set(&param, "preset", "slow", 0);
        av_dict_set(&param, "tune", "zerolatency", 0);
        //av_dict_set(&param, "profile", "main", 0);
    }
    //H.265
    if(pCodecCtx->codec_id == AV_CODEC_ID_H265){
        av_dict_set(&param, "preset", "ultrafast", 0);
        av_dict_set(&param, "tune", "zero-latency", 0);
    }

    //Show some Information //输出格式的信息,例如时间,比特率,数据流,容器,元数据,辅助数据,编码,时间戳
    av_dump_format(pFormatCtx, 0, out_file, 1);

    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);//查找编码器
    if (!pCodec){
        printf("Can not find encoder! \n");
        return ;
    }
       // 打开编码器,并设置参数 param
    if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){
        printf("Failed to open encoder! \n");
        return ;
    }

        //设置原始数据 AVFrame
    pFrame = av_frame_alloc();

    picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
   //    // 将 picture_size 转换成字节数据
    picture_buf = (uint8_t *)av_malloc(picture_size);
   //        // 设置原始数据 AVFrame 的每一个frame 的图片大小,AVFrame 这里存储着 YUV 非压缩数据
   avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
    //Write File Header 写封装格式文件头
    avformat_write_header(pFormatCtx,NULL);

        //创建编码后的数据 AVPacket 结构体来存储 AVFrame 编码后生成的数据  //编码前:AVFrame  //编码后:AVPacket
    av_new_packet(&pkt,picture_size);

        // 设置 yuv 数据中Y亮度图片的宽高,写入数据到 AVFrame 结构体中
    y_size = pCodecCtx->width * pCodecCtx->height;
}

encoder::~encoder(){
    //Flush Encoder //输出编码器中剩余的AVPacket
        int ret = flush_encoder(pFormatCtx,0);
        if (ret < 0) {
            printf("Flushing encoder failed\n");
            return ;
        }

        //Write file trailer // 写入数据流尾部到输出文件当中,表示结束并释放文件的私有数据
        av_write_trailer(pFormatCtx);

        //Clean
        if (video_st){
                    // 关闭编码器
            avcodec_close(video_st->codec);
                    // 释放 AVFrame
            av_free(pFrame);

                    // 释放图片 buf
            av_free(picture_buf);
        }
        avio_close(pFormatCtx->pb);
        avformat_free_context(pFormatCtx);

}
//H.265码流与YUV输入的帧数不同。经过观察对比其他程序后发现需要调用flush_encoder()将编码器中剩余的视频帧输出。当av_read_frame()循环退出的时候,实际上解码器中可能还包含剩余的几帧数据。
//因此需要通过“flush_decoder”将这几帧数据输出。“flush_decoder”功能简而言之即直接调用avcodec_decode_video2()获得AVFrame,而不再向解码器传递AVPacket
int encoder::flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
    int ret;
    int got_frame;
    AVPacket enc_pkt;
//    if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities & CODEC_CAP_DELAY))
//        return 0;
    while (1) {
        enc_pkt.data = NULL;
        enc_pkt.size = 0;
        av_init_packet(&enc_pkt);
        ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
            NULL, &got_frame);
        av_frame_free(NULL);
        if (ret < 0)
            break;
        if (!got_frame){
            ret=0;
            break;
        }
        printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
        /* mux encoded frame */
        ret = av_write_frame(fmt_ctx, &enc_pkt);
        if (ret < 0)
            break;
    }
    return ret;
}
void encoder::encode(cv::Mat srcImg, int img_sequence)
{
//       cv::Mat yuvImg;
//       cv::cvtColor(srcImg, yuvImg, cv::COLOR_BGR2YUV_I420);
//       memcpy(picture_buf, yuvImg.data, bufLen*sizeof(unsigned char));

//        pFrame->data[0] = picture_buf;              // Y
//        pFrame->data[1] = picture_buf+ y_size;      // U
//        pFrame->data[2] = picture_buf+ y_size*5/4;  // V

    struct SwsContext *sws_ctx_bgr_yuv = NULL;
    sws_ctx_bgr_yuv = sws_getContext(pCodecCtx->width,
                                     pCodecCtx->height,
                                     AV_PIX_FMT_BGR24,
                                     pCodecCtx->width,
                                     pCodecCtx->height,
                                     pCodecCtx->pix_fmt //AV_PIX_FMT_YUV420p
                                     ,0,0,NULL,NULL);


    if( sws_ctx_bgr_yuv == NULL)
    {
        printf("sws_getContext fail ");
        return ;
    }


    const int kStide[] = { (int)srcImg.step[0] };
    sws_scale(sws_ctx_bgr_yuv, &srcImg.data, kStide, 0, srcImg.rows, pFrame->data, pFrame->linesize);

        //PTS //顺序显示解码后的视频帧
        pFrame->pts=img_sequence;
        //设置这一帧的显示时间

       // pFrame->pts=i*(video_st->time_base.den)/((video_st->time_base.num)*25);
        int got_picture=0;
        //Encode //编码一帧视频。即将AVFrame(存储YUV像素数据)编码为AVPacket(存储H.264等格式的码流数据)
        int ret = avcodec_encode_video2(pCodecCtx, &pkt,pFrame, &got_picture);
        if(ret < 0){
            printf("Failed to encode! \n");
            return ;
        }
        if (got_picture==1){
            printf("Succeed to encode frame: %5d\tsize:%5d\n",framecnt,pkt.size);
            framecnt++;
            pkt.stream_index = video_st->index;
            ret = av_write_frame(pFormatCtx, &pkt);//将编码后的视频码流写入文件(fwrite)
            av_free_packet(&pkt);//释放内存
        }
}
Makefile

CC = g++
LD = g++

SRCS = $(wildcard *.cpp)
OBJS = $(patsubst %c, %o, $(SRCS))

TARGET = main

.PHONY:all clean

all: $(TARGET)

$(TARGET): $(OBJS)
	$(LD) $^ -g -o $@ `pkg-config --cflags --libs opencv` -std=c++11 -I/usr/local/include -L/usr/local/lib  -lavformat -lavcodec -lavutil  -lswresample -lswscale 
  

%o:%c
	$(CC) -cpp $^

clean:
	rm -f $(OBJS) $(TARGET)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单的示例代码,可以将 OpenCV 的 `cv::Mat` 对象换为 FFmpeg 的 `AVFrame`,并将其编码为 YUV 420P 格式。 ```c++ #include <opencv2/opencv.hpp> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> int main() { // 初始化 FFmpeg av_register_all(); // 创建格式上下文 AVFormatContext* format_ctx = avformat_alloc_context(); if (!format_ctx) { std::cerr << "Failed to allocate format context" << std::endl; return -1; } // 设置输出格式 AVOutputFormat* output_fmt = av_guess_format("mp4", nullptr, nullptr); if (!output_fmt) { std::cerr << "Failed to guess output format" << std::endl; return -1; } format_ctx->oformat = output_fmt; // 打开输出文件 AVIOContext* io_ctx = nullptr; if (avio_open(&io_ctx, "output.mp4", AVIO_FLAG_WRITE) < 0) { std::cerr << "Failed to open output file" << std::endl; return -1; } format_ctx->pb = io_ctx; // 创建视频流 AVStream* video_stream = avformat_new_stream(format_ctx, nullptr); if (!video_stream) { std::cerr << "Failed to create video stream" << std::endl; return -1; } // 设置编码器参数 AVCodecParameters* codec_params = video_stream->codecpar; codec_params->codec_type = AVMEDIA_TYPE_VIDEO; codec_params->codec_id = output_fmt->video_codec; codec_params->width = 640; codec_params->height = 480; codec_params->format = AV_PIX_FMT_YUV420P; // 查找编码器 AVCodec* codec = avcodec_find_encoder(output_fmt->video_codec); if (!codec) { std::cerr << "Failed to find encoder" << std::endl; return -1; } // 创建编码器上下文 AVCodecContext* codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { std::cerr << "Failed to allocate codec context" << std::endl; return -1; } codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->width = codec_params->width; codec_ctx->height = codec_params->height; codec_ctx->pix_fmt = codec_params->format; codec_ctx->time_base = {1, 25}; // 打开编码器 if (avcodec_open2(codec_ctx, codec, nullptr) < 0) { std::cerr << "Failed to open codec" << std::endl; return -1; } // 创建帧 AVFrame* frame = av_frame_alloc(); if (!frame) { std::cerr << "Failed to allocate frame" << std::endl; return -1; } frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; // 分配帧数据空间 if (av_frame_get_buffer(frame, 0) < 0) { std::cerr << "Failed to allocate frame data" << std::endl; return -1; } // 创建格式转换器 SwsContext* sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, AV_PIX_FMT_BGR24, codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); if (!sws_ctx) { std::cerr << "Failed to create format converter" << std::endl; return -1; } // 读取输入帧 cv::Mat input_frame = cv::imread("input.jpg"); if (input_frame.empty()) { std::cerr << "Failed to read input frame" << std::endl; return -1; } // 换输入帧 uint8_t* input_data[AV_NUM_DATA_POINTERS] = {0}; input_data[0] = input_frame.data; int input_linesize[AV_NUM_DATA_POINTERS] = {0}; input_linesize[0] = input_frame.step; sws_scale(sws_ctx, input_data, input_linesize, 0, codec_ctx->height, frame->data, frame->linesize); // 编码帧 AVPacket pkt; av_init_packet(&pkt); pkt.data = nullptr; pkt.size = 0; int got_packet = 0; if (avcodec_encode_video2(codec_ctx, &pkt, frame, &got_packet) < 0) { std::cerr << "Failed to encode frame" << std::endl; return -1; } // 写入输出文件 if (got_packet) { av_packet_rescale_ts(&pkt, codec_ctx->time_base, video_stream->time_base); pkt.stream_index = video_stream->index; if (av_interleaved_write_frame(format_ctx, &pkt) < 0) { std::cerr << "Failed to write packet" << std::endl; return -1; } av_packet_unref(&pkt); } // 写入文件尾 av_write_trailer(format_ctx); // 释放资源 avcodec_free_context(&codec_ctx); av_frame_free(&frame); avio_closep(&format_ctx->pb); avformat_free_context(format_ctx); sws_freeContext(sws_ctx); return 0; } ``` 需要注意的是,上述代码中的 `AV_PIX_FMT_BGR24` 表示输入图像的像素格式,如果您的输入图像格式不是 BGR24,需要相应地修改代码。另外,上述代码中的像素格式硬编码为 YUV420P,如果您需要使用其他像素格式,也需要相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值