Skip to content

How it works

trueflac reads the decoded samples of a file and looks for two traces that a lossy encoder leaves behind, in this order:

  1. Cut highs, on the average spectrum. Quick, and enough for most MP3 and AAC files.
  2. Codec grid, when the highs tell nothing: the block grid of an MP3, AAC or Vorbis encoder.

The first clue found makes the file suspect; the search stops there.

An encoder spends its bits where the ear hears them. Above a low-pass frequency that depends on its settings (about 16 kHz for an MP3 at 128 kbit/s, 19 to 20 kHz at 256 or 320 kbit/s), it throws the sound away. Decoded, the file holds nothing there but the rounding noise of its bit depth.

trueflac averages the spectrum of eight segments of four seconds spread over the track (an FFT of 8192 samples, silent passages left out). Two shapes make a cut:

  • A cliff: the level falls by at least 30 dB within 600 Hz, and the spectrum stays flat above that point.
  • A dead band: the highs sit exactly on the rounding floor of the file’s bit depth (within 3 dB). A recording, even a 78 rpm transfer or a quiet piano, keeps a little noise of its own above that floor. When a decoder adds dither, the floor rises by about 5 dB; trueflac then allows 6.5 dB, but only after a fall of at least 6 dB over the last 500 Hz. Genuine files never fall more than 1.5 dB there.

A cut is suspect below 20.3 kHz (92 % of the 22.05 kHz limit of a CD). Some CD masters are filtered at 20.8 to 21 kHz: those stay ok. The search stops at 22.05 kHz, so a lossy file resampled to 96 kHz is judged on its audible band, not on the images of the resampler.

Some encoders keep the highs: MP3 V0 and 320 kbit/s, AAC at 256 kbit/s, Vorbis. Their trace is elsewhere.

An encoder cuts the sound into blocks, moves each block to frequencies with a fixed transform, and quantizes the coefficients: many of them, judged inaudible, become exactly zero. The decoder runs the inverse transform. So, when the decoded file is analyzed again with the same transform, at the same block start, those zeros come back. At any other start they are smeared over all the coefficients. A recording has no preferred start: every start looks the same.

This follows the idea of the “inverse decoder” of Herre and Schug, and of the detectors of Derrien and his co-authors (see References). trueflac applies it this way:

  • Every start is tried: the 576 starts of an MP3 granule, the 1024 starts of an AAC or Vorbis block. For each, trueflac counts the coefficients under several limits of “zero” (the rounding floor of the file, then 2⁻¹⁹, 2⁻¹⁷ and 2⁻¹⁵ for AAC and Vorbis, 2⁻¹⁸ to 2⁻¹³ for MP3: a 24-bit or dithered decode does not land exactly on the floor).
  • The contrast is how far the best start stands above all the others, in robust deviations (median and median absolute deviation). Genuine tracks have stayed at 6 or below; a grid is suspect from 10.
  • Two places must agree. The measure is taken at 35 % and 65 % of the track, separately. A grid counts only when both places find the same start (to 128 samples for Vorbis, whose short blocks shift its grid). A chance peak lands anywhere; a codec grid stays put.
  • MP3 (MPEG-1 Layer III) codes each granule of 576 lines through two stages: a polyphase filter bank of 32 bands, then an 18-point MDCT in each band, with alias butterflies between them. trueflac rebuilds the 576 line shapes from the standard (the polyphase window, the MDCT with its sine window, the butterflies and the frequency inversion of odd bands) and projects the track on them. The shapes were checked against the minimp3 decoder: they match to 2 × 10⁻⁷.
  • AAC and Vorbis use a 2048-point MDCT, moved by 1024 samples. trueflac computes it at every start through a quarter-length FFT, with the three windows their encoders use: the sine window (which fits Apple’s AAC encoder best on the bench), the Kaiser-Bessel derived window with alpha 4 (ffmpeg’s AAC encoder), and the Vorbis power window. The window that fits best also names the codec.

MP3 is tried first; AAC and Vorbis only when MP3 is not suspect.

On an Apple Silicon Mac, about 1.5 s for a genuine track, the longest case since every test runs, and less for a suspect one. The grid search works on short excerpts (16 blocks or 12 granules at each of the two places), whatever the length of the track.

  • Counting holes in a short-time spectrum: the window leaks energy into the zeros, and a genuine 78 rpm transfer showed as many holes as most fakes.
  • Highs folding to mono (the side channel vanishing in the highs, as joint stereo does): it did not separate genuine files from fakes on the bench.
  • Confirming only the best grid candidate at the second place: faster, but it lost 8 of about 370 detections on the bench. trueflac confirms the best three.
Built with Kiln