-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspdif-encoder.c
157 lines (142 loc) · 3.46 KB
/
spdif-encoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* SPDIF encoder
*
* Copyright (C) 2015, 2023 Stephan "Kiffie" <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program 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
* General Public License for more details.
*/
#include "spdif-encoder.h"
#include <linux/string.h>
static uint16_t spdif_biphase_encode(bool last, uint8_t data){
int i;
uint16_t result=0;
for( i=0; i< 8; i++){
result<<=2;
if( data&1 ){
result|= 0b10;
}else{
result|= 0b11;
}
if( last ){
result^= 0b11;
}
last= result&1;
data>>=1;
}
return result;
}
static uint16_t spdif_preamble_encode(bool last, uint8_t data){
uint16_t result=0;
int i;
switch( data & SPDIF_PREAMBLE_MASK ){
case SPDIF_PREAMBLE_X:
result= 0b11100010;
break;
case SPDIF_PREAMBLE_Y:
result= 0b11100100;
break;
case SPDIF_PREAMBLE_Z:
result= 0b11101000;
break;
}
if( last ){
result^= 0xff;
}
last= result&1;
data>>=4;
for(i=0; i<4; i++){
result<<=2;
if( data&1 ){
result|= 0b10;
}else{
result|= 0b11;
}
if( last ){
result^= 0b11;
}
last= result&1;
data>>=1;
}
return result;
}
void spdif_fast_encode(struct spdif_encoder *spdif,
void *encoded_buf, uint32_t subframe)
{
uint32_t parity;
uint32_t *encoded = (uint32_t *)encoded_buf;
bool last;
uint32_t data0, data1;
/* add channel status bit */
if((spdif->channel_status[spdif->frame_ctr / 8] >> (spdif->frame_ctr % 8)) & 0x01)
{
subframe |= SPDIF_C_MASK;
}
parity = subframe & ~SPDIF_PREAMBLE_MASK; /* exclude preamble bits */
parity ^= parity >> 16; /* slightly faster than calling __builtin_parity() */
parity ^= parity >> 8;
parity ^= parity >> 4;
parity &= 0xf;
parity = 0x69960000 << parity;
parity &= 0x80000000;
subframe |= parity;
last = spdif->last;
data1 = spdif->first_byte[subframe & 0xff];
if(last){
data1^= 0xffff;
}
last = data1&1;
data0 = spdif->byte[(subframe >> 8) & 0xff];
if(last){
data0^= 0xffff;
}
last= data0&1;
encoded[0] = data1 << 16 | data0;
data1 = spdif->byte[(subframe >> 16) & 0xff];
if(last){
data1^= 0xffff;
}
last = data1&1;
data0 = spdif->byte[(subframe >> 24) & 0xff];
if(last){
data0^= 0xffff;
}
spdif->last= data0&1;
encoded[1] = data1 << 16 | data0;
}
void spdif_encode_frame_generic(struct spdif_encoder *spdif,
void *encoded,
int left_shifted, int right_shifted)
{
uint8_t *p= encoded;
uint32_t subframe;
subframe= spdif->frame_ctr == 0 ? SPDIF_PREAMBLE_Z : SPDIF_PREAMBLE_X;
subframe|= left_shifted;
spdif_fast_encode(spdif, p, subframe);
p+= SPDIF_FRAMESIZE/2;
subframe= SPDIF_PREAMBLE_Y | right_shifted;
spdif_fast_encode(spdif, p, subframe);
if( ++spdif->frame_ctr >= SPDIF_BLOCKSIZE ){
spdif->frame_ctr= 0;
}
}
void spdif_encoder_init(struct spdif_encoder *spdif){
int i;
for(i=0; i< 256; i++){
spdif->first_byte[i]= spdif_preamble_encode(0, i);
spdif->byte[i]= spdif_biphase_encode(0, i);
}
spdif_encoder_set_channel_status(spdif, NULL, 0);
}
void spdif_encoder_set_channel_status(struct spdif_encoder *spdif,
const void *cs, size_t len)
{
memset(spdif->channel_status, 0, SPDIF_CHSTATSIZE);
memcpy(spdif->channel_status, cs, len <= SPDIF_CHSTATSIZE ? len : SPDIF_CHSTATSIZE);
}