user1107819
user1107819

Reputation: 51

How to confirm opus encode buffer size?

Function opus_encode need frame size as parameter. in api doc it says buffer size is number of samples per channel. But how to determine which size should i use?

I use opus in android. sample rate 16k, buffer size 1280. when i set frame size to 640 in encode and decode, the length of decoded file is half of raw pcm. when i set to 960, decoded file is 2/3 of raw pcm. but set to 1280, encode will return -1 as arg error.

When i use cool edit to play decoded, it is faster than raw pcm.

there must be something about my parameters. Is anyone using opus can help me. Thanks a lot.

Upvotes: 3

Views: 7358

Answers (1)

Dmitry Poroh
Dmitry Poroh

Reputation: 3825

Opus encode definition:

opus_int32 opus_encode  ( OpusEncoder *         st,
                          const opus_int16 *    pcm,
                          int                   frame_size,
                          unsigned char *       data,
                          opus_int32            max_data_bytes )

When you specify frame_size you need to set it to number of samples per one channel available in pcm buffer.

OPUS codec supports stereo and mono signals and corresponding configuration of encoder is channels parameter that you specify when you call opus_encoder_create function.

Also you need to know about supported frame sizes by OPUS codec. It supports frames with: 2.5, 5, 10, 20, 40 or 60 ms of audio data.

One millisecond of audio with 16kHz is 16 samples (16000/1000). So for mono you can specify frame_size set to:

  • 16 * 2.5 = 40 (very rare)
  • 16 * 5 = 80 (rare)
  • 16 * 10 = 160
  • 16 * 20 = 320
  • 16 * 40 = 640
  • 16 * 60 = 960

OPUS codec will not accept another sizes. The best way to deal with buffer size of 1280 samples is to split on four 20ms packets or two 40ms packets.

So you encode two or four packets from one buffer received from buffer.

Upvotes: 10

Related Questions