Exemple #1
0
pipeinfo::~pipeinfo()
{
	m_b_closed = true;
	pi_logfunc("");


	// Change to non-blocking socket so calling threads can exit
	m_b_blocking = false;

	m_lock_tx.lock();
	m_lock_rx.lock();
	m_lock.lock();

	if (m_timer_handle) {
		g_p_event_handler_manager->unregister_timer_event(this, m_timer_handle);
		m_timer_handle = NULL;
	}
	
	statistics_print();

	m_lock_tx.unlock();
	m_lock_rx.unlock();
	m_lock.unlock();

	pi_logfunc("done");
}
Exemple #2
0
void
sigint_quit(void)
{
	int             pid = getpid();

	printf("\n--- jpegplay (%5d) caught SIGINT ---", pid);
	statistics_print(&STAT);
	printf("\n---\n");

	exit(-1);
}
Exemple #3
0
cq_mgr::~cq_mgr()
{
	cq_logdbg("destroying CQ as %s", (m_b_is_rx?"Rx":"Tx"));

	int ret = 0;
	uint32_t ret_total = 0;
	uint64_t cq_poll_sn = 0;
	mem_buf_desc_t* buff = NULL;
	struct ibv_wc wce[MCE_MAX_CQ_POLL_BATCH];
	while ((ret = poll(wce, MCE_MAX_CQ_POLL_BATCH, &cq_poll_sn)) > 0) {
		for (int i = 0; i < ret; i++) {
			if (m_b_is_rx) {
				buff = process_cq_element_rx(&wce[i]);
			} else {
				buff = process_cq_element_tx(&wce[i]);
			}
			if (buff)
				m_rx_queue.push_back(buff);
		}
		ret_total += ret;
	}
	m_b_was_drained = true;
	if (ret_total > 0) {
		cq_logdbg("Drained %d wce", ret_total);
	}

	if (m_rx_queue.size() + m_rx_pool.size()) {
		cq_logdbg("Returning %d buffers to global Rx pool (ready queue %d, free pool %d))", m_rx_queue.size() + m_rx_pool.size(), m_rx_queue.size(), m_rx_pool.size());

		g_buffer_pool_rx->put_buffers_thread_safe(&m_rx_queue, m_rx_queue.size());
		m_p_cq_stat->n_rx_sw_queue_len = m_rx_queue.size();

		g_buffer_pool_rx->put_buffers_thread_safe(&m_rx_pool, m_rx_pool.size());
		m_p_cq_stat->n_buffer_pool_len = m_rx_pool.size();
	}

	cq_logfunc("destroying ibv_cq");
	IF_VERBS_FAILURE(ibv_destroy_cq(m_p_ibv_cq)) {
		cq_logerr("destroy cq failed (errno=%d %m)", errno);
	} ENDIF_VERBS_FAILURE;

	statistics_print();
	if (m_b_is_rx)
		vma_stats_instance_remove_cq_block(m_p_cq_stat);

	cq_logdbg("done");
}
Exemple #4
0
int 
jpeg_display_rgb(int argc, char *argv[])
{
	u_int8_t       *rgb_buf;/* RGB data */
	u_int8_t       *jpeg_buf;	/* JPEG data */
	buf_t           jpeg_src_buf;
	size_t          read_size;	/* read size from file */
	int             row_stride;
	int             w, h, d;/* display size and depth(byte) */
	boolean         draw_screen;	/* draw screen surface or another
					 * surface */
	int             screen_status; /* 0=general 1=blue 2=red */
	/* for jpeg library */
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;

	/* for SDL */
	SDL_Surface    *screen;
	SDL_Surface    *sdl_image = NULL;
//	SDL_Rect        dstrect;

	/* for visualize packet loss */
	SDL_Surface    *sdl_img_blue = NULL;
	SDL_Surface    *sdl_img_red  = NULL;
	u_int8_t        pixel_blue[]={0,0,0xff};
	u_int8_t        pixel_red[]={0xff,0,0};
	u_int32_t       tick_start, tick_now;
	int             buf_status; /* -1=full */

	/* for realtime play */
	struct timeval  tv_start,tv_now;
	int64_t         ts_display,ts_diff;
	u_int32_t       ts_nowblk,ts_lastblk=0;
	
	/* for debug */
	struct timeval  tv_tmp1, tv_tmp2;
	

	/* SDL init */
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		ComplainAndExit();
	}
	atexit(SDL_Quit);

	/* jpeg library init / get image size and depth */
	cinfo.err = jpeg_std_error(&jerr);
	cinfo.err->error_exit = my_jpeg_abort_decompress;
	jpeg_create_decompress(&cinfo);

#ifdef USE_JPEG_MEM_SRC
	jpeg_buf = jpeg_mem_src_init(&cinfo, JPEG_BUF_MAX);	/* init */
#else
	jpeg_buf = (u_int8_t *) malloc(JPEG_BUF_MAX);
#endif

	decoder_buf_read();
	read_size = decoder_buf_get(jpeg_buf, JPEG_BUF_MAX,&ts_nowblk);

#ifdef USE_JPEG_MEM_SRC
	jpeg_mem_src(&cinfo, jpeg_buf, read_size);	/* read from memory */
#else
	jpeg_src_buf.buf = jpeg_buf;
	jpeg_buf_src(&cinfo, jpeg_buf, read_size);	/* read from memory */
#endif

	jpeg_read_header(&cinfo, TRUE);
	w = cinfo.image_width;
	h = cinfo.image_height;
	d = cinfo.num_components;
	jpeg_abort_decompress(&cinfo);

	d_printf("\nJPEG info: image=(%d,%d), output=(%d,%d), Bpp=%d\n",
		 cinfo.image_width, cinfo.image_height, w, h, d);

	/* SDL setup / cleanup screen-surface */
	screen = SDL_SetVideoMode(w, h, 0, SDL_HWSURFACE);
	if (screen == NULL) {
		ComplainAndExit();
	}
	if (OPT.loss_visual){
		sdl_img_blue = SDL_CreateRGBSurface(
			SDL_HWSURFACE, w, h, 24
			,OPT.sdl_mask_R, OPT.sdl_mask_G
			,OPT.sdl_mask_B, OPT.sdl_mask_A);
		sdl_img_red = SDL_CreateRGBSurface(
			SDL_HWSURFACE, w, h, 24
			,OPT.sdl_mask_R, OPT.sdl_mask_G
			,OPT.sdl_mask_B, OPT.sdl_mask_A);
		jpeg_fillimg1color(sdl_img_red,pixel_red,3);
		jpeg_fillimg1color(sdl_img_blue,pixel_blue,3);
	}
	if (screen->format->BytesPerPixel == RGB_PIXELSIZE &&
	    screen->format->Rmask == OPT.sdl_mask_R &&
	    screen->format->Gmask == OPT.sdl_mask_G &&
	    screen->format->Bmask == OPT.sdl_mask_B) {
		draw_screen = TRUE;
	} else {
		draw_screen = FALSE;
	}

	d1_printf("\nSDL screen  info: bpp=%d, Bpp=%d, "
		  "R/G/B-mask=%06x/%06x/%06x, Direct=%s",
		screen->format->BitsPerPixel, screen->format->BytesPerPixel,
		  screen->format->Rmask, screen->format->Gmask,
		  screen->format->Bmask,
		  draw_screen ? "ON" : "OFF");

	if (draw_screen==TRUE) {
		/* RGB_PIXELSIZE is defined in jmorecfg.h */
		row_stride = screen->pitch;
	        rgb_buf = (u_int8_t *) screen->pixels;
	} else {
		sdl_image = SDL_CreateRGBSurface(
						 SDL_HWSURFACE, w, h, 24,	/* depth (bit per pixel) */
					     OPT.sdl_mask_R, OPT.sdl_mask_G,
					    OPT.sdl_mask_B, OPT.sdl_mask_A);
		row_stride = sdl_image->pitch;
		d1_printf("\nSDL surface info: bpp=%d, Bpp=%d, "
			  "R/G/B-mask=%06x/%06x/%06x\n",
			  sdl_image->format->BitsPerPixel,
			  sdl_image->format->BytesPerPixel,
			  sdl_image->format->Rmask,
			  sdl_image->format->Gmask,
			  sdl_image->format->Bmask
			);
		rgb_buf = (u_int8_t *) sdl_image->pixels;
	}

	STAT.skip_count = 0;
	STAT.wait_count = 0;

	tick_start = SDL_GetTicks();	/* get start time */
	gettimeofday(&STAT.start, NULL);
	gettimeofday(&tv_start, NULL);
	ts_display=0;

	STAT.f_sigint = 0;
	signal(SIGINT, sigint_trap);



	jpeg_fillimg1color(screen,pixel_blue,3);
	screen_status=1;

	for (STAT.frame_count = 1;; STAT.frame_count++) {
		if (STAT.f_sigint)
			sigint_quit();

		jpeg_has_error = 0;	/* reset flag */
		buf_status=decoder_buf_read();
#if 0
		read_size = decoder_buf_get(jpeg_buf, JPEG_BUF_MAX);
#ifdef REALTIME_PLAY
		tick_now = SDL_GetTicks();
		if (tick_now - tick_start > STAT.frame_count * OPT.mspf) {
			/* skip frame because it's too late */
			STAT.skip_count++;
			continue;
		}
#endif
#else
		read_size = decoder_buf_get(jpeg_buf, JPEG_BUF_MAX,&ts_nowblk);
#ifdef REALTIME_PLAY
		if(buf_status==-1){
			/* need more cpu power */
			if(OPT.loss_visual){
				jpeg_blitimg2screen(sdl_img_red,screen);
				screen_status=2;
			}
			d1_printf("INFO:buffer skip\n");
			STAT.skip_count+=decoder_buf_get_datanum()/2;
			STAT.frame_count+=decoder_buf_get_datanum()/2;
			decoder_buf_rm(decoder_buf_get_datanum()/2);
			ts_display=0;
			gettimeofday(&tv_start, NULL);
			continue;
		}
#endif
#endif
		if (read_size == 0) {	/* buffer empty */
			d1_printf("INFO:buffer empty\n");
			STAT.frame_count--;	/* no skip, no display */
			if(OPT.loss_visual && screen_status!=1){
			  jpeg_blitimg2screen(sdl_img_blue,screen);
			  screen_status=1;
			}
			decoder_buf_prebuf();
			ts_display=0;
			gettimeofday(&tv_start, NULL);
			continue;
		} else if (read_size == -2) {
		    break;	/* end of all files */
		}
#ifdef USE_JPEG_MEM_SRC
		jpeg_mem_src(&cinfo, jpeg_buf, read_size);
#else
		jpeg_src_buf.buf = jpeg_buf;
		jpeg_buf_src(&cinfo, jpeg_buf, read_size);
#endif
		jpeg_read_header(&cinfo, TRUE);

		cinfo.output_width = w;
		cinfo.output_height = h;
		cinfo.out_color_space = JCS_RGB;	/* default */
		cinfo.output_components = d;
		/* more fast decompression */
		cinfo.dct_method = JDCT_FASTEST;
		//cinfo.dct_method = JDCT_FLOAT;
		cinfo.do_fancy_upsampling = FALSE;

		jpeg_start_decompress(&cinfo);

		/* screen surface lock */

		if (SDL_MUSTLOCK(screen)) {
			if (SDL_LockSurface(screen) < 0) {
				ComplainAndExit();
			}
		}
		/* JPEG decode start */
		gettimeofday(&tv_tmp1, NULL);
		while (cinfo.output_scanline < cinfo.output_height &&
		       !jpeg_has_error) {
			JSAMPLE        *rgb_scanline;
			rgb_scanline = &(rgb_buf[cinfo.output_scanline *
						 row_stride]);
			jpeg_read_scanlines(&cinfo, &rgb_scanline, 1);
		}
		/* screen surface unlock */
		if (SDL_MUSTLOCK(screen)) {
			SDL_UnlockSurface(screen);
		}
		jpeg_finish_decompress(&cinfo);
		/* JPEG decode finish */
		gettimeofday(&tv_tmp2, NULL);
		STAT.decode_usec += timeval_diff_usec(&tv_tmp2, &tv_tmp1);
#ifdef REALTIME_PLAY
		wait4rtdisplay(&tv_start,ts_display);

		if(ts_display==0){
		  ts_diff=OPT.freq;
		}else{
		  ts_diff=(u_int32_t)(ts_nowblk-ts_lastblk);
		}
		ts_lastblk=ts_nowblk;

		if(ts_diff<OPT.freq){
	 	  e_printf("INFO:reset TS interval\n");
		}
		if(ts_diff>OPT.freq){
		  d2_printf("blue back start for %u TS\n",(u_int32_t)ts_diff-OPT.freq);
		  ts_display+=(ts_diff-OPT.freq);
		  if(OPT.loss_visual && screen_status!=1){
  		    jpeg_blitimg2screen(sdl_img_blue,screen);
		    screen_status=1;
		  }
		  wait4rtdisplay(&tv_start,ts_display);
		  d2_printf("blue back end.\n");
		}else{
		  d3_printf("no blue back .\n");
		}
		ts_display+= OPT.freq;
#endif
		if (draw_screen==TRUE) {
			SDL_UpdateRect(screen, 0, 0, 0, 0);
			screen_status=0;
		} else {
			jpeg_blitimg2screen(sdl_image,screen);
			screen_status=0;
#if 0
			dstrect.x = 0;
			dstrect.y = 0;
			dstrect.w = sdl_image->w;
			dstrect.h = sdl_image->h;
			if (SDL_BlitSurface(sdl_image, NULL, screen, &dstrect) < 0) {
				SDL_FreeSurface(sdl_image);
				ComplainAndExit();
			}
#if 0
			if (SDL_MUSTLOCK(screen)) {
				SDL_UnlockSurface(screen);
			}
#endif
			SDL_UpdateRects(screen, 1, &dstrect);
#endif 
		}
	}			/* loop for all jpeg file */
	STAT.frame_count--;	/* because count from 1 */
	tick_now = SDL_GetTicks();

	if (!draw_screen) {
		SDL_FreeSurface(sdl_image);
	}
	jpeg_destroy_decompress(&cinfo);
	statistics_print(&STAT);
	return 0;
}
Exemple #5
0
int 
jpeg_display_yuv(int argc, char *argv[])
{
	u_int8_t       *y_buf = NULL, *u_buf = NULL, *v_buf = NULL;
	u_int8_t       *yuv_buf_tmp;
	u_int8_t       *jpeg_buf;
	buf_t           jpeg_src_buf;
	size_t          read_size;	/* read size from file */
	int             i, x, y, offU, offV;
	int             row_stride;
	int             w, h, d;/* display size and depth(byte) */
	boolean         draw_screen;	/* draw screen surface or another
					 * surface */

	/* for jpeg library */
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;

	/* for SDL */
	SDL_Surface    *screen;
	SDL_Overlay    *sdl_overlay = NULL;
	SDL_Rect        dstrect;
	u_int32_t       tick_start, tick_now;

	/* for realtime play */
	struct timeval  tv_start,tv_now;
	int64_t         ts_display,ts_diff;
	u_int32_t       ts_nowblk,ts_lastblk;

	/* for debug */
	struct timeval  tv_tmp1, tv_tmp2;

	/* SDL init */
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		ComplainAndExit();
	}
	SDL_WM_SetCaption(OPT.title, OPT.title);
	atexit(SDL_Quit);

	/* jpeg library init / get image size and depth */
	cinfo.err = jpeg_std_error(&jerr);
	cinfo.err->error_exit = my_jpeg_abort_decompress;
	jpeg_create_decompress(&cinfo);

