From 3b4313d844c206e240144b650ae7fd73b81dc8cb Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 24 Apr 2025 14:53:18 -0700 Subject: [PATCH] HDCD: Fix how unsigned audio may be processed Unsigned will alter the input, so move it like the other integer sample processors do. Signed-off-by: Christopher Snowhill --- Audio/Chain/ChunkList.m | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Audio/Chain/ChunkList.m b/Audio/Chain/ChunkList.m index b2321f3e0..ea0931be7 100644 --- a/Audio/Chain/ChunkList.m +++ b/Audio/Chain/ChunkList.m @@ -837,10 +837,18 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes if(hdcd_decoder) { // implied bits per sample is 16, produces 32 bit int scale samplesRead = bytesReadFromInput / 2; const size_t buffer_adder = (inputBuffer == &tempData[0]) ? buffer_adder_base : 0; - if(isUnsigned) + if(isUnsigned) { + if(!inputChanged) { + memcpy(&tempData[buffer_adder], inputBuffer, samplesRead * 2); + inputBuffer = &tempData[buffer_adder]; + inputChanged = YES; + } convert_u16_to_s16((int16_t *)inputBuffer, samplesRead); - convert_s16_to_hdcd_input((int32_t *)(&tempData[buffer_adder]), (int16_t *)inputBuffer, samplesRead); - hdcd_process_stereo((hdcd_state_stereo_t *)hdcd_decoder, (int32_t *)(&tempData[buffer_adder]), (int)(samplesRead / 2)); + isUnsigned = NO; + } + const size_t buffer_adder2 = (inputBuffer == &tempData[0]) ? buffer_adder_base : 0; + convert_s16_to_hdcd_input((int32_t *)(&tempData[buffer_adder2]), (int16_t *)inputBuffer, samplesRead); + hdcd_process_stereo((hdcd_state_stereo_t *)hdcd_decoder, (int32_t *)(&tempData[buffer_adder2]), (int)(samplesRead / 2)); if(((hdcd_state_stereo_t *)hdcd_decoder)->channel[0].sustain && ((hdcd_state_stereo_t *)hdcd_decoder)->channel[1].sustain) { hdcdSustained = YES; @@ -850,7 +858,7 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes bitsPerSample = 32; bytesReadFromInput = samplesRead * 4; isUnsigned = NO; - inputBuffer = &tempData[buffer_adder]; + inputBuffer = &tempData[buffer_adder2]; inputChanged = YES; } else { // Discard the output of the decoder and process again