How Samsung SSC frames work
SSC turned out to be CELT 1.1 (from Opus) with fixed parameters and a handful of Samsung changes, most notably a 24-bit range coder. Follow one 864-sample frame through the decoder.
Code shown from samsung-ssc-decoder@b942531. Every excerpt links to its exact lines.
1 · The 4-byte header
Every frame starts with FF Ex, a sequence counter and a bitrate code (0xF0–0xF6 or 0xE0–0xE6). There is no length field on the wire; the receiver derives everything from these bytes and the A2DP configuration.
int ssc_parse_frame_info(const uint8_t *f, int len, ssc_frame_info *h) {
static const int frate[7] = {88000, 96000, 128000, 192000, 229000, 328000, 256000};
static const int erate[7] = {291000, 308000, 442000, 584000, 886000, 250000, 152000};
int code;
if (len < 6 || f[0] != 0xFF || (f[1] & 0xF0) != 0xE0) return -1;
code = f[3] & 0x1F;
if (code >= 0x10 && code <= 0x16) {
h->bitrate = frate[code - 0x10];
h->hq = (code == 0x15 || code == 0x16);
h->seq = f[2] >> 4;
h->end_band = NB; /* bits 2-3 of byte 2 are side information only */
} else if (code <= 0x06) {
h->bitrate = erate[code];
h->hq = 1;
h->seq = ((f[1] & 0xF) << 4) | (f[2] >> 4);
h->end_band = (f[3] == 0xE6) ? 18 : NB - ((f[2] >> 2) & 3);
} else {
return -1;
}
return 0;
}
2 · Frame size from the bitrate
Because the size isn’t transmitted, it’s computed: bitrate × 864 / (Fs × 8) with Fs = 44100 or 96000, plus 3/128 extra for the non-“hq” rates, forced odd, plus the header. 44.1 and 48 kHz share a mode, so the sample rate never appears in the frame.
int ssc_frame_bytes(const uint8_t *frame, int avail, int sample_rate) {
ssc_frame_info h;
int fs, base;
if (avail < 6 || ssc_parse_frame_info(frame, avail, &h)) return 0;
fs = sample_rate == 96000 ? 96000 : 44100; /* 48 kHz streams use the 44.1 kHz mode */
/* Same formula as the encoder: base = bitrate*864/(Fs*8), +3/128 for non-hq rates, forced odd;
* the range-coded payload is base-1 bytes after the 4-byte header. */
base = (int)(((int64_t)h.bitrate * N) / ((int64_t)fs * 8));
if (!h.hq) base += base * 3 >> 7;
base |= 1;
return base - 1 + 4;
}
/* lrintf() in the default rounding mode (nearest, ties to even) for |v| < 2^22, without a libm call (lrintf
* is a library call on the ESP32). Adding 1.5 * 2^23 rounds v to an integer in the low mantissa bits. */3 · Bit budget, postfilter, transient, coarse energy
The range decoder gets the whole payload, but the bit budget is clamped to 1275 bytes, a Samsung quirk the encoder shares. Then come the pitch postfilter parameters (SSC drops CELT’s tapset symbol), the transient flag and the coarse band energies.
ssc_decoder/ssc_decoder.c:150–201 ↗static int decode_frame(ssc_decoder *st, const uint8_t *frame, int len) {
float *X = ssc_postfilter_buffer(st->pf[0]), *Y = ssc_postfilter_buffer(st->pf[1]);
const short *eBands = st->am->eBands;
ssc_frame_info h;
ec_dec dec;
int payload, i, c, end;
int32_t total_bits, tell, total_bits8, total_boost, bits, balance;
int silence, isTransient, shortBlocks, intra, spread, alloc_trim;
int pf_on = 0, pf_T = 0, pf_qg = 0;
int tf_res[NB], offsets[NB], fine_quant[NB], pulses[NB], fine_priority[NB];
int dynalloc_logp, codedBands, intensity = 0, dual_stereo = 0, anti_collapse_rsv, anti_collapse_on = 0;
float logE[C * NB];
PROF(0);
st->out_valid = 0;
if (ssc_parse_frame_info(frame, len, &h)) return -1;
end = h.end_band;
payload = len - 4;
total_bits = 8 * (payload < 1275 ? payload : 1275); /* encoder clamps the budget, not the buffer */
ec_dec_init(&dec, (unsigned char *)frame + 4, (opus_uint32)payload);
/* silence */
tell = ec_tell(&dec);
if (tell >= total_bits) silence = 1;
else if (tell == 1) silence = ec_dec_bit_logp(&dec, 15);
else silence = 0;
if (silence) {
tell = total_bits;
dec.nbits_total += total_bits - ec_tell(&dec);
}
/* postfilter (no tapset symbol in SSC) */
if (tell + 16 <= total_bits) {
if (ec_dec_bit_logp(&dec, 1)) {
int octave = (int)ec_dec_uint(&dec, 6);
pf_T = (16 << octave) + (int)ec_dec_bits(&dec, 4 + octave) - 1;
pf_qg = (int)ec_dec_bits(&dec, 3);
pf_on = 1;
}
tell = ec_tell(&dec);
}
/* transient */
isTransient = 0;
if (tell + 3 <= total_bits) isTransient = ec_dec_bit_logp(&dec, 3);
shortBlocks = isTransient ? M : 0;
/* coarse energy */
intra = (ec_tell(&dec) + 3 <= total_bits) ? ec_dec_bit_logp(&dec, 3) : 0;
ssc_unquant_coarse_energy(NB, 0, end, st->oldBandE, intra, &dec, C, total_bits);
PROF(1); /* header, postfilter params, coarse energy */
4 · TF, spreading and bit allocation
Time–frequency resolution (skipped for “hq” rates), the spreading decision, dynamic allocation boosts, the allocation trim, then CELT’s allocator splits the remaining bits across 22 bands, using Samsung’s own allocation tables.
ssc_decoder/ssc_decoder.c:202–241 ↗/* time-frequency resolution: not coded for hq rates. Budget is the full storage. */
if (!h.hq) {
uint32_t budget = dec.storage * 8, t = (uint32_t)ec_tell(&dec);
int logp = isTransient ? 2 : 4, curr = 0, tf_changed = 0, tf_select = 0;
int tf_select_rsv = (t + logp + 1 <= budget);
budget -= tf_select_rsv;
for (i = 0; i < NB; i++) {
if (t + logp <= budget) {
curr ^= ec_dec_bit_logp(&dec, logp);
t = (uint32_t)ec_tell(&dec);
tf_changed |= curr;
}
tf_res[i] = curr;
logp = isTransient ? 4 : 5;
}
if (tf_select_rsv && tf_select_table_LM3[4 * isTransient + 0 + tf_changed] !=
tf_select_table_LM3[4 * isTransient + 2 + tf_changed])
tf_select = ec_dec_bit_logp(&dec, 1);
for (i = 0; i < NB; i++) tf_res[i] = tf_select_table_LM3[4 * isTransient + 2 * tf_select + tf_res[i]];
} else {
for (i = 0; i < NB; i++) tf_res[i] = 0;
}
/* spreading */
spread = 2;
if (ec_tell(&dec) + 4 <= total_bits) spread = ec_dec_icdf(&dec, spread_icdf, 5);
/* dynamic allocation */
dynalloc_logp = 6;
total_bits8 = total_bits << 3;
total_boost = 0;
tell = (int32_t)ec_tell_frac(&dec);
for (i = 0; i < NB; i++) offsets[i] = 0;
for (i = 0; i < end; i++) {
int quanta = (eBands[i + 1] - eBands[i]) << 4;
int loop_logp = dynalloc_logp, boost = 0;
if (quanta < 48) quanta = 48;
while (tell + (loop_logp << 3) < total_bits8 - total_boost && boost < st->am->cap[i]) {
int flag = ec_dec_bit_logp(&dec, (unsigned)loop_logp);
tell = (int32_t)ec_tell_frac(&dec);5 · Fine energy and band shapes (PVQ)
Fine energy refines each band’s level, then pyramid vector quantization decodes the shape of each band: which bins carry the energy. This is the bulk of the bits, and it is stock Opus 1.1.
ssc_decoder/ssc_decoder.c:276–297 ↗/* The frame part of the postfilter buffers is free here: the previous frame is also in the history. */
memset(X, 0, sizeof(float) * N);
memset(Y, 0, sizeof(float) * N);
ssc_quant_all_bands_dec(&st->bm, end, X, Y, st->collapse_masks, pulses, shortBlocks, spread, dual_stereo,
intensity, tf_res, total_bits8 - anti_collapse_rsv, balance, &dec, codedBands,
&st->rng, &st->scratch.qab);
PROF(3); /* fine energy + band shapes (PVQ) */
if (anti_collapse_rsv > 0) anti_collapse_on = (int)ec_dec_bits(&dec, 1);
ssc_unquant_energy_finalise(NB, 0, end, st->oldBandE, fine_quant, fine_priority, total_bits - ec_tell(&dec),
&dec, C);
for (i = 0; i < C * NB; i++) logE[i] = st->oldBandE[i] * (1.f / (1 << DB_SHIFT));
if (anti_collapse_on)
ssc_anti_collapse(&st->bm, X, Y, st->collapse_masks, 0, end, logE, st->oldLogE, st->oldLogE2, pulses,
st->rng);
PROF(4); /* finalise + anti-collapse */
for (c = 0; c < C; c++) {
float *freq = c ? Y : X; /* denormalised in place, then replaced by the time signal */6 · Denormalise, IMDCT, overlap-add, postfilter
Shapes are scaled back up by their band energies, run through the IMDCT with overlap-add, and the pitch postfilter restores harmonic detail. Bands above the coded end are set to −28, where CELT would use 0.
ssc_decoder/ssc_decoder.c:296–317 ↗for (c = 0; c < C; c++) {
float *freq = c ? Y : X; /* denormalised in place, then replaced by the time signal */
if (silence)
memset(freq, 0, sizeof(float) * N);
else
ssc_denormalise_bands(eBands, freq, freq, logE + c * NB, 0, end);
PROF(5); /* denormalise */
ssc_synthesis_frame(st->scratch.synth, freq, shortBlocks, st->ola[c]);
PROF(6); /* IMDCT + overlap-add */
ssc_postfilter_run(st->pf[c], pf_on, pf_T, pf_qg);
PROF(7); /* postfilter */
}
/* Samsung: bands above the coded end get -28 (CELT uses 0) */
if (silence)
for (i = 0; i < C * NB; i++) st->oldBandE[i] = NEG28;
for (c = 0; c < C; c++)
for (i = end; i < NB; i++) st->oldBandE[c * NB + i] = NEG28;
/* anti-collapse energy history (as opus 1.1) */
for (i = 0; i < C * NB; i++) logE[i] = st->oldBandE[i] * (1.f / (1 << DB_SHIFT));
if (!isTransient) {