#ifdef USE_JPEG_MEM_SRC
	jpeg_buf = jpeg_mem_src_init(&cinfo, JPEG_BUF_MAX);	/* init */
#else				/* Mr.Okamura's */
	jpeg_buf = (u_int8_t *) malloc(JPEG_BUF_MAX);
#endif

	decoder_buf_read();	/* pipe -> buffer */
	read_size = decoder_buf_get(jpeg_buf, JPEG_BUF_MAX,&ts_nowblk);	/* buffer -> mem */

#ifdef USE_JPEG_MEM_SRC		/* emon's original */
	jpeg_mem_src(&cinfo, jpeg_buf, read_size);
#else				/* Mr.Okamura's */
	jpeg_src_buf.buf = jpeg_buf;
	jpeg_buf_src(&cinfo, jpeg_buf, read_size);
#endif
	jpeg_read_header(&cinfo, TRUE);
	w = cinfo.image_width;
	h = cinfo.image_height;
	d = cinfo.num_components;
	jpeg_abort_decompress(&cinfo);
	offU = (w * h) * 4 / 4;
	offV = (w * h) * 5 / 4;

	d_printf("\nJPEG info: image=(%d,%d), output=(%d,%d), Bpp=%d\n",
		 cinfo.image_width, cinfo.image_height, w, h, d);

	/* SDL setup */
	draw_screen = FALSE;	/* can I draw image VRAM directly ? */
	if ((screen = SDL_SetVideoMode(w, h, 0, SDL_ASYNCBLIT)) == NULL) {
		ComplainAndExit();
	}
	d1_printf("\nSDL screen  info: bpp=%d, Bpp=%d, "
		  "R/G/B-mask=%06x/%06x/%06x\n",
		  screen->format->BitsPerPixel,
		  screen->format->BytesPerPixel,
		  screen->format->Rmask,
		  screen->format->Gmask,
		  screen->format->Bmask
		);

	if ((sdl_overlay = my_SDL_CreateYUVOverlay(w, h, screen)) == NULL) {
		ComplainAndExit();
	}
	d1_printf("\nSDL surface info: format=0x%08x, planes=%d, "
		  "hw_overlay_flag=%d\n",
		  sdl_overlay->format,
		  sdl_overlay->planes,
		  sdl_overlay->hw_overlay
		);

	row_stride = JPEG_YCbCr_PITCH * w;
	yuv_buf_tmp = malloc(JPEG_YCbCr_PITCH * w * h);

	STAT.skip_count = 0;
	STAT.wait_count = 0;
	tick_start = SDL_GetTicks();	/* get start time */
	gettimeofday(&STAT.start, NULL);

	STAT.f_sigint = 0;
	signal(SIGINT, sigint_trap);
	for (STAT.frame_count = 1;; STAT.frame_count++) {
		int             sleep_usec = 0;
		if (STAT.f_sigint)
			sigint_quit();

		jpeg_has_error = 0;	/* reset flag */

		decoder_buf_read();
		read_size = decoder_buf_get(jpeg_buf, JPEG_BUF_MAX,&ts_nowblk);
#ifdef REALTIME_PLAY
		tick_now = SDL_GetTicks();
		if (tick_now - tick_start > STAT.frame_count * OPT.mspf) {
			/* skip frame because it's too late */
			STAT.skip_count++;
			d2_printf("s");
			continue;
		}
#endif
		if (read_size == 0) {	/* buffer empty */
			usleep(OPT.mspf * (1000 * (9 / 10)));
			STAT.frame_count--;	/* no skip, no display */
			continue;
		} else if (read_size == -1) {
			break;	/* end of all files */
		}
#ifdef REALTIME_PLAY
		sleep_usec =
			((STAT.frame_count - 1) * OPT.mspf
			 - (tick_now - tick_start)) * 1000;
		if (sleep_usec > 0) {
			usleep(sleep_usec);
			STAT.wait_count++;
			//decoder_buf_read();	/* check new data */
		}
#endif
#ifdef USE_JPEG_MEM_SRC
		jpeg_mem_src(&cinfo, jpeg_buf, read_size);
#else
		jpeg_src_buf.buf = jpeg_buf;
		jpeg_buf_src(&cinfo, jpeg_buf, read_size);
#endif
		jpeg_read_header(&cinfo, TRUE);

		cinfo.output_width = w;
		cinfo.output_height = h;
		cinfo.out_color_space = JCS_YCbCr;
		/* more fast decompression */
		cinfo.dct_method = JDCT_FASTEST;
		//cinfo.dct_method = JDCT_FLOAT;
		cinfo.do_fancy_upsampling = FALSE;

		if (!jpeg_has_error) {
			jpeg_start_decompress(&cinfo);
		}
		/* JPEG decode start */
		gettimeofday(&tv_tmp1, NULL);
		while (cinfo.output_scanline < cinfo.output_height &&
		       !jpeg_has_error) {
			JSAMPLE        *yuv_scanline;
			yuv_scanline = &(yuv_buf_tmp[cinfo.output_scanline *
						     row_stride]);
			jpeg_read_scanlines(&cinfo, &yuv_scanline, 1);
		}
		jpeg_finish_decompress(&cinfo);
		/* JPEG decode finish */
		gettimeofday(&tv_tmp2, NULL);
		STAT.decode_usec += timeval_diff_usec(&tv_tmp2, &tv_tmp1);

		decoder_buf_read();	/* check new data */

		switch (sdl_overlay->format) {

		case SDL_YV12_OVERLAY:
#ifdef  SDL_1_1_5		/* for < SDL 1.1.5 */
			y_buf = (u_int8_t *) sdl_overlay->pixels;
			u_buf = &(y_buf[offU]);
			v_buf = &(y_buf[offV]);
#else
			y_buf = sdl_overlay->pixels[0];
			u_buf = sdl_overlay->pixels[1];
			v_buf = sdl_overlay->pixels[2];
#endif
			for (i = 0; i < w * h; i++) {
				y_buf[i] = yuv_buf_tmp[i * JPEG_YCbCr_PITCH];
			}
			for (i = 0, y = 0; y < h; y += 2) {
				for (x = 0; x < w; x += 2, i++) {
					const int       p =
					(y * w + x) * JPEG_YCbCr_PITCH;
					u_buf[i] = yuv_buf_tmp[p + 2];
					v_buf[i] = yuv_buf_tmp[p + 1];
				}
			}
			break;
		case SDL_IYUV_OVERLAY:
#ifdef  SDL_1_1_5		/* for < SDL 1.1.5 */
			y_buf = (u_int8_t *) sdl_overlay->pixels;
			v_buf = &(y_buf[offU]);
			u_buf = &(y_buf[offV]);
#else
			y_buf = sdl_overlay->pixels[0];
			v_buf = sdl_overlay->pixels[1];
			u_buf = sdl_overlay->pixels[2];
#endif
			for (i = 0; i < w * h; i++) {
				y_buf[i] = yuv_buf_tmp[i * JPEG_YCbCr_PITCH];
			}
			for (i = 0, y = 0; y < h; y += 2) {
				for (x = 0; x < w; x += 2, i++) {
					const int       p =
					(y * w + x) * JPEG_YCbCr_PITCH;
					u_buf[i] = yuv_buf_tmp[p + 2];
					v_buf[i] = yuv_buf_tmp[p + 1];
				}
			}
			break;
		default:
			d_printf("\nI'm sorry. not support YUV format: %0x08x\n",
				 sdl_overlay->format);
			exit(1);
			break;
		}

		if (OPT.f_disp) {	/* display flag check */
			dstrect.x = 0;
			dstrect.y = 0;
			dstrect.w = sdl_overlay->w;
			dstrect.h = sdl_overlay->h;

			if (SDL_LockYUVOverlay(sdl_overlay) < 0) {
				ComplainAndExit();
			}
			if (SDL_DisplayYUVOverlay(sdl_overlay, &dstrect) < 0) {
				SDL_FreeYUVOverlay(sdl_overlay);
				ComplainAndExit();
			}
			SDL_UnlockYUVOverlay(sdl_overlay);
		}
	}			/* loop for all jpeg file */

	STAT.frame_count--;	/* because count up from 1 */
	tick_now = SDL_GetTicks();
	if (!draw_screen) {
		SDL_FreeYUVOverlay(sdl_overlay);
	}
	jpeg_destroy_decompress(&cinfo);
	statistics_print(&STAT);
	return 0;
}
Exemple #6
0
/**
 * @brief edax main function.
 *
 * Do a global initialization and choose a User Interface protocol.
 *
 * @param argc Number of arguments.
 * @param argv Command line arguments.
 */
