Coverage Report

Created: 2026-10-07 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/video/video-resampler.c
Line
Count
Source
1
/* GStreamer
2
 * Copyright (C) <2014> Wim Taymans <wim.taymans@gmail.com>
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Library General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Library General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Library General Public
15
 * License along with this library; if not, write to the
16
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17
 * Boston, MA 02110-1301, USA.
18
 */
19
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23
24
#include <string.h>
25
#include <stdio.h>
26
#include <math.h>
27
28
#include "video-resampler.h"
29
30
#ifndef GST_DISABLE_GST_DEBUG
31
#define GST_CAT_DEFAULT ensure_debug_category()
32
static GstDebugCategory *
33
ensure_debug_category (void)
34
0
{
35
0
  static gsize cat_gonce = 0;
36
37
0
  if (g_once_init_enter (&cat_gonce)) {
38
0
    gsize cat_done;
39
40
0
    cat_done = (gsize) _gst_debug_category_new ("video-resampler", 0,
41
0
        "video-resampler object");
42
43
0
    g_once_init_leave (&cat_gonce, cat_done);
44
0
  }
45
46
0
  return (GstDebugCategory *) cat_gonce;
47
0
}
48
#else
49
#define ensure_debug_category() /* NOOP */
50
#endif /* GST_DISABLE_GST_DEBUG */
51
52
/**
53
 * SECTION:gstvideoresampler
54
 * @title: GstVideoResampler
55
 * @short_description: Utility structure for resampler information
56
 *
57
 * #GstVideoResampler is a structure which holds the information
58
 * required to perform various kinds of resampling filtering.
59
 *
60
 */
61
62
63
2.24k
#define DEFAULT_OPT_CUBIC_B (1.0 / 3.0)
64
2.24k
#define DEFAULT_OPT_CUBIC_C (1.0 / 3.0)
65
66
0
#define DEFAULT_OPT_ENVELOPE 2.0
67
2.69k
#define DEFAULT_OPT_SHARPNESS 1.0
68
2.69k
#define DEFAULT_OPT_SHARPEN 0.0
69
70
2.69k
#define DEFAULT_OPT_MAX_TAPS 128
71
72
typedef struct _ResamplerParams ResamplerParams;
73
74
struct _ResamplerParams
75
{
76
  GstVideoResamplerMethod method;
77
  GstVideoResamplerFlags flags;
78
79
  gdouble shift;
80
81
    gdouble (*get_tap) (ResamplerParams * params, gint l, gint xi, gdouble x);
82
83
  /* for cubic */
84
  gdouble b, c;
85
  /* used by lanczos */
86
  gdouble ex, fx, dx;
87
  /* extra params */
88
  gdouble envelope;
89
  gdouble sharpness;
90
  gdouble sharpen;
91
92
  GstVideoResampler *resampler;
93
};
94
95
static gdouble
96
get_opt_double (GstStructure * options, const gchar * name, gdouble def)
97
9.87k
{
98
9.87k
  gdouble res;
99
9.87k
  if (!options || !gst_structure_get_double (options, name, &res))
100
9.87k
    res = def;
101
9.87k
  return res;
102
9.87k
}
103
104
static gint
105
get_opt_int (GstStructure * options, const gchar * name, gint def)
106
2.69k
{
107
2.69k
  gint res;
108
2.69k
  if (!options || !gst_structure_get_int (options, name, &res))
109
2.69k
    res = def;
110
2.69k
  return res;
111
2.69k
}
112
113
2.24k
#define GET_OPT_CUBIC_B(options) get_opt_double(options, \
114
2.24k
    GST_VIDEO_RESAMPLER_OPT_CUBIC_B, DEFAULT_OPT_CUBIC_B)
115
2.24k
#define GET_OPT_CUBIC_C(options) get_opt_double(options, \
116
2.24k
    GST_VIDEO_RESAMPLER_OPT_CUBIC_C, DEFAULT_OPT_CUBIC_C)
117
0
#define GET_OPT_ENVELOPE(options) get_opt_double(options, \
118
0
    GST_VIDEO_RESAMPLER_OPT_ENVELOPE, DEFAULT_OPT_ENVELOPE)
119
2.69k
#define GET_OPT_SHARPNESS(options) get_opt_double(options, \
120
2.69k
    GST_VIDEO_RESAMPLER_OPT_SHARPNESS, DEFAULT_OPT_SHARPNESS)
121
2.69k
#define GET_OPT_SHARPEN(options) get_opt_double(options, \
122
2.69k
    GST_VIDEO_RESAMPLER_OPT_SHARPEN, DEFAULT_OPT_SHARPEN)
