|
| 1 | +/** |
| 2 | + * @file vo_postprocess/delay.c |
| 3 | + * @author Martin Pulec <[email protected]> |
| 4 | + */ |
| 5 | +/* |
| 6 | + * Copyright (c) 2024 CESNET z.s.p.o. |
| 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 <assert.h> |
| 39 | +#include <limits.h> |
| 40 | +#include <math.h> |
| 41 | +#include <stdlib.h> |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +#include "debug.h" |
| 45 | +#include "lib_common.h" |
| 46 | +#include "types.h" |
| 47 | +#include "utils/color_out.h" |
| 48 | +#include "utils/list.h" |
| 49 | +#include "utils/macros.h" |
| 50 | +#include "video_codec.h" |
| 51 | +#include "video_display.h" |
| 52 | +#include "video_frame.h" |
| 53 | +#include "vo_postprocess.h" |
| 54 | + |
| 55 | +#define MOD_NAME "[vpp/delay] " |
| 56 | + |
| 57 | +struct state_delay { |
| 58 | + double delay_sec; |
| 59 | + int delay_frames; |
| 60 | + struct video_desc desc; |
| 61 | + struct simple_linked_list *cached_frames; |
| 62 | +}; |
| 63 | + |
| 64 | +static bool |
| 65 | +delay_get_property(void *state, int property, void *val, size_t *len) |
| 66 | +{ |
| 67 | + (void) state; |
| 68 | + if (property != VO_PP_PROPERTY_CODECS) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + const size_t codecs_len = (VC_END - VC_FIRST) * sizeof(codec_t); |
| 72 | + assert(codecs_len <= *len); |
| 73 | + *len = codecs_len; |
| 74 | + codec_t *codecs = val; |
| 75 | + for (int i = VC_FIRST; i < VC_END; ++i) { |
| 76 | + codecs[i] = i; |
| 77 | + } |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +static void |
| 82 | +usage() |
| 83 | +{ |
| 84 | + color_printf(TBOLD("delay") " postprocessor settings:\n"); |
| 85 | + color_printf( |
| 86 | + TBOLD(TRED("\t-p delay:[seconds=<s>|frames=<f>]")) " | " TBOLD( |
| 87 | + "-p delay:help") "\n"); |
| 88 | + color_printf("\nSeconds can be given as a decimal number.\n"); |
| 89 | + color_printf("\n"); |
| 90 | +} |
| 91 | + |
| 92 | +static void * |
| 93 | +delay_init(const char *config) |
| 94 | +{ |
| 95 | + if (!IS_KEY_PREFIX(config, "seconds") && |
| 96 | + !IS_KEY_PREFIX(config, "frames")) { |
| 97 | + usage(); |
| 98 | + return NULL; |
| 99 | + } |
| 100 | + |
| 101 | + const char *val_s = strchr(config, '=') + 1; |
| 102 | + double val = strtod(val_s, NULL); |
| 103 | + if (val <= 0 || val > INT_MAX) { |
| 104 | + MSG(ERROR, "Wrong delay value: %s\n", val_s); |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + if (IS_KEY_PREFIX(config, "frames") && fabs(round(val) - val) > 0.0) { |
| 108 | + MSG(ERROR, "Number of frames should be an integer, given: %s\n", |
| 109 | + val_s); |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + |
| 113 | + struct state_delay *s = calloc(1, sizeof(struct state_delay)); |
| 114 | + assert(s != NULL); |
| 115 | + if (IS_KEY_PREFIX(config, "seconds")) { |
| 116 | + s->delay_sec = val; |
| 117 | + } else { |
| 118 | + s->delay_frames = (int) val; |
| 119 | + } |
| 120 | + s->cached_frames = simple_linked_list_init(); |
| 121 | + |
| 122 | + return s; |
| 123 | +} |
| 124 | + |
| 125 | +static bool |
| 126 | +delay_reconfigure(void *state, struct video_desc desc) |
| 127 | +{ |
| 128 | + struct state_delay *s = (struct state_delay *) state; |
| 129 | + s->desc = desc; |
| 130 | + |
| 131 | + struct video_frame *f = NULL; |
| 132 | + while ((f = simple_linked_list_pop(s->cached_frames)) != NULL) { |
| 133 | + vf_free(f); |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +static struct video_frame * |
| 140 | +delay_getf(void *state) |
| 141 | +{ |
| 142 | + struct state_delay *s = state; |
| 143 | + |
| 144 | + return vf_alloc_desc_data(s->desc); |
| 145 | +} |
| 146 | + |
| 147 | +static bool |
| 148 | +delay_postprocess(void *state, struct video_frame *in, struct video_frame *out, |
| 149 | + int req_pitch) |
| 150 | +{ |
| 151 | + struct state_delay *s = state; |
| 152 | + |
| 153 | + simple_linked_list_append(s->cached_frames, in); |
| 154 | + |
| 155 | + const int list_size = simple_linked_list_size(s->cached_frames); |
| 156 | + if (s->delay_frames >= 0 && list_size < s->delay_frames) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + if (list_size / s->desc.fps < s->delay_sec) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + struct video_frame *f = simple_linked_list_pop(s->cached_frames); |
| 165 | + const size_t linesize = |
| 166 | + vc_get_linesize(f->tiles[0].width, f->color_spec); |
| 167 | + for (size_t i = 0; i < s->desc.height; ++i) { |
| 168 | + memcpy(out->tiles[0].data + i * req_pitch, |
| 169 | + f->tiles[0].data + i * linesize, linesize); |
| 170 | + } |
| 171 | + vf_free(f); |
| 172 | + |
| 173 | + return true; |
| 174 | +} |
| 175 | + |
| 176 | +static void |
| 177 | +delay_done(void *state) |
| 178 | +{ |
| 179 | + struct state_delay *s = state; |
| 180 | + |
| 181 | + struct video_frame *f = NULL; |
| 182 | + while ((f = simple_linked_list_pop(s->cached_frames)) != NULL) { |
| 183 | + vf_free(f); |
| 184 | + } |
| 185 | + simple_linked_list_destroy(s->cached_frames); |
| 186 | + free(s); |
| 187 | +} |
| 188 | + |
| 189 | +static void |
| 190 | +delay_get_out_desc(void *state, struct video_desc *out, int *in_display_mode, |
| 191 | + int *out_frames) |
| 192 | +{ |
| 193 | + struct state_delay *s = state; |
| 194 | + |
| 195 | + *out = s->desc; |
| 196 | + *in_display_mode = DISPLAY_PROPERTY_VIDEO_MERGED; |
| 197 | + *out_frames = 1; |
| 198 | +} |
| 199 | + |
| 200 | +static const struct vo_postprocess_info vo_pp_delay_info = { |
| 201 | + delay_init, delay_reconfigure, delay_getf, delay_get_out_desc, |
| 202 | + delay_get_property, delay_postprocess, delay_done, |
| 203 | +}; |
| 204 | + |
| 205 | +REGISTER_MODULE(delay, &vo_pp_delay_info, LIBRARY_CLASS_VIDEO_POSTPROCESS, |
| 206 | + VO_PP_ABI_VERSION); |
0 commit comments