int main(int argc, char **argv)
{
	UI *ui;
	int i, r, level = 0, size = 8;
	char *problem_file = NULL;
	char *wthor_file = NULL;
	char *count_type = NULL;
	int n_bench = 0;

	// options.n_task default to system cpu number
	options.n_task = get_cpu_number();

	// options from edax.ini
	options_parse("edax.ini");

	// allocate ui
	ui = (UI*) malloc(sizeof *ui);
	if (ui == NULL) fatal_error("Cannot allocate a user interface.\n");
	ui->type = UI_EDAX;
	ui->init = ui_init_edax;
	ui->free = ui_free_edax;
	ui->loop = ui_loop_edax;

	// parse arguments
	for (i = 1; i < argc; i++) {
		char *arg = argv[i];
		while (*arg == '-') ++arg;
		if (strcmp(arg, "v") == 0 || strcmp(arg, "version") == 0) version();
		else if (ui_switch(ui, arg)) ;
		else if ((r = (options_read(arg, argv[i + 1]))) > 0) i += r - 1;
		else if (strcmp(arg, "solve") == 0 && argv[i + 1]) problem_file = argv[++i];
		else if (strcmp(arg, "wtest") == 0 && argv[i + 1]) wthor_file = argv[++i];
		else if (strcmp(arg, "bench") == 0 && argv[i + 1]) n_bench = atoi(argv[++i]);
		else if (strcmp(arg, "count") == 0 && argv[i + 1]) {
			count_type = argv[++i];
			if (argv[i + 1]) level = string_to_int(argv[++i], 0);
			if (argv[i + 1] && strcmp(argv[i + 1], "6x6") == 0) {
				size = 6;
				++i;
			}
			
	
		}
		else usage();
	}
	options_bound();

	// initialize
	edge_stability_init();
	hash_code_init();
	hash_move_init();
	statistics_init();
	eval_open(options.eval_file);
	search_global_init();

	// solver & tester
	if (problem_file || wthor_file || n_bench) {
		Search search[1];
		search_init(search);
		search->options.header = " depth|score|       time   |  nodes (N)  |   N/s    | principal variation";
		search->options.separator = "------+-----+--------------+-------------+----------+---------------------";
		if (options.verbosity) version();
		if (problem_file) obf_test(search, problem_file, NULL);
		if (wthor_file) wthor_test(wthor_file, search);
		if (n_bench) obf_speed(search, n_bench);
		search_free(search);

	} else if (count_type){
		Board board[1];
		board_init(board);
		if (strcmp(count_type, "games") == 0) quick_count_games(board, level, size);		
		else if (strcmp(count_type, "positions") == 0) count_positions(board, level, size);		
		else if (strcmp(count_type, "shapes") == 0) count_shapes(board, level, size);		

	} else if (ui->type == UI_CASSIO) {
		engine_loop();

	// other protocols
	} else {
		ui_event_init(ui);
		ui->init(ui);
		ui->loop(ui);
		if (ui->free) ui->free(ui);
		ui_event_free(ui);
	}

	// display statistics
	statistics_print(stdout);


	// free;
	eval_close();
	options_free();
	free(ui);

	return 0;
}