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

📄 ac.cpp

📁 《Visual C++小波变换技术与工程实践》靳济芳编著的光盘程序。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* $Id: ac.c,v 1.32 1998/05/14 21:41:16 hjlee Exp $ */

/****************************************************************************/
/*   MPEG4 Visual Texture Coding (VTC) Mode Software                        */
/*                                                                          */
/*   This software was jointly developed by the following participants:     */
/*                                                                          */
/*   Single-quant,  multi-quant and flow control                            */
/*   are provided by  Sarnoff Corporation                                   */
/*     Iraj Sodagar   (iraj@sarnoff.com)                                    */
/*     Hung-Ju Lee    (hjlee@sarnoff.com)                                   */
/*     Paul Hatrack   (hatrack@sarnoff.com)                                 */
/*     Shipeng Li     (shipeng@sarnoff.com)                                 */
/*     Bing-Bing Chai (bchai@sarnoff.com)                                   */
/*     B.S. Srinivas  (bsrinivas@sarnoff.com)                               */
/*                                                                          */
/*   Bi-level is provided by Texas Instruments                              */
/*     Jie Liang      (liang@ti.com)                                        */
/*                                                                          */
/*   Shape Coding is provided by  OKI Electric Industry Co., Ltd.           */
/*     Zhixiong Wu    (sgo@hlabs.oki.co.jp)                                 */
/*     Yoshihiro Ueda (yueda@hlabs.oki.co.jp)                               */
/*     Toshifumi Kanamaru (kanamaru@hlabs.oki.co.jp)                        */
/*                                                                          */
/*   OKI, Sharp, Sarnoff, TI and Microsoft contributed to bitstream         */
/*   exchange and bug fixing.                                               */
/*                                                                          */
/*                                                                          */
/* In the course of development of the MPEG-4 standard, this software       */
/* module is an implementation of a part of one or more MPEG-4 tools as     */
/* specified by the MPEG-4 standard.                                        */
/*                                                                          */
/* The copyright of this software belongs to ISO/IEC. ISO/IEC gives use     */
/* of the MPEG-4 standard free license to use this  software module or      */
/* modifications thereof for hardware or software products claiming         */
/* conformance to the MPEG-4 standard.                                      */
/*                                                                          */
/* Those intending to use this software module in hardware or software      */
/* products are advised that use may infringe existing  patents. The        */
/* original developers of this software module and their companies, the     */
/* subsequent editors and their companies, and ISO/IEC have no liability    */
/* and ISO/IEC have no liability for use of this software module or         */
/* modification thereof in an implementation.                               */
/*                                                                          */
/* Permission is granted to MPEG members to use, copy, modify,              */
/* and distribute the software modules ( or portions thereof )              */
/* for standardization activity within ISO/IEC JTC1/SC29/WG11.              */
/*                                                                          */
/* Copyright 1995, 1996, 1997, 1998 ISO/IEC                                 */
/****************************************************************************/

/************************************************************/
/*     Sarnoff Very Low Bit Rate Still Image Coder          */
/*     Copyright 1995, 1996, 1997, 1998 Sarnoff Corporation */
/************************************************************/

/************************************************************/
/*  Filename: ac.c                                          */
/*  Author: B.S. Srinivas                                   */
/*  Date Modified: April 23, 1998                           */
/*                                                          */
/*  Descriptions:                                           */
/*    This file contains routines for Integer arithmetic    */
/*    coding, which is based on the ac.c file from the SOL  */
/*    package.                                              */
/*                                                          */
/*    The following routines are modified or created for    */
/*    the latest VTC package:                               */
/*      static Void mzte_output_bit()                       */
/*      Void mzte_ac_encoder_init()                         */
/*      Int mzte_ac_encoder_done()                          */
/*      static Int mzte_input_bit()                         */
/*      Void mzte_ac_decoder_init()                         */
/*      Void mzte_ac_decoder_done()                         */
/*                                                          */
/************************************************************/

//#include <stdio.h>
//#include <stdlib.h>
//#include <assert.h>
#include "dataStruct.hpp"
#include "ac.hpp"
#include "bitpack.hpp"
#include "errorHandler.hpp"
#include "msg.hpp"

#define codeValueBits 16
#define peakValue (((long)1<<codeValueBits)-1)
#define firstQtr  (peakValue/4+1)
#define Half      (2*firstQtr)
#define thirdQtr  (3*firstQtr)

#define BYTETYPE	UChar
#define SHORTTYPE	UShort
#define MIN(a,b)  (((a)<(b)) ? (a) : (b))

//Changed by Sarnoff for error resilience, 3/5/99
//#define STUFFING_CNT 22
static Int STUFFING_CNT=22;  //setting for error resi case
//End changed by Sarnoff for error resilience, 3/5/99

#define MIXED 		2

/* static function prototypes */

static Void mzte_output_bit(ac_encoder *,Int);
static Void mzte_bit_plus_follow(ac_encoder *,Int);
static Void mzte_update_model(ac_model *,ac_model *,Int);
static Int mzte_input_bit(ac_decoder *);


static Int zeroStrLen=0;

/************************************************************************/
/*              Error Checking and Handling Macros                      */
/************************************************************************/

