Encoding added Requested Encoding to the request, made request async, some refactoring

This commit is contained in:
Samuele Lorefice
2025-12-15 19:00:31 +01:00
parent db20f5d54d
commit db6873c93c
6 changed files with 108 additions and 44 deletions

View File

@@ -1,7 +1,10 @@
using System.Runtime.CompilerServices;
using Microsoft.Net.Http.Headers;
namespace Encoder;
public static class Utils {
static Tuple<int, int>[] QPTable = new Tuple<int, int>[] {
private static readonly Tuple<int, int>[] QPTable = new Tuple<int, int>[] {
new(1280*720, 64),
new(1920*1080, 96),
new(3840*2160, 128),
@@ -9,30 +12,43 @@ public static class Utils {
new(8128*4096, 120) //VR8K
}.OrderBy(t => t.Item1).ToArray();
static float lerp(float v0, float v1, float t) {
public static float Lerp(float v0, float v1, float t) {
return v0 + t * (v1 - v0);
}
static float remap(float value, float from1, float to1, float from2, float to2) {
return from2 + (value - from1) * (to2 - from2) / (to1 - from1);
}
public static float Remap(float value, float from1, float to1, float from2, float to2) => from2 + (value - from1) * (to2 - from2) / (to1 - from1);
public static int ToQPValue(int W, int H) {
int pixels = W * H;
for (var i = 0; i < QPTable.Length; i++) {
var t = QPTable[i];
if (pixels <= t.Item1) {
if (pixels <= QPTable[i].Item1) {
var minQP = QPTable[i - 1].Item2;
var maxQP = QPTable[i].Item2;
var minPixels = QPTable[i - 1].Item1;
var maxPixels = QPTable[i].Item1;
var tPixels = remap(pixels, minPixels, maxPixels, 0, 1);
var tPixels = Remap(pixels, minPixels, maxPixels, 0, 1);
return (int)lerp(minQP, maxQP, tPixels);
return (int)Lerp(minQP, maxQP, tPixels);
}
}
// Return the highest QP for anything higher than 8K VR
return QPTable.Last().Item2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string AbsoluteOrProcessPath(this string path) {
return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath)!, path));
}
public static bool IsMultipartContentType(string contentType)
=> !string.IsNullOrEmpty(contentType) && contentType.Contains("multipart/form-data");
public static string? GetBoundary(string contentType)
{
var boundary = HeaderUtilities.RemoveQuotes(
MediaTypeHeaderValue.Parse(contentType).Boundary
).Value;
return boundary;
}
}