4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bccmodels.h"
32 unsigned int EffectTV::fastrand_val = 0;
34 EffectTV::EffectTV(int w, int h)
38 background = (unsigned char*)new uint16_t[w * h];
39 diff = new unsigned char[w * h];
40 diff2 = new unsigned char[w * h];
52 int EffectTV::yuv_init()
56 for(i = 0; i < 256; i++)
58 YtoRGB[i] = (int)( 1.164 * (i - 16));
59 VtoR[i] = (int)( 1.596 * (i - 128));
60 VtoG[i] = (int)(-0.813 * (i - 128));
61 UtoG[i] = (int)(-0.391 * (i - 128));
62 UtoB[i] = (int)( 2.018 * (i - 128));
63 RtoY[i] = (int)( 0.257 * i);
64 RtoU[i] = (int)(-0.148 * i);
65 RtoV[i] = (int)( 0.439 * i);
66 GtoY[i] = (int)( 0.504 * i);
67 GtoU[i] = (int)(-0.291 * i);
68 GtoV[i] = (int)(-0.368 * i);
69 BtoY[i] = (int)( 0.098 * i);
70 BtoV[i] = (int)(-0.071 * i);
71 //printf("EffectTV::yuv_init %d %d %d\n", RtoY[i], GtoY[i], BtoY[i]);
77 #define FRAME_TO_EFFECTTV(type, components) \
79 for(int i = 0; i < width; i++) \
81 uint32_t *row = tmp + i * width * sizeof(uint32_t); \
82 type *input_row = ((type**)frame->get_rows())[i]; \
84 for(int j = 0; j < width; j++) \
86 if(sizeof(type) == 1) \
88 row[j * sizeof(uint32_t)] = ((uint32_t)input_row[0]) << 16; \
89 row[j * sizeof(uint32_t)] |= ((uint32_t)input_row[1]) << 8; \
90 row[j * sizeof(uint32_t)] |= input_row[2]; \
94 row[j * sizeof(uint32_t)] = ((uint32_t)input_row[0]) << 8; \
95 row[j * sizeof(uint32_t)] |= ((uint32_t)input_row[1]); \
96 row[j * sizeof(uint32_t)] |= input_row[2] >> 8; \
98 input_row += components; \
105 void EffectTV::frame_to_effecttv(VFrame *frame, uint32_t *tmp)
107 int width = frame->get_w();
108 //int height = frame->get_h();
110 switch(frame->get_color_model())
114 FRAME_TO_EFFECTTV(uint8_t, 3);
119 FRAME_TO_EFFECTTV(uint8_t, 4);
124 FRAME_TO_EFFECTTV(uint16_t, 3);
127 case BC_RGBA16161616:
128 case BC_YUVA16161616:
129 FRAME_TO_EFFECTTV(uint16_t, 4);
136 #define EFFECTTV_TO_FRAME(type, components, alpha) \
138 for(int i = 0; i < width; i++) \
140 uint32_t *row = tmp + i * width * sizeof(uint32_t); \
141 type *output_row = ((type**)frame->get_rows())[i]; \
143 for(int j = 0; j < width; j++) \
145 if(sizeof(type) == 1) \
147 output_row[0] = (row[j * sizeof(uint32_t)] & 0xff0000) >> 16; \
148 output_row[1] = (row[j * sizeof(uint32_t)] & 0xff00) >> 8; \
149 output_row[2] = row[j * sizeof(uint32_t)] & 0xff; \
150 if(components == 4) output_row[3] = alpha; \
154 output_row[0] = (row[j * sizeof(uint32_t)] & 0xff0000) >> 8; \
155 output_row[1] = row[j * sizeof(uint32_t)] & 0xff00; \
156 output_row[2] = (row[j * sizeof(uint32_t)] & 0xff) << 8; \
157 if(components == 4) output_row[3] = alpha; \
159 output_row += components; \
166 void EffectTV::effecttv_to_frame(VFrame *frame, uint32_t *tmp)
168 int width = frame->get_w();
169 //int height = frame->get_h();
171 switch(frame->get_color_model())
175 EFFECTTV_TO_FRAME(uint8_t, 3, 0xff);
180 EFFECTTV_TO_FRAME(uint8_t, 4, 0xff);
185 EFFECTTV_TO_FRAME(uint16_t, 3, 0xffff);
188 case BC_RGBA16161616:
189 case BC_YUVA16161616:
190 EFFECTTV_TO_FRAME(uint16_t, 4, 0xffff);
197 void EffectTV::image_set_threshold_y(int threshold)
199 y_threshold = threshold * 7; /* fake-Y value is timed by 7 */
204 /* Background image is refreshed every frame */
206 IMAGE_BGSUBTRACT_UPDATE_Y(type, components, is_yuv) \
215 q = (int16_t *)background; \
218 for(i = 0; i < h; i++) \
220 p = (type*)input_rows[i]; \
222 for(j = 0; j < w; j++) \
224 if(is_yuv && sizeof(type) == 2) \
226 R = G = B = (int)p[0] >> 8; \
231 if(is_yuv && sizeof(type) == 1) \
233 R = G = B = (int)p[0]; \
238 if(sizeof(type) == 4) \
240 R = (int)(p[0] * 0x1ff); \
241 G = (int)(p[1] * 0x3ff); \
242 B = (int)(p[2] * 0xff); \
243 CLAMP(R, 0, 0x1ff); \
244 CLAMP(G, 0, 0x3ff); \
248 if(sizeof(type) == 2) \
250 R = (int)p[0] >> (8 - 1); \
251 G = (int)p[1] >> (8 - 2); \
252 B = (int)p[2] >> 8; \
256 R = (int)p[0] << 1; \
257 G = (int)p[1] << 2; \
261 v = (R + G + B) - (int)(*q); \
262 *q = (int16_t)(R + G + B); \
263 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24); \
278 unsigned char* EffectTV::image_bgsubtract_update_y(unsigned char **input_rows,
279 unsigned char **output_rows,
285 IMAGE_BGSUBTRACT_UPDATE_Y(uint8_t, 3, 0);
288 IMAGE_BGSUBTRACT_UPDATE_Y(uint8_t, 3, 1);
291 IMAGE_BGSUBTRACT_UPDATE_Y(float, 3, 0);
294 IMAGE_BGSUBTRACT_UPDATE_Y(uint8_t, 4, 0);
297 IMAGE_BGSUBTRACT_UPDATE_Y(float, 4, 0);
300 IMAGE_BGSUBTRACT_UPDATE_Y(uint8_t, 4, 1);
303 IMAGE_BGSUBTRACT_UPDATE_Y(uint16_t, 3, 0);
306 IMAGE_BGSUBTRACT_UPDATE_Y(uint16_t, 3, 1);
308 case BC_RGBA16161616:
309 IMAGE_BGSUBTRACT_UPDATE_Y(uint16_t, 4, 0);
311 case BC_YUVA16161616:
312 IMAGE_BGSUBTRACT_UPDATE_Y(uint16_t, 4, 1);
324 #define IMAGE_BGSUBTRACT_Y(type, components, is_yuv) \
330 for(i = 0; i < h; i++) \
332 p = (type*)input_rows[i]; \
334 for(j = 0; j < w; j++) \
336 if(is_yuv && sizeof(type) == 2) \
338 R = G = B = (int)p[0] >> 8; \
343 if(is_yuv && sizeof(type) == 1) \
345 R = G = B = (int)p[0]; \
350 if(sizeof(type) == 4) \
352 R = (int)(p[0] * 0x1ff); \
353 G = (int)(p[1] * 0x3ff); \
354 B = (int)(p[2] * 0xff); \
355 CLAMP(R, 0, 0x1ff); \
356 CLAMP(G, 0, 0x3ff); \
360 if(sizeof(type) == 2) \
362 R = (int)p[0] >> (8 - 1); \
363 G = (int)p[1] >> (8 - 2); \
364 B = (int)p[2] >> 8; \
368 R = (int)p[0] << 1; \
369 G = (int)p[1] << 2; \
373 v = (R + G + B) - (int)(*q); \
374 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24); \
384 unsigned char* EffectTV::image_bgsubtract_y(unsigned char **input_rows,
391 q = (int16_t *)background;
399 IMAGE_BGSUBTRACT_Y(uint8_t, 3, 0);
402 IMAGE_BGSUBTRACT_Y(uint8_t, 3, 1);
405 IMAGE_BGSUBTRACT_Y(float, 3, 0);
408 IMAGE_BGSUBTRACT_Y(float, 4, 0);
411 IMAGE_BGSUBTRACT_Y(uint8_t, 4, 0);
414 IMAGE_BGSUBTRACT_Y(uint8_t, 4, 1);
417 IMAGE_BGSUBTRACT_Y(uint16_t, 3, 0);
420 IMAGE_BGSUBTRACT_Y(uint16_t, 3, 1);
422 case BC_RGBA16161616:
423 IMAGE_BGSUBTRACT_Y(uint16_t, 4, 0);
425 case BC_YUVA16161616:
426 IMAGE_BGSUBTRACT_Y(uint16_t, 4, 1);
437 /* The origin of subtraction function is;
438 * diff(src, dest) = (abs(src - dest) > threshold) ? 0xff : 0;
440 * This functions is transformed to;
441 * (threshold > (src - dest) > -threshold) ? 0 : 0xff;
443 * (v + threshold)>>24 is 0xff when v is less than -threshold.
444 * (v - threshold)>>24 is 0xff when v is less than threshold.
445 * So, ((v + threshold)>>24) | ((threshold - v)>>24) will become 0xff when
446 * abs(src - dest) > threshold.
455 unsigned char* EffectTV::image_diff_filter(unsigned char *diff)
458 unsigned char *src, *dest;
460 unsigned int sum1, sum2, sum3;
465 dest = diff2 + width + 1;
466 for(y = 1; y < height - 1; y++)
468 sum1 = src[0] + src[width] + src[width * 2];
469 sum2 = src[1] + src[width + 1] + src[width * 2 + 1];
472 for(x = 1; x < width - 1; x++)
474 sum3 = src[0] + src[width] + src[width * 2];
475 count = sum1 + sum2 + sum3;
478 *dest++ = (0xff * 3 - count) >> 24;
493 #define IMAGE_BGSET_Y(type, components, is_yuv) \
499 int width = frame->get_w(); \
500 int height = frame->get_h(); \
502 q = (int16_t *)background; \
506 for(i = 0; i < height; i++) \
508 p = (type*)frame->get_rows()[i]; \
510 for(j = 0; j < width; j++) \
512 if(is_yuv && sizeof(type) == 2) \
514 R = G = B = (int)p[0] >> 8; \
519 if(is_yuv && sizeof(type) == 1) \
521 R = G = B = (int)p[0]; \
526 if(sizeof(type) == 4) \
528 R = (int)(p[0] * 0x1ff); \
529 G = (int)(p[1] * 0x3ff); \
530 B = (int)(p[2] * 0xff); \
531 CLAMP(R, 0, 0x1ff); \
532 CLAMP(G, 0, 0x3ff); \
536 if(sizeof(type) == 2) \
538 R = (int)p[0] >> (8 - 1); \
539 G = (int)p[1] >> (8 - 2); \
540 B = (int)p[2] >> 8; \
544 R = (int)p[0] << 1; \
545 G = (int)p[1] << 2; \
549 *q = (int16_t)(R + G + B); \
562 void EffectTV::image_bgset_y(VFrame *frame)
564 switch(frame->get_color_model())
567 IMAGE_BGSET_Y(uint8_t, 3, 0);
570 IMAGE_BGSET_Y(float, 3, 0);
573 IMAGE_BGSET_Y(uint8_t, 3, 1);
576 IMAGE_BGSET_Y(uint8_t, 3, 0);
579 IMAGE_BGSET_Y(float, 3, 0);
582 IMAGE_BGSET_Y(uint8_t, 3, 1);
585 IMAGE_BGSET_Y(uint16_t, 3, 0);
588 IMAGE_BGSET_Y(uint16_t, 3, 1);
590 case BC_RGBA16161616:
591 IMAGE_BGSET_Y(uint16_t, 4, 0);
593 case BC_YUVA16161616:
594 IMAGE_BGSET_Y(uint16_t, 4, 1);