📄 fake_decoder.cpp
字号:
/*
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 1999-2006 Intel Corporation. All Rights Reserved.
//
// Intel(R) Integrated Performance Primitives Gaussian Mixture Sample for Windows*
//
// By downloading and installing this sample, you hereby agree that the
// accompanying Materials are being provided to you under the terms and
// conditions of the End User License Agreement for the Intel(R) Integrated
// Performance Primitives product previously accepted by you. Please refer
// to the file ippEULA.rtf located in the root directory of your Intel(R) IPP
// product installation for more information.
//
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ipps.h>
#include <ippsr.h>
#include "fake_decoder.h"
#include "prob_cache.h"
#define MAX_RAND (0x7fff)
Fake_Decoder::Fake_Decoder(int N, int L) {
state_number = N;
avr_active = N/5;
max_live = L;
live_len = 256;
frame_num = 256+max_live;
shift = 0;
all_prob = 0;
rept_prob = 0;
prob_factor = 0;
t = 0;
sum = 0.0;
sumsq = 0.0;
cache = NULL;
last_act = NULL;
state_weight = NULL;
last_len = NULL;
act_num = NULL;
sum_inact = NULL;
sumsq_inact = NULL;
live_stat = NULL;
if_calc = NULL;
flog = NULL;
ready = -2;
}
int Fake_Decoder::Init_LogFile(char *prob_log) {
if (ready!=0) return -10;
if (!prob_log) {
flog=NULL;
} else if (!prob_log[0]) {
flog=NULL;
} else {
if (!(flog=fopen(prob_log, "wb"))) return -3;
}
return 0;
}
int Fake_Decoder::Init_Decoder(void) {
if (ready!=-2) return -10;
if (state_number<=0) return -1;
if (avr_active<=0) return -1;
if (max_live<=0) return -1;
if (frame_num<=0) return -1;
if (avr_active>state_number)
avr_active=state_number;
if (!(last_act = ippsMalloc_32s(state_number))) return -2;
if (!(state_weight = ippsMalloc_32f(state_number))) return -2;
if (!(last_len = ippsMalloc_32s(state_number))) return -2;
if (!(if_calc = ippsMalloc_8u(state_number))) return -2;
if (!(act_num = ippsMalloc_32s(frame_num+max_live))) return -2;
if (!(sum_inact = ippsMalloc_64f(frame_num+max_live))) return -2;
if (!(sumsq_inact = ippsMalloc_64f(frame_num+max_live))) return -2;
if (!(live_stat = ippsMalloc_32s(live_len))) return -2;
int i;
int *q=(int*)state_weight;
sum=0.0;
for (i=0; i<state_number; i++) {
q[i]=rand()&MAX_RAND;
sum+=(double)q[i];
}
sum=1.0/sum;
for (i=0; i<state_number; i++) {
state_weight[i]=(float)(q[i]*sum);
}
sum=0.0;
sumsq=0.0;
for (i=0; i<state_number; i++) {
sum+=state_weight[i];
sumsq+=state_weight[i]*state_weight[i];
}
ippsSet_32s(-1,last_act,state_number);
ippsZero_32f((float*)last_len,state_number);
ippsSet_64f(sum,sum_inact,frame_num);
ippsSet_64f(sumsq,sumsq_inact,frame_num);
ippsZero_32f((float*)act_num,frame_num);
ippsZero_32f((float*)live_stat,live_len);
ippsZero_8u(if_calc,state_number);
rept_prob=0;
all_prob=0;
t=0;
ready=-1;
return 0;
}
Fake_Decoder::~Fake_Decoder(void) {
state_number = 0;
avr_active = 0;
max_live = 0;
live_len = 0;
frame_num = 0;
shift = 0;
all_prob = 0;
rept_prob = 0;
prob_factor = 0;
t = 0;
sum = 0.0;
sumsq = 0.0;
if (live_stat) ippsFree(live_stat);
if (sumsq_inact) ippsFree(sumsq_inact);
if (sum_inact) ippsFree(sum_inact);
if (act_num) ippsFree(act_num);
if (last_len) ippsFree(last_len);
if (state_weight) ippsFree(state_weight);
if (last_act) ippsFree(last_act);
if (flog) fclose(flog);
cache = NULL;
last_act = NULL;
state_weight = NULL;
last_len = NULL;
act_num = NULL;
sum_inact = NULL;
sumsq_inact = NULL;
live_stat = NULL;
if_calc = NULL;
flog = NULL;
ready = -2;
}
void Fake_Decoder::Get_Statistics(Len_Stat *stat) {
// all - all probabilities calculated
// lost - repeated probabilities
// nums[i] - number of intervals of length i+1
ippsCopy_32f((float*)live_stat,(float*)(stat->nums),stat->len);
stat->lost=rept_prob;
stat->all=all_prob;
}
bool Fake_Decoder::Attach_Cache(Prob_Cache *c) {
if (ready!=-1) return false;
cache=c;
if (!cache) return false;
if (!cache->Ready()) return false;
if (cache->State_Number()<state_number) return false;
int fc=cache->Get_Scale();
if (abs(fc)>29) return false;
if (fc>0) prob_factor=1.0f/(float)(1<<fc);
else prob_factor=(float)(1<<-fc);
ready=0;
return true;
}
int Fake_Decoder::Activity_Interval(int max_len) {
int l;
float p=(float)(rand()&MAX_RAND)/(float)MAX_RAND;
l=(int)(p*max_len+1.0);
if (l<1) l=1;
return l;
}
int Fake_Decoder::Get_Probability(int state, int *prob) {
rept_prob+=if_calc[state];
if_calc[state]=1;
return cache->Get_Probability(state,prob);
}
int Fake_Decoder::Use_Probability(int state, int *prob) {
if (flog) {
float pr=(float)(prob[0]*prob_factor);
fwrite((void*)&pr, sizeof(int), 1, flog);
}
*prob=(int)exp(log(*prob+1.0));
return 0;
}
void Fake_Decoder::Close_Decoder(void) {
int i;
for (i=0; i<state_number; i++) {
if (last_len[i]>=live_len) last_len[i]=live_len;
if (last_len[i]) live_stat[last_len[i]-1]++;
last_len[i]=0;
}
}
bool Fake_Decoder::Decode_Frame(int M) {
// tends to M different states calculated once
int i,j,MM,live;
float prand,plim,act_prob;
int prob;
float pp=(float)(1.0/sum_inact[t-shift]);
bool all=false;
if (ready!=0) return false;
if (M>0) avr_active=M;
if (avr_active>state_number) avr_active=state_number;
if (avr_active<=act_num[t-shift]) {
for (i=0; i<state_number; i++) {
if (last_act[i]>=t) {
if (Get_Probability(i,&prob))
Use_Probability(i,&prob);
}
}
} else {
if ((sum_inact[t-shift]<=0)||(avr_active>=state_number)) {
MM=avr_active-act_num[t-shift];
all=true;
} else {
MM=(int)((double)(avr_active-act_num[t-shift])/
(1.0-(avr_active-act_num[t-shift])*sumsq_inact[t-shift]*pp*pp*0.5));
MM=(int)((double)(avr_active-act_num[t-shift])/(1.0-MM*sumsq_inact[t-shift]*pp*pp*0.5));
all=false;
}
for (i=0; i<state_number; i++) {
if (last_act[i]>=t) {
if (Get_Probability(i,&prob))
Use_Probability(i,&prob);
} else {
if (all) act_prob=1.0f;
else act_prob=(float)(1.0-exp(MM*log(1.0-state_weight[i]*pp)));
prand=(float)(rand()&MAX_RAND);
plim=(float)(act_prob*MAX_RAND);
if (prand<=plim) {
if (Get_Probability(i,&prob))
Use_Probability(i,&prob);
live=Activity_Interval(max_live);
last_act[i]=t+live;
last_len[i]+=live;
for (j=t; j<=last_act[i]; j++) {
act_num[j-shift]++;
sum_inact[j-shift]-=state_weight[i];
sumsq_inact[j-shift]-=state_weight[i]*state_weight[i];
}
} else {
if (last_len[i]) {
if (last_len[i]>=live_len) last_len[i]=live_len;
live_stat[last_len[i]-1]++;
}
last_len[i]=0;
}
}
}
}
ippsZero_8u(if_calc,state_number);
all_prob+=act_num[t-shift];
if (++t>=frame_num-max_live+shift) {
shift+=frame_num-max_live;
ippsCopy_32f((float*)act_num+frame_num-max_live,(float*)act_num,max_live);
ippsZero_32f((float*)act_num+max_live,frame_num-max_live);
ippsCopy_64f(sum_inact+frame_num-max_live,sum_inact,max_live);
ippsSet_64f(sum,sum_inact+max_live,frame_num-max_live);
ippsCopy_64f(sumsq_inact+frame_num-max_live,sumsq_inact,max_live);
ippsSet_64f(sumsq,sumsq_inact+max_live,frame_num-max_live);
}
return cache->Step_Forward();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -