ffmpeg之NV12转BGR24

1、git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg 

2、./configure --prefix=./out 

make && make install

3、test.cpp

编译:g++ test.cpp -o test -I./ffmpeg/out/include -L./ffmpeg/out/lib -lavformat -lavcodec  -lavdevice -lavfilter   -lswscale -lswresample -lavutil -lpthread

#include <stdio.h>
#include<stdlib.h>  
#include<time.h>  
#include <string.h>

#include <malloc.h>
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/pixfmt.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>

//#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif

int main(int argc, char **argv){
    av_register_all();
    const int width = 1920, height = 1080;

    enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_NV12, dst_pix_fmt = AV_PIX_FMT_BGR24;
    FILE* fp = NULL;
   fp = fopen("frame-0008.yuv", "rb");
    uint8_t* yuvbuf = (uint8_t*)malloc(width * height * 3/2);

    fread(yuvbuf,  1,width * height * 3/2, fp);
    fclose(fp);

    AVFrame *pFrameRGB = av_frame_alloc();
    uint8_t *out_buffer = new uint8_t[avpicture_get_size(dst_pix_fmt, width, height)];
    //avpicture_fill是给pFrameYUV初始化一些字段,并且给填充data和linesize
    avpicture_fill((AVPicture *)pFrameRGB, out_buffer, dst_pix_fmt, width, height);

    AVFrame *yuvFrame = av_frame_alloc();

    avpicture_fill((AVPicture *)yuvFrame, yuvbuf, src_pix_fmt, width, height);

    SwsContext *sws_ctx = sws_getContext(
        width, height, src_pix_fmt,
        width, height, dst_pix_fmt,
        SWS_BILINEAR, NULL, NULL, NULL);

    /*
    int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
              const int srcStride[], int srcSliceY, int srcSliceH,
              uint8_t *const dst[], const int dstStride[]);
    srcSlice[],dst[]是输入输出图像数据各颜色通道的buffer指针数组
    srcStride[],dstStride[] 为输入输出图像数据各颜色通道每行存储的字节数数组
    srcSliceY 为从输入图像数据的第多少列开始逐行扫描,通常设为0
    srcSliceH 为需要扫描多少行,通常为输入图像数据的高度
    */
    // *(rgbFrame->linesize)--->3*width
    // *(pFrameYUV->linesize)-->width
    sws_scale(sws_ctx, yuvFrame->data, yuvFrame->linesize, 0, height, pFrameRGB->data, pFrameRGB->linesize);
    sws_freeContext(sws_ctx);

    FILE* fpout = NULL;
    fpout = fopen( "1920x1080.rgb", "wb");
    fwrite(pFrameRGB->data[0], 1, width * height * 3, fpout);
    fclose(fpout);
    delete out_buffer;
    free(yuvbuf);
    av_frame_free(&pFrameRGB);
    av_frame_free(&yuvFrame);
    return 0;
}

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值