| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2022 Paul B Mahol | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavutil/avassert.h" | ||
| 22 | #include "libavutil/channel_layout.h" | ||
| 23 | #include "libavutil/opt.h" | ||
| 24 | #include "libavutil/parseutils.h" | ||
| 25 | #include "avfilter.h" | ||
| 26 | #include "filters.h" | ||
| 27 | #include "formats.h" | ||
| 28 | #include "audio.h" | ||
| 29 | #include "video.h" | ||
| 30 | |||
| 31 | typedef struct Audio3dScopeContext { | ||
| 32 | const AVClass *class; | ||
| 33 | int w, h; | ||
| 34 | int size; | ||
| 35 | float fov; | ||
| 36 | float roll; | ||
| 37 | float pitch; | ||
| 38 | float yaw; | ||
| 39 | float zoom[3]; | ||
| 40 | float eye[3]; | ||
| 41 | |||
| 42 | AVRational frame_rate; | ||
| 43 | int nb_samples; | ||
| 44 | |||
| 45 | float view_matrix[4][4]; | ||
| 46 | float projection_matrix[4][4]; | ||
| 47 | |||
| 48 | AVFrame *frames[60]; | ||
| 49 | } Audio3dScopeContext; | ||
| 50 | |||
| 51 | #define OFFSET(x) offsetof(Audio3dScopeContext, x) | ||
| 52 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
| 53 | #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
| 54 | |||
| 55 | static const AVOption a3dscope_options[] = { | ||
| 56 | { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS }, | ||
| 57 | { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS }, | ||
| 58 | { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, | ||
| 59 | { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, | ||
| 60 | { "fov", "set camera FoV", OFFSET(fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 40, 150, TFLAGS }, | ||
| 61 | { "roll", "set camera roll",OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180, 180, TFLAGS }, | ||
| 62 | { "pitch","set camera pitch",OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180, 180, TFLAGS }, | ||
| 63 | { "yaw", "set camera yaw", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180, 180, TFLAGS }, | ||
| 64 | { "xzoom","set camera zoom", OFFSET(zoom[0]),AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.01, 10, TFLAGS }, | ||
| 65 | { "yzoom","set camera zoom", OFFSET(zoom[1]),AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.01, 10, TFLAGS }, | ||
| 66 | { "zzoom","set camera zoom", OFFSET(zoom[2]),AV_OPT_TYPE_FLOAT, {.dbl=1.f}, 0.01, 10, TFLAGS }, | ||
| 67 | { "xpos", "set camera position", OFFSET(eye[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.f},-60.f, 60.f, TFLAGS }, | ||
| 68 | { "ypos", "set camera position", OFFSET(eye[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.f},-60.f, 60.f, TFLAGS }, | ||
| 69 | { "zpos", "set camera position", OFFSET(eye[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.f},-60.f, 60.f, TFLAGS }, | ||
| 70 | { "length","set length", OFFSET(size), AV_OPT_TYPE_INT, {.i64=15}, 1, 60, FLAGS }, | ||
| 71 | { NULL } | ||
| 72 | }; | ||
| 73 | |||
| 74 | AVFILTER_DEFINE_CLASS(a3dscope); | ||
| 75 | |||
| 76 | ✗ | static int query_formats(const AVFilterContext *ctx, | |
| 77 | AVFilterFormatsConfig **cfg_in, | ||
| 78 | AVFilterFormatsConfig **cfg_out) | ||
| 79 | { | ||
| 80 | ✗ | AVFilterFormats *formats = NULL; | |
| 81 | static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; | ||
| 82 | static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE }; | ||
| 83 | int ret; | ||
| 84 | |||
| 85 | ✗ | formats = ff_make_format_list(sample_fmts); | |
| 86 | ✗ | if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0) | |
| 87 | ✗ | return ret; | |
| 88 | |||
| 89 | ✗ | formats = ff_make_format_list(pix_fmts); | |
| 90 | ✗ | if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0) | |
| 91 | ✗ | return ret; | |
| 92 | |||
| 93 | ✗ | return 0; | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | static int config_input(AVFilterLink *inlink) | |
| 97 | { | ||
| 98 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 99 | ✗ | Audio3dScopeContext *s = ctx->priv; | |
| 100 | |||
| 101 | ✗ | s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num)); | |
| 102 | |||
| 103 | ✗ | return 0; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | static int config_output(AVFilterLink *outlink) | |
| 107 | { | ||
| 108 | ✗ | FilterLink *l = ff_filter_link(outlink); | |
| 109 | ✗ | Audio3dScopeContext *s = outlink->src->priv; | |
| 110 | |||
| 111 | ✗ | outlink->w = s->w; | |
| 112 | ✗ | outlink->h = s->h; | |
| 113 | ✗ | outlink->sample_aspect_ratio = (AVRational){1,1}; | |
| 114 | ✗ | l->frame_rate = s->frame_rate; | |
| 115 | ✗ | outlink->time_base = av_inv_q(l->frame_rate); | |
| 116 | |||
| 117 | ✗ | return 0; | |
| 118 | } | ||
| 119 | |||
| 120 | ✗ | static void projection_matrix(float fov, float a, float near, float far, | |
| 121 | float matrix[4][4]) | ||
| 122 | { | ||
| 123 | float f; | ||
| 124 | |||
| 125 | ✗ | memset(matrix, 0, sizeof(*matrix)); | |
| 126 | |||
| 127 | ✗ | f = 1.0f / tanf(fov * 0.5f * M_PI / 180.f); | |
| 128 | ✗ | matrix[0][0] = f * a; | |
| 129 | ✗ | matrix[1][1] = f; | |
| 130 | ✗ | matrix[2][2] = -(far + near) / (far - near); | |
| 131 | ✗ | matrix[2][3] = -1.f; | |
| 132 | ✗ | matrix[3][2] = -(near * far) / (far - near); | |
| 133 | ✗ | } | |
| 134 | |||
| 135 | ✗ | static inline void vmultiply(const float v[4], const float m[4][4], float d[4]) | |
| 136 | { | ||
| 137 | ✗ | d[0] = v[0] * m[0][0] + v[1] * m[1][0] + v[2] * m[2][0] + v[3] * m[3][0]; | |
| 138 | ✗ | d[1] = v[0] * m[0][1] + v[1] * m[1][1] + v[2] * m[2][1] + v[3] * m[3][1]; | |
| 139 | ✗ | d[2] = v[0] * m[0][2] + v[1] * m[1][2] + v[2] * m[2][2] + v[3] * m[3][2]; | |
| 140 | ✗ | d[3] = v[0] * m[0][3] + v[1] * m[1][3] + v[2] * m[2][3] + v[3] * m[3][3]; | |
| 141 | ✗ | } | |
| 142 | |||
| 143 | ✗ | static void mmultiply(const float m2[4][4], const float m1[4][4], float m[4][4]) | |
| 144 | { | ||
| 145 | ✗ | vmultiply(m2[0], m1, m[0]); | |
| 146 | ✗ | vmultiply(m2[1], m1, m[1]); | |
| 147 | ✗ | vmultiply(m2[2], m1, m[2]); | |
| 148 | ✗ | vmultiply(m2[3], m1, m[3]); | |
| 149 | ✗ | } | |
| 150 | |||
| 151 | ✗ | static float vdot(const float x[3], const float y[3]) | |
| 152 | { | ||
| 153 | ✗ | return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]; | |
| 154 | } | ||
| 155 | |||
| 156 | ✗ | static void view_matrix(const float eye[3], | |
| 157 | const float z[3], | ||
| 158 | const float roll, | ||
| 159 | const float pitch, | ||
| 160 | const float yaw, float m[4][4]) | ||
| 161 | { | ||
| 162 | ✗ | float cr = cosf(roll * M_PI / 180.f); | |
| 163 | ✗ | float sr = sinf(roll * M_PI / 180.f); | |
| 164 | ✗ | float cp = cosf(pitch * M_PI / 180.f); | |
| 165 | ✗ | float sp = sinf(pitch * M_PI / 180.f); | |
| 166 | ✗ | float cy = cosf(yaw * M_PI / 180.f); | |
| 167 | ✗ | float sy = sinf(yaw * M_PI / 180.f); | |
| 168 | float t[4][4]; | ||
| 169 | ✗ | float rx[4][4] = { | |
| 170 | ✗ | {z[0], 0.f, 0.f, 0.f }, | |
| 171 | ✗ | { 0.f, cy, -sy, 0.f }, | |
| 172 | { 0.f, sy, cy, 0.f }, | ||
| 173 | { 0.f, 0.f, 0.f, 1.f }, | ||
| 174 | }; | ||
| 175 | |||
| 176 | ✗ | float ry[4][4] = { | |
| 177 | { cp, 0.f, sp, 0.f }, | ||
| 178 | ✗ | { 0.f,z[1], 0.f, 0.f }, | |
| 179 | ✗ | {-sp, 0.f, cp, 0.f }, | |
| 180 | { 0.f, 0.f, 0.f, 1.f }, | ||
| 181 | }; | ||
| 182 | |||
| 183 | ✗ | float rz[4][4] = { | |
| 184 | ✗ | { cr, -sr, 0.f, 0.f }, | |
| 185 | { sr, cr, 0.f, 0.f }, | ||
| 186 | ✗ | { 0.f, 0.f,z[2], 0.f }, | |
| 187 | { 0.f, 0.f, 0.f, 1.f }, | ||
| 188 | }; | ||
| 189 | |||
| 190 | ✗ | memset(m, 0, sizeof(*m)); | |
| 191 | |||
| 192 | ✗ | mmultiply(rx, ry, t); | |
| 193 | ✗ | mmultiply(rz, t, m); | |
| 194 | |||
| 195 | ✗ | m[3][0] = -vdot(m[0], eye); | |
| 196 | ✗ | m[3][1] = -vdot(m[1], eye); | |
| 197 | ✗ | m[3][2] = -vdot(m[2], eye); | |
| 198 | ✗ | } | |
| 199 | |||
| 200 | ✗ | static void draw_dot(AVFrame *out, unsigned x, unsigned y, float z, | |
| 201 | int r, int g, int b) | ||
| 202 | { | ||
| 203 | ✗ | const ptrdiff_t linesize = out->linesize[0]; | |
| 204 | uint8_t *dst; | ||
| 205 | |||
| 206 | ✗ | dst = out->data[0] + y * linesize + x * 4; | |
| 207 | ✗ | dst[0] = r * z; | |
| 208 | ✗ | dst[1] = g * z; | |
| 209 | ✗ | dst[2] = b * z; | |
| 210 | ✗ | dst[3] = 255 * z; | |
| 211 | ✗ | } | |
| 212 | |||
| 213 | ✗ | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 214 | { | ||
| 215 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 216 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 217 | ✗ | Audio3dScopeContext *s = ctx->priv; | |
| 218 | ✗ | const float half_height = (s->h - 1) * 0.5f; | |
| 219 | ✗ | const float half_width = (s->w - 1) * 0.5f; | |
| 220 | float matrix[4][4]; | ||
| 221 | ✗ | const int w = s->w; | |
| 222 | ✗ | const int h = s->h; | |
| 223 | AVFrame *out; | ||
| 224 | |||
| 225 | ✗ | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 226 | ✗ | if (!out) { | |
| 227 | ✗ | av_frame_free(&in); | |
| 228 | ✗ | return AVERROR(ENOMEM); | |
| 229 | } | ||
| 230 | |||
| 231 | ✗ | s->frames[0] = in; | |
| 232 | |||
| 233 | ✗ | out->sample_aspect_ratio = (AVRational){1,1}; | |
| 234 | ✗ | for (int y = 0; y < outlink->h; y++) | |
| 235 | ✗ | memset(out->data[0] + y * out->linesize[0], 0, outlink->w * 4); | |
| 236 | ✗ | out->pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base); | |
| 237 | ✗ | out->duration = 1; | |
| 238 | |||
| 239 | ✗ | projection_matrix(s->fov, half_width / half_height, 0.1f, 1000000.f, s->projection_matrix); | |
| 240 | ✗ | view_matrix(s->eye, s->zoom, s->roll, s->pitch, s->yaw, s->view_matrix); | |
| 241 | ✗ | mmultiply(s->projection_matrix, s->view_matrix, matrix); | |
| 242 | |||
| 243 | ✗ | for (int nb_frame = s->size - 1; nb_frame >= 0; nb_frame--) { | |
| 244 | ✗ | const float scale = 1.f / s->nb_samples; | |
| 245 | ✗ | AVFrame *frame = s->frames[nb_frame]; | |
| 246 | float channels; | ||
| 247 | |||
| 248 | ✗ | if (!frame) | |
| 249 | ✗ | continue; | |
| 250 | |||
| 251 | ✗ | channels = frame->ch_layout.nb_channels; | |
| 252 | ✗ | for (int ch = 0; ch < channels; ch++) { | |
| 253 | ✗ | const float *src = (float *)frame->extended_data[ch]; | |
| 254 | ✗ | const int r = 128.f + 127.f * sinf(ch / (channels - 1) * M_PI); | |
| 255 | ✗ | const int g = 128.f + 127.f * ch / (channels - 1); | |
| 256 | ✗ | const int b = 128.f + 127.f * cosf(ch / (channels - 1) * M_PI); | |
| 257 | |||
| 258 | ✗ | for (int n = frame->nb_samples - 1, nn = s->nb_samples * nb_frame; n >= 0; n--, nn++) { | |
| 259 | ✗ | float v[4] = { src[n], ch - (channels - 1) * 0.5f, -0.1f + -nn * scale, 1.f }; | |
| 260 | float d[4]; | ||
| 261 | int x, y; | ||
| 262 | |||
| 263 | ✗ | vmultiply(v, matrix, d); | |
| 264 | |||
| 265 | ✗ | d[0] /= d[3]; | |
| 266 | ✗ | d[1] /= d[3]; | |
| 267 | |||
| 268 | ✗ | x = d[0] * half_width + half_width; | |
| 269 | ✗ | y = d[1] * half_height + half_height; | |
| 270 | |||
| 271 | ✗ | if (x >= w || y >= h || x < 0 || y < 0) | |
| 272 | ✗ | continue; | |
| 273 | |||
| 274 | ✗ | draw_dot(out, x, y, av_clipf(1.f / d[3], 0.f, 1.f), | |
| 275 | r, g, b); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | ✗ | av_frame_free(&s->frames[59]); | |
| 281 | ✗ | memmove(&s->frames[1], &s->frames[0], 59 * sizeof(AVFrame *)); | |
| 282 | ✗ | s->frames[0] = NULL; | |
| 283 | |||
| 284 | ✗ | return ff_filter_frame(outlink, out); | |
| 285 | } | ||
| 286 | |||
| 287 | ✗ | static int activate(AVFilterContext *ctx) | |
| 288 | { | ||
| 289 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 290 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 291 | ✗ | Audio3dScopeContext *s = ctx->priv; | |
| 292 | AVFrame *in; | ||
| 293 | int ret; | ||
| 294 | |||
| 295 | ✗ | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); | |
| 296 | |||
| 297 | ✗ | ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in); | |
| 298 | ✗ | if (ret < 0) | |
| 299 | ✗ | return ret; | |
| 300 | ✗ | if (ret > 0) | |
| 301 | ✗ | return filter_frame(inlink, in); | |
| 302 | |||
| 303 | ✗ | if (ff_inlink_queued_samples(inlink) >= s->nb_samples) { | |
| 304 | ✗ | ff_filter_set_ready(ctx, 10); | |
| 305 | ✗ | return 0; | |
| 306 | } | ||
| 307 | |||
| 308 | ✗ | FF_FILTER_FORWARD_STATUS(inlink, outlink); | |
| 309 | ✗ | FF_FILTER_FORWARD_WANTED(outlink, inlink); | |
| 310 | |||
| 311 | ✗ | return FFERROR_NOT_READY; | |
| 312 | } | ||
| 313 | |||
| 314 | ✗ | static av_cold void uninit(AVFilterContext *ctx) | |
| 315 | { | ||
| 316 | ✗ | Audio3dScopeContext *s = ctx->priv; | |
| 317 | |||
| 318 | ✗ | for (int n = 0; n < 60; n++) | |
| 319 | ✗ | av_frame_free(&s->frames[n]); | |
| 320 | ✗ | } | |
| 321 | |||
| 322 | static const AVFilterPad audio3dscope_inputs[] = { | ||
| 323 | { | ||
| 324 | .name = "default", | ||
| 325 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 326 | .config_props = config_input, | ||
| 327 | }, | ||
| 328 | }; | ||
| 329 | |||
| 330 | static const AVFilterPad audio3dscope_outputs[] = { | ||
| 331 | { | ||
| 332 | .name = "default", | ||
| 333 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 334 | .config_props = config_output, | ||
| 335 | }, | ||
| 336 | }; | ||
| 337 | |||
| 338 | const FFFilter ff_avf_a3dscope = { | ||
| 339 | .p.name = "a3dscope", | ||
| 340 | .p.description = NULL_IF_CONFIG_SMALL("Convert input audio to 3d scope video output."), | ||
| 341 | .p.priv_class = &a3dscope_class, | ||
| 342 | .uninit = uninit, | ||
| 343 | .priv_size = sizeof(Audio3dScopeContext), | ||
| 344 | .activate = activate, | ||
| 345 | FILTER_INPUTS(audio3dscope_inputs), | ||
| 346 | FILTER_OUTPUTS(audio3dscope_outputs), | ||
| 347 | FILTER_QUERY_FUNC2(query_formats), | ||
| 348 | .process_command = ff_filter_process_command, | ||
| 349 | }; | ||
| 350 |