/* 绘制屏幕上的内容。 * 注意程序在绘图之前调用了prepare_buffer,结束前调用了display_buffer。 * prepare_buffer会准备一个空白的绘图缓冲区,display_buffer则会将缓冲区绘制到屏幕上, * draw_pixel或draw_string绘制的内容将保存在缓冲区内(暂时不会显示在屏幕上),调用 * display_buffer后才会显示。 */ void redraw_screen() { fly_t it; const char *hit, *miss; prepare_buffer(); /* 准备缓冲区 */ /* 绘制每个字符 */ for (it = characters(); it != NULL; it = it->_next) { static char buf[2]; buf[0] = it->text + 'A'; buf[1] = 0; draw_string(buf, F2int(it->x), it->y, 15); } /* 绘制命中数、miss数、最后一次按键扫描码和fps */ draw_string(itoa(last_key_code()), SCR_HEIGHT - 8, 0, 48); hit = itoa(get_hit()); draw_string(hit, 0, SCR_WIDTH - strlen(hit) * 8, 10); miss = itoa(get_miss()); draw_string(miss, SCR_HEIGHT - 8, SCR_WIDTH - strlen(miss) * 8, 12); draw_string(itoa(get_fps()), 0, 0, 14); draw_string("FPS", 0, strlen(itoa(get_fps())) * 8, 14); display_buffer(); /* 绘制缓冲区 */ }
void SDL_UpdateRect(SDL_Surface *screen, int x, int y, int w, int h) { assert(screen); assert(screen->pitch == 320); if(screen->flags & SDL_HWSURFACE) { if(x == 0 && y == 0) { /* Draw FPS */ vmem = VMEM_ADDR; char buf[80]; sprintf(buf, "%dFPS", get_fps()); draw_string(buf, 0, 0, 10); } return; } /* TODO: Copy the pixels in the rectangle area to the screen. */ // assert(0); int i; uint8_t *dst = (void*)0xa0000 + x + y * 320; uint8_t *src = screen->pixels + x + y * screen->w; for(i = 0; i < h; i++) { memcpy(dst, src, w); src += screen->w; dst += 320; } }
static int m5mols_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms) { struct m5mols_info *info = to_m5mols(sd); struct v4l2_captureparm *cp = &parms->parm.capture; int ret = -EINVAL; if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL; ret = m5mols_set_mode_backup(sd, MODE_PARMSET); if (!ret) ret = get_fps(sd, cp); /* set right FPS to denominator. */ if (!ret) ret = i2c_w8_param(sd, CAT1_MONITOR_FPS, m5mols_reg_fps[cp->timeperframe.denominator]); if (!ret) ret = m5mols_set_mode_restore(sd); if (!ret) { cp->capability = V4L2_CAP_TIMEPERFRAME; info->tpf = cp->timeperframe; } v4l2msg("denominator: %d / numerator: %d.\n", cp->timeperframe.denominator, cp->timeperframe.numerator); return ret; }
inline void CvCapture_FFMPEG::seek(s64 _frame_number) { _frame_number = std::min(_frame_number, get_total_frames()); int delta = 16; // if we have not grabbed a single frame before first seek, let's read the first frame // and get some valuable information during the process if( first_frame_number < 0 && get_total_frames() > 1 ) grabFrame(); for(;;) { s64 _frame_number_temp = std::max(_frame_number-delta, (s64)0); double sec = (double)_frame_number_temp / get_fps(); s64 time_stamp = ic->streams[video_stream]->start_time; double time_base = r2d(ic->streams[video_stream]->time_base); time_stamp += (s64)(sec / time_base + 0.5); if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD); avcodec_flush_buffers(ic->streams[video_stream]->codec); if( _frame_number > 0 ) { grabFrame(); if( _frame_number > 1 ) { frame_number = dts_to_frame_number(picture_pts) - first_frame_number; //printf("_frame_number = %d, frame_number = %d, delta = %d\n", // (int)_frame_number, (int)frame_number, delta); if( frame_number < 0 || frame_number > _frame_number-1 ) { if( _frame_number_temp == 0 || delta >= INT_MAX/4 ) break; delta = delta < 16 ? delta*2 : delta*3/2; continue; } while( frame_number < _frame_number-1 ) { if(!grabFrame()) break; } frame_number++; break; } else { frame_number = 1; break; } } else { frame_number = 0; break; } } }
int64_t CvCapture_FFMPEG::get_total_frames() { int64_t nbf = ic->streams[video_stream]->nb_frames; if (nbf == 0) { nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5); } return nbf; }
inline s64 CvCapture_FFMPEG::get_total_frames() { s64 nbf = ic->streams[video_stream]->nb_frames; if (nbf == 0) { nbf = (s64)floor(get_duration_sec() * get_fps() + 0.5); } return nbf; }
static void redraw(void) { int w = al_get_display_width(example.display); int h = al_get_display_height(example.display); int i; int f1, f2; int fh = al_get_font_line_height(example.font); char const *info[] = {"textures", "memory buffers"}; char const *binfo[] = {"alpha", "additive", "tinted", "solid"}; ALLEGRO_COLOR tint = example.white; if (example.blending == 0) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); tint = example.half_white; } else if (example.blending == 1) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE); tint = example.dark; } else if (example.blending == 2) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); tint = example.red; } else if (example.blending == 3) al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); for (i = 0; i < example.sprite_count; i++) { Sprite *s = example.sprites + i; al_draw_tinted_bitmap(example.bitmap, tint, s->x, s->y, 0); } al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); if (example.show_help) { for (i = 0; i < 5; i++) al_draw_text(example.font, example.white, 0, h - 10 * fh + i * fh * 2 + fh * 0.5, 0, text[i]); } al_draw_textf(example.font, example.white, 0, 0, 0, "count: %d", example.sprite_count); al_draw_textf(example.font, example.white, 0, fh, 0, "size: %d", example.bitmap_size); al_draw_textf(example.font, example.white, 0, fh * 2, 0, "%s", info[example.use_memory_bitmaps]); al_draw_textf(example.font, example.white, 0, fh * 3, 0, "%s", binfo[example.blending]); get_fps(&f1, &f2); al_draw_textf(example.font, example.white, w, 0, ALLEGRO_ALIGN_RIGHT, "FPS: %4d +- %-4d", f1, f2); al_draw_textf(example.font, example.white, w, fh, ALLEGRO_ALIGN_RIGHT, "%4d / sec", (int)(1.0 / example.direct_speed_measure)); }
void SDL_UpdateRect(SDL_Surface *screen, int x, int y, int w, int h) { assert(screen); assert(screen->pitch == 320); if(screen->flags & SDL_HWSURFACE) { if(x == 0 && y == 0) { /* Draw FPS */ vmem = VMEM_ADDR; char buf[80]; sprintf(buf, "%dFPS", get_fps()); draw_string(buf, 0, 0, 10); } return; } /* TODO: Copy the pixels in the rectangle area to the screen. */ assert(0); }
void read_pcs_start (parser_state_t *ps, sup_header_t h) { sup_pcs_start_t pcss; sup_pcs_start_obj_t pcsso; int fps_num, fps_den, i; safe_read(&pcss, sizeof(pcss), ps->fh, "PCSS structure"); conv_sup_pcs_start(&pcss); if (pcss.m != 0) die(ps->fh, sizeof(pcss), "Invalid PCSS magic."); printf("PCS start\n"); printf("\tframe width = %u\n", pcss.width); printf("\tframe height = %u\n", pcss.height); if (!get_fps(pcss.fps_id, &fps_num, &fps_den)) die(ps->fh, sizeof(pcss), "Invalid FPS ID in PCSS."); printf("\tfps id = %u (%u/%u)\n", pcss.fps_id, fps_num, fps_den); printf("\tcomposition = %u\n", pcss.comp_num); printf("\tfollower = 0x%02X (%s)\n", pcss.follower, pcss.follower == 0x80 ? "no" : "within 2f"); printf("\tobjects = %u\n", pcss.objects); if (pcss.objects > 2) die(ps->fh, sizeof(pcss), "Invalid number of objects (must be 1 or 2)."); for (i = 0; i < pcss.objects; i++) { safe_read(&pcsso, sizeof(pcsso), ps->fh, "PCSSO structure"); conv_sup_pcs_start_obj(&pcsso); if (pcsso.window != 0 && pcsso.window != 1) die(ps->fh, sizeof(pcsso), "Invalid window id in PCSS object."); printf("\tObject %u\n", i + 1); if (pcsso.forced && (pcsso.forced != 64)) die(ps->fh, sizeof(pcsso), "Invalid forced flag in PCSS object."); printf("\t\tpicture = %u\n", pcsso.picture); printf("\t\twindow = %u\n", pcsso.window); printf("\t\tforced = %u\n", pcsso.forced); printf("\t\tx offset = %u\n", pcsso.x_off); printf("\t\ty offset = %u\n", pcsso.y_off); } }
int ff_vbv_update(MpegEncContext *s, int frame_size) { RateControlContext *rcc = &s->rc_context; const double fps = get_fps(s->avctx); const int buffer_size = s->avctx->rc_buffer_size; const double min_rate = s->avctx->rc_min_rate / fps; const double max_rate = s->avctx->rc_max_rate / fps; av_dlog(s, "%d %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate); if (buffer_size) { int left; rcc->buffer_index -= frame_size; if (rcc->buffer_index < 0) { av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n"); if (frame_size > max_rate && s->qscale == s->avctx->qmax) { av_log(s->avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n"); } rcc->buffer_index = 0; } left = buffer_size - rcc->buffer_index - 1; rcc->buffer_index += av_clip(left, min_rate, max_rate); if (rcc->buffer_index > buffer_size) { int stuffing = ceil((rcc->buffer_index - buffer_size) / 8); if (stuffing < 4 && s->codec_id == AV_CODEC_ID_MPEG4) stuffing = 4; rcc->buffer_index -= 8 * stuffing; if (s->avctx->debug & FF_DEBUG_RC) av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing); return stuffing; } } return 0; }
void read_pcs_end (parser_state_t *ps, sup_header_t h) { sup_pcs_end_t pcse; int fps_num, fps_den; safe_read(&pcse, sizeof(pcse), ps->fh, "PCSE structure"); conv_sup_pcs_end(&pcse); if (pcse.m != 0) die(ps->fh, sizeof(pcse), "Invalid PCSE magic."); printf("PCS end\n"); printf("\tsubtitle width = %u\n", pcse.width); printf("\tsubtitle height = %u\n", pcse.height); if (!get_fps(pcse.fps_id, &fps_num, &fps_den)) die(ps->fh, sizeof(pcse), "Invalid FPS ID in PCSE."); printf("\tfps id = %u (%u/%u)\n", pcse.fps_id, fps_num, fps_den); printf("\tcomposition = %u\n", pcse.comp_num); printf("\tStats\n\t\ttot_ob_size = %u\n\t\timages = %u\n\t\tpalettes = %u\n\t\tdiff_palettes = %u\n", ps->total_object_sizes, ps->images, ps->palettes, ps->diff_palettes); clear_stats(ps); clear_palette(ps); }
inline double CvCapture_FFMPEG::getProperty( int property_id ) { if( !video_st ) return 0; switch( property_id ) { case CV_FFMPEG_CAP_PROP_POS_MSEC: return 1000.0*(double)frame_number/get_fps(); case CV_FFMPEG_CAP_PROP_POS_FRAMES: return (double)frame_number; case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO: return r2d(ic->streams[video_stream]->time_base); case CV_FFMPEG_CAP_PROP_FRAME_COUNT: return (double)get_total_frames(); case CV_FFMPEG_CAP_PROP_FRAME_WIDTH: return (double)frame.width; case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT: return (double)frame.height; case CV_FFMPEG_CAP_PROP_FPS: #if LIBAVCODEC_BUILD > 4753 return av_q2d(video_st->r_frame_rate); #else return (double)video_st->codec.frame_rate / (double)video_st->codec.frame_rate_base; #endif case CV_FFMPEG_CAP_PROP_FOURCC: #if LIBAVFORMAT_BUILD > 4628 return (double)video_st->codec->codec_tag; #else return (double)video_st->codec.codec_tag; #endif default: break; } return 0; }
inline void CvCapture_FFMPEG::seek(double sec) { seek((s64)(sec * get_fps() + 0.5)); }
void nextFrame(void){ float adjust = (100.0/get_fps()); /* Framerate Correction */ /* Player Constants */ ship.max_vel=5; ship.acceleration=0.01*adjust; ship.max_rotation_velocity=1; ship.rotation_acceleration=0.025*adjust; ship.rotation-=ship.rotation>360?-360:0; ship.rotation+=ship.rotation<0?360:0; if(ship.rotation_direction==1){ rot_plus+=1.0*adjust; if(rot_plus>30) rot_plus=30; }else if(ship.rotation_direction==-1){ rot_plus-=1.0*adjust; if(rot_plus<-30) rot_plus=-30; }else{ if(rot_plus>0){ rot_plus-=1.0*adjust; if(rot_plus<0) rot_plus=0; }else if(rot_plus<0){ rot_plus+=1.0*adjust; if(rot_plus>0) rot_plus=0; } } if(game_end)return; /* Player Movement */ ship.x+=ship.vel*sin(ship.rotation*degtorad)*adjust; ship.y=getheight(-ship.x,-ship.z)+YSHIFT; if(ship.y>YSHIFT+50){ game_end=1; return; } ship.z+=ship.vel*cos(ship.rotation*degtorad)*adjust; ship.rotation+=ship.rotation_velocity*adjust; /* Velocity Calculations */ if(ship.direction < 0){ ship.vel-=ship.acceleration; ship.vel =ship.vel<-ship.max_vel?-ship.max_vel:ship.vel; }else{ ship.vel+=3*ship.acceleration; ship.vel =ship.vel>0?0:ship.vel; } if(ship.rotation_direction>0){ ship.rotation_velocity+=ship.rotation_acceleration; if(ship.rotation_velocity>ship.max_rotation_velocity) ship.rotation_velocity=ship.max_rotation_velocity; }else if(ship.rotation_direction<0){ ship.rotation_velocity-=ship.rotation_acceleration; if(ship.rotation_velocity<-ship.max_rotation_velocity) ship.rotation_velocity=-ship.max_rotation_velocity; }else{ if(ship.rotation_velocity > 0){ ship.rotation_velocity -= ship.rotation_acceleration; if(ship.rotation_velocity<0) ship.rotation_velocity=0; }else if(ship.rotation_velocity < 0){ ship.rotation_velocity += ship.rotation_acceleration; if(ship.rotation_velocity>0) ship.rotation_velocity=0; } } if(game_start)score+=ship.direction?1*adjust:-2*adjust; }
int main(int argc, char *argv[]) { int time; char method[SCREEN_UPDATE_STR_LEN]; char fps_text[16]; if (argc > 1) { if (strstr(argv[1], "help")) { printf("Type \"kwestkingdom\" to play!\n"); exit(0); } } if (!init_kwestkingdom()) { return 1; } init_engine(); /** * Reset the timers just before the game begins. */ reset_timer(); /** * BEGIN MAIN LOOP */ while (!quitting) { while (get_ticks() == 0) { rest(1); } while (get_ticks() > 0) { time = get_ticks(); /** * UPDATE */ update_engine(); decrease_timer(); if (time <= get_ticks()) { break; } } /** * DRAW */ paint_engine(grab_canvas()); /** * Show FPS */ sprintf(fps_text, "FPS %d", get_fps()); show_text(grab_canvas(), fps_text, 10, 10, WHITE, BLACK); /** * Show the method of screen updating. */ find_screen_update_method(method); show_text( grab_canvas(), method, grab_tile_size() / 5, /* x */ grab_canvas_height() - (grab_tile_size() / 2), /* y */ WHITE, BLACK ); /** * Show all of the new screen changes */ refresh_screen(); mark_frame_complete(); } /** * END MAIN LOOP */ /** * Cleanup */ stop_engine(); stop_screen(); stop_resources(); check_memory(); return 0; }
static void draw(void) { float x, y; int iw = al_get_bitmap_width(ex.pattern); int ih = al_get_bitmap_height(ex.pattern); ALLEGRO_BITMAP *screen, *temp; ALLEGRO_LOCKED_REGION *lock; void *data; int size, i, format; al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); al_clear_to_color(ex.background); screen = al_get_target_bitmap(); set_xy(8, 8); /* Test 1. */ /* Disabled: drawing to same bitmap is not supported. */ /* print("Screen -> Screen (%.1f fps)", get_fps(0)); get_xy(&x, &y); al_draw_bitmap(ex.pattern, x, y, 0); start_timer(0); al_draw_bitmap_region(screen, x, y, iw, ih, x + 8 + iw, y, 0); stop_timer(0); set_xy(x, y + ih); */ /* Test 2. */ print("Screen -> Bitmap -> Screen (%.1f fps)", get_fps(1)); get_xy(&x, &y); al_draw_bitmap(ex.pattern, x, y, 0); temp = al_create_bitmap(iw, ih); al_set_target_bitmap(temp); al_clear_to_color(al_map_rgba_f(1, 0, 0, 1)); start_timer(1); al_draw_bitmap_region(screen, x, y, iw, ih, 0, 0, 0); al_set_target_bitmap(screen); al_draw_bitmap(temp, x + 8 + iw, y, 0); stop_timer(1); set_xy(x, y + ih); al_destroy_bitmap(temp); /* Test 3. */ print("Screen -> Memory -> Screen (%.1f fps)", get_fps(2)); get_xy(&x, &y); al_draw_bitmap(ex.pattern, x, y, 0); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); temp = al_create_bitmap(iw, ih); al_set_target_bitmap(temp); al_clear_to_color(al_map_rgba_f(1, 0, 0, 1)); start_timer(2); al_draw_bitmap_region(screen, x, y, iw, ih, 0, 0, 0); al_set_target_bitmap(screen); al_draw_bitmap(temp, x + 8 + iw, y, 0); stop_timer(2); set_xy(x, y + ih); al_destroy_bitmap(temp); al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); /* Test 4. */ print("Screen -> Locked -> Screen (%.1f fps)", get_fps(3)); get_xy(&x, &y); al_draw_bitmap(ex.pattern, x, y, 0); start_timer(3); lock = al_lock_bitmap_region(screen, x, y, iw, ih, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY); format = lock->format; size = lock->pixel_size; data = malloc(size * iw * ih); for (i = 0; i < ih; i++) memcpy((char*)data + i * size * iw, (char*)lock->data + i * lock->pitch, size * iw); al_unlock_bitmap(screen); lock = al_lock_bitmap_region(screen, x + 8 + iw, y, iw, ih, format, ALLEGRO_LOCK_WRITEONLY); for (i = 0; i < ih; i++) memcpy((char*)lock->data + i * lock->pitch, (char*)data + i * size * iw, size * iw); al_unlock_bitmap(screen); free(data); stop_timer(3); set_xy(x, y + ih); }
int Game::on_frame(void) { // Handle OS events float delta_x = 0.0f; float delta_y = 0.0f; const SystemEvent* event = app_pop_event(); while (event) { switch(event->type) { case kEventResize: _render->resize(event->data.resize.width, event->data.resize.height); printf("W: %d H: %d\n", event->data.resize.width, event->data.resize.height); break; case kEventKeyDown: if(event->data.key == KEY_ESCAPE) return 1; if(event->data.key == KEY_F1) _render->toggle_debug_graphics(); if(event->data.key == KEY_F2) _render->toggle_deferred(); break; case kEventMouseDown: if(event->data.mouse.button == MOUSE_LEFT) app_lock_and_hide_cursor(); debug_output("Mouse: %.0f %.0f\n", event->data.mouse.x, event->data.mouse.y); break; case kEventMouseUp: if(event->data.mouse.button == MOUSE_LEFT) app_unlock_and_show_cursor(); break; case kEventMouseMove: delta_x += event->data.mouse.x; delta_y += event->data.mouse.y; break; default: break; } event = app_pop_event(); } // Beginning of frame stuff _delta_time = (float)timer_delta_time(&_timer); update_fps(&_fps, _delta_time); float3 xaxis = {1.0f, 0.0f, 0.0f}; quaternion q = quaternionFromAxisAngle(&xaxis, _delta_time*0.15f); Transform t = _world.entity(_sun_id)->transform(); t.orientation = quaternionMultiply(&q, &t.orientation); //_world.entity(_sun_id)->set_transform(t); if(quaternionGetZAxis(&t.orientation).y > 0.0f) { //_world.entity(_sun_id)->deactivate_component(kLightComponent); } else { //_world.entity(_sun_id)->activate_component(kLightComponent); } // Frame _control_camera(delta_x, delta_y); float4x4 view = TransformGetMatrix(&_camera); _render->set_3d_view_matrix(view); _world.update(_delta_time); _render->render(); // End of frame stuff if(++_frame_count % 16 == 0) { debug_output("%.2fms (%.0f FPS)\n", get_frametime(&_fps), get_fps(&_fps)); } return 0; }
static void redraw(void) { int w = al_get_display_width(example.display); int h = al_get_display_height(example.display); int i; int f1, f2; int fh = al_get_font_line_height(example.font); char const *info[] = {"textures", "memory buffers"}; char const *binfo[] = {"alpha", "additive", "tinted", "solid", "alpha test"}; ALLEGRO_COLOR tint = example.white; if (example.blending == 0) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); tint = example.half_white; } else if (example.blending == 1) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE); tint = example.dark; } else if (example.blending == 2) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); tint = example.red; } else if (example.blending == 3) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); } if (example.blending == 4) { al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_set_render_state(ALLEGRO_ALPHA_TEST, true); al_set_render_state(ALLEGRO_ALPHA_FUNCTION, ALLEGRO_RENDER_GREATER); al_set_render_state(ALLEGRO_ALPHA_TEST_VALUE, 128); } else { al_set_render_state(ALLEGRO_ALPHA_TEST, false); } if (example.hold_bitmap_drawing) { al_hold_bitmap_drawing(true); } for (i = 0; i < example.sprite_count; i++) { Sprite *s = example.sprites + i; al_draw_tinted_bitmap(example.bitmap, tint, s->x, s->y, 0); } if (example.hold_bitmap_drawing) { al_hold_bitmap_drawing(false); } al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); if (example.show_help) { int dh = fh * 3.5; for (i = 5; i >= 0; i--) { al_draw_text(example.font, example.white, 0, h - dh, 0, text[i]); dh += fh * 6; } } al_draw_textf(example.font, example.white, 0, 0, 0, "count: %d", example.sprite_count); al_draw_textf(example.font, example.white, 0, fh, 0, "size: %d", example.bitmap_size); al_draw_textf(example.font, example.white, 0, fh * 2, 0, "%s", info[example.use_memory_bitmaps]); al_draw_textf(example.font, example.white, 0, fh * 3, 0, "%s", binfo[example.blending]); get_fps(&f1, &f2); al_draw_textf(example.font, example.white, w, 0, ALLEGRO_ALIGN_RIGHT, "FPS: %4d +- %-4d", f1, f2); al_draw_textf(example.font, example.white, w, fh, ALLEGRO_ALIGN_RIGHT, "%4d / sec", (int)(1.0 / example.direct_speed_measure)); }
const double cFrameRateManager::get_factor() { if(get_fps() == 0) { return 0; } return 1.0/get_fps(); }
int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts) { double sec = dts_to_sec(dts); return (int64_t)(get_fps() * sec + 0.5); }
void CvCapture_FFMPEG::seek(double sec) { seek((int64_t)(sec * get_fps() + 0.5)); }
void *ref1(void*){ int target_fps = 0; int target_Ug = 0; int target_Uc = 0; start_sampling = 1; int fps; int Ug; static unsigned long long Ug_bp,Ug_tp; get_gpu_time(&Ug_bp,&Ug_tp); int Uc; static unsigned long long Uc_bp,Uc_tp; get_cpu_time(4,&Uc_bp,&Uc_tp); int Fc,Fg; while(true){ if(game == -1){sleep(1);start_sampling=1;continue;} Fc=get_cpu_freq(0); Fg=get_gpu_freq(); //printf("Sample%d Fc=%d,Fg=%d,Uc=%d,Ug=%d\n",start_sampling,get_cpu_freq(0),get_gpu_freq(),Uc,Ug); if(start_sampling == 1){ fps = get_fps(binder); Uc = get_cpu_util(4,&Uc_bp,&Uc_tp); Ug = get_gpu_util(&Ug_bp,&Ug_tp); printf("Sample%d Fc=%d,Fg=%d,Uc=%d,Ug=%d\n",start_sampling,Fc,Fg,Uc,Ug); if(target_fps < fps)target_fps = fps; if(target_Uc < Uc)target_Uc = Uc; if(target_Ug < Ug)target_Ug = Ug; set_cpu_freq(0,FL[3]); set_gpu_freq(GFL[4]); start_sampling=2; sleep(1);continue; }else if(start_sampling == 2){ fps = get_fps(binder); Uc = get_cpu_util(4,&Uc_bp,&Uc_tp); Ug = get_gpu_util(&Ug_bp,&Ug_tp); printf("Sample%d Fc=%d,Fg=%d,Uc=%d,Ug=%d\n",start_sampling,Fc,Fg,Uc,Ug); if(target_fps < fps)target_fps = fps; if(target_Uc < Uc)target_Uc = Uc; if(target_Ug < Ug)target_Ug = Ug; set_cpu_freq(0,FL[10]); set_gpu_freq(GFL[0]); start_sampling=3; sleep(1);continue; }else if(start_sampling == 3){ fps = get_fps(binder); Uc = get_cpu_util(4,&Uc_bp,&Uc_tp); Ug = get_gpu_util(&Ug_bp,&Ug_tp); printf("Sample%d Fc=%d,Fg=%d,Uc=%d,Ug=%d\n",start_sampling,Fc,Fg,Uc,Ug); if(target_fps < fps)target_fps = fps; if(target_Uc < Uc)target_Uc = Uc; if(target_Ug < Ug)target_Ug = Ug; set_cpu_freq(0,FL[10]); set_gpu_freq(GFL[4]); start_sampling=4; sleep(1);continue; }else if(start_sampling == 4){ printf("Sample%d Fc=%d,Fg=%d\n",start_sampling,get_cpu_freq(0),get_gpu_freq()); printf("Q : %d , Ug : %d , Uc : %d\n",target_fps,target_Ug,target_Uc); start_sampling=5; } fps = get_fps(binder); int fps_dev = (target_fps - fps)*100/target_fps; static int FL_point = freq_level-2; static int pre_FL = FL_point; static int GFL_point = gpu_freq_level-1; static int pre_GFL = GFL_point; int CPU_Sensitive = 1; int Fc_adj = 0; int Fg_adj = 0; int Ug_adj = 0; if(fps_dev > 10){ //printf("UP! %d %d %d\n",fps,target_fps,fps_dev); Uc = get_cpu_util(4,&Uc_bp,&Uc_tp); Ug = get_gpu_util(&Ug_bp,&Ug_tp); int fps_dif = (target_fps - fps); int Uc_dev = (target_Uc - Uc)*100/target_Uc; int Ug_dev = (target_Ug - Ug)*100/target_Ug; if(CPU_Sensitive){ //CPU Sensitive Fc_adj = (double)fps_dif * reciprocal(Coef_Fc2Q[game]) + FL[FL_point]; Ug_adj = Ug + (double)fps_dif * reciprocal(Coef_Ug2Q[game]); if(Ug_adj > target_Ug){ Fg_adj = (double)(Ug_adj - target_Ug) * -reciprocal(Coef_Fg2Ug[game]) + GFL[GFL_point]; } }else{ //GPU Sensitive Fg_adj = (double)fps_dif * reciprocal(Coef_Fg2Q[game]) + GFL[GFL_point]; Ug_adj = Ug + (double)fps_dif * reciprocal(Coef_Uc2Q[game]); if(Ug_adj > target_Ug){ Fc_adj = (double)(Ug_adj - target_Ug) * -reciprocal(Coef_Fc2Uc[game]) + FL[FL_point]; } } while(FL_point < freq_level && FL[FL_point] < Fc_adj)FL_point++; while(GFL_point < gpu_freq_level && GFL[GFL_point] < Fg_adj)GFL_point++; if(FL_point != pre_FL){ if(FL_point > freq_level-2){ FL_point = freq_level-2; }else if(FL_point < 2){ FL_point = 2; } pre_FL = FL_point; set_cpu_freq(0,FL[FL_point]); } if(GFL_point != pre_GFL){ if(GFL_point > gpu_freq_level-1){ GFL_point = gpu_freq_level-1; }else if(GFL_point < 0){ GFL_point = 0; } pre_GFL = GFL_point; set_gpu_freq(GFL[GFL_point]); } }else if(fps_dev <= 10){ //printf("DOWN! POWER SAVE %d %d %d\n",fps,target_fps,fps_dev); Uc = get_cpu_util(4,&Uc_bp,&Uc_tp); Ug = get_gpu_util(&Ug_bp,&Ug_tp); int Uc_dev = target_Uc - Uc; int Ug_dev = target_Ug - Ug; if(Uc_dev*100/Uc > 10) Fc_adj = (double)(Uc_dev) * reciprocal(Coef_Fc2Uc[game]) + FL[FL_point]; if(Ug_dev*100/Ug > 10) Fg_adj = (double)(Ug_dev) * reciprocal(Coef_Fg2Ug[game]) + GFL[GFL_point]; while(FL_point > 0 && FL[FL_point] > Fc_adj)FL_point--; while(GFL_point > 0 && GFL[GFL_point] > Fg_adj)GFL_point--; if(FL_point != pre_FL){ if(FL_point > freq_level-2){ FL_point = freq_level-2; }else if(FL_point < 2){ FL_point = 2; } pre_FL = FL_point; set_cpu_freq(0,FL[FL_point]); } if(GFL_point != pre_GFL){ if(GFL_point > gpu_freq_level-1){ GFL_point = gpu_freq_level-1; }else if(GFL_point < 0){ GFL_point = 0; } pre_GFL = GFL_point; set_gpu_freq(GFL[GFL_point]); } } sleep(1); } return 0; }
av_cold int ff_rate_control_init(MpegEncContext *s) { RateControlContext *rcc = &s->rc_context; int i, res; static const char * const const_names[] = { "PI", "E", "iTex", "pTex", "tex", "mv", "fCode", "iCount", "mcVar", "var", "isI", "isP", "isB", "avgQP", "qComp", #if 0 "lastIQP", "lastPQP", "lastBQP", "nextNonBQP", #endif "avgIITex", "avgPITex", "avgPPTex", "avgBPTex", "avgTex", NULL }; static double (* const func1[])(void *, double) = { (void *)bits2qp, (void *)qp2bits, NULL }; static const char * const func1_names[] = { "bits2qp", "qp2bits", NULL }; emms_c(); if (!s->avctx->rc_max_available_vbv_use && s->avctx->rc_buffer_size) { if (s->avctx->rc_max_rate) { s->avctx->rc_max_available_vbv_use = av_clipf(s->avctx->rc_max_rate/(s->avctx->rc_buffer_size*get_fps(s->avctx)), 1.0/3, 1.0); } else s->avctx->rc_max_available_vbv_use = 1.0; } res = av_expr_parse(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx); if (res < 0) { av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq); return res; } for (i = 0; i < 5; i++) { rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0; rcc->pred[i].count = 1.0; rcc->pred[i].decay = 0.4; rcc->i_cplx_sum [i] = rcc->p_cplx_sum [i] = rcc->mv_bits_sum[i] = rcc->qscale_sum [i] = rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5; } rcc->buffer_index = s->avctx->rc_initial_buffer_occupancy; if (!rcc->buffer_index) rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4; if (s->flags & CODEC_FLAG_PASS2) { int i; char *p; /* find number of pics */ p = s->avctx->stats_in; for (i = -1; p; i++) p = strchr(p + 1, ';'); i += s->max_b_frames; if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry)) return -1; rcc->entry = av_mallocz(i * sizeof(RateControlEntry)); rcc->num_entries = i; /* init all to skipped p frames * (with b frames we might have a not encoded frame at the end FIXME) */ for (i = 0; i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; rce->pict_type = rce->new_pict_type = AV_PICTURE_TYPE_P; rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2; rce->misc_bits = s->mb_num + 10; rce->mb_var_sum = s->mb_num * 100; } /* read stats */ p = s->avctx->stats_in; for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) { RateControlEntry *rce; int picture_number; int e; char *next; next = strchr(p, ';'); if (next) { (*next) = 0; // sscanf in unbelievably slow on looong strings // FIXME copy / do not write next++; } e = sscanf(p, " in:%d ", &picture_number); assert(picture_number >= 0); assert(picture_number < rcc->num_entries); rce = &rcc->entry[picture_number]; e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%"SCNd64" var:%"SCNd64" icount:%d skipcount:%d hbits:%d", &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits, &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count, &rce->skip_count, &rce->header_bits); if (e != 14) { av_log(s->avctx, AV_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e); return -1; } p = next; } if (init_pass2(s) < 0) return -1; // FIXME maybe move to end if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) { #if CONFIG_LIBXVID return ff_xvid_rate_control_init(s); #else av_log(s->avctx, AV_LOG_ERROR, "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n"); return -1; #endif } } if (!(s->flags & CODEC_FLAG_PASS2)) { rcc->short_term_qsum = 0.001; rcc->short_term_qcount = 0.001; rcc->pass1_rc_eq_output_sum = 0.001; rcc->pass1_wanted_bits = 0.001; if (s->avctx->qblur > 1.0) { av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n"); return -1; } /* init stuff with the user specified complexity */ if (s->avctx->rc_initial_cplx) { for (i = 0; i < 60 * 30; i++) { double bits = s->avctx->rc_initial_cplx * (i / 10000.0 + 1.0) * s->mb_num; RateControlEntry rce; if (i % ((s->gop_size + 3) / 4) == 0) rce.pict_type = AV_PICTURE_TYPE_I; else if (i % (s->max_b_frames + 1)) rce.pict_type = AV_PICTURE_TYPE_B; else rce.pict_type = AV_PICTURE_TYPE_P; rce.new_pict_type = rce.pict_type; rce.mc_mb_var_sum = bits * s->mb_num / 100000; rce.mb_var_sum = s->mb_num; rce.qscale = FF_QP2LAMBDA * 2; rce.f_code = 2; rce.b_code = 1; rce.misc_bits = 1; if (s->pict_type == AV_PICTURE_TYPE_I) { rce.i_count = s->mb_num; rce.i_tex_bits = bits; rce.p_tex_bits = 0; rce.mv_bits = 0; } else { rce.i_count = 0; // FIXME we do know this approx rce.i_tex_bits = 0; rce.p_tex_bits = bits * 0.9; rce.mv_bits = bits * 0.1; } rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale; rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale; rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits; rcc->frame_count[rce.pict_type]++; get_qscale(s, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i); // FIXME misbehaves a little for variable fps rcc->pass1_wanted_bits += s->bit_rate / get_fps(s->avctx); } } } return 0; }
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num) { RateControlContext *rcc = &s->rc_context; const double buffer_size = s->avctx->rc_buffer_size; const double fps = get_fps(s->avctx); const double min_rate = s->avctx->rc_min_rate / fps; const double max_rate = s->avctx->rc_max_rate / fps; const int pict_type = rce->new_pict_type; int qmin, qmax; get_qminmax(&qmin, &qmax, s, pict_type); /* modulation */ if (s->avctx->rc_qmod_freq && frame_num % s->avctx->rc_qmod_freq == 0 && pict_type == AV_PICTURE_TYPE_P) q *= s->avctx->rc_qmod_amp; /* buffer overflow/underflow protection */ if (buffer_size) { double expected_size = rcc->buffer_index; double q_limit; if (min_rate) { double d = 2 * (buffer_size - expected_size) / buffer_size; if (d > 1.0) d = 1.0; else if (d < 0.0001) d = 0.0001; q *= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity); q_limit = bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index) * s->avctx->rc_min_vbv_overflow_use, 1)); if (q > q_limit) { if (s->avctx->debug & FF_DEBUG_RC) av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); q = q_limit; } } if (max_rate) { double d = 2 * expected_size / buffer_size; if (d > 1.0) d = 1.0; else if (d < 0.0001) d = 0.0001; q /= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity); q_limit = bits2qp(rce, FFMAX(rcc->buffer_index * s->avctx->rc_max_available_vbv_use, 1)); if (q < q_limit) { if (s->avctx->debug & FF_DEBUG_RC) av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); q = q_limit; } } } av_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n", q, max_rate, min_rate, buffer_size, rcc->buffer_index, s->avctx->rc_buffer_aggressivity); if (s->avctx->rc_qsquish == 0.0 || qmin == qmax) { if (q < qmin) q = qmin; else if (q > qmax) q = qmax; } else { double min2 = log(qmin); double max2 = log(qmax); q = log(q); q = (q - min2) / (max2 - min2) - 0.5; q *= -4.0; q = 1.0 / (1.0 + exp(q)); q = q * (max2 - min2) + min2; q = exp(q); } return q; }
inline s64 CvCapture_FFMPEG::dts_to_frame_number(s64 dts) { double sec = dts_to_sec(dts); return (s64)(get_fps() * sec + 0.5); }
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run) { float q; int qmin, qmax; float br_compensation; double diff; double short_term_q; double fps; int picture_number = s->picture_number; int64_t wanted_bits; RateControlContext *rcc = &s->rc_context; AVCodecContext *a = s->avctx; RateControlEntry local_rce, *rce; double bits; double rate_factor; int64_t var; const int pict_type = s->pict_type; Picture * const pic = &s->current_picture; emms_c(); #if CONFIG_LIBXVID if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) return ff_xvid_rate_estimate_qscale(s, dry_run); #endif get_qminmax(&qmin, &qmax, s, pict_type); fps = get_fps(s->avctx); /* update predictors */ if (picture_number > 2 && !dry_run) { const int64_t last_var = s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum; av_assert1(s->frame_bits >= s->stuffing_bits); update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits - s->stuffing_bits); } if (s->flags & CODEC_FLAG_PASS2) { assert(picture_number >= 0); if (picture_number >= rcc->num_entries) { av_log(s, AV_LOG_ERROR, "Input is longer than 2-pass log file\n"); return -1; } rce = &rcc->entry[picture_number]; wanted_bits = rce->expected_bits; } else { Picture *dts_pic; rce = &local_rce; /* FIXME add a dts field to AVFrame and ensure it is set and use it * here instead of reordering but the reordering is simpler for now * until H.264 B-pyramid must be handled. */ if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) dts_pic = s->current_picture_ptr; else dts_pic = s->last_picture_ptr; if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE) wanted_bits = (uint64_t)(s->bit_rate * (double)picture_number / fps); else wanted_bits = (uint64_t)(s->bit_rate * (double)dts_pic->f->pts / fps); } diff = s->total_bits - wanted_bits; br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance; if (br_compensation <= 0.0) br_compensation = 0.001; var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum; short_term_q = 0; /* avoid warning */ if (s->flags & CODEC_FLAG_PASS2) { if (pict_type != AV_PICTURE_TYPE_I) assert(pict_type == rce->new_pict_type); q = rce->new_qscale / br_compensation; av_dlog(s, "%f %f %f last:%d var:%"PRId64" type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type); } else { rce->pict_type = rce->new_pict_type = pict_type; rce->mc_mb_var_sum = pic->mc_mb_var_sum; rce->mb_var_sum = pic->mb_var_sum; rce->qscale = FF_QP2LAMBDA * 2; rce->f_code = s->f_code; rce->b_code = s->b_code; rce->misc_bits = 1; bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var)); if (pict_type == AV_PICTURE_TYPE_I) { rce->i_count = s->mb_num; rce->i_tex_bits = bits; rce->p_tex_bits = 0; rce->mv_bits = 0; } else { rce->i_count = 0; // FIXME we do know this approx rce->i_tex_bits = 0; rce->p_tex_bits = bits * 0.9; rce->mv_bits = bits * 0.1; } rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale; rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale; rcc->mv_bits_sum[pict_type] += rce->mv_bits; rcc->frame_count[pict_type]++; bits = rce->i_tex_bits + rce->p_tex_bits; rate_factor = rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum * br_compensation; q = get_qscale(s, rce, rate_factor, picture_number); if (q < 0) return -1; assert(q > 0.0); q = get_diff_limited_q(s, rce, q); assert(q > 0.0); // FIXME type dependent blur like in 2-pass if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) { rcc->short_term_qsum *= a->qblur; rcc->short_term_qcount *= a->qblur; rcc->short_term_qsum += q; rcc->short_term_qcount++; q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount; } assert(q > 0.0); q = modify_qscale(s, rce, q, picture_number); rcc->pass1_wanted_bits += s->bit_rate / fps; assert(q > 0.0); } if (s->avctx->debug & FF_DEBUG_RC) { av_log(s->avctx, AV_LOG_DEBUG, "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f " "size:%d var:%"PRId64"/%"PRId64" br:%d fps:%d\n", av_get_picture_type_char(pict_type), qmin, q, qmax, picture_number, (int)wanted_bits / 1000, (int)s->total_bits / 1000, br_compensation, short_term_q, s->frame_bits, pic->mb_var_sum, pic->mc_mb_var_sum, s->bit_rate / 1000, (int)fps); } if (q < qmin) q = qmin; else if (q > qmax) q = qmax; if (s->adaptive_quant) adaptive_quantization(s, q); else q = (int)(q + 0.5); if (!dry_run) { rcc->last_qscale = q; rcc->last_mc_mb_var_sum = pic->mc_mb_var_sum; rcc->last_mb_var_sum = pic->mb_var_sum; } return q; }
void redraw(void) { #line 67 All * a = global_a; #line 69 if (a->load_after_redraw) { if (! a->overview) { render_loading_screen(); } #line 72 if (a->load_after_redraw == 1) { a->load_after_redraw = 2; } #line 74 else if (a->load_after_redraw == 2) { // wait for logic code to advance #line 74 ; } #line 77 if (a->load_after_redraw > 2) { a->load_after_redraw++; double t = land_get_time(); while (1) { if (! blocks_preload(game->blocks)) { a->load_after_redraw = 0; #line 84 if (a->overview) { overview_render_next(game->overview); } #line 86 break; } #line 87 if (land_get_time() > t + 0.01) { break; } } } #line 90 if (! a->overview) { #line 90 return ; } } float w = land_display_width(); float h = land_display_height(); //float fh = land_font_height(a->font) #line 97 if (a->title) { title_render(); } #line 98 else { #line 100 render(game, w, h); } if (a->show_fps) { double f1, f2; get_fps(& f1, & f2); land_text_pos(w, 0); land_font_set(a->font); land_color(a->text.r, a->text.g, a->text.b, a->text.a); land_print_right("FPS: %4d +- %-4d", (int) f1, (int) f2); land_print_right("%4d / sec", (int)(1.0 / a->direct_speed_measure)); } }
static int init_pass2(MpegEncContext *s) { RateControlContext *rcc = &s->rc_context; AVCodecContext *a = s->avctx; int i, toobig; double fps = get_fps(s->avctx); double complexity[5] = { 0 }; // approximate bits at quant=1 uint64_t const_bits[5] = { 0 }; // quantizer independent bits uint64_t all_const_bits; uint64_t all_available_bits = (uint64_t)(s->bit_rate * (double)rcc->num_entries / fps); double rate_factor = 0; double step; const int filter_size = (int)(a->qblur * 4) | 1; double expected_bits = 0; // init to silence gcc warning double *qscale, *blurred_qscale, qscale_sum; /* find complexity & const_bits & decide the pict_types */ for (i = 0; i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; rce->new_pict_type = rce->pict_type; rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale; rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale; rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits; rcc->frame_count[rce->pict_type]++; complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale; const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits; } all_const_bits = const_bits[AV_PICTURE_TYPE_I] + const_bits[AV_PICTURE_TYPE_P] + const_bits[AV_PICTURE_TYPE_B]; if (all_available_bits < all_const_bits) { av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n"); return -1; } qscale = av_malloc_array(rcc->num_entries, sizeof(double)); blurred_qscale = av_malloc_array(rcc->num_entries, sizeof(double)); toobig = 0; for (step = 256 * 256; step > 0.0000001; step *= 0.5) { expected_bits = 0; rate_factor += step; rcc->buffer_index = s->avctx->rc_buffer_size / 2; /* find qscale */ for (i = 0; i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; qscale[i] = get_qscale(s, &rcc->entry[i], rate_factor, i); rcc->last_qscale_for[rce->pict_type] = qscale[i]; } assert(filter_size % 2 == 1); /* fixed I/B QP relative to P mode */ for (i = FFMAX(0, rcc->num_entries - 300); i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; qscale[i] = get_diff_limited_q(s, rce, qscale[i]); } for (i = rcc->num_entries - 1; i >= 0; i--) { RateControlEntry *rce = &rcc->entry[i]; qscale[i] = get_diff_limited_q(s, rce, qscale[i]); } /* smooth curve */ for (i = 0; i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; const int pict_type = rce->new_pict_type; int j; double q = 0.0, sum = 0.0; for (j = 0; j < filter_size; j++) { int index = i + j - filter_size / 2; double d = index - i; double coeff = a->qblur == 0 ? 1.0 : exp(-d * d / (a->qblur * a->qblur)); if (index < 0 || index >= rcc->num_entries) continue; if (pict_type != rcc->entry[index].new_pict_type) continue; q += qscale[index] * coeff; sum += coeff; } blurred_qscale[i] = q / sum; } /* find expected bits */ for (i = 0; i < rcc->num_entries; i++) { RateControlEntry *rce = &rcc->entry[i]; double bits; rce->new_qscale = modify_qscale(s, rce, blurred_qscale[i], i); bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits; bits += 8 * ff_vbv_update(s, bits); rce->expected_bits = expected_bits; expected_bits += bits; } av_dlog(s->avctx, "expected_bits: %f all_available_bits: %d rate_factor: %f\n", expected_bits, (int)all_available_bits, rate_factor); if (expected_bits > all_available_bits) { rate_factor -= step; ++toobig; } } av_free(qscale); av_free(blurred_qscale); /* check bitrate calculations and print info */ qscale_sum = 0.0; for (i = 0; i < rcc->num_entries; i++) { av_dlog(s, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n", i, rcc->entry[i].new_qscale, rcc->entry[i].new_qscale / FF_QP2LAMBDA); qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA, s->avctx->qmin, s->avctx->qmax); } assert(toobig <= 40); av_log(s->avctx, AV_LOG_DEBUG, "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n", s->bit_rate, (int)(expected_bits / ((double)all_available_bits / s->bit_rate))); av_log(s->avctx, AV_LOG_DEBUG, "[lavc rc] estimated target average qp: %.3f\n", (float)qscale_sum / rcc->num_entries); if (toobig == 0) { av_log(s->avctx, AV_LOG_INFO, "[lavc rc] Using all of requested bitrate is not " "necessary for this video with these parameters.\n"); } else if (toobig == 40) { av_log(s->avctx, AV_LOG_ERROR, "[lavc rc] Error: bitrate too low for this video " "with these parameters.\n"); return -1; } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) { av_log(s->avctx, AV_LOG_ERROR, "[lavc rc] Error: 2pass curve failed to converge\n"); return -1; } return 0; }
int gamemove(void){ float adjust = (100.0/get_fps()); /* Framerate Correction */ float scale = 360; /* m/s -> km/h */ float hover = sin(2*(SDL_GetTicks()/1000.0)); /* Player Constants */ player.maxvel = 2300 / scale; /* Maximum Velocity */ player.accel = 3 / scale * adjust; /* Acceleration */ player.rotmax = 360 / scale; /* Maximum Rotational Velocity */ player.rotacc = 10 / scale * adjust; /* Rotational Acceleration */ if(player.rot >= 360) player.rot -= 360; if(player.rot <= 0) player.rot += 360; if(player.rotdir == 1){ if(rotplus <= 30) rotplus += 1 * adjust; }else if(player.rotdir == -1){ if(rotplus >= -30) rotplus -= 1 * adjust; }else{ if(rotplus > 0) rotplus -= 1 * adjust; else if(rotplus < 0) rotplus += 1 * adjust; } /* Player Movement */ player.x += player.vel * sin(player.rot*degtorad) * adjust; player.y = getheight(-player.x, -player.z) + hover + YSHIFT; player.z += player.vel * cos(player.rot*degtorad) * adjust; player.rot += player.rotvel * adjust; if(brakes) player.dir = 0; /* Velocity Calculations */ if(player.dir > 0){ /* Backward Motion */ if(player.vel <= 0.5*player.maxvel) player.vel += player.accel * player.dir; /* Accelerate to Max Speed */ }else if(player.dir < 0){ /* Forward Motion */ if(player.vel >= -player.maxvel) player.vel += player.accel * player.dir; /* Accelerate to Max Speed */ }else{ if(player.vel > 5/scale) if(brakes) player.vel -= 3*player.accel; /* Forward Deccel */ else player.vel -= player.accel/2; else if(player.vel < -5/scale) if(brakes) player.vel += 3*player.accel; /* Forward Deccel */ else player.vel += player.accel/2; /* Backward Deccel */ else player.vel = 0; /* Stop Moving */ } /* Rotational Velocity Calculations */ if(player.rotdir > 0){ /* Right Rotation */ if(player.rotvel <= player.rotmax) player.rotvel += player.rotacc * player.rotdir; /* Accelerate to Max Rotation */ }else if(player.rotdir < 0){ /* Left Rotation */ if(player.rotvel >= -player.rotmax) player.rotvel += player.rotacc * player.rotdir; /* Accelerate to Max Rotation */ }else{ if(player.rotvel > 0) player.rotvel -= player.rotacc; /* Left Deccel */ else if (player.rotvel < 0) player.rotvel += player.rotacc; /* Right Deccel */ else player.rotvel = 0; /* Stop Rotating*/ } return 1; }