openMSX
Deflicker.cc
Go to the documentation of this file.
1#include "Deflicker.hh"
2#include "PixelOperations.hh"
3#include "RawFrame.hh"
4#include "vla.hh"
5#include "xrange.hh"
6#ifdef __SSE2__
7#include <emmintrin.h>
8#endif
9
10namespace openmsx {
11
12using Pixel = uint32_t;
13
14Deflicker::Deflicker(std::span<std::unique_ptr<RawFrame>, 4> lastFrames_)
15 : lastFrames(lastFrames_)
16{
17}
18
24
25unsigned Deflicker::getLineWidth(unsigned line) const
26{
27 return lastFrames[0]->getLineWidthDirect(line);
28}
29
30#ifdef __SSE2__
31static __m128i blend(__m128i x, __m128i y)
32{
33 // 32bpp
34 return _mm_avg_epu8(x, y);
35}
36
37static __m128i uload(const Pixel* ptr, ptrdiff_t byteOffst)
38{
39 const auto* p8 = reinterpret_cast<const char *>(ptr);
40 const auto* p128 = reinterpret_cast<const __m128i*>(p8 + byteOffst);
41 return _mm_loadu_si128(p128);
42}
43
44static void ustore(Pixel* ptr, ptrdiff_t byteOffst, __m128i val)
45{
46 auto* p8 = reinterpret_cast< char *>(ptr);
47 auto* p128 = reinterpret_cast<__m128i*>(p8 + byteOffst);
48 return _mm_storeu_si128(p128, val);
49}
50
51static __m128i compare(__m128i x, __m128i y)
52{
53 // 32bpp
54 return _mm_cmpeq_epi32(x, y);
55}
56#endif
57
59 unsigned line, unsigned& width, void* buf_, unsigned bufWidth) const
60{
61 unsigned width0 = lastFrames[0]->getLineWidthDirect(line);
62 unsigned width1 = lastFrames[1]->getLineWidthDirect(line);
63 unsigned width2 = lastFrames[2]->getLineWidthDirect(line);
64 unsigned width3 = lastFrames[3]->getLineWidthDirect(line);
65 const Pixel* line0 = lastFrames[0]->getLineDirect(line).data();
66 const Pixel* line1 = lastFrames[1]->getLineDirect(line).data();
67 const Pixel* line2 = lastFrames[2]->getLineDirect(line).data();
68 const Pixel* line3 = lastFrames[3]->getLineDirect(line).data();
69 if ((width0 != width3) || (width0 != width2) || (width0 != width1)) {
70 // Not all the same width.
71 width = width0;
72 return line0;
73 }
74
75 // Prefer to write directly to the output buffer, if that's not
76 // possible store the intermediate result in a temp buffer.
77 VLA_SSE_ALIGNED(Pixel, buf2, width0);
78 auto* buf = static_cast<Pixel*>(buf_);
79 Pixel* out = (width0 <= bufWidth) ? buf : buf2.data();
80
81 // Detect pixels that alternate between two different color values and
82 // replace those with the average color. We search for an alternating
83 // sequence with length (at least) 4. Or IOW we look for "A B A B".
84 // The implementation below also detects a constant pixel value
85 // "A A A A" as alternating between "A" and "A", but that's fine.
86 Pixel* dst = out;
87 size_t remaining = width0;
88#ifdef __SSE2__
89 size_t pixelsPerSSE = sizeof(__m128i) / sizeof(Pixel);
90 size_t widthSSE = remaining & ~(pixelsPerSSE - 1); // rounded down to a multiple of pixels in a SSE register
91 line0 += widthSSE;
92 line1 += widthSSE;
93 line2 += widthSSE;
94 line3 += widthSSE;
95 dst += widthSSE;
96 auto byteOffst = -ptrdiff_t(widthSSE * sizeof(Pixel));
97
98 while (byteOffst < 0) {
99 __m128i a0 = uload(line0, byteOffst);
100 __m128i a1 = uload(line1, byteOffst);
101 __m128i a2 = uload(line2, byteOffst);
102 __m128i a3 = uload(line3, byteOffst);
103
104 __m128i e02 = compare(a0, a2); // a0 == a2
105 __m128i e13 = compare(a1, a3); // a1 == a3
106 __m128i cnd = _mm_and_si128(e02, e13); // (a0==a2) && (a1==a3)
107
108 __m128i a01 = blend(a0, a1);
109 __m128i p = _mm_xor_si128(a0, a01);
110 __m128i q = _mm_and_si128(p, cnd);
111 __m128i r = _mm_xor_si128(q, a0); // select(a0, a01, cnd)
112
113 ustore(dst, byteOffst, r);
114 byteOffst += sizeof(__m128i);
115 }
116 remaining &= pixelsPerSSE - 1;
117#endif
118 PixelOperations pixelOps;
119 for (auto x : xrange(remaining)) {
120 dst[x] = ((line0[x] == line2[x]) && (line1[x] == line3[x]))
121 ? pixelOps.template blend<1, 1>(line0[x], line1[x])
122 : line0[x];
123 }
124
125 if (width0 <= bufWidth) {
126 // It it already fits, we're done
127 width = width0;
128 } else {
129 // Otherwise scale so that it does fit.
130 width = bufWidth;
131 scaleLine(std::span<const Pixel>{out, width0}, std::span{buf, bufWidth});
132 }
133 return buf;
134}
135
136} // namespace openmsx
const void * getLineInfo(unsigned line, unsigned &width, void *buf, unsigned bufWidth) const override
Abstract implementation of getLinePtr().
Definition Deflicker.cc:58
Deflicker(std::span< std::unique_ptr< RawFrame >, 4 > lastFrames)
Definition Deflicker.cc:14
unsigned getLineWidth(unsigned line) const override
Gets the number of display pixels on the given line.
Definition Deflicker.cc:25
void scaleLine(std::span< const Pixel > in, std::span< Pixel > out) const
void setHeight(unsigned height_)
void init(FieldType fieldType_)
(Re)initialize an existing FrameSource.
unsigned getHeight() const
Gets the number of lines in this frame.
@ FIELD_NONINTERLACED
Interlacing is off for this frame.
This file implemented 3 utility functions:
Definition Autofire.cc:9
CharacterConverter::Pixel Pixel
#define VLA_SSE_ALIGNED(TYPE, NAME, LENGTH)
Definition vla.hh:50
constexpr auto xrange(T e)
Definition xrange.hh:132