-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstviperddc.c
380 lines (312 loc) · 11.1 KB
/
gstviperddc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "gstviperddc.h"
/**
* SECTION:element-viperddc
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch audiotestsrc ! audioconverter ! viperddc ! ! audioconverter ! autoaudiosink
* ]|
* </refsect2>
*/
#include <stdio.h>
#include <string.h>
#include <gst/gst.h>
#include <gst/base/base.h>
#include <gst/base/gstbasetransform.h>
#include <gst/audio/audio.h>
#include <gst/audio/gstaudiofilter.h>
#include <gst/controller/controller.h>
#include <math.h>
#include <locale.h>
GST_DEBUG_CATEGORY_STATIC (gst_viperddc_debug);
#define GST_CAT_DEFAULT gst_viperddc_debug
/* Filter signals and args */
enum {
/* FILL ME */
LAST_SIGNAL
};
enum {
PROP_0,
PROP_DDC_ENABLE,
PROP_DDC_FILE,
};
#define gst_viperddc_parent_class parent_class
G_DEFINE_TYPE (Gstviperddc, gst_viperddc, GST_TYPE_AUDIO_FILTER);
static void gst_viperddc_set_property(GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec);
static void gst_viperddc_get_property(GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec);
static void gst_viperddc_finalize(GObject *object);
static gboolean gst_viperddc_setup(GstAudioFilter *self,
const GstAudioInfo *info);
static gboolean gst_viperddc_stop(GstBaseTransform *base);
static GstFlowReturn gst_viperddc_transform_ip(GstBaseTransform *base,
GstBuffer *outbuf);
/* GObject vmethod implementations */
/* initialize the viperddc's class */
static void
gst_viperddc_class_init(GstviperddcClass *klass) {
GObjectClass *gobject_class = (GObjectClass *) klass;
GstElementClass *gstelement_class = (GstElementClass *) klass;
GstBaseTransformClass *basetransform_class = (GstBaseTransformClass *) klass;
GstAudioFilterClass *audioself_class = (GstAudioFilterClass *) klass;
GstCaps *caps;
/* debug category for fltering log messages
*/
GST_DEBUG_CATEGORY_INIT (gst_viperddc_debug, "viperddc", 0, "viperddc element");
gobject_class->set_property = gst_viperddc_set_property;
gobject_class->get_property = gst_viperddc_get_property;
gobject_class->finalize = gst_viperddc_finalize;
/* global switch */
g_object_class_install_property(gobject_class, PROP_DDC_ENABLE,
g_param_spec_boolean("ddc_enable", "DDCEnabled", "Enable processing",
FALSE, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
g_object_class_install_property(gobject_class, PROP_DDC_FILE,
g_param_spec_string("ddc_file", "DDCFilePath", "VDC path",
"", G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
gst_element_class_set_static_metadata(gstelement_class,
"viperddc",
"Filter/Effect/Audio",
"ViPER-DDC open-source replacement for GStreamer1",
"ThePBone <[email protected]>");
caps = gst_caps_from_string(ALLOWED_CAPS);
gst_audio_filter_class_add_pad_templates(GST_VIPERDDC_CLASS (klass), caps);
gst_caps_unref(caps);
audioself_class->setup = GST_DEBUG_FUNCPTR (gst_viperddc_setup);
basetransform_class->transform_ip =
GST_DEBUG_FUNCPTR (gst_viperddc_transform_ip);
basetransform_class->transform_ip_on_passthrough = FALSE;
basetransform_class->stop = GST_DEBUG_FUNCPTR (gst_viperddc_stop);
}
/* initialize the new element
* allocate private resources
*/
static void
gst_viperddc_init(Gstviperddc *self) {
gst_base_transform_set_in_place(GST_BASE_TRANSFORM (self), TRUE);
gst_base_transform_set_gap_aware(GST_BASE_TRANSFORM (self), TRUE);
/* initialize properties */
self->ddc_enable = FALSE;
self->ddc_file = malloc(256);
memset(self->ddc_file, 0, 256);
g_mutex_init(&self->lock);
}
char *memory_read_ascii(char *path) {
int c;
long size;
FILE *file;
int i = 0;
file = fopen(path, "r");
if (file) {
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char *) malloc(size * sizeof(char));
while ((c = getc(file)) != EOF) {
buffer[i] = (char) c;
i++;
}
fclose(file);
return buffer;
}
return NULL;
}
static void sync_parameters(GObject *object) {
Gstviperddc *self = GST_VIPERDDC (object);
setlocale(LC_NUMERIC, "C");
if (!self->ddc_enable) {
if (self->sosCount) {
for (int i = 0; i < self->sosCount; i++) {
free(self->df441[i]);
free(self->df48[i]);
}
free(self->df441);
self->df441 = 0;
free(self->df48);
self->df48 = 0;
self->sosCount = 0;
self->sosPointer = 0;
}
return;
}
if (!self->ddc_file || self->ddc_file == NULL) {
GST_CAT_ERROR(gst_viperddc_debug, "DDC path is not set");
return;
}
char *ddcString = memory_read_ascii(self->ddc_file);
if (!ddcString || ddcString == NULL) {
GST_CAT_ERROR(gst_viperddc_debug, "Unable to open DDC file");
return;
}
int d = 0;
for (int i = 0; i < strlen(ddcString); ++i) {
d |= ddcString[i];
}
if (d == 0) {
GST_CAT_ERROR(gst_viperddc_debug, "DDC contents contain no data");
if (ddcString != NULL) {
free(ddcString);
}
return;
}
int begin = strcspn(ddcString, "S");
if (strcspn(ddcString, "R") != begin + 1) { //check for 'SR' in the string
GST_CAT_ERROR(gst_viperddc_debug, "Invalid DDC string");
if (ddcString != NULL) {
free(ddcString);
}
return;
}
self->sosCount = DDCParser(ddcString, &self->df441, &self->df48);
GST_CAT_DEBUG(gst_viperddc_debug, "SOS count %d", self->sosCount);
if(self->sosCount < 1){
GST_CAT_ERROR(gst_viperddc_debug, "SOS count is zero");
}
if (self->samplerate == 44100 && self->df441) {
self->sosPointer = self->df441;
self->usedSOSCount = self->sosCount;
} else if (self->samplerate == 48000 && self->df48) {
self->sosPointer = self->df48;
self->usedSOSCount = self->sosCount;
} else {
GST_CAT_ERROR(gst_viperddc_debug, "Invalid sampling rate");
}
if (ddcString != NULL) {
free(ddcString);
}
GST_CAT_DEBUG(gst_viperddc_debug, "VDC num of SOS: %d", self->sosCount);
GST_CAT_DEBUG(gst_viperddc_debug, "VDC df48[0].b0: %1.14f", (float) self->df48[0]->b0);
}
/* free private resources
*/
static void
gst_viperddc_finalize(GObject *object) {
Gstviperddc *self = GST_VIPERDDC (object);
self->ddc_enable = FALSE;
sync_parameters(object);
g_mutex_clear(&self->lock);
G_OBJECT_CLASS (parent_class)->finalize(object);
}
static void
gst_viperddc_set_property(GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec) {
Gstviperddc *self = GST_VIPERDDC (object);
switch (prop_id) {
case PROP_DDC_ENABLE: {
g_mutex_lock(&self->lock);
self->ddc_enable = g_value_get_boolean(value);
sync_parameters(object);
g_mutex_unlock(&self->lock);
}
break;
case PROP_DDC_FILE: {
g_mutex_lock(&self->lock);
if (strlen(g_value_get_string(value)) < 256) {
self->ddc_file = malloc(256);
strncpy(self->ddc_file,
g_value_get_string(value), 256);
sync_parameters(object);
}
g_mutex_unlock(&self->lock);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_viperddc_get_property(GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec) {
Gstviperddc *self = GST_VIPERDDC (object);
switch (prop_id) {
case PROP_DDC_ENABLE:
g_value_set_boolean(value, self->ddc_enable);
break;
case PROP_DDC_FILE:
g_value_set_string(value, self->ddc_file);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* GstBaseTransform vmethod implementations */
static gboolean
gst_viperddc_setup(GstAudioFilter *base, const GstAudioInfo *info) {
Gstviperddc *self = GST_VIPERDDC (base);
if (info) {
self->samplerate = GST_AUDIO_INFO_RATE (info);
} else {
self->samplerate = GST_AUDIO_FILTER_RATE (self);
}
if (self->samplerate <= 0)
return FALSE;
GST_DEBUG_OBJECT (self, "current sample_rate = %d", self->samplerate);
sync_parameters((GObject *) self);
return TRUE;
}
static gboolean
gst_viperddc_stop(GstBaseTransform *base) {
Gstviperddc *self = GST_VIPERDDC (base);
return TRUE;
}
/* this function does the actual processing
*/
static GstFlowReturn
gst_viperddc_transform_ip(GstBaseTransform *base, GstBuffer *buf) {
Gstviperddc *filter = GST_VIPERDDC (base);
guint idx, num_samples;
float *pcm_data;
GstClockTime timestamp, stream_time;
GstMapInfo map;
timestamp = GST_BUFFER_TIMESTAMP (buf);
stream_time =
gst_segment_to_stream_time(&base->segment, GST_FORMAT_TIME, timestamp);
if (GST_CLOCK_TIME_IS_VALID (stream_time))
gst_object_sync_values(GST_OBJECT (filter), stream_time);
if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_GAP)))
return GST_FLOW_OK;
if (filter->ddc_enable) {
gst_buffer_map(buf, &map, GST_MAP_READWRITE);
num_samples = map.size / GST_AUDIO_FILTER_BPS (filter) / 2;
pcm_data = (float *) (map.data);
g_mutex_lock(&filter->lock);
int framePos, framePos2x;
for (framePos = 0; framePos < num_samples; framePos++) {
framePos2x = framePos << 1;
int indexL = framePos2x;
int indexR = framePos2x + 1;
for (int j = 0; j < filter->usedSOSCount; j++) {
SOS_DF2_Float_StereoProcess(filter->sosPointer[j], pcm_data[indexL], pcm_data[indexR],
&pcm_data[indexL], &pcm_data[indexR]);
}
}
g_mutex_unlock(&filter->lock);
gst_buffer_unmap(buf, &map);
}
return GST_FLOW_OK;
}
/* entry point to initialize the plug-in
* initialize the plug-in itself
* register the element factories and other features
*/
static gboolean
viperddc_init(GstPlugin *viperddc) {
return gst_element_register(viperddc, "viperddc", GST_RANK_NONE,
GST_TYPE_VIPERDDC);
}
/* gstreamer looks for this structure to register viperddcs
*/
GST_PLUGIN_DEFINE (
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
viperddc,
"ViperDDC element",
viperddc_init,
VERSION,
"GPL",
"GStreamer",
"http://gstreamer.net/"
)