Linux Media 02 Convert YUV to RGB

发布于 2022-11-28  108 次阅读


  Here is an example code that convert YUV format to RGB format via FFmpeg.

int VIDEOWRP_YUYV422_to_RGB24(int imgWidth, int imgHeight, 
                            uint8_t* inputData, uint8_t* outputData, 
                            int inputLength)
{
    struct SwsContext* ctx = sws_getContext(imgWidth, imgHeight, AV_PIX_FMT_YUYV422,
                                            imgWidth, imgHeight, AV_PIX_FMT_RGB24,
                                            SWS_BICUBIC, NULL, NULL, NULL);

    struct AVFrame* input_pFrame = av_frame_alloc();
    struct AVFrame* output_pFrame = av_frame_alloc();

    av_image_fill_arrays(input_pFrame->data, input_pFrame->linesize, 
                        inputData, AV_PIX_FMT_YUYV422, imgWidth, imgHeight, 1);
    av_image_fill_arrays(output_pFrame->data, output_pFrame->linesize, 
                        outputData, AV_PIX_FMT_RGB24, imgWidth, imgHeight, 1);

    sws_scale(ctx, 
            (const uint8_t**)input_pFrame->data, 
            input_pFrame->linesize, 0, imgHeight, 
            output_pFrame->data, output_pFrame->linesize);

#if 0
    FILE *fl;
    fl = fopen("img.rgb", "w");
    if (NULL == fl)
    {
        fprintf(stderr, "open write file failed.");
    }
    fwrite(outputData, inputLength * 1.5, 1, fl);
    fclose(fl);
#endif

    // end
    if (input_pFrame) av_free(input_pFrame);
    if (output_pFrame) av_free(output_pFrame);
    return 0;
}