-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo.cpp
518 lines (432 loc) · 13.6 KB
/
video.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* author : Shuichi TAKANO
* since : Mon Aug 12 2019 12:22:58
*/
#include "video.h"
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include "worker.h"
#include "chrono.h"
namespace
{
void setValue(std::vector<uint32_t> &v, int idx, int value)
{
((uint8_t *)&v[idx >> 2])[(idx & 3) ^ 3] = value;
}
} // namespace
void NTSCEncoder::makeSinTable(float signalScale)
{
for (int i = 0; i < SIN_TABLE_SIZE; ++i)
{
float t = i * (3.14159265359f * 2 / SIN_TABLE_SIZE);
sinTable_[i] = {
int8_t(sinf(t) * 128 / 2.03f * signalScale),
int8_t(cosf(t) * 128 / 1.14f * signalScale)};
}
}
void NTSCEncoder::makeLineSinTable()
{
lineSinTable_.clear();
lineSinTable_.reserve(width_ * 2);
for (int i = 0; i < 2; ++i)
{
auto ph = phaseStep24_ * (hStart_ - phaseOrigin_) + getSCPhase(i & 1);
for (int x = 0; x < width_; ++x)
{
const auto &sc = sinTable_[(ph >> (24 - SIN_TABLE_BITS)) & (SIN_TABLE_SIZE - 1)];
lineSinTable_.push_back(sc);
ph += phaseStep24_;
}
}
}
void NTSCEncoder::setSize(int w, int h)
{
width_ = w;
height_ = h;
vStart_ = ((242 - height_) >> 1) + 11;
hStart_ = (((videoHSamples_ - width_) >> 1) + baseHOfs_ + 2) & ~3;
printf("(%d x %d) ofs %d, %d\n", w, h, vStart_, hStart_);
makeLineSinTable();
}
void NTSCEncoder::init(uint32_t freq, int targetNumSCPerLine2)
{
freq_ = freq;
auto hCycle = (targetNumSCPerLine2 / (2.0f * 3579545)) * freq;
signalStride_ = int(hCycle * 0.25 + 0.5) * 4;
printf("signal freq %d, stride %d\n", freq, signalStride_);
printf("line sc cycle %f\n", (double)signalStride_ / freq * 3579545);
signalStrideInDW_ = signalStride_ >> 2;
halfLineSignalSizeInDW_ = signalStride_ >> 3;
// 同期信号 -40IRE
// カラーバースト幅 40IRE
// 白レベル 100IRE
// ピーク 133IRE
auto colorBurstAmp = 256 * 20 / (133 + 40);
pedestalLevel_ = 256 * 40 / (133 + 40);
setupLevel_ = pedestalLevel_;
float signalScale = 100.0f / (133 + 40) * 0.75f; // 75%
signalScale128_ = signalScale * 128.0f;
makeSinTable(signalScale);
auto iv = pedestalLevel_ * 0x01010101;
vsync_[0].resize(9 * signalStrideInDW_, iv);
vsync_[1].resize(10 * signalStrideInDW_ - halfLineSignalSizeInDW_, iv);
const int signalBufferLines = 255;
video_.resize(signalStrideInDW_ * signalBufferLines, iv);
printf("vsync %zd+%zdbytes, video %zdbytes.\n", vsync_[0].size() * 4, vsync_[1].size() * 4, video_.size() * 4);
// 信号全体をフロントポーチ(1.5us)分オフセットして考える
// 水平同期4.7us
// 水平同期からカラーバーストまで19sc
// カラーバースト9sc
// 水平同期からセットアップまで9.4us
// 1ライン63.55556us=227.5sc
// 映像期間63.6-10.9=52.7us
// 等価パルス幅2.35us
// 切り込みパルス幅4.7us
baseHOfs_ = 9.4e-6 * freq_;
videoHSamples_ = 52.7e-6 * freq_;
int hsyncPulseWidth = 4.7e-6 * freq_;
int equivPulseWidth = hsyncPulseWidth >> 1;
int invSerratioPulseWidth = (signalStride_ >> 1) - hsyncPulseWidth;
auto makePulse = [&](auto &v, int ofs, int w) {
for (int i = 0; i < w; ++i)
{
setValue(v, i + ofs, 0);
}
};
auto makeVSync = [&](auto &v, int ofs) {
for (int i = 0; i < 6; ++i)
{
makePulse(v, ofs + (signalStride_ * i >> 1), equivPulseWidth);
makePulse(v, ofs + (signalStride_ * (i + 12) >> 1), equivPulseWidth);
}
for (int i = 0; i < 6; ++i)
{
makePulse(v, ofs + (signalStride_ * (i + 6) >> 1), invSerratioPulseWidth);
}
};
makeVSync(vsync_[0], 0);
makeVSync(vsync_[1], (signalStride_ >> 1) - halfLineSignalSizeInDW_ * 4);
#if 0
// ライン長からPLL想定
freqSC_ = 227.5 * freq / signalStride_;
#else
freqSC_ = 3579545;
#endif
phaseStep24_ = 16777216.0 * freqSC_ / freq;
phaseOffsetPerLine_ = targetNumSCPerLine2 & 1 ? 16777216 / 2 : 0;
int colorBurstStart = (19.0f / freqSC_) * freq + 0.5f;
int colorBurstWidth = (28.0f / freqSC_) * freq - colorBurstStart;
// auto colorBurstStartPhase24 = phaseStep24_ * colorBurstStart;
phaseOrigin_ = colorBurstStart;
// phaseOrigin_ = 0;
printf("colorBurst %d, %d\n", colorBurstStart, colorBurstWidth);
auto makeHSync = [&](int ofs, bool phase) {
makePulse(video_, ofs, hsyncPulseWidth);
ofs += colorBurstStart;
// auto ph = colorBurstStartPhase24 + getSCPhase(phase);
auto ph = getSCPhase(phase); // カラーバーストを位相の起点にする
//auto ph = phaseStep24_ * ofs;
for (int i = 0; i < colorBurstWidth; ++i)
{
float phase = (ph & 16777215) * (3.14159265f * 2 / 16777216.0f);
int value = -sinf(phase) * colorBurstAmp;
setValue(video_, ofs, value + pedestalLevel_);
++ofs;
ph += phaseStep24_;
}
};
for (int i = 0; i < signalBufferLines; ++i)
{
makeHSync(i * signalStride_, i & 1);
}
}
void NTSCEncoder::makeColorBar()
{
// 75% white, yellow, cyan, green, megenta, red, blue, black
auto makeLine = [&](int ofs, bool odd) {
ofs += hStart_;
auto ph = phaseStep24_ * (hStart_ - phaseOrigin_) + getSCPhase(odd);
//auto ph = phaseStep24_ * ofs;
auto unitWidth = width_ >> 3;
uint8_t col[] = {7, 3, 6, 2, 5, 1, 4, 0};
for (int i = 0; i < 8; ++i)
{
auto cm = col[i];
float r = cm & 1 ? signalScale128_ * 2 : 0;
float g = cm & 2 ? signalScale128_ * 2 : 0;
float b = cm & 4 ? signalScale128_ * 2 : 0;
for (int j = 0; j < unitWidth; ++j)
{
float phase = (ph & 16777215) * (3.14159265f * 2 / 16777216.0f);
float sp = sinf(phase);
float cp = cosf(phase);
// Y = 0.587G+0.114B+0.299R
// Cb = B-Y = -0.587G+0.886B-0.299R
// Cr = R-Y = -0.587G-0.114B+0.701R
// Cb' = Cb/2.03
// Cr' = Cr/1.14
// NTSC = Y + Cb' sin(2 pi f t) +Cr' cos(2 pi f t)
float y = 0.587f * g + 0.114f * b + 0.299 * r;
float cb = (b - y) * (1 / 2.03f);
float cr = (r - y) * (1 / 1.14f);
int v = y + cb * sp + cr * cp;
setValue(video_, ofs, v + pedestalLevel_);
ofs += 1;
ph += phaseStep24_;
}
}
};
int ofs = 0;
for (int i = 0; i < 255; ++i)
{
makeLine(ofs, i & 1);
ofs += signalStride_;
}
}
void NTSCEncoder::makeColotLUT(int rBits, int gBits, int bBits,
int rShift, int gShift, int bShift)
{
int bpp = rBits + gBits + bBits;
int ct = 1 << bpp;
colorLUT_.resize(ct);
int rMax = 1 << rBits;
int gMax = 1 << gBits;
int bMax = 1 << bBits;
int rMask = (rMax - 1) << rShift;
int gMask = (gMax - 1) << gShift;
int bMask = (bMax - 1) << bShift;
float rNorm = 1.0f / rMask;
float gNorm = 1.0f / gMask;
float bNorm = 1.0f / bMask;
float yscale = signalScale128_ * (1 << 7);
float ybias = pedestalLevel_ << 6;
for (int i = 0; i < ct; ++i)
{
float r = (i & rMask) * rNorm;
float g = (i & gMask) * gNorm;
float b = (i & bMask) * bNorm;
float y = 0.299f * r + 0.587f * g + 0.114f * b;
float cb = b - y;
float cr = r - y;
colorLUT_[i] = DiffColor{(uint16_t)(y * yscale + ybias),
(int8_t)(cb * (1 << 7)),
(int8_t)(cr * (1 << 7))};
}
}
#if 1
void NTSCEncoder::setLinex2(int w, int line, const uint16_t *img)
{
line += vStart_;
int ofs = hStart_ + line * signalStride_;
assert((ofs & 3) == 0);
auto *p = (uint8_t *)&video_[ofs >> 2];
auto *sct = getLineSinTable(line);
auto ct = w;
while (ct > 0)
{
auto c1 = colorLUT_[img[0]];
auto c2 = colorLUT_[img[1]];
int v0 = c1.compute(sct[0]);
int v1 = c1.compute(sct[1]);
int v2 = c2.compute(sct[2]);
int v3 = c2.compute(sct[3]);
p[0] = v3;
p[1] = v2;
p[2] = v1;
p[3] = v0;
p += 4;
sct += 4;
img += 2;
ct -= 2;
}
}
void NTSCEncoder::setLinex4(int w, int line, const uint16_t *img)
{
line += vStart_;
int ofs = hStart_ + line * signalStride_;
assert((ofs & 3) == 0);
auto *p = (uint8_t *)&video_[ofs >> 2];
auto *sct = getLineSinTable(line);
auto ct = w;
while (ct > 0)
{
auto c = colorLUT_[img[0]];
int v0 = c.compute(sct[0]);
int v1 = c.compute(sct[1]);
int v2 = c.compute(sct[2]);
int v3 = c.compute(sct[3]);
p[0] = v3;
p[1] = v2;
p[2] = v1;
p[3] = v0;
p += 4;
sct += 4;
img += 1;
ct -= 1;
}
}
#else
namespace
{
struct YCbCr
{
int y; // 8:20
int cb; // 8:13
int cr; // 8:13
inline YCbCr(uint16_t src, int ss128)
{
static constexpr int rcoef = int(0.299f * 2048) << 5;
static constexpr int gcoef = int(0.587f * 1024);
static constexpr int bcoef = int(0.114f * 2048) << 5;
int r = src >> 11;
int g = src & (63 << 5);
int b = src & 31;
y = rcoef * r + gcoef * g + bcoef * b;
cb = (b << 16) - y;
cr = (r << 16) - y;
y *= ss128;
}
inline int compute(const NTSCEncoder::SinCos &sc) const
{
return (y + cb * sc.sin_ + cr * sc.cos_) >> 20;
}
};
} // namespace
void NTSCEncoder::setLinex2(int w, int line, const uint16_t *img)
{
line += vStart_;
int ofs = hStart_ + line * signalStride_;
assert((ofs & 3) == 0);
auto *p = (uint8_t *)&video_[ofs >> 2];
auto *sct = getLineSinTable(line);
auto ct = w;
while (ct > 0)
{
YCbCr c1(img[0], signalScale128_);
YCbCr c2(img[1], signalScale128_);
int v0 = c1.compute(sct[0]) + pedestalLevel_;
int v1 = c1.compute(sct[1]) + pedestalLevel_;
int v2 = c2.compute(sct[2]) + pedestalLevel_;
int v3 = c2.compute(sct[3]) + pedestalLevel_;
p[0] = v3;
p[1] = v2;
p[2] = v1;
p[3] = v0;
p += 4;
sct += 4;
img += 2;
ct -= 2;
}
}
void NTSCEncoder::setLinex4(int w, int line, const uint16_t *img)
{
line += vStart_;
int ofs = hStart_ + line * signalStride_;
assert((ofs & 3) == 0);
auto *p = &video_[ofs >> 2];
auto *sct = getLineSinTable(line);
auto ct = w;
while (ct)
{
YCbCr c(*img, signalScale128_);
int v0 = c.compute(sct[0]) + pedestalLevel_;
int v1 = c.compute(sct[1]) + pedestalLevel_;
int v2 = c.compute(sct[2]) + pedestalLevel_;
int v3 = c.compute(sct[3]) + pedestalLevel_;
*p = (v0 << 24) | (v1 << 16) | (v2 << 8) | v3;
++p;
sct += 4;
++img;
--ct;
}
}
#endif
#if 0
namespace
{
using WorkerFunc = void (*)(void *);
void *volatile workerParam_ = nullptr;
volatile WorkerFunc workerFunc_ = nullptr;
} // namespace
int worker(void *)
{
while (1)
{
if (workerFunc_)
{
workerFunc_(workerParam_);
workerFunc_ = nullptr;
}
}
}
void setWorkload(WorkerFunc f, void *param)
{
while (workerFunc_)
;
workerParam_ = param;
workerFunc_ = f;
}
void startWorker()
{
register_core1(worker, nullptr);
}
#endif
namespace
{
template <class F>
void setImageImpl(int w, int h, int pitch, const uint16_t *img, int lineOffset, const F &lineFunc)
{
#if 0
int line = lineOffset;
for (int i = 0; i < h; ++i)
{
setLinex4(w, line, img);
++line;
img += pitch;
}
#else
struct Param
{
const F &func_;
int w, h, pitch, lineOffset;
const uint16_t *img;
volatile bool done = false;
int proc()
{
// auto prevClk = getClockCounter();
int line = lineOffset;
for (int i = 0; i < h; i += 2)
{
func_(w, line, img);
line += 2;
img += pitch * 2;
}
done = true;
// auto dt = clockToMicroSec(getClockCounter() - prevClk);
// printf("core %d: %dus\n", (int)current_coreid(), dt);
return 0;
}
};
auto p0 = Param{lineFunc, w, h, pitch, lineOffset + 0, img + pitch * 0};
auto p1 = Param{lineFunc, w, h, pitch, lineOffset + 1, img + pitch * 1};
// setWorkload([](void *p) { ((Param *)p)->proc(); }, &p1);
addWorkload([&] { p1.proc(); });
p0.proc();
while (!p1.done)
;
#endif
}
} // namespace
void NTSCEncoder::setImagex2(int w, int h, int pitch, const uint16_t *img,
int lineOffset)
{
setImageImpl(w, h, pitch, img, lineOffset,
[&](int w, int line, const uint16_t *img) { setLinex2(w, line, img); });
}
void NTSCEncoder::setImagex4(int w, int h, int pitch, const uint16_t *img,
int lineOffset)
{
setImageImpl(w, h, pitch, img, lineOffset,
[&](int w, int line, const uint16_t *img) { setLinex4(w, line, img); });
}