{"record":{"id":"19e4e2b9f4a4fefb","repo":"DrKLO/Telegram","slug":"opus-custom-encode-failed-s-n","errorCode":null,"errorMessage":"opus_custom_encode() failed: %s\\n","messagePattern":"opus_custom_encode\\(\\) failed: (.+?)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"TMessagesProj/jni/opus/celt/opus_custom_demo.c","lineNumber":139,"sourceCode":"\n   if (argc>7)\n   {\n      complexity=atoi(argv[5]);\n      opus_custom_encoder_ctl(enc,OPUS_SET_COMPLEXITY(complexity));\n   }\n\n   in = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));\n   out = (opus_int16*)malloc(frame_size*channels*sizeof(opus_int16));\n\n   while (!feof(fin))\n   {\n      int ret;\n      err = fread(in, sizeof(short), frame_size*channels, fin);\n      if (feof(fin))\n         break;\n      len = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);\n      if (len <= 0)\n         fprintf (stderr, \"opus_custom_encode() failed: %s\\n\", opus_strerror(len));\n\n      /* This is for simulating bit errors */\n#if 0\n      int errors = 0;\n      int eid = 0;\n      /* This simulates random bit error */\n      for (i=0;i<len*8;i++)\n      {\n         if (rand()%atoi(argv[8])==0)\n         {\n            if (i<64)\n            {\n               errors++;\n               eid = i;\n            }\n            data[i/8] ^= 1<<(7-(i%8));\n         }\n      }","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/opus/celt/opus_custom_demo.c#L121-L157","documentation":"opus_custom_encode(enc, in, frame_size, data, bytes_per_packet) returned len <= 0 during the encoding loop. The message prints opus_strerror(len). Critically, this is a WARNING only — the code prints the error but does NOT break the loop or skip the subsequent decode, so it feeds a garbage/zero len to the decoder on failure.","triggerScenarios":"The encoder returns a negative OPUS_* error code (e.g. OPUS_BAD_ARG if frame_size is invalid for the mode, OPUS_INTERNAL_ERROR) or 0. The input buffer 'in' is filled by fread each iteration; an incomplete final read (short frame) can also feed bad data.","commonSituations":"frame_size not a valid CELT frame size for the mode; input PCM shorter than a full frame on the last iteration (fread returns fewer samples but the code only checks feof after the read); bytes_per_packet of 0 forcing zero-length output; corrupted input samples.","solutions":["Check fread's return count and break/skip the iteration if it's less than frame_size*channels (partial frame).","On len <= 0, skip the decode step (continue) instead of feeding the bad length to the decoder.","Validate frame_size is an allowed CELT size for the chosen mode/rate before the loop.","Read opus_strerror(len) to distinguish OPUS_BAD_ARG from OPUS_BUFFER_TOO_SMALL."],"exampleFix":"// before\nlen = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);\nif (len <= 0)\n    fprintf (stderr, \"opus_custom_encode() failed: %s\\n\", opus_strerror(len));\n\n// after: skip the rest of the loop body on encode failure\nlen = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);\nif (len <= 0) {\n    fprintf (stderr, \"opus_custom_encode() failed: %s\\n\", opus_strerror(len));\n    continue;\n}","handlingStrategy":"validation","validationCode":"// Skip frames where the read was short or encode failed.\nerr = fread(in, sizeof(short), frame_size*channels, fin);\nif (err != (size_t)(frame_size*channels)) { /* short read: end of data */ break; }\nlen = opus_custom_encode(enc, in, frame_size, data, bytes_per_packet);\nif (len <= 0) { fprintf(stderr, \"opus_custom_encode() failed: %s\\n\", opus_strerror(len)); continue; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check fread's count and skip partial frames rather than feeding them to the encoder.","On len <= 0, continue the loop so decode isn't called with a bad length.","Validate frame_size is an allowed CELT size before the loop."],"tags":["opus","encoder","encode-loop","error-handling","audio","c"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}