2 Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.
3 Permission is granted to make verbatim copies of this file for private
4 use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.
6 This file is: "ppmc.c" (exclusion)
7 Email: arturo@arturocampos.com
8 Web: http://www.arturocampos.com
10 Part of the ppmc encoder and decoder.
12 This module contains the whole ppmc encoder. It uses hash tables for
13 managing most of the orders. And a maximum order of 4. It codes bytes.
14 Order-1-0-(-1) are all handled in tables. Order-2 has a table with
15 direct hashing with pointers to the linked lists. Order-4 and order-3
16 both have hash tables with pointers to contexts in a linked lists which
17 finally have a pointer to the start of the linked list with the
18 probability distribution. Update exclusion is used, but exclusion is not.
20 Please, note that if the machine where the decoder is run doesn't has as
21 much memory as the computer where the encoder was ran, the decoder will
22 not be able to properly decode the file, because it will not be able to
23 keep track of new statistics, in this case it will just exit.
25 For applications where the loss of data is not admisible, I suggest you to
26 limit both encoder and decoder's memory requeriments to a given minimum.
28 Using exclusion. It's up to the main encoding routine to clear this table
40 // Ruotines used by ppmc. Not including the range coder.
42 // They are for initializing of both encoder and decoder, and unless there
43 // are two version, both encoder and decoder use the same routines. Like
44 // "ppmc_initialize_contexts".
47 // This one allocs the memory needed by ppmc, and adjust some pointers used
48 // for allocating elements in the linked lists. The mempool arrays must be
50 void ppmc_alloc_memory(void)
52 unsigned long counter;
55 // Init mempool buffers
57 for(counter=0;counter!=_mempool_max_index;++counter)
59 _bytes_pool_array[counter]=0;
60 _context_pool_array[counter]=0;
63 _bytes_pool_index=1; //first entry will be used now
64 _context_pool_index=1;
67 // Allocate memory for ppmc structures and adjust some variables
68 if((_bytes_pool=(struct _byte_and_freq *)malloc
69 (sizeof(struct _byte_and_freq)*_bytes_pool_elements))==NULL)
71 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
72 ppmc_out_of_memory=1; //flush
76 if((_context_pool=(struct context *)malloc
77 (sizeof(struct context)*_context_pool_elements))==NULL)
79 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
80 ppmc_out_of_memory=1; //flush
85 //save pointers in the array for freeing
86 _bytes_pool_array[0]=_bytes_pool;
87 _context_pool_array[0]=_context_pool;
91 _bytes_pool_max=_bytes_pool+_bytes_pool_elements;
92 _context_pool_max=_context_pool+_context_pool_elements;
94 ppmc_out_of_memory=0; //we still have memory
98 // This routine initializes all the contexts, and all the tables including
99 // those who care about the number of bytes defined in a context.
100 void ppmc_initialize_contexts(void)
102 unsigned long counter, counter2;
106 for(counter=0;counter!=256;++counter) //clear table
107 order0_array[counter]=0;
109 order0_defined_bytes=0; //adjust variables
114 for(counter=0;counter!=256;++counter) //erase every table of every context
115 for(counter2=0;counter2!=256;++counter2)
116 order1_array[counter][counter2]=0;
118 for(counter=0;counter!=256;++counter) //adjust variables
120 order1_defined_bytes_array[counter]=0;
121 order1_max_cump_array[counter]=0;
126 for(counter=0;counter!=65536;++counter)
128 //order2_array[counter].prob=0; //clear pointer to bytes and frequencies
129 //order2_array[counter].max_cump=0;
130 order2_array[counter].defined_bytes=0;
135 for(counter=0;counter!=65536;++counter) //order-4-3
137 order4_hasht[counter]=0;
138 order3_hasht[counter]=0;
143 // This routine initializes the encode model by outputting as many bytes as
144 // needed to prepare the models. This should be called before the main loop
145 // and after the memory has been allocated and tables initialized.
147 // It does not need uses the range coder. It output the first 1 bytes.
148 void ppmc_encoder_initialize(void)
151 // Initialize order-0 and prepare different bytes for orders
152 fputc((byte=fgetc(file_input)),file_output);
153 o4_byte=byte; //order-4
155 fputc((byte=fgetc(file_input)),file_output);
156 o3_byte=byte; //order-3
158 fputc((byte=fgetc(file_input)),file_output);
159 o2_byte=byte; //order-2
160 ppmc_update_order0();
162 fputc((byte=fgetc(file_input)),file_output);
168 // This routine initializes the decoder model, should be called to do the same
169 // changes as "ppmc_encoder_initialize()" did.
170 void ppmc_decoder_initialize(void)
173 // Initialize order-0 and context bytes
174 byte=fgetc(file_input);
175 o4_byte=byte; //order-4
176 fputc(byte,file_output);
178 byte=fgetc(file_input);
179 o3_byte=byte; //order-3
180 fputc(byte,file_output);
182 byte=fgetc(file_input);
183 o2_byte=byte; //order-2
185 fputc(byte,file_output); //output first byte
186 ppmc_update_order0();
188 byte=fgetc(file_input);
189 o1_byte=byte; //order-1
190 fputc(byte,file_output);
194 // Once coding or decoding is finished you have to call this routine.
195 // It must be called when done.
196 void ppmc_free_memory(void)
198 unsigned long counter;
200 // Free the memory buffers
202 for(counter=0;counter!=_mempool_max_index;++counter)
204 if(_bytes_pool_array[counter]!=0)
205 free(_bytes_pool_array[counter]);
207 if(_context_pool_array[counter]!=0)
208 free(_context_pool_array[counter]);
214 // This routine flushes the memory and restarts all the tables of
215 // probabilities, current order bytes are not modified, this function
216 // is called when we ran out of memory. We have to output the code
217 // number 256 which means memory flushing, for doing this we have to go
218 // to order-(-1) so we have to output an escape code in all the orders
219 // till we reach order-(-1) where we can code it. Then we free all the
220 // memory, alloc it again, and reinitialize all the orders.
222 // However we may find the case when the current order is not initialized,
223 // in this case we don't need to output an escape code.
224 void ppmc_flush_mem_enc(void)
226 unsigned long counter;
229 // Clear exclusion table
230 for(counter=0;counter!=256;++counter)
234 // Code an escape code in order-4
235 if(ppmc_get_totf_order4()!=0) //if 0 no need of escape code
238 ppmc_get_escape_prob_order4(); //get prob and cump
239 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
244 // Code an escape code in order-3
245 if(ppmc_get_totf_order3()!=0) //if 0 no need of escape code
248 ppmc_get_escape_prob_order3(); //get prob and cump
249 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
254 // Code an escape code in order-2
256 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
258 // First check if current order-2 context is empty
259 if(order2_array[o2_cntxt].defined_bytes!=0) //it's not empty
261 ppmc_get_totf_order2();
262 ppmc_get_escape_prob_order2();
263 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
267 // Code an escape code in order-1
269 // First check if current order-1 table is empty
270 if(order1_defined_bytes_array[o1_byte]!=0) //it's not empty
272 ppmc_get_totf_order1();
273 ppmc_get_escape_prob_order1();
274 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
278 // Code an escape code in order-0. Order-0 always has at least one symbol
280 ppmc_get_totf_order0();
281 ppmc_get_escape_prob_order0();
282 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
286 // Now we can code the code 256
291 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
294 // Now that decoder knows the flushing, free memory and reinit
298 ppmc_initialize_contexts();
301 // Be sure that order-0 has at least one probability
303 order0_array[o1_byte]++;
305 order0_defined_bytes++;
310 // When the decoder gets the symbol of flushing, most of the job is done
311 // because we already got all the escape codes, so we only have to reinit.
312 void ppmc_flush_mem_dec(void)
315 // Free memory and reinit
319 ppmc_initialize_contexts();
322 // Be sure that order-0 has at least one probability
324 order0_array[o1_byte]++;
326 order0_defined_bytes++;
333 // ORDER-(-1) functions, also called ordern1 (Negative1) in functions
335 // Because order-(-1) does not need to update its probability tables, it
336 // has no tables, and relies on the fact that the cump of byte is its own
337 // value, and the probability is fixed, 1, and the total cump is 257.
339 // The alphabet has the following distribution: 0-255 the bytes. 256 is
340 // an special symbol which means that we have flushed the encoder tables,
341 // and thus the encoder must flush its tables too.
343 // The rest of the tables only have 256 symbols, because we have no need
344 // of assign a symbol to the flush code (which already is the order-(-1)
345 // table) nor to the escape code.
347 // For order-(-1) we don't use exclusion.
350 // Gets the probability for a given symbol in the order-(-1) (ordern1)
351 void ppmc_get_prob_ordern1(void)
353 symb_cump=byte; //its value
354 symb_prob=1; //flat probability
355 total_cump=257; //total cump
359 // Returns in the variable "total_cump" the current total cump of
361 void ppmc_get_totf_ordern1(void)
363 total_cump=257; //this is fixed
367 // Returns the symbol for a given cump under order-(-1)
368 unsigned long ppmc_get_symbol_ordern1 (void)
377 // Due to the fact that order-0 has no context, I use an array for all the
378 // probabilities under order-0, just as you could do in a basic model for
379 // arithmetic coding.
381 // The main array is: order0_array. Where order0_array[byte] contains the
382 // probability for a given byte. The same applies to order-1.
384 // To ensure that the updating and coding is done correctly, "byte" can't
385 // be changed till all the coding and updating is done.
387 // Order-0 uses exclusions. Exclusion values are always prepared in "get_totf"
388 // so there's no need to get them again. However order-0 doesn't have to
389 // update exclude table, because order-(-1) will not use it
392 // Returns in the variable "total_cump" the current total cump of
393 // order-0. We have to read the whole array because we can't
394 // guarante that all the bytes are used.
395 void ppmc_get_totf_order0(void)
397 unsigned long temp_cump, //temp value for the cump
403 // Read the number of defined bytes by reading the count of every byte
404 // and if it's present in the exclusion table.
405 for(counter=0;counter!=256;++counter)
407 if(excluded[counter]==0) //only if it's not excluded
408 if(order0_array[counter]!=0) //if it has a nonzero count, then it's present
411 exc_max_cump+=order0_array[counter];
415 // Total cump is current total cump plus the probability for the escape code
416 exc_total_cump=exc_max_cump+exc_defined_bytes;
420 // Codes a byte under order-0 and returns 1, otherwise it returns a 0 and
421 // has coded an escape code. In this case further coding is needed.
423 // Returns: 1 in case a byte was coded. 0 in case of escape code.
424 char ppmc_code_byte_order0(void)
426 unsigned long counter;
428 ppmc_get_totf_order0(); //get total cump
430 // It's possible that due to excluding, there's no byte left, in that case
432 if(exc_defined_bytes==0)
435 // See if the byte is present
436 if(order0_array[byte]==0) //a probability of 0
439 // Because it was not present, output an escape code, prepare variables
441 symb_cump=exc_max_cump; //obviously its cump is current max_cump
442 //without escape code's space
444 symb_prob=exc_defined_bytes; //the number of defined bytes
446 total_cump=exc_total_cump;
448 // Code the escape code
449 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
451 return 0; //byte not coded
458 // The symbol is present, code it under order-0
460 symb_prob=order0_array[byte]; //get probability directly
462 // Make cump for current symbol
464 symb_cump=0; //for first symbol is 0
465 for(counter=0; counter!=byte ; ++counter)
467 if(excluded[counter]==0)
468 symb_cump+=order0_array[counter]; //sum probabilities before our symbol
471 total_cump=exc_total_cump;
474 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
476 return 1; //symbol coded under order-0
481 // This functions update order-0 probabilities with current byte, also takes
482 // care about updating variables, and renormalizing.
483 void ppmc_update_order0(void)
485 if(order0_array[byte]==0)
487 // It had a zero probability
488 order0_array[byte]++; //increment symbol probability
489 ++order0_defined_bytes; //new byte defined
490 ++order0_max_cump; //total cump
495 // It had a non-zero probability
497 // Increment its probability
498 order0_array[byte]++; //increment symbol probability
499 ++order0_max_cump; //total cump
501 // Check to see if its the maximum in this case renormalize
502 if(order0_array[byte]==255)
503 ppmc_renormalize_order0();
510 // This functions renormalizes the probabilities at order-0 updating variables
511 void ppmc_renormalize_order0(void)
513 unsigned long counter;
515 // Initialize variables
516 order0_defined_bytes=0; //clear them
519 // Loop over all probabilities, divide them by a factor of 2 and update variables
520 for(counter=0 ; counter!=256 ; ++counter)
522 if(order0_array[counter]!=0)
524 order0_array[counter]>>=1; //divide by a factor of 2
525 if(order0_array[counter]==0)
526 order0_array[counter]=1;
529 if(order0_array[counter]!=0) //see if it has a non zero probability
530 order0_defined_bytes++;
532 order0_max_cump+=order0_array[counter]; //sum to the total cump
537 // Decodes the symbol correspoding to the current order, it returns the byte
538 // or in case of a escape code it returns -1
539 void ppmc_decode_order0(void)
541 unsigned long current_cump, counter;
544 // Get the total cump needed for decoding symbol
545 ppmc_get_totf_order0(); //total cump needed for decoding
547 // It's possible that due to excluding, there's no byte left, in that case
549 if(exc_defined_bytes==0)
555 symb_cump=range_decoder_decode(&rc_decoder,exc_total_cump); //decode it
557 // Now check if it's an escape code
558 if(symb_cump>=exc_max_cump) //the defined code space for the escape code
561 // Update coding values
563 ppmc_get_escape_prob_order0();
564 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
566 // Mark as escape code
570 return; //an escape code
574 // Now we have to check what symbol it is
576 current_cump=0; //cump of the current symbol
578 for(counter=0 ; counter!= 256 ; ++counter)
580 if(symb_cump<current_cump)
581 break; //symbol found, break search loop
582 if(excluded[counter]==0)
583 current_cump+=order0_array[counter]; //update cump
586 counter--; //-1 (see ac_ppmc.html)
588 byte=counter; //update the variable with the found symbol
591 // Get the cump and prob of byte
593 symb_prob=order0_array[byte]; //the probabilty
594 symb_cump=current_cump-order0_array[byte]; //using the cump which was
597 // Update coding state
599 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
601 coded_in_order=0; //decoded under order-0
609 // This function returns the probability for the escape codes in the variables
610 void ppmc_get_escape_prob_order0(void)
612 // To understand that remember that the escape allocated for the escape code
613 // is above the current maximum cump and that it has a probability determined
615 symb_prob=exc_defined_bytes;
616 symb_cump=exc_max_cump;
617 total_cump=exc_defined_bytes+exc_max_cump;
624 // Order-1 uses 256 arrays one for every context, obviously they are accessed
625 // like that: order1_array[o1_byte][byte]
627 // Also for computing the value of the current escape code in a given context
628 // we need to know how many bytes are defined in a given context, they are
629 // stored in an array, and you use them like that:
630 // order1_defined_bytes_array[o1_byte]
632 // The same goes for the maximum cump of a given context:
633 // order1_max_cump_array[o1_byte]
635 // Read the order-0 explanation.
637 // To ensure that the updating and coding is done correctly, "byte" and
638 // "o1_byte" can't be changed till all the coding and updating is done.
640 // Order-1 uses exclusions. Exclusion values are always prepared in "get_totf"
641 // so there's no need to get them again. It does update the exclude table.
644 // Returns in the variable "total_cump" the current total cump of
645 // order-1. o1_byte should contain the order-1
646 void ppmc_get_totf_order1(void)
648 unsigned long temp_cump, //temp value for the cump
654 // Read the number of defined bytes by reading the count of every byte
655 // and if it's present in the exclusion table.
656 for(counter=0;counter!=256;++counter)
658 if(excluded[counter]==0) //only if it's not excluded
659 if(order1_array[o1_byte][counter]!=0) //if it has a nonzero count, then it's present
662 exc_max_cump+=order1_array[o1_byte][counter];
666 // Total cump is current total cump plus the probability for the escape code
667 exc_total_cump=exc_max_cump+exc_defined_bytes;
671 // Codes a byte under order-1 and returns 1.
672 // Otherwise it returns a 0 it may be that it has coded an escape code, or
673 // that current table was empty, in this case nothing is outputted
674 // In both cases further coding is needed.
676 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
677 // In case the byte is coded under this context, coded_in_order=1.
678 char ppmc_code_byte_order1(void)
680 unsigned long counter;
683 // First check if current order-1 table is empty
684 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
686 return 0; //byte not coded, nothing done
690 // Now try to code this byte under order-1
692 ppmc_get_totf_order1(); //get total cump
694 // It's possible that due to excluding, there's no byte left, in that case
696 if(exc_defined_bytes==0)
700 // See if the byte is present
701 if(order1_array[o1_byte][byte]==0) //a probability of 0
704 // Because it was not present, output an escape code, prepare variables
706 symb_cump=exc_max_cump;
708 symb_prob=exc_defined_bytes;
710 // Code the escape code
711 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
713 // Now update "exclude" table
714 for(counter=0;counter!=256;++counter)
715 if(order1_array[o1_byte][counter]!=0)
716 excluded[counter]=1; //occurred but was not code, now exclude
718 return 0; //byte not coded, escape coded
723 coded_in_order=1; //because we coded it under order-1
725 // The symbol is present, code it under order-1
727 symb_prob=order1_array[o1_byte][byte]; //get probability directly
729 // Make cump for current symbol
731 symb_cump=0; //for first symbol is 0
732 for(counter=0; counter!=byte ; ++counter)
734 if(excluded[counter]==0)
735 symb_cump+=order1_array[o1_byte][counter]; //sum probabilities before our symbol
739 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
741 return 1; //symbol coded under order-1
746 // This functions update order-1 probabilities with current byte, also takes
747 // care about updating variables, and renormalizing. o1_byte must be filled.
748 void ppmc_update_order1(void)
750 if(order1_array[o1_byte][byte]==0)
752 // It had a zero probability
753 order1_array[o1_byte][byte]++; //increment symbol probability
754 ++order1_defined_bytes_array[o1_byte]; //new byte defined
755 ++order1_max_cump_array[o1_byte]; //total cump
760 // It had a non-zero probability
762 // Increment its probability
763 order1_array[o1_byte][byte]++; //increment symbol probability
764 ++order1_max_cump_array[o1_byte]; //total cump
766 // Check to see if its the maximum in this case renormalize
767 if(order1_array[o1_byte][byte]==255)
768 ppmc_renormalize_order1();
775 // This functions renormalizes the probabilities at order-1 updating variables
776 void ppmc_renormalize_order1(void)
778 unsigned long counter;
780 // Initialize variables
781 order1_defined_bytes_array[o1_byte]=0; //clear them
782 order1_max_cump_array[o1_byte]=0;
784 // Loop over all probabilities, divide them by a factor of 2 and update variables
785 for(counter=0 ; counter!=256 ; ++counter)
787 if(order1_array[o1_byte][counter]!=0)
789 order1_array[o1_byte][counter]>>=1; //divide by a factor of 2
790 if(order1_array[o1_byte][counter]==0)
791 order1_array[o1_byte][counter]=1; //don't let it have a 0 count
794 if(order1_array[o1_byte][counter]!=0) //see if it has a non zero probability
795 order1_defined_bytes_array[o1_byte]++;
797 order1_max_cump_array[o1_byte]+=order1_array[o1_byte][counter]; //sum to the total cump
802 // Decodes the symbol correspoding to the current order, it returns the byte
803 // or in case of an escape code or empty table it returns -1.
804 // It updates "coded_in_order".
805 void ppmc_decode_order1(void)
807 unsigned long current_cump, counter;
810 // First check if current order-1 table is empty
811 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
813 byte=-1; //byte not coded, nothing done
818 // Get the total cump needed for decoding symbol
819 ppmc_get_totf_order1(); //total cump needed for decoding
821 // It's possible that due to excluding, there's no byte left, in that case
823 if(exc_defined_bytes==0)
829 symb_cump=range_decoder_decode(&rc_decoder,exc_total_cump); //decode it
831 // Now check if it's an escape code
832 if(symb_cump>=exc_max_cump) //the defined code space for the escape code
835 // Update coding values
837 ppmc_get_escape_prob_order1();
838 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
841 // Now update "exclude" table
842 for(counter=0;counter!=256;++counter)
843 if(order1_array[o1_byte][counter]!=0)
844 excluded[counter]=1; //occurred but was not code, now exclude
846 // Mark as escape code (in fact nothing coded)
850 return; //an escape code
854 // Now we have to check what symbol it is
856 current_cump=0; //cump of the current symbol
858 for(counter=0 ; counter!= 256 ; ++counter)
860 if(symb_cump<current_cump)
861 break; //symbol found, break search loop
862 if(excluded[counter]==0)
863 current_cump+=order1_array[o1_byte][counter]; //update cump
866 counter--; //-1 (see ppmc.txt, searching for a given symbol)
868 byte=counter; //update the variable with the found symbol
871 // Get the cump and prob of byte
873 symb_prob=order1_array[o1_byte][byte]; //the probabilty
874 symb_cump=current_cump-order1_array[o1_byte][byte]; //using the cump which was
877 // Update coding state
879 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
882 // Update coded_in_order used for update exclusion
892 // This function returns the probability for the escape codes in the variables
893 void ppmc_get_escape_prob_order1(void)
895 // To understand that remember that the escape allocated for the escape code
896 // is above the current maximum cump and that it has a probability determined
898 symb_prob=exc_defined_bytes;
899 symb_cump=exc_max_cump;
900 total_cump=exc_defined_bytes+exc_max_cump;
907 // Order-2 uses a table which holds the contexts data structure of every
908 // structure, its accessed with "ppmc_order2_hash_key(o1_byte, o2_byte)"
909 // and because it uses direct addressing there's no need to check for hash
910 // collisions, which makes it faster. The variable "o2_cntxt" contains o1_byte
911 // and o2_byte used directly to index the order-2 array.
913 // Though order-2 uses the same routines as lower orders they are rather
914 // different, because we can't allocate memory for all the probabilities
915 // tables, we have to use linked lists for the bytes and probabilities.
917 // Under order-2 we don't let a probability to be lower than 1, that is,
918 // there can't be a symbol in a linked list with a 0 probability, I choosed
919 // this for three factors: the code is easier to read (it isn't messy)
920 // we gain some speed, and the loss of compression seems little.
922 // To ensure that the updating is done correctly, "o2_cntxt" and
923 // "o2_ll_node" must not be modified by any routine except those who manage
926 // Order-2 uses exclusions. Exclusion values are always prepared in "get_totf"
927 // so there's no need to get them again. It does update the exclude table.
930 // Returns in the variable "total_cump" the current total cump of
931 // order-2. cntxt must have o1_byte and o2_byte hashed with the hash key
934 // Here we have to compute both "exc_defined_bytes" and "exc_max_cump"
935 void ppmc_get_totf_order2(void)
937 struct _byte_and_freq *node;
940 // Read the whole linked list for making the values
941 node=order2_array[o2_cntxt].prob;
946 if(excluded[node->byte]==0)
949 exc_max_cump+=node->freq; //add the probability of this byte to the cump
953 node=node->next; //next element in the linked list
957 // Total cump is current total cump plus the probability for the escape code
958 exc_total_cump=exc_max_cump+exc_defined_bytes;
963 // Codes a byte under order-2 and returns 1.
964 // Otherwise it returns a 0. It may be that it has coded an escape code, or
965 // that current table was empty.
967 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
968 // In case the byte is coded under this context, coded_in_order=2.
969 char ppmc_code_byte_order2(void)
971 unsigned long counter;
972 struct _byte_and_freq *node;
975 // Initialize o2_cntxt
977 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
980 // First check if current order-2 context is empty
981 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
983 return 0; //byte not coded, nothing done
987 // Now try to code this byte under order-2
989 ppmc_get_totf_order2(); //get total cump
992 // It's possible that due to exclusion, there's no byte left, in that case
994 if(exc_defined_bytes==0)
998 // See if the byte is present and compute its cump at the same time
1000 node=order2_array[o2_cntxt].prob; //pointer to first element in the linked list
1002 symb_cump=0; //the first symbol always has a 0 cump
1005 // Now search the byte in the linked list
1008 if(node->byte==byte)
1009 goto ppmc_o2_byte_found; //bad thing, I know, anyone has a better idea?
1010 if(excluded[node->byte]==0)
1011 symb_cump+=node->freq; //add the probability of this byte to the cump
1014 node=node->next; //next element in the linked list
1018 // If we reach that point, the byte was not found in the linked list
1019 // so we don't need the cump, we have to output an escape code, whose
1020 // probabilities are know using the context structure in the table.
1022 // Byte was not present in the linked list, current node is the last one,
1023 // and that's the node needed for creating a new node, save it.
1027 // Now get the probability and cump of the escape code
1029 symb_cump=exc_max_cump;
1030 symb_prob=exc_defined_bytes;
1032 // Code the escape code
1033 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
1035 // Then, update "excluded" table
1037 node=order2_array[o2_cntxt].prob;
1040 excluded[node->byte]=1;
1043 node=node->next; //next element in the linked list
1046 return 0; //now exit
1049 // That code is executed when the byte is found in the linked list
1054 // Everything has been tested, now we can feel free to code the byte,
1055 // the symb_cump is already computed, now get its probability and code
1056 // the byte, also save pointer to this element in the linked lists for
1059 coded_in_order=2; //successfully coded under order-2
1061 o2_ll_node=node; //save it for updating
1063 symb_prob=node->freq; //get the probability of the byte
1067 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
1069 return 1; //byte coded under order-2
1073 // This functions update order-2 probabilities with current byte, also takes
1074 // care about updating variables, and renormalizing.
1075 // Of course "o2_ll_node" must point to the element to update or the last one,
1076 // based on the "coded_in_order" and checking the pointer of the element we
1077 // can decide what to do.
1079 // This updating is only for encoding.
1080 void ppmc_update_order2(void)
1082 struct _byte_and_freq *node;
1085 // First of all check if that's the first byte in this context, in that case
1086 // we have to initialize some variables in the context structure.
1088 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
1091 if(ppmc_out_of_memory==1)
1092 return; //exit this function, we can't allocate memory
1094 order2_array[o2_cntxt].defined_bytes=1;
1095 order2_array[o2_cntxt].max_cump=1;
1096 order2_array[o2_cntxt].prob=_bytes_pool;
1098 _bytes_pool->byte=byte; //initialize byte to current one
1099 _bytes_pool->freq=1; //it appeared once
1100 _bytes_pool->next=0; //now this is last element in ll
1102 // Do update of linked list variables and memory use
1104 ++_bytes_pool; //next time use next entry (this is a pointer)
1106 if(_bytes_pool==_bytes_pool_max) //maximum reached
1109 // First check to see that we still have entries in the array
1111 if(_bytes_pool_index==_mempool_max_index)
1113 ppmc_out_of_memory=1; //flush
1117 // Allocate memory for new buffer
1119 if((_bytes_pool=(struct _byte_and_freq *)malloc
1120 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1122 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1123 ppmc_out_of_memory=1; //flush
1127 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1129 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1131 _bytes_pool_index++;
1135 return; //nothing else to do
1139 // The byte was coded under order two, otherwise it was coded under a
1140 // lower order (never higher ones, remember that we are using update
1141 // exclusion) in this case we have to create a new node in the list.
1143 if(coded_in_order==2) //coded under order-2
1146 // Update its count and variables of this context and check for renormalization
1148 o2_ll_node->freq++; //increment its frequency (rather probability)
1150 order2_array[o2_cntxt].max_cump++; //total cump
1152 if(o2_ll_node->freq==255) //do we have to renormalize?
1153 ppmc_renormalize_order2(); //renormalize
1159 // Once every paranoid check has been done we are sure that this byte
1160 // did not existed and so we have to create a new node in the linked
1161 // list. Also we have to take care of memory issues.
1163 // However due to the use of exclusion, we have to ensure that "o2_ll_node"
1164 // points to the last element in the linked lists of this context
1166 node=order2_array[o2_cntxt].prob; //get pointer to linked list
1170 if(node->next==0) //check for the end of the linked list
1172 node=node->next; //next node
1178 if(ppmc_out_of_memory==1)
1179 return; //exit this function, we can't allocate mem
1181 o2_ll_node->next=_bytes_pool; //put it in the next free entry
1182 _bytes_pool->byte=byte; //initialize byte to current one
1183 _bytes_pool->freq=1; //it appeared once
1184 _bytes_pool->next=0; //now this is last element in ll
1186 order2_array[o2_cntxt].max_cump++; //total cump
1187 order2_array[o2_cntxt].defined_bytes++; //total cump
1189 // Do update of linked list variables and memory use
1191 ++_bytes_pool; //next time use next entry (this is a pointer)
1193 if(_bytes_pool==_bytes_pool_max) //maximum reached
1196 // First check to see that we still have entries in the array
1198 if(_bytes_pool_index==_mempool_max_index)
1200 ppmc_out_of_memory=1; //flush
1204 // Allocate memory for new buffer
1206 if((_bytes_pool=(struct _byte_and_freq *)malloc
1207 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1209 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1210 ppmc_out_of_memory=1; //flush
1214 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1216 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1218 _bytes_pool_index++;
1227 // This functions renormalizes the probabilities at order-2 updating context
1229 void ppmc_renormalize_order2(void)
1231 unsigned long counter;
1232 struct _byte_and_freq *node;
1234 // Initialize variables. Defined bytes remain the same.
1235 order2_array[o2_cntxt].max_cump=0; //clear them
1237 node=order2_array[o2_cntxt].prob; //get pointer to lined lists
1239 // Divide all the probabilities by 2 and update context variables
1242 node->freq>>=1; //divide by a factor of 2
1244 if(node->freq==0) //don't allow a probability to be 0
1247 order2_array[o2_cntxt].max_cump+=node->freq; //sum to the total cump
1249 if(node->next==0) //last element
1255 //printf("\nRenormalization, context:%c%c",o2_byte,o1_byte);
1260 // Decodes the symbol correspoding to the current order, it returns the byte
1261 // or in case of an escape code or empty table it returns -1.
1262 // It updates "coded_in_order".
1264 // If we decode an escape code, we don't modify "o2_ll_node" and thus its
1265 // work of the updating routine to read till the end of the linked list
1266 // (for adding a new element) however if we decode a byte, we save on
1267 // "o2_ll_node" the pointer to its node. (so updating is faster)
1268 void ppmc_decode_order2(void)
1270 unsigned long current_cump, counter;
1271 struct _byte_and_freq *node;
1273 // Initialize o2_cntxt
1275 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
1278 // First check if current order-2 context is empty
1279 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
1281 byte=-1; //byte not coded, nothing done
1286 // Get the total cump needed for decoding symbol
1287 ppmc_get_totf_order2(); //total cump needed for decoding
1290 // It's possible that due to exclusion, there's no byte left, in that case
1292 if(exc_defined_bytes==0)
1295 return; //byte not coded, nothing done
1299 symb_cump=range_decoder_decode(&rc_decoder,exc_total_cump); //decode it
1301 // Now check if it's an escape code
1302 if(symb_cump>=exc_max_cump) //the defined code space for the escape code
1305 // Update coding values
1307 ppmc_get_escape_prob_order2();
1308 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
1310 // Mark as escape code
1314 // Then, update "excluded" table
1316 node=order2_array[o2_cntxt].prob;
1319 excluded[node->byte]=1;
1322 node=node->next; //next element in the linked list
1325 return; //an escape code
1329 // Now we have to check what symbol it is
1331 current_cump=0; //cump of the current symbol
1333 node=order2_array[o2_cntxt].prob; //get pointer to linked lists
1337 if(excluded[node->byte]==0) //only if it's not excluded
1338 current_cump+=node->freq; //update cump
1339 if(symb_cump<current_cump)
1340 break; //symbol found, break search loop
1342 node=node->next; //next element
1343 //we have no need to check for the last symbol, we'll never read further
1344 //the end of the linked lists, before we'll found the last byte.
1348 //read byte value and probability
1350 symb_prob=node->freq; //get the probability for updating the state
1351 byte=node->byte; //get byte
1352 o2_ll_node=node; //used for updating
1355 // Get the cump of byte
1357 symb_cump=current_cump-symb_prob;
1359 // Update coding state
1361 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
1363 // Update coded_in_order used for update exclusion
1373 // This is the routine for updating while decoding. We have to search the byte
1374 // in the linked list, if it's present, update its count, otherwise we have
1375 // hitted the end of the linked list, and there we have to create a new node.
1377 // Of course if the byte was matched in order-2 we'll have a pointer to it
1378 // in "o2_ll_node" so we don't need to read the linked list. (we already did
1381 // Another case which we also have to specially deal with, this is the case
1382 // when the context has not been initalized yet.
1383 void ppmc_update_dec_order2(void)
1385 struct _byte_and_freq *node;
1388 // Handle the case when the context is not initialized
1389 // This code is the same as the one for the encoding.
1391 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
1394 if(ppmc_out_of_memory==1)
1395 return; //exit this function, we can't allocate memory
1397 order2_array[o2_cntxt].defined_bytes=1;
1398 order2_array[o2_cntxt].max_cump=1;
1399 order2_array[o2_cntxt].prob=_bytes_pool;
1401 _bytes_pool->byte=byte; //initialize byte to current one
1402 _bytes_pool->freq=1; //it appeared once
1403 _bytes_pool->next=0; //now this is last element in ll
1405 // Do update of linked list variables and memory use
1407 ++_bytes_pool; //next time use next entry (this is a pointer)
1409 if(_bytes_pool==_bytes_pool_max) //maximum reached
1412 // First check to see that we still have entries in the array
1414 if(_bytes_pool_index==_mempool_max_index)
1416 ppmc_out_of_memory=1; //flush
1420 // Allocate memory for new buffer
1422 if((_bytes_pool=(struct _byte_and_freq *)malloc
1423 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1425 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1426 ppmc_out_of_memory=1; //flush
1430 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1432 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1434 _bytes_pool_index++;
1439 return; //nothing else to do
1443 // Current context is initalized, proceed
1445 if(coded_in_order==2) //check if it was decoded under order-2
1448 // We can be sure that the pointer "o2_ll_node" points to its entry, and
1449 // it has a non 0 probability (otherwise it couldn't be coded) so just
1450 // update its probability and max_cump
1452 o2_ll_node->freq++; //the probability of the byte
1453 order2_array[o2_cntxt].max_cump++; //the max_cump
1455 if(o2_ll_node->freq==255) //check for renormalization
1456 ppmc_renormalize_order2();
1462 // An escape code was decoded under order-2, we have to read till the
1463 // end of the linked list so we can add a new node for this new byte.
1465 node=order2_array[o2_cntxt].prob; //get pointer to linked list
1469 if(node->next==0) //check for the end of the linked list
1471 node=node->next; //next node
1475 // We reached the end of the linked list, add a new node if possible,
1476 // we are using the same code of "ppmc_update_order2()" with the
1477 // difference that the pointer to the linked list is "node"
1479 if(ppmc_out_of_memory==1)
1480 return; //exit this function, we can't allocate mem
1482 node->next=_bytes_pool; //put it in the next free entry
1483 _bytes_pool->byte=byte; //initialize byte to current one
1484 _bytes_pool->freq=1; //it appeared once
1485 _bytes_pool->next=0; //now this is last element in ll
1487 order2_array[o2_cntxt].max_cump++; //total cump
1488 order2_array[o2_cntxt].defined_bytes++; //total cump
1490 // Do update of linked list variables and memory use
1492 ++_bytes_pool; //next time use next entry (this is a pointer)
1494 if(_bytes_pool==_bytes_pool_max) //maximum reached
1497 // First check to see that we still have entries in the array
1499 if(_bytes_pool_index==_mempool_max_index)
1501 ppmc_out_of_memory=1; //flush
1505 // Allocate memory for new buffer
1507 if((_bytes_pool=(struct _byte_and_freq *)malloc
1508 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1510 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1511 ppmc_out_of_memory=1; //flush
1515 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1517 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1519 _bytes_pool_index++;
1523 return; //we are finished updating
1530 // This function returns the probability for the escape codes in the variables
1531 void ppmc_get_escape_prob_order2(void)
1533 // To understand that remember that the escape allocated for the escape code
1534 // is above the current maximum cump and that it has a probability determined
1537 symb_prob=exc_defined_bytes;
1538 symb_cump=exc_max_cump;
1543 // ORDER-3 functions
1545 // The difference between order-3 and order-3 are just a few, instead of
1546 // keeping a table with the context structures, we keep a hash table with
1547 // pointers to linked lists with the context, so it's only a matter of
1548 // searching current context in the linked list corresponding to its hash
1549 // entry. This is done in "ppmc_get_totf_order3" because that's the first
1550 // routine that both encoding and decoding routines call.
1553 // Returns in the variable "total_cump" the current total cump of
1554 // order-3. Must be called while encoding or decoding before anything else
1555 // because it initializes the pointers to the context structure in
1556 // "o3_context" and o3_cntxt.
1558 // If the hash entry is not initialized it returns "o3_context"=0
1559 // If the context is not present in the linked list of context, "o3_context"
1560 // will point to the last element in the linked list.
1561 // If the context is present "o3_context" will point to the context to use.
1562 // One can distinguish the last two by checking the context value of the
1563 // structure, if it's not the same, is the last element.
1565 // The routine returns 0 if the hash entry is not initialized or if the
1566 // the context was not present. Otherwise it returns 1, meaning that we
1567 // have to code under this context.
1568 char ppmc_get_totf_order3(void)
1570 struct context *cntxt_node;
1571 struct _byte_and_freq *node;
1574 // First make the hash key for order-3
1576 o3_cntxt=ppmc_order3_hash_key(o1_byte,o2_byte,o3_byte);
1577 full_o3_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16); //order-3
1580 // Now check the hash entry in the table
1582 if(order3_hasht[o3_cntxt]==0) //if 0, not initialized
1585 o3_context=0; //no hash entry
1587 return 0; //hash entry not initialized
1591 // Now read trough the linked list of context searching current one
1593 cntxt_node=order3_hasht[o3_cntxt];
1598 if(cntxt_node->order4321==full_o3_cntxt) //compare context
1599 goto ppmc_gtf_cntxt_found;
1601 if(cntxt_node->next==0) //end of context's linked list
1604 cntxt_node=cntxt_node->next; //next element
1609 // Once there the context was not found
1610 o3_context=cntxt_node; //pointer to last element in the linked list
1612 return 0; //it was not present
1615 // The context is found, so return pointer and cump
1617 ppmc_gtf_cntxt_found:
1619 o3_context=cntxt_node;
1621 // Read the whole linked list for making the values
1622 node=o3_context->prob;
1624 exc_defined_bytes=0;
1627 if(excluded[node->byte]==0)
1629 exc_defined_bytes++;
1630 exc_max_cump+=node->freq; //add the probability of this byte to the cump
1634 node=node->next; //next element in the linked list
1638 // Total cump is current total cump plus the probability for the escape code
1639 exc_total_cump=exc_max_cump+exc_defined_bytes;
1642 return 1; //context found
1647 // Codes a byte under order-3 and returns 1.
1648 // Otherwise it returns a 0.
1650 // In case the byte is coded under this context, coded_in_order=3.
1651 char ppmc_code_byte_order3(void)
1653 unsigned long counter;
1654 struct _byte_and_freq *node;
1657 // Get current context (if present) and total cump.
1659 if(ppmc_get_totf_order3()==0)
1663 // It's possible that due to exclusion, there's no byte left, in that case
1665 if(exc_defined_bytes==0)
1669 // See if the byte is present and compute its cump at the same time
1671 node=o3_context->prob; //pointer to first element in the linked list
1673 symb_cump=0; //the first symbol always has a 0 cump
1676 // Now search the byte in the linked list
1679 if(node->byte==byte)
1680 goto ppmc_o3_byte_found; //bad thing, I know, anyone has a better idea?
1681 if(excluded[node->byte]==0)
1682 symb_cump+=node->freq; //add the probability of this byte to the cump
1685 node=node->next; //next element in the linked list
1689 // If we reach that point, the byte was not found in the linked list
1690 // so we don't need the cump, we have to output an escape code, whose
1691 // probabilities are know using the context structure in the table.
1693 // Byte was not present in the linked list, current node is the last one,
1694 // and that's the node needed for creating a new node, save it.
1698 // Now get the probability and cump of the escape code
1700 symb_cump=exc_max_cump;
1701 symb_prob=exc_defined_bytes;
1703 // Code the escape code
1704 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
1706 // Then, update "excluded" table
1708 node=o3_context->prob;
1711 excluded[node->byte]=1;
1714 node=node->next; //next element in the linked list
1720 // That code is executed when the byte is found in the linked list
1725 // Everything has been tested, now we can feel free to code the byte,
1726 // the symb_cump is already computed, now get its probability and code
1727 // the byte, also save pointer to this element in the linked lists for
1730 coded_in_order=3; //successfully coded under order-3
1732 o3_ll_node=node; //save it for updating
1734 symb_prob=node->freq; //get the probability of the byte
1738 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
1740 return 1; //byte coded under order-3
1744 // This functions update order-3 probabilities with current byte, also takes
1745 // care about updating variables, and renormalizing.
1747 // "o3_ll_node" must point to the element to update or the last one,
1748 // based on the "coded_in_order" and checking the pointer of the element we
1749 // can decide what to do. Also "o3_context" must be initialized.
1751 // This updating is only for encoding.
1752 void ppmc_update_order3(void)
1754 struct _byte_and_freq *node;
1757 // First thing first, check if the hash entry is initialized
1759 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
1762 if(ppmc_out_of_memory==1)
1763 return; //exit this function, we can't allocate memory
1766 // First create the context
1768 order3_hasht[o3_cntxt]=_context_pool;
1770 _context_pool->next=0; //this is the last element
1771 _context_pool->order4321=full_o3_cntxt; //put context
1772 _context_pool->prob=_bytes_pool; //pointer to linked list
1773 _context_pool->max_cump=1;
1774 _context_pool->defined_bytes=1;
1777 // Do update of linked list variables and memory use of contexts
1779 ++_context_pool; //next time use next entry (this is a pointer)
1781 if(_context_pool==_context_pool_max) //maximum reached
1784 // First check to see that we still have entries in the array
1786 if(_context_pool_index==_mempool_max_index)
1788 ppmc_out_of_memory=1; //flush
1792 // Allocate memory for new buffer
1794 if((_context_pool=(struct context *)malloc
1795 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1797 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1798 ppmc_out_of_memory=1; //flush
1802 _context_pool_array[_context_pool_index]=_context_pool;
1804 _context_pool_max=_context_pool+_context_pool_elements_inc;
1806 _context_pool_index++;
1811 // Now care about the first (and last) linked list element
1813 _bytes_pool->byte=byte; //initialize byte to current one
1814 _bytes_pool->freq=1; //it appeared once
1815 _bytes_pool->next=0; //now this is last element in ll
1817 // Do update of linked list variables and memory use
1819 ++_bytes_pool; //next time use next entry (this is a pointer)
1821 if(_bytes_pool==_bytes_pool_max) //maximum reached
1824 // First check to see that we still have entries in the array
1826 if(_bytes_pool_index==_mempool_max_index)
1828 ppmc_out_of_memory=1; //flush
1832 // Allocate memory for new buffer
1834 if((_bytes_pool=(struct _byte_and_freq *)malloc
1835 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1837 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1838 ppmc_out_of_memory=1; //flush
1842 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1844 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1846 _bytes_pool_index++;
1850 return; //nothing else to do
1856 // The byte was coded under order three, otherwise it was coded under a
1857 // lower order (never higher ones, remember that we are using update
1858 // exclusion) in this case we have to create a new node in the list.
1860 if(coded_in_order==3) //coded under order-3
1863 // Update its count and variables of this context and check for renormalization
1865 o3_ll_node->freq++; //increment its frequency (rather probability)
1867 o3_context->max_cump++; //total cump
1869 if(o3_ll_node->freq==255) //do we have to renormalize?
1870 ppmc_renormalize_order3(); //renormalize
1876 // Now we have two cases, under a given context (which we actually found)
1877 // we coded an escape coded, in that case just create a new node in the
1878 // linked list of bytes and probabilities. Otherwise we didn't find the
1879 // same node so we have to create it in the linked list for context.
1880 // And we can be sure that it at least has one element and that
1881 // "o3_context" points to the last element, so we can put the new element.
1883 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
1884 { //element or a context found
1886 // However due to the use of exclusion, we have to ensure that "o3_ll_node"
1887 // points to the last element in the linked lists of this context
1889 node=o3_context->prob; //get pointer to linked list
1893 if(node->next==0) //check for the end of the linked list
1895 node=node->next; //next node
1900 if(ppmc_out_of_memory==1)
1901 return; //exit this function, we can't allocate mem
1903 o3_ll_node->next=_bytes_pool; //put it in the next free entry
1904 _bytes_pool->byte=byte; //initialize byte to current one
1905 _bytes_pool->freq=1; //it appeared once
1906 _bytes_pool->next=0; //now this is last element in ll
1908 o3_context->max_cump++; //total cump
1909 o3_context->defined_bytes++; //total cump
1911 // Do update of linked list variables and memory use
1913 ++_bytes_pool; //next time use next entry (this is a pointer)
1915 if(_bytes_pool==_bytes_pool_max) //maximum reached
1918 // First check to see that we still have entries in the array
1920 if(_bytes_pool_index==_mempool_max_index)
1922 ppmc_out_of_memory=1; //flush
1926 // Allocate memory for new buffer
1928 if((_bytes_pool=(struct _byte_and_freq *)malloc
1929 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1931 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1932 ppmc_out_of_memory=1; //flush
1936 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1938 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1940 _bytes_pool_index++;
1947 // Ensure that we are at the end of the linked list of contexts
1948 o3_context=order3_hasht[o3_cntxt];
1951 if(o3_context->next==0)
1953 o3_context=o3_context->next;
1956 // We have to create a new context node
1958 if(ppmc_out_of_memory==1)
1959 return; //exit this function, we can't allocate memory
1962 // First create the context
1964 o3_context->next=_context_pool;
1966 _context_pool->next=0; //this is the last element
1967 _context_pool->order4321=full_o3_cntxt; //put context
1968 _context_pool->prob=_bytes_pool; //pointer to linked list
1969 _context_pool->max_cump=1;
1970 _context_pool->defined_bytes=1;
1973 // Do update of linked list variables and memory use of contexts
1975 ++_context_pool; //next time use next entry (this is a pointer)
1977 if(_context_pool==_context_pool_max) //maximum reached
1980 // First check to see that we still have entries in the array
1982 if(_context_pool_index==_mempool_max_index)
1984 ppmc_out_of_memory=1; //flush
1988 // Allocate memory for new buffer
1990 if((_context_pool=(struct context *)malloc
1991 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1993 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1994 ppmc_out_of_memory=1; //flush
1998 _context_pool_array[_context_pool_index]=_context_pool;
2000 _context_pool_max=_context_pool+_context_pool_elements_inc;
2002 _context_pool_index++;
2006 // Now care about the first (and last) linked list element
2008 _bytes_pool->byte=byte; //initialize byte to current one
2009 _bytes_pool->freq=1; //it appeared once
2010 _bytes_pool->next=0; //now this is last element in ll
2012 // Do update of linked list variables and memory use
2014 ++_bytes_pool; //next time use next entry (this is a pointer)
2016 if(_bytes_pool==_bytes_pool_max) //maximum reached
2019 // First check to see that we still have entries in the array
2021 if(_bytes_pool_index==_mempool_max_index)
2023 ppmc_out_of_memory=1; //flush
2027 // Allocate memory for new buffer
2029 if((_bytes_pool=(struct _byte_and_freq *)malloc
2030 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2032 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2033 ppmc_out_of_memory=1; //flush
2037 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2039 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2041 _bytes_pool_index++;
2052 // This functions renormalizes the probabilities at order-3 updating context
2054 void ppmc_renormalize_order3(void)
2056 unsigned long counter;
2057 struct _byte_and_freq *node;
2060 // Initialize variables. Defined bytes remain the same.
2061 o3_context->max_cump=0; //clear them
2063 node=o3_context->prob; //get pointer to lined lists
2065 // Divide all the probabilities by 2 and update context variables
2068 node->freq>>=1; //divide by a factor of 2
2070 if(node->freq==0) //don't allow a probability to be 0
2073 o3_context->max_cump+=node->freq; //sum to the total cump
2075 if(node->next==0) //last element
2084 // Decodes the symbol correspoding to the current order, it returns the byte
2085 // or in case of an escape code or empty table it returns -1.
2086 // It updates "coded_in_order".
2088 // If we decode an escape code, we don't modify "o3_ll_node" and thus its
2089 // work of the updating routine to read till the end of the linked list
2090 // (for adding a new element) however if we decode a byte, we save on
2091 // "o3_ll_node" the pointer to its node. (so updating is faster)
2092 void ppmc_decode_order3(void)
2094 unsigned long current_cump, counter;
2095 struct _byte_and_freq *node;
2098 // Get current context (if present) and total cump.
2100 if(ppmc_get_totf_order3()==0)
2106 // It's possible that due to exclusion, there's no byte left, in that case
2108 if(exc_defined_bytes==0)
2116 // Decode current cump
2118 symb_cump=range_decoder_decode(&rc_decoder,exc_total_cump); //decode it
2120 // Now check if it's an escape code
2121 if(symb_cump>=exc_max_cump) //the defined code space for the escape code
2124 // Update coding values
2126 ppmc_get_escape_prob_order3();
2127 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
2129 // Mark as escape code
2133 // Then, update "excluded" table
2135 node=o3_context->prob;
2138 excluded[node->byte]=1;
2141 node=node->next; //next element in the linked list
2144 return; //an escape code
2148 // Now we have to check what symbol it is
2150 current_cump=0; //cump of the current symbol
2152 node=o3_context->prob; //get pointer to linked lists
2156 if(excluded[node->byte]==0)
2157 current_cump+=node->freq; //update cump
2158 if(symb_cump<current_cump)
2159 break; //symbol found, break search loop
2161 node=node->next; //next element
2162 //we have no need to check for the last symbol, we'll never read further
2163 //the end of the linked lists, before we'll found the last byte.
2167 //read byte value and probability
2169 symb_prob=node->freq; //get the probability for updating the state
2170 byte=node->byte; //get byte
2171 o3_ll_node=node; //used for updating
2174 // Get the cump of byte
2176 symb_cump=current_cump-symb_prob;
2178 // Update coding state
2180 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
2182 // Update coded_in_order used for update exclusion
2192 // This is the routine for updating while decoding. The only difference with
2193 // the routine for coding is that when an escape code was coded, "o3_ll_node"
2194 // is not initialized so we have to read till the end of the linked list.
2195 // Fortunately "o3_context" will be initialized so we don't need to read its
2197 void ppmc_update_dec_order3(void)
2199 struct _byte_and_freq *node;
2201 // First thing first, check if the hash entry is initialized
2203 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
2206 if(ppmc_out_of_memory==1)
2207 return; //exit this function, we can't allocate memory
2210 // First create the context
2212 order3_hasht[o3_cntxt]=_context_pool;
2214 _context_pool->next=0; //this is the last element
2215 _context_pool->order4321=full_o3_cntxt; //put context
2216 _context_pool->prob=_bytes_pool; //pointer to linked list
2217 _context_pool->max_cump=1;
2218 _context_pool->defined_bytes=1;
2221 // Do update of linked list variables and memory use of contexts
2223 ++_context_pool; //next time use next entry (this is a pointer)
2225 if(_context_pool==_context_pool_max) //maximum reached
2228 // First check to see that we still have entries in the array
2230 if(_context_pool_index==_mempool_max_index)
2232 ppmc_out_of_memory=1; //flush
2236 // Allocate memory for new buffer
2238 if((_context_pool=(struct context *)malloc
2239 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2241 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2242 ppmc_out_of_memory=1; //flush
2246 _context_pool_array[_context_pool_index]=_context_pool;
2248 _context_pool_max=_context_pool+_context_pool_elements_inc;
2250 _context_pool_index++;
2254 // Now care about the first (and last) linked list element
2256 _bytes_pool->byte=byte; //initialize byte to current one
2257 _bytes_pool->freq=1; //it appeared once
2258 _bytes_pool->next=0; //now this is last element in ll
2260 // Do update of linked list variables and memory use
2262 ++_bytes_pool; //next time use next entry (this is a pointer)
2264 if(_bytes_pool==_bytes_pool_max) //maximum reached
2267 // First check to see that we still have entries in the array
2269 if(_bytes_pool_index==_mempool_max_index)
2271 ppmc_out_of_memory=1; //flush
2275 // Allocate memory for new buffer
2277 if((_bytes_pool=(struct _byte_and_freq *)malloc
2278 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2280 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2281 ppmc_out_of_memory=1; //flush
2285 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2287 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2289 _bytes_pool_index++;
2293 return; //nothing else to do
2297 // The byte was coded under order three, otherwise it was coded under a
2298 // lower order (never higher ones, remember that we are using update
2299 // exclusion) in this case we have to create a new node in the list.
2301 if(coded_in_order==3) //coded under order-3
2304 // Update its count and variables of this context and check for renormalization
2306 o3_ll_node->freq++; //increment its frequency (rather probability)
2308 o3_context->max_cump++; //total cump
2310 if(o3_ll_node->freq==255) //do we have to renormalize?
2311 ppmc_renormalize_order3(); //renormalize
2317 // Now we have two cases, under a given context (which we actually found)
2318 // we coded an escape coded, in that case just create a new node in the
2319 // linked list of bytes and probabilities. Otherwise we didn't find the
2320 // same node so we have to create it in the linked list for context.
2321 // And we can be sure that it at least has one element and that
2322 // "o3_context" points to the last element, so we can put the new element.
2324 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
2325 { //element or the a context found
2327 // Read till the end of the linked list
2329 node=o3_context->prob; //get pointer to linked list
2333 if(node->next==0) //check for the end of the linked list
2335 node=node->next; //next node
2340 if(ppmc_out_of_memory==1)
2341 return; //exit this function, we can't allocate mem
2343 node->next=_bytes_pool; //put it in the next free entry
2344 _bytes_pool->byte=byte; //initialize byte to current one
2345 _bytes_pool->freq=1; //it appeared once
2346 _bytes_pool->next=0; //now this is last element in ll
2348 o3_context->max_cump++; //total cump
2349 o3_context->defined_bytes++; //total cump
2351 // Do update of linked list variables and memory use
2353 ++_bytes_pool; //next time use next entry (this is a pointer)
2355 if(_bytes_pool==_bytes_pool_max) //maximum reached
2358 // First check to see that we still have entries in the array
2360 if(_bytes_pool_index==_mempool_max_index)
2362 ppmc_out_of_memory=1; //flush
2366 // Allocate memory for new buffer
2368 if((_bytes_pool=(struct _byte_and_freq *)malloc
2369 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2371 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2372 ppmc_out_of_memory=1; //flush
2376 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2378 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2380 _bytes_pool_index++;
2387 // We have to create a new context node
2389 if(ppmc_out_of_memory==1)
2390 return; //exit this function, we can't allocate memory
2393 // First create the context
2395 o3_context->next=_context_pool;
2397 _context_pool->next=0; //this is the last element
2398 _context_pool->order4321=full_o3_cntxt; //put context
2399 _context_pool->prob=_bytes_pool; //pointer to linked list
2400 _context_pool->max_cump=1;
2401 _context_pool->defined_bytes=1;
2404 // Do update of linked list variables and memory use of contexts
2406 ++_context_pool; //next time use next entry (this is a pointer)
2408 if(_context_pool==_context_pool_max) //maximum reached
2411 // First check to see that we still have entries in the array
2413 if(_context_pool_index==_mempool_max_index)
2415 ppmc_out_of_memory=1; //flush
2419 // Allocate memory for new buffer
2421 if((_context_pool=(struct context *)malloc
2422 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2424 printf("Can't allocate memory for context's memory pool.");
2428 _context_pool_array[_context_pool_index]=_context_pool;
2430 _context_pool_max=_context_pool+_context_pool_elements_inc;
2432 _context_pool_index++;
2436 // Now care about the first (and last) linked list element
2438 _bytes_pool->byte=byte; //initialize byte to current one
2439 _bytes_pool->freq=1; //it appeared once
2440 _bytes_pool->next=0; //now this is last element in ll
2442 // Do update of linked list variables and memory use
2444 ++_bytes_pool; //next time use next entry (this is a pointer)
2446 if(_bytes_pool==_bytes_pool_max) //maximum reached
2449 // First check to see that we still have entries in the array
2451 if(_bytes_pool_index==_mempool_max_index)
2453 ppmc_out_of_memory=1; //flush
2457 // Allocate memory for new buffer
2459 if((_bytes_pool=(struct _byte_and_freq *)malloc
2460 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2462 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2463 ppmc_out_of_memory=1; //flush
2467 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2469 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2471 _bytes_pool_index++;
2483 // This function returns the probability for the escape codes in the variables
2484 void ppmc_get_escape_prob_order3(void)
2486 // To understand that remember that the escape allocated for the escape code
2487 // is above the current maximum cump and that it has a probability determined
2490 symb_prob=exc_defined_bytes;
2491 symb_cump=exc_max_cump;
2496 // ORDER-4 functions
2498 // The routines for order-4 are *equal* to those for order-3, there are a few
2499 // changes like different global variables, and different hash keys.
2501 // If you want to go to higher orders, you'd use the same code and data
2502 // structures, with the difference of the context bytes (order4321)
2503 // stored in every context's linked list.
2506 // Returns in the variable "total_cump" the current total cump of
2507 // order-4. Must be called while encoding or decoding before anything else
2508 // because it initializes the pointers to the context structure in
2509 // "o4_context" and o4_cntxt.
2511 // If the hash entry is not initialized it returns "o4_context"=0
2512 // If the context is not present in the linked list of context, "o4_context"
2513 // will point to the last element in the linked list.
2514 // If the context is present "o4_context" will point to the context to use.
2515 // One can distinguish the last two by checking the context value of the
2516 // structure, if it's not the same, is the last element.
2518 // The routine returns 0 if the hash entry is not initialized or if the
2519 // the context was not present. Otherwise it returns 1, meaning that we
2520 // have to code under this context.
2521 char ppmc_get_totf_order4(void)
2523 struct context *cntxt_node;
2524 struct _byte_and_freq *node;
2527 // First make the hash key for order-4
2529 o4_cntxt=ppmc_order4_hash_key(o1_byte,o2_byte,o3_byte,o4_byte);
2530 full_o4_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16)+(o4_byte<<24); //order-4
2533 // Now check the hash entry in the table
2535 if(order4_hasht[o4_cntxt]==0) //if 0, not initialized
2538 o4_context=0; //no hash entry
2540 return 0; //hash entry not initialized
2544 // Now read trough the linked list of context searching current one
2546 cntxt_node=order4_hasht[o4_cntxt];
2551 if(cntxt_node->order4321==full_o4_cntxt) //compare context
2552 goto ppmc_gtfo4_cntxt_found;
2554 if(cntxt_node->next==0) //end of context's linked list
2557 cntxt_node=cntxt_node->next; //next element
2562 // Once there the context was not found
2563 o4_context=cntxt_node; //pointer to last element in the linked list
2565 return 0; //it was not present
2568 // The context is found, so return pointer and cump
2570 ppmc_gtfo4_cntxt_found:
2572 o4_context=cntxt_node;
2574 // Read the whole linked list for making the values
2575 node=o4_context->prob;
2577 exc_defined_bytes=0;
2580 if(excluded[node->byte]==0)
2582 exc_defined_bytes++;
2583 exc_max_cump+=node->freq; //add the probability of this byte to the cump
2587 node=node->next; //next element in the linked list
2591 // Total cump is current total cump plus the probability for the escape code
2592 exc_total_cump=exc_max_cump+exc_defined_bytes;
2595 return 1; //context found
2600 // Codes a byte under order-4 and returns 1.
2601 // Otherwise it returns a 0.
2603 // In case the byte is coded under this context, coded_in_order=4.
2604 char ppmc_code_byte_order4(void)
2606 unsigned long counter;
2607 struct _byte_and_freq *node;
2610 // Get current context (if present) and total cump.
2612 if(ppmc_get_totf_order4()==0)
2616 // It's possible that due to exclusion, there's no byte left, in that case
2618 if(exc_defined_bytes==0)
2622 // See if the byte is present and compute its cump at the same time
2624 node=o4_context->prob; //pointer to first element in the linked list
2626 symb_cump=0; //the first symbol always has a 0 cump
2629 // Now search the byte in the linked list
2632 if(node->byte==byte)
2633 goto ppmc_o4_byte_found; //bad thing, I know, anyone has a better idea?
2634 if(excluded[node->byte]==0)
2635 symb_cump+=node->freq; //add the probability of this byte to the cump
2638 node=node->next; //next element in the linked list
2642 // If we reach that point, the byte was not found in the linked list
2643 // so we don't need the cump, we have to output an escape code, whose
2644 // probabilities are know using the context structure in the table.
2646 // Byte was not present in the linked list, current node is the last one,
2647 // and that's the node needed for creating a new node, save it.
2651 // Now get the probability and cump of the escape code
2653 symb_cump=exc_max_cump;
2654 symb_prob=exc_defined_bytes;
2656 // Code the escape code
2657 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
2659 // Then, update "excluded" table
2661 node=o4_context->prob;
2664 excluded[node->byte]=1;
2667 node=node->next; //next element in the linked list
2673 // That code is executed when the byte is found in the linked list
2678 // Everything has been tested, now we can feel free to code the byte,
2679 // the symb_cump is already computed, now get its probability and code
2680 // the byte, also save pointer to this element in the linked lists for
2683 coded_in_order=4; //successfully coded under order-4
2685 o4_ll_node=node; //save it for updating
2687 symb_prob=node->freq; //get the probability of the byte
2691 range_coder_encode(&rc_coder,exc_total_cump,symb_cump,symb_prob);
2693 return 1; //byte coded under order-4
2697 // This functions update order-4 probabilities with current byte, also takes
2698 // care about updating variables, and renormalizing.
2700 // "o4_ll_node" must point to the element to update or the last one,
2701 // based on the "coded_in_order" and checking the pointer of the element we
2702 // can decide what to do. Also "o4_context" must be initialized.
2704 // This updating is only for encoding.
2705 void ppmc_update_order4(void)
2708 // First thing first, check if the hash entry is initialized
2710 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
2713 if(ppmc_out_of_memory==1)
2714 return; //exit this function, we can't allocate memory
2717 // First create the context
2719 order4_hasht[o4_cntxt]=_context_pool;
2721 _context_pool->next=0; //this is the last element
2722 _context_pool->order4321=full_o4_cntxt; //put context
2723 _context_pool->prob=_bytes_pool; //pointer to linked list
2724 _context_pool->max_cump=1;
2725 _context_pool->defined_bytes=1;
2728 // Do update of linked list variables and memory use of contexts
2730 ++_context_pool; //next time use next entry (this is a pointer)
2732 if(_context_pool==_context_pool_max) //maximum reached
2735 // First check to see that we still have entries in the array
2737 if(_context_pool_index==_mempool_max_index)
2739 ppmc_out_of_memory=1; //flush
2743 // Allocate memory for new buffer
2745 if((_context_pool=(struct context *)malloc
2746 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2748 printf("Can't allocate memory for context's memory pool.");
2752 _context_pool_array[_context_pool_index]=_context_pool;
2754 _context_pool_max=_context_pool+_context_pool_elements_inc;
2756 _context_pool_index++;
2761 // Now care about the first (and last) linked list element
2763 _bytes_pool->byte=byte; //initialize byte to current one
2764 _bytes_pool->freq=1; //it appeared once
2765 _bytes_pool->next=0; //now this is last element in ll
2767 // Do update of linked list variables and memory use
2769 ++_bytes_pool; //next time use next entry (this is a pointer)
2771 if(_bytes_pool==_bytes_pool_max) //maximum reached
2774 // First check to see that we still have entries in the array
2776 if(_bytes_pool_index==_mempool_max_index)
2778 ppmc_out_of_memory=1; //flush
2782 // Allocate memory for new buffer
2784 if((_bytes_pool=(struct _byte_and_freq *)malloc
2785 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2787 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2788 ppmc_out_of_memory=1; //flush
2792 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2794 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2796 _bytes_pool_index++;
2800 return; //nothing else to do
2806 // The byte was coded under order three, otherwise it was coded under a
2807 // lower order (never higher ones, remember that we are using update
2808 // exclusion) in this case we have to create a new node in the list.
2810 if(coded_in_order==4) //coded under order-4
2813 // Update its count and variables of this context and check for renormalization
2815 o4_ll_node->freq++; //increment its frequency (rather probability)
2817 o4_context->max_cump++; //total cump
2819 if(o4_ll_node->freq==255) //do we have to renormalize?
2820 ppmc_renormalize_order4(); //renormalize
2826 // Now we have two cases, under a given context (which we actually found)
2827 // we coded an escape coded, in that case just create a new node in the
2828 // linked list of bytes and probabilities. Otherwise we didn't find the
2829 // same node so we have to create it in the linked list for context.
2830 // And we can be sure that it at least has one element and that
2831 // "o4_context" points to the last element, so we can put the new element.
2833 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
2834 { //element or the a context found
2836 if(ppmc_out_of_memory==1)
2837 return; //exit this function, we can't allocate mem
2839 o4_ll_node->next=_bytes_pool; //put it in the next free entry
2840 _bytes_pool->byte=byte; //initialize byte to current one
2841 _bytes_pool->freq=1; //it appeared once
2842 _bytes_pool->next=0; //now this is last element in ll
2844 o4_context->max_cump++; //total cump
2845 o4_context->defined_bytes++; //total cump
2847 // Do update of linked list variables and memory use
2849 ++_bytes_pool; //next time use next entry (this is a pointer)
2851 if(_bytes_pool==_bytes_pool_max) //maximum reached
2854 // First check to see that we still have entries in the array
2856 if(_bytes_pool_index==_mempool_max_index)
2858 ppmc_out_of_memory=1; //flush
2862 // Allocate memory for new buffer
2864 if((_bytes_pool=(struct _byte_and_freq *)malloc
2865 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2867 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2868 ppmc_out_of_memory=1; //flush
2872 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2874 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2876 _bytes_pool_index++;
2883 // We have to create a new context node
2885 if(ppmc_out_of_memory==1)
2886 return; //exit this function, we can't allocate memory
2889 // First create the context
2891 o4_context->next=_context_pool;
2893 _context_pool->next=0; //this is the last element
2894 _context_pool->order4321=full_o4_cntxt; //put context
2895 _context_pool->prob=_bytes_pool; //pointer to linked list
2896 _context_pool->max_cump=1;
2897 _context_pool->defined_bytes=1;
2900 // Do update of linked list variables and memory use of contexts
2902 ++_context_pool; //next time use next entry (this is a pointer)
2904 if(_context_pool==_context_pool_max) //maximum reached
2907 // First check to see that we still have entries in the array
2909 if(_context_pool_index==_mempool_max_index)
2911 ppmc_out_of_memory=1; //flush
2915 // Allocate memory for new buffer
2917 if((_context_pool=(struct context *)malloc
2918 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2920 printf("Can't allocate memory for context's memory pool.");
2924 _context_pool_array[_context_pool_index]=_context_pool;
2926 _context_pool_max=_context_pool+_context_pool_elements_inc;
2928 _context_pool_index++;
2932 // Now care about the first (and last) linked list element
2934 _bytes_pool->byte=byte; //initialize byte to current one
2935 _bytes_pool->freq=1; //it appeared once
2936 _bytes_pool->next=0; //now this is last element in ll
2938 // Do update of linked list variables and memory use
2940 ++_bytes_pool; //next time use next entry (this is a pointer)
2942 if(_bytes_pool==_bytes_pool_max) //maximum reached
2945 // First check to see that we still have entries in the array
2947 if(_bytes_pool_index==_mempool_max_index)
2949 ppmc_out_of_memory=1; //flush
2953 // Allocate memory for new buffer
2955 if((_bytes_pool=(struct _byte_and_freq *)malloc
2956 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2958 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2959 ppmc_out_of_memory=1; //flush
2963 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2965 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2967 _bytes_pool_index++;
2978 // This functions renormalizes the probabilities at order-4 updating context
2980 void ppmc_renormalize_order4(void)
2982 unsigned long counter;
2983 struct _byte_and_freq *node;
2986 // Initialize variables. Defined bytes remain the same.
2987 o4_context->max_cump=0; //clear them
2989 node=o4_context->prob; //get pointer to lined lists
2991 // Divide all the probabilities by 2 and update context variables
2994 node->freq>>=1; //divide by a factor of 2
2996 if(node->freq==0) //don't allow a probability to be 0
2999 o4_context->max_cump+=node->freq; //sum to the total cump
3001 if(node->next==0) //last element
3010 // Decodes the symbol correspoding to the current order, it returns the byte
3011 // or in case of an escape code or empty table it returns -1.
3012 // It updates "coded_in_order".
3014 // If we decode an escape code, we don't modify "o4_ll_node" and thus its
3015 // work of the updating routine to read till the end of the linked list
3016 // (for adding a new element) however if we decode a byte, we save on
3017 // "o4_ll_node" the pointer to its node. (so updating is faster)
3018 void ppmc_decode_order4(void)
3020 unsigned long current_cump, counter;
3021 struct _byte_and_freq *node;
3024 // Get current context (if present) and total cump.
3026 if(ppmc_get_totf_order4()==0)
3033 // It's possible that due to exclusion, there's no byte left, in that case
3035 if(exc_defined_bytes==0)
3042 // Decode current cump
3044 symb_cump=range_decoder_decode(&rc_decoder,exc_total_cump); //decode it
3046 // Now check if it's an escape code
3047 if(symb_cump>=exc_max_cump) //the defined code space for the escape code
3050 // Update coding values
3052 ppmc_get_escape_prob_order4();
3053 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
3055 // Mark as escape code
3059 // Then, update "excluded" table
3061 node=o4_context->prob;
3064 excluded[node->byte]=1;
3067 node=node->next; //next element in the linked list
3070 return; //an escape code
3074 // Now we have to check what symbol it is
3076 current_cump=0; //cump of the current symbol
3078 node=o4_context->prob; //get pointer to linked lists
3082 if(excluded[node->byte]==0)
3083 current_cump+=node->freq; //update cump
3084 if(symb_cump<current_cump)
3085 break; //symbol found, break search loop
3087 node=node->next; //next element
3088 //we have no need to check for the last symbol, we'll never read further
3089 //the end of the linked lists, before we'll found the last byte.
3093 //read byte value and probability
3095 symb_prob=node->freq; //get the probability for updating the state
3096 byte=node->byte; //get byte
3097 o4_ll_node=node; //used for updating
3100 // Get the cump of byte
3102 symb_cump=current_cump-symb_prob;
3104 // Update coding state
3106 range_decoder_update(&rc_decoder,exc_total_cump,symb_cump,symb_prob);
3108 // Update coded_in_order used for update exclusion
3118 // This is the routine for updating while decoding. The only difference with
3119 // the routine for coding is that when an escape code was coded, "o4_ll_node"
3120 // is not initialized so we have to read till the end of the linked list.
3121 // Fortunately "o4_context" will be initialized so we don't need to read its
3123 void ppmc_update_dec_order4(void)
3125 struct _byte_and_freq *node;
3127 // First thing first, check if the hash entry is initialized
3129 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
3132 if(ppmc_out_of_memory==1)
3133 return; //exit this function, we can't allocate memory
3136 // First create the context
3138 order4_hasht[o4_cntxt]=_context_pool;
3140 _context_pool->next=0; //this is the last element
3141 _context_pool->order4321=full_o4_cntxt; //put context
3142 _context_pool->prob=_bytes_pool; //pointer to linked list
3143 _context_pool->max_cump=1;
3144 _context_pool->defined_bytes=1;
3147 // Do update of linked list variables and memory use of contexts
3149 ++_context_pool; //next time use next entry (this is a pointer)
3151 if(_context_pool==_context_pool_max) //maximum reached
3154 // First check to see that we still have entries in the array
3156 if(_context_pool_index==_mempool_max_index)
3158 ppmc_out_of_memory=1; //flush
3162 // Allocate memory for new buffer
3164 if((_context_pool=(struct context *)malloc
3165 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
3167 printf("Can't allocate memory for context's memory pool.");
3171 _context_pool_array[_context_pool_index]=_context_pool;
3173 _context_pool_max=_context_pool+_context_pool_elements_inc;
3175 _context_pool_index++;
3179 // Now care about the first (and last) linked list element
3181 _bytes_pool->byte=byte; //initialize byte to current one
3182 _bytes_pool->freq=1; //it appeared once
3183 _bytes_pool->next=0; //now this is last element in ll
3185 // Do update of linked list variables and memory use
3187 ++_bytes_pool; //next time use next entry (this is a pointer)
3189 if(_bytes_pool==_bytes_pool_max) //maximum reached
3192 // First check to see that we still have entries in the array
3194 if(_bytes_pool_index==_mempool_max_index)
3196 ppmc_out_of_memory=1; //flush
3200 // Allocate memory for new buffer
3202 if((_bytes_pool=(struct _byte_and_freq *)malloc
3203 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
3205 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3206 ppmc_out_of_memory=1; //flush
3210 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3212 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3214 _bytes_pool_index++;
3218 return; //nothing else to do
3222 // The byte was coded under order four, otherwise it was coded under a
3223 // lower order (never higher ones, remember that we are using update
3224 // exclusion) in this case we have to create a new node in the list.
3226 if(coded_in_order==4) //coded under order-4
3229 // Update its count and variables of this context and check for renormalization
3231 o4_ll_node->freq++; //increment its frequency (rather probability)
3233 o4_context->max_cump++; //total cump
3235 if(o4_ll_node->freq==255) //do we have to renormalize?
3236 ppmc_renormalize_order4(); //renormalize
3242 // Now we have two cases, under a given context (which we actually found)
3243 // we coded an escape coded, in that case just create a new node in the
3244 // linked list of bytes and probabilities. Otherwise we didn't find the
3245 // same node so we have to create it in the linked list for context.
3246 // And we can be sure that it at least has one element and that
3247 // "o4_context" points to the last element, so we can put the new element.
3249 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
3250 { //element or the a context found
3252 // Read till the end of the linked list
3254 node=o4_context->prob; //get pointer to linked list
3258 if(node->next==0) //check for the end of the linked list
3260 node=node->next; //next node
3265 if(ppmc_out_of_memory==1)
3266 return; //exit this function, we can't allocate mem
3268 node->next=_bytes_pool; //put it in the next free entry
3269 _bytes_pool->byte=byte; //initialize byte to current one
3270 _bytes_pool->freq=1; //it appeared once
3271 _bytes_pool->next=0; //now this is last element in ll
3273 o4_context->max_cump++; //total cump
3274 o4_context->defined_bytes++; //total cump
3276 // Do update of linked list variables and memory use
3278 ++_bytes_pool; //next time use next entry (this is a pointer)
3280 if(_bytes_pool==_bytes_pool_max) //maximum reached
3283 // First check to see that we still have entries in the array
3285 if(_bytes_pool_index==_mempool_max_index)
3287 ppmc_out_of_memory=1; //flush
3291 // Allocate memory for new buffer
3293 if((_bytes_pool=(struct _byte_and_freq *)malloc
3294 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
3296 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3297 ppmc_out_of_memory=1; //flush
3301 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3303 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3305 _bytes_pool_index++;
3312 // We have to create a new context node
3314 if(ppmc_out_of_memory==1)
3315 return; //exit this function, we can't allocate memory
3318 // First create the context
3320 o4_context->next=_context_pool;
3322 _context_pool->next=0; //this is the last element
3323 _context_pool->order4321=full_o4_cntxt; //put context
3324 _context_pool->prob=_bytes_pool; //pointer to linked list
3325 _context_pool->max_cump=1;
3326 _context_pool->defined_bytes=1;
3329 // Do update of linked list variables and memory use of contexts
3331 ++_context_pool; //next time use next entry (this is a pointer)
3333 if(_context_pool==_context_pool_max) //maximum reached
3336 // First check to see that we still have entries in the array
3338 if(_context_pool_index==_mempool_max_index)
3340 ppmc_out_of_memory=1; //flush
3344 // Allocate memory for new buffer
3346 if((_context_pool=(struct context *)malloc
3347 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
3349 printf("Can't allocate memory for context's memory pool.");
3353 _context_pool_array[_context_pool_index]=_context_pool;
3355 _context_pool_max=_context_pool+_context_pool_elements_inc;
3357 _context_pool_index++;
3361 // Now care about the first (and last) linked list element
3363 _bytes_pool->byte=byte; //initialize byte to current one
3364 _bytes_pool->freq=1; //it appeared once
3365 _bytes_pool->next=0; //now this is last element in ll
3367 // Do update of linked list variables and memory use
3369 ++_bytes_pool; //next time use next entry (this is a pointer)
3371 if(_bytes_pool==_bytes_pool_max) //maximum reached
3374 // First check to see that we still have entries in the array
3376 if(_bytes_pool_index==_mempool_max_index)
3378 ppmc_out_of_memory=1; //flush
3382 // Allocate memory for new buffer
3384 if((_bytes_pool=(struct _byte_and_freq *)malloc
3385 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
3387 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3388 ppmc_out_of_memory=1; //flush
3392 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3394 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3396 _bytes_pool_index++;
3408 // This function returns the probability for the escape codes in the variables
3409 void ppmc_get_escape_prob_order4(void)
3411 // To understand that remember that the escape allocated for the escape code
3412 // is above the current maximum cump and that it has a probability determined
3415 symb_prob=exc_defined_bytes;
3416 symb_cump=exc_max_cump;