# LOCO

*This page is originally based on a description written by User:Kostya.*

- FourCC: LOCO
- Samples: http://samples.mplayerhq.hu/V-codecs/LOCO/

LOCO codec written by Mohammad Rezaei and based on the JPEG-LS lossy/lossless compression scheme, originally known as LOCO-I.

More information on LOCO-I can be found on:

- FCD14495 (JPEG-LS final comittee draft)
- http://www.hpl.hp.com/loco/ (LOCO-I description)

## Decoding Process

- get predictor
- decode residue
- output predictor minus residue as new pixel

## Prediction

A pixel (X) can predict from up to 3 surrounding pixels: up (A), left (B), and up-left (C):

+---+---+ | C | A | +---+---+ | B | X | +---+---+

The prediction logic works as follows:

- A,B,C are unavailable (X is top left pixel) - use 128 as prediction value
- A and C are unavailable (X is in the top line) - use B as predictor
- B and C are unavailable (X is in the first column) - use A as predictor
- A,B,C are available (this one is the JPEG-LS prediction scheme):
- if C >= max(A,B) then use min(A,B) as predictor
- if C <= min(A,B) then use max(A,B) as predictor
- else use (A+B-C) as predictor

## Residue Decoding

Residue decoding is based on Rice codes. There are some differences though:

- Numbers are stored as their absolute value in Rice code and one trailing bit shows their sign - 0 for positive, 1 for negative integers
- Rice parameter is not static but updates every time

If there is lossy compression mode, then add loss value:

if(val > 0)val += loss if(val < 0)val -= loss

## Rice Code Decoding

- Get standard Rice code with given parameter
- It new code is zero
- If we have some saved bits:
- Get Rice(2) code meaning run length of zeroes
- If run length is more than one then add (run_length + 1) bits to saved bit, else substract 3 from saved bits

- Else increment decode_run_length

- If we have some saved bits:
- If new code is non-zero:
- If decode_run_length > 2 then add it to saved bits, else substract 3 from saved bits
- Set decode_run_length to zero

- Return new code

## Rice Code Parameter Updating

Rice decoder choses its parameter based on two things - sum of absolute values of decoded residues and their number. To avoid overflow if count went up to sixteen then halve both count and sum.

Rice parameter is log2(sum/number) clamped to interval [0..9]