nv12縮放(雙線性插值)及查看

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>

typedef unsigned char uint8_t;
#undef clamp_g
#define clamp_g(x, minValue, maxValue) ((x) < (minValue) ? (minValue) : ((x) > (maxValue) ? (maxValue) : (x)))
/*
輸入的寬高和輸出的寬高都要是偶數


*/

int NV12Linear(unsigned char * srcImage, unsigned char * destImage, int srcW, int srcH, int destW, int destH)
{

    float timeX = 0.0f;
    float timeY = 0.0f;

    timeX = srcW * 1.0f / destW;
    timeY = srcH * 1.0f / destH;

    int i = 0;
    int j = 0;
    int k = 0;

    unsigned char * srcUV = srcImage + srcW * srcH;
    unsigned char * destUV = destImage + destW * destH;

    for (i = 0; i < destH; i++)
    {
        for (j = 0; j < destW; j++)
        {

            int srcIdX = 0;
            int srcIdY = 0;

            float srcfIdX = 0.0f;
            float srcfIdY = 0.0f;


            float weightX[2] = { 0.0f, 0.0f };
            float weightY[2] = { 0.0f, 0.0f };
//srcpoint
            srcfIdX = j * timeX;
            srcfIdY = i * timeY;
//[X] to int
            srcIdX = (int)(srcfIdX);
            srcIdY = (int)(srcfIdY);
//
            weightX[1] = srcfIdX - srcIdX;
            weightX[0] = 1.0f - weightX[1];

            weightY[1] = srcfIdY - srcIdY;
            weightY[0] = 1.0f - weightY[1];

            destImage[(i * destW + j)] = (srcImage[(srcIdY * srcW + srcIdX)] * weightX[0] + srcImage[(srcIdY * srcW + clamp_g(srcIdX + 1, 0, srcW - 1))] * weightX[1]) * weightY[0] +
                (srcImage[(clamp_g(srcIdY + 1, 0, srcH - 1) * srcW + srcIdX)] * weightX[0] + srcImage[(clamp_g(srcIdY + 1, 0, srcH - 1) * srcW + clamp_g(srcIdX + 1, 0, srcW - 1))] * weightX[1]) * weightY[1];


            if (0 == i % 2 && 0 == j % 2)
            {

                int srcVUW = srcW / 2;
                int srcVUH = srcH / 2;

                int destVUy = i / 2;
                int destVUx = j / 2;


                int UVsrcIdX = 0;
                int UVsrcIdY = 0;

                float UVsrcfIdX = 0.0f;
                float UVsrcfIdY = 0.0f;


                float UVweightX[2] = { 0.0f };
                float UVweightY[2] = { 0.0f };

                UVsrcfIdX = destVUx * timeX;
                UVsrcfIdY = destVUy * timeY;


                UVsrcIdX = (int)(UVsrcfIdX);
                UVsrcIdY = (int)(UVsrcfIdY);



                UVweightX[1] = UVsrcfIdX - UVsrcIdX;
                UVweightX[0] = 1.0f - UVweightX[1];

                UVweightY[1] = UVsrcfIdY - UVsrcIdY;
                UVweightY[0] = 1.0f - UVweightY[1];


                destUV[(i / 2 * (destW / 2) + j / 2) * 2 + 0] =
                    (srcUV[(UVsrcIdY * srcVUW + UVsrcIdX) * 2 + 0] * weightX[0] + srcUV[(UVsrcIdY * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 0] * weightX[1]) * weightY[0] +
                    (srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + UVsrcIdX) * 2 + 0] * weightX[0] + srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 0] * weightX[1]) * weightY[1];


                destUV[(i / 2 * (destW / 2) + j / 2) * 2 + 1] =
                    (srcUV[(UVsrcIdY * srcVUW + UVsrcIdX) * 2 + 1] * weightX[0] + srcUV[(UVsrcIdY * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 1] * weightX[1]) * weightY[0] +
                    (srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + UVsrcIdX) * 2 + 1] * weightX[0] + srcUV[(clamp_g(UVsrcIdY + 1, 0, srcVUH - 1) * srcVUW + clamp_g(UVsrcIdX + 1, 0, srcVUW - 1)) * 2 + 1] * weightX[1]) * weightY[1];


            }

        }
    }

    return 0;
}
int main(int argc, char **argv)
{
    int sw=1280 ;
    int sh =720;
    int dw=0 ;
    int dh =0;
    scanf("%d %d", &dw, &dh);
    FILE *inputfp = NULL;
    FILE *outputfp = NULL;

    inputfp = fopen("nv12out.yuv", "rb");
    if (!inputfp)
    {
        fprintf(stderr, "fopen failed for input file[nv12_7.bin]\n");
        return -1;
    }

    outputfp = fopen("nv12out_1.yuv", "wb");

    if (!outputfp)
    {
        fprintf(stderr, "fopen failed for output file[nv12.nv12]\n");
        return -1;
    }


    if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0)
    {
        fprintf(stderr, "parameter error [sw= %d,sh= %d,dw= %d,dh= %d]\n", sw, sh, dw, dh);
        return -1;
    }

    int inPixels = sw * sh * 3 / 2;

    printf("inPixels = %d\n", inPixels);//test in size

    int outPixels = dw * dh * 3 / 2;

    uint8_t *pInBuffer = (uint8_t *)malloc(inPixels);
    fread(pInBuffer, 1, inPixels, inputfp);
    uint8_t *pOutBuffer = (uint8_t *)malloc(outPixels);
    printf("\ncheck\npinbuffer == %d\n\n",strlen(pInBuffer));

    NV12Linear(pInBuffer, pOutBuffer, sw, sh, dw, dh);

    fwrite(pOutBuffer, 1, outPixels, outputfp);

    free(pInBuffer);
    free(pOutBuffer);
    fclose(inputfp);
    fclose(outputfp);
    pInBuffer = NULL;
    pOutBuffer = NULL;
    inputfp = NULL;
    outputfp = NULL;
    return 0;
}

可使用pyuv軟件查看,也可在Linux下使用ffplay查看
命令

ffplay -f rawvideo -video_size 3840*2160 yuv1.yuv

參考自:https://blog.csdn.net/baidu_31872269/article/details/64449755?utm_source=blogxgwz4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章