Linear Predictive Coding: Difference between revisions
Jump to navigation
Jump to search
(24 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
|Synopsis=This is the heart of both fLaC compression and predicting the next sound, I think | |Synopsis=This is the heart of both fLaC compression and predicting the next sound, I think | ||
|Analysis=Going to decode here and see how easily it can be introduced into my playAlong code. | |Analysis=Going to decode here and see how easily it can be introduced into my playAlong code. | ||
|Thread=Arts, Anarchist Spellbook, Religion and Philosophy, STEM | |Thread=Arts, Anarchist Spellbook, Religion and Philosophy, STEM, FLAC Agent Build | ||
|EditDate=2025- | |EditDate=2025-02-13 | ||
|Prime Category=Belief | |Prime Category=Belief | ||
|WebRef=https://en.wikipedia.org/wiki/Linear_prediction | |WebRef=https://en.wikipedia.org/wiki/Linear_prediction | ||
Line 13: | Line 13: | ||
|+ Frame Header 2 | |+ Frame Header 2 | ||
|- | |- | ||
! Line Number !! Code Package !! LOC | ! Line Number !! Code Package !! LOC title with code or operation Descript | ||
|- | |- | ||
| 2987 || SDC || read_residual_partitioned_rice | | 2987 || SDC || read_residual_partitioned_rice sample += u; <br/> | ||
Key parameters | Key parameters | ||
* sample = 4088 for partition 0 4096 thereafter | * sample = 4088 for partition 0 4096 thereafter | ||
|- | |- | ||
| 2883 || SDC || read_subframe_lpc | | 2883 || SDC || read_subframe_lpc ''break'' | ||
|- | |- | ||
| 2889 ||SDC || Do Full Decode | | 2889 ||SDC || Do Full Decode | ||
|- | |- | ||
| 2892 || SDC || Get Warm-up values | | 2892 || SDC || Get Warm-up values 8 previously stored warm-up values based on order=8 | ||
|- | |- | ||
| 2894 || SDC || call FLAC lpc max bps || | |Next set of routines are hidden because they are primarily used to determine whether we are 32 bit or 64 bit architecture. We will always be 64 bit. | ||
<ref> | |||
{| class="wikitable" style="margin:auto" | |||
| 2894 || SDC || call FLAC lpc max bps || '' if(FLAC__lpc_max_residual_bps(bps, subframe->qlp_coeff, order, subframe->quantization_level) <= 32 && FLAC__lpc_max_prediction_before_shift_bps(bps, subframe->qlp_coeff, order) <= 32)'' Checking that absolute sub of qlp_coeff + bps <= 32 bits | |||
'''Does nothing to calculate value. Used to determine whether 64 bit wide or not we follow 64 bit wide path!''' | |||
|- | |- | ||
| | | 958 || LPC ||'''FLAC__lpc_max_residual_bps''' ||''uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization) | ||
'' Variables | |||
* uint32_t subframe_bps = 24 | * uint32_t subframe_bps = 24 | ||
* const FLAC__int32 * | * qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc | ||
Address 0 - 3 4 - 7 8 - B C - F | |||
0000555555859EF0 08000000 0F000000 0D000000 E51D0000 | |||
0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 | |||
0000555555859F10 17210000 72010000 6EF2FFFF | |||
* uint32_t order = 8 | |||
* lp_quantization = 13 0xD | |||
|- | |||
| 960 || LPC || '''FLAC__lpc_max_residual_bps''' ||'' FLAC__int32 predictor_sum_bps = FLAC__lpc_max_prediction_before_shift_bps(subframe_bps, qlp_coeff, order) - lp_quantization;'' | |||
* subframe_bps = 0x18 = 24 | |||
* qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc | |||
Address 0 - 3 4 - 7 8 - B C - F | Address 0 - 3 4 - 7 8 - B C - F | ||
0000555555859EF0 08000000 0F000000 0D000000 E51D0000 | 0000555555859EF0 08000000 0F000000 0D000000 E51D0000 | ||
0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 | 0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 | ||
0000555555859F10 17210000 72010000 6EF2FFFF | 0000555555859F10 17210000 72010000 6EF2FFFF | ||
* uint32_t order = 8 | * uint32_t order = 8 | ||
'''predictor_sum_bps = 0x1c = 28 ''' | |||
|- | |- | ||
| 948 || LPC || FLAC__lpc_max_prediction_before_shift_bps||''FLAC__int32 abs_sum_of_qlp_coeff = 0;'' | | 948 || LPC || '''FLAC__lpc_max_prediction_before_shift_bps''' ||''FLAC__int32 abs_sum_of_qlp_coeff = 0;'' | ||
|- | |- | ||
| 951 || LPC || FLAC__lpc_max_prediction_before_shift_bps|| | | 951 || LPC || '''FLAC__lpc_max_prediction_before_shift_bps'''|| '' for(i = 0; i < order; i++) abs_sum_of_qlp_coeff += abs(qlp_coeff[i]);'' <info>Iterative based on i < order = 8 if abs_sum_of_qlp_coeff = 0 = 1</info> | ||
* abs_sum_of_qlp_coeff = '''0xd56b''' = 54635 | * qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc | ||
Address 0 - 3 4 - 7 8 - B C - F | |||
0000555555859EF0 08000000 0F000000 0D000000 E51D0000 | |||
0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 | |||
0000555555859F10 17210000 72010000 6EF2FFFF | |||
* uint32_t order = 8 | |||
abs_sum_of_qlp_coeff = '''0xd56b''' = 54635 | |||
}} | |||
</ref> | |||
|- | |- | ||
|2898 || SDC | |2898 || SDC ||''FLAC__lpc_restore_signal_'''wide'''(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);'' | ||
* channel = 0 | * channel = 0 | ||
* order = 8 | * order = 8 | ||
Line 53: | Line 73: | ||
* subframe->quantization_level = 13 0xd | * subframe->quantization_level = 13 0xd | ||
* decoder->private_->output[channel]+order = 0x5555558599c0 +0x8*8(32 Bits) = 0x5555558599c0 + 0x40 = 0x555555859A00 | * decoder->private_->output[channel]+order = 0x5555558599c0 +0x8*8(32 Bits) = 0x5555558599c0 + 0x40 = 0x555555859A00 | ||
|- | |||
|1238 ||LPC || ''void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict data)'' | |||
* residual = 0xfffffb81 = -1151 | |||
* data_len = 4088 = 0xff8 | |||
* qlp_coeff = 0x555555859efc address to start of 32 bit array | |||
* order = 8 | |||
* lp_quantization = 13 0xd | |||
* data = 0x55555585f350 '''''Looks like this is a virgin address need to check''''' | |||
*''(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization));'' | |||
* data = 0x55555585f350 -> 0x55555585f394 | |||
0x55555585f390 0000 0000 -> c670ffff = ffff70c6 = 30 testBigEndian.raw file. | |||
* r = 0x555555863340 | |||
81FB FFFF 81FD FFFF FE06 0000 D0FC FFFF | |||
09FE FFFF 8AFE FFFF 2504 0000 76F9 FFFF | |||
* sum = 0xffffffffeea8a9d8 | |||
* lp_quantization = 0xD = 13 | |||
|- | |||
|1264 ||LPC ||'''FLAC__lpc_restore_signal_wide''' ||*(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization)); | |||
* *data -> FFFF70C6 | |||
* sum = 0xffffffffeea8a9d8 | |||
* lp_quantization = 13 = 0xd | |||
* *r = 0xfffffb81 | |||
0xffffffffeea8a9d8»0xd = 7FFFFFFFF7545 | |||
7FFFFFFFF7545 + 0xfffffb81 = 80000FFFF70C6 | |||
|} | |} | ||
== | ===First Sample Raw=== | ||
=== | 1256 LPC ''for(j = 0; j < order; j++) sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));'' | ||
* order = 8 | |||
* qlp_coeff '''const FLAC__int32 * restrict 0x555555859efc''' | |||
1264 LPC *(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization)); First result: | 0x555555859efc E51D0000 282E0000 E0E2FFFF F4CFFFFF 170C0000 17210000 72010000 6EF2FFFF | ||
* Little Endian in Memory '''C670FFFF''' | |||
* history = data = history '''FLAC__int32 * 0x55555585f350''' | |||
0x55555585f340 DF91FFFF A28EFFFF 6186FFFF 717BFFFF | |||
=== | 0x55555585f350 00000000 00000000 00000000 00000000 | ||
1264 LPC ''*(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization));'' First result: | |||
* data = '''FLAC__int32 * restrict 0x55555585f350''' | |||
** *data = '''FLAC__int32 0''' | |||
* r '''const FLAC__int32 * 0x555555863340''' | |||
** *r '''const FLAC__int32 -1151 0xfffffb81''' | |||
* sum = '''FLAC__int64 -290936360 = 0xffffffffeea8a9d8''' | |||
* lp_quantization lp_quantization = '''int 13 = 0xd''' | |||
Little Endian in Memory '''C670FFFF''' | |||
Big Endian as read '''FFFF70C6''' | |||
testBigEndian.raw 0030 '''FF70C6''' | |||
==FLAC Secret Source== | |||
<nowiki> | |||
/* | |||
* Linear predictive coding to decompress encoded fLaC file | |||
* Variable Values | |||
* *data Address pointing to end of array of samples order deep in starting with warmup values in test case 8 samples | |||
* *coefficient pointer to start of an array of coefficients | |||
* *residual pointer to dataLen long array of 32 bit residual values previously calculated | |||
* order driving number for coefficients and previous samples available for calculation 8 | |||
* quantization number of bits we have to shift answer to fit to bps in first case 13. Hope to eliminate as not really needed. | |||
* | |||
*/ | |||
int32_t lpc(uint64_t *data, int32_t *coefficient, uint64_t *residual, uint32_t order, | |||
uint32_t quantization) { | |||
/* Calculate predicted sample */ | |||
int32_t sum = 0; // Predicted value being calculated | |||
/* For each coefficient sample going backwards one at a time add coefficient * sample */ | |||
for (uint32_t j = 0; j < order; j++) | |||
sum += (int64_t) coefficient[j] * (int64_t) (*(--data)); | |||
/* add residual and ensure fits in bit size */ | |||
sum = (int32_t) (*(residual++) + (sum >> quantization)); | |||
return sum; | |||
} | |||
</nowiki> | |||
<ref> | |||
<nowiki> | |||
for(i = 0; i < data_len; i++) { | |||
sum = 0; | |||
history = data; | |||
for(j = 0; j < order; j++) | |||
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); | |||
#ifdef FLAC__OVERFLOW_DETECT | |||
if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) { | |||
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization))); | |||
break; | |||
} | |||
#endif | |||
*(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization)); | |||
} | |||
</nowiki> | |||
</ref> | |||
==Notes for code analysis== | |||
4 bytes / int 32 Bits. Need to see how to start recording at 32 bits and most importantly storing 32 bit fLaC PCM samples that are not floating point if possible. Not sure it is possible at this point. | |||
==References== | |||
Latest revision as of 19:31, 16 February 2025
EditDate | 2025-02-13 |
---|
Related Threads: Arts, Anarchist Spellbook, Religion and Philosophy, STEM, FLAC Agent Build
This is the heart of both fLaC compression and predicting the next sound, I think
Going to decode here and see how easily it can be introduced into my playAlong code.Web References https://en.wikipedia.org/wiki/Linear prediction
Linear Predictive Coding
Line Number | Code Package | LOC title with code or operation Descript | |
---|---|---|---|
2987 | SDC | read_residual_partitioned_rice sample += u; Key parameters
| |
2883 | SDC | read_subframe_lpc break | |
2889 | SDC | Do Full Decode | |
2892 | SDC | Get Warm-up values 8 previously stored warm-up values based on order=8 | |
Next set of routines are hidden because they are primarily used to determine whether we are 32 bit or 64 bit architecture. We will always be 64 bit. | |||
2898 | SDC | FLAC__lpc_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
| |
1238 | LPC | void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict data)
0x55555585f390 0000 0000 -> c670ffff = ffff70c6 = 30 testBigEndian.raw file.
81FB FFFF 81FD FFFF FE06 0000 D0FC FFFF 09FE FFFF 8AFE FFFF 2504 0000 76F9 FFFF
| |
1264 | LPC | FLAC__lpc_restore_signal_wide | *(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization));
0xffffffffeea8a9d8»0xd = 7FFFFFFFF7545 7FFFFFFFF7545 + 0xfffffb81 = 80000FFFF70C6 |
First Sample Raw
1256 LPC for(j = 0; j < order; j++) sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
- order = 8
- qlp_coeff const FLAC__int32 * restrict 0x555555859efc
0x555555859efc E51D0000 282E0000 E0E2FFFF F4CFFFFF 170C0000 17210000 72010000 6EF2FFFF
- history = data = history FLAC__int32 * 0x55555585f350
0x55555585f340 DF91FFFF A28EFFFF 6186FFFF 717BFFFF 0x55555585f350 00000000 00000000 00000000 00000000
1264 LPC *(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization)); First result:
- data = FLAC__int32 * restrict 0x55555585f350
- *data = FLAC__int32 0
- r const FLAC__int32 * 0x555555863340
- *r const FLAC__int32 -1151 0xfffffb81
- sum = FLAC__int64 -290936360 = 0xffffffffeea8a9d8
- lp_quantization lp_quantization = int 13 = 0xd
Little Endian in Memory C670FFFF Big Endian as read FFFF70C6 testBigEndian.raw 0030 FF70C6
FLAC Secret Source
/* * Linear predictive coding to decompress encoded fLaC file * Variable Values * *data Address pointing to end of array of samples order deep in starting with warmup values in test case 8 samples * *coefficient pointer to start of an array of coefficients * *residual pointer to dataLen long array of 32 bit residual values previously calculated * order driving number for coefficients and previous samples available for calculation 8 * quantization number of bits we have to shift answer to fit to bps in first case 13. Hope to eliminate as not really needed. * */ int32_t lpc(uint64_t *data, int32_t *coefficient, uint64_t *residual, uint32_t order, uint32_t quantization) { /* Calculate predicted sample */ int32_t sum = 0; // Predicted value being calculated /* For each coefficient sample going backwards one at a time add coefficient * sample */ for (uint32_t j = 0; j < order; j++) sum += (int64_t) coefficient[j] * (int64_t) (*(--data)); /* add residual and ensure fits in bit size */ sum = (int32_t) (*(residual++) + (sum >> quantization)); return sum; }
Notes for code analysis
4 bytes / int 32 Bits. Need to see how to start recording at 32 bits and most importantly storing 32 bit fLaC PCM samples that are not floating point if possible. Not sure it is possible at this point.
References
- ↑
2894 SDC call FLAC lpc max bps if(FLAC__lpc_max_residual_bps(bps, subframe->qlp_coeff, order, subframe->quantization_level) <= 32 && FLAC__lpc_max_prediction_before_shift_bps(bps, subframe->qlp_coeff, order) <= 32) Checking that absolute sub of qlp_coeff + bps <= 32 bits Does nothing to calculate value. Used to determine whether 64 bit wide or not we follow 64 bit wide path!
958 LPC FLAC__lpc_max_residual_bps uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization) Variables
- uint32_t subframe_bps = 24
- qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc
Address 0 - 3 4 - 7 8 - B C - F 0000555555859EF0 08000000 0F000000 0D000000 E51D0000 0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 0000555555859F10 17210000 72010000 6EF2FFFF
- uint32_t order = 8
- lp_quantization = 13 0xD
960 LPC FLAC__lpc_max_residual_bps FLAC__int32 predictor_sum_bps = FLAC__lpc_max_prediction_before_shift_bps(subframe_bps, qlp_coeff, order) - lp_quantization; - subframe_bps = 0x18 = 24
- qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc
Address 0 - 3 4 - 7 8 - B C - F 0000555555859EF0 08000000 0F000000 0D000000 E51D0000 0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 0000555555859F10 17210000 72010000 6EF2FFFF
- uint32_t order = 8
predictor_sum_bps = 0x1c = 28
948 LPC FLAC__lpc_max_prediction_before_shift_bps FLAC__int32 abs_sum_of_qlp_coeff = 0; 951 LPC FLAC__lpc_max_prediction_before_shift_bps for(i = 0; i < order; i++) abs_sum_of_qlp_coeff += abs(qlp_coeff[i]); Iterative based on i < order = 8 if abs_sum_of_qlp_coeff = 0 = 1 - qlp_coeff = qlp_coeff const FLAC__int32 * restrict 0x555555859efc
Address 0 - 3 4 - 7 8 - B C - F 0000555555859EF0 08000000 0F000000 0D000000 E51D0000 0000555555859F00 282E0000 E0E2FFFF F4CFFFFF 170C0000 0000555555859F10 17210000 72010000 6EF2FFFF
- uint32_t order = 8
abs_sum_of_qlp_coeff = 0xd56b = 54635
}}
- ↑ for(i = 0; i < data_len; i++) { sum = 0; history = data; for(j = 0; j < order; j++) sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); #ifdef FLAC__OVERFLOW_DETECT if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) { fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization))); break; } #endif *(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization)); }