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.
38 // Ruotines used by ppmc. Not including the range coder.
40 // They are for initializing of both encoder and decoder, and unless there
41 // are two version, both encoder and decoder use the same routines. Like
42 // "ppmc_initialize_contexts".
45 // This one allocs the memory needed by ppmc, and adjust some pointers used
46 // for allocating elements in the linked lists. The mempool arrays must be
48 void ppmc_alloc_memory(void)
50 unsigned long counter;
53 // Init mempool buffers
55 for(counter=0;counter!=_mempool_max_index;++counter)
57 _bytes_pool_array[counter]=0;
58 _context_pool_array[counter]=0;
61 _bytes_pool_index=1; //first entry will be used now
62 _context_pool_index=1;
65 // Allocate memory for ppmc structures and adjust some variables
66 if((_bytes_pool=(struct _byte_and_freq *)malloc
67 (sizeof(struct _byte_and_freq)*_bytes_pool_elements))==NULL)
69 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
70 ppmc_out_of_memory=1; //flush
74 if((_context_pool=(struct context *)malloc
75 (sizeof(struct context)*_context_pool_elements))==NULL)
77 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
78 ppmc_out_of_memory=1; //flush
83 //save pointers in the array for freeing
84 _bytes_pool_array[0]=_bytes_pool;
85 _context_pool_array[0]=_context_pool;
89 _bytes_pool_max=_bytes_pool+_bytes_pool_elements;
90 _context_pool_max=_context_pool+_context_pool_elements;
92 ppmc_out_of_memory=0; //we still have memory
96 // This routine initializes all the contexts, and all the tables including
97 // those who care about the number of bytes defined in a context.
98 void ppmc_initialize_contexts(void)
100 unsigned long counter, counter2;
104 for(counter=0;counter!=256;++counter) //clear table
105 order0_array[counter]=0;
107 order0_defined_bytes=0; //adjust variables
112 for(counter=0;counter!=256;++counter) //erase every table of every context
113 for(counter2=0;counter2!=256;++counter2)
114 order1_array[counter][counter2]=0;
116 for(counter=0;counter!=256;++counter) //adjust variables
118 order1_defined_bytes_array[counter]=0;
119 order1_max_cump_array[counter]=0;
124 for(counter=0;counter!=65536;++counter)
126 //order2_array[counter].prob=0; //clear pointer to bytes and frequencies
127 //order2_array[counter].max_cump=0;
128 order2_array[counter].defined_bytes=0;
133 for(counter=0;counter!=65536;++counter) //order-4-3
135 order4_hasht[counter]=0;
136 order3_hasht[counter]=0;
141 // This routine initializes the encode model by outputting as many bytes as
142 // needed to prepare the models. This should be called before the main loop
143 // and after the memory has been allocated and tables initialized.
145 // It does not need uses the range coder. It output the first 1 bytes.
146 void ppmc_encoder_initialize(void)
149 // Initialize order-0 and prepare different bytes for orders
150 fputc((byte=fgetc(file_input)),file_output);
151 o4_byte=byte; //order-4
153 fputc((byte=fgetc(file_input)),file_output);
154 o3_byte=byte; //order-3
156 fputc((byte=fgetc(file_input)),file_output);
157 o2_byte=byte; //order-2
158 ppmc_update_order0();
160 fputc((byte=fgetc(file_input)),file_output);
166 // This routine initializes the decoder model, should be called to do the same
167 // changes as "ppmc_encoder_initialize()" did.
168 void ppmc_decoder_initialize(void)
171 // Initialize order-0 and context bytes
172 byte=fgetc(file_input);
173 o4_byte=byte; //order-4
174 fputc(byte,file_output);
176 byte=fgetc(file_input);
177 o3_byte=byte; //order-3
178 fputc(byte,file_output);
180 byte=fgetc(file_input);
181 o2_byte=byte; //order-2
183 fputc(byte,file_output); //output first byte
184 ppmc_update_order0();
186 byte=fgetc(file_input);
187 o1_byte=byte; //order-1
188 fputc(byte,file_output);
192 // Once coding or decoding is finished you have to call this routine.
193 // It must be called when done.
194 void ppmc_free_memory(void)
196 unsigned long counter;
198 // Free the memory buffers
200 for(counter=0;counter!=_mempool_max_index;++counter)
202 if(_bytes_pool_array[counter]!=0)
203 free(_bytes_pool_array[counter]);
205 if(_context_pool_array[counter]!=0)
206 free(_context_pool_array[counter]);
212 // This routine flushes the memory and restarts all the tables of
213 // probabilities, current order bytes are not modified, this function
214 // is called when we ran out of memory. We have to output the code
215 // number 256 which means memory flushing, for doing this we have to go
216 // to order-(-1) so we have to output an escape code in all the orders
217 // till we reach order-(-1) where we can code it. Then we free all the
218 // memory, alloc it again, and reinitialize all the orders.
220 // However we may find the case when the current order is not initialized,
221 // in this case we don't need to output an escape code.
222 void ppmc_flush_mem_enc(void)
225 // Code an escape code in order-4
226 if(ppmc_get_totf_order4()!=0) //if 0 no need of escape code
229 ppmc_get_escape_prob_order4(); //get prob and cump
230 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
235 // Code an escape code in order-3
236 if(ppmc_get_totf_order3()!=0) //if 0 no need of escape code
239 ppmc_get_escape_prob_order3(); //get prob and cump
240 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
245 // Code an escape code in order-2
247 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
249 // First check if current order-2 context is empty
250 if(order2_array[o2_cntxt].defined_bytes!=0) //it's not empty
252 ppmc_get_totf_order2();
253 ppmc_get_escape_prob_order2();
254 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
258 // Code an escape code in order-1
260 // First check if current order-1 table is empty
261 if(order1_defined_bytes_array[o1_byte]!=0) //it's not empty
263 ppmc_get_totf_order1();
264 ppmc_get_escape_prob_order1();
265 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
269 // Code an escape code in order-0. Order-0 always has at least one symbol
271 ppmc_get_totf_order0();
272 ppmc_get_escape_prob_order0();
273 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
277 // Now we can code the code 256
282 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
285 // Now that decoder knows the flushing, free memory and reinit
289 ppmc_initialize_contexts();
292 // Be sure that order-0 has at least one probability
294 order0_array[o1_byte]++;
296 order0_defined_bytes++;
301 // When the decoder gets the symbol of flushing, most of the job is done
302 // because we already got all the escape codes, so we only have to reinit.
303 void ppmc_flush_mem_dec(void)
306 // Free memory and reinit
310 ppmc_initialize_contexts();
313 // Be sure that order-0 has at least one probability
315 order0_array[o1_byte]++;
317 order0_defined_bytes++;
324 // ORDER-(-1) functions, also called ordern1 (Negative1) in functions
326 // Because order-(-1) does not need to update its probability tables, it
327 // has no tables, and relies on the fact that the cump of byte is its own
328 // value, and the probability is fixed, 1, and the total cump is 257.
330 // The alphabet has the following distribution: 0-255 the bytes. 256 is
331 // an special symbol which means that we have flushed the encoder tables,
332 // and thus the encoder must flush its tables too.
334 // The rest of the tables only have 256 symbols, because we have no need
335 // of assign a symbol to the flush code (which already is the order-(-1)
336 // table) nor to the escape code.
339 // Gets the probability for a given symbol in the order-(-1) (ordern1)
340 void ppmc_get_prob_ordern1(void)
342 symb_cump=byte; //its value
343 symb_prob=1; //flat probability
344 total_cump=257; //total cump
348 // Returns in the variable "total_cump" the current total cump of
350 void ppmc_get_totf_ordern1(void)
352 total_cump=257; //this is fixed
356 // Returns the symbol for a given cump under order-(-1)
357 unsigned long ppmc_get_symbol_ordern1 (void)
366 // Due to the fact that order-0 has no context, I use an array for all the
367 // probabilities under order-0, just as you could do in a basic model for
368 // arithmetic coding.
370 // The main array is: order0_array. Where order0_array[byte] contains the
371 // probability for a given byte. The same applies to order-1.
373 // To ensure that the updating and coding is done correctly, "byte" can't
374 // be changed till all the coding and updating is done.
377 // Returns in the variable "total_cump" the current total cump of
379 void ppmc_get_totf_order0(void)
381 // Total cump is current total cump plus the escape for the escape code
382 total_cump=order0_defined_bytes+order0_max_cump;
386 // Codes a byte under order-0 and returns 1, otherwise it returns a 0 and
387 // has coded an escape code. In this case further coding is needed.
389 // Returns: 1 in case a byte was coded. 0 in case of escape code.
390 char ppmc_code_byte_order0(void)
392 unsigned long counter;
394 ppmc_get_totf_order0(); //get total cump
396 // See if the byte is present
397 if(order0_array[byte]==0) //a probability of 0
400 // Because it was not present, output an escape code, prepare variables
402 symb_cump=order0_max_cump; //obviously its cump is current max_cump
403 //without escape code's space
405 symb_prob=order0_defined_bytes; //the number of defined bytes
407 // Code the escape code
408 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
410 return 0; //byte not coded
417 // The symbol is present, code it under order-0
419 symb_prob=order0_array[byte]; //get probability directly
421 // Make cump for current symbol
423 symb_cump=0; //for first symbol is 0
424 for(counter=0; counter!=byte ; ++counter)
425 symb_cump+=order0_array[counter]; //sum probabilities before our symbol
428 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
430 return 1; //symbol coded under order-0
435 // This functions update order-0 probabilities with current byte, also takes
436 // care about updating variables, and renormalizing.
437 void ppmc_update_order0(void)
439 if(order0_array[byte]==0)
441 // It had a zero probability
442 order0_array[byte]++; //increment symbol probability
443 ++order0_defined_bytes; //new byte defined
444 ++order0_max_cump; //total cump
449 // It had a non-zero probability
451 // Increment its probability
452 order0_array[byte]++; //increment symbol probability
453 ++order0_max_cump; //total cump
455 // Check to see if its the maximum in this case renormalize
456 if(order0_array[byte]==255)
457 ppmc_renormalize_order0();
464 // This functions renormalizes the probabilities at order-0 updating variables
465 void ppmc_renormalize_order0(void)
467 unsigned long counter;
469 // Initialize variables
470 order0_defined_bytes=0; //clear them
473 // Loop over all probabilities, divide them by a factor of 2 and update variables
474 for(counter=0 ; counter!=256 ; ++counter)
476 order0_array[counter]>>=1; //divide by a factor of 2
478 if(order0_array[counter]!=0) //see if it has a non zero probability
479 order0_defined_bytes++;
481 order0_max_cump+=order0_array[counter]; //sum to the total cump
486 // Decodes the symbol correspoding to the current order, it returns the byte
487 // or in case of a escape code it returns -1
488 void ppmc_decode_order0(void)
490 unsigned long current_cump, counter;
493 // Get the total cump needed for decoding symbol
494 ppmc_get_totf_order0(); //total cump needed for decoding
495 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
497 // Now check if it's an escape code
498 if(symb_cump>=order0_max_cump) //the defined code space for the escape code
501 // Update coding values
503 ppmc_get_escape_prob_order0();
504 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
506 // Mark as escape code
510 return; //an escape code
514 // Now we have to check what symbol it is
516 current_cump=0; //cump of the current symbol
518 for(counter=0 ; counter!= 256 ; ++counter)
520 if(symb_cump<current_cump)
521 break; //symbol found, break search loop
522 current_cump+=order0_array[counter]; //update cump
525 counter--; //-1 (see ac_ppmc.html, searching for a given symbol)
527 byte=counter; //update the variable with the found symbol
530 // Get the cump and prob of byte
532 symb_prob=order0_array[byte]; //the probabilty
533 symb_cump=current_cump-order0_array[byte]; //using the cump which was
536 // Update coding state
538 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
540 coded_in_order=0; //decoded under order-0
548 // This function returns the probability for the escape codes in the variables
549 void ppmc_get_escape_prob_order0(void)
551 // To understand that remember that the escape allocated for the escape code
552 // is above the current maximum cump and that it has a probability determined
554 symb_prob=order0_defined_bytes;
555 symb_cump=order0_max_cump;
556 total_cump=order0_defined_bytes+order0_max_cump;
563 // Order-1 uses 256 arrays one for every context, obviously they are accessed
564 // like that: order1_array[o1_byte][byte]
566 // Also for computing the value of the current escape code in a given context
567 // we need to know how many bytes are defined in a given context, they are
568 // stored in an array, and you use them like that:
569 // order1_defined_bytes_array[o1_byte]
571 // The same goes for the maximum cump of a given context:
572 // order1_max_cump_array[o1_byte]
574 // Read the order-0 explanation.
576 // To ensure that the updating and coding is done correctly, "byte" and
577 // "o1_byte" can't be changed till all the coding and updating is done.
580 // Returns in the variable "total_cump" the current total cump of
581 // order-1. o1_byte should contain the order-1
582 void ppmc_get_totf_order1(void)
584 // Total cump is current total cump plus the escape for the escape code
585 total_cump=order1_defined_bytes_array[o1_byte]+order1_max_cump_array[o1_byte];
589 // Codes a byte under order-1 and returns 1.
590 // Otherwise it returns a 0 it may be that it has coded an escape code, or
591 // that current table was empty, in this case nothing is outputted
592 // In both cases further coding is needed.
594 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
595 // In case the byte is coded under this context, coded_in_order=1.
596 char ppmc_code_byte_order1(void)
598 unsigned long counter;
601 // First check if current order-1 table is empty
602 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
604 return 0; //byte not coded, nothing done
608 // Now try to code this byte under order-1
610 ppmc_get_totf_order1(); //get total cump
612 // See if the byte is present
613 if(order1_array[o1_byte][byte]==0) //a probability of 0
616 // Because it was not present, output an escape code, prepare variables
618 symb_cump=order1_max_cump_array[o1_byte];//obviously its cump is current max_cump
619 //without escape code's space
621 symb_prob=order1_defined_bytes_array[o1_byte];//the number of defined bytes
623 // Code the escape code
624 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
626 return 0; //byte not coded, escape coded
631 coded_in_order=1; //because we coded it under order-1
633 // The symbol is present, code it under order-1
635 symb_prob=order1_array[o1_byte][byte]; //get probability directly
637 // Make cump for current symbol
639 symb_cump=0; //for first symbol is 0
640 for(counter=0; counter!=byte ; ++counter)
641 symb_cump+=order1_array[o1_byte][counter]; //sum probabilities before our symbol
644 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
646 return 1; //symbol coded under order-1
651 // This functions update order-1 probabilities with current byte, also takes
652 // care about updating variables, and renormalizing. o1_byte must be filled.
653 void ppmc_update_order1(void)
655 if(order1_array[o1_byte][byte]==0)
657 // It had a zero probability
658 order1_array[o1_byte][byte]++; //increment symbol probability
659 ++order1_defined_bytes_array[o1_byte]; //new byte defined
660 ++order1_max_cump_array[o1_byte]; //total cump
665 // It had a non-zero probability
667 // Increment its probability
668 order1_array[o1_byte][byte]++; //increment symbol probability
669 ++order1_max_cump_array[o1_byte]; //total cump
671 // Check to see if its the maximum in this case renormalize
672 if(order1_array[o1_byte][byte]==255)
673 ppmc_renormalize_order1();
680 // This functions renormalizes the probabilities at order-1 updating variables
681 void ppmc_renormalize_order1(void)
683 unsigned long counter;
685 // Initialize variables
686 order1_defined_bytes_array[o1_byte]=0; //clear them
687 order1_max_cump_array[o1_byte]=0;
689 // Loop over all probabilities, divide them by a factor of 2 and update variables
690 for(counter=0 ; counter!=256 ; ++counter)
692 order1_array[o1_byte][counter]>>=1; //divide by a factor of 2
694 if(order1_array[o1_byte][counter]!=0) //see if it has a non zero probability
695 order1_defined_bytes_array[o1_byte]++;
697 order1_max_cump_array[o1_byte]+=order1_array[o1_byte][counter]; //sum to the total cump
702 // Decodes the symbol correspoding to the current order, it returns the byte
703 // or in case of an escape code or empty table it returns -1.
704 // It updates "coded_in_order".
705 void ppmc_decode_order1(void)
707 unsigned long current_cump, counter;
710 // First check if current order-1 table is empty
711 if(order1_defined_bytes_array[o1_byte]==0) //it's empty
713 byte=-1; //byte not coded, nothing done
718 // Get the total cump needed for decoding symbol
719 ppmc_get_totf_order1(); //total cump needed for decoding
720 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
722 // Now check if it's an escape code
723 if(symb_cump>=order1_max_cump_array[o1_byte]) //the defined code space for the escape code
726 // Update coding values
728 ppmc_get_escape_prob_order1();
729 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
731 // Mark as escape code
735 return; //an escape code
739 // Now we have to check what symbol it is
741 current_cump=0; //cump of the current symbol
743 for(counter=0 ; counter!= 256 ; ++counter)
745 if(symb_cump<current_cump)
746 break; //symbol found, break search loop
747 current_cump+=order1_array[o1_byte][counter]; //update cump
750 counter--; //-1 (see ac_ppmc.html, searching for a given symbol)
752 byte=counter; //update the variable with the found symbol
755 // Get the cump and prob of byte
757 symb_prob=order1_array[o1_byte][byte]; //the probabilty
758 symb_cump=current_cump-order1_array[o1_byte][byte]; //using the cump which was
761 // Update coding state
763 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
766 // Update coded_in_order used for update exclusion
776 // This function returns the probability for the escape codes in the variables
777 void ppmc_get_escape_prob_order1(void)
779 // To understand that remember that the escape allocated for the escape code
780 // is above the current maximum cump and that it has a probability determined
782 symb_prob=order1_defined_bytes_array[o1_byte];
783 symb_cump=order1_max_cump_array[o1_byte];
784 total_cump=order1_defined_bytes_array[o1_byte]+order1_max_cump_array[o1_byte];
791 // Order-2 uses a table which holds the contexts data structure of every
792 // structure, its accessed with "ppmc_order2_hash_key(o1_byte, o2_byte)"
793 // and because it uses direct addressing there's no need to check for hash
794 // collisions, which makes it faster. The variable "o2_cntxt" contains o1_byte
795 // and o2_byte used directly to index the order-2 array.
797 // Though order-2 uses the same routines as lower orders they are rather
798 // different, because we can't allocate memory for all the probabilities
799 // tables, we have to use linked lists for the bytes and probabilities.
801 // Under order-2 we don't let a probability to be lower than 1, that is,
802 // there can't be a symbol in a linked list with a 0 probability, I choosed
803 // this for three factors: the code is easier to read (it isn't messy)
804 // we gain some speed, and the loss of compression seems little.
806 // To ensure that the updating is done correctly, "o2_cntxt" and
807 // "o2_ll_node" must not be modified by any routine except those who manage
811 // Returns in the variable "total_cump" the current total cump of
812 // order-2. cntxt must have o1_byte and o2_byte hashed with the hash key
814 void ppmc_get_totf_order2(void)
816 // Total cump is current total cump plus the escape for the escape code
817 total_cump=order2_array[o2_cntxt].defined_bytes+order2_array[o2_cntxt].max_cump;
821 // Codes a byte under order-2 and returns 1.
822 // Otherwise it returns a 0. It may be that it has coded an escape code, or
823 // that current table was empty.
825 // Returns: 1 in case a byte was coded. 0 in case of escape code or empty table.
826 // In case the byte is coded under this context, coded_in_order=2.
827 char ppmc_code_byte_order2(void)
829 unsigned long counter;
830 struct _byte_and_freq *node;
833 // Initialize o2_cntxt
835 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
838 // First check if current order-2 context is empty
839 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
841 return 0; //byte not coded, nothing done
845 // Now try to code this byte under order-2
847 ppmc_get_totf_order2(); //get total cump
850 // See if the byte is present and compute its cump at the same time
852 node=order2_array[o2_cntxt].prob; //pointer to first element in the linked list
854 symb_cump=0; //the first symbol always has a 0 cump
857 // Now search the byte in the linked list
861 goto ppmc_o2_byte_found; //bad thing, I know, anyone has a better idea?
862 symb_cump+=node->freq; //add the probability of this byte to the cump
865 node=node->next; //next element in the linked list
869 // If we reach that point, the byte was not found in the linked list
870 // so we don't need the cump, we have to output an escape code, whose
871 // probabilities are know using the context structure in the table.
873 // Byte was not present in the linked list, current node is the last one,
874 // and that's the node needed for creating a new node, save it.
878 // Now get the probability and cump of the escape code
880 symb_cump=order2_array[o2_cntxt].max_cump;
881 symb_prob=order2_array[o2_cntxt].defined_bytes;
883 // Code the escape code
884 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
889 // That code is executed when the byte is found in the linked list
894 // Everything has been tested, now we can feel free to code the byte,
895 // the symb_cump is already computed, now get its probability and code
896 // the byte, also save pointer to this element in the linked lists for
899 coded_in_order=2; //successfully coded under order-2
901 o2_ll_node=node; //save it for updating
903 symb_prob=node->freq; //get the probability of the byte
907 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
909 return 1; //byte coded under order-2
913 // This functions update order-2 probabilities with current byte, also takes
914 // care about updating variables, and renormalizing.
915 // Of course "o2_ll_node" must point to the element to update or the last one,
916 // based on the "coded_in_order" and checking the pointer of the element we
917 // can decide what to do.
919 // This updating is only for encoding.
920 void ppmc_update_order2(void)
923 // First of all check if that's the first byte in this context, in that case
924 // we have to initialize some variables in the context structure.
926 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
929 if(ppmc_out_of_memory==1)
930 return; //exit this function, we can't allocate memory
932 order2_array[o2_cntxt].defined_bytes=1;
933 order2_array[o2_cntxt].max_cump=1;
934 order2_array[o2_cntxt].prob=_bytes_pool;
936 _bytes_pool->byte=byte; //initialize byte to current one
937 _bytes_pool->freq=1; //it appeared once
938 _bytes_pool->next=0; //now this is last element in ll
940 // Do update of linked list variables and memory use
942 ++_bytes_pool; //next time use next entry (this is a pointer)
944 if(_bytes_pool==_bytes_pool_max) //maximum reached
947 // First check to see that we still have entries in the array
949 if(_bytes_pool_index==_mempool_max_index)
951 ppmc_out_of_memory=1; //flush
955 // Allocate memory for new buffer
957 if((_bytes_pool=(struct _byte_and_freq *)malloc
958 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
960 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
961 ppmc_out_of_memory=1; //flush
965 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
967 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
973 return; //nothing else to do
977 // The byte was coded under order two, otherwise it was coded under a
978 // lower order (never higher ones, remember that we are using update
979 // exclusion) in this case we have to create a new node in the list.
981 if(coded_in_order==2) //coded under order-2
984 // Update its count and variables of this context and check for renormalization
986 o2_ll_node->freq++; //increment its frequency (rather probability)
988 order2_array[o2_cntxt].max_cump++; //total cump
990 if(o2_ll_node->freq==255) //do we have to renormalize?
991 ppmc_renormalize_order2(); //renormalize
997 // Once every paranoid check has been done we are sure that this byte
998 // did not existed and so we have to create a new node in the linked
999 // list. Also we have to take care of memory issues.
1001 if(ppmc_out_of_memory==1)
1002 return; //exit this function, we can't allocate mem
1004 o2_ll_node->next=_bytes_pool; //put it in the next free entry
1005 _bytes_pool->byte=byte; //initialize byte to current one
1006 _bytes_pool->freq=1; //it appeared once
1007 _bytes_pool->next=0; //now this is last element in ll
1009 order2_array[o2_cntxt].max_cump++; //total cump
1010 order2_array[o2_cntxt].defined_bytes++; //total cump
1012 // Do update of linked list variables and memory use
1014 ++_bytes_pool; //next time use next entry (this is a pointer)
1016 if(_bytes_pool==_bytes_pool_max) //maximum reached
1019 // First check to see that we still have entries in the array
1021 if(_bytes_pool_index==_mempool_max_index)
1023 ppmc_out_of_memory=1; //flush
1027 // Allocate memory for new buffer
1029 if((_bytes_pool=(struct _byte_and_freq *)malloc
1030 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1032 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1033 ppmc_out_of_memory=1; //flush
1037 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1039 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1041 _bytes_pool_index++;
1050 // This functions renormalizes the probabilities at order-2 updating context
1052 void ppmc_renormalize_order2(void)
1054 unsigned long counter;
1055 struct _byte_and_freq *node;
1057 // Initialize variables. Defined bytes remain the same.
1058 order2_array[o2_cntxt].max_cump=0; //clear them
1060 node=order2_array[o2_cntxt].prob; //get pointer to lined lists
1062 // Divide all the probabilities by 2 and update context variables
1065 node->freq>>=1; //divide by a factor of 2
1067 if(node->freq==0) //don't allow a probability to be 0
1070 order2_array[o2_cntxt].max_cump+=node->freq; //sum to the total cump
1072 if(node->next==0) //last element
1078 //printf("\nRenormalization, context:%c%c",o2_byte,o1_byte);
1083 // Decodes the symbol correspoding to the current order, it returns the byte
1084 // or in case of an escape code or empty table it returns -1.
1085 // It updates "coded_in_order".
1087 // If we decode an escape code, we don't modify "o2_ll_node" and thus its
1088 // work of the updating routine to read till the end of the linked list
1089 // (for adding a new element) however if we decode a byte, we save on
1090 // "o2_ll_node" the pointer to its node. (so updating is faster)
1091 void ppmc_decode_order2(void)
1093 unsigned long current_cump, counter;
1094 struct _byte_and_freq *node;
1096 // Initialize o2_cntxt
1098 o2_cntxt=ppmc_order2_hash_key(o1_byte,o2_byte);
1101 // First check if current order-2 context is empty
1102 if(order2_array[o2_cntxt].defined_bytes==0) //it's empty
1104 byte=-1; //byte not coded, nothing done
1109 // Get the total cump needed for decoding symbol
1110 ppmc_get_totf_order2(); //total cump needed for decoding
1111 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
1113 // Now check if it's an escape code
1114 if(symb_cump>=order2_array[o2_cntxt].max_cump) //the defined code space for the escape code
1117 // Update coding values
1119 ppmc_get_escape_prob_order2();
1120 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1122 // Mark as escape code
1126 return; //an escape code
1130 // Now we have to check what symbol it is
1132 current_cump=0; //cump of the current symbol
1134 node=order2_array[o2_cntxt].prob; //get pointer to linked lists
1138 current_cump+=node->freq; //update cump
1139 if(symb_cump<current_cump)
1140 break; //symbol found, break search loop
1142 node=node->next; //next element
1143 //we have no need to check for the last symbol, we'll never read further
1144 //the end of the linked lists, before we'll found the last byte.
1148 //read byte value and probability
1150 symb_prob=node->freq; //get the probability for updating the state
1151 byte=node->byte; //get byte
1152 o2_ll_node=node; //used for updating
1155 // Get the cump of byte
1157 symb_cump=current_cump-symb_prob;
1159 // Update coding state
1161 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1163 // Update coded_in_order used for update exclusion
1173 // This is the routine for updating while decoding. We have to search the byte
1174 // in the linked list, if it's present, update its count, otherwise we have
1175 // hitted the end of the linked list, and there we have to create a new node.
1177 // Of course if the byte was matched in order-2 we'll have a pointer to it
1178 // in "o2_ll_node" so we don't need to read the linked list. (we already did
1181 // Another case which we also have to specially deal with, this is the case
1182 // when the context has not been initalized yet.
1183 void ppmc_update_dec_order2(void)
1185 struct _byte_and_freq *node;
1188 // Handle the case when the context is not initialized
1189 // This code is the same as the one for the encoding.
1191 if(order2_array[o2_cntxt].defined_bytes==0) //no byte defined yet
1194 if(ppmc_out_of_memory==1)
1195 return; //exit this function, we can't allocate memory
1197 order2_array[o2_cntxt].defined_bytes=1;
1198 order2_array[o2_cntxt].max_cump=1;
1199 order2_array[o2_cntxt].prob=_bytes_pool;
1201 _bytes_pool->byte=byte; //initialize byte to current one
1202 _bytes_pool->freq=1; //it appeared once
1203 _bytes_pool->next=0; //now this is last element in ll
1205 // Do update of linked list variables and memory use
1207 ++_bytes_pool; //next time use next entry (this is a pointer)
1209 if(_bytes_pool==_bytes_pool_max) //maximum reached
1212 // First check to see that we still have entries in the array
1214 if(_bytes_pool_index==_mempool_max_index)
1216 ppmc_out_of_memory=1; //flush
1220 // Allocate memory for new buffer
1222 if((_bytes_pool=(struct _byte_and_freq *)malloc
1223 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1225 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1226 ppmc_out_of_memory=1; //flush
1230 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1232 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1234 _bytes_pool_index++;
1239 return; //nothing else to do
1243 // Current context is initalized, proceed
1245 if(coded_in_order==2) //check if it was decoded under order-2
1248 // We can be sure that the pointer "o2_ll_node" points to its entry, and
1249 // it has a non 0 probability (otherwise it couldn't be coded) so just
1250 // update its probability and max_cump
1252 o2_ll_node->freq++; //the probability of the byte
1253 order2_array[o2_cntxt].max_cump++; //the max_cump
1255 if(o2_ll_node->freq==255) //check for renormalization
1256 ppmc_renormalize_order2();
1262 // An escape code was decoded under order-2, we have to read till the
1263 // end of the linked list so we can add a new node for this new byte.
1265 node=order2_array[o2_cntxt].prob; //get pointer to linked list
1269 if(node->next==0) //check for the end of the linked list
1271 node=node->next; //next node
1275 // We reached the end of the linked list, add a new node if possible,
1276 // we are using the same code of "ppmc_update_order2()" with the
1277 // difference that the pointer to the linked list is "node"
1279 if(ppmc_out_of_memory==1)
1280 return; //exit this function, we can't allocate mem
1282 node->next=_bytes_pool; //put it in the next free entry
1283 _bytes_pool->byte=byte; //initialize byte to current one
1284 _bytes_pool->freq=1; //it appeared once
1285 _bytes_pool->next=0; //now this is last element in ll
1287 order2_array[o2_cntxt].max_cump++; //total cump
1288 order2_array[o2_cntxt].defined_bytes++; //total cump
1290 // Do update of linked list variables and memory use
1292 ++_bytes_pool; //next time use next entry (this is a pointer)
1294 if(_bytes_pool==_bytes_pool_max) //maximum reached
1297 // First check to see that we still have entries in the array
1299 if(_bytes_pool_index==_mempool_max_index)
1301 ppmc_out_of_memory=1; //flush
1305 // Allocate memory for new buffer
1307 if((_bytes_pool=(struct _byte_and_freq *)malloc
1308 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1310 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1311 ppmc_out_of_memory=1; //flush
1315 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1317 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1319 _bytes_pool_index++;
1323 return; //we are finished updating
1330 // This function returns the probability for the escape codes in the variables
1331 void ppmc_get_escape_prob_order2(void)
1333 // To understand that remember that the escape allocated for the escape code
1334 // is above the current maximum cump and that it has a probability determined
1337 symb_prob=order2_array[o2_cntxt].defined_bytes;
1338 symb_cump=order2_array[o2_cntxt].max_cump;
1343 // ORDER-3 functions
1345 // The difference between order-3 and order-3 are just a few, instead of
1346 // keeping a table with the context structures, we keep a hash table with
1347 // pointers to linked lists with the context, so it's only a matter of
1348 // searching current context in the linked list corresponding to its hash
1349 // entry. This is done in "ppmc_get_totf_order3" because that's the first
1350 // routine that both encoding and decoding routines call.
1353 // Returns in the variable "total_cump" the current total cump of
1354 // order-3. Must be called while encoding or decoding before anything else
1355 // because it initializes the pointers to the context structure in
1356 // "o3_context" and o3_cntxt.
1358 // If the hash entry is not initialized it returns "o3_context"=0
1359 // If the context is not present in the linked list of context, "o3_context"
1360 // will point to the last element in the linked list.
1361 // If the context is present "o3_context" will point to the context to use.
1362 // One can distinguish the last two by checking the context value of the
1363 // structure, if it's not the same, is the last element.
1365 // The routine returns 0 if the hash entry is not initialized or if the
1366 // the context was not present. Otherwise it returns 1, meaning that we
1367 // have to code under this context.
1368 char ppmc_get_totf_order3(void)
1370 struct context *cntxt_node;
1373 // First make the hash key for order-3
1375 o3_cntxt=ppmc_order3_hash_key(o1_byte,o2_byte,o3_byte);
1376 full_o3_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16); //order-3
1379 // Now check the hash entry in the table
1381 if(order3_hasht[o3_cntxt]==0) //if 0, not initialized
1384 o3_context=0; //no hash entry
1386 return 0; //hash entry not initialized
1390 // Now read trough the linked list of context searching current one
1392 cntxt_node=order3_hasht[o3_cntxt];
1397 if(cntxt_node->order4321==full_o3_cntxt) //compare context
1398 goto ppmc_gtf_cntxt_found;
1400 if(cntxt_node->next==0) //end of context's linked list
1403 cntxt_node=cntxt_node->next; //next element
1408 // Once there the context was not found
1409 o3_context=cntxt_node; //pointer to last element in the linked list
1411 return 0; //it was not present
1414 // The context is found, so return pointer and cump
1416 ppmc_gtf_cntxt_found:
1418 o3_context=cntxt_node;
1420 // Total cump is current total cump plus the escape for the escape code
1422 total_cump=o3_context->defined_bytes+o3_context->max_cump;
1424 return 1; //context found
1429 // Codes a byte under order-3 and returns 1.
1430 // Otherwise it returns a 0.
1432 // In case the byte is coded under this context, coded_in_order=3.
1433 char ppmc_code_byte_order3(void)
1435 unsigned long counter;
1436 struct _byte_and_freq *node;
1439 // Get current context (if present) and total cump.
1441 if(ppmc_get_totf_order3()==0)
1445 // See if the byte is present and compute its cump at the same time
1447 node=o3_context->prob; //pointer to first element in the linked list
1449 symb_cump=0; //the first symbol always has a 0 cump
1452 // Now search the byte in the linked list
1455 if(node->byte==byte)
1456 goto ppmc_o3_byte_found; //bad thing, I know, anyone has a better idea?
1457 symb_cump+=node->freq; //add the probability of this byte to the cump
1460 node=node->next; //next element in the linked list
1464 // If we reach that point, the byte was not found in the linked list
1465 // so we don't need the cump, we have to output an escape code, whose
1466 // probabilities are know using the context structure in the table.
1468 // Byte was not present in the linked list, current node is the last one,
1469 // and that's the node needed for creating a new node, save it.
1473 // Now get the probability and cump of the escape code
1475 symb_cump=o3_context->max_cump;
1476 symb_prob=o3_context->defined_bytes;
1478 // Code the escape code
1479 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
1484 // That code is executed when the byte is found in the linked list
1489 // Everything has been tested, now we can feel free to code the byte,
1490 // the symb_cump is already computed, now get its probability and code
1491 // the byte, also save pointer to this element in the linked lists for
1494 coded_in_order=3; //successfully coded under order-3
1496 o3_ll_node=node; //save it for updating
1498 symb_prob=node->freq; //get the probability of the byte
1502 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
1504 return 1; //byte coded under order-3
1508 // This functions update order-3 probabilities with current byte, also takes
1509 // care about updating variables, and renormalizing.
1511 // "o3_ll_node" must point to the element to update or the last one,
1512 // based on the "coded_in_order" and checking the pointer of the element we
1513 // can decide what to do. Also "o3_context" must be initialized.
1515 // This updating is only for encoding.
1516 void ppmc_update_order3(void)
1519 // First thing first, check if the hash entry is initialized
1521 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
1524 if(ppmc_out_of_memory==1)
1525 return; //exit this function, we can't allocate memory
1528 // First create the context
1530 order3_hasht[o3_cntxt]=_context_pool;
1532 _context_pool->next=0; //this is the last element
1533 _context_pool->order4321=full_o3_cntxt; //put context
1534 _context_pool->prob=_bytes_pool; //pointer to linked list
1535 _context_pool->max_cump=1;
1536 _context_pool->defined_bytes=1;
1539 // Do update of linked list variables and memory use of contexts
1541 ++_context_pool; //next time use next entry (this is a pointer)
1543 if(_context_pool==_context_pool_max) //maximum reached
1546 // First check to see that we still have entries in the array
1548 if(_context_pool_index==_mempool_max_index)
1550 ppmc_out_of_memory=1; //flush
1554 // Allocate memory for new buffer
1556 if((_context_pool=(struct context *)malloc
1557 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1559 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1560 ppmc_out_of_memory=1; //flush
1564 _context_pool_array[_context_pool_index]=_context_pool;
1566 _context_pool_max=_context_pool+_context_pool_elements_inc;
1568 _context_pool_index++;
1573 // Now care about the first (and last) linked list element
1575 _bytes_pool->byte=byte; //initialize byte to current one
1576 _bytes_pool->freq=1; //it appeared once
1577 _bytes_pool->next=0; //now this is last element in ll
1579 // Do update of linked list variables and memory use
1581 ++_bytes_pool; //next time use next entry (this is a pointer)
1583 if(_bytes_pool==_bytes_pool_max) //maximum reached
1586 // First check to see that we still have entries in the array
1588 if(_bytes_pool_index==_mempool_max_index)
1590 ppmc_out_of_memory=1; //flush
1594 // Allocate memory for new buffer
1596 if((_bytes_pool=(struct _byte_and_freq *)malloc
1597 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1599 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1600 ppmc_out_of_memory=1; //flush
1604 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1606 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1608 _bytes_pool_index++;
1612 return; //nothing else to do
1618 // The byte was coded under order three, otherwise it was coded under a
1619 // lower order (never higher ones, remember that we are using update
1620 // exclusion) in this case we have to create a new node in the list.
1622 if(coded_in_order==3) //coded under order-3
1625 // Update its count and variables of this context and check for renormalization
1627 o3_ll_node->freq++; //increment its frequency (rather probability)
1629 o3_context->max_cump++; //total cump
1631 if(o3_ll_node->freq==255) //do we have to renormalize?
1632 ppmc_renormalize_order3(); //renormalize
1638 // Now we have two cases, under a given context (which we actually found)
1639 // we coded an escape coded, in that case just create a new node in the
1640 // linked list of bytes and probabilities. Otherwise we didn't find the
1641 // same node so we have to create it in the linked list for context.
1642 // And we can be sure that it at least has one element and that
1643 // "o3_context" points to the last element, so we can put the new element.
1645 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
1646 { //element or the a context found
1648 if(ppmc_out_of_memory==1)
1649 return; //exit this function, we can't allocate mem
1651 o3_ll_node->next=_bytes_pool; //put it in the next free entry
1652 _bytes_pool->byte=byte; //initialize byte to current one
1653 _bytes_pool->freq=1; //it appeared once
1654 _bytes_pool->next=0; //now this is last element in ll
1656 o3_context->max_cump++; //total cump
1657 o3_context->defined_bytes++; //total cump
1659 // Do update of linked list variables and memory use
1661 ++_bytes_pool; //next time use next entry (this is a pointer)
1663 if(_bytes_pool==_bytes_pool_max) //maximum reached
1666 // First check to see that we still have entries in the array
1668 if(_bytes_pool_index==_mempool_max_index)
1670 ppmc_out_of_memory=1; //flush
1674 // Allocate memory for new buffer
1676 if((_bytes_pool=(struct _byte_and_freq *)malloc
1677 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1679 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1680 ppmc_out_of_memory=1; //flush
1684 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1686 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1688 _bytes_pool_index++;
1695 // We have to create a new context node
1697 if(ppmc_out_of_memory==1)
1698 return; //exit this function, we can't allocate memory
1701 // First create the context
1703 o3_context->next=_context_pool;
1705 _context_pool->next=0; //this is the last element
1706 _context_pool->order4321=full_o3_cntxt; //put context
1707 _context_pool->prob=_bytes_pool; //pointer to linked list
1708 _context_pool->max_cump=1;
1709 _context_pool->defined_bytes=1;
1712 // Do update of linked list variables and memory use of contexts
1714 ++_context_pool; //next time use next entry (this is a pointer)
1716 if(_context_pool==_context_pool_max) //maximum reached
1719 // First check to see that we still have entries in the array
1721 if(_context_pool_index==_mempool_max_index)
1723 ppmc_out_of_memory=1; //flush
1727 // Allocate memory for new buffer
1729 if((_context_pool=(struct context *)malloc
1730 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1732 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1733 ppmc_out_of_memory=1; //flush
1737 _context_pool_array[_context_pool_index]=_context_pool;
1739 _context_pool_max=_context_pool+_context_pool_elements_inc;
1741 _context_pool_index++;
1745 // Now care about the first (and last) linked list element
1747 _bytes_pool->byte=byte; //initialize byte to current one
1748 _bytes_pool->freq=1; //it appeared once
1749 _bytes_pool->next=0; //now this is last element in ll
1751 // Do update of linked list variables and memory use
1753 ++_bytes_pool; //next time use next entry (this is a pointer)
1755 if(_bytes_pool==_bytes_pool_max) //maximum reached
1758 // First check to see that we still have entries in the array
1760 if(_bytes_pool_index==_mempool_max_index)
1762 ppmc_out_of_memory=1; //flush
1766 // Allocate memory for new buffer
1768 if((_bytes_pool=(struct _byte_and_freq *)malloc
1769 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1771 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1772 ppmc_out_of_memory=1; //flush
1776 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
1778 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
1780 _bytes_pool_index++;
1791 // This functions renormalizes the probabilities at order-3 updating context
1793 void ppmc_renormalize_order3(void)
1795 unsigned long counter;
1796 struct _byte_and_freq *node;
1799 // Initialize variables. Defined bytes remain the same.
1800 o3_context->max_cump=0; //clear them
1802 node=o3_context->prob; //get pointer to lined lists
1804 // Divide all the probabilities by 2 and update context variables
1807 node->freq>>=1; //divide by a factor of 2
1809 if(node->freq==0) //don't allow a probability to be 0
1812 o3_context->max_cump+=node->freq; //sum to the total cump
1814 if(node->next==0) //last element
1823 // Decodes the symbol correspoding to the current order, it returns the byte
1824 // or in case of an escape code or empty table it returns -1.
1825 // It updates "coded_in_order".
1827 // If we decode an escape code, we don't modify "o3_ll_node" and thus its
1828 // work of the updating routine to read till the end of the linked list
1829 // (for adding a new element) however if we decode a byte, we save on
1830 // "o3_ll_node" the pointer to its node. (so updating is faster)
1831 void ppmc_decode_order3(void)
1833 unsigned long current_cump, counter;
1834 struct _byte_and_freq *node;
1837 // Get current context (if present) and total cump.
1839 if(ppmc_get_totf_order3()==0)
1846 // Decode current cump
1848 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
1850 // Now check if it's an escape code
1851 if(symb_cump>=o3_context->max_cump) //the defined code space for the escape code
1854 // Update coding values
1856 ppmc_get_escape_prob_order3();
1857 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1859 // Mark as escape code
1863 return; //an escape code
1867 // Now we have to check what symbol it is
1869 current_cump=0; //cump of the current symbol
1871 node=o3_context->prob; //get pointer to linked lists
1875 current_cump+=node->freq; //update cump
1876 if(symb_cump<current_cump)
1877 break; //symbol found, break search loop
1879 node=node->next; //next element
1880 //we have no need to check for the last symbol, we'll never read further
1881 //the end of the linked lists, before we'll found the last byte.
1885 //read byte value and probability
1887 symb_prob=node->freq; //get the probability for updating the state
1888 byte=node->byte; //get byte
1889 o3_ll_node=node; //used for updating
1892 // Get the cump of byte
1894 symb_cump=current_cump-symb_prob;
1896 // Update coding state
1898 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
1900 // Update coded_in_order used for update exclusion
1910 // This is the routine for updating while decoding. The only difference with
1911 // the routine for coding is that when an escape code was coded, "o3_ll_node"
1912 // is not initialized so we have to read till the end of the linked list.
1913 // Fortunately "o3_context" will be initialized so we don't need to read its
1915 void ppmc_update_dec_order3(void)
1917 struct _byte_and_freq *node;
1919 // First thing first, check if the hash entry is initialized
1921 if(order3_hasht[o3_cntxt]==0) //no pointer to linked list defined yet
1924 if(ppmc_out_of_memory==1)
1925 return; //exit this function, we can't allocate memory
1928 // First create the context
1930 order3_hasht[o3_cntxt]=_context_pool;
1932 _context_pool->next=0; //this is the last element
1933 _context_pool->order4321=full_o3_cntxt; //put context
1934 _context_pool->prob=_bytes_pool; //pointer to linked list
1935 _context_pool->max_cump=1;
1936 _context_pool->defined_bytes=1;
1939 // Do update of linked list variables and memory use of contexts
1941 ++_context_pool; //next time use next entry (this is a pointer)
1943 if(_context_pool==_context_pool_max) //maximum reached
1946 // First check to see that we still have entries in the array
1948 if(_context_pool_index==_mempool_max_index)
1950 ppmc_out_of_memory=1; //flush
1954 // Allocate memory for new buffer
1956 if((_context_pool=(struct context *)malloc
1957 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
1959 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
1960 ppmc_out_of_memory=1; //flush
1964 _context_pool_array[_context_pool_index]=_context_pool;
1966 _context_pool_max=_context_pool+_context_pool_elements_inc;
1968 _context_pool_index++;
1972 // Now care about the first (and last) linked list element
1974 _bytes_pool->byte=byte; //initialize byte to current one
1975 _bytes_pool->freq=1; //it appeared once
1976 _bytes_pool->next=0; //now this is last element in ll
1978 // Do update of linked list variables and memory use
1980 ++_bytes_pool; //next time use next entry (this is a pointer)
1982 if(_bytes_pool==_bytes_pool_max) //maximum reached
1985 // First check to see that we still have entries in the array
1987 if(_bytes_pool_index==_mempool_max_index)
1989 ppmc_out_of_memory=1; //flush
1993 // Allocate memory for new buffer
1995 if((_bytes_pool=(struct _byte_and_freq *)malloc
1996 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
1998 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
1999 ppmc_out_of_memory=1; //flush
2003 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2005 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2007 _bytes_pool_index++;
2011 return; //nothing else to do
2015 // The byte was coded under order three, otherwise it was coded under a
2016 // lower order (never higher ones, remember that we are using update
2017 // exclusion) in this case we have to create a new node in the list.
2019 if(coded_in_order==3) //coded under order-3
2022 // Update its count and variables of this context and check for renormalization
2024 o3_ll_node->freq++; //increment its frequency (rather probability)
2026 o3_context->max_cump++; //total cump
2028 if(o3_ll_node->freq==255) //do we have to renormalize?
2029 ppmc_renormalize_order3(); //renormalize
2035 // Now we have two cases, under a given context (which we actually found)
2036 // we coded an escape coded, in that case just create a new node in the
2037 // linked list of bytes and probabilities. Otherwise we didn't find the
2038 // same node so we have to create it in the linked list for context.
2039 // And we can be sure that it at least has one element and that
2040 // "o3_context" points to the last element, so we can put the new element.
2042 if(o3_context->order4321==full_o3_cntxt) //chech if that's the last
2043 { //element or the a context found
2045 // Read till the end of the linked list
2047 node=o3_context->prob; //get pointer to linked list
2051 if(node->next==0) //check for the end of the linked list
2053 node=node->next; //next node
2058 if(ppmc_out_of_memory==1)
2059 return; //exit this function, we can't allocate mem
2061 node->next=_bytes_pool; //put it in the next free entry
2062 _bytes_pool->byte=byte; //initialize byte to current one
2063 _bytes_pool->freq=1; //it appeared once
2064 _bytes_pool->next=0; //now this is last element in ll
2066 o3_context->max_cump++; //total cump
2067 o3_context->defined_bytes++; //total cump
2069 // Do update of linked list variables and memory use
2071 ++_bytes_pool; //next time use next entry (this is a pointer)
2073 if(_bytes_pool==_bytes_pool_max) //maximum reached
2076 // First check to see that we still have entries in the array
2078 if(_bytes_pool_index==_mempool_max_index)
2080 ppmc_out_of_memory=1; //flush
2084 // Allocate memory for new buffer
2086 if((_bytes_pool=(struct _byte_and_freq *)malloc
2087 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2089 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2090 ppmc_out_of_memory=1; //flush
2094 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2096 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2098 _bytes_pool_index++;
2105 // We have to create a new context node
2107 if(ppmc_out_of_memory==1)
2108 return; //exit this function, we can't allocate memory
2111 // First create the context
2113 o3_context->next=_context_pool;
2115 _context_pool->next=0; //this is the last element
2116 _context_pool->order4321=full_o3_cntxt; //put context
2117 _context_pool->prob=_bytes_pool; //pointer to linked list
2118 _context_pool->max_cump=1;
2119 _context_pool->defined_bytes=1;
2122 // Do update of linked list variables and memory use of contexts
2124 ++_context_pool; //next time use next entry (this is a pointer)
2126 if(_context_pool==_context_pool_max) //maximum reached
2129 // First check to see that we still have entries in the array
2131 if(_context_pool_index==_mempool_max_index)
2133 ppmc_out_of_memory=1; //flush
2137 // Allocate memory for new buffer
2139 if((_context_pool=(struct context *)malloc
2140 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2142 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2143 ppmc_out_of_memory=1; //flush
2147 _context_pool_array[_context_pool_index]=_context_pool;
2149 _context_pool_max=_context_pool+_context_pool_elements_inc;
2151 _context_pool_index++;
2155 // Now care about the first (and last) linked list element
2157 _bytes_pool->byte=byte; //initialize byte to current one
2158 _bytes_pool->freq=1; //it appeared once
2159 _bytes_pool->next=0; //now this is last element in ll
2161 // Do update of linked list variables and memory use
2163 ++_bytes_pool; //next time use next entry (this is a pointer)
2165 if(_bytes_pool==_bytes_pool_max) //maximum reached
2168 // First check to see that we still have entries in the array
2170 if(_bytes_pool_index==_mempool_max_index)
2172 ppmc_out_of_memory=1; //flush
2176 // Allocate memory for new buffer
2178 if((_bytes_pool=(struct _byte_and_freq *)malloc
2179 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2181 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2182 ppmc_out_of_memory=1; //flush
2186 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2188 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2190 _bytes_pool_index++;
2202 // This function returns the probability for the escape codes in the variables
2203 void ppmc_get_escape_prob_order3(void)
2205 // To understand that remember that the escape allocated for the escape code
2206 // is above the current maximum cump and that it has a probability determined
2209 symb_prob=o3_context->defined_bytes;
2210 symb_cump=o3_context->max_cump;
2215 // ORDER-4 functions
2217 // The routines for order-4 are *equal* to those for order-3, there are a few
2218 // changes like different global variables, and different hash keys.
2220 // If you want to go to higher orders, you'd use the same code and data
2221 // structures, with the difference of the context bytes (order4321)
2222 // stored in every context's linked list.
2225 // Returns in the variable "total_cump" the current total cump of
2226 // order-4. Must be called while encoding or decoding before anything else
2227 // because it initializes the pointers to the context structure in
2228 // "o4_context" and o4_cntxt.
2230 // If the hash entry is not initialized it returns "o4_context"=0
2231 // If the context is not present in the linked list of context, "o4_context"
2232 // will point to the last element in the linked list.
2233 // If the context is present "o4_context" will point to the context to use.
2234 // One can distinguish the last two by checking the context value of the
2235 // structure, if it's not the same, is the last element.
2237 // The routine returns 0 if the hash entry is not initialized or if the
2238 // the context was not present. Otherwise it returns 1, meaning that we
2239 // have to code under this context.
2240 char ppmc_get_totf_order4(void)
2242 struct context *cntxt_node;
2245 // First make the hash key for order-4
2247 o4_cntxt=ppmc_order4_hash_key(o1_byte,o2_byte,o3_byte,o4_byte);
2248 full_o4_cntxt=(o1_byte)+(o2_byte<<8)+(o3_byte<<16)+(o4_byte<<24); //order-4
2251 // Now check the hash entry in the table
2253 if(order4_hasht[o4_cntxt]==0) //if 0, not initialized
2256 o4_context=0; //no hash entry
2258 return 0; //hash entry not initialized
2262 // Now read trough the linked list of context searching current one
2264 cntxt_node=order4_hasht[o4_cntxt];
2269 if(cntxt_node->order4321==full_o4_cntxt) //compare context
2270 goto ppmc_gtfo4_cntxt_found;
2272 if(cntxt_node->next==0) //end of context's linked list
2275 cntxt_node=cntxt_node->next; //next element
2280 // Once there the context was not found
2281 o4_context=cntxt_node; //pointer to last element in the linked list
2283 return 0; //it was not present
2286 // The context is found, so return pointer and cump
2288 ppmc_gtfo4_cntxt_found:
2290 o4_context=cntxt_node;
2292 // Total cump is current total cump plus the escape for the escape code
2294 total_cump=o4_context->defined_bytes+o4_context->max_cump;
2296 return 1; //context found
2301 // Codes a byte under order-4 and returns 1.
2302 // Otherwise it returns a 0.
2304 // In case the byte is coded under this context, coded_in_order=4.
2305 char ppmc_code_byte_order4(void)
2307 unsigned long counter;
2308 struct _byte_and_freq *node;
2311 // Get current context (if present) and total cump.
2313 if(ppmc_get_totf_order4()==0)
2317 // See if the byte is present and compute its cump at the same time
2319 node=o4_context->prob; //pointer to first element in the linked list
2321 symb_cump=0; //the first symbol always has a 0 cump
2324 // Now search the byte in the linked list
2327 if(node->byte==byte)
2328 goto ppmc_o4_byte_found; //bad thing, I know, anyone has a better idea?
2329 symb_cump+=node->freq; //add the probability of this byte to the cump
2332 node=node->next; //next element in the linked list
2336 // If we reach that point, the byte was not found in the linked list
2337 // so we don't need the cump, we have to output an escape code, whose
2338 // probabilities are know using the context structure in the table.
2340 // Byte was not present in the linked list, current node is the last one,
2341 // and that's the node needed for creating a new node, save it.
2345 // Now get the probability and cump of the escape code
2347 symb_cump=o4_context->max_cump;
2348 symb_prob=o4_context->defined_bytes;
2350 // Code the escape code
2351 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
2356 // That code is executed when the byte is found in the linked list
2361 // Everything has been tested, now we can feel free to code the byte,
2362 // the symb_cump is already computed, now get its probability and code
2363 // the byte, also save pointer to this element in the linked lists for
2366 coded_in_order=4; //successfully coded under order-4
2368 o4_ll_node=node; //save it for updating
2370 symb_prob=node->freq; //get the probability of the byte
2374 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
2376 return 1; //byte coded under order-4
2380 // This functions update order-4 probabilities with current byte, also takes
2381 // care about updating variables, and renormalizing.
2383 // "o4_ll_node" must point to the element to update or the last one,
2384 // based on the "coded_in_order" and checking the pointer of the element we
2385 // can decide what to do. Also "o4_context" must be initialized.
2387 // This updating is only for encoding.
2388 void ppmc_update_order4(void)
2391 // First thing first, check if the hash entry is initialized
2393 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
2396 if(ppmc_out_of_memory==1)
2397 return; //exit this function, we can't allocate memory
2400 // First create the context
2402 order4_hasht[o4_cntxt]=_context_pool;
2404 _context_pool->next=0; //this is the last element
2405 _context_pool->order4321=full_o4_cntxt; //put context
2406 _context_pool->prob=_bytes_pool; //pointer to linked list
2407 _context_pool->max_cump=1;
2408 _context_pool->defined_bytes=1;
2411 // Do update of linked list variables and memory use of contexts
2413 ++_context_pool; //next time use next entry (this is a pointer)
2415 if(_context_pool==_context_pool_max) //maximum reached
2418 // First check to see that we still have entries in the array
2420 if(_context_pool_index==_mempool_max_index)
2422 ppmc_out_of_memory=1; //flush
2426 // Allocate memory for new buffer
2428 if((_context_pool=(struct context *)malloc
2429 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2431 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2432 ppmc_out_of_memory=1; //flush
2436 _context_pool_array[_context_pool_index]=_context_pool;
2438 _context_pool_max=_context_pool+_context_pool_elements_inc;
2440 _context_pool_index++;
2445 // Now care about the first (and last) linked list element
2447 _bytes_pool->byte=byte; //initialize byte to current one
2448 _bytes_pool->freq=1; //it appeared once
2449 _bytes_pool->next=0; //now this is last element in ll
2451 // Do update of linked list variables and memory use
2453 ++_bytes_pool; //next time use next entry (this is a pointer)
2455 if(_bytes_pool==_bytes_pool_max) //maximum reached
2458 // First check to see that we still have entries in the array
2460 if(_bytes_pool_index==_mempool_max_index)
2462 ppmc_out_of_memory=1; //flush
2466 // Allocate memory for new buffer
2468 if((_bytes_pool=(struct _byte_and_freq *)malloc
2469 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2471 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2472 ppmc_out_of_memory=1; //flush
2476 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2478 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2480 _bytes_pool_index++;
2484 return; //nothing else to do
2490 // The byte was coded under order three, otherwise it was coded under a
2491 // lower order (never higher ones, remember that we are using update
2492 // exclusion) in this case we have to create a new node in the list.
2494 if(coded_in_order==4) //coded under order-4
2497 // Update its count and variables of this context and check for renormalization
2499 o4_ll_node->freq++; //increment its frequency (rather probability)
2501 o4_context->max_cump++; //total cump
2503 if(o4_ll_node->freq==255) //do we have to renormalize?
2504 ppmc_renormalize_order4(); //renormalize
2510 // Now we have two cases, under a given context (which we actually found)
2511 // we coded an escape coded, in that case just create a new node in the
2512 // linked list of bytes and probabilities. Otherwise we didn't find the
2513 // same node so we have to create it in the linked list for context.
2514 // And we can be sure that it at least has one element and that
2515 // "o4_context" points to the last element, so we can put the new element.
2517 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
2518 { //element or the a context found
2520 if(ppmc_out_of_memory==1)
2521 return; //exit this function, we can't allocate mem
2523 o4_ll_node->next=_bytes_pool; //put it in the next free entry
2524 _bytes_pool->byte=byte; //initialize byte to current one
2525 _bytes_pool->freq=1; //it appeared once
2526 _bytes_pool->next=0; //now this is last element in ll
2528 o4_context->max_cump++; //total cump
2529 o4_context->defined_bytes++; //total cump
2531 // Do update of linked list variables and memory use
2533 ++_bytes_pool; //next time use next entry (this is a pointer)
2535 if(_bytes_pool==_bytes_pool_max) //maximum reached
2538 // First check to see that we still have entries in the array
2540 if(_bytes_pool_index==_mempool_max_index)
2542 ppmc_out_of_memory=1; //flush
2546 // Allocate memory for new buffer
2548 if((_bytes_pool=(struct _byte_and_freq *)malloc
2549 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2551 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2552 ppmc_out_of_memory=1; //flush
2556 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2558 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2560 _bytes_pool_index++;
2567 // We have to create a new context node
2569 if(ppmc_out_of_memory==1)
2570 return; //exit this function, we can't allocate memory
2573 // First create the context
2575 o4_context->next=_context_pool;
2577 _context_pool->next=0; //this is the last element
2578 _context_pool->order4321=full_o4_cntxt; //put context
2579 _context_pool->prob=_bytes_pool; //pointer to linked list
2580 _context_pool->max_cump=1;
2581 _context_pool->defined_bytes=1;
2584 // Do update of linked list variables and memory use of contexts
2586 ++_context_pool; //next time use next entry (this is a pointer)
2588 if(_context_pool==_context_pool_max) //maximum reached
2591 // First check to see that we still have entries in the array
2593 if(_context_pool_index==_mempool_max_index)
2595 ppmc_out_of_memory=1; //flush
2599 // Allocate memory for new buffer
2601 if((_context_pool=(struct context *)malloc
2602 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2604 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2605 ppmc_out_of_memory=1; //flush
2609 _context_pool_array[_context_pool_index]=_context_pool;
2611 _context_pool_max=_context_pool+_context_pool_elements_inc;
2613 _context_pool_index++;
2617 // Now care about the first (and last) linked list element
2619 _bytes_pool->byte=byte; //initialize byte to current one
2620 _bytes_pool->freq=1; //it appeared once
2621 _bytes_pool->next=0; //now this is last element in ll
2623 // Do update of linked list variables and memory use
2625 ++_bytes_pool; //next time use next entry (this is a pointer)
2627 if(_bytes_pool==_bytes_pool_max) //maximum reached
2630 // First check to see that we still have entries in the array
2632 if(_bytes_pool_index==_mempool_max_index)
2634 ppmc_out_of_memory=1; //flush
2638 // Allocate memory for new buffer
2640 if((_bytes_pool=(struct _byte_and_freq *)malloc
2641 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2643 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2644 ppmc_out_of_memory=1; //flush
2648 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2650 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2652 _bytes_pool_index++;
2663 // This functions renormalizes the probabilities at order-4 updating context
2665 void ppmc_renormalize_order4(void)
2667 unsigned long counter;
2668 struct _byte_and_freq *node;
2671 // Initialize variables. Defined bytes remain the same.
2672 o4_context->max_cump=0; //clear them
2674 node=o4_context->prob; //get pointer to lined lists
2676 // Divide all the probabilities by 2 and update context variables
2679 node->freq>>=1; //divide by a factor of 2
2681 if(node->freq==0) //don't allow a probability to be 0
2684 o4_context->max_cump+=node->freq; //sum to the total cump
2686 if(node->next==0) //last element
2695 // Decodes the symbol correspoding to the current order, it returns the byte
2696 // or in case of an escape code or empty table it returns -1.
2697 // It updates "coded_in_order".
2699 // If we decode an escape code, we don't modify "o4_ll_node" and thus its
2700 // work of the updating routine to read till the end of the linked list
2701 // (for adding a new element) however if we decode a byte, we save on
2702 // "o4_ll_node" the pointer to its node. (so updating is faster)
2703 void ppmc_decode_order4(void)
2705 unsigned long current_cump, counter;
2706 struct _byte_and_freq *node;
2709 // Get current context (if present) and total cump.
2711 if(ppmc_get_totf_order4()==0)
2718 // Decode current cump
2720 symb_cump=range_decoder_decode(&rc_decoder,total_cump); //decode it
2722 // Now check if it's an escape code
2723 if(symb_cump>=o4_context->max_cump) //the defined code space for the escape code
2726 // Update coding values
2728 ppmc_get_escape_prob_order4();
2729 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
2731 // Mark as escape code
2735 return; //an escape code
2739 // Now we have to check what symbol it is
2741 current_cump=0; //cump of the current symbol
2743 node=o4_context->prob; //get pointer to linked lists
2747 current_cump+=node->freq; //update cump
2748 if(symb_cump<current_cump)
2749 break; //symbol found, break search loop
2751 node=node->next; //next element
2752 //we have no need to check for the last symbol, we'll never read further
2753 //the end of the linked lists, before we'll found the last byte.
2757 //read byte value and probability
2759 symb_prob=node->freq; //get the probability for updating the state
2760 byte=node->byte; //get byte
2761 o4_ll_node=node; //used for updating
2764 // Get the cump of byte
2766 symb_cump=current_cump-symb_prob;
2768 // Update coding state
2770 range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
2772 // Update coded_in_order used for update exclusion
2782 // This is the routine for updating while decoding. The only difference with
2783 // the routine for coding is that when an escape code was coded, "o4_ll_node"
2784 // is not initialized so we have to read till the end of the linked list.
2785 // Fortunately "o4_context" will be initialized so we don't need to read its
2787 void ppmc_update_dec_order4(void)
2789 struct _byte_and_freq *node;
2791 // First thing first, check if the hash entry is initialized
2793 if(order4_hasht[o4_cntxt]==0) //no pointer to linked list defined yet
2796 if(ppmc_out_of_memory==1)
2797 return; //exit this function, we can't allocate memory
2800 // First create the context
2802 order4_hasht[o4_cntxt]=_context_pool;
2804 _context_pool->next=0; //this is the last element
2805 _context_pool->order4321=full_o4_cntxt; //put context
2806 _context_pool->prob=_bytes_pool; //pointer to linked list
2807 _context_pool->max_cump=1;
2808 _context_pool->defined_bytes=1;
2811 // Do update of linked list variables and memory use of contexts
2813 ++_context_pool; //next time use next entry (this is a pointer)
2815 if(_context_pool==_context_pool_max) //maximum reached
2818 // First check to see that we still have entries in the array
2820 if(_context_pool_index==_mempool_max_index)
2822 ppmc_out_of_memory=1; //flush
2826 // Allocate memory for new buffer
2828 if((_context_pool=(struct context *)malloc
2829 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
2831 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
2832 ppmc_out_of_memory=1; //flush
2836 _context_pool_array[_context_pool_index]=_context_pool;
2838 _context_pool_max=_context_pool+_context_pool_elements_inc;
2840 _context_pool_index++;
2844 // Now care about the first (and last) linked list element
2846 _bytes_pool->byte=byte; //initialize byte to current one
2847 _bytes_pool->freq=1; //it appeared once
2848 _bytes_pool->next=0; //now this is last element in ll
2850 // Do update of linked list variables and memory use
2852 ++_bytes_pool; //next time use next entry (this is a pointer)
2854 if(_bytes_pool==_bytes_pool_max) //maximum reached
2857 // First check to see that we still have entries in the array
2859 if(_bytes_pool_index==_mempool_max_index)
2861 ppmc_out_of_memory=1; //flush
2865 // Allocate memory for new buffer
2867 if((_bytes_pool=(struct _byte_and_freq *)malloc
2868 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2870 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2871 ppmc_out_of_memory=1; //flush
2875 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2877 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2879 _bytes_pool_index++;
2883 return; //nothing else to do
2887 // The byte was coded under order four, otherwise it was coded under a
2888 // lower order (never higher ones, remember that we are using update
2889 // exclusion) in this case we have to create a new node in the list.
2891 if(coded_in_order==4) //coded under order-4
2894 // Update its count and variables of this context and check for renormalization
2896 o4_ll_node->freq++; //increment its frequency (rather probability)
2898 o4_context->max_cump++; //total cump
2900 if(o4_ll_node->freq==255) //do we have to renormalize?
2901 ppmc_renormalize_order4(); //renormalize
2907 // Now we have two cases, under a given context (which we actually found)
2908 // we coded an escape coded, in that case just create a new node in the
2909 // linked list of bytes and probabilities. Otherwise we didn't find the
2910 // same node so we have to create it in the linked list for context.
2911 // And we can be sure that it at least has one element and that
2912 // "o4_context" points to the last element, so we can put the new element.
2914 if(o4_context->order4321==full_o4_cntxt) //chech if that's the last
2915 { //element or the a context found
2917 // Read till the end of the linked list
2919 node=o4_context->prob; //get pointer to linked list
2923 if(node->next==0) //check for the end of the linked list
2925 node=node->next; //next node
2930 if(ppmc_out_of_memory==1)
2931 return; //exit this function, we can't allocate mem
2933 node->next=_bytes_pool; //put it in the next free entry
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 o4_context->max_cump++; //total cump
2939 o4_context->defined_bytes++; //total cump
2941 // Do update of linked list variables and memory use
2943 ++_bytes_pool; //next time use next entry (this is a pointer)
2945 if(_bytes_pool==_bytes_pool_max) //maximum reached
2948 // First check to see that we still have entries in the array
2950 if(_bytes_pool_index==_mempool_max_index)
2952 ppmc_out_of_memory=1; //flush
2956 // Allocate memory for new buffer
2958 if((_bytes_pool=(struct _byte_and_freq *)malloc
2959 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
2961 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
2962 ppmc_out_of_memory=1; //flush
2966 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
2968 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
2970 _bytes_pool_index++;
2977 // We have to create a new context node
2979 if(ppmc_out_of_memory==1)
2980 return; //exit this function, we can't allocate memory
2983 // First create the context
2985 o4_context->next=_context_pool;
2987 _context_pool->next=0; //this is the last element
2988 _context_pool->order4321=full_o4_cntxt; //put context
2989 _context_pool->prob=_bytes_pool; //pointer to linked list
2990 _context_pool->max_cump=1;
2991 _context_pool->defined_bytes=1;
2994 // Do update of linked list variables and memory use of contexts
2996 ++_context_pool; //next time use next entry (this is a pointer)
2998 if(_context_pool==_context_pool_max) //maximum reached
3001 // First check to see that we still have entries in the array
3003 if(_context_pool_index==_mempool_max_index)
3005 ppmc_out_of_memory=1; //flush
3009 // Allocate memory for new buffer
3011 if((_context_pool=(struct context *)malloc
3012 (sizeof(struct context)*_context_pool_elements_inc))==NULL)
3014 printf("Can't allocate memory for context's memory pool.\nIndex: %d",_context_pool_index);
3015 ppmc_out_of_memory=1; //flush
3019 _context_pool_array[_context_pool_index]=_context_pool;
3021 _context_pool_max=_context_pool+_context_pool_elements_inc;
3023 _context_pool_index++;
3027 // Now care about the first (and last) linked list element
3029 _bytes_pool->byte=byte; //initialize byte to current one
3030 _bytes_pool->freq=1; //it appeared once
3031 _bytes_pool->next=0; //now this is last element in ll
3033 // Do update of linked list variables and memory use
3035 ++_bytes_pool; //next time use next entry (this is a pointer)
3037 if(_bytes_pool==_bytes_pool_max) //maximum reached
3040 // First check to see that we still have entries in the array
3042 if(_bytes_pool_index==_mempool_max_index)
3044 ppmc_out_of_memory=1; //flush
3048 // Allocate memory for new buffer
3050 if((_bytes_pool=(struct _byte_and_freq *)malloc
3051 (sizeof(struct _byte_and_freq)*_bytes_pool_elements_inc))==NULL)
3053 printf("\nCan't allocate memory for bytes memory pool.\nIndex: %d",_bytes_pool_index);
3054 ppmc_out_of_memory=1; //flush
3058 _bytes_pool_array[_bytes_pool_index]=_bytes_pool;
3060 _bytes_pool_max=_bytes_pool+_bytes_pool_elements_inc;
3062 _bytes_pool_index++;
3074 // This function returns the probability for the escape codes in the variables
3075 void ppmc_get_escape_prob_order4(void)
3077 // To understand that remember that the escape allocated for the escape code
3078 // is above the current maximum cump and that it has a probability determined
3081 symb_prob=o4_context->defined_bytes;
3082 symb_cump=o4_context->max_cump;