求教: mpeg4形状编码中的CAE(内容算术编码)方法!(可以再加分)(200分)

  • 主题发起人 主题发起人 GanQuan
  • 开始时间 开始时间
G

GanQuan

Unregistered / Unconfirmed
GUEST, unregistred user!
向各位大侠请教,关于MPEG4形状编码中的CAE(内容算术编码)方法.
关于算法思想,实现方案等. 希望能够提供此方面的介绍文章和对此方面的看法!!!
 
cae.h内容:

/*************************************************************************

This software module was adopted from MPEG4 VM7.0 text, modified by

Wei-ge Chen (wchen@microsoft.com), Microsoft Corporation
(date: April, 1997)

and edited by
Yoshihiro Kikuchi (TOSHIBA CORPORATION)
Takeshi Nagai (TOSHIBA CORPORATION)
Toshiaki Watanabe (TOSHIBA CORPORATION)
Noboru Yamaguchi (TOSHIBA CORPORATION)

in the course of development of the MPEG-4 Video (ISO/IEC 14496-2).
This software module is an implementation of a part of one or more MPEG-4 Video tools
as specified by the MPEG-4 Video.
ISO/IEC gives users of the MPEG-4 Video free license to this software module or modifications
thereof for use in hardware or software products claiming conformance to the MPEG-4 Video.
Those intending to use this software module in hardware or software products are advised that its use may infringe existing patents.
The original developer of this software module and his/her company,
the subsequent editors and their companies,
and ISO/IEC have no liability for use of this software module or modifications thereof in an implementation.
Copyright is not released for non MPEG-4 Video conforming products.
Microsoft retains full right to use the code for his/her own purpose,
assign ordo
nate the code to a third party and to inhibit third parties from using the code for non <MPEG standard> conforming products.
This copyright notice must be included in all copies or derivative works.

Copyright (c) 1996, 1997.

Module Name:

cae.h

Abstract:

Context-based arithmatic coding

Revision History:

*************************************************************************/

typedef unsigned short int USInt;
#define CODE_BITS 32
#define HALF ((unsigned) 1 << (CODE_BITS-1))
#define QUARTER (1 << (CODE_BITS-2))

#define ArCodec arcodec
#define ArCoder arcodec
#define ArDecoder arcodec

Class COutBitStream;
Class CInBitStream;
Class arcodec {
public:
arcodec::~arcodec () {};
arcodec::arcodec () {};
UInt L;
/* lower bound */
UInt R;
/* code range */
UInt V;
/* current code value */
UInt arpipe;
Int bits_to_follow;
/* follow bit count */
Int first_bit;
Int nzeros;
Int nonzero;
Int nzerosf;
Int extrabits;
Int nBits;
};


#define MAXHEADING 8
#define MAXMIDDLE 16
#define MAXTRAILING 8
// Added for error resilient mode by Toshiba(1997-11-14)
#define MAXHEADING_ERR 3
#define MAXMIDDLE_ERR 10
#define MAXTRAILING_ERR 2
// End Toshiba(1997-11-14)

API Void StartArCoder(ArCoder *coder);
API Int StopArCoder(ArCoder *coder, COutBitStream *bitstream);
API Void BitByItself(Int bit, ArCoder *coder, COutBitStream *bitstream);
API Void BitPlusFollow(Int bit, ArCoder *coder, COutBitStream *bitstream);
API Void ArCodeSymbol(Int bit, USInt c0, ArCoder *coder, COutBitStream *bitstream);
API Void ENCODE_RENORMALISE(ArCoder *coder, COutBitStream *bitstream);
API Void StartArDecoder(ArDecoder *decoder, CInBitStream *bitstream);
API Void StopArDecoder(ArDecoder *decoder, CInBitStream *bitstream);
API Void AddNextInputBit(CInBitStream *bitstream, ArDecoder *decoder);
API Int ArDecodeSymbol(USInt c0, ArDecoder *decoder, CInBitStream *bitstream);
API Void DECODE_RENORMALISE(ArDecoder *decoder, CInBitStream *bitstream);
API Void BitstreamFlushBits(CInBitStream *bitstream, Int nBits);

