Skip to content

Commit 3d88354

Browse files
authored
Merge pull request #231 from Buffersolve/fix-macos
fix(macos): correct video aspect ratio and rewrite frame pipeline to fix tearing, stalls and frozen playback
2 parents c2aaf97 + 734b9e5 commit 3d88354

6 files changed

Lines changed: 1313 additions & 827 deletions

File tree

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/mac/MacFrameUtils.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@ import java.nio.ByteBuffer
44

55
internal fun calculateFrameHash(
66
buffer: ByteBuffer,
7-
pixelCount: Int,
7+
width: Int,
8+
height: Int,
9+
rowBytes: Int,
810
): Int {
9-
if (pixelCount <= 0) return 0
11+
if (width <= 0 || height <= 0) return 0
1012

13+
val pixelCount = width * height
1114
var hash = 1
12-
val step = if (pixelCount <= 200) 1 else pixelCount / 200
13-
for (i in 0 until pixelCount step step) {
14-
hash = 31 * hash + buffer.getInt(i * 4)
15+
var step = if (pixelCount <= 200) 1 else pixelCount / 200
16+
// A step that is a multiple of the width would pin every sample to the same x column
17+
// (e.g. 720x400 → step 1440 → x always 0); with a static edge (letterboxing, dark scene
18+
// border) the hash would never change and the dedup would freeze the video.
19+
if (width > 1 && step % width == 0) step++
20+
var i = 0
21+
while (i < pixelCount) {
22+
// Map the linear sample index to its real byte offset, honoring row padding
23+
// (rowBytes may exceed width*4) so we sample true pixels rather than padding bytes.
24+
val x = i % width
25+
val y = i / width
26+
hash = 31 * hash + buffer.getInt(y * rowBytes + x * 4)
27+
i += step
1528
}
1629
return hash
1730
}

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/mac/MacNativeBridge.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ internal object MacNativeBridge {
6565

6666
@JvmStatic external fun nGetFrameHeight(handle: Long): Int
6767

68+
@JvmStatic external fun nGetDisplayAspectRatio(handle: Long): Double
69+
6870
@JvmStatic external fun nSetOutputSize(
6971
handle: Long,
7072
width: Int,

0 commit comments

Comments
 (0)