nv121_nvl2函数用法

nv121_nvl2函数用法#include#include#include#include#includetypedefunsignedcharuint8_t; /**  *@paramsrcinputnv12rawdataarray  *@paramdstoutputnv12rawdataresult,  *thememor

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用
#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <sys/stat.h>

typedef unsigned char uint8_t;

 

/** 

 * @param src input nv12 raw data array  

 * @param dst output nv12 raw data result, 

 * the memory need to be allocated outside of the function 

 * @param srcWidth width of the input nv12 image 

 * @param srcHeight height of the input nv12 image 

 * @param dstWidth

 * @param dstHeight 

 */

void nv12_nearest_scale(uint8_t* __restrict src, uint8_t* __restrict dst,

                        int srcWidth, int srcHeight, int dstWidth, int

dstHeight)      //restrict keyword is for compiler to optimize program

{

    register int sw = srcWidth;  //register keyword is for local var to accelorate 

    register int sh = srcHeight;

    register int dw = dstWidth;

    register int dh = dstHeight;

    register int y, x;

    unsigned long int srcy, srcx, src_index, dst_index;

    unsigned long int xrIntFloat_16 = (sw << 16) / dw + 1; //better than float division

    unsigned long int yrIntFloat_16 = (sh << 16) / dh + 1;

    uint8_t* dst_uv = dst + dh * dw; //memory start pointer of dest uv

    uint8_t* src_uv = src + sh * sw; //memory start pointer of source uv

    uint8_t* dst_uv_yScanline;

    uint8_t* src_uv_yScanline;

    uint8_t* dst_y_slice = dst; //memory start pointer of dest y

    uint8_t* src_y_slice;

    uint8_t* sp;

    uint8_t* dp;

 

    for (y = 0; y < (dh & ~7); ++y)  //’dh & ~7′ is to generate faster assembly code

    {

        srcy = (y * yrIntFloat_16) >> 16;

        src_y_slice = src + srcy * sw;

        if((y & 1) == 0)

        {

            dst_uv_yScanline = dst_uv + (y / 2) * dw;

            src_uv_yScanline = src_uv + (srcy / 2) * sw;

        }

        for(x = 0; x < (dw & ~7); ++x)

        {

            srcx = (x * xrIntFloat_16) >> 16;

            dst_y_slice[x] = src_y_slice[srcx];

            if((y & 1) == 0) //y is even

            {

                if((x & 1) == 0) //x is even

                {

                    src_index = (srcx / 2) * 2;

            

                    sp = dst_uv_yScanline + x;

                    dp = src_uv_yScanline + src_index;

                    *sp = *dp;

                    ++sp;

                    ++dp;

                    *sp = *dp;

                }

             }

         }

         dst_y_slice += dw;

    }

}

void nv12_bilinear_scale (uint8_t* src, uint8_t* dst,

        int srcWidth, int srcHeight, int dstWidth,int dstHeight)

