2 * Copyright (c) 2019 Eugene Lyapustin
4 * This file is part of FFmpeg.
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.
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.
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
23 * 360 video conversion filter.
24 * Principle of operation:
26 * (for each pixel in output frame)
27 * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
28 * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
29 * 3) Calculate pixel position (u, v) in input frame
30 * 4) Calculate interpolation window and weight for each pixel
33 * 5) Remap input frame to output frame using precalculated data
36 #include "libavutil/avassert.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/opt.h"
46 typedef struct ThreadData {
51 #define OFFSET(x) offsetof(V360Context, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption v360_options[] = {
55 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
56 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
57 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
58 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
59 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
60 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
61 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
62 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
63 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
64 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
65 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
66 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
67 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
68 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
69 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
70 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
71 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
72 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
73 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
74 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
75 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
76 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
77 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
78 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
79 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
80 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
81 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
82 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
83 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
84 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
85 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
86 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
87 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
88 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
89 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
90 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
91 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
92 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
93 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
94 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
95 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
96 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
97 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
98 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
99 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 180.f, FLAGS, "h_fov"},
100 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 90.f, FLAGS, "v_fov"},
101 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
102 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
103 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
104 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
105 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
106 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
107 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
111 AVFILTER_DEFINE_CLASS(v360);
113 static int query_formats(AVFilterContext *ctx)
115 static const enum AVPixelFormat pix_fmts[] = {
117 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
118 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
119 AV_PIX_FMT_YUVA444P16,
122 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
123 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
124 AV_PIX_FMT_YUVA422P16,
127 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
128 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
131 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
132 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
136 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
137 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
138 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
141 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
142 AV_PIX_FMT_YUV440P12,
145 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
146 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
147 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
150 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
151 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
152 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
161 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
162 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
163 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
166 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
167 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
170 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
171 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
172 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
177 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
179 return AVERROR(ENOMEM);
180 return ff_set_common_formats(ctx, fmts_list);
183 #define DEFINE_REMAP1_LINE(bits, div) \
184 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
185 ptrdiff_t in_linesize, \
186 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
188 const uint##bits##_t *s = (const uint##bits##_t *)src; \
189 uint##bits##_t *d = (uint##bits##_t *)dst; \
191 in_linesize /= div; \
193 for (int x = 0; x < width; x++) \
194 d[x] = s[v[x] * in_linesize + u[x]]; \
197 DEFINE_REMAP1_LINE( 8, 1)
198 DEFINE_REMAP1_LINE(16, 2)
200 typedef struct XYRemap {
207 * Generate remapping function with a given window size and pixel depth.
209 * @param ws size of interpolation window
210 * @param bits number of bits per pixel
212 #define DEFINE_REMAP(ws, bits) \
213 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
215 ThreadData *td = (ThreadData*)arg; \
216 const V360Context *s = ctx->priv; \
217 const AVFrame *in = td->in; \
218 AVFrame *out = td->out; \
220 for (int plane = 0; plane < s->nb_planes; plane++) { \
221 const int in_linesize = in->linesize[plane]; \
222 const int out_linesize = out->linesize[plane]; \
223 const uint8_t *src = in->data[plane]; \
224 uint8_t *dst = out->data[plane]; \
225 const int width = s->planewidth[plane]; \
226 const int height = s->planeheight[plane]; \
228 const int slice_start = (height * jobnr ) / nb_jobs; \
229 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
231 for (int y = slice_start; y < slice_end; y++) { \
232 const unsigned map = s->map[plane]; \
233 const uint16_t *u = s->u[map] + y * width * ws * ws; \
234 const uint16_t *v = s->v[map] + y * width * ws * ws; \
235 const int16_t *ker = s->ker[map] + y * width * ws * ws; \
237 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
251 #define DEFINE_REMAP_LINE(ws, bits, div) \
252 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
253 ptrdiff_t in_linesize, \
254 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
256 const uint##bits##_t *s = (const uint##bits##_t *)src; \
257 uint##bits##_t *d = (uint##bits##_t *)dst; \
259 in_linesize /= div; \
261 for (int x = 0; x < width; x++) { \
262 const uint16_t *uu = u + x * ws * ws; \
263 const uint16_t *vv = v + x * ws * ws; \
264 const int16_t *kker = ker + x * ws * ws; \
267 for (int i = 0; i < ws; i++) { \
268 for (int j = 0; j < ws; j++) { \
269 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
273 d[x] = av_clip_uint##bits(tmp >> 14); \
277 DEFINE_REMAP_LINE(2, 8, 1)
278 DEFINE_REMAP_LINE(4, 8, 1)
279 DEFINE_REMAP_LINE(2, 16, 2)
280 DEFINE_REMAP_LINE(4, 16, 2)
282 void ff_v360_init(V360Context *s, int depth)
286 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
289 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
293 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
298 ff_v360_init_x86(s, depth);
302 * Save nearest pixel coordinates for remapping.
304 * @param du horizontal relative coordinate
305 * @param dv vertical relative coordinate
306 * @param r_tmp calculated 4x4 window
307 * @param u u remap data
308 * @param v v remap data
309 * @param ker ker remap data
311 static void nearest_kernel(float du, float dv, const XYRemap *r_tmp,
312 uint16_t *u, uint16_t *v, int16_t *ker)
314 const int i = roundf(dv) + 1;
315 const int j = roundf(du) + 1;
317 u[0] = r_tmp->u[i][j];
318 v[0] = r_tmp->v[i][j];
322 * Calculate kernel for bilinear interpolation.
324 * @param du horizontal relative coordinate
325 * @param dv vertical relative coordinate
326 * @param r_tmp calculated 4x4 window
327 * @param u u remap data
328 * @param v v remap data
329 * @param ker ker remap data
331 static void bilinear_kernel(float du, float dv, const XYRemap *r_tmp,
332 uint16_t *u, uint16_t *v, int16_t *ker)
336 for (i = 0; i < 2; i++) {
337 for (j = 0; j < 2; j++) {
338 u[i * 2 + j] = r_tmp->u[i + 1][j + 1];
339 v[i * 2 + j] = r_tmp->v[i + 1][j + 1];
343 ker[0] = (1.f - du) * (1.f - dv) * 16384;
344 ker[1] = du * (1.f - dv) * 16384;
345 ker[2] = (1.f - du) * dv * 16384;
346 ker[3] = du * dv * 16384;
350 * Calculate 1-dimensional cubic coefficients.
352 * @param t relative coordinate
353 * @param coeffs coefficients
355 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
357 const float tt = t * t;
358 const float ttt = t * t * t;
360 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
361 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
362 coeffs[2] = t + tt / 2.f - ttt / 2.f;
363 coeffs[3] = - t / 6.f + ttt / 6.f;
367 * Calculate kernel for bicubic interpolation.
369 * @param du horizontal relative coordinate
370 * @param dv vertical relative coordinate
371 * @param r_tmp calculated 4x4 window
372 * @param u u remap data
373 * @param v v remap data
374 * @param ker ker remap data
376 static void bicubic_kernel(float du, float dv, const XYRemap *r_tmp,
377 uint16_t *u, uint16_t *v, int16_t *ker)
383 calculate_bicubic_coeffs(du, du_coeffs);
384 calculate_bicubic_coeffs(dv, dv_coeffs);
386 for (i = 0; i < 4; i++) {
387 for (j = 0; j < 4; j++) {
388 u[i * 4 + j] = r_tmp->u[i][j];
389 v[i * 4 + j] = r_tmp->v[i][j];
390 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
396 * Calculate 1-dimensional lanczos coefficients.
398 * @param t relative coordinate
399 * @param coeffs coefficients
401 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
406 for (i = 0; i < 4; i++) {
407 const float x = M_PI * (t - i + 1);
411 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
416 for (i = 0; i < 4; i++) {
422 * Calculate kernel for lanczos interpolation.
424 * @param du horizontal relative coordinate
425 * @param dv vertical relative coordinate
426 * @param r_tmp calculated 4x4 window
427 * @param u u remap data
428 * @param v v remap data
429 * @param ker ker remap data
431 static void lanczos_kernel(float du, float dv, const XYRemap *r_tmp,
432 uint16_t *u, uint16_t *v, int16_t *ker)
438 calculate_lanczos_coeffs(du, du_coeffs);
439 calculate_lanczos_coeffs(dv, dv_coeffs);
441 for (i = 0; i < 4; i++) {
442 for (j = 0; j < 4; j++) {
443 u[i * 4 + j] = r_tmp->u[i][j];
444 v[i * 4 + j] = r_tmp->v[i][j];
445 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
451 * Modulo operation with only positive remainders.
456 * @return positive remainder of (a / b)
458 static inline int mod(int a, int b)
460 const int res = a % b;
469 * Convert char to corresponding direction.
470 * Used for cubemap options.
472 static int get_direction(char c)
493 * Convert char to corresponding rotation angle.
494 * Used for cubemap options.
496 static int get_rotation(char c)
513 * Convert char to corresponding rotation order.
515 static int get_rorder(char c)
533 * Prepare data for processing cubemap input format.
535 * @param ctx filter context
539 static int prepare_cube_in(AVFilterContext *ctx)
541 V360Context *s = ctx->priv;
543 for (int face = 0; face < NB_FACES; face++) {
544 const char c = s->in_forder[face];
548 av_log(ctx, AV_LOG_ERROR,
549 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
550 return AVERROR(EINVAL);
553 direction = get_direction(c);
554 if (direction == -1) {
555 av_log(ctx, AV_LOG_ERROR,
556 "Incorrect direction symbol '%c' in in_forder option.\n", c);
557 return AVERROR(EINVAL);
560 s->in_cubemap_face_order[direction] = face;
563 for (int face = 0; face < NB_FACES; face++) {
564 const char c = s->in_frot[face];
568 av_log(ctx, AV_LOG_ERROR,
569 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
570 return AVERROR(EINVAL);
573 rotation = get_rotation(c);
574 if (rotation == -1) {
575 av_log(ctx, AV_LOG_ERROR,
576 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
577 return AVERROR(EINVAL);
580 s->in_cubemap_face_rotation[face] = rotation;
587 * Prepare data for processing cubemap output format.
589 * @param ctx filter context
593 static int prepare_cube_out(AVFilterContext *ctx)
595 V360Context *s = ctx->priv;
597 for (int face = 0; face < NB_FACES; face++) {
598 const char c = s->out_forder[face];
602 av_log(ctx, AV_LOG_ERROR,
603 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
604 return AVERROR(EINVAL);
607 direction = get_direction(c);
608 if (direction == -1) {
609 av_log(ctx, AV_LOG_ERROR,
610 "Incorrect direction symbol '%c' in out_forder option.\n", c);
611 return AVERROR(EINVAL);
614 s->out_cubemap_direction_order[face] = direction;
617 for (int face = 0; face < NB_FACES; face++) {
618 const char c = s->out_frot[face];
622 av_log(ctx, AV_LOG_ERROR,
623 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
624 return AVERROR(EINVAL);
627 rotation = get_rotation(c);
628 if (rotation == -1) {
629 av_log(ctx, AV_LOG_ERROR,
630 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
631 return AVERROR(EINVAL);
634 s->out_cubemap_face_rotation[face] = rotation;
640 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
666 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
697 static void normalize_vector(float *vec)
699 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
707 * Calculate 3D coordinates on sphere for corresponding cubemap position.
708 * Common operation for every cubemap.
710 * @param s filter context
711 * @param uf horizontal cubemap coordinate [0, 1)
712 * @param vf vertical cubemap coordinate [0, 1)
713 * @param face face of cubemap
714 * @param vec coordinates on sphere
716 static void cube_to_xyz(const V360Context *s,
717 float uf, float vf, int face,
720 const int direction = s->out_cubemap_direction_order[face];
723 uf /= (1.f - s->out_pad);
724 vf /= (1.f - s->out_pad);
726 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
765 normalize_vector(vec);
769 * Calculate cubemap position for corresponding 3D coordinates on sphere.
770 * Common operation for every cubemap.
772 * @param s filter context
773 * @param vec coordinated on sphere
774 * @param uf horizontal cubemap coordinate [0, 1)
775 * @param vf vertical cubemap coordinate [0, 1)
776 * @param direction direction of view
778 static void xyz_to_cube(const V360Context *s,
780 float *uf, float *vf, int *direction)
782 const float phi = atan2f(vec[0], -vec[2]);
783 const float theta = asinf(-vec[1]);
784 float phi_norm, theta_threshold;
787 if (phi >= -M_PI_4 && phi < M_PI_4) {
790 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
792 phi_norm = phi + M_PI_2;
793 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
795 phi_norm = phi - M_PI_2;
798 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
801 theta_threshold = atanf(cosf(phi_norm));
802 if (theta > theta_threshold) {
804 } else if (theta < -theta_threshold) {
808 switch (*direction) {
810 *uf = vec[2] / vec[0];
811 *vf = -vec[1] / vec[0];
814 *uf = vec[2] / vec[0];
815 *vf = vec[1] / vec[0];
818 *uf = vec[0] / vec[1];
819 *vf = -vec[2] / vec[1];
822 *uf = -vec[0] / vec[1];
823 *vf = -vec[2] / vec[1];
826 *uf = -vec[0] / vec[2];
827 *vf = vec[1] / vec[2];
830 *uf = -vec[0] / vec[2];
831 *vf = -vec[1] / vec[2];
837 face = s->in_cubemap_face_order[*direction];
838 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
840 (*uf) *= s->input_mirror_modifier[0];
841 (*vf) *= s->input_mirror_modifier[1];
845 * Find position on another cube face in case of overflow/underflow.
846 * Used for calculation of interpolation window.
848 * @param s filter context
849 * @param uf horizontal cubemap coordinate
850 * @param vf vertical cubemap coordinate
851 * @param direction direction of view
852 * @param new_uf new horizontal cubemap coordinate
853 * @param new_vf new vertical cubemap coordinate
854 * @param face face position on cubemap
856 static void process_cube_coordinates(const V360Context *s,
857 float uf, float vf, int direction,
858 float *new_uf, float *new_vf, int *face)
861 * Cubemap orientation
868 * +-------+-------+-------+-------+ ^ e |
870 * | left | front | right | back | | g |
871 * +-------+-------+-------+-------+ v h v
877 *face = s->in_cubemap_face_order[direction];
878 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
880 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
881 // There are no pixels to use in this case
884 } else if (uf < -1.f) {
920 } else if (uf >= 1.f) {
956 } else if (vf < -1.f) {
992 } else if (vf >= 1.f) {
1034 *face = s->in_cubemap_face_order[direction];
1035 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1039 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1041 * @param s filter context
1042 * @param i horizontal position on frame [0, height)
1043 * @param j vertical position on frame [0, width)
1044 * @param width frame width
1045 * @param height frame height
1046 * @param vec coordinates on sphere
1048 static void cube3x2_to_xyz(const V360Context *s,
1049 int i, int j, int width, int height,
1052 const float ew = width / 3.f;
1053 const float eh = height / 2.f;
1055 const int u_face = floorf(i / ew);
1056 const int v_face = floorf(j / eh);
1057 const int face = u_face + 3 * v_face;
1059 const int u_shift = ceilf(ew * u_face);
1060 const int v_shift = ceilf(eh * v_face);
1061 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1062 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1064 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1065 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1067 cube_to_xyz(s, uf, vf, face, vec);
1071 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1073 * @param s filter context
1074 * @param vec coordinates on sphere
1075 * @param width frame width
1076 * @param height frame height
1077 * @param us horizontal coordinates for interpolation window
1078 * @param vs vertical coordinates for interpolation window
1079 * @param du horizontal relative coordinate
1080 * @param dv vertical relative coordinate
1082 static void xyz_to_cube3x2(const V360Context *s,
1083 const float *vec, int width, int height,
1084 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1086 const float ew = width / 3.f;
1087 const float eh = height / 2.f;
1092 int direction, face;
1095 xyz_to_cube(s, vec, &uf, &vf, &direction);
1097 uf *= (1.f - s->in_pad);
1098 vf *= (1.f - s->in_pad);
1100 face = s->in_cubemap_face_order[direction];
1103 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1104 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1106 uf = 0.5f * ewi * (uf + 1.f);
1107 vf = 0.5f * ehi * (vf + 1.f);
1115 for (i = -1; i < 3; i++) {
1116 for (j = -1; j < 3; j++) {
1117 int new_ui = ui + j;
1118 int new_vi = vi + i;
1119 int u_shift, v_shift;
1120 int new_ewi, new_ehi;
1122 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1123 face = s->in_cubemap_face_order[direction];
1127 u_shift = ceilf(ew * u_face);
1128 v_shift = ceilf(eh * v_face);
1130 uf = 2.f * new_ui / ewi - 1.f;
1131 vf = 2.f * new_vi / ehi - 1.f;
1133 uf /= (1.f - s->in_pad);
1134 vf /= (1.f - s->in_pad);
1136 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1138 uf *= (1.f - s->in_pad);
1139 vf *= (1.f - s->in_pad);
1143 u_shift = ceilf(ew * u_face);
1144 v_shift = ceilf(eh * v_face);
1145 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1146 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1148 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1149 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1152 us[i + 1][j + 1] = u_shift + new_ui;
1153 vs[i + 1][j + 1] = v_shift + new_vi;
1159 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1161 * @param s filter context
1162 * @param i horizontal position on frame [0, height)
1163 * @param j vertical position on frame [0, width)
1164 * @param width frame width
1165 * @param height frame height
1166 * @param vec coordinates on sphere
1168 static void cube1x6_to_xyz(const V360Context *s,
1169 int i, int j, int width, int height,
1172 const float ew = width;
1173 const float eh = height / 6.f;
1175 const int face = floorf(j / eh);
1177 const int v_shift = ceilf(eh * face);
1178 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1180 const float uf = 2.f * i / ew - 1.f;
1181 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1183 cube_to_xyz(s, uf, vf, face, vec);
1187 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1189 * @param s filter context
1190 * @param i horizontal position on frame [0, height)
1191 * @param j vertical position on frame [0, width)
1192 * @param width frame width
1193 * @param height frame height
1194 * @param vec coordinates on sphere
1196 static void cube6x1_to_xyz(const V360Context *s,
1197 int i, int j, int width, int height,
1200 const float ew = width / 6.f;
1201 const float eh = height;
1203 const int face = floorf(i / ew);
1205 const int u_shift = ceilf(ew * face);
1206 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1208 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1209 const float vf = 2.f * j / eh - 1.f;
1211 cube_to_xyz(s, uf, vf, face, vec);
1215 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1217 * @param s filter context
1218 * @param vec coordinates on sphere
1219 * @param width frame width
1220 * @param height frame height
1221 * @param us horizontal coordinates for interpolation window
1222 * @param vs vertical coordinates for interpolation window
1223 * @param du horizontal relative coordinate
1224 * @param dv vertical relative coordinate
1226 static void xyz_to_cube1x6(const V360Context *s,
1227 const float *vec, int width, int height,
1228 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1230 const float eh = height / 6.f;
1231 const int ewi = width;
1236 int direction, face;
1238 xyz_to_cube(s, vec, &uf, &vf, &direction);
1240 uf *= (1.f - s->in_pad);
1241 vf *= (1.f - s->in_pad);
1243 face = s->in_cubemap_face_order[direction];
1244 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1246 uf = 0.5f * ewi * (uf + 1.f);
1247 vf = 0.5f * ehi * (vf + 1.f);
1255 for (i = -1; i < 3; i++) {
1256 for (j = -1; j < 3; j++) {
1257 int new_ui = ui + j;
1258 int new_vi = vi + i;
1262 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1263 face = s->in_cubemap_face_order[direction];
1265 v_shift = ceilf(eh * face);
1267 uf = 2.f * new_ui / ewi - 1.f;
1268 vf = 2.f * new_vi / ehi - 1.f;
1270 uf /= (1.f - s->in_pad);
1271 vf /= (1.f - s->in_pad);
1273 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1275 uf *= (1.f - s->in_pad);
1276 vf *= (1.f - s->in_pad);
1278 v_shift = ceilf(eh * face);
1279 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1281 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1282 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1285 us[i + 1][j + 1] = new_ui;
1286 vs[i + 1][j + 1] = v_shift + new_vi;
1292 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1294 * @param s filter context
1295 * @param vec coordinates on sphere
1296 * @param width frame width
1297 * @param height frame height
1298 * @param us horizontal coordinates for interpolation window
1299 * @param vs vertical coordinates for interpolation window
1300 * @param du horizontal relative coordinate
1301 * @param dv vertical relative coordinate
1303 static void xyz_to_cube6x1(const V360Context *s,
1304 const float *vec, int width, int height,
1305 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1307 const float ew = width / 6.f;
1308 const int ehi = height;
1313 int direction, face;
1315 xyz_to_cube(s, vec, &uf, &vf, &direction);
1317 uf *= (1.f - s->in_pad);
1318 vf *= (1.f - s->in_pad);
1320 face = s->in_cubemap_face_order[direction];
1321 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1323 uf = 0.5f * ewi * (uf + 1.f);
1324 vf = 0.5f * ehi * (vf + 1.f);
1332 for (i = -1; i < 3; i++) {
1333 for (j = -1; j < 3; j++) {
1334 int new_ui = ui + j;
1335 int new_vi = vi + i;
1339 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1340 face = s->in_cubemap_face_order[direction];
1342 u_shift = ceilf(ew * face);
1344 uf = 2.f * new_ui / ewi - 1.f;
1345 vf = 2.f * new_vi / ehi - 1.f;
1347 uf /= (1.f - s->in_pad);
1348 vf /= (1.f - s->in_pad);
1350 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1352 uf *= (1.f - s->in_pad);
1353 vf *= (1.f - s->in_pad);
1355 u_shift = ceilf(ew * face);
1356 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1358 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1359 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1362 us[i + 1][j + 1] = u_shift + new_ui;
1363 vs[i + 1][j + 1] = new_vi;
1369 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1371 * @param s filter context
1372 * @param i horizontal position on frame [0, height)
1373 * @param j vertical position on frame [0, width)
1374 * @param width frame width
1375 * @param height frame height
1376 * @param vec coordinates on sphere
1378 static void equirect_to_xyz(const V360Context *s,
1379 int i, int j, int width, int height,
1382 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1383 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1385 const float sin_phi = sinf(phi);
1386 const float cos_phi = cosf(phi);
1387 const float sin_theta = sinf(theta);
1388 const float cos_theta = cosf(theta);
1390 vec[0] = cos_theta * sin_phi;
1391 vec[1] = -sin_theta;
1392 vec[2] = -cos_theta * cos_phi;
1396 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1398 * @param s filter context
1399 * @param i horizontal position on frame [0, height)
1400 * @param j vertical position on frame [0, width)
1401 * @param width frame width
1402 * @param height frame height
1403 * @param vec coordinates on sphere
1405 static void stereographic_to_xyz(const V360Context *s,
1406 int i, int j, int width, int height,
1409 const float x = ((2.f * i) / width - 1.f) * (s->h_fov / 180.f) * M_PI;
1410 const float y = ((2.f * j) / height - 1.f) * (s->v_fov / 90.f) * M_PI_2;
1411 const float xy = x * x + y * y;
1413 vec[0] = 2.f * x / (1.f + xy);
1414 vec[1] = (-1.f + xy) / (1.f + xy);
1415 vec[2] = 2.f * y / (1.f + xy);
1417 normalize_vector(vec);
1421 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1423 * @param s filter context
1424 * @param vec coordinates on sphere
1425 * @param width frame width
1426 * @param height frame height
1427 * @param us horizontal coordinates for interpolation window
1428 * @param vs vertical coordinates for interpolation window
1429 * @param du horizontal relative coordinate
1430 * @param dv vertical relative coordinate
1432 static void xyz_to_equirect(const V360Context *s,
1433 const float *vec, int width, int height,
1434 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1436 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1437 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1442 uf = (phi / M_PI + 1.f) * width / 2.f;
1443 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1450 for (i = -1; i < 3; i++) {
1451 for (j = -1; j < 3; j++) {
1452 us[i + 1][j + 1] = mod(ui + j, width);
1453 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1459 * Prepare data for processing equi-angular cubemap input format.
1461 * @param ctx filter context
1463 * @return error code
1465 static int prepare_eac_in(AVFilterContext *ctx)
1467 V360Context *s = ctx->priv;
1469 if (s->ih_flip && s->iv_flip) {
1470 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1471 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1472 s->in_cubemap_face_order[UP] = TOP_LEFT;
1473 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1474 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1475 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1476 } else if (s->ih_flip) {
1477 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1478 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1479 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1480 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1481 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1482 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1483 } else if (s->iv_flip) {
1484 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1485 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1486 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1487 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1488 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1489 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1491 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1492 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1493 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1494 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1495 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1496 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1500 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1501 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1502 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1503 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1504 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1505 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1507 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1508 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1509 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1510 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1511 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1512 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1519 * Prepare data for processing equi-angular cubemap output format.
1521 * @param ctx filter context
1523 * @return error code
1525 static int prepare_eac_out(AVFilterContext *ctx)
1527 V360Context *s = ctx->priv;
1529 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1530 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1531 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1532 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1533 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1534 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1536 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1537 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1538 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1539 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1540 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1541 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1547 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1549 * @param s filter context
1550 * @param i horizontal position on frame [0, height)
1551 * @param j vertical position on frame [0, width)
1552 * @param width frame width
1553 * @param height frame height
1554 * @param vec coordinates on sphere
1556 static void eac_to_xyz(const V360Context *s,
1557 int i, int j, int width, int height,
1560 const float pixel_pad = 2;
1561 const float u_pad = pixel_pad / width;
1562 const float v_pad = pixel_pad / height;
1564 int u_face, v_face, face;
1566 float l_x, l_y, l_z;
1568 float uf = (float)i / width;
1569 float vf = (float)j / height;
1571 // EAC has 2-pixel padding on faces except between faces on the same row
1572 // Padding pixels seems not to be stretched with tangent as regular pixels
1573 // Formulas below approximate original padding as close as I could get experimentally
1575 // Horizontal padding
1576 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1580 } else if (uf >= 3.f) {
1584 u_face = floorf(uf);
1585 uf = fmodf(uf, 1.f) - 0.5f;
1589 v_face = floorf(vf * 2.f);
1590 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1592 if (uf >= -0.5f && uf < 0.5f) {
1593 uf = tanf(M_PI_2 * uf);
1597 if (vf >= -0.5f && vf < 0.5f) {
1598 vf = tanf(M_PI_2 * vf);
1603 face = u_face + 3 * v_face;
1644 normalize_vector(vec);
1648 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1650 * @param s filter context
1651 * @param vec coordinates on sphere
1652 * @param width frame width
1653 * @param height frame height
1654 * @param us horizontal coordinates for interpolation window
1655 * @param vs vertical coordinates for interpolation window
1656 * @param du horizontal relative coordinate
1657 * @param dv vertical relative coordinate
1659 static void xyz_to_eac(const V360Context *s,
1660 const float *vec, int width, int height,
1661 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1663 const float pixel_pad = 2;
1664 const float u_pad = pixel_pad / width;
1665 const float v_pad = pixel_pad / height;
1670 int direction, face;
1673 xyz_to_cube(s, vec, &uf, &vf, &direction);
1675 face = s->in_cubemap_face_order[direction];
1679 uf = M_2_PI * atanf(uf) + 0.5f;
1680 vf = M_2_PI * atanf(vf) + 0.5f;
1682 // These formulas are inversed from eac_to_xyz ones
1683 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1684 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1695 for (i = -1; i < 3; i++) {
1696 for (j = -1; j < 3; j++) {
1697 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1698 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1704 * Prepare data for processing flat output format.
1706 * @param ctx filter context
1708 * @return error code
1710 static int prepare_flat_out(AVFilterContext *ctx)
1712 V360Context *s = ctx->priv;
1714 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1715 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1717 const float sin_phi = sinf(h_angle);
1718 const float cos_phi = cosf(h_angle);
1719 const float sin_theta = sinf(v_angle);
1720 const float cos_theta = cosf(v_angle);
1722 s->flat_range[0] = cos_theta * sin_phi;
1723 s->flat_range[1] = sin_theta;
1724 s->flat_range[2] = -cos_theta * cos_phi;
1730 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1732 * @param s filter context
1733 * @param i horizontal position on frame [0, height)
1734 * @param j vertical position on frame [0, width)
1735 * @param width frame width
1736 * @param height frame height
1737 * @param vec coordinates on sphere
1739 static void flat_to_xyz(const V360Context *s,
1740 int i, int j, int width, int height,
1743 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
1744 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1745 const float l_z = s->flat_range[2];
1751 normalize_vector(vec);
1755 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1757 * @param s filter context
1758 * @param vec coordinates on sphere
1759 * @param width frame width
1760 * @param height frame height
1761 * @param us horizontal coordinates for interpolation window
1762 * @param vs vertical coordinates for interpolation window
1763 * @param du horizontal relative coordinate
1764 * @param dv vertical relative coordinate
1766 static void xyz_to_dfisheye(const V360Context *s,
1767 const float *vec, int width, int height,
1768 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1770 const float scale = 1.f - s->in_pad;
1772 const float ew = width / 2.f;
1773 const float eh = height;
1775 const float phi = atan2f(-vec[1], -vec[0]) * s->input_mirror_modifier[0];
1776 const float theta = acosf(fabsf(vec[2])) / M_PI * s->input_mirror_modifier[1];
1778 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1779 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1788 u_shift = ceilf(ew);
1798 for (i = -1; i < 3; i++) {
1799 for (j = -1; j < 3; j++) {
1800 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
1801 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
1807 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1809 * @param s filter context
1810 * @param i horizontal position on frame [0, height)
1811 * @param j vertical position on frame [0, width)
1812 * @param width frame width
1813 * @param height frame height
1814 * @param vec coordinates on sphere
1816 static void barrel_to_xyz(const V360Context *s,
1817 int i, int j, int width, int height,
1820 const float scale = 0.99f;
1821 float l_x, l_y, l_z;
1823 if (i < 4 * width / 5) {
1824 const float theta_range = M_PI / 4.f;
1826 const int ew = 4 * width / 5;
1827 const int eh = height;
1829 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1830 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1832 const float sin_phi = sinf(phi);
1833 const float cos_phi = cosf(phi);
1834 const float sin_theta = sinf(theta);
1835 const float cos_theta = cosf(theta);
1837 l_x = cos_theta * sin_phi;
1839 l_z = -cos_theta * cos_phi;
1841 const int ew = width / 5;
1842 const int eh = height / 2;
1847 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1848 vf = 2.f * (j ) / eh - 1.f;
1857 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1858 vf = 2.f * (j - eh) / eh - 1.f;
1873 normalize_vector(vec);
1877 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
1879 * @param s filter context
1880 * @param vec coordinates on sphere
1881 * @param width frame width
1882 * @param height frame height
1883 * @param us horizontal coordinates for interpolation window
1884 * @param vs vertical coordinates for interpolation window
1885 * @param du horizontal relative coordinate
1886 * @param dv vertical relative coordinate
1888 static void xyz_to_barrel(const V360Context *s,
1889 const float *vec, int width, int height,
1890 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1892 const float scale = 0.99f;
1894 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1895 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1896 const float theta_range = M_PI / 4.f;
1899 int u_shift, v_shift;
1904 if (theta > -theta_range && theta < theta_range) {
1908 u_shift = s->ih_flip ? width / 5 : 0;
1911 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
1912 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1917 u_shift = s->ih_flip ? 0 : 4 * ew;
1919 if (theta < 0.f) { // UP
1920 uf = vec[0] / vec[1];
1921 vf = -vec[2] / vec[1];
1924 uf = -vec[0] / vec[1];
1925 vf = -vec[2] / vec[1];
1929 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
1930 vf *= s->input_mirror_modifier[1];
1932 uf = 0.5f * ew * (uf * scale + 1.f);
1933 vf = 0.5f * eh * (vf * scale + 1.f);
1942 for (i = -1; i < 3; i++) {
1943 for (j = -1; j < 3; j++) {
1944 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1945 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1950 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
1952 for (int i = 0; i < 3; i++) {
1953 for (int j = 0; j < 3; j++) {
1956 for (int k = 0; k < 3; k++)
1957 sum += a[i][k] * b[k][j];
1965 * Calculate rotation matrix for yaw/pitch/roll angles.
1967 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
1968 float rot_mat[3][3],
1969 const int rotation_order[3])
1971 const float yaw_rad = yaw * M_PI / 180.f;
1972 const float pitch_rad = pitch * M_PI / 180.f;
1973 const float roll_rad = roll * M_PI / 180.f;
1975 const float sin_yaw = sinf(-yaw_rad);
1976 const float cos_yaw = cosf(-yaw_rad);
1977 const float sin_pitch = sinf(pitch_rad);
1978 const float cos_pitch = cosf(pitch_rad);
1979 const float sin_roll = sinf(roll_rad);
1980 const float cos_roll = cosf(roll_rad);
1985 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
1986 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
1987 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
1989 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
1990 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
1991 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
1993 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
1994 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
1995 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
1997 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
1998 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2002 * Rotate vector with given rotation matrix.
2004 * @param rot_mat rotation matrix
2007 static inline void rotate(const float rot_mat[3][3],
2010 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2011 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2012 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2019 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2022 modifier[0] = h_flip ? -1.f : 1.f;
2023 modifier[1] = v_flip ? -1.f : 1.f;
2024 modifier[2] = d_flip ? -1.f : 1.f;
2027 static inline void mirror(const float *modifier, float *vec)
2029 vec[0] *= modifier[0];
2030 vec[1] *= modifier[1];
2031 vec[2] *= modifier[2];
2034 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2036 s->u[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
2037 s->v[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
2038 if (!s->u[p] || !s->v[p])
2039 return AVERROR(ENOMEM);
2041 s->ker[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_ker);
2043 return AVERROR(ENOMEM);
2049 static int config_output(AVFilterLink *outlink)
2051 AVFilterContext *ctx = outlink->src;
2052 AVFilterLink *inlink = ctx->inputs[0];
2053 V360Context *s = ctx->priv;
2054 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2055 const int depth = desc->comp[0].depth;
2062 float output_mirror_modifier[3];
2063 void (*in_transform)(const V360Context *s,
2064 const float *vec, int width, int height,
2065 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv);
2066 void (*out_transform)(const V360Context *s,
2067 int i, int j, int width, int height,
2069 void (*calculate_kernel)(float du, float dv, const XYRemap *r_tmp,
2070 uint16_t *u, uint16_t *v, int16_t *ker);
2071 float rot_mat[3][3];
2073 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2074 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2076 switch (s->interp) {
2078 calculate_kernel = nearest_kernel;
2079 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2081 sizeof_uv = sizeof(uint16_t) * elements;
2085 calculate_kernel = bilinear_kernel;
2086 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2088 sizeof_uv = sizeof(uint16_t) * elements;
2089 sizeof_ker = sizeof(uint16_t) * elements;
2092 calculate_kernel = bicubic_kernel;
2093 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2095 sizeof_uv = sizeof(uint16_t) * elements;
2096 sizeof_ker = sizeof(uint16_t) * elements;
2099 calculate_kernel = lanczos_kernel;
2100 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2102 sizeof_uv = sizeof(uint16_t) * elements;
2103 sizeof_ker = sizeof(uint16_t) * elements;
2109 ff_v360_init(s, depth);
2111 for (int order = 0; order < NB_RORDERS; order++) {
2112 const char c = s->rorder[order];
2116 av_log(ctx, AV_LOG_ERROR,
2117 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2118 return AVERROR(EINVAL);
2121 rorder = get_rorder(c);
2123 av_log(ctx, AV_LOG_ERROR,
2124 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2125 return AVERROR(EINVAL);
2128 s->rotation_order[order] = rorder;
2132 case EQUIRECTANGULAR:
2133 in_transform = xyz_to_equirect;
2139 in_transform = xyz_to_cube3x2;
2140 err = prepare_cube_in(ctx);
2141 wf = inlink->w / 3.f * 4.f;
2145 in_transform = xyz_to_cube1x6;
2146 err = prepare_cube_in(ctx);
2147 wf = inlink->w * 4.f;
2148 hf = inlink->h / 3.f;
2151 in_transform = xyz_to_cube6x1;
2152 err = prepare_cube_in(ctx);
2153 wf = inlink->w / 3.f * 2.f;
2154 hf = inlink->h * 2.f;
2157 in_transform = xyz_to_eac;
2158 err = prepare_eac_in(ctx);
2160 hf = inlink->h / 9.f * 8.f;
2163 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2164 return AVERROR(EINVAL);
2166 in_transform = xyz_to_dfisheye;
2172 in_transform = xyz_to_barrel;
2174 wf = inlink->w / 5.f * 4.f;
2178 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2187 case EQUIRECTANGULAR:
2188 out_transform = equirect_to_xyz;
2194 out_transform = cube3x2_to_xyz;
2195 err = prepare_cube_out(ctx);
2196 w = roundf(wf / 4.f * 3.f);
2200 out_transform = cube1x6_to_xyz;
2201 err = prepare_cube_out(ctx);
2202 w = roundf(wf / 4.f);
2203 h = roundf(hf * 3.f);
2206 out_transform = cube6x1_to_xyz;
2207 err = prepare_cube_out(ctx);
2208 w = roundf(wf / 2.f * 3.f);
2209 h = roundf(hf / 2.f);
2212 out_transform = eac_to_xyz;
2213 err = prepare_eac_out(ctx);
2215 h = roundf(hf / 8.f * 9.f);
2218 out_transform = flat_to_xyz;
2219 err = prepare_flat_out(ctx);
2220 w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f);
2224 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
2225 return AVERROR(EINVAL);
2227 out_transform = barrel_to_xyz;
2229 w = roundf(wf / 4.f * 5.f);
2233 out_transform = stereographic_to_xyz;
2235 w = FFMAX(roundf(wf), roundf(hf));
2239 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2247 // Override resolution with user values if specified
2248 if (s->width > 0 && s->height > 0) {
2251 } else if (s->width > 0 || s->height > 0) {
2252 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2253 return AVERROR(EINVAL);
2255 if (s->out_transpose)
2258 if (s->in_transpose)
2262 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2263 s->planeheight[0] = s->planeheight[3] = h;
2264 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2265 s->planewidth[0] = s->planewidth[3] = w;
2270 s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
2271 s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
2272 s->inplanewidth[1] = s->inplanewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
2273 s->inplanewidth[0] = s->inplanewidth[3] = inlink->w;
2274 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2276 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2277 s->nb_allocated = 1;
2278 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2279 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2280 } else if (desc->log2_chroma_h == desc->log2_chroma_w) {
2281 s->nb_allocated = 2;
2283 s->map[1] = s->map[2] = 1;
2285 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2286 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2288 s->nb_allocated = 3;
2293 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2294 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2295 allocate_plane(s, sizeof_uv, sizeof_ker, 2);
2298 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat, s->rotation_order);
2299 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, output_mirror_modifier);
2301 // Calculate remap data
2302 for (p = 0; p < s->nb_allocated; p++) {
2303 const int width = s->planewidth[p];
2304 const int height = s->planeheight[p];
2305 const int in_width = s->inplanewidth[p];
2306 const int in_height = s->inplaneheight[p];
2312 for (i = 0; i < width; i++) {
2313 for (j = 0; j < height; j++) {
2314 uint16_t *u = s->u[p] + (j * width + i) * elements;
2315 uint16_t *v = s->v[p] + (j * width + i) * elements;
2316 int16_t *ker = s->ker[p] + (j * width + i) * elements;
2318 if (s->out_transpose)
2319 out_transform(s, j, i, height, width, vec);
2321 out_transform(s, i, j, width, height, vec);
2322 rotate(rot_mat, vec);
2323 mirror(output_mirror_modifier, vec);
2324 if (s->in_transpose)
2325 in_transform(s, vec, in_height, in_width, r_tmp.v, r_tmp.u, &du, &dv);
2327 in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv);
2328 calculate_kernel(du, dv, &r_tmp, u, v, ker);
2336 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2338 AVFilterContext *ctx = inlink->dst;
2339 AVFilterLink *outlink = ctx->outputs[0];
2340 V360Context *s = ctx->priv;
2344 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2347 return AVERROR(ENOMEM);
2349 av_frame_copy_props(out, in);
2354 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2357 return ff_filter_frame(outlink, out);
2360 static av_cold void uninit(AVFilterContext *ctx)
2362 V360Context *s = ctx->priv;
2365 for (p = 0; p < s->nb_allocated; p++) {
2368 av_freep(&s->ker[p]);
2372 static const AVFilterPad inputs[] = {
2375 .type = AVMEDIA_TYPE_VIDEO,
2376 .filter_frame = filter_frame,
2381 static const AVFilterPad outputs[] = {
2384 .type = AVMEDIA_TYPE_VIDEO,
2385 .config_props = config_output,
2390 AVFilter ff_vf_v360 = {
2392 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2393 .priv_size = sizeof(V360Context),
2395 .query_formats = query_formats,
2398 .priv_class = &v360_class,
2399 .flags = AVFILTER_FLAG_SLICE_THREADS,