forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpuEncoder.test.ts
More file actions
140 lines (121 loc) · 4.22 KB
/
Copy pathgpuEncoder.test.ts
File metadata and controls
140 lines (121 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { afterEach, describe, expect, it, vi } from "vitest";
import {
getCompiledGpuEncoders,
getGpuEncoderName,
getProbeArgs,
mapPresetForGpuEncoder,
selectUsableGpuEncoder,
} from "./gpuEncoder.js";
afterEach(() => {
vi.useRealTimers();
});
describe("getCompiledGpuEncoders", () => {
it("recognizes AMD AMF in FFmpeg's encoder list", () => {
expect(
getCompiledGpuEncoders(`
V....D h264_nvenc NVIDIA NVENC H.264 encoder
V....D h264_amf AMD AMF H.264 Encoder
V....D h264_qsv H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video)
`),
).toEqual(["nvenc", "qsv", "amf"]);
});
});
describe("selectUsableGpuEncoder", () => {
it("runs probe checks concurrently while preserving candidate priority", async () => {
vi.useFakeTimers();
const started: string[] = [];
const usable = selectUsableGpuEncoder(["nvenc", "amf"], async (encoder) => {
started.push(encoder);
await new Promise((resolve) => setTimeout(resolve, encoder === "nvenc" ? 50 : 1));
return true;
});
await vi.advanceTimersByTimeAsync(1);
expect(started).toEqual(["nvenc", "amf"]);
await vi.advanceTimersByTimeAsync(49);
expect(await usable).toBe("nvenc");
});
it("falls through from compiled-but-unusable NVENC to usable AMD AMF", async () => {
const usable = await selectUsableGpuEncoder(["nvenc", "amf"], async (encoder) => {
return encoder === "amf";
});
expect(usable).toBe("amf");
});
it("treats rejected probe checks as unusable", async () => {
const usable = await selectUsableGpuEncoder(["nvenc", "amf"], async (encoder) => {
if (encoder === "nvenc") {
throw new Error("driver probe failed");
}
return encoder === "amf";
});
expect(usable).toBe("amf");
});
});
describe("getGpuEncoderName", () => {
it("maps AMD AMF to FFmpeg's h264 and hevc encoder names", () => {
expect(getGpuEncoderName("amf", "h264")).toBe("h264_amf");
expect(getGpuEncoderName("amf", "h265")).toBe("hevc_amf");
});
});
describe("mapPresetForGpuEncoder", () => {
describe("nvenc", () => {
it.each([
["ultrafast", "p1"],
["superfast", "p1"],
["veryfast", "p2"],
["faster", "p3"],
["fast", "p4"],
["medium", "p4"],
["slow", "p5"],
["slower", "p6"],
["veryslow", "p7"],
["placebo", "p7"],
])("maps libx264 preset %s to NVENC %s", (input, expected) => {
expect(mapPresetForGpuEncoder("nvenc", input)).toBe(expected);
});
it.each(["p1", "p2", "p3", "p4", "p5", "p6", "p7"])(
"passes NVENC-native preset %s through unchanged",
(preset) => {
expect(mapPresetForGpuEncoder("nvenc", preset)).toBe(preset);
},
);
it("falls back to p4 for unknown preset values", () => {
expect(mapPresetForGpuEncoder("nvenc", "nonsense")).toBe("p4");
});
});
describe("qsv", () => {
it.each([
["ultrafast", "veryfast"],
["superfast", "veryfast"],
["placebo", "veryslow"],
])("rewrites libx264-only preset %s to QSV-supported %s", (input, expected) => {
expect(mapPresetForGpuEncoder("qsv", input)).toBe(expected);
});
it.each(["veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"])(
"passes supported preset %s through unchanged",
(preset) => {
expect(mapPresetForGpuEncoder("qsv", preset)).toBe(preset);
},
);
});
describe("other encoders", () => {
it.each(["videotoolbox", "vaapi", "amf"] as const)(
"passes preset through unchanged for %s",
(encoder) => {
expect(mapPresetForGpuEncoder(encoder, "medium")).toBe("medium");
expect(mapPresetForGpuEncoder(encoder, "ultrafast")).toBe("ultrafast");
},
);
it("passes preset through unchanged when encoder is null (CPU)", () => {
expect(mapPresetForGpuEncoder(null, "ultrafast")).toBe("ultrafast");
});
});
});
describe("getProbeArgs", () => {
it("uses 320x240 probe dimensions for all GPU encoders", () => {
const encoders = ["nvenc", "videotoolbox", "vaapi", "qsv", "amf"] as const;
for (const encoder of encoders) {
const args = getProbeArgs(encoder);
expect(args).toContain("color=size=320x240:rate=1:duration=1");
}
});
});