| 1 | /* |
| 2 | * aech0r.cpp |
| 3 | * |
| 4 | * This frei0r plugin aims to simulate a video echo with some colors tweaks. |
| 5 | * Version 0.1 sept 2020 |
| 6 | * |
| 7 | * Copyright (C) 2018-2020 d-j-a-y & vloop |
| 8 | * |
| 9 | * This source code is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This source code is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Please refer |
| 17 | * to the GNU Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Public License along |
| 20 | * with this source code; if not, write to: Free Software Foundation, |
| 21 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | */ |
| 24 | #include "frei0r.hpp" |
| 25 | #include "frei0r/math.h" |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <climits> |
| 29 | |
| 30 | ////// Uncomment to force non optimisation version |
| 31 | //~ #undef __SSE2__ |
| 32 | |
| 33 | ////// TODO / IDEAS / ... ////// |
| 34 | // IDEA - directionnal echo ? (X/Y like parameter ) |
| 35 | // TODO RGB gradient by fading influence need more love (See '//Fade by color layers')! |
| 36 | // FIXME SSE2 version doesn't support RGB fading influence ! |
| 37 | |
| 38 | // FIXME (Veejay specifics?) on activate/deactivate/activate/..., some buffers must be cleared! |
| 39 | |
| 40 | // EXPLORE ME - |
| 41 | //~ if((skip_count++)>m_skip) { |
| 42 | //~ - skip_count = 0; |
| 43 | //~ - m_factor += (factor * 64); |
| 44 | //~ [...] |
| 45 | //~ } |
| 46 | |
| 47 | // EXPLORE ME a very high m_factor value give some interesting color result ( m_factor += (factor * 64) * m_skip;) |
| 48 | |
| 49 | |
| 50 | /* Intrinsic declarations */ |
| 51 | #if defined(__SSE2__) |
| 52 | #include <emmintrin.h> |
| 53 | // TODO mmx support and others |
| 54 | // #elif defined(__MMX__) |
| 55 | // #include <mmintrin.h> |
| 56 | #endif |
| 57 | |
| 58 | #define SKIP_MAX_IMAGES 8 |
| 59 | #define M_FACTOR_MAV 127 |
| 60 | |
| 61 | union px_t { |
| 62 | uint32_t u; |
| 63 | unsigned char c[4]; // 0=B, 1=G,2=R,3=A ? i think :P |
| 64 | }; |
| 65 | |
| 66 | class aech0r : public frei0r::filter { |
| 67 | private: |
| 68 | f0r_param_double factor; |
| 69 | //~ f0r_param_double factor_r; //Fade by color layers |
| 70 | //~ f0r_param_double factor_g; |
| 71 | //~ f0r_param_double factor_b; |
| 72 | f0r_param_double strobe_period; |
| 73 | bool bright; |
| 74 | bool flag_r; |
| 75 | bool flag_g; |
| 76 | bool flag_b; |
| 77 | |
| 78 | |
| 79 | //~ f0r_param_double fade_rgb; //Fade by color layers |
| 80 | //~ f0r_param_double flag_rgb; |
| 81 | |
| 82 | unsigned int m_factor; |
| 83 | |
| 84 | //~ unsigned int m_factor_r; //Fade by color layers |
| 85 | //~ unsigned int m_factor_g; |
| 86 | //~ unsigned int m_factor_b; |
| 87 | |
| 88 | //~ unsigned int m_flag_rgb; //Fade by color layers |
| 89 | //~ unsigned int m_flag_r; |
| 90 | //~ unsigned int m_flag_g; |
| 91 | //~ unsigned int m_flag_b; |
| 92 | |
| 93 | unsigned int m_skip; |
| 94 | unsigned int m_skip_count; |
| 95 | |
| 96 | bool firsttime; |
| 97 | |
| 98 | //~ unsigned int m_rgb; //Fade by color layers |
| 99 | |
| 100 | #ifdef __SSE2__ |
| 101 | long long int m_factor_sse2; |
| 102 | |
| 103 | inline void tracesse2_add(uint32_t* out, const uint32_t* in); |
| 104 | inline void tracesse2_sub(uint32_t* out, const uint32_t* in); |
| 105 | #else |
| 106 | unsigned int m_factor_r; |
| 107 | unsigned int m_factor_g; |
| 108 | unsigned int m_factor_b; |
| 109 | |
| 110 | inline void trace_add(uint32_t* out, const uint32_t* in); |
| 111 | inline void trace_sub(uint32_t* out, const uint32_t* in); |
| 112 | #endif |
| 113 | public: |
| 114 | |
| 115 | aech0r(unsigned int width, unsigned int height) { |
| 116 | |
| 117 | factor = 0.15; // Quasi full echo has default |
| 118 | bright = false; // Dark mode has default |
| 119 | |
| 120 | flag_r = false; // No RGB flag has default |
| 121 | flag_g = false; |
| 122 | flag_b = false; |
| 123 | |
| 124 | strobe_period = 0; // No strobe has default |
| 125 | |
| 126 | //~ fade_rgb = ? //Fade by color layers |
| 127 | |
| 128 | firsttime = true; |
| 129 | m_skip_count = 0; |
| 130 | |
| 131 | register_param(factor, "Fade Factor", "Disappearance rate of the echo"); // 0 No fade, 1 No Trace |
| 132 | register_param(bright, "Direction", "Darker or Brighter echo"); // Add or Subtract data |
| 133 | register_param(flag_r, "Keep RED", "Influence on Red channel"); // 0 Fade canal, 1 Keep canal data |
| 134 | register_param(flag_g, "Keep GREEN", "Influence on Green channel"); // 0 Fade canal, 1 Keep canal data |
| 135 | register_param(flag_b, "Keep BLUE", "Influence on Blue channel"); // 0 Fade canal, 1 Keep canal data |
| 136 | register_param(strobe_period, "Strobe period", "Rate of the stroboscope: from 0 to 8 frames"); |
| 137 | |
| 138 | //~ register_param(fade_rgb, "Plans fade", "RGB"); //Fade by color layers |
| 139 | //~ register_param(factor_r, "Fade R", "influence"); // 0 No fade, 1 No Trace |
| 140 | //~ register_param(factor_g, "Fade G", "influence"); // 0 No fade, 1 No Trace |
| 141 | //~ register_param(factor_b, "Fade B", "influence"); // 0 No fade, 1 No Trace |
| 142 | //~ register_param(flag_rgb, "Plans comparison", "RGB"); |
| 143 | |
| 144 | } |
| 145 | ~aech0r() { |
| 146 | } |
| 147 | |
| 148 | virtual void update(double time, |
| 149 | uint32_t* out, |
| 150 | const uint32_t* in) { |
| 151 | |
| 152 | if (firsttime) { |
| 153 | memcpy(out, in, size * sizeof(uint32_t) ); // assuming we are RGBA only |
| 154 | firsttime = false; |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | m_skip = (strobe_period * SKIP_MAX_IMAGES); |
| 159 | if(m_skip_count++ < m_skip) { |
| 160 | return; |
| 161 | } |
| 162 | m_skip_count = 0; |
| 163 | |
| 164 | //~ m_factor = m_factor_sse2 = 0; //blink |
| 165 | |
| 166 | unsigned int bright_factor = (bright)? 0:UINT_MAX; |
| 167 | |
| 168 | //~ m_rgb = (fade_rgb * 8); //Fade by color layers |
| 169 | m_factor = (factor * M_FACTOR_MAV); //MAgic Value ;-) |
| 170 | |
| 171 | #ifdef __SSE2__ |
| 172 | // sse2 mask for fade operation |
| 173 | m_factor_sse2 = 0; |
| 174 | m_factor_sse2 = (flag_r==true)?(bright_factor << 24):(m_factor << 16); |
| 175 | m_factor_sse2 += (flag_g==true)?(bright_factor << 16):(m_factor << 8); |
| 176 | m_factor_sse2 += (flag_b==true)?(bright_factor << 8):(m_factor << 0); |
| 177 | #else |
| 178 | m_factor_r = (flag_r==true)?(bright_factor):(m_factor); |
| 179 | m_factor_g = (flag_g==true)?(bright_factor):(m_factor); |
| 180 | m_factor_b = (flag_b==true)?(bright_factor):(m_factor); |
| 181 | #endif |
| 182 | |
| 183 | //~ m_factor_r = m_factor * factor_r; //Fade by color layers |
| 184 | //~ m_factor_g = m_factor * factor_g; |
| 185 | //~ m_factor_b = m_factor * factor_b; |
| 186 | //~ m_flag_rgb=1+flag_rgb*6; |
| 187 | //~ m_flag_b = (m_flag_rgb & 1) == 1; |
| 188 | //~ m_flag_g = (m_flag_rgb & 2) == 2; |
| 189 | //~ m_flag_r = (m_flag_rgb & 4) == 4; |
| 190 | //~ m_factor_sse2 = (m_factor << 16) + (m_factor << 8) + m_factor ; |
| 191 | |
| 192 | if(bright) { |
| 193 | for(unsigned int i = 0 ; i < size ; i+=4) { |
| 194 | #ifdef __SSE2__ |
| 195 | tracesse2_sub(out+i, in+i); |
| 196 | #else |
| 197 | trace_sub(out+i, in+i); |
| 198 | trace_sub(out+i+1, in+i+1); |
| 199 | trace_sub(out+i+2, in+i+2); |
| 200 | trace_sub(out+i+3, in+i+3); |
| 201 | #endif |
| 202 | } |
| 203 | } else { |
| 204 | for(unsigned int i = 0 ; i < size ; i+=4) { |
| 205 | #ifdef __SSE2__ |
| 206 | tracesse2_add(out+i, in+i); |
| 207 | #else |
| 208 | trace_add(out+i, in+i); |
| 209 | trace_add(out+i+1, in+i+1); |
| 210 | trace_add(out+i+2, in+i+2); |
| 211 | trace_add(out+i+3, in+i+3); |
| 212 | #endif |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | #ifdef __SSE2__ |
| 220 | inline void aech0r::tracesse2_sub(uint32_t* out, const uint32_t* in) { |
| 221 | __m128i aa = _mm_load_si128((__m128i*)in); |
| 222 | __m128i bb = _mm_load_si128((__m128i*)out); |
| 223 | // set a fade (rgb) computation |
| 224 | __m128i ff = _mm_set1_epi32(m_factor_sse2); |
| 225 | bb = _mm_subs_epu8(bb, ff); |
| 226 | |
| 227 | // unsigned a < b |
| 228 | __m128i tmp = _mm_cmpeq_epi8( aa, _mm_min_epu8(aa, bb)); |
| 229 | |
| 230 | // create a bit mask |
| 231 | ff = _mm_cmpeq_epi32 (tmp, _mm_set1_epi8(0xff)); |
| 232 | |
| 233 | bb = _mm_or_si128 (_mm_andnot_si128 (ff, aa),_mm_and_si128 (bb,ff)); |
| 234 | _mm_store_si128((__m128i*)&out[0], bb); |
| 235 | } |
| 236 | |
| 237 | inline void aech0r::tracesse2_add(uint32_t* out, const uint32_t* in) { |
| 238 | __m128i aa = _mm_load_si128((__m128i*)in); |
| 239 | __m128i bb = _mm_load_si128((__m128i*)out); |
| 240 | |
| 241 | // set a fade (rgb) value |
| 242 | __m128i ff = _mm_set1_epi32(m_factor_sse2); |
| 243 | bb = _mm_adds_epu8(bb, ff); |
| 244 | |
| 245 | // unsigned a >= b |
| 246 | __m128i tmp = _mm_cmpeq_epi8( aa, _mm_max_epu8(aa, bb)); |
| 247 | |
| 248 | // create a bit mask |
| 249 | ff = _mm_cmpeq_epi32 (tmp, _mm_set1_epi8(0xff)); |
| 250 | |
| 251 | bb = _mm_or_si128 (_mm_andnot_si128 (ff, aa),_mm_and_si128 (bb,ff)); |
| 252 | _mm_store_si128((__m128i*)&out[0], bb); |
| 253 | } |
| 254 | |
| 255 | #else |
| 256 | |
| 257 | inline void aech0r::trace_sub(uint32_t* out, const uint32_t* in) { |
| 258 | |
| 259 | px_t po, pi; |
| 260 | po.u = *out; |
| 261 | pi.u = *in; |
| 262 | |
| 263 | //~ po.c[0]=(m_rgb & 4)?pi.c[0]:CLAMP0255(po.c[0] - m_factor_b);//Fade by color layers |
| 264 | //~ po.c[1]=(m_rgb & 2)?pi.c[1]:CLAMP0255(po.c[1] - m_factor_g); |
| 265 | //~ po.c[2]=(m_rgb & 1)?pi.c[2]:CLAMP0255(po.c[2] - m_factor_r); |
| 266 | //NOTA : BGR order come from Frei0r spec |
| 267 | po.c[0]=(CLAMP0255(po.c[0] - m_factor_b)); |
| 268 | po.c[1]=(CLAMP0255(po.c[1] - m_factor_g)); |
| 269 | po.c[2]=(CLAMP0255(po.c[2] - m_factor_r)); |
| 270 | *out = po.u; |
| 271 | if( (po.c[0]<=pi.c[0]) | |
| 272 | (po.c[1]<=pi.c[1]) | |
| 273 | (po.c[2]<=pi.c[2]) ) { |
| 274 | *out = pi.u; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | inline void aech0r::trace_add(uint32_t* out, const uint32_t* in) { |
| 279 | |
| 280 | px_t po, pi; |
| 281 | po.u = *out; |
| 282 | pi.u = *in; |
| 283 | |
| 284 | //NOTA : BGR order come from Frei0r spec |
| 285 | po.c[0] = CLAMP0255(po.c[0] + m_factor_b); |
| 286 | po.c[1] = CLAMP0255(po.c[1] + m_factor_g); |
| 287 | po.c[2] = CLAMP0255(po.c[2] + m_factor_r); |
| 288 | //~ po.c[0]=(m_rgb & 4)?pi.c[0]:CLAMP0255(po.c[0] + m_factor_b); //Fade by color layers |
| 289 | //~ po.c[1]=(m_rgb & 2)?pi.c[1]:CLAMP0255(po.c[1] + m_factor_g); |
| 290 | //~ po.c[2]=(m_rgb & 1)?pi.c[2]:CLAMP0255(po.c[2] + m_factor_r); |
| 291 | *out = po.u; |
| 292 | //~ if( ((po.c[0]>pi.c[0])&m_flag_b) | //Fade by color layers (why this test is here and not in trace_sub?) |
| 293 | //~ ((po.c[1]>pi.c[1])&m_flag_g) | |
| 294 | //~ ((po.c[2]>pi.c[2])&m_flag_r) ) { |
| 295 | if( ((po.c[0]>pi.c[0])) | |
| 296 | ((po.c[1]>pi.c[1])) | |
| 297 | ((po.c[2]>pi.c[2])) ) { |
| 298 | *out = pi.u; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | #endif // __SSE2__ |
| 303 | |
| 304 | frei0r::construct<aech0r> plugin("aech0r", |
| 305 | "analog video echo", |
| 306 | "d-j-a-y & vloop", |
| 307 | 0,1); |