📄 saa7113h.c
字号:
/* created by antiscle <hzh12@tom.com> 2005/07/04*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <errno.h>#include <unistd.h>#include <termios.h>#include <linux/fb.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <jpeglib.h>#define __user#include "videodev.h"#include "pxa_camera.h"/****************************************************/#define S3C2440A//#define PXA27X//#define SCCB_INIT#define USER_I2C_INIT //use i2c initialize//#define YCbCr_TO_RGB_TEST#define FIXED_SOURCE_WIDTH 720#define FIXED_SOURCE_HEIGHT 625/****************************************************/static struct { int palette; char *name;} optional_image_format[] = { { VIDEO_PALETTE_YUV422P, "YCbCr422 planar", }, { VIDEO_PALETTE_RGB565, "RGB565", },/* { VIDEO_PALETTE_RGB24, "RGB24", },*/};#define MAX_IMAGE_FORMAT (sizeof(optional_image_format)/sizeof(optional_image_format[0]))#ifndef GLOBAL#define GLOBAL //defined in jmorecfg.h#endifstatic unsigned short image_width;static unsigned short image_height;static unsigned char *image_buffer;static unsigned short optimization = 75;static unsigned short image_format;static char *image_file = "jpgfile";static unsigned short file_index;static unsigned short mmap_camera;static unsigned short capture_video;static unsigned short cpu_type;static void delay_ms(long ms);__u32 Conv_YCbCr_Rgb(__u8 y0, __u8 y1, __u8 cb0, __u8 cr0);/****************************************************/GLOBAL(void)write_JPEG_file (char * filename, int quality){ struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; /* More stuff */ FILE * outfile; /* target file */ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ int row_stride; /* physical row width in image buffer */ cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG compression object. */ jpeg_create_compress(&cinfo); if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "can't open %s\n", filename); exit(1); } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = image_width; /* image width and height, in pixels */ cinfo.image_height = image_height; cinfo.input_components = 3; /* # of color components per pixel */ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); } /* Step 6: Finish compression */ jpeg_finish_compress(&cinfo); /* After finish_compress, we can close the output file. */ fclose(outfile); jpeg_destroy_compress(&cinfo); /* And we're done! */}// support RGB16 or YCbCr422static void save_picture(unsigned char *src){ int y, x; char name[64]; unsigned long rgb_data, rgb_data1; unsigned short w = image_width; unsigned short h = image_height; unsigned char *src_y = src; unsigned char *src_cb = src_y+image_width*image_height; unsigned char *src_cr = src_cb+image_width*image_height/2; int palette = optional_image_format[image_format].palette; image_buffer = malloc(w*h*3); //RGB24 if(image_buffer==NULL) { printf("allocate memory fail in saving picture!\n"); return; } if(palette==VIDEO_PALETTE_RGB565) for(y=0; y<h; y++) for(x=0; x<w; x++) { image_buffer[(y*w+x)*3] = (((__u16 *)src)[y*w+x]&0xf800)>>8; image_buffer[(y*w+x)*3+1] = (((__u16 *)src)[y*w+x]&0x07e0)>>3; image_buffer[(y*w+x)*3+2] = (((__u16 *)src)[y*w+x]&0x001f)<<3; } else if(palette==VIDEO_PALETTE_YUV422P) { short idx[1024]; for(y=0; y<313; y++) idx[y] = 2*y; for(; y<625; y++) idx[y] = (y-313)*2+1; //idx[624] = 624; delay_ms(200);#if 0 for(y=0; y<312; y++) for(x=0; x<w; x++) { rgb_data = Conv_YCbCr_Rgb(src_y[y*w+x], src_y[y*w+x+1], src_cb[(y*w+x)>>1], src_cr[(y*w+x)>>1]); rgb_data1 = Conv_YCbCr_Rgb(src_y[(y+313)*w+x], src_y[(y+313)*w+x+1], src_cb[((y+313)*w+x)>>1], src_cr[((y+313)*w+x)>>1]); image_buffer[(2*y*w+x)*3] = (rgb_data&0xf800)>>8; image_buffer[(2*y*w+x)*3+1] = (rgb_data&0x07e0)>>3; image_buffer[(2*y*w+x)*3+2] = (rgb_data&0x001f)<<3; image_buffer[((2*y+1)*w+x)*3] = (rgb_data1&0xf800)>>8; image_buffer[((2*y+1)*w+x)*3+1] = (rgb_data1&0x07e0)>>3; image_buffer[((2*y+1)*w+x)*3+2] = (rgb_data1&0x001f)<<3; x++; image_buffer[(2*y*w+x)*3] = (rgb_data&0xf8000000)>>24; image_buffer[(2*y*w+x)*3+1] = (rgb_data&0x07e00000)>>19; image_buffer[(2*y*w+x)*3+2] = (rgb_data&0x001f0000)>>13; image_buffer[((2*y+1)*w+x)*3] = (rgb_data1&0xf8000000)>>24; image_buffer[((2*y+1)*w+x)*3+1] = (rgb_data1&0x07e00000)>>19; image_buffer[((2*y+1)*w+x)*3+2] = (rgb_data1&0x001f0000)>>13; }#else for(y=0; y<625; y++) for(x=0; x<w; x++) { rgb_data = Conv_YCbCr_Rgb(src_y[y*w+x], src_y[y*w+x+1], src_cb[(y*w+x)>>1], src_cr[(y*w+x)>>1]); image_buffer[(idx[y]*w+x)*3] = (rgb_data&0xf800)>>8; image_buffer[(idx[y]*w+x)*3+1] = (rgb_data&0x07e0)>>3; image_buffer[(idx[y]*w+x)*3+2] = (rgb_data&0x001f)<<3; x++; image_buffer[(idx[y]*w+x)*3] = (rgb_data&0xf8000000)>>24; image_buffer[(idx[y]*w+x)*3+1] = (rgb_data&0x07e00000)>>19; image_buffer[(idx[y]*w+x)*3+2] = (rgb_data&0x001f0000)>>13; }#endif } else { printf("unsupported input image format for saving jpeg file\n"); free(image_buffer); return; } memset(name, 0, sizeof(name)); sprintf(name, "%s%04d.jpg", image_file, file_index++); write_JPEG_file(name, optimization); free(image_buffer);}/****************************************************/struct reg_set_s { int val1; int val2;};static int fb_xres;static int fb_yres;static int fb_bpp;#define YCbCrtoR(Y,Cb,Cr) (1000*Y + 1371*(Cr-128))/1000#define YCbCrtoG(Y,Cb,Cr) (1000*Y - 336*(Cb-128) - 698*(Cr-128))/1000#define YCbCrtoB(Y,Cb,Cr) (1000*Y + 1732*(Cb-128))/1000#define min(x1, x2) (((x1)<(x2))?(x1):(x2))__u32 Conv_YCbCr_Rgb(__u8 y0, __u8 y1, __u8 cb0, __u8 cr0){ // bit order is // YCbCr = [Cr0 Y1 Cb0 Y0], RGB=[R1,G1,B1,R0,G0,B0]. int r0, g0, b0, r1, g1, b1; __u16 rgb0, rgb1; __u32 rgb; #if 1 // 4 frames/s @192MHz, 12MHz ; 6 frames/s @450MHz, 12MHz r0 = YCbCrtoR(y0, cb0, cr0); g0 = YCbCrtoG(y0, cb0, cr0); b0 = YCbCrtoB(y0, cb0, cr0); r1 = YCbCrtoR(y1, cb0, cr0); g1 = YCbCrtoG(y1, cb0, cr0); b1 = YCbCrtoB(y1, cb0, cr0); #endif if (r0>255 ) r0 = 255; if (r0<0) r0 = 0; if (g0>255 ) g0 = 255; if (g0<0) g0 = 0; if (b0>255 ) b0 = 255; if (b0<0) b0 = 0; if (r1>255 ) r1 = 255; if (r1<0) r1 = 0; if (g1>255 ) g1 = 255; if (g1<0) g1 = 0; if (b1>255 ) b1 = 255; if (b1<0) b1 = 0; // 5:6:5 16bit format rgb0 = (((__u16)r0>>3)<<11) | (((__u16)g0>>2)<<5) | (((__u16)b0>>3)<<0); //RGB565. rgb1 = (((__u16)r1>>3)<<11) | (((__u16)g1>>2)<<5) | (((__u16)b1>>3)<<0); //RGB565. rgb = (rgb1<<16) | rgb0; return(rgb);}static char yuv_interval[] = {0, 2, 4, 8, 16};static void show_cam_img(void *scr, __u8 *y_buf, __u8 *cb_buf, __u8 *cr_buf){ __u16 x, y, w, h, i, f; __u16 *fb_buf = (__u16 *)scr; __u32 rgb_data; int palette = optional_image_format[image_format].palette; __u8 *y1_buf; __u8 *cb1_buf; __u8 *cr1_buf; for(i=0; i<4; i++) { //0,1,2,3 if((image_width>>i)<=fb_xres) { f = 0; w = min(image_width>>i, fb_xres); h = min(image_height>>i, fb_yres); break; } if((image_height>>i)<=fb_yres) { f = 1; w = min(image_width>>i, fb_yres); h = min(image_height>>i, fb_xres); break; } } if(i>=4) return; //printf("%d,%d %d,%d\n", i, f, w, h); if(palette==VIDEO_PALETTE_RGB565) { if(!f) { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { fb_buf[x] = ((__u16 *)y_buf)[x<<i]; fb_buf[x+1] = ((__u16 *)y_buf)[(x+1)<<i]; } fb_buf += fb_xres; (__u16 *)y_buf += image_width<<i; } } else { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { fb_buf[(fb_yres-x-1)*fb_xres+y] = ((__u16 *)y_buf)[x<<i]; fb_buf[(fb_yres-x-2)*fb_xres+y] = ((__u16 *)y_buf)[(x+1)<<i]; } (__u16 *)y_buf += image_width<<i; } } } else if(palette==VIDEO_PALETTE_YUV422P) { if(!f) { y1_buf = y_buf + image_width*313; cb1_buf = cb_buf + (image_width*313)/2; cr1_buf = cr_buf + (image_width*313)/2; for(y=0; y<h; ) { for(x=0; x<w; x+=2) { //calculate 2 times if(i) { fb_buf[x] = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[x+1] = Conv_YCbCr_Rgb(y_buf[(x<<i)+yuv_interval[i]], y_buf[(x<<i)+1+yuv_interval[i]], cb_buf[((x<<i)+yuv_interval[i])>>1], cr_buf[((x<<i)+yuv_interval[i])>>1]); } else { rgb_data = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[x] = rgb_data; fb_buf[x+1] = rgb_data>>16; } } fb_buf += fb_xres; y++; for(x=0; x<w; x+=2) { //calculate 2 times if(i) { fb_buf[x] = Conv_YCbCr_Rgb(y1_buf[x<<i], y1_buf[(x<<i)+1], cb1_buf[(x<<i)>>1], cr1_buf[(x<<i)>>1]); fb_buf[x+1] = Conv_YCbCr_Rgb(y1_buf[(x<<i)+yuv_interval[i]], y1_buf[(x<<i)+1+yuv_interval[i]], cb1_buf[((x<<i)+yuv_interval[i])>>1], cr1_buf[((x<<i)+yuv_interval[i])>>1]); } else { rgb_data = Conv_YCbCr_Rgb(y1_buf[x<<i], y1_buf[(x<<i)+1], cb1_buf[(x<<i)>>1], cr1_buf[(x<<i)>>1]); fb_buf[x] = rgb_data; fb_buf[x+1] = rgb_data>>16; } } fb_buf += fb_xres; y++; y_buf += image_width<<i; cb_buf += (image_width<<i)>>1; cr_buf += (image_width<<i)>>1; y1_buf += image_width<<i; cb1_buf += (image_width<<i)>>1; cr1_buf += (image_width<<i)>>1; } } else { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { if(i) { fb_buf[(fb_yres-x-1)*fb_xres+y] = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[(fb_yres-x-2)*fb_xres+y] = Conv_YCbCr_Rgb(y_buf[(x<<i)+yuv_interval[i]], y_buf[(x<<i)+1+yuv_interval[i]], cb_buf[((x<<i)+yuv_interval[i])>>1], cr_buf[((x<<i)+yuv_interval[i])>>1]); } else { rgb_data = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[(fb_yres-x-1)*fb_xres+y] = rgb_data; fb_buf[(fb_yres-x-2)*fb_xres+y] = fb_buf[x+1] = rgb_data>>16; } } y_buf += image_width<<i; cb_buf += (image_width<<i)>>1; cr_buf += (image_width<<i)>>1; } } } }#ifdef YCbCr_TO_RGB_TEST#include "422jpeg.h"static void test_ycbcr_to_rgb(void *scr){ __u8 *buffer_y, *buffer_cb, *buffer_cr; buffer_y = (__u8 *)c422jpeg; buffer_cb = buffer_y + 240 * 320; buffer_cr = buffer_cb + 240 * 320 / 2; image_width = 240; image_height = 320; printf("Test YCbCr422 to RGB...\n"); show_cam_img(scr, buffer_y, buffer_cb, buffer_cr); printf("Press enter key to continue...\n"); getchar(); image_width = 0; image_height = 0;}#elsestatic void test_ycbcr_to_rgb(void *scr) {}#endif#if defined(USER_I2C_INIT) || defined(SCCB_INIT)#define CAMERA_I2C_ADDR 0x24#define CAMERA_MIDH 0x1c#define CAMERA_MIDL 0x1d#define CAMERA_PIDH 0x0a#define CAMERA_PIDL 0x0b#define CHIP_DELAY 0xFFstruct i2c_reg_t { int subaddr; int value;};static struct i2c_reg_t camera_regs[] = {{0x00, 0x00}, //ADR 0x00h PH7113_CHIP_VERSION 00 - ID byte{0x01, 0x08}, //ADR 0x01h PH7113_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0{0x02, 0xc0}, //ADR 0x02h PH7113_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0{0x03, 0x23}, //ADR 0x03h PH7113_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18{0x04, 0x00}, //ADR 0x04h PH7113_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10{0x05, 0x00}, //ADR 0x05h PH7113_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20{0x06, 0xe9}, //ADR 0x06h PH7113_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0{0x07, 0x0d}, //ADR 0x07h PH7113_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0{0x08, 0x98}, //ADR 0x08h PH7113_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0{0x09, 0x01}, //ADR 0x09h PH7113_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0{0x0a, 0x80}, //ADR 0x0ah PH7113_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0{0x0b, 0x47}, //ADR 0x0bh PH7113_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0{0x0c, 0x40}, //ADR 0x0ch PH7113_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0{0x0d, 0x00}, //ADR 0x0dh PH7113_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0{0x0e, 0x05}, //ADR 0x0eh PH7113_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0{0x0f, 0x24}, //ADR 0x0fh PH7113_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0{0x10, 0x00}, //ADR 0x10h PH7113_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0{0x11, 0x1d},//0x1c//ADR 0x11h PH7113_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO{0x12, 0xa7},//0xc7 , //ADR 0x12h PH7113_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 //RTS0 is VLINE,RTS1 is VFRAME{0x13, 0x81},//0x01//ADR 0x13h PH7113_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0{0x14, 0x00}, //ADR 0x14h RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1){0x15, 0x38}, //ADR 0x15h PH7113_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0{0x16, 0x00}, //ADR 0x16h PH7113_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0{0x17, 0x01}, //ADR 0x17h PH7113_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8{0x18, 0x00}, //ADR 0x18h{0x19, 0x00}, //ADR 0x19h{0x1a, 0x00}, //ADR 0x1ah{0x1b, 0x00}, //ADR 0x1bh{0x1c, 0x00}, //ADR 0x1ch{0x1d, 0x00}, //ADR 0x1dh{0x1e, 0x00}, //ADR 0x1eh{0x1f, 0x00}, //ADR 0x1fh{0x20, 0x00}, //ADR 0x20h{0x21, 0x00}, //ADR 0x21h{0x22, 0x00}, //ADR 0x22h{0x23, 0x00}, //ADR 0x23h{0x24, 0x00}, //ADR 0x24h{0x25, 0x00}, //ADR 0x25h{0x26, 0x00}, //ADR 0x26h{0x27, 0x00}, //ADR 0x27h{0x28, 0x00}, //ADR 0x28h{0x29, 0x00}, //ADR 0x29h{0x2a, 0x00}, //ADR 0x2ah{0x2b, 0x00}, //ADR 0x2bh{0x2c, 0x00}, //ADR 0x2ch{0x2d, 0x00}, //ADR 0x2dh{0x2e, 0x00}, //ADR 0x2eh{0x2f, 0x00}, //ADR 0x2fh{0x30, 0x00}, //ADR 0x30h{0x31, 0x00}, //ADR 0x31h{0x32, 0x00}, //ADR 0x32h{0x33, 0x00}, //ADR 0x33h{0x34, 0x00}, //ADR 0x34h{0x35, 0x00}, //ADR 0x35h{0x36, 0x00}, //ADR 0x36h{0x37, 0x00}, //ADR 0x37h{0x38, 0x00}, //ADR 0x38h{0x39, 0x00}, //ADR 0x39h{0x3a, 0x00}, //ADR 0x3ah{0x3b, 0x00}, //ADR 0x3bh{0x3c, 0x00}, //ADR 0x3ch{0x3d, 0x00}, //ADR 0x3dh{0x3e, 0x00}, //ADR 0x3eh{0x3f, 0x00}, //ADR 0x3fh{0x40, 0x02}, //ADR 0x40h{0x41, 0xff}, //ADR 0x41h{0x42, 0xff}, //ADR 0x42h{0x43, 0xff}, //ADR 0x43h{0x44, 0xff}, //ADR 0x44h{0x45, 0xff}, //ADR 0x45h{0x46, 0xff}, //ADR 0x46h{0x47, 0xff}, //ADR 0x47h{0x48, 0xff}, //ADR 0x48h{0x49, 0xff}, //ADR 0x49h{0x4a, 0xff}, //ADR 0x4ah{0x4b, 0xff}, //ADR 0x4bh{0x4c, 0xff}, //ADR 0x4ch{0x4d, 0xff}, //ADR 0x4dh{0x4e, 0xff}, //ADR 0x4eh{0x4f, 0xff}, //ADR 0x4fh{0x50, 0xff}, //ADR 0x50h{0x51, 0xff}, //ADR 0x51h{0x52, 0xff}, //ADR 0x52h{0x53, 0xff}, //ADR 0x53h{0x54, 0xff}, //ADR 0x54h{0x55, 0xff}, //ADR 0x55h{0x56, 0xff}, //ADR 0x56h{0x57, 0xff}, //ADR 0x57h{0x58, 0x00}, //ADR 0x58h{0x59, 0x54}, //ADR 0x59h{0x5a, 0x07}, //ADR 0x5ah{0x5b, 0x83}, //ADR 0x5bh{0x5c, 0x00}, //ADR 0x5ch{0x5d, 0x00}, //ADR 0x5dh{0x5e, 0x00}, //ADR 0x5eh{0x5f, 0x00}, //ADR 0x5fh{0x60, 0x00}, //ADR 0x60h{0x61, 0x00}, //ADR 0x61h{0x62, 0x00} //ADR 0x62h};#define CAMERA_REGS (sizeof(camera_regs)/sizeof(camera_regs[0]))#include <sys/time.h>static void delay_ms(long ms){/* struct timeval tvs, tve; struct timezone tzs, tze; if(!ms) return; gettimeofday(&tvs, &tzs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -