siren: Run gst-indent script

Includes running dos2unix on decoder.c
This commit is contained in:
Benjamin Otte 2010-03-21 21:37:37 +01:00
parent 692471aae7
commit 46f4c7a6c1
6 changed files with 597 additions and 453 deletions

View file

@ -28,10 +28,10 @@ float standard_deviation[64];
float deviation_inverse[64];
float region_power_table_boundary[63];
int expected_bits_table[8] = {52, 47, 43, 37, 29, 22, 16, 0};
int vector_dimension[8] = {2, 2, 2, 4, 4, 5, 5, 1};
int number_of_vectors[8] = {10, 10, 10, 5, 5, 4, 4, 20};
float dead_zone[8] = {0.3f, 0.33f, 0.36f, 0.39f, 0.42f, 0.45f, 0.5f, 0.5f};
int expected_bits_table[8] = { 52, 47, 43, 37, 29, 22, 16, 0 };
int vector_dimension[8] = { 2, 2, 2, 4, 4, 5, 5, 1 };
int number_of_vectors[8] = { 10, 10, 10, 5, 5, 4, 4, 20 };
float dead_zone[8] = { 0.3f, 0.33f, 0.36f, 0.39f, 0.42f, 0.45f, 0.5f, 0.5f };
int max_bin[8] = {
13,
@ -41,7 +41,8 @@ int max_bin[8] = {
3,
2,
1,
1};
1
};
float step_size[8] = {
0.3536f,
@ -51,7 +52,8 @@ float step_size[8] = {
1.4141999f,
2.0f,
2.8283999f,
2.8283999f};
2.8283999f
};
float step_size_inverse[8];
@ -62,7 +64,9 @@ static int siren_initialized = 0;
*/
#define STEPSIZE 0.3010299957
void siren_init() {
void
siren_init ()
{
int i;
float region_power;
@ -70,36 +74,39 @@ void siren_init() {
return;
region_size = 20;
region_size_inverse = 1.0f/region_size;
region_size_inverse = 1.0f / region_size;
for (i = 0; i < 64; i++) {
region_power = (float) pow(10, (i-24) * STEPSIZE);
standard_deviation[i] = (float) sqrt(region_power);
region_power = (float) pow (10, (i - 24) * STEPSIZE);
standard_deviation[i] = (float) sqrt (region_power);
deviation_inverse[i] = (float) 1.0 / standard_deviation[i];
}
for (i = 0; i < 63; i++)
region_power_table_boundary[i] = (float) pow(10, (i-24 + 0.5) * STEPSIZE);
region_power_table_boundary[i] =
(float) pow (10, (i - 24 + 0.5) * STEPSIZE);
for (i = 0; i < 8; i++)
step_size_inverse[i] = (float) 1.0 / step_size[i];
siren_dct4_init();
siren_rmlt_init();
siren_dct4_init ();
siren_rmlt_init ();
siren_initialized = 1;
}
int categorize_regions(int number_of_regions, int number_of_available_bits, int *absolute_region_power_index, int *power_categories, int *category_balance) {
int
categorize_regions (int number_of_regions, int number_of_available_bits,
int *absolute_region_power_index, int *power_categories,
int *category_balance)
{
int region, delta, i, temp;
int expected_number_of_code_bits;
int min, max;
int offset,
num_rate_control_possibilities,
raw_value,
raw_max_idx = 0,
raw_min_idx = 0;
raw_value, raw_max_idx = 0, raw_min_idx = 0;
int max_rate_categories[28];
int min_rate_categories[28];
int temp_category_balances[64];
@ -108,12 +115,14 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
if (number_of_regions == 14) {
num_rate_control_possibilities = 16;
if ( number_of_available_bits > 320)
number_of_available_bits = ((number_of_available_bits - 320) * 5/8) + 320;
if (number_of_available_bits > 320)
number_of_available_bits =
((number_of_available_bits - 320) * 5 / 8) + 320;
} else {
num_rate_control_possibilities = 32;
if (number_of_regions == 28 && number_of_available_bits > 640)
number_of_available_bits = ((number_of_available_bits - 640) * 5/8) + 640;
if (number_of_regions == 28 && number_of_available_bits > 640)
number_of_available_bits =
((number_of_available_bits - 640) * 5 / 8) + 640;
}
offset = -32;
@ -130,30 +139,34 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
expected_number_of_code_bits += expected_bits_table[i];
}
if (expected_number_of_code_bits >= number_of_available_bits-32)
if (expected_number_of_code_bits >= number_of_available_bits - 32)
offset += delta;
}
expected_number_of_code_bits = 0;
for (region = 0; region < number_of_regions; region++) {
for (region = 0; region < number_of_regions; region++) {
i = (offset - absolute_region_power_index[region]) >> 1;
if (i > 7)
i = 7;
else if (i < 0)
i = 0;
max_rate_categories[region] = min_rate_categories[region] = power_categories[region] = i;
max_rate_categories[region] = min_rate_categories[region] =
power_categories[region] = i;
expected_number_of_code_bits += expected_bits_table[i];
}
min = max = expected_number_of_code_bits;
min_rate_ptr = max_rate_ptr = temp_category_balances + num_rate_control_possibilities;
for (i = 0; i < num_rate_control_possibilities -1; i++) {
min_rate_ptr = max_rate_ptr =
temp_category_balances + num_rate_control_possibilities;
for (i = 0; i < num_rate_control_possibilities - 1; i++) {
if (min + max > number_of_available_bits * 2) {
raw_value = -99;
for (region = number_of_regions-1; region >= 0; region--) {
for (region = number_of_regions - 1; region >= 0; region--) {
if (min_rate_categories[region] < 7) {
temp = offset - absolute_region_power_index[region] - 2*min_rate_categories[region];
temp =
offset - absolute_region_power_index[region] -
2 * min_rate_categories[region];
if (temp > raw_value) {
raw_value = temp;
raw_min_idx = region;
@ -161,13 +174,17 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
}
}
*min_rate_ptr++ = raw_min_idx;
min += expected_bits_table[min_rate_categories[raw_min_idx] + 1] - expected_bits_table[min_rate_categories[raw_min_idx]];
min +=
expected_bits_table[min_rate_categories[raw_min_idx] + 1] -
expected_bits_table[min_rate_categories[raw_min_idx]];
min_rate_categories[raw_min_idx]++;
} else {
raw_value = 99;
for (region = 0; region < number_of_regions; region++) {
if (max_rate_categories[region] > 0 ) {
temp = offset - absolute_region_power_index[region] - 2*max_rate_categories[region];
if (max_rate_categories[region] > 0) {
temp =
offset - absolute_region_power_index[region] -
2 * max_rate_categories[region];
if (temp < raw_value) {
raw_value = temp;
raw_max_idx = region;
@ -176,7 +193,9 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
}
*--max_rate_ptr = raw_max_idx;
max += expected_bits_table[max_rate_categories[raw_max_idx] - 1] - expected_bits_table[max_rate_categories[raw_max_idx]];
max +=
expected_bits_table[max_rate_categories[raw_max_idx] - 1] -
expected_bits_table[max_rate_categories[raw_max_idx]];
max_rate_categories[raw_max_idx]--;
}
}
@ -184,7 +203,7 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
for (region = 0; region < number_of_regions; region++)
power_categories[region] = max_rate_categories[region];
for (i = 0; i < num_rate_control_possibilities-1; i++)
for (i = 0; i < num_rate_control_possibilities - 1; i++)
category_balance[i] = *max_rate_ptr++;
@ -201,7 +220,13 @@ int categorize_regions(int number_of_regions, int number_of_available_bits, int
3 : sample rate is variable and there is one for each frame
*/
int GetSirenCodecInfo(int flag, int sample_rate, int *number_of_coefs, int *sample_rate_bits, int *rate_control_bits, int *rate_control_possibilities, int *checksum_bits, int *esf_adjustment, int *scale_factor, int *number_of_regions, int *sample_rate_code, int *bits_per_frame ) {
int
GetSirenCodecInfo (int flag, int sample_rate, int *number_of_coefs,
int *sample_rate_bits, int *rate_control_bits,
int *rate_control_possibilities, int *checksum_bits, int *esf_adjustment,
int *scale_factor, int *number_of_regions, int *sample_rate_code,
int *bits_per_frame)
{
switch (flag) {
case 0:
*number_of_coefs = 320;
@ -499,7 +524,6 @@ int GetSirenCodecInfo(int flag, int sample_rate, int *number_of_coefs, int *samp
return 6;
}
*bits_per_frame = sample_rate / 50;
*bits_per_frame = sample_rate / 50;
return 0;
}

View file

@ -25,7 +25,8 @@
#define PI 3.1415926
typedef struct {
typedef struct
{
float cos;
float msin;
} dct_table_type;
@ -40,39 +41,44 @@ static dct_table_type dct_table_80[80];
static dct_table_type dct_table_160[160];
static dct_table_type dct_table_320[320];
static dct_table_type dct_table_640[640];
static dct_table_type *dct_tables[8] = {dct_table_5,
dct_table_10,
dct_table_20,
dct_table_40,
dct_table_80,
dct_table_160,
dct_table_320,
dct_table_640};
static dct_table_type *dct_tables[8] = { dct_table_5,
dct_table_10,
dct_table_20,
dct_table_40,
dct_table_80,
dct_table_160,
dct_table_320,
dct_table_640
};
static int dct4_initialized = 0;
void siren_dct4_init() {
void
siren_dct4_init ()
{
int i, j = 0;
double scale_320 = (float) sqrt(2.0/320);
double scale_640 = (float) sqrt(2.0/640);
double scale_320 = (float) sqrt (2.0 / 320);
double scale_640 = (float) sqrt (2.0 / 640);
double angle;
double scale;
/* set up dct4 tables */
for(i = 0; i < 10; i++) {
angle = (float) ((i + 0.5) * PI);
for (j = 0 ; j < 10; j++) {
dct_core_320[(i*10)+j] = (float) (scale_320 * cos((j + 0.5) * angle / 10));
dct_core_640[(i*10)+j] = (float) (scale_640 * cos((j + 0.5) * angle / 10));
for (i = 0; i < 10; i++) {
angle = (float) ((i + 0.5) * PI);
for (j = 0; j < 10; j++) {
dct_core_320[(i * 10) + j] =
(float) (scale_320 * cos ((j + 0.5) * angle / 10));
dct_core_640[(i * 10) + j] =
(float) (scale_640 * cos ((j + 0.5) * angle / 10));
}
}
for(i = 0; i < 8; i++) {
for (i = 0; i < 8; i++) {
scale = (float) (PI / ((5 << i) * 4));
for (j = 0 ; j < (5 << i); j++) {
for (j = 0; j < (5 << i); j++) {
angle = (float) (j + 0.5) * scale;
dct_tables[i][j].cos = (float) cos(angle);
dct_tables[i][j].msin = (float) -sin(angle);
dct_tables[i][j].cos = (float) cos (angle);
dct_tables[i][j].msin = (float) -sin (angle);
}
}
@ -80,11 +86,13 @@ void siren_dct4_init() {
}
void siren_dct4(float *Source, float *Destination, int dct_length) {
void
siren_dct4 (float *Source, float *Destination, int dct_length)
{
int log_length = 0;
float * dct_core = NULL;
dct_table_type ** dct_table_ptr_ptr = NULL;
dct_table_type * dct_table_ptr = NULL;
float *dct_core = NULL;
dct_table_type **dct_table_ptr_ptr = NULL;
dct_table_type *dct_table_ptr = NULL;
float OutBuffer1[640];
float OutBuffer2[640];
float *Out_ptr;
@ -97,10 +105,10 @@ void siren_dct4(float *Source, float *Destination, int dct_length) {
float *Out_ptr_low = NULL;
float *Out_ptr_high = NULL;
float mult1, mult2, mult3, mult4, mult5, mult6, mult7, mult8, mult9, mult10;
int i,j;
int i, j;
if (dct4_initialized == 0)
siren_dct4_init();
siren_dct4_init ();
if (dct_length == 640) {
log_length = 5;
@ -116,7 +124,7 @@ void siren_dct4(float *Source, float *Destination, int dct_length) {
for (i = 0; i <= log_length; i++) {
for (j = 0; j < (1 << i); j++) {
Out_ptr_low = Out_ptr + (j * (dct_length >> i));
Out_ptr_high = Out_ptr + ( (j+1) * (dct_length >> i));
Out_ptr_high = Out_ptr + ((j + 1) * (dct_length >> i));
do {
In_val_low = *In_Ptr++;
In_val_high = *In_Ptr++;
@ -131,20 +139,19 @@ void siren_dct4(float *Source, float *Destination, int dct_length) {
}
for (i = 0; i < (2 << log_length); i++) {
for (j = 0 ; j < 10 ; j ++) {
mult1 = In_Ptr[(i*10)] * dct_core[j*10];
mult2 = In_Ptr[(i*10) + 1] * dct_core[(j*10) + 1];
mult3 = In_Ptr[(i*10) + 2] * dct_core[(j*10) + 2];
mult4 = In_Ptr[(i*10) + 3] * dct_core[(j*10) + 3];
mult5 = In_Ptr[(i*10) + 4] * dct_core[(j*10) + 4];
mult6 = In_Ptr[(i*10) + 5] * dct_core[(j*10) + 5];
mult7 = In_Ptr[(i*10) + 6] * dct_core[(j*10) + 6];
mult8 = In_Ptr[(i*10) + 7] * dct_core[(j*10) + 7];
mult9 = In_Ptr[(i*10) + 8] * dct_core[(j*10) + 8];
mult10 = In_Ptr[(i*10) + 9] * dct_core[(j*10) + 9];
Out_ptr[(i*10)+j] = mult1 + mult2 + mult3 + mult4 +
mult5 + mult6 + mult7 + mult8 +
mult9 + mult10;
for (j = 0; j < 10; j++) {
mult1 = In_Ptr[(i * 10)] * dct_core[j * 10];
mult2 = In_Ptr[(i * 10) + 1] * dct_core[(j * 10) + 1];
mult3 = In_Ptr[(i * 10) + 2] * dct_core[(j * 10) + 2];
mult4 = In_Ptr[(i * 10) + 3] * dct_core[(j * 10) + 3];
mult5 = In_Ptr[(i * 10) + 4] * dct_core[(j * 10) + 4];
mult6 = In_Ptr[(i * 10) + 5] * dct_core[(j * 10) + 5];
mult7 = In_Ptr[(i * 10) + 6] * dct_core[(j * 10) + 6];
mult8 = In_Ptr[(i * 10) + 7] * dct_core[(j * 10) + 7];
mult9 = In_Ptr[(i * 10) + 8] * dct_core[(j * 10) + 8];
mult10 = In_Ptr[(i * 10) + 9] * dct_core[(j * 10) + 9];
Out_ptr[(i * 10) + j] = mult1 + mult2 + mult3 + mult4 +
mult5 + mult6 + mult7 + mult8 + mult9 + mult10;
}
}
@ -157,7 +164,7 @@ void siren_dct4(float *Source, float *Destination, int dct_length) {
dct_table_ptr_ptr++;
for (j = 0; j < (1 << i); j++) {
dct_table_ptr = *dct_table_ptr_ptr;
if ( i == 0 )
if (i == 0)
Out_ptr_low = Destination + (j * (dct_length >> i));
else
Out_ptr_low = Out_ptr + (j * (dct_length >> i));
@ -165,13 +172,21 @@ void siren_dct4(float *Source, float *Destination, int dct_length) {
Out_ptr_high = Out_ptr_low + (dct_length >> i);
In_Ptr_low = In_Ptr + (j * (dct_length >> i));
In_Ptr_high = In_Ptr_low + (dct_length >> (i+1));
In_Ptr_high = In_Ptr_low + (dct_length >> (i + 1));
do {
*Out_ptr_low++ = (*In_Ptr_low * (*dct_table_ptr).cos) - (*In_Ptr_high * (*dct_table_ptr).msin);
*--Out_ptr_high = (*In_Ptr_high++ * (*dct_table_ptr).cos) + (*In_Ptr_low++ * (*dct_table_ptr).msin);
*Out_ptr_low++ =
(*In_Ptr_low * (*dct_table_ptr).cos) -
(*In_Ptr_high * (*dct_table_ptr).msin);
*--Out_ptr_high =
(*In_Ptr_high++ * (*dct_table_ptr).cos) +
(*In_Ptr_low++ * (*dct_table_ptr).msin);
dct_table_ptr++;
*Out_ptr_low++ = (*In_Ptr_low * (*dct_table_ptr).cos) + (*In_Ptr_high * (*dct_table_ptr).msin);
*--Out_ptr_high = (*In_Ptr_low++ * (*dct_table_ptr).msin) - (*In_Ptr_high++ * (*dct_table_ptr).cos);
*Out_ptr_low++ =
(*In_Ptr_low * (*dct_table_ptr).cos) +
(*In_Ptr_high * (*dct_table_ptr).msin);
*--Out_ptr_high =
(*In_Ptr_low++ * (*dct_table_ptr).msin) -
(*In_Ptr_high++ * (*dct_table_ptr).cos);
dct_table_ptr++;
} while (Out_ptr_low < Out_ptr_high);
}

View file

@ -1,234 +1,253 @@
/*
* Siren Encoder/Decoder library
*
* @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "siren7.h"
SirenDecoder Siren7_NewDecoder(int sample_rate) {
SirenDecoder decoder = (SirenDecoder) malloc(sizeof(struct stSirenDecoder));
decoder->sample_rate = sample_rate;
decoder->WavHeader.riff.RiffId = ME_TO_LE32(RIFF_ID);
decoder->WavHeader.riff.RiffSize = sizeof(PCMWavHeader) - 2*sizeof(int);
decoder->WavHeader.riff.RiffSize = ME_TO_LE32(decoder->WavHeader.riff.RiffSize);
decoder->WavHeader.WaveId = ME_TO_LE32(WAVE_ID);
decoder->WavHeader.FmtId = ME_TO_LE32(FMT__ID);
decoder->WavHeader.FmtSize = ME_TO_LE32(sizeof(FmtChunk));
decoder->WavHeader.fmt.Format = ME_TO_LE16(0x01);
decoder->WavHeader.fmt.Channels = ME_TO_LE16(1);
decoder->WavHeader.fmt.SampleRate = ME_TO_LE32(16000);
decoder->WavHeader.fmt.ByteRate = ME_TO_LE32(32000);
decoder->WavHeader.fmt.BlockAlign = ME_TO_LE16(2);
decoder->WavHeader.fmt.BitsPerSample = ME_TO_LE16(16);
decoder->WavHeader.FactId = ME_TO_LE32(FACT_ID);
decoder->WavHeader.FactSize = ME_TO_LE32(sizeof(int));
decoder->WavHeader.Samples = ME_TO_LE32(0);
decoder->WavHeader.DataId = ME_TO_LE32(DATA_ID);
decoder->WavHeader.DataSize = ME_TO_LE32(0);
memset(decoder->context, 0, sizeof(decoder->context));
memset(decoder->backup_frame, 0, sizeof(decoder->backup_frame));
decoder->dw1 = 1;
decoder->dw2 = 1;
decoder->dw3 = 1;
decoder->dw4 = 1;
siren_init();
return decoder;
}
void Siren7_CloseDecoder(SirenDecoder decoder) {
free(decoder);
}
int Siren7_DecodeFrame(SirenDecoder decoder, unsigned char *DataIn, unsigned char *DataOut) {
int number_of_coefs,
sample_rate_bits,
rate_control_bits,
rate_control_possibilities,
checksum_bits,
esf_adjustment,
scale_factor,
number_of_regions,
sample_rate_code,
bits_per_frame;
int decoded_sample_rate_code;
static int absolute_region_power_index[28] = {0};
static float decoder_standard_deviation[28] = {0};
static int power_categories[28] = {0};
static int category_balance[28] = {0};
int ChecksumTable[4] = {0x7F80, 0x7878, 0x6666, 0x5555};
int i, j;
int dwRes = 0;
int envelope_bits = 0;
int rate_control = 0;
int number_of_available_bits;
int number_of_valid_coefs;
int frame_error = 0;
int In[20];
float coefs[320];
float BufferOut[320];
int sum;
int checksum;
int calculated_checksum;
int idx;
int temp1;
int temp2;
for (i = 0; i < 20; i++)
#ifdef __BIG_ENDIAN__
In[i] = ((short *) DataIn)[i];
#else
In[i] = ((((short *) DataIn)[i] << 8) & 0xFF00) | ((((short *) DataIn)[i] >> 8) & 0x00FF);
#endif
dwRes = GetSirenCodecInfo(1, decoder->sample_rate, &number_of_coefs, &sample_rate_bits, &rate_control_bits, &rate_control_possibilities, &checksum_bits, &esf_adjustment, &scale_factor, &number_of_regions, &sample_rate_code, &bits_per_frame );
if (dwRes != 0)
return dwRes;
set_bitstream(In);
decoded_sample_rate_code = 0;
for (i = 0; i < sample_rate_bits; i++) {
decoded_sample_rate_code <<= 1;
decoded_sample_rate_code |= next_bit();
}
if (decoded_sample_rate_code != sample_rate_code)
return 7;
number_of_valid_coefs = region_size * number_of_regions;
number_of_available_bits = bits_per_frame - sample_rate_bits - checksum_bits ;
envelope_bits = decode_envelope(number_of_regions, decoder_standard_deviation, absolute_region_power_index, esf_adjustment);
number_of_available_bits -= envelope_bits;
for (i = 0; i < rate_control_bits; i++) {
rate_control <<= 1;
rate_control |= next_bit();
}
number_of_available_bits -= rate_control_bits;
categorize_regions(number_of_regions, number_of_available_bits, absolute_region_power_index, power_categories, category_balance);
for (i = 0; i < rate_control; i++) {
power_categories[category_balance[i]]++;
}
number_of_available_bits = decode_vector(decoder, number_of_regions, number_of_available_bits, decoder_standard_deviation, power_categories, coefs, scale_factor);
frame_error = 0;
if (number_of_available_bits > 0) {
for (i = 0; i < number_of_available_bits; i++) {
if (next_bit() == 0)
frame_error = 1;
}
} else if (number_of_available_bits < 0 && rate_control + 1 < rate_control_possibilities) {
frame_error |= 2;
}
for (i = 0; i < number_of_regions; i++) {
if (absolute_region_power_index[i] > 33 || absolute_region_power_index[i] < -31)
frame_error |= 4;
}
if (checksum_bits > 0) {
bits_per_frame >>= 4;
checksum = In[bits_per_frame - 1] & ((1 << checksum_bits) - 1);
In[bits_per_frame - 1] &= ~checksum;
sum = 0;
idx = 0;
do {
sum ^= (In[idx] & 0xFFFF) << (idx % 15);
} while (++idx < bits_per_frame);
sum = (sum >> 15) ^ (sum & 0x7FFF);
calculated_checksum = 0;
for (i = 0; i < 4; i++) {
temp1 = ChecksumTable[i] & sum;
for (j = 8; j > 0; j >>= 1) {
temp2 = temp1 >> j;
temp1 ^= temp2;
}
calculated_checksum <<= 1;
calculated_checksum |= temp1 & 1;
}
if (checksum != calculated_checksum)
frame_error |= 8;
}
if (frame_error != 0) {
for (i = 0; i < number_of_valid_coefs; i++) {
coefs[i] = decoder->backup_frame[i];
decoder->backup_frame[i] = 0;
}
} else {
for (i = 0; i < number_of_valid_coefs; i++)
decoder->backup_frame[i] = coefs[i];
}
for (i = number_of_valid_coefs; i < number_of_coefs; i++)
coefs[i] = 0;
dwRes = siren_rmlt_decode_samples(coefs, decoder->context, 320, BufferOut);
for (i = 0; i < 320; i++) {
if (BufferOut[i] > 32767.0)
((short *)DataOut)[i] = (short) ME_TO_LE16((short) 32767);
else if (BufferOut[i] <= -32768.0)
((short *)DataOut)[i] = (short) ME_TO_LE16((short) 32768);
else
((short *)DataOut)[i] = (short) ME_TO_LE16((short) BufferOut[i]);
}
decoder->WavHeader.Samples = ME_FROM_LE32(decoder->WavHeader.Samples);
decoder->WavHeader.Samples += 320;
decoder->WavHeader.Samples = ME_TO_LE32(decoder->WavHeader.Samples);
decoder->WavHeader.DataSize = ME_FROM_LE32(decoder->WavHeader.DataSize);
decoder->WavHeader.DataSize += 640;
decoder->WavHeader.DataSize = ME_TO_LE32(decoder->WavHeader.DataSize);
decoder->WavHeader.riff.RiffSize = ME_FROM_LE32(decoder->WavHeader.riff.RiffSize);
decoder->WavHeader.riff.RiffSize += 640;
decoder->WavHeader.riff.RiffSize = ME_TO_LE32(decoder->WavHeader.riff.RiffSize);
return 0;
}
/*
* Siren Encoder/Decoder library
*
* @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "siren7.h"
SirenDecoder
Siren7_NewDecoder (int sample_rate)
{
SirenDecoder decoder = (SirenDecoder) malloc (sizeof (struct stSirenDecoder));
decoder->sample_rate = sample_rate;
decoder->WavHeader.riff.RiffId = ME_TO_LE32 (RIFF_ID);
decoder->WavHeader.riff.RiffSize = sizeof (PCMWavHeader) - 2 * sizeof (int);
decoder->WavHeader.riff.RiffSize =
ME_TO_LE32 (decoder->WavHeader.riff.RiffSize);
decoder->WavHeader.WaveId = ME_TO_LE32 (WAVE_ID);
decoder->WavHeader.FmtId = ME_TO_LE32 (FMT__ID);
decoder->WavHeader.FmtSize = ME_TO_LE32 (sizeof (FmtChunk));
decoder->WavHeader.fmt.Format = ME_TO_LE16 (0x01);
decoder->WavHeader.fmt.Channels = ME_TO_LE16 (1);
decoder->WavHeader.fmt.SampleRate = ME_TO_LE32 (16000);
decoder->WavHeader.fmt.ByteRate = ME_TO_LE32 (32000);
decoder->WavHeader.fmt.BlockAlign = ME_TO_LE16 (2);
decoder->WavHeader.fmt.BitsPerSample = ME_TO_LE16 (16);
decoder->WavHeader.FactId = ME_TO_LE32 (FACT_ID);
decoder->WavHeader.FactSize = ME_TO_LE32 (sizeof (int));
decoder->WavHeader.Samples = ME_TO_LE32 (0);
decoder->WavHeader.DataId = ME_TO_LE32 (DATA_ID);
decoder->WavHeader.DataSize = ME_TO_LE32 (0);
memset (decoder->context, 0, sizeof (decoder->context));
memset (decoder->backup_frame, 0, sizeof (decoder->backup_frame));
decoder->dw1 = 1;
decoder->dw2 = 1;
decoder->dw3 = 1;
decoder->dw4 = 1;
siren_init ();
return decoder;
}
void
Siren7_CloseDecoder (SirenDecoder decoder)
{
free (decoder);
}
int
Siren7_DecodeFrame (SirenDecoder decoder, unsigned char *DataIn,
unsigned char *DataOut)
{
int number_of_coefs,
sample_rate_bits,
rate_control_bits,
rate_control_possibilities,
checksum_bits,
esf_adjustment,
scale_factor, number_of_regions, sample_rate_code, bits_per_frame;
int decoded_sample_rate_code;
static int absolute_region_power_index[28] = { 0 };
static float decoder_standard_deviation[28] = { 0 };
static int power_categories[28] = { 0 };
static int category_balance[28] = { 0 };
int ChecksumTable[4] = { 0x7F80, 0x7878, 0x6666, 0x5555 };
int i, j;
int dwRes = 0;
int envelope_bits = 0;
int rate_control = 0;
int number_of_available_bits;
int number_of_valid_coefs;
int frame_error = 0;
int In[20];
float coefs[320];
float BufferOut[320];
int sum;
int checksum;
int calculated_checksum;
int idx;
int temp1;
int temp2;
for (i = 0; i < 20; i++)
#ifdef __BIG_ENDIAN__
In[i] = ((short *) DataIn)[i];
#else
In[i] =
((((short *) DataIn)[i] << 8) & 0xFF00) | ((((short *) DataIn)[i] >> 8)
& 0x00FF);
#endif
dwRes =
GetSirenCodecInfo (1, decoder->sample_rate, &number_of_coefs,
&sample_rate_bits, &rate_control_bits, &rate_control_possibilities,
&checksum_bits, &esf_adjustment, &scale_factor, &number_of_regions,
&sample_rate_code, &bits_per_frame);
if (dwRes != 0)
return dwRes;
set_bitstream (In);
decoded_sample_rate_code = 0;
for (i = 0; i < sample_rate_bits; i++) {
decoded_sample_rate_code <<= 1;
decoded_sample_rate_code |= next_bit ();
}
if (decoded_sample_rate_code != sample_rate_code)
return 7;
number_of_valid_coefs = region_size * number_of_regions;
number_of_available_bits = bits_per_frame - sample_rate_bits - checksum_bits;
envelope_bits =
decode_envelope (number_of_regions, decoder_standard_deviation,
absolute_region_power_index, esf_adjustment);
number_of_available_bits -= envelope_bits;
for (i = 0; i < rate_control_bits; i++) {
rate_control <<= 1;
rate_control |= next_bit ();
}
number_of_available_bits -= rate_control_bits;
categorize_regions (number_of_regions, number_of_available_bits,
absolute_region_power_index, power_categories, category_balance);
for (i = 0; i < rate_control; i++) {
power_categories[category_balance[i]]++;
}
number_of_available_bits =
decode_vector (decoder, number_of_regions, number_of_available_bits,
decoder_standard_deviation, power_categories, coefs, scale_factor);
frame_error = 0;
if (number_of_available_bits > 0) {
for (i = 0; i < number_of_available_bits; i++) {
if (next_bit () == 0)
frame_error = 1;
}
} else if (number_of_available_bits < 0
&& rate_control + 1 < rate_control_possibilities) {
frame_error |= 2;
}
for (i = 0; i < number_of_regions; i++) {
if (absolute_region_power_index[i] > 33
|| absolute_region_power_index[i] < -31)
frame_error |= 4;
}
if (checksum_bits > 0) {
bits_per_frame >>= 4;
checksum = In[bits_per_frame - 1] & ((1 << checksum_bits) - 1);
In[bits_per_frame - 1] &= ~checksum;
sum = 0;
idx = 0;
do {
sum ^= (In[idx] & 0xFFFF) << (idx % 15);
} while (++idx < bits_per_frame);
sum = (sum >> 15) ^ (sum & 0x7FFF);
calculated_checksum = 0;
for (i = 0; i < 4; i++) {
temp1 = ChecksumTable[i] & sum;
for (j = 8; j > 0; j >>= 1) {
temp2 = temp1 >> j;
temp1 ^= temp2;
}
calculated_checksum <<= 1;
calculated_checksum |= temp1 & 1;
}
if (checksum != calculated_checksum)
frame_error |= 8;
}
if (frame_error != 0) {
for (i = 0; i < number_of_valid_coefs; i++) {
coefs[i] = decoder->backup_frame[i];
decoder->backup_frame[i] = 0;
}
} else {
for (i = 0; i < number_of_valid_coefs; i++)
decoder->backup_frame[i] = coefs[i];
}
for (i = number_of_valid_coefs; i < number_of_coefs; i++)
coefs[i] = 0;
dwRes = siren_rmlt_decode_samples (coefs, decoder->context, 320, BufferOut);
for (i = 0; i < 320; i++) {
if (BufferOut[i] > 32767.0)
((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) 32767);
else if (BufferOut[i] <= -32768.0)
((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) 32768);
else
((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) BufferOut[i]);
}
decoder->WavHeader.Samples = ME_FROM_LE32 (decoder->WavHeader.Samples);
decoder->WavHeader.Samples += 320;
decoder->WavHeader.Samples = ME_TO_LE32 (decoder->WavHeader.Samples);
decoder->WavHeader.DataSize = ME_FROM_LE32 (decoder->WavHeader.DataSize);
decoder->WavHeader.DataSize += 640;
decoder->WavHeader.DataSize = ME_TO_LE32 (decoder->WavHeader.DataSize);
decoder->WavHeader.riff.RiffSize =
ME_FROM_LE32 (decoder->WavHeader.riff.RiffSize);
decoder->WavHeader.riff.RiffSize += 640;
decoder->WavHeader.riff.RiffSize =
ME_TO_LE32 (decoder->WavHeader.riff.RiffSize);
return 0;
}

View file

@ -24,67 +24,72 @@
#include "siren7.h"
SirenEncoder Siren7_NewEncoder(int sample_rate) {
SirenEncoder encoder = (SirenEncoder) malloc(sizeof(struct stSirenEncoder));
SirenEncoder
Siren7_NewEncoder (int sample_rate)
{
SirenEncoder encoder = (SirenEncoder) malloc (sizeof (struct stSirenEncoder));
encoder->sample_rate = sample_rate;
encoder->WavHeader.riff.RiffId = ME_TO_LE32(RIFF_ID);
encoder->WavHeader.riff.RiffSize = sizeof(SirenWavHeader) - 2*sizeof(int);
encoder->WavHeader.riff.RiffSize = ME_TO_LE32(encoder->WavHeader.riff.RiffSize);
encoder->WavHeader.WaveId = ME_TO_LE32(WAVE_ID);
encoder->WavHeader.riff.RiffId = ME_TO_LE32 (RIFF_ID);
encoder->WavHeader.riff.RiffSize = sizeof (SirenWavHeader) - 2 * sizeof (int);
encoder->WavHeader.riff.RiffSize =
ME_TO_LE32 (encoder->WavHeader.riff.RiffSize);
encoder->WavHeader.WaveId = ME_TO_LE32 (WAVE_ID);
encoder->WavHeader.FmtId = ME_TO_LE32(FMT__ID);
encoder->WavHeader.FmtSize = ME_TO_LE32(sizeof(SirenFmtChunk));
encoder->WavHeader.FmtId = ME_TO_LE32 (FMT__ID);
encoder->WavHeader.FmtSize = ME_TO_LE32 (sizeof (SirenFmtChunk));
encoder->WavHeader.fmt.fmt.Format = ME_TO_LE16(0x028E);
encoder->WavHeader.fmt.fmt.Channels = ME_TO_LE16(1);
encoder->WavHeader.fmt.fmt.SampleRate = ME_TO_LE32(16000);
encoder->WavHeader.fmt.fmt.ByteRate = ME_TO_LE32(2000);
encoder->WavHeader.fmt.fmt.BlockAlign = ME_TO_LE16(40);
encoder->WavHeader.fmt.fmt.BitsPerSample = ME_TO_LE16(0);
encoder->WavHeader.fmt.ExtraSize = ME_TO_LE16(2);
encoder->WavHeader.fmt.DctLength = ME_TO_LE16(320);
encoder->WavHeader.fmt.fmt.Format = ME_TO_LE16 (0x028E);
encoder->WavHeader.fmt.fmt.Channels = ME_TO_LE16 (1);
encoder->WavHeader.fmt.fmt.SampleRate = ME_TO_LE32 (16000);
encoder->WavHeader.fmt.fmt.ByteRate = ME_TO_LE32 (2000);
encoder->WavHeader.fmt.fmt.BlockAlign = ME_TO_LE16 (40);
encoder->WavHeader.fmt.fmt.BitsPerSample = ME_TO_LE16 (0);
encoder->WavHeader.fmt.ExtraSize = ME_TO_LE16 (2);
encoder->WavHeader.fmt.DctLength = ME_TO_LE16 (320);
encoder->WavHeader.FactId = ME_TO_LE32(FACT_ID);
encoder->WavHeader.FactSize = ME_TO_LE32(sizeof(int));
encoder->WavHeader.Samples = ME_TO_LE32(0);
encoder->WavHeader.FactId = ME_TO_LE32 (FACT_ID);
encoder->WavHeader.FactSize = ME_TO_LE32 (sizeof (int));
encoder->WavHeader.Samples = ME_TO_LE32 (0);
encoder->WavHeader.DataId = ME_TO_LE32(DATA_ID);
encoder->WavHeader.DataSize = ME_TO_LE32(0);
encoder->WavHeader.DataId = ME_TO_LE32 (DATA_ID);
encoder->WavHeader.DataSize = ME_TO_LE32 (0);
memset(encoder->context, 0, sizeof(encoder->context));
memset (encoder->context, 0, sizeof (encoder->context));
siren_init();
siren_init ();
return encoder;
}
void Siren7_CloseEncoder(SirenEncoder encoder) {
free(encoder);
void
Siren7_CloseEncoder (SirenEncoder encoder)
{
free (encoder);
}
int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned char *DataOut) {
int
Siren7_EncodeFrame (SirenEncoder encoder, unsigned char *DataIn,
unsigned char *DataOut)
{
int number_of_coefs,
sample_rate_bits,
rate_control_bits,
rate_control_possibilities,
checksum_bits,
esf_adjustment,
scale_factor,
number_of_regions,
sample_rate_code,
bits_per_frame;
scale_factor, number_of_regions, sample_rate_code, bits_per_frame;
int sample_rate = encoder->sample_rate;
static int absolute_region_power_index[28] = {0};
static int power_categories[28] = {0};
static int category_balance[28] = {0};
static int drp_num_bits[30] = {0};
static int drp_code_bits[30] = {0};
static int region_mlt_bit_counts[28] = {0};
static int region_mlt_bits[112] = {0};
int ChecksumTable[4] = {0x7F80, 0x7878, 0x6666, 0x5555};
static int absolute_region_power_index[28] = { 0 };
static int power_categories[28] = { 0 };
static int category_balance[28] = { 0 };
static int drp_num_bits[30] = { 0 };
static int drp_code_bits[30] = { 0 };
static int region_mlt_bit_counts[28] = { 0 };
static int region_mlt_bits[112] = { 0 };
int ChecksumTable[4] = { 0x7F80, 0x7878, 0x6666, 0x5555 };
int i, j;
int dwRes = 0;
@ -109,31 +114,44 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
float *context = encoder->context;
for (i = 0; i < 320; i++)
In[i] = (float) ((short) ME_FROM_LE16(((short *) DataIn)[i]));
In[i] = (float) ((short) ME_FROM_LE16 (((short *) DataIn)[i]));
dwRes = siren_rmlt_encode_samples(In, context, 320, coefs);
dwRes = siren_rmlt_encode_samples (In, context, 320, coefs);
if (dwRes != 0)
return dwRes;
dwRes = GetSirenCodecInfo(1, sample_rate, &number_of_coefs, &sample_rate_bits, &rate_control_bits, &rate_control_possibilities, &checksum_bits, &esf_adjustment, &scale_factor, &number_of_regions, &sample_rate_code, &bits_per_frame );
dwRes =
GetSirenCodecInfo (1, sample_rate, &number_of_coefs, &sample_rate_bits,
&rate_control_bits, &rate_control_possibilities, &checksum_bits,
&esf_adjustment, &scale_factor, &number_of_regions, &sample_rate_code,
&bits_per_frame);
if (dwRes != 0)
return dwRes;
envelope_bits = compute_region_powers(number_of_regions, coefs, drp_num_bits, drp_code_bits, absolute_region_power_index, esf_adjustment);
envelope_bits =
compute_region_powers (number_of_regions, coefs, drp_num_bits,
drp_code_bits, absolute_region_power_index, esf_adjustment);
number_of_available_bits = bits_per_frame - rate_control_bits - envelope_bits - sample_rate_bits - checksum_bits ;
number_of_available_bits =
bits_per_frame - rate_control_bits - envelope_bits - sample_rate_bits -
checksum_bits;
categorize_regions(number_of_regions, number_of_available_bits, absolute_region_power_index, power_categories, category_balance);
categorize_regions (number_of_regions, number_of_available_bits,
absolute_region_power_index, power_categories, category_balance);
for(region = 0; region < number_of_regions; region++) {
for (region = 0; region < number_of_regions; region++) {
absolute_region_power_index[region] += 24;
region_mlt_bit_counts[region] = 0;
}
rate_control = quantize_mlt(number_of_regions, rate_control_possibilities, number_of_available_bits, coefs, absolute_region_power_index, power_categories, category_balance, region_mlt_bit_counts, region_mlt_bits);
rate_control =
quantize_mlt (number_of_regions, rate_control_possibilities,
number_of_available_bits, coefs, absolute_region_power_index,
power_categories, category_balance, region_mlt_bit_counts,
region_mlt_bits);
idx = 0;
bits_left = 16 - sample_rate_bits;
@ -152,20 +170,23 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
}
}
for (region = 0; region < number_of_regions && (16*idx) < bits_per_frame; region++) {
for (region = 0; region < number_of_regions && (16 * idx) < bits_per_frame;
region++) {
current_word_bits_left = region_bit_count = region_mlt_bit_counts[region];
if (current_word_bits_left > 32)
current_word_bits_left = 32;
current_word = region_mlt_bits[region*4];
current_word = region_mlt_bits[region * 4];
i = 1;
while(region_bit_count > 0 && (16*idx) < bits_per_frame) {
while (region_bit_count > 0 && (16 * idx) < bits_per_frame) {
if (current_word_bits_left < bits_left) {
bits_left -= current_word_bits_left;
out_word += (current_word >> (32 - current_word_bits_left)) << bits_left;
out_word +=
(current_word >> (32 - current_word_bits_left)) << bits_left;
current_word_bits_left = 0;
} else {
BufferOut[idx++] = (short) (out_word + (current_word >> (32 - bits_left)));
BufferOut[idx++] =
(short) (out_word + (current_word >> (32 - bits_left)));
current_word_bits_left -= bits_left;
current_word <<= bits_left;
bits_left = 16;
@ -173,7 +194,7 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
}
if (current_word_bits_left == 0) {
region_bit_count -= 32;
current_word = region_mlt_bits[(region*4) + i++];
current_word = region_mlt_bits[(region * 4) + i++];
current_word_bits_left = region_bit_count;
if (current_word_bits_left > 32)
current_word_bits_left = 32;
@ -182,19 +203,19 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
}
while ( (16*idx) < bits_per_frame) {
while ((16 * idx) < bits_per_frame) {
BufferOut[idx++] = (short) ((0xFFFF >> (16 - bits_left)) + out_word);
bits_left = 16;
out_word = 0;
}
if (checksum_bits > 0) {
BufferOut[idx-1] &= (-1 << checksum_bits);
BufferOut[idx - 1] &= (-1 << checksum_bits);
sum = 0;
idx = 0;
do {
sum ^= (BufferOut[idx] & 0xFFFF) << (idx % 15);
} while ((16*++idx) < bits_per_frame);
} while ((16 * ++idx) < bits_per_frame);
sum = (sum >> 15) ^ (sum & 0x7FFF);
checksum = 0;
@ -207,7 +228,7 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
checksum <<= 1;
checksum |= temp1 & 1;
}
BufferOut[idx-1] |= ((1 << checksum_bits) -1) & checksum;
BufferOut[idx - 1] |= ((1 << checksum_bits) - 1) & checksum;
}
@ -215,20 +236,22 @@ int Siren7_EncodeFrame(SirenEncoder encoder, unsigned char *DataIn, unsigned cha
#ifdef __BIG_ENDIAN__
((short *) DataOut)[i] = BufferOut[i];
#else
((short *) DataOut)[i] = ((BufferOut[i] << 8) & 0xFF00) | ((BufferOut[i] >> 8) & 0x00FF);
((short *) DataOut)[i] =
((BufferOut[i] << 8) & 0xFF00) | ((BufferOut[i] >> 8) & 0x00FF);
#endif
encoder->WavHeader.Samples = ME_FROM_LE32(encoder->WavHeader.Samples);
encoder->WavHeader.Samples = ME_FROM_LE32 (encoder->WavHeader.Samples);
encoder->WavHeader.Samples += 320;
encoder->WavHeader.Samples = ME_TO_LE32(encoder->WavHeader.Samples);
encoder->WavHeader.DataSize = ME_FROM_LE32(encoder->WavHeader.DataSize);
encoder->WavHeader.Samples = ME_TO_LE32 (encoder->WavHeader.Samples);
encoder->WavHeader.DataSize = ME_FROM_LE32 (encoder->WavHeader.DataSize);
encoder->WavHeader.DataSize += 40;
encoder->WavHeader.DataSize = ME_TO_LE32(encoder->WavHeader.DataSize);
encoder->WavHeader.riff.RiffSize = ME_FROM_LE32(encoder->WavHeader.riff.RiffSize);
encoder->WavHeader.DataSize = ME_TO_LE32 (encoder->WavHeader.DataSize);
encoder->WavHeader.riff.RiffSize =
ME_FROM_LE32 (encoder->WavHeader.riff.RiffSize);
encoder->WavHeader.riff.RiffSize += 40;
encoder->WavHeader.riff.RiffSize = ME_TO_LE32(encoder->WavHeader.riff.RiffSize);
encoder->WavHeader.riff.RiffSize =
ME_TO_LE32 (encoder->WavHeader.riff.RiffSize);
return 0;
}

View file

@ -28,7 +28,9 @@ static short current_word = 0;
static int bit_idx = 0;
static int *bitstream_ptr = NULL;
int next_bit() {
int
next_bit ()
{
if (bitstream_ptr == NULL)
return -1;
@ -40,14 +42,19 @@ int next_bit() {
return (current_word >> --bit_idx) & 1;
}
void set_bitstream(int *stream) {
void
set_bitstream (int *stream)
{
bitstream_ptr = stream;
current_word = *bitstream_ptr;
current_word = *bitstream_ptr;
bit_idx = 0;
}
int compute_region_powers(int number_of_regions, float *coefs, int *drp_num_bits, int *drp_code_bits, int *absolute_region_power_index, int esf_adjustment) {
int
compute_region_powers (int number_of_regions, float *coefs, int *drp_num_bits,
int *drp_code_bits, int *absolute_region_power_index, int esf_adjustment)
{
float region_power = 0;
int num_bits;
int idx;
@ -56,8 +63,9 @@ int compute_region_powers(int number_of_regions, float *coefs, int *drp_num_bits
for (region = 0; region < number_of_regions; region++) {
region_power = 0.0f;
for (i = 0 ; i < region_size; i++) {
region_power += coefs[(region*region_size)+i] * coefs[(region*region_size)+i];
for (i = 0; i < region_size; i++) {
region_power +=
coefs[(region * region_size) + i] * coefs[(region * region_size) + i];
}
region_power *= region_size_inverse;
@ -65,7 +73,7 @@ int compute_region_powers(int number_of_regions, float *coefs, int *drp_num_bits
max_idx = 64;
for (i = 0; i < 6; i++) {
idx = (min_idx + max_idx) / 2;
if (region_power_table_boundary[idx-1] <= region_power) {
if (region_power_table_boundary[idx - 1] <= region_power) {
min_idx = idx;
} else {
max_idx = idx;
@ -75,67 +83,78 @@ int compute_region_powers(int number_of_regions, float *coefs, int *drp_num_bits
}
for (region = number_of_regions-2; region >= 0; region--) {
if (absolute_region_power_index[region] < absolute_region_power_index[region+1] - 11)
absolute_region_power_index[region] = absolute_region_power_index[region+1] - 11;
for (region = number_of_regions - 2; region >= 0; region--) {
if (absolute_region_power_index[region] <
absolute_region_power_index[region + 1] - 11)
absolute_region_power_index[region] =
absolute_region_power_index[region + 1] - 11;
}
if (absolute_region_power_index[0] < (1-esf_adjustment))
absolute_region_power_index[0] = (1-esf_adjustment);
if (absolute_region_power_index[0] < (1 - esf_adjustment))
absolute_region_power_index[0] = (1 - esf_adjustment);
if (absolute_region_power_index[0] > (31-esf_adjustment))
absolute_region_power_index[0] = (31-esf_adjustment);
if (absolute_region_power_index[0] > (31 - esf_adjustment))
absolute_region_power_index[0] = (31 - esf_adjustment);
drp_num_bits[0] = 5;
drp_code_bits[0] = absolute_region_power_index[0] + esf_adjustment;
for(region = 1; region < number_of_regions; region++) {
for (region = 1; region < number_of_regions; region++) {
if (absolute_region_power_index[region] < (-8 - esf_adjustment))
absolute_region_power_index[region] = (-8 - esf_adjustment);
if (absolute_region_power_index[region] > (31-esf_adjustment))
absolute_region_power_index[region] = (31-esf_adjustment);
if (absolute_region_power_index[region] > (31 - esf_adjustment))
absolute_region_power_index[region] = (31 - esf_adjustment);
}
num_bits = 5;
for(region = 0; region < number_of_regions-1; region++) {
idx = absolute_region_power_index[region+1] - absolute_region_power_index[region] + 12;
for (region = 0; region < number_of_regions - 1; region++) {
idx =
absolute_region_power_index[region + 1] -
absolute_region_power_index[region] + 12;
if (idx < 0)
idx = 0;
absolute_region_power_index[region+1] = absolute_region_power_index[region] + idx - 12;
drp_num_bits[region+1] = differential_region_power_bits[region][idx];
drp_code_bits[region+1] = differential_region_power_codes[region][idx];
num_bits += drp_num_bits[region+1];
absolute_region_power_index[region + 1] =
absolute_region_power_index[region] + idx - 12;
drp_num_bits[region + 1] = differential_region_power_bits[region][idx];
drp_code_bits[region + 1] = differential_region_power_codes[region][idx];
num_bits += drp_num_bits[region + 1];
}
return num_bits;
}
int decode_envelope(int number_of_regions, float *decoder_standard_deviation, int *absolute_region_power_index, int esf_adjustment) {
int
decode_envelope (int number_of_regions, float *decoder_standard_deviation,
int *absolute_region_power_index, int esf_adjustment)
{
int index;
int i;
int envelope_bits = 0;
index = 0;
for (i = 0; i < 5; i++)
index = (index<<1) | next_bit();
index = (index << 1) | next_bit ();
envelope_bits = 5;
absolute_region_power_index[0] = index - esf_adjustment;
decoder_standard_deviation[0] = standard_deviation[absolute_region_power_index[0] + 24];
decoder_standard_deviation[0] =
standard_deviation[absolute_region_power_index[0] + 24];
for (i = 1; i < number_of_regions; i++) {
index = 0;
do {
index = differential_decoder_tree[i-1][index][next_bit()];
index = differential_decoder_tree[i - 1][index][next_bit ()];
envelope_bits++;
} while (index > 0);
absolute_region_power_index[i] = absolute_region_power_index[i-1] - index - 12;
decoder_standard_deviation[i] = standard_deviation[absolute_region_power_index[i] + 24];
absolute_region_power_index[i] =
absolute_region_power_index[i - 1] - index - 12;
decoder_standard_deviation[i] =
standard_deviation[absolute_region_power_index[i] + 24];
}
return envelope_bits;
@ -143,7 +162,9 @@ int decode_envelope(int number_of_regions, float *decoder_standard_deviation, in
static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
static int
huffman_vector (int category, int power_idx, float *mlts, int *out)
{
int i, j;
float temp_value = deviation_inverse[power_idx] * step_size_inverse[category];
int sign_idx, idx, non_zeroes, max, bits_available;
@ -154,11 +175,11 @@ static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
for (i = 0; i < number_of_vectors[category]; i++) {
sign_idx = idx = non_zeroes = 0;
for (j = 0; j < vector_dimension[category]; j++) {
max = (int) ((fabs(*mlts) * temp_value) + dead_zone[category]);
max = (int) ((fabs (*mlts) * temp_value) + dead_zone[category]);
if (max != 0) {
sign_idx <<= 1;
non_zeroes++;
if (*mlts > 0)
if (*mlts > 0)
sign_idx++;
if (max > max_bin[category] || max < 0)
max = max_bin[category];
@ -171,11 +192,17 @@ static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
region_bits += bitcount_tables[category][idx] + non_zeroes;
bits_available -= bitcount_tables[category][idx] + non_zeroes;
if (bits_available < 0) {
*out++ = current_word + (((code_tables[category][idx] << non_zeroes) + sign_idx) >> -bits_available);
*out++ =
current_word + (((code_tables[category][idx] << non_zeroes) +
sign_idx) >> -bits_available);
bits_available += 32;
current_word = ((code_tables[category][idx] << non_zeroes) + sign_idx) << bits_available;
current_word =
((code_tables[category][idx] << non_zeroes) +
sign_idx) << bits_available;
} else {
current_word += ((code_tables[category][idx] << non_zeroes) + sign_idx) << bits_available;
current_word +=
((code_tables[category][idx] << non_zeroes) +
sign_idx) << bits_available;
}
}
@ -184,20 +211,28 @@ static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
return region_bits;
}
int quantize_mlt(int number_of_regions, int rate_control_possibilities, int number_of_available_bits, float *coefs, int *absolute_region_power_index, int *power_categories, int *category_balance, int *region_mlt_bit_counts, int *region_mlt_bits) {
int
quantize_mlt (int number_of_regions, int rate_control_possibilities,
int number_of_available_bits, float *coefs,
int *absolute_region_power_index, int *power_categories,
int *category_balance, int *region_mlt_bit_counts, int *region_mlt_bits)
{
int region;
int mlt_bits = 0;
int rate_control;
for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1); rate_control++)
for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1);
rate_control++)
power_categories[category_balance[rate_control]]++;
for (region = 0; region < number_of_regions; region++) {
if (power_categories[region] > 6)
region_mlt_bit_counts[region] = 0;
else
region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4*region));
region_mlt_bit_counts[region] =
huffman_vector (power_categories[region],
absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4 * region));
mlt_bits += region_mlt_bit_counts[region];
}
@ -214,13 +249,16 @@ int quantize_mlt(int number_of_regions, int rate_control_possibilities, int numb
if (power_categories[region] > 6)
region_mlt_bit_counts[region] = 0;
else
region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4*region));
region_mlt_bit_counts[region] =
huffman_vector (power_categories[region],
absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4 * region));
mlt_bits += region_mlt_bit_counts[region];
}
while(mlt_bits > number_of_available_bits && rate_control < rate_control_possibilities) {
while (mlt_bits > number_of_available_bits
&& rate_control < rate_control_possibilities) {
region = category_balance[rate_control];
power_categories[region]++;
mlt_bits -= region_mlt_bit_counts[region];
@ -228,8 +266,10 @@ int quantize_mlt(int number_of_regions, int rate_control_possibilities, int numb
if (power_categories[region] > 6)
region_mlt_bit_counts[region] = 0;
else
region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4*region));
region_mlt_bit_counts[region] =
huffman_vector (power_categories[region],
absolute_region_power_index[region], coefs + (region_size * region),
region_mlt_bits + (4 * region));
mlt_bits += region_mlt_bit_counts[region];
@ -239,7 +279,9 @@ int quantize_mlt(int number_of_regions, int rate_control_possibilities, int numb
return rate_control;
}
static int get_dw(SirenDecoder decoder) {
static int
get_dw (SirenDecoder decoder)
{
int ret = decoder->dw1 + decoder->dw4;
if ((ret & 0x8000) != 0)
@ -256,7 +298,11 @@ static int get_dw(SirenDecoder decoder) {
int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_available_bits, float *decoder_standard_deviation, int *power_categories, float *coefs, int scale_factor) {
int
decode_vector (SirenDecoder decoder, int number_of_regions,
int number_of_available_bits, float *decoder_standard_deviation,
int *power_categories, float *coefs, int scale_factor)
{
float *coefs_ptr;
float decoded_value;
float noise;
@ -286,7 +332,7 @@ int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_ava
break;
}
index = decoder_tree[index + next_bit()];
index = decoder_tree[index + next_bit ()];
number_of_available_bits--;
} while ((index & 1) == 0);
@ -294,11 +340,12 @@ int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_ava
if (error == 0 && number_of_available_bits >= 0) {
for (j = 0; j < vector_dimension[category]; j++) {
decoded_value = mlt_quant[category][index & ((1 << index_table[category]) - 1)];
decoded_value =
mlt_quant[category][index & ((1 << index_table[category]) - 1)];
index >>= index_table[category];
if (decoded_value != 0) {
if (next_bit() == 0)
if (next_bit () == 0)
decoded_value *= -decoder_standard_deviation[region];
else
decoded_value *= decoder_standard_deviation[region];
@ -328,7 +375,7 @@ int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_ava
for (j = 0; j < region_size; j++) {
if (*coefs_ptr != 0) {
i++;
if (fabs(*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
if (fabs (*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
i += 3;
}
}
@ -345,7 +392,7 @@ int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_ava
noise = decoder_standard_deviation[region] * noise_category6[i];
} else if (category == 7) {
noise = decoder_standard_deviation[region] * noise_category7;
noise = decoder_standard_deviation[region] * noise_category7;
} else {
noise = 0;
}
@ -353,10 +400,10 @@ int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_ava
coefs_ptr = coefs + (region * region_size);
if (category == 5 || category == 6 || category == 7) {
dw1 = get_dw(decoder);
dw2 = get_dw(decoder);
dw1 = get_dw (decoder);
dw2 = get_dw (decoder);
for (j=0; j<10; j++) {
for (j = 0; j < 10; j++) {
if (category == 7 || *coefs_ptr == 0) {
if ((dw1 & 1))
*coefs_ptr = noise;

View file

@ -29,23 +29,28 @@ static float rmlt_window_320[320];
#define PI_2 1.57079632679489661923
void siren_rmlt_init() {
void
siren_rmlt_init ()
{
int i = 0;
float angle;
for (i = 0; i < 640; i++) {
angle = (float) (((i + 0.5) * PI_2) / 640);
rmlt_window_640[i] = (float) sin(angle);
rmlt_window_640[i] = (float) sin (angle);
}
for (i = 0; i < 320; i++) {
angle = (float) (((i + 0.5) * PI_2) / 320);
rmlt_window_320[i] = (float) sin(angle);
rmlt_window_320[i] = (float) sin (angle);
}
rmlt_initialized = 1;
}
int siren_rmlt_encode_samples(float *samples, float *old_samples, int dct_length, float *rmlt_coefs) {
int
siren_rmlt_encode_samples (float *samples, float *old_samples, int dct_length,
float *rmlt_coefs)
{
int half_dct_length = dct_length / 2;
float *old_ptr = old_samples + half_dct_length;
float *coef_high = rmlt_coefs + half_dct_length;
@ -57,7 +62,7 @@ int siren_rmlt_encode_samples(float *samples, float *old_samples, int dct_length
int i = 0;
if (rmlt_initialized == 0)
siren_rmlt_init();
siren_rmlt_init ();
if (dct_length == 320)
window_low = rmlt_window_320;
@ -71,21 +76,26 @@ int siren_rmlt_encode_samples(float *samples, float *old_samples, int dct_length
for (i = 0; i < half_dct_length; i++) {
*--coef_low = *--old_ptr;
*coef_high++ = (*samples_low * *--window_high) - (*--samples_high * *window_low);
*old_ptr = (*samples_high * *window_high) + (*samples_low++ * *window_low++);
*coef_high++ =
(*samples_low * *--window_high) - (*--samples_high * *window_low);
*old_ptr =
(*samples_high * *window_high) + (*samples_low++ * *window_low++);
}
siren_dct4(rmlt_coefs, rmlt_coefs, dct_length);
siren_dct4 (rmlt_coefs, rmlt_coefs, dct_length);
return 0;
}
int siren_rmlt_decode_samples(float *coefs, float *old_coefs, int dct_length, float *samples) {
int
siren_rmlt_decode_samples (float *coefs, float *old_coefs, int dct_length,
float *samples)
{
int half_dct_length = dct_length / 2;
float *old_low = old_coefs;
float *old_high = old_coefs + half_dct_length;
float *samples_low = samples ;
float *samples_low = samples;
float *samples_high = samples + dct_length;
float *samples_middle_low = samples + half_dct_length;
float *samples_middle_high = samples + half_dct_length;
@ -100,7 +110,7 @@ int siren_rmlt_decode_samples(float *coefs, float *old_coefs, int dct_length, fl
int i = 0;
if (rmlt_initialized == 0)
siren_rmlt_init();
siren_rmlt_init ();
if (dct_length == 320)
window_low = rmlt_window_320;
@ -114,17 +124,23 @@ int siren_rmlt_decode_samples(float *coefs, float *old_coefs, int dct_length, fl
window_middle_low = window_low + half_dct_length;
window_middle_high = window_low + half_dct_length;
siren_dct4(coefs, samples, dct_length);
siren_dct4 (coefs, samples, dct_length);
for (i = 0; i < half_dct_length; i+=2) {
for (i = 0; i < half_dct_length; i += 2) {
sample_low_val = *samples_low;
sample_high_val = *--samples_high;
sample_middle_low_val = *--samples_middle_low;
sample_middle_high_val = *samples_middle_high;
*samples_low++ = (*old_low * *--window_high) + (sample_middle_low_val * *window_low);
*samples_high = (sample_middle_low_val * *window_high) - (*old_low * *window_low++);
*samples_middle_high++ = (sample_low_val * *window_middle_high) - (*--old_high * *--window_middle_low);
*samples_middle_low = (*old_high * *window_middle_high++) + (sample_low_val * *window_middle_low);
*samples_low++ =
(*old_low * *--window_high) + (sample_middle_low_val * *window_low);
*samples_high =
(sample_middle_low_val * *window_high) - (*old_low * *window_low++);
*samples_middle_high++ =
(sample_low_val * *window_middle_high) -
(*--old_high * *--window_middle_low);
*samples_middle_low =
(*old_high * *window_middle_high++) +
(sample_low_val * *window_middle_low);
*old_low++ = sample_middle_high_val;
*old_high = sample_high_val;
}