huffman.cpp
上传用户:panstart
上传日期:2022-04-12
资源大小:199k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

C++ Builder

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //    Project     : VideoNet version 1.1.
  5. //    Description : Peer to Peer Video Conferencing over the LAN.
  6. //   Author      : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
  7. //    Date        : 15-6-2004.
  8. //
  9. //    I have converted origional fast h.263 encoder library from C to C++ 
  10. //   so that it can be integrated into any windows application easily.
  11. //   I have removed some of unnecessary codes/files from the
  12. //   fast h263 library.Also moved definitions and declarations
  13. //   in their proper .h and .cpp files.
  14. //
  15. //    File description : 
  16. //    Name    : huffman.cpp
  17. //
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. /*****************************************************************
  21.  * tmn (TMN encoder) 
  22.  * Copyright (C) 1995 Telenor R&D
  23.  *                    Karl Olav Lillevold <kol@nta.no>                    
  24.  *
  25.  * These routines are written by Andy C. Hung
  26.  *
  27.  *****************************************************************/
  28. /*************************************************************
  29. Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
  30. PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
  31. Group. If you use this software, you agree to the following: This
  32. program package is purely experimental, and is licensed "as is".
  33. Permission is granted to use, modify, and distribute this program
  34. without charge for any purpose, provided this license/ disclaimer
  35. notice appears in the copies.  No warranty or maintenance is given,
  36. either expressed or implied.  In no event shall the author(s) be
  37. liable to you or a third party for any special, incidental,
  38. consequential, or other damages, arising out of the use or inability
  39. to use the program for any purpose (or the loss of data), even if we
  40. have been advised of such possibilities.  Any public reference or
  41. advertisement of this source code should refer to it as the Portable
  42. Video Research Group (PVRG) code, and not by any author(s) (or
  43. Stanford University) name.
  44. *************************************************************/
  45. /*
  46. ************************************************************
  47. huffman.c
  48. This file contains the Huffman routines.  They are constructed to use
  49. no look-ahead in the stream.
  50. ************************************************************
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include "huffman.h"
  55. #include "ctables.h"
  56. #define MakeStructure(S) (S *) malloc(sizeof(S))
  57. EHUFF *vlc_3d;
  58. EHUFF *vlc_cbpcm;
  59. EHUFF *vlc_cbpcm_intra;
  60. EHUFF *vlc_cbpy;
  61. EHUFF *vlc_mv;
  62. /**********************************************************************
  63.  *
  64.  * Name: InitHuff
  65.  * Description:    Initializes vlc-tables
  66.  *
  67.  * Input:       
  68.  * Returns:       
  69.  * Side effects:
  70.  *
  71.  * Date: 941128 Author: Karl.Lillevold@ta.no
  72.  *         Idea: see above
  73.  *
  74.  ***********************************************************************/
  75. void InitHuff()
  76. {
  77.   vlc_3d = MakeEhuff(8192);
  78.   vlc_cbpcm = MakeEhuff(256);
  79.   vlc_cbpcm_intra = MakeEhuff(256);
  80.   vlc_cbpy = MakeEhuff(16);
  81.   vlc_mv = MakeEhuff(65);
  82.   LoadETable(vlc_3d_coeff,vlc_3d);
  83.   LoadETable(vlc_cbpcm_coeff,vlc_cbpcm);
  84.   LoadETable(vlc_cbpcm_intra_coeff,vlc_cbpcm_intra);
  85.   LoadETable(vlc_cbpy_coeff,vlc_cbpy);
  86.   LoadETable(vlc_mv_coeff,vlc_mv);
  87.   return;
  88. }
  89. /* FreeHuff(): Frees the VLC-tables */
  90. void FreeHuff()
  91. {
  92.   FreeEhuff(vlc_3d);
  93.   FreeEhuff(vlc_cbpcm);
  94.   FreeEhuff(vlc_cbpcm_intra);
  95.   FreeEhuff(vlc_cbpy);
  96.   FreeEhuff(vlc_mv);
  97. }
  98.     
  99. /*
  100. MakeEhuff() constructs an encoder huff with a designated table-size.
  101. This table-size, n, is used for the lookup of Huffman values, and must
  102. represent the largest positive Huffman value.
  103. */
  104. EHUFF *MakeEhuff(int n)
  105. {
  106.     int i;
  107.     EHUFF *temp;
  108.     temp = MakeStructure(EHUFF);
  109.     temp->n = n;
  110.     temp->Hlen = (int *) calloc(n,sizeof(int));
  111.     temp->Hcode = (int *) calloc(n,sizeof(int));
  112.     for(i=0;i<n;i++)
  113. {
  114.     temp->Hlen[i] = -1;
  115.     temp->Hcode[i] = -1;
  116. }
  117.     return(temp);
  118. }
  119. void FreeEhuff(EHUFF *eh)
  120. {
  121.     free(eh->Hlen);
  122.     free(eh->Hcode);
  123.     free(eh);
  124. }
  125. /*
  126. LoadETable() is used to load an array into an encoder table.  The
  127. array is grouped in triplets and the first negative value signals the
  128. end of the table.
  129. */
  130. void LoadETable(int *array,EHUFF *table)
  131. {
  132.     while(*array>=0)
  133. {
  134.     if (*array>table->n)
  135. {
  136.     printf("Table overflow.n");
  137.     exit(-1);
  138. }
  139.     table->Hlen[*array] = array[1];
  140.     table->Hcode[*array] = array[2];
  141.     array+=3;
  142. }
  143. }
  144. /*
  145. PrintEhuff() prints the encoder Huffman structure passed into it.
  146. */
  147. /*$void PrintEhuff(EHUFF *huff)
  148. {
  149.     int i;
  150.     printf("Modified Huffman Encoding Structure: %xn",&huff);
  151.     printf("Number of values %dn",huff->n);
  152.     for(i=0;i<huff->n;i++)
  153. {
  154.     if (huff->Hlen[i]>=0)
  155. {
  156.     printf("Value: %x  Length: %d  Code: %xn",
  157.    i,huff->Hlen[i],huff->Hcode[i]);
  158. }
  159. }
  160. }$*/
  161. /*
  162. PrintTable() prints out 256 elements in a nice byte ordered fashion.
  163. */
  164. void PrintTable(int *table)
  165. {
  166.     int i,j;
  167.     for(i=0;i<16;i++)
  168. {
  169.     for(j=0;j<16;j++)
  170. printf("%2x ",*(table++));
  171.     printf("n");
  172. }
  173. }
  174. /*
  175. Encode() encodes a symbol according to a designated encoder Huffman
  176. table out to the stream. It returns the number of bits written to the
  177. stream and a zero on error.
  178. */
  179. int Encode(int val,EHUFF *huff)
  180. {
  181.     if (val < 0)
  182. {
  183.     fprintf(stderr,"Out of bounds val:%d.n",val);
  184.     fflush(Global::tf);
  185.     exit(-1);
  186. }
  187.     else if (val >= huff->n) {
  188. return 0; /* No serious error, can occur with some values */
  189.     }
  190.     else if (huff->Hlen[val] < 0) {
  191. return 0;
  192.     }
  193.     else {
  194. mputv(huff->Hlen[val],huff->Hcode[val]); 
  195. return(huff->Hlen[val]);
  196.     }
  197. }
  198. char *BitPrint(int length, int val)
  199. {
  200.     int m;
  201.     char *bit = (char *)malloc(sizeof(char)*(length+3));
  202.     m = length;
  203.     bit[0] = '"';
  204.     while (m--) 
  205. bit[length-m] = (val & (1<<m)) ? '1' : '0';
  206.     bit[length+1] = '"';
  207.     bit[length+2] = '';
  208.     return bit;
  209. }