Skip to content

Commit a0cd358

Browse files
authored
Fix Logging with TQ and Remove Repetitions (#1118)
``` TQ-Probes: [(17.25, 80.16), (17.50, 79.98), (35.00, 71.96)] Final Q=17.25, Final Score=80.16 ``` In the above example, it actually uses 17.50 = 79.98 (because it's closer) But the system logs the latest probe instead which is wrong. This happens when the TQ range is very narrow.
1 parent 167842f commit a0cd358

File tree

1 file changed

+35
-57
lines changed

1 file changed

+35
-57
lines changed

av1an-core/src/target_quality.rs

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ impl TargetQuality {
166166
let mut lower_quantizer_limit = self.min_q as f32;
167167
let mut upper_quantizer_limit = self.max_q as f32;
168168

169+
let skip_reason;
170+
169171
loop {
170172
let next_quantizer = predict_quantizer(
171173
lower_quantizer_limit,
@@ -183,26 +185,12 @@ impl TargetQuality {
183185
step,
184186
)?;
185187

186-
if let Some((quantizer, score)) = quantizer_score_history
188+
if quantizer_score_history
187189
.iter()
188-
.find(|(quantizer, _)| *quantizer == next_quantizer)
190+
.any(|(quantizer, _)| *quantizer == next_quantizer)
189191
{
190192
// Predicted quantizer has already been probed
191-
log_probes(
192-
&quantizer_score_history,
193-
self.metric,
194-
target,
195-
chunk.frames() as u32,
196-
self.probing_rate as u32,
197-
self.video_params.as_ref(),
198-
&chunk.name(),
199-
*quantizer,
200-
match self.metric {
201-
TargetMetric::ButteraugliINF | TargetMetric::Butteraugli3 => -score,
202-
_ => *score,
203-
},
204-
SkipProbingReason::None,
205-
);
193+
skip_reason = SkipProbingReason::None;
206194
break;
207195
}
208196

@@ -228,25 +216,11 @@ impl TargetQuality {
228216
quantizer_score_history.push((next_quantizer, score));
229217

230218
if score_within_range || quantizer_score_history.len() >= self.probes as usize {
231-
log_probes(
232-
&quantizer_score_history,
233-
self.metric,
234-
target,
235-
chunk.frames() as u32,
236-
self.probing_rate as u32,
237-
self.video_params.as_ref(),
238-
&chunk.name(),
239-
next_quantizer,
240-
match self.metric {
241-
TargetMetric::ButteraugliINF | TargetMetric::Butteraugli3 => -score,
242-
_ => score,
243-
},
244-
if score_within_range {
245-
SkipProbingReason::WithinTolerance
246-
} else {
247-
SkipProbingReason::ProbeLimitReached
248-
},
249-
);
219+
skip_reason = if score_within_range {
220+
SkipProbingReason::WithinTolerance
221+
} else {
222+
SkipProbingReason::ProbeLimitReached
223+
};
250224
break;
251225
}
252226

@@ -263,29 +237,16 @@ impl TargetQuality {
263237

264238
// Ensure quantizer limits are valid
265239
if lower_quantizer_limit > upper_quantizer_limit {
266-
log_probes(
267-
&quantizer_score_history,
268-
self.metric,
269-
target,
270-
chunk.frames() as u32,
271-
self.probing_rate as u32,
272-
self.video_params.as_ref(),
273-
&chunk.name(),
274-
next_quantizer,
275-
match self.metric {
276-
TargetMetric::ButteraugliINF | TargetMetric::Butteraugli3 => -score,
277-
_ => score,
278-
},
279-
if score > target_range.1 {
280-
SkipProbingReason::QuantizerTooHigh
281-
} else {
282-
SkipProbingReason::QuantizerTooLow
283-
},
284-
);
240+
skip_reason = if score > target_range.1 {
241+
SkipProbingReason::QuantizerTooHigh
242+
} else {
243+
SkipProbingReason::QuantizerTooLow
244+
};
285245
break;
286246
}
287247
}
288248

249+
// Calculate final quantizer and score BEFORE logging
289250
let final_quantizer_score = quantizer_score_history
290251
.iter()
291252
.filter(|(_, score)| {
@@ -326,8 +287,25 @@ impl TargetQuality {
326287
|highest_quantizer_score_within_range| highest_quantizer_score_within_range,
327288
);
328289

329-
// Note: if the score is to be returned in the future, ensure to invert it back
330-
// if metric is inverse (eg. Butteraugli)
290+
log_probes(
291+
&quantizer_score_history,
292+
self.metric,
293+
target,
294+
chunk.frames() as u32,
295+
self.probing_rate as u32,
296+
self.video_params.as_ref(),
297+
&chunk.name(),
298+
final_quantizer_score.0,
299+
// Inverse reverse metrics
300+
match self.metric {
301+
TargetMetric::ButteraugliINF | TargetMetric::Butteraugli3 => {
302+
-final_quantizer_score.1
303+
},
304+
_ => final_quantizer_score.1,
305+
},
306+
skip_reason,
307+
);
308+
331309
Ok(final_quantizer_score.0)
332310
}
333311

0 commit comments

Comments
 (0)