{

    int x, y;

    int ox, oy;

    int tmpx, tmpy;

    int xratio = (srcWidth << 8)/dstWidth;

    int yratio = (srcHeight << 8)/dstHeight;

    uint8_t* dst_y = dst;

    uint8_t* dst_uv = dst + dstHeight * dstWidth;

    uint8_t* src_y = src;

    uint8_t* src_uv = src + srcHeight * srcWidth;

    uint8_t y_plane_color[2][2];

    uint8_t u_plane_color[2][2];

    uint8_t v_plane_color[2][2];

    int j,i;

    int size = srcWidth * srcHeight;

    int offsetY;

    int y_final, u_final, v_final; 

    int u_final1 = 0;

    int v_final1 = 0;

    int u_final2 = 0;

    int v_final2 = 0;

    int u_final3 = 0;

    int v_final3 = 0;

    int u_final4 = 0;

    int v_final4 = 0;

    int u_sum = 0;

    int v_sum = 0;

    tmpy = 0;

    for (j = 0; j < (dstHeight & ~7); ++j)

    {

        //tmpy = j * yratio;

    oy = tmpy >> 8;

    y = tmpy & 0xFF;

    tmpx = 0;

    for (i = 0; i < (dstWidth & ~7); ++i)

    {

        // tmpx = i * xratio;

        ox = tmpx >> 8;

        x = tmpx & 0xFF;

    

        offsetY = oy * srcWidth;

            //YYYYYYYYYYYYYYYY

        y_plane_color[0][0] = src[ offsetY + ox ];

        y_plane_color[1][0] = src[ offsetY + ox + 1 ];

        y_plane_color[0][1] = src[ offsetY + srcWidth + ox ];

        y_plane_color[1][1] = src[ offsetY + srcWidth + ox + 1 ];

            

        int y_final = (0x100 – x) * (0x100 – y) * y_plane_color[0][0]

            + x * (0x100 – y) * y_plane_color[1][0]

            + (0x100 – x) * y * y_plane_color[0][1]

            + x * y * y_plane_color[1][1];

        y_final = y_final >> 16;

        if (y_final>255)

            y_final = 255;

        if (y_final<0)

            y_final = 0;

        dst_y[ j * dstWidth + i] = (uint8_t)y_final; //set Y in dest array 

            //UVUVUVUVUVUV

        if((j & 1) == 0) //j is even

        {

            if((i & 1) == 0) //i is even

            {

                u_plane_color[0][0] = src[ size + offsetY + ox ];

                u_plane_color[1][0] = src[ size + offsetY + ox ];

                u_plane_color[0][1] = src[ size + offsetY + ox ];

                u_plane_color[1][1] = src[ size + offsetY + ox ];

                v_plane_color[0][0] = src[ size + offsetY + ox + 1];

                v_plane_color[1][0] = src[ size + offsetY + ox + 1];

                v_plane_color[0][1] = src[ size + offsetY + ox + 1];

                v_plane_color[1][1] = src[ size + offsetY + ox + 1];

            }

            else //i is odd

            {

                u_plane_color[0][0] = src[ size + offsetY + ox – 1 ];

                u_plane_color[1][0] = src[ size + offsetY + ox + 1 ];

                u_plane_color[0][1] = src[ size + offsetY + ox – 1 ];

                u_plane_color[1][1] = src[ size + offsetY + ox + 1 ];

                v_plane_color[0][0] = src[ size + offsetY + ox ];

                v_plane_color[1][0] = src[ size + offsetY + ox + 1 ];

                v_plane_color[0][1] = src[ size + offsetY + ox ];

                v_plane_color[1][1] = src[ size + offsetY + ox + 1 ];

            }

        }

        else // j is odd

        {

            if((i & 1) == 0) //i is even

            {

                u_plane_color[0][0] = src[ size + offsetY + ox ];

                u_plane_color[1][0] = src[ size + offsetY + ox ];

                u_plane_color[0][1] = src[ size + offsetY + srcWidth + ox ];

                u_plane_color[1][1] = src[ size + offsetY + srcWidth + ox ];

                    

                v_plane_color[0][0] = src[ size + offsetY + ox + 1];

                v_plane_color[1][0] = src[ size + offsetY + ox + 1];

                v_plane_color[0][1] = src[ size + offsetY + srcWidth + ox + 1];

                v_plane_color[1][1] = src[ size + offsetY + srcWidth + ox + 1];                                                    

            }

            else //i is odd

            {

                u_plane_color[0][0] = src[ size + offsetY + ox – 1 ];

                u_plane_color[1][0] = src[ size + offsetY + srcWidth + ox – 1 ];

                u_plane_color[0][1] = src[ size + offsetY + ox + 1];

                u_plane_color[1][1] = src[ size + offsetY + srcWidth + ox + 1];

                v_plane_color[0][0] = src[ size + offsetY + ox ];

                v_plane_color[1][0] = src[ size + offsetY + srcWidth + ox ];

                v_plane_color[0][1] = src[ size + offsetY + ox + 2 ];

                v_plane_color[1][1] = src[ size + offsetY + srcWidth + ox + 2 ];

            }

        }

       int u_final = (0x100 – x) * (0x100 – y) * u_plane_color[0][0]

                     + x * (0x100 – y) * u_plane_color[1][0]

                     + (0x100 – x) * y * u_plane_color[0][1]

                     + x * y * u_plane_color[1][1];

       u_final = u_final >> 16;

       int v_final = (0x100 – x) * (0x100 – y) * v_plane_color[0][0]

                      + x * (0x100 – y) * v_plane_color[1][0]

                      + (0x100 – x) * y * v_plane_color[0][1]

                      + x * y * v_plane_color[1][1];

       v_final = v_final >> 16;

       if((j & 1) == 0)

       {

           if((i & 1) == 0)

           {    

               //set U in dest array  

               dst_uv[(j / 2) * dstWidth + i ] = (uint8_t)(u_sum / 4);

               //set V in dest array

               dst_uv[(j / 2) * dstWidth + i + 1] = (uint8_t)(v_sum / 4);

               u_sum = 0;

               v_sum = 0;

           }

       }

       else

       {

           u_sum += u_final;

           v_sum += v_final;

       }

       tmpx += xratio;

    }

    tmpy += yratio;

    }

}

int ImageResize(uint8_t * src, uint8_t* dst, int sw,

        int sh,int dw,int dh)

