t113全志g2d应用,解析摄像头数据

// g2d 解析时用到的东西

 //

my_ion_lib.h

#ifndef __MY_ION_LIB_H__
#define __MY_ION_LIB_H__

#ifdef __cplusplus
extern "C" {
#endif
struct ion_memory
{
    unsigned int size;
    int fd;
    void* virt_addr;  //虚拟地址
    unsigned int phy_addr;  //物理地址
};

// 注意使用前,请确认,有 /dev/ion 和 /dev/cedar_dev

int my_ion_init(void);
int my_ion_release(void);
int my_ion_alloc(struct ion_memory* mem,unsigned int size);
int my_ion_free(struct ion_memory* mem);
int my_ion_flush(struct ion_memory* mem);


#ifdef __cplusplus
} //extern "C"
#endif

#endif //__MY_ION_LIB_H__



/*

date -s 2023-06-13     
date -s 9:33:30
hwclock -w

*/

/*

# gcc head.h method_1.c method_2.c -fPIC -shared -o method.so  
# aarch64-linux-gnu-gcc -lstdc++ -fPIC -shared -o libai_dface.so ai_dface_check.cpp feature_memory_database.cpp movingaverage.cpp -I. -I./include -I/opt/EmbedSky/TQT507/CoreA/longan//platform/framework/auto/sdk_lib/include -I/opt/install/include
cp ./libai_dface.so /opt/EmbedSky/TQT507/CoreA/longan/out/t507/evb/longan/buildroot/target/usr/lib/

*/







my_ion_lib.c






#include "my_ion_lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include <pthread.h>
#include <asm-generic/ioctl.h>


// package/allwinner/libuapi/src/ion_alloc_5.4.h
#define __u64  unsigned long long  
#define __u32  unsigned long  

struct aw_ion_new_alloc_data {
    __u64 len;
    __u32 heap_id_mask;
    __u32 flags;
    __u32 fd;
    __u32 unused;
};



#define SZ_64M 0x04000000
#define SZ_4M 0x00400000
#define SZ_1M 0x00100000
#define SZ_64K 0x00010000
#define SZ_4k 0x00001000
#define SZ_1k 0x00000400

enum ion_heap_type {
    AW_ION_SYSTEM_HEAP_TYPE,
    AW_ION_SYSTEM_CONTIG_HEAP_TYPE,
    AW_ION_CARVEOUT_HEAP_TYPE,
    AW_ION_TYPE_HEAP_CHUNK,
    AW_ION_TYPE_HEAP_DMA,
    AW_ION_TYPE_HEAP_CUSTOM,

    AW_ION_TYPE_HEAP_SECURE,

    AW_ION_NUM_HEAPS = 16,
};



#define AW_MEM_ENGINE_REQ 0x206
#define AW_MEM_ENGINE_REL 0x207
#define AW_MEM_GET_IOMMU_ADDR    0x502
#define AW_MEM_FREE_IOMMU_ADDR    0x503

#define AW_ION_CACHED_FLAG 1
#define AW_ION_CACHED_NEEDS_SYNC_FLAG 2

#define ION_IOC_SUNXI_FLUSH_RANGE           5
#define ION_IOC_SUNXI_FLUSH_ALL             6
#define ION_IOC_SUNXI_PHYS_ADDR             7
#define ION_IOC_SUNXI_DMA_COPY              8

#define ION_IOC_SUNXI_TEE_ADDR              17
#define AW_ION_SYSTEM_HEAP_MASK        (1 << AW_ION_SYSTEM_HEAP_TYPE)
#define AW_ION_SYSTEM_CONTIG_HEAP_MASK    (1 << AW_ION_SYSTEM_CONTIG_HEAP_TYPE)
#define AW_ION_CARVEOUT_HEAP_MASK        (1 << AW_ION_CARVEOUT_HEAP_TYPE)
#define AW_ION_DMA_HEAP_MASK        (1 << AW_ION_TYPE_HEAP_DMA)
#define AW_ION_SECURE_HEAP_MASK      (1 << AW_ION_TYPE_HEAP_SECURE)


