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
12 changes: 12 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,18 @@ rate is `FrameRateParameters.customFps` (a plain number, default 48).
x64 ship v24 (`floor((n - 1) * ratio) + 1`). The script trims or extends
to `floor(n * ratio)` after the call (`test_184`); a frame-count assertion
that passes on one platform proves nothing about the others.
- **The target is chosen against the rate the pass receives (task #139).**
That rate is `frameRateInputFps` (app): the source's after Deinterlace and
Unblend, the only passes ahead that change it (the worker's
`rate_before(PassType::FrameRate)`). The model's default stays `pal25`.
Switching the pass on with a video selected starts it at about twice that
rate (`startingFrameRateTarget`: 25 → 50, 29.97 → 59.94, 23.976 → Custom
47.952), but only when the current target is equal or within 5% of it
(`frameRateCloseness`), so a deliberate conversion is kept, and never from
45 fps up. A target equal or within 5% is also advised on
(`pass_advice.dart`, enabled pass only, never blocking): at the same rate
RIFE and Repeat frames pass every frame through, and nearby motion looks no
smoother (issue #177).
- **Don't check an NTSC output rate on an MKV.** Matroska's 1/1000 timebase
can't hold 60000/1001, so ffprobe reports an estimate (`19001/317`) for any
NTSC rate. `integration_frame_mapping_test.dart` encodes its custom-rate
Expand Down
2 changes: 1 addition & 1 deletion app/assets/filters/core/frame_rate.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
],
"ui": {
"label": "Target rate",
"description": "The frame rate the output should run at. Custom lets you type any rate.",
"description": "The frame rate the output should run at. Custom lets you type any rate. Switched on with a video selected, the pass starts at about twice the video's rate when 25 fps would change nothing visible.",
"widget": "dropdown",
"optionLabels": {
"pal25": "PAL / SECAM (25 fps)",
Expand Down
92 changes: 92 additions & 0 deletions app/lib/models/frame_rate_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class FrameRateParameters {
);
}

/// The target in frames per second, as the worker will run it
/// ([frameRateTargetFps]).
double get targetFps => frameRateTargetFps(target, customFps);

String get summary {
if (!enabled) return 'Off';
if (target != FrameRateTarget.custom) return target.displayName;
Expand All @@ -108,3 +112,91 @@ class FrameRateParameters {
_$FrameRateParametersFromJson(json);
Map<String, dynamic> toJson() => _$FrameRateParametersToJson(this);
}

/// The rate [target] stands for, in frames per second; for
/// [FrameRateTarget.custom], [customFps] read as the worker reads it
/// (`custom_fps_fraction`): clamped to 1–240, and an NTSC decimal (23.976,
/// 29.97, 47.952, 59.94) taken as n×1000/1001. Shared by the Frame Rate and
/// Unblend passes, whose custom rates the worker reads the same way.
double frameRateTargetFps(FrameRateTarget target, double customFps) {
switch (target) {
case FrameRateTarget.pal25:
return 25;
case FrameRateTarget.ntsc2997:
return 30000 / 1001;
case FrameRateTarget.film23976:
return 24000 / 1001;
case FrameRateTarget.film24:
return 24;
case FrameRateTarget.pal50:
return 50;
case FrameRateTarget.ntsc5994:
return 60000 / 1001;
case FrameRateTarget.custom:
return customFpsValue(customFps);
}
}

/// [fps] as the worker's `custom_fps_fraction` makes it exact.
double customFpsValue(double fps) {
final v = fps.isFinite ? fps.clamp(1.0, 240.0).toDouble() : 48.0;
final ntsc = v * 1.001;
if ((v - v.roundToDouble()).abs() > 0.0015 &&
(ntsc - ntsc.roundToDouble()).abs() < 0.0015) {
return ntsc.roundToDouble() * 1000 / 1001;
}
return (v * 1000).roundToDouble() / 1000;
}

/// How a Frame Rate target compares with the rate the pass receives.
enum FrameRateCloseness {
/// Nothing to convert (within 0.05%).
equal,

/// Within 5%: a real conversion, but motion looks no smoother.
close,

/// Anything else.
far,
}

/// Compares [targetFps] with [inputFps], the rate at the pass
/// ([frameRateInputFps]). The one test behind both the panel's advice and the
/// starting target ([startingFrameRateTarget]), so the two can't disagree.
FrameRateCloseness frameRateCloseness(double targetFps, double inputFps) {
if (!(inputFps > 0)) return FrameRateCloseness.far;
final diff = (targetFps - inputFps).abs() / inputFps;
if (diff < 0.0005) return FrameRateCloseness.equal;
if (diff <= 0.05) return FrameRateCloseness.close;
return FrameRateCloseness.far;
}

/// The target to start the Frame Rate pass at when it is switched on with a
/// video selected (task #139): about twice [inputFps], the rate the pass
/// receives — or `null` to leave [current] alone. Only a target equal or
/// close to the input rate is replaced (the pass would do nothing visible),
/// so a deliberate conversion (25 → 29.97, 50 → 25) is kept; and an input of
/// 45 fps or more is left alone, since doubling it is no sensible default.
/// The model's own default stays [FrameRateTarget.pal25].
FrameRateParameters? startingFrameRateTarget(
FrameRateParameters current, double inputFps) {
if (!(inputFps > 0) || inputFps >= 45) return null;
if (frameRateCloseness(current.targetFps, inputFps) ==
FrameRateCloseness.far) {
return null;
}
bool near(double a, double b, double tolerance) =>
(a - b).abs() / b < tolerance;
if (near(inputFps, 25, 0.005)) {
return current.copyWith(target: FrameRateTarget.pal50);
}
if (near(inputFps, 30000 / 1001, 0.0005)) {
return current.copyWith(target: FrameRateTarget.ntsc5994);
}
if (near(inputFps, 24000 / 1001, 0.0005)) {
// The worker reads 47.952 as 48000/1001 (custom_fps_fraction).
return current.copyWith(target: FrameRateTarget.custom, customFps: 47.952);
}
final doubled = (inputFps * 2 * 1000).roundToDouble() / 1000;
return current.copyWith(target: FrameRateTarget.custom, customFps: doubled);
}
63 changes: 59 additions & 4 deletions app/lib/models/pass_advice.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import '../services/field_order_detector.dart' show VideoInfo;
import 'frame_rate_parameters.dart';
import 'processing_pipeline.dart';
import 'qtgmc_parameters.dart';
import 'unblend_parameters.dart';

/// A note about how an enabled pass interacts with the rest of the pipeline.
class PassAdvice {
Expand Down Expand Up @@ -39,7 +42,11 @@ class PassAdvice {
/// Pass order is fixed (see `PassListPanel.stages` and `script_generator.rs`),
/// which is what makes the ordering advice statable at all: Sharpen genuinely
/// always runs after Noise Reduction, so there is no case to qualify.
List<PassAdvice> adviseOn(ProcessingPipeline pipeline) {
///
/// [info] is the selected video, when there is one: advice that depends on the
/// source (the Frame Rate target against the rate the pass receives) is
/// silent without it.
List<PassAdvice> adviseOn(ProcessingPipeline pipeline, {VideoInfo? info}) {
final advice = <PassAdvice>[];

bool on(PassType pass) => pipeline.isPassEnabled(pass);
Expand Down Expand Up @@ -142,6 +149,46 @@ List<PassAdvice> adviseOn(ProcessingPipeline pipeline) {
}
}

// --- A Frame Rate target at (or near) the rate the pass receives ---
// The target defaults to 25 fps whatever the source (task #139, issue #177).
// At the rate the pass already receives it converts nothing (RIFE and Repeat
// frames pass every real frame through); within a few percent it is a real
// conversion, but motion looks no smoother. The rate is the one at the pass,
// after Deinterlace and Unblend: a 25i source deinterlaced at double rate
// reaches it at 50, where a 25 target is a halving and says nothing here.
if (on(PassType.frameRate) && info != null && info.frameRate > 0) {
final input = frameRateInputFps(
pipeline.deinterlace, pipeline.unblend, info.frameRate,
softTelecineDecimates: info.softTelecineDecimates);
final target = pipeline.frameRate.targetFps;
switch (frameRateCloseness(target, input)) {
case FrameRateCloseness.equal:
advice.add(PassAdvice(
PassType.frameRate,
'The target, ${_fps(target)} fps, is the rate this pass already '
'receives, so there is nothing to convert: the output has the same '
'frames as the input (RIFE and Repeat frames pass them through '
'unchanged). For smoother motion choose a higher rate, 50 or 59.94 '
'fps or Custom (48 for film), or switch the pass off.',
));
break;
case FrameRateCloseness.close:
final percent = ((target - input).abs() / input * 100);
advice.add(PassAdvice(
PassType.frameRate,
'The target, ${_fps(target)} fps, is within '
'${percent.toStringAsFixed(1)}% of the rate this pass receives '
'(${_fps(input)} fps). That suits matching a delivery standard, but '
'motion will not look smoother: most output frames sit close to a '
'real one. For smoother motion choose 50 or 59.94 fps, or Custom '
'(48 for film).',
));
break;
case FrameRateCloseness.far:
break;
}
}

// --- Levels set by hand on top of levels set automatically ---
// Automatic levels measures the picture and places black and white itself,
// and it runs first — so the manual input/output points are dropped (the
Expand Down Expand Up @@ -227,9 +274,17 @@ List<PassAdvice> adviseOn(ProcessingPipeline pipeline) {
return advice;
}

/// A rate to three decimals, trailing zeros dropped: 25, 23.976, 29.97.
String _fps(double v) {
final s = v.toStringAsFixed(3);
return s.replaceFirst(RegExp(r'0+$'), '').replaceFirst(RegExp(r'\.$'), '');
}

/// The advice for one pass, or null when there is none.
String? adviceFor(PassType pass, ProcessingPipeline pipeline) {
final matches =
adviseOn(pipeline).where((a) => a.pass == pass).map((a) => a.message);
String? adviceFor(PassType pass, ProcessingPipeline pipeline,
{VideoInfo? info}) {
final matches = adviseOn(pipeline, info: info)
.where((a) => a.pass == pass)
.map((a) => a.message);
return matches.isEmpty ? null : matches.join('\n\n');
}
45 changes: 23 additions & 22 deletions app/lib/models/unblend_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,9 @@ class UnblendParameters {
);
}

/// The target in frames per second.
double get targetFps {
switch (target) {
case FrameRateTarget.pal25:
return 25;
case FrameRateTarget.ntsc2997:
return 30000 / 1001;
case FrameRateTarget.film23976:
return 24000 / 1001;
case FrameRateTarget.film24:
return 24;
case FrameRateTarget.pal50:
return 50;
case FrameRateTarget.ntsc5994:
return 60000 / 1001;
case FrameRateTarget.custom:
return customFps;
}
}
/// The target in frames per second, as the worker reads it
/// ([frameRateTargetFps]).
double get targetFps => frameRateTargetFps(target, customFps);

String get summary {
if (!enabled) return 'Off';
Expand All @@ -79,20 +63,37 @@ class UnblendParameters {
/// The rate the Unblend pass converts from: the source's, as changed by the
/// Deinterlace pass ahead of it. Mirrors the worker's `FrameMap`s for that
/// pass (`deinterlace_frame_map`): double rate doubles it, IVTC keeps
/// cycle-1 of every cycle.
double unblendInputFps(QTGMCParameters deinterlace, double sourceFps) {
/// cycle-1 of every cycle. Soft Telecine decimates only a source decoded at a
/// pulldown rate ([softTelecineDecimates], `VideoInfo.softTelecineDecimates`);
/// at the film rate it keeps every frame, as the worker's map does.
double unblendInputFps(QTGMCParameters deinterlace, double sourceFps,
{bool softTelecineDecimates = true}) {
if (!deinterlace.enabled) return sourceFps;
switch (deinterlace.method) {
case DeinterlaceMethod.qtgmc:
case DeinterlaceMethod.bwdif:
return (deinterlace.fpsDivisor ?? 1) == 1 ? sourceFps * 2 : sourceFps;
case DeinterlaceMethod.ivtc:
case DeinterlaceMethod.softTelecine:
if (!softTelecineDecimates) return sourceFps;
return sourceFps * 4 / 5;
case DeinterlaceMethod.ivtc:
final cycle = (deinterlace.ivtcCycle ?? 5).clamp(2, 1000);
return sourceFps * (cycle - 1) / cycle;
}
}

/// The rate the Frame Rate pass receives: the source's, as changed by
/// Deinterlace ([unblendInputFps]) and then Unblend, the only passes ahead of
/// it that change the rate. Mirrors the worker's
/// `rate_before(PassType::FrameRate)`.
double frameRateInputFps(
QTGMCParameters deinterlace, UnblendParameters unblend, double sourceFps,
{bool softTelecineDecimates = true}) {
final afterDeinterlace = unblendInputFps(deinterlace, sourceFps,
softTelecineDecimates: softTelecineDecimates);
return unblend.enabled ? unblend.targetFps : afterDeinterlace;
}

/// Why Unblend can't recover [targetFps] from a pass running at [inputFps],
/// or `null` when it plausibly can. The worker makes the exact decision (it
/// also refuses a few ratios srestore would only approximate) and refuses the
Expand Down
24 changes: 24 additions & 0 deletions app/lib/viewmodels/main_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import '../models/qtgmc_parameters.dart';
import '../models/queue_item.dart';
import '../models/source_model.dart';
import '../models/processing_pipeline.dart';
import '../models/frame_rate_parameters.dart';
import '../models/unblend_parameters.dart';
import '../models/subtitle_parameters.dart';
import '../models/video_job.dart';
import '../models/processing_preset.dart';
Expand Down Expand Up @@ -1976,7 +1978,11 @@ class MainViewModel extends ChangeNotifier {

/// Toggles a pass on or off.
void togglePass(PassType pass, bool enabled) {
final wasEnabled = _processingPipeline.isPassEnabled(pass);
_processingPipeline = _processingPipeline.togglePass(pass, enabled);
if (pass == PassType.frameRate && enabled && !wasEnabled) {
_startFrameRateFromSource();
}
// Keep qtgmcParams in sync
_qtgmcParams = _processingPipeline.deinterlace;
// Sync dynamic params cache with new enabled state
Expand All @@ -1990,6 +1996,24 @@ class MainViewModel extends ChangeNotifier {
_requestPreviewUpdate();
}

/// Switching the Frame Rate pass on with a video selected starts it at
/// about twice the rate it receives (task #139), when its target would
/// otherwise convert nothing visible (`startingFrameRateTarget`). The
/// cached panel parameters are rebuilt so the dropdown shows the new target
/// and a later panel edit doesn't write the old one back.
void _startFrameRateFromSource() {
final info = videoInfo;
if (info == null || info.frameRate <= 0) return;
final input = frameRateInputFps(_processingPipeline.deinterlace,
_processingPipeline.unblend, info.frameRate,
softTelecineDecimates: info.softTelecineDecimates);
final started =
startingFrameRateTarget(_processingPipeline.frameRate, input);
if (started == null) return;
_processingPipeline = _processingPipeline.copyWith(frameRate: started);
_dynamicParams['frame_rate'] = ParameterConverter.fromFrameRate(started);
}

/// Convert PassType to filter ID.
String _passTypeToFilterId(PassType pass) {
switch (pass) {
Expand Down
7 changes: 5 additions & 2 deletions app/lib/views/pass_settings/pass_settings_inline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class PassSettingsInline extends StatelessWidget {
/// in the advisory (not error) style, because every combination it comments on
/// still renders. See [adviceFor].
Widget _buildInteractionAdvice(BuildContext context, MainViewModel viewModel) {
final message = adviceFor(passType, viewModel.processingPipeline);
final message = adviceFor(passType, viewModel.processingPipeline,
info: viewModel.videoInfo);
if (message == null) return const SizedBox.shrink();
return WarningBanner(message: message);
}
Expand All @@ -198,7 +199,9 @@ class PassSettingsInline extends StatelessWidget {
final pipeline = viewModel.processingPipeline;
final message = unblendTargetProblem(
pipeline.unblend.targetFps,
unblendInputFps(pipeline.deinterlace, fps),
unblendInputFps(pipeline.deinterlace, fps,
softTelecineDecimates:
viewModel.videoInfo!.softTelecineDecimates),
);
if (message == null) return const SizedBox.shrink();
return WarningBanner(message: message);
Expand Down
Loading