----------------------------------------------------------------------------------------

cae.cpp内容:

/*************************************************************************

This software module was adopted from MPEG4 VM7.0 text, modified by

Wei-ge Chen (wchen@microsoft.com), Microsoft Corporation
(date: April, 1997)

and also edited by
Yoshihiro Kikuchi (TOSHIBA CORPORATION)
Takeshi Nagai (TOSHIBA CORPORATION)
Toshiaki Watanabe (TOSHIBA CORPORATION)
Noboru Yamaguchi (TOSHIBA CORPORATION)

in the course of development of the MPEG-4 Video (ISO/IEC 14496-2).
This software module is an implementation of a part of one or more MPEG-4 Video tools
as specified by the MPEG-4 Video.
ISO/IEC gives users of the MPEG-4 Video free license to this software module or modifications
thereof for use in hardware or software products claiming conformance to the MPEG-4 Video.
Those intending to use this software module in hardware or software products are advised that its use may infringe existing patents.
The original developer of this software module and his/her company,
the subsequent editors and their companies,
and ISO/IEC have no liability for use of this software module or modifications thereof in an implementation.
Copyright is not released for non MPEG-4 Video conforming products.
Microsoft retains full right to use the code for his/her own purpose,
assign ordo
nate the code to a third party and to inhibit third parties from using the code for non <MPEG standard> conforming products.
This copyright notice must be included in all copies or derivative works.

Copyright (c) 1996, 1997.

Module Name:

cae.c

Abstract:

Context-based arithmatic coding

Revision History:

*************************************************************************/

#include "typeapi.h"
#include "basic.hpp"
#include "global.hpp" // Added for error resilient mode by Toshiba(1997-11-14)
#include "entropy/bitstrm.hpp"
#include "cae.h"

#ifdef __MFC_
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW
#endif // __MFC_


/* START ENCODING A STREAM OF SYMBOLS */

Void StartArCoder(ArCoder *coder)
{
coder->L = 0;
coder->R = HALF-1;
coder->bits_to_follow = 0;
coder->first_bit = 1;
coder->nzeros = g_iMaxHeading; // Modified for error resilient mode by Toshiba(1997-11-14)
coder->nonzero = 0;
coder->nBits = 0; //added by wchen
}

/* FINISH ENCODING THE STREAM */

Int StopArCoder(ArCoder *coder, COutBitStream* bitstream)
{

Int a = (coder->L) >> (CODE_BITS-3);
Int b = (coder->R + coder->L) >> (CODE_BITS-3);
Int nbits, bits, i;

if (b == 0)
b = 8;

if (b - a >= 4 || (b - a == 3 &amp;&amp;
(a &amp;
1))) {
nbits = 2;
bits = (a>>1) + 1;
}
else
{
nbits = 3;
bits = a + 1;
}

for (i = 1;
i <= nbits;
i++)
BitPlusFollow (((bits >> (nbits - i)) &amp;
1), coder, bitstream);

if (coder->nzeros < g_iMaxMiddle-g_iMaxTrailing || coder->nonzero == 0) { // Modified for error resilient mode by Toshiba(1997-11-14)
BitPlusFollow (1, coder, bitstream);
}
// return;
return 0;
}

Void BitByItself(Int bit, ArCoder *coder, COutBitStream* bitstream) {
//BitstreamPutBit(bitstream,bit); /* Output the bit */
if (bitstream != NULL)
bitstream->putBits (bit, 1, "MB_CAE_Bit");
coder->nBits++;
if (bit == 0) {
coder->nzeros--;
if (coder->nzeros == 0) {
// BitstreamPutBit(bitstream,1);
if (bitstream != NULL)
bitstream->putBits (1, (UInt) 1, "MB_CAE_Bit");
coder->nBits++;
coder->nonzero = 1;
coder->nzeros = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
}
}
else
{
coder->nonzero = 1;
coder->nzeros = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
}
}

/* OUTPUT BITS PLUS FOLLOWING OPPOSITE BITS */