#define AW_MEM_ION_IOC_MAGIC 'I'
#define AW_ION_IOC_NEW_ALLOC _IOWR(AW_MEM_ION_IOC_MAGIC, 0, struct aw_ion_new_alloc_data)


// lichee/linux-5.4/drivers/media/cedar-ve/cedar_ve.h
enum IOCTL_CMD {
    IOCTL_UNKOWN = 0x100,
    IOCTL_GET_ENV_INFO,
    IOCTL_WAIT_VE_DE,
    IOCTL_WAIT_VE_EN,
    IOCTL_RESET_VE,
    IOCTL_ENABLE_VE,
    IOCTL_DISABLE_VE,
    IOCTL_SET_VE_FREQ,

    IOCTL_CONFIG_AVS2 = 0x200,
    IOCTL_GETVALUE_AVS2,
    IOCTL_PAUSE_AVS2,
    IOCTL_START_AVS2,
    IOCTL_RESET_AVS2,
    IOCTL_ADJUST_AVS2,
    IOCTL_ENGINE_REQ,
    IOCTL_ENGINE_REL,
    IOCTL_ENGINE_CHECK_DELAY,
    IOCTL_GET_IC_VER,
    IOCTL_ADJUST_AVS2_ABS,
    IOCTL_FLUSH_CACHE,
    IOCTL_SET_REFCOUNT,
    IOCTL_FLUSH_CACHE_ALL,
    IOCTL_TEST_VERSION,

    IOCTL_GET_LOCK = 0x310,
    IOCTL_RELEASE_LOCK,

    IOCTL_SET_VOL = 0x400,

    IOCTL_WAIT_JPEG_DEC = 0x500,
    /*for get the ve ref_count for ipc to delete the semphore*/
    IOCTL_GET_REFCOUNT,

    /*for iommu*/
    IOCTL_GET_IOMMU_ADDR,
    IOCTL_FREE_IOMMU_ADDR,

    /* map/unmap dma buffer to get/free phyaddr by dma fd */
    /* get/free iommu addr will not use since kernel 5.4  */
    IOCTL_MAP_DMA_BUF,
    IOCTL_UNMAP_DMA_BUF,

    /*for fush cache range since kernel 5.4*/
    IOCTL_FLUSH_CACHE_RANGE,

    /*for debug*/
    IOCTL_SET_PROC_INFO,
    IOCTL_STOP_PROC_INFO,
    IOCTL_COPY_PROC_INFO,

    IOCTL_SET_DRAM_HIGH_CHANNAL = 0x600,

    /* debug for decoder and encoder*/
    IOCTL_PROC_INFO_COPY = 0x610,
    IOCTL_PROC_INFO_STOP,
    IOCTL_POWER_SETUP = 0x700,
    IOCTL_POWER_SHUTDOWN,
};

#define VE_LOCK_VDEC        0x01
#define VE_LOCK_VENC        0x02
#define VE_LOCK_JDEC        0x04
#define VE_LOCK_00_REG      0x08
#define VE_LOCK_04_REG      0x10
#define VE_LOCK_ERR         0x80

#define VE_LOCK_PROC_INFO   0x1000








// prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702/sysroot/usr/include/linux/dma-buf.h:41
struct dma_buf_sync {
    __u64 flags;
};

#define DMA_BUF_SYNC_READ      (1 << 0)
#define DMA_BUF_SYNC_WRITE     (2 << 0)
#define DMA_BUF_SYNC_RW        (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE)
#define DMA_BUF_SYNC_START     (0 << 2)
#define DMA_BUF_SYNC_END       (1 << 2)
#define DMA_BUF_SYNC_VALID_FLAGS_MASK \
    (DMA_BUF_SYNC_RW | DMA_BUF_SYNC_END)

#define DMA_BUF_NAME_LEN    32

