Opus audio, opus_decode() always fills output buffer with zeros

168 Views Asked by At

I slightly modified the opus provide sample file "trivial_example.c" I modified it so instead of loading a pcm file from disk, I just create some noise using random numbers. Maybe that is the problem, but I dont see how.

I am using Visual Studio 2019. Windows SDK 10. Windows7 x64.

none of the opus functions return an error.

opus_encode() seems to work. It fills cbits, as expected. But opus_decode() only fills the output buffer with zeros.

This is opus provided sample code, but it doesnt work. Is there some computer setup I needed to do before using opus.

Ive been trying to get opus to work for days. Please help. Thank you.

#define FRAME_SIZE 960
#define SAMPLE_RATE 48000
#define CHANNELS 2
#define APPLICATION OPUS_APPLICATION_AUDIO
#define BITRATE 64000

#define MAX_FRAME_SIZE 6*960
#define MAX_PACKET_SIZE (3*1276)

int main(int argc, char **argv)
{
   char *inFile;
   FILE *fin;
   char *outFile;
   FILE *fout;
   opus_int16 in[FRAME_SIZE*CHANNELS];
   opus_int16 out[MAX_FRAME_SIZE*CHANNELS];
   unsigned char cbits[MAX_PACKET_SIZE];
   int nbBytes;
   /*Holds the state of the encoder and decoder */
   OpusEncoder *encoder;
   OpusDecoder *decoder;
   int err;


   /*Create a new encoder state */
   encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err);
   if (err<0)
   {
      fprintf(stderr, "failed to create an encoder: %s\n", opus_strerror(err));
      return EXIT_FAILURE;
   }
   /* Set the desired bit-rate. You can also set other parameters if needed.
      The Opus library is designed to have good defaults, so only set
      parameters you know you need. Doing otherwise is likely to result
      in worse quality, but better. */
   err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
   if (err<0)
   {
      fprintf(stderr, "failed to set bitrate: %s\n", opus_strerror(err));
      return EXIT_FAILURE;
   }


   /* Create a new decoder state. */
   decoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &err);
   if (err<0)
   {
      fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err));
      return EXIT_FAILURE;
   }

    ////////////////////////////////////////////////////////////////////// create audio noise
    #define RandZeroToOne    ((float)rand() / RAND_MAX)
    #define RandNegOneToOne (((float)rand() / RAND_MAX)*2.0-1.0)
    opus_int16* pcmdata = (opus_int16*)calloc(65536, sizeof(opus_int16));
    opus_int16* pDstData = (opus_int16*)calloc(65536, sizeof(opus_int16));
    for(short i=0;i<6553;i++)
    {
        pcmdata[i] = (opus_int16)(RandZeroToOne*32767.0);
        if((i&0x1)==0)
            pcmdata[i] = -pcmdata[i];
        int T=0;
    }
    //////////////////////////////////////////////////////////////////////

   //while (1)
   {
      int i;
      int frame_size;

      /* Convert from little-endian ordering. */
      for (i=0;i<CHANNELS*FRAME_SIZE;i++)
         in[i]=pcmdata[2*i+1]<<8|pcmdata[2*i];

      /* Encode the frame. */
      nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);;//<---------------
      if (nbBytes<0)
      {
         fprintf(stderr, "encode failed: %s\n", opus_strerror(nbBytes));
         return EXIT_FAILURE;
      }

      /* Decode the data. In this example, frame_size will be constant because
         the encoder is using a constant frame size. However, that may not
         be the case for all encoders, so the decoder must always check
         the frame size returned. */
      opus_decoder_ctl(decoder, OPUS_RESET_STATE);
      frame_size = opus_decode(decoder, cbits, nbBytes, out, MAX_FRAME_SIZE, 0);//<---------------
      if (frame_size<0)
      {
         fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));
         return EXIT_FAILURE;
      }

      /* Convert to little-endian ordering. */
      for(i=0;i<CHANNELS*frame_size;i++)
      {
         pcmdata[2*i]=out[i]&0xFF;
         pcmdata[2*i+1]=(out[i]>>8)&0xFF;
      }
   }
   opus_encoder_destroy(encoder);
   opus_decoder_destroy(decoder);
   return EXIT_SUCCESS;
}
0

There are 0 best solutions below