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.
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
27 which both machines have.
36 static unsigned ordern1_table[] = {
37 100, 100, 100, 100, 100, 100, 100, 100,
38 100, 4303574, 88451153, 100, 100, 100, 100, 100,
39 100, 100, 100, 100, 100, 100, 100, 100,
40 100, 100, 100, 100, 100, 100, 100, 100,
41 143318, 9172380, 1146547, 100, 143318, 573273, 859910, 10318928,
42 10318928, 6019374, 573273, 26657231, 1576502, 43138853, 10462246, 3439642,
43 6162693, 6162693, 6162693, 6162693, 6162693, 6162693, 6162693, 6162693,
44 6162693, 6162693, 8742425, 4299553, 14188526, 3439642, 9888973, 429955,
45 143318, 27230505, 2436413, 22357678, 10892202, 40989076, 1576502, 1433184,
46 286636, 37406115, 573273, 286636, 19204672, 13901889, 19491309, 10462246,
47 16481621, 1003229, 21211130, 13615252, 17914806, 2866368, 4012916, 429955,
48 1719821, 1146547, 143318, 286636, 143318, 286636, 143318, 1576502,
49 143318, 315013952, 32246651, 165962764, 171122228, 418203235, 23790862, 26800550,
50 17054895, 230742703, 7452559, 1003229, 163669669, 75385504, 225153284, 261412851,
51 88140846, 24794091, 215837585, 223003507, 162523121, 110355206, 38122707, 5302782,
52 7595877, 18918035, 8885743, 859910, 100, 859910, 100, 100,
53 100, 100, 100, 100, 100, 100, 100, 100,
54 100, 100, 100, 100, 100, 100, 100, 100,
55 100, 100, 100, 100, 100, 100, 100, 100,
56 100, 100, 100, 100, 100, 100, 100, 100,
57 100, 100, 100, 100, 100, 100, 100, 100,
58 100, 100, 100, 100, 100, 100, 100, 100,
59 100, 100, 100, 100, 100, 100, 100, 100,
60 100, 100, 100, 100, 100, 100, 100, 100,
61 100, 100, 100, 100, 100, 100, 100, 100,
62 100, 100, 100, 100, 100, 100, 100, 100,
63 100, 100, 100, 100, 100, 100, 100, 100,
64 100, 100, 100, 100, 100, 100, 100, 100,
65 100, 100, 100, 100, 100, 100, 100, 100,
66 100, 100, 100, 100, 100, 100, 100, 100,
67 100, 100, 100, 100, 100, 100, 100, 100,
68 100, 100, 100, 100, 100, 100, 100, 100,
70 #define ordern1_total_cump 3651941255u
72 // Ruotines used by ppmc. Not including the range coder.
74 // They are for initializing of both encoder and decoder, and unless there
75 // are two version, both encoder and decoder use the same routines. Like
76 // "ppmc_initialize_contexts".
79 // This one allocs the memory needed by ppmc, and adjust some pointers used
80 // for allocating elements in the linked lists. The mempool arrays must be
82 void ppmc_alloc_memory(void)
84 unsigned long counter;
87 // Init mempool buffers
89 for(counter=0;counter!=_mempool_max_index;++counter)
91 _bytes_pool_array[counter]=0;
92 _context_pool_array[counter]=0;
95 _bytes_pool_index=1; //first entry will be used now
96 _context_pool_index=1;
99 // Allocate memory for ppmc structures and adjust some variables
100 if((_bytes_pool=(struct _byte_and_freq *)malloc
101 (sizeof(struct _byte_and_freq)*_bytes_pool_elements))==NULL)
103 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
104 ppmc_out_of_memory=1; //flush
108 if((_context_pool=(struct context *)malloc
109 (sizeof(struct context)*_context_pool_elements))==NULL)
111 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
112 ppmc_out_of_memory=1; //flush
117 //save pointers in the array for freeing
118 _bytes_pool_array[0]=_bytes_pool;
119 _context_pool_array[0]=_context_pool;
123 _bytes_pool_max=_bytes_pool+_bytes_pool_elements;
124 _context_pool_max=_context_pool+_context_pool_elements;
126 ppmc_out_of_memory=0; //we still have memory
130 // This routine initializes all the contexts, and all the tables including
131 // those who care about the number of bytes defined in a context.
132 void ppmc_initialize_contexts(void)
134 unsigned long counter, counter2;
138 for(counter=0;counter!=256;++counter) //clear table
139 order0_array[counter]=0;
141 order0_defined_bytes=0; //adjust variables
146 for(counter=0;counter!=256;++counter) //erase every table of every context
147 for(counter2=0;counter2!=256;++counter2)
148 order1_array[counter][counter2]=0;
150 for(counter=0;counter!=256;++counter) //adjust variables
152 order1_defined_bytes_array[counter]=0;
153 order1_max_cump_array[counter]=0;
158 for(counter=0;counter!=65536;++counter)
160 //order2_array[counter].prob=0; //clear pointer to bytes and frequencies
161 //order2_array[counter].max_cump=0;
162 order2_array[counter].defined_bytes=0;
167 for(counter=0;counter!=65536;++counter) //order-4-3
169 order4_hasht[counter]=0;
170 order3_hasht[counter]=0;
175 // This routine initializes the encode model by outputting as many bytes as
176 // needed to prepare the models. This should be called before the main loop
177 // and after the memory has been allocated and tables initialized.
179 // It does not need uses the range coder. It output the first 1 bytes.
180 void ppmc_encoder_initialize(void)
183 // Initialize order-0 and prepare different bytes for orders
184 fputc((byte=fgetc(file_input)),file_output);
185 o4_byte=byte; //order-4
187 fputc((byte=fgetc(file_input)),file_output);
188 o3_byte=byte; //order-3
190 fputc((byte=fgetc(file_input)),file_output);
191 o2_byte=byte; //order-2
192 ppmc_update_order0();
194 fputc((byte=fgetc(file_input)),file_output);
200 // This routine initializes the decoder model, should be called to do the same
201 // changes as "ppmc_encoder_initialize()" did.
202 void ppmc_decoder_initialize(void)
205 // Initialize order-0 and context bytes
206 byte=fgetc(file_input);
207 o4_byte=byte; //order-4
208 fputc(byte,file_output);
210 byte=fgetc(file_input);
211 o3_byte=byte; //order-3
212 fputc(byte,file_output);
214 byte=fgetc(file_input);
215 o2_byte=byte; //order-2
217 fputc(byte,file_output); //output first byte
218 ppmc_update_order0();
220 byte=fgetc(file_input);
221 o1_byte=byte; //order-1
222 fputc(byte,file_output);
226 // Once coding or decoding is finished you have to call this routine.
227 // It must be called when done.
228 void ppmc_free_memory(void)
230 unsigned long counter;
232 // Free the memory buffers
234 for(counter=0;counter!=_mempool_max_index;++counter)
236 if(_bytes_pool_array[counter]!=0)
237 free(_bytes_pool_array[counter]);
239 if(_context_pool_array[counter]!=0)
240 free(_context_pool_array[counter]);
246 // This routine flushes the memory and restarts all the tables of
247 // probabilities, current order bytes are not modified, this function
248 // is called when we ran out of memory. We have to output the code
249 // number 256 which means memory flushing, for doing this we have to go
250 // to order-(-1) so we have to output an escape code in all the orders
251 // till we reach order-(-1) where we can code it. Then we free all the
252 // memory, alloc it again, and reinitialize all the orders.
254 // However we may find the case when the current order is not initialized,
255 // in this case we don't need to output an escape code.
256 void ppmc_flush_mem_enc(void)
259 // Code an escape code in order-4
260 if(ppmc_get_totf_order4()!=0) //if 0 no need of escape code
263 ppmc_get_escape_prob_order4(); //get prob and cump
264 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
269 // Code an escape code in order-3
270 if(ppmc_get_totf_order3()!=0) //if 0 no need of escape code
273 ppmc_get_escape_prob_order3(); //get prob and cump
274 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
279 // Code an escape code in order-2
281 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
283 // First check if current order-2 context is empty
284 if(order2_array[o2_cntxt].defined_bytes!=0) //it's not empty
286 ppmc_get_totf_order2();
287 ppmc_get_escape_prob_order2();
288 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
292 // Code an escape code in order-1
294 // First check if current order-1 table is empty
295 if(order1_defined_bytes_array[o1_byte]!=0) //it's not empty
297 ppmc_get_totf_order1();
298 ppmc_get_escape_prob_order1();
299 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
303 // Code an escape code in order-0. Order-0 always has at least one symbol
305 ppmc_get_totf_order0();
306 ppmc_get_escape_prob_order0();
307 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
311 // Now we can code the code 256
316 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
319 // Now that decoder knows the flushing, free memory and reinit
323 ppmc_initialize_contexts();
326 // Be sure that order-0 has at least one probability
328 order0_array[o1_byte]++;
330 order0_defined_bytes++;
335 // When the decoder gets the symbol of flushing, most of the job is done
336 // because we already got all the escape codes, so we only have to reinit.
337 void ppmc_flush_mem_dec(void)
340 // Free memory and reinit
344 ppmc_initialize_contexts();
347 // Be sure that order-0 has at least one probability
349 order0_array[o1_byte]++;
351 order0_defined_bytes++;
358 // ORDER-(-1) functions, also called ordern1 (Negative1) in functions
360 // Because order-(-1) does not need to update its probability tables, it
361 // has no tables, and relies on the fact that the cump of byte is its own
362 // value, and the probability is fixed, 1, and the total cump is 257.
364 // The alphabet has the following distribution: 0-255 the bytes. 256 is
365 // an special symbol which means that we have flushed the encoder tables,
366 // and thus the encoder must flush its tables too.
368 // The rest of the tables only have 256 symbols, because we have no need
369 // of assign a symbol to the flush code (which already is the order-(-1)
370 // table) nor to the escape code.
373 // Gets the probability for a given symbol in the order-(-1) (ordern1)
374 void ppmc_get_prob_ordern1(void)
378 for (i = 0; i < 256 || i == byte; ++i)
379 acum += ordern1_table[i];
381 symb_prob=ordern1_table[i];
382 total_cump=ordern1_total_cump;
386 // Returns in the variable "total_cump" the current total cump of
388 void ppmc_get_totf_ordern1(void)
390 total_cump=ordern1_total_cump;
394 // Returns the symbol for a given cump under order-(-1)
395 unsigned long ppmc_get_symbol_ordern1 (void)
404 // Due to the fact that order-0 has no context, I use an array for all the
405 // probabilities under order-0, just as you could do in a basic model for
406 // arithmetic coding.
408 // The main array is: order0_array. Where order0_array[byte] contains the
409 // probability for a given byte. The same applies to order-1.
411 // To ensure that the updating and coding is done correctly, "byte" can't
412 // be changed till all the coding and updating is done.
415 // Returns in the variable "total_cump" the current total cump of
417 void ppmc_get_totf_order0(void)
419 // Total cump is current total cump plus the escape for the escape code
420 total_cump=order0_defined_bytes+order0_max_cump;
424 // Codes a byte under order-0 and returns 1, otherwise it returns a 0 and
425 // has coded an escape code. In this case further coding is needed.
427 // Returns: 1 in case a byte was coded. 0 in case of escape code.
428 char ppmc_code_byte_order0(void)
430 unsigned long counter;
432 ppmc_get_totf_order0(); //get total cump
434 // See if the byte is present
435 if(order0_array[byte]==0) //a probability of 0
438 // Because it was not present, output an escape code, prepare variables
440 symb_cump=order0_max_cump; //obviously its cump is current max_cump
441 //without escape code's space
443 symb_prob=order0_defined_bytes; //the number of defined bytes
445 // Code the escape code
446 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
448 return 0; //byte not coded
455 // The symbol is present, code it under order-0
457 symb_prob=order0_array[byte]; //get probability directly
459 // Make cump for current symbol
461 symb_cump=0; //for first symbol is 0
462 for(counter=0; counter!=byte ; ++counter)
463 symb_cump+=order0_array[counter]; //sum probabilities before our symbol
466 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
468 return 1; //symbol coded under order-0
473 // This functions update order-0 probabilities with current byte, also takes
474 // care about updating variables, and renormalizing.
475 void ppmc_update_order0(void)
477 if(order0_array[byte]==0)
479 // It had a zero probability
480 order0_array[byte]++; //increment symbol probability
481 ++order0_defined_bytes; //new byte defined
482 ++order0_max_cump; //total cump
487 // It had a non-zero probability
489 // Increment its probability
490 order0_array[byte]++; //increment symbol probability
491 ++order0_max_cump; //total cump
493 // Check to see if its the maximum in this case renormalize
494 if(order0_array[byte]==255)
495 ppmc_renormalize_order0();
502 // This functions renormalizes the probabilities at order-0 updating variables
503 void ppmc_renormalize_order0(void)
505 unsigned long counter;
507 // Initialize variables
508 order0_defined_bytes=0; //clear them
511 // Loop over all probabilities, divide them by a factor of 2 and update variables
512 for(counter=0 ; counter!=256 ; ++counter)
514 order0_array[counter]>>=1; //divide by a factor of 2
516 if(order0_array[counter]!=0) //see if it has a non zero probability
517 order0_defined_bytes++;
519 order0_max_cump+=order0_array[counter]; //sum to the total cump
524 // Decodes the symbol correspoding to the current order, it returns the byte
525 // or in case of a escape code it returns -1
526 void ppmc_decode_order0(void)
528 unsigned long current_cump, counter;
531 // Get the total cump needed for decoding symbol
532 ppmc_get_totf_order0(); //total cump needed for decoding
533 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
535 // Now check if it's an escape code
536 if(symb_cump>=order0_max_cump) //the defined code space for the escape code
539 // Update coding values
541 ppmc_get_escape_prob_order0();
542 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
544 // Mark as escape code
548 return; //an escape code
552 // Now we have to check what symbol it is
554 current_cump=0; //cump of the current symbol
556 for(counter=0 ; counter!= 256 ; ++counter)
558 if(symb_cump<current_cump)
559 break; //symbol found, break search loop
560 current_cump+=order0_array[counter]; //update cump
563 counter--; //-1 (see ac_ppmc.html, searching for a given symbol)
565 byte=counter; //update the variable with the found symbol
568 // Get the cump and prob of byte
570 symb_prob=order0_array[byte]; //the probabilty
571 symb_cump=current_cump-order0_array[byte]; //using the cump which was
574 // Update coding state
576 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
578 coded_in_order=0; //decoded under order-0
586 // This function returns the probability for the escape codes in the variables
587 void ppmc_get_escape_prob_order0(void)
589 // To understand that remember that the escape allocated for the escape code
590 // is above the current maximum cump and that it has a probability determined
592 symb_prob=order0_defined_bytes;
593 symb_cump=order0_max_cump;
594 total_cump=order0_defined_bytes+order0_max_cump;
601 // Order-1 uses 256 arrays one for every context, obviously they are accessed
602 // like that: order1_array[o1_byte][byte]
604 // Also for computing the value of the current escape code in a given context
605 // we need to know how many bytes are defined in a given context, they are
606 // stored in an array, and you use them like that:
607 // order1_defined_bytes_array[o1_byte]
609 // The same goes for the maximum cump of a given context:
610 // order1_max_cump_array[o1_byte]
612 // Read the order-0 explanation.
614 // To ensure that the updating and coding is done correctly, "byte" and
615 // "o1_byte" can't be changed till all the coding and updating is done.
618 // Returns in the variable "total_cump" the current total cump of
619 // order-1. o1_byte should contain the order-1
620 void ppmc_get_totf_order1(void)
622 // Total cump is current total cump plus the escape for the escape code
623 total_cump=order1_defined_bytes_array[o1_byte]+order1_max_cump_array[o1_byte];
627 // Codes a byte under order-1 and returns 1.
628 // Otherwise it returns a 0 it may be that it has coded an escape code, or
629 // that current table was empty, in this case nothing is outputted
630 // In both cases further coding is needed.
632 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
633 // In case the byte is coded under this context, coded_in_order=1.
634 char ppmc_code_byte_order1(void)
636 unsigned long counter;
639 // First check if current order-1 table is empty
640 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
642 return 0; //byte not coded, nothing done
646 // Now try to code this byte under order-1
648 ppmc_get_totf_order1(); //get total cump
650 // See if the byte is present
651 if(order1_array[o1_byte][byte]==0) //a probability of 0
654 // Because it was not present, output an escape code, prepare variables
656 symb_cump=order1_max_cump_array[o1_byte];//obviously its cump is current max_cump
657 //without escape code's space
659 symb_prob=order1_defined_bytes_array[o1_byte];//the number of defined bytes
661 // Code the escape code
662 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
664 return 0; //byte not coded, escape coded
669 coded_in_order=1; //because we coded it under order-1
671 // The symbol is present, code it under order-1
673 symb_prob=order1_array[o1_byte][byte]; //get probability directly
675 // Make cump for current symbol
677 symb_cump=0; //for first symbol is 0
678 for(counter=0; counter!=byte ; ++counter)
679 symb_cump+=order1_array[o1_byte][counter]; //sum probabilities before our symbol
682 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
684 return 1; //symbol coded under order-1
689 // This functions update order-1 probabilities with current byte, also takes
690 // care about updating variables, and renormalizing. o1_byte must be filled.
691 void ppmc_update_order1(void)
693 if(order1_array[o1_byte][byte]==0)
695 // It had a zero probability
696 order1_array[o1_byte][byte]++; //increment symbol probability
697 ++order1_defined_bytes_array[o1_byte]; //new byte defined
698 ++order1_max_cump_array[o1_byte]; //total cump
703 // It had a non-zero probability
705 // Increment its probability
706 order1_array[o1_byte][byte]++; //increment symbol probability
707 ++order1_max_cump_array[o1_byte]; //total cump
709 // Check to see if its the maximum in this case renormalize
710 if(order1_array[o1_byte][byte]==255)
711 ppmc_renormalize_order1();
718 // This functions renormalizes the probabilities at order-1 updating variables
719 void ppmc_renormalize_order1(void)
721 unsigned long counter;
723 // Initialize variables
724 order1_defined_bytes_array[o1_byte]=0; //clear them
725 order1_max_cump_array[o1_byte]=0;
727 // Loop over all probabilities, divide them by a factor of 2 and update variables
728 for(counter=0 ; counter!=256 ; ++counter)
730 order1_array[o1_byte][counter]>>=1; //divide by a factor of 2
732 if(order1_array[o1_byte][counter]!=0) //see if it has a non zero probability
733 order1_defined_bytes_array[o1_byte]++;
735 order1_max_cump_array[o1_byte]+=order1_array[o1_byte][counter]; //sum to the total cump
740 // Decodes the symbol correspoding to the current order, it returns the byte
741 // or in case of an escape code or empty table it returns -1.
742 // It updates "coded_in_order".
743 void ppmc_decode_order1(void)
745 unsigned long current_cump, counter;
748 // First check if current order-1 table is empty
749 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
751 byte=-1; //byte not coded, nothing done
756 // Get the total cump needed for decoding symbol
757 ppmc_get_totf_order1(); //total cump needed for decoding
758 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
760 // Now check if it's an escape code
761 if(symb_cump>=order1_max_cump_array[o1_byte]) //the defined code space for the escape code
764 // Update coding values
766 ppmc_get_escape_prob_order1();
767 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
769 // Mark as escape code
773 return; //an escape code
777 // Now we have to check what symbol it is
779 current_cump=0; //cump of the current symbol
781 for(counter=0 ; counter!= 256 ; ++counter)
783 if(symb_cump<current_cump)
784 break; //symbol found, break search loop
785 current_cump+=order1_array[o1_byte][counter]; //update cump
788 counter--; //-1 (see ac_ppmc.html, searching for a given symbol)
790 byte=counter; //update the variable with the found symbol
793 // Get the cump and prob of byte
795 symb_prob=order1_array[o1_byte][byte]; //the probabilty
796 symb_cump=current_cump-order1_array[o1_byte][byte]; //using the cump which was
799 // Update coding state
801 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
804 // Update coded_in_order used for update exclusion
814 // This function returns the probability for the escape codes in the variables
815 void ppmc_get_escape_prob_order1(void)
817 // To understand that remember that the escape allocated for the escape code
818 // is above the current maximum cump and that it has a probability determined
820 symb_prob=order1_defined_bytes_array[o1_byte];
821 symb_cump=order1_max_cump_array[o1_byte];
822 total_cump=order1_defined_bytes_array[o1_byte]+order1_max_cump_array[o1_byte];
829 // Order-2 uses a table which holds the contexts data structure of every
830 // structure, its accessed with "ppmc_order2_hash_key(o1_byte, o2_byte)"
831 // and because it uses direct addressing there's no need to check for hash
832 // collisions, which makes it faster. The variable "o2_cntxt" contains o1_byte
833 // and o2_byte used directly to index the order-2 array.
835 // Though order-2 uses the same routines as lower orders they are rather
836 // different, because we can't allocate memory for all the probabilities
837 // tables, we have to use linked lists for the bytes and probabilities.
839 // Under order-2 we don't let a probability to be lower than 1, that is,
840 // there can't be a symbol in a linked list with a 0 probability, I choosed
841 // this for three factors: the code is easier to read (it isn't messy)
842 // we gain some speed, and the loss of compression seems little.
844 // To ensure that the updating is done correctly, "o2_cntxt" and
845 // "o2_ll_node" must not be modified by any routine except those who manage
849 // Returns in the variable "total_cump" the current total cump of
850 // order-2. cntxt must have o1_byte and o2_byte hashed with the hash key
852 void ppmc_get_totf_order2(void)
854 // Total cump is current total cump plus the escape for the escape code
855 total_cump=order2_array[o2_cntxt].defined_bytes+order2_array[o2_cntxt].max_cump;
859 // Codes a byte under order-2 and returns 1.
860 // Otherwise it returns a 0. It may be that it has coded an escape code, or
861 // that current table was empty.
863 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
864 // In case the byte is coded under this context, coded_in_order=2.
865 char ppmc_code_byte_order2(void)
867 unsigned long counter;
868 struct _byte_and_freq *node;
871 // Initialize o2_cntxt
873 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
876 // First check if current order-2 context is empty
877 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
879 return 0; //byte not coded, nothing done
883 // Now try to code this byte under order-2
885 ppmc_get_totf_order2(); //get total cump
888 // See if the byte is present and compute its cump at the same time
890 node=order2_array[o2_cntxt].prob; //pointer to first element in the linked list
892 symb_cump=0; //the first symbol always has a 0 cump
895 // Now search the byte in the linked list
899 goto ppmc_o2_byte_found; //bad thing, I know, anyone has a better idea?
900 symb_cump+=node->freq; //add the probability of this byte to the cump
903 node=node->next; //next element in the linked list
907 // If we reach that point, the byte was not found in the linked list
908 // so we don't need the cump, we have to output an escape code, whose
909 // probabilities are know using the context structure in the table.
911 // Byte was not present in the linked list, current node is the last one,
912 // and that's the node needed for creating a new node, save it.
916 // Now get the probability and cump of the escape code
918 symb_cump=order2_array[o2_cntxt].max_cump;
919 symb_prob=order2_array[o2_cntxt].defined_bytes;
921 // Code the escape code
922 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
927 // That code is executed when the byte is found in the linked list
932 // Everything has been tested, now we can feel free to code the byte,
933 // the symb_cump is already computed, now get its probability and code
934 // the byte, also save pointer to this element in the linked lists for
937 coded_in_order=2; //successfully coded under order-2
939 o2_ll_node=node; //save it for updating
941 symb_prob=node->freq; //get the probability of the byte
945 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
947 return 1; //byte coded under order-2
951 // This functions update order-2 probabilities with current byte, also takes
952 // care about updating variables, and renormalizing.
953 // Of course "o2_ll_node" must point to the element to update or the last one,
954 // based on the "coded_in_order" and checking the pointer of the element we
955 // can decide what to do.
957 // This updating is only for encoding.
958 void ppmc_update_order2(void)
961 // First of all check if that's the first byte in this context, in that case
962 // we have to initialize some variables in the context structure.
964 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
967 if(ppmc_out_of_memory==1)
968 return; //exit this function, we can't allocate memory
970 order2_array[o2_cntxt].defined_bytes=1;
971 order2_array[o2_cntxt].max_cump=1;
972 order2_array[o2_cntxt].prob=_bytes_pool;
974 _bytes_pool->byte=byte; //initialize byte to current one
975 _bytes_pool->freq=1; //it appeared once
976 _bytes_pool->next=0; //now this is last element in ll
978 // Do update of linked list variables and memory use
980 ++_bytes_pool; //next time use next entry (this is a pointer)
982 if(_bytes_pool==_bytes_pool_max) //maximum reached
985 // First check to see that we still have entries in the array
987 if(_bytes_pool_index==_mempool_max_index)
989 ppmc_out_of_memory=1; //flush
993 // Allocate memory for new buffer
995 if((_bytes_pool=(struct _byte_and_freq *)malloc
996 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
998 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
999 ppmc_out_of_memory=1; //flush
1003 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1005 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1007 _bytes_pool_index++;
1011 return; //nothing else to do
1015 // The byte was coded under order two, otherwise it was coded under a
1016 // lower order (never higher ones, remember that we are using update
1017 // exclusion) in this case we have to create a new node in the list.
1019 if(coded_in_order==2) //coded under order-2
1022 // Update its count and variables of this context and check for renormalization
1024 o2_ll_node->freq++; //increment its frequency (rather probability)
1026 order2_array[o2_cntxt].max_cump++; //total cump
1028 if(o2_ll_node->freq==255) //do we have to renormalize?
1029 ppmc_renormalize_order2(); //renormalize
1035 // Once every paranoid check has been done we are sure that this byte
1036 // did not existed and so we have to create a new node in the linked
1037 // list. Also we have to take care of memory issues.
1039 if(ppmc_out_of_memory==1)
1040 return; //exit this function, we can't allocate mem
1042 o2_ll_node->next=_bytes_pool; //put it in the next free entry
1043 _bytes_pool->byte=byte; //initialize byte to current one
1044 _bytes_pool->freq=1; //it appeared once
1045 _bytes_pool->next=0; //now this is last element in ll
1047 order2_array[o2_cntxt].max_cump++; //total cump
1048 order2_array[o2_cntxt].defined_bytes++; //total cump
1050 // Do update of linked list variables and memory use
1052 ++_bytes_pool; //next time use next entry (this is a pointer)
1054 if(_bytes_pool==_bytes_pool_max) //maximum reached
1057 // First check to see that we still have entries in the array
1059 if(_bytes_pool_index==_mempool_max_index)
1061 ppmc_out_of_memory=1; //flush
1065 // Allocate memory for new buffer
1067 if((_bytes_pool=(struct _byte_and_freq *)malloc
1068 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1070 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1071 ppmc_out_of_memory=1; //flush
1075 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1077 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1079 _bytes_pool_index++;
1088 // This functions renormalizes the probabilities at order-2 updating context
1090 void ppmc_renormalize_order2(void)
1092 unsigned long counter;
1093 struct _byte_and_freq *node;
1095 // Initialize variables. Defined bytes remain the same.
1096 order2_array[o2_cntxt].max_cump=0; //clear them
1098 node=order2_array[o2_cntxt].prob; //get pointer to lined lists
1100 // Divide all the probabilities by 2 and update context variables
1103 node->freq>>=1; //divide by a factor of 2
1105 if(node->freq==0) //don't allow a probability to be 0
1108 order2_array[o2_cntxt].max_cump+=node->freq; //sum to the total cump
1110 if(node->next==0) //last element
1116 //printf("\nRenormalization, context:%c%c",o2_byte,o1_byte);
1121 // Decodes the symbol correspoding to the current order, it returns the byte
1122 // or in case of an escape code or empty table it returns -1.
1123 // It updates "coded_in_order".
1125 // If we decode an escape code, we don't modify "o2_ll_node" and thus its
1126 // work of the updating routine to read till the end of the linked list
1127 // (for adding a new element) however if we decode a byte, we save on
1128 // "o2_ll_node" the pointer to its node. (so updating is faster)
1129 void ppmc_decode_order2(void)
1131 unsigned long current_cump, counter;
1132 struct _byte_and_freq *node;
1134 // Initialize o2_cntxt
1136 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
1139 // First check if current order-2 context is empty
1140 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
1142 byte=-1; //byte not coded, nothing done
1147 // Get the total cump needed for decoding symbol
1148 ppmc_get_totf_order2(); //total cump needed for decoding
1149 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
1151 // Now check if it's an escape code
1152 if(symb_cump>=order2_array[o2_cntxt].max_cump) //the defined code space for the escape code
1155 // Update coding values
1157 ppmc_get_escape_prob_order2();
1158 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1160 // Mark as escape code
1164 return; //an escape code
1168 // Now we have to check what symbol it is
1170 current_cump=0; //cump of the current symbol
1172 node=order2_array[o2_cntxt].prob; //get pointer to linked lists
1176 current_cump+=node->freq; //update cump
1177 if(symb_cump<current_cump)
1178 break; //symbol found, break search loop
1180 node=node->next; //next element
1181 //we have no need to check for the last symbol, we'll never read further
1182 //the end of the linked lists, before we'll found the last byte.
1186 //read byte value and probability
1188 symb_prob=node->freq; //get the probability for updating the state
1189 byte=node->byte; //get byte
1190 o2_ll_node=node; //used for updating
1193 // Get the cump of byte
1195 symb_cump=current_cump-symb_prob;
1197 // Update coding state
1199 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1201 // Update coded_in_order used for update exclusion
1211 // This is the routine for updating while decoding. We have to search the byte
1212 // in the linked list, if it's present, update its count, otherwise we have
1213 // hitted the end of the linked list, and there we have to create a new node.
1215 // Of course if the byte was matched in order-2 we'll have a pointer to it
1216 // in "o2_ll_node" so we don't need to read the linked list. (we already did
1219 // Another case which we also have to specially deal with, this is the case
1220 // when the context has not been initalized yet.
1221 void ppmc_update_dec_order2(void)
1223 struct _byte_and_freq *node;
1226 // Handle the case when the context is not initialized
1227 // This code is the same as the one for the encoding.
1229 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
1232 if(ppmc_out_of_memory==1)
1233 return; //exit this function, we can't allocate memory
1235 order2_array[o2_cntxt].defined_bytes=1;
1236 order2_array[o2_cntxt].max_cump=1;
1237 order2_array[o2_cntxt].prob=_bytes_pool;
1239 _bytes_pool->byte=byte; //initialize byte to current one
1240 _bytes_pool->freq=1; //it appeared once
1241 _bytes_pool->next=0; //now this is last element in ll
1243 // Do update of linked list variables and memory use
1245 ++_bytes_pool; //next time use next entry (this is a pointer)
1247 if(_bytes_pool==_bytes_pool_max) //maximum reached
1250 // First check to see that we still have entries in the array
1252 if(_bytes_pool_index==_mempool_max_index)
1254 ppmc_out_of_memory=1; //flush
1258 // Allocate memory for new buffer
1260 if((_bytes_pool=(struct _byte_and_freq *)malloc
1261 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1263 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1264 ppmc_out_of_memory=1; //flush
1268 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1270 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1272 _bytes_pool_index++;
1277 return; //nothing else to do
1281 // Current context is initalized, proceed
1283 if(coded_in_order==2) //check if it was decoded under order-2
1286 // We can be sure that the pointer "o2_ll_node" points to its entry, and
1287 // it has a non 0 probability (otherwise it couldn't be coded) so just
1288 // update its probability and max_cump
1290 o2_ll_node->freq++; //the probability of the byte
1291 order2_array[o2_cntxt].max_cump++; //the max_cump
1293 if(o2_ll_node->freq==255) //check for renormalization
1294 ppmc_renormalize_order2();
1300 // An escape code was decoded under order-2, we have to read till the
1301 // end of the linked list so we can add a new node for this new byte.
1303 node=order2_array[o2_cntxt].prob; //get pointer to linked list
1307 if(node->next==0) //check for the end of the linked list
1309 node=node->next; //next node
1313 // We reached the end of the linked list, add a new node if possible,
1314 // we are using the same code of "ppmc_update_order2()" with the
1315 // difference that the pointer to the linked list is "node"
1317 if(ppmc_out_of_memory==1)
1318 return; //exit this function, we can't allocate mem
1320 node->next=_bytes_pool; //put it in the next free entry
1321 _bytes_pool->byte=byte; //initialize byte to current one
1322 _bytes_pool->freq=1; //it appeared once
1323 _bytes_pool->next=0; //now this is last element in ll
1325 order2_array[o2_cntxt].max_cump++; //total cump
1326 order2_array[o2_cntxt].defined_bytes++; //total cump
1328 // Do update of linked list variables and memory use
1330 ++_bytes_pool; //next time use next entry (this is a pointer)
1332 if(_bytes_pool==_bytes_pool_max) //maximum reached
1335 // First check to see that we still have entries in the array
1337 if(_bytes_pool_index==_mempool_max_index)
1339 ppmc_out_of_memory=1; //flush
1343 // Allocate memory for new buffer
1345 if((_bytes_pool=(struct _byte_and_freq *)malloc
1346 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1348 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1349 ppmc_out_of_memory=1; //flush
1353 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1355 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1357 _bytes_pool_index++;
1361 return; //we are finished updating
1368 // This function returns the probability for the escape codes in the variables
1369 void ppmc_get_escape_prob_order2(void)
1371 // To understand that remember that the escape allocated for the escape code
1372 // is above the current maximum cump and that it has a probability determined
1375 symb_prob=order2_array[o2_cntxt].defined_bytes;
1376 symb_cump=order2_array[o2_cntxt].max_cump;
1381 // ORDER-3 functions
1383 // The difference between order-3 and order-3 are just a few, instead of
1384 // keeping a table with the context structures, we keep a hash table with
1385 // pointers to linked lists with the context, so it's only a matter of
1386 // searching current context in the linked list corresponding to its hash
1387 // entry. This is done in "ppmc_get_totf_order3" because that's the first
1388 // routine that both encoding and decoding routines call.
1391 // Returns in the variable "total_cump" the current total cump of
1392 // order-3. Must be called while encoding or decoding before anything else
1393 // because it initializes the pointers to the context structure in
1394 // "o3_context" and o3_cntxt.
1396 // If the hash entry is not initialized it returns "o3_context"=0
1397 // If the context is not present in the linked list of context, "o3_context"
1398 // will point to the last element in the linked list.
1399 // If the context is present "o3_context" will point to the context to use.
1400 // One can distinguish the last two by checking the context value of the
1401 // structure, if it's not the same, is the last element.
1403 // The routine returns 0 if the hash entry is not initialized or if the
1404 // the context was not present. Otherwise it returns 1, meaning that we
1405 // have to code under this context.
1406 char ppmc_get_totf_order3(void)
1408 struct context *cntxt_node;
1411 // First make the hash key for order-3
1413 o3_cntxt=ppmc_order3_hash_key(o1_byte,o2_byte,o3_byte);
1414 full_o3_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16); //order-3
1417 // Now check the hash entry in the table
1419 if(order3_hasht[o3_cntxt]==0) //if 0, not initialized
1422 o3_context=0; //no hash entry
1424 return 0; //hash entry not initialized
1428 // Now read trough the linked list of context searching current one
1430 cntxt_node=order3_hasht[o3_cntxt];
1435 if(cntxt_node->order4321==full_o3_cntxt) //compare context
1436 goto ppmc_gtf_cntxt_found;
1438 if(cntxt_node->next==0) //end of context's linked list
1441 cntxt_node=cntxt_node->next; //next element
1446 // Once there the context was not found
1447 o3_context=cntxt_node; //pointer to last element in the linked list
1449 return 0; //it was not present
1452 // The context is found, so return pointer and cump
1454 ppmc_gtf_cntxt_found:
1456 o3_context=cntxt_node;
1458 // Total cump is current total cump plus the escape for the escape code
1460 total_cump=o3_context->defined_bytes+o3_context->max_cump;
1462 return 1; //context found
1467 // Codes a byte under order-3 and returns 1.
1468 // Otherwise it returns a 0.
1470 // In case the byte is coded under this context, coded_in_order=3.
1471 char ppmc_code_byte_order3(void)
1473 unsigned long counter;
1474 struct _byte_and_freq *node;
1477 // Get current context (if present) and total cump.
1479 if(ppmc_get_totf_order3()==0)
1483 // See if the byte is present and compute its cump at the same time
1485 node=o3_context->prob; //pointer to first element in the linked list
1487 symb_cump=0; //the first symbol always has a 0 cump
1490 // Now search the byte in the linked list
1493 if(node->byte==byte)
1494 goto ppmc_o3_byte_found; //bad thing, I know, anyone has a better idea?
1495 symb_cump+=node->freq; //add the probability of this byte to the cump
1498 node=node->next; //next element in the linked list
1502 // If we reach that point, the byte was not found in the linked list
1503 // so we don't need the cump, we have to output an escape code, whose
1504 // probabilities are know using the context structure in the table.
1506 // Byte was not present in the linked list, current node is the last one,
1507 // and that's the node needed for creating a new node, save it.
1511 // Now get the probability and cump of the escape code
1513 symb_cump=o3_context->max_cump;
1514 symb_prob=o3_context->defined_bytes;
1516 // Code the escape code
1517 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
1522 // That code is executed when the byte is found in the linked list
1527 // Everything has been tested, now we can feel free to code the byte,
1528 // the symb_cump is already computed, now get its probability and code
1529 // the byte, also save pointer to this element in the linked lists for
1532 coded_in_order=3; //successfully coded under order-3
1534 o3_ll_node=node; //save it for updating
1536 symb_prob=node->freq; //get the probability of the byte
1540 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
1542 return 1; //byte coded under order-3
1546 // This functions update order-3 probabilities with current byte, also takes
1547 // care about updating variables, and renormalizing.
1549 // "o3_ll_node" must point to the element to update or the last one,
1550 // based on the "coded_in_order" and checking the pointer of the element we
1551 // can decide what to do. Also "o3_context" must be initialized.
1553 // This updating is only for encoding.
1554 void ppmc_update_order3(void)
1557 // First thing first, check if the hash entry is initialized
1559 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
1562 if(ppmc_out_of_memory==1)
1563 return; //exit this function, we can't allocate memory
1566 // First create the context
1568 order3_hasht[o3_cntxt]=_context_pool;
1570 _context_pool->next=0; //this is the last element
1571 _context_pool->order4321=full_o3_cntxt; //put context
1572 _context_pool->prob=_bytes_pool; //pointer to linked list
1573 _context_pool->max_cump=1;
1574 _context_pool->defined_bytes=1;
1577 // Do update of linked list variables and memory use of contexts
1579 ++_context_pool; //next time use next entry (this is a pointer)
1581 if(_context_pool==_context_pool_max) //maximum reached
1584 // First check to see that we still have entries in the array
1586 if(_context_pool_index==_mempool_max_index)
1588 ppmc_out_of_memory=1; //flush
1592 // Allocate memory for new buffer
1594 if((_context_pool=(struct context *)malloc
1595 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1597 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1598 ppmc_out_of_memory=1; //flush
1602 _context_pool_array[_context_pool_index]=_context_pool;
1604 _context_pool_max=_context_pool+_context_pool_elements_inc;
1606 _context_pool_index++;
1611 // Now care about the first (and last) linked list element
1613 _bytes_pool->byte=byte; //initialize byte to current one
1614 _bytes_pool->freq=1; //it appeared once
1615 _bytes_pool->next=0; //now this is last element in ll
1617 // Do update of linked list variables and memory use
1619 ++_bytes_pool; //next time use next entry (this is a pointer)
1621 if(_bytes_pool==_bytes_pool_max) //maximum reached
1624 // First check to see that we still have entries in the array
1626 if(_bytes_pool_index==_mempool_max_index)
1628 ppmc_out_of_memory=1; //flush
1632 // Allocate memory for new buffer
1634 if((_bytes_pool=(struct _byte_and_freq *)malloc
1635 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1637 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1638 ppmc_out_of_memory=1; //flush
1642 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1644 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1646 _bytes_pool_index++;
1650 return; //nothing else to do
1656 // The byte was coded under order three, otherwise it was coded under a
1657 // lower order (never higher ones, remember that we are using update
1658 // exclusion) in this case we have to create a new node in the list.
1660 if(coded_in_order==3) //coded under order-3
1663 // Update its count and variables of this context and check for renormalization
1665 o3_ll_node->freq++; //increment its frequency (rather probability)
1667 o3_context->max_cump++; //total cump
1669 if(o3_ll_node->freq==255) //do we have to renormalize?
1670 ppmc_renormalize_order3(); //renormalize
1676 // Now we have two cases, under a given context (which we actually found)
1677 // we coded an escape coded, in that case just create a new node in the
1678 // linked list of bytes and probabilities. Otherwise we didn't find the
1679 // same node so we have to create it in the linked list for context.
1680 // And we can be sure that it at least has one element and that
1681 // "o3_context" points to the last element, so we can put the new element.
1683 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
1684 { //element or the a context found
1686 if(ppmc_out_of_memory==1)
1687 return; //exit this function, we can't allocate mem
1689 o3_ll_node->next=_bytes_pool; //put it in the next free entry
1690 _bytes_pool->byte=byte; //initialize byte to current one
1691 _bytes_pool->freq=1; //it appeared once
1692 _bytes_pool->next=0; //now this is last element in ll
1694 o3_context->max_cump++; //total cump
1695 o3_context->defined_bytes++; //total cump
1697 // Do update of linked list variables and memory use
1699 ++_bytes_pool; //next time use next entry (this is a pointer)
1701 if(_bytes_pool==_bytes_pool_max) //maximum reached
1704 // First check to see that we still have entries in the array
1706 if(_bytes_pool_index==_mempool_max_index)
1708 ppmc_out_of_memory=1; //flush
1712 // Allocate memory for new buffer
1714 if((_bytes_pool=(struct _byte_and_freq *)malloc
1715 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1717 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1718 ppmc_out_of_memory=1; //flush
1722 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1724 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1726 _bytes_pool_index++;
1733 // We have to create a new context node
1735 if(ppmc_out_of_memory==1)
1736 return; //exit this function, we can't allocate memory
1739 // First create the context
1741 o3_context->next=_context_pool;
1743 _context_pool->next=0; //this is the last element
1744 _context_pool->order4321=full_o3_cntxt; //put context
1745 _context_pool->prob=_bytes_pool; //pointer to linked list
1746 _context_pool->max_cump=1;
1747 _context_pool->defined_bytes=1;
1750 // Do update of linked list variables and memory use of contexts
1752 ++_context_pool; //next time use next entry (this is a pointer)
1754 if(_context_pool==_context_pool_max) //maximum reached
1757 // First check to see that we still have entries in the array
1759 if(_context_pool_index==_mempool_max_index)
1761 ppmc_out_of_memory=1; //flush
1765 // Allocate memory for new buffer
1767 if((_context_pool=(struct context *)malloc
1768 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1770 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1771 ppmc_out_of_memory=1; //flush
1775 _context_pool_array[_context_pool_index]=_context_pool;
1777 _context_pool_max=_context_pool+_context_pool_elements_inc;
1779 _context_pool_index++;
1783 // Now care about the first (and last) linked list element
1785 _bytes_pool->byte=byte; //initialize byte to current one
1786 _bytes_pool->freq=1; //it appeared once
1787 _bytes_pool->next=0; //now this is last element in ll
1789 // Do update of linked list variables and memory use
1791 ++_bytes_pool; //next time use next entry (this is a pointer)
1793 if(_bytes_pool==_bytes_pool_max) //maximum reached
1796 // First check to see that we still have entries in the array
1798 if(_bytes_pool_index==_mempool_max_index)
1800 ppmc_out_of_memory=1; //flush
1804 // Allocate memory for new buffer
1806 if((_bytes_pool=(struct _byte_and_freq *)malloc
1807 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1809 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1810 ppmc_out_of_memory=1; //flush
1814 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1816 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1818 _bytes_pool_index++;
1829 // This functions renormalizes the probabilities at order-3 updating context
1831 void ppmc_renormalize_order3(void)
1833 unsigned long counter;
1834 struct _byte_and_freq *node;
1837 // Initialize variables. Defined bytes remain the same.
1838 o3_context->max_cump=0; //clear them
1840 node=o3_context->prob; //get pointer to lined lists
1842 // Divide all the probabilities by 2 and update context variables
1845 node->freq>>=1; //divide by a factor of 2
1847 if(node->freq==0) //don't allow a probability to be 0
1850 o3_context->max_cump+=node->freq; //sum to the total cump
1852 if(node->next==0) //last element
1861 // Decodes the symbol correspoding to the current order, it returns the byte
1862 // or in case of an escape code or empty table it returns -1.
1863 // It updates "coded_in_order".
1865 // If we decode an escape code, we don't modify "o3_ll_node" and thus its
1866 // work of the updating routine to read till the end of the linked list
1867 // (for adding a new element) however if we decode a byte, we save on
1868 // "o3_ll_node" the pointer to its node. (so updating is faster)
1869 void ppmc_decode_order3(void)
1871 unsigned long current_cump, counter;
1872 struct _byte_and_freq *node;
1875 // Get current context (if present) and total cump.
1877 if(ppmc_get_totf_order3()==0)
1884 // Decode current cump
1886 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
1888 // Now check if it's an escape code
1889 if(symb_cump>=o3_context->max_cump) //the defined code space for the escape code
1892 // Update coding values
1894 ppmc_get_escape_prob_order3();
1895 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1897 // Mark as escape code
1901 return; //an escape code
1905 // Now we have to check what symbol it is
1907 current_cump=0; //cump of the current symbol
1909 node=o3_context->prob; //get pointer to linked lists
1913 current_cump+=node->freq; //update cump
1914 if(symb_cump<current_cump)
1915 break; //symbol found, break search loop
1917 node=node->next; //next element
1918 //we have no need to check for the last symbol, we'll never read further
1919 //the end of the linked lists, before we'll found the last byte.
1923 //read byte value and probability
1925 symb_prob=node->freq; //get the probability for updating the state
1926 byte=node->byte; //get byte
1927 o3_ll_node=node; //used for updating
1930 // Get the cump of byte
1932 symb_cump=current_cump-symb_prob;
1934 // Update coding state
1936 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1938 // Update coded_in_order used for update exclusion
1948 // This is the routine for updating while decoding. The only difference with
1949 // the routine for coding is that when an escape code was coded, "o3_ll_node"
1950 // is not initialized so we have to read till the end of the linked list.
1951 // Fortunately "o3_context" will be initialized so we don't need to read its
1953 void ppmc_update_dec_order3(void)
1955 struct _byte_and_freq *node;
1957 // First thing first, check if the hash entry is initialized
1959 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
1962 if(ppmc_out_of_memory==1)
1963 return; //exit this function, we can't allocate memory
1966 // First create the context
1968 order3_hasht[o3_cntxt]=_context_pool;
1970 _context_pool->next=0; //this is the last element
1971 _context_pool->order4321=full_o3_cntxt; //put context
1972 _context_pool->prob=_bytes_pool; //pointer to linked list
1973 _context_pool->max_cump=1;
1974 _context_pool->defined_bytes=1;
1977 // Do update of linked list variables and memory use of contexts
1979 ++_context_pool; //next time use next entry (this is a pointer)
1981 if(_context_pool==_context_pool_max) //maximum reached
1984 // First check to see that we still have entries in the array
1986 if(_context_pool_index==_mempool_max_index)
1988 ppmc_out_of_memory=1; //flush
1992 // Allocate memory for new buffer
1994 if((_context_pool=(struct context *)malloc
1995 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1997 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1998 ppmc_out_of_memory=1; //flush
2002 _context_pool_array[_context_pool_index]=_context_pool;
2004 _context_pool_max=_context_pool+_context_pool_elements_inc;
2006 _context_pool_index++;
2010 // Now care about the first (and last) linked list element
2012 _bytes_pool->byte=byte; //initialize byte to current one
2013 _bytes_pool->freq=1; //it appeared once
2014 _bytes_pool->next=0; //now this is last element in ll
2016 // Do update of linked list variables and memory use
2018 ++_bytes_pool; //next time use next entry (this is a pointer)
2020 if(_bytes_pool==_bytes_pool_max) //maximum reached
2023 // First check to see that we still have entries in the array
2025 if(_bytes_pool_index==_mempool_max_index)
2027 ppmc_out_of_memory=1; //flush
2031 // Allocate memory for new buffer
2033 if((_bytes_pool=(struct _byte_and_freq *)malloc
2034 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2036 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2037 ppmc_out_of_memory=1; //flush
2041 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2043 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2045 _bytes_pool_index++;
2049 return; //nothing else to do
2053 // The byte was coded under order three, otherwise it was coded under a
2054 // lower order (never higher ones, remember that we are using update
2055 // exclusion) in this case we have to create a new node in the list.
2057 if(coded_in_order==3) //coded under order-3
2060 // Update its count and variables of this context and check for renormalization
2062 o3_ll_node->freq++; //increment its frequency (rather probability)
2064 o3_context->max_cump++; //total cump
2066 if(o3_ll_node->freq==255) //do we have to renormalize?
2067 ppmc_renormalize_order3(); //renormalize
2073 // Now we have two cases, under a given context (which we actually found)
2074 // we coded an escape coded, in that case just create a new node in the
2075 // linked list of bytes and probabilities. Otherwise we didn't find the
2076 // same node so we have to create it in the linked list for context.
2077 // And we can be sure that it at least has one element and that
2078 // "o3_context" points to the last element, so we can put the new element.
2080 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
2081 { //element or the a context found
2083 // Read till the end of the linked list
2085 node=o3_context->prob; //get pointer to linked list
2089 if(node->next==0) //check for the end of the linked list
2091 node=node->next; //next node
2096 if(ppmc_out_of_memory==1)
2097 return; //exit this function, we can't allocate mem
2099 node->next=_bytes_pool; //put it in the next free entry
2100 _bytes_pool->byte=byte; //initialize byte to current one
2101 _bytes_pool->freq=1; //it appeared once
2102 _bytes_pool->next=0; //now this is last element in ll
2104 o3_context->max_cump++; //total cump
2105 o3_context->defined_bytes++; //total cump
2107 // Do update of linked list variables and memory use
2109 ++_bytes_pool; //next time use next entry (this is a pointer)
2111 if(_bytes_pool==_bytes_pool_max) //maximum reached
2114 // First check to see that we still have entries in the array
2116 if(_bytes_pool_index==_mempool_max_index)
2118 ppmc_out_of_memory=1; //flush
2122 // Allocate memory for new buffer
2124 if((_bytes_pool=(struct _byte_and_freq *)malloc
2125 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2127 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2128 ppmc_out_of_memory=1; //flush
2132 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2134 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2136 _bytes_pool_index++;
2143 // We have to create a new context node
2145 if(ppmc_out_of_memory==1)
2146 return; //exit this function, we can't allocate memory
2149 // First create the context
2151 o3_context->next=_context_pool;
2153 _context_pool->next=0; //this is the last element
2154 _context_pool->order4321=full_o3_cntxt; //put context
2155 _context_pool->prob=_bytes_pool; //pointer to linked list
2156 _context_pool->max_cump=1;
2157 _context_pool->defined_bytes=1;
2160 // Do update of linked list variables and memory use of contexts
2162 ++_context_pool; //next time use next entry (this is a pointer)
2164 if(_context_pool==_context_pool_max) //maximum reached
2167 // First check to see that we still have entries in the array
2169 if(_context_pool_index==_mempool_max_index)
2171 ppmc_out_of_memory=1; //flush
2175 // Allocate memory for new buffer
2177 if((_context_pool=(struct context *)malloc
2178 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2180 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2181 ppmc_out_of_memory=1; //flush
2185 _context_pool_array[_context_pool_index]=_context_pool;
2187 _context_pool_max=_context_pool+_context_pool_elements_inc;
2189 _context_pool_index++;
2193 // Now care about the first (and last) linked list element
2195 _bytes_pool->byte=byte; //initialize byte to current one
2196 _bytes_pool->freq=1; //it appeared once
2197 _bytes_pool->next=0; //now this is last element in ll
2199 // Do update of linked list variables and memory use
2201 ++_bytes_pool; //next time use next entry (this is a pointer)
2203 if(_bytes_pool==_bytes_pool_max) //maximum reached
2206 // First check to see that we still have entries in the array
2208 if(_bytes_pool_index==_mempool_max_index)
2210 ppmc_out_of_memory=1; //flush
2214 // Allocate memory for new buffer
2216 if((_bytes_pool=(struct _byte_and_freq *)malloc
2217 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2219 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2220 ppmc_out_of_memory=1; //flush
2224 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2226 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2228 _bytes_pool_index++;
2240 // This function returns the probability for the escape codes in the variables
2241 void ppmc_get_escape_prob_order3(void)
2243 // To understand that remember that the escape allocated for the escape code
2244 // is above the current maximum cump and that it has a probability determined
2247 symb_prob=o3_context->defined_bytes;
2248 symb_cump=o3_context->max_cump;
2253 // ORDER-4 functions
2255 // The routines for order-4 are *equal* to those for order-3, there are a few
2256 // changes like different global variables, and different hash keys.
2258 // If you want to go to higher orders, you'd use the same code and data
2259 // structures, with the difference of the context bytes (order4321)
2260 // stored in every context's linked list.
2263 // Returns in the variable "total_cump" the current total cump of
2264 // order-4. Must be called while encoding or decoding before anything else
2265 // because it initializes the pointers to the context structure in
2266 // "o4_context" and o4_cntxt.
2268 // If the hash entry is not initialized it returns "o4_context"=0
2269 // If the context is not present in the linked list of context, "o4_context"
2270 // will point to the last element in the linked list.
2271 // If the context is present "o4_context" will point to the context to use.
2272 // One can distinguish the last two by checking the context value of the
2273 // structure, if it's not the same, is the last element.
2275 // The routine returns 0 if the hash entry is not initialized or if the
2276 // the context was not present. Otherwise it returns 1, meaning that we
2277 // have to code under this context.
2278 char ppmc_get_totf_order4(void)
2280 struct context *cntxt_node;
2283 // First make the hash key for order-4
2285 o4_cntxt=ppmc_order4_hash_key(o1_byte,o2_byte,o3_byte,o4_byte);
2286 full_o4_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16)+(o4_byte<<24); //order-4
2289 // Now check the hash entry in the table
2291 if(order4_hasht[o4_cntxt]==0) //if 0, not initialized
2294 o4_context=0; //no hash entry
2296 return 0; //hash entry not initialized
2300 // Now read trough the linked list of context searching current one
2302 cntxt_node=order4_hasht[o4_cntxt];
2307 if(cntxt_node->order4321==full_o4_cntxt) //compare context
2308 goto ppmc_gtfo4_cntxt_found;
2310 if(cntxt_node->next==0) //end of context's linked list
2313 cntxt_node=cntxt_node->next; //next element
2318 // Once there the context was not found
2319 o4_context=cntxt_node; //pointer to last element in the linked list
2321 return 0; //it was not present
2324 // The context is found, so return pointer and cump
2326 ppmc_gtfo4_cntxt_found:
2328 o4_context=cntxt_node;
2330 // Total cump is current total cump plus the escape for the escape code
2332 total_cump=o4_context->defined_bytes+o4_context->max_cump;
2334 return 1; //context found
2339 // Codes a byte under order-4 and returns 1.
2340 // Otherwise it returns a 0.
2342 // In case the byte is coded under this context, coded_in_order=4.
2343 char ppmc_code_byte_order4(void)
2345 unsigned long counter;
2346 struct _byte_and_freq *node;
2349 // Get current context (if present) and total cump.
2351 if(ppmc_get_totf_order4()==0)
2355 // See if the byte is present and compute its cump at the same time
2357 node=o4_context->prob; //pointer to first element in the linked list
2359 symb_cump=0; //the first symbol always has a 0 cump
2362 // Now search the byte in the linked list
2365 if(node->byte==byte)
2366 goto ppmc_o4_byte_found; //bad thing, I know, anyone has a better idea?
2367 symb_cump+=node->freq; //add the probability of this byte to the cump
2370 node=node->next; //next element in the linked list
2374 // If we reach that point, the byte was not found in the linked list
2375 // so we don't need the cump, we have to output an escape code, whose
2376 // probabilities are know using the context structure in the table.
2378 // Byte was not present in the linked list, current node is the last one,
2379 // and that's the node needed for creating a new node, save it.
2383 // Now get the probability and cump of the escape code
2385 symb_cump=o4_context->max_cump;
2386 symb_prob=o4_context->defined_bytes;
2388 // Code the escape code
2389 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
2394 // That code is executed when the byte is found in the linked list
2399 // Everything has been tested, now we can feel free to code the byte,
2400 // the symb_cump is already computed, now get its probability and code
2401 // the byte, also save pointer to this element in the linked lists for
2404 coded_in_order=4; //successfully coded under order-4
2406 o4_ll_node=node; //save it for updating
2408 symb_prob=node->freq; //get the probability of the byte
2412 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
2414 return 1; //byte coded under order-4
2418 // This functions update order-4 probabilities with current byte, also takes
2419 // care about updating variables, and renormalizing.
2421 // "o4_ll_node" must point to the element to update or the last one,
2422 // based on the "coded_in_order" and checking the pointer of the element we
2423 // can decide what to do. Also "o4_context" must be initialized.
2425 // This updating is only for encoding.
2426 void ppmc_update_order4(void)
2429 // First thing first, check if the hash entry is initialized
2431 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
2434 if(ppmc_out_of_memory==1)
2435 return; //exit this function, we can't allocate memory
2438 // First create the context
2440 order4_hasht[o4_cntxt]=_context_pool;
2442 _context_pool->next=0; //this is the last element
2443 _context_pool->order4321=full_o4_cntxt; //put context
2444 _context_pool->prob=_bytes_pool; //pointer to linked list
2445 _context_pool->max_cump=1;
2446 _context_pool->defined_bytes=1;
2449 // Do update of linked list variables and memory use of contexts
2451 ++_context_pool; //next time use next entry (this is a pointer)
2453 if(_context_pool==_context_pool_max) //maximum reached
2456 // First check to see that we still have entries in the array
2458 if(_context_pool_index==_mempool_max_index)
2460 ppmc_out_of_memory=1; //flush
2464 // Allocate memory for new buffer
2466 if((_context_pool=(struct context *)malloc
2467 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2469 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2470 ppmc_out_of_memory=1; //flush
2474 _context_pool_array[_context_pool_index]=_context_pool;
2476 _context_pool_max=_context_pool+_context_pool_elements_inc;
2478 _context_pool_index++;
2483 // Now care about the first (and last) linked list element
2485 _bytes_pool->byte=byte; //initialize byte to current one
2486 _bytes_pool->freq=1; //it appeared once
2487 _bytes_pool->next=0; //now this is last element in ll
2489 // Do update of linked list variables and memory use
2491 ++_bytes_pool; //next time use next entry (this is a pointer)
2493 if(_bytes_pool==_bytes_pool_max) //maximum reached
2496 // First check to see that we still have entries in the array
2498 if(_bytes_pool_index==_mempool_max_index)
2500 ppmc_out_of_memory=1; //flush
2504 // Allocate memory for new buffer
2506 if((_bytes_pool=(struct _byte_and_freq *)malloc
2507 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2509 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2510 ppmc_out_of_memory=1; //flush
2514 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2516 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2518 _bytes_pool_index++;
2522 return; //nothing else to do
2528 // The byte was coded under order three, otherwise it was coded under a
2529 // lower order (never higher ones, remember that we are using update
2530 // exclusion) in this case we have to create a new node in the list.
2532 if(coded_in_order==4) //coded under order-4
2535 // Update its count and variables of this context and check for renormalization
2537 o4_ll_node->freq++; //increment its frequency (rather probability)
2539 o4_context->max_cump++; //total cump
2541 if(o4_ll_node->freq==255) //do we have to renormalize?
2542 ppmc_renormalize_order4(); //renormalize
2548 // Now we have two cases, under a given context (which we actually found)
2549 // we coded an escape coded, in that case just create a new node in the
2550 // linked list of bytes and probabilities. Otherwise we didn't find the
2551 // same node so we have to create it in the linked list for context.
2552 // And we can be sure that it at least has one element and that
2553 // "o4_context" points to the last element, so we can put the new element.
2555 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
2556 { //element or the a context found
2558 if(ppmc_out_of_memory==1)
2559 return; //exit this function, we can't allocate mem
2561 o4_ll_node->next=_bytes_pool; //put it in the next free entry
2562 _bytes_pool->byte=byte; //initialize byte to current one
2563 _bytes_pool->freq=1; //it appeared once
2564 _bytes_pool->next=0; //now this is last element in ll
2566 o4_context->max_cump++; //total cump
2567 o4_context->defined_bytes++; //total cump
2569 // Do update of linked list variables and memory use
2571 ++_bytes_pool; //next time use next entry (this is a pointer)
2573 if(_bytes_pool==_bytes_pool_max) //maximum reached
2576 // First check to see that we still have entries in the array
2578 if(_bytes_pool_index==_mempool_max_index)
2580 ppmc_out_of_memory=1; //flush
2584 // Allocate memory for new buffer
2586 if((_bytes_pool=(struct _byte_and_freq *)malloc
2587 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2589 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2590 ppmc_out_of_memory=1; //flush
2594 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2596 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2598 _bytes_pool_index++;
2605 // We have to create a new context node
2607 if(ppmc_out_of_memory==1)
2608 return; //exit this function, we can't allocate memory
2611 // First create the context
2613 o4_context->next=_context_pool;
2615 _context_pool->next=0; //this is the last element
2616 _context_pool->order4321=full_o4_cntxt; //put context
2617 _context_pool->prob=_bytes_pool; //pointer to linked list
2618 _context_pool->max_cump=1;
2619 _context_pool->defined_bytes=1;
2622 // Do update of linked list variables and memory use of contexts
2624 ++_context_pool; //next time use next entry (this is a pointer)
2626 if(_context_pool==_context_pool_max) //maximum reached
2629 // First check to see that we still have entries in the array
2631 if(_context_pool_index==_mempool_max_index)
2633 ppmc_out_of_memory=1; //flush
2637 // Allocate memory for new buffer
2639 if((_context_pool=(struct context *)malloc
2640 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2642 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2643 ppmc_out_of_memory=1; //flush
2647 _context_pool_array[_context_pool_index]=_context_pool;
2649 _context_pool_max=_context_pool+_context_pool_elements_inc;
2651 _context_pool_index++;
2655 // Now care about the first (and last) linked list element
2657 _bytes_pool->byte=byte; //initialize byte to current one
2658 _bytes_pool->freq=1; //it appeared once
2659 _bytes_pool->next=0; //now this is last element in ll
2661 // Do update of linked list variables and memory use
2663 ++_bytes_pool; //next time use next entry (this is a pointer)
2665 if(_bytes_pool==_bytes_pool_max) //maximum reached
2668 // First check to see that we still have entries in the array
2670 if(_bytes_pool_index==_mempool_max_index)
2672 ppmc_out_of_memory=1; //flush
2676 // Allocate memory for new buffer
2678 if((_bytes_pool=(struct _byte_and_freq *)malloc
2679 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2681 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2682 ppmc_out_of_memory=1; //flush
2686 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2688 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2690 _bytes_pool_index++;
2701 // This functions renormalizes the probabilities at order-4 updating context
2703 void ppmc_renormalize_order4(void)
2705 unsigned long counter;
2706 struct _byte_and_freq *node;
2709 // Initialize variables. Defined bytes remain the same.
2710 o4_context->max_cump=0; //clear them
2712 node=o4_context->prob; //get pointer to lined lists
2714 // Divide all the probabilities by 2 and update context variables
2717 node->freq>>=1; //divide by a factor of 2
2719 if(node->freq==0) //don't allow a probability to be 0
2722 o4_context->max_cump+=node->freq; //sum to the total cump
2724 if(node->next==0) //last element
2733 // Decodes the symbol correspoding to the current order, it returns the byte
2734 // or in case of an escape code or empty table it returns -1.
2735 // It updates "coded_in_order".
2737 // If we decode an escape code, we don't modify "o4_ll_node" and thus its
2738 // work of the updating routine to read till the end of the linked list
2739 // (for adding a new element) however if we decode a byte, we save on
2740 // "o4_ll_node" the pointer to its node. (so updating is faster)
2741 void ppmc_decode_order4(void)
2743 unsigned long current_cump, counter;
2744 struct _byte_and_freq *node;
2747 // Get current context (if present) and total cump.
2749 if(ppmc_get_totf_order4()==0)
2756 // Decode current cump
2758 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
2760 // Now check if it's an escape code
2761 if(symb_cump>=o4_context->max_cump) //the defined code space for the escape code
2764 // Update coding values
2766 ppmc_get_escape_prob_order4();
2767 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
2769 // Mark as escape code
2773 return; //an escape code
2777 // Now we have to check what symbol it is
2779 current_cump=0; //cump of the current symbol
2781 node=o4_context->prob; //get pointer to linked lists
2785 current_cump+=node->freq; //update cump
2786 if(symb_cump<current_cump)
2787 break; //symbol found, break search loop
2789 node=node->next; //next element
2790 //we have no need to check for the last symbol, we'll never read further
2791 //the end of the linked lists, before we'll found the last byte.
2795 //read byte value and probability
2797 symb_prob=node->freq; //get the probability for updating the state
2798 byte=node->byte; //get byte
2799 o4_ll_node=node; //used for updating
2802 // Get the cump of byte
2804 symb_cump=current_cump-symb_prob;
2806 // Update coding state
2808 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
2810 // Update coded_in_order used for update exclusion
2820 // This is the routine for updating while decoding. The only difference with
2821 // the routine for coding is that when an escape code was coded, "o4_ll_node"
2822 // is not initialized so we have to read till the end of the linked list.
2823 // Fortunately "o4_context" will be initialized so we don't need to read its
2825 void ppmc_update_dec_order4(void)
2827 struct _byte_and_freq *node;
2829 // First thing first, check if the hash entry is initialized
2831 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
2834 if(ppmc_out_of_memory==1)
2835 return; //exit this function, we can't allocate memory
2838 // First create the context
2840 order4_hasht[o4_cntxt]=_context_pool;
2842 _context_pool->next=0; //this is the last element
2843 _context_pool->order4321=full_o4_cntxt; //put context
2844 _context_pool->prob=_bytes_pool; //pointer to linked list
2845 _context_pool->max_cump=1;
2846 _context_pool->defined_bytes=1;
2849 // Do update of linked list variables and memory use of contexts
2851 ++_context_pool; //next time use next entry (this is a pointer)
2853 if(_context_pool==_context_pool_max) //maximum reached
2856 // First check to see that we still have entries in the array
2858 if(_context_pool_index==_mempool_max_index)
2860 ppmc_out_of_memory=1; //flush
2864 // Allocate memory for new buffer
2866 if((_context_pool=(struct context *)malloc
2867 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2869 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2870 ppmc_out_of_memory=1; //flush
2874 _context_pool_array[_context_pool_index]=_context_pool;
2876 _context_pool_max=_context_pool+_context_pool_elements_inc;
2878 _context_pool_index++;
2882 // Now care about the first (and last) linked list element
2884 _bytes_pool->byte=byte; //initialize byte to current one
2885 _bytes_pool->freq=1; //it appeared once
2886 _bytes_pool->next=0; //now this is last element in ll
2888 // Do update of linked list variables and memory use
2890 ++_bytes_pool; //next time use next entry (this is a pointer)
2892 if(_bytes_pool==_bytes_pool_max) //maximum reached
2895 // First check to see that we still have entries in the array
2897 if(_bytes_pool_index==_mempool_max_index)
2899 ppmc_out_of_memory=1; //flush
2903 // Allocate memory for new buffer
2905 if((_bytes_pool=(struct _byte_and_freq *)malloc
2906 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2908 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2909 ppmc_out_of_memory=1; //flush
2913 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2915 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2917 _bytes_pool_index++;
2921 return; //nothing else to do
2925 // The byte was coded under order four, otherwise it was coded under a
2926 // lower order (never higher ones, remember that we are using update
2927 // exclusion) in this case we have to create a new node in the list.
2929 if(coded_in_order==4) //coded under order-4
2932 // Update its count and variables of this context and check for renormalization
2934 o4_ll_node->freq++; //increment its frequency (rather probability)
2936 o4_context->max_cump++; //total cump
2938 if(o4_ll_node->freq==255) //do we have to renormalize?
2939 ppmc_renormalize_order4(); //renormalize
2945 // Now we have two cases, under a given context (which we actually found)
2946 // we coded an escape coded, in that case just create a new node in the
2947 // linked list of bytes and probabilities. Otherwise we didn't find the
2948 // same node so we have to create it in the linked list for context.
2949 // And we can be sure that it at least has one element and that
2950 // "o4_context" points to the last element, so we can put the new element.
2952 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
2953 { //element or the a context found
2955 // Read till the end of the linked list
2957 node=o4_context->prob; //get pointer to linked list
2961 if(node->next==0) //check for the end of the linked list
2963 node=node->next; //next node
2968 if(ppmc_out_of_memory==1)
2969 return; //exit this function, we can't allocate mem
2971 node->next=_bytes_pool; //put it in the next free entry
2972 _bytes_pool->byte=byte; //initialize byte to current one
2973 _bytes_pool->freq=1; //it appeared once
2974 _bytes_pool->next=0; //now this is last element in ll
2976 o4_context->max_cump++; //total cump
2977 o4_context->defined_bytes++; //total cump
2979 // Do update of linked list variables and memory use
2981 ++_bytes_pool; //next time use next entry (this is a pointer)
2983 if(_bytes_pool==_bytes_pool_max) //maximum reached
2986 // First check to see that we still have entries in the array
2988 if(_bytes_pool_index==_mempool_max_index)
2990 ppmc_out_of_memory=1; //flush
2994 // Allocate memory for new buffer
2996 if((_bytes_pool=(struct _byte_and_freq *)malloc
2997 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2999 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3000 ppmc_out_of_memory=1; //flush
3004 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3006 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3008 _bytes_pool_index++;
3015 // We have to create a new context node
3017 if(ppmc_out_of_memory==1)
3018 return; //exit this function, we can't allocate memory
3021 // First create the context
3023 o4_context->next=_context_pool;
3025 _context_pool->next=0; //this is the last element
3026 _context_pool->order4321=full_o4_cntxt; //put context
3027 _context_pool->prob=_bytes_pool; //pointer to linked list
3028 _context_pool->max_cump=1;
3029 _context_pool->defined_bytes=1;
3032 // Do update of linked list variables and memory use of contexts
3034 ++_context_pool; //next time use next entry (this is a pointer)
3036 if(_context_pool==_context_pool_max) //maximum reached
3039 // First check to see that we still have entries in the array
3041 if(_context_pool_index==_mempool_max_index)
3043 ppmc_out_of_memory=1; //flush
3047 // Allocate memory for new buffer
3049 if((_context_pool=(struct context *)malloc
3050 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
3052 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
3053 ppmc_out_of_memory=1; //flush
3057 _context_pool_array[_context_pool_index]=_context_pool;
3059 _context_pool_max=_context_pool+_context_pool_elements_inc;
3061 _context_pool_index++;
3065 // Now care about the first (and last) linked list element
3067 _bytes_pool->byte=byte; //initialize byte to current one
3068 _bytes_pool->freq=1; //it appeared once
3069 _bytes_pool->next=0; //now this is last element in ll
3071 // Do update of linked list variables and memory use
3073 ++_bytes_pool; //next time use next entry (this is a pointer)
3075 if(_bytes_pool==_bytes_pool_max) //maximum reached
3078 // First check to see that we still have entries in the array
3080 if(_bytes_pool_index==_mempool_max_index)
3082 ppmc_out_of_memory=1; //flush
3086 // Allocate memory for new buffer
3088 if((_bytes_pool=(struct _byte_and_freq *)malloc
3089 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
3091 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3092 ppmc_out_of_memory=1; //flush
3096 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3098 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3100 _bytes_pool_index++;
3112 // This function returns the probability for the escape codes in the variables
3113 void ppmc_get_escape_prob_order4(void)
3115 // To understand that remember that the escape allocated for the escape code
3116 // is above the current maximum cump and that it has a probability determined
3119 symb_prob=o4_context->defined_bytes;
3120 symb_cump=o4_context->max_cump;