diff --git a/README.md b/README.md index ca60d4d..71dbd12 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ Includes a desktop **SDL2 simulator** (`--sim`) with a procedural test scene (`- ## Key Features - **Option A Split Layout (`320×170`):** Live `226×170` viewfinder alongside a focusable `94×170` control rail (`1P · 2V · 3T`), with live **`[W]`** rail-side toggle (`right` default or `left`) and **`[T]`** Zinc Dark / Light theme toggle. -- **Photo Mode + 9 Filters:** True ISP `ScalerCrop` zoom (`1x`–`4x` via `5`/`7`), independent `2x` viewfinder-only **Loupe** (`L`), EV (`-2.0` to `+2.0`), `8MP` / `2MP` presets (`R`), `Off`/`3s`/`10s` self-timer (`C`), `[G]` rule-of-thirds grid, and 9 shared RGB24/`libjpeg` filters (`None` zero-re-encode passthrough, `B&W`, `Hi-Con`, `Sepia`, `Cool`, `Vivid`, `Invert`, `Retro`, and Bayer-dithered `GameBoy` DMG). -- **Hardware H.264 Video Mode:** `720p30` (default), `1080p30`, and `480p30` `.mp4` recording with ES8389 MEMS mic ON by default (`[M]` idle mute toggle, graceful muted fallback on `audio_unavailable`), live 16:9 letterboxed viewfinder, and live digital zoom during capture. -- **Foreground Timelapse Mode:** `2MP` default (`1080p` / `8MP` presets), customizable interval/FPS/stop presets (`C` / `P`), output to `TL_/` JPEGs or compiled `TL_.mp4` (`h264_v4l2m2m` with automatic `libx264` fallback and `8MP`→`1440×1080` scaling for VideoCore IV's `2048px` encoder ceiling), sysfs backlight auto-dim + **dimmed safe-wake** (any keypress only wakes the display), and startup recovery for orphaned `.tl_work_*` sessions. +- **Photo Mode + 9 Filters:** True ISP `ScalerCrop` zoom (`1x`–`4x` via `5`/`7`), independent `2x` viewfinder-only **Loupe** (`Z`), EV (`-2.0` to `+2.0`), `8MP` / `2MP` presets (`R`), `Off`/`3s`/`10s` self-timer (`C`), `[G]` rule-of-thirds grid, and 9 shared RGB24/`libjpeg` filters (`None` zero-re-encode passthrough, `B&W`, `Hi-Con`, `Sepia`, `Cool`, `Vivid`, `Invert`, `Retro`, and Bayer-dithered `GameBoy` DMG). +- **Hardware H.264 Video Mode:** `720p30` (default), `1080p30`, and `480p30` `.mp4` recording with ES8389 MEMS mic ON by default (`[M]` idle mute toggle, graceful muted fallback on `audio_unavailable`), live 16:9 letterboxed viewfinder, and live digital zoom (`5`/`7`) and `2x` Loupe (`Z`) during capture. +- **Foreground Timelapse Mode:** `2MP` default (`1080p` / `8MP` presets), customizable interval, any FPS `1..30`, and stop presets (`C` / `P`), live zoom (`5`/`7`) & `2x` Loupe (`Z`), output to `TL_/` JPEGs or compiled `TL_.mp4` (`h264_v4l2m2m` at memory-safe `1024×768` / `1280×720` with low-RAM `libx264` fallback), sysfs backlight auto-dim + **dimmed safe-wake** (any keypress only wakes the display), and startup recovery for orphaned `.tl_work_*` sessions. - **Full-Screen Gallery & Video Playback (`[8]`):** Pauses the camera backend while open, supports `TL_*` session drill-down, plays `.mp4` videos (with or without audio tracks) via `ffmpeg`/`aplay` (or SDL2 audio on desktop) with `-5s`/`+5s` seek, and enforces `[Y/N]` delete confirmation. - **Settings & Directory Tree Picker (`[S]`):** Output folder browser (plus `[T]` manual path entry), `Camera Orient` (`auto`, `0°`, `180°`, `H-Flip`, `V-Flip` via ISP `Transform`), rail side, theme, grid, and auto-dim timeout. - **Sync-Safe & Hardware-Decoupled:** Writes exclusively to `.`-prefixed staging paths until finalized via `fsync` + atomic `rename()` (with a `100 MB` free-space guard), keeping zero runtime/packaging dependencies on external sync tools or direct `libcamera.so` linkage. @@ -76,9 +76,9 @@ python3 -m unittest discover -s packaging/tests -v | `1`, `2`, `3` | Main View | Switch between **Photo**, **Video**, and **Timelapse** | | `Enter`, `Space`, `6` | Main View | Trigger shutter / Start-Stop recording / Open Timelapse stop prompt | | `Up`/`Down` or `k`/`j` | Rail / Lists / Modals | Move selection cursor | -| `Left`/`Right` or `h`/`l` | Rail / Settings | Cycle selected card value (`l` toggles `2x` Loupe in idle Photo view) | -| `5` / `7` | Photo / Video | Step ISP digital crop zoom (`1.0x`–`4.0x`) | -| `L` | Photo | Toggle `2x` viewfinder-only Loupe | +| `Left`/`Right` or `h`/`l` | Rail / Settings | Cycle selected card value | +| `5` / `7` | Photo / Video / Timelapse | Step ISP digital crop zoom (`1.0x`–`4.0x`, live during Video & Timelapse) | +| `Z` | Photo / Video / Timelapse | Toggle `2x` viewfinder-only Loupe (works idle or recording/capturing) | | `F` | Photo | Cycle 9 photo filters (`None` .. `GameBoy`) | | `R` | Photo / Video / TL (or Offline) | Cycle resolution preset (or recover camera when `Offline`) | | `C` | Photo / Timelapse | Cycle self-timer (`Off`/`3s`/`10s`) in Photo; open Custom TL modal in Timelapse | diff --git a/app/src/app_ui.cpp b/app/src/app_ui.cpp index 511a37a..717656d 100644 --- a/app/src/app_ui.cpp +++ b/app/src/app_ui.cpp @@ -825,8 +825,7 @@ struct AppView::Impl { if (show_live_frame) { scaled_vf.resize(static_cast(feed_w) * static_cast(feed_h) * 3u); - const float loupe = - (state.mode == AppMode::Photo && state.loupe_enabled) ? 2.0f : 1.0f; + const float loupe = state.loupe_enabled ? 2.0f : 1.0f; scale_crop_rgb24(live_frame->rgb24.data(), live_frame->width, live_frame->height, scaled_vf.data(), feed_w, feed_h, loupe); @@ -875,7 +874,7 @@ struct AppView::Impl { // Top-right Zoom / Loupe badge int right_edge = vf_x + kVfW - 5; - if (state.mode == AppMode::Photo && state.loupe_enabled) { + if (state.loupe_enabled) { const std::string ltext = "LOUPE 2x"; const int lw = measure_pill_badge_w(canvas, ltext, false); draw_pill_badge(canvas, right_edge - lw, badge_y, 16, ltext, @@ -1009,7 +1008,9 @@ struct AppView::Impl { false}; cards[1] = { "OUT", - state.config.timelapse_output_mode == "photos" ? "JPG" : "MP4", + state.config.timelapse_output_mode == "photos" + ? "JPG" + : ("MP4 " + std::to_string(state.config.timelapse_fps)), 0, false}; cards[2] = {"RES", @@ -1059,24 +1060,27 @@ struct AppView::Impl { draw_kbd_hint(canvas, pal, hx2, 156, "S", "Set", 8, 2); } else if (state.mode == AppMode::Video) { if (state.video_recording) { - draw_kbd_hint(canvas, pal, hx1, 143, "Esc", "Stop Rec", 8, 2); - draw_kbd_hint(canvas, pal, hx2, 156, "5/7", "Zoom", 8, 2); - } else { - draw_kbd_hint(canvas, pal, hx1, 143, "Ent", "Rec", 8, 4); - draw_kbd_hint(canvas, pal, hx1, 143, "M", "Mic", 8, 2); + draw_kbd_hint(canvas, pal, hx1, 143, "Esc", "Stop", 8, 2); + draw_kbd_hint(canvas, pal, hx1, 143, "Z", "Lpe", 8, 2); draw_kbd_hint(canvas, pal, hx2, 156, "5/7", "Zm", 8, 3); - draw_kbd_hint(canvas, pal, hx2, 156, "S", "Set", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "G", "Grd", 8, 2); + } else { + draw_kbd_hint(canvas, pal, hx1, 143, "Ent", "Rec", 8, 3); + draw_kbd_hint(canvas, pal, hx1, 143, "M", "Mic", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "5/7", "Zm", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "Z", "Lpe", 8, 2); } } else { if (state.timelapse.running) { draw_kbd_hint(canvas, pal, hx1, 143, "Esc", "Stop", 8, 2); - canvas.draw_text(FontRole::Sans, 8, inner_x, 157, "Capturing...", - pal.status_online); + draw_kbd_hint(canvas, pal, hx1, 143, "Z", "Lpe", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "5/7", "Zm", 8, 3); + draw_kbd_hint(canvas, pal, hx2, 156, "G", "Grd", 8, 2); } else { draw_kbd_hint(canvas, pal, hx1, 143, "Ent", "Go", 8, 4); draw_kbd_hint(canvas, pal, hx1, 143, "C", "Cust", 8, 2); - draw_kbd_hint(canvas, pal, hx2, 156, "8", "Gal", 8, 4); - draw_kbd_hint(canvas, pal, hx2, 156, "S", "Set", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "5/7", "Zm", 8, 2); + draw_kbd_hint(canvas, pal, hx2, 156, "Z", "Lpe", 8, 2); } } } @@ -1408,7 +1412,8 @@ struct AppView::Impl { const std::array, 4> rows = {{ {"Interval (s)", std::to_string(state.tl_custom_interval_sec) + "s"}, - {"Video FPS", std::to_string(state.tl_custom_fps) + " fps"}, + {"Video FPS (1-30)", + std::to_string(state.tl_custom_fps) + " fps"}, {"Stop Mode", state.tl_custom_stop_mode}, {"Stop Value", stop_val_str}, }}; diff --git a/app/src/config.cpp b/app/src/config.cpp index 71b3e9d..22cb857 100644 --- a/app/src/config.cpp +++ b/app/src/config.cpp @@ -56,10 +56,7 @@ AppConfig sanitize_config(AppConfig cfg) { if (!is_one_of(cfg.timelapse_output_mode, {"video", "photos"})) { cfg.timelapse_output_mode = "video"; } - if (cfg.timelapse_fps != 15 && cfg.timelapse_fps != 24 && - cfg.timelapse_fps != 30) { - cfg.timelapse_fps = 24; - } + cfg.timelapse_fps = std::clamp(cfg.timelapse_fps, 1, 30); if (!is_one_of(cfg.timelapse_stop_mode, {"manual", "shots", "duration"})) { cfg.timelapse_stop_mode = "manual"; } diff --git a/app/src/timelapse_engine.cpp b/app/src/timelapse_engine.cpp index f4f936c..2ebd53c 100644 --- a/app/src/timelapse_engine.cpp +++ b/app/src/timelapse_engine.cpp @@ -160,6 +160,10 @@ std::vector TimelapseEngine::build_compile_command( "ffmpeg", "-nostdin", "-y", + "-threads", + "1", + "-filter_threads", + "1", "-framerate", std::to_string(safe_fps), "-start_number", @@ -168,25 +172,35 @@ std::vector TimelapseEngine::build_compile_command( input_pattern, }; - if (to_lower_ascii(resolution) == "8mp") { + // Scale to CM0/VideoCore IV memory-safe dimensions while preserving aspect ratio. + if (to_lower_ascii(resolution) == "1080p") { args.push_back("-vf"); - args.push_back("scale=1440:1080:flags=lanczos"); + args.push_back("scale=1280:720:flags=bilinear,setsar=1"); + } else { + args.push_back("-vf"); + args.push_back("scale=1024:768:flags=bilinear,setsar=1"); } if (use_hw_encoder) { args.push_back("-c:v"); args.push_back("h264_v4l2m2m"); + args.push_back("-num_output_buffers"); + args.push_back("4"); args.push_back("-b:v"); - args.push_back("6M"); + args.push_back("5M"); args.push_back("-pix_fmt"); args.push_back("yuv420p"); } else { args.push_back("-c:v"); args.push_back("libx264"); + args.push_back("-threads"); + args.push_back("2"); args.push_back("-preset"); - args.push_back("veryfast"); + args.push_back("ultrafast"); args.push_back("-crf"); - args.push_back("22"); + args.push_back("23"); + args.push_back("-x264-params"); + args.push_back("rc-lookahead=0:ref=1:bframes=0:threads=2:lookahead-threads=1:sync-lookahead=0"); args.push_back("-pix_fmt"); args.push_back("yuv420p"); } @@ -246,7 +260,8 @@ bool TimelapseEngine::start(const AppConfig& cfg, uint64_t now_ms) { const std::string res = cfg.timelapse_resolution.empty() ? "2mp" : to_lower_ascii(cfg.timelapse_resolution); const int interval_sec = std::clamp(cfg.timelapse_interval_sec, 1, 3600); - const int fps = (cfg.timelapse_fps > 0) ? cfg.timelapse_fps : 24; + const int fps = + (cfg.timelapse_fps > 0) ? std::clamp(cfg.timelapse_fps, 1, 30) : 24; TimelapseManifest m; m.session_id = paths.session_id; @@ -576,18 +591,7 @@ bool TimelapseEngine::run_compile_attempt( on_progress(cur, total_frames); } } - } else if (line == "progress=end" && total_frames > 0 && - last_reported < total_frames) { - last_reported = total_frames; - { - std::lock_guard lock(mutex_); - state_.compile_current = total_frames; - state_.compile_total = total_frames; - state_.compile_progress = 1.0f; - } - if (on_progress) { - on_progress(total_frames, total_frames); - } + // progress=end is emitted even on failed encoder init; leave 100% completion to compile_session() on exit 0. } }; @@ -776,6 +780,11 @@ bool TimelapseEngine::compile_session( if (!ok && !cancelled) { remove_silent(paths.mp4_tmp_path); + { + std::lock_guard lock(mutex_); + state_.compile_current = 0; + state_.compile_progress = 0.0f; + } ok = run_compile_attempt(ffmpeg, paths, fps, resolution, false, total_frames, on_progress, err); std::lock_guard plock(compile_proc_mutex_); diff --git a/app/src/ui_controller.cpp b/app/src/ui_controller.cpp index cebb7e8..73e741b 100644 --- a/app/src/ui_controller.cpp +++ b/app/src/ui_controller.cpp @@ -22,7 +22,6 @@ constexpr uint64_t kCaptureFlashMs = 150ULL; constexpr uint64_t kToastDurationMs = 3000ULL; constexpr std::array kZoomSteps = {1.0f, 1.5f, 2.0f, 3.0f, 4.0f}; constexpr std::array kIntervalPresets = {2, 5, 10, 30, 60, 300}; -constexpr std::array kFpsPresets = {15, 24, 30}; constexpr std::array kDimPresets = {15, 30, 60, 0}; constexpr std::array kOrientPresets = {"auto", "0", "180", "hflip", "vflip"}; @@ -1123,7 +1122,7 @@ void UiController::open_timelapse_custom_input() { state_.modal = OverlayModal::TimelapseCustomInput; state_.tl_custom_cursor = 0; state_.tl_custom_interval_sec = state_.config.timelapse_interval_sec; - state_.tl_custom_fps = state_.config.timelapse_fps; + state_.tl_custom_fps = std::clamp(state_.config.timelapse_fps, 1, 30); state_.tl_custom_stop_mode = state_.config.timelapse_stop_mode; state_.tl_custom_stop_value = state_.config.timelapse_stop_value; state_.tl_custom_text.clear(); @@ -1185,13 +1184,25 @@ bool UiController::handle_none_modal_key(char ch, ? static_cast(std::tolower(static_cast(ch))) : 0; - // Active Timelapse running locks all mode/settings switches + // Active Timelapse running locks all mode/settings switches while keeping zoom/loupe/grid active if (tl_running) { if (code == KeyCode::Enter || code == KeyCode::Esc || lower == '6' || lower == 'q') { state_.modal = OverlayModal::TimelapseStopConfirm; return true; } + if (lower == '5') { + adjust_zoom(-1); + return true; + } + if (lower == '7') { + adjust_zoom(+1); + return true; + } + if (lower == 'z') { + state_.loupe_enabled = !state_.loupe_enabled; + return true; + } if (lower == 'g') { state_.config.grid_enabled = !state_.config.grid_enabled; persist_config(); @@ -1200,7 +1211,7 @@ bool UiController::handle_none_modal_key(char ch, return false; } - // Active Video recording locks transitions while keeping zoom and stop active + // Active Video recording locks transitions while keeping zoom/loupe/grid/stop active if (vid_recording) { if (code == KeyCode::Enter || code == KeyCode::Space || code == KeyCode::Esc || lower == '6') { @@ -1215,6 +1226,10 @@ bool UiController::handle_none_modal_key(char ch, adjust_zoom(+1); return true; } + if (lower == 'z') { + state_.loupe_enabled = !state_.loupe_enabled; + return true; + } if (lower == 'g') { state_.config.grid_enabled = !state_.config.grid_enabled; persist_config(); @@ -1268,11 +1283,6 @@ bool UiController::handle_none_modal_key(char ch, return true; } if (code == KeyCode::Right || lower == 'l') { - // Note: lowercase 'l' toggles Loupe in Photo mode per spec §5.4/§5.8! - if (lower == 'l' && state_.mode == AppMode::Photo) { - state_.loupe_enabled = !state_.loupe_enabled; - return true; - } adjust_rail_setting(+1); return true; } @@ -1297,12 +1307,9 @@ bool UiController::handle_none_modal_key(char ch, case '7': adjust_zoom(+1); return true; - case 'l': - if (state_.mode == AppMode::Photo) { - state_.loupe_enabled = !state_.loupe_enabled; - return true; - } - return false; + case 'z': + state_.loupe_enabled = !state_.loupe_enabled; + return true; case 'f': if (state_.mode == AppMode::Photo) { state_.filter = next_filter(state_.filter, +1); @@ -1765,7 +1772,7 @@ bool UiController::handle_tl_custom_key(char ch, (code == KeyCode::Char && (ch == '\n' || ch == '\r'))) { state_.config.timelapse_interval_sec = std::clamp(state_.tl_custom_interval_sec, 1, 3600); - state_.config.timelapse_fps = state_.tl_custom_fps; + state_.config.timelapse_fps = std::clamp(state_.tl_custom_fps, 1, 30); state_.config.timelapse_stop_mode = state_.tl_custom_stop_mode; state_.config.timelapse_stop_value = (state_.tl_custom_stop_mode == "manual") @@ -1797,16 +1804,19 @@ bool UiController::handle_tl_custom_key(char ch, return true; } - // Numeric entry for Interval (row 0) or Stop Value (row 3) + // Numeric entry for Interval (row 0), Video FPS (row 1), or Stop Value (row 3) if (code == KeyCode::Char && std::isdigit(static_cast(ch))) { - if (state_.tl_custom_cursor == 0 || + if (state_.tl_custom_cursor == 0 || state_.tl_custom_cursor == 1 || (state_.tl_custom_cursor == 3 && state_.tl_custom_stop_mode != "manual")) { - if (state_.tl_custom_text.size() < 4) { + const size_t max_len = (state_.tl_custom_cursor == 1) ? 2 : 4; + if (state_.tl_custom_text.size() < max_len) { state_.tl_custom_text.push_back(ch); const int val = std::atoi(state_.tl_custom_text.c_str()); if (state_.tl_custom_cursor == 0) { state_.tl_custom_interval_sec = std::clamp(val, 1, 3600); + } else if (state_.tl_custom_cursor == 1) { + state_.tl_custom_fps = std::clamp(val, 1, 30); } else if (state_.tl_custom_stop_mode == "shots") { state_.tl_custom_stop_value = std::clamp(val, 1, 9999); } else { @@ -1820,6 +1830,9 @@ bool UiController::handle_tl_custom_key(char ch, if (code == KeyCode::Backspace || (code == KeyCode::Char && (ch == '\b' || static_cast(ch) == 0x7f))) { + if (state_.tl_custom_cursor == 1 && state_.tl_custom_text.empty()) { + state_.tl_custom_text = std::to_string(std::clamp(state_.tl_custom_fps, 1, 30)); + } if (!state_.tl_custom_text.empty()) { state_.tl_custom_text.pop_back(); const int val = @@ -1827,6 +1840,8 @@ bool UiController::handle_tl_custom_key(char ch, : std::atoi(state_.tl_custom_text.c_str()); if (state_.tl_custom_cursor == 0) { state_.tl_custom_interval_sec = std::clamp(val, 1, 3600); + } else if (state_.tl_custom_cursor == 1) { + state_.tl_custom_fps = std::clamp(val, 1, 30); } else if (state_.tl_custom_stop_mode == "shots") { state_.tl_custom_stop_value = std::clamp(val, 1, 9999); } else if (state_.tl_custom_stop_mode == "duration") { @@ -1845,9 +1860,8 @@ bool UiController::handle_tl_custom_key(char ch, state_.tl_custom_interval_sec = std::clamp(state_.tl_custom_interval_sec + delta * step, 1, 3600); } else if (state_.tl_custom_cursor == 1) { - const int idx = find_index_int(kFpsPresets, state_.tl_custom_fps); state_.tl_custom_fps = - kFpsPresets[static_cast((idx + delta + 3) % 3)]; + std::clamp(state_.tl_custom_fps + delta, 1, 30); } else if (state_.tl_custom_cursor == 2) { static constexpr std::array kStopModes = { "manual", "shots", "duration"}; diff --git a/app/tests/test_config_and_storage.cpp b/app/tests/test_config_and_storage.cpp index 060e364..d328a1b 100644 --- a/app/tests/test_config_and_storage.cpp +++ b/app/tests/test_config_and_storage.cpp @@ -307,6 +307,15 @@ void test_config_defaults_roundtrip_and_corrupt_fallback() { assert(loaded.ev == custom.ev); assert(loaded.camera_transform == custom.camera_transform); + // Custom timelapse FPS (1..30) round-trips and out-of-range values clamp to [1, 30] + custom.timelapse_fps = 12; + assert(cz::save_config(custom)); + assert(cz::load_config().timelapse_fps == 12); + write_bytes(env_cfg, "{ \"timelapse_fps\": 0 }"); + assert(cz::load_config().timelapse_fps == 1); + write_bytes(env_cfg, "{ \"timelapse_fps\": 99 }"); + assert(cz::load_config().timelapse_fps == 30); + // Corrupt JSON falls back gracefully to defaults without throwing write_bytes(env_cfg, "{ \"theme\": \"light\", broken_json "); const cz::AppConfig recovered = cz::load_config(); diff --git a/app/tests/test_timelapse_and_gallery.cpp b/app/tests/test_timelapse_and_gallery.cpp index c8c828a..732c8b3 100644 --- a/app/tests/test_timelapse_and_gallery.cpp +++ b/app/tests/test_timelapse_and_gallery.cpp @@ -310,23 +310,31 @@ void test_timelapse_compile_8mp_scaling_and_h264_fallback() { const auto cmd_8mp_hw = cz::TimelapseEngine::build_compile_command(paths, 24, "8mp", true); - assert(has_arg_pair(cmd_8mp_hw, "-vf", "scale=1440:1080:flags=lanczos")); + assert(has_arg_pair(cmd_8mp_hw, "-threads", "1")); + assert(has_arg_pair(cmd_8mp_hw, "-filter_threads", "1")); + assert(has_arg_pair(cmd_8mp_hw, "-vf", "scale=1024:768:flags=bilinear,setsar=1")); assert(has_arg_pair(cmd_8mp_hw, "-c:v", "h264_v4l2m2m")); + assert(has_arg_pair(cmd_8mp_hw, "-num_output_buffers", "4")); + assert(has_arg_pair(cmd_8mp_hw, "-b:v", "5M")); assert(has_arg_pair(cmd_8mp_hw, "-f", "mp4")); assert(has_arg_pair(cmd_8mp_hw, "-movflags", "+faststart")); assert(has_arg(cmd_8mp_hw, paths.mp4_tmp_path)); const auto cmd_2mp_sw = - cz::TimelapseEngine::build_compile_command(paths, 24, "2mp", false); - assert(!has_arg(cmd_2mp_sw, "-vf")); + cz::TimelapseEngine::build_compile_command(paths, 12, "2mp", false); + assert(has_arg_pair(cmd_2mp_sw, "-framerate", "12")); + assert(has_arg_pair(cmd_2mp_sw, "-vf", "scale=1024:768:flags=bilinear,setsar=1")); assert(has_arg_pair(cmd_2mp_sw, "-c:v", "libx264")); - assert(has_arg_pair(cmd_2mp_sw, "-preset", "veryfast")); - assert(has_arg_pair(cmd_2mp_sw, "-crf", "22")); + assert(has_arg_pair(cmd_2mp_sw, "-threads", "2")); + assert(has_arg_pair(cmd_2mp_sw, "-preset", "ultrafast")); + assert(has_arg_pair(cmd_2mp_sw, "-crf", "23")); + assert(has_arg_pair(cmd_2mp_sw, "-x264-params", + "rc-lookahead=0:ref=1:bframes=0:threads=2:lookahead-threads=1:sync-lookahead=0")); assert(has_arg_pair(cmd_2mp_sw, "-f", "mp4")); const auto cmd_1080_hw = cz::TimelapseEngine::build_compile_command(paths, 30, "1080p", true); - assert(!has_arg(cmd_1080_hw, "-vf")); + assert(has_arg_pair(cmd_1080_hw, "-vf", "scale=1280:720:flags=bilinear,setsar=1")); assert(has_arg_pair(cmd_1080_hw, "-framerate", "30")); // Generate 3 real JPEGs and compile with 8mp scaling (triggers h264_v4l2m2m -> libx264 fallback on macOS) @@ -366,15 +374,15 @@ void test_timelapse_compile_8mp_scaling_and_h264_fallback() { assert(!engine.state().compiling); assert(engine.state().last_error.empty()); - // Probe compiled MP4 via GalleryStore to verify 1440x1080 scaled output + // Probe compiled MP4 via GalleryStore to verify CM0-safe 1024x768 scaled output cz::GalleryStore gallery; assert(gallery.refresh(out_dir)); assert(gallery.items().size() == 1); cz::GalleryItem item = gallery.items()[0]; assert(item.type == cz::GalleryItemType::Video); assert(gallery.probe_video_meta(item)); - assert(item.width == 1440); - assert(item.height == 1080); + assert(item.width == 1024); + assert(item.height == 768); // Also test video-mode live capture + request_stop() + cancel_compile() preserving staging files cz::SimCameraBackend sim; diff --git a/app/tests/test_ui_controller.cpp b/app/tests/test_ui_controller.cpp index d5405f3..53880d9 100644 --- a/app/tests/test_ui_controller.cpp +++ b/app/tests/test_ui_controller.cpp @@ -269,18 +269,27 @@ void test_photo_controls_zoom_loupe_filters_and_timer() { ctrl.handle_key('5', cz::KeyCode::Char, now); assert(ctrl.state().zoom == 1.0f); - // Loupe L toggles 2x viewfinder magnification without changing sensor zoom + // Loupe Z toggles 2x viewfinder magnification without changing sensor zoom; + // lowercase 'l' acts as Right (adjust_rail_setting(+1)) for hjkl navigation. ctrl.handle_key('7', cz::KeyCode::Char, now); assert(ctrl.state().zoom == 1.5f); const int calls_before_loupe = spy->set_controls_calls; - ctrl.handle_key('l', cz::KeyCode::Char, now); + ctrl.handle_key('z', cz::KeyCode::Char, now); assert(ctrl.state().loupe_enabled); assert(ctrl.state().zoom == 1.5f); assert(spy->last_zoom == 1.5f); assert(spy->set_controls_calls == calls_before_loupe); - ctrl.handle_key('L', cz::KeyCode::Char, now); + ctrl.handle_key('Z', cz::KeyCode::Char, now); assert(!ctrl.state().loupe_enabled); + assert(ctrl.state().rail_cursor == 1); + assert(ctrl.state().config.ev == 0.0); + ctrl.handle_key('l', cz::KeyCode::Char, now); + assert(!ctrl.state().loupe_enabled); + assert(ctrl.state().config.ev == 0.5); + ctrl.handle_key('h', cz::KeyCode::Char, now); + assert(ctrl.state().config.ev == 0.0); + // F cycles through all 9 filters and wraps back to None const cz::FilterId expected_filters[9] = { cz::FilterId::BW, cz::FilterId::HiCon, cz::FilterId::Sepia, @@ -461,6 +470,25 @@ void test_video_mode_locks_transitions_while_recording() { assert(ctrl.state().config.ev == ev_before); assert(cz::load_config(cfg_path).ev == ev_before); + // Idle Z toggles Loupe in Video mode too and renders distinctly + assert(!ctrl.state().loupe_enabled); + cz::PreviewFrame vid_live; + assert(ctrl.backend()->copy_latest_preview(0, vid_live)); + cz::AppView vid_view; + vid_view.render(ctrl.state(), &vid_live, nullptr); + std::vector vid_no_loupe( + vid_view.pixels(), + vid_view.pixels() + cz::AppView::kWidth * cz::AppView::kHeight); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(ctrl.state().loupe_enabled); + vid_view.render(ctrl.state(), &vid_live, nullptr); + std::vector vid_with_loupe( + vid_view.pixels(), + vid_view.pixels() + cz::AppView::kWidth * cz::AppView::kHeight); + assert(vid_no_loupe != vid_with_loupe); + ctrl.handle_key('Z', cz::KeyCode::Char, now); + assert(!ctrl.state().loupe_enabled); + // Start recording with Enter ctrl.handle_key(0, cz::KeyCode::Enter, now); assert(ctrl.state().video_recording); @@ -479,9 +507,13 @@ void test_video_mode_locks_transitions_while_recording() { assert(!ctrl.should_quit()); } - // Zoom 5/7 still works live during recording + // Zoom 5/7 and Loupe Z still work live during recording ctrl.handle_key('7', cz::KeyCode::Char, now); assert(ctrl.state().zoom == 1.5f); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(ctrl.state().loupe_enabled); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(!ctrl.state().loupe_enabled); now += 1500; ctrl.tick(now); @@ -818,19 +850,80 @@ void test_text_input_timelapse_custom_recovery_and_modal_renders() { assert(ctrl.state().modal == cz::OverlayModal::TimelapseCustomInput); ctrl.handle_key('1', cz::KeyCode::Char, now); ctrl.handle_key('2', cz::KeyCode::Char, now); - assert(ctrl.state().tl_custom_interval_sec == 12); - ctrl.handle_key(0, cz::KeyCode::Down, now); - ctrl.handle_key(0, cz::KeyCode::Right, now); // 24 -> 30 fps - assert(ctrl.state().tl_custom_fps == 30); - ctrl.handle_key(0, cz::KeyCode::Down, now); - ctrl.handle_key(0, cz::KeyCode::Right, now); // manual -> shots - assert(ctrl.state().tl_custom_stop_mode == "shots"); - view.render(ctrl.state(), &live, nullptr); - ctrl.handle_key(0, cz::KeyCode::Enter, now); - assert(ctrl.state().modal == cz::OverlayModal::None); - assert(ctrl.state().config.timelapse_interval_sec == 12); - assert(ctrl.state().config.timelapse_fps == 30); - assert(ctrl.state().config.timelapse_stop_mode == "shots"); + assert(ctrl.state().tl_custom_interval_sec == 12); + ctrl.handle_key(0, cz::KeyCode::Down, now); + // Custom FPS supports +/-1 stepping AND direct 1..30 numeric typing + assert(ctrl.state().tl_custom_fps == 24); + ctrl.handle_key(0, cz::KeyCode::Right, now); + assert(ctrl.state().tl_custom_fps == 25); + ctrl.handle_key('h', cz::KeyCode::Char, now); + assert(ctrl.state().tl_custom_fps == 24); + ctrl.handle_key('1', cz::KeyCode::Char, now); + ctrl.handle_key('2', cz::KeyCode::Char, now); + assert(ctrl.state().tl_custom_fps == 12); + ctrl.handle_key(0, cz::KeyCode::Backspace, now); + assert(ctrl.state().tl_custom_fps == 1); + ctrl.handle_key(0, cz::KeyCode::Backspace, now); + ctrl.handle_key('3', cz::KeyCode::Char, now); + ctrl.handle_key('5', cz::KeyCode::Char, now); + assert(ctrl.state().tl_custom_fps == 30); + ctrl.handle_key(0, cz::KeyCode::Backspace, now); + ctrl.handle_key(0, cz::KeyCode::Backspace, now); + ctrl.handle_key('7', cz::KeyCode::Char, now); + assert(ctrl.state().tl_custom_fps == 7); + ctrl.handle_key(0, cz::KeyCode::Down, now); + ctrl.handle_key(0, cz::KeyCode::Right, now); // manual -> shots + assert(ctrl.state().tl_custom_stop_mode == "shots"); + view.render(ctrl.state(), &live, nullptr); + ctrl.handle_key(0, cz::KeyCode::Enter, now); + assert(ctrl.state().modal == cz::OverlayModal::None); + assert(ctrl.state().config.timelapse_interval_sec == 12); + assert(ctrl.state().config.timelapse_fps == 7); + assert(cz::load_config(cfg_path).timelapse_fps == 7); + assert(ctrl.state().config.timelapse_stop_mode == "shots"); + + // Timelapse idle & active zoom (5/7) and Loupe (Z) work cleanly + assert(ctrl.state().zoom == 1.0f); + assert(!ctrl.state().loupe_enabled); + ctrl.handle_key('7', cz::KeyCode::Char, now); + assert(ctrl.state().zoom == 1.5f); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(ctrl.state().loupe_enabled); + view.render(ctrl.state(), &live, nullptr); + std::vector tl_with_loupe( + view.pixels(), view.pixels() + cz::AppView::kWidth * cz::AppView::kHeight); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(!ctrl.state().loupe_enabled); + view.render(ctrl.state(), &live, nullptr); + std::vector tl_no_loupe( + view.pixels(), view.pixels() + cz::AppView::kWidth * cz::AppView::kHeight); + assert(tl_with_loupe != tl_no_loupe); + + // Also verify zoom & Loupe stay responsive while Timelapse is actively running + ctrl.handle_key(0, cz::KeyCode::Enter, now); + ctrl.tick(now); + assert(ctrl.state().timelapse.running); + ctrl.handle_key('7', cz::KeyCode::Char, now); + assert(ctrl.state().zoom == 2.0f); + ctrl.handle_key('5', cz::KeyCode::Char, now); + assert(ctrl.state().zoom == 1.5f); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(ctrl.state().loupe_enabled); + ctrl.handle_key('z', cz::KeyCode::Char, now); + assert(!ctrl.state().loupe_enabled); + ctrl.handle_key(0, cz::KeyCode::Esc, now); + assert(ctrl.state().modal == cz::OverlayModal::TimelapseStopConfirm); + ctrl.handle_key('y', cz::KeyCode::Char, now); + { + const auto dl = + std::chrono::steady_clock::now() + std::chrono::seconds(8); + while (ctrl.state().modal == cz::OverlayModal::TimelapseCompiling && + std::chrono::steady_clock::now() < dl) { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + now += 20; + ctrl.tick(now); + } + } // Settings -> DirPicker -> [T] TextInput manual directory entry ctrl.handle_key('s', cz::KeyCode::Char, now); diff --git a/cam-helper/czcamera_cam/picam2_worker.py b/cam-helper/czcamera_cam/picam2_worker.py index 6033460..ce6af34 100644 --- a/cam-helper/czcamera_cam/picam2_worker.py +++ b/cam-helper/czcamera_cam/picam2_worker.py @@ -614,8 +614,11 @@ class Picam2Worker: self._camera_properties, transform_cls=self._transform_cls, ) + crop_aspect = (16, 9) if _is_16_9_mode(self._mode, req_w, req_h) else (4, 3) still_controls = { - "ScalerCrop": compute_scaler_crop(self._sensor_size, 4, 3, self._zoom), + "ScalerCrop": compute_scaler_crop( + self._sensor_size, crop_aspect[0], crop_aspect[1], self._zoom + ), "ExposureValue": float(self._ev), } still_cfg = self._picam2.create_still_configuration( diff --git a/docs/architecture-and-protocol.md b/docs/architecture-and-protocol.md index 64710e4..2120986 100644 --- a/docs/architecture-and-protocol.md +++ b/docs/architecture-and-protocol.md @@ -89,7 +89,7 @@ Every preview frame packet starts with a fixed **16-byte little-endian** header - **Sony IMX219 Sensor Modes:** `640×480` (4:3, crop `(1000,752)/1280×960`), `1640×1232` (4:3 full-FOV 2×2 binned), `1920×1080` (16:9 crop), `3280×2464` (4:3 8MP full sensor). - **Video Output (`FfmpegOutput`):** `_FfmpegOutputTarget` overrides `.split()` cleanly around `picamera2.outputs.FfmpegOutput`'s whitespace `.split()` so staging paths containing spaces or special characters (and ending in `.mp4.part`, with `-f mp4 -movflags +faststart`) remain exact `argv` tokens. -- **Timelapse Compilation:** [`TimelapseEngine`](../app/src/timelapse_engine.hpp) invokes `ffmpeg` with `-vf scale=1440:1080:flags=lanczos` for `8MP` sessions to stay under VideoCore IV's `2048px` H.264 limit, tries `h264_v4l2m2m` (`6M`, `yuv420p`), and retries automatically with `libx264 -preset veryfast -crf 22 -pix_fmt yuv420p` on failure. +- **Timelapse Compilation:** [`TimelapseEngine`](../app/src/timelapse_engine.hpp) invokes `ffmpeg` with `-threads 1 -filter_threads 1` and scales `4:3` (`2mp`/`8mp`) to `1024:768` or `16:9` (`1080p`) to `1280:720` (`bilinear,setsar=1`) to stay safely within CardputerZero's 512 MB RAM / 64 MB CMA budget and VideoCore IV `/dev/video11` constraints, encoding with `h264_v4l2m2m` (`-num_output_buffers 4`, `5M`, `yuv420p`) and falling back to low-memory `libx264` (`-threads 2 -preset ultrafast -crf 23 -x264-params rc-lookahead=0:ref=1:bframes=0:threads=2:lookahead-threads=1:sync-lookahead=0`) on failure. - **Gallery Playback:** [`VideoPlayer`](../app/src/video_player.hpp) probes duration & audio presence via `ffprobe`, spawns `ffmpeg` raw `rgb24` frames timed cleanly to PTS, and drives audio through ALSA `aplay` on Linux or `set_pcm_sink` (SDL2 audio queue) on desktop—allowing silent `.mp4` files (compiled timelapses or muted clips) to play smoothly without audio failure. --- diff --git a/docs/usage-and-controls.md b/docs/usage-and-controls.md index 416d868..aca9ca2 100644 --- a/docs/usage-and-controls.md +++ b/docs/usage-and-controls.md @@ -21,8 +21,8 @@ The `320×170` canvas splits into two regions whose horizontal side swaps dynami ### Photo Mode (`1`) - **Resolutions (`R` or card 0):** `8MP` (`3280×2464` JPEG) default, `2MP` (`1640×1232` JPEG). - **Digital Crop Zoom vs. Viewfinder Loupe:** - - **`5` / `7`**: Steps ISP `ScalerCrop` (`1.0x`, `1.5x`, `2.0x`, `3.0x`, `4.0x`). Both the live preview and captured JPEG are cropped cleanly to that region. - - **`L`** (or `l` on Photo idle view): Toggles a **`2x` Viewfinder-Only Loupe** for checking framing/focus without cropping the saved full-resolution photo. + - **`5` / `7`**: Steps ISP `ScalerCrop` (`1.0x`, `1.5x`, `2.0x`, `3.0x`, `4.0x`). Both the live preview and captured media are cropped cleanly to that region (active across Photo, Video, and Timelapse). + - **`Z`**: Toggles a **`2x` Viewfinder-Only Loupe** for checking framing/focus in Photo, Video, or Timelapse (idle or active capture) without cropping saved images/frames. Lowercase `h` / `l` remain dedicated to `Left` / `Right` `hjkl` navigation everywhere. - **Exposure Value (card 1, `Left`/`Right`):** `-2.0` to `+2.0` in `0.5` EV steps. - **Photo Filters (`F` or card 2):** Cycles all 9 filters live on the preview and applies them on JPEG save (`None` writes the sensor JPEG directly with zero re-encode): `None` (`none`), `B&W` (`bw`), `Hi-Con` (`hicon`), `Sepia` (`sepia`), `Cool` (`cool`), `Vivid` (`vivid`), `Invert` (`invert`), `Retro` (`retro`), `GameBoy` (`gameboy`, scaled integer blocks with 4-shade Bayer DMG palette). @@ -32,7 +32,7 @@ The `320×170` canvas splits into two regions whose horizontal side swaps dynami ### Video Mode (`2`) - **Resolutions (`R` or card 0, while idle):** `720p` (`1280×720` @ 30fps, default), `1080p` (`1920×1080` @ 30fps), `480p` (`640×480` @ 30fps). - **Audio (`M` or card 1, while idle):** ES8389 MEMS microphone ON by default (`audio=true`). If ALSA capture is unavailable or busy at recording start, `czcamera-cam` emits `audio_unavailable`, shows a warning toast, and falls back to muted `.mp4` recording instead of aborting. -- **Live Zoom (`5` / `7` or card 2):** Active both before and during recording (`1.0x`–`4.0x`). +- **Live Zoom & Loupe (`5` / `7` and `Z`):** Zoom (`1.0x`–`4.0x`) and `2x` Loupe remain active both before and during recording. - **Storage Indicator (card 3):** Displays read-only `FREE` space remaining on the target mount (`100 MB` minimum enforced every 2 seconds while recording; auto-stops safely if space drops below `100 MB`). - **Recording Lock:** While `● REC` is active, mode switches (`1`/`2`/`3`), `[8]` Gallery, `[S]` Settings, `[W]` rail flip, `[T]` theme toggle, `[M]`/`[R]`, and `Q` are locked out. Press `Enter`, `Space`, `6`, or `Esc` to stop recording cleanly. @@ -42,11 +42,12 @@ The `320×170` canvas splits into two regions whose horizontal side swaps dynami - **Interval (card 0):** Cycles `2s`, `5s` (default), `10s`, `30s`, `60s`, `300s` (or any custom `1..3600s` via `[C]` / `[P]`). - **Output Mode (card 1):** `video` (`TL_.mp4`, default) or `photos` (`TL_/frame_0001.jpg`...). - **Stop Condition (card 3):** `Manual` (default), `60 shots`, `120 shots`, `300 shots`, `15 min`, `30 min`, `60 min` (or custom via `[C]` / `[P]`). -- **Custom Input Modal (`C` or `P` while idle):** Adjusts exact Interval (`1..3600s`), FPS (`15`, `24`, `30`), Stop Mode (`manual` / `shots` / `duration`), and Stop Value (`1..9999` shots or `1..1440` min) with numeric digits or `Left`/`Right`. `Enter` saves and closes; `Esc` discards. -- **MP4 Compilation & VideoCore IV Compliance:** +- **Live Zoom & Loupe (`5` / `7` and `Z`):** Adjust crop zoom (`1.0x`–`4.0x`) or toggle `2x` viewfinder Loupe both before and during active timelapse capture. +- **Custom Input Modal (`C` or `P` while idle):** Adjusts exact Interval (`1..3600s`), Video FPS (`1..30` via `Left`/`Right`/`h`/`l` `±1` stepping or typing digits `0-9` + `Backspace`), Stop Mode (`manual` / `shots` / `duration`), and Stop Value (`1..9999` shots or `1..1440` min). `Enter` saves and closes; `Esc` discards. +- **MP4 Compilation & CM0 Memory/V4L2 Compliance:** - Frames stage inside `.tl_work_/` with a `manifest.json` heartbeat. - - On stop/completion in `video` mode, `ffmpeg` prefers hardware `h264_v4l2m2m` (`6M`, `yuv420p`) and automatically falls back to `libx264 -preset veryfast -crf 22 -pix_fmt yuv420p` if hardware encoding fails. - - Because Raspberry Pi VideoCore IV's hardware H.264 encoder caps dimensions at `2048px`, **`8MP` (`3280×2464`) frames automatically scale to `1440×1080` (`lanczos`)** during MP4 compile (while individual `Photos`-mode frames remain full `8MP`). + - On stop/completion in `video` mode, `ffmpeg` runs single-threaded input/filter decode (`-threads 1 -filter_threads 1`), scales frames cleanly to CM0 hardware-friendly bounds (`1024×768` bilinear for `4:3` `2mp`/`8mp`, `1280×720` bilinear for `16:9` `1080p`), encodes via `h264_v4l2m2m` (`-num_output_buffers 4`, `5M`, `yuv420p`), and automatically falls back to low-RAM `libx264` (`-threads 2 -preset ultrafast -crf 23 -x264-params rc-lookahead=0:ref=1:bframes=0:...`) if hardware init fails. + - Individual `Photos`-mode frames remain full native capture resolution (`2MP`, `1080p`, or `8MP`). - Press `Esc` during `TimelapseCompiling` to cancel compilation cleanly and preserve the raw frames in a non-hidden `TL_/` folder. - **Auto-Dim & Safe Wake:** - When capturing, the screen dims after `timelapse_dim_sec` idle seconds (`15s` default; configurable `15s`/`30s`/`60s`/`Off` in Settings) via `/sys/class/backlight/*/brightness` (or a software black frame fallback on desktop/headless). @@ -104,7 +105,7 @@ Persisted atomically to `~/.config/czcamera/config.json` (override path with `CZ | `timelapse_resolution` | `"2mp"` | `"2mp"`, `"1080p"`, `"8mp"` | | `timelapse_interval_sec` | `5` | `1..3600` (seconds) | | `timelapse_output_mode` | `"video"` | `"video"`, `"photos"` | -| `timelapse_fps` | `24` | `15`, `24`, `30` | +| `timelapse_fps` | `24` | `1..30` | | `timelapse_stop_mode` | `"manual"` | `"manual"`, `"shots"`, `"duration"` | | `timelapse_stop_value` | `0` | `>= 0` (shot count or seconds when `"duration"`) | | `timelapse_dim_sec` | `15` | `0..3600` (`0` = never auto-dim) |