www.pudn.com > bladeenc-0.90.0-src.zip > huffman.c


/* 
			(c) Copyright 1998, 1999 - Tord Jansson 
			======================================= 
 
		This file is part of the BladeEnc MP3 Encoder, based on 
		ISO's reference code for MPEG Layer 3 compression, and might 
		contain smaller or larger sections that are directly taken 
		from ISO's reference code. 
 
		All changes to the ISO reference code herein are either 
		copyrighted by Tord Jansson (tord.jansson@swipnet.se) 
		or sublicensed to Tord Jansson by a third party. 
 
	BladeEnc is free software; you can redistribute this file 
	and/or modify it under the terms of the GNU Lesser General Public 
	License as published by the Free Software Foundation; either 
	version 2.1 of the License, or (at your option) any later version. 
 
*/ 
 
#include  
#include  
#include "system.h" 
#include "common.h" 
#include "huffman.h" 
 
HUFFBITS dmask = 1 << (sizeof(HUFFBITS)*8-1); 
unsigned int hs = sizeof(HUFFBITS)*8; 
 
struct huffcodetab ht[HTN];     /* array of all huffcodtable headers    */ 
                                /* 0..31 Huffman code table 0..31       */ 
                                /* 32,33 count1-tables                  */ 
 
/* read the huffman encode table */ 
int read_huffcodetab( void ) 
{ 
 
        int             index = 0; 
 
  char line[100],command[40],huffdata[40]; 
  unsigned int t,i,j,k,nn,x,y,n=0; 
  unsigned int xl, yl, len; 
  HUFFBITS h; 
  int   hsize; 
 
  hsize = sizeof(HUFFBITS)*8; 
  do 
        { 
    strcpy( line, aHuffcode[index++] ); 
/*      fgets(line,99,fi); */ 
  } while ((line[0] == '#') || (line[0] < ' ') ); 
 
  do 
        { 
    while ((line[0]=='#') || (line[0] < ' ')) 
                { 
            strcpy( line, aHuffcode[index++] ); 
/*      fgets(line,99,fi); */ 
    } 
 
    sscanf(line,"%s %s %u %u %u",command,ht[n].tablename, 
                                 &xl,&yl,&ht[n].linbits); 
    if (strcmp(command,".end")==0) 
      return n; 
    else if (strcmp(command,".table")!=0) { 
      fprintf(stderr,"huffman table %u data corrupted\n",n); 
      return -1; 
    } 
    ht[n].linmax = (1<1) 
            sscanf(line,"%u %u %u %s",&x, &y, &len,huffdata); 
          else 
            sscanf(line,"%u %u %s",&x,&len,huffdata); 
          h=0;k=0; 
          while (huffdata[k]) { 
            h <<= 1; 
            if (huffdata[k] == '1') 
              h++; 
            else if (huffdata[k] != '0'){ 
              fprintf(stderr,"huffman-table %u bit error\n",n); 
              return (-5); 
            }; 
            k++; 
          }; 
          if (k != len) { 
           fprintf(stderr, 
              "warning: wrong codelen in table %u, pos [%2u][%2u]\n", 
               n,i,j); 
          }; 
          ht[n].table[i*xl+j] = h; 
          ht[n].hlen[i*xl+j] = (unsigned char) len; 
          do { 
            strcpy( line, aHuffcode[index++] ); 
/*                                      fgets(line,99,fi); */ 
          } while ((line[0] == '#') || (line[0] < ' ')); 
        } 
      } 
    } 
    n++; 
  } while (1); 
}