Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mean k #144

Merged
merged 5 commits into from
Jan 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions ffv1.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,31 @@ The alternative state transition table has been built using iterative minimizati

### Golomb Rice Mode

This coding mode uses Golomb Rice codes. The VLC is split into 2 parts, the prefix stores the most significant bits and the suffix stores the k least significant bits or stores the whole number in the ESC case. The end of the bitstream of the `Frame` is filled with 0-bits until that the bitstream contains a multiple of 8 bits.
The end of the bitstream of the `Frame` is filled with 0-bits until that the bitstream contains a multiple of 8 bits.

#### Prefix
#### Signed Golomb Rice Codes

This coding mode uses Golomb Rice codes. The VLC is split into 2 parts, the prefix stores the most significant bits and the suffix stores the k least significant bits or stores the whole number in the ESC case.

```c
pseudo-code | type
--------------------------------------------------------------|-----
int get_ur_golomb(k) { |
for (prefix = 0; prefix < 12; prefix++) { |
if ( get_bits(1) ) |
return get_bits(k) + (prefix << k) |
} |
return get_bits(bits) + 11 |
} |
|
int get_sr_golomb(k) { |
v = get_ur_golomb(k); |
if (v & 1) return - (v >> 1) - 1; |
else return (v >> 1); |
}
```

##### Prefix

|bits | value |
|:--------------|:------|
Expand All @@ -643,14 +665,14 @@ This coding mode uses Golomb Rice codes. The VLC is split into 2 parts, the pref
|0000 0000 0001 | 11 |
|0000 0000 0000 | ESC |

#### Suffix
##### Suffix

| | |
|:-------------|:--------------------------------------------------------|
|non ESC | the k least significant bits MSB first |
|ESC | the value - 11, in MSB first order, ESC may only be used if the value cannot be coded as non ESC|

#### Examples
##### Examples

| k | bits | value |
|:---:|:--------------------------|------:|
Expand All @@ -665,7 +687,7 @@ This coding mode uses Golomb Rice codes. The VLC is split into 2 parts, the pref

Run mode is entered when the context is 0 and left as soon as a non-0 difference is found. The level is identical to the predicted one. The run and the first different level are coded.

#### Run Length Coding
##### Run Length Coding

The run value is encoded in 2 parts, the prefix part stores the more significant part of the run as well as adjusting the run\_index that determines the number of bits in the less significant part of the run. The 2nd part of the value stores the less significant part of the run as it is. The run_index is reset for each `Plane` and slice to 0.

Expand Down Expand Up @@ -700,17 +722,73 @@ if (run_count == 0 && run_mode == 1) { |

The log2\_run function is also used within [@ISO.14495-1.1999].

#### Level Coding
##### Level Coding

Level coding is identical to the normal difference coding with the exception that the 0 value is removed as it cannot occur:

```c
if (diff>0) diff--;
encode(diff);
diff = get_vlc_symbol(context_state);
if (diff >= 0)
diff++;
```

Note, this is different from JPEG-LS, which doesn’t use prediction in run mode and uses a different encoding and context model for the last difference On a small set of test `Samples` the use of prediction slightly improved the compression rate.

#### Scalar Mode

Each difference is coded with the per context mean prediction removed and a per context value for k.

```c
get_vlc_symbol(state) {
i = state->count;
k = 0;
while (i < state->error_sum) {
k++;
i += i;
}

v = get_sr_golomb(k);

if (2 * state->drift < -state->count)
v = - 1 - v;

ret = sign_extend(v + state->bias, bits);

state->error_sum += abs(v);
state->drift += v;

if (state->count == 128) {
state->count >>= 1;
state->drift >>= 1;
state->error_sum >>= 1;
}
state->count++;
if (state->drift <= -state->count) {
state->bias = max(state->bias - 1, -128);

state->drift = max(state->drift + state->count,
-state->count + 1);
} else if (state->drift > 0) {
state->bias = min(state->bias + 1, 127);

state->drift = min(state->drift - state->count, 0);
}

return ret;
}
```

#### Initial Values for the VLC context state

At keyframes all coder state variables are set to their initial state.

```c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing carriage return.

drift = 0;
error_sum = 4;
bias = 0;
count = 1;
```

# Bitstream

An FFV1 bitstream is composed of a series of 1 or more `Frames` and (when required) a `Configuration Record`.
Expand All @@ -720,7 +798,7 @@ Within the following sub-sections, pseudo-code is used to explain the structure
|Symbol| Definition |
|------|--------------------------------------------------------|
| u(n) | unsigned big endian integer using n bits |
| sg | Golomb Rice coded signed scalar symbol coded with the method described in [Huffman Coding Mode](#golomb-rice-mode) |
| sg | Golomb Rice coded signed scalar symbol coded with the method described in [Signed Golomb Rice Codes](#golomb-rice-mode) |
| br | Range coded Boolean (1-bit) symbol with the method described in [Range binary values](#range-binary-values) |
| ur | Range coded unsigned scalar symbol coded with the method described in [Range non binary values](#range-non-binary-values) |
| sr | Range coded signed scalar symbol coded with the method described in [Range non binary values](#range-non-binary-values) |
Expand Down