2025-04-26 04:23:57 +02:00
|
|
|
|
import AspectRatioType from '@src/common/enums/AspectRatioType.enum';
|
|
|
|
|
|
import ExtensionMode from '@src/common/enums/ExtensionMode.enum';
|
|
|
|
|
|
import { ArVariant } from '@src/common/interfaces/ArInterface';
|
|
|
|
|
|
import { ExtensionEnvironment } from '@src/common/interfaces/SettingsInterface';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import EventBus from '../EventBus';
|
2025-07-15 19:41:00 +02:00
|
|
|
|
import Settings from '../settings/Settings';
|
2025-01-29 23:58:16 +01:00
|
|
|
|
import { SiteSettings } from '../settings/SiteSettings';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import VideoData from '../video-data/VideoData';
|
2025-04-05 01:31:06 +02:00
|
|
|
|
import { AardDebugUi } from './AardDebugUi';
|
2025-04-15 18:51:34 +02:00
|
|
|
|
import { AardTimer } from './AardTimers';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import { Corner } from './enums/corner.enum';
|
2024-11-07 02:26:19 +01:00
|
|
|
|
import { VideoPlaybackState } from './enums/video-playback-state.enum';
|
2024-12-26 14:58:14 +01:00
|
|
|
|
import { FallbackCanvas } from './gl/FallbackCanvas';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import { GlCanvas } from './gl/GlCanvas';
|
2025-04-03 02:59:25 +02:00
|
|
|
|
import { GlDebugCanvas, GlDebugType } from './gl/GlDebugCanvas';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import { AardCanvasStore } from './interfaces/aard-canvas-store.interface';
|
2024-11-07 02:26:19 +01:00
|
|
|
|
import { AardDetectionSample, generateSampleArray, resetSamples } from './interfaces/aard-detection-sample.interface';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import { AardStatus, initAardStatus } from './interfaces/aard-status.interface';
|
2025-10-12 22:03:55 +02:00
|
|
|
|
import { AardTestResult_SubtitleRegion, AardTestResults, initAardTestResults, resetAardTestResults, resetGuardLine, resetSubtitleScanResults } from './interfaces/aard-test-results.interface';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
import { AardTimers, initAardTimers } from './interfaces/aard-timers.interface';
|
2025-05-04 02:18:58 +02:00
|
|
|
|
import { ComponentLogger } from '../logging/ComponentLogger';
|
2025-10-08 19:35:12 +02:00
|
|
|
|
import { AardPollingOptions } from './enums/aard-polling-options.enum';
|
2025-10-12 22:03:55 +02:00
|
|
|
|
import { AardSubtitleCropMode } from './enums/aard-subtitle-crop-mode.enum';
|
|
|
|
|
|
import { LetterboxOrientation } from './enums/letterbox-orientation.enum';
|
|
|
|
|
|
import { Edge } from './enums/edge.enum';
|
|
|
|
|
|
import { AardUncertainReason } from './enums/aard-letterbox-uncertain-reason.enum';
|
2025-10-13 17:35:37 +02:00
|
|
|
|
import { result } from 'lodash';
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* /\
|
|
|
|
|
|
* //\\ Automatic
|
|
|
|
|
|
* // \\ Aspect
|
|
|
|
|
|
* // \\ Ratio
|
|
|
|
|
|
* \\ Detector
|
|
|
|
|
|
* //XXXX \\
|
|
|
|
|
|
* // \\ (Totes not a Witcher reference)
|
|
|
|
|
|
* // \\ (Witcher 2 best Witcher)
|
|
|
|
|
|
* //XXXXXXXXXXXXXX\\
|
|
|
|
|
|
*
|
2024-10-14 00:28:13 +02:00
|
|
|
|
*/
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
|
|
|
|
|
const PIXEL_SIZE = 4;
|
|
|
|
|
|
const PIXEL_SIZE_FRACTION = 0.25;
|
|
|
|
|
|
let ROW_SIZE = -1;
|
|
|
|
|
|
|
2024-11-07 02:26:19 +01:00
|
|
|
|
export class Aard {
|
2024-10-14 00:28:13 +02:00
|
|
|
|
//#region configuration parameters
|
2025-05-04 02:18:58 +02:00
|
|
|
|
private logger: ComponentLogger;
|
2024-10-22 23:43:25 +02:00
|
|
|
|
private videoData: VideoData;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
private settings: Settings;
|
2025-01-29 23:58:16 +01:00
|
|
|
|
private siteSettings: SiteSettings;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
private eventBus: EventBus;
|
|
|
|
|
|
private arid: string;
|
2025-04-26 23:24:57 +02:00
|
|
|
|
private arVariant: ArVariant;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
|
|
|
|
|
private eventBusCommands = {
|
2025-01-29 23:58:16 +01:00
|
|
|
|
'uw-environment-change': {
|
|
|
|
|
|
function: (newEnvironment: ExtensionEnvironment) => {
|
2025-10-08 19:35:12 +02:00
|
|
|
|
// console.log('received extension environment:', newEnvironment, 'player env:', this.videoData?.player?.environment);
|
2025-01-29 23:58:16 +01:00
|
|
|
|
this.startCheck();
|
|
|
|
|
|
}
|
2025-04-10 00:45:39 +02:00
|
|
|
|
},
|
|
|
|
|
|
'aard-enable-debug': {
|
|
|
|
|
|
function: (enabled: boolean) => {
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
|
this.showDebugCanvas();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.hideDebugCanvas();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-08 19:35:12 +02:00
|
|
|
|
},
|
|
|
|
|
|
'url-changed': {
|
|
|
|
|
|
function: () => {
|
|
|
|
|
|
this.clearAutoDisabled();
|
|
|
|
|
|
}
|
2025-01-29 23:58:16 +01:00
|
|
|
|
}
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// 'get-aard-timing': {
|
2024-10-14 00:28:13 +02:00
|
|
|
|
// function: () => this.handlePerformanceDataRequest()
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// }
|
2024-10-14 00:28:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
private video: HTMLVideoElement;
|
|
|
|
|
|
|
|
|
|
|
|
private animationFrame: number;
|
|
|
|
|
|
|
|
|
|
|
|
//#region internal state
|
|
|
|
|
|
public status: AardStatus = initAardStatus();
|
|
|
|
|
|
private timers: AardTimers = initAardTimers();
|
2024-12-26 14:58:14 +01:00
|
|
|
|
private inFallback: boolean = false;
|
|
|
|
|
|
private fallbackReason: any;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
private canvasStore: AardCanvasStore;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
private testResults: AardTestResults;
|
2025-04-15 18:51:34 +02:00
|
|
|
|
private verticalTestResults: AardTestResults;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
private canvasSamples: AardDetectionSample;
|
2024-12-30 23:02:55 +01:00
|
|
|
|
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
private forceFullRecheck: boolean = true;
|
2025-04-05 01:31:06 +02:00
|
|
|
|
|
|
|
|
|
|
private debugConfig: any = {};
|
2025-04-15 18:51:34 +02:00
|
|
|
|
private timer: AardTimer;
|
2025-04-21 22:24:32 +02:00
|
|
|
|
private lastAnimationFrameTime: number = Infinity;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region getters
|
|
|
|
|
|
get defaultAr() {
|
|
|
|
|
|
if (!this.video) {
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-26 14:58:14 +01:00
|
|
|
|
this.video.setAttribute('crossOrigin', 'anonymous');
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
const ratio = this.video.videoWidth / this.video.videoHeight;
|
|
|
|
|
|
if (isNaN(ratio)) {
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
return ratio;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion getters
|
|
|
|
|
|
|
|
|
|
|
|
//#region lifecycle
|
|
|
|
|
|
constructor(videoData: VideoData){
|
2025-05-04 02:18:58 +02:00
|
|
|
|
this.logger = new ComponentLogger(videoData.logAggregator, 'Aard', {});
|
2024-10-22 23:43:25 +02:00
|
|
|
|
this.videoData = videoData;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
this.video = videoData.video;
|
|
|
|
|
|
this.settings = videoData.settings;
|
2025-01-29 23:58:16 +01:00
|
|
|
|
this.siteSettings = videoData.siteSettings;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
this.eventBus = videoData.eventBus;
|
|
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
this.eventBus.subscribeMulti(this.eventBusCommands, this);
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
this.arid = (Math.random()*100).toFixed();
|
|
|
|
|
|
|
|
|
|
|
|
// we can tick manually, for debugging
|
2025-05-04 02:18:58 +02:00
|
|
|
|
this.logger.log('ctor', `creating new ArDetector. arid: ${this.arid}`);
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-04-21 22:24:32 +02:00
|
|
|
|
this.timer = new AardTimer();
|
2024-10-15 17:38:04 +02:00
|
|
|
|
this.init();
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 23:43:25 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Initializes Aard with default values and starts autodetection loop.
|
|
|
|
|
|
* This method should only ever be called from constructor.
|
|
|
|
|
|
*/
|
2024-10-14 00:28:13 +02:00
|
|
|
|
private init() {
|
|
|
|
|
|
this.canvasStore = {
|
2024-12-26 14:58:14 +01:00
|
|
|
|
main: this.createCanvas('main-gl')
|
2024-10-14 00:28:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-07 00:18:13 +01:00
|
|
|
|
|
2024-10-19 16:04:20 +02:00
|
|
|
|
this.canvasSamples = {
|
|
|
|
|
|
top: generateSampleArray(
|
|
|
|
|
|
this.settings.active.arDetect.sampling.staticCols,
|
|
|
|
|
|
this.settings.active.arDetect.canvasDimensions.sampleCanvas.width
|
|
|
|
|
|
),
|
|
|
|
|
|
bottom: generateSampleArray(
|
|
|
|
|
|
this.settings.active.arDetect.sampling.staticCols,
|
|
|
|
|
|
this.settings.active.arDetect.canvasDimensions.sampleCanvas.width
|
|
|
|
|
|
),
|
2025-10-12 22:03:55 +02:00
|
|
|
|
left: generateSampleArray(
|
|
|
|
|
|
this.settings.active.arDetect.sampling.staticCols,
|
|
|
|
|
|
this.settings.active.arDetect.canvasDimensions.sampleCanvas.height
|
|
|
|
|
|
),
|
|
|
|
|
|
right: generateSampleArray(
|
|
|
|
|
|
this.settings.active.arDetect.sampling.staticCols,
|
|
|
|
|
|
this.settings.active.arDetect.canvasDimensions.sampleCanvas.height
|
|
|
|
|
|
)
|
2024-10-19 16:04:20 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
2025-04-10 00:45:39 +02:00
|
|
|
|
// try {
|
|
|
|
|
|
// this.showDebugCanvas();
|
|
|
|
|
|
// } catch (e) {
|
|
|
|
|
|
// console.error('FALIED TO CREATE DEBUGG CANVAS', e);
|
|
|
|
|
|
// }
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
2025-04-21 22:24:32 +02:00
|
|
|
|
try {
|
|
|
|
|
|
if (this.settings.active.ui.dev?.aardDebugOverlay?.showOnStartup) {
|
|
|
|
|
|
this.showDebugCanvas();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(`[uw::aard] failed to create debug UI:`, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-29 23:58:16 +01:00
|
|
|
|
this.startCheck();
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2025-04-27 17:52:04 +02:00
|
|
|
|
private createCanvas(canvasId: string, canvasType?: 'webgl' | 'legacy') {
|
2025-10-12 22:03:55 +02:00
|
|
|
|
ROW_SIZE = this.settings.active.arDetect.canvasDimensions.sampleCanvas.width * PIXEL_SIZE;
|
|
|
|
|
|
|
2024-12-26 14:58:14 +01:00
|
|
|
|
if (canvasType) {
|
|
|
|
|
|
if (canvasType === this.settings.active.arDetect.aardType || this.settings.active.arDetect.aardType === 'auto') {
|
|
|
|
|
|
if (canvasType === 'webgl') {
|
|
|
|
|
|
return new GlCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'main-gl'});
|
2025-04-27 17:52:04 +02:00
|
|
|
|
} else if (canvasType === 'legacy') {
|
|
|
|
|
|
return new FallbackCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'main-legacy'});
|
2024-12-26 14:58:14 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
// TODO: throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// TODO: throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (['auto', 'webgl'].includes(this.settings.active.arDetect.aardType)) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return new GlCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'main-gl'});
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
if (this.settings.active.arDetect.aardType !== 'webgl') {
|
2025-04-27 17:53:50 +02:00
|
|
|
|
return new FallbackCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'main-legacy'});
|
2024-12-26 14:58:14 +01:00
|
|
|
|
}
|
2025-05-04 02:18:58 +02:00
|
|
|
|
this.logger.error('createCanvas', 'could not create webgl canvas:', e);
|
2024-12-26 14:58:14 +01:00
|
|
|
|
this.eventBus.send('uw-config-broadcast', {type: 'aard-error', aardErrors: {webglError: true}});
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (this.settings.active.arDetect.aardType === 'legacy') {
|
2025-04-27 17:53:50 +02:00
|
|
|
|
return new FallbackCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'main-legacy'});
|
2024-12-26 14:58:14 +01:00
|
|
|
|
} else {
|
2025-05-04 02:18:58 +02:00
|
|
|
|
this.logger.error('createCanvas', 'invalid value in settings.arDetect.aardType:', this.settings.active.arDetect.aardType);
|
2024-12-26 14:58:14 +01:00
|
|
|
|
this.eventBus.send('uw-config-broadcast', {type: 'aard-error', aardErrors: {invalidSettings: true}});
|
|
|
|
|
|
throw 'AARD_INVALID_SETTINGS';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Creates and shows debug canvas
|
|
|
|
|
|
* @param canvasId
|
|
|
|
|
|
*/
|
|
|
|
|
|
private showDebugCanvas() {
|
|
|
|
|
|
if (!this.canvasStore.debug) {
|
|
|
|
|
|
this.canvasStore.debug = new GlDebugCanvas({...this.settings.active.arDetect.canvasDimensions.sampleCanvas, id: 'uw-debug-gl'});
|
|
|
|
|
|
}
|
2025-04-05 01:31:06 +02:00
|
|
|
|
this.canvasStore.debug.enableFx();
|
|
|
|
|
|
if (!this.debugConfig.debugUi) {
|
|
|
|
|
|
this.debugConfig.debugUi = new AardDebugUi(this);
|
|
|
|
|
|
this.debugConfig.debugUi.initContainer();
|
|
|
|
|
|
this.debugConfig.debugUi.attachCanvases(this.canvasStore.main.canvas, this.canvasStore.debug.canvas);
|
|
|
|
|
|
|
|
|
|
|
|
// if we don't draw a dummy frame from _real_ sources, we can't update buffer later
|
|
|
|
|
|
this.canvasStore.debug.drawVideoFrame(this.canvasStore.main.canvas);
|
|
|
|
|
|
}
|
2025-04-03 02:59:25 +02:00
|
|
|
|
}
|
2025-04-10 00:45:39 +02:00
|
|
|
|
|
|
|
|
|
|
private hideDebugCanvas() {
|
|
|
|
|
|
if (this.debugConfig.debugUi) {
|
|
|
|
|
|
this.debugConfig?.debugUi.destroyContainer();
|
|
|
|
|
|
this.debugConfig.debugUi = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
//#endregion
|
2024-10-22 23:43:25 +02:00
|
|
|
|
|
2025-01-29 23:58:16 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Checks whether autodetection can run
|
|
|
|
|
|
*/
|
2025-04-26 04:23:57 +02:00
|
|
|
|
startCheck(arVariant?: ArVariant) {
|
2025-04-26 23:24:57 +02:00
|
|
|
|
this.arVariant = arVariant;
|
|
|
|
|
|
|
2025-01-29 23:58:16 +01:00
|
|
|
|
if (!this.videoData.player) {
|
2025-04-26 23:24:57 +02:00
|
|
|
|
// console.warn('Player not detected!');
|
|
|
|
|
|
// console.log('--- video data: ---\n', this.videoData);
|
2025-04-15 18:51:34 +02:00
|
|
|
|
return;
|
2025-01-29 23:58:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (this.siteSettings.data.enableAard[this.videoData.player.environment] === ExtensionMode.Enabled) {
|
|
|
|
|
|
this.start();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 19:35:12 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Clears autoDisable flag
|
|
|
|
|
|
*/
|
|
|
|
|
|
clearAutoDisabled() {
|
|
|
|
|
|
this.status.autoDisabled = false;
|
|
|
|
|
|
this.timers.autoDisableAt = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 23:43:25 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Starts autodetection loop.
|
|
|
|
|
|
*/
|
2024-10-14 00:28:13 +02:00
|
|
|
|
start() {
|
2025-10-08 19:35:12 +02:00
|
|
|
|
this.clearAutoDisabled();
|
2024-12-30 23:02:55 +01:00
|
|
|
|
this.forceFullRecheck = true;
|
2024-10-22 23:43:25 +02:00
|
|
|
|
if (this.videoData.resizer.lastAr.type === AspectRatioType.AutomaticUpdate) {
|
2024-10-14 00:28:13 +02:00
|
|
|
|
// ensure first autodetection will run in any case
|
2024-10-22 23:43:25 +02:00
|
|
|
|
this.videoData.resizer.lastAr = {type: AspectRatioType.AutomaticUpdate, ratio: this.defaultAr};
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-13 01:43:07 +01:00
|
|
|
|
// do full reset of test samples
|
|
|
|
|
|
this.testResults = initAardTestResults(this.settings.active.arDetect);
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.verticalTestResults = initAardTestResults(this.settings.active.arDetect);
|
2024-11-13 01:43:07 +01:00
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
if (this.animationFrame) {
|
|
|
|
|
|
window.cancelAnimationFrame(this.animationFrame);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.status.aardActive = true;
|
|
|
|
|
|
this.animationFrame = window.requestAnimationFrame( (ts: DOMHighResTimeStamp) => this.onAnimationFrame(ts));
|
2025-10-08 19:35:12 +02:00
|
|
|
|
|
|
|
|
|
|
// set auto-disable timer if detection timeout is set
|
|
|
|
|
|
if (this.settings.active.arDetect.autoDisable.ifNotChanged) {
|
|
|
|
|
|
this.timers.autoDisableAt = Date.now() + this.settings.active.arDetect.autoDisable.ifNotChangedTimeout;
|
|
|
|
|
|
}
|
2024-10-22 23:43:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Runs autodetection ONCE.
|
|
|
|
|
|
* If autodetection loop is running, this will also stop autodetection loop.
|
|
|
|
|
|
*/
|
2025-04-15 18:51:34 +02:00
|
|
|
|
step(options?: {noCache?: boolean}) {
|
2024-10-22 23:43:25 +02:00
|
|
|
|
this.stop();
|
2025-04-15 18:51:34 +02:00
|
|
|
|
|
|
|
|
|
|
if (options?.noCache) {
|
|
|
|
|
|
this.testResults = initAardTestResults(this.settings.active.arDetect);
|
|
|
|
|
|
this.verticalTestResults = initAardTestResults(this.settings.active.arDetect);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 23:43:25 +02:00
|
|
|
|
this.main();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Stops autodetection.
|
|
|
|
|
|
*/
|
|
|
|
|
|
stop() {
|
|
|
|
|
|
if (this.animationFrame) {
|
|
|
|
|
|
window.cancelAnimationFrame(this.animationFrame);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#region animationFrame, scheduling, and other shit
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Checks whether conditions for granting a frame check are fulfilled
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
private canTriggerFrameCheck() {
|
2025-10-08 19:35:12 +02:00
|
|
|
|
|
|
|
|
|
|
// console.log('ard check status:', this.status);
|
2024-10-22 23:43:25 +02:00
|
|
|
|
|
|
|
|
|
|
// if video was paused & we know that we already checked that frame,
|
|
|
|
|
|
// we will not check it again.
|
|
|
|
|
|
const videoState = this.getVideoPlaybackState();
|
2025-10-08 19:35:12 +02:00
|
|
|
|
const polling = this.settings.active.arDetect.polling;
|
|
|
|
|
|
const now = Date.now();
|
2024-10-22 23:43:25 +02:00
|
|
|
|
|
|
|
|
|
|
if (videoState !== VideoPlaybackState.Playing) {
|
|
|
|
|
|
if (this.status.lastVideoStatus === videoState) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-08 19:35:12 +02:00
|
|
|
|
if (this.status.autoDisabled) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.timers.autoDisableAt < now) {
|
|
|
|
|
|
this.status.autoDisabled = true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-10-22 23:43:25 +02:00
|
|
|
|
this.status.lastVideoStatus = videoState;
|
|
|
|
|
|
|
2025-10-08 19:35:12 +02:00
|
|
|
|
const tabVisible = document.visibilityState === 'visible';
|
|
|
|
|
|
if (!tabVisible && polling.runInBackgroundTabs === AardPollingOptions.No) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.videoData.player.isTooSmall && polling.runOnSmallVideos === AardPollingOptions.No) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-12-30 23:02:55 +01:00
|
|
|
|
|
2025-10-08 19:35:12 +02:00
|
|
|
|
const isActive = (tabVisible || polling.runInBackgroundTabs !== AardPollingOptions.Reduced)
|
|
|
|
|
|
&& (!this.videoData.player.isTooSmall || polling.runOnSmallVideos !== AardPollingOptions.Reduced);
|
|
|
|
|
|
const nextCheck = isActive ? this.timers.nextFrameCheckTime : this.timers.reducedPollingNextCheckTime;
|
|
|
|
|
|
|
|
|
|
|
|
if (now < nextCheck) {
|
2024-10-22 23:43:25 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
this.timers.nextFrameCheckTime = now + this.settings.active.arDetect.timers.playing;
|
|
|
|
|
|
this.timers.reducedPollingNextCheckTime = now + this.settings.active.arDetect.timers.playingReduced;
|
2024-10-22 23:43:25 +02:00
|
|
|
|
return true;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 19:35:12 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Bootstraps the main loop.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Honestly this doesn't need description, but I want to put some green
|
|
|
|
|
|
* between two adjacent functions.
|
|
|
|
|
|
*/
|
2024-10-14 00:28:13 +02:00
|
|
|
|
private onAnimationFrame(ts: DOMHighResTimeStamp) {
|
|
|
|
|
|
if (this.canTriggerFrameCheck()) {
|
2024-10-22 23:43:25 +02:00
|
|
|
|
resetAardTestResults(this.testResults);
|
2024-11-07 02:26:19 +01:00
|
|
|
|
resetSamples(this.canvasSamples);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
resetSubtitleScanResults(this.testResults);
|
2024-10-14 00:28:13 +02:00
|
|
|
|
this.main();
|
2024-12-30 23:02:55 +01:00
|
|
|
|
this.forceFullRecheck = false;
|
2024-11-07 02:26:19 +01:00
|
|
|
|
} else {
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
this.animationFrame = window.requestAnimationFrame( (ts: DOMHighResTimeStamp) => this.onAnimationFrame(ts));
|
|
|
|
|
|
}
|
2024-10-22 23:43:25 +02:00
|
|
|
|
//#endregion
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Main loop for scanning aspect ratio changes
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async main() {
|
2025-10-12 22:03:55 +02:00
|
|
|
|
const arConf = this.settings.active.arDetect;
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
try {
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.next();
|
|
|
|
|
|
|
2025-04-03 02:59:25 +02:00
|
|
|
|
let imageData: Uint8Array;
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.current.start = performance.now();
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
// We abuse a do-while loop to eat our cake (get early returns)
|
|
|
|
|
|
// and have it, too (if we return early, we still execute code
|
|
|
|
|
|
// at the end of this function)
|
2025-10-12 22:03:55 +02:00
|
|
|
|
scanFrame:
|
|
|
|
|
|
{
|
2025-04-03 02:59:25 +02:00
|
|
|
|
imageData = await new Promise<Uint8Array>(
|
2024-10-14 00:28:13 +02:00
|
|
|
|
resolve => {
|
2024-12-26 14:58:14 +01:00
|
|
|
|
try {
|
|
|
|
|
|
this.canvasStore.main.drawVideoFrame(this.video);
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.current.draw = performance.now() - this.timer.current.start;
|
2024-12-26 14:58:14 +01:00
|
|
|
|
resolve(this.canvasStore.main.getImageData());
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
if (e.name === 'SecurityError') {
|
|
|
|
|
|
this.eventBus.send('uw-config-broadcast', {type: 'aard-error', aardErrors: {cors: true}});
|
|
|
|
|
|
this.stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.canvasStore.main instanceof FallbackCanvas) {
|
|
|
|
|
|
if (this.inFallback) {
|
|
|
|
|
|
this.eventBus.send('uw-config-broadcast', {type: 'aard-error', aardErrors: this.fallbackReason});
|
|
|
|
|
|
this.stop();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.eventBus.send('uw-config-broadcast', {type: 'aard-error', aardErrors: {fallbackCanvasError: true}});
|
|
|
|
|
|
this.stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (arConf.aardType === 'auto') {
|
2024-12-26 14:58:14 +01:00
|
|
|
|
this.canvasStore.main.destroy();
|
2025-04-27 17:52:04 +02:00
|
|
|
|
this.canvasStore.main = this.createCanvas('main-gl', 'legacy');
|
2024-12-26 14:58:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
this.inFallback = true;
|
|
|
|
|
|
this.fallbackReason = {cors: true};
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (arConf.aardType !== 'auto') {
|
2024-12-26 14:58:14 +01:00
|
|
|
|
this.stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
);
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.current.getImage = performance.now() - this.timer.current.start;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2024-10-15 17:38:04 +02:00
|
|
|
|
// STEP 1:
|
|
|
|
|
|
// Test if corners are black. If they're not, we can immediately quit the loop.
|
2025-10-12 22:03:55 +02:00
|
|
|
|
// For performances of measurements, checking orientation of letterbox is part of fastBlackLevel
|
|
|
|
|
|
const lastValidLetterboxOrientation = this.testResults.lastValidLetterboxOrientation;
|
|
|
|
|
|
|
|
|
|
|
|
orientationCheck:
|
|
|
|
|
|
{
|
|
|
|
|
|
this.getBlackLevelFast(
|
|
|
|
|
|
imageData, 3, 1,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.width,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.height
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.NotLetterbox) {
|
|
|
|
|
|
break orientationCheck;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.letterboxOrientationScan(
|
|
|
|
|
|
imageData,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.width,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.height
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.current.fastBlackLevel = performance.now() - this.timer.current.start;
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
// if we detect no letterbox, we don't test anything — instead, we immediately reset
|
|
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.NotLetterbox) {
|
2024-10-14 00:28:13 +02:00
|
|
|
|
// TODO: reset aspect ratio to "AR not applied"
|
|
|
|
|
|
this.testResults.lastStage = 1;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
this.testResults.letterboxSize = 0;
|
2024-12-31 02:50:33 +01:00
|
|
|
|
this.testResults.letterboxOffset = 0;
|
2024-12-31 03:14:29 +01:00
|
|
|
|
resetGuardLine(this.testResults);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
|
|
|
|
|
break scanFrame;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we detect both letterbox and pillarbox, we keep things as they are but avoid scanning further
|
|
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.Both) {
|
|
|
|
|
|
this.testResults.lastStage = 1;
|
|
|
|
|
|
|
|
|
|
|
|
break scanFrame;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
// If lastValidLetterboxOrientation changed, we reset guard line (& gang), but continue processing
|
|
|
|
|
|
if (lastValidLetterboxOrientation !== this.testResults.lastValidLetterboxOrientation) {
|
|
|
|
|
|
this.forceFullRecheck = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We only do subtitle check when orientation is letterbox
|
|
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.Letterbox) {
|
|
|
|
|
|
this.subtitleScan(
|
|
|
|
|
|
imageData,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.width,
|
|
|
|
|
|
arConf.canvasDimensions.sampleCanvas.height,
|
|
|
|
|
|
false,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2024-10-22 23:43:25 +02:00
|
|
|
|
// Note that subtitle check should reset aspect ratio outright, regardless of what other tests revealed.
|
|
|
|
|
|
// Also note that subtitle check should run on newest aspect ratio data, rather than lag one frame behind
|
|
|
|
|
|
// But implementation details are something for future Tam to figure out
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// If forceFullRecheck is set, then 'not letterbox' should always force-reset the aspect ratio
|
|
|
|
|
|
// (as aspect ratio may have been set manually while autodetection was off)
|
|
|
|
|
|
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// If debugging is enable,
|
|
|
|
|
|
this.canvasStore.debug?.drawBuffer(imageData);
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
processUpdate:
|
|
|
|
|
|
{
|
|
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.NotLetterbox) {
|
2025-04-21 22:24:32 +02:00
|
|
|
|
// console.warn('DETECTED NOT LETTERBOX! (resetting)')
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.arChanged();
|
2025-10-12 22:03:55 +02:00
|
|
|
|
this.updateAspectRatio(this.defaultAr, {forceReset: true});
|
|
|
|
|
|
break processUpdate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (this.testResults.subtitleDetected && arConf.subtitles.subtitleCropMode !== AardSubtitleCropMode.CropSubtitles) {
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (arConf.subtitles.subtitleCropMode === AardSubtitleCropMode.ResetAR) {
|
|
|
|
|
|
this.updateAspectRatio(this.defaultAr, {forceReset: true});
|
|
|
|
|
|
this.timers.pauseUntil = Date.now() + arConf.subtitles.resumeAfter;
|
|
|
|
|
|
|
|
|
|
|
|
} else if (arConf.subtitles.subtitleCropMode === AardSubtitleCropMode.ResetAndDisable) {
|
|
|
|
|
|
this.updateAspectRatio(this.defaultAr, {forceReset: true});
|
|
|
|
|
|
this.status.autoDisabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break processUpdate;
|
2025-04-05 01:31:06 +02:00
|
|
|
|
}
|
2024-12-30 23:02:55 +01:00
|
|
|
|
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// if detection is uncertain, we don't do anything at all (unless if guardline was broken, in which case we reset)
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (this.testResults.aspectRatioUncertain) {
|
|
|
|
|
|
// this.timer.arChanged();
|
|
|
|
|
|
|
|
|
|
|
|
// if (
|
|
|
|
|
|
// this.testResults.aspectRatioCheck.frontCandidate < this.testResults.guardLine.front
|
|
|
|
|
|
// || this.testResults.aspectRatioCheck.backCandidate > this.testResults.guardLine.back
|
|
|
|
|
|
// || this.testResults.aspectRatioCheck.frontCandidate === -1
|
|
|
|
|
|
// || this.testResults.aspectRatioCheck.backCandidate === -1
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
// this.updateAspectRatio(this.defaultAr, {uncertainDetection: true, forceReset: true});
|
|
|
|
|
|
// }
|
2025-04-05 01:31:06 +02:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
break processUpdate;
|
2025-04-05 01:31:06 +02:00
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// TODO: emit debug values if debugging is enabled
|
|
|
|
|
|
this.testResults.isFinished = true;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.testResults.guardLine.front = this.testResults.aspectRatioCheck.frontCandidate;
|
|
|
|
|
|
this.testResults.guardLine.back = this.testResults.aspectRatioCheck.backCandidate;
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-22 02:44:28 +02:00
|
|
|
|
const finalAr = this.getAr();
|
|
|
|
|
|
if (finalAr > 0) {
|
|
|
|
|
|
this.updateAspectRatio(finalAr);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.testResults.aspectRatioInvalid = true;
|
|
|
|
|
|
this.testResults.aspectRatioInvalidReason = finalAr.toFixed(3);
|
|
|
|
|
|
}
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// }
|
2024-12-30 23:02:55 +01:00
|
|
|
|
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// if we got "no letterbox" OR aspectRatioUpdated
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
2025-04-03 02:59:25 +02:00
|
|
|
|
|
|
|
|
|
|
if (this.canvasStore.debug) {
|
2025-04-05 01:31:06 +02:00
|
|
|
|
// this.canvasStore.debug.drawBuffer(imageData);
|
2025-04-15 18:51:34 +02:00
|
|
|
|
this.timer.getAverage();
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.debugConfig?.debugUi?.updateTestResults(this.testResults, this.timers);
|
2025-04-03 02:59:25 +02:00
|
|
|
|
}
|
2024-10-22 23:43:25 +02:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.warn('[Ultrawidify] Aspect ratio autodetection crashed for some reason.\n\nsome reason:', e);
|
2025-04-26 23:24:57 +02:00
|
|
|
|
this.videoData.resizer.setAr({type: AspectRatioType.AutomaticUpdate, ratio: this.defaultAr, variant: this.arVariant});
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getVideoPlaybackState(): VideoPlaybackState {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (this.video.ended) {
|
|
|
|
|
|
return VideoPlaybackState.Ended;
|
|
|
|
|
|
} else if (this.video.paused) {
|
|
|
|
|
|
return VideoPlaybackState.Paused;
|
|
|
|
|
|
} else if (this.video.error) {
|
|
|
|
|
|
return VideoPlaybackState.Error;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return VideoPlaybackState.Playing;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2025-05-04 02:18:58 +02:00
|
|
|
|
this.logger.warn('getVideoPlaybackState]', `There was an error while determining video playback state.`, e);
|
2024-10-14 00:28:13 +02:00
|
|
|
|
return VideoPlaybackState.Error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#region buffer tests
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get black level of a given frame. We sample black level on very few
|
|
|
|
|
|
* positions — just the corners of the frame. If letterboxing or pillarboxing
|
|
|
|
|
|
* exists, then pixels in the corners of the frame should be the blackest
|
|
|
|
|
|
* it gets.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Sampling pattern are four lines, each shooting from its respective corner.
|
|
|
|
|
|
* Value of 'sample' parameter determines how many pixels along this line we
|
|
|
|
|
|
* are going to sample. Offset means how many pixels of those four lines we
|
|
|
|
|
|
* are going to skip before we start sampling.
|
|
|
|
|
|
*
|
|
|
|
|
|
* x→ 0 1 ... ... x-1
|
|
|
|
|
|
* y↓ × ------------... ...------------ ×
|
|
|
|
|
|
* 0 | 1 1 |
|
|
|
|
|
|
* 1 | 2 2 |
|
|
|
|
|
|
* : | . . :
|
|
|
|
|
|
* : . .
|
|
|
|
|
|
*
|
|
|
|
|
|
* : . . :
|
|
|
|
|
|
* | . . |
|
|
|
|
|
|
* | 2 2 |
|
|
|
|
|
|
* h-1 | 1 1 |
|
|
|
|
|
|
* × ------------... ...------------ ×
|
|
|
|
|
|
*
|
|
|
|
|
|
*
|
|
|
|
|
|
* IMPORTANT NOTES
|
|
|
|
|
|
* <> imageData is one-dimensional array, so we need to account for that.
|
|
|
|
|
|
* <> blackLevel is the darkest brightest subpixel detected
|
|
|
|
|
|
* <> If image has no crop, then this function WILL NOT get the true black level.
|
|
|
|
|
|
* In that case, we don't get an accurate black level, but we know straight
|
|
|
|
|
|
* away that the image is uncropped. If image is uncropped, we can skip other,
|
|
|
|
|
|
* more expensive tests.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param imageData array of pixels (4 bytes/fields per pixel)
|
|
|
|
|
|
* @param samples number of samples per corner
|
|
|
|
|
|
* @param width width of the frame
|
|
|
|
|
|
* @param height height of the frame
|
|
|
|
|
|
*/
|
|
|
|
|
|
private getBlackLevelFast(imageData: Uint8Array, samples: number, offset: number, width: number, height: number) {
|
|
|
|
|
|
// there's 4 points for each sample, and 3 components for each of the sampling points.
|
|
|
|
|
|
const pixelValues = new Array<number>(samples * 12);
|
|
|
|
|
|
let pvi = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* We should ensure we are accessing pixels in ordered manner in order to
|
|
|
|
|
|
* take advantage of data locality.
|
|
|
|
|
|
*/
|
|
|
|
|
|
const end = offset + samples;
|
|
|
|
|
|
for (let i = offset; i < end; i++) {
|
2025-10-12 22:03:55 +02:00
|
|
|
|
const px_r = (i * ROW_SIZE) + (i * PIXEL_SIZE); // red component starts here
|
2024-10-14 00:28:13 +02:00
|
|
|
|
pixelValues[pvi++] = imageData[px_r];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[px_r + 1];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[px_r + 2];
|
2025-04-03 02:59:25 +02:00
|
|
|
|
imageData[px_r + 3] = GlDebugType.BlackLevelSample;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
const endpx_r = px_r + ROW_SIZE - (i * PIXEL_SIZE * 2) - PIXEL_SIZE; // - twice the offset to mirror the diagonal
|
2024-10-14 00:28:13 +02:00
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r + 1];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r + 2];
|
2025-04-03 02:59:25 +02:00
|
|
|
|
imageData[endpx_r + 3] = GlDebugType.BlackLevelSample;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// now let's populate the bottom two corners
|
|
|
|
|
|
for (let i = end; i --> offset;) {
|
|
|
|
|
|
const row = height - i - 1; // since first row is 0, last row is height - 1
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
const px_r = (row * ROW_SIZE) + (i * PIXEL_SIZE);
|
2024-10-14 00:28:13 +02:00
|
|
|
|
pixelValues[pvi++] = imageData[px_r];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[px_r + 1];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[px_r + 2];
|
2025-04-03 02:59:25 +02:00
|
|
|
|
imageData[px_r + 3] = GlDebugType.BlackLevelSample;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
const endpx_r = px_r + (ROW_SIZE) - (i * PIXEL_SIZE * 2) - PIXEL_SIZE; // - twice the offset to mirror the diagonal
|
2024-10-14 00:28:13 +02:00
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r + 1];
|
|
|
|
|
|
pixelValues[pvi++] = imageData[endpx_r + 2];
|
2025-04-03 02:59:25 +02:00
|
|
|
|
imageData[endpx_r + 3] = GlDebugType.BlackLevelSample;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let min = 255;
|
|
|
|
|
|
let avg = 0;
|
|
|
|
|
|
let p = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < pixelValues.length; i++) {
|
|
|
|
|
|
p = pixelValues[i];
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
|
|
if (p < pixelValues[i]) {
|
|
|
|
|
|
p = pixelValues[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
|
|
if (p < pixelValues[i]) {
|
|
|
|
|
|
p = pixelValues[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
avg += p;
|
|
|
|
|
|
if (p < min) {
|
|
|
|
|
|
min = p;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
// While there's 4 bytes / 3 values per pixel, a
|
|
|
|
|
|
// avg only contains highest subpixel ... so we really
|
|
|
|
|
|
// only take one sample per pixel instead of 3/4
|
|
|
|
|
|
avg = avg / samples;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (avg > this.testResults.blackThreshold) {
|
|
|
|
|
|
this.testResults.letterboxOrientation = LetterboxOrientation.NotLetterbox;
|
|
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
|
|
|
|
|
// only update black level if not letterbox.
|
|
|
|
|
|
// NOTE: but maybe we could, if blackLevel can only get lower than
|
|
|
|
|
|
// the default value.
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.NotLetterbox) {
|
2024-12-30 23:02:55 +01:00
|
|
|
|
this.testResults.aspectRatioUncertain = false;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (min < this.testResults.blackLevel) {
|
|
|
|
|
|
this.testResults.blackLevel = min;
|
|
|
|
|
|
this.testResults.blackThreshold = min + 16;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-30 23:02:55 +01:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Checks orientation of black bars.
|
|
|
|
|
|
* @param imageData
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
|
|
|
|
|
private letterboxOrientationScan(imageData: Uint8Array, width: number, height: number) {
|
|
|
|
|
|
const lastPixelOffset = ROW_SIZE - PIXEL_SIZE;
|
|
|
|
|
|
const imageSize = ROW_SIZE * height;
|
|
|
|
|
|
|
|
|
|
|
|
const xLimit = this.settings.active.arDetect.letterboxOrientationScan.letterboxLimit;
|
|
|
|
|
|
const yLimit = this.settings.active.arDetect.letterboxOrientationScan.pillarboxLimit;
|
|
|
|
|
|
|
|
|
|
|
|
let letterbox = true, pillarbox = true;
|
|
|
|
|
|
let xCount = 0, yCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// scan top row
|
|
|
|
|
|
for (let i = 0; i < ROW_SIZE; i += PIXEL_SIZE) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
imageData[i ] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+1] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+2] > this.testResults.blackThreshold
|
|
|
|
|
|
) {
|
|
|
|
|
|
imageData[i + 3] = GlDebugType.LetterboxOrientationScanImageDetection
|
|
|
|
|
|
if (++xCount > xLimit) {
|
|
|
|
|
|
letterbox = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
imageData[i+3] = GlDebugType.LetterboxOrientationScanTrace
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
|
|
|
|
|
// scan sides
|
|
|
|
|
|
for (let i = 0; i < imageSize; i += ROW_SIZE) {
|
|
|
|
|
|
const lastPx = i + lastPixelOffset;
|
|
|
|
|
|
|
|
|
|
|
|
// left side
|
|
|
|
|
|
if (
|
|
|
|
|
|
imageData[i ] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+1] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+2] > this.testResults.blackThreshold
|
|
|
|
|
|
) {
|
|
|
|
|
|
imageData[i+3] = GlDebugType.LetterboxOrientationScanImageDetection;
|
|
|
|
|
|
if (++yCount > yLimit) {
|
|
|
|
|
|
pillarbox = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
imageData[i+3] = GlDebugType.LetterboxOrientationScanTrace;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// right side
|
|
|
|
|
|
if (
|
|
|
|
|
|
imageData[lastPx ] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[lastPx+1] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[lastPx+2] > this.testResults.blackThreshold
|
|
|
|
|
|
) {
|
|
|
|
|
|
imageData[lastPx+3] = GlDebugType.LetterboxOrientationScanImageDetection;
|
|
|
|
|
|
if (++yCount > yLimit) {
|
|
|
|
|
|
pillarbox = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
imageData[lastPx+3] = GlDebugType.LetterboxOrientationScanTrace;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// scan bottom row
|
|
|
|
|
|
if (letterbox) {
|
|
|
|
|
|
for (let i = ROW_SIZE * (height - 1); i < imageSize; i += PIXEL_SIZE) {
|
|
|
|
|
|
if ( imageData[i ] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+1] > this.testResults.blackThreshold
|
|
|
|
|
|
|| imageData[i+2] > this.testResults.blackThreshold
|
|
|
|
|
|
) {
|
|
|
|
|
|
imageData[i + 3] = GlDebugType.LetterboxOrientationScanImageDetection
|
|
|
|
|
|
if (++xCount > xLimit) {
|
|
|
|
|
|
letterbox = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
imageData[i+3] = GlDebugType.LetterboxOrientationScanTrace
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// determine result
|
|
|
|
|
|
if (letterbox && pillarbox) {
|
|
|
|
|
|
this.testResults.letterboxOrientation = LetterboxOrientation.Both;
|
|
|
|
|
|
} else if (letterbox) {
|
|
|
|
|
|
this.testResults.letterboxOrientation = LetterboxOrientation.Letterbox;
|
|
|
|
|
|
this.testResults.lastValidLetterboxOrientation = LetterboxOrientation.Letterbox;
|
|
|
|
|
|
} else if (pillarbox) {
|
|
|
|
|
|
this.testResults.letterboxOrientation = LetterboxOrientation.Pillarbox;
|
|
|
|
|
|
this.testResults.lastValidLetterboxOrientation = LetterboxOrientation.Pillarbox;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.testResults.letterboxOrientation = LetterboxOrientation.NotLetterbox;
|
|
|
|
|
|
}
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Updates letterbox edge (updates imageLine and guardLine)
|
|
|
|
|
|
* @param crossDimension height of the sample frame (or width, if pillarbox)
|
|
|
|
|
|
* @param topCandidate First line of image data on the top of the frame
|
|
|
|
|
|
* @param bottomCandidate First line with image data on the bottom of the frame
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
private updateLetterboxEdgeCandidates(crossDimension: number, topCandidate: number, bottomCandidate: number) {
|
|
|
|
|
|
const bottomDistance = (crossDimension - bottomCandidate);
|
|
|
|
|
|
const maxOffset = ~~(crossDimension * this.settings.active.arDetect.edgeDetection.maxLetterboxOffset);
|
|
|
|
|
|
const diff = Math.abs(topCandidate - bottomDistance);
|
|
|
|
|
|
const candidateAvg = ~~((topCandidate + bottomDistance) * 0.5);
|
|
|
|
|
|
|
|
|
|
|
|
this.testResults.aspectRatioCheck.frontCandidate = topCandidate;
|
|
|
|
|
|
this.testResults.aspectRatioCheck.backCandidate = bottomCandidate;
|
|
|
|
|
|
|
|
|
|
|
|
if (diff > maxOffset) {
|
|
|
|
|
|
this.testResults.aspectRatioUncertain = true;
|
|
|
|
|
|
this.testResults.aspectRatioUncertainReason = AardUncertainReason.LetterboxNotCenteredEnough;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.testResults.letterboxSize = candidateAvg;
|
|
|
|
|
|
this.testResults.letterboxOffset = diff;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-13 17:35:37 +02:00
|
|
|
|
* Scans for subtitles
|
2024-10-14 00:28:13 +02:00
|
|
|
|
* @param imageData
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
2025-10-13 17:35:37 +02:00
|
|
|
|
* @returns
|
2024-10-14 00:28:13 +02:00
|
|
|
|
*/
|
2025-10-13 17:35:37 +02:00
|
|
|
|
private subtitleScan(imageData: Uint8Array, width: number, height: number, skipAdvancedScan: boolean) {
|
|
|
|
|
|
const scanConf = this.settings.active.arDetect.subtitles;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.testResults.subtitleDetected = false;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// SubtitleScanResult Regions
|
|
|
|
|
|
const ssrRegions = this.testResults.subtitleScan.regions;
|
|
|
|
|
|
const halfHeight = Math.floor(height / 2);
|
2024-12-31 03:15:10 +01:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
resetSubtitleScanResults(this.testResults);
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.subtitleScanRegionIterative(
|
|
|
|
|
|
imageData, height,
|
|
|
|
|
|
2, halfHeight,
|
|
|
|
|
|
scanConf.refiningScanSpacing, scanConf.minDetections,
|
|
|
|
|
|
ssrRegions.top,
|
|
|
|
|
|
)
|
2024-12-31 03:15:10 +01:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.subtitleScanRegionIterative(
|
|
|
|
|
|
imageData, height,
|
|
|
|
|
|
height - 3, halfHeight,
|
|
|
|
|
|
-scanConf.refiningScanSpacing, scanConf.minDetections,
|
|
|
|
|
|
ssrRegions.bottom,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ssrRegions.top.uncertain || ssrRegions.bottom.uncertain) {
|
|
|
|
|
|
this.testResults.aspectRatioUncertain = true;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
} else {
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.testResults.aspectRatioUncertain = false;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (ssrRegions.top.firstSubtitle !== -1 || ssrRegions.bottom.firstSubtitle !== -1) {
|
|
|
|
|
|
this.testResults.subtitleDetected = true;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.updateLetterboxEdgeCandidates(
|
|
|
|
|
|
height,
|
|
|
|
|
|
ssrRegions.top.firstImage,
|
|
|
|
|
|
ssrRegions.bottom.firstImage
|
|
|
|
|
|
);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
this.timer.current.subtitleScan = performance.now() - this.timer.current.start;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
/**
|
2025-10-13 17:35:37 +02:00
|
|
|
|
* Scans region of video frame for presence of subtitles
|
2024-10-14 00:28:13 +02:00
|
|
|
|
* @param imageData
|
2025-10-13 17:35:37 +02:00
|
|
|
|
* @param startRow
|
|
|
|
|
|
* @param endRow
|
|
|
|
|
|
* @param minDetections
|
|
|
|
|
|
* @param results
|
2024-10-14 00:28:13 +02:00
|
|
|
|
*/
|
2025-10-13 17:35:37 +02:00
|
|
|
|
private subtitleScanRegionLinear(
|
|
|
|
|
|
imageData: Uint8Array,
|
|
|
|
|
|
height: number,
|
|
|
|
|
|
startRow: number,
|
|
|
|
|
|
endRow: number,
|
|
|
|
|
|
scanSpacing: number,
|
|
|
|
|
|
minDetections: number,
|
|
|
|
|
|
results: AardTestResult_SubtitleRegion,
|
|
|
|
|
|
) {
|
|
|
|
|
|
results.uncertain = false;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const scanConf = this.settings.active.arDetect.subtitles;
|
|
|
|
|
|
const arConf = this.settings.active.arDetect;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
let letterCount, imageSegmentCount, potentialFadedLetterCount, potentialFadedLetterCountInvalidated, nonGradientPixelCount, letterSize, imageSize, imageSegmentSize, imageWeightedSize, segmentWeights, imageSegmentAlignment, imageSegmentAlignmentSamples,
|
|
|
|
|
|
isOnLetter, isOnImage, isBlank,
|
|
|
|
|
|
gradientRowOffset_before, gradientRowOffset_after;
|
|
|
|
|
|
let rowStart, rowEnd, rowMid, rowGTA, rowGTB; // GT = gradient test
|
|
|
|
|
|
let imageConfirmPass = false, subtitleConfirmPass = false;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
let innerIteration, outerIteration = 0;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const rowMargin = Math.floor(scanConf.scanMargin * ROW_SIZE);
|
|
|
|
|
|
const imageThreshold = Math.floor((ROW_SIZE - (rowMargin * 2)) * arConf.edgeDetection.minValidImage * PIXEL_SIZE_FRACTION);
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// search in top letterbox
|
|
|
|
|
|
outerLoop:
|
|
|
|
|
|
for (
|
|
|
|
|
|
let searchRow = startRow;
|
|
|
|
|
|
(scanSpacing > 0 && searchRow < endRow) || (scanSpacing < 0 && searchRow > endRow);
|
|
|
|
|
|
searchRow += scanSpacing
|
|
|
|
|
|
) {
|
|
|
|
|
|
innerIteration = 0;
|
|
|
|
|
|
outerIteration++;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
letterCount = 0;
|
|
|
|
|
|
potentialFadedLetterCount = 0;
|
|
|
|
|
|
imageSegmentCount = 0;
|
|
|
|
|
|
imageSegmentSize = 0;
|
|
|
|
|
|
nonGradientPixelCount = 0;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
imageSize = 0;
|
|
|
|
|
|
imageWeightedSize = 0;
|
|
|
|
|
|
segmentWeights = 0;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
letterSize = 0;
|
|
|
|
|
|
isOnLetter = false;
|
|
|
|
|
|
isOnImage = false;
|
|
|
|
|
|
isBlank = true;
|
|
|
|
|
|
potentialFadedLetterCountInvalidated = false;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
imageSegmentAlignment = 0;
|
|
|
|
|
|
imageSegmentAlignmentSamples = 0;
|
2024-10-15 17:38:04 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// Scan region is centered,
|
|
|
|
|
|
rowStart = (searchRow * ROW_SIZE) + rowMargin;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (scanSpacing > 0) {
|
|
|
|
|
|
gradientRowOffset_before = (Math.max(searchRow - 2, 0) * ROW_SIZE);
|
|
|
|
|
|
gradientRowOffset_after = (Math.min(searchRow + 2, height) * ROW_SIZE);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
} else {
|
2025-10-13 17:35:37 +02:00
|
|
|
|
gradientRowOffset_before = (Math.max(searchRow - 2, 0) * ROW_SIZE);
|
|
|
|
|
|
gradientRowOffset_after = (Math.min(searchRow + 2, height) * ROW_SIZE);
|
2024-10-15 17:38:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// exact row doesn't matter ... unless scanMargin is 0
|
|
|
|
|
|
rowEnd = ((searchRow + 1) * ROW_SIZE) - rowMargin;
|
|
|
|
|
|
rowMid = (rowStart + rowEnd) * 0.5;
|
2024-10-14 00:28:13 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const resetSubtitlePass =() => {
|
|
|
|
|
|
if (subtitleConfirmPass) {
|
|
|
|
|
|
subtitleConfirmPass = false;
|
|
|
|
|
|
searchRow -= (scanSpacing > 0 ? + 1 : -1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const updateImage = (candidate: number) => {
|
|
|
|
|
|
if (scanSpacing > 0) {
|
|
|
|
|
|
if (results.firstImage === -1 || candidate < results.firstImage) {
|
|
|
|
|
|
results.firstImage = candidate;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (results.firstImage === -1 || candidate > results.firstImage) {
|
|
|
|
|
|
results.firstImage = candidate;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* This function can break or return early only in the following situations:
|
|
|
|
|
|
*
|
|
|
|
|
|
* * we have enough letters to detect subtitles — continue outerLoop
|
|
|
|
|
|
* * Single "letter" is too wide — immediate return, as we found where the image starts
|
|
|
|
|
|
*
|
|
|
|
|
|
* Other instances require a bit more complex analysis.
|
|
|
|
|
|
*/
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
while (rowStart < rowEnd) {
|
|
|
|
|
|
innerIteration++;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const r = imageData[rowStart], g = imageData[rowStart + 1], b = imageData[rowStart + 2];
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const on = r > scanConf.subtitleSubpixelThresholdOn
|
|
|
|
|
|
|| g > scanConf.subtitleSubpixelThresholdOn
|
|
|
|
|
|
|| b > scanConf.subtitleSubpixelThresholdOn;
|
|
|
|
|
|
const off = r < scanConf.subtitleSubpixelThresholdOff
|
|
|
|
|
|
&& g < scanConf.subtitleSubpixelThresholdOff
|
|
|
|
|
|
&& b < scanConf.subtitleSubpixelThresholdOff;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (off) {
|
|
|
|
|
|
imageData[rowStart + 3] = GlDebugType.SubtitleThresholdOff;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// if isOnLetter was set, that means we've just concluded a letter segment
|
|
|
|
|
|
if (isOnLetter) {
|
|
|
|
|
|
letterCount++;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (letterCount > minDetections) {
|
|
|
|
|
|
if (results.firstSubtitle === -1) {
|
|
|
|
|
|
results.firstSubtitle = searchRow;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// if detecting subtitles only resets AR, we can return immediately
|
|
|
|
|
|
if (scanConf.subtitleCropMode !== AardSubtitleCropMode.CropSubtitles) {
|
|
|
|
|
|
break outerLoop;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
results.lastSubtitle = searchRow;
|
|
|
|
|
|
isBlank = false;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
imageConfirmPass = false;
|
|
|
|
|
|
continue outerLoop;
|
|
|
|
|
|
}
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (isOnImage) {
|
|
|
|
|
|
if (imageSegmentSize < scanConf.maxValidLetter) {
|
|
|
|
|
|
potentialFadedLetterCount++;
|
|
|
|
|
|
|
|
|
|
|
|
// track potential letter alignment
|
|
|
|
|
|
imageSegmentAlignment += (rowStart - rowMid) * imageSegmentSize * PIXEL_SIZE_FRACTION;
|
|
|
|
|
|
imageSegmentAlignmentSamples += imageSegmentSize;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
potentialFadedLetterCountInvalidated = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (imageSegmentSize > arConf.edgeDetection.minEdgeSegmentSize) {
|
|
|
|
|
|
imageSegmentCount++;
|
|
|
|
|
|
imageWeightedSize += imageSegmentSize * imageSegmentSize; // longer segments should have bigger weight
|
|
|
|
|
|
segmentWeights += imageSegmentSize;
|
|
|
|
|
|
imageSegmentSize = 0;
|
|
|
|
|
|
}
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
isOnLetter = false;
|
|
|
|
|
|
isOnImage = false;
|
|
|
|
|
|
letterSize = 0;
|
|
|
|
|
|
imageSegmentSize = 0;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
imageData[rowStart + 3] = GlDebugType.SubtitleThresholdNone;
|
|
|
|
|
|
isOnImage = true;
|
|
|
|
|
|
isBlank = false;
|
|
|
|
|
|
imageSegmentSize++;
|
|
|
|
|
|
imageSize++;
|
|
|
|
|
|
|
|
|
|
|
|
// let's see if we're accidentally detecting gradient.
|
|
|
|
|
|
// We only need to run these tests when pixels are dark.
|
|
|
|
|
|
// We keep track of number of pixels that pass the test, or don't
|
|
|
|
|
|
// need the test to begin with.
|
|
|
|
|
|
if (!on && (
|
|
|
|
|
|
imageData[rowStart ] < arConf.edgeDetection.gradientThreshold
|
|
|
|
|
|
&& imageData[rowStart + 1] < arConf.edgeDetection.gradientThreshold
|
|
|
|
|
|
&& imageData[rowStart + 2] < arConf.edgeDetection.gradientThreshold
|
|
|
|
|
|
)) {
|
|
|
|
|
|
rowGTB = rowStart - gradientRowOffset_before;
|
|
|
|
|
|
rowGTA = rowStart + gradientRowOffset_after;
|
|
|
|
|
|
|
|
|
|
|
|
// if true, then gradient.
|
|
|
|
|
|
// The first row gives technically incorrect answers for pixels directly under subtitles, but since
|
|
|
|
|
|
// "nonGradientPixelCount" is a shorthand for "we're relatively confident in this detection", potentially
|
|
|
|
|
|
// mistaking non-black pixels under subtitles for gradient isn't too problematic
|
|
|
|
|
|
if ( (imageData[rowStart ] - imageData[rowGTB ]) < arConf.edgeDetection.gradientTestMinDelta
|
|
|
|
|
|
&& (imageData[rowStart + 1] - imageData[rowGTB + 1]) < arConf.edgeDetection.gradientTestMinDelta
|
|
|
|
|
|
&& (imageData[rowStart + 2] - imageData[rowGTB + 2]) < arConf.edgeDetection.gradientTestMinDelta
|
|
|
|
|
|
&& imageData[rowGTA ] - imageData[rowStart ] > arConf.edgeDetection.gradientTestMinDeltaAfter
|
|
|
|
|
|
&& imageData[rowGTA + 1] - imageData[rowStart + 1] > arConf.edgeDetection.gradientTestMinDeltaAfter
|
|
|
|
|
|
&& imageData[rowGTA + 2] - imageData[rowStart + 2] > arConf.edgeDetection.gradientTestMinDeltaAfter
|
|
|
|
|
|
&& imageData[rowGTA ] - imageData[rowStart ] < arConf.edgeDetection.gradientTestMaxDeltaAfter
|
|
|
|
|
|
&& imageData[rowGTA + 1] - imageData[rowStart + 1] < arConf.edgeDetection.gradientTestMaxDeltaAfter
|
|
|
|
|
|
&& imageData[rowGTA + 2] - imageData[rowStart + 2] < arConf.edgeDetection.gradientTestMaxDeltaAfter
|
|
|
|
|
|
) {
|
|
|
|
|
|
// we do nothing (at this moment)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
nonGradientPixelCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
nonGradientPixelCount++;
|
|
|
|
|
|
}
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (on) { // used to detect subtitles specifically
|
|
|
|
|
|
imageData[rowStart + 3] = GlDebugType.SubtitleThresholdOn;
|
|
|
|
|
|
isOnLetter = true;
|
|
|
|
|
|
letterSize++;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// bail on invalid letter sizes — this means we're seeing image.
|
|
|
|
|
|
// in this case, we do not need image confirmation step
|
|
|
|
|
|
if (letterSize > scanConf.maxValidLetter) {
|
|
|
|
|
|
updateImage(searchRow);
|
|
|
|
|
|
isBlank = false;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
return;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
rowStart += PIXEL_SIZE;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// we need to do this once more, otherwise imageSegmentCount can be 0 & bugs happen
|
|
|
|
|
|
if (isOnImage) {
|
|
|
|
|
|
if (imageSegmentSize < scanConf.maxValidLetter) {
|
|
|
|
|
|
potentialFadedLetterCount++;
|
|
|
|
|
|
|
|
|
|
|
|
// track potential letter alignment
|
|
|
|
|
|
imageSegmentAlignment += (rowStart - rowMid) * imageSegmentSize * PIXEL_SIZE_FRACTION;
|
|
|
|
|
|
imageSegmentAlignmentSamples += imageSegmentSize;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
potentialFadedLetterCountInvalidated = true;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (imageSegmentSize > arConf.edgeDetection.minEdgeSegmentSize) {
|
|
|
|
|
|
imageSegmentCount++;
|
|
|
|
|
|
imageWeightedSize += imageSegmentSize * imageSegmentSize; // longer segments should have bigger weight
|
|
|
|
|
|
segmentWeights += imageSegmentSize;
|
|
|
|
|
|
imageSegmentSize = 0;
|
|
|
|
|
|
}
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (!imageSegmentCount || isBlank) {
|
|
|
|
|
|
isBlank = true;
|
|
|
|
|
|
imageConfirmPass = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (results.firstBlank === -1) {
|
|
|
|
|
|
results.firstBlank = searchRow;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
results.lastBlank = searchRow;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
resetSubtitlePass();
|
|
|
|
|
|
continue outerLoop;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
const averageImageSegmentSize = segmentWeights > 0 ? imageWeightedSize / segmentWeights : 0;
|
|
|
|
|
|
const gradientDetectionFrequency = 1 - (nonGradientPixelCount / imageSize);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// That's probably a subtitle as well. If subs are fading in and out,
|
|
|
|
|
|
// then there's a good chance that they won't meet the "yes, this is a letter" threshold.
|
|
|
|
|
|
if (
|
|
|
|
|
|
!potentialFadedLetterCountInvalidated
|
|
|
|
|
|
&& potentialFadedLetterCount > scanConf.minDetections
|
|
|
|
|
|
// FOR SOME REASON THIS CONDITION CAUSES EVERYTHING TO HANG:
|
|
|
|
|
|
// && (imageSegmentAlignmentSamples && imageSegmentAlignment && Math.abs(imageSegmentAlignment / imageSegmentAlignmentSamples) > scanConf.maxPotentialSubtitleMisalignment)
|
|
|
|
|
|
&& averageImageSegmentSize < scanConf.maxValidLetter
|
|
|
|
|
|
) {
|
|
|
|
|
|
if (subtitleConfirmPass) {
|
|
|
|
|
|
if (results.firstSubtitle === -1) {
|
|
|
|
|
|
results.firstSubtitle = searchRow;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// if detecting subtitles only resets AR, we can return immediately
|
|
|
|
|
|
if (scanConf.subtitleCropMode !== AardSubtitleCropMode.CropSubtitles) {
|
|
|
|
|
|
break outerLoop;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
results.lastSubtitle = searchRow;
|
|
|
|
|
|
isBlank = false;
|
|
|
|
|
|
|
|
|
|
|
|
resetSubtitlePass();
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
subtitleConfirmPass = true;
|
|
|
|
|
|
searchRow -= scanSpacing + (scanSpacing > 0 ? - 1 : 1);
|
|
|
|
|
|
continue outerLoop;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// If we are here, subtitles weren't confirmed
|
|
|
|
|
|
resetSubtitlePass();
|
2025-04-15 18:51:34 +02:00
|
|
|
|
|
2024-10-19 16:04:20 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// If we detect gradient, that's instant fail.
|
|
|
|
|
|
// We still save uncertain detection to firstImage, because iterative scan uses that
|
|
|
|
|
|
// in order to determine which region to scan further
|
|
|
|
|
|
if (gradientDetectionFrequency > arConf.edgeDetection.gradientThreshold) {
|
|
|
|
|
|
results.uncertain = true;
|
|
|
|
|
|
updateImage(searchRow);
|
|
|
|
|
|
break outerLoop;
|
2024-10-19 16:04:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// Cases which require confirmation
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (
|
2025-10-13 17:35:37 +02:00
|
|
|
|
imageSegmentCount > arConf.edgeDetection.maxEdgeSegments // we need to confirm if there's too many segments
|
|
|
|
|
|
|| averageImageSegmentSize < arConf.edgeDetection.averageEdgeThreshold // we also need to confirm if segments are too small
|
2025-10-12 22:03:55 +02:00
|
|
|
|
) {
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (imageConfirmPass) {
|
|
|
|
|
|
results.uncertain = true;
|
|
|
|
|
|
updateImage(searchRow - 1);
|
|
|
|
|
|
break outerLoop;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
imageConfirmPass = true;
|
|
|
|
|
|
// imageConfirmPass must happen on the next row, but it's possible that we aren't
|
|
|
|
|
|
// checking row-by-row. Hence, we need to modify our scan a bit
|
|
|
|
|
|
searchRow -= scanSpacing + (scanSpacing > 0 ? - 1 : 1);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
continue outerLoop;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
if (averageImageSegmentSize > arConf.edgeDetection.averageEdgeThreshold || imageSize > imageThreshold) {
|
|
|
|
|
|
updateImage(searchRow);
|
|
|
|
|
|
break outerLoop;
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-13 17:35:37 +02:00
|
|
|
|
// we can only reach this far if we detected image
|
|
|
|
|
|
if (imageConfirmPass) {
|
|
|
|
|
|
updateImage(searchRow - 1);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
updateImage(searchRow);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
break outerLoop;
|
|
|
|
|
|
} // end of outer loop
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Tries to determine letterbox through subtitles
|
|
|
|
|
|
* @param imageData
|
|
|
|
|
|
* @param startRow
|
|
|
|
|
|
* @param endRow
|
2025-10-13 17:35:37 +02:00
|
|
|
|
* @param ROW_SIZE
|
2025-10-12 22:03:55 +02:00
|
|
|
|
* @param scanSpacing
|
|
|
|
|
|
* @param minDetections
|
|
|
|
|
|
* @param results
|
|
|
|
|
|
* @param ssrRegionName
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
private subtitleScanRegionIterative(
|
|
|
|
|
|
imageData: Uint8Array,
|
2025-10-13 17:35:37 +02:00
|
|
|
|
height: number,
|
2025-10-12 22:03:55 +02:00
|
|
|
|
startRow: number,
|
|
|
|
|
|
endRow: number,
|
|
|
|
|
|
scanSpacing: number,
|
|
|
|
|
|
minDetections: number,
|
|
|
|
|
|
results: AardTestResult_SubtitleRegion,
|
|
|
|
|
|
): boolean {
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
if (scanSpacing > -1 && scanSpacing < 1) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.subtitleScanRegionLinear(
|
|
|
|
|
|
imageData, height,
|
|
|
|
|
|
startRow, endRow,
|
|
|
|
|
|
scanSpacing, minDetections, results
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (results.firstImage === -1) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-10-13 17:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// We need to ensure small amount of overlap, in case we landed on a sus row
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (scanSpacing > 0) {
|
2025-10-13 17:35:37 +02:00
|
|
|
|
startRow = Math.max(results.firstImage - Math.floor(scanSpacing * 2), 0);
|
|
|
|
|
|
endRow = Math.min(results.firstImage + Math.floor(scanSpacing * 2), height - 1);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
} else {
|
2025-10-13 17:35:37 +02:00
|
|
|
|
startRow = Math.min(results.firstImage + Math.floor(-scanSpacing * 2), height - 1);
|
|
|
|
|
|
endRow = Math.max(results.firstImage - Math.floor(-scanSpacing * 2), 0);
|
2025-10-12 22:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scanSpacing = scanSpacing / 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-31 02:50:33 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Updates aspect ratio if new aspect ratio is different enough from the old one
|
|
|
|
|
|
*/
|
2025-10-12 22:03:55 +02:00
|
|
|
|
private updateAspectRatio(ar: number, options?: {uncertainDetection?: boolean, forceReset?: boolean}) {
|
2024-12-31 02:50:33 +01:00
|
|
|
|
// Calculate difference between two ratios
|
2025-10-12 22:03:55 +02:00
|
|
|
|
|
|
|
|
|
|
// We need to detect updates even if subtitles are detected — we just don't trigger
|
|
|
|
|
|
// the actual aspect ratio change if everything is paused.
|
|
|
|
|
|
if (this.timers.pauseUntil > Date.now() && !options?.forceReset) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-31 02:50:33 +01:00
|
|
|
|
const maxRatio = Math.max(ar, this.testResults.activeAspectRatio);
|
|
|
|
|
|
const diff = Math.abs(ar - this.testResults.activeAspectRatio);
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if ((diff / maxRatio) > this.settings.active.arDetect.allowedArVariance || options?.forceReset) {
|
2024-12-31 02:50:33 +01:00
|
|
|
|
this.videoData.resizer.updateAr({
|
|
|
|
|
|
type: AspectRatioType.AutomaticUpdate,
|
2025-10-12 22:03:55 +02:00
|
|
|
|
ratio: ar,
|
2025-04-26 23:24:57 +02:00
|
|
|
|
offset: this.testResults.letterboxOffset,
|
|
|
|
|
|
variant: this.arVariant
|
2024-12-31 02:50:33 +01:00
|
|
|
|
});
|
|
|
|
|
|
this.testResults.activeAspectRatio = ar;
|
2025-10-08 19:35:12 +02:00
|
|
|
|
|
|
|
|
|
|
if (!options?.uncertainDetection) {
|
|
|
|
|
|
if (this.settings.active.arDetect.autoDisable.onFirstChange) {
|
|
|
|
|
|
this.status.autoDisabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.settings.active.arDetect.autoDisable.ifNotChanged) {
|
|
|
|
|
|
this.timers.autoDisableAt = Date.now() + this.settings.active.arDetect.autoDisable.ifNotChangedTimeout;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
this.testResults.aspectRatioUpdated = true;
|
2024-12-31 02:50:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 23:43:25 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Calculates video's current aspect ratio based on data in testResults.
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
2025-07-15 23:44:06 +02:00
|
|
|
|
private getAr(): number {
|
2024-10-22 23:43:25 +02:00
|
|
|
|
const fileAr = this.video.videoWidth / this.video.videoHeight;
|
|
|
|
|
|
const canvasAr = this.canvasStore.main.width / this.canvasStore.main.height;
|
|
|
|
|
|
|
2025-04-10 00:46:44 +02:00
|
|
|
|
const compensatedWidth = fileAr === canvasAr ? this.canvasStore.main.width : this.video.videoWidth * this.canvasStore.main.height / (this.video.videoHeight);
|
2024-10-22 23:43:25 +02:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// console.log(`
|
|
|
|
|
|
// ———— ASPECT RATIO CALCULATION: —————
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// canvas size: ${this.canvasStore.main.width} x ${this.canvasStore.main.height} (1:${this.canvasStore.main.width / this.canvasStore.main.height})
|
|
|
|
|
|
// file size: ${this.video.videoWidth} x ${this.video.videoHeight} (1:${this.video.videoWidth / this.video.videoHeight})
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// compensated size: ${compensatedWidth} x ${this.canvasStore.main.height} (1:${compensatedWidth / this.canvasStore.main.height})
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// letterbox height: ${this.testResults.letterboxWidth}
|
|
|
|
|
|
// net video height: ${this.canvasStore.main.height - (this.testResults.letterboxWidth * 2)}
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// calculated aspect ratio -----
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2024-12-30 23:02:55 +01:00
|
|
|
|
// ${compensatedWidth} ${compensatedWidth} ${compensatedWidth}
|
|
|
|
|
|
// ——————————————— = —————————————— = —————— = ${compensatedWidth / (this.canvasStore.main.height - (this.testResults.letterboxWidth * 2))}
|
|
|
|
|
|
// ${this.canvasStore.main.height} - 2 x ${this.testResults.letterboxWidth} ${this.canvasStore.main.height} - ${2 * this.testResults.letterboxWidth} ${this.canvasStore.main.height - (this.testResults.letterboxWidth * 2)}
|
|
|
|
|
|
// `);
|
2024-12-26 14:58:14 +01:00
|
|
|
|
|
2025-10-12 22:03:55 +02:00
|
|
|
|
if (this.testResults.letterboxOrientation === LetterboxOrientation.Pillarbox) {
|
|
|
|
|
|
const compensationFactor = compensatedWidth / this.canvasStore.main.width;
|
|
|
|
|
|
const pillarboxCompensated = (this.testResults.letterboxSize * 2 * compensationFactor);
|
|
|
|
|
|
return (compensatedWidth - pillarboxCompensated) / this.canvasStore.main.height;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const heightWithoutLetterbox = this.canvasStore.main.height - (this.testResults.letterboxSize * 2);
|
|
|
|
|
|
return compensatedWidth / heightWithoutLetterbox;
|
|
|
|
|
|
}
|
2024-10-22 23:43:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-19 16:04:20 +02:00
|
|
|
|
//#endregion
|
|
|
|
|
|
|
2024-10-14 00:28:13 +02:00
|
|
|
|
}
|