Void BitPlusFollow(Int bit, ArCoder *coder, COutBitStream *bitstream)
{
if (!coder->first_bit)
BitByItself(bit, coder, bitstream);
else

coder->first_bit = 0;
while ((coder->bits_to_follow) > 0) {
BitByItself(!bit, coder, bitstream);
coder->bits_to_follow -= 1;
}
}


/* ENCODE A BINARY SYMBOL */

Void ArCodeSymbol(Int bit, USInt c0, ArCoder *coder, COutBitStream *bitstream)
{
Int bits = 0;
USInt c1 = (1<<16) - c0;

Int LPS = (c0 > c1);
USInt cLPS = (LPS) ? c1 : c0;
assert (cLPS != 0);

unsigned long rLPS;

rLPS = ((coder->R) >> 16) * cLPS;

if (bit == LPS) {
coder->L += coder->R - rLPS;
coder->R = rLPS;
}
else

coder->R -= rLPS;

ENCODE_RENORMALISE(coder,bitstream);
}


Void ENCODE_RENORMALISE(ArCoder *coder, COutBitStream* bitstream)
{
while (coder->R < QUARTER) {
if (coder->L >= HALF) {
BitPlusFollow(1,coder,bitstream);
coder->L -= HALF;
}
else

if (coder->L + coder->R <= HALF)
BitPlusFollow(0,coder,bitstream);
else
{
coder->bits_to_follow++;
coder->L -= QUARTER;
}
coder->L += coder->L;
coder->R += coder->R;
}
}

//Decoder part
Void StartArDecoder(ArDecoder *decoder, CInBitStream *bitstream)
{
Int i,j;

decoder->V = 0;

decoder->nzerosf = g_iMaxHeading; // Modified for error resilient mode by Toshiba(1997-11-14)
decoder->extrabits = 0;

for (i = 1;
i<CODE_BITS;
i++) {
// j = BitstreamLookBit(bitstream,i+decoder->extrabits);
j = bitstream->peekOneBit (i + decoder->extrabits);
decoder->V += decoder->V + j;
if (j == 0) {
decoder->nzerosf--;
if (decoder->nzerosf == 0) {
decoder->extrabits++;
decoder->nzerosf = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
}
}
else

decoder->nzerosf = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
}

decoder->L = 0;
decoder->R = HALF - 1;
decoder->bits_to_follow = 0;
decoder->arpipe = decoder->V;
decoder->nzeros = g_iMaxHeading; // Modified for error resilient mode by Toshiba(1997-11-14)
decoder->nonzero = 0;
}


Void StopArDecoder(ArDecoder *decoder, CInBitStream *bitstream)
{
Int a = decoder->L >> (CODE_BITS - 3);
Int b = (decoder->R + decoder->L) >> (CODE_BITS - 3);

Int nbits,i;

if (b == 0) {
b = 8;
}
if (b - a >= 4 || (b - a == 3 &amp;&amp;
a&amp;1))
nbits = 2;
else

nbits = 3;

for (i = 1;
i <= nbits - 1;
i++)
AddNextInputBit (bitstream, decoder);

if (decoder->nzeros < g_iMaxMiddle-g_iMaxTrailing || decoder->nonzero == 0) { // Modified for error resilient mode by Toshiba(1997-11-14)
BitstreamFlushBits(bitstream,1);
}
}


Void AddNextInputBit(CInBitStream *bitstream, ArDecoder *decoder)
{
Int i;
if (((decoder->arpipe >> (CODE_BITS-2)) &amp;
1) == 0) {
decoder->nzeros--;
if (decoder->nzeros == 0) {
BitstreamFlushBits(bitstream,1);
decoder->extrabits--;
decoder->nzeros = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
decoder->nonzero = 1;
}
}
else
{
decoder->nzeros = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
decoder->nonzero = 1;
}

BitstreamFlushBits(bitstream, 1);
// i = (Int)BitstreamLookBit(bitstream, CODE_BITS-1+decoder->extrabits);
i = bitstream->peekOneBit (CODE_BITS-1+decoder->extrabits);
decoder->V += decoder->V + i;
decoder->arpipe += decoder->arpipe + i;

if (i == 0) {
decoder->nzerosf--;
if (decoder->nzerosf == 0) {
decoder->nzerosf = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
decoder->extrabits++;
}
}
else

decoder->nzerosf = g_iMaxMiddle; // Modified for error resilient mode by Toshiba(1997-11-14)
}


