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"
93 typedef struct V360Context {
103 int in_cubemap_face_order[6];
104 int out_cubemap_direction_order[6];
105 int in_cubemap_face_rotation[6];
106 int out_cubemap_face_rotation[6];
108 float in_pad, out_pad;
110 float yaw, pitch, roll;
112 int h_flip, v_flip, d_flip;
117 int planewidth[4], planeheight[4];
118 int inplanewidth[4], inplaneheight[4];
121 uint16_t *u[4], *v[4];
124 int (*remap_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
127 typedef struct ThreadData {
132 #define OFFSET(x) offsetof(V360Context, x)
133 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
135 static const AVOption v360_options[] = {
136 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
137 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
138 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
139 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
140 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
141 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
142 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
143 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
144 { "c1x6", "cubemap1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
145 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
146 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
147 { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
148 { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
149 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
150 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
151 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
152 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
153 { "c1x6", "cubemap1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
154 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
155 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
156 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
157 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
158 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
159 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
160 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
161 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
162 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
163 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
164 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
165 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
166 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
167 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
168 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
169 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
170 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
171 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
172 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
173 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
174 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.f, 180.f, FLAGS, "h_fov"},
175 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.f, 90.f, FLAGS, "v_fov"},
176 { "h_flip", "flip video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
177 { "v_flip", "flip video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
178 { "d_flip", "flip video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
182 AVFILTER_DEFINE_CLASS(v360);
184 static int query_formats(AVFilterContext *ctx)
186 static const enum AVPixelFormat pix_fmts[] = {
188 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
189 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
190 AV_PIX_FMT_YUVA444P16,
193 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
194 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
195 AV_PIX_FMT_YUVA422P16,
198 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
199 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
202 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
203 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
207 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
208 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
209 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
212 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
213 AV_PIX_FMT_YUV440P12,
216 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
217 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
218 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
221 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
222 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
223 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
232 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
233 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
234 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
237 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
238 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
241 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
242 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
243 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
248 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
250 return AVERROR(ENOMEM);
251 return ff_set_common_formats(ctx, fmts_list);
255 * Generate no-interpolation remapping function with a given pixel depth.
257 * @param bits number of bits per pixel
258 * @param div number of bytes per pixel
260 #define DEFINE_REMAP1(bits, div) \
261 static int remap1_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
263 ThreadData *td = (ThreadData*)arg; \
264 const V360Context *s = ctx->priv; \
265 const AVFrame *in = td->in; \
266 AVFrame *out = td->out; \
270 for (plane = 0; plane < s->nb_planes; plane++) { \
271 const int in_linesize = in->linesize[plane] / div; \
272 const int out_linesize = out->linesize[plane] / div; \
273 const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \
274 uint##bits##_t *dst = (uint##bits##_t *)out->data[plane]; \
275 const int width = s->planewidth[plane]; \
276 const int height = s->planeheight[plane]; \
278 const int slice_start = (height * jobnr ) / nb_jobs; \
279 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
281 for (y = slice_start; y < slice_end; y++) { \
282 const uint16_t *u = s->u[plane] + y * width; \
283 const uint16_t *v = s->v[plane] + y * width; \
284 uint##bits##_t *d = dst + y * out_linesize; \
285 for (x = 0; x < width; x++) \
286 *d++ = src[v[x] * in_linesize + u[x]]; \
296 typedef struct XYRemap {
303 * Generate remapping function with a given window size and pixel depth.
305 * @param ws size of interpolation window
306 * @param bits number of bits per pixel
307 * @param div number of bytes per pixel
309 #define DEFINE_REMAP(ws, bits, div) \
310 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
312 ThreadData *td = (ThreadData*)arg; \
313 const V360Context *s = ctx->priv; \
314 const AVFrame *in = td->in; \
315 AVFrame *out = td->out; \
317 int plane, x, y, i, j; \
319 for (plane = 0; plane < s->nb_planes; plane++) { \
320 const int in_linesize = in->linesize[plane] / div; \
321 const int out_linesize = out->linesize[plane] / div; \
322 const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \
323 uint##bits##_t *dst = (uint##bits##_t *)out->data[plane]; \
324 const int width = s->planewidth[plane]; \
325 const int height = s->planeheight[plane]; \
327 const int slice_start = (height * jobnr ) / nb_jobs; \
328 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
330 for (y = slice_start; y < slice_end; y++) { \
331 uint##bits##_t *d = dst + y * out_linesize; \
332 const uint16_t *u = s->u[plane] + y * width * ws * ws; \
333 const uint16_t *v = s->v[plane] + y * width * ws * ws; \
334 const int16_t *ker = s->ker[plane] + y * width * ws * ws; \
335 for (x = 0; x < width; x++) { \
336 const uint16_t *uu = u + x * ws * ws; \
337 const uint16_t *vv = v + x * ws * ws; \
338 const int16_t *kker = ker + x * ws * ws; \
341 for (i = 0; i < ws; i++) { \
342 for (j = 0; j < ws; j++) { \
343 tmp += kker[i * ws + j] * src[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
347 *d++ = av_clip_uint##bits(tmp >> (15 - ws)); \
355 DEFINE_REMAP(2, 8, 1)
356 DEFINE_REMAP(4, 8, 1)
357 DEFINE_REMAP(2, 16, 2)
358 DEFINE_REMAP(4, 16, 2)
361 * Save nearest pixel coordinates for remapping.
363 * @param du horizontal relative coordinate
364 * @param dv vertical relative coordinate
365 * @param r_tmp calculated 4x4 window
366 * @param u u remap data
367 * @param v v remap data
368 * @param ker ker remap data
370 static void nearest_kernel(float du, float dv, const XYRemap *r_tmp,
371 uint16_t *u, uint16_t *v, int16_t *ker)
373 const int i = roundf(dv) + 1;
374 const int j = roundf(du) + 1;
376 u[0] = r_tmp->u[i][j];
377 v[0] = r_tmp->v[i][j];
381 * Calculate kernel for bilinear interpolation.
383 * @param du horizontal relative coordinate
384 * @param dv vertical relative coordinate
385 * @param r_tmp calculated 4x4 window
386 * @param u u remap data
387 * @param v v remap data
388 * @param ker ker remap data
390 static void bilinear_kernel(float du, float dv, const XYRemap *r_tmp,
391 uint16_t *u, uint16_t *v, int16_t *ker)
395 for (i = 0; i < 2; i++) {
396 for (j = 0; j < 2; j++) {
397 u[i * 2 + j] = r_tmp->u[i + 1][j + 1];
398 v[i * 2 + j] = r_tmp->v[i + 1][j + 1];
402 ker[0] = (1.f - du) * (1.f - dv) * 8192;
403 ker[1] = du * (1.f - dv) * 8192;
404 ker[2] = (1.f - du) * dv * 8192;
405 ker[3] = du * dv * 8192;
409 * Calculate 1-dimensional cubic coefficients.
411 * @param t relative coordinate
412 * @param coeffs coefficients
414 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
416 const float tt = t * t;
417 const float ttt = t * t * t;
419 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
420 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
421 coeffs[2] = t + tt / 2.f - ttt / 2.f;
422 coeffs[3] = - t / 6.f + ttt / 6.f;
426 * Calculate kernel for bicubic interpolation.
428 * @param du horizontal relative coordinate
429 * @param dv vertical relative coordinate
430 * @param r_tmp calculated 4x4 window
431 * @param u u remap data
432 * @param v v remap data
433 * @param ker ker remap data
435 static void bicubic_kernel(float du, float dv, const XYRemap *r_tmp,
436 uint16_t *u, uint16_t *v, int16_t *ker)
442 calculate_bicubic_coeffs(du, du_coeffs);
443 calculate_bicubic_coeffs(dv, dv_coeffs);
445 for (i = 0; i < 4; i++) {
446 for (j = 0; j < 4; j++) {
447 u[i * 4 + j] = r_tmp->u[i][j];
448 v[i * 4 + j] = r_tmp->v[i][j];
449 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 2048;
455 * Calculate 1-dimensional lanczos coefficients.
457 * @param t relative coordinate
458 * @param coeffs coefficients
460 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
465 for (i = 0; i < 4; i++) {
466 const float x = M_PI * (t - i + 1);
470 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
475 for (i = 0; i < 4; i++) {
481 * Calculate kernel for lanczos interpolation.
483 * @param du horizontal relative coordinate
484 * @param dv vertical relative coordinate
485 * @param r_tmp calculated 4x4 window
486 * @param u u remap data
487 * @param v v remap data
488 * @param ker ker remap data
490 static void lanczos_kernel(float du, float dv, const XYRemap *r_tmp,
491 uint16_t *u, uint16_t *v, int16_t *ker)
497 calculate_lanczos_coeffs(du, du_coeffs);
498 calculate_lanczos_coeffs(dv, dv_coeffs);
500 for (i = 0; i < 4; i++) {
501 for (j = 0; j < 4; j++) {
502 u[i * 4 + j] = r_tmp->u[i][j];
503 v[i * 4 + j] = r_tmp->v[i][j];
504 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 2048;
510 * Modulo operation with only positive remainders.
515 * @return positive remainder of (a / b)
517 static inline int mod(int a, int b)
519 const int res = a % b;
528 * Convert char to corresponding direction.
529 * Used for cubemap options.
531 static int get_direction(char c)
552 * Convert char to corresponding rotation angle.
553 * Used for cubemap options.
555 static int get_rotation(char c)
572 * Prepare data for processing cubemap input format.
574 * @param ctx filter context
578 static int prepare_cube_in(AVFilterContext *ctx)
580 V360Context *s = ctx->priv;
582 for (int face = 0; face < NB_FACES; face++) {
583 const char c = s->in_forder[face];
587 av_log(ctx, AV_LOG_ERROR,
588 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
589 return AVERROR(EINVAL);
592 direction = get_direction(c);
593 if (direction == -1) {
594 av_log(ctx, AV_LOG_ERROR,
595 "Incorrect direction symbol '%c' in in_forder option.\n", c);
596 return AVERROR(EINVAL);
599 s->in_cubemap_face_order[direction] = face;
602 for (int face = 0; face < NB_FACES; face++) {
603 const char c = s->in_frot[face];
607 av_log(ctx, AV_LOG_ERROR,
608 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
609 return AVERROR(EINVAL);
612 rotation = get_rotation(c);
613 if (rotation == -1) {
614 av_log(ctx, AV_LOG_ERROR,
615 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
616 return AVERROR(EINVAL);
619 s->in_cubemap_face_rotation[face] = rotation;
626 * Prepare data for processing cubemap output format.
628 * @param ctx filter context
632 static int prepare_cube_out(AVFilterContext *ctx)
634 V360Context *s = ctx->priv;
636 for (int face = 0; face < NB_FACES; face++) {
637 const char c = s->out_forder[face];
641 av_log(ctx, AV_LOG_ERROR,
642 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
643 return AVERROR(EINVAL);
646 direction = get_direction(c);
647 if (direction == -1) {
648 av_log(ctx, AV_LOG_ERROR,
649 "Incorrect direction symbol '%c' in out_forder option.\n", c);
650 return AVERROR(EINVAL);
653 s->out_cubemap_direction_order[face] = direction;
656 for (int face = 0; face < NB_FACES; face++) {
657 const char c = s->out_frot[face];
661 av_log(ctx, AV_LOG_ERROR,
662 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
663 return AVERROR(EINVAL);
666 rotation = get_rotation(c);
667 if (rotation == -1) {
668 av_log(ctx, AV_LOG_ERROR,
669 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
670 return AVERROR(EINVAL);
673 s->out_cubemap_face_rotation[face] = rotation;
679 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
705 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
732 * Calculate 3D coordinates on sphere for corresponding cubemap position.
733 * Common operation for every cubemap.
735 * @param s filter context
736 * @param uf horizontal cubemap coordinate [0, 1)
737 * @param vf vertical cubemap coordinate [0, 1)
738 * @param face face of cubemap
739 * @param vec coordinates on sphere
741 static void cube_to_xyz(const V360Context *s,
742 float uf, float vf, int face,
745 const int direction = s->out_cubemap_direction_order[face];
749 uf /= (1.f - s->out_pad);
750 vf /= (1.f - s->out_pad);
752 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
787 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
794 * Calculate cubemap position for corresponding 3D coordinates on sphere.
795 * Common operation for every cubemap.
797 * @param s filter context
798 * @param vec coordinated on sphere
799 * @param uf horizontal cubemap coordinate [0, 1)
800 * @param vf vertical cubemap coordinate [0, 1)
801 * @param direction direction of view
803 static void xyz_to_cube(const V360Context *s,
805 float *uf, float *vf, int *direction)
807 const float phi = atan2f(vec[0], -vec[2]);
808 const float theta = asinf(-vec[1]);
809 float phi_norm, theta_threshold;
812 if (phi >= -M_PI_4 && phi < M_PI_4) {
815 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
817 phi_norm = phi + M_PI_2;
818 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
820 phi_norm = phi - M_PI_2;
823 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
826 theta_threshold = atanf(cosf(phi_norm));
827 if (theta > theta_threshold) {
829 } else if (theta < -theta_threshold) {
833 switch (*direction) {
835 *uf = vec[2] / vec[0];
836 *vf = -vec[1] / vec[0];
839 *uf = vec[2] / vec[0];
840 *vf = vec[1] / vec[0];
843 *uf = vec[0] / vec[1];
844 *vf = -vec[2] / vec[1];
847 *uf = -vec[0] / vec[1];
848 *vf = -vec[2] / vec[1];
851 *uf = -vec[0] / vec[2];
852 *vf = vec[1] / vec[2];
855 *uf = -vec[0] / vec[2];
856 *vf = -vec[1] / vec[2];
862 face = s->in_cubemap_face_order[*direction];
863 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
867 * Find position on another cube face in case of overflow/underflow.
868 * Used for calculation of interpolation window.
870 * @param s filter context
871 * @param uf horizontal cubemap coordinate
872 * @param vf vertical cubemap coordinate
873 * @param direction direction of view
874 * @param new_uf new horizontal cubemap coordinate
875 * @param new_vf new vertical cubemap coordinate
876 * @param face face position on cubemap
878 static void process_cube_coordinates(const V360Context *s,
879 float uf, float vf, int direction,
880 float *new_uf, float *new_vf, int *face)
883 * Cubemap orientation
890 * +-------+-------+-------+-------+ ^ e |
892 * | left | front | right | back | | g |
893 * +-------+-------+-------+-------+ v h v
899 *face = s->in_cubemap_face_order[direction];
900 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
902 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
903 // There are no pixels to use in this case
906 } else if (uf < -1.f) {
942 } else if (uf >= 1.f) {
978 } else if (vf < -1.f) {
1014 } else if (vf >= 1.f) {
1016 switch (direction) {
1056 *face = s->in_cubemap_face_order[direction];
1057 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1061 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1063 * @param s filter context
1064 * @param i horizontal position on frame [0, height)
1065 * @param j vertical position on frame [0, width)
1066 * @param width frame width
1067 * @param height frame height
1068 * @param vec coordinates on sphere
1070 static void cube3x2_to_xyz(const V360Context *s,
1071 int i, int j, int width, int height,
1074 const float ew = width / 3.f;
1075 const float eh = height / 2.f;
1077 const int u_face = floorf(i / ew);
1078 const int v_face = floorf(j / eh);
1079 const int face = u_face + 3 * v_face;
1081 const int u_shift = ceilf(ew * u_face);
1082 const int v_shift = ceilf(eh * v_face);
1083 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1084 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1086 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1087 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1089 cube_to_xyz(s, uf, vf, face, vec);
1093 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1095 * @param s filter context
1096 * @param vec coordinates on sphere
1097 * @param width frame width
1098 * @param height frame height
1099 * @param us horizontal coordinates for interpolation window
1100 * @param vs vertical coordinates for interpolation window
1101 * @param du horizontal relative coordinate
1102 * @param dv vertical relative coordinate
1104 static void xyz_to_cube3x2(const V360Context *s,
1105 const float *vec, int width, int height,
1106 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1108 const float ew = width / 3.f;
1109 const float eh = height / 2.f;
1114 int direction, face;
1117 xyz_to_cube(s, vec, &uf, &vf, &direction);
1119 uf *= (1.f - s->in_pad);
1120 vf *= (1.f - s->in_pad);
1122 face = s->in_cubemap_face_order[direction];
1125 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1126 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1128 uf = 0.5f * ewi * (uf + 1.f);
1129 vf = 0.5f * ehi * (vf + 1.f);
1137 for (i = -1; i < 3; i++) {
1138 for (j = -1; j < 3; j++) {
1139 int new_ui = ui + j;
1140 int new_vi = vi + i;
1141 int u_shift, v_shift;
1142 int new_ewi, new_ehi;
1144 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1145 face = s->in_cubemap_face_order[direction];
1149 u_shift = ceilf(ew * u_face);
1150 v_shift = ceilf(eh * v_face);
1152 uf = 2.f * new_ui / ewi - 1.f;
1153 vf = 2.f * new_vi / ehi - 1.f;
1155 uf /= (1.f - s->in_pad);
1156 vf /= (1.f - s->in_pad);
1158 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1160 uf *= (1.f - s->in_pad);
1161 vf *= (1.f - s->in_pad);
1165 u_shift = ceilf(ew * u_face);
1166 v_shift = ceilf(eh * v_face);
1167 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1168 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1170 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1171 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1175 us[i + 1][j + 1] = u_shift + new_ui;
1176 vs[i + 1][j + 1] = v_shift + new_vi;
1182 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1184 * @param s filter context
1185 * @param i horizontal position on frame [0, height)
1186 * @param j vertical position on frame [0, width)
1187 * @param width frame width
1188 * @param height frame height
1189 * @param vec coordinates on sphere
1191 static void cube1x6_to_xyz(const V360Context *s,
1192 int i, int j, int width, int height,
1195 const float ew = width;
1196 const float eh = height / 6.f;
1198 const int face = floorf(j / eh);
1200 const int v_shift = ceilf(eh * face);
1201 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1203 const float uf = 2.f * i / ew - 1.f;
1204 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1206 cube_to_xyz(s, uf, vf, face, vec);
1210 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1212 * @param s filter context
1213 * @param i horizontal position on frame [0, height)
1214 * @param j vertical position on frame [0, width)
1215 * @param width frame width
1216 * @param height frame height
1217 * @param vec coordinates on sphere
1219 static void cube6x1_to_xyz(const V360Context *s,
1220 int i, int j, int width, int height,
1223 const float ew = width / 6.f;
1224 const float eh = height;
1226 const int face = floorf(i / ew);
1228 const int u_shift = ceilf(ew * face);
1229 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1231 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1232 const float vf = 2.f * j / eh - 1.f;
1234 cube_to_xyz(s, uf, vf, face, vec);
1238 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1240 * @param s filter context
1241 * @param vec coordinates on sphere
1242 * @param width frame width
1243 * @param height frame height
1244 * @param us horizontal coordinates for interpolation window
1245 * @param vs vertical coordinates for interpolation window
1246 * @param du horizontal relative coordinate
1247 * @param dv vertical relative coordinate
1249 static void xyz_to_cube1x6(const V360Context *s,
1250 const float *vec, int width, int height,
1251 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1253 const float eh = height / 6.f;
1254 const int ewi = width;
1259 int direction, face;
1261 xyz_to_cube(s, vec, &uf, &vf, &direction);
1263 uf *= (1.f - s->in_pad);
1264 vf *= (1.f - s->in_pad);
1266 face = s->in_cubemap_face_order[direction];
1267 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1269 uf = 0.5f * ewi * (uf + 1.f);
1270 vf = 0.5f * ehi * (vf + 1.f);
1278 for (i = -1; i < 3; i++) {
1279 for (j = -1; j < 3; j++) {
1280 int new_ui = ui + j;
1281 int new_vi = vi + i;
1285 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1286 face = s->in_cubemap_face_order[direction];
1288 v_shift = ceilf(eh * face);
1290 uf = 2.f * new_ui / ewi - 1.f;
1291 vf = 2.f * new_vi / ehi - 1.f;
1293 uf /= (1.f - s->in_pad);
1294 vf /= (1.f - s->in_pad);
1296 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1298 uf *= (1.f - s->in_pad);
1299 vf *= (1.f - s->in_pad);
1301 v_shift = ceilf(eh * face);
1302 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1304 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1305 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1309 us[i + 1][j + 1] = new_ui;
1310 vs[i + 1][j + 1] = v_shift + new_vi;
1316 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1318 * @param s filter context
1319 * @param vec coordinates on sphere
1320 * @param width frame width
1321 * @param height frame height
1322 * @param us horizontal coordinates for interpolation window
1323 * @param vs vertical coordinates for interpolation window
1324 * @param du horizontal relative coordinate
1325 * @param dv vertical relative coordinate
1327 static void xyz_to_cube6x1(const V360Context *s,
1328 const float *vec, int width, int height,
1329 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1331 const float ew = width / 6.f;
1332 const int ehi = height;
1337 int direction, face;
1339 xyz_to_cube(s, vec, &uf, &vf, &direction);
1341 uf *= (1.f - s->in_pad);
1342 vf *= (1.f - s->in_pad);
1344 face = s->in_cubemap_face_order[direction];
1345 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1347 uf = 0.5f * ewi * (uf + 1.f);
1348 vf = 0.5f * ehi * (vf + 1.f);
1356 for (i = -1; i < 3; i++) {
1357 for (j = -1; j < 3; j++) {
1358 int new_ui = ui + j;
1359 int new_vi = vi + i;
1363 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1364 face = s->in_cubemap_face_order[direction];
1366 u_shift = ceilf(ew * face);
1368 uf = 2.f * new_ui / ewi - 1.f;
1369 vf = 2.f * new_vi / ehi - 1.f;
1371 uf /= (1.f - s->in_pad);
1372 vf /= (1.f - s->in_pad);
1374 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1376 uf *= (1.f - s->in_pad);
1377 vf *= (1.f - s->in_pad);
1379 u_shift = ceilf(ew * face);
1380 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1382 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1383 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1387 us[i + 1][j + 1] = u_shift + new_ui;
1388 vs[i + 1][j + 1] = new_vi;
1394 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1396 * @param s filter context
1397 * @param i horizontal position on frame [0, height)
1398 * @param j vertical position on frame [0, width)
1399 * @param width frame width
1400 * @param height frame height
1401 * @param vec coordinates on sphere
1403 static void equirect_to_xyz(const V360Context *s,
1404 int i, int j, int width, int height,
1407 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1408 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1410 const float sin_phi = sinf(phi);
1411 const float cos_phi = cosf(phi);
1412 const float sin_theta = sinf(theta);
1413 const float cos_theta = cosf(theta);
1415 vec[0] = cos_theta * sin_phi;
1416 vec[1] = -sin_theta;
1417 vec[2] = -cos_theta * cos_phi;
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]);
1437 const float theta = asinf(-vec[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 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1470 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1471 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1472 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1473 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1474 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1476 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1477 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1478 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1479 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1480 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1481 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1487 * Prepare data for processing equi-angular cubemap output format.
1489 * @param ctx filter context
1491 * @return error code
1493 static int prepare_eac_out(AVFilterContext *ctx)
1495 V360Context *s = ctx->priv;
1497 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1498 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1499 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1500 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1501 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1502 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1504 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1505 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1506 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1507 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1508 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1509 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1515 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1517 * @param s filter context
1518 * @param i horizontal position on frame [0, height)
1519 * @param j vertical position on frame [0, width)
1520 * @param width frame width
1521 * @param height frame height
1522 * @param vec coordinates on sphere
1524 static void eac_to_xyz(const V360Context *s,
1525 int i, int j, int width, int height,
1528 const float pixel_pad = 2;
1529 const float u_pad = pixel_pad / width;
1530 const float v_pad = pixel_pad / height;
1532 int u_face, v_face, face;
1534 float l_x, l_y, l_z;
1537 float uf = (float)i / width;
1538 float vf = (float)j / height;
1540 // EAC has 2-pixel padding on faces except between faces on the same row
1541 // Padding pixels seems not to be stretched with tangent as regular pixels
1542 // Formulas below approximate original padding as close as I could get experimentally
1544 // Horizontal padding
1545 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1549 } else if (uf >= 3.f) {
1553 u_face = floorf(uf);
1554 uf = fmodf(uf, 1.f) - 0.5f;
1558 v_face = floorf(vf * 2.f);
1559 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1561 if (uf >= -0.5f && uf < 0.5f) {
1562 uf = tanf(M_PI_2 * uf);
1566 if (vf >= -0.5f && vf < 0.5f) {
1567 vf = tanf(M_PI_2 * vf);
1572 face = u_face + 3 * v_face;
1609 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1610 vec[0] = l_x / norm;
1611 vec[1] = l_y / norm;
1612 vec[2] = l_z / norm;
1616 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1618 * @param s filter context
1619 * @param vec coordinates on sphere
1620 * @param width frame width
1621 * @param height frame height
1622 * @param us horizontal coordinates for interpolation window
1623 * @param vs vertical coordinates for interpolation window
1624 * @param du horizontal relative coordinate
1625 * @param dv vertical relative coordinate
1627 static void xyz_to_eac(const V360Context *s,
1628 const float *vec, int width, int height,
1629 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1631 const float pixel_pad = 2;
1632 const float u_pad = pixel_pad / width;
1633 const float v_pad = pixel_pad / height;
1638 int direction, face;
1641 xyz_to_cube(s, vec, &uf, &vf, &direction);
1643 face = s->in_cubemap_face_order[direction];
1647 uf = M_2_PI * atanf(uf) + 0.5f;
1648 vf = M_2_PI * atanf(vf) + 0.5f;
1650 // These formulas are inversed from eac_to_xyz ones
1651 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1652 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1663 for (i = -1; i < 3; i++) {
1664 for (j = -1; j < 3; j++) {
1665 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1666 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1672 * Prepare data for processing flat output format.
1674 * @param ctx filter context
1676 * @return error code
1678 static int prepare_flat_out(AVFilterContext *ctx)
1680 V360Context *s = ctx->priv;
1682 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1683 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1685 const float sin_phi = sinf(h_angle);
1686 const float cos_phi = cosf(h_angle);
1687 const float sin_theta = sinf(v_angle);
1688 const float cos_theta = cosf(v_angle);
1690 s->flat_range[0] = cos_theta * sin_phi;
1691 s->flat_range[1] = sin_theta;
1692 s->flat_range[2] = -cos_theta * cos_phi;
1698 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1700 * @param s filter context
1701 * @param i horizontal position on frame [0, height)
1702 * @param j vertical position on frame [0, width)
1703 * @param width frame width
1704 * @param height frame height
1705 * @param vec coordinates on sphere
1707 static void flat_to_xyz(const V360Context *s,
1708 int i, int j, int width, int height,
1711 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
1712 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1713 const float l_z = s->flat_range[2];
1715 const float norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1717 vec[0] = l_x / norm;
1718 vec[1] = l_y / norm;
1719 vec[2] = l_z / norm;
1723 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1725 * @param s filter context
1726 * @param vec coordinates on sphere
1727 * @param width frame width
1728 * @param height frame height
1729 * @param us horizontal coordinates for interpolation window
1730 * @param vs vertical coordinates for interpolation window
1731 * @param du horizontal relative coordinate
1732 * @param dv vertical relative coordinate
1734 static void xyz_to_dfisheye(const V360Context *s,
1735 const float *vec, int width, int height,
1736 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1738 const float scale = 1.f - s->in_pad;
1740 const float ew = width / 2.f;
1741 const float eh = height;
1743 const float phi = atan2f(-vec[1], -vec[0]);
1744 const float theta = acosf(fabsf(vec[2])) / M_PI;
1746 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1747 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1756 u_shift = ceilf(ew);
1766 for (i = -1; i < 3; i++) {
1767 for (j = -1; j < 3; j++) {
1768 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
1769 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
1775 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1777 * @param s filter context
1778 * @param i horizontal position on frame [0, height)
1779 * @param j vertical position on frame [0, width)
1780 * @param width frame width
1781 * @param height frame height
1782 * @param vec coordinates on sphere
1784 static void barrel_to_xyz(const V360Context *s,
1785 int i, int j, int width, int height,
1788 const float scale = 0.99f;
1789 float l_x, l_y, l_z;
1791 if (i < 4 * width / 5) {
1792 const float theta_range = M_PI / 4.f;
1794 const int ew = 4 * width / 5;
1795 const int eh = height;
1797 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1798 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1800 const float sin_phi = sinf(phi);
1801 const float cos_phi = cosf(phi);
1802 const float sin_theta = sinf(theta);
1803 const float cos_theta = cosf(theta);
1805 l_x = cos_theta * sin_phi;
1807 l_z = -cos_theta * cos_phi;
1809 const int ew = width / 5;
1810 const int eh = height / 2;
1816 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1817 vf = 2.f * (j ) / eh - 1.f;
1826 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1827 vf = 2.f * (j - eh) / eh - 1.f;
1837 norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1850 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
1852 * @param s filter context
1853 * @param vec coordinates on sphere
1854 * @param width frame width
1855 * @param height frame height
1856 * @param us horizontal coordinates for interpolation window
1857 * @param vs vertical coordinates for interpolation window
1858 * @param du horizontal relative coordinate
1859 * @param dv vertical relative coordinate
1861 static void xyz_to_barrel(const V360Context *s,
1862 const float *vec, int width, int height,
1863 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1865 const float scale = 0.99f;
1867 const float phi = atan2f(vec[0], -vec[2]);
1868 const float theta = asinf(-vec[1]);
1869 const float theta_range = M_PI / 4.f;
1872 int u_shift, v_shift;
1877 if (theta > -theta_range && theta < theta_range) {
1884 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
1885 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1892 if (theta < 0.f) { // UP
1893 uf = vec[0] / vec[1];
1894 vf = -vec[2] / vec[1];
1897 uf = -vec[0] / vec[1];
1898 vf = -vec[2] / vec[1];
1902 uf = 0.5f * ew * (uf * scale + 1.f);
1903 vf = 0.5f * eh * (vf * scale + 1.f);
1912 for (i = -1; i < 3; i++) {
1913 for (j = -1; j < 3; j++) {
1914 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1915 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1922 * Calculate rotation matrix for yaw/pitch/roll angles.
1924 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
1925 float rot_mat[3][3])
1927 const float yaw_rad = yaw * M_PI / 180.f;
1928 const float pitch_rad = pitch * M_PI / 180.f;
1929 const float roll_rad = roll * M_PI / 180.f;
1931 const float sin_yaw = sinf(-yaw_rad);
1932 const float cos_yaw = cosf(-yaw_rad);
1933 const float sin_pitch = sinf(pitch_rad);
1934 const float cos_pitch = cosf(pitch_rad);
1935 const float sin_roll = sinf(roll_rad);
1936 const float cos_roll = cosf(roll_rad);
1938 rot_mat[0][0] = sin_yaw * sin_pitch * sin_roll + cos_yaw * cos_roll;
1939 rot_mat[0][1] = sin_yaw * sin_pitch * cos_roll - cos_yaw * sin_roll;
1940 rot_mat[0][2] = sin_yaw * cos_pitch;
1942 rot_mat[1][0] = cos_pitch * sin_roll;
1943 rot_mat[1][1] = cos_pitch * cos_roll;
1944 rot_mat[1][2] = -sin_pitch;
1946 rot_mat[2][0] = cos_yaw * sin_pitch * sin_roll - sin_yaw * cos_roll;
1947 rot_mat[2][1] = cos_yaw * sin_pitch * cos_roll + sin_yaw * sin_roll;
1948 rot_mat[2][2] = cos_yaw * cos_pitch;
1952 * Rotate vector with given rotation matrix.
1954 * @param rot_mat rotation matrix
1957 static inline void rotate(const float rot_mat[3][3],
1960 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
1961 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
1962 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
1969 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
1972 modifier[0] = h_flip ? -1.f : 1.f;
1973 modifier[1] = v_flip ? -1.f : 1.f;
1974 modifier[2] = d_flip ? -1.f : 1.f;
1977 static inline void mirror(const float *modifier, float *vec)
1979 vec[0] *= modifier[0];
1980 vec[1] *= modifier[1];
1981 vec[2] *= modifier[2];
1984 static int config_output(AVFilterLink *outlink)
1986 AVFilterContext *ctx = outlink->src;
1987 AVFilterLink *inlink = ctx->inputs[0];
1988 V360Context *s = ctx->priv;
1989 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1990 const int depth = desc->comp[0].depth;
1997 float mirror_modifier[3];
1998 void (*in_transform)(const V360Context *s,
1999 const float *vec, int width, int height,
2000 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv);
2001 void (*out_transform)(const V360Context *s,
2002 int i, int j, int width, int height,
2004 void (*calculate_kernel)(float du, float dv, const XYRemap *r_tmp,
2005 uint16_t *u, uint16_t *v, int16_t *ker);
2006 float rot_mat[3][3];
2008 switch (s->interp) {
2010 calculate_kernel = nearest_kernel;
2011 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2013 sizeof_uv = sizeof(uint16_t) * elements;
2017 calculate_kernel = bilinear_kernel;
2018 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2020 sizeof_uv = sizeof(uint16_t) * elements;
2021 sizeof_ker = sizeof(uint16_t) * elements;
2024 calculate_kernel = bicubic_kernel;
2025 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2027 sizeof_uv = sizeof(uint16_t) * elements;
2028 sizeof_ker = sizeof(uint16_t) * elements;
2031 calculate_kernel = lanczos_kernel;
2032 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2034 sizeof_uv = sizeof(uint16_t) * elements;
2035 sizeof_ker = sizeof(uint16_t) * elements;
2042 case EQUIRECTANGULAR:
2043 in_transform = xyz_to_equirect;
2049 in_transform = xyz_to_cube3x2;
2050 err = prepare_cube_in(ctx);
2051 wf = inlink->w / 3.f * 4.f;
2055 in_transform = xyz_to_cube1x6;
2056 err = prepare_cube_in(ctx);
2057 wf = inlink->w * 4.f;
2058 hf = inlink->h / 3.f;
2061 in_transform = xyz_to_cube6x1;
2062 err = prepare_cube_in(ctx);
2063 wf = inlink->w / 3.f * 2.f;
2064 hf = inlink->h * 2.f;
2067 in_transform = xyz_to_eac;
2068 err = prepare_eac_in(ctx);
2070 hf = inlink->h / 9.f * 8.f;
2073 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2074 return AVERROR(EINVAL);
2076 in_transform = xyz_to_dfisheye;
2082 in_transform = xyz_to_barrel;
2084 wf = inlink->w / 5.f * 4.f;
2088 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2097 case EQUIRECTANGULAR:
2098 out_transform = equirect_to_xyz;
2104 out_transform = cube3x2_to_xyz;
2105 err = prepare_cube_out(ctx);
2106 w = roundf(wf / 4.f * 3.f);
2110 out_transform = cube1x6_to_xyz;
2111 err = prepare_cube_out(ctx);
2112 w = roundf(wf / 4.f);
2113 h = roundf(hf * 3.f);
2116 out_transform = cube6x1_to_xyz;
2117 err = prepare_cube_out(ctx);
2118 w = roundf(wf / 2.f * 3.f);
2119 h = roundf(hf / 2.f);
2122 out_transform = eac_to_xyz;
2123 err = prepare_eac_out(ctx);
2125 h = roundf(hf / 8.f * 9.f);
2128 out_transform = flat_to_xyz;
2129 err = prepare_flat_out(ctx);
2130 w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f);
2134 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
2135 return AVERROR(EINVAL);
2137 out_transform = barrel_to_xyz;
2139 w = roundf(wf / 4.f * 5.f);
2143 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2151 // Override resolution with user values if specified
2152 if (s->width > 0 && s->height > 0) {
2155 } else if (s->width > 0 || s->height > 0) {
2156 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2157 return AVERROR(EINVAL);
2160 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2161 s->planeheight[0] = s->planeheight[3] = h;
2162 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2163 s->planewidth[0] = s->planewidth[3] = w;
2168 s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
2169 s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
2170 s->inplanewidth[1] = s->inplanewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
2171 s->inplanewidth[0] = s->inplanewidth[3] = inlink->w;
2172 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2174 for (p = 0; p < s->nb_planes; p++) {
2175 s->u[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
2176 s->v[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
2177 if (!s->u[p] || !s->v[p])
2178 return AVERROR(ENOMEM);
2180 s->ker[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_ker);
2182 return AVERROR(ENOMEM);
2186 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat);
2187 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, mirror_modifier);
2189 // Calculate remap data
2190 for (p = 0; p < s->nb_planes; p++) {
2191 const int width = s->planewidth[p];
2192 const int height = s->planeheight[p];
2193 const int in_width = s->inplanewidth[p];
2194 const int in_height = s->inplaneheight[p];
2200 for (i = 0; i < width; i++) {
2201 for (j = 0; j < height; j++) {
2202 uint16_t *u = s->u[p] + (j * width + i) * elements;
2203 uint16_t *v = s->v[p] + (j * width + i) * elements;
2204 int16_t *ker = s->ker[p] + (j * width + i) * elements;
2206 out_transform(s, i, j, width, height, vec);
2207 rotate(rot_mat, vec);
2208 mirror(mirror_modifier, vec);
2209 in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv);
2210 calculate_kernel(du, dv, &r_tmp, u, v, ker);
2218 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2220 AVFilterContext *ctx = inlink->dst;
2221 AVFilterLink *outlink = ctx->outputs[0];
2222 V360Context *s = ctx->priv;
2226 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2229 return AVERROR(ENOMEM);
2231 av_frame_copy_props(out, in);
2236 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2239 return ff_filter_frame(outlink, out);
2242 static av_cold void uninit(AVFilterContext *ctx)
2244 V360Context *s = ctx->priv;
2247 for (p = 0; p < s->nb_planes; p++) {
2250 av_freep(&s->ker[p]);
2254 static const AVFilterPad inputs[] = {
2257 .type = AVMEDIA_TYPE_VIDEO,
2258 .filter_frame = filter_frame,
2263 static const AVFilterPad outputs[] = {
2266 .type = AVMEDIA_TYPE_VIDEO,
2267 .config_props = config_output,
2272 AVFilter ff_vf_v360 = {
2274 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2275 .priv_size = sizeof(V360Context),
2277 .query_formats = query_formats,
2280 .priv_class = &v360_class,
2281 .flags = AVFILTER_FLAG_SLICE_THREADS,