FFmpeg
Loading...
Searching...
No Matches
avf_a3dscope.c
Go to the documentation of this file.
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"
23#include "libavutil/opt.h"
25#include "avfilter.h"
26#include "filters.h"
27#include "formats.h"
28#include "audio.h"
29#include "video.h"
30
31typedef 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
44
45 float view_matrix[4][4];
46 float projection_matrix[4][4];
47
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
55static 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
75
77 AVFilterFormatsConfig **cfg_in,
78 AVFilterFormatsConfig **cfg_out)
79{
82 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
83 int ret;
84
86 if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0)
87 return ret;
88
90 if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0)
91 return ret;
92
93 return 0;
94}
95
96static 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
106static 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
120static 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
135static 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
143static 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
151static 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
156static 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
200static 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
213static 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
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) {
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
315{
316 Audio3dScopeContext *s = ctx->priv;
317
318 for (int n = 0; n < 60; n++)
319 av_frame_free(&s->frames[n]);
320}
321
323 {
324 .name = "default",
325 .type = AVMEDIA_TYPE_AUDIO,
326 .config_props = config_input,
327 },
328};
329
331 {
332 .name = "default",
333 .type = AVMEDIA_TYPE_VIDEO,
334 .config_props = config_output,
335 },
336};
337
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),
348 .process_command = ff_filter_process_command,
349};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int config_input(AVFilterLink *inlink)
#define TFLAGS
Definition af_afade.c:66
const FFFilter ff_avf_a3dscope
static FILE * out
static AVFormatContext * ctx
channels
Definition aptx.h:31
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVFilterPad audio3dscope_outputs[]
static float vdot(const float x[3], const float y[3])
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static void view_matrix(const float eye[3], const float z[3], const float roll, const float pitch, const float yaw, float m[4][4])
static void draw_dot(AVFrame *out, unsigned x, unsigned y, float z, int r, int g, int b)
static const AVOption a3dscope_options[]
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static void mmultiply(const float m2[4][4], const float m1[4][4], float m[4][4])
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad audio3dscope_inputs[]
static void vmultiply(const float v[4], const float m[4][4], float d[4])
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static void projection_matrix(float fov, float a, float near, float far, float matrix[4][4])
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1540
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1495
Main libavfilter public API header.
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static AVFrame * frame
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
av_warn_unused_result AVFilterFormats * ff_make_sample_format_list(const enum AVSampleFormat *fmts)
Create a list of supported sample formats.
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
#define sinf(x)
Definition libm.h:421
#define cosf(x)
Definition libm.h:80
uint8_t w
Definition llvidencdsp.c:39
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
AVOptions.
misc parsing utilities
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
AVRational frame_rate
float projection_matrix[4][4]
AVFrame * frames[60]
float view_matrix[4][4]
#define src
Definition vp8dsp.c:248
int size
const char * g
Definition vf_curves.c:128
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
static void zoom(float *u, float *v, float amount)
Definition vf_xfade.c:1695
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89