|
| 1 | +/** |
| 2 | + * @file capture_filter/noise.c |
| 3 | + * @author Martin Pulec <[email protected]> |
| 4 | + */ |
| 5 | +/* |
| 6 | + * Copyright (c) 2025 CESNET, zájmové sdružení právnických osob |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, is permitted provided that the following conditions |
| 11 | + * are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * |
| 16 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer in the |
| 18 | + * documentation and/or other materials provided with the distribution. |
| 19 | + * |
| 20 | + * 3. Neither the name of CESNET nor the names of its contributors may be |
| 21 | + * used to endorse or promote products derived from this software without |
| 22 | + * specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS |
| 25 | + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 27 | + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 28 | + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 29 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 34 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 35 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | + */ |
| 37 | + |
| 38 | +#include <limits.h> // for UCHAR_MAX, UINT_MAX |
| 39 | +#include <stdlib.h> // for calloc, free, strtol |
| 40 | +#include <string.h> // for memcpy, strchr, strcmp, strdup, strtok_r |
| 41 | + |
| 42 | +#include "capture_filter.h" // for CAPTURE_FILTER_ABI_VERSION, capture_fil... |
| 43 | +#include "debug.h" // for LOG_LEVEL_ERROR, MSG |
| 44 | +#include "lib_common.h" // for REGISTER_MODULE, library_class |
| 45 | +#include "types.h" // for tile, video_frame, video_frame_callbacks |
| 46 | +#include "utils/color_out.h" // for color_printf, TBOLD, TRED |
| 47 | +#include "utils/macros.h" // for IS_KEY_PREFIX |
| 48 | +#include "utils/random.h" // for ug_rand |
| 49 | +#include "video_frame.h" // for vf_alloc_desc, video_desc_from_frame |
| 50 | +struct module; |
| 51 | + |
| 52 | +#define MOD_NAME "[cf/noise] " |
| 53 | + |
| 54 | +#if __STDC_VERSION__ < 202311L |
| 55 | +#define nullptr NULL |
| 56 | +#endif |
| 57 | + |
| 58 | +static void done(void *state); |
| 59 | + |
| 60 | +enum { |
| 61 | + DEFAULT_MAGNITUDE = 30, |
| 62 | +}; |
| 63 | + |
| 64 | +struct state_noise { |
| 65 | + unsigned magnitude; |
| 66 | +}; |
| 67 | + |
| 68 | +static void |
| 69 | +usage() |
| 70 | +{ |
| 71 | + color_printf(TRED(TBOLD("noise")) " capture filter adds white noise\n"); |
| 72 | + color_printf("\n"); |
| 73 | + |
| 74 | + color_printf("THe main use case is for testing (to add entropy for compression).\n"); |
| 75 | + color_printf("See also \"noise\" parameter of " TBOLD("testcard2") ".\n"); |
| 76 | + color_printf("\n"); |
| 77 | + |
| 78 | + color_printf("Usage:\n"); |
| 79 | + color_printf( |
| 80 | + "\t" TBOLD(TRED("-F noise") "[:magnitude=<m>]") " (default %d)\n", |
| 81 | + DEFAULT_MAGNITUDE); |
| 82 | + color_printf("\t" TBOLD("-F noise:help") "\n"); |
| 83 | +} |
| 84 | + |
| 85 | +static int |
| 86 | +parse_fmt(struct state_noise *s, char *ccfg) |
| 87 | +{ |
| 88 | + if (strcmp(ccfg, "help") == 0) { |
| 89 | + return 1; |
| 90 | + } |
| 91 | + char *item = nullptr; |
| 92 | + char *endptr = nullptr; |
| 93 | + while ((item = strtok_r(ccfg, ":", &endptr)) != nullptr) { |
| 94 | + ccfg = nullptr; |
| 95 | + if (IS_KEY_PREFIX(item, "magnitude")) { |
| 96 | + char *endptr = nullptr; |
| 97 | + const long val = |
| 98 | + strtol(strchr(item, '=') + 1, &endptr, 0); |
| 99 | + if (val <= 0 || val > UINT_MAX || *endptr != '\0') { |
| 100 | + MSG(ERROR, "Wrong value given: %ld!\n", val); |
| 101 | + return -1; |
| 102 | + } |
| 103 | + s->magnitude = val; |
| 104 | + } else { |
| 105 | + MSG(ERROR, "Unknown option: %s!", item); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +static int |
| 113 | +init(struct module *parent, const char *cfg, void **state) |
| 114 | +{ |
| 115 | + (void) parent; |
| 116 | + struct state_noise *s = calloc(1, sizeof(struct state_noise)); |
| 117 | + s->magnitude = DEFAULT_MAGNITUDE; |
| 118 | + char *ccfg = strdup(cfg); |
| 119 | + int rc = parse_fmt(s, ccfg); |
| 120 | + free(ccfg); |
| 121 | + if (rc != 0) { |
| 122 | + usage(); |
| 123 | + done(s); |
| 124 | + return rc; |
| 125 | + } |
| 126 | + |
| 127 | + *state = s; |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +static void |
| 132 | +done(void *state) |
| 133 | +{ |
| 134 | + free(state); |
| 135 | +} |
| 136 | + |
| 137 | +static struct video_frame * |
| 138 | +filter(void *state, struct video_frame *in) |
| 139 | +{ |
| 140 | + struct state_noise *s = state; |
| 141 | + |
| 142 | + struct video_frame *out = vf_alloc_desc_data(video_desc_from_frame(in)); |
| 143 | + out->callbacks.dispose = vf_free; |
| 144 | + |
| 145 | + unsigned char *out_data = (unsigned char *) out->tiles[0].data; |
| 146 | + |
| 147 | + memcpy(out_data, in->tiles[0].data, in->tiles[0].data_len); |
| 148 | + |
| 149 | + const unsigned char *const end = out_data + in->tiles[0].data_len; |
| 150 | + out_data += (ug_rand() % s->magnitude); |
| 151 | + while (out_data < end) { |
| 152 | + *out_data = ug_rand() % (UCHAR_MAX + 1); |
| 153 | + out_data += 1 + (ug_rand() % s->magnitude); |
| 154 | + } |
| 155 | + |
| 156 | + VIDEO_FRAME_DISPOSE(in); |
| 157 | + |
| 158 | + return out; |
| 159 | +} |
| 160 | + |
| 161 | +static const struct capture_filter_info capture_filter_noise = { |
| 162 | + .init = init, |
| 163 | + .done = done, |
| 164 | + .filter = filter, |
| 165 | +}; |
| 166 | + |
| 167 | +REGISTER_MODULE(noise, &capture_filter_noise, LIBRARY_CLASS_CAPTURE_FILTER, |
| 168 | + CAPTURE_FILTER_ABI_VERSION); |
0 commit comments