import com.gorios.lanvoice.avatar.* import com.gorios.lanvoice.dialogue.RoleEmotion import android.media.CodecFixture import android.graphics.BitmapFactory import android.graphics.SurfaceTexture import android.view.Surface import android.os.TestScheduler import android.opengl.GLFixture @Suppress("UNCHECKED_CAST") fun dynamicRuntimeFixture(count: Int=121,fpsN: Int=30,fpsD: Int=1,sha: String="c".repeat(64),width: Int=480,height: Int=848): Map { val old=continuousFixture() val pts=List(count) { it*1_000_000L*fpsD/fpsN } val base=(old["patches"] as List>).first() val patches=pts.mapIndexed { i,t -> val levels=List(5) { l -> mapOf("levelIndex" to l,"aperture" to l/4f,"path" to "patches/f$i-l$l.png","sha256" to sha) } base+mapOf("frameIndex" to i,"ptsUs" to t,"levels" to levels,"path" to levels[4]["path"],"sha256" to sha) } val states=(old["mouthStates"] as List>).map { state -> val n=(state["globalAlphaByFrame"] as List<*>).size state+("ptsUs" to List(n) { it*1_000_000L/24 }) } return old+mapOf("schemaVersion" to 2,"packId" to "a01-expression-action-v1-test-runtime-04", "body" to (old["body"] as Map)+mapOf("sha256" to sha,"width" to width,"height" to height,"frameCount" to count, "fpsNumerator" to fpsN,"fpsDenominator" to fpsD,"ptsUs" to pts,"durationUs" to (count*1_000_000L*fpsD+fpsN/2)/fpsN), "patches" to patches,"mouthStates" to states,"patchFormat" to mapOf("bodySha256" to sha, "format" to "PNG RGBA 8bit","alphaEncoding" to "straight_unassociated","apertureLevels" to listOf(0f,.25f,.5f,.75f,1f),"requiresRendererExtension" to true)) } fun dynamicPack(count: Int,fps: Int,sha: String,width: Int=480,height: Int=848): ContinuousAvatarPack = checkNotNull( ContinuousAvatarManifest.read(dynamicRuntimeFixture(count,fps,1,sha,width,height),sha,sha,true,"dynamic/")) @Suppress("UNCHECKED_CAST") fun dynamicBodyCheck() { var boundaries=0 for((count,fpsN,fpsD) in listOf(Triple(121,30,1),Triple(49,24,1),Triple(61,30000,1001))) { val sha="c".repeat(64);val root=dynamicRuntimeFixture(count,fpsN,fpsD,sha) fun read(data: Map)=ContinuousAvatarManifest.read(data,sha,sha,true,"fixture/") val pack=checkNotNull(read(root));check(pack.body.frameCount==count && pack.textureImages.size==count*5) val presenter=ContinuousFramePresenter(pack) { 0 } repeat(2) { loop -> for(i in 0 until count) { val pts=pack.body.ptsUs[i];presenter.decoded(pts) val frame=checkNotNull(presenter.present(pts*1000,loop*10000L+pts/1000) {}) check(frame.key.index==i && frame.patch.ptsUs==pts && frame.key.loopOrdinal==loop.toLong()) for(level in 0..4) { val s=selectMouthPatch(pack,i,level/4f) check(s.textureSlot==i*5+level && s.opacity==if(level==0) 0f else 1f) boundaries++ } } presenter.wrap() } val body=root["body"] as Map;val patches=root["patches"] as List> fun invalid(data: Map) { check(read(data)==null);boundaries++ } invalid(root+("body" to body+("frameCount" to 97))) invalid(root+("body" to body+("durationUs" to 100_000_000L))) invalid(root+("body" to body+("fpsDenominator" to 0))) invalid(root+("body" to body+("ptsUs" to pack.body.ptsUs.dropLast(1)))) invalid(root+("body" to body+("ptsUs" to listOf(1L)+pack.body.ptsUs.drop(1)))) invalid(root+("patches" to listOf(patches[0]+("ptsUs" to 1L))+patches.drop(1))) invalid(root+("patches" to patches.dropLast(1))) invalid(root+("patches" to patches.map { it+mapOf("width" to 128,"height" to 128) })) invalid(root+("patchFormat" to (root["patchFormat"] as Map)+("bodySha256" to "b".repeat(64)))) invalid(root+("schemaVersion" to 1)) invalid(root+("body" to body+("frameCount" to 721))) invalid(root+("body" to body+("frameCount" to 4_294_967_417L))) invalid(root+("body" to body+("fpsNumerator" to 61))) invalid(root+("body" to body+("ptsUs" to listOf(0L,0L)+pack.body.ptsUs.drop(2)))) val levels=patches[0]["levels"] as List> invalid(root+("patches" to listOf(patches[0]+("levels" to levels.dropLast(1)))+patches.drop(1))) invalid(root+("patches" to listOf(patches[0]+("levels" to listOf(levels[0],levels[1]+("path" to levels[0]["path"]))+levels.drop(2)))+patches.drop(1))) val states=root["mouthStates"] as List> invalid(root+("mouthStates" to listOf(states[0]+("ptsUs" to listOf(0L)))+states.drop(1))) for((stateIndex,key,value) in listOf(Triple(1,"frameCount",count),Triple(2,"ptsUs",listOf(0L)))) { val changed=states.mapIndexed { i,s -> if(i==stateIndex) s+(key to value) else s } val fields=mutableListOf() check(ContinuousAvatarManifest.read(root+("mouthStates" to changed),sha,sha,true,"fixture/",fields::add)==null) check(fields==listOf("mouthStates[$stateIndex].$key"));boundaries++ } val acceptedFields=mutableListOf() check(ContinuousAvatarManifest.read(root,sha,sha,true,"fixture/",acceptedFields::add)!=null && acceptedFields.isEmpty()) check(runCatching { ContinuousFramePresenter(pack).decoded(pack.body.ptsUs[1]) }.isFailure) check(runCatching { selectMouthPatch(pack,count,.5f) }.isFailure) if(fpsD==1) { TestScheduler.reset();CodecFixture.reset();GLFixture.reset();BitmapFactory.wrongSize=false CodecFixture.frames=count;CodecFixture.fps=fpsN CodecFixture.onRender={ pts -> SurfaceTexture.lastCreated?.emit(pts*1000) } val frames=mutableListOf();val errors=mutableListOf() val renderer=ContinuousBodyRenderer(android.content.Context(),pack,Surface(SurfaceTexture(0)),480,848, { frame,_ -> frames.add(frame) },errors::add) renderer.intent(true,1,1);TestScheduler.advanceBy((pack.body.durationUs/1000)*2+100) check(errors.isEmpty() && frames.size>count && frames[count].key.index==0 && frames[count].key.loopOrdinal==1L) { "dynamic renderer count=$count fps=$fpsN errors=$errors frames=${frames.size} wrap=${frames.getOrNull(count)?.key}" } check(frames.all { it.patch.ptsUs==pack.body.ptsUs[it.key.index] && it.selection.textureSlot in pack.textureImages.indices }) renderer.close();TestScheduler.advanceBy(100) } } val sha="c".repeat(64);val root=dynamicRuntimeFixture();val body=root["body"] as Map val pts=(body["ptsUs"] as List).mapIndexed { i,t -> t+if(i%2==1) 1000 else 0 } val patches=(root["patches"] as List>).mapIndexed { i,p -> p+("ptsUs" to pts[i]) } val variable=root+mapOf("body" to body+("ptsUs" to pts),"patches" to patches) check(ContinuousAvatarManifest.read(variable,sha,sha,true,"variable/")!=null) // Run the production decoder/shader driver with 720 format, not a copied state model. run { val large=dynamicRuntimeFixture(97,24,1,sha,720,1280) val rows=large["patches"] as List> val moved=large+("patches" to rows.map { it+mapOf("x" to 650.25f,"y" to 1200.5f) }) val pack=checkNotNull(ContinuousAvatarManifest.read(moved,sha,sha,true,"large/")) check(pack.patches.all { it.x>480 && it.y>848 }) fun rejected(meta: Map) { check(ContinuousAvatarManifest.read(meta,sha,sha,true,"large/")==null) } rejected(moved+("patches" to rows.map { it+("x" to 690f) })) rejected(moved+("patches" to rows.map { it+("y" to 1260f) })) rejected(moved+("body" to (large["body"] as Map)+mapOf("width" to 720,"height" to 848))) rejected(moved+("body" to (large["body"] as Map)+mapOf("width" to 1080,"height" to 1920))) val legacy=continuousFixture() rejected(legacy+("body" to (legacy["body"] as Map)+mapOf("width" to 720,"height" to 1280))) val fit=bodyFit(360,800,720,1280) check(fit.left==0f && fit.top==80f && fit.width==360f && fit.height==640f) for((w,h) in listOf(720 to 1280,360 to 800,800 to 480)) { val original=bodyFit(w,h,720,1280) val zoomed=bodyFit(w,h,720,1280,1.45f,.489f) fun near(a: Float,b: Float)=kotlin.math.abs(a-b)<.001f check(near(zoomed.width,original.width*1.45f) && near(zoomed.height,original.height*1.45f)) check(near(zoomed.left+zoomed.width/2,w/2f) && near(zoomed.top+zoomed.height*.489f,h/2f)) // The exact same source coordinates map body pixels and mouth rects. val patch=pack.patches[0] val displayX=zoomed.left+(patch.x+patch.width/2f)*zoomed.width/720 val sourceX=(displayX-zoomed.left)*720/zoomed.width check(near(sourceX,patch.x+patch.width/2f)) } for(zoom in listOf(Float.NaN,Float.POSITIVE_INFINITY,.9f,2.1f))check(runCatching { bodyFit(720,1280,720,1280,zoom) }.isFailure) for(center in listOf(Float.NaN,.39f,.61f))check(runCatching { bodyFit(720,1280,720,1280,1.45f,center) }.isFailure) fun render(codecW: Int,codecH: Int): Pair,List> { TestScheduler.reset();CodecFixture.reset();GLFixture.reset() CodecFixture.width=codecW;CodecFixture.height=codecH;CodecFixture.frames=97;CodecFixture.fps=24 CodecFixture.onRender={ t -> SurfaceTexture.lastCreated?.emit(t*1000) } val frames=mutableListOf();val errors=mutableListOf() val renderer=ContinuousBodyRenderer(android.content.Context(),pack,Surface(SurfaceTexture(0)),360,800, { frame,_ -> frames+=frame },errors::add) renderer.intent(true,1,1);TestScheduler.advanceBy(8500) if(errors.isEmpty()) check(GLFixture.bodySize==720f to 1280f && frames.size>97 && frames[97].key.loopOrdinal==1L) renderer.close();TestScheduler.advanceBy(100);check(CodecFixture.releases==1 || errors.isNotEmpty()) val count=frames.size;SurfaceTexture.lastCreated?.emit(0);TestScheduler.advanceBy(100);check(frames.size==count) return frames to errors } val ok=render(720,1280);check(ok.second.isEmpty() && ok.first.all { it.patch==pack.patches[it.key.index] }) val wrong=render(480,848);check(wrong.first.isEmpty() && wrong.second.isNotEmpty()) println("PASS: schema2 720x1280 production codec/render/uniform/wrap/release, pixel bounds/Fit, mismatched decoder and legacy/unsupported shape rejection; GPU/device not claimed") } val curve=MouthCurve("opening",400,listOf(0f,.25f,1f),listOf(0L,100_000L,300_000L)) val mouth=ContinuousMouth(continuousPack().curves+("opening" to curve)) mouth.request(true,1,1);mouth.sample(0) check(kotlin.math.abs(mouth.sample(200).alpha-.625f)<.0001f) println("PASS: dynamic121/49/61 bodies + rational/nonuniform actual PTS, $boundaries frame/level/rejection checks; production decoder/render wrap on121@30/49@24, per-curve timing, SHA/cache/schema fences; fixtures only") }