123
2.69k
#define GET_OPT_MAX_TAPS(options) get_opt_int(options, \
124
2.69k
    GST_VIDEO_RESAMPLER_OPT_MAX_TAPS, DEFAULT_OPT_MAX_TAPS)
125
126
static double
127
sinc (double x)
128
0
{
129
0
  if (x == 0)
130
0
    return 1;
131
132
0
  return sin (G_PI * x) / (G_PI * x);
133
0
}
134
135
static double
136
envelope (double x)
137
0
{
138
0
  if (x <= -1 || x >= 1)
139
0
    return 0;
140
0
  return sinc (x);
141
0
}
142
143
static gdouble
144
get_nearest_tap (ResamplerParams * params, gint l, gint xi, gdouble x)
145
0
{
146
0
  return 1.0;
147
0
}
148
149
static gdouble
150
get_linear_tap (ResamplerParams * params, gint l, gint xi, gdouble x)
151
61.3k
{
152
61.3k
  gdouble res, a;
153
61.3k
  gint xl = xi + l;
154
155
61.3k
  a = fabs (x - xl) * params->fx;
156
157
61.3k
  if (a < 1.0)
158
58.3k
    res = 1.0 - a;
159
3.05k
  else
160
3.05k
    res = 0.0;
161
162
61.3k
  return res;
163
61.3k
}
164
165
static gdouble
166
get_cubic_tap (ResamplerParams * params, gint l, gint xi, gdouble x)
167
1.00M
{
168
1.00M
  gdouble a, a2, a3, b, c;
169
1.00M
  gint xl = xi + l;
170
171
1.00M
  a = fabs (x - xl) * params->fx;
172
1.00M
  a2 = a * a;
173
1.00M
  a3 = a2 * a;
174
175
1.00M
  b = params->b;
176
1.00M
  c = params->c;
177
178
1.00M
  if (a <= 1.0)
179
546k
    return ((12.0 - 9.0 * b - 6.0 * c) * a3 +
180
546k
        (-18.0 + 12.0 * b + 6.0 * c) * a2 + (6.0 - 2.0 * b)) / 6.0;
181
454k
  else if (a <= 2.0)
182
437k
    return ((-b - 6.0 * c) * a3 +
183
437k
        (6.0 * b + 30.0 * c) * a2 +
184
437k
        (-12.0 * b - 48.0 * c) * a + (8.0 * b + 24.0 * c)) / 6.0;
185
17.1k
  else
186
17.1k
    return 0.0;
187
1.00M
}
188
189
static gdouble
190
get_sinc_tap (ResamplerParams * params, gint l, gint xi, gdouble x)
191
0
{
192
0
  gint xl = xi + l;
193
0
  return sinc ((x - xl) * params->fx);
194
0
}
195
196
static gdouble
197
get_lanczos_tap (ResamplerParams * params, gint l, gint xi, gdouble x)
198
0
{
199
0
  gint xl = xi + l;
200
0
  gdouble env = envelope ((x - xl) * params->ex);
201
0
  return (sinc ((x - xl) * params->fx) - params->sharpen) * env;
202
0
}
203
204
static void
205
resampler_calculate_taps (ResamplerParams * params)
206
2.69k
{
207
2.69k
  GstVideoResampler *resampler = params->resampler;
208
2.69k
  gint j;
209
2.69k
  guint32 *offset, *n_taps, *phase;
210
2.69k
  gint tap_offs;
211
2.69k
  gint max_taps;
212
2.69k
  gint in_size, out_size;
213
2.69k
  gdouble shift;
214
2.69k
  gdouble corr;
215
216
2.69k
  in_size = resampler->in_size;
217
2.69k
  out_size = resampler->out_size;
218
219
2.69k
  max_taps = resampler->max_taps;
220
2.69k
  tap_offs = (max_taps - 1) / 2;
221
2.69k
  corr = (max_taps == 1 ? 0.0 : 0.5);
222
223
2.69k
  shift = params->shift;
224
225
2.69k
  resampler->taps = g_malloc (sizeof (gdouble) * max_taps * out_size);
226
2.69k
  n_taps = resampler->n_taps = g_malloc (sizeof (guint32) * out_size);
227
2.69k
  offset = resampler->offset = g_malloc (sizeof (guint32) * out_size);
228
2.69k
  phase = resampler->phase = g_malloc (sizeof (guint32) * out_size);
229
230
221k
  for (j = 0; j < out_size; j++) {
231
218k
    gdouble ox, x;
232
218k
    gint xi;
233
218k
    gint l;
234
218k
    gdouble weight;
235
218k
    gdouble *taps;
236
237
    /* center of the output pixel */
238
218k
    ox = (0.5 + (gdouble) j - shift) / out_size;
239
    /* x is the source pixel to use, can be fractional */
240
218k
    x = ox * (gdouble) in_size - corr;
241
218k
    x = CLAMP (x, 0, in_size - 1);
242
    /* this is the first source pixel to use */
243
218k
    xi = floor (x - tap_offs);
244
245
218k
    offset[j] = xi;
246
218k
    phase[j] = j;
247
218k
    n_taps[j] = max_taps;
248
218k
    weight = 0;
249
218k
    taps = resampler->taps + j * max_taps;
250
251
1.28M
    for (l = 0; l < max_taps; l++) {
252
1.06M
      taps[l] = params->get_tap (params, l, xi, x);
253
1.06M
      weight += taps[l];
254
1.06M
    }
255
256
1.28M
    for (l = 0; l < max_taps; l++)
257
1.06M
      taps[l] /= weight;
258
259
218k
    if (xi < 0) {
260
9.56k
      gint sh = -xi;
261
262
29.7k
      for (l = 0; l < sh; l++) {
263
20.2k
        taps[sh] += taps[l];
264
20.2k
      }
265
68.4k
      for (l = 0; l < max_taps - sh; l++) {
266
58.9k
        taps[l] = taps[sh + l];
267
58.9k
      }
268
29.7k
      for (; l < max_taps; l++) {
269
20.2k
        taps[l] = 0;
270
20.2k
      }
271
9.56k
      offset[j] += sh;
272
9.56k
    }
273
218k
    if (xi > in_size - max_taps) {
274
13.4k
      gint sh = xi - (in_size - max_taps);
275
276
38.7k
      for (l = 0; l < sh; l++) {
277
25.3k
        taps[max_taps - sh - 1] += taps[max_taps - sh + l];
278
25.3k
      }
279
72.8k
      for (l = 0; l < max_taps - sh; l++) {
280
59.4k
        taps[max_taps - 1 - l] = taps[max_taps - 1 - sh - l];
281
59.4k
      }
282
38.7k
      for (l = 0; l < sh; l++) {
283
25.3k
        taps[l] = 0;
284
25.3k
      }
285
13.4k
      offset[j] -= sh;
286
13.4k
    }
287
218k
  }
288
2.69k
}
289
290
static void
291
resampler_dump (GstVideoResampler * resampler)
292
2.69k
{
293
#if 0
294
  gint i, max_taps, out_size;
295
296
  out_size = resampler->out_size;
297
  max_taps = resampler->max_taps;
298
299
  for (i = 0; i < out_size; i++) {
300
    gint j, o, phase, n_taps;
301
    gdouble sum;
302
303
    o = resampler->offset[i];
304
    n_taps = resampler->n_taps[i];
305
    phase = resampler->phase[i];
306
307
    printf ("%u: \t%d  ", i, o);
308
    sum = 0;
309
    for (j = 0; j < n_taps; j++) {
310
      gdouble tap;
311
      tap = resampler->taps[phase * max_taps + j];
312
      printf ("\t%f ", tap);
313
      sum += tap;
314
    }
315
    printf ("\t: sum %f\n", sum);
316
  }
317
#endif
318
2.69k
}
319
320
321
/**
322
 * gst_video_resampler_new:
323
 * @resampler: a #GstVideoResampler
324
 * @method: a #GstVideoResamplerMethod
325
 * @flags: #GstVideoResamplerFlags
326
 * @n_phases: number of phases to use
327
 * @n_taps: number of taps to use
328
 * @in_size: number of source elements
329
 * @out_size: number of destination elements
330
 * @options: extra options
331
 *
332
 * Make a new resampler. @in_size source elements will
333
 * be resampled to @out_size destination elements.
334
 *
335
 * @n_taps specifies the amount of elements to use from the source for one output
336
 * element. If n_taps is 0, this function chooses a good value automatically based
337
 * on the @method and @in_size/@out_size.
338
 *
339
 * Returns: %TRUE on success
340
 *
341
 * Since: 1.6
342
 */
