DrKLO/Telegram · error

Sample counts do not match (%lu!=%lu).\n

Error message

Sample counts do not match (%lu!=%lu).\n

What it means

The two input files have mismatched sample counts. After reading both, opus_compare asserts xlength == ylength*downsample. file1 is always read as stereo (2 channels) internally then optionally mono-mixed; file2 is read at nchannels. When the -r rate2 downsampling is active, file1 (48000-equivalent) must be 'downsample' times longer than file2.

Source

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

    fprintf(stderr,"Error opening '%s'.\n",_argv[1]);
    return EXIT_FAILURE;
  }
  fin2=fopen(_argv[2],"rb");
  if(fin2==NULL){
    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,

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Ensure both files derive from the same audio source and have lengths consistent with the chosen rate (file1 length == file2 length * downsample).
  2. Verify the -r value matches the actual rate of file2.
  3. Trim or pad the shorter file so the counts satisfy the ratio.
  4. Re-decode file2 from the same source used for file1.

Example fix

// before: file2 was decoded at 16000 but -r omitted (assumed 48000)
./opus_compare ref48000.sw dec16000.sw
# => Sample counts do not match (300000!=100000).

// after: tell opus_compare the second file is 16000
./opus_compare -r 16000 ref48000.sw dec16000.sw
Defensive patterns

Strategy: validation

Validate before calling

// Verify sample-count ratio before the expensive analysis.
if (xlength != ylength*downsample) {
    fprintf(stderr, "Sample counts do not match (%lu!=%lu).\n",
            (unsigned long)xlength, (unsigned long)ylength*downsample);
    return EXIT_FAILURE;
}

Prevention

When it happens

Trigger: xlength != ylength*downsample. With default 48000 (downsample=1), both files must have identical sample counts. With -r 8000 (downsample=6), file1 must have 6x the samples of file2. Mismatch means the files weren't produced from the same source at the expected rates.

Common situations: Comparing a reference file against a decode that has a different length (trimming/padding at encode time); wrong -r rate making the expected ratio wrong; comparing files from different source material; off-by-one frame rounding at decode boundaries.

Related errors


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