?? testcamera1.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 320
#define FIXED_SOURCE_HEIGHT 240
/****************************************************/
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
#endif
static 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;
__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 YCbCr422
static void save_picture(unsigned char *src)
{
int y, x;
char name[64];
unsigned long rgb_data;
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)
for(y=0; y<h; 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[(y*w+x)*3] = (rgb_data&0xf800)>>8;
image_buffer[(y*w+x)*3+1] = (rgb_data&0x07e0)>>3;
image_buffer[(y*w+x)*3+2] = (rgb_data&0x001f)<<3;
x++;
image_buffer[(y*w+x)*3] = (rgb_data&0xf8000000)>>24;
image_buffer[(y*w+x)*3+1] = (rgb_data&0x07e00000)>>19;
image_buffer[(y*w+x)*3+2] = (rgb_data&0x001f0000)>>13;
}
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;
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) {
for(y=0; y<h; y++) {
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_buf += image_width<<i;
cb_buf += (image_width<<i)>>1;
cr_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;
}
#else
static void test_ycbcr_to_rgb(void *scr) {}
#endif
#if defined(USER_I2C_INIT) || defined(SCCB_INIT)
#define CAMERA_I2C_ADDR 0x30
#define CAMERA_MIDH 0x1c
#define CAMERA_MIDL 0x1d
#define CAMERA_PIDH 0x0a
#define CAMERA_PIDL 0x0b
#define CHIP_DELAY 0xFF
struct i2c_reg_t {
int subaddr;
int value;
};
static struct i2c_reg_t camera_regs[] =
{
{0x12, 0x80}, // Camera Soft reset. Self cleared after reset.
{CHIP_DELAY, 20},
//change 0x11[0x81->0x80], 0x15[0x02->0x20], 0x3a[0x01->0x0d]hzh
//{0x12,0x80},
{0x11,0x81},{0x6a,0x3e},{0x3b,0x09},{0x13,0xe0},{0x01,0x80},{0x02,0x80},{0x00,0x00},{0x10,0x00},
{0x13,0xe5},{0x39,0x43},{0x38,0x12},{0x37,0x00},{0x35,0x91},{0x0e,0xa0},{0x1e,0x04},{0xA8,0x80},
{0x12,0x40},{0x04,0x00},{0x0c,0x04},{0x0d,0x80},{0x18,0xc6},{0x17,0x26},{0x32,0xad},{0x03,0x00},
{0x1a,0x3d},{0x19,0x01},{0x3f,0xa6},{0x14,0x2e},{0x15,0x20},{0x41,0x02},{0x42,0x08},{0x1b,0x00},
{0x16,0x06},{0x33,0xe2},{0x34,0xbf},{0x96,0x04},{0x3a,0x00},{0x8e,0x00},{0x3c,0x77},{0x8B,0x06},
{0x94,0x88},{0x95,0x88},{0x40,0xc1},{0x29,0x3f},{0x0f,0x42},{0x3d,0x92},{0x69,0x40},{0x5C,0xb9},
{0x5D,0x96},{0x5E,0x10},{0x59,0xc0},{0x5A,0xaf},{0x5B,0x55},{0x43,0xf0},{0x44,0x10},{0x45,0x68},
{0x46,0x96},{0x47,0x60},{0x48,0x80},{0x5F,0xe0},{0x60,0x8c},{0x61,0x20},{0xa5,0xd9},{0xa4,0x74},
{0x8d,0x02},{0x13,0xe7},{0x4f,0x3a},{0x50,0x3d},{0x51,0x03},{0x52,0x12},{0x53,0x26},{0x54,0x38},
{0x55,0x40},{0x56,0x40},{0x57,0x40},{0x58,0x0d},{0x8C,0x23},{0x3E,0x02},{0xa9,0xb8},{0xaa,0x92},
{0xab,0x0a},{0x8f,0xdf},{0x90,0x00},{0x91,0x00},{0x9f,0x00},{0xa0,0x00},{0x3A,0x0d},{0x24,0x70},
{0x25,0x64},{0x26,0xc3},{0x2a,0x00},{0x2b,0x00},{0x6c,0x40},{0x6d,0x30},{0x6e,0x4b},{0x6f,0x60},
{0x70,0x70},{0x71,0x70},{0x72,0x70},{0x73,0x70},{0x74,0x60},{0x75,0x60},{0x76,0x50},{0x77,0x48},
{0x78,0x3a},{0x79,0x2e},{0x7a,0x28},{0x7b,0x22},{0x7c,0x04},{0x7d,0x07},{0x7e,0x10},{0x7f,0x28},
{0x80,0x36},{0x81,0x44},{0x82,0x52},{0x83,0x60},{0x84,0x6c},{0x85,0x78},{0x86,0x8c},{0x87,0x9e},
{0x88,0xbb},{0x89,0xd2},{0x8a,0xe6},
// {0x3a, 0x0d}, //
//{0x3a, 0x1d}, //for test
//{0x67, 'U'}, //fixed value for U
//{0x68, 'V'}, //fixed value for V
//{0x15, 0x12}, //PCLK reverse, VSYNC negative
//{0x12, 0x10}, //QVGA
//{0x04, 0x20}, //QQVGA
//{0x15, 0x20}, //no PCLK when HREF is low
};
#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);
while(1) {
long ds, dus;
gettimeofday(&tve, &tze);
dus = tve.tv_usec - tvs.tv_usec;
if(dus<0) {
dus += 1000000;
tve.tv_sec--;
}
ds = tve.tv_sec - tvs.tv_sec;
if((ds*1000000+dus)/1000>=ms)
break;
}
*/
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = ms*1000;
select(1, &rfds, NULL, NULL, &tv);
}
#endif
#ifdef USER_I2C_INIT
static int i2c_write(int fd, __u8 reg, __u8 val)
{
int retries;
__u8 data[2];
data[0] = reg;
data[1] = val;
for(retries=5; retries; retries--) {
if(write(fd, data, 2)==2)
return 0;
delay_ms(2);
}
//printf("write fail %x %x\n", data[0], data[1]);
return -1;
}
static int i2c_read(int fd, __u8 reg, __u8 *val)
{
int retries;
for(retries=5; retries; retries--) {
if(write(fd, ®, 1)==1)
if(read(fd, val, 1)==1)
return 0;
delay_ms(2);
}
//printf("read fail\n");
return -1;
}
#define I2C_SLAVE 0x0703 /* Change slave address */
static int camera_i2c_init(void)
{
int i, fd;
__u8 id[4];
//delay_ms(100);
printf("open i2c device...\n");
fd = open("/dev/i2c/0", O_RDWR);
if(fd<0) {
fd = open("/dev/misc/i2c", O_RDWR);
if(fd<0) {
printf("fail to open i2c adapter device!\n");
return -1;
}
}
if(ioctl(fd, I2C_SLAVE, CAMERA_I2C_ADDR)<0) {
printf("fail to set i2c device slave address!\n");
close(fd);
return -1;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -