Esempio n. 1
0
static void rfx_rlgr_code_gr(RFX_BITSTREAM* bs, int* krp, UINT32 val)
{
	int kr = *krp >> LSGR;

	/* unary part of GR code */

	UINT32 vk = (val) >> kr;
	OutputBit(vk, 1);
	OutputBit(1, 0);

	/* remainder part of GR code, if needed */
	if (kr)
	{
		OutputBits(kr, val & ((1 << kr) - 1));
	}

	/* update krp, only if it is not equal to 1 */
	if (vk == 0)
	{
		UpdateParam(*krp, -2, kr);
	}
 	else if (vk > 1)
	{
		UpdateParam(*krp, vk, kr);
	}
}
Esempio n. 2
0
void CompressFile( FILE *input, BIT_FILE *output, int argc, char *argv[] ){
/*FILE *input;
BIT_FILE *output;
int argc;
char *argv[];
{
*/
    int compress[ 256 ];
    int steps;
    int bits;
    int value;
    int i;
    int j;
    int c;
/*
* The first section of code determines the number of bits to use
* for output codes, then writes it to the compressed file. The
* length of the input file is also written out.
*/
    if ( argc-- > 0 )
        bits = atoi( *argv );
    else
        bits = 4;
    printf( "Compressing using %d bits per sample...\n", bits );
    steps = ( 1 << ( bits - 1 ) );
    OutputBits( output, (unsigned long) bits, 8 );
    OutputBits( output, (unsigned long) get_file_length( input), 32 );
/*
* The compression table is built here. Each input code maps to
* a single output code. There are "steps" codes to be used in
* the output space. This builds an exponential output function.
*/
    for ( i = steps ; i > 0; i-- ) {
        value = 128.0 * ( pow( 2.0, (double) i / steps ) - 1.0 ) + 0.5;
        for ( j = value ; j > 0 ; j-- ) {
            compress[ j + 127 ] = i + steps - 1;
            compress[ 128 - j ] = steps - i;
        }
    }
/*
* The actual compression takes place here.
*/
    while ( ( c = getc( input ) ) != EOF )
        OutputBits( output, (unsigned long) compress[ c ], bits ); }
Esempio n. 3
0
static void OutputBits(TCmpStruct * pWork, unsigned int nbits, unsigned long bit_buff)
{
    unsigned int out_bits;

    // If more than 8 bits to output, do recursion
    if(nbits > 8)
    {
        OutputBits(pWork, 8, bit_buff);
        bit_buff >>= 8;
        nbits -= 8;
    }
Esempio n. 4
0
char *argv[]
{
 int character;
 int string_code;
 unsigned int index;
 InitializeStorage();
 InitializeDictionary();
 if ( ( string_code = getc( input ) ) == EOF )
 string_code = END_OF_STREAM;
 while ( ( character = getc( input ) ) != EOF ) {
 index = find_child_node( string_code, character );
 if ( DICT( index ).code_value != -1)
 string_code = DICT( index ).code_value;
 else {
 DICT( index ).code_value = next_code++;
 DICT( index ).parent_code = string_code;
 DICT( index ).character = (char) character;
 OutputBits( output,
 (unsigned long) string_code, current_code_bits );
 string_code = character;
 if ( next_code > MAX_CODE ) {
 OutputBits( output,
 (unsigned long) FLUSH_CODE, current_code_bits );
 InitializeDictionary();
 } else if ( next_code > next_bump_code ) {
 OutputBits( output,
 (unsigned long) BUMP_CODE, current_code_bits );
 current_code_bits++;
 next_bump_code <<= 1;
 next_bump_code |= 1;
 putc( 'B', stdout );
 }
 }
 }
 OutputBits( output, (unsigned long) string_code, current_code_bits );
 OutputBits( output, (unsigned long) END_OF_STREAM, current_code_bits);
 while ( argc— > 0 )
 printf( "Unknown argument: %s\n", *argv++ );
}
Esempio n. 5
0
int rfx_rlgr_encode(RLGR_MODE mode, const INT16* data, int data_size, BYTE* buffer, int buffer_size)
{
	int k;
	int kp;
	int krp;
	RFX_BITSTREAM* bs;
	int processed_size;

	bs = (RFX_BITSTREAM*) malloc(sizeof(RFX_BITSTREAM));
	ZeroMemory(bs, sizeof(RFX_BITSTREAM));

	rfx_bitstream_attach(bs, buffer, buffer_size);

	/* initialize the parameters */
	k = 1;
	kp = 1 << LSGR;
	krp = 1 << LSGR;

	/* process all the input coefficients */
	while (data_size > 0)
	{
		int input;

		if (k)
		{
			int numZeros;
			int runmax;
			int mag;
			int sign;

			/* RUN-LENGTH MODE */

			/* collect the run of zeros in the input stream */
			numZeros = 0;
			GetNextInput(input);
			while (input == 0 && data_size > 0)
			{
				numZeros++;
				GetNextInput(input);
			}

			// emit output zeros
			runmax = 1 << k;
			while (numZeros >= runmax)
			{
				OutputBit(1, 0); /* output a zero bit */
				numZeros -= runmax;
				UpdateParam(kp, UP_GR, k); /* update kp, k */
				runmax = 1 << k;
			}

			/* output a 1 to terminate runs */
			OutputBit(1, 1);

			/* output the remaining run length using k bits */
			OutputBits(k, numZeros);

			/* note: when we reach here and the last byte being encoded is 0, we still
			   need to output the last two bits, otherwise mstsc will crash */

			/* encode the nonzero value using GR coding */
			mag = (input < 0 ? -input : input); /* absolute value of input coefficient */
			sign = (input < 0 ? 1 : 0);  /* sign of input coefficient */

			OutputBit(1, sign); /* output the sign bit */
			CodeGR(&krp, mag ? mag - 1 : 0); /* output GR code for (mag - 1) */

			UpdateParam(kp, -DN_GR, k);
		}
		else
		{
			/* GOLOMB-RICE MODE */

			if (mode == RLGR1)
			{
				UINT32 twoMs;

				/* RLGR1 variant */

				/* convert input to (2*magnitude - sign), encode using GR code */
				GetNextInput(input);
				twoMs = Get2MagSign(input);
				CodeGR(&krp, twoMs);

				/* update k, kp */
				/* NOTE: as of Aug 2011, the algorithm is still wrongly documented
				   and the update direction is reversed */
				if (twoMs)
				{
					UpdateParam(kp, -DQ_GR, k);
				}
				else
				{
					UpdateParam(kp, UQ_GR, k);
				}
			}
			else /* mode == RLGR3 */
			{
				UINT32 twoMs1;
				UINT32 twoMs2;
				UINT32 sum2Ms;
				UINT32 nIdx;

				/* RLGR3 variant */

				/* convert the next two input values to (2*magnitude - sign) and */
				/* encode their sum using GR code */

				GetNextInput(input);
				twoMs1 = Get2MagSign(input);
				GetNextInput(input);
				twoMs2 = Get2MagSign(input);
				sum2Ms = twoMs1 + twoMs2;

				CodeGR(&krp, sum2Ms);

				/* encode binary representation of the first input (twoMs1). */
				GetMinBits(sum2Ms, nIdx);
				OutputBits(nIdx, twoMs1);

				/* update k,kp for the two input values */

				if (twoMs1 && twoMs2)
				{
					UpdateParam(kp, -2 * DQ_GR, k);
				}
				else if (!twoMs1 && !twoMs2)
				{
					UpdateParam(kp, 2 * UQ_GR, k);
				}
			}
		}
	}

	processed_size = rfx_bitstream_get_processed_bytes(bs);
	free(bs);

	return processed_size;
}
Esempio n. 6
0
int AACQuantize(CoderInfo *coderInfo,
                PsyInfo *psyInfo,
                ChannelInfo *channelInfo,
                int *cb_width,
                int num_cb,
                double *xr,
		AACQuantCfg *aacquantCfg)
{
    int sb, i, do_q = 0;
    int bits = 0, sign;
    double xr_pow[FRAME_LEN];
    double xmin[MAX_SCFAC_BANDS];
    int xi[FRAME_LEN];

    /* Use local copy's */
    int *scale_factor = coderInfo->scale_factor;

    /* Set all scalefactors to 0 */
    coderInfo->global_gain = 0;
    for (sb = 0; sb < coderInfo->nr_of_sfb; sb++)
        scale_factor[sb] = 0;

    /* Compute xr_pow */
    for (i = 0; i < FRAME_LEN; i++) {
        double temp = fabs(xr[i]);
        xr_pow[i] = sqrt(temp * sqrt(temp));
        do_q += (temp > 1E-20);
    }

    if (do_q) {
        CalcAllowedDist(coderInfo, psyInfo, xr, xmin, aacquantCfg->quality);
	coderInfo->global_gain = 0;
	FixNoise(coderInfo, xr, xr_pow, xi, xmin,
		 aacquantCfg->pow43, aacquantCfg->adj43);
	BalanceEnergy(coderInfo, xr, xi, aacquantCfg->pow43);
	UpdateRequant(coderInfo, xi, aacquantCfg->pow43);

        for ( i = 0; i < FRAME_LEN; i++ )  {
            sign = (xr[i] < 0) ? -1 : 1;
            xi[i] *= sign;
            coderInfo->requantFreq[i] *= sign;
        }
    } else {
        coderInfo->global_gain = 0;
        SetMemory(xi, 0, FRAME_LEN*sizeof(int));
    }

    BitSearch(coderInfo, xi);

    /* offset the difference of common_scalefac and scalefactors by SF_OFFSET  */
    for (i = 0; i < coderInfo->nr_of_sfb; i++) {
        if ((coderInfo->book_vector[i]!=INTENSITY_HCB)&&(coderInfo->book_vector[i]!=INTENSITY_HCB2)) {
            scale_factor[i] = coderInfo->global_gain - scale_factor[i] + SF_OFFSET;
        }
    }
    coderInfo->global_gain = scale_factor[0];
#if 0
	printf("global gain: %d\n", coderInfo->global_gain);
	for (i = 0; i < coderInfo->nr_of_sfb; i++)
	  printf("sf %d: %d\n", i, coderInfo->scale_factor[i]);
#endif

    /* place the codewords and their respective lengths in arrays data[] and len[] respectively */
    /* there are 'counter' elements in each array, and these are variable length arrays depending on the input */
#ifdef DRM
    coderInfo->iLenReordSpData = 0; /* init length of reordered spectral data */
    coderInfo->iLenLongestCW = 0; /* init length of longest codeword */
    coderInfo->cur_cw = 0; /* init codeword counter */
#endif
    coderInfo->spectral_count = 0;
    sb = 0;
    for(i = 0; i < coderInfo->nr_of_sfb; i++) {
        OutputBits(
            coderInfo,
#ifdef DRM
            &coderInfo->book_vector[i], /* needed for VCB11 */
#else
            coderInfo->book_vector[i],
#endif
            xi,
            coderInfo->sfb_offset[i],
            coderInfo->sfb_offset[i+1]-coderInfo->sfb_offset[i]);

        if (coderInfo->book_vector[i])
              sb = i;
    }

    // FIXME: Check those max_sfb/nr_of_sfb. Isn't it the same?
    coderInfo->max_sfb = coderInfo->nr_of_sfb = sb + 1;

    return bits;
}
Esempio n. 7
0
int
rfx_rlgr1_encode(const sint16* data, int data_size, uint8* buffer, int buffer_size)
{
    int k;
    int kp;
    int krp;

    int input;
    int numZeros;
    int runmax;
    int mag;
    int sign;
    int processed_size;
    int lmag;

    RFX_BITSTREAM bs;

    uint32 twoMs;

    rfx_bitstream_attach(bs, buffer, buffer_size);

    /* initialize the parameters */
    k = 1;
    kp = 1 << LSGR;
    krp = 1 << LSGR;

    /* process all the input coefficients */
    while (data_size > 0)
    {
        if (k)
        {
            /* RUN-LENGTH MODE */

            /* collect the run of zeros in the input stream */
            numZeros = 0;
            GetNextInput(input);
            while (input == 0 && data_size > 0)
            {
                numZeros++;
                GetNextInput(input);
            }

            /* emit output zeros */
            runmax = 1 << k;
            while (numZeros >= runmax)
            {
                OutputBit(1, 0); /* output a zero bit */
                numZeros -= runmax;
                UpdateParam(kp, UP_GR, k); /* update kp, k */
                runmax = 1 << k;
            }

            /* output a 1 to terminate runs */
            OutputBit(1, 1);

            /* output the remaining run length using k bits */
            OutputBits(k, numZeros);

            /* note: when we reach here and the last byte being encoded is 0, we still
               need to output the last two bits, otherwise mstsc will crash */

            /* encode the nonzero value using GR coding */
            mag = (input < 0 ? -input : input); /* absolute value of input coefficient */
            sign = (input < 0 ? 1 : 0);  /* sign of input coefficient */

            OutputBit(1, sign); /* output the sign bit */
            lmag = mag ? mag - 1 : 0;
            CodeGR(krp, lmag); /* output GR code for (mag - 1) */

            UpdateParam(kp, -DN_GR, k);
        }
        else
        {
            /* GOLOMB-RICE MODE */

            /* RLGR1 variant */

            /* convert input to (2*magnitude - sign), encode using GR code */
            GetNextInput(input);
            twoMs = Get2MagSign(input);
            CodeGR(krp, twoMs);

            /* update k, kp */
            /* NOTE: as of Aug 2011, the algorithm is still wrongly documented
               and the update direction is reversed */
            if (twoMs)
            {
                UpdateParam(kp, -DQ_GR, k);
            }
            else
            {
                UpdateParam(kp, UQ_GR, k);
            }

        }
    }

    processed_size = rfx_bitstream_get_processed_bytes(bs);

    return processed_size;
}
Esempio n. 8
0
WORD BurstTx(WORD *inBuffer, WORD *inSize, WORD *outBuffer, WORD *outSize, BYTE *blockObjStruct) {
    BURST_TX_STATE_TYPE *state = (BURST_TX_STATE_TYPE *)blockObjStruct;

    switch(state->state) {
        case BURST_TX_IDLE:
            // No burst in process
            state->txLength = 0;
            state->payloadLength = 0;
            state->checksum = 0;
            state->buffer = 0;
            state->bufferCount = 0;

            OutputBits(state, BURST_SYNC, BURST_SYNC_BITS);

            state->state = BURST_TX_TX;
            *inSize = 0;

            break;
        case BURST_TX_TX:
            if(*inSize > 0) {
                // Receiving characters, transmitting burst
                OutputBits(state, *inBuffer, BURST_BYTE_BITS);
                state->payloadLength++;
                state->checksum ^= *inBuffer;
                *inSize = 1;

                state->txLength++;
                if(state->txLength == BURST_PAYLOAD_LENGTH) {
                    state->state = BURST_TX_END;
                }
            }
            else if(IsCurrentPipelineEmptyDownStream()) {
                // No more characters when we need characters: flush with zeros
                state->state = BURST_TX_FLUSH;
            }

            break;
        case BURST_TX_FLUSH:
            // No characters remaining: flush with zeros
            OutputBits(state, 0, BURST_BYTE_BITS);

            state->txLength++;
            if(state->txLength == BURST_PAYLOAD_LENGTH) {
                state->state = BURST_TX_END;
            }
            *inSize = 0;

            break;
        case BURST_TX_END:
            // End burst with length and checksum fields 
            OutputBits(state, state->payloadLength - 1, BURST_LENGTH_BITS);
            OutputBits(state, state->checksum, BURST_CHECKSUM_BITS);

            state->state = BURST_TX_IDLE;
            *inSize = 0;

            break;
    }

    // Output a WORD if one is available: we guarantee no dangling bits are left
    // in the buffer at the end by making burst length a multiple of WORD length
    if(state->bufferCount >= BITS_IN_WORD) {
        *outBuffer = (WORD)(state->buffer & 0xFFFFUL);
        state->bufferCount -= BITS_IN_WORD;
        state->buffer >>= BITS_IN_WORD;
        *outSize = 1;
    }
Esempio n. 9
0
//void CompressFile( FILE *input, BIT_FILE *output, int argc, char *argv[] )
void CompressFile( FILE *input, BIT_FILE *output, int, char*[])
{
    int look_ahead[ BUFFER_SIZE ];
    int index;
    int i;
    int run_length;

	//Update so Silence and Compand do not conflict
    int silence_match[6] = { 31, 0, 31, 0, 30, 0 };

	// Compand addition
    int compress[ 256 ];
    int steps;
    int bits;
    int value;
    int k;
    int j;

	bits = 5;
    steps = ( 1 << ( bits - 1 ) );

  //  OutputBits( output, (unsigned long) bits, 8 );
  //  OutputBits( output, (unsigned long) get_file_length( input ), 32 );

    for ( k = steps ; k > 0; k-- ) {
        value = (int)
           ( 128.0 * ( pow( 2.0, (double) k  /  steps ) - 1.0 ) + 0.5 );
        for ( j = value ; j > 0 ; j-- ) {
            compress[ j + 127 ] = k + steps - 1;
            compress[ 128 - j ] = steps - k;
        }
    }

    for ( i = 0 ; i < BUFFER_SIZE ; i++ )
        look_ahead[ i ] = getc( input );
    index = 0;
    for ( ; ; ) 
		{
        if ( look_ahead[ index ] == EOF )
            break;
		/*
		 * If a run has started, I handle it here.   I sit in the do loop until
		 * the run is complete, loading new characters all the while.
		 */
        if ( silence_run( look_ahead, index ) ) 
			{
            run_length = 0;

            do {
                look_ahead[ index++ ] = getc( input );
                index &= BUFFER_MASK;
                if ( ++run_length == 255 ) 
					{
					for( i=0; i<6; i++ )
						{
						OutputBits( output, (unsigned long) silence_match[i], bits );
						}
					OutputBits( output, (unsigned long) run_length, 8 );
					}
            } while ( !end_of_silence( look_ahead, index ) );
            if ( run_length > 0 ) 
				{
				for( i=0; i<6;i++ )
					{
					OutputBits( output, (unsigned long) silence_match[i], bits );
					}
				OutputBits( output, (unsigned long) run_length, 8 );
				}
			}
		/*
		 * Eventually, any run of silence is over, and I output some plain codes.
		 * Any code that accidentally matches the silence code gets silently changed.
		 */
		OutputBits( output, (unsigned long) compress[ look_ahead[ index ] ], bits );

        look_ahead[ index++ ] = getc( input );
        index &= BUFFER_MASK;
		}

	}
Esempio n. 10
0
void AACQuantize(CoderInfo *coderInfo,
                    ChannelInfo *channelInfo,
                    int32_t *cb_width,
                    int32_t num_cb,
                    coef_t *xr,
                    pow_t *xr2,
                    AACQuantCfg *aacquantCfg)
{
    register int32_t sb, i;
    coef_t xr_pow[FRAME_LEN];
    real_32_t xmin[MAX_SCFAC_BANDS];
    int32_t xi[FRAME_LEN];
    int32_t *scale_factor;

    /* Use local copy's */
    scale_factor = coderInfo->scale_factor;

    /* Set all scalefactors to 0 */
    coderInfo->global_gain = 0;
    for (sb = 0; sb < coderInfo->nr_of_sfb; sb++)
        scale_factor[sb] = 0;

    /* Compute xr_pow */
    for (i = 0; i < FRAME_LEN; i++) {
        xr_pow[i] = faac_pow34(xr[i]);
#ifdef DUMP_XR_POW
        printf("xr_pow[%d] = %.8f\n",i,COEF2FLOAT(xr_pow[i]));
#endif
    }
#ifdef DUMP_XR_POW
//    exit(1);
#endif
    
    CalcAllowedDist(coderInfo, xr2, xmin, aacquantCfg->quality);
    coderInfo->global_gain = 0;
    
    FixNoise(coderInfo, xr_pow, xi, xmin);
   
    for ( i = 0; i < FRAME_LEN; i++ )  {
        if ( xr[i] < 0 )
            xi[i] = -xi[i];
    }

    BitSearch(coderInfo, xi);

    /* offset the difference of common_scalefac and scalefactors by SF_OFFSET  */
    for (i = 0; i < coderInfo->nr_of_sfb; i++) {
        if ( (coderInfo->book_vector[i]!=INTENSITY_HCB) && 
            (coderInfo->book_vector[i]!=INTENSITY_HCB2) ) {
            scale_factor[i] = coderInfo->global_gain - scale_factor[i] + SF_OFFSET;
        }
    }
    coderInfo->global_gain = scale_factor[0];

    /* place the codewords and their respective lengths in arrays data[] and len[] respectively */
    /* there are 'counter' elements in each array, and these are variable length arrays depending on the input */
    coderInfo->spectral_count = 0;
    sb = 0;
    for(i = 0; i < coderInfo->nr_of_sfb; i++) {
        OutputBits(
            coderInfo,
            coderInfo->book_vector[i],
            xi,
            coderInfo->sfb_offset[i],
            coderInfo->sfb_offset[i+1]-coderInfo->sfb_offset[i]);

        if (coderInfo->book_vector[i])
            sb = i;
    }

    // FIXME: Check those max_sfb/nr_of_sfb. Isn't it the same?
    coderInfo->max_sfb = coderInfo->nr_of_sfb = sb + 1;
}
Esempio n. 11
0
ubyte *lzw_compress( ubyte *inputbuf, ubyte *outputbuf, int input_size, int *output_size )
{
	BIT_BUF *output;
	int character;
	int string_code;
	unsigned int index;
	int i;

	output = OpenOutputBitBuf();

	if ( outputbuf == NULL )
	{
		output->buf = (ubyte *)malloc(input_size*sizeof(ubyte));
		if (output->buf == NULL)
		{
			//printf("    ERROR : OpenOutputBitBuf - Not enough memory to read buffer.\n");
			//exit(1);
			return NULL;
		}
		outputbuf = output->buf;
	}
	else
	{
		output->buf = outputbuf;
	}

	InitializeStorage();
	InitializeDictionary();
	string_code = ( *inputbuf++ );
	for ( i=0 ; i<input_size ; i++ )
	{

		if (output->current_byte+4 >= input_size)
		{
			CloseOutputBitBuf( output );
			FreeStorage();
			free( outputbuf );
			*output_size = -1;
			return NULL;
		}

		character = ( *inputbuf++ );
		index = find_child_node( string_code, character );
		if ( dict[ index ].code_value != - 1 )
		{
			string_code = dict[ index ].code_value;
		}
		else
		{
			dict[ index ].code_value = next_code++;
			dict[ index ].parent_code = string_code;
			dict[ index ].character = (char) character;
			OutputBits( output,(unsigned long) string_code, current_code_bits );
			string_code = character;

			if ( next_code > MAX_CODE )
			{
				OutputBits( output,(unsigned long) FLUSH_CODE, current_code_bits );
				InitializeDictionary();
			}
			else if ( next_code > next_bump_code )
			{
				OutputBits( output,(unsigned long) BUMP_CODE, current_code_bits );
				current_code_bits++;
				next_bump_code <<= 1;
				next_bump_code |= 1;
			}
		}
	}
	OutputBits( output, (unsigned long) string_code, current_code_bits );
	OutputBits( output, (unsigned long) END_OF_STREAM, current_code_bits);

	*output_size = output->current_byte + 1;

	CloseOutputBitBuf( output );
	FreeStorage();

	return outputbuf;
}