int wait_proper_time(struct timeval *start, int fps, unsigned int frame_num, int wait_flag) /* * wait_flag : 0 => return -1 or 0 * !0 => return -1, 0 or 1> * * return -1, 0 or over then 1 * -1 : no wait because too late * 0 : no wait, just in time * 1>: wait some second */ { struct timeval now; struct timeval elapse; struct timeval estimate; struct timeval now2; int diff_usec; int slp_c = 0; /* start of time check */ for (;;){ gettimeofday(&now, NULL); elapse = timeval_sub_timeval(&now, start); estimate.tv_sec = frame_num / fps; estimate.tv_usec = ((frame_num % fps)*1000*1000) / fps; diff_usec = timeval_diff_usec(&estimate, &elapse); if (!wait_flag){ break; } if (diff_usec <= 0){ break; } else { usleep(diff_usec); timeval_add_usec(&STAT.usleep_total, diff_usec); gettimeofday(&now2, NULL); diff_usec = timeval_diff_usec(&now2, &now); timeval_add_usec(&STAT.usleepr_total, diff_usec); d3_printf(" us:%d", diff_usec); slp_c++; /* break; */ /* check again! (=loop again!) */ /* to cape with too short usleep */ } } if (diff_usec < -(1000*1000/fps)) { /* too late, skip */ d3_printf("\nfram=%u: skip", frame_num); return -1; } d3_printf(" [%d]", slp_c); STAT.usleep_count += slp_c; return slp_c; }
/* read from socket and write to buffer */ int decoder_buf_read(void) { int len = 0; int count = 0; char header[PIPE_HEADER_LEN]; #define MAX_LOOP 1 #if 0 fprintf(stderr, "decoder_buf_read\n"); #endif len=0; memset(header, 0, PIPE_HEADER_LEN); while (fd_can_read(fd,1000) && count++ < MAX_LOOP) { if (BUFFER_NEXT(head) == tail) { /* buffer full */ return -1; } /* TODO:呼出す関数はfd_canでもブロックされ得る,要改良 */ len = pipe_blocked_read_message(fd, header,dbuf[head].buf,buf_size); #if 1 { static int c=0; d3_printf(" (%3d): len=%d\n", c++, len); } #endif if (len > 0) { dbuf[head].len = len; dbuf[head].ts=pipe_get_timestamp(header); d3_printf(" TS%8x\n",dbuf[head].ts); head = BUFFER_NEXT(head); } #if 0 else if (len < 0) { dbuf[head].len = -2; /* end marker */ head = BUFFER_NEXT(head); //sock_close(); } else { /* len == 0 */ dbuf[head].len = 0; /* ????? */ head = BUFFER_NEXT(head); break; } #else else if(len==PIPE_END || len==PIPE_ERROR){ len=0; dbuf[head].len = -1; /* end marker */ head = BUFFER_NEXT(head); break; } #endif }
static int sym_lookup(char *lb, struct s_table st[], char **val) { int i, k = 0; // skip white space while (lb[k] && isspace(lb[k])) k++; // check if it's a comment line if (lb[k] == '#' || !lb[k]) return -2; // search the table for (i=0; i<N_REC_SYMBOLS; i++) { if (strncmp(st[i].sym, lb+k, strlen(st[i].sym))) continue; while (lb[k] && !isspace(lb[k])) k++; while (lb[k] && isspace(lb[k])) k++; *val = lb + k; // d3_printf("val: %s\n", *val); return st[i].val; } // symbol not found d3_printf("Not found\n"); return -1; }
/* tell the guider to use the star at x,y as a guide target */ void guider_set_target(struct guider *guider, struct ccd_frame *fr, struct gui_star *gs) { double dx, dy, derr; int ret; ret = guide_star_position_centroid(fr, gs->x, gs->y, &dx, &dy, &derr); d3_printf("spc dx:%.3f dy:%.3f derr:%.3f ret:%d\n", dx, dy, derr, ret); if (ret >= 0) { guider->xbias = dx; guider->ybias = dy; } else { guider->xbias = 0; guider->ybias = 0; } guider->state |= GUIDER_TARGET_SET; if (guider->gs) gui_star_release(guider->gs); guider->gs = gs; gui_star_ref(gs); guider->xtgt = gs->x; guider->ytgt = gs->y; }
void paint_from_rgb_cache(GtkWidget *widget, struct map_cache *cache, GdkRectangle *area) { unsigned char *dat; if (!area_in_cache(area, cache)) { err_printf("paint_from_cache: oops - area not in cache\n"); d3_printf("area is %d by %d starting at %d,%d\n", area->width, area->height, area->x, area->y); d3_printf("cache is %d by %d starting at %d,%d\n", cache->w, cache->h, cache->x, cache->y); // return; } dat = cache->dat + (area->x - cache->x + (area->y - cache->y) * cache->w) * 3; gdk_draw_rgb_image (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], area->x, area->y, area->width, area->height, GDK_RGB_DITHER_MAX, dat, cache->w*3); }
/* print the token in a friendly form */ void print_token(GScanner * scan, GTokenType tok) { if (tok < G_TOKEN_NONE) d3_printf("tok: %c\n", tok); else { if (tok == G_TOKEN_STRING) d3_printf("tok: string val: %s\n", g_scanner_cur_value(scan).v_string); else if (tok == G_TOKEN_IDENTIFIER) d3_printf("tok: ident val: %s\n", g_scanner_cur_value(scan).v_string); else if (tok == G_TOKEN_SYMBOL) d3_printf("tok: symbol [%s] val: %d\n", symname[g_scanner_cur_value(scan).v_int], (int)g_scanner_cur_value(scan).v_int); else if (tok == G_TOKEN_INT) d3_printf("tok: int val: %d\n", (int)g_scanner_cur_value(scan).v_int); else if (tok == G_TOKEN_FLOAT) d3_printf("tok: float val: %f\n", g_scanner_cur_value(scan).v_float); else d3_printf("tok: %d\n", tok); } }
/* Search the frame for suitable guide star * Return 0 if found, -1 for an error. update x & y * with the star's coordinates if found. */ struct gui_star *detect_guide_star(struct ccd_frame *fr, struct region *reg) { struct gui_star *gs; struct sources *src; double score, max_score = 0; int gsi = 0, i = 0; double xc, yc, ds; //rf // double ref_flux = 0.0, ref_fwhm = 0.0; src = new_sources(P_INT(SD_MAX_STARS)); if (src == NULL) { err_printf("detect_guide_star: cannot create sources\n"); return NULL; } // rf = (fr->w * fr->w + fr->h * fr->h) / PI / 2; xc = fr->w / 2; yc = fr->h / 2; extract_stars(fr, reg, 0, P_DBL(SD_SNR), src); if (src->ns <= 0) return NULL; for (i = 0; i < src->ns; i++) { if (src->s[i].peak > P_DBL(AP_SATURATION)) continue; // ref_flux = src->s[i].flux; // ref_fwhm = src->s[i].fwhm; break; } for (i = 0; i < src->ns; i++) { if (src->s[i].peak > P_DBL(AP_SATURATION)) continue; xc = cos((src->s[i].x - fr->w / 2) / fr->w * PI); yc = cos((src->s[i].y - fr->h / 2) / fr->h * PI); ds = sqr(xc * yc); score = src->s[i].flux * ds; d3_printf("%5.1f %5.1f ds: %.3f score: %.1f\n", src->s[i].x, src->s[i].y, ds, score); if (score > max_score) { max_score = score; gsi = i; } } gs = gui_star_new(); gs->x = src->s[gsi].x; gs->y = src->s[gsi].y; gs->size = 1.0 * P_INT(DO_DEFAULT_STAR_SZ); gs->flags = STAR_TYPE_ALIGN; gs->s = (struct star *)g_malloc(sizeof(struct star)); *STAR(gs->s) = src->s[gsi]; STAR(gs->s)->ref_count = 1; release_sources(src); return gs; }
static void wait4rtdisplay(struct timeval *tv_start,u_int64_t ts_limit){ int64_t ts_diff; struct timeval now; while(1){ gettimeofday(&now, NULL); ts_diff=timeval_sub_ts(&now,tv_start,OPT.clock); if(ts_diff >= ts_limit){ break; } d3_printf("sleep.\n"); decoder_buf_read(); /* check new data */ } }
ssize_t pipe_blocked_read_packet_ex(PIPE_CONTEXT pp, void *hd, void *payload) { TRUE_PIPE_CONTEXT p=(TRUE_PIPE_CONTEXT)pp; int length,ret; int len_sum; if(p->num_left==0){ len_sum=0; }else{ length=fd_read_ignoreEOF(p->fd,payload ,MIN(p->num_left,p->packet_len)); if(length==-1) return PIPE_ERROR; p->num_left-=length; len_sum=length; } /* Now, num_left==0 or len_sum==p->packet_len*/ while(!p->flag_marker && len_sum < p->packet_len){ if ((ret = pipe_blocked_read_header(p->fd,p->header))<0) return ret; p->num_left=pipe_get_length(p->header); p->flag_marker=pipe_get_marker(p->header); d3_printf("pbrpe: read pipe header num_left:%d marker:%d.\n" ,p->num_left,p->flag_marker); length=fd_read_ignoreEOF(p->fd,payload+len_sum ,MIN(p->num_left,p->packet_len-len_sum)); if(length==-1) return PIPE_ERROR; p->num_left-=length; len_sum+=length; } /* fill PIPE header */ memset(hd, 0, PIPE_HEADER_LEN); pipe_set_version(hd,1); pipe_set_timestamp(hd,pipe_get_timestamp(p->header)); pipe_set_length(hd,len_sum); if(p->flag_marker&&p->num_left==0){ pipe_set_marker(hd,1); p->flag_marker=0; } return len_sum; }
ssize_t pipe_blocked_read_block(int fd, void *hd, void *payload, int maxlen) { ssize_t io_ret, left_size, ret; char *pos = (char *) payload; if ((ret = pipe_blocked_read_header(fd,hd))<0) return ret; left_size = pipe_get_length(hd); d3_printf("left_size=%d\n", left_size); if(left_size > maxlen) { d_printf("Length over %d(must be less than %d), skip.\n" ,left_size,maxlen); pipe_set_length(hd,0); io_ret=fd_lseek_ignoreEOF(fd,left_size); if (io_ret < 0) return PIPE_ERROR; }else{ io_ret=fd_read_ignoreEOF(fd,pos,left_size); if (io_ret < 0) return PIPE_ERROR; } return pipe_get_length(hd); }
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; }
int main(int argc, char *argv[]) { #ifndef RTPESND char p_header[PIPE_HEADER_LEN]; #endif RTPSEND char r_header[RTP_HEADER_LEN]; char *buffer; struct iovec iov[3]; ssize_t length; u_int16_t seq; PIPE_CONTEXT p; int dummy_cnt; /* dummy(very old packet)cnt */ #ifdef RTPSEND int count=0; /* for Debug */ struct timeval blk_start_tv; int blk_wait4start=1; int blk_pktcnt = 0; u_int32_t waitperframe; #endif sigset_t sigset; ssize_t io_ret; /* sigfillset(&sigset); */ /* sigdelset(&sigset,SIGINT); */ /* sigdelset(&sigset,SIGTSTP); */ if (sigprocmask(SIG_BLOCK,&sigset,NULL)!=0){ e_printf("sigprocmask fail.\n"); exit(0); } signal(SIGINT,cb_sig); signal(SIGTSTP,cb_sig); signal(SIGPIPE,cb_sig); if (opt_etc(argc, argv) == -1) { return -1; } if (isatty(STDIN)) { e_printf("Standard input must be binded with pipe.\n"); return -1; } #ifndef RTPSEND if (isatty(STDOUT)) { e_printf("Standard output must be binded with pipe.\n"); return -1; } #endif /* ! RTPSEND */ if ((buffer = malloc(OPT.plen)) == NULL) { e_printf("cannot malloc for buffer\n"); return -1; } if (OPT.compatible_mode) { seq = 0; } else { srand(time(NULL)); seq = rand() & 0xffff; } d1_printf("first seq/rtp=%u\n",seq); rtp_reset(r_header, RTP_HEADER_LEN); rtp_set_version(r_header, 1); rtp_set_ptype(r_header, OPT.payload_type); memset(buffer, 0, OPT.plen); #ifdef RTPSEND switch(OPT.shaping_lev) { case 0: default: OPT.shaping_lev=0; waitperframe=1; d1_printf("shaping_mode : frame\n"); break; case 1: waitperframe=OPT.rs_N; d1_printf("shaping_mode : packet with dups\n"); break; case 2: waitperframe=OPT.rs_N*OPT.send_double; d1_printf("shaping_mode : all packet(include dups)\n"); break; } iov[0].iov_base = r_header; iov[0].iov_len = RTP_HEADER_LEN; iov[1].iov_base = buffer; /* iov[1].iov_len = OPT.plen;*/ #else /* RTPSEND */ iov[0].iov_base = p_header; iov[0].iov_len = PIPE_HEADER_LEN; iov[1].iov_base = r_header; iov[1].iov_len = RTP_HEADER_LEN; iov[2].iov_base = buffer; /* iov[2].iov_len = OPT.plen;*/ #endif /* RTPSEND */ p=pipe_context_init(STDIN,1 /*OPT.rs_N*/, OPT.plen); wclk_ps_tvstart.tv_sec=0; while ((length = pipe_blocked_read_packet_ex(p, p_header, buffer))>=0){ #ifdef RTPSEND if(wclk_ps_tvstart.tv_sec==0){ /* recode start time */ gettimeofday(&wclk_ps_tvstart, NULL); } count++; if(blk_wait4start){ gettimeofday(&blk_start_tv,NULL); blk_wait4start=0; blk_pktcnt = 0; }else{ blk_pktcnt++; } blk_wait4start=pipe_get_marker(p_header); #endif #ifdef RTPSEND iov[1].iov_len =length; pipe_set_length(p_header,length); #else iov[2].iov_len =length; pipe_set_length(p_header,length + RTP_HEADER_LEN); #endif /* RTPSEND*/ rtp_set_timestamp(r_header, pipe_get_timestamp(p_header)); rtp_set_marker(r_header, pipe_get_marker(p_header)); rtp_set_seqnum(r_header, seq++); if(seq>=0x10000) seq-=0x10000; d3_printf("rtpenc: marker %d : seq %d\n",pipe_get_marker(p_header),seq-1); #ifdef RTPSEND if (display_time_all_packet) { d_printf("\n%d:", seq); } rtsend_tslimit+=(u_int32_t)(pipe_get_timestamp(p_header) - pkt_tslast); pkt_tslast=pipe_get_timestamp(p_header); #if 0 wait4rtsend(&wclk_tvstart,blk_pktcnt); #else if(!(OPT.shaping_lev==0 && blk_pktcnt>=1)){ wait4rtsend(&wclk_tvstart,&rtsend_tslimit,blk_pktcnt,waitperframe); } #endif if (OPT.send_err_rate > 0) { if ((rand() % OPT.send_err_rate) == 0) { total_drop++; continue; } } d2_printf(" write %d bytes", pipe_get_length(p_header)); if (writev(OPT.sfd, iov, 2) == -1) { total_drop++; d2_printf(" writev-NG(ts/sq=%d/%d)\n", pipe_get_timestamp(p_header), count); } else { d2_printf(" writev-OK(ts/sq=%d/%d)\n", pipe_get_timestamp(p_header), count); total_send++; } if(OPT.dup2dummy){ rtp_set_seqnum(r_header, (seq+0x10000-0x100)&0xffff); } for(dummy_cnt=1;dummy_cnt<OPT.send_double;dummy_cnt++){ if(OPT.shaping_lev==2){ blk_pktcnt++; wait4rtsend(&wclk_tvstart,&rtsend_tslimit,blk_pktcnt,waitperframe); } d2_printf(" write dup(%d/%d) \n",dummy_cnt,OPT.send_double); io_ret=writev(OPT.sfd, iov, 2); if ( io_ret == -1) { total_drop++; d2_printf(" writev-NG(ts/sq=%d/%d)\n", pipe_get_timestamp(p_header), count); } else { d2_printf(" writev-OK(ts/sq=%d/%d)\n", pipe_get_timestamp(p_header), count); total_send++; } } #else /* RTPSEND */ d2_printf(" write pkt\n"); writev(STDOUT, iov, 3); if(OPT.dup2dummy){ rtp_set_seqnum(r_header, (seq+0x10000-0x100)&0xffff); } for(dummy_cnt=1;dummy_cnt<OPT.send_double;dummy_cnt++){ d2_printf(" write dup(%d/%d) \n",dummy_cnt,OPT.send_double); io_ret=writev(STDOUT, iov, 3); if(io_ret==-1){ exit_req=1; break; } } #endif /* RTPSEND */ if(exit_req==1){ break; } } #ifdef RTPSEND last_seq=seq-1; statistics_display(); sleep(1); #endif return 0; }
int capt_capt(opt_t *opt) { int frame_size; unsigned int frame_num; unsigned char *frame_buf_yuv, *frame_buf_rgb, *jpeg_buf; struct timeval start; const int fps = opt->fps_opt; const int wait_flag = (opt->fps_opt < opt->fps_dev); const int w = opt->width, h = opt->height; int q = 75; /* jpeg compress quality */ int len; char mh[PIPE_HEADER_LEN]; /* EMON system Message Header */ unsigned int ts; /* timestamp in Message Header */ frame_size = mchip_hsize() * mchip_vsize() * 3; frame_buf_yuv = (unsigned char*)malloc(frame_size); frame_buf_rgb = (unsigned char*)malloc(frame_size); jpeg_buf = (unsigned char*)malloc(frame_size); /* allocate enougth memory for jpeg_buf */ if (frame_buf_yuv == NULL || frame_buf_rgb == NULL ||jpeg_buf == NULL){ e_printf("cannot malloc for frame_buf or jpeg_buf\n"); return -1; } stat_init(&STAT); ts = rand() * opt->freq; mchip_continuous_start(); gettimeofday(&start, NULL); STAT.start = start; /* copy struct timeval */ d2_printf("\njpegcapt: %ld.%06ld: wait_flag=%d", start.tv_sec, start.tv_usec, wait_flag); for (frame_num = 0; opt->max_frame == 0 || frame_num < opt->max_frame; frame_num++, ts += opt->freq){ struct timeval c, b, a; /* capture, before(encoding), after */ int d1, d2; if (debug_level > 0 && (frame_num % opt->stat_freq)== 0){ stat_print(&STAT, frame_num); } if (wait_proper_time(&start, fps, frame_num, 1) < 0){ STAT.skip_count++; continue; /* skip capture because it's too late */ } STAT.capt_count++; gettimeofday(&c, NULL); mchip_continuous_read(frame_buf_yuv, mchip_hsize()*mchip_vsize()*2); yuv_convert(frame_buf_yuv, frame_buf_rgb, mchip_hsize(), mchip_vsize()); gettimeofday(&b, NULL); len = jpeg_encode(frame_buf_rgb, jpeg_buf, w, h, q); gettimeofday(&a, NULL); d1 = timeval_diff_usec(&b, &c); d2 = timeval_diff_usec(&a, &b); timeval_add_usec(&STAT.capt_total, d1); timeval_add_usec(&STAT.jpgenc_total, d2); d3_printf("\n frame=%d, ts=%d, jpg_len=%d, q=%d" ", t1=%d, t2=%d", frame_num, ts, len, q, d1, d2); if (len > opt->dsize ){ q *= 0.75; continue; /* skip this picture */ }else if (len < opt->dsize * 0.9 && q < 90) { q++; } bzero(&mh, PIPE_HEADER_LEN); pipe_set_version(&mh, 1); pipe_set_marker(&mh, 1); pipe_set_length(&mh, len); pipe_set_timestamp(&mh, ts); if (pipe_blocked_write_block(STDOUT, &mh, jpeg_buf) ==PIPE_ERROR){ d1_printf("\npipe_blocked_write_block error!!" "len=%d, ts=%ud", len, ts); } else { STAT.out_count++; } } if (debug_level > 0){ stat_print(&STAT, frame_num); } return 0; }
/* * update the cache so that it contains a representation of the given area */ static void update_cache(struct map_cache *cache, struct map_geometry *geom, struct image_channel *channel, GdkRectangle *area) { struct ccd_frame *fr; int fx, fy, fw, fh; int zoom_in = 1; int zoom_out = 1; if (geom->zoom > 1.0 && geom->zoom <= 16.0) { zoom_in = floor(geom->zoom + 0.5); } else if (geom->zoom < 1.0 && geom->zoom >= (1.0 / 16.0)) { zoom_out = floor(1.0 / geom->zoom + 0.5); } cache->zoom = geom->zoom; fr = channel->fr; if (cached_area_size(area, cache) > cache->size) { /* free the cache and realloc */ d3_printf("freeing old cache\n"); free(cache->dat); cache->dat = NULL; } if (cache->dat == NULL) { /* we need to alloc (new) data area */ void *dat; if (channel->color) cache->type = MAP_CACHE_RGB; else cache->type = MAP_CACHE_GRAY; cache->size = cached_area_size(area, cache); dat = malloc(cache->size); if (dat == NULL) { err_printf("update cache: alloc error\n"); return ; } cache->dat = dat; } // d3_printf("expose area is %d by %d starting at %d, %d\n", // area->width, area->height, area->x, area->y); /* calculate the frame coords for the exposed area */ fx = area->x * zoom_out / zoom_in; fy = area->y * zoom_out / zoom_in; fw = area->width * zoom_out / zoom_in + (zoom_in > 1 ? 2 : 0); fh = area->height * zoom_out / zoom_in + (zoom_in > 1 ? 2 : 0); if (fx > fr->w - 1) fx = fr->w - 1; if (fx + fw > fr->w - 1) fw = fr->w - fx; if (fy > fr->h - 1) fy = fr->h - 1; if (fy + fh > fr->h - 1) fh = fr->h - fy; // d3_printf("frame region: %d by %d at %d, %d\n", fw, fh, fx, fy); /* and now to the actual cache rendering. We call different functions for each frame format / zoom mode combination */ if (zoom_out > 1) cache_render_float_zo(cache, channel, zoom_out, fx, fy, fw, fh); else cache_render_float_zi(cache, channel, zoom_in, fx, fy, fw, fh); // d3_printf("cache area is %d by %d starting at %d, %d\n", // cache->w, cache->h, cache->x, cache->y); }