int convertframe(unsigned char *dst,unsigned char *src, int width,int height, int formatIn, int qualite) { int i; unsigned char tmp; int jpegsize =0; switch (formatIn){ case VIDEO_PALETTE_JPEG: jpegsize = get_jpegsize(src, width*height); if(jpegsize > 0) memcpy(dst,src,jpegsize); break; case VIDEO_PALETTE_YUV420P: jpegsize = encode_image(src,dst,qualite,YUVto420,width,height); break; case VIDEO_PALETTE_RGB24: jpegsize = encode_image(src,dst,qualite,RGBto420,width,height); break; case VIDEO_PALETTE_RGB565: jpegsize = encode_image(src,dst,qualite,RGB565to420,width,height); break; case VIDEO_PALETTE_RGB32: jpegsize = encode_image(src,dst,qualite,RGB32to420,width,height); break; default: break; } return jpegsize; }
/* Get a frame from the camera. Serial protocol char: I */ void get_frame () { unsigned char ch; move_image((unsigned char *)DMA_BUF1, (unsigned char *)DMA_BUF2, (unsigned char *)FRAME_BUF, imgWidth, imgHeight); if (overlay_flag) { frame[9] = (framecount % 10) + 0x30; frame[8] = ((framecount/10)% 10) + 0x30; frame[7] = ((framecount/100)% 10) + 0x30; set_caption(frame, imgWidth); } output_start = (unsigned char *)JPEG_BUF; output_end = encode_image((unsigned char *)FRAME_BUF, output_start, quality, FOUR_TWO_TWO, imgWidth, imgHeight); image_size = (unsigned int)(output_end - output_start); led1_on(); framecount++; uart0SendString(imgHead); uart0SendChar((unsigned char)(image_size & 0x000000FF)); uart0SendChar((unsigned char)((image_size & 0x0000FF00) >> 8)); uart0SendChar((unsigned char)((image_size & 0x00FF0000) >> 16)); uart0SendChar(0x00); cp = (unsigned char *)JPEG_BUF; for (i=0; i<image_size; i++) { while (*pPORTHIO & 0x0001) { continue; } uart0SendChar(*cp++); } while (uart0GetChar(&ch)) { // flush input continue; } }
int compress_frame(void *data, void *jpeg_data, struct lavcData *lavc_data, VidBuff *proc_buff) { struct JPEG_ENCODER_STRUCTURE **jpeg_struct = (struct JPEG_ENCODER_STRUCTURE **) jpeg_data; BYTE *prgb =NULL; struct ALL_DATA *all_data = (struct ALL_DATA *) data; struct GLOBAL *global = all_data->global; struct VideoFormatData *videoF = all_data->videoF; long framesize = 0; int jpeg_size = 0; int ret = 0; videoF->vpts = proc_buff->time_stamp; switch (global->VidCodec) { case CODEC_MJPEG: /*MJPG*/ /* save MJPG frame */ if((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_MJPEG)) { ret = write_video_data (all_data, proc_buff->frame, proc_buff->bytes_used); } else { /* use built in encoder */ if (!global->jpeg) { global->jpeg = g_new0(BYTE, ((global->width)*(global->height))>>1); } if(!(*jpeg_struct)) { *jpeg_struct = g_new0(struct JPEG_ENCODER_STRUCTURE, 1); /* Initialization of JPEG control structure */ initialization (*jpeg_struct,global->width,global->height); /* Initialization of Quantization Tables */ initialize_quantization_tables (*jpeg_struct); } jpeg_size = encode_image(proc_buff->frame, global->jpeg, *jpeg_struct, 0, global->width, global->height); ret = write_video_data (all_data, global->jpeg, jpeg_size); }
/** use free() to free data. */ int store_picture_to_buffer(guint8 **data, guint *data_len) { struct GLOBAL *global = all_data.global; struct vdIn *videoIn = all_data.videoIn; struct JPEG_ENCODER_STRUCTURE *jpeg_struct = NULL; BYTE *pim = NULL; BYTE *jpeg = NULL; int jpeg_size = 0; /* TODO snapshot by rock. I save raw (no filter, no image processing) frame in snapshot. So here should save picture from snapshot, not framebuffer. */ switch(global->imgFormat) { case 0:/*jpg*/ /* Save directly from MJPG frame */ #if 0 // TODO #1432, some webcam's mjpeg frame could not directly save as jpeg. if((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_MJPEG)) { if(SaveJPG(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } } else #endif if ((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_JPEG)) { *data_len = videoIn->buf.bytesused; *data = malloc(videoIn->buf.bytesused); memmove(*data, videoIn->tmpbuffer, videoIn->buf.bytesused); #if 0 if (SaveBuff(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif } else { /* use built in encoder */ jpeg = g_new0(BYTE, ((global->width)*(global->height))>>1); jpeg_struct = g_new0(struct JPEG_ENCODER_STRUCTURE, 1); /* Initialization of JPEG control structure */ initialization (jpeg_struct,global->width,global->height); /* Initialization of Quantization Tables */ initialize_quantization_tables (jpeg_struct); //jpeg_size = encode_image(videoIn->framebuffer, jpeg, jpeg_size = encode_image(videoIn->snapshot, jpeg, jpeg_struct, 1, global->width, global->height); *data_len = jpeg_size; *data = malloc(jpeg_size); memmove(*data, jpeg, jpeg_size); #if 0 if(SaveBuff(videoIn->ImageFName, jpeg_size, jpeg)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif } break; case 1:/*bmp*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2bgr(videoIn->framebuffer,pim,global->width,global->height); yuyv2bgr(videoIn->snapshot,pim,global->width,global->height); _store_picture_to_buffer_bmp(global->width, global->height, 24, pim, data, data_len); #if 0 if(SaveBPM(videoIn->ImageFName, global->width, global->height, 24, pim)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif break; case 2:/*png*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2rgb(videoIn->framebuffer,pim,global->width,global->height); yuyv2rgb(videoIn->snapshot,pim,global->width,global->height); _store_picture_to_buffer_png(global->width, global->height, pim, data, data_len); //write_png(videoIn->ImageFName, global->width, global->height, pim); break; case 3:/*raw*/ videoIn->cap_raw = 1; return 1; } if(jpeg_struct) g_free(jpeg_struct); jpeg_struct=NULL; if(jpeg) g_free(jpeg); jpeg = NULL; if(pim) g_free(pim); pim=NULL; return 0; }
int store_picture(void *data) { struct ALL_DATA *all_data = (struct ALL_DATA *) data; struct GLOBAL *global = all_data->global; struct vdIn *videoIn = all_data->videoIn; struct JPEG_ENCODER_STRUCTURE *jpeg_struct = NULL; BYTE *pim = NULL; BYTE *jpeg = NULL; int jpeg_size = 0; int rc = 0; /* TODO snapshot by rock. I save raw (no filter, no image processing) frame in snapshot. So here should save picture from snapshot, not framebuffer. */ switch(global->imgFormat) { case IMG_FORMAT_JPG:/*jpg*/ /* Save directly from MJPG frame */ #if 0 // TODO #1432, some webcam's mjpeg frame could not directly save as jpeg. if((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_MJPEG)) { if(SaveJPG(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } } else #endif if ((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_JPEG)) { if (SaveBuff(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } } else { /* use built in encoder */ jpeg = g_new0(BYTE, ((global->width)*(global->height))>>1); jpeg_struct = g_new0(struct JPEG_ENCODER_STRUCTURE, 1); /* Initialization of JPEG control structure */ initialization (jpeg_struct,global->width,global->height); /* Initialization of Quantization Tables */ initialize_quantization_tables (jpeg_struct); //jpeg_size = encode_image(videoIn->framebuffer, jpeg, jpeg_size = encode_image(videoIn->snapshot, jpeg, jpeg_struct, 1, global->width, global->height); if(SaveBuff(videoIn->ImageFName, jpeg_size, jpeg)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } } break; case IMG_FORMAT_BMP:/*bmp*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2bgr(videoIn->framebuffer,pim,global->width,global->height); yuyv2bgr(videoIn->snapshot,pim,global->width,global->height); if(SaveBPM(videoIn->ImageFName, global->width, global->height, 24, pim)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } break; case IMG_FORMAT_PNG:/*png*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2rgb(videoIn->framebuffer,pim,global->width,global->height); yuyv2rgb(videoIn->snapshot,pim,global->width,global->height); write_png(videoIn->ImageFName, global->width, global->height, pim); break; case IMG_FORMAT_RAW:/*raw*/ videoIn->cap_raw = 1; //return 1; rc = 1; } end_func: if(jpeg_struct) g_free(jpeg_struct); jpeg_struct=NULL; if(jpeg) g_free(jpeg); jpeg = NULL; if(pim) g_free(pim); pim=NULL; return rc; }
void *computervision_thread_main(void* data) { // Video Input struct vid_struct vid; vid.device = (char*)"/dev/video1"; vid.w=1280; vid.h=720; vid.n_buffers = 4; if (video_init(&vid)<0) { printf("Error initialising video\n"); computervision_thread_status = -1; return 0; } // Frame Grabbing struct img_struct* img_new = video_create_image(&vid); // Frame Resizing uint8_t quality_factor = IMAGE_QUALITY_FACTOR; uint8_t dri_jpeg_header = 1; struct img_struct small; small.w = vid.w / IMAGE_DOWNSIZE_FACTOR; small.h = vid.h / IMAGE_DOWNSIZE_FACTOR; small.buf = (uint8_t*)malloc(small.w*small.h*2); // Commpressed image buffer uint8_t* jpegbuf = (uint8_t*)malloc(vid.h*vid.w*2); // file index (search from 0) int file_index = 0; int microsleep = (int)(1000000. / IMAGE_FPS); while (computer_vision_thread_command > 0) { usleep(microsleep); video_grab_image(&vid, img_new); // Resize resize_uyuv(img_new, &small, IMAGE_DOWNSIZE_FACTOR); // JPEG encode the image: uint32_t image_format = FOUR_TWO_TWO; // format (in jpeg.h) uint8_t* end = encode_image (small.buf, jpegbuf, quality_factor, image_format, small.w, small.h, dri_jpeg_header); uint32_t size = end-(jpegbuf); #if IMAGE_SAVE FILE* save; char save_name[128]; if (system("mkdir -p /data/video/images") == 0) { // search available index (max is 99) for ( ; file_index < 99; file_index++) { printf("search %d\n",file_index); sprintf(save_name,"/data/video/images/img_%02d.jpg",file_index); // test if file exists or not if (access(save_name, F_OK) == -1) { printf("access\n"); save = fopen(save_name, "w"); if (save != NULL) { fwrite(jpegbuf, sizeof(uint8_t), size, save); fclose(save); } else { printf("Error when opening file %s\n", save_name); } // leave for loop break; } else {printf("file exists\n");} } } #endif // Fork process int status; pid_t pid = fork(); if (pid == 0) { // Open process to send using netcat in child process char nc_cmd[64]; sprintf(nc_cmd, "nc %s %d", IMAGE_SERVER_IP, IMAGE_SERVER_PORT); FILE* netcat; netcat = popen(nc_cmd, "w"); if (netcat != NULL) { fwrite(jpegbuf, sizeof(uint8_t), size, netcat); if (pclose(netcat) == 0) { printf("Sending image succesfully\n"); } } else { printf("Fail sending image\n"); } exit(0); } else if (pid < 0) { printf("Fork failed\n"); } else { // Parent is waiting for child to terminate wait(&status); } } printf("Thread Closed\n"); video_close(&vid); computervision_thread_status = -100; return 0; }