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 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
58 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
59 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
60 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
61 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
62 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
63 { "c1x6", "cubemap1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
64 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
65 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
66 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
67 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
68 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
69 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
70 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
71 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
72 { "c1x6", "cubemap1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
73 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
74 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
75 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
76 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
77 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
78 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
79 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
80 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
81 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
82 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
83 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
84 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
85 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
86 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
87 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
88 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
89 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
90 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
91 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
92 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
93 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
94 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.f, 180.f, FLAGS, "h_fov"},
95 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.f, 90.f, FLAGS, "v_fov"},
96 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
97 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
98 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
99 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
100 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
104 AVFILTER_DEFINE_CLASS(v360);
106 static int query_formats(AVFilterContext *ctx)
108 static const enum AVPixelFormat pix_fmts[] = {
110 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
111 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
112 AV_PIX_FMT_YUVA444P16,
115 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
116 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
117 AV_PIX_FMT_YUVA422P16,
120 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
121 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
124 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
125 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
129 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
130 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
131 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
134 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
135 AV_PIX_FMT_YUV440P12,
138 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
139 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
140 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
143 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
144 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
145 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
154 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
155 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
156 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
159 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
160 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
163 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
164 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
165 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
170 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
172 return AVERROR(ENOMEM);
173 return ff_set_common_formats(ctx, fmts_list);
176 #define DEFINE_REMAP1_LINE(bits, div) \
177 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
178 ptrdiff_t in_linesize, \
179 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
181 const uint##bits##_t *s = (const uint##bits##_t *)src; \
182 uint##bits##_t *d = (uint##bits##_t *)dst; \
184 in_linesize /= div; \
186 for (int x = 0; x < width; x++) \
187 d[x] = s[v[x] * in_linesize + u[x]]; \
190 DEFINE_REMAP1_LINE( 8, 1)
191 DEFINE_REMAP1_LINE(16, 2)
193 typedef struct XYRemap {
200 * Generate remapping function with a given window size and pixel depth.
202 * @param ws size of interpolation window
203 * @param bits number of bits per pixel
205 #define DEFINE_REMAP(ws, bits) \
206 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
208 ThreadData *td = (ThreadData*)arg; \
209 const V360Context *s = ctx->priv; \
210 const AVFrame *in = td->in; \
211 AVFrame *out = td->out; \
213 for (int plane = 0; plane < s->nb_planes; plane++) { \
214 const int in_linesize = in->linesize[plane]; \
215 const int out_linesize = out->linesize[plane]; \
216 const uint8_t *src = in->data[plane]; \
217 uint8_t *dst = out->data[plane]; \
218 const int width = s->planewidth[plane]; \
219 const int height = s->planeheight[plane]; \
221 const int slice_start = (height * jobnr ) / nb_jobs; \
222 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
224 for (int y = slice_start; y < slice_end; y++) { \
225 const unsigned map = s->map[plane]; \
226 const uint16_t *u = s->u[map] + y * width * ws * ws; \
227 const uint16_t *v = s->v[map] + y * width * ws * ws; \
228 const int16_t *ker = s->ker[map] + y * width * ws * ws; \
230 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
244 #define DEFINE_REMAP_LINE(ws, bits, div) \
245 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
246 ptrdiff_t in_linesize, \
247 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
249 const uint##bits##_t *s = (const uint##bits##_t *)src; \
250 uint##bits##_t *d = (uint##bits##_t *)dst; \
252 in_linesize /= div; \
254 for (int x = 0; x < width; x++) { \
255 const uint16_t *uu = u + x * ws * ws; \
256 const uint16_t *vv = v + x * ws * ws; \
257 const int16_t *kker = ker + x * ws * ws; \
260 for (int i = 0; i < ws; i++) { \
261 for (int j = 0; j < ws; j++) { \
262 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
266 d[x] = av_clip_uint##bits(tmp >> 14); \
270 DEFINE_REMAP_LINE(2, 8, 1)
271 DEFINE_REMAP_LINE(4, 8, 1)
272 DEFINE_REMAP_LINE(2, 16, 2)
273 DEFINE_REMAP_LINE(4, 16, 2)
275 void ff_v360_init(V360Context *s, int depth)
279 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
282 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
286 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
291 ff_v360_init_x86(s, depth);
295 * Save nearest pixel coordinates for remapping.
297 * @param du horizontal relative coordinate
298 * @param dv vertical relative coordinate
299 * @param r_tmp calculated 4x4 window
300 * @param u u remap data
301 * @param v v remap data
302 * @param ker ker remap data
304 static void nearest_kernel(float du, float dv, const XYRemap *r_tmp,
305 uint16_t *u, uint16_t *v, int16_t *ker)
307 const int i = roundf(dv) + 1;
308 const int j = roundf(du) + 1;
310 u[0] = r_tmp->u[i][j];
311 v[0] = r_tmp->v[i][j];
315 * Calculate kernel for bilinear interpolation.
317 * @param du horizontal relative coordinate
318 * @param dv vertical relative coordinate
319 * @param r_tmp calculated 4x4 window
320 * @param u u remap data
321 * @param v v remap data
322 * @param ker ker remap data
324 static void bilinear_kernel(float du, float dv, const XYRemap *r_tmp,
325 uint16_t *u, uint16_t *v, int16_t *ker)
329 for (i = 0; i < 2; i++) {
330 for (j = 0; j < 2; j++) {
331 u[i * 2 + j] = r_tmp->u[i + 1][j + 1];
332 v[i * 2 + j] = r_tmp->v[i + 1][j + 1];
336 ker[0] = (1.f - du) * (1.f - dv) * 16384;
337 ker[1] = du * (1.f - dv) * 16384;
338 ker[2] = (1.f - du) * dv * 16384;
339 ker[3] = du * dv * 16384;
343 * Calculate 1-dimensional cubic coefficients.
345 * @param t relative coordinate
346 * @param coeffs coefficients
348 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
350 const float tt = t * t;
351 const float ttt = t * t * t;
353 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
354 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
355 coeffs[2] = t + tt / 2.f - ttt / 2.f;
356 coeffs[3] = - t / 6.f + ttt / 6.f;
360 * Calculate kernel for bicubic interpolation.
362 * @param du horizontal relative coordinate
363 * @param dv vertical relative coordinate
364 * @param r_tmp calculated 4x4 window
365 * @param u u remap data
366 * @param v v remap data
367 * @param ker ker remap data
369 static void bicubic_kernel(float du, float dv, const XYRemap *r_tmp,
370 uint16_t *u, uint16_t *v, int16_t *ker)
376 calculate_bicubic_coeffs(du, du_coeffs);
377 calculate_bicubic_coeffs(dv, dv_coeffs);
379 for (i = 0; i < 4; i++) {
380 for (j = 0; j < 4; j++) {
381 u[i * 4 + j] = r_tmp->u[i][j];
382 v[i * 4 + j] = r_tmp->v[i][j];
383 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
389 * Calculate 1-dimensional lanczos coefficients.
391 * @param t relative coordinate
392 * @param coeffs coefficients
394 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
399 for (i = 0; i < 4; i++) {
400 const float x = M_PI * (t - i + 1);
404 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
409 for (i = 0; i < 4; i++) {
415 * Calculate kernel for lanczos interpolation.
417 * @param du horizontal relative coordinate
418 * @param dv vertical relative coordinate
419 * @param r_tmp calculated 4x4 window
420 * @param u u remap data
421 * @param v v remap data
422 * @param ker ker remap data
424 static void lanczos_kernel(float du, float dv, const XYRemap *r_tmp,
425 uint16_t *u, uint16_t *v, int16_t *ker)
431 calculate_lanczos_coeffs(du, du_coeffs);
432 calculate_lanczos_coeffs(dv, dv_coeffs);
434 for (i = 0; i < 4; i++) {
435 for (j = 0; j < 4; j++) {
436 u[i * 4 + j] = r_tmp->u[i][j];
437 v[i * 4 + j] = r_tmp->v[i][j];
438 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
444 * Modulo operation with only positive remainders.
449 * @return positive remainder of (a / b)
451 static inline int mod(int a, int b)
453 const int res = a % b;
462 * Convert char to corresponding direction.
463 * Used for cubemap options.
465 static int get_direction(char c)
486 * Convert char to corresponding rotation angle.
487 * Used for cubemap options.
489 static int get_rotation(char c)
506 * Convert char to corresponding rotation order.
508 static int get_rorder(char c)
526 * Prepare data for processing cubemap input format.
528 * @param ctx filter context
532 static int prepare_cube_in(AVFilterContext *ctx)
534 V360Context *s = ctx->priv;
536 for (int face = 0; face < NB_FACES; face++) {
537 const char c = s->in_forder[face];
541 av_log(ctx, AV_LOG_ERROR,
542 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
543 return AVERROR(EINVAL);
546 direction = get_direction(c);
547 if (direction == -1) {
548 av_log(ctx, AV_LOG_ERROR,
549 "Incorrect direction symbol '%c' in in_forder option.\n", c);
550 return AVERROR(EINVAL);
553 s->in_cubemap_face_order[direction] = face;
556 for (int face = 0; face < NB_FACES; face++) {
557 const char c = s->in_frot[face];
561 av_log(ctx, AV_LOG_ERROR,
562 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
563 return AVERROR(EINVAL);
566 rotation = get_rotation(c);
567 if (rotation == -1) {
568 av_log(ctx, AV_LOG_ERROR,
569 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
570 return AVERROR(EINVAL);
573 s->in_cubemap_face_rotation[face] = rotation;
580 * Prepare data for processing cubemap output format.
582 * @param ctx filter context
586 static int prepare_cube_out(AVFilterContext *ctx)
588 V360Context *s = ctx->priv;
590 for (int face = 0; face < NB_FACES; face++) {
591 const char c = s->out_forder[face];
595 av_log(ctx, AV_LOG_ERROR,
596 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
597 return AVERROR(EINVAL);
600 direction = get_direction(c);
601 if (direction == -1) {
602 av_log(ctx, AV_LOG_ERROR,
603 "Incorrect direction symbol '%c' in out_forder option.\n", c);
604 return AVERROR(EINVAL);
607 s->out_cubemap_direction_order[face] = direction;
610 for (int face = 0; face < NB_FACES; face++) {
611 const char c = s->out_frot[face];
615 av_log(ctx, AV_LOG_ERROR,
616 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
617 return AVERROR(EINVAL);
620 rotation = get_rotation(c);
621 if (rotation == -1) {
622 av_log(ctx, AV_LOG_ERROR,
623 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
624 return AVERROR(EINVAL);
627 s->out_cubemap_face_rotation[face] = rotation;
633 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
659 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
686 * Calculate 3D coordinates on sphere for corresponding cubemap position.
687 * Common operation for every cubemap.
689 * @param s filter context
690 * @param uf horizontal cubemap coordinate [0, 1)
691 * @param vf vertical cubemap coordinate [0, 1)
692 * @param face face of cubemap
693 * @param vec coordinates on sphere
695 static void cube_to_xyz(const V360Context *s,
696 float uf, float vf, int face,
699 const int direction = s->out_cubemap_direction_order[face];
703 uf /= (1.f - s->out_pad);
704 vf /= (1.f - s->out_pad);
706 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
741 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
748 * Calculate cubemap position for corresponding 3D coordinates on sphere.
749 * Common operation for every cubemap.
751 * @param s filter context
752 * @param vec coordinated on sphere
753 * @param uf horizontal cubemap coordinate [0, 1)
754 * @param vf vertical cubemap coordinate [0, 1)
755 * @param direction direction of view
757 static void xyz_to_cube(const V360Context *s,
759 float *uf, float *vf, int *direction)
761 const float phi = atan2f(vec[0], -vec[2]);
762 const float theta = asinf(-vec[1]);
763 float phi_norm, theta_threshold;
766 if (phi >= -M_PI_4 && phi < M_PI_4) {
769 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
771 phi_norm = phi + M_PI_2;
772 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
774 phi_norm = phi - M_PI_2;
777 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
780 theta_threshold = atanf(cosf(phi_norm));
781 if (theta > theta_threshold) {
783 } else if (theta < -theta_threshold) {
787 switch (*direction) {
789 *uf = vec[2] / vec[0];
790 *vf = -vec[1] / vec[0];
793 *uf = vec[2] / vec[0];
794 *vf = vec[1] / vec[0];
797 *uf = vec[0] / vec[1];
798 *vf = -vec[2] / vec[1];
801 *uf = -vec[0] / vec[1];
802 *vf = -vec[2] / vec[1];
805 *uf = -vec[0] / vec[2];
806 *vf = vec[1] / vec[2];
809 *uf = -vec[0] / vec[2];
810 *vf = -vec[1] / vec[2];
816 face = s->in_cubemap_face_order[*direction];
817 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
821 * Find position on another cube face in case of overflow/underflow.
822 * Used for calculation of interpolation window.
824 * @param s filter context
825 * @param uf horizontal cubemap coordinate
826 * @param vf vertical cubemap coordinate
827 * @param direction direction of view
828 * @param new_uf new horizontal cubemap coordinate
829 * @param new_vf new vertical cubemap coordinate
830 * @param face face position on cubemap
832 static void process_cube_coordinates(const V360Context *s,
833 float uf, float vf, int direction,
834 float *new_uf, float *new_vf, int *face)
837 * Cubemap orientation
844 * +-------+-------+-------+-------+ ^ e |
846 * | left | front | right | back | | g |
847 * +-------+-------+-------+-------+ v h v
853 *face = s->in_cubemap_face_order[direction];
854 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
856 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
857 // There are no pixels to use in this case
860 } else if (uf < -1.f) {
896 } else if (uf >= 1.f) {
932 } else if (vf < -1.f) {
968 } else if (vf >= 1.f) {
1010 *face = s->in_cubemap_face_order[direction];
1011 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1015 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1017 * @param s filter context
1018 * @param i horizontal position on frame [0, height)
1019 * @param j vertical position on frame [0, width)
1020 * @param width frame width
1021 * @param height frame height
1022 * @param vec coordinates on sphere
1024 static void cube3x2_to_xyz(const V360Context *s,
1025 int i, int j, int width, int height,
1028 const float ew = width / 3.f;
1029 const float eh = height / 2.f;
1031 const int u_face = floorf(i / ew);
1032 const int v_face = floorf(j / eh);
1033 const int face = u_face + 3 * v_face;
1035 const int u_shift = ceilf(ew * u_face);
1036 const int v_shift = ceilf(eh * v_face);
1037 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1038 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1040 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1041 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1043 cube_to_xyz(s, uf, vf, face, vec);
1047 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1049 * @param s filter context
1050 * @param vec coordinates on sphere
1051 * @param width frame width
1052 * @param height frame height
1053 * @param us horizontal coordinates for interpolation window
1054 * @param vs vertical coordinates for interpolation window
1055 * @param du horizontal relative coordinate
1056 * @param dv vertical relative coordinate
1058 static void xyz_to_cube3x2(const V360Context *s,
1059 const float *vec, int width, int height,
1060 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1062 const float ew = width / 3.f;
1063 const float eh = height / 2.f;
1068 int direction, face;
1071 xyz_to_cube(s, vec, &uf, &vf, &direction);
1073 uf *= (1.f - s->in_pad);
1074 vf *= (1.f - s->in_pad);
1076 face = s->in_cubemap_face_order[direction];
1079 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1080 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1082 uf = 0.5f * ewi * (uf + 1.f);
1083 vf = 0.5f * ehi * (vf + 1.f);
1091 for (i = -1; i < 3; i++) {
1092 for (j = -1; j < 3; j++) {
1093 int new_ui = ui + j;
1094 int new_vi = vi + i;
1095 int u_shift, v_shift;
1096 int new_ewi, new_ehi;
1098 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1099 face = s->in_cubemap_face_order[direction];
1103 u_shift = ceilf(ew * u_face);
1104 v_shift = ceilf(eh * v_face);
1106 uf = 2.f * new_ui / ewi - 1.f;
1107 vf = 2.f * new_vi / ehi - 1.f;
1109 uf /= (1.f - s->in_pad);
1110 vf /= (1.f - s->in_pad);
1112 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1114 uf *= (1.f - s->in_pad);
1115 vf *= (1.f - s->in_pad);
1119 u_shift = ceilf(ew * u_face);
1120 v_shift = ceilf(eh * v_face);
1121 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1122 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1124 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1125 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1129 us[i + 1][j + 1] = u_shift + new_ui;
1130 vs[i + 1][j + 1] = v_shift + new_vi;
1136 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1138 * @param s filter context
1139 * @param i horizontal position on frame [0, height)
1140 * @param j vertical position on frame [0, width)
1141 * @param width frame width
1142 * @param height frame height
1143 * @param vec coordinates on sphere
1145 static void cube1x6_to_xyz(const V360Context *s,
1146 int i, int j, int width, int height,
1149 const float ew = width;
1150 const float eh = height / 6.f;
1152 const int face = floorf(j / eh);
1154 const int v_shift = ceilf(eh * face);
1155 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1157 const float uf = 2.f * i / ew - 1.f;
1158 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1160 cube_to_xyz(s, uf, vf, face, vec);
1164 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1166 * @param s filter context
1167 * @param i horizontal position on frame [0, height)
1168 * @param j vertical position on frame [0, width)
1169 * @param width frame width
1170 * @param height frame height
1171 * @param vec coordinates on sphere
1173 static void cube6x1_to_xyz(const V360Context *s,
1174 int i, int j, int width, int height,
1177 const float ew = width / 6.f;
1178 const float eh = height;
1180 const int face = floorf(i / ew);
1182 const int u_shift = ceilf(ew * face);
1183 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1185 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1186 const float vf = 2.f * j / eh - 1.f;
1188 cube_to_xyz(s, uf, vf, face, vec);
1192 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1194 * @param s filter context
1195 * @param vec coordinates on sphere
1196 * @param width frame width
1197 * @param height frame height
1198 * @param us horizontal coordinates for interpolation window
1199 * @param vs vertical coordinates for interpolation window
1200 * @param du horizontal relative coordinate
1201 * @param dv vertical relative coordinate
1203 static void xyz_to_cube1x6(const V360Context *s,
1204 const float *vec, int width, int height,
1205 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1207 const float eh = height / 6.f;
1208 const int ewi = width;
1213 int direction, face;
1215 xyz_to_cube(s, vec, &uf, &vf, &direction);
1217 uf *= (1.f - s->in_pad);
1218 vf *= (1.f - s->in_pad);
1220 face = s->in_cubemap_face_order[direction];
1221 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1223 uf = 0.5f * ewi * (uf + 1.f);
1224 vf = 0.5f * ehi * (vf + 1.f);
1232 for (i = -1; i < 3; i++) {
1233 for (j = -1; j < 3; j++) {
1234 int new_ui = ui + j;
1235 int new_vi = vi + i;
1239 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1240 face = s->in_cubemap_face_order[direction];
1242 v_shift = ceilf(eh * face);
1244 uf = 2.f * new_ui / ewi - 1.f;
1245 vf = 2.f * new_vi / ehi - 1.f;
1247 uf /= (1.f - s->in_pad);
1248 vf /= (1.f - s->in_pad);
1250 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1252 uf *= (1.f - s->in_pad);
1253 vf *= (1.f - s->in_pad);
1255 v_shift = ceilf(eh * face);
1256 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1258 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1259 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1263 us[i + 1][j + 1] = new_ui;
1264 vs[i + 1][j + 1] = v_shift + new_vi;
1270 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1272 * @param s filter context
1273 * @param vec coordinates on sphere
1274 * @param width frame width
1275 * @param height frame height
1276 * @param us horizontal coordinates for interpolation window
1277 * @param vs vertical coordinates for interpolation window
1278 * @param du horizontal relative coordinate
1279 * @param dv vertical relative coordinate
1281 static void xyz_to_cube6x1(const V360Context *s,
1282 const float *vec, int width, int height,
1283 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1285 const float ew = width / 6.f;
1286 const int ehi = height;
1291 int direction, face;
1293 xyz_to_cube(s, vec, &uf, &vf, &direction);
1295 uf *= (1.f - s->in_pad);
1296 vf *= (1.f - s->in_pad);
1298 face = s->in_cubemap_face_order[direction];
1299 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1301 uf = 0.5f * ewi * (uf + 1.f);
1302 vf = 0.5f * ehi * (vf + 1.f);
1310 for (i = -1; i < 3; i++) {
1311 for (j = -1; j < 3; j++) {
1312 int new_ui = ui + j;
1313 int new_vi = vi + i;
1317 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1318 face = s->in_cubemap_face_order[direction];
1320 u_shift = ceilf(ew * face);
1322 uf = 2.f * new_ui / ewi - 1.f;
1323 vf = 2.f * new_vi / ehi - 1.f;
1325 uf /= (1.f - s->in_pad);
1326 vf /= (1.f - s->in_pad);
1328 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1330 uf *= (1.f - s->in_pad);
1331 vf *= (1.f - s->in_pad);
1333 u_shift = ceilf(ew * face);
1334 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1336 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1337 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1341 us[i + 1][j + 1] = u_shift + new_ui;
1342 vs[i + 1][j + 1] = new_vi;
1348 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1350 * @param s filter context
1351 * @param i horizontal position on frame [0, height)
1352 * @param j vertical position on frame [0, width)
1353 * @param width frame width
1354 * @param height frame height
1355 * @param vec coordinates on sphere
1357 static void equirect_to_xyz(const V360Context *s,
1358 int i, int j, int width, int height,
1361 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1362 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1364 const float sin_phi = sinf(phi);
1365 const float cos_phi = cosf(phi);
1366 const float sin_theta = sinf(theta);
1367 const float cos_theta = cosf(theta);
1369 vec[0] = cos_theta * sin_phi;
1370 vec[1] = -sin_theta;
1371 vec[2] = -cos_theta * cos_phi;
1375 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1377 * @param s filter context
1378 * @param vec coordinates on sphere
1379 * @param width frame width
1380 * @param height frame height
1381 * @param us horizontal coordinates for interpolation window
1382 * @param vs vertical coordinates for interpolation window
1383 * @param du horizontal relative coordinate
1384 * @param dv vertical relative coordinate
1386 static void xyz_to_equirect(const V360Context *s,
1387 const float *vec, int width, int height,
1388 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1390 const float phi = atan2f(vec[0], -vec[2]);
1391 const float theta = asinf(-vec[1]);
1396 uf = (phi / M_PI + 1.f) * width / 2.f;
1397 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1404 for (i = -1; i < 3; i++) {
1405 for (j = -1; j < 3; j++) {
1406 us[i + 1][j + 1] = mod(ui + j, width);
1407 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1413 * Prepare data for processing equi-angular cubemap input format.
1415 * @param ctx filter context
1417 * @return error code
1419 static int prepare_eac_in(AVFilterContext *ctx)
1421 V360Context *s = ctx->priv;
1423 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1424 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1425 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1426 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1427 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1428 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1430 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1431 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1432 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1433 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1434 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1435 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1441 * Prepare data for processing equi-angular cubemap output format.
1443 * @param ctx filter context
1445 * @return error code
1447 static int prepare_eac_out(AVFilterContext *ctx)
1449 V360Context *s = ctx->priv;
1451 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1452 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1453 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1454 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1455 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1456 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1458 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1459 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1460 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1461 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1462 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1463 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1469 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1471 * @param s filter context
1472 * @param i horizontal position on frame [0, height)
1473 * @param j vertical position on frame [0, width)
1474 * @param width frame width
1475 * @param height frame height
1476 * @param vec coordinates on sphere
1478 static void eac_to_xyz(const V360Context *s,
1479 int i, int j, int width, int height,
1482 const float pixel_pad = 2;
1483 const float u_pad = pixel_pad / width;
1484 const float v_pad = pixel_pad / height;
1486 int u_face, v_face, face;
1488 float l_x, l_y, l_z;
1491 float uf = (float)i / width;
1492 float vf = (float)j / height;
1494 // EAC has 2-pixel padding on faces except between faces on the same row
1495 // Padding pixels seems not to be stretched with tangent as regular pixels
1496 // Formulas below approximate original padding as close as I could get experimentally
1498 // Horizontal padding
1499 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1503 } else if (uf >= 3.f) {
1507 u_face = floorf(uf);
1508 uf = fmodf(uf, 1.f) - 0.5f;
1512 v_face = floorf(vf * 2.f);
1513 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1515 if (uf >= -0.5f && uf < 0.5f) {
1516 uf = tanf(M_PI_2 * uf);
1520 if (vf >= -0.5f && vf < 0.5f) {
1521 vf = tanf(M_PI_2 * vf);
1526 face = u_face + 3 * v_face;
1563 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1564 vec[0] = l_x / norm;
1565 vec[1] = l_y / norm;
1566 vec[2] = l_z / norm;
1570 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1572 * @param s filter context
1573 * @param vec coordinates on sphere
1574 * @param width frame width
1575 * @param height frame height
1576 * @param us horizontal coordinates for interpolation window
1577 * @param vs vertical coordinates for interpolation window
1578 * @param du horizontal relative coordinate
1579 * @param dv vertical relative coordinate
1581 static void xyz_to_eac(const V360Context *s,
1582 const float *vec, int width, int height,
1583 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1585 const float pixel_pad = 2;
1586 const float u_pad = pixel_pad / width;
1587 const float v_pad = pixel_pad / height;
1592 int direction, face;
1595 xyz_to_cube(s, vec, &uf, &vf, &direction);
1597 face = s->in_cubemap_face_order[direction];
1601 uf = M_2_PI * atanf(uf) + 0.5f;
1602 vf = M_2_PI * atanf(vf) + 0.5f;
1604 // These formulas are inversed from eac_to_xyz ones
1605 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1606 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1617 for (i = -1; i < 3; i++) {
1618 for (j = -1; j < 3; j++) {
1619 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1620 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1626 * Prepare data for processing flat output format.
1628 * @param ctx filter context
1630 * @return error code
1632 static int prepare_flat_out(AVFilterContext *ctx)
1634 V360Context *s = ctx->priv;
1636 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1637 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1639 const float sin_phi = sinf(h_angle);
1640 const float cos_phi = cosf(h_angle);
1641 const float sin_theta = sinf(v_angle);
1642 const float cos_theta = cosf(v_angle);
1644 s->flat_range[0] = cos_theta * sin_phi;
1645 s->flat_range[1] = sin_theta;
1646 s->flat_range[2] = -cos_theta * cos_phi;
1652 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1654 * @param s filter context
1655 * @param i horizontal position on frame [0, height)
1656 * @param j vertical position on frame [0, width)
1657 * @param width frame width
1658 * @param height frame height
1659 * @param vec coordinates on sphere
1661 static void flat_to_xyz(const V360Context *s,
1662 int i, int j, int width, int height,
1665 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
1666 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1667 const float l_z = s->flat_range[2];
1669 const float norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1671 vec[0] = l_x / norm;
1672 vec[1] = l_y / norm;
1673 vec[2] = l_z / norm;
1677 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1679 * @param s filter context
1680 * @param vec coordinates on sphere
1681 * @param width frame width
1682 * @param height frame height
1683 * @param us horizontal coordinates for interpolation window
1684 * @param vs vertical coordinates for interpolation window
1685 * @param du horizontal relative coordinate
1686 * @param dv vertical relative coordinate
1688 static void xyz_to_dfisheye(const V360Context *s,
1689 const float *vec, int width, int height,
1690 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1692 const float scale = 1.f - s->in_pad;
1694 const float ew = width / 2.f;
1695 const float eh = height;
1697 const float phi = atan2f(-vec[1], -vec[0]);
1698 const float theta = acosf(fabsf(vec[2])) / M_PI;
1700 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1701 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1710 u_shift = ceilf(ew);
1720 for (i = -1; i < 3; i++) {
1721 for (j = -1; j < 3; j++) {
1722 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
1723 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
1729 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1731 * @param s filter context
1732 * @param i horizontal position on frame [0, height)
1733 * @param j vertical position on frame [0, width)
1734 * @param width frame width
1735 * @param height frame height
1736 * @param vec coordinates on sphere
1738 static void barrel_to_xyz(const V360Context *s,
1739 int i, int j, int width, int height,
1742 const float scale = 0.99f;
1743 float l_x, l_y, l_z;
1745 if (i < 4 * width / 5) {
1746 const float theta_range = M_PI / 4.f;
1748 const int ew = 4 * width / 5;
1749 const int eh = height;
1751 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1752 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1754 const float sin_phi = sinf(phi);
1755 const float cos_phi = cosf(phi);
1756 const float sin_theta = sinf(theta);
1757 const float cos_theta = cosf(theta);
1759 l_x = cos_theta * sin_phi;
1761 l_z = -cos_theta * cos_phi;
1763 const int ew = width / 5;
1764 const int eh = height / 2;
1770 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1771 vf = 2.f * (j ) / eh - 1.f;
1780 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1781 vf = 2.f * (j - eh) / eh - 1.f;
1791 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1804 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
1806 * @param s filter context
1807 * @param vec coordinates on sphere
1808 * @param width frame width
1809 * @param height frame height
1810 * @param us horizontal coordinates for interpolation window
1811 * @param vs vertical coordinates for interpolation window
1812 * @param du horizontal relative coordinate
1813 * @param dv vertical relative coordinate
1815 static void xyz_to_barrel(const V360Context *s,
1816 const float *vec, int width, int height,
1817 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1819 const float scale = 0.99f;
1821 const float phi = atan2f(vec[0], -vec[2]);
1822 const float theta = asinf(-vec[1]);
1823 const float theta_range = M_PI / 4.f;
1826 int u_shift, v_shift;
1831 if (theta > -theta_range && theta < theta_range) {
1838 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
1839 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1846 if (theta < 0.f) { // UP
1847 uf = vec[0] / vec[1];
1848 vf = -vec[2] / vec[1];
1851 uf = -vec[0] / vec[1];
1852 vf = -vec[2] / vec[1];
1856 uf = 0.5f * ew * (uf * scale + 1.f);
1857 vf = 0.5f * eh * (vf * scale + 1.f);
1866 for (i = -1; i < 3; i++) {
1867 for (j = -1; j < 3; j++) {
1868 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1869 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1875 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
1877 for (int i = 0; i < 3; i++) {
1878 for (int j = 0; j < 3; j++) {
1881 for (int k = 0; k < 3; k++)
1882 sum += a[i][k] * b[k][j];
1890 * Calculate rotation matrix for yaw/pitch/roll angles.
1892 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
1893 float rot_mat[3][3],
1894 const int rotation_order[3])
1896 const float yaw_rad = yaw * M_PI / 180.f;
1897 const float pitch_rad = pitch * M_PI / 180.f;
1898 const float roll_rad = roll * M_PI / 180.f;
1900 const float sin_yaw = sinf(-yaw_rad);
1901 const float cos_yaw = cosf(-yaw_rad);
1902 const float sin_pitch = sinf(pitch_rad);
1903 const float cos_pitch = cosf(pitch_rad);
1904 const float sin_roll = sinf(roll_rad);
1905 const float cos_roll = cosf(roll_rad);
1910 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
1911 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
1912 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
1914 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
1915 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
1916 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
1918 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
1919 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
1920 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
1922 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
1923 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
1927 * Rotate vector with given rotation matrix.
1929 * @param rot_mat rotation matrix
1932 static inline void rotate(const float rot_mat[3][3],
1935 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
1936 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
1937 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
1944 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
1947 modifier[0] = h_flip ? -1.f : 1.f;
1948 modifier[1] = v_flip ? -1.f : 1.f;
1949 modifier[2] = d_flip ? -1.f : 1.f;
1952 static inline void mirror(const float *modifier, float *vec)
1954 vec[0] *= modifier[0];
1955 vec[1] *= modifier[1];
1956 vec[2] *= modifier[2];
1959 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
1961 s->u[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
1962 s->v[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
1963 if (!s->u[p] || !s->v[p])
1964 return AVERROR(ENOMEM);
1966 s->ker[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_ker);
1968 return AVERROR(ENOMEM);
1974 static int config_output(AVFilterLink *outlink)
1976 AVFilterContext *ctx = outlink->src;
1977 AVFilterLink *inlink = ctx->inputs[0];
1978 V360Context *s = ctx->priv;
1979 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1980 const int depth = desc->comp[0].depth;
1987 float mirror_modifier[3];
1988 void (*in_transform)(const V360Context *s,
1989 const float *vec, int width, int height,
1990 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv);
1991 void (*out_transform)(const V360Context *s,
1992 int i, int j, int width, int height,
1994 void (*calculate_kernel)(float du, float dv, const XYRemap *r_tmp,
1995 uint16_t *u, uint16_t *v, int16_t *ker);
1996 float rot_mat[3][3];
1998 switch (s->interp) {
2000 calculate_kernel = nearest_kernel;
2001 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2003 sizeof_uv = sizeof(uint16_t) * elements;
2007 calculate_kernel = bilinear_kernel;
2008 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2010 sizeof_uv = sizeof(uint16_t) * elements;
2011 sizeof_ker = sizeof(uint16_t) * elements;
2014 calculate_kernel = bicubic_kernel;
2015 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2017 sizeof_uv = sizeof(uint16_t) * elements;
2018 sizeof_ker = sizeof(uint16_t) * elements;
2021 calculate_kernel = lanczos_kernel;
2022 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2024 sizeof_uv = sizeof(uint16_t) * elements;
2025 sizeof_ker = sizeof(uint16_t) * elements;
2031 ff_v360_init(s, depth);
2033 for (int order = 0; order < NB_RORDERS; order++) {
2034 const char c = s->rorder[order];
2038 av_log(ctx, AV_LOG_ERROR,
2039 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2040 return AVERROR(EINVAL);
2043 rorder = get_rorder(c);
2045 av_log(ctx, AV_LOG_ERROR,
2046 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2047 return AVERROR(EINVAL);
2050 s->rotation_order[order] = rorder;
2054 case EQUIRECTANGULAR:
2055 in_transform = xyz_to_equirect;
2061 in_transform = xyz_to_cube3x2;
2062 err = prepare_cube_in(ctx);
2063 wf = inlink->w / 3.f * 4.f;
2067 in_transform = xyz_to_cube1x6;
2068 err = prepare_cube_in(ctx);
2069 wf = inlink->w * 4.f;
2070 hf = inlink->h / 3.f;
2073 in_transform = xyz_to_cube6x1;
2074 err = prepare_cube_in(ctx);
2075 wf = inlink->w / 3.f * 2.f;
2076 hf = inlink->h * 2.f;
2079 in_transform = xyz_to_eac;
2080 err = prepare_eac_in(ctx);
2082 hf = inlink->h / 9.f * 8.f;
2085 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2086 return AVERROR(EINVAL);
2088 in_transform = xyz_to_dfisheye;
2094 in_transform = xyz_to_barrel;
2096 wf = inlink->w / 5.f * 4.f;
2100 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2109 case EQUIRECTANGULAR:
2110 out_transform = equirect_to_xyz;
2116 out_transform = cube3x2_to_xyz;
2117 err = prepare_cube_out(ctx);
2118 w = roundf(wf / 4.f * 3.f);
2122 out_transform = cube1x6_to_xyz;
2123 err = prepare_cube_out(ctx);
2124 w = roundf(wf / 4.f);
2125 h = roundf(hf * 3.f);
2128 out_transform = cube6x1_to_xyz;
2129 err = prepare_cube_out(ctx);
2130 w = roundf(wf / 2.f * 3.f);
2131 h = roundf(hf / 2.f);
2134 out_transform = eac_to_xyz;
2135 err = prepare_eac_out(ctx);
2137 h = roundf(hf / 8.f * 9.f);
2140 out_transform = flat_to_xyz;
2141 err = prepare_flat_out(ctx);
2142 w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f);
2146 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
2147 return AVERROR(EINVAL);
2149 out_transform = barrel_to_xyz;
2151 w = roundf(wf / 4.f * 5.f);
2155 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2163 // Override resolution with user values if specified
2164 if (s->width > 0 && s->height > 0) {
2167 } else if (s->width > 0 || s->height > 0) {
2168 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2169 return AVERROR(EINVAL);
2171 if (s->out_transpose)
2174 if (s->in_transpose)
2178 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2179 s->planeheight[0] = s->planeheight[3] = h;
2180 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2181 s->planewidth[0] = s->planewidth[3] = w;
2186 s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
2187 s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
2188 s->inplanewidth[1] = s->inplanewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
2189 s->inplanewidth[0] = s->inplanewidth[3] = inlink->w;
2190 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2192 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2193 s->nb_allocated = 1;
2194 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2195 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2196 } else if (desc->log2_chroma_h == desc->log2_chroma_w) {
2197 s->nb_allocated = 2;
2199 s->map[1] = s->map[2] = 1;
2201 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2202 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2204 s->nb_allocated = 3;
2209 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2210 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2211 allocate_plane(s, sizeof_uv, sizeof_ker, 2);
2214 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat, s->rotation_order);
2215 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, mirror_modifier);
2217 // Calculate remap data
2218 for (p = 0; p < s->nb_allocated; p++) {
2219 const int width = s->planewidth[p];
2220 const int height = s->planeheight[p];
2221 const int in_width = s->inplanewidth[p];
2222 const int in_height = s->inplaneheight[p];
2228 for (i = 0; i < width; i++) {
2229 for (j = 0; j < height; j++) {
2230 uint16_t *u = s->u[p] + (j * width + i) * elements;
2231 uint16_t *v = s->v[p] + (j * width + i) * elements;
2232 int16_t *ker = s->ker[p] + (j * width + i) * elements;
2234 if (s->out_transpose)
2235 out_transform(s, j, i, height, width, vec);
2237 out_transform(s, i, j, width, height, vec);
2238 rotate(rot_mat, vec);
2239 mirror(mirror_modifier, vec);
2240 if (s->in_transpose)
2241 in_transform(s, vec, in_height, in_width, r_tmp.v, r_tmp.u, &du, &dv);
2243 in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv);
2244 calculate_kernel(du, dv, &r_tmp, u, v, ker);
2252 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2254 AVFilterContext *ctx = inlink->dst;
2255 AVFilterLink *outlink = ctx->outputs[0];
2256 V360Context *s = ctx->priv;
2260 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2263 return AVERROR(ENOMEM);
2265 av_frame_copy_props(out, in);
2270 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2273 return ff_filter_frame(outlink, out);
2276 static av_cold void uninit(AVFilterContext *ctx)
2278 V360Context *s = ctx->priv;
2281 for (p = 0; p < s->nb_allocated; p++) {
2284 av_freep(&s->ker[p]);
2288 static const AVFilterPad inputs[] = {
2291 .type = AVMEDIA_TYPE_VIDEO,
2292 .filter_frame = filter_frame,
2297 static const AVFilterPad outputs[] = {
2300 .type = AVMEDIA_TYPE_VIDEO,
2301 .config_props = config_output,
2306 AVFilter ff_vf_v360 = {
2308 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2309 .priv_size = sizeof(V360Context),
2311 .query_formats = query_formats,
2314 .priv_class = &v360_class,
2315 .flags = AVFILTER_FLAG_SLICE_THREADS,