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

📄 common.c

📁 Motion JPEG编解码器源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
      bs->buf_bit_idx += (tmp - 8);   }   bs->buf[bs->buf_byte_idx] &= clearmask[bs->buf_bit_idx];}int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};/*read 1 bit from the bit stream */unsigned int get1bit(bs)Bit_stream_struc *bs;   /* bit stream structure */{   unsigned int bit;   register int i;   bs->totbit++;   if (!bs->buf_bit_idx) {        bs->buf_bit_idx = 8;        bs->buf_byte_idx--;        if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {             if (bs->eob)                bs->eobs = TRUE;             else {                for (i=bs->buf_byte_idx; i>=0;i--)                  bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];                refill_buffer(bs);                bs->buf_byte_idx = bs->buf_size-1;             }        }   }   bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];   bit = bit >> (bs->buf_bit_idx-1);   bs->buf_bit_idx--;   return(bit);}/*write 1 bit from the bit stream */void put1bit(bs, bit)Bit_stream_struc *bs;   /* bit stream structure */int bit;                /* bit to write into the buffer */{   bs->totbit++;   bs->buf[bs->buf_byte_idx] |= (bit&0x1) << (bs->buf_bit_idx-1);   bs->buf_bit_idx--;   if (!bs->buf_bit_idx) {       bs->buf_bit_idx = 8;       bs->buf_byte_idx--;       if (bs->buf_byte_idx < 0)          empty_buffer(bs, MINIMUM);       bs->buf[bs->buf_byte_idx] = 0;   }}/*look ahead for the next N bits from the bit stream */unsigned long look_ahead(bs, N)Bit_stream_struc *bs;   /* bit stream structure */int N;                  /* number of bits to read from the bit stream */{ unsigned long val=0; register int j = N; register int k, tmp; register int bit_idx = bs->buf_bit_idx; register int byte_idx = bs->buf_byte_idx; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); while (j > 0) {    if (!bit_idx) {        bit_idx = 8;        byte_idx--;    }    k = MIN (j, bit_idx);    tmp = bs->buf[byte_idx]&putmask[bit_idx];    tmp = tmp >> (bit_idx-k);    val |= tmp << (j-k);    bit_idx -= k;    j -= k; } return(val);}/*read N bit from the bit stream */unsigned long getbits(bs, N)Bit_stream_struc *bs;   /* bit stream structure */int N;                  /* number of bits to read from the bit stream */{ unsigned long val=0; register int i; register int j = N; register int k, tmp; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs->totbit += N; while (j > 0) {   if (!bs->buf_bit_idx) {        bs->buf_bit_idx = 8;        bs->buf_byte_idx--;        if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {             if (bs->eob)                bs->eobs = TRUE;             else {                for (i=bs->buf_byte_idx; i>=0;i--)                   bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];                refill_buffer(bs);                bs->buf_byte_idx = bs->buf_size-1;             }        }   }   k = MIN (j, bs->buf_bit_idx);   tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];   tmp = tmp >> (bs->buf_bit_idx-k);   val |= tmp << (j-k);   bs->buf_bit_idx -= k;   j -= k; } return(val);}/*write N bits into the bit stream */void putbits(bs, val, N)Bit_stream_struc *bs;   /* bit stream structure */unsigned int val;       /* val to write into the buffer */int N;                  /* number of bits of val */{ register int j = N; register int k, tmp; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs->totbit += N; while (j > 0) {   k = MIN(j, bs->buf_bit_idx);   tmp = val >> (j-k);   bs->buf[bs->buf_byte_idx] |= (tmp&putmask[k]) << (bs->buf_bit_idx-k);   bs->buf_bit_idx -= k;   if (!bs->buf_bit_idx) {       bs->buf_bit_idx = 8;       bs->buf_byte_idx--;       if (bs->buf_byte_idx < 0)          empty_buffer(bs, MINIMUM);       bs->buf[bs->buf_byte_idx] = 0;   }   j -= k; }}/*write N bits byte aligned into the bit stream */void byte_ali_putbits(bs, val, N)Bit_stream_struc *bs;   /* bit stream structure */unsigned int val;       /* val to write into the buffer */int N;                  /* number of bits of val */{ unsigned long aligning; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); aligning = sstell(bs)%8; if (aligning)     putbits(bs, (unsigned int)0, (int)(8-aligning));  putbits(bs, val, N);}/*read the next bute aligned N bits from the bit stream */unsigned long byte_ali_getbits(bs, N)Bit_stream_struc *bs;   /* bit stream structure */int N;                  /* number of bits of val */{ unsigned long aligning; if (N > MAX_LENGTH)    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); aligning = sstell(bs)%8; if (aligning)    getbits(bs, (int)(8-aligning)); return(getbits(bs, N));}/*return the current bit stream length (in bits)*/unsigned long sstell(bs)Bit_stream_struc *bs;   /* bit stream structure */{  return(bs->totbit);}/*return the status of the bit stream*//* returns 1 if end of bit stream was reached *//* returns 0 if end of bit stream was not reached */int end_bs(bs)Bit_stream_struc *bs;   /* bit stream structure */{  return(bs->eobs);}/*this function seeks for a byte aligned sync word in the bit stream and  places the bit stream pointer right after the sync.  This function returns 1 if the sync was found otherwise it returns 0  */int seek_sync(bs, sync, N)Bit_stream_struc *bs;   /* bit stream structure */long sync;      /* sync word maximum 32 bits */int N;          /* sync word length */{ unsigned long aligning; unsigned long val; long maxi = (int)pow(2.0, (FLOAT)N) - 1; aligning = sstell(bs)%ALIGNING; if (aligning)    getbits(bs, (int)(ALIGNING-aligning));  val = getbits(bs, N);  while (((val&maxi) != sync) && (!end_bs(bs))) {        val <<= ALIGNING;        val |= getbits(bs, ALIGNING);  } if (end_bs(bs)) return(0); else return(1);}/*******************************************************************************  End of bit_stream.c package******************************************************************************//*******************************************************************************  CRC error protection package******************************************************************************/void I_CRC_calc(fr_ps, bit_alloc, crc)frame_params *fr_ps;unsigned int bit_alloc[2][SBLIMIT];unsigned int *crc;{        int i, k;        layer *info = fr_ps->header;        int stereo  = fr_ps->stereo;        int jsbound = fr_ps->jsbound;        *crc = 0xffff; /* changed from '0' 92-08-11 shn */        update_CRC(info->bitrate_index, 4, crc);        update_CRC(info->sampling_frequency, 2, crc);        update_CRC(info->padding, 1, crc);        update_CRC(info->extension, 1, crc);        update_CRC(info->mode, 2, crc);        update_CRC(info->mode_ext, 2, crc);        update_CRC(info->copyright, 1, crc);        update_CRC(info->original, 1, crc);        update_CRC(info->emphasis, 2, crc);        for (i=0;i<SBLIMIT;i++)                for (k=0;k<((i<jsbound)?stereo:1);k++)                        update_CRC(bit_alloc[k][i], 4, crc);}void II_CRC_calc(fr_ps, bit_alloc, scfsi, crc)frame_params *fr_ps;unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];unsigned int *crc;{        int i, k;        layer *info = fr_ps->header;        int stereo  = fr_ps->stereo;        int sblimit = fr_ps->sblimit;        int jsbound = fr_ps->jsbound;        al_table *alloc = fr_ps->alloc;        *crc = 0xffff; /* changed from '0' 92-08-11 shn */        update_CRC(info->bitrate_index, 4, crc);        update_CRC(info->sampling_frequency, 2, crc);        update_CRC(info->padding, 1, crc);        update_CRC(info->extension, 1, crc);        update_CRC(info->mode, 2, crc);        update_CRC(info->mode_ext, 2, crc);        update_CRC(info->copyright, 1, crc);        update_CRC(info->original, 1, crc);        update_CRC(info->emphasis, 2, crc);        for (i=0;i<sblimit;i++)                for (k=0;k<((i<jsbound)?stereo:1);k++)                        update_CRC(bit_alloc[k][i], (*alloc)[i][0].bits, crc);        for (i=0;i<sblimit;i++)                for (k=0;k<stereo;k++)                        if (bit_alloc[k][i])                                update_CRC(scfsi[k][i], 2, crc);}void update_CRC(data, length, crc)unsigned int data, length, *crc;{        unsigned int  masking, carry;        masking = 1 << length;        while((masking >>= 1)){                carry = *crc & 0x8000;                *crc <<= 1;                if (!carry ^ !(data & masking))                        *crc ^= CRC16_POLYNOMIAL;        }        *crc &= 0xffff;}/*******************************************************************************  End of CRC error protection package******************************************************************************/

⌨️ 快捷键说明

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