#define error(m)                                           \
do {                                                      \
  fflush(stdout);                                         \
  fprIntf(stderr, "%s:%d: error: ", __File__, __LINE__);  \
  fprIntf(stderr, m);                                     \
  fprIntf(stderr, "\n");                                  \
  exit(1);                                                \
} while (0)

#define check(b,m)                                         \
do { 													   \
  if (b)                                                   \
	 error(m);                                            \
} while (0)

/************************************************************************/
/*                           Bit Output                                 */
/************************************************************************/

/**************************************************/
/*  Added bit stuffing to prevent start code      */
/*  emulation, i.e., add a "1" bit after every 22 */
/*  consecutive "0" bits in the bit stream        */
/*                                                */
/*  Modified to use a fixed buffer and write to   */
/*  file directly after the buffer is full. So the*/
/*  ace->bitstreamLength now only has the # of 	  */
/*  bytes in current buffer. Total bits (bitCount)*/
/*  will indicate the total # bits for arithmetic */
/*  coding part.								  */
/**************************************************/

Void CVTCEncoder::mzte_output_bit(ac_encoder *ace,Int bit)
{
	register flag=0;

	ace->buffer *= 2;
	ace->buffer |= (bit)?0x01:0;
	(ace->bitsLeft)--;
	(ace->bitCount)++;
	if (!(ace->bitsLeft)) {
		if (!(ace->bitstream))
			errorHandler("Failure to allocate space for array Bitstream " \
                            "in ac_encoder structure");
		switch (flag=(ace->bitstreamLength>=MAX_BUFFER)) {
			case 1:
				write_to_bitstream(ace->bitstream,MAX_BUFFER<<3);
				ace->bitstreamLength=0;
				break;
			default:
				break;
		}
		ace->bitstream[(ace->bitstreamLength)++] = ace->buffer;
		ace->bitsLeft = 8;
	}
	/* Dealing with bit stuffing when 0's are encountered */
	zeroStrLen+=(!bit)?1:-zeroStrLen;
	if (zeroStrLen==STUFFING_CNT) {
		mzte_output_bit(ace,1);
		zeroStrLen=0;
	}
	return;
}

Void CVTCEncoder::mzte_bit_plus_follow(ac_encoder *ace,Int bit)
{
	register followBits;

	followBits = ace->followBits;
	mzte_output_bit(ace,bit);
	while (followBits) {
		mzte_output_bit(ace,!bit);
		--followBits;
	}
	ace->followBits = followBits;
	return;
}

/************************************************************************/
/*                             Encoder                                  */
/************************************************************************/

Void CVTCEncoder::mzte_ac_encoder_init(ac_encoder *ace)
{
	ace->low = 0;
	ace->high = peakValue;
	ace->followBits = 0;
	ace->bitsLeft = 8;
	ace->buffer = 0;
	ace->bitCount = 0;
	ace->bitstreamLength = 0;
	ace->bitstream=(BYTETYPE *)malloc((MAX_BUFFER+10)*sizeof(BYTETYPE));
	if (ace->bitstream == NULL)
		errorHandler("can't allocate memory for ace->bitstream");
//	assert((ace->bitstream=(BYTETYPE *)malloc((MAX_BUFFER+10)*sizeof(BYTETYPE))));
	zeroStrLen=0;

	//Added by Sarnoff for error resilience, 3/5/99
	if(!mzte_codec.m_usErrResiDisable)
		STUFFING_CNT=15;
	//End Added by Sarnoff for error resilience, 3/5/99

	/* always start arithmetic coding bitstream with a 1 bit. */
	emit_bits(1,1);
	return;
}


/***************************************************************/
/* Added stuffing bits to prevent start code emulation.        */
/***************************************************************/

Int CVTCEncoder::mzte_ac_encoder_done(ac_encoder *ace)
{
	register bitCount,bitsLeft;
	register bitsToWrite,streamLength;
	register flag;

	++(ace->followBits);
	flag = (ace->low >= firstQtr);
	mzte_bit_plus_follow(ace,flag);
	bitsLeft = ace->bitsLeft;
	bitCount = ace->bitCount;
	streamLength = ace->bitstreamLength;
	if (bitsLeft != 8) {
		ace->bitstream[streamLength++] = (ace->buffer << bitsLeft);
		if (!(ace->bitstream[streamLength-1]&(1<<bitsLeft))) {
	  		ace->bitstream[streamLength-1] += (1<<bitsLeft)-1;
			++bitCount;
		}
	}
	bitsToWrite = (streamLength > MAX_BUFFER)?(MAX_BUFFER<<3):0;
	bitsToWrite += (bitCount % (MAX_BUFFER<<3));
	if ((bitsToWrite==0) && (streamLength==MAX_BUFFER))
		bitsToWrite=(MAX_BUFFER<<3);
	write_to_bitstream(ace->bitstream,bitsToWrite);
	if ((bitsLeft==8) && (!(ace->bitstream[streamLength-1]&1))) {
		/* stuffing bits to prevent start code emulation */
		emit_bits(1,1);
		++bitCount;
	}
	ace->bitstreamLength=streamLength;
	ace->bitCount=bitCount;
	free(ace->bitstream);
	return(ace->bitCount);
}

Int CVTCEncoder::mzte_ac_encode_symbol(ac_encoder *ace, ac_model *acm, Int sym)

⌨️ 快捷键说明

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