Skip to content

Commit 86633ef

Browse files
mikeysklarclaude
andcommitted
picodvi: pass the sample rate to hstx_packet_set_audio_samples
It takes the rate in Hz, like hstx_packet_set_audio_infoframe, and looks up the IEC 60958 sample frequency code itself, as upstream pico_hdmi does. The packets sent are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 7dc7502 commit 86633ef

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

‎ports/raspberrypi/common-hal/picodvi/AudioOut.c‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ typedef struct {
2828
uint32_t sample_rate;
2929
// The usual ACR N, for when no N gives an exact CTS.
3030
uint32_t usual_n;
31-
// IEC 60958 channel status byte 3.
32-
uint8_t channel_status;
3331
} dvi_audio_rate_t;
3432

3533
// The basic audio rates every DVI sink takes.
3634
static const dvi_audio_rate_t dvi_audio_rates[] = {
37-
{ 32000, 4096, 0x03 },
38-
{ 44100, 6272, 0x00 },
39-
{ 48000, 6144, 0x02 },
35+
{ 32000, 4096 },
36+
{ 44100, 6272 },
37+
{ 48000, 6144 },
4038
};
4139
#define DVI_AUDIO_RATE_COUNT MP_ARRAY_SIZE(dvi_audio_rates)
4240
#define DVI_AUDIO_H_TOTAL (MODE_640_H_FRONT_PORCH + MODE_640_H_SYNC_WIDTH + MODE_640_H_BACK_PORCH + MODE_640_H_ACTIVE_PIXELS)
@@ -286,7 +284,7 @@ static void dvi_audio_init_packets(dvi_audio_state_t *audio) {
286284
// Frame position 4 never sets the block start flag, and frames 4 to 7
287285
// carry no channel status bits, so silence is the same at every rate.
288286
audio_sample_t silence[4] = {{0}};
289-
hstx_packet_set_audio_samples(&packet, silence, 4, 4, dvi_audio_rates[DVI_AUDIO_RATE_COUNT - 1].channel_status);
287+
hstx_packet_set_audio_samples(&packet, silence, 4, 4, dvi_audio_rates[DVI_AUDIO_RATE_COUNT - 1].sample_rate);
290288
for (int vsync = 0; vsync < 2; vsync++) {
291289
dvi_audio_encode(audio->silence[vsync], &packet, vsync);
292290
}
@@ -468,7 +466,7 @@ static const uint32_t *dvi_audio_next_packet(dvi_audio_state_t *audio, const int
468466
}
469467
hstx_packet_t packet;
470468
audio->channel_frame = hstx_packet_set_audio_samples(&packet, packet_samples, n, audio->channel_frame,
471-
dvi_audio_rates[audio->rate_index].channel_status);
469+
dvi_audio_rates[audio->rate_index].sample_rate);
472470
uint32_t *buffer = audio->pool[audio->pool_head];
473471
dvi_audio_encode(buffer, &packet, vsync);
474472
audio->pool_position[audio->pool_head] = audio->write_position;

‎ports/raspberrypi/common-hal/picodvi/dvi_audio_packet.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,22 @@ void hstx_packet_set_avi_infoframe(hstx_packet_t *packet, uint8_t vic, uint8_t p
183183
compute_all_parity(packet);
184184
}
185185

186+
// IEC 60958-3 channel status byte 3 sample frequency code.
187+
static uint8_t channel_status_sample_frequency(uint32_t sample_rate) {
188+
switch (sample_rate) {
189+
case 32000:
190+
return 0x03;
191+
case 44100:
192+
return 0x00;
193+
case 48000:
194+
return 0x02;
195+
default:
196+
return 0x01; // Sample rate not indicated.
197+
}
198+
}
199+
186200
int hstx_packet_set_audio_samples(hstx_packet_t *packet, const audio_sample_t *samples, int num_samples,
187-
int frame_count, uint8_t channel_status) {
201+
int frame_count, uint32_t sample_rate) {
188202
hstx_packet_init(packet);
189203
if (num_samples < 1) {
190204
num_samples = 1;
@@ -209,6 +223,7 @@ int hstx_packet_set_audio_samples(hstx_packet_t *packet, const audio_sample_t *s
209223
packet->header[2] = b_flags << 4;
210224
compute_header_parity(packet);
211225

226+
uint8_t sample_frequency = channel_status_sample_frequency(sample_rate);
212227
for (int i = 0; i < num_samples; i++) {
213228
uint8_t *d = packet->subpacket[i];
214229
int16_t left = samples[i].left;
@@ -221,10 +236,9 @@ int hstx_packet_set_audio_samples(hstx_packet_t *packet, const audio_sample_t *s
221236
d[4] = right & 0xFF;
222237
d[5] = (right >> 8) & 0xFF;
223238

224-
// IEC 60958 byte 0 = 0x04 (copy permitted), byte 3 = channel_status
225-
// (the sample rate).
239+
// IEC 60958 byte 0 = 0x04 (copy permitted), byte 3 = sample frequency.
226240
int frame = (frame_count + i) % 192;
227-
uint8_t status = frame / 8 == 0 ? 0x04 : (frame / 8 == 3 ? channel_status : 0);
241+
uint8_t status = frame / 8 == 0 ? 0x04 : (frame / 8 == 3 ? sample_frequency : 0);
228242
uint8_t c = (status >> (frame % 8)) & 1;
229243
bool p_left = compute_parity3(d[1], d[2], c);
230244
bool p_right = compute_parity3(d[4], d[5], c);

‎ports/raspberrypi/common-hal/picodvi/dvi_audio_packet.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ void hstx_packet_set_acr(hstx_packet_t *packet, uint32_t n, uint32_t cts);
3535
void hstx_packet_set_audio_infoframe(hstx_packet_t *packet, uint32_t sample_rate, uint8_t channels, uint8_t bits_per_sample);
3636
void hstx_packet_set_avi_infoframe(hstx_packet_t *packet, uint8_t vic, uint8_t pixel_repetition);
3737
int hstx_packet_set_audio_samples(hstx_packet_t *packet, const audio_sample_t *samples, int num_samples, int frame_count,
38-
uint8_t channel_status);
38+
uint32_t sample_rate);
3939
void hstx_encode_data_island(hstx_data_island_t *out, const hstx_packet_t *packet, bool vsync_active, bool hsync_active);

0 commit comments

Comments
 (0)