-
大小: 4KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-06-05
- 语言: 其他
- 标签:
资源简介
Arithmetic Coding Library
This package was adapted from the program in "Arithmetic Coding for Data Compression"
代码片段和文件信息
#include
#include
#include “ac.h“
#define Code_value_bits 16
#define Top_value (((long)1< #define First_qtr (Top_value/4+1)
#define Half (2*First_qtr)
#define Third_qtr (3*First_qtr)
#define Max_frequency 16383
static void output_bit (ac_encoder * int);
static void bit_plus_follow (ac_encoder * int);
static int input_bit (ac_decoder *);
static void update_model (ac_model * int);
#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(bm) \
do { \
if (b) \
error (m); \
} while (0)
static void
output_bit (ac_encoder *ace int bit)
{
ace->buffer >>= 1;
if (bit)
ace->buffer |= 0x80;
ace->bits_to_go -= 1;
ace->total_bits += 1;
if (ace->bits_to_go==0) {
if (ace->fp)
putc (ace->buffer ace->fp);
ace->bits_to_go = 8;
}
return;
}
static void
bit_plus_follow (ac_encoder *ace int bit)
{
output_bit (ace bit);
while (ace->fbits > 0) {
output_bit (ace !bit);
ace->fbits -= 1;
}
return;
}
static int
input_bit (ac_decoder *acd)
{
int t;
if (acd->bits_to_go==0) {
acd->buffer = getc(acd->fp);
if (acd->buffer==EOF) {
acd->garbage_bits += 1;
if (acd->garbage_bits>Code_value_bits-2)
error (“arithmetic decoder bad input file“);
}
acd->bits_to_go = 8;
}
t = acd->buffer&1;
acd->buffer >>= 1;
acd->bits_to_go -= 1;
return t;
}
static void
update_model (ac_model *acm int sym)
{
int i;
if (acm->cfreq[0]==Max_frequency) {
int cum = 0;
acm->cfreq[acm->nsym] = 0;
for (i = acm->nsym-1; i>=0; i--) {
acm->freq[i] = (acm->freq[i] + 1) / 2;
cum += acm->freq[i];
acm->cfreq[i] = cum;
}
}
acm->freq[sym] += 1;
for (i=sym; i>=0; i--)
acm->cfreq[i] += 1;
return;
}
void
ac_encoder_init (ac_encoder *ace const char *fn)
{
if (fn) {
ace->fp = fopen (fn “wb“); /* open in binary mode */
check (!ace->fp “arithmetic encoder could not open file“);
} else {
ace->fp = NULL;
}
ace->bits_to_go = 8;
ace->low = 0;
ace->high = Top_value;
ace->fbits = 0;
ace->buffer = 0;
ace->total_bits = 0;
return;
}
void
ac_encoder_done (ac_encoder *ace)
{
ace->fbits += 1;
if (ace->low < First_qtr)
bit_plus_follow (ace 0);
else
bit_plus_follow (ace 1);
if (ace->fp)
putc (ace->buffer >> ace->bits_to_go ace->fp);
if (ace->fp)
fclose (ace->fp);
r
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2528 2007-03-18 21:34 Adaptive arithmetic code\acdemo.c
文件 2615 2007-03-18 21:34 Adaptive arithmetic code\ac.txt
文件 816 2007-03-18 21:34 Adaptive arithmetic code\ac.h
文件 6306 2007-03-18 21:34 Adaptive arithmetic code\ac.c
目录 0 2007-03-18 21:35 Adaptive arithmetic code
----------- --------- ---------- ----- ----
12265 5
- 上一篇:sdio 3.0 spec
- 下一篇:单片机C51-键盘 定时扫描
评论
共有 条评论