⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitstream1.c

📁 jpeg快速算法,用于TI和philips的DSP开发
💻 C
字号:
/*
 *********************************************************************
 * File name: bitstream.c
 * Version: 5.0(release v1.0)    Date: Jan 12, 2006
 * Author:  xiezm                Email: xiezm@wxintech.cn
 * Company: Wuxi Intech co., ltd.
 *
 * Project: Jpeg Decoder for Trio
 *********************************************************************
 */
#define BITSTREAM_GLOBALS

#include "cf6_chess.h"

#ifdef WIN32
#include <stdio.h>
#endif

#include "djpg.h"

extern volatile int chess_storage(YMEM)  DEBUG_FLAG[];
static int count=0;
extern int huffFindCount;
extern int frameCount;
extern int speedUp;

void bitstream_fill_buffer(void)
{
#ifdef WIN32
	fpos_t pos;
	
/*	if((count>=0x200)&&(count<0x280))
	{
		if(speedUp == 0x21)
		{
			fseek(INPUT,-20*BUFFER_SIZE*4,SEEK_CUR);
			fgetpos(INPUT, &pos);
			speedUp = 0xb;
		}
	}
	else if(count==0x280)
	{
		count=0x0;
	}

	count++;
*/
	int num=fread(input_buffer, 4, BUFFER_SIZE, INPUT);
	fgetpos(INPUT, &pos);
	if(num==0)
		exit(34);
#else
	int i;
	for(i=0;i<BUFFER_SIZE;i++)
	{
		input_buffer[i] = INPUT;
	}
#endif

    buffer_inop = input_buffer;
	buffer_end = buffer_inop + BUFFER_SIZE - 1;
}

void bitstream_init(void)
{
    int i;

    bitstream_fill_buffer();
    
    is_stuff = 0;
    current = 0;
    bits_left = 0;
    next = 0;
    bits_next = 0;

    for(i=0;i<16;i++)
    {
        bits_mask[i] = (1<<i) - 1;
    }
}

unsigned int get_byte(void)
{
	int chess_storage(y0) INT_Pff = 0xff;
    int chess_storage(y1) INT_Pff00 = 0xff00;
    int chess_storage(b1) INT_P8 = 8;
    int chess_storage(x1) INT_P16 = 16;	

    if(0 == bits_left)
    {
		huffFindCount = buffer_inop - input_buffer;

		if(buffer_inop == buffer_end)
		{
			current = ((*buffer_inop)>>INT_P8) | ((*buffer_inop)<<INT_P8);
			bits_left = INT_P16;
			bitstream_fill_buffer();
		}
		else
		{
			current = ((*buffer_inop)>>INT_P16) | ((*buffer_inop)&INT_Pff00) | ((*buffer_inop)<<INT_P16);
			buffer_inop++;
    		bits_left = 24;
		}
    }
    return((current >> (bits_left -= INT_P8)) & INT_Pff);
}

unsigned int get_word(void)
{
    unsigned int ret_val;
    
    ret_val = get_byte();
    ret_val <<= 8;
    ret_val |= get_byte();

    return (ret_val);
}

void fill_op_word(unsigned int *op_word, unsigned int *bits_opword)
{
	int chess_storage(y0) INT_Pff = 0xff;
    int chess_storage(y1) INT_Pff00 = 0xff00;
    int chess_storage(b1) INT_P8 = 8;
    int chess_storage(x1) INT_P16 = 16;	

    if(buffer_inop == buffer_end)
    {
        *bits_opword = INT_P16;
        *op_word = *buffer_inop;
        bitstream_fill_buffer();
        if(is_stuff && (0x00 == ((*op_word) & INT_Pff)))
        {
            *op_word >>= INT_P8;
    	    *bits_opword = INT_P8;
            if(0x00ff == (*op_word))
            {
                is_stuff = 1;
            }
            else
            {
                is_stuff = 0;
            }
            return;
        }
        else if(INT_Pff == (*op_word))
        {
            *bits_opword = INT_P8;
			is_stuff = 0;
			return;
        }
		*op_word = (((*op_word) << INT_P8) & INT_Pff00) | (((*op_word)>> INT_P8 ) & INT_Pff);
		if(INT_Pff == ((*op_word) & INT_Pff))
		{
			is_stuff = 1;
		}
		else
		{
			is_stuff = 0;
		}
        return;
    }

    *bits_opword = 24; 
    *op_word = (((*buffer_inop)>>INT_P16) | ((*buffer_inop)&INT_Pff00) | ((*buffer_inop)<<INT_P16)) & 0x00ffffff;
    buffer_inop++;
    if(is_stuff && (0x00 == ((*op_word) >> INT_P16)))
    {
        *bits_opword -= INT_P8;
    }
    if(INT_Pff00 == ((*op_word) >> INT_P8))
    {
        *bits_opword -= INT_P8;
        *op_word = (((*op_word) >> INT_P8) & INT_Pff00) | ((*op_word) & INT_Pff);
    }
    else if(INT_Pff00 == ((*op_word) & 0x00ffff))
    {
        *bits_opword -= INT_P8;		
        *op_word >>= INT_P8;
        is_stuff = 0;
        return;
    }

    if(INT_Pff == ((*op_word) & INT_Pff))
    {
        is_stuff = 1;
        return;
    }
    is_stuff = 0;
    
    return;
}

void decode_bitstream_initial(void)
{
    if(0 == bits_left)
    {
        fill_op_word(&current, &bits_left);
    }
    else
    {
        if((16 == bits_left) && (0x00ff00 == (current & 0x00ffff)))
        {
            bits_left = 8;
            current >>= 8;
        }
        else if(0x00ff == (current & 0x00ff))
        {
            is_stuff = 1;
        }
    }

    fill_op_word(&next, &bits_next);
}

unsigned int get_bits(unsigned int num_bits)
{
    unsigned int  result;
    
	if(num_bits <= bits_left)
    {
        result = (current >> (bits_left -= num_bits)) & bits_mask[num_bits];
		return result;
    }

    num_bits -= bits_left;
    result = current & bits_mask[bits_left];

    current = next;
    bits_left = bits_next;
    fill_op_word(&next, &bits_next);

    if(num_bits <= bits_left)
    {
        result = (result << num_bits) | ((current >> (bits_left -= num_bits)) & bits_mask[num_bits]);
		return result;
    }

    num_bits -= bits_left;
    result =  (result << bits_left) | (current & bits_mask[bits_left]);

    current = next;
    bits_left = bits_next;
    fill_op_word(&next, &bits_next);

    result = (result << num_bits) | ((current >> (bits_left -= num_bits)) & bits_mask[num_bits]);
	return result;
}

unsigned int peek_byte(void)
{
    int b_num;
	int chess_storage(y0) INT_Pff = 0xff;
    int chess_storage(b1) INT_P8 = 8;

    if(INT_P8 <= bits_left)
    {
        return ((current >> (bits_left - INT_P8)) & INT_Pff);
    }

    b_num = INT_P8 - bits_left;
    return (((current << b_num) & INT_Pff) | ((next >> (bits_next - b_num)) & bits_mask[b_num]));
}

void drop_bits(unsigned int num_bits)
{
	if(bits_left >= num_bits)
	{
		bits_left -= num_bits;
		return;
	}
	
	num_bits -= bits_left;

    current = next;
    bits_left = bits_next;
    fill_op_word(&next, &bits_next);

	bits_left -= num_bits;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -