DrKLO/Telegram · error

Insufficient sample data (%lu<%i).\n

Error message

Insufficient sample data (%lu<%i).\n

What it means

opus_compare is a quality-assessment tool that compares a 48 kHz stereo reference PCM16 file against a degraded file at a possibly lower sample rate. After reading both files, it checks that the reference has at least TEST_WIN_SIZE (480) samples. With fewer samples than the analysis window, the per-band spectral energy computation in band_energy() cannot produce even one analysis frame, so the tool aborts with EXIT_FAILURE. The 480-sample floor corresponds to 10 ms of audio at 48 kHz.

Source

Thrown at TMessagesProj/jni/opus/src/opus_compare.c:244

    fprintf(stderr,"Error opening '%s'.\n",_argv[2]);
    fclose(fin1);
    return EXIT_FAILURE;
  }
  /*Read in the data and allocate scratch space.*/
  xlength=read_pcm16(&x,fin1,2);
  if(nchannels==1){
    for(xi=0;xi<xlength;xi++)x[xi]=.5*(x[2*xi]+x[2*xi+1]);
  }
  fclose(fin1);
  ylength=read_pcm16(&y,fin2,nchannels);
  fclose(fin2);
  if(xlength!=ylength*downsample){
    fprintf(stderr,"Sample counts do not match (%lu!=%lu).\n",
     (unsigned long)xlength,(unsigned long)ylength*downsample);
    return EXIT_FAILURE;
  }
  if(xlength<TEST_WIN_SIZE){
    fprintf(stderr,"Insufficient sample data (%lu<%i).\n",
     (unsigned long)xlength,TEST_WIN_SIZE);
    return EXIT_FAILURE;
  }
  nframes=(xlength-TEST_WIN_SIZE+TEST_WIN_STEP)/TEST_WIN_STEP;
  xb=(float *)opus_malloc(nframes*NBANDS*nchannels*sizeof(*xb));
  X=(float *)opus_malloc(nframes*NFREQS*nchannels*sizeof(*X));
  Y=(float *)opus_malloc(nframes*yfreqs*nchannels*sizeof(*Y));
  /*Compute the per-band spectral energy of the original signal
     and the error.*/
  band_energy(xb,X,BANDS,NBANDS,x,nchannels,nframes,
   TEST_WIN_SIZE,TEST_WIN_STEP,1);
  free(x);
  band_energy(NULL,Y,BANDS,ybands,y,nchannels,nframes,
   TEST_WIN_SIZE/downsample,TEST_WIN_STEP/downsample,downsample);
  free(y);
  for(xi=0;xi<nframes;xi++){
    /*Frequency masking (low to high): 10 dB/Bark slope.*/
    for(bi=1;bi<NBANDS;bi++){

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Ensure the reference input is raw PCM16 (signed 16-bit little-endian, no header) with at least 480 stereo frames (~1920 bytes).
  2. Verify file1 is the 48 kHz reference and file2 is the degraded output; swap them if reversed.
  3. If the degraded file is at a lower sample rate, pass -r <rate> so downsample is computed correctly and the sample-count check (xlength == ylength*downsample) holds.
  4. Generate test vectors with opus_demo at sufficient duration (at least 1 second of audio is safe).

Example fix

// before: short or wrong-format reference
opus_compare ref_short.sw degraded.sw
// after: use a properly-sized raw PCM16 reference
opus_compare ref_48k_stereo.sw degraded.sw
// verify minimum size before running:
//   ref must be >= 480 * 2 channels * 2 bytes = 1920 bytes
Defensive patterns

Strategy: validation

Validate before calling

// Before running opus_compare, verify file size meets minimum
// 480 stereo frames * 2 channels * 2 bytes = 1920 bytes minimum
#include <sys/stat.h>
int check_min_samples(const char *path) {
    struct stat st;
    if (stat(path, &st) != 0) return -1;
    size_t min_bytes = 480 * 2 * 2; // TEST_WIN_SIZE * 2ch * sizeof(int16)
    return (st.st_size >= (off_t)min_bytes) ? 0 : -1;
}

Prevention

When it happens

Trigger: The reference file (file1) contains fewer than 480 stereo sample-pairs after read_pcm16 returns. This happens when the raw PCM16 input is truncated, shorter than ~1920 bytes of stereo data, or in a format read_pcm16 does not expect (it reads raw little-endian int16, not WAV/OGUS containers). A corrupted file that causes fread to return 0 early in read_pcm16 also produces a short xlength.

Common situations: Using very short audio clips as Opus regression test vectors; generating the reference file with a different tool that writes a WAV header (opus_compare expects headerless raw PCM); file truncation during network copy or incomplete encode; accidentally passing the degraded file as file1 when it is at a much lower sample rate without the -r flag.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/7ee0c47b61f59d77. Report an issue: GitHub.