Support bounded 720x1280 avatar shapes through GLES and production gates

This commit is contained in:
zenord
2026-10-13 17:32:21 +08:00
parent 62dce32bb7
commit ddd75fc571
18 changed files with 352 additions and 83 deletions
@@ -15,6 +15,12 @@ data class ContinuousAvatarPack(val body: ContinuousBodyAsset,val patches: List<
val rgbaBytes: Long get() = patches.sumOf { it.width*it.height*4L*(if(apertureMode) 5 else 1) }
}
/** Deliberately bounded schema2 shapes; schema1 keeps its original pixel contract. */
object AvatarShape {
fun supported(width: Int,height: Int): Boolean =
(width==480 && height==848) || (width==720 && height==1280)
}
/** Metadata validation only; importer must additionally verify decoded frame PTS. */
object BodyTimeline {
const val MAX_TALK_FRAMES = 720
@@ -63,7 +69,8 @@ object ContinuousAvatarManifest {
val body=ContinuousBodyAsset(b["path"] as String,hash(b["sha256"]),smallInt(b["width"]),smallInt(b["height"]),
count,integer(b["durationUs"]),pts,fpsN,fpsD)
require(body.path.matches(Regex("body/[a-zA-Z0-9_.-]+\\.mp4")))
require(body.width==480 && body.height==848 && b["hasAudio"]==false)
field="body.width/height"
require((if(dynamic) AvatarShape.supported(body.width,body.height) else body.width==480 && body.height==848) && b["hasAudio"]==false)
if(dynamic) require(BodyTimeline.valid(pts,count,body.durationUs,fpsN,fpsD)) else {
require(count==97 && kotlin.math.abs(body.durationUs-4_041_667)<=2 && fpsN==24 && fpsD==1)
require(integer(b["frameDurationUsNumerator"])==1_000_000L && integer(b["frameDurationUsDenominator"])==24L)
@@ -85,7 +92,7 @@ object ContinuousAvatarManifest {
require(patch.frameIndex==index && if(dynamic) patch.ptsUs==pts[index] else kotlin.math.abs(patch.ptsUs-pts[index])<=2)
require(patch.path.matches(Regex("patches/[a-zA-Z0-9_.-]+\\.png")))
require(patch.width in 1..128 && patch.height in 1..128 && patch.x>=0 && patch.y>=0 &&
patch.x+patch.width<=480 && patch.y+patch.height<=848)
patch.x+patch.width<=body.width && patch.y+patch.height<=body.height)
if(apertureMode) {
field="patches[$index].levels"
val levels=(p["levels"] as List<Map<String,Any?>>).also { require(it.size==5) }.mapIndexed { level,row ->
@@ -61,7 +61,7 @@ class ContinuousBodyRenderer(private val context: Context,private val pack: Cont
put(floatArrayOf(0f,0f,1f,0f,0f,1f,1f,1f));position(0)
}
private var aPosition=0; private var aUv=0; private var uTextureMatrix=0
private var uBody=0; private var uPatch=0; private var uRect=0; private var uOpacity=0
private var uBodySize=0; private var uBody=0; private var uPatch=0; private var uRect=0; private var uOpacity=0
private var startedMs=SystemClock.elapsedRealtime()
private var progressMs=startedMs
private val timeout=Runnable { fail("frame_timeout") }
@@ -93,6 +93,7 @@ class ContinuousBodyRenderer(private val context: Context,private val pack: Cont
continueBody()
} } } }
private fun initialize() {
check(AvatarShape.supported(pack.body.width,pack.body.height))
check(pack.rgbaBytes<=BodyTimeline.MAX_RGBA_BYTES && (!mouthEnabled || pack.patches.size==pack.body.frameCount))
check(startFrame in pack.body.ptsUs.indices)
display=EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);check(display!=EGL14.EGL_NO_DISPLAY)
@@ -109,8 +110,9 @@ class ContinuousBodyRenderer(private val context: Context,private val pack: Cont
aPosition=GLES20.glGetAttribLocation(program,"aPosition");aUv=GLES20.glGetAttribLocation(program,"aUv")
uTextureMatrix=GLES20.glGetUniformLocation(program,"uTextureMatrix")
uBody=GLES20.glGetUniformLocation(program,"uBody");uPatch=GLES20.glGetUniformLocation(program,"uPatch")
uBodySize=GLES20.glGetUniformLocation(program,"uBodySize")
uRect=GLES20.glGetUniformLocation(program,"uRect");uOpacity=GLES20.glGetUniformLocation(program,"uOpacity")
check(listOf(aPosition,aUv,uTextureMatrix,uBody,uPatch,uRect,uOpacity).all { it>=0 })
check(listOf(aPosition,aUv,uTextureMatrix,uBodySize,uBody,uPatch,uRect,uOpacity).all { it>=0 })
val texture=IntArray(1);GLES20.glGenTextures(1,texture,0);bodyTexture=texture[0]
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,bodyTexture)
configureTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES)
@@ -148,7 +150,7 @@ class ContinuousBodyRenderer(private val context: Context,private val pack: Cont
video
} else { check(ex.trackCount==1);0 }
val format=ex.getTrackFormat(track);check(format.getString(MediaFormat.KEY_MIME)=="video/avc")
check(format.getInteger(MediaFormat.KEY_WIDTH)==480 && format.getInteger(MediaFormat.KEY_HEIGHT)==848)
check(format.getInteger(MediaFormat.KEY_WIDTH)==pack.body.width && format.getInteger(MediaFormat.KEY_HEIGHT)==pack.body.height)
ex.selectTrack(track);check(ex.sampleTime==0L)
codec=MediaCodec.createDecoderByType("video/avc").also { it.configure(format,checkNotNull(inputSurface),null,0);it.start() }
progressMs=SystemClock.elapsedRealtime()
@@ -229,13 +231,14 @@ class ContinuousBodyRenderer(private val context: Context,private val pack: Cont
private fun draw(frame: ComposedBodyFrame) {
check(width>0 && height>0)
GLES20.glViewport(0,0,width,height);GLES20.glClearColor(1f,1f,1f,1f);GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
val rect=bodyFit(width,height)
val rect=bodyFit(width,height,pack.body.width,pack.body.height)
val left=rect.left/width*2-1;val right=(rect.left+rect.width)/width*2-1
val top=1-rect.top/height*2;val bottom=1-(rect.top+rect.height)/height*2
positions.clear();positions.put(floatArrayOf(left,bottom,right,bottom,left,top,right,top));positions.position(0);uv.position(0)
GLES20.glUseProgram(program)
GLES20.glVertexAttribPointer(aPosition,2,GLES20.GL_FLOAT,false,0,positions);GLES20.glEnableVertexAttribArray(aPosition)
GLES20.glVertexAttribPointer(aUv,2,GLES20.GL_FLOAT,false,0,uv);GLES20.glEnableVertexAttribArray(aUv)
GLES20.glUniform2f(uBodySize,pack.body.width.toFloat(),pack.body.height.toFloat())
GLES20.glUniformMatrix4fv(uTextureMatrix,1,false,textureMatrix,0)
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,bodyTexture);GLES20.glUniform1i(uBody,0)
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
@@ -297,11 +300,11 @@ internal object AvatarLayerShader {
#extension GL_OES_EGL_image_external : require
precision highp float;
uniform samplerExternalOES uBody; uniform sampler2D uPatch;
uniform vec4 uRect; uniform float uOpacity;
uniform vec2 uBodySize; uniform vec4 uRect; uniform float uOpacity;
varying vec2 vBody; varying vec2 vLogical;
void main() {
vec4 body=texture2D(uBody,vBody);
vec2 pixel=vec2(vLogical.x*480.0,(1.0-vLogical.y)*848.0);
vec2 pixel=vec2(vLogical.x,1.0-vLogical.y)*uBodySize;
vec2 local=(pixel-uRect.xy)/uRect.zw;
if(uOpacity<=0.0 || local.x<0.0 || local.y<0.0 || local.x>=1.0 || local.y>=1.0) {
gl_FragColor=vec4(body.rgb,1.0);
@@ -56,6 +56,10 @@ object EmotionActionManifest {
field="conversationPackages.cacheBytes/fallbackPackageId"
require(packages.values.sumOf { it.rgbaBytes }<=BodyTimeline.MAX_PACK_CACHE_BYTES)
require(visuals.single { it.id==fallback }.emotion==RoleEmotion.NEUTRAL)
field="conversationPackages.width/height"
val shape=packages.getValue(fallback).body
require(AvatarShape.supported(shape.width,shape.height) &&
packages.values.all { it.body.width==shape.width && it.body.height==shape.height })
field="edges"
val edges=root["edges"] as List<Map<String,Any?>>
require(edges.size<=100 && edges.map { it["id"] }.distinct().size==edges.size)
@@ -71,7 +75,7 @@ object EmotionActionManifest {
require(edge["fromAnchor"]=="A" && edge["toAnchor"]=="A")
field="$edgeField.clip"
val clip=obj(edge["clip"]);path(clip["path"]);hash(clip["sha256"])
require(clip["hasAudio"]==false && integer(clip["width"])==480L && integer(clip["height"])==848L)
require(clip["hasAudio"]==false && integer(clip["width"])==shape.width.toLong() && integer(clip["height"])==shape.height.toLong())
val count=boundedInt(clip["frameCount"],BodyTimeline.MAX_TALK_FRAMES)
val pts=(clip["ptsUs"] as List<*>).also { require(it.size==count) }.map(::integer)
require(BodyTimeline.valid(pts,count,integer(clip["durationUs"]),
@@ -109,7 +113,7 @@ object EmotionActionManifest {
require(mouthRuntime==null || edge["mouthAllowed"]!=false)
ConversationEdge(edge["id"] as String,edge["from"] as String,edge["to"] as String,
integer(edge["fromFrame"]).toInt(),integer(edge["toFrame"]).toInt(),
ContinuousBodyAsset(path(clip["path"]),hash(clip["sha256"]),480,848,pts.size,
ContinuousBodyAsset(path(clip["path"]),hash(clip["sha256"]),shape.width,shape.height,pts.size,
integer(clip["durationUs"]),pts,boundedInt(clip["fpsNumerator"],120_000),boundedInt(clip["fpsDenominator"],10_000)),nativeLoop,mouthRuntime)
}
field="combinedRuntimeCacheBytes"
@@ -144,7 +148,7 @@ object EmotionActionManifest {
require(clip["hasAudio"]==true && integer(action["audioEndUs"]) in 1..duration)
val count=boundedInt(clip["frameCount"],5400)
val pts=(clip["ptsUs"] as List<*>).also { require(it.size==count) }.map(::integer)
require(integer(clip["width"])==480L && integer(clip["height"])==848L)
require(integer(clip["width"])==shape.width.toLong() && integer(clip["height"])==shape.height.toLong())
require(BodyTimeline.valid(pts,count,duration,
boundedInt(clip["fpsNumerator"],120_000),boundedInt(clip["fpsDenominator"],10_000),5400,90_000_000))
require(duration in 1_000_000..90_000_000)
@@ -159,7 +163,7 @@ object EmotionActionManifest {
require(integer(action["sourcePtsUs"])==runtime.body.ptsUs[sourceFrame.toInt()] &&
integer(action["returnPtsUs"])==runtime.body.ptsUs[returnFrame.toInt()])
ActionAsset(action["id"] as String,(duration+999)/1000,ActionMedia(path(clip["path"]),hash(clip["sha256"]),pathRoot,
pts,duration,480,848,fallback,sourceFrame.toInt(),fallback,returnFrame.toInt(),integer(action["audioEndUs"]),
pts,duration,shape.width,shape.height,fallback,sourceFrame.toInt(),fallback,returnFrame.toInt(),integer(action["audioEndUs"]),
boundedInt(clip["fpsNumerator"],120_000),boundedInt(clip["fpsDenominator"],10_000)))
}
ReviewedActionPack(actualSha,assets)
@@ -30,7 +30,7 @@ object StartupAvatarRepository {
val app=context.applicationContext
val info=app.packageManager.getPackageInfo(app.packageName,0)
val key=StartupAssetKey(info.versionCode.toLong(),info.lastUpdateTime,BuildConfig.EMOTION_MANIFEST_SHA,
"avatar/a01-expression-action-v1/",1)
"avatar/a01-expression-action-v1/",2)
var cancelled=false
var basicFailed=false
// Both requests use one serial IO worker: the tiny baseline is prioritized.
@@ -45,9 +45,9 @@ object StartupAvatarRepository {
val bytes=readBounded(app.assets.open(path),64_000_000);check(bytes.size<=64_000_000 && sha(bytes)==b.getString("sha256"))
val poster=readBounded(app.assets.open(ROOT+"poster.png"),100_000);check(poster.size<=100_000 && sha(poster)==json.getString("posterSha256"))
val pts=b.getJSONArray("ptsUs");val timeline=List(pts.length()) { pts.getLong(it) }
check(b.getInt("width")==480 && b.getInt("height")==848 &&
check(AvatarShape.supported(b.getInt("width"),b.getInt("height")) &&
BodyTimeline.valid(timeline,b.getInt("frameCount"),b.getLong("durationUs"),b.getInt("fpsNumerator"),b.getInt("fpsDenominator")))
val body=ContinuousBodyAsset(path,b.getString("sha256"),480,848,timeline.size,b.getLong("durationUs"),
val body=ContinuousBodyAsset(path,b.getString("sha256"),b.getInt("width"),b.getInt("height"),timeline.size,b.getLong("durationUs"),
timeline,b.getInt("fpsNumerator"),b.getInt("fpsDenominator"))
StartupTrace.mark(StartupTrace.Phase.BASIC_VERIFY,start)
ContinuousAvatarPack(body,emptyList(),emptyMap(),"",BASIC_SHA)