📄 avq_cod.c
字号:
RE8_cod(&xriq[pos*8], &nq[pos], &I[pos], &kv[8*pos]);
if (nq[pos]>0) {
k = pos_max;
if (pos > k) {
k = pos;
}
/* check for overflow and compute number of bits-1 (n) */
n = calc_bits(nq[pos]);
if ((n_bits+n+k) > NB_BITS) { /* if budget overflow */
nq[pos] = 0; /* force Q0 */
} else {
n_bits += n;
pos_max = k; /* update index of last described subvector (last) */
}
}
}
}
/*
writ_all_nq(n_pack, nq, pos_n, NB_BITS, last, parm_ptr)
ENCODE AND MULTIPLEX ALL CODEBOOKS NUMBERS IN nq[] TRACK-BY-TRACK
-> n_pack : number of packets
-> nq : table of codebook numbers [0..Nsv-1]
-> pos_n : table of pointers to write nq in packets [0..n_pack-1]
-> NB_BITS : total bit allocation
<- last : index of last subvector for which an index is written
<-> parm : bistream
*/
static void writ_all_nq(int n_pack, int *nq, int *pos_n, int NB_BITS, int Nsv, int *last, int **parm_ptr)
{
int p, pos, l, i, n_bits, *parm;
n_bits = NB_BITS;
*last = -1;
/* write nq[l] for l=0...Nsv-1 in packet #p= mod(l,number_of_packets)*/
for (l=0;l<Nsv;l++) {
p = l%n_pack; /* alternative : apply mask 0x01 / 0x03 */
parm = parm_ptr[p];
pos = pos_n[p];
/* compute the minimal bit budget to write nq[l] and the related index */
i = calc_bits(nq[l]);
/* if the budget is exceeded, force nq[l] to 0
else decrement the number of left bits */
if (i > n_bits) {
nq[l] = 0;
} else {
n_bits -= i;
}
/* update "last" */
if (nq[l] >= 2) {
*last=l;
}
/* write the unary code (except stop bit) for nq[l] in packet #p */
i = nq[l] - 1;
while (i-- > 0) {
parm[pos/4] += 1<<(pos%4);
pos--;
}
/* if bit budget is not empty, write stop bit of unary code */
if (n_bits > 0) {
pos--;
n_bits--;
}
pos_n[p] = pos;
}
return;
}
/* check if n groups of 4 bits fit in n_bits bits */
static void chk_ovf(int n_bits, int n, int *n1, int *n2)
{
if (4*n<=n_bits) {
*n1=n;
*n2=0;
} else {
*n1=n_bits/4; /* >> 2*/
*n2=n-*n1;
}
}
/* write n groups of 4-bit for base codebook index (I) */
static void writ_I(int n, int *pos_i, long *I, int *parm)
{
int pos;
/* base codebook index */
pos = *pos_i/4;
while (n-- > 0) {
parm[pos++] = (*I & 0x0F);
*I = *I>>4;
}
*pos_i=pos*4;
}
/* write n groups of 4-bit for Voronoi index (k[]) */
static void writ_k(int n, int *pos_i, int *k, int *parm, int flag)
{
int i, ival, delta, *kv, pos;
delta = 4*flag;
pos = *pos_i/4;
while (n-- > 0) {
kv=k+delta;
ival = 0;
for (i=0; i<4; i++) {
ival <<= 1;
ival += (kv[i] & 0x01);
kv[i] >>= 1;
}
parm[pos++] = ival;
delta = (delta+4)%8; /* circular shift */
}
*pos_i=pos*4;
}
/* find in each packet the positions where overflow occurs */
static void init_pos_i_ovf(int n_pack, int *nq, int *pos_n, int last, int *pos_i_ovf)
{
int p, pos, n_bits, l, n1, n2;
for (p=0; p<n_pack; p++) {
pos = 0;
n_bits = pos_n[p]+1; /* pos_n[p] - 0 +1 */
for (l=p; l<=last; l+=n_pack) {
if (nq[l] > 0) {
chk_ovf(n_bits, nq[l], &n1, &n2);
n_bits -= 4*n1;
pos += n1;
}
}
pos_i_ovf[p] = pos*4;
}
}
/* write bits in overflow */
static void writ_ovf(int n_pack, int *parm_ovf, int n, int *pos_i_ovf, int *pos_n, int **parm_ptr)
{
int pos_ovf, p, n_bits, pos, *parm, moved_bit;
/* initialize position in overflow packet (parm_ovf[]) */
pos_ovf=0;
/* move bits from overflow packet (parm_ovf[]) to packets (parm_ptr[][])
[write 4-bit by 4-bit] */
for (p=0; p<n_pack; p++) {
/* compute number of bits left in packet #p */
n_bits = pos_n[p]-pos_i_ovf[p]+1;
if ((n_bits >= 4) && (n>0)) {
pos=pos_i_ovf[p]/4;
parm = parm_ptr[p];
do {
parm[pos++]=parm_ovf[pos_ovf++]; /* move 4 bits */
n_bits -= 4;
n -= 4;
} while ((n_bits>=4) && (n>0));
pos_i_ovf[p]=pos*4;
}
}
pos_ovf *=4;
/* move bits remaining in overflow packet
[write bit-by-bit (3 bits at maximum per packet)] */
for (p=0; p<n_pack; p++) {
/* compute number of bits left in packet #p */
n_bits = pos_n[p]-pos_i_ovf[p]+1;
if ((n_bits > 0) && (n>0)) {
pos = pos_i_ovf[p];
parm = parm_ptr[p];
do {
/* write a single bit */
moved_bit = (parm_ovf[pos_ovf/4] >> (pos_ovf%4)) & 0x01;
parm[pos/4] += moved_bit<<(pos%4);
pos++;
pos_ovf++;
n_bits--;
n--;
} while ((n_bits > 0) && (n > 0));
pos_i_ovf[p] = pos;
}
}
}
/*
writ_all_i(nq, p, pos_n, n_pack, last, pos_ovf, n_bits_left, parm_ptr)
MULTIPLEX AN INDEX (I,kv) OF 4*nq BITS INTO BITSTREAM
(THE INDEX CORRESPONDS TO A SUBVECTOR IN TRACK #p)
-> nq : codebook number (scalar)
-> p : index of track
-> pos_i : pointer to write i in packet [0..n_pack-1]
-> n_pack : number of packets
<-> pos_ovf : pointers for overflow [0..n_pack-1]
<-> n_bits_left : number of unused bits in packets [0..n_pack-1]
-> I : base codebook index
-> kv : Voronoi index
<-> parm : bistream [initialized to zero when writ_all_i is called]
important note:
- if the index fits completely in packet #p, multiplexing is done as
in writ_parm from top to bottom:
the 1st bit of I is written at the top
the last bit of kv at the bottom
*/
static void writ_all_i(int n_pack, int *nq, int *pos_n, int last, long *I, int *kv, int **parm_ptr)
{
int pos_ovf, p, l, n_bits, ni, nk, n1, i, n2, pos, k[8], *parm;
long index;
int parm_ovf[NQ_MAX];
int pos_i_ovf[N_PACK_MAX];
/* initialize overflow packet */
for (i=0; i<NQ_MAX; i++) {
parm_ovf[i]=0;
}
/* find positions in each packet from which the bits in overflow can be
written */
init_pos_i_ovf(n_pack, nq, pos_n, last, pos_i_ovf);
/* write indices */
for (p=0; p<n_pack; p++) {
pos = 0;
parm = parm_ptr[p];
for (l=p; l<=last; l+=n_pack) {
if (nq[l] > 0) {
/* compute number of bits left in packet #p */
n_bits = pos_n[p]-pos+1;
/* compute number of 4-bit groups for base codebook index (ni)
and Voronoi index (nk) */
ni=nq[l];
nk = 0;
if (ni > 4) {
nk = (ni-4+1)/2; /* nkv*2 = number of 4-bit groups */
nk *= 2;
ni -= nk;
}
/* write base codebook index (in packet #p / overflow packet) */
index = I[l];
chk_ovf(n_bits, ni, &n1, &n2);
writ_I(n1, &pos, &index, parm);
n_bits -= 4*n1;
if (n2>0) {
/* write 4-bit groups in overflow packet */
pos_ovf = 0;
writ_I(n2, &pos_ovf, &index, parm_ovf);
/* distribute bits from overflow packet to packets
#0 to n_pack-1 */
writ_ovf(n_pack, parm_ovf, 4*n2, pos_i_ovf, pos_n, parm_ptr);
for (i=0; i<n2; i++) {
parm_ovf[i]=0;
}
}
if (nk>0) {
/* write Voronoi index (in packet #p / overflow packet) */
for (i=0; i<8; i++) {
k[i] = kv[8*l+i];
}
chk_ovf(n_bits, nk, &n1, &n2);
writ_k(n1, &pos, k, parm, 0);
if (n2>0) {
pos_ovf = 0;
writ_k(n2, &pos_ovf, k, parm_ovf,n1%2);
writ_ovf(n_pack, parm_ovf, 4*n2, pos_i_ovf, pos_n, parm_ptr);
for (i=0; i<n2; i++) {
parm_ovf[i]=0;
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -