forked from ofZach/RTP_SFPC_SUMMER20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeOutput.h
More file actions
305 lines (272 loc) · 8.88 KB
/
CompositeOutput.h
File metadata and controls
305 lines (272 loc) · 8.88 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once
#include "driver/i2s.h"
typedef struct
{
float lineMicros;
float syncMicros;
float blankEndMicros;
float backMicros;
float shortVSyncMicros;
float overscanLeftMicros;
float overscanRightMicros;
float syncVolts;
float blankVolts;
float blackVolts;
float whiteVolts;
short lines;
short linesFirstTop;
short linesOverscanTop;
short linesOverscanBottom;
float imageAspect;
}TechProperties;
const TechProperties PALProperties = {
.lineMicros = 64,
.syncMicros = 4.7,
.blankEndMicros = 10.4,
.backMicros = 1.65,
.shortVSyncMicros = 2.35,
.overscanLeftMicros = 1.6875,
.overscanRightMicros = 1.6875,
.syncVolts = -0.3,
.blankVolts = 0.0,
.blackVolts = 0.005,//specs 0.0,
.whiteVolts = 0.7,
.lines = 625,
.linesFirstTop = 23,
.linesOverscanTop = 9,
.linesOverscanBottom = 9,
.imageAspect = 4./3.
};
const TechProperties NTSCProperties = {
.lineMicros = 63.492,
.syncMicros = 4.7,
.blankEndMicros = 9.2,
.backMicros = 1.5,
.shortVSyncMicros = 2.3,
.overscanLeftMicros = 0,//1.3,
.overscanRightMicros = 0,//1,
.syncVolts = -0.286,
.blankVolts = 0.0,
.blackVolts = 0.05, //specs 0.054,
.whiteVolts = 0.714,
.lines = 525,
.linesFirstTop = 20,
.linesOverscanTop = 6,
.linesOverscanBottom = 9,
.imageAspect = 4./3.
};
class CompositeOutput
{
public:
int samplesLine;
int samplesSync;
int samplesBlank;
int samplesBack;
int samplesActive;
int samplesBlackLeft;
int samplesBlackRight;
int samplesVSyncShort;
int samplesVSyncLong;
char levelSync;
char levelBlank;
char levelBlack;
char levelWhite;
char grayValues;
int targetXres;
int targetYres;
int targetYresEven;
int targetYresOdd;
int linesEven;
int linesOdd;
int linesEvenActive;
int linesOddActive;
int linesEvenVisible;
int linesOddVisible;
int linesEvenBlankTop;
int linesEvenBlankBottom;
int linesOddBlankTop;
int linesOddBlankBottom;
float pixelAspect;
unsigned short *line;
static const i2s_port_t I2S_PORT = (i2s_port_t)I2S_NUM_0;
enum Mode
{
PAL,
NTSC
};
const TechProperties &properties;
CompositeOutput(Mode mode, int xres, int yres, double Vcc = 3.3)
:properties((mode==NTSC) ? NTSCProperties: PALProperties)
{
int linesSyncTop = 5;
int linesSyncBottom = 3;
linesOdd = properties.lines / 2;
linesEven = properties.lines - linesOdd;
linesEvenActive = linesEven - properties.linesFirstTop - linesSyncBottom;
linesOddActive = linesOdd - properties.linesFirstTop - linesSyncBottom;
linesEvenVisible = linesEvenActive - properties.linesOverscanTop - properties.linesOverscanBottom;
linesOddVisible = linesOddActive - properties.linesOverscanTop - properties.linesOverscanBottom;
targetYresOdd = (yres / 2 < linesOddVisible) ? yres / 2 : linesOddVisible;
targetYresEven = (yres - targetYresOdd < linesEvenVisible) ? yres - targetYresOdd : linesEvenVisible;
targetYres = targetYresEven + targetYresOdd;
linesEvenBlankTop = properties.linesFirstTop - linesSyncTop + properties.linesOverscanTop + (linesEvenVisible - targetYresEven) / 2;
linesEvenBlankBottom = linesEven - linesEvenBlankTop - targetYresEven - linesSyncBottom;
linesOddBlankTop = linesEvenBlankTop;
linesOddBlankBottom = linesOdd - linesOddBlankTop - targetYresOdd - linesSyncBottom;
double samplesPerSecond = 160000000.0 / 3.0 / 2.0 / 2.0;
double samplesPerMicro = samplesPerSecond * 0.000001;
samplesLine = (int)(samplesPerMicro * properties.lineMicros + 1.5) & ~1;
samplesSync = samplesPerMicro * properties.syncMicros + 0.5;
samplesBlank = samplesPerMicro * (properties.blankEndMicros - properties.syncMicros + properties.overscanLeftMicros) + 0.5;
samplesBack = samplesPerMicro * (properties.backMicros + properties.overscanRightMicros) + 0.5;
samplesActive = samplesLine - samplesSync - samplesBlank - samplesBack;
targetXres = xres < samplesActive ? xres : samplesActive;
samplesVSyncShort = samplesPerMicro * properties.shortVSyncMicros + 0.5;
samplesBlackLeft = (samplesActive - targetXres) / 2;
samplesBlackRight = samplesActive - targetXres - samplesBlackLeft;
double dacPerVolt = 255.0 / Vcc;
levelSync = 0;
levelBlank = (properties.blankVolts - properties.syncVolts) * dacPerVolt + 0.5;
levelBlack = (properties.blackVolts - properties.syncVolts) * dacPerVolt + 0.5;
levelWhite = (properties.whiteVolts - properties.syncVolts) * dacPerVolt + 0.5;
grayValues = levelWhite - levelBlack + 1;
pixelAspect = (float(samplesActive) / (linesEvenVisible + linesOddVisible)) / properties.imageAspect;
}
void init()
{
line = (unsigned short*)malloc(sizeof(unsigned short) * samplesLine);
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = 1000000, //not really used
.bits_per_sample = (i2s_bits_per_sample_t)I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = samplesLine //a buffer per line
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL); //start i2s driver
i2s_set_pin(I2S_PORT, NULL); //use internal DAC
i2s_set_sample_rates(I2S_PORT, 1000000); //dummy sample rate, since the function fails at high values
//this is the hack that enables the highest sampling rate possible ~13MHz, have fun
SET_PERI_REG_BITS(I2S_CLKM_CONF_REG(0), I2S_CLKM_DIV_A_V, 1, I2S_CLKM_DIV_A_S);
SET_PERI_REG_BITS(I2S_CLKM_CONF_REG(0), I2S_CLKM_DIV_B_V, 1, I2S_CLKM_DIV_B_S);
SET_PERI_REG_BITS(I2S_CLKM_CONF_REG(0), I2S_CLKM_DIV_NUM_V, 2, I2S_CLKM_DIV_NUM_S);
SET_PERI_REG_BITS(I2S_SAMPLE_RATE_CONF_REG(0), I2S_TX_BCK_DIV_NUM_V, 2, I2S_TX_BCK_DIV_NUM_S);
}
void sendLine()
{
esp_err_t error = ESP_OK;
size_t bytes_written = 0;
size_t bytes_to_write = samplesLine * sizeof(unsigned short);
size_t cursor = 0;
while (error == ESP_OK && bytes_to_write > 0) {
error = i2s_write(I2S_PORT, (const char *)line + cursor, bytes_to_write, &bytes_written, portMAX_DELAY);
bytes_to_write -= bytes_written;
cursor += bytes_written;
}
}
inline void fillValues(int &i, unsigned char value, int count)
{
for(int j = 0; j < count; j++)
line[i++^1] = value << 8;
}
void fillLine(char *pixels)
{
int i = 0;
fillValues(i, levelSync, samplesSync);
fillValues(i, levelBlank, samplesBlank);
fillValues(i, levelBlack, samplesBlackLeft);
for(int x = 0; x < targetXres / 2; x++)
{
short pix = (levelBlack + pixels[x]) << 8;
line[i++^1] = pix;
line[i++^1] = pix;
}
fillValues(i, levelBlack, samplesBlackRight);
fillValues(i, levelBlank, samplesBack);
}
void fillLong(int &i)
{
fillValues(i, levelSync, samplesLine / 2 - samplesVSyncShort);
fillValues(i, levelBlank, samplesVSyncShort);
}
void fillShort(int &i)
{
fillValues(i, levelSync, samplesVSyncShort);
fillValues(i, levelBlank, samplesLine / 2 - samplesVSyncShort);
}
void fillBlank()
{
int i = 0;
fillValues(i, levelSync, samplesSync);
fillValues(i, levelBlank, samplesBlank);
fillValues(i, levelBlack, samplesActive);
fillValues(i, levelBlank, samplesBack);
}
void fillHalfBlank(int &i)
{
fillValues(i, levelSync, samplesSync);
fillValues(i, levelBlank, samplesLine / 2 - samplesSync);
}
void sendFrameHalfResolution(char ***frame)
{
//Even Halfframe
int i = 0;
fillLong(i); fillLong(i);
sendLine(); sendLine();
i = 0;
fillLong(i); fillShort(i);
sendLine();
i = 0;
fillShort(i); fillShort(i);
sendLine(); sendLine();
fillBlank();
for(int y = 0; y < linesEvenBlankTop; y++)
sendLine();
for(int y = 0; y < targetYresEven; y++)
{
char *pixels = (*frame)[y];
fillLine(pixels);
sendLine();
}
fillBlank();
for(int y = 0; y < linesEvenBlankBottom; y++)
sendLine();
i = 0;
fillShort(i); fillShort(i);
sendLine(); sendLine();
i = 0;
fillShort(i);
//odd half frame
fillLong(i);
sendLine();
i = 0;
fillLong(i); fillLong(i);
sendLine(); sendLine();
i = 0;
fillShort(i); fillShort(i);
sendLine(); sendLine();
i = 0;
fillShort(i); fillValues(i, levelBlank, samplesLine / 2);
sendLine();
fillBlank();
for(int y = 0; y < linesOddBlankTop; y++)
sendLine();
for(int y = 0; y < targetYresOdd; y++)
{
char *pixels = (*frame)[y];
fillLine(pixels);
sendLine();
}
fillBlank();
for(int y = 0; y < linesOddBlankBottom; y++)
sendLine();
i = 0;
fillHalfBlank(i); fillShort(i);
sendLine();
i = 0;
fillShort(i); fillShort(i);
sendLine(); sendLine();
}
};