app+docs: 第三十八轮——raw 录制调试开关+逐级重放定案(参考减除消 98.4%,官方 13 函数二次核对无差异,NUC 域残差差 1.6×);显示链对齐官方两级(nativeOut 去 2× 内插 + 640×480 中间级低通),123 单测全绿
This commit is contained in:
@@ -93,6 +93,24 @@ class RenderPipeline(
|
||||
* chain keeps the raw tail value.
|
||||
*/
|
||||
var fpaTempFix: Int = 0,
|
||||
|
||||
/**
|
||||
* Emit the native 160x120 palette output instead of the PC-SDK's 2x
|
||||
* upscaled 320x240 buffer.
|
||||
*
|
||||
* The official ANDROID app has no intermediate scaling stage at all: the SDK
|
||||
* hands `ImageViewer` a `Bitmap.createBitmap(fpaWidth, fpaHeight)` and the app
|
||||
* draws it once with a filtering paint (`PaintFlagsDrawFilter` +
|
||||
* FILTER_BITMAP, jadx) — a single filtered scale from the sensor grid to the
|
||||
* screen. Our port kept the PC-SDK's `upscale2x` (bilinear, even rows keep
|
||||
* the sample, odd rows get the neighbour average), which fabricates an
|
||||
* interleaved full/half-amplitude pattern the official pipeline does not
|
||||
* have — measured as the row-direction weave in round 38
|
||||
* (docs/android_app/rendering_diff_phone_20261006.md §17). true skips that
|
||||
* stage: the consumer (LiveRenderer) does ONE filtered draw to the screen,
|
||||
* exactly like the official viewer.
|
||||
*/
|
||||
var nativeOut: Boolean = false,
|
||||
) {
|
||||
private val npix = w * h
|
||||
|
||||
@@ -189,8 +207,8 @@ class RenderPipeline(
|
||||
private var meanPrev = 0
|
||||
|
||||
// ---- output ----
|
||||
val outWidth: Int = w * 2
|
||||
val outHeight: Int = h * 2
|
||||
val outWidth: Int get() = if (nativeOut) w else w * 2
|
||||
val outHeight: Int get() = if (nativeOut) h else h * 2
|
||||
|
||||
private val lock = Any()
|
||||
|
||||
@@ -831,6 +849,16 @@ class RenderPipeline(
|
||||
if (enhanceStrength > 0) {
|
||||
detailEnhance.enhance(nuc, gray160, enhanceStrength, detailGain())
|
||||
}
|
||||
if (nativeOut) {
|
||||
// official android path: no intermediate scaling stage — the SDK's
|
||||
// output IS the sensor-grid image; the consumer scales it once
|
||||
val pal = this.pal
|
||||
for (i in 0 until npix) {
|
||||
outArgb[i] = pal[gray160[i].toInt() and 0xFF]
|
||||
}
|
||||
renderedOnce = true
|
||||
return true
|
||||
}
|
||||
upscale2x()
|
||||
// palette colorize 320x240 -> ARGB
|
||||
val pal = this.pal
|
||||
|
||||
@@ -56,8 +56,12 @@ class LiveRenderer(
|
||||
}
|
||||
|
||||
private val density = surfaceView.resources.displayMetrics.density
|
||||
private val bitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.ARGB_8888)
|
||||
private val paint = Paint(Paint.FILTER_BITMAP_FLAG)
|
||||
// Buffer size follows the pipeline output (160x120 native since round 38);
|
||||
// rebuilt lazily because the session may connect after this renderer.
|
||||
private var bitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.ARGB_8888)
|
||||
private var bufW = 320
|
||||
private var bufH = 240
|
||||
private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
color = Color.WHITE
|
||||
typeface = Typeface.SANS_SERIF
|
||||
@@ -78,7 +82,7 @@ class LiveRenderer(
|
||||
* exactly on the frames the user is watching. The flipped frame, the marker
|
||||
* list and the colour-bar segments are all recycled instead.
|
||||
*/
|
||||
private val flipped = IntArray(320 * 240)
|
||||
private var flipped = IntArray(320 * 240)
|
||||
private val marks = ArrayList<com.mag160c.thermal.media.MarkerPainter.Mark>(8)
|
||||
private val barPaint = Paint()
|
||||
private val srcRect = android.graphics.Rect()
|
||||
@@ -86,6 +90,35 @@ class LiveRenderer(
|
||||
private var barBitmap: Bitmap? = null
|
||||
private var barPaletteIdx = -1
|
||||
|
||||
/** Match the draw bitmap to the pipeline's output size (once per change). */
|
||||
private fun ensureBuffer(w: Int, h: Int): Boolean {
|
||||
if (w == bufW && h == bufH) return true
|
||||
if (w <= 0 || h <= 0) return false
|
||||
bufW = w; bufH = h
|
||||
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
|
||||
flipped = IntArray(w * h)
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 640x480 offscreen stage, mirroring the official viewer's display chain:
|
||||
* jadx `ImageViewer` draws the 160x120 SDK bitmap into a 640x480 offscreen
|
||||
* bitmap with the filtering paint (PaintFlagsDrawFilter + FILTER_BITMAP) and
|
||||
* only the SCREEN scales that further. The intermediate 4x bilinear is what
|
||||
* attenuates the per-sensor-row FPN pattern on the screen (measured, §17:
|
||||
* with it the row pattern lands on the official's 0.12e-2 arc; without, our
|
||||
* single-scale render shows it at full amplitude). The stage costs one GPU
|
||||
* blit per frame.
|
||||
*/
|
||||
private var mid: Bitmap? = null
|
||||
private val midRect = android.graphics.RectF(0f, 0f, 640f, 480f)
|
||||
|
||||
private fun ensureMid(): Bitmap {
|
||||
mid?.let { return it }
|
||||
return Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888).also { mid = it }
|
||||
}
|
||||
|
||||
|
||||
private companion object {
|
||||
const val BAR_SEGMENTS = 96
|
||||
}
|
||||
@@ -180,6 +213,8 @@ class LiveRenderer(
|
||||
val h = canvas.height.toFloat()
|
||||
canvas.drawColor(Color.BLACK)
|
||||
val frame = vm.latestFrame ?: return
|
||||
if (!ensureBuffer(vm.bufferW(), vm.bufferH())) return
|
||||
if (frame.size < bufW * bufH) return
|
||||
setFramePixels(frame)
|
||||
|
||||
// available area (minus UI overlays)
|
||||
@@ -199,10 +234,12 @@ class LiveRenderer(
|
||||
|
||||
val zoom = vm.state.value.zoom
|
||||
val crop = ImageTransform.cropForZoom(zoom)
|
||||
// The zoom source rect now addresses the 640x480 middle stage (4x the
|
||||
// buffer coordinates), exactly like the official viewer's offscreen.
|
||||
val src: android.graphics.Rect? = if (zoom > 1) {
|
||||
val cw = (320 * (crop[2] - crop[0])).toInt()
|
||||
val ch = (240 * (crop[3] - crop[1])).toInt()
|
||||
srcRect.set(160 - cw / 2, 120 - ch / 2, 160 + cw / 2, 120 + ch / 2)
|
||||
val cw = (640 * (crop[2] - crop[0])).toInt()
|
||||
val ch = (480 * (crop[3] - crop[1])).toInt()
|
||||
srcRect.set(320 - cw / 2, 240 - ch / 2, 320 + cw / 2, 240 + ch / 2)
|
||||
srcRect
|
||||
} else null
|
||||
|
||||
@@ -217,8 +254,14 @@ class LiveRenderer(
|
||||
fit.cx - size[0] / 2f, fit.cy - size[1] / 2f,
|
||||
fit.cx + size[0] / 2f, fit.cy + size[1] / 2f,
|
||||
)
|
||||
if (src != null) canvas.drawBitmap(bitmap, src, dstRect, paint)
|
||||
else canvas.drawBitmap(bitmap, null, dstRect, paint)
|
||||
// two-step display like the official viewer: buffer -> 640x480 (filtered)
|
||||
// -> screen. The GPU blits are cheap; the attenuated row pattern is the point.
|
||||
val m = ensureMid()
|
||||
val mc = Canvas(m)
|
||||
mc.drawColor(Color.BLACK)
|
||||
mc.drawBitmap(bitmap, null, midRect, paint)
|
||||
if (src != null) canvas.drawBitmap(m, src, dstRect, paint)
|
||||
else canvas.drawBitmap(m, null, dstRect, paint)
|
||||
canvas.restore()
|
||||
|
||||
drawOsd(canvas, vm.state.value)
|
||||
@@ -236,21 +279,23 @@ class LiveRenderer(
|
||||
*/
|
||||
private fun setFramePixels(frame: IntArray) {
|
||||
val p = vm.imageParams()
|
||||
val bw = bufW
|
||||
val bh = bufH
|
||||
if (!p.flipH && !p.flipV) {
|
||||
bitmap.setPixels(frame, 0, 320, 0, 0, 320, 240)
|
||||
bitmap.setPixels(frame, 0, bw, 0, 0, bw, bh)
|
||||
return
|
||||
}
|
||||
for (y in 0 until 240) {
|
||||
val sy = if (p.flipV) 239 - y else y
|
||||
val srcRow = sy * 320
|
||||
val dstRow = y * 320
|
||||
for (y in 0 until bh) {
|
||||
val sy = if (p.flipV) bh - 1 - y else y
|
||||
val srcRow = sy * bw
|
||||
val dstRow = y * bw
|
||||
if (!p.flipH) {
|
||||
System.arraycopy(frame, srcRow, flipped, dstRow, 320)
|
||||
System.arraycopy(frame, srcRow, flipped, dstRow, bw)
|
||||
} else {
|
||||
for (x in 0 until 320) flipped[dstRow + x] = frame[srcRow + (319 - x)]
|
||||
for (x in 0 until bw) flipped[dstRow + x] = frame[srcRow + (bw - 1 - x)]
|
||||
}
|
||||
}
|
||||
bitmap.setPixels(flipped, 0, 320, 0, 0, 320, 240)
|
||||
bitmap.setPixels(flipped, 0, bw, 0, 0, bw, bh)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -120,12 +120,13 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
const val PIP_RIGHT_MARGIN_DP = 32
|
||||
|
||||
/**
|
||||
* Video is encoded at this multiple of the sensor frame (3x = 960x720, or
|
||||
* 720x960 in portrait). The sensor data is only 160x120 either way, so this
|
||||
* buys nothing for the image — it is what makes the burned-in readouts sharp,
|
||||
* which is the whole point: at 1x the temperature text was visibly blurry.
|
||||
* Video is encoded at this multiple of the RENDER BUFFER (native 160x120
|
||||
* since round 38: 6x = 960x720, or 720x960 in portrait). The sensor data
|
||||
* is only 160x120 either way, so this buys nothing for the image — it is
|
||||
* what makes the burned-in readouts sharp, which is the whole point: at
|
||||
* 1x the temperature text was visibly blurry.
|
||||
*/
|
||||
const val RECORD_SCALE = 3
|
||||
const val RECORD_SCALE = 6
|
||||
}
|
||||
|
||||
private var usbReceiver: android.content.BroadcastReceiver? = null
|
||||
@@ -280,8 +281,10 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
session.recorderHook = { argb ->
|
||||
val rec = recorder
|
||||
if (rec != null && rec.isRecording()) {
|
||||
val bmp = android.graphics.Bitmap.createBitmap(320, 240, android.graphics.Bitmap.Config.ARGB_8888)
|
||||
bmp.setPixels(argb, 0, 320, 0, 0, 320, 240)
|
||||
val bw = session.outW()
|
||||
val bh = session.outH()
|
||||
val bmp = android.graphics.Bitmap.createBitmap(bw, bh, android.graphics.Bitmap.Config.ARGB_8888)
|
||||
bmp.setPixels(argb, 0, bw, 0, 0, bw, bh)
|
||||
// Hand over the CURRENT annotations so the recording shows the same
|
||||
// markers and temperatures as the screen. Read per frame because the
|
||||
// user can add or remove probes while recording.
|
||||
@@ -554,6 +557,10 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
|
||||
fun triggerFfc() = session.triggerFfc()
|
||||
|
||||
/** Live buffer size the renderer must allocate (native 160x120 since round 38). */
|
||||
fun bufferW(): Int = session.outW()
|
||||
fun bufferH(): Int = session.outH()
|
||||
|
||||
fun setPalette(index: Int) {
|
||||
session.setPalette(index)
|
||||
_state.value = _state.value.copy(paletteIndex = index)
|
||||
@@ -659,11 +666,13 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
* only in the presentation layer.
|
||||
*/
|
||||
/**
|
||||
* Exact 160x120 sensor-grid copy of the 320x240 display buffer. The pipeline's
|
||||
* 2x upscale keeps the source value at every even index (`o[2x] = src[x]`), so
|
||||
* Exact 160x120 sensor-grid copy of the display buffer. With the native
|
||||
* output (round 38) the buffer IS the sensor grid; the 2x-upscaled 320x240
|
||||
* variant keeps the source value at every even index (`o[2x] = src[x]`), so
|
||||
* sampling (2x, 2y) recovers the native pixels without any resampling.
|
||||
*/
|
||||
private fun sensorFrame(display: IntArray): IntArray {
|
||||
if (display.size == 160 * 120) return display.copyOf()
|
||||
val out = IntArray(160 * 120)
|
||||
var i = 0
|
||||
for (y in 0 until 120) {
|
||||
@@ -874,18 +883,20 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
if (rec == null) {
|
||||
// Same orientation and scale policy as a photo, so the recording matches
|
||||
// the stills: the capture rotation (live rotation + physical grip) is
|
||||
// baked in and the frame is encoded at RECORD_SCALE x the sensor size.
|
||||
// Encoding at the native 320x240 made the burned-in readouts blurry once
|
||||
// the video was viewed full screen ("视频里面的温度标点糊糊的").
|
||||
// baked in and the frame is encoded at RECORD_SCALE x the render buffer
|
||||
// (native 160x120). Encoding at the native size made the burned-in
|
||||
// readouts blurry once the video was viewed full screen.
|
||||
//
|
||||
// The grip is sampled once, at start: changing hands mid-clip would need
|
||||
// mid-stream rotation, and the encoder's dimensions are fixed. Every
|
||||
// camera behaves this way.
|
||||
val rot = captureRotationDeg()
|
||||
val portrait = rot == 90 || rot == 270
|
||||
val bw = session.outW()
|
||||
val bh = session.outH()
|
||||
val r = com.mag160c.thermal.media.Mp4Recorder(
|
||||
width = if (portrait) 240 * RECORD_SCALE else 320 * RECORD_SCALE,
|
||||
height = if (portrait) 320 * RECORD_SCALE else 240 * RECORD_SCALE,
|
||||
width = if (portrait) bh * RECORD_SCALE else bw * RECORD_SCALE,
|
||||
height = if (portrait) bw * RECORD_SCALE else bh * RECORD_SCALE,
|
||||
).apply {
|
||||
mirror = com.mag160c.thermal.media.PhotoSaver.Mirror(flipH, flipV)
|
||||
rotDeg = rot
|
||||
@@ -959,6 +970,7 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
* state on the main dispatcher.
|
||||
*/
|
||||
private suspend fun computeAndPublishTemps() {
|
||||
pollRawCapFlag()
|
||||
if (uiTick++ % 12 == 0) {
|
||||
// ~5 s heartbeat: what the UI currently sees (debug round 11)
|
||||
com.mag160c.thermal.media.DebugLog.log(
|
||||
@@ -1033,6 +1045,84 @@ class LiveViewModel(app: Application) : AndroidViewModel(app) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- debug raw-frame capture (round 38, rendering_diff_phone_20261006.md §16.3) ----
|
||||
//
|
||||
// The residual row-FPN investigation needs the RAW sensor frames (raw AND the
|
||||
// closed-shutter reference frames of a full FFC window) replayed offline. The
|
||||
// switch is a flag FILE rather than UI or a broadcast receiver: `adb shell
|
||||
// su -c 'touch /data/data/com.mag160c.thermal/files/rawcap.enable'` starts a
|
||||
// capture (and fires a manual FFC so the window is surely inside), deleting the
|
||||
// flag stops early; the capture also stops by itself after RawFrameRecorder's
|
||||
// frame budget and clears the flag. Progress lands in `rawcap.state`.
|
||||
|
||||
@Volatile
|
||||
private var rawCap: com.mag160c.thermal.usb.RawFrameRecorder? = null
|
||||
|
||||
/** Scratch for the NUC snapshot handed to the recorder (reader thread only). */
|
||||
private val rawCapNuc = IntArray(19200)
|
||||
|
||||
private fun rawCapFlag(): java.io.File =
|
||||
java.io.File(getApplication<Application>().filesDir, "rawcap.enable")
|
||||
|
||||
private fun rawCapState(line: String) {
|
||||
try {
|
||||
java.io.File(
|
||||
getApplication<Application>().filesDir, "rawcap.state",
|
||||
).writeText(line + "\n", Charsets.UTF_8)
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Poll (400 ms, from the temperature loop): start/stop the capture per the flag. */
|
||||
private fun pollRawCapFlag() {
|
||||
val active = rawCap
|
||||
val flag = rawCapFlag()
|
||||
if (active == null) {
|
||||
if (flag.exists()) startRawCap()
|
||||
} else if (!flag.exists()) {
|
||||
// flag removed while recording -> early stop (frames so far are kept)
|
||||
stopRawCap("flag_removed")
|
||||
}
|
||||
}
|
||||
|
||||
private fun startRawCap() {
|
||||
if (rawCap != null) return
|
||||
val ctx = getApplication<Application>()
|
||||
val dir = java.io.File(ctx.filesDir, "rawcap").apply { mkdirs() }
|
||||
val name = "run_" + java.text.SimpleDateFormat(
|
||||
"yyyyMMdd_HHmmss", java.util.Locale.US,
|
||||
).format(java.util.Date())
|
||||
val rec = com.mag160c.thermal.usb.RawFrameRecorder(java.io.File(dir, name))
|
||||
rawCap = rec
|
||||
session.rawFrameSink = { raw, phase, shutter, rendered ->
|
||||
val r = rawCap
|
||||
if (r != null) {
|
||||
val hasNuc = rendered && session.copyNuc(rawCapNuc)
|
||||
val more = r.offer(raw, phase, shutter, rendered, if (hasNuc) rawCapNuc else null)
|
||||
if (!more) stopRawCap("frames_done")
|
||||
}
|
||||
}
|
||||
rawCapState("running $name")
|
||||
com.mag160c.thermal.media.DebugLog.log(
|
||||
"vm", "rawcap started -> $name (max ${com.mag160c.thermal.usb.RawFrameRecorder.DEFAULT_MAX_FRAMES} frames)",
|
||||
)
|
||||
// make sure a full FFC window is inside the capture; a no-op while no
|
||||
// session is up (then the automatic first-frame FFC of a new connection
|
||||
// is captured instead)
|
||||
session.triggerFfc()
|
||||
}
|
||||
|
||||
private fun stopRawCap(reason: String) {
|
||||
val rec = rawCap ?: return
|
||||
rawCap = null
|
||||
session.rawFrameSink = null
|
||||
rec.close()
|
||||
rawCapFlag().delete()
|
||||
val line = "done ${rec.file.name} frames=${rec.frameCount} reason=$reason path=${rec.file.absolutePath}"
|
||||
rawCapState(line)
|
||||
com.mag160c.thermal.media.DebugLog.log("vm", "rawcap $line")
|
||||
}
|
||||
|
||||
private var uiTick = 0
|
||||
|
||||
override fun onCleared() {
|
||||
|
||||
@@ -110,6 +110,15 @@ class IrSession(context: Context) {
|
||||
@Volatile
|
||||
var rawHook: ((ByteArray) -> Unit)? = null
|
||||
|
||||
/**
|
||||
* Optional debug raw-frame capture sink (see [RawFrameRecorder]), runs on the
|
||||
* reader thread after the pipeline processed the frame, so phase/shutter
|
||||
* describe exactly this frame. The `raw` buffer is reused for the next frame —
|
||||
* copy or write it out synchronously.
|
||||
*/
|
||||
@Volatile
|
||||
var rawFrameSink: ((raw: ByteArray, phase: Int, shutter: Int, rendered: Boolean) -> Unit)? = null
|
||||
|
||||
fun isStreaming(): Boolean = running.get()
|
||||
|
||||
/** Latest raw frame (with 0x38-byte header) for MDT capture. */
|
||||
@@ -158,6 +167,10 @@ class IrSession(context: Context) {
|
||||
|
||||
fun identitySnapshot(): CameraIdentity = identity.copy()
|
||||
|
||||
/** Output buffer size of the active pipeline (160x120 with nativeOut). */
|
||||
fun outW(): Int = pipeline?.outWidth ?: 320
|
||||
fun outH(): Int = pipeline?.outHeight ?: 240
|
||||
|
||||
/**
|
||||
* Connect + start the live stream. Must be called after USB permission.
|
||||
*
|
||||
@@ -349,6 +362,11 @@ class IrSession(context: Context) {
|
||||
// table interpolation (official UsbCommunication.startTransfer); it is
|
||||
// set from the device's parameter block below
|
||||
fpaTempFix = 0,
|
||||
// official android display path: no intermediate 2x stage inside the
|
||||
// pipeline — the consumer (LiveRenderer) draws the native 160x120
|
||||
// palette image to the screen in ONE filtered scale, exactly like the
|
||||
// official ImageViewer (round 38, rendering_diff §17)
|
||||
nativeOut = true,
|
||||
)
|
||||
if (!pipe.loadDdt(ddt)) {
|
||||
DebugLog.log("session", "ddt_fail (fetched/bundled ${ddt.size} B not loadable)")
|
||||
@@ -606,7 +624,7 @@ class IrSession(context: Context) {
|
||||
loopRunning.set(true)
|
||||
val stream = FrameStream(38400)
|
||||
val frameBuf = ByteArray(0x38 + 38400)
|
||||
val out = IntArray(320 * 240)
|
||||
val out = IntArray(pipe.outWidth * pipe.outHeight)
|
||||
val tmp = ByteArray(0x8000)
|
||||
val noop = ByteArray(0)
|
||||
DebugLog.log("stream", "reader loop start: sync bulkTransfer (${TIMEOUT_MS} ms) + halt recovery")
|
||||
@@ -708,6 +726,7 @@ class IrSession(context: Context) {
|
||||
// camera temperature) refers to THIS frame, which is what a
|
||||
// remote client needs to reproduce the image.
|
||||
lastRawFrame?.let { raw -> rawHook?.invoke(raw) }
|
||||
rawFrameSink?.invoke(frameBuf, pipe.ffcPhase(), pipe.lastShutter(), rendered)
|
||||
if (rendered) {
|
||||
renderCount++
|
||||
listener?.onFrameReady(out)
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.mag160c.thermal.usb
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Debug raw-frame capture (round 38, docs/android_app/rendering_diff_phone_20261006.md
|
||||
* §16.3): writes the streamed USB frames — raw, WITH the 0x38-byte header — to one
|
||||
* binary file so the render chain can be replayed stage by stage OFFLINE
|
||||
* (raw -> reference -> d2 -> NUC), which is how the residual row-FPN we have and the
|
||||
* official app doesn't is being located. The capture must cover a full FFC window
|
||||
* (closed shutter -> 4 reference frames -> table rebuild), so the flag file also
|
||||
* triggers a manual FFC when a session is already streaming (LiveViewModel).
|
||||
*
|
||||
* Not for shipping analysis — a debug tool. Enabled by LiveViewModel through the
|
||||
* `rawcap.enable` flag file; a machine-readable status line lands in `rawcap.state`
|
||||
* so a device test can be driven over adb without the debug log.
|
||||
*
|
||||
* File layout (all integers u32 little-endian):
|
||||
*
|
||||
* global header: ASCII "MAGRCAP1" + u32 width + u32 height (8+4+4 bytes)
|
||||
* per frame record:
|
||||
* u32 magic 0x1BB0CAFE | u32 seq | u32 phase | u32 shutter | u32 flags
|
||||
* [0x38 + 38400] bytes raw USB frame (header included)
|
||||
* [flags & HAS_NUC] u32 0x1BB0C0DE + 38400 bytes NUC counts as u16 LE
|
||||
*
|
||||
* phase mirrors the pipeline state AFTER the frame was processed:
|
||||
* 0 normal / 1 shutter closed / 2 reference collection. The NUC block is only
|
||||
* present for rendered (normal) frames — during an FFC window the pipeline
|
||||
* holds its NUC buffer stale.
|
||||
*/
|
||||
class RawFrameRecorder(
|
||||
val file: File,
|
||||
private val maxFrames: Int = DEFAULT_MAX_FRAMES,
|
||||
) {
|
||||
companion object {
|
||||
/** ~20 s at 15 fps: covers warmup + two full FFC windows with margin. */
|
||||
const val DEFAULT_MAX_FRAMES = 300
|
||||
|
||||
private const val FRAME_MAGIC = 0x1BB0CAFE.toInt()
|
||||
private const val NUC_MAGIC = 0x1BB0C0DE.toInt()
|
||||
|
||||
const val FLAG_RENDERED = 1
|
||||
const val FLAG_HAS_NUC = 2
|
||||
|
||||
const val NUC_PIXELS = 160 * 120
|
||||
const val RAW_FRAME_LEN = 0x38 + 38400
|
||||
|
||||
fun magic(): Int = FRAME_MAGIC
|
||||
fun nucMagic(): Int = NUC_MAGIC
|
||||
}
|
||||
|
||||
private val lock = Any()
|
||||
private var out: FileOutputStream? = null
|
||||
private var seq = 0
|
||||
private var failed = false
|
||||
|
||||
/** Frames accepted so far (opened lazily on the first offer). */
|
||||
val frameCount: Int get() = synchronized(lock) { seq }
|
||||
|
||||
/**
|
||||
* Write one frame record. Synchronous (the caller is the USB reader thread and
|
||||
* the buffer is reused for the next frame). Returns false when the capture is
|
||||
* finished — full, failed, or closed — so the owner can unhook its sink.
|
||||
*/
|
||||
fun offer(frame: ByteArray, phase: Int, shutter: Int, rendered: Boolean, nuc: IntArray?): Boolean =
|
||||
synchronized(lock) {
|
||||
if (failed || seq >= maxFrames) return false
|
||||
val stream = out ?: run {
|
||||
// lazy open: the flag can be set before the camera streams
|
||||
try {
|
||||
file.parentFile?.mkdirs()
|
||||
FileOutputStream(file).also { opened ->
|
||||
out = opened
|
||||
opened.write(HEADER)
|
||||
opened.write(le32(160))
|
||||
opened.write(le32(120))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
failed = true
|
||||
return false
|
||||
}
|
||||
out!!
|
||||
}
|
||||
val hasNuc = rendered && nuc != null && nuc.size >= NUC_PIXELS
|
||||
val flags = (if (rendered) FLAG_RENDERED else 0) or (if (hasNuc) FLAG_HAS_NUC else 0)
|
||||
val head = ByteArray(20)
|
||||
put32(head, 0, FRAME_MAGIC)
|
||||
put32(head, 4, seq)
|
||||
put32(head, 8, phase)
|
||||
put32(head, 12, shutter)
|
||||
put32(head, 16, flags)
|
||||
try {
|
||||
stream.write(head)
|
||||
stream.write(frame, 0, RAW_FRAME_LEN)
|
||||
if (hasNuc) {
|
||||
val nb = ByteArray(4 + NUC_PIXELS * 2)
|
||||
put32(nb, 0, NUC_MAGIC)
|
||||
for (i in 0 until NUC_PIXELS) {
|
||||
val v = nuc[i].coerceIn(0, 0xFFFF)
|
||||
nb[4 + i * 2] = (v and 0xFF).toByte()
|
||||
nb[4 + i * 2 + 1] = ((v shr 8) and 0xFF).toByte()
|
||||
}
|
||||
stream.write(nb)
|
||||
}
|
||||
// flush to the page cache: keeps the file readable while recording
|
||||
// (crash-safe enough for a debug tool) without an fsync per frame
|
||||
stream.flush()
|
||||
} catch (e: Exception) {
|
||||
failed = true
|
||||
return false
|
||||
}
|
||||
seq++
|
||||
seq < maxFrames
|
||||
}
|
||||
|
||||
/** Flush + close. Idempotent. */
|
||||
fun close() {
|
||||
synchronized(lock) {
|
||||
try {
|
||||
out?.flush()
|
||||
out?.close()
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
out = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun le32(v: Int): ByteArray {
|
||||
val b = ByteArray(4)
|
||||
put32(b, 0, v)
|
||||
return b
|
||||
}
|
||||
|
||||
private fun put32(b: ByteArray, off: Int, v: Int) {
|
||||
b[off] = (v and 0xFF).toByte()
|
||||
b[off + 1] = ((v shr 8) and 0xFF).toByte()
|
||||
b[off + 2] = ((v shr 16) and 0xFF).toByte()
|
||||
b[off + 3] = ((v shr 24) and 0xFF).toByte()
|
||||
}
|
||||
|
||||
private val HEADER = "MAGRCAP1".toByteArray(Charsets.US_ASCII)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.mag160c.thermal.core
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Round-38 (rendering_diff_phone_20261006.md §17): the official ANDROID display
|
||||
* path has no intermediate scaling stage — the SDK hands the app a native
|
||||
* 160x120 bitmap and the app draws it once with a filtering paint. The native
|
||||
* output mode must (a) produce exactly the sensor-grid samples of the classic
|
||||
* 320x240 path (the upscale's even indices were always the untouched source
|
||||
* values) and (b) keep the golden 320x240 path untouched by default.
|
||||
*/
|
||||
class NativeOutTest {
|
||||
private fun res(name: String): ByteArray =
|
||||
javaClass.classLoader.getResourceAsStream(name)!!.readBytes()
|
||||
|
||||
private fun run(nativeOut: Boolean): Pair<RenderPipeline, MutableList<IntArray>> {
|
||||
val pipeline = RenderPipeline(
|
||||
w = 160, h = 120,
|
||||
ffcPeriod = 1800, ffcDrift = 250, warmFrames = 20,
|
||||
cdfPivot75 = true, force75 = true,
|
||||
onFfc = {},
|
||||
nativeOut = nativeOut,
|
||||
)
|
||||
assertTrue(pipeline.loadDdt(res("mag160c_official.ddt")))
|
||||
val frames = res("test_frames.bin")
|
||||
val frameSize = 0x38 + 38400
|
||||
val nframes = frames.size / frameSize
|
||||
val w = pipeline.outWidth
|
||||
val h = pipeline.outHeight
|
||||
val out = IntArray(w * h)
|
||||
val rendered = ArrayList<IntArray>()
|
||||
for (f in 0 until nframes) {
|
||||
val fr = frames.copyOfRange(f * frameSize, (f + 1) * frameSize)
|
||||
if (pipeline.frame(fr, true, out)) rendered.add(out.copyOf())
|
||||
}
|
||||
assertTrue("expected rendered frames", rendered.isNotEmpty())
|
||||
return pipeline to rendered
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nativeOutputMatchesEvenSamplesOfThe2xPath() {
|
||||
val (p2x, frames2x) = run(false)
|
||||
assertEquals(320, p2x.outWidth)
|
||||
assertEquals(240, p2x.outHeight)
|
||||
val (pNat, framesNat) = run(true)
|
||||
assertEquals(160, pNat.outWidth)
|
||||
assertEquals(120, pNat.outHeight)
|
||||
assertEquals(frames2x.size, framesNat.size)
|
||||
for (fi in frames2x.indices) {
|
||||
val big = frames2x[fi]
|
||||
val nat = framesNat[fi]
|
||||
assertEquals(160 * 120, nat.size)
|
||||
var i = 0
|
||||
for (y in 0 until 120) {
|
||||
val row = y * 2 * 320
|
||||
for (x in 0 until 160) {
|
||||
assertEquals("frame $fi pixel ($x,$y)", big[row + x * 2], nat[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun defaultPathStays320x240() {
|
||||
val pipeline = RenderPipeline(w = 160, h = 120, onFfc = {})
|
||||
assertEquals(320, pipeline.outWidth)
|
||||
assertEquals(240, pipeline.outHeight)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.mag160c.thermal.usb
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
|
||||
/**
|
||||
* Format lock for the debug raw-frame capture: the offline replay
|
||||
* (rendering_diff_phone_20261006.md §16.3) parses these files with a
|
||||
* Python tool, so the on-disk layout — header string, little-endian u32
|
||||
* fields, NUC block only for rendered frames — must stay exactly as
|
||||
* documented in [RawFrameRecorder].
|
||||
*/
|
||||
class RawFrameRecorderTest {
|
||||
@get:Rule
|
||||
val tmp = TemporaryFolder()
|
||||
|
||||
private fun rawFrame(seq: Int): ByteArray {
|
||||
val f = ByteArray(RawFrameRecorder.RAW_FRAME_LEN)
|
||||
// header marker + sequence at offset 4 (the USB header's counter field)
|
||||
f[0] = 0x55.toByte(); f[1] = 0xAA.toByte()
|
||||
f[4] = (seq and 0xFF).toByte()
|
||||
f[5] = ((seq shr 8) and 0xFF).toByte()
|
||||
// a distinguishable pixel: first u16 of the payload
|
||||
f[0x38] = 0x34; f[0x39] = 0x12
|
||||
return f
|
||||
}
|
||||
|
||||
private fun nucBuffer(seed: Int): IntArray =
|
||||
IntArray(RawFrameRecorder.NUC_PIXELS) { (it + seed) and 0xFFFF }
|
||||
|
||||
@Test
|
||||
fun writesHeaderAndRoundTrips() {
|
||||
val f = File(tmp.root, "run_test.bin")
|
||||
// offer() returns "keep recording": false on the LAST budgeted frame
|
||||
// (which is still written) and on any call after that
|
||||
val rec = RawFrameRecorder(f, maxFrames = 3)
|
||||
val raw0 = rawFrame(0)
|
||||
val raw1 = rawFrame(1)
|
||||
val raw2 = rawFrame(2)
|
||||
assertTrue(rec.offer(raw0, phase = 0, shutter = 11442, rendered = true, nuc = nucBuffer(7)))
|
||||
assertTrue(rec.offer(raw1, phase = 2, shutter = 15084, rendered = false, nuc = null))
|
||||
assertFalse(rec.offer(raw2, phase = 0, shutter = 11444, rendered = true, nuc = nucBuffer(9)))
|
||||
assertFalse(rec.offer(rawFrame(3), 0, 0, true, null)) // budget exhausted
|
||||
rec.close()
|
||||
assertEquals(3, rec.frameCount)
|
||||
|
||||
val bytes = Files.readAllBytes(f.toPath())
|
||||
assertEquals("MAGRCAP1", String(bytes, 0, 8, Charsets.US_ASCII))
|
||||
assertEquals(160, le32(bytes, 8))
|
||||
assertEquals(120, le32(bytes, 12))
|
||||
|
||||
var off = 16
|
||||
// frame 0: rendered with NUC block
|
||||
assertEquals(RawFrameRecorder.magic().toLong(), le32(bytes, off).toLong() and 0xFFFFFFFFL)
|
||||
assertEquals(0, le32(bytes, off + 4))
|
||||
assertEquals(0, le32(bytes, off + 8))
|
||||
assertEquals(11442, le32(bytes, off + 12))
|
||||
assertEquals(RawFrameRecorder.FLAG_RENDERED or RawFrameRecorder.FLAG_HAS_NUC, le32(bytes, off + 16))
|
||||
assertTrue(
|
||||
bytes.copyOfRange(off + 20, off + 20 + 4).contentEquals(
|
||||
raw0.copyOfRange(0, 4),
|
||||
),
|
||||
)
|
||||
off += 20 + RawFrameRecorder.RAW_FRAME_LEN
|
||||
assertEquals(RawFrameRecorder.nucMagic().toLong(), le32(bytes, off).toLong() and 0xFFFFFFFFL)
|
||||
assertEquals(7, le16(bytes, off + 4))
|
||||
assertEquals(9, le16(bytes, off + 4 + 2 * 2))
|
||||
off += 4 + RawFrameRecorder.NUC_PIXELS * 2
|
||||
|
||||
// frame 1: FFC window frame, no NUC block
|
||||
assertEquals(2, le32(bytes, off + 8))
|
||||
assertEquals(15084, le32(bytes, off + 12))
|
||||
assertEquals(0, le32(bytes, off + 16))
|
||||
off += 20 + RawFrameRecorder.RAW_FRAME_LEN
|
||||
|
||||
// frame 2
|
||||
assertEquals(11444, le32(bytes, off + 12))
|
||||
off += 20 + RawFrameRecorder.RAW_FRAME_LEN + 4 + RawFrameRecorder.NUC_PIXELS * 2
|
||||
|
||||
assertEquals(bytes.size, off)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun opensLazilyAndRejectsAfterClose() {
|
||||
val f = File(tmp.root, "run_lazy.bin")
|
||||
val rec = RawFrameRecorder(f, maxFrames = 2)
|
||||
// no file before the first offer (the flag can be set before streaming)
|
||||
assertFalse(f.exists())
|
||||
val nuc = nucBuffer(0)
|
||||
assertTrue(rec.offer(rawFrame(0), 0, 100, true, nuc))
|
||||
assertFalse(rec.offer(rawFrame(1), 0, 101, true, nuc)) // last budgeted frame
|
||||
assertFalse(rec.offer(rawFrame(2), 0, 102, true, nuc)) // rejected
|
||||
assertEquals(2, rec.frameCount)
|
||||
rec.close()
|
||||
rec.close() // idempotent
|
||||
val expectLen = (2 * (20 + RawFrameRecorder.RAW_FRAME_LEN) + 16 +
|
||||
2 * (4 + RawFrameRecorder.NUC_PIXELS * 2)).toLong()
|
||||
assertEquals(expectLen, f.length())
|
||||
}
|
||||
|
||||
private fun le32(b: ByteArray, off: Int): Int =
|
||||
(b[off].toInt() and 0xFF) or
|
||||
((b[off + 1].toInt() and 0xFF) shl 8) or
|
||||
((b[off + 2].toInt() and 0xFF) shl 16) or
|
||||
((b[off + 3].toInt() and 0xFF) shl 24)
|
||||
|
||||
private fun le16(b: ByteArray, off: Int): Int =
|
||||
(b[off].toInt() and 0xFF) or ((b[off + 1].toInt() and 0xFF) shl 8)
|
||||
}
|
||||
Reference in New Issue
Block a user