⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipassexample.c

📁 德州仪器新推出的达芬奇技术dm355的源代码
💻 C
字号:
/* include files */
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <asm/arch/davinci_previewer.h>
#include <sys/mman.h>
/* header file containing bayer image */
#include "bayerimage.h"

/* Size of the input image */
#define HEIGHT	160
#define WIDTH	2560

#define OUTPUT_FILE_NAME    "out.YUV"
/* structure buffer used for storing mapped buffer's addresses 
     and their size */
struct buffer {
	void *addr;
	int length;
};

/* Array containing CFA coefficients */
#define R_PHASE \
0,   0,    0, 0, \
0,   64,   0, 0, \
0,   0,    0, 0, \
0,   0,    0, 0, \
\
0,   16,   0, 0, \
16,   0,   16,0, \
\
16,   0,   16,0,  \
0,    0,   0, 0, \
16,   0,   16,0, \
0,    0,   0, 0, \
\
0,    16,  0, 0,  \
0,    0,   0, 0, \
\
0,   0,    0, 0,  \
0,   64,   0, 0, \
0,   0,    0, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
32,  0,   32, 0, \
\
16,-26,   16, 0,  \
26, 0,    26, 0, \
16,-26,   16, 0, \
0,  0,    0,  0, \
\
0,  0,    0,  0, \
0,  0,    0,  0,\
\
0,  0,    0,  0,  \
0, 64,    0,  0,  \
0,  0,    0,  0, \
0,  0,    0,  0, \
\
0, 32,    0,  0, \
0,  0,    0,  0, \
\
16, 26,   16, 0, \
-26, 0,  -26, 0, \
16, 26,   16, 0, \
0,   0,    0, 0, \
\
0,  32,    0, 0, \
0,  0,     0, 0,

#define GR_PHASE \
-8,  0,   -8, 0,  \
32, 32,   32, 0, \
-8,  0,   -8, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,  64,    0, 0, \
\
-10,32,  -10, 0,  \
0,  40,    0, 0, \
-10,32,  -10, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
32,  0,   32, 0, \
0,   0,    0, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,  64,    0, 0, \
\
-13,32,  -13, 0,  \
0,  52,    0, 0,\
-13,32,  -13, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,   0,    0, 0, \
\
-16, 0,  -16, 0,\
 32, 64,  32, 0, \
-16, 0,  -16, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,  64,    0, 0, \
\
0,  32,    0, 0,  \
0,  0,     0, 0, \
0,  32,    0, 0, \
0,  0,     0, 0, \
\
0,  0,     0, 0,\
0,  0,     0, 0,

#define GB_PHASE \
-8, 32, -8, 0,  \
0,  32,  0, 0, \
-8, 32, -8, 0, \
0,   0,  0, 0, \
\
0,   0,  0, 0, \
0,  64,  0, 0, \
\
-8,  0, -8, 0, \
32, 32, 32, 0, \
-8, 0,  -8, 0, \
0,  0,   0, 0, \
\
0,  0,   0, 0, \
0,  0,   0, 0, \
\
-16,32,  -16, 0,  \
0,  64,  0,   0, \
-16,32, -16,  0, \
0,  0,   0,   0, \
\
0,  0,   0,   0, \
0, 64,   0,   0, \
\
0,  0,   0,   0, \
29, 6,  29,   0,  \
0,  0,   0,   0, \
0,  0,   0,   0, \
\
0,  0,   0,   0, \
0,  0,   0,   0, \
\
0, 32,   0,   0,  \
0,  0,   0,   0,  \
0, 32,   0,   0, \
0,  0,   0,   0, \
\
0,  0,   0,   0, \
0, 64,   0,   0, \
\
-13,0,  -13,  0, \
32, 52, 32,   0, \
-13, 0,-13,   0, \
0,   0, 0,    0,  \
\
0,   0, 0,    0, \
0,   0,  0, 0,

#define B_PHASE  \
16,-6,    16, 0, \
6,  0,     6, 0, \
16, -6,   16, 0, \
0,   0,    0, 0, \
\
0,   16,   0, 0,\
16,   0,  16, 0, \
\
0,    0,   0, 0, \
0,   64,   0, 0,  \
0,    0,   0, 0, \
0,    0,   0, 0, \
\
0,   16,   0, 0, \
0,    0,   0, 0, \
\
16, -32,  16, 0, \
32,   0,  32, 0, \
16, -32,  16, 0, \
0,    0,   0, 0, \
\
0,    0,   0, 0, \
32,   0,  32, 0, \
\
0,    0,   0, 0, \
0,   64,   0, 0,  \
0,    0,   0, 0, \
0,    0,   0, 0, \
\
0,    0,   0, 0, \
0,    0,   0, 0, \
\
16, 26,   16, 0,\
-26, 0,  -26, 0, \
16, 26,   16, 0, \
0,   0,    0, 0, \
\
0,  32,    0, 0, \
0,   0,    0, 0, \
\
0,   0,    0, 0, \
0,  64,    0, 0, \
0,   0,    0, 0, \
0,   0,    0, 0, \
\
0,  32,    0, 0, \
0, 0,  0, 0,




static const char cfa_coef[] = {
	R_PHASE GR_PHASE GB_PHASE B_PHASE
};

