Debian Codegraph @

src/filter/vectorscope/vectorscope.c

raw symbols SCIP
1 /* vectorscope.c
2 * Copyright (C) 2008 Albert Frisch (albert.frisch AT gmail.com)
3 * Copyright (C) 2008 Richard Spindler (richard.spindler AT gmail.com)
4 * This file is a Frei0r plugin.
5 *
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.
10 *
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.
15 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdlib.h>
22 #include <math.h>
23 #include <assert.h>
24 #include "frei0r.h"
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <gavl/gavl.h>
29
30 #include "vectorscope_image.h"
31
32 #define OFFSET_R 0
33 #define OFFSET_G 8
34 #define OFFSET_B 16
35 #define OFFSET_A 24
36
37 #define SCOPE_WIDTH 256
38 #define SCOPE_HEIGHT 256
39
40 /* c99 seems to be extra clever, and removes the definition of M_PI,
41 * this adds it again */
42 #ifndef M_PI
43 #define M_PI 3.14159265358979323846
44 #endif
45
46 typedef struct {
47 double Y, Cb, Cr;
48 } YCbCr_t;
49
50 typedef struct {
51 double red, green, blue;
52 } rgb_t;
53
54 typedef struct vectorscope_instance {
55 int w, h;
56 unsigned char* scala;
57 gavl_video_scaler_t* scope_scaler;
58 gavl_video_frame_t* scope_frame_src;
59 gavl_video_frame_t* scope_frame_dst;
60 double mix;
61 double overlay_sides;
62 } vectorscope_instance_t;
63
64 int f0r_init()
65 {
66 return 1;
67 }
68 void f0r_deinit()
69 { /* empty */ }
70
71 void f0r_get_plugin_info( f0r_plugin_info_t* info )
72 {
73 info->name = "Vectorscope";
74 info->author = "Albert Frisch";
75 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
76 info->color_model = F0R_COLOR_MODEL_RGBA8888;
77 info->frei0r_version = FREI0R_MAJOR_VERSION;
78 info->major_version = 0;
79 info->minor_version = 2;
80 info->num_params = 2;
81 info->explanation = "Displays the vectorscope of the video-data";
82 }
83
84 void f0r_get_param_info( f0r_param_info_t* info, int param_index )
85 {
86 switch(param_index)
87 {
88 case 0:
89 info->name = "mix";
90 info->type = F0R_PARAM_DOUBLE;
91 info->explanation = "The amount of source image mixed into background of display";
92 break;
93 case 1:
94 info->name = "overlay sides";
95 info->type = F0R_PARAM_BOOL;
96 info->explanation = "If false, the sides of image are shown without overlay";
97 break;
98 }
99 }
100
101 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
102 {
103 vectorscope_instance_t* inst = (vectorscope_instance_t*)calloc(1, sizeof(*inst));
104 inst->w = width;
105 inst->h = height;
106 if ( width == 0 || height == 0 ) {
107 free(inst);
108 return NULL;
109 }
110
111 inst->mix = 0.0;
112 inst->overlay_sides = 1.0;
113
114 inst->scala = (unsigned char*)malloc( width * height * 4 );
115
116 gavl_video_scaler_t* video_scaler;
117 gavl_video_frame_t* frame_src;
118 gavl_video_frame_t* frame_dst;
119
120 video_scaler = gavl_video_scaler_create();
121 frame_src = gavl_video_frame_create( 0 );
122 frame_dst = gavl_video_frame_create( 0 );
123 frame_dst->strides[0] = width * 4;
124 frame_src->strides[0] = vectorscope_image.width * 4;
125
126 gavl_video_options_t* options = gavl_video_scaler_get_options( video_scaler );
127 gavl_video_format_t format_src;
128 gavl_video_format_t format_dst;
129
130 memset(&format_src, 0, sizeof(format_src));
131 memset(&format_dst, 0, sizeof(format_dst));
132
133 format_dst.frame_width = inst->w;
134 format_dst.frame_height = inst->h;
135 format_dst.image_width = inst->w;
136 format_dst.image_height = inst->h;
137 format_dst.pixel_width = 1;
138 format_dst.pixel_height = 1;
139 format_dst.pixelformat = GAVL_RGBA_32;
140 format_dst.interlace_mode = GAVL_INTERLACE_NONE;
141
142 format_src.frame_width = vectorscope_image.width;
143 format_src.frame_height = vectorscope_image.height;
144 format_src.image_width = vectorscope_image.width;
145 format_src.image_height = vectorscope_image.height;
146 format_src.pixel_width = 1;
147 format_src.pixel_height = 1;
148 format_src.pixelformat = GAVL_RGBA_32;
149 format_src.interlace_mode = GAVL_INTERLACE_NONE;
150
151 gavl_rectangle_f_t src_rect;
152 gavl_rectangle_i_t dst_rect;
153
154 src_rect.x = 0;
155 src_rect.y = 0;
156 src_rect.w = vectorscope_image.width;
157 src_rect.h = vectorscope_image.height;
158
159 float dst_x, dst_y, dst_w, dst_h;
160 if ( (float)inst->w / inst->h > (float)vectorscope_image.width / vectorscope_image.height ) {
161 dst_y = 0;
162 dst_h = inst->h;
163 dst_w = ((float)vectorscope_image.width / vectorscope_image.height) * inst->h;
164 dst_x = ( inst->w - dst_w ) / 2.0;
165 } else {
166 dst_x = 0;
167 dst_w = inst->w;
168 dst_h = ((float)vectorscope_image.height / vectorscope_image.width) * inst->w;
169 dst_y = ( inst->h - dst_h ) / 2.0;
170 }
171 dst_rect.x = (int)(dst_x);
172 dst_rect.y = (int)(dst_y);
173 dst_rect.w = (int)(dst_w);
174 dst_rect.h = (int)(dst_h);
175
176 gavl_video_options_set_rectangles( options, &src_rect, &dst_rect );
177 gavl_video_scaler_init( video_scaler, &format_src, &format_dst );
178
179 frame_src->planes[0] = (uint8_t *)vectorscope_image.pixel_data;
180 frame_dst->planes[0] = (uint8_t *)inst->scala;
181
182 /* Pad the source image to make the stride a multiple of 16. */
183 gavl_video_frame_t* padded = gavl_video_frame_create( &format_src );
184 gavl_video_frame_copy( &format_src, padded, frame_src );
185
186 float transparent[4] = { 0.0, 0.0, 0.0, 0.0 };
187 gavl_video_frame_fill( frame_dst, &format_dst, transparent );
188
189 gavl_video_scaler_scale( video_scaler, padded, frame_dst );
190
191 gavl_video_scaler_destroy(video_scaler);
192 gavl_video_frame_null( frame_src );
193 gavl_video_frame_destroy( frame_src );
194 gavl_video_frame_null( frame_dst );
195 gavl_video_frame_destroy( frame_dst );
196 gavl_video_frame_destroy( padded );
197
198 inst->scope_scaler = gavl_video_scaler_create();
199 inst->scope_frame_src = gavl_video_frame_create(0);
200 inst->scope_frame_dst = gavl_video_frame_create(0);
201 inst->scope_frame_src->strides[0] = SCOPE_WIDTH * 4;
202 inst->scope_frame_dst->strides[0] = width * 4;
203 options = gavl_video_scaler_get_options( inst->scope_scaler );
204
205 format_src.frame_width = SCOPE_WIDTH;
206 format_src.frame_height = SCOPE_HEIGHT;
207 format_src.image_width = SCOPE_WIDTH;
208 format_src.image_height = SCOPE_HEIGHT;
209 format_src.pixel_width = 1;
210 format_src.pixel_height = 1;
211 format_src.pixelformat = GAVL_RGBA_32;
212 format_dst.frame_width = width;
213 format_dst.frame_height = height;
214 format_dst.image_width = width;
215 format_dst.image_height = height;
216 format_dst.pixel_width = 1;
217 format_dst.pixel_height = 1;
218 format_dst.pixelformat = GAVL_RGBA_32;
219
220 gavl_rectangle_f_set_all( &src_rect, &format_src );
221 if (width > height) {
222 dst_rect.x = (width-height)/2;
223 dst_rect.y = 0;
224 dst_rect.w = height;
225 dst_rect.h = height;
226 }
227 else {
228 dst_rect.x = 0;
229 dst_rect.y = (height-width)/2;
230 dst_rect.w = width;
231 dst_rect.h = width;
232 }
233 gavl_video_options_set_rectangles( options, &src_rect, &dst_rect );
234 gavl_video_scaler_init( inst->scope_scaler, &format_src, &format_dst );
235
236 return (f0r_instance_t)inst;
237 }
238
239 void f0r_destruct(f0r_instance_t instance)
240 {
241 vectorscope_instance_t* inst = (vectorscope_instance_t*)instance;
242 if ( inst->w == 0 || inst->h == 0 ) {
243 free(instance);
244 return;
245 }
246 free(inst->scala);
247 gavl_video_scaler_destroy( inst->scope_scaler );
248 gavl_video_frame_null( inst->scope_frame_src );
249 gavl_video_frame_destroy( inst->scope_frame_src );
250 gavl_video_frame_null( inst->scope_frame_dst );
251 gavl_video_frame_destroy( inst->scope_frame_dst );
252 free(instance);
253 }
254
255 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
256 {
257 assert(instance);
258 vectorscope_instance_t* inst = (vectorscope_instance_t*)instance;
259
260 switch(param_index)
261 {
262 case 0:
263 *((double *)param) = inst->mix;
264 break;
265 case 1:
266 *((double *)param) = inst->overlay_sides;
267 break;
268 }
269 }
270
271 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
272 {
273 assert(instance);
274 vectorscope_instance_t* inst = (vectorscope_instance_t*)instance;
275
276 switch(param_index)
277 {
278 case 0:
279 inst->mix = *((double *)param);
280 break;
281 case 1:
282 inst->overlay_sides = *((double *)param);
283 break;
284 }
285 }
286
287 /* RGB to YCbCr range 0-255 */
288 YCbCr_t rgb_to_YCbCr(rgb_t rgb)
289 {
290 YCbCr_t dest;
291 dest.Y = (float)((0.299 * (float)rgb.red + 0.587 * (float)rgb.green + 0.114 * (float)rgb.blue));
292 dest.Cb = 128 + (float)((-0.16874 * (float)rgb.red - 0.33126 * (float)rgb.green + 0.5 * (float)rgb.blue));
293 dest.Cr = 128 + (float)((0.5 * (float)rgb.red - 0.41869 * (float)rgb.green - 0.08131 * (float)rgb.blue));
294 return dest;
295 }
296
297 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
298 {
299 assert(instance);
300 vectorscope_instance_t* inst = (vectorscope_instance_t*)instance;
301
302 int width = inst->w;
303 int height = inst->h;
304 double mix = inst->mix;
305 int len = inst->w * inst->h;
306 int scope_len = SCOPE_WIDTH * SCOPE_HEIGHT;
307
308 uint32_t* dst = outframe;
309 uint32_t* dst_end;
310 const uint32_t* src = inframe;
311 const uint32_t* src_end;
312 uint32_t* scope = (uint32_t*)malloc( scope_len * 4 );
313 uint32_t* scope_end;
314
315 YCbCr_t YCbCr;
316 rgb_t rgb;
317 uint8_t* pixel;
318 int x, y;
319 dst_end = dst + len;
320 src_end = src + len;
321 scope_end = scope + scope_len;
322
323 if ( inst->overlay_sides > 0.5) {
324 while ( dst < dst_end ) {
325 *(dst++) = 0xFF000000;
326 }
327 } else {
328 while ( dst < dst_end ) {
329 *(dst++) = *(src++);
330 }
331 src -= len;
332 }
333
334 dst = outframe;
335 while ( scope < scope_end ) {
336 *(scope++) = 0xFF000000;
337 }
338 scope -= scope_len;
339
340 while ( src < src_end ) {
341 rgb.red = (((*src) & 0x000000FF) >> OFFSET_R);
342 rgb.green = (((*src) & 0x0000FF00) >> OFFSET_G);
343 rgb.blue = (((*src) & 0x00FF0000) >> OFFSET_B);
344 src++;
345 YCbCr = rgb_to_YCbCr(rgb);
346 x = YCbCr.Cb;
347 y = 255-YCbCr.Cr;
348 if ( x >= 0 && x < SCOPE_WIDTH && y >= 0 && y < SCOPE_HEIGHT ) {
349 pixel = (uint8_t*)&scope[x+SCOPE_WIDTH*y];
350 if ( pixel[0] < 255 ) {
351 pixel[0]++;
352 pixel[1]++;
353 pixel[2]++;
354 }
355 }
356 }
357
358 inst->scope_frame_src->planes[0] = (uint8_t *)scope;
359 inst->scope_frame_dst->planes[0] = (uint8_t *)dst;
360
361 gavl_video_scaler_scale( inst->scope_scaler, inst->scope_frame_src, inst->scope_frame_dst );
362
363 unsigned char *scala8, *dst8, *dst8_end, *src8;
364
365 scala8 = inst->scala;
366 src8 = (unsigned char*)inframe;
367 dst8 = (unsigned char*)outframe;
368 dst8_end = dst8 + ( len * 4 );
369 if (mix > 0.001 ) { // to not lose performance for non-mixing users
370 while ( dst8 < dst8_end ) {
371 dst8[0] = ( ( ( scala8[0] - dst8[0] ) * 255 * scala8[3] ) >> 16 ) + dst8[0];
372 dst8[1] = ( ( ( scala8[1] - dst8[1] ) * 255 * scala8[3] ) >> 16 ) + dst8[1];
373 dst8[2] = ( ( ( scala8[2] - dst8[2] ) * 255 * scala8[3] ) >> 16 ) + dst8[2];
374 if (dst8[0] == 0) {
375 dst8[0] = src8[0] * mix;
376 dst8[1] = src8[1] * mix;
377 dst8[2] = src8[2] * mix;
378 }
379 scala8 += 4;
380 dst8 += 4;
381 src8 += 4;
382 }
383 } else {
384 while ( dst8 < dst8_end ) {
385 dst8[0] = ( ( ( scala8[0] - dst8[0] ) * 255 * scala8[3] ) >> 16 ) + dst8[0];
386 dst8[1] = ( ( ( scala8[1] - dst8[1] ) * 255 * scala8[3] ) >> 16 ) + dst8[1];
387 dst8[2] = ( ( ( scala8[2] - dst8[2] ) * 255 * scala8[3] ) >> 16 ) + dst8[2];
388 scala8 += 4;
389 dst8 += 4;
390 }
391 }
392 free(scope);
393 }
394