Debian Codegraph @

src/filter/ntsc/ntsc-effect.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "frei0r.h"
5 #include "frei0r/math.h"
6
7 #include "crt_core.h"
8
9 // the actual NTSC emulation code is from here: https://github.com/LMP88959/NTSC-CRT
10
11
12 typedef struct ntsc_instance
13 {
14 // image dimensions
15 int width;
16 int height;
17
18 // parameters
19 struct CRT crt;
20 struct NTSC_SETTINGS ntsc;
21
22 int noise;
23 int field;
24
25 } ntsc_instance_t;
26
27
28 // these functions are for frei0r
29 // mostly copy/paste/slightly modified from the other frei0r effects
30 int f0r_init()
31 {
32 return 1;
33 }
34
35 void f0r_deinit()
36 {}
37
38 void f0r_get_plugin_info(f0r_plugin_info_t* info)
39 {
40 info->name = "NTSC";
41 info->author = "EMMIR, esmane";
42 info->explanation = "Simulates NTSC analog video.";
43 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
44 info->color_model = F0R_COLOR_MODEL_RGBA8888;
45 info->frei0r_version = FREI0R_MAJOR_VERSION;
46 info->major_version = 0;
47 info->minor_version = 2;
48 info->num_params = 3;
49 }
50
51 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
52 {
53 switch(param_index)
54 {
55 case 0:
56 info->name = "Signal Noise";
57 info->explanation = "Amount of noise introduced into the NTSC signal.";
58 info->type = F0R_PARAM_DOUBLE;
59 break;
60
61 case 1:
62 info->name = "Progressive Scan";
63 info->explanation = "Toggles progressive scan (Interlaced if off).";
64 info->type = F0R_PARAM_BOOL;
65 break;
66
67 case 2:
68 info->name = "Scanlines";
69 info->explanation = "Draw borders between scanlines.";
70 info->type = F0R_PARAM_BOOL;
71 break;
72 }
73 }
74
75
76 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
77 {
78 ntsc_instance_t* inst = (ntsc_instance_t*)calloc(1, sizeof(*inst));
79
80 inst->width = width;
81 inst->height = height;
82
83 inst->ntsc.w = width;
84 inst->ntsc.h = height;
85 inst->ntsc.field = 0;
86 inst->ntsc.frame = 0;
87 inst->ntsc.iirs_initialized = 0;
88
89 inst->noise = 0;
90 inst->field = 0;
91
92 crt_init(&(inst->crt), width, height, NULL);
93 inst->crt.scanlines = 0;
94 inst->crt.progressive = 0;
95
96
97
98 return (f0r_instance_t)inst;
99 }
100
101 void f0r_destruct(f0r_instance_t instance)
102 {
103 ntsc_instance_t* inst = (ntsc_instance_t*)instance;
104 free(instance);
105 }
106
107
108 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
109 {
110 ntsc_instance_t* inst = (ntsc_instance_t*)instance;
111 switch(param_index)
112 {
113 case 0:
114 inst->noise = *((double*)param) * 200;
115 break;
116 case 1:
117 inst->crt.progressive = (*((double*)param) >= 0.5);
118 break;
119 case 2:
120 inst->crt.scanlines = (*((double*)param) >= 0.5);
121 break;
122 }
123 }
124
125 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
126 {
127 ntsc_instance_t* inst = (ntsc_instance_t*)instance;
128 switch(param_index)
129 {
130 case 0:
131 *((double*)param) = (inst->noise / 200);
132 break;
133 case 1:
134 *((double*)param) = (inst->crt.progressive ? 1.0 : 0.0);
135 break;
136 case 2:
137 *((double*)param) = (inst->crt.scanlines ? 1.0 : 0.0);
138 break;
139 }
140 }
141
142 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
143 {
144 ntsc_instance_t* inst = (ntsc_instance_t*)instance;
145
146 // clear the output frame
147 memset(outframe, 0, inst->width * inst->height * sizeof(uint32_t));
148
149 // set everything up for the simulation
150 inst->crt.out = (char*)outframe;
151 inst->ntsc.data = (const char*)inframe;
152
153 _render_field:
154 inst->ntsc.field = inst->field & 1;
155
156 if (inst->ntsc.field == 0) {
157 /* a frame is two fields */
158 inst->ntsc.frame ^= 1;
159 }
160
161 // encode and decode ntsc signal
162 crt_modulate(&(inst->crt), &(inst->ntsc));
163 crt_demodulate(&(inst->crt), inst->noise);
164
165 inst->field ^= 1;
166 // if we are in progressive mode, we render both fields onto the frame.
167 // in interlaced mode, we will hit the opposite field on the next frame.
168 if (inst->field && inst->crt.progressive) {
169 goto _render_field;
170 }
171 }