ffmpeg之NV12转BGR24

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 18:56   11   0

1、gitclonegit://source.ffmpeg.org/ffmpeg.gitffmpeg

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;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP