📄 yuv_4p.c
字号:
/* Copyright (C) 2004 Samsung Electronics <SW.LEE, hitchcar@sec.samsung.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <ctype.h>#include <errno.h>#include <sys/mman.h>#include <sys/time.h>#include <sys/ioctl.h>/* maximum */#define CAM_WIDTH 640#define CAM_HEIGHT 480static int cam_fp = -1;#include "../MiscDriver/userapp.h"camif_param_t camif_cfg;static unsigned char *g_yuv;#define CODEC_NAME "/dev/misc/codec"void fmalloc(int size){ unsigned int i=0; unsigned int *ptr32; g_yuv = (unsigned char*)malloc(size); if(!g_yuv) { printf("Memory Allcation Failed \n"); exit(1); } ptr32 = (unsigned int *)g_yuv; for (;i<size/4; i++) *ptr32++ = 0xff00ff00; /* Red */}static inline int cam_init(void){ int dev_fp = -1; dev_fp = open(CODEC_NAME, O_RDWR); if (dev_fp < 0) { perror(CODEC_NAME); printf("Open Failed \n"); return -1; } return dev_fp;}static inline void save_yuv(int width, int height, int bpp, int iter){ FILE *yuv_fp = NULL; char file_name[100]; /* read from device */ if (read(cam_fp, g_yuv, width*height*bpp/8) < 0) { perror("read()"); } if (bpp == 16 ) { sprintf(&file_name[0], "422X%d.yuv", iter); printf("422X%d.yuv", iter); } else { sprintf(&file_name[0], "420X%d.yuv", iter); printf("420X%d.yuv\n", iter); } fflush(stdout); /* file create/open, note to "wb" */ yuv_fp = fopen(&file_name[0], "wb"); if (!yuv_fp) { perror(&file_name[0]); } fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp); fclose(yuv_fp);}static inline void print_fps(struct timeval *s, struct timeval *e){ unsigned long time; unsigned long sec; unsigned long usec; int fps = 0; sec = e->tv_sec - s->tv_sec; if (e->tv_usec > s->tv_usec) usec = e->tv_usec - s->tv_usec; else { usec = e->tv_usec + 1000000 - s->tv_usec; sec--; } time = sec * 1000 + (usec+1) / 1000; fps = 1000 / (time / 30); printf("%d fps\n", fps);} int main(int argc, char *argv[]){ char cmdLine[100]; struct timeval start_tv, end_tv; struct timezone tz; int cam_width = CAM_WIDTH; int cam_height = CAM_HEIGHT; int i, goal, bpp,frames =0; int found = 0; if (argc != 6) { printf("%s <width> <height> <bpp> <num> <flip>\n", argv[0]); printf(" width: < 240 \n"); printf(" height: < 320 \n"); printf(" bpp: 420, 422 \n"); printf(" num:\n"); printf(" 0 is non-stop capturing\n" " X is the capture count\n"); printf(" flip: \n" " 0 is normal \n" " 1 is x-axis mirrorX \n" " 2 is y-axis mirrorX \n" " 3 is 180 rotation \n"); printf("EX) \n" " T_SHELL> %s 240 320 420 0 0 \n", argv[0]); goto err; } camif_cfg.dst_x = cam_width = atoi(argv[1]); camif_cfg.dst_y = cam_height = atoi(argv[2]); bpp = atoi(argv[3]); switch (bpp) { case 422: camif_cfg.bpp = 422; bpp = 16; break; case 420: camif_cfg.bpp = 420; bpp = 12; break; default: printf("Wrong Bpp format 422:420 \n"); return; } fmalloc(cam_width*cam_height*bpp/8); goal = atoi(argv[4]); camif_cfg.flip = atoi(argv[5]); if ((cam_fp = cam_init()) < 0) goto err; if (ioctl(cam_fp, CMD_CAMERA_INIT, &camif_cfg)) { perror("ioctl"); goto err; } gettimeofday(&start_tv, &tz); /* Start Camera Codec */ write(cam_fp,"O",2); while (!found) { frames++; save_yuv(cam_width, cam_height, bpp, frames); if( frames == goal ) { found = 1; break;} if ((frames % 30) == 0) { gettimeofday(&end_tv, &tz); print_fps(&start_tv, &end_tv); gettimeofday(&start_tv, &tz); } }err: /* Stop Camera Codec */ write(cam_fp,"X",2); if (cam_fp) close(cam_fp); free(g_yuv); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -