Reference
Codec State Machine
Look up syntax, contracts, layouts, algorithms, and exact behavior.
A conforming implementation can be reconstructed as two lockstep state machines. The encoder and decoder both maintain a context buffer, a predictor handle, a unit index, and stream cursors. The encoder additionally observes the source unit at each index; the decoder additionally observes the next decision bit and residual cursor.
state E = {
context: unit[]
index: number
predictor: Predictor
decisions: bit[]
residuals: unit[]
}
for source[index]:
prediction = predictor.argmax(context)
if prediction == source[index]:
decisions.push(1)
else:
decisions.push(0)
residuals.push(source[index])
context.push(source[index])
index += 1state D = {
context: seed
index: number
predictor: Predictor
decisionCursor: number
residualCursor: number
}
while output.length < originalLength:
prediction = predictor.argmax(context)
if decisions[decisionCursor] == 1:
unit = prediction
else:
unit = residuals[residualCursor]
residualCursor += 1
output.push(unit)
context.push(unit)
decisionCursor += 1