{

    if( (src == NULL) || (dst == NULL) || (0 == dw) || (0 == dh) ||

            (0 == sw) || (0 == sh))

    {

        printf(“params error\n”);

        return -1;

    }

        nv12_nearest_scale(src, dst, sw, sh, dw, dh);

    //nv12_bilinear_scale(src, dst, sw, sh, dw, dh);

    //greyscale(src, dst, sw, sh, dw, dh);

    return 0;

}

int main(int argc,char**argv)

{

    if(argc!=7)

    {

        printf(“Input Error!\n”);

        printf(“Usage :  <Input NV12file> <Output NV12file> 

                <sw><sh> <dw> <dh>”);

        return 0;

    }

 

    FILE *inputfp = NULL;

    FILE *outputfp = NULL;

 

    inputfp = fopen(argv[1], “rb”);

    if (!inputfp)

    {

        fprintf(stderr, “fopen failed for input file[%s]\n”,argv[1]);

        return -1;

    }

 

    outputfp = fopen(argv[2], “wb”);

 

    if (!outputfp)

    {

        fprintf(stderr, “fopen failed for output file[%s]\n”,argv[2]);

        return -1;

    }

 

    int sw = atoi(argv[3]);

    int sh = atoi(argv[4]);

    int dw = atoi(argv[5]);

    int dh = atoi(argv[6]);

 

    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;

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

 

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

        //compute frame per second

    int i = 0;

    clock_t start = clock();

 

    for(;i<1000;++i)

    {

        ImageResize(pInBuffer,pOutBuffer,1536,1088,1024,600);//can change to be any resolution    

    }

    clock_t finish = clock();

    float duration = (float)(finish-start)/CLOCKS_PER_SEC;

    float fps = 1000 / duration;

    printf(“nv12Scaling:%d*%d–>%d*%d,time cost:%6.2ffps\n”,sw,sh,dw,dh,fps);

 

    fwrite(pOutBuffer, 1 , outPixels, outputfp);

 

    free(pInBuffer);

    free(pOutBuffer);

    fclose(inputfp);

    fclose(outputfp);

    pInBuffer = NULL;

    pOutBuffer = NULL;

    inputfp = NULL;

    outputfp = NULL;

    return 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189959.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JRebel热部署

    JRebel热部署

    2021年7月11日
    74
  • MD5 编码 转换

    MD5 编码 转换

    2021年5月9日
    218
  • Jlink或者stlink用于SWD接口下载程序

    Jlink或者stlink用于SWD接口下载程序最近要使用stm32f103c8t6最小系统板,直接ISP串口下载程序太麻烦,就想着使用swd接口来调试。结果:通过SWD接口下载程序成功,但调试失败,还不知原因,会的的人麻烦交流一下。SWD接口:3.3VDIO(数据)CLK(时钟)GND1.首先声明jlink和stlink都有jtag和swd调试功能。jlink接口如下:如图,我使用的就是VCC…

    2022年4月25日
    48
  • 大数据项目实训教学解决方案

    大数据项目实训教学解决方案大数据项目实训教学解决方案【课程资源】大数据项目实训和课程设计课程体系中职、高职还有本科,实训教学最关键的要素都是课程资源。唯众以大数据基础课程、核心技术课程为基础,以大数据产业实际应用案例为原型,遵照院校实训教学规范,开发了一系列的项目实训、课程设计课程资源,基本满足各院校大数据实训教学需求。【软件系统】大数据教学云平台大数据教学云平台是一个开放式的课程平台,除了唯众的课程体系之外,老师可自主开发在线课程,支持Word、PPT、PDF、视频等常见课件直接转换成在线课程,从而让老师很方便的将专业基

    2022年5月11日
    37
  • 《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项

    《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上。MyBatis提供了一级缓存、二级缓存这两个缓存机制,能够很好地处理和维护缓存,以提高系统的性能。本文的目的则是向读者详细介绍MyBatis的一级缓存,深入源码,解析MyBatis一级缓存的实现原理,并且针对一级缓存的特点提出了在实际使用过程中应该注意的事项。读完本文,你将会学到:1、什么是一级缓存?为什么使用一级缓存?2、MyBatis的一级缓存是怎样组织的?(即SqlS

    2022年5月21日
    42
  • 线程池参数及配置「建议收藏」

    线程池参数及配置「建议收藏」线程池-线程池参数及配置在实际项目中线程的应用都会使用线程池来管理,线程池的常用参数及配置学习记录。目录线程池-线程池参数及配置一、线程池在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收。所以提高服务程序效率的一个手段就是尽可能减少创建和销毁对象的次数,特别是一些很耗资源的对象创建和销毁。如果并发的线程数多,并且每个线程都是…

    2022年5月3日
    90

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号