Int ArDecodeSymbol(USInt c0, ArDecoder *decoder, CInBitStream *bitstream)
{
Int bit;
Int c1 = (1<<16) - c0;
Int LPS = c0 > c1;
Int cLPS = LPS ? c1 : c0;
unsigned long rLPS;

rLPS = ((decoder->R) >> 16) * cLPS;
if ((decoder->V - decoder->L) >= (decoder->R - rLPS)) {
bit = LPS;
decoder->L += decoder->R - rLPS;
decoder->R = rLPS;
}
else
{
bit = (1-LPS);
decoder->R -= rLPS;
}

DECODE_RENORMALISE(decoder,bitstream);
return(bit);
}

Void DECODE_RENORMALISE(ArDecoder *decoder, CInBitStream *bitstream)
{
while (decoder->R < QUARTER) {
if (decoder->L >= HALF) {
decoder->V -= HALF;
decoder->L -= HALF;
decoder->bits_to_follow = 0;
}
else

if (decoder->L + decoder->R <= HALF)
decoder->bits_to_follow = 0;
else
{
decoder->V -= QUARTER;
decoder->L -= QUARTER;
(decoder->bits_to_follow)++;
}
decoder->L += decoder->L;
decoder->R += decoder->R;
AddNextInputBit(bitstream, decoder);
}
}

Void BitstreamFlushBits(CInBitStream *bitstream, Int nBits)
{
assert (nBits >= 0);
bitstream->getBits (nBits);
}


/*
//decoding
pf = fopen (TEST_FILENAME, "r");
ac = (ArDecoder*) malloc (sizeof (ArDecoder));
StartArDecoder (ac, pf);
for (i = 0;
i < NUM_SYMBOL;
i++) {
j = ArDecodeSymbol (intra_prob , ac, pf); //context is simply "i"
assert (j == i % 2); //symbol should be 0, 1 alternatively
}
StopArDecoder(ac, pf);
free (ac);
fclose (pf);
*/

C语言的不懂,所以你自己看着办,我这有mpeg4的完全源码,但是C++的,如果你要并能做成delphi控件,
就告诉我。
 
dwq:
能不能把你的元码mail给我,有时间我把它搞成delphi.
my email : testmyself@163.net
 
To dwq:
MPEG4的源代码我也有,看来一段时间,看到头大了几圈. 我想单靠源码是无法
看懂的. 我想找一些CAE的算法详细介绍文章,如果你有的话,请mail给我 .
lucky_gan@263.net
thanks.


 
没有人回答吗? 再加 200 分.
可能不是加分所能解决的. 不知怎么回事,一直找不到关于这方面的资料.
一些关于MPEG4的书籍谈到形状编码和其中的CAE算法时也都是一笔代过. why?
 
天外客:源码已妹过去,请查看!
 
可以给我一份吗?programfree@21cn.com
谢谢
 
dwq:
可以给我一份吗?luke@tang.com.cn
谢谢
 
oookkk,luket:我已寄过去了,请查收!
 
Dwq:
可以给我一份吗? chemhawk@sina.com
 
Lera:已发过去,请查收
 
原代码找到了已经不错了,我想也是从别的国家的间接传过来的。
美国禁止这种技术向国外出售!封锁!
我想大家还是找一些c++的高手,自己想把法把,有远代码,沸点力气,还是可以高顶的
希望大富翁早日作出delphi 版 的。
关注!
 
我也想要源码
tian.dan@263.net
 
toy-lin@163.net
 
Dwq:
可以给我一份吗? lin_jinsong@21cn.com
 
dwq:
可以给我一份吗?

xin_jian@21cn.com
 
接受答案了.
 
我来的晚,不过能给我Mail一份吗 ln_other@263.net
 
也Mail 我一份:lhaoyan@163.net, 谢谢!
 
dwq:
请给我一份MPEG4的CAE的算法!谢了!
woocasing@163.com
 
后退
顶部