/* main functions */
int main()
{
	struct prev_params params;
	struct prev_reqbufs reqbufs;
	struct prev_buffer t_buff, in_buff, out_buff;
	struct prev_convert convert;
	int prevfd, ret, i, j;
	char ay[HEIGHT * WIDTH], acb[HEIGHT * WIDTH / 2],
	    acr[HEIGHT * WIDTH / 2];
	int cy = 0, ccb = 0, ccr = 0;
	struct buffer buff[2];
	FILE *fp;
	int *addr = NULL;
	char *adr = NULL, *inadr, *outadr;

	/* Initialize parameters to zero */
	memset(&params, 0, sizeof(params));

	/* set down sampling rate to zero */
	params.sample_rate = 1;

	/* Set input image size */
	params.size_params.hstart = 0;
	params.size_params.vstart = 0;
	params.size_params.hsize = 1280 + 4;
	params.size_params.vsize = HEIGHT;

	/* Set input image pixel size to 8 bit */
	params.size_params.pixsize = PREV_INWIDTH_8BIT;

	/* Set input image line offset */
	params.size_params.in_pitch = WIDTH;

	/* Set output image line offset */
	params.size_params.out_pitch = (WIDTH) * 2;

	/* Set white balancing parameters */
	params.white_balance_params.wb_dgain = 0x100;
	params.white_balance_params.wb_gain[0] = 0x20;
	params.white_balance_params.wb_gain[1] = 0x20;
	params.white_balance_params.wb_gain[2] = 0x20;
	params.white_balance_params.wb_gain[3] = 0x20;
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			params.white_balance_params.wb_coefmatrix[i][j] = 0x0;
		}
	}

	/* Set RGB 2 RGB blending gains */
	params.rgbblending_params.blending[0][0] = 0x100;
	params.rgbblending_params.blending[0][1] = 0;
	params.rgbblending_params.blending[0][2] = 0;
	params.rgbblending_params.blending[1][0] = 0;
	params.rgbblending_params.blending[1][1] = 0x100;
	params.rgbblending_params.blending[1][2] = 0;
	params.rgbblending_params.blending[2][0] = 0;
	params.rgbblending_params.blending[2][1] = 0;
	params.rgbblending_params.blending[2][2] = 0x100;

	/* Set RGB 2 RGB blending offsets */
	params.rgbblending_params.offset[0] = 0;
	params.rgbblending_params.offset[1] = 0;
	params.rgbblending_params.offset[2] = 0;

	/* Set RGB 2 YCbCr color conversion gains */
	params.rgb2ycbcr_params.coeff[0][0] = 0x4d;
	params.rgb2ycbcr_params.coeff[0][1] = 0x96;
	params.rgb2ycbcr_params.coeff[0][2] = 0x1D;
	params.rgb2ycbcr_params.coeff[1][0] = 0x3d4;
	params.rgb2ycbcr_params.coeff[1][1] = 0x3ac;
	params.rgb2ycbcr_params.coeff[1][2] = 0x80;
	params.rgb2ycbcr_params.coeff[2][0] = 0x80;
	params.rgb2ycbcr_params.coeff[2][1] = 0x395;
	params.rgb2ycbcr_params.coeff[2][2] = 0x3eb;

	/* Set RGB 2 YCbCr color conversion offsets */
	params.rgb2ycbcr_params.offset[0] = 0;
	params.rgb2ycbcr_params.offset[1] = 0;
	params.rgb2ycbcr_params.offset[2] = 0;

	/* Set Black Adjustment offsets */
	params.black_adjst_params.blueblkadj = 0;
	params.black_adjst_params.redblkadj = 0;
	params.black_adjst_params.greenblkadj = 0;

	/* Set output pixel format */
	params.pix_fmt = PREV_PIXORDER_YCBYCR;

	/* set brightness and contrast */
	params.brightness = 0;
	params.contrast = 0x10;

	/* Enable CFA Interpolation */
	params.features = PREV_CFA;

	/* Set CFA coeffiecients */
	for (i = 0; i < CFA_COEFF_TABLE_SIZE; i++)
		params.cfa_coeffs.coeffs[i] = cfa_coef[i];

	/* Set CFA thresholds */
	params.cfa_coeffs.vthreshold = params.cfa_coeffs.hthreshold = 0x28;

	/* Open the Previewer Driver */
	prevfd = open("/dev/davinci_previewer", O_RDWR);

	/* If open fails, return error */
	if (prevfd < 0) {
		printf("\nError opening device");
		return -1;
	}

	/* Call PREV_SET_PARAM ioctl to to configuration */
	ret = ioctl(prevfd, PREV_SET_PARAM, &params);

	/* if it fails,return error */
	if (ret < 0) {
		printf("\n error in setting parameters");
		goto out;
	}

	/* request one input buffer of size height*width */
	reqbufs.buf_type = PREV_BUF_IN;
	reqbufs.size = WIDTH * HEIGHT;
	reqbufs.count = 1;

	/* Call PREV_REQBUF ioctl to allocate memory for input buffer */
	ret = ioctl(prevfd, PREV_REQBUF, &reqbufs);

	/* return error if it returns error */
	if (ret < 0) {
		printf("\ncannot get the requested buffer");
		goto out;
	}

	/* request one output buffer of size height*width*2 */
	reqbufs.buf_type = PREV_BUF_OUT;
	reqbufs.size = WIDTH * HEIGHT * 2;
	reqbufs.count = 1;

	/* Call PREV_REQBUF ioctl to allocate memory for output buffer */
	ret = ioctl(prevfd, PREV_REQBUF, &reqbufs);

	/* return error if it returns error */
	if (ret < 0) {
		printf("\ncannot get the requested buffer");
		goto out;
	}

	/* Get the physical address of the input buffer */

	/* fill parameters in buffer */
	t_buff.index = 0;
	t_buff.buf_type = PREV_BUF_IN;

	/* Call PREV_QUERYBUF ioctl to get physical address of input buffer */
	ret = ioctl(prevfd, PREV_QUERYBUF, &t_buff);
	/* return error if it returns error */
	if (ret < 0) {
		printf("\ncannot get in buffer address");
		goto out;
	}
	in_buff.index = 0;
	in_buff.offset = t_buff.offset;

	/* mmap input buffer in user space */
	buff[0].length = t_buff.size;
	buff[0].addr =
	    mmap(NULL, t_buff.size, PROT_READ | PROT_WRITE, MAP_SHARED,
		 prevfd, t_buff.offset);
	/* if mmaping fails return error */
	if (buff[0].addr == MAP_FAILED) {
		printf("\ncannot mmap buffer");
		goto out;
	}

	/* Get the physical address of the output buffer */

	/* fill parameters in buffer */
	t_buff.index = 0;
	t_buff.buf_type = PREV_BUF_OUT;
	/* Call PREV_QUERYBUF ioctl to get physical address of output buffer */
	ret = ioctl(prevfd, PREV_QUERYBUF, &t_buff);
	/* return error if it returns error */
	if (ret < 0) {
		printf("\ncannot get out buffer address");
		goto out;
	}
	out_buff.index = 0;
	out_buff.offset = t_buff.offset;

	/* mmap output buffer in user space */
	buff[1].length = t_buff.size;
	buff[1].addr =
	    mmap(NULL, t_buff.size, PROT_READ | PROT_WRITE, MAP_SHARED,
		 prevfd, t_buff.offset);

	/* if mmaping fails return error */
	if (buff[1].addr == MAP_FAILED) {
		printf("\ncannot mmap buffer");
		goto out;
	}

	/* store the buffer offset in convert structure */
	convert.in_buff.index = convert.out_buff.index = -1;
	convert.in_buff.offset = in_buff.offset;
	convert.out_buff.offset = out_buff.offset;

	/* Store size of the buffer in convert */
	convert.in_buff.size = WIDTH * HEIGHT;
	convert.out_buff.size = WIDTH * HEIGHT * 2;

	/* Copy input image in the input buffer */
	memcpy(buff[0].addr, bayerimage, WIDTH * HEIGHT);

	inadr = buff[0].addr;
	outadr = buff[1].addr;

	/* Call PREV_PREVIEW ioctl to do previewing for the First pass */
	ret = ioctl(prevfd, PREV_PREVIEW, &convert);

	/* return error if it returns error */
	if (ret < 0) {
		perror("\ncannot convert");
		goto out;
	}

	/* change offsets for the second pass */
	convert.in_buff.offset = in_buff.offset + 1280;	/*max 1280 minus 
							  croppping pixels */
	convert.out_buff.offset = out_buff.offset + (1280) * 2;

	convert.in_buff.size = WIDTH * HEIGHT;
	convert.out_buff.size = WIDTH * HEIGHT * 2;

	/* Call PREV_SET_PARAMS ioctl to set parameters */
	params.size_params.hsize = 1280;
	ret = ioctl(prevfd, PREV_SET_PARAM, &params);

	/* return error if it returns error */
	if (ret < 0) {
		printf("\n error in setting parameters");
		goto out;
	}

	/* Call PREV_PREVIEW ioctl to do previewing for second pass */
	ret = ioctl(prevfd, PREV_PREVIEW, &convert);

	/* return error if it returns error */
	if (ret < 0) {
		printf("\ncannot convert");
		goto out;
	}
	/* Open the output file */
	fp = fopen(OUTPUT_FILE_NAME, "w");
	if (!fp) {
		printf("\n cannot open the output file");
		goto out;
	}

	adr = (char *) buff[1].addr;
	addr = (int *) buff[1].addr;
	/* Separate Y,CB and CR component */
	for (i = 0; i < HEIGHT - 4; i++) {
		for (j = 0; j < WIDTH / 2 - 2; j++) {
			ay[cy++] = (*addr) & 0xff;
			ay[cy++] = ((*addr) & 0xff0000) >> 16;
			acb[ccb++] = (((*addr) & 0xff00) >> 8);
			acr[ccr++] = ((*addr) & 0xff000000) >> 24;

			addr++;
		}
		addr += 2;
	}
	/* Write Y, CB and Cr in the output file */
	fwrite(ay, 1, cy, fp);
	fwrite(acb, 1, ccb, fp);
	fwrite(acr, 1, ccr, fp);
	/* close the output file */
	fclose(fp);

      out:
	/* unmap input/output buffers */
	munmap(buff[0].addr, buff[0].length);
	munmap(buff[1].addr, buff[1].length);

	/* Close previewer driver */
	close(prevfd);

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -