package com.elphel.imagej.cuas.rt;

import com.sun.jna.Library;
import com.sun.jna.Pointer;

/**
 * JNA boundary to the native LibTorch DNN inference library (libtpdnn.so, tile_processor_gpu/jna/tp_dnn.cpp).
 * Mirrors the C-ABI: load TorchScript L1(+L2), build the temporal pyramid from an uploaded stack, and run
 * shift-and-stitch + decode (+ L2 recurrence/age) per level. Loaded by base name "tpdnn" via
 * {@code Native.load} with {@code -Djna.library.path=<dir with libtpdnn.so>}. By Claude on 06/27/2026.
 */
public interface TpDnnJna extends Library {
	/** Load TorchScript L1 (+optional L2) onto CUDA. l2_path empty/NULL -> L1-only. Returns an opaque ctx. */
	Pointer tpdnn_init(String l1_path, String l2_path, int N, int P, int vr, int l2_ch);

	/** Build the temporal pyramid from host log [T,H,W] (row-major float32). */
	void tpdnn_upload(Pointer ctx, float[] log, int T, int H, int W);

	int tpdnn_num_levels(Pointer ctx);
	int tpdnn_level_frames(Pointer ctx, int level);

	/** One INFER over `count` scenes of `level`. Writes offset5 into out_o5 [count*nch*H*W] and roi into
	 *  out_roi [count*rh*rw*nvel] (native float order). Returns nch (5 L1-only, 6 with L2:+age). */
	int tpdnn_infer(Pointer ctx, int level, int start, int count, int stride,
			int rx, int ry, int rw, int rh, double rmax,
			int l2_enable, int l2_reset, double noise_scale,
			float[] out_o5, float[] out_roi);

	void tpdnn_free(Pointer ctx);
}
