1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
| #include "uvc_camera_capture.h" #include "time.h"
static int jpeg_to_rgb(const char *jpegData, char *rgbData, int size) { clock_t start_time, end_time; start_time = clock();
struct jpeg_error_mgr jerr; struct jpeg_decompress_struct cinfo; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_mem_src(&cinfo, jpegData, size); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo);
int row_stride = cinfo.output_width * cinfo.output_components; unsigned char *buffer = malloc(row_stride); int i = 0; while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, &buffer, 1); memcpy(rgbData + i * 640 * 3, buffer, row_stride); i++; }
jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); free(buffer);
end_time = clock(); DBG("mjpeg to rgb waste time %f s\n", (double)(end_time - start_time)/CLOCKS_PER_SEC); return 1; }
static void lcd_show_rgb(struct lcdFramebuffer *lfb, unsigned char *rgbData, int width ,int height) { clock_t start_time, end_time; start_time = clock();
unsigned int *row_start = lfb->base; unsigned int *ptr = NULL; unsigned char *srcData = NULL; unsigned char r, g, b; for(int i = 0; i < height; i++){ ptr = row_start; srcData = rgbData; for(int j = 0; j < width; j++){ *ptr++ = (0xFF << 24) | ((*srcData++) << 16) | ((*srcData++) << 8) | (*srcData++); } rgbData += width * 3; row_start += lfb->width; }
end_time = clock(); DBG("Write fb waste time %f s\n", (double)(end_time - start_time)/CLOCKS_PER_SEC); }
static void fcc2s(char* fmtString, unsigned int size, unsigned int pixelformat) { if ( size < 8 ) { fmtString[0] = '\0'; return; }
fmtString[0] = pixelformat & 0x7f; fmtString[1] = (pixelformat >> 8 ) & 0x7f; fmtString[2] = (pixelformat >> 16 ) & 0x7f; fmtString[3] = (pixelformat >> 24 ) & 0x7f; if (pixelformat & (1 << 31)) { fmtString[4] = '-'; fmtString[5] = 'B'; fmtString[6] = 'E'; fmtString[7] = '\0'; } else { fmtString[4] = '\0'; } return; }
static int init_v4l2(struct vdIn *vd) { int i; int ret = 0; int pixelFromatNum = 0; int formatFrameSizeNum = 0; vd->fd = open(vd->videodevice, O_RDWR); if(vd->fd == -1){ perror("ERROR opening V4L interface"); return -1; }
memset(&vd->cap, 0, sizeof(struct v4l2_capability)); ret = ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap); if(ret < 0) { fprintf(stderr, "Error opening device %s: unable to query deivce.\n", vd->videodevice); goto fatal; }
DBG("driver:\t\t%s\n", vd->cap.driver); DBG("card:\t\t%s\n", vd->cap.card); DBG("bus_info:\t%s\n", vd->cap.bus_info); DBG("version:\t%d\n", vd->cap.version); DBG("capabilities:\t%x\n", vd->cap.capabilities); if((vd->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) { fprintf(stderr, "Error opening device %s: video capture not supported.\n", vd->videodevice); goto fatal;; } DBG("%s supports capture.\n", vd->videodevice);
if(vd->cap.capabilities & V4L2_CAP_STREAMING == 0) { fprintf(stderr, "%s does not support streaming i/o\n", vd->videodevice); goto fatal; } DBG("%s supports streaming.\n", vd->videodevice);
memset(&vd->fmtdesc, 0, sizeof(struct v4l2_fmtdesc)); memset(&vd->streamparm, 0, sizeof(struct v4l2_streamparm)); memset(&vd->frmsizeenum, 0, sizeof(struct v4l2_frmsizeenum)); while(1) { vd->fmtdesc.index = pixelFromatNum++; vd->fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ret = ioctl(vd->fd, VIDIOC_ENUM_FMT, &vd->fmtdesc); if(ret != 0) break; vd->streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ret = ioctl(vd->fd, VIDIOC_G_PARM, &vd->streamparm); if(ret == 0) DBG("Default FPS: %d fps\n", vd->streamparm.parm.capture.timeperframe.denominator); while(1) { vd->frmsizeenum.index = formatFrameSizeNum++; vd->frmsizeenum.pixel_format = vd->fmtdesc.pixelformat; ret = ioctl(vd->fd, VIDIOC_ENUM_FRAMESIZES, &vd->frmsizeenum); if(ret == 0) DBG("Support Format:%s, %d, FrameSize %d: %d x %d\n", vd->fmtdesc.description, vd->fmtdesc.pixelformat, formatFrameSizeNum, vd->frmsizeenum.discrete.width, vd->frmsizeenum.discrete.height); else break; } formatFrameSizeNum = 0; }
memset(&vd->fmt, 0, sizeof(struct v4l2_format)); vd->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; vd->fmt.fmt.pix.width = vd->width; vd->fmt.fmt.pix.height = vd->height; vd->fmt.fmt.pix.pixelformat = vd->formatIn; vd->fmt.fmt.pix.field = V4L2_FIELD_ANY; ret = ioctl(vd->fd, VIDIOC_S_FMT, &vd->fmt); if(ret < 0) { fprintf(stderr, "Unable to set format: %d res: %dx%d\n", vd->formatIn, vd->width, vd->height); goto fatal; }
if((vd->fmt.fmt.pix.width != vd->width) || (vd->fmt.fmt.pix.height != vd->height)){ vd->width = vd->fmt.fmt.pix.width; vd->height = vd->fmt.fmt.pix.height; }
if(vd->formatIn != vd->fmt.fmt.pix.pixelformat){ char fmtStringRequested[8]; char fmtStringObtained[8]; fcc2s(fmtStringObtained, 8, vd->fmt.fmt.pix.pixelformat); fcc2s(fmtStringRequested, 8, vd->formatIn); fprintf(stderr, " i: Could not obtain the requested pixelformat: %s , driver gave us: %s\n",fmtStringRequested, fmtStringObtained); fprintf(stderr, " i: The specified resolution is unavailable, using: width %d height %d instead \n", vd->fmt.fmt.pix.width, vd->fmt.fmt.pix.height); switch(vd->fmt.fmt.pix.pixelformat){ case V4L2_PIX_FMT_JPEG: case V4L2_PIX_FMT_MJPEG: fprintf(stderr, " ... Falling back to the faster MJPG mode (consider changing cmd line options).\n"); vd->formatIn = vd->fmt.fmt.pix.pixelformat; break; case V4L2_PIX_FMT_YUYV: fprintf(stderr, " ... Falling back to YUV mode (consider using -yuv option). Note that this requires much more CPU power\n"); vd->formatIn = vd->fmt.fmt.pix.pixelformat; break; case V4L2_PIX_FMT_UYVY: fprintf(stderr, " ... Falling back to UYVY mode (consider using -uyvy option). Note that this requires much more CPU power\n"); vd->formatIn = vd->fmt.fmt.pix.pixelformat; break; case V4L2_PIX_FMT_RGB24: fprintf(stderr, " ... Falling back to RGB24 mode (consider using -fourcc RGB24 option). Note that this requires much more CPU power\n"); vd->formatIn = vd->fmt.fmt.pix.pixelformat; break; case V4L2_PIX_FMT_RGB565: fprintf(stderr, " ... Falling back to RGB565 mode (consider using -fourcc RGBP option). Note that this requires much more CPU power\n"); vd->formatIn = vd->fmt.fmt.pix.pixelformat; break; default: goto fatal; break; } }
char fmtStringRequested[8]; fcc2s(fmtStringRequested, 8, vd->formatIn); DBG("Set Format: %s, FrameSize: %d x %d\n", fmtStringRequested, vd->width, vd->height);
memset(&vd->rb, 0, sizeof(struct v4l2_requestbuffers)); vd->rb.count = NB_BUFFER; vd->rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; vd->rb.memory = V4L2_MEMORY_MMAP; ret = ioctl(vd->fd, VIDIOC_REQBUFS, &vd->rb); if(ret < 0){ perror("Unable to allocate buffers"); goto fatal; }
for(i = 0; i < NB_BUFFER; i++){ memset(&vd->buf, 0, sizeof(struct v4l2_buffer)); vd->buf.index = i; vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; vd->buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf); if(ret < 0) { perror("Unable to query buffer"); goto fatal; }
vd->mem[i] = mmap(0, vd->buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, vd->fd, vd->buf.m.offset); if(vd->mem[i] == MAP_FAILED){ perror("Unable to map buffer"); goto fatal; } }
for(i = 0; i < NB_BUFFER; i++){ memset(&vd->buf, 0, sizeof(struct v4l2_buffer)); vd->buf.index = i; vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; vd->buf.memory = V4L2_MEMORY_MMAP; ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf); if(ret < 0) { perror("Unable to queue buffer"); goto fatal; } }
return 0;
fatal: fprintf(stderr, "Init v4L2 failed !! exit fatal\n"); return -1; }
static int init_videoIn(struct vdIn *vd, char *device, int width, int height, int fps, int format) { if(vd == NULL || device == NULL) return -1; if(width == 0 || height == 0) return -1;
vd->videodevice = (char *)calloc(1, 16 * sizeof(char)); if(snprintf(vd->videodevice, (16 - 1), "%s", device) < 0) { free(vd->videodevice); vd->videodevice = NULL; return -1; } vd->width = width; vd->height = height; vd->fps = fps; vd->formatIn = format; if(init_v4l2(vd) < 0) { free(vd->videodevice); vd->videodevice = NULL; close(vd->fd); return -1; }
return 0; }
static int video_enable(struct vdIn *vd) { int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; int ret;
ret = ioctl(vd->fd, VIDIOC_STREAMON, &type); if(ret < 0) { perror("Unable to start capture"); return ret; } DBG("Starting capture\n"); return 0; }
static int video_disable(struct vdIn *vd) { int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; int ret;
ret = ioctl(vd->fd, VIDIOC_STREAMOFF, &type); if(ret != 0) { perror("Unable to stop capture"); return ret; } DBG("Stopping capture\n"); return 0; }
static int video_handle(struct vdIn *vd, struct lcdFramebuffer *lfb) { int ret = 0; struct pollfd fds[1]; char filename[128]; int file_cnt = 0;
unsigned char rgbData[640 * 480 * 3];
clock_t start_time, end_time; while(1) { start_time = clock(); memset(fds, 0, sizeof(fds)); fds[0].fd = vd->fd; fds[0].events = POLLIN; if(poll(fds, 1, -1) > 0) { memset(&vd->buf, 0, sizeof(struct v4l2_buffer)); vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; vd->buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf); if(ret < 0) { perror("Unable to dequeue buffer"); return -1; }
jpeg_to_rgb(vd->mem[vd->buf.index], rgbData, vd->buf.length); lcd_show_rgb(lfb, rgbData, 640, 480);
ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf); if(ret < 0) { perror("Unable to queue buffer"); return -1; } } end_time = clock(); DBG("Each frame waste time: %f s\n", (double)(end_time - start_time)/CLOCKS_PER_SEC); } return 0; }
static int init_framebuffer(struct lcdFramebuffer *lfb, int width, int height) { if(lfb == NULL) return -1;
lfb->fd = open("/dev/fb0", O_RDWR); if(lfb->fd < 0) { perror("/dev/fb0"); return -1; } if (ioctl(lfb->fd, FBIOGET_VSCREENINFO, &lfb->var)) { perror("Unable to get fb_var_screeninfo"); return -1; }
lfb->base = (unsigned int*)mmap(NULL, width * height * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lfb->fd, 0); if(lfb->base == MAP_FAILED) { perror("Unable to map buffer"); return -1; } }
int main(int argc, char **argv) { int i; struct vdIn *vd; struct lcdFramebuffer *lfb; lfb = (struct lcdFramebuffer *)malloc(sizeof(struct lcdFramebuffer)); vd = (struct vdIn *)malloc(sizeof(struct vdIn));
init_videoIn(vd, "/dev/video1", 640, 480, 30, V4L2_PIX_FMT_MJPEG);
video_enable(vd); lfb->width = 1024; lfb->height = 600; init_framebuffer(lfb, 1024, 600);
video_handle(vd, lfb);
for (i = 0; i < NB_BUFFER; i++) { munmap(vd->mem[i], vd->buf.length); } free(vd->videodevice); vd->videodevice = NULL; video_disable(vd); close(vd->fd); free(vd); munmap(lfb->base, lfb->width * lfb->height); free(lfb); return 0; }
|