Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ private static UnsupportedOperationException unavailable() {
private static final MethodHandle msgSend_ptr_ptr;
/** (id, SEL) → BOOL */
private static final MethodHandle msgSend_ret_bool;
/** (id, SEL) → CGAffineTransform */
private static final MethodHandle msgSend_ret_transform;

/** CGAffineTransform: six CGFloat members {@code a, b, c, d, tx, ty}. */
private static final StructLayout CGAFFINETRANSFORM = MemoryLayout.structLayout(
ValueLayout.JAVA_DOUBLE.withName("a"),
ValueLayout.JAVA_DOUBLE.withName("b"),
ValueLayout.JAVA_DOUBLE.withName("c"),
ValueLayout.JAVA_DOUBLE.withName("d"),
ValueLayout.JAVA_DOUBLE.withName("tx"),
ValueLayout.JAVA_DOUBLE.withName("ty"));

static {
if (AVAILABLE) {
Expand All @@ -91,14 +80,11 @@ private static UnsupportedOperationException unavailable() {
ValueLayout.ADDRESS, ValueLayout.ADDRESS));
msgSend_ret_bool = CoreFoundation.msgSend(FunctionDescriptor.of(ValueLayout.JAVA_BYTE,
ValueLayout.ADDRESS, ValueLayout.ADDRESS));
msgSend_ret_transform = CoreFoundation.msgSend(FunctionDescriptor.of(CGAFFINETRANSFORM,
ValueLayout.ADDRESS, ValueLayout.ADDRESS));
} else {
msgSend = null;
msgSend_ptr = null;
msgSend_ptr_ptr = null;
msgSend_ret_bool = null;
msgSend_ret_transform = null;
}
}

Expand Down Expand Up @@ -166,33 +152,6 @@ private static MemorySegment playableVideoTrack(Arena arena, Path videoFile)
}
}

/**
* Returns whether the track's preferred transform turns the picture by an
* odd number of quarter turns, so that the displayed frame has the stored
* width and height swapped. The frame generator applies that transform,
* while the track's natural size does not include it.
*/
private static boolean displayTransposed(Path videoFile) {
MemorySegment pool = CoreFoundation.autoreleasePoolPush();
try (Arena arena = Arena.ofConfined()) {
MemorySegment track = playableVideoTrack(arena, videoFile);
if (MemorySegment.NULL.equals(track)) return false;
MemorySegment transform =
(MemorySegment) msgSend_ret_transform.invokeExact(
(SegmentAllocator) arena, track,
CoreFoundation.selRegisterName(arena, "preferredTransform"));
double a = transform.get(ValueLayout.JAVA_DOUBLE, 0);
double b = transform.get(ValueLayout.JAVA_DOUBLE, 8);
if (a == 0 && b == 0) return false;
long quarterTurns = Math.round(Math.toDegrees(Math.atan2(b, a)) / 90.0);
return (quarterTurns & 1) != 0;
} catch (Throwable ignored) {
return false;
} finally {
CoreFoundation.autoreleasePoolPop(pool);
}
}

@Override
public BufferedImage extractFrame(Path videoFile, Duration time) throws IOException {
return extractFrame(videoFile, time, null);
Expand Down Expand Up @@ -235,11 +194,10 @@ public VideoInfo getInfo(Path videoFile) throws IOException {
MemorySegment pool = CoreFoundation.autoreleasePoolPush();
try (Arena arena = Arena.ofConfined()) {
var info = AVFoundation.getVideoInfo(arena, videoFile.toAbsolutePath().toString());
// Report the displayed size, which is what extractFrame returns.
boolean transposed = displayTransposed(videoFile);
// The binding reports the displayed size, which is what
// extractFrame returns.
return new VideoInfo(
transposed ? info.height() : info.width(),
transposed ? info.width() : info.height(),
info.width(), info.height(),
Duration.ofMillis(info.durationMillis()), null, info.frameRate());
} catch (RuntimeException e) {
throw new IOException("Video info extraction failed", e);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>26</maven.compiler.release>
<junit.version>6.1.2</junit.version>
<panama-media.version>0.1.4</panama-media.version>
<panama-media.version>0.1.5</panama-media.version>
<ffmpeg-ffm.version>8.1.2-0.3.2</ffmpeg-ffm.version>
</properties>

Expand Down
Loading