DrKLO/Telegram · error

failed to create a mode\n

Error message

failed to create a mode\n

What it means

opus_custom_mode_create(rate, frame_size, NULL) returned NULL — Opus could not build a custom mode for the given sample rate and frame size. The custom mode is the prerequisite for both encoder and decoder creation, so nothing else can proceed.

Solutions

  1. Use a rate/frame_size pair known valid for CELT custom mode (e.g. 48000 with frame_size in {120, 240, 480, 960, 1920, 2880}).
  2. Check that the build defines CUSTOM_MODES; without it opus_custom_mode_create always returns NULL.
  3. Validate argv values are positive integers before calling atol.
  4. Inspect the error* out-param (here passed as NULL) — pass a non-NULL int* to get the specific OPUS_BAD_ARG / allocation code.

Example fix

// before
mode = opus_custom_mode_create(rate, frame_size, NULL);
if (mode == NULL) { fprintf(stderr, "failed to create a mode\n"); return 1; }

// after: capture the error code
int mode_err = 0;
mode = opus_custom_mode_create(rate, frame_size, &mode_err);
if (mode == NULL) { fprintf(stderr, "failed to create a mode: %s\n", opus_strerror(mode_err)); return 1; }
Defensive patterns

Strategy: validation

Validate before calling

// Pass a real error out-param and validate rate/frame_size first.
if (rate <= 0 || frame_size <= 0) { fprintf(stderr, "rate and frame_size must be positive\n"); return 1; }
int mode_err = OPUS_OK;
mode = opus_custom_mode_create(rate, frame_size, &mode_err);
if (mode == NULL) { fprintf(stderr, "failed to create a mode: %s\n", opus_strerror(mode_err)); return 1; }

Prevention

When it happens

Trigger: Passing an invalid rate or frame_size combination. opus_custom_mode_create requires specific constraints: rate typically 8000–48000, frame_size must fit the mode's internal CELT layout (e.g. multiples of the MDCT window / 2.5ms sub-frames), and the product must be representable. NULL is also returned on internal allocation failure.

Common situations: Using a frame size not supported by the custom CELT mode (e.g. non-multiple of 120 at 48kHz); an unsupported rate like 22050; passing 0 or negative values via atol on a malformed CLI arg; opus built without CUSTOM_MODES so the API rejects custom configs.

Related errors


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

Appendix: source

Thrown at TMessagesProj/jni/opus/celt/opus_custom_demo.c:76

#endif
   int count = 0;
   opus_int32 skip;
   opus_int16 *in, *out;
   if (argc != 9 && argc != 8 && argc != 7)
   {
      fprintf (stderr, "Usage: test_opus_custom <rate> <channels> <frame size> "
               " <bytes per packet> [<complexity> [packet loss rate]] "
               "<input> <output>\n");
      return 1;
   }

   rate = (opus_int32)atol(argv[1]);
   channels = atoi(argv[2]);
   frame_size = atoi(argv[3]);
   mode = opus_custom_mode_create(rate, frame_size, NULL);
   if (mode == NULL)
   {
      fprintf(stderr, "failed to create a mode\n");
      return 1;
   }

   bytes_per_packet = atoi(argv[4]);
   if (bytes_per_packet < 0 || bytes_per_packet > MAX_PACKET)
   {
      fprintf (stderr, "bytes per packet must be between 0 and %d\n",
                        MAX_PACKET);
      return 1;
   }

   inFile = argv[argc-2];
   fin = fopen(inFile, "rb");
   if (!fin)
   {
      fprintf (stderr, "Could not open input file %s\n", argv[argc-2]);
      return 1;
   }

View on GitHub (pinned to 45ab8f4308)