343
gboolean
344
gst_video_resampler_init (GstVideoResampler * resampler,
345
    GstVideoResamplerMethod method, GstVideoResamplerFlags flags,
346
    guint n_phases, guint n_taps, gdouble shift, guint in_size, guint out_size,
347
    GstStructure * options)
348
2.69k
{
349
2.69k
  ResamplerParams params;
350
2.69k
  gint max_taps;
351
2.69k
  gdouble scale_factor;
352
353
2.69k
  g_return_val_if_fail (in_size != 0, FALSE);
354
2.69k
  g_return_val_if_fail (out_size != 0, FALSE);
355
2.69k
  g_return_val_if_fail (n_phases == out_size, FALSE);
356
357
2.69k
  resampler->in_size = in_size;
358
2.69k
  resampler->out_size = out_size;
359
2.69k
  resampler->n_phases = n_phases;
360
361
2.69k
  params.method = method;
362
2.69k
  params.flags = flags;
363
2.69k
  params.shift = shift;
364
2.69k
  params.resampler = resampler;
365
366
2.69k
  GST_DEBUG ("%d %u  %u->%u", method, n_taps, in_size, out_size);
367
368
2.69k
  params.sharpness = GET_OPT_SHARPNESS (options);
369
2.69k
  params.sharpen = GET_OPT_SHARPEN (options);
370
371
2.69k
  scale_factor = in_size / (gdouble) out_size;
372
2.69k
  if (scale_factor > 1.0) {
373
1.44k
    params.fx = (1.0 / scale_factor) * params.sharpness;
374
1.44k
  } else {
375
1.25k
    params.fx = (1.0) * params.sharpness;
376
1.25k
  }
377
378
2.69k
  max_taps = GET_OPT_MAX_TAPS (options);
379
2.69k
  n_taps = MIN (n_taps, max_taps);
380
381
2.69k
  switch (method) {
382
0
    case GST_VIDEO_RESAMPLER_METHOD_NEAREST:
383
0
      params.envelope = GET_OPT_ENVELOPE (options);
384
0
      params.get_tap = get_nearest_tap;
385
0
      if (n_taps == 0)
386
0
        n_taps = 1;
387
0
      break;
388
456
    case GST_VIDEO_RESAMPLER_METHOD_LINEAR:
389
456
      params.get_tap = get_linear_tap;
390
456
      params.envelope = 1.0;
391
456
      break;
392
2.24k
    case GST_VIDEO_RESAMPLER_METHOD_CUBIC:
393
2.24k
      params.b = GET_OPT_CUBIC_B (options);
394
2.24k
      params.c = GET_OPT_CUBIC_C (options);
395
2.24k
      params.envelope = 2.0;
396
2.24k
      params.get_tap = get_cubic_tap;
397
2.24k
      break;
398
0
    case GST_VIDEO_RESAMPLER_METHOD_SINC:
399
0
      params.envelope = GET_OPT_ENVELOPE (options);
400
0
      params.get_tap = get_sinc_tap;
401
0
      break;
402
0
    case GST_VIDEO_RESAMPLER_METHOD_LANCZOS:
403
0
      params.envelope = GET_OPT_ENVELOPE (options);
404
0
      params.get_tap = get_lanczos_tap;
405
0
      break;
406
0
    default:
407
0
      break;
408
2.69k
  }
409
410
2.69k
  if (n_taps == 0) {
411
2.64k
    params.dx = ceil (2.0 * params.envelope / params.fx);
412
2.64k
    n_taps = CLAMP (params.dx, 0, max_taps);
413
2.64k
  }
414
2.69k
  if (flags & GST_VIDEO_RESAMPLER_FLAG_HALF_TAPS && n_taps > 3)
415
0
    n_taps /= 2;
416
2.69k
  params.fx = 2.0 * params.envelope / n_taps;
417
2.69k
  params.ex = 2.0 / n_taps;
418
419
2.69k
  if (n_taps > in_size)
420
1.01k
    n_taps = in_size;
421
422
2.69k
  resampler->max_taps = n_taps;
423
424
2.69k
  resampler_calculate_taps (&params);
425
426
2.69k
  resampler_dump (resampler);
427
428
2.69k
  return TRUE;
429
2.69k
}
430
431
/**
432
 * gst_video_resampler_clear:
433
 * @resampler: a #GstVideoResampler
434
 *
435
 * Clear a previously initialized #GstVideoResampler @resampler.
436
 *
437
 * Since: 1.6
438
 */
439
void
440
gst_video_resampler_clear (GstVideoResampler * resampler)
441
2.75k
{
442
2.75k
  g_return_if_fail (resampler != NULL);
443
444
2.75k
  g_free (resampler->phase);
445
2.75k
  g_free (resampler->offset);
446
2.75k
  g_free (resampler->n_taps);
447
2.75k
  g_free (resampler->taps);
448
2.75k
}