Example #1
0
//Processes the raw data into something that we can display.
void CameraCanvas::processNewData() {
    if(isRGBMode) convert_to_rgb();
    
    working_matrix = convertToCvMatrix();
    overlayObjects();
    overlayText();
    convert_thresh_displayable();
    
    working_threshold = convertThresholdMatrix();    
}
Example #2
0
void *decode_video_thread(void *arg)
{
    INFO("Started decode video thread!");
    av_session_t *_phone = arg;
    _phone->running_decvid = 1;

    //CodecState *cs = get_cs_temp(_phone->av);
    //cs->video_stream = 0;

    //int recved_size;
    //uint8_t dest[RTP_PAYLOAD_SIZE];

    //int dec_frame_finished;
    //AVFrame *r_video_frame;
    //r_video_frame = avcodec_alloc_frame();
    //AVPacket dec_video_packet;
    //av_new_packet (&dec_video_packet, 65536);
    int width = 0;
    int height = 0;

    while (_phone->running_decvid) {
        //recved_size = toxav_recv_rtp_payload(_phone->av, TypeVideo, dest);
        //if (recved_size) {
        vpx_image_t *image;

        if (toxav_recv_video(_phone->av, &image) == 0) {
            //memcpy(dec_video_packet.data, dest, recved_size);
            //dec_video_packet.size = recved_size;

            //avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet);

            //if (dec_frame_finished) {

            /* Check if size has changed */
            if (image->d_w != width || image->d_h != height) {

                width = image->d_w;
                height = image->d_h;

                printf("w: %d h: %d \n", width, height);

                screen = SDL_SetVideoMode(width, height, 0, 0);

                //if (_phone->video_picture.bmp)
                //   SDL_FreeYUVOverlay(_phone->video_picture.bmp);

                //_phone->video_picture.bmp = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, screen);
                // _phone->sws_SDL_r_ctx = sws_getContext(width, height, cs->video_decoder_ctx->pix_fmt, width, height, PIX_FMT_YUV420P,
                //                                      SWS_BILINEAR, NULL, NULL, NULL);
            }

            uint8_t *rgb_image = malloc(width * height * 3);
            convert_to_rgb(image, rgb_image);
            SDL_Surface *img_surface = SDL_CreateRGBSurfaceFrom(rgb_image, width, height, 24, width * 3, mask32(0), mask32(1),
                                       mask32(2), 0);

            if (SDL_BlitSurface(img_surface, NULL, screen, NULL) == 0)
                SDL_UpdateRect(screen, 0, 0, 0, 0);

            /*
            SDL_LockYUVOverlay(_phone->video_picture.bmp);
            memcpy(_phone->video_picture.bmp->pixels[0], image->planes[VPX_PLANE_Y], _phone->video_picture.bmp->pitches[0] * height);
            memcpy(_phone->video_picture.bmp->pixels[1], image->planes[VPX_PLANE_V], _phone->video_picture.bmp->pitches[1] * height / 2);
            memcpy(_phone->video_picture.bmp->pixels[2], image->planes[VPX_PLANE_U], _phone->video_picture.bmp->pitches[2] * height / 2);

            SDL_Rect rect;
            rect.x = 0;
            rect.y = 0;
            rect.w = width;
            rect.h = height;
            SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);*/
            free(rgb_image);
            //display_received_frame(_phone, image);

        } //else {

        /* TODO: request the sender to create a new i-frame immediatly */
        //printf("Bad video packet\n");
        //}
        //}

        usleep(1000);
    }

    /* clean up codecs */
    //av_free(r_video_frame);

    //pthread_mutex_lock(&cs->ctrl_mutex);
    //avcodec_close(cs->video_decoder_ctx);
    //pthread_mutex_unlock(&cs->ctrl_mutex);

    _phone->running_decvid = -1;

    pthread_exit ( NULL );
}
Example #3
0
void*
V4lThread(void* arg)
{
  chain_t* v4l_service=NULL;
  v4lthread_info_t *info=NULL;
  long int skip_counter;
  float tmp;
  //struct video_picture p;

  struct v4l2_format pf;

  v4l_service=(chain_t*)arg;
  pthread_mutex_lock(&v4l_service->mutex_data);
  info=(v4lthread_info_t*)v4l_service->data;
  camera_t *cam=v4l_service->camera;

  skip_counter=0;
  v4l_service->processed_frames=0;

  /* These settings depend on the thread. For 100% safe deferred-cancel
   threads, I advise you use a custom thread cancel flag. See display thread.*/
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
  pthread_mutex_unlock(&v4l_service->mutex_data);
  
  // time inits:
  v4l_service->prev_time = 0;
  v4l_service->prev_period = 0;
  v4l_service->drop_warning = 0;
  v4l_service->processed_frames=0;

  while (1) { 
    /* Clean cancel handlers */
    pthread_mutex_lock(&info->mutex_cancel);
    if (info->cancel_req>0) {
      break;
    }
    else {
      pthread_mutex_unlock(&info->mutex_cancel);
      pthread_mutex_lock(&v4l_service->mutex_data);
      if(GetBufferFromPrevious(v4l_service)) { // have buffers been rolled?
	// check params
	V4lThreadCheckParams(v4l_service);
	
	/* IF we have mono data then set V4L for mono(grey) output */
	/* Only do this ONCE before writing the first frame */
	if ((v4l_service->current_buffer->frame.color_coding == DC1394_COLOR_CODING_MONO8) && v4l_service->processed_frames==0) {
	  Warning("Setting V4L device to GREY pix_fmt");
          pf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
	  if (ioctl(info->v4l_dev,VIDIOC_G_FMT,&pf) < 0) 
	    Error("ioctl(VIDIOC_G_FMT) error");
	  else {
	    pf.fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
            pf.fmt.pix.sizeimage = v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1];
	    if (ioctl(info->v4l_dev,VIDIOC_S_FMT,&pf) < 0) 
	      Error("ioctl(VIDIOC_S_FMT) Error");
	  }
	}
	else if ((v4l_service->current_buffer->frame.color_coding == DC1394_COLOR_CODING_RAW8) && v4l_service->processed_frames==0) {
	  Warning("Setting V4L device to RGGB pix_fmt");
          pf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
	  if (ioctl(info->v4l_dev,VIDIOC_G_FMT,&pf) < 0) 
	    Error("ioctl(VIDIOC_G_FMT) error");
	  else {
	    pf.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
            pf.fmt.pix.sizeimage = v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1];
	    if (ioctl(info->v4l_dev,VIDIOC_S_FMT,&pf) < 0) 
	      Error("ioctl(VIDIOC_S_FMT) Error");
	  }
	}
	else if (v4l_service->processed_frames==0) { // Default is RGB24
	  Warning("Setting V4L device to RGB24 pix_fmt");
          pf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
	  if (ioctl(info->v4l_dev,VIDIOC_G_FMT,&pf) < 0) 
	    Error("ioctl(VIDIOC_G_FMT) error");
	  else {
	    pf.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
            pf.fmt.pix.sizeimage = v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1]*3;
	    if (ioctl(info->v4l_dev,VIDIOC_S_FMT,&pf) < 0) 
	      Error("ioctl(VIDIOC_S_FMT) Error");
	  }
	}

	// Convert to RGB unless we are using direct GREY palette
	if ((v4l_service->current_buffer->frame.color_coding != DC1394_COLOR_CODING_MONO8) &&
	    (v4l_service->current_buffer->frame.color_coding != DC1394_COLOR_CODING_RAW8)) {
	  convert_to_rgb(&v4l_service->current_buffer->frame, &info->frame);
	  //swap_rb(info->frame.image, v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1]*3);
	}

	if (v4l_service->current_buffer->frame.size[0]!=-1) {
	  if (skip_counter>=(cam->prefs.v4l_period-1)) {
	    skip_counter=0;
	    if ((v4l_service->current_buffer->frame.color_coding != DC1394_COLOR_CODING_MONO8) &&
		(v4l_service->current_buffer->frame.color_coding != DC1394_COLOR_CODING_RAW8))
	      write(info->v4l_dev,info->frame.image,v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1]*3);
	    else
	      write(info->v4l_dev,v4l_service->current_buffer->frame.image,v4l_service->current_buffer->frame.size[0]*v4l_service->current_buffer->frame.size[1]);
	    v4l_service->processed_frames++;
	  }
	  else
	    skip_counter++;
	  
	  // FPS computation:
	  tmp=((float)(v4l_service->current_buffer->frame.timestamp-v4l_service->prev_time))/1000000.0;
	  if (v4l_service->prev_time==0) {
	    v4l_service->fps=fabs(0.0);
	  }
	  else {
	    if (tmp==0)
	      v4l_service->fps=fabs(0.0);
	    else
	      v4l_service->fps=fabs(1/tmp);
	  }
	  if (v4l_service->prev_time!=0) {
	    v4l_service->prev_period=tmp;
	  }
	  // produce a drop warning if the period difference is over 50%
	  if (v4l_service->prev_period!=0) {
	    if (fabs(v4l_service->prev_period-tmp)/(v4l_service->prev_period/2+tmp/2)>=.5)
	      v4l_service->drop_warning++;
	  }
	  v4l_service->prev_time=v4l_service->current_buffer->frame.timestamp;

	}
	PublishBufferForNext(v4l_service);
	pthread_mutex_unlock(&v4l_service->mutex_data);
      }
      else {
	pthread_mutex_unlock(&v4l_service->mutex_data);
      }
    }
    usleep(100);
  }

  pthread_mutex_unlock(&info->mutex_cancel);
  return ((void*)1);

}
Example #4
0
int sanei_canon_pp_read_segment(image_segment **dest, scanner_parameters *sp, 
		scan_parameters *scanp, int scanline_number, int do_adjust,
		int scanlines_left)
{
	unsigned char *input_buffer = NULL;
	image_segment *output_image = NULL;

	unsigned char packet_header[4];
	unsigned char packet_req_command[10];

	int read_data_size;
	int scanline_size;

	if (scanp->mode == 1) /* RGB */
		scanline_size = scanp->width * 3.75;
	else /* Greyscale */
		scanline_size = scanp->width * 1.25;

	read_data_size = scanline_size * scanline_number;

	/* Allocate output_image struct */
	if ((output_image = malloc(sizeof(*output_image))) == NULL)
	{
		DBG(1, "read_segment: Error: Not enough memory for scanner "
				"input buffer\n");
		goto error_out;
	}

	/* Allocate memory for input buffer */
	if ((input_buffer = malloc(scanline_size * scanline_number)) == NULL)
	{
		DBG(1, "read_segment: Error: Not enough memory for scanner "
				"input buffer\n");
		goto error_out;
	}

	output_image->width = scanp->width;
	output_image->height = scanline_number;

	/* Allocate memory for dest image segment */

	output_image->image_data = 
		malloc(output_image->width * output_image->height * 
				(scanp->mode ? 3 : 1) * 2);

	if (output_image->image_data == NULL)
	{
		DBG(1, "read_segment: Error: Not enough memory for "
				"image data\n");
		goto error_out;
	}

	/* Set up packet request command */
	memcpy(packet_req_command, cmd_packet_req, 10);
	packet_req_command[7] = ((read_data_size + 4) & 0xFF00) >> 8;
	packet_req_command[8] = (read_data_size + 4) & 0xFF;

	/* Send packet req. and wait for the scanner's READY signal */
	if (send_command(sp->port, packet_req_command, 10, 9000, 2000000))
	{
		DBG(1, "read_segment: Error: didn't get response within 2s "
				"of sending request");
		goto error_out;
	}

	/* Read packet header */
	if (sanei_canon_pp_read(sp->port, 4, packet_header))
	{
		DBG(1, "read_segment: Error reading packet header\n");
		goto error_out;
	}

	if ((packet_header[2]<<8) + packet_header[3] != read_data_size)
	{
		DBG(1, "read_segment: Error: Expected data size: %i bytes.\n", 
				read_data_size);
		DBG(1, "read_segment: Expecting %i bytes times %i "
				"scanlines.\n", scanline_size, scanline_number);
		DBG(1, "read_segment: Actual data size: %i bytes.\n", 
				(packet_header[2] << 8) + packet_header[3]);
		goto error_out;
	}

	/* Read scanlines_this_packet scanlines into the input buf */
	
	if (sanei_canon_pp_read(sp->port, read_data_size, input_buffer))
	{
		DBG(1, "read_segment: Segment read incorrectly, and we don't "
				"know how to recover.\n");
		goto error_out;
	}

	/* This is the only place we can abort safely - 
	 * between reading one segment and requesting the next one. */
	if (sp->abort_now) goto error_out;

	if (scanlines_left >= (scanline_number * 2)) 
	{
		DBG(100, "read_segment: Speculatively starting more scanning "
				"(%d left)\n", scanlines_left);
		sanei_canon_pp_write(sp->port, 10, packet_req_command);
		/* Don't read status, it's unlikely to be ready *just* yet */
	}

	DBG(100, "read_segment: Convert to RGB\n");
	/* Convert data */
	convert_to_rgb(output_image, input_buffer, scanp->width, 
			scanline_number, scanp->mode);

	/* Adjust pixel readings according to calibration data */
	if (do_adjust) {
		DBG(100, "read_segment: Adjust output\n");
		adjust_output(output_image, scanp, sp);
	}

	/* output */
	*dest = output_image;
	/* finished with this now */
	free(input_buffer);
	return 0;	

	error_out:
	if (output_image && output_image->image_data) 
		free(output_image->image_data);
	if (output_image) free(output_image);
	if (input_buffer) free(input_buffer);
	sp->abort_now = 0;
	return -1;
}
Example #5
0
SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * data,
	   SANE_Int max_length, SANE_Int * length)
{
  struct hp5590_scanner	*scanner = handle;
  SANE_Status 		ret;

  DBG (DBG_proc, "%s, length %u, left %u\n",
       __FUNCTION__,
       max_length,
       scanner->transferred_image_size);

  if (!length)
    {
      scanner->scanning = SANE_FALSE;
      return SANE_STATUS_INVAL;
    }

  if (scanner->transferred_image_size == 0)
    {
      *length = 0;
      DBG (DBG_verbose, "Setting scan count\n");

      ret = hp5590_inc_scan_count (scanner->dn, scanner->proto_flags);
      if (ret != SANE_STATUS_GOOD)
	return ret;

      /* Dont free bulk read state, some bytes could be left
       * for the next images from ADF
       */
      return SANE_STATUS_EOF;
    }

  if (!scanner->bulk_read_state)
    {
      ret = hp5590_low_init_bulk_read_state (&scanner->bulk_read_state);
      if (ret != SANE_STATUS_GOOD)
	{
	  scanner->scanning = SANE_FALSE;
	  return ret;
	}
    }

  *length = max_length;
  if (*length > scanner->transferred_image_size)
    *length = scanner->transferred_image_size;

  if (   scanner->depth == DEPTH_COLOR_24
      || scanner->depth == DEPTH_COLOR_48)
   {
      unsigned int bytes_per_line;
      ret = calc_image_params (scanner,
			       NULL, NULL,
			       &bytes_per_line,
			       NULL, NULL);
      if (ret != SANE_STATUS_GOOD)
	return ret;

     *length -= *length % bytes_per_line;
     DBG (2, "Aligning requested size to bytes per line "
	  "(requested: %u, aligned: %u)\n",
	  max_length, *length);
   }

  ret = hp5590_read (scanner->dn, scanner->proto_flags,
  		     data, *length, scanner->bulk_read_state);
  if (ret != SANE_STATUS_GOOD)
    {
      scanner->scanning = SANE_FALSE;
      return ret;
    }

  scanner->transferred_image_size -= *length;

  ret = convert_to_rgb (scanner, data, *length);
  if (ret != SANE_STATUS_GOOD)
    {
      scanner->scanning = SANE_FALSE;
      return ret;
    }

  ret = convert_lineart (scanner, data, *length);
  if (ret != SANE_STATUS_GOOD)
    return ret;

  return SANE_STATUS_GOOD;
}