Exemplo n.º 1
0
int
main (int argc,
      char *argv[])
{
   unicap_handle_t handle;
   unicap_format_t format;
   
   /* Initialize */
   gtk_init (&argc, &argv);
   g_thread_init(NULL);
   gdk_threads_init ();
   gtk_gl_init (&argc, &argv);
   init_extensions ();
   init_gl_resources ();

   handle = open_device();
   set_format( handle );
   unicap_get_format( handle, &format );
   if( ( format.size.width != 640 ) ||
       ( format.size.height != 480 ) )
   {
      g_warning( "The default .cg file assumes a video format of 640x480 pixels. \nYou need to change the yuv.cg file to match your size.\n" );
   }
   
   prepare_yuv_buffer(&yuvbuffer, &format);
   unicap_register_callback( handle, UNICAP_EVENT_NEW_FRAME, (unicap_callback_t)new_frame_cb, NULL ); 
   unicap_start_capture( handle );
   

   /* Gtk window & container */
   GtkWindow *window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
   glarea = gtk_drawing_area_new ();
   gtk_widget_set_size_request (GTK_WIDGET (glarea), WINDOW_WIDTH, WINDOW_HEIGHT);
   g_signal_connect (glarea, "expose-event", G_CALLBACK (on_expose), NULL);
   g_signal_connect (glarea, "configure-event", G_CALLBACK (on_configure), NULL);
   g_signal_connect (glarea, "map-event", G_CALLBACK (on_mapped), NULL);
   g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);

   GdkGLConfig *gl_config;
   gl_config = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE);

   if (gl_config == NULL) 
      g_critical ("Failed to setup a double-buffered RGB visual");

   if (! gtk_widget_set_gl_capability (GTK_WIDGET (glarea), 
				       gl_config,
				       NULL,
				       TRUE,
				       GDK_GL_RGBA_TYPE))
      g_critical ("Failed to add gl capability");

   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (glarea));
   gtk_widget_show_all (GTK_WIDGET (window));


   /* Main loop */
   gtk_main ();
   return 0;
}
Exemplo n.º 2
0
unicap_format_t
getformat(unicap_handle_t handle)
{
  unicap_format_t format;
  if (!SUCCESS(unicap_get_format(handle, &format))) 
    {
      fprintf(stderr, "cannot get format\n");
      return format;
    }
  return format;
}
Exemplo n.º 3
0
//--------------------------------------------------------------------
// If a 24 bit video format is founded this is the preferred one, if not, the first
// returned by unicap is selected.
//
// Then it tries to set the desired width and height, if these fails, tries find the
// nearest size or to set the default width and height.
//
// On V4L devices 24 bit format is always BGR, so it needs conversion.
// On some V4L devices using non-default width/heigth it reports BGR but returns RGB.
// ffmpeg color conversion
void ofUCUtils::set_format(int w, int h) {
	if(!deviceReady)
		return;
	d_width=w;
	d_height=h;
	unicap_format_t formats[MAX_FORMATS];
	int format_count;
	unicap_status_t status = STATUS_SUCCESS;
	int rgb24 = -1;

	ofLog(OF_NOTICE,"ofUCUtils : Available formats for this device:");
	for (format_count = 0; SUCCESS (status) && (format_count < MAX_FORMATS); format_count++) {
		status = unicap_enumerate_formats (handle, NULL, &formats[format_count], format_count);
		if (SUCCESS (status)) {
			if (formats[format_count].bpp == 24) {
				rgb24 = format_count;
			}
			ofLog(OF_NOTICE,
					"ofUCUtils : %d: %s, min size: %dx%d, max size:%dx%d, default size: %dx%d",
					format_count, formats[format_count].identifier,
					formats[format_count].min_size.width,
					formats[format_count].min_size.height,
					formats[format_count].max_size.width,
					formats[format_count].max_size.height,
					formats[format_count].size.width,
					formats[format_count].size.height);
			ofLog(OF_VERBOSE,"ofUCUtils: available sizes for this format:");
			for(int i=0; i<formats[format_count].size_count;i++){
				ofLog(OF_VERBOSE,"          %dx%d",formats[format_count].sizes[i].width,formats[format_count].sizes[i].height);
			}
		}
	}

	if (format_count > 0) {
		int selected_format = 0;
		if (rgb24 != -1)
			selected_format = rgb24;
		else{
			for(selected_format=0;selected_format<format_count;selected_format++){
				format = formats[selected_format];
				if(fourcc_to_pix_fmt(format.fourcc)!=-1)
					break;
			}
		}
		format = formats[selected_format];

		bool exactMatch  = false;
		int sizeDiff = 99999999;
		int mostAproxSize = -1;

		for(int i=0; i<format.size_count;i++){
			if(format.sizes[i].width == w && format.sizes[i].height==h){
				exactMatch=true;
				format.size.width  = format.sizes[i].width;
				format.size.height = format.sizes[i].height;
				break;
			}else{
				if(abs(format.sizes[i].width-w)+abs(format.sizes[i].height-h)<sizeDiff){
					sizeDiff=abs(format.sizes[i].width-w)+abs(format.sizes[i].height-h);
					mostAproxSize=i;
				}
			}
		}
		if(!exactMatch && mostAproxSize!=-1){
			format.size.width  = format.sizes[mostAproxSize].width;
			format.size.height = format.sizes[mostAproxSize].height;

			ofLog(OF_WARNING, "ofUCUtils : Can't set video format %s, with size %dx%d, will use %dx%d",
							format.identifier, w, h,
							format.size.width, format.size.height);
		}else if(format.size_count==0){
			int defaultFormatWidth = format.size.width;
			int defaultFormatHeight = format.size.height;

			format.size.width  = w;
			format.size.height = h;

			ofLog(OF_WARNING, "ofUCUtils : Can't recognize supported video sizes for %s, trying with requested size: %i,%i",
										format.identifier, format.size.width, format.size.height);

			if ( !SUCCESS ( unicap_set_format (handle, &format) ) ) {
				format.size.width  = defaultFormatWidth;
				format.size.height = defaultFormatHeight;

				ofLog(OF_WARNING, "ofUCUtils : Can't set requested size, trying with format defaults: %i,%i",
						defaultFormatWidth, defaultFormatHeight);
			}
			ofLog(OF_WARNING, "ofUCUtils : If this doesn't work try using the reported default size in initGrabber:",
					defaultFormatWidth, defaultFormatHeight);
		}
		if ( !SUCCESS ( unicap_set_format (handle, &format) ) ) {
			ofLog(OF_ERROR, "ofUCUtils : Failed to set alternative video format!");
			return;
		}
		ofLog(OF_NOTICE,"ofUCUtils : Selected format: %s, with size %dx%d\n", format.identifier,
				format.size.width, format.size.height);

		src_pix_fmt=fourcc_to_pix_fmt(format.fourcc);
		if( src_pix_fmt==-1){
			ofLog(OF_ERROR,"ofUCUtils : Format not suported\n");
			return;
		}

		if(src_pix_fmt!=PIX_FMT_RGB24 || !exactMatch){
			src=new AVPicture;
			avpicture_alloc(src,src_pix_fmt,format.size.width,format.size.height);
			dst=new AVPicture;
			avpicture_alloc(dst,PIX_FMT_RGB24,d_width,d_height);

			toRGB_convert_ctx = sws_getContext(
							format.size.width, format.size.height, src_pix_fmt,
							d_width, d_height, PIX_FMT_RGB24,
							VIDEOGRABBER_RESIZE_FLAGS, NULL, NULL, NULL);


			ofLog(OF_NOTICE,"ofUCUtils: Converting to RGB24 (%i,%i)\n",w,h);

			pixels=new unsigned char[d_width*d_height*3];
		}

	   if( !SUCCESS( unicap_get_format( handle, &format ) ) )
	   {
		   ofLog(OF_ERROR, "can't get format" );
		   return;
	   }

		format.buffer_type = UNICAP_BUFFER_TYPE_SYSTEM;

	   if( !SUCCESS( unicap_set_format( handle, &format ) ) )
	   {
		   ofLog(OF_WARNING, "ofUCUtils: Failed to activate SYSTEM_BUFFERS" );
	   }
	}
}