#define DMA_BUF_BASE        'b'
#define DMA_BUF_IOCTL_SYNC    _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
#define DMA_BUF_SET_NAME    _IOW(DMA_BUF_BASE, 1, const char *)



// package/allwinner/tina_multimedia/libcedarc/include/veInterface.h
struct user_iommu_param {
    int            fd;
    unsigned int   iommu_addr;
};














static int ion_fd = -1;
static int cedar_fd = -1;
int my_ion_init(void){
    my_ion_release();
    ion_fd = open("/dev/ion", O_RDWR);
    cedar_fd = open("/dev/cedar_dev", O_RDONLY);
    
    ioctl(cedar_fd, IOCTL_ENGINE_REQ, 0);
    return 0;
}
int my_ion_release(void){
    if (cedar_fd != -1)
    {
       ioctl(cedar_fd, IOCTL_ENGINE_REL, 0);
       close(cedar_fd);
       cedar_fd = -1;
    }
    
    if (ion_fd != -1)
    {
       close(ion_fd);
       ion_fd = -1;
    }
    return 0;
}
int my_ion_alloc(struct ion_memory* mem,unsigned int size){
    struct aw_ion_new_alloc_data alloc_data;
    alloc_data.len = size;
    alloc_data.heap_id_mask = AW_ION_SYSTEM_HEAP_MASK;
    alloc_data.flags = AW_ION_CACHED_FLAG | AW_ION_CACHED_NEEDS_SYNC_FLAG;
    alloc_data.fd = 0;
    alloc_data.unused = 0;
    ioctl(ion_fd, AW_ION_IOC_NEW_ALLOC, &alloc_data);
    
    void* virt_addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, alloc_data.fd, 0);
    
    struct user_iommu_param iommu_param;
    iommu_param.fd = alloc_data.fd;
    iommu_param.iommu_addr = 0;
    ioctl(cedar_fd, IOCTL_GET_IOMMU_ADDR, &iommu_param);
    
    mem->size = size;
    mem->fd = alloc_data.fd;
    mem->virt_addr = virt_addr;
    mem->phy_addr = iommu_param.iommu_addr;
    return 0;
}
int my_ion_free(struct ion_memory* mem){
    if (mem->fd == -1)
    return 0;
    
    struct user_iommu_param iommu_param;
    iommu_param.fd = mem->fd;
    ioctl(cedar_fd, IOCTL_FREE_IOMMU_ADDR, &iommu_param);
    
    munmap(mem->virt_addr, mem->size);
    
    close(mem->fd);
    
    mem->size = 0;
    mem->fd = -1;
    mem->virt_addr = 0;
    mem->phy_addr = 0;
    return 0;
}
int my_ion_flush(struct ion_memory* mem){
    struct dma_buf_sync sync;
    sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
    ioctl(mem->fd, DMA_BUF_IOCTL_SYNC, &sync);
    return 0;
}


/*
// g2d 显示
// 步骤1
ion_allocator ion;
ion.open();
struct ion_memory yuv_ion;
ion.alloc(rgb_size, &rgb_ion);
struct ion_memory rgb_ion;
ion.alloc(yuv_size, &yuv_ion);
int g2d_fd = ::open("/dev/g2d", O_RDWR);
// 步骤2
memcpy((unsigned char*)yuv_ion.virt_addr, yuv420sp, yuv_size);
ion.flush(&yuv_ion);
// 步骤3
g2d_blt_h blit;
memset(&blit, 0, sizeof(blit));
blit.flag_h = G2D_BLT_NONE_H;
blit.src_image_h.format = G2D_FORMAT_YUV420UVC_V1U1V0U0;
blit.src_image_h.width = width;
blit.src_image_h.height = height;
blit.src_image_h.align[0] = 0;
blit.src_image_h.align[1] = 0;
blit.src_image_h.clip_rect.x = 0;
blit.src_image_h.clip_rect.y = 0;
blit.src_image_h.clip_rect.w = width;
blit.src_image_h.clip_rect.h = height;
blit.src_image_h.gamut = G2D_BT601;
blit.src_image_h.bpremul = 0;
blit.src_image_h.mode = G2D_PIXEL_ALPHA;
blit.src_image_h.use_phy_addr = 0;
blit.src_image_h.fd = yuv_ion.fd;
blit.dst_image_h.format = G2D_FORMAT_RGB888;
blit.dst_image_h.width = width;
blit.dst_image_h.height = height;
blit.dst_image_h.align[0] = 0;
blit.dst_image_h.clip_rect.x = 0;
blit.dst_image_h.clip_rect.y = 0;
blit.dst_image_h.clip_rect.w = width;
blit.dst_image_h.clip_rect.h = height;
blit.dst_image_h.gamut = G2D_BT601;
blit.dst_image_h.bpremul = 0;
blit.dst_image_h.mode = G2D_PIXEL_ALPHA;
blit.dst_image_h.use_phy_addr = 0;
blit.dst_image_h.fd = rgb_ion.fd;
ioctl(g2d_fd, G2D_CMD_BITBLT_H, &blit);
// 步骤4
ion.flush(&rgb_ion);
memcpy(rgb, (const unsigned char*)rgb_ion.virt_addr, rgb_size);
// 步骤5
ion.free(&rgb_ion);
ion.free(&yuv_ion);
ion.close();
::close(g2d_fd);


*/





my_g2d_lib.h

#ifndef __MY_G2D_LIB_H__
#define __MY_G2D_LIB_H__

#ifdef __cplusplus
extern "C" {
#endif
#include "g2d_driver.h"
#include "my_ion_lib.h"

enum {
    MY_G2D_ROT_NONE   = 0,
    MY_G2D_ROT_90  = 0x00000100,
    MY_G2D_ROT_180 = 0x00000200,
    MY_G2D_ROT_270 = 0x00000300,
    MY_G2D_ROT_H = 0x00001000,
    MY_G2D_ROT_V = 0x00002000,
};


int my_g2d_init(void);
int my_g2d_release(void);
int my_g2d_type_transe(struct ion_memory* inbuf,unsigned int intype,int inw,int inh,struct ion_memory* outbuf,unsigned int outtype,int outw,int outh);
int my_g2d_type_rotate(int rotate,struct ion_memory* inbuf,struct ion_memory* outbuf,unsigned int outtype,int outw,int outh);

#ifdef __cplusplus
} //extern "C"
#endif

#endif // __MY_G2D_LIB_H__


/*
#include "my_g2d_lib.h"
#include "my_ion_lib.h"

struct ion_memory inbuf;
struct ion_memory outbuf;


#include <fcntl.h> // for open
#include <stdio.h>
#include <unistd.h> // for close
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int stopflag=0;
void sigintHandler(int signum) {
    if(stopflag>0){return ;}
    stopflag=1;

    my_ion_free(&inbuf);
    my_ion_free(&outbuf);
    my_g2d_release();
    my_ion_release();

    usleep(1000*200);//等待退出
    printf("waitfor normal exit!!!\n");
    exit(0);
}
int main(int argc, char **argv){
    // 注册SIGINT信号处理函数
    signal(SIGINT, sigintHandler);
    signal(SIGSEGV, sigintHandler);

    // 初始化
    my_g2d_init();
    my_ion_init();
    my_ion_alloc(&inbuf,CAM_WIDTH * CAM_HEIGHT * 4);
    my_ion_alloc(&outbuf,SCREEN_WIDTH * SCREEN_HEIGHT * 4);


    while(!stopflag){
        // 获取数据  param,paramlen

        // 转化数据
        if(CAM_FORMAT==V4L2_PIX_FMT_YUYV){
            memcpy(inbuf.virt_addr,param,paramlen);// G2D_FORMAT_BGRA8888
            my_g2d_type_transe(&inbuf,G2D_FORMAT_IYUV422_V0Y1U0Y0,video_width,video_height,&outbuf,G2D_FORMAT_ARGB8888,video_width,video_height);
            memcpy(yuv_buffer,outbuf.virt_addr,CAM_WIDTH * CAM_HEIGHT * 4);
        }
        else if(CAM_FORMAT==V4L2_PIX_FMT_NV21){
            memcpy(inbuf.virt_addr,param,paramlen);
            my_g2d_type_transe(&inbuf,G2D_FORMAT_YUV420UVC_U1V1U0V0,video_width,video_height,&outbuf,G2D_FORMAT_ARGB8888,video_width,video_height);
            memcpy(yuv_buffer,outbuf.virt_addr,CAM_WIDTH * CAM_HEIGHT * 4);
        }

        // 显示数据
        lcd_image(0, CAM_WIDTH, 0, CAM_HEIGHT, yuv_buffer);
    }

    // 资源回收
    sigintHandler(0);

}
*/



my_g2d_lib.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include <pthread.h>
#include <asm-generic/ioctl.h>
#include <sys/ioctl.h>


#include "my_g2d_lib.h"
#include "my_ion_lib.h"
static int g2d_fd =0;
int my_g2d_init(void){
    g2d_fd = open("/dev/g2d", O_RDWR);
    return 0;
}

int my_g2d_release(void){
    close(g2d_fd);
    return 0;
}

int my_g2d_type_transe(struct ion_memory* inbuf,unsigned int intype,int inw,int inh,struct ion_memory* outbuf,unsigned int outtype,int outw,int outh){
    my_ion_flush(inbuf);
    // G2D_FORMAT_YUV420UVC_V1U1V0U0  G2D_FORMAT_BGRA8888
    g2d_blt_h blit;
    memset(&blit, 0, sizeof(blit));
    blit.flag_h = G2D_BLT_NONE_H;
    blit.src_image_h.format = intype;
    blit.src_image_h.width = inw;
    blit.src_image_h.height = inh;
    blit.src_image_h.align[0] = 0;
    blit.src_image_h.align[1] = 0;
    blit.src_image_h.clip_rect.x = 0;
    blit.src_image_h.clip_rect.y = 0;
    blit.src_image_h.clip_rect.w = outw;
    blit.src_image_h.clip_rect.h = outh;
    blit.src_image_h.gamut = G2D_BT601;
    blit.src_image_h.bpremul = 0;
    blit.src_image_h.mode = G2D_PIXEL_ALPHA;
    blit.src_image_h.use_phy_addr = 0;
    blit.src_image_h.fd = inbuf->fd;
    blit.dst_image_h.format = outtype;
    blit.dst_image_h.width = outw;
    blit.dst_image_h.height = outh;
    blit.dst_image_h.align[0] = 0;
    blit.dst_image_h.clip_rect.x = 0;
    blit.dst_image_h.clip_rect.y = 0;
    blit.dst_image_h.clip_rect.w = outw;
    blit.dst_image_h.clip_rect.h = outh;
    blit.dst_image_h.gamut = G2D_BT601;
    blit.dst_image_h.bpremul = 0;
    blit.dst_image_h.mode = G2D_PIXEL_ALPHA;
    blit.dst_image_h.use_phy_addr = 0;
    blit.dst_image_h.fd = outbuf->fd;
    ioctl(g2d_fd, G2D_CMD_BITBLT_H, &blit);
    my_ion_flush(outbuf);
    return 0;
}

int my_g2d_type_rotate(int rotate,struct ion_memory* inbuf,struct ion_memory* outbuf,unsigned int outtype,int outw,int outh){
    if(rotate==0){return 0;}
    if(rotate==0||rotate==G2D_ROT_90||rotate==G2D_ROT_180||rotate==G2D_ROT_270||rotate==G2D_ROT_H||rotate==G2D_ROT_V){ }else{return -1;}
    my_ion_flush(inbuf);
    // G2D_FORMAT_YUV420UVC_V1U1V0U0  G2D_FORMAT_BGRA8888
    // g2d_blt_h 方案有噪声
    g2d_blt_h blit;
    memset(&blit, 0, sizeof(blit));
    blit.flag_h = rotate;
    blit.src_image_h.format = outtype;
    blit.src_image_h.width = outw;
    blit.src_image_h.height = outh;
    blit.src_image_h.align[0] = 0;
    blit.src_image_h.align[1] = 0;
    blit.src_image_h.clip_rect.x = 0;
    blit.src_image_h.clip_rect.y = 0;
    blit.src_image_h.clip_rect.w = outw;
    blit.src_image_h.clip_rect.h = outh;
    blit.src_image_h.gamut = G2D_BT601;
    blit.src_image_h.bpremul = 0;
    blit.src_image_h.mode = G2D_PIXEL_ALPHA;
    blit.src_image_h.use_phy_addr = 0;
    blit.src_image_h.fd = inbuf->fd;

    blit.dst_image_h.format = outtype;
    blit.dst_image_h.width = outw;
    blit.dst_image_h.height = outh;
    blit.dst_image_h.align[0] = 0;
    blit.dst_image_h.clip_rect.x = 0;
    blit.dst_image_h.clip_rect.y = 0;
    blit.dst_image_h.clip_rect.w = outw;
    blit.dst_image_h.clip_rect.h = outh;
    blit.dst_image_h.gamut = G2D_BT601;
    blit.dst_image_h.bpremul = 0;
    blit.dst_image_h.mode = G2D_PIXEL_ALPHA;
    blit.dst_image_h.use_phy_addr = 0;
    blit.dst_image_h.fd = outbuf->fd;
    ioctl(g2d_fd, G2D_CMD_BITBLT_H, &blit);

    // if(rotate==G2D_ROT_90){rotate=G2D_BLT_ROTATE90;}
    // else if(rotate==G2D_ROT_180){rotate=G2D_BLT_ROTATE180;}
    // else if(rotate==G2D_ROT_270){rotate=G2D_BLT_ROTATE270;}
    // else if(rotate==G2D_ROT_H){rotate=G2D_BLT_FLIP_HORIZONTAL;}
    // else if(rotate==G2D_ROT_V){rotate=G2D_BLT_FLIP_VERTICAL;}
    // else {return -1;}

    // g2d_blt blit;
    // memset(&blit, 0, sizeof(blit));
    // blit.flag = rotate;
    // blit.src_image.format = outtype;
    // blit.src_image.w = outw;
    // blit.src_image.h = outh;
    // blit.src_image.align[0] = 0;
    // blit.src_image.align[1] = 0;
    // blit.src_image.clip_rect.x = 0;
    // blit.src_image.clip_rect.y = 0;
    // blit.src_image.clip_rect.w = outw;
    // blit.src_image.clip_rect.h = outh;
    // blit.src_image.gamut = G2D_BT601;
    // blit.src_image.bpremul = 0;
    // blit.src_image.mode = G2D_PIXEL_ALPHA;
    // blit.src_image.use_phy_addr = 0;
    // blit.src_image.fd = inbuf->fd;

    // blit.dst_image_h.format = outtype;
    // blit.dst_image_h.width = outw;
    // blit.dst_image_h.height = outh;
    // blit.dst_image_h.align[0] = 0;
    // blit.dst_image_h.clip_rect.x = 0;
    // blit.dst_image_h.clip_rect.y = 0;
    // blit.dst_image_h.clip_rect.w = outw;
    // blit.dst_image_h.clip_rect.h = outh;
    // blit.dst_image_h.gamut = G2D_BT601;
    // blit.dst_image_h.bpremul = 0;
    // blit.dst_image_h.mode = G2D_PIXEL_ALPHA;
    // blit.dst_image_h.use_phy_addr = 0;
    // blit.dst_image_h.fd = outbuf->fd;
    // ioctl(g2d_fd, G2D_CMD_BITBLT_H, &blit);



    my_ion_flush(outbuf);
    return 0;
}

 

posted @ 2024-07-05 11:28  小城熊儿  阅读(4)  评论(0编辑  收藏  举报