include/it/vlc.h

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2006 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /*
00021   Variable Length Codes
00022   Copyright (C) 2005 Herve Jegou
00023 */
00024 
00025 #ifndef __it_vlc_h
00026 #define __it_vlc_h
00027 
00028 #include <it/vec.h>
00029 
00030 #ifndef DUMM_NODE
00031 #define DUMM_NODE (NULL_INDEX)
00032 #endif
00033 
00034 /*---------------------------------------------------------------------------*/
00035 /*! \addtogroup entropycoding Entropy Coding                                 */
00036 /* @{                                                                        */
00037 /*---------------------------------------------------------------------------*/
00038 
00039 /*----------------------------------------------------------------------*/
00040 /* The vlc_t structure 
00041    Note: the height of the VLC codetree is limited to sizeof(int)-1     */
00042 typedef struct {
00043   int  nb_symb;   /* Number of symbols defined by the VLC codetree */
00044   int  nb_nodes;    /* The total number of nodes                     */
00045 
00046   ivec cwd;     /* The codewords, stored in a compact form       */
00047   ivec cwd_length;    /* The length of the corresponding symbols       */
00048   ivec map[2];    /* The mapping between nodes                     */
00049 
00050   int  node_root;   /* The index of the node root                    */
00051 } vlc_t;
00052 
00053 
00054 /*----------------------------------------------------------------------*/
00055 /* Initialization of a new VLC code                                    */
00056 vlc_t *vlc_new (int n);
00057 
00058 /* Create a clone of a given VLC                                       */
00059 vlc_t *vlc_clone (const vlc_t * vlc);
00060 
00061 /* Delete a VLC code                                                   */
00062 void vlc_delete (vlc_t * vlc);
00063 
00064 /* Return the integer code associated with a node.                     */
00065 static inline int vlc_get_cwd (const vlc_t * vlc, int node) {
00066   return vlc->cwd[node];
00067 }
00068 
00069 /* Return the length of integer code associated with a node.           */
00070 static inline int vlc_get_cwd_length (const vlc_t * vlc, int node) {
00071   return vlc->cwd_length[node];
00072 }
00073 
00074 /* The child node of s if the next bit transition is a 0               */
00075 static inline int vlc_get_child0 (const vlc_t * vlc, int s) {
00076   return vlc->map[0][s];
00077 }
00078 
00079 /* The child node of s if the next bit transition is a 1               */
00080 static inline int vlc_get_child1 (const vlc_t * vlc, int s) {
00081   return vlc->map[1][s];
00082 }
00083 
00084 /* Return 1 if s is a leaf, 0 otherwise                                 */
00085 static inline int vlc_is_leaf (const vlc_t * vlc, int s) {
00086   return s < vlc->nb_symb;
00087 }
00088 
00089 /* Return 1 if s is a internal node, 0 otherwise                        */
00090 static inline int vlc_is_node (const vlc_t * vlc, int s) {
00091   return s >= vlc->nb_symb;
00092 }
00093 
00094 
00095 /*----------------------------------------------------------------------*/
00096 /* VLC design                                                           */
00097 
00098 /* Sort the VLC so that it is the "more" lexicographic as possible      */
00099 void vlc_quasi_lexicographic (const vlc_t * vlc,
00100             const vec pdf, const vec symb);
00101 
00102 /* Return the Huffman code corresponding to the probability law pdf     */
00103 vlc_t *vlc_huffman (const vec pdf);
00104 
00105 /* Return the Hu-Tucker code corresponding to the probability law pdf,
00106  assuming that these symbols are sorted in the natural order            */
00107 vlc_t *vlc_hu_tucker (const vec pdf);
00108 
00109 /* Return a Fixed length Code for the number of bits                    */
00110 vlc_t *vlc_flc (int nb_bits);
00111 
00112 /* Read a variable length code from a string                            */
00113 vlc_t *vlc_read (const char *s);
00114 
00115 /* Assign the codewords according to the definition of variable map     */
00116 void vlc_affect_codewords (vlc_t * vlc);
00117 
00118 
00119 /*----------------------------------------------------------------------*/
00120 /* VLC performance and measures                                         */
00121 
00122 /* The length of the shortest codeword                                  */
00123 int  vlc_minh (const vlc_t * vlc);
00124 
00125 /* The length of the longest codeword                                   */
00126 int  vlc_maxh (const vlc_t * vlc);
00127 
00128 /* Kraft sum. (sum_s 2^{-L(s)})                                         */
00129 double vlc_kraft_sum (const vlc_t * vlc);
00130 
00131 /* Return the average length of the code (i.e. the expectation 
00132    of the length) if the distribution of the source is pdf              */
00133 double vlc_mdl (const vlc_t * vlc, const vec pdf);
00134 
00135 /* Return the respective probabilities of the nodes                     */
00136 vec  vlc_nodes_pdf (const vlc_t * vlc, vec pdf);
00137 
00138 /* Return the probability to emit a zero for each possible internal node*/
00139 vec  vlc_nodes_proba0 (const vlc_t * vlc, vec pdf);
00140 
00141 /* Return the respective expectations of the nodes                      */
00142 vec  vlc_nodes_expectation (const vlc_t * vlc, vec pdf, vec symb);
00143 
00144 /* Return the value of variance when being in a node                    */
00145 double vlc_node_variance (const vlc_t * vlc, int node, vec pdf,
00146           vec symbols);
00147 vec  vlc_nodes_variance (const vlc_t * vlc, vec pdf, vec symb);
00148 
00149 /* Return the binary entropy of the internal nodes of the VLC           */
00150 vec  vlc_nodes_entropy (const vlc_t * vlc, vec pdf);
00151 
00152 /* Expectation of the decrease of MSE corresponding to internal nodes   */
00153 vec  vlc_nodes_delta_energy (const vlc_t * vlc, vec pdf, vec symb);
00154 
00155 /* Return the order between node so that each transmitted bit is the one
00156    that minimizes the rate-distortion curve (assuming that the VLC 
00157    is encoded with an optimal entropy coder afterward                   */
00158 ivec vlc_energy_order (const vlc_t * vlc, vec pdf, vec symb);
00159 
00160 /*----------------------------------------------------------------------*/
00161 /* VLC I/O                                                              */
00162 
00163 void vlc_print (const vlc_t * vlc);
00164 void vlc_print_all (const vlc_t * vlc, vec pdf, vec symb);
00165 
00166 /*----------------------------------------------------------------------*/
00167 /* Return the number of bits required to encode a given source          */
00168 int  vlc_nb_bits_required (const vlc_t * vlc, ivec S);
00169 
00170 /*----------------------------------------------------------------------*/
00171 /* Encode a symbol flow using concatenation of codewords                */
00172 
00173 /* Encode a source with a VLC. The codeword and transmitted concatenated*/
00174 bvec vlc_encode_concat (const vlc_t * vlc, ivec S);
00175 
00176 /* Decode a VLC encoded bitstream                                       */
00177 ivec vlc_decode_concat (const vlc_t * vlc, bvec E);
00178 
00179 /* The same, but with an expected number of symbols equal to N.
00180    Hence, the bitstream may be truncated or filled with -1;             */
00181 ivec vlc_decode_concat_N (const vlc_t * vlc, bvec E, idx_t N);
00182 
00183 /* @} */
00184 
00185 
00186 #endif

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau