lesson4.cpp
/////////////////
//// Lesson 4 - Priors
/////////////////
/*
  This lesson illustrates how priors may be used to help describe correlations in a system
  In the sce4narios below, the index of the stream helps to decide what happens.
  Even values must be 0 and odd values may be 1 or 3.
  Given this foreknowledge, we know that the index of the stream is an excellent prior
  That can help the arithmetic coding system encode the bitstream efficiently.

  the EXERCISE is finding priors for a truly random walk.

  You could simply subtract the previous value from the next, and store things in a prior buffer

  To do this exercise well, you may need to adjust CODING_BUFFER_SIZE to 1000000 in test_harness.h
  to allow for a longer stream to for the coder to learn on.
  The prior number in its entirety is usually a good prior since there are only 2 possible outcomes
  for each previous value
  But even each corresponding bit as a prior could be sufficient for fairly good performance
 */
Lesson 4 - Priors
This lesson illustrates how priors may be used to help describe correlations in a system
In the sce4narios below, the index of the stream helps to decide what happens.
Even values must be 0 and odd values may be 1 or 3.
Given this foreknowledge, we know that the index of the stream is an excellent prior
That can help the arithmetic coding system encode the bitstream efficiently.

the EXERCISE is finding priors for a truly random walk.

You could simply subtract the previous value from the next, and store things in a prior buffer

To do this exercise well, you may need to adjust CODING_BUFFER_SIZE to 1000000 in test_harness.h
to allow for a longer stream to for the coder to learn on.
The prior number in its entirety is usually a good prior since there are only 2 possible outcomes
for each previous value
But even each corresponding bit as a prior could be sufficient for fairly good performance
Lesson 4 - Priors This lesson illustrates how priors may be used to help describe correlations in a system In the sce4narios below the index of the stream helps to decide what happens . Even values must be 0 and odd values may be 1 or 3 . Given this foreknowledge we know that the index of the stream is an excellent prior That can help the arithmetic coding system encode the bitstream efficiently . the EXERCISE is finding priors for a truly random walk . You could simply subtract the previous value from the next and store things in a prior buffer To do this exercise well you may need to adjust CODING_BUFFER_SIZE to 1000000 in test_harness . h to allow for a longer stream to for the coder to learn on . The prior number in its entirety is usually a good prior since there are only 2 possible outcomes for each previous value But even each corresponding bit as a prior could be sufficient for fairly good performance
void encode_with_adaptive_probability() {
    DynProb encode[2][256];
    vpx_writer wri ={0};
    vpx_start_encode(&wri, compressed);
    for (size_t i = 0; i < sizeof(uncompressed); ++i) {
        uint8_t encoded_so_far = 0; 
        for(int bit = 0; bit < 8; ++bit) {
            bool cur_bit = !!(uncompressed[i] & (1 << (7 - bit)));
            vpx_write(&wri, cur_bit, encode[i%2][encoded_so_far + (1 << bit)].prob);
            encode[i%2][encoded_so_far + (1 << bit)].record_bit(cur_bit);
            if (cur_bit) {
                encoded_so_far |= (1 << bit); 
            }
        }
    }
    vpx_stop_encode(&wri);
    printf("Buffer encoded dynamically results in %d size (%.2f%%)\n",
           wri.pos,
           100 * wri.pos / float(sizeof(uncompressed)));
    DynProb decode[2][256];
    vpx_reader rea={0};
    vpx_reader_init(&rea,
                    wri.buffer,
                    wri.pos);
    memset(roundtrip, 0, sizeof(roundtrip));
    for (size_t i = 0; i < sizeof(roundtrip); ++i) {
        uint8_t decoded_so_far = 0; 
        for(int bit = 0; bit < 8; ++bit) {
            if (vpx_read(&rea, decode[i%2][decoded_so_far + (1 << bit)].prob)) {
                roundtrip[i] |= (1 << (7 - bit));
                decode[i%2][decoded_so_far + (1 << bit)].record_bit(true);
                decoded_so_far |= (1 << bit); 
            } else {
                decode[i%2][decoded_so_far + (1 << bit)].record_bit(false);
            }
        }
    }
    assert(vpx_reader_has_error(&rea) == 0);
    assert(memcmp(uncompressed, roundtrip, sizeof(uncompressed)) == 0);
}
void encode_with_adaptive_probability ( ) { DynProb encode [ 2 ] [ 256 ] ; vpx_writer wri = { 0 } ; vpx_start_encode ( & wri , compressed ) ; for ( size_t i = 0 ; i < sizeof ( uncompressed ) ; ++ i ) { uint8_t encoded_so_far = 0 ; for ( int bit = 0 ; bit < 8 ; ++ bit ) { bool cur_bit = ! ! ( uncompressed [ i ] & ( 1 << ( 7 - bit ) ) ) ; vpx_write ( & wri , cur_bit , encode [ i % 2 ] [ encoded_so_far + ( 1 << bit ) ] . prob ) ; encode [ i % 2 ] [ encoded_so_far + ( 1 << bit ) ] . record_bit ( cur_bit ) ; if ( cur_bit ) { encoded_so_far |= ( 1 << bit ) ; } } } vpx_stop_encode ( & wri ) ; printf ( " \n " , wri . pos , 100 * wri . pos / float ( sizeof ( uncompressed ) ) ) ; DynProb decode [ 2 ] [ 256 ] ; vpx_reader rea = { 0 } ; vpx_reader_init ( & rea , wri . buffer , wri . pos ) ; memset ( roundtrip , 0 , sizeof ( roundtrip ) ) ; for ( size_t i = 0 ; i < sizeof ( roundtrip ) ; ++ i ) { uint8_t decoded_so_far = 0 ; for ( int bit = 0 ; bit < 8 ; ++ bit ) { if ( vpx_read ( & rea , decode [ i % 2 ] [ decoded_so_far + ( 1 << bit ) ] . prob ) ) { roundtrip [ i ] |= ( 1 << ( 7 - bit ) ) ; decode [ i % 2 ] [ decoded_so_far + ( 1 << bit ) ] . record_bit ( true ) ; decoded_so_far |= ( 1 << bit ) ; } else { decode [ i % 2 ] [ decoded_so_far + ( 1 << bit ) ] . record_bit ( false ) ; } } } assert ( vpx_reader_has_error ( & rea ) == 0 ) ; assert ( memcmp ( uncompressed , roundtrip , sizeof ( uncompressed ) ) == 0 ) ; }
Lesson 4 - Priors
This lesson illustrates how priors may be used to help describe correlations in a system
In the sce4narios below, the index of the stream helps to decide what happens.
Lesson 4 - Priors This lesson illustrates how priors may be used to help describe correlations in a system In the sce4narios below the index of the stream helps to decide what happens .
// one probability per number encoded so far and per index mod 2 // <-- this is the number encoded so far (in reverse order), as a prior for the rest // <-- this is the number encoded so far, as a prior for the rest // <-- this is the number decoded so far, as a prior for the rest // <-- this is the number decoded so far, as a prior for the rest