preloading h264

This commit is contained in:
wrapper 2025-09-25 14:22:24 +07:00
parent cdb882863d
commit 9269b71295

View file

@ -125,14 +125,18 @@ public class SplashAnimationRenderer {
private BufferH264ES es;
public H264Provider(String file, AtomicInteger frames) {
public H264Provider(String file, AtomicInteger frames, boolean preload) {
noFrames = frames;
bufferSize = Math.max(5, (int) Math.ceil(1 / frameDelay));
bufferSize = Math.max(5, (int) Math.ceil(1 / frameDelay)) * 4;
try {
File f = new File(file);
es = new BufferH264ES(NIOUtils.fetchFromFile(f));
handleDecode();
if (preload) {
while (handleDecode());
} else {
handleDecode();
}
if (imageList.isEmpty()) {
throw new Exception("first decoding failed!");
@ -184,17 +188,18 @@ public class SplashAnimationRenderer {
return dst;
}
private final Picture buf = Picture.create(1920, 1088, ColorSpace.YUV420);
public boolean handleDecode() {
newFrame = es.nextFrame();
if (newFrame != null) {
try {
Picture buf = Picture.create(1920, 1088, ColorSpace.YUV420);
Picture out = decoder.decodeFrame(newFrame.getData(), buf.getData()).cropped();
Picture pic = out.createCompatible();
final Picture out = decoder.decodeFrame(newFrame.getData(), buf.getData()).cropped();
final Picture pic = out.createCompatible();
pic.copyFrom(out);
Transform transform = ColorUtil.getTransform(pic.getColor(), ColorSpace.RGB);
Picture rgb = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.RGB);
final Transform transform = ColorUtil.getTransform(pic.getColor(), ColorSpace.RGB);
final Picture rgb = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.RGB);
transform.transform(pic, rgb);
synchronized (imageList) {
@ -215,14 +220,8 @@ public class SplashAnimationRenderer {
@Override
public void run() {
while (run) {
try {
while (imageList.size() < bufferSize) {
if (!handleDecode()) break;
}
Thread.sleep(Math.round(frameDelay * 1000));
} catch (InterruptedException e) {
while (imageList.size() < bufferSize) {
if (!handleDecode()) break;
}
}
}
@ -236,6 +235,7 @@ public class SplashAnimationRenderer {
private static boolean animationScaleUp = false;
private static boolean animationScaleDown = false;
private static boolean animationScaleFilter = false;
private static boolean debugLogs = false;
private static int backgroundColor = 0;
private static float frameDelay = 0;
private static float fadeOutTime = 0;
@ -284,13 +284,15 @@ public class SplashAnimationRenderer {
frameDelay = config.getFloat("frameDelay", "animation", 0.03f, 0.005f, 1.0f, "The delay for each frame of animation, in seconds.");
fadeOutTime = config.getFloat("fadeOutTime", "animation", 1.0f, 0.0f, 5.0f, "The fade out time after the final frame of animation.");
String frameStr = config.getString("frameFileDirectory", "animation", "animation", "The directory containing the animation frames, which should be of the filename format [number].[extension].");
boolean doPreload = config.getBoolean("preload", "animation", true, "If using H264 video, should it preload the entire video?");
debugLogs = config.getBoolean("debugLogs", "animation", false, "SplashPlayer debug logs");
if (config.hasChanged()) {
config.save();
}
if (frameStr.endsWith(".h264") || frameStr.endsWith(".264")) {
h264provider = new H264Provider(frameStr, h264Frames);
h264provider = new H264Provider(frameStr, h264Frames, doPreload);
if (h264provider.noFile) {
System.err.println("H264 open failed!");
stage = 2;
@ -426,10 +428,12 @@ public class SplashAnimationRenderer {
if (h264provider != null && h264Frames.get() != -1 && frameCount == -1) {
frameCount = h264Frames.get();
if (debugLogs) System.out.println(String.format("frameCount: %d, frameIndex: %d", frameCount, renderFrameIndex));
}
if (renderFrameIndex >= frameCount && frameCount != -1) {
float fadeProgress = ((renderFrameIndex - frameCount) * frameDelay) / fadeOutTime;
if (debugLogs) System.out.println(String.format("fadeProgress: %f, frameIndex: %d, frameCount: %d", fadeProgress, renderFrameIndex, frameCount));
alpha = 1.0f - fadeProgress;
if (alpha <= 0.0f) {
stage = 2;