You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4319 lines
139KB

  1. /*
  2. * Copyright (c) 2019 Eugene Lyapustin
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * 360 video conversion filter.
  23. * Principle of operation:
  24. *
  25. * (for each pixel in output frame)
  26. * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
  27. * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
  28. * 3) Calculate pixel position (u, v) in input frame
  29. * 4) Calculate interpolation window and weight for each pixel
  30. *
  31. * (for each frame)
  32. * 5) Remap input frame to output frame using precalculated data
  33. */
  34. #include <math.h>
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/opt.h"
  39. #include "avfilter.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #include "video.h"
  43. #include "v360.h"
  44. typedef struct ThreadData {
  45. AVFrame *in;
  46. AVFrame *out;
  47. } ThreadData;
  48. #define OFFSET(x) offsetof(V360Context, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  51. static const AVOption v360_options[] = {
  52. { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
  53. { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
  54. { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
  55. { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
  56. { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
  57. { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
  58. { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
  59. { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  60. {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  61. { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
  62. { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
  63. { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
  64. { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
  65. { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
  66. { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
  67. { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
  68. { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
  69. {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
  70. { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
  71. { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "in" },
  72. {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
  73. {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
  74. {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "in" },
  75. { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
  76. { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
  77. { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
  78. { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
  79. { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
  80. { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
  81. { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
  82. { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
  83. { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
  84. { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
  85. { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  86. {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  87. { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
  88. { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
  89. { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
  90. { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
  91. { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
  92. { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
  93. { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
  94. { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
  95. {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
  96. { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
  97. { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
  98. {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
  99. {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
  100. {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
  101. {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
  102. { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
  103. { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
  104. { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
  105. { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
  106. { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
  107. { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
  108. { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
  109. { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
  110. { "lagrange9", "lagrange9 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LAGRANGE9}, 0, 0, FLAGS, "interp" },
  111. { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
  112. { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
  113. { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
  114. { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
  115. { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
  116. { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
  117. { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
  118. { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
  119. { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
  120. { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
  121. { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
  122. {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
  123. { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
  124. { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
  125. { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
  126. { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
  127. {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
  128. { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
  129. { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
  130. { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "in_pad"},
  131. { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "out_pad"},
  132. { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
  133. { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
  134. { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
  135. { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
  136. { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
  137. { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
  138. { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
  139. { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
  140. { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
  141. { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
  142. { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
  143. { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
  144. { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
  145. { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
  146. { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
  147. { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
  148. { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
  149. { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
  150. { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
  151. {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
  152. { NULL }
  153. };
  154. AVFILTER_DEFINE_CLASS(v360);
  155. static int query_formats(AVFilterContext *ctx)
  156. {
  157. V360Context *s = ctx->priv;
  158. static const enum AVPixelFormat pix_fmts[] = {
  159. // YUVA444
  160. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
  161. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
  162. AV_PIX_FMT_YUVA444P16,
  163. // YUVA422
  164. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
  165. AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
  166. AV_PIX_FMT_YUVA422P16,
  167. // YUVA420
  168. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
  169. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  170. // YUVJ
  171. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  172. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  173. AV_PIX_FMT_YUVJ411P,
  174. // YUV444
  175. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
  176. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  177. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
  178. // YUV440
  179. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
  180. AV_PIX_FMT_YUV440P12,
  181. // YUV422
  182. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
  183. AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
  184. AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
  185. // YUV420
  186. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
  187. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
  188. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
  189. // YUV411
  190. AV_PIX_FMT_YUV411P,
  191. // YUV410
  192. AV_PIX_FMT_YUV410P,
  193. // GBR
  194. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
  195. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  196. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  197. // GBRA
  198. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
  199. AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  200. // GRAY
  201. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  202. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
  203. AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  204. AV_PIX_FMT_NONE
  205. };
  206. static const enum AVPixelFormat alpha_pix_fmts[] = {
  207. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
  208. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
  209. AV_PIX_FMT_YUVA444P16,
  210. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
  211. AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
  212. AV_PIX_FMT_YUVA422P16,
  213. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
  214. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  215. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
  216. AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  217. AV_PIX_FMT_NONE
  218. };
  219. AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
  220. if (!fmts_list)
  221. return AVERROR(ENOMEM);
  222. return ff_set_common_formats(ctx, fmts_list);
  223. }
  224. #define DEFINE_REMAP1_LINE(bits, div) \
  225. static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
  226. ptrdiff_t in_linesize, \
  227. const int16_t *const u, const int16_t *const v, \
  228. const int16_t *const ker) \
  229. { \
  230. const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
  231. uint##bits##_t *d = (uint##bits##_t *)dst; \
  232. \
  233. in_linesize /= div; \
  234. \
  235. for (int x = 0; x < width; x++) \
  236. d[x] = s[v[x] * in_linesize + u[x]]; \
  237. }
  238. DEFINE_REMAP1_LINE( 8, 1)
  239. DEFINE_REMAP1_LINE(16, 2)
  240. /**
  241. * Generate remapping function with a given window size and pixel depth.
  242. *
  243. * @param ws size of interpolation window
  244. * @param bits number of bits per pixel
  245. */
  246. #define DEFINE_REMAP(ws, bits) \
  247. static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  248. { \
  249. ThreadData *td = arg; \
  250. const V360Context *s = ctx->priv; \
  251. const AVFrame *in = td->in; \
  252. AVFrame *out = td->out; \
  253. \
  254. for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
  255. for (int plane = 0; plane < s->nb_planes; plane++) { \
  256. const unsigned map = s->map[plane]; \
  257. const int in_linesize = in->linesize[plane]; \
  258. const int out_linesize = out->linesize[plane]; \
  259. const int uv_linesize = s->uv_linesize[plane]; \
  260. const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
  261. const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
  262. const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
  263. const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
  264. const uint8_t *const src = in->data[plane] + \
  265. in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
  266. uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
  267. const uint8_t *mask = plane == 3 ? s->mask : NULL; \
  268. const int width = s->pr_width[plane]; \
  269. const int height = s->pr_height[plane]; \
  270. \
  271. const int slice_start = (height * jobnr ) / nb_jobs; \
  272. const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
  273. \
  274. for (int y = slice_start; y < slice_end && !mask; y++) { \
  275. const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
  276. const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
  277. const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
  278. \
  279. s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
  280. } \
  281. \
  282. for (int y = slice_start; y < slice_end && mask; y++) { \
  283. memcpy(dst + y * out_linesize, mask + y * width * (bits >> 3), width * (bits >> 3)); \
  284. } \
  285. } \
  286. } \
  287. \
  288. return 0; \
  289. }
  290. DEFINE_REMAP(1, 8)
  291. DEFINE_REMAP(2, 8)
  292. DEFINE_REMAP(3, 8)
  293. DEFINE_REMAP(4, 8)
  294. DEFINE_REMAP(1, 16)
  295. DEFINE_REMAP(2, 16)
  296. DEFINE_REMAP(3, 16)
  297. DEFINE_REMAP(4, 16)
  298. #define DEFINE_REMAP_LINE(ws, bits, div) \
  299. static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
  300. ptrdiff_t in_linesize, \
  301. const int16_t *const u, const int16_t *const v, \
  302. const int16_t *const ker) \
  303. { \
  304. const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
  305. uint##bits##_t *d = (uint##bits##_t *)dst; \
  306. \
  307. in_linesize /= div; \
  308. \
  309. for (int x = 0; x < width; x++) { \
  310. const int16_t *const uu = u + x * ws * ws; \
  311. const int16_t *const vv = v + x * ws * ws; \
  312. const int16_t *const kker = ker + x * ws * ws; \
  313. int tmp = 0; \
  314. \
  315. for (int i = 0; i < ws; i++) { \
  316. for (int j = 0; j < ws; j++) { \
  317. tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
  318. } \
  319. } \
  320. \
  321. d[x] = av_clip_uint##bits(tmp >> 14); \
  322. } \
  323. }
  324. DEFINE_REMAP_LINE(2, 8, 1)
  325. DEFINE_REMAP_LINE(3, 8, 1)
  326. DEFINE_REMAP_LINE(4, 8, 1)
  327. DEFINE_REMAP_LINE(2, 16, 2)
  328. DEFINE_REMAP_LINE(3, 16, 2)
  329. DEFINE_REMAP_LINE(4, 16, 2)
  330. void ff_v360_init(V360Context *s, int depth)
  331. {
  332. switch (s->interp) {
  333. case NEAREST:
  334. s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
  335. break;
  336. case BILINEAR:
  337. s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
  338. break;
  339. case LAGRANGE9:
  340. s->remap_line = depth <= 8 ? remap3_8bit_line_c : remap3_16bit_line_c;
  341. break;
  342. case BICUBIC:
  343. case LANCZOS:
  344. case SPLINE16:
  345. case GAUSSIAN:
  346. s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
  347. break;
  348. }
  349. if (ARCH_X86)
  350. ff_v360_init_x86(s, depth);
  351. }
  352. /**
  353. * Save nearest pixel coordinates for remapping.
  354. *
  355. * @param du horizontal relative coordinate
  356. * @param dv vertical relative coordinate
  357. * @param rmap calculated 4x4 window
  358. * @param u u remap data
  359. * @param v v remap data
  360. * @param ker ker remap data
  361. */
  362. static void nearest_kernel(float du, float dv, const XYRemap *rmap,
  363. int16_t *u, int16_t *v, int16_t *ker)
  364. {
  365. const int i = lrintf(dv) + 1;
  366. const int j = lrintf(du) + 1;
  367. u[0] = rmap->u[i][j];
  368. v[0] = rmap->v[i][j];
  369. }
  370. /**
  371. * Calculate kernel for bilinear interpolation.
  372. *
  373. * @param du horizontal relative coordinate
  374. * @param dv vertical relative coordinate
  375. * @param rmap calculated 4x4 window
  376. * @param u u remap data
  377. * @param v v remap data
  378. * @param ker ker remap data
  379. */
  380. static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
  381. int16_t *u, int16_t *v, int16_t *ker)
  382. {
  383. for (int i = 0; i < 2; i++) {
  384. for (int j = 0; j < 2; j++) {
  385. u[i * 2 + j] = rmap->u[i + 1][j + 1];
  386. v[i * 2 + j] = rmap->v[i + 1][j + 1];
  387. }
  388. }
  389. ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
  390. ker[1] = lrintf( du * (1.f - dv) * 16385.f);
  391. ker[2] = lrintf((1.f - du) * dv * 16385.f);
  392. ker[3] = lrintf( du * dv * 16385.f);
  393. }
  394. /**
  395. * Calculate 1-dimensional lagrange coefficients.
  396. *
  397. * @param t relative coordinate
  398. * @param coeffs coefficients
  399. */
  400. static inline void calculate_lagrange_coeffs(float t, float *coeffs)
  401. {
  402. coeffs[0] = (t - 1.f) * (t - 2.f) * 0.5f;
  403. coeffs[1] = -t * (t - 2.f);
  404. coeffs[2] = t * (t - 1.f) * 0.5f;
  405. }
  406. /**
  407. * Calculate kernel for lagrange interpolation.
  408. *
  409. * @param du horizontal relative coordinate
  410. * @param dv vertical relative coordinate
  411. * @param rmap calculated 4x4 window
  412. * @param u u remap data
  413. * @param v v remap data
  414. * @param ker ker remap data
  415. */
  416. static void lagrange_kernel(float du, float dv, const XYRemap *rmap,
  417. int16_t *u, int16_t *v, int16_t *ker)
  418. {
  419. float du_coeffs[3];
  420. float dv_coeffs[3];
  421. calculate_lagrange_coeffs(du, du_coeffs);
  422. calculate_lagrange_coeffs(dv, dv_coeffs);
  423. for (int i = 0; i < 3; i++) {
  424. for (int j = 0; j < 3; j++) {
  425. u[i * 3 + j] = rmap->u[i + 1][j + 1];
  426. v[i * 3 + j] = rmap->v[i + 1][j + 1];
  427. ker[i * 3 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  428. }
  429. }
  430. }
  431. /**
  432. * Calculate 1-dimensional cubic coefficients.
  433. *
  434. * @param t relative coordinate
  435. * @param coeffs coefficients
  436. */
  437. static inline void calculate_bicubic_coeffs(float t, float *coeffs)
  438. {
  439. const float tt = t * t;
  440. const float ttt = t * t * t;
  441. coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
  442. coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
  443. coeffs[2] = t + tt / 2.f - ttt / 2.f;
  444. coeffs[3] = - t / 6.f + ttt / 6.f;
  445. }
  446. /**
  447. * Calculate kernel for bicubic interpolation.
  448. *
  449. * @param du horizontal relative coordinate
  450. * @param dv vertical relative coordinate
  451. * @param rmap calculated 4x4 window
  452. * @param u u remap data
  453. * @param v v remap data
  454. * @param ker ker remap data
  455. */
  456. static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
  457. int16_t *u, int16_t *v, int16_t *ker)
  458. {
  459. float du_coeffs[4];
  460. float dv_coeffs[4];
  461. calculate_bicubic_coeffs(du, du_coeffs);
  462. calculate_bicubic_coeffs(dv, dv_coeffs);
  463. for (int i = 0; i < 4; i++) {
  464. for (int j = 0; j < 4; j++) {
  465. u[i * 4 + j] = rmap->u[i][j];
  466. v[i * 4 + j] = rmap->v[i][j];
  467. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  468. }
  469. }
  470. }
  471. /**
  472. * Calculate 1-dimensional lanczos coefficients.
  473. *
  474. * @param t relative coordinate
  475. * @param coeffs coefficients
  476. */
  477. static inline void calculate_lanczos_coeffs(float t, float *coeffs)
  478. {
  479. float sum = 0.f;
  480. for (int i = 0; i < 4; i++) {
  481. const float x = M_PI * (t - i + 1);
  482. if (x == 0.f) {
  483. coeffs[i] = 1.f;
  484. } else {
  485. coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
  486. }
  487. sum += coeffs[i];
  488. }
  489. for (int i = 0; i < 4; i++) {
  490. coeffs[i] /= sum;
  491. }
  492. }
  493. /**
  494. * Calculate kernel for lanczos interpolation.
  495. *
  496. * @param du horizontal relative coordinate
  497. * @param dv vertical relative coordinate
  498. * @param rmap calculated 4x4 window
  499. * @param u u remap data
  500. * @param v v remap data
  501. * @param ker ker remap data
  502. */
  503. static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
  504. int16_t *u, int16_t *v, int16_t *ker)
  505. {
  506. float du_coeffs[4];
  507. float dv_coeffs[4];
  508. calculate_lanczos_coeffs(du, du_coeffs);
  509. calculate_lanczos_coeffs(dv, dv_coeffs);
  510. for (int i = 0; i < 4; i++) {
  511. for (int j = 0; j < 4; j++) {
  512. u[i * 4 + j] = rmap->u[i][j];
  513. v[i * 4 + j] = rmap->v[i][j];
  514. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  515. }
  516. }
  517. }
  518. /**
  519. * Calculate 1-dimensional spline16 coefficients.
  520. *
  521. * @param t relative coordinate
  522. * @param coeffs coefficients
  523. */
  524. static void calculate_spline16_coeffs(float t, float *coeffs)
  525. {
  526. coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
  527. coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
  528. coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
  529. coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
  530. }
  531. /**
  532. * Calculate kernel for spline16 interpolation.
  533. *
  534. * @param du horizontal relative coordinate
  535. * @param dv vertical relative coordinate
  536. * @param rmap calculated 4x4 window
  537. * @param u u remap data
  538. * @param v v remap data
  539. * @param ker ker remap data
  540. */
  541. static void spline16_kernel(float du, float dv, const XYRemap *rmap,
  542. int16_t *u, int16_t *v, int16_t *ker)
  543. {
  544. float du_coeffs[4];
  545. float dv_coeffs[4];
  546. calculate_spline16_coeffs(du, du_coeffs);
  547. calculate_spline16_coeffs(dv, dv_coeffs);
  548. for (int i = 0; i < 4; i++) {
  549. for (int j = 0; j < 4; j++) {
  550. u[i * 4 + j] = rmap->u[i][j];
  551. v[i * 4 + j] = rmap->v[i][j];
  552. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  553. }
  554. }
  555. }
  556. /**
  557. * Calculate 1-dimensional gaussian coefficients.
  558. *
  559. * @param t relative coordinate
  560. * @param coeffs coefficients
  561. */
  562. static void calculate_gaussian_coeffs(float t, float *coeffs)
  563. {
  564. float sum = 0.f;
  565. for (int i = 0; i < 4; i++) {
  566. const float x = t - (i - 1);
  567. if (x == 0.f) {
  568. coeffs[i] = 1.f;
  569. } else {
  570. coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
  571. }
  572. sum += coeffs[i];
  573. }
  574. for (int i = 0; i < 4; i++) {
  575. coeffs[i] /= sum;
  576. }
  577. }
  578. /**
  579. * Calculate kernel for gaussian interpolation.
  580. *
  581. * @param du horizontal relative coordinate
  582. * @param dv vertical relative coordinate
  583. * @param rmap calculated 4x4 window
  584. * @param u u remap data
  585. * @param v v remap data
  586. * @param ker ker remap data
  587. */
  588. static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
  589. int16_t *u, int16_t *v, int16_t *ker)
  590. {
  591. float du_coeffs[4];
  592. float dv_coeffs[4];
  593. calculate_gaussian_coeffs(du, du_coeffs);
  594. calculate_gaussian_coeffs(dv, dv_coeffs);
  595. for (int i = 0; i < 4; i++) {
  596. for (int j = 0; j < 4; j++) {
  597. u[i * 4 + j] = rmap->u[i][j];
  598. v[i * 4 + j] = rmap->v[i][j];
  599. ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
  600. }
  601. }
  602. }
  603. /**
  604. * Modulo operation with only positive remainders.
  605. *
  606. * @param a dividend
  607. * @param b divisor
  608. *
  609. * @return positive remainder of (a / b)
  610. */
  611. static inline int mod(int a, int b)
  612. {
  613. const int res = a % b;
  614. if (res < 0) {
  615. return res + b;
  616. } else {
  617. return res;
  618. }
  619. }
  620. /**
  621. * Reflect y operation.
  622. *
  623. * @param y input vertical position
  624. * @param h input height
  625. */
  626. static inline int reflecty(int y, int h)
  627. {
  628. if (y < 0) {
  629. return -y;
  630. } else if (y >= h) {
  631. return 2 * h - 1 - y;
  632. }
  633. return y;
  634. }
  635. /**
  636. * Reflect x operation for equirect.
  637. *
  638. * @param x input horizontal position
  639. * @param y input vertical position
  640. * @param w input width
  641. * @param h input height
  642. */
  643. static inline int ereflectx(int x, int y, int w, int h)
  644. {
  645. if (y < 0 || y >= h)
  646. x += w / 2;
  647. return mod(x, w);
  648. }
  649. /**
  650. * Reflect x operation.
  651. *
  652. * @param x input horizontal position
  653. * @param y input vertical position
  654. * @param w input width
  655. * @param h input height
  656. */
  657. static inline int reflectx(int x, int y, int w, int h)
  658. {
  659. if (y < 0 || y >= h)
  660. return w - 1 - x;
  661. return mod(x, w);
  662. }
  663. /**
  664. * Convert char to corresponding direction.
  665. * Used for cubemap options.
  666. */
  667. static int get_direction(char c)
  668. {
  669. switch (c) {
  670. case 'r':
  671. return RIGHT;
  672. case 'l':
  673. return LEFT;
  674. case 'u':
  675. return UP;
  676. case 'd':
  677. return DOWN;
  678. case 'f':
  679. return FRONT;
  680. case 'b':
  681. return BACK;
  682. default:
  683. return -1;
  684. }
  685. }
  686. /**
  687. * Convert char to corresponding rotation angle.
  688. * Used for cubemap options.
  689. */
  690. static int get_rotation(char c)
  691. {
  692. switch (c) {
  693. case '0':
  694. return ROT_0;
  695. case '1':
  696. return ROT_90;
  697. case '2':
  698. return ROT_180;
  699. case '3':
  700. return ROT_270;
  701. default:
  702. return -1;
  703. }
  704. }
  705. /**
  706. * Convert char to corresponding rotation order.
  707. */
  708. static int get_rorder(char c)
  709. {
  710. switch (c) {
  711. case 'Y':
  712. case 'y':
  713. return YAW;
  714. case 'P':
  715. case 'p':
  716. return PITCH;
  717. case 'R':
  718. case 'r':
  719. return ROLL;
  720. default:
  721. return -1;
  722. }
  723. }
  724. /**
  725. * Prepare data for processing cubemap input format.
  726. *
  727. * @param ctx filter context
  728. *
  729. * @return error code
  730. */
  731. static int prepare_cube_in(AVFilterContext *ctx)
  732. {
  733. V360Context *s = ctx->priv;
  734. for (int face = 0; face < NB_FACES; face++) {
  735. const char c = s->in_forder[face];
  736. int direction;
  737. if (c == '\0') {
  738. av_log(ctx, AV_LOG_ERROR,
  739. "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
  740. return AVERROR(EINVAL);
  741. }
  742. direction = get_direction(c);
  743. if (direction == -1) {
  744. av_log(ctx, AV_LOG_ERROR,
  745. "Incorrect direction symbol '%c' in in_forder option.\n", c);
  746. return AVERROR(EINVAL);
  747. }
  748. s->in_cubemap_face_order[direction] = face;
  749. }
  750. for (int face = 0; face < NB_FACES; face++) {
  751. const char c = s->in_frot[face];
  752. int rotation;
  753. if (c == '\0') {
  754. av_log(ctx, AV_LOG_ERROR,
  755. "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
  756. return AVERROR(EINVAL);
  757. }
  758. rotation = get_rotation(c);
  759. if (rotation == -1) {
  760. av_log(ctx, AV_LOG_ERROR,
  761. "Incorrect rotation symbol '%c' in in_frot option.\n", c);
  762. return AVERROR(EINVAL);
  763. }
  764. s->in_cubemap_face_rotation[face] = rotation;
  765. }
  766. return 0;
  767. }
  768. /**
  769. * Prepare data for processing cubemap output format.
  770. *
  771. * @param ctx filter context
  772. *
  773. * @return error code
  774. */
  775. static int prepare_cube_out(AVFilterContext *ctx)
  776. {
  777. V360Context *s = ctx->priv;
  778. for (int face = 0; face < NB_FACES; face++) {
  779. const char c = s->out_forder[face];
  780. int direction;
  781. if (c == '\0') {
  782. av_log(ctx, AV_LOG_ERROR,
  783. "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
  784. return AVERROR(EINVAL);
  785. }
  786. direction = get_direction(c);
  787. if (direction == -1) {
  788. av_log(ctx, AV_LOG_ERROR,
  789. "Incorrect direction symbol '%c' in out_forder option.\n", c);
  790. return AVERROR(EINVAL);
  791. }
  792. s->out_cubemap_direction_order[face] = direction;
  793. }
  794. for (int face = 0; face < NB_FACES; face++) {
  795. const char c = s->out_frot[face];
  796. int rotation;
  797. if (c == '\0') {
  798. av_log(ctx, AV_LOG_ERROR,
  799. "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
  800. return AVERROR(EINVAL);
  801. }
  802. rotation = get_rotation(c);
  803. if (rotation == -1) {
  804. av_log(ctx, AV_LOG_ERROR,
  805. "Incorrect rotation symbol '%c' in out_frot option.\n", c);
  806. return AVERROR(EINVAL);
  807. }
  808. s->out_cubemap_face_rotation[face] = rotation;
  809. }
  810. return 0;
  811. }
  812. static inline void rotate_cube_face(float *uf, float *vf, int rotation)
  813. {
  814. float tmp;
  815. switch (rotation) {
  816. case ROT_0:
  817. break;
  818. case ROT_90:
  819. tmp = *uf;
  820. *uf = -*vf;
  821. *vf = tmp;
  822. break;
  823. case ROT_180:
  824. *uf = -*uf;
  825. *vf = -*vf;
  826. break;
  827. case ROT_270:
  828. tmp = -*uf;
  829. *uf = *vf;
  830. *vf = tmp;
  831. break;
  832. default:
  833. av_assert0(0);
  834. }
  835. }
  836. static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
  837. {
  838. float tmp;
  839. switch (rotation) {
  840. case ROT_0:
  841. break;
  842. case ROT_90:
  843. tmp = -*uf;
  844. *uf = *vf;
  845. *vf = tmp;
  846. break;
  847. case ROT_180:
  848. *uf = -*uf;
  849. *vf = -*vf;
  850. break;
  851. case ROT_270:
  852. tmp = *uf;
  853. *uf = -*vf;
  854. *vf = tmp;
  855. break;
  856. default:
  857. av_assert0(0);
  858. }
  859. }
  860. /**
  861. * Normalize vector.
  862. *
  863. * @param vec vector
  864. */
  865. static void normalize_vector(float *vec)
  866. {
  867. const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
  868. vec[0] /= norm;
  869. vec[1] /= norm;
  870. vec[2] /= norm;
  871. }
  872. /**
  873. * Calculate 3D coordinates on sphere for corresponding cubemap position.
  874. * Common operation for every cubemap.
  875. *
  876. * @param s filter private context
  877. * @param uf horizontal cubemap coordinate [0, 1)
  878. * @param vf vertical cubemap coordinate [0, 1)
  879. * @param face face of cubemap
  880. * @param vec coordinates on sphere
  881. * @param scalew scale for uf
  882. * @param scaleh scale for vf
  883. */
  884. static void cube_to_xyz(const V360Context *s,
  885. float uf, float vf, int face,
  886. float *vec, float scalew, float scaleh)
  887. {
  888. const int direction = s->out_cubemap_direction_order[face];
  889. float l_x, l_y, l_z;
  890. uf /= scalew;
  891. vf /= scaleh;
  892. rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
  893. switch (direction) {
  894. case RIGHT:
  895. l_x = 1.f;
  896. l_y = vf;
  897. l_z = -uf;
  898. break;
  899. case LEFT:
  900. l_x = -1.f;
  901. l_y = vf;
  902. l_z = uf;
  903. break;
  904. case UP:
  905. l_x = uf;
  906. l_y = -1.f;
  907. l_z = vf;
  908. break;
  909. case DOWN:
  910. l_x = uf;
  911. l_y = 1.f;
  912. l_z = -vf;
  913. break;
  914. case FRONT:
  915. l_x = uf;
  916. l_y = vf;
  917. l_z = 1.f;
  918. break;
  919. case BACK:
  920. l_x = -uf;
  921. l_y = vf;
  922. l_z = -1.f;
  923. break;
  924. default:
  925. av_assert0(0);
  926. }
  927. vec[0] = l_x;
  928. vec[1] = l_y;
  929. vec[2] = l_z;
  930. normalize_vector(vec);
  931. }
  932. /**
  933. * Calculate cubemap position for corresponding 3D coordinates on sphere.
  934. * Common operation for every cubemap.
  935. *
  936. * @param s filter private context
  937. * @param vec coordinated on sphere
  938. * @param uf horizontal cubemap coordinate [0, 1)
  939. * @param vf vertical cubemap coordinate [0, 1)
  940. * @param direction direction of view
  941. */
  942. static void xyz_to_cube(const V360Context *s,
  943. const float *vec,
  944. float *uf, float *vf, int *direction)
  945. {
  946. const float phi = atan2f(vec[0], vec[2]);
  947. const float theta = asinf(vec[1]);
  948. float phi_norm, theta_threshold;
  949. int face;
  950. if (phi >= -M_PI_4 && phi < M_PI_4) {
  951. *direction = FRONT;
  952. phi_norm = phi;
  953. } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
  954. *direction = LEFT;
  955. phi_norm = phi + M_PI_2;
  956. } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
  957. *direction = RIGHT;
  958. phi_norm = phi - M_PI_2;
  959. } else {
  960. *direction = BACK;
  961. phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
  962. }
  963. theta_threshold = atanf(cosf(phi_norm));
  964. if (theta > theta_threshold) {
  965. *direction = DOWN;
  966. } else if (theta < -theta_threshold) {
  967. *direction = UP;
  968. }
  969. switch (*direction) {
  970. case RIGHT:
  971. *uf = -vec[2] / vec[0];
  972. *vf = vec[1] / vec[0];
  973. break;
  974. case LEFT:
  975. *uf = -vec[2] / vec[0];
  976. *vf = -vec[1] / vec[0];
  977. break;
  978. case UP:
  979. *uf = -vec[0] / vec[1];
  980. *vf = -vec[2] / vec[1];
  981. break;
  982. case DOWN:
  983. *uf = vec[0] / vec[1];
  984. *vf = -vec[2] / vec[1];
  985. break;
  986. case FRONT:
  987. *uf = vec[0] / vec[2];
  988. *vf = vec[1] / vec[2];
  989. break;
  990. case BACK:
  991. *uf = vec[0] / vec[2];
  992. *vf = -vec[1] / vec[2];
  993. break;
  994. default:
  995. av_assert0(0);
  996. }
  997. face = s->in_cubemap_face_order[*direction];
  998. rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
  999. (*uf) *= s->input_mirror_modifier[0];
  1000. (*vf) *= s->input_mirror_modifier[1];
  1001. }
  1002. /**
  1003. * Find position on another cube face in case of overflow/underflow.
  1004. * Used for calculation of interpolation window.
  1005. *
  1006. * @param s filter private context
  1007. * @param uf horizontal cubemap coordinate
  1008. * @param vf vertical cubemap coordinate
  1009. * @param direction direction of view
  1010. * @param new_uf new horizontal cubemap coordinate
  1011. * @param new_vf new vertical cubemap coordinate
  1012. * @param face face position on cubemap
  1013. */
  1014. static void process_cube_coordinates(const V360Context *s,
  1015. float uf, float vf, int direction,
  1016. float *new_uf, float *new_vf, int *face)
  1017. {
  1018. /*
  1019. * Cubemap orientation
  1020. *
  1021. * width
  1022. * <------->
  1023. * +-------+
  1024. * | | U
  1025. * | up | h ------->
  1026. * +-------+-------+-------+-------+ ^ e |
  1027. * | | | | | | i V |
  1028. * | left | front | right | back | | g |
  1029. * +-------+-------+-------+-------+ v h v
  1030. * | | t
  1031. * | down |
  1032. * +-------+
  1033. */
  1034. *face = s->in_cubemap_face_order[direction];
  1035. rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
  1036. if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
  1037. // There are no pixels to use in this case
  1038. *new_uf = uf;
  1039. *new_vf = vf;
  1040. } else if (uf < -1.f) {
  1041. uf += 2.f;
  1042. switch (direction) {
  1043. case RIGHT:
  1044. direction = FRONT;
  1045. *new_uf = uf;
  1046. *new_vf = vf;
  1047. break;
  1048. case LEFT:
  1049. direction = BACK;
  1050. *new_uf = uf;
  1051. *new_vf = vf;
  1052. break;
  1053. case UP:
  1054. direction = LEFT;
  1055. *new_uf = vf;
  1056. *new_vf = -uf;
  1057. break;
  1058. case DOWN:
  1059. direction = LEFT;
  1060. *new_uf = -vf;
  1061. *new_vf = uf;
  1062. break;
  1063. case FRONT:
  1064. direction = LEFT;
  1065. *new_uf = uf;
  1066. *new_vf = vf;
  1067. break;
  1068. case BACK:
  1069. direction = RIGHT;
  1070. *new_uf = uf;
  1071. *new_vf = vf;
  1072. break;
  1073. default:
  1074. av_assert0(0);
  1075. }
  1076. } else if (uf >= 1.f) {
  1077. uf -= 2.f;
  1078. switch (direction) {
  1079. case RIGHT:
  1080. direction = BACK;
  1081. *new_uf = uf;
  1082. *new_vf = vf;
  1083. break;
  1084. case LEFT:
  1085. direction = FRONT;
  1086. *new_uf = uf;
  1087. *new_vf = vf;
  1088. break;
  1089. case UP:
  1090. direction = RIGHT;
  1091. *new_uf = -vf;
  1092. *new_vf = uf;
  1093. break;
  1094. case DOWN:
  1095. direction = RIGHT;
  1096. *new_uf = vf;
  1097. *new_vf = -uf;
  1098. break;
  1099. case FRONT:
  1100. direction = RIGHT;
  1101. *new_uf = uf;
  1102. *new_vf = vf;
  1103. break;
  1104. case BACK:
  1105. direction = LEFT;
  1106. *new_uf = uf;
  1107. *new_vf = vf;
  1108. break;
  1109. default:
  1110. av_assert0(0);
  1111. }
  1112. } else if (vf < -1.f) {
  1113. vf += 2.f;
  1114. switch (direction) {
  1115. case RIGHT:
  1116. direction = UP;
  1117. *new_uf = vf;
  1118. *new_vf = -uf;
  1119. break;
  1120. case LEFT:
  1121. direction = UP;
  1122. *new_uf = -vf;
  1123. *new_vf = uf;
  1124. break;
  1125. case UP:
  1126. direction = BACK;
  1127. *new_uf = -uf;
  1128. *new_vf = -vf;
  1129. break;
  1130. case DOWN:
  1131. direction = FRONT;
  1132. *new_uf = uf;
  1133. *new_vf = vf;
  1134. break;
  1135. case FRONT:
  1136. direction = UP;
  1137. *new_uf = uf;
  1138. *new_vf = vf;
  1139. break;
  1140. case BACK:
  1141. direction = UP;
  1142. *new_uf = -uf;
  1143. *new_vf = -vf;
  1144. break;
  1145. default:
  1146. av_assert0(0);
  1147. }
  1148. } else if (vf >= 1.f) {
  1149. vf -= 2.f;
  1150. switch (direction) {
  1151. case RIGHT:
  1152. direction = DOWN;
  1153. *new_uf = -vf;
  1154. *new_vf = uf;
  1155. break;
  1156. case LEFT:
  1157. direction = DOWN;
  1158. *new_uf = vf;
  1159. *new_vf = -uf;
  1160. break;
  1161. case UP:
  1162. direction = FRONT;
  1163. *new_uf = uf;
  1164. *new_vf = vf;
  1165. break;
  1166. case DOWN:
  1167. direction = BACK;
  1168. *new_uf = -uf;
  1169. *new_vf = -vf;
  1170. break;
  1171. case FRONT:
  1172. direction = DOWN;
  1173. *new_uf = uf;
  1174. *new_vf = vf;
  1175. break;
  1176. case BACK:
  1177. direction = DOWN;
  1178. *new_uf = -uf;
  1179. *new_vf = -vf;
  1180. break;
  1181. default:
  1182. av_assert0(0);
  1183. }
  1184. } else {
  1185. // Inside cube face
  1186. *new_uf = uf;
  1187. *new_vf = vf;
  1188. }
  1189. *face = s->in_cubemap_face_order[direction];
  1190. rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
  1191. }
  1192. /**
  1193. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
  1194. *
  1195. * @param s filter private context
  1196. * @param i horizontal position on frame [0, width)
  1197. * @param j vertical position on frame [0, height)
  1198. * @param width frame width
  1199. * @param height frame height
  1200. * @param vec coordinates on sphere
  1201. */
  1202. static int cube3x2_to_xyz(const V360Context *s,
  1203. int i, int j, int width, int height,
  1204. float *vec)
  1205. {
  1206. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
  1207. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
  1208. const float ew = width / 3.f;
  1209. const float eh = height / 2.f;
  1210. const int u_face = floorf(i / ew);
  1211. const int v_face = floorf(j / eh);
  1212. const int face = u_face + 3 * v_face;
  1213. const int u_shift = ceilf(ew * u_face);
  1214. const int v_shift = ceilf(eh * v_face);
  1215. const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
  1216. const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
  1217. const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
  1218. const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
  1219. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1220. return 1;
  1221. }
  1222. /**
  1223. * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
  1224. *
  1225. * @param s filter private context
  1226. * @param vec coordinates on sphere
  1227. * @param width frame width
  1228. * @param height frame height
  1229. * @param us horizontal coordinates for interpolation window
  1230. * @param vs vertical coordinates for interpolation window
  1231. * @param du horizontal relative coordinate
  1232. * @param dv vertical relative coordinate
  1233. */
  1234. static int xyz_to_cube3x2(const V360Context *s,
  1235. const float *vec, int width, int height,
  1236. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1237. {
  1238. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
  1239. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
  1240. const float ew = width / 3.f;
  1241. const float eh = height / 2.f;
  1242. float uf, vf;
  1243. int ui, vi;
  1244. int ewi, ehi;
  1245. int direction, face;
  1246. int u_face, v_face;
  1247. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1248. uf *= scalew;
  1249. vf *= scaleh;
  1250. face = s->in_cubemap_face_order[direction];
  1251. u_face = face % 3;
  1252. v_face = face / 3;
  1253. ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
  1254. ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
  1255. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1256. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1257. ui = floorf(uf);
  1258. vi = floorf(vf);
  1259. *du = uf - ui;
  1260. *dv = vf - vi;
  1261. for (int i = 0; i < 4; i++) {
  1262. for (int j = 0; j < 4; j++) {
  1263. int new_ui = ui + j - 1;
  1264. int new_vi = vi + i - 1;
  1265. int u_shift, v_shift;
  1266. int new_ewi, new_ehi;
  1267. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1268. face = s->in_cubemap_face_order[direction];
  1269. u_face = face % 3;
  1270. v_face = face / 3;
  1271. u_shift = ceilf(ew * u_face);
  1272. v_shift = ceilf(eh * v_face);
  1273. } else {
  1274. uf = 2.f * new_ui / ewi - 1.f;
  1275. vf = 2.f * new_vi / ehi - 1.f;
  1276. uf /= scalew;
  1277. vf /= scaleh;
  1278. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1279. uf *= scalew;
  1280. vf *= scaleh;
  1281. u_face = face % 3;
  1282. v_face = face / 3;
  1283. u_shift = ceilf(ew * u_face);
  1284. v_shift = ceilf(eh * v_face);
  1285. new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
  1286. new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
  1287. new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
  1288. new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
  1289. }
  1290. us[i][j] = u_shift + new_ui;
  1291. vs[i][j] = v_shift + new_vi;
  1292. }
  1293. }
  1294. return 1;
  1295. }
  1296. /**
  1297. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
  1298. *
  1299. * @param s filter private context
  1300. * @param i horizontal position on frame [0, width)
  1301. * @param j vertical position on frame [0, height)
  1302. * @param width frame width
  1303. * @param height frame height
  1304. * @param vec coordinates on sphere
  1305. */
  1306. static int cube1x6_to_xyz(const V360Context *s,
  1307. int i, int j, int width, int height,
  1308. float *vec)
  1309. {
  1310. const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
  1311. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
  1312. const float ew = width;
  1313. const float eh = height / 6.f;
  1314. const int face = floorf(j / eh);
  1315. const int v_shift = ceilf(eh * face);
  1316. const int ehi = ceilf(eh * (face + 1)) - v_shift;
  1317. const float uf = 2.f * (i + 0.5f) / ew - 1.f;
  1318. const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
  1319. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1320. return 1;
  1321. }
  1322. /**
  1323. * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
  1324. *
  1325. * @param s filter private context
  1326. * @param i horizontal position on frame [0, width)
  1327. * @param j vertical position on frame [0, height)
  1328. * @param width frame width
  1329. * @param height frame height
  1330. * @param vec coordinates on sphere
  1331. */
  1332. static int cube6x1_to_xyz(const V360Context *s,
  1333. int i, int j, int width, int height,
  1334. float *vec)
  1335. {
  1336. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
  1337. const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
  1338. const float ew = width / 6.f;
  1339. const float eh = height;
  1340. const int face = floorf(i / ew);
  1341. const int u_shift = ceilf(ew * face);
  1342. const int ewi = ceilf(ew * (face + 1)) - u_shift;
  1343. const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
  1344. const float vf = 2.f * (j + 0.5f) / eh - 1.f;
  1345. cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
  1346. return 1;
  1347. }
  1348. /**
  1349. * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
  1350. *
  1351. * @param s filter private context
  1352. * @param vec coordinates on sphere
  1353. * @param width frame width
  1354. * @param height frame height
  1355. * @param us horizontal coordinates for interpolation window
  1356. * @param vs vertical coordinates for interpolation window
  1357. * @param du horizontal relative coordinate
  1358. * @param dv vertical relative coordinate
  1359. */
  1360. static int xyz_to_cube1x6(const V360Context *s,
  1361. const float *vec, int width, int height,
  1362. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1363. {
  1364. const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
  1365. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
  1366. const float eh = height / 6.f;
  1367. const int ewi = width;
  1368. float uf, vf;
  1369. int ui, vi;
  1370. int ehi;
  1371. int direction, face;
  1372. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1373. uf *= scalew;
  1374. vf *= scaleh;
  1375. face = s->in_cubemap_face_order[direction];
  1376. ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
  1377. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1378. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1379. ui = floorf(uf);
  1380. vi = floorf(vf);
  1381. *du = uf - ui;
  1382. *dv = vf - vi;
  1383. for (int i = 0; i < 4; i++) {
  1384. for (int j = 0; j < 4; j++) {
  1385. int new_ui = ui + j - 1;
  1386. int new_vi = vi + i - 1;
  1387. int v_shift;
  1388. int new_ehi;
  1389. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1390. face = s->in_cubemap_face_order[direction];
  1391. v_shift = ceilf(eh * face);
  1392. } else {
  1393. uf = 2.f * new_ui / ewi - 1.f;
  1394. vf = 2.f * new_vi / ehi - 1.f;
  1395. uf /= scalew;
  1396. vf /= scaleh;
  1397. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1398. uf *= scalew;
  1399. vf *= scaleh;
  1400. v_shift = ceilf(eh * face);
  1401. new_ehi = ceilf(eh * (face + 1)) - v_shift;
  1402. new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
  1403. new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
  1404. }
  1405. us[i][j] = new_ui;
  1406. vs[i][j] = v_shift + new_vi;
  1407. }
  1408. }
  1409. return 1;
  1410. }
  1411. /**
  1412. * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
  1413. *
  1414. * @param s filter private context
  1415. * @param vec coordinates on sphere
  1416. * @param width frame width
  1417. * @param height frame height
  1418. * @param us horizontal coordinates for interpolation window
  1419. * @param vs vertical coordinates for interpolation window
  1420. * @param du horizontal relative coordinate
  1421. * @param dv vertical relative coordinate
  1422. */
  1423. static int xyz_to_cube6x1(const V360Context *s,
  1424. const float *vec, int width, int height,
  1425. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1426. {
  1427. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
  1428. const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
  1429. const float ew = width / 6.f;
  1430. const int ehi = height;
  1431. float uf, vf;
  1432. int ui, vi;
  1433. int ewi;
  1434. int direction, face;
  1435. xyz_to_cube(s, vec, &uf, &vf, &direction);
  1436. uf *= scalew;
  1437. vf *= scaleh;
  1438. face = s->in_cubemap_face_order[direction];
  1439. ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
  1440. uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
  1441. vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
  1442. ui = floorf(uf);
  1443. vi = floorf(vf);
  1444. *du = uf - ui;
  1445. *dv = vf - vi;
  1446. for (int i = 0; i < 4; i++) {
  1447. for (int j = 0; j < 4; j++) {
  1448. int new_ui = ui + j - 1;
  1449. int new_vi = vi + i - 1;
  1450. int u_shift;
  1451. int new_ewi;
  1452. if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
  1453. face = s->in_cubemap_face_order[direction];
  1454. u_shift = ceilf(ew * face);
  1455. } else {
  1456. uf = 2.f * new_ui / ewi - 1.f;
  1457. vf = 2.f * new_vi / ehi - 1.f;
  1458. uf /= scalew;
  1459. vf /= scaleh;
  1460. process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
  1461. uf *= scalew;
  1462. vf *= scaleh;
  1463. u_shift = ceilf(ew * face);
  1464. new_ewi = ceilf(ew * (face + 1)) - u_shift;
  1465. new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
  1466. new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
  1467. }
  1468. us[i][j] = u_shift + new_ui;
  1469. vs[i][j] = new_vi;
  1470. }
  1471. }
  1472. return 1;
  1473. }
  1474. /**
  1475. * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
  1476. *
  1477. * @param s filter private context
  1478. * @param i horizontal position on frame [0, width)
  1479. * @param j vertical position on frame [0, height)
  1480. * @param width frame width
  1481. * @param height frame height
  1482. * @param vec coordinates on sphere
  1483. */
  1484. static int equirect_to_xyz(const V360Context *s,
  1485. int i, int j, int width, int height,
  1486. float *vec)
  1487. {
  1488. const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
  1489. const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
  1490. const float sin_phi = sinf(phi);
  1491. const float cos_phi = cosf(phi);
  1492. const float sin_theta = sinf(theta);
  1493. const float cos_theta = cosf(theta);
  1494. vec[0] = cos_theta * sin_phi;
  1495. vec[1] = sin_theta;
  1496. vec[2] = cos_theta * cos_phi;
  1497. return 1;
  1498. }
  1499. /**
  1500. * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
  1501. *
  1502. * @param s filter private context
  1503. * @param i horizontal position on frame [0, width)
  1504. * @param j vertical position on frame [0, height)
  1505. * @param width frame width
  1506. * @param height frame height
  1507. * @param vec coordinates on sphere
  1508. */
  1509. static int hequirect_to_xyz(const V360Context *s,
  1510. int i, int j, int width, int height,
  1511. float *vec)
  1512. {
  1513. const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
  1514. const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
  1515. const float sin_phi = sinf(phi);
  1516. const float cos_phi = cosf(phi);
  1517. const float sin_theta = sinf(theta);
  1518. const float cos_theta = cosf(theta);
  1519. vec[0] = cos_theta * sin_phi;
  1520. vec[1] = sin_theta;
  1521. vec[2] = cos_theta * cos_phi;
  1522. return 1;
  1523. }
  1524. /**
  1525. * Prepare data for processing stereographic output format.
  1526. *
  1527. * @param ctx filter context
  1528. *
  1529. * @return error code
  1530. */
  1531. static int prepare_stereographic_out(AVFilterContext *ctx)
  1532. {
  1533. V360Context *s = ctx->priv;
  1534. s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
  1535. s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
  1536. return 0;
  1537. }
  1538. /**
  1539. * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
  1540. *
  1541. * @param s filter private context
  1542. * @param i horizontal position on frame [0, width)
  1543. * @param j vertical position on frame [0, height)
  1544. * @param width frame width
  1545. * @param height frame height
  1546. * @param vec coordinates on sphere
  1547. */
  1548. static int stereographic_to_xyz(const V360Context *s,
  1549. int i, int j, int width, int height,
  1550. float *vec)
  1551. {
  1552. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1553. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1554. const float r = hypotf(x, y);
  1555. const float theta = atanf(r) * 2.f;
  1556. const float sin_theta = sinf(theta);
  1557. vec[0] = x / r * sin_theta;
  1558. vec[1] = y / r * sin_theta;
  1559. vec[2] = cosf(theta);
  1560. normalize_vector(vec);
  1561. return 1;
  1562. }
  1563. /**
  1564. * Prepare data for processing stereographic input format.
  1565. *
  1566. * @param ctx filter context
  1567. *
  1568. * @return error code
  1569. */
  1570. static int prepare_stereographic_in(AVFilterContext *ctx)
  1571. {
  1572. V360Context *s = ctx->priv;
  1573. s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
  1574. s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
  1575. return 0;
  1576. }
  1577. /**
  1578. * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
  1579. *
  1580. * @param s filter private context
  1581. * @param vec coordinates on sphere
  1582. * @param width frame width
  1583. * @param height frame height
  1584. * @param us horizontal coordinates for interpolation window
  1585. * @param vs vertical coordinates for interpolation window
  1586. * @param du horizontal relative coordinate
  1587. * @param dv vertical relative coordinate
  1588. */
  1589. static int xyz_to_stereographic(const V360Context *s,
  1590. const float *vec, int width, int height,
  1591. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1592. {
  1593. const float theta = acosf(vec[2]);
  1594. const float r = tanf(theta * 0.5f);
  1595. const float c = r / hypotf(vec[0], vec[1]);
  1596. const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
  1597. const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
  1598. const float uf = (x + 1.f) * width / 2.f;
  1599. const float vf = (y + 1.f) * height / 2.f;
  1600. const int ui = floorf(uf);
  1601. const int vi = floorf(vf);
  1602. const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1603. *du = visible ? uf - ui : 0.f;
  1604. *dv = visible ? vf - vi : 0.f;
  1605. for (int i = 0; i < 4; i++) {
  1606. for (int j = 0; j < 4; j++) {
  1607. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1608. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1609. }
  1610. }
  1611. return visible;
  1612. }
  1613. /**
  1614. * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
  1615. *
  1616. * @param s filter private context
  1617. * @param vec coordinates on sphere
  1618. * @param width frame width
  1619. * @param height frame height
  1620. * @param us horizontal coordinates for interpolation window
  1621. * @param vs vertical coordinates for interpolation window
  1622. * @param du horizontal relative coordinate
  1623. * @param dv vertical relative coordinate
  1624. */
  1625. static int xyz_to_equirect(const V360Context *s,
  1626. const float *vec, int width, int height,
  1627. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1628. {
  1629. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  1630. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  1631. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  1632. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1633. const int ui = floorf(uf);
  1634. const int vi = floorf(vf);
  1635. *du = uf - ui;
  1636. *dv = vf - vi;
  1637. for (int i = 0; i < 4; i++) {
  1638. for (int j = 0; j < 4; j++) {
  1639. us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
  1640. vs[i][j] = reflecty(vi + i - 1, height);
  1641. }
  1642. }
  1643. return 1;
  1644. }
  1645. /**
  1646. * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
  1647. *
  1648. * @param s filter private context
  1649. * @param vec coordinates on sphere
  1650. * @param width frame width
  1651. * @param height frame height
  1652. * @param us horizontal coordinates for interpolation window
  1653. * @param vs vertical coordinates for interpolation window
  1654. * @param du horizontal relative coordinate
  1655. * @param dv vertical relative coordinate
  1656. */
  1657. static int xyz_to_hequirect(const V360Context *s,
  1658. const float *vec, int width, int height,
  1659. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1660. {
  1661. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  1662. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  1663. const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
  1664. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1665. const int ui = floorf(uf);
  1666. const int vi = floorf(vf);
  1667. const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
  1668. *du = uf - ui;
  1669. *dv = vf - vi;
  1670. for (int i = 0; i < 4; i++) {
  1671. for (int j = 0; j < 4; j++) {
  1672. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1673. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1674. }
  1675. }
  1676. return visible;
  1677. }
  1678. /**
  1679. * Prepare data for processing flat input format.
  1680. *
  1681. * @param ctx filter context
  1682. *
  1683. * @return error code
  1684. */
  1685. static int prepare_flat_in(AVFilterContext *ctx)
  1686. {
  1687. V360Context *s = ctx->priv;
  1688. s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
  1689. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  1690. return 0;
  1691. }
  1692. /**
  1693. * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
  1694. *
  1695. * @param s filter private context
  1696. * @param vec coordinates on sphere
  1697. * @param width frame width
  1698. * @param height frame height
  1699. * @param us horizontal coordinates for interpolation window
  1700. * @param vs vertical coordinates for interpolation window
  1701. * @param du horizontal relative coordinate
  1702. * @param dv vertical relative coordinate
  1703. */
  1704. static int xyz_to_flat(const V360Context *s,
  1705. const float *vec, int width, int height,
  1706. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1707. {
  1708. const float theta = acosf(vec[2]);
  1709. const float r = tanf(theta);
  1710. const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
  1711. const float zf = vec[2];
  1712. const float h = hypotf(vec[0], vec[1]);
  1713. const float c = h <= 1e-6f ? 1.f : rr / h;
  1714. float uf = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
  1715. float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
  1716. int visible, ui, vi;
  1717. uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
  1718. vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
  1719. ui = floorf(uf);
  1720. vi = floorf(vf);
  1721. visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
  1722. *du = uf - ui;
  1723. *dv = vf - vi;
  1724. for (int i = 0; i < 4; i++) {
  1725. for (int j = 0; j < 4; j++) {
  1726. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1727. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1728. }
  1729. }
  1730. return visible;
  1731. }
  1732. /**
  1733. * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
  1734. *
  1735. * @param s filter private context
  1736. * @param vec coordinates on sphere
  1737. * @param width frame width
  1738. * @param height frame height
  1739. * @param us horizontal coordinates for interpolation window
  1740. * @param vs vertical coordinates for interpolation window
  1741. * @param du horizontal relative coordinate
  1742. * @param dv vertical relative coordinate
  1743. */
  1744. static int xyz_to_mercator(const V360Context *s,
  1745. const float *vec, int width, int height,
  1746. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1747. {
  1748. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  1749. const float theta = vec[1] * s->input_mirror_modifier[1];
  1750. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  1751. const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
  1752. const int ui = floorf(uf);
  1753. const int vi = floorf(vf);
  1754. *du = uf - ui;
  1755. *dv = vf - vi;
  1756. for (int i = 0; i < 4; i++) {
  1757. for (int j = 0; j < 4; j++) {
  1758. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1759. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1760. }
  1761. }
  1762. return 1;
  1763. }
  1764. /**
  1765. * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
  1766. *
  1767. * @param s filter private context
  1768. * @param i horizontal position on frame [0, width)
  1769. * @param j vertical position on frame [0, height)
  1770. * @param width frame width
  1771. * @param height frame height
  1772. * @param vec coordinates on sphere
  1773. */
  1774. static int mercator_to_xyz(const V360Context *s,
  1775. int i, int j, int width, int height,
  1776. float *vec)
  1777. {
  1778. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
  1779. const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
  1780. const float div = expf(2.f * y) + 1.f;
  1781. const float sin_phi = sinf(phi);
  1782. const float cos_phi = cosf(phi);
  1783. const float sin_theta = 2.f * expf(y) / div;
  1784. const float cos_theta = (expf(2.f * y) - 1.f) / div;
  1785. vec[0] = -sin_theta * cos_phi;
  1786. vec[1] = cos_theta;
  1787. vec[2] = sin_theta * sin_phi;
  1788. return 1;
  1789. }
  1790. /**
  1791. * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
  1792. *
  1793. * @param s filter private context
  1794. * @param vec coordinates on sphere
  1795. * @param width frame width
  1796. * @param height frame height
  1797. * @param us horizontal coordinates for interpolation window
  1798. * @param vs vertical coordinates for interpolation window
  1799. * @param du horizontal relative coordinate
  1800. * @param dv vertical relative coordinate
  1801. */
  1802. static int xyz_to_ball(const V360Context *s,
  1803. const float *vec, int width, int height,
  1804. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1805. {
  1806. const float l = hypotf(vec[0], vec[1]);
  1807. const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
  1808. const float uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
  1809. const float vf = (1.f + r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
  1810. const int ui = floorf(uf);
  1811. const int vi = floorf(vf);
  1812. *du = uf - ui;
  1813. *dv = vf - vi;
  1814. for (int i = 0; i < 4; i++) {
  1815. for (int j = 0; j < 4; j++) {
  1816. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1817. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1818. }
  1819. }
  1820. return 1;
  1821. }
  1822. /**
  1823. * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
  1824. *
  1825. * @param s filter private context
  1826. * @param i horizontal position on frame [0, width)
  1827. * @param j vertical position on frame [0, height)
  1828. * @param width frame width
  1829. * @param height frame height
  1830. * @param vec coordinates on sphere
  1831. */
  1832. static int ball_to_xyz(const V360Context *s,
  1833. int i, int j, int width, int height,
  1834. float *vec)
  1835. {
  1836. const float x = (2.f * i + 1.f) / width - 1.f;
  1837. const float y = (2.f * j + 1.f) / height - 1.f;
  1838. const float l = hypotf(x, y);
  1839. if (l <= 1.f) {
  1840. const float z = 2.f * l * sqrtf(1.f - l * l);
  1841. vec[0] = z * x / (l > 0.f ? l : 1.f);
  1842. vec[1] = z * y / (l > 0.f ? l : 1.f);
  1843. vec[2] = 1.f - 2.f * l * l;
  1844. } else {
  1845. vec[0] = 0.f;
  1846. vec[1] = 1.f;
  1847. vec[2] = 0.f;
  1848. return 0;
  1849. }
  1850. return 1;
  1851. }
  1852. /**
  1853. * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
  1854. *
  1855. * @param s filter private context
  1856. * @param i horizontal position on frame [0, width)
  1857. * @param j vertical position on frame [0, height)
  1858. * @param width frame width
  1859. * @param height frame height
  1860. * @param vec coordinates on sphere
  1861. */
  1862. static int hammer_to_xyz(const V360Context *s,
  1863. int i, int j, int width, int height,
  1864. float *vec)
  1865. {
  1866. const float x = ((2.f * i + 1.f) / width - 1.f);
  1867. const float y = ((2.f * j + 1.f) / height - 1.f);
  1868. const float xx = x * x;
  1869. const float yy = y * y;
  1870. const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
  1871. const float a = M_SQRT2 * x * z;
  1872. const float b = 2.f * z * z - 1.f;
  1873. const float aa = a * a;
  1874. const float bb = b * b;
  1875. const float w = sqrtf(1.f - 2.f * yy * z * z);
  1876. vec[0] = w * 2.f * a * b / (aa + bb);
  1877. vec[1] = M_SQRT2 * y * z;
  1878. vec[2] = w * (bb - aa) / (aa + bb);
  1879. normalize_vector(vec);
  1880. return 1;
  1881. }
  1882. /**
  1883. * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
  1884. *
  1885. * @param s filter private context
  1886. * @param vec coordinates on sphere
  1887. * @param width frame width
  1888. * @param height frame height
  1889. * @param us horizontal coordinates for interpolation window
  1890. * @param vs vertical coordinates for interpolation window
  1891. * @param du horizontal relative coordinate
  1892. * @param dv vertical relative coordinate
  1893. */
  1894. static int xyz_to_hammer(const V360Context *s,
  1895. const float *vec, int width, int height,
  1896. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1897. {
  1898. const float theta = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  1899. const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
  1900. const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
  1901. const float y = vec[1] / z * s->input_mirror_modifier[1];
  1902. const float uf = (x + 1.f) * width / 2.f;
  1903. const float vf = (y + 1.f) * height / 2.f;
  1904. const int ui = floorf(uf);
  1905. const int vi = floorf(vf);
  1906. *du = uf - ui;
  1907. *dv = vf - vi;
  1908. for (int i = 0; i < 4; i++) {
  1909. for (int j = 0; j < 4; j++) {
  1910. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1911. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1912. }
  1913. }
  1914. return 1;
  1915. }
  1916. /**
  1917. * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
  1918. *
  1919. * @param s filter private context
  1920. * @param i horizontal position on frame [0, width)
  1921. * @param j vertical position on frame [0, height)
  1922. * @param width frame width
  1923. * @param height frame height
  1924. * @param vec coordinates on sphere
  1925. */
  1926. static int sinusoidal_to_xyz(const V360Context *s,
  1927. int i, int j, int width, int height,
  1928. float *vec)
  1929. {
  1930. const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
  1931. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
  1932. const float sin_phi = sinf(phi);
  1933. const float cos_phi = cosf(phi);
  1934. const float sin_theta = sinf(theta);
  1935. const float cos_theta = cosf(theta);
  1936. vec[0] = cos_theta * sin_phi;
  1937. vec[1] = sin_theta;
  1938. vec[2] = cos_theta * cos_phi;
  1939. normalize_vector(vec);
  1940. return 1;
  1941. }
  1942. /**
  1943. * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
  1944. *
  1945. * @param s filter private context
  1946. * @param vec coordinates on sphere
  1947. * @param width frame width
  1948. * @param height frame height
  1949. * @param us horizontal coordinates for interpolation window
  1950. * @param vs vertical coordinates for interpolation window
  1951. * @param du horizontal relative coordinate
  1952. * @param dv vertical relative coordinate
  1953. */
  1954. static int xyz_to_sinusoidal(const V360Context *s,
  1955. const float *vec, int width, int height,
  1956. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1957. {
  1958. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  1959. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
  1960. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  1961. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1962. const int ui = floorf(uf);
  1963. const int vi = floorf(vf);
  1964. *du = uf - ui;
  1965. *dv = vf - vi;
  1966. for (int i = 0; i < 4; i++) {
  1967. for (int j = 0; j < 4; j++) {
  1968. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1969. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1970. }
  1971. }
  1972. return 1;
  1973. }
  1974. /**
  1975. * Prepare data for processing equi-angular cubemap input format.
  1976. *
  1977. * @param ctx filter context
  1978. *
  1979. * @return error code
  1980. */
  1981. static int prepare_eac_in(AVFilterContext *ctx)
  1982. {
  1983. V360Context *s = ctx->priv;
  1984. if (s->ih_flip && s->iv_flip) {
  1985. s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
  1986. s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
  1987. s->in_cubemap_face_order[UP] = TOP_LEFT;
  1988. s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
  1989. s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
  1990. s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
  1991. } else if (s->ih_flip) {
  1992. s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
  1993. s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
  1994. s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
  1995. s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
  1996. s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
  1997. s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
  1998. } else if (s->iv_flip) {
  1999. s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
  2000. s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
  2001. s->in_cubemap_face_order[UP] = TOP_RIGHT;
  2002. s->in_cubemap_face_order[DOWN] = TOP_LEFT;
  2003. s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
  2004. s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
  2005. } else {
  2006. s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
  2007. s->in_cubemap_face_order[LEFT] = TOP_LEFT;
  2008. s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
  2009. s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
  2010. s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
  2011. s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
  2012. }
  2013. if (s->iv_flip) {
  2014. s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
  2015. s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
  2016. s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
  2017. s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
  2018. s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
  2019. s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
  2020. } else {
  2021. s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2022. s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2023. s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2024. s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2025. s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2026. s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2027. }
  2028. return 0;
  2029. }
  2030. /**
  2031. * Prepare data for processing equi-angular cubemap output format.
  2032. *
  2033. * @param ctx filter context
  2034. *
  2035. * @return error code
  2036. */
  2037. static int prepare_eac_out(AVFilterContext *ctx)
  2038. {
  2039. V360Context *s = ctx->priv;
  2040. s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
  2041. s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
  2042. s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
  2043. s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
  2044. s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
  2045. s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
  2046. s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2047. s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2048. s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2049. s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2050. s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2051. s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2052. return 0;
  2053. }
  2054. /**
  2055. * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
  2056. *
  2057. * @param s filter private context
  2058. * @param i horizontal position on frame [0, width)
  2059. * @param j vertical position on frame [0, height)
  2060. * @param width frame width
  2061. * @param height frame height
  2062. * @param vec coordinates on sphere
  2063. */
  2064. static int eac_to_xyz(const V360Context *s,
  2065. int i, int j, int width, int height,
  2066. float *vec)
  2067. {
  2068. const float pixel_pad = 2;
  2069. const float u_pad = pixel_pad / width;
  2070. const float v_pad = pixel_pad / height;
  2071. int u_face, v_face, face;
  2072. float l_x, l_y, l_z;
  2073. float uf = (i + 0.5f) / width;
  2074. float vf = (j + 0.5f) / height;
  2075. // EAC has 2-pixel padding on faces except between faces on the same row
  2076. // Padding pixels seems not to be stretched with tangent as regular pixels
  2077. // Formulas below approximate original padding as close as I could get experimentally
  2078. // Horizontal padding
  2079. uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
  2080. if (uf < 0.f) {
  2081. u_face = 0;
  2082. uf -= 0.5f;
  2083. } else if (uf >= 3.f) {
  2084. u_face = 2;
  2085. uf -= 2.5f;
  2086. } else {
  2087. u_face = floorf(uf);
  2088. uf = fmodf(uf, 1.f) - 0.5f;
  2089. }
  2090. // Vertical padding
  2091. v_face = floorf(vf * 2.f);
  2092. vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
  2093. if (uf >= -0.5f && uf < 0.5f) {
  2094. uf = tanf(M_PI_2 * uf);
  2095. } else {
  2096. uf = 2.f * uf;
  2097. }
  2098. if (vf >= -0.5f && vf < 0.5f) {
  2099. vf = tanf(M_PI_2 * vf);
  2100. } else {
  2101. vf = 2.f * vf;
  2102. }
  2103. face = u_face + 3 * v_face;
  2104. switch (face) {
  2105. case TOP_LEFT:
  2106. l_x = -1.f;
  2107. l_y = vf;
  2108. l_z = uf;
  2109. break;
  2110. case TOP_MIDDLE:
  2111. l_x = uf;
  2112. l_y = vf;
  2113. l_z = 1.f;
  2114. break;
  2115. case TOP_RIGHT:
  2116. l_x = 1.f;
  2117. l_y = vf;
  2118. l_z = -uf;
  2119. break;
  2120. case BOTTOM_LEFT:
  2121. l_x = -vf;
  2122. l_y = 1.f;
  2123. l_z = -uf;
  2124. break;
  2125. case BOTTOM_MIDDLE:
  2126. l_x = -vf;
  2127. l_y = -uf;
  2128. l_z = -1.f;
  2129. break;
  2130. case BOTTOM_RIGHT:
  2131. l_x = -vf;
  2132. l_y = -1.f;
  2133. l_z = uf;
  2134. break;
  2135. default:
  2136. av_assert0(0);
  2137. }
  2138. vec[0] = l_x;
  2139. vec[1] = l_y;
  2140. vec[2] = l_z;
  2141. normalize_vector(vec);
  2142. return 1;
  2143. }
  2144. /**
  2145. * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
  2146. *
  2147. * @param s filter private context
  2148. * @param vec coordinates on sphere
  2149. * @param width frame width
  2150. * @param height frame height
  2151. * @param us horizontal coordinates for interpolation window
  2152. * @param vs vertical coordinates for interpolation window
  2153. * @param du horizontal relative coordinate
  2154. * @param dv vertical relative coordinate
  2155. */
  2156. static int xyz_to_eac(const V360Context *s,
  2157. const float *vec, int width, int height,
  2158. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2159. {
  2160. const float pixel_pad = 2;
  2161. const float u_pad = pixel_pad / width;
  2162. const float v_pad = pixel_pad / height;
  2163. float uf, vf;
  2164. int ui, vi;
  2165. int direction, face;
  2166. int u_face, v_face;
  2167. xyz_to_cube(s, vec, &uf, &vf, &direction);
  2168. face = s->in_cubemap_face_order[direction];
  2169. u_face = face % 3;
  2170. v_face = face / 3;
  2171. uf = M_2_PI * atanf(uf) + 0.5f;
  2172. vf = M_2_PI * atanf(vf) + 0.5f;
  2173. // These formulas are inversed from eac_to_xyz ones
  2174. uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
  2175. vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
  2176. uf *= width;
  2177. vf *= height;
  2178. uf -= 0.5f;
  2179. vf -= 0.5f;
  2180. ui = floorf(uf);
  2181. vi = floorf(vf);
  2182. *du = uf - ui;
  2183. *dv = vf - vi;
  2184. for (int i = 0; i < 4; i++) {
  2185. for (int j = 0; j < 4; j++) {
  2186. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2187. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2188. }
  2189. }
  2190. return 1;
  2191. }
  2192. /**
  2193. * Prepare data for processing flat output format.
  2194. *
  2195. * @param ctx filter context
  2196. *
  2197. * @return error code
  2198. */
  2199. static int prepare_flat_out(AVFilterContext *ctx)
  2200. {
  2201. V360Context *s = ctx->priv;
  2202. s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
  2203. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2204. return 0;
  2205. }
  2206. /**
  2207. * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
  2208. *
  2209. * @param s filter private context
  2210. * @param i horizontal position on frame [0, width)
  2211. * @param j vertical position on frame [0, height)
  2212. * @param width frame width
  2213. * @param height frame height
  2214. * @param vec coordinates on sphere
  2215. */
  2216. static int flat_to_xyz(const V360Context *s,
  2217. int i, int j, int width, int height,
  2218. float *vec)
  2219. {
  2220. const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
  2221. const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
  2222. vec[0] = l_x;
  2223. vec[1] = l_y;
  2224. vec[2] = 1.f;
  2225. normalize_vector(vec);
  2226. return 1;
  2227. }
  2228. /**
  2229. * Prepare data for processing fisheye output format.
  2230. *
  2231. * @param ctx filter context
  2232. *
  2233. * @return error code
  2234. */
  2235. static int prepare_fisheye_out(AVFilterContext *ctx)
  2236. {
  2237. V360Context *s = ctx->priv;
  2238. s->flat_range[0] = s->h_fov / 180.f;
  2239. s->flat_range[1] = s->v_fov / 180.f;
  2240. return 0;
  2241. }
  2242. /**
  2243. * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
  2244. *
  2245. * @param s filter private context
  2246. * @param i horizontal position on frame [0, width)
  2247. * @param j vertical position on frame [0, height)
  2248. * @param width frame width
  2249. * @param height frame height
  2250. * @param vec coordinates on sphere
  2251. */
  2252. static int fisheye_to_xyz(const V360Context *s,
  2253. int i, int j, int width, int height,
  2254. float *vec)
  2255. {
  2256. const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
  2257. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2258. const float phi = atan2f(vf, uf);
  2259. const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
  2260. const float sin_phi = sinf(phi);
  2261. const float cos_phi = cosf(phi);
  2262. const float sin_theta = sinf(theta);
  2263. const float cos_theta = cosf(theta);
  2264. vec[0] = cos_theta * cos_phi;
  2265. vec[1] = cos_theta * sin_phi;
  2266. vec[2] = sin_theta;
  2267. normalize_vector(vec);
  2268. return 1;
  2269. }
  2270. /**
  2271. * Prepare data for processing fisheye input format.
  2272. *
  2273. * @param ctx filter context
  2274. *
  2275. * @return error code
  2276. */
  2277. static int prepare_fisheye_in(AVFilterContext *ctx)
  2278. {
  2279. V360Context *s = ctx->priv;
  2280. s->iflat_range[0] = s->ih_fov / 180.f;
  2281. s->iflat_range[1] = s->iv_fov / 180.f;
  2282. return 0;
  2283. }
  2284. /**
  2285. * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
  2286. *
  2287. * @param s filter private context
  2288. * @param vec coordinates on sphere
  2289. * @param width frame width
  2290. * @param height frame height
  2291. * @param us horizontal coordinates for interpolation window
  2292. * @param vs vertical coordinates for interpolation window
  2293. * @param du horizontal relative coordinate
  2294. * @param dv vertical relative coordinate
  2295. */
  2296. static int xyz_to_fisheye(const V360Context *s,
  2297. const float *vec, int width, int height,
  2298. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2299. {
  2300. const float h = hypotf(vec[0], vec[1]);
  2301. const float lh = h > 0.f ? h : 1.f;
  2302. const float phi = atan2f(h, vec[2]) / M_PI;
  2303. float uf = vec[0] / lh * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
  2304. float vf = vec[1] / lh * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
  2305. const int visible = hypotf(uf, vf) <= 0.5f;
  2306. int ui, vi;
  2307. uf = (uf + 0.5f) * width;
  2308. vf = (vf + 0.5f) * height;
  2309. ui = floorf(uf);
  2310. vi = floorf(vf);
  2311. *du = visible ? uf - ui : 0.f;
  2312. *dv = visible ? vf - vi : 0.f;
  2313. for (int i = 0; i < 4; i++) {
  2314. for (int j = 0; j < 4; j++) {
  2315. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2316. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2317. }
  2318. }
  2319. return visible;
  2320. }
  2321. /**
  2322. * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
  2323. *
  2324. * @param s filter private context
  2325. * @param i horizontal position on frame [0, width)
  2326. * @param j vertical position on frame [0, height)
  2327. * @param width frame width
  2328. * @param height frame height
  2329. * @param vec coordinates on sphere
  2330. */
  2331. static int pannini_to_xyz(const V360Context *s,
  2332. int i, int j, int width, int height,
  2333. float *vec)
  2334. {
  2335. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2336. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2337. const float d = s->h_fov;
  2338. const float k = uf * uf / ((d + 1.f) * (d + 1.f));
  2339. const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
  2340. const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
  2341. const float S = (d + 1.f) / (d + clon);
  2342. const float lon = atan2f(uf, S * clon);
  2343. const float lat = atan2f(vf, S);
  2344. vec[0] = sinf(lon) * cosf(lat);
  2345. vec[1] = sinf(lat);
  2346. vec[2] = cosf(lon) * cosf(lat);
  2347. normalize_vector(vec);
  2348. return 1;
  2349. }
  2350. /**
  2351. * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
  2352. *
  2353. * @param s filter private context
  2354. * @param vec coordinates on sphere
  2355. * @param width frame width
  2356. * @param height frame height
  2357. * @param us horizontal coordinates for interpolation window
  2358. * @param vs vertical coordinates for interpolation window
  2359. * @param du horizontal relative coordinate
  2360. * @param dv vertical relative coordinate
  2361. */
  2362. static int xyz_to_pannini(const V360Context *s,
  2363. const float *vec, int width, int height,
  2364. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2365. {
  2366. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  2367. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  2368. const float d = s->ih_fov;
  2369. const float S = (d + 1.f) / (d + cosf(phi));
  2370. const float x = S * sinf(phi);
  2371. const float y = S * tanf(theta);
  2372. const float uf = (x + 1.f) * width / 2.f;
  2373. const float vf = (y + 1.f) * height / 2.f;
  2374. const int ui = floorf(uf);
  2375. const int vi = floorf(vf);
  2376. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
  2377. *du = uf - ui;
  2378. *dv = vf - vi;
  2379. for (int i = 0; i < 4; i++) {
  2380. for (int j = 0; j < 4; j++) {
  2381. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2382. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2383. }
  2384. }
  2385. return visible;
  2386. }
  2387. /**
  2388. * Prepare data for processing cylindrical output format.
  2389. *
  2390. * @param ctx filter context
  2391. *
  2392. * @return error code
  2393. */
  2394. static int prepare_cylindrical_out(AVFilterContext *ctx)
  2395. {
  2396. V360Context *s = ctx->priv;
  2397. s->flat_range[0] = M_PI * s->h_fov / 360.f;
  2398. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2399. return 0;
  2400. }
  2401. /**
  2402. * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
  2403. *
  2404. * @param s filter private context
  2405. * @param i horizontal position on frame [0, width)
  2406. * @param j vertical position on frame [0, height)
  2407. * @param width frame width
  2408. * @param height frame height
  2409. * @param vec coordinates on sphere
  2410. */
  2411. static int cylindrical_to_xyz(const V360Context *s,
  2412. int i, int j, int width, int height,
  2413. float *vec)
  2414. {
  2415. const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
  2416. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2417. const float phi = uf;
  2418. const float theta = atanf(vf);
  2419. const float sin_phi = sinf(phi);
  2420. const float cos_phi = cosf(phi);
  2421. const float sin_theta = sinf(theta);
  2422. const float cos_theta = cosf(theta);
  2423. vec[0] = cos_theta * sin_phi;
  2424. vec[1] = sin_theta;
  2425. vec[2] = cos_theta * cos_phi;
  2426. normalize_vector(vec);
  2427. return 1;
  2428. }
  2429. /**
  2430. * Prepare data for processing cylindrical input format.
  2431. *
  2432. * @param ctx filter context
  2433. *
  2434. * @return error code
  2435. */
  2436. static int prepare_cylindrical_in(AVFilterContext *ctx)
  2437. {
  2438. V360Context *s = ctx->priv;
  2439. s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
  2440. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  2441. return 0;
  2442. }
  2443. /**
  2444. * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
  2445. *
  2446. * @param s filter private context
  2447. * @param vec coordinates on sphere
  2448. * @param width frame width
  2449. * @param height frame height
  2450. * @param us horizontal coordinates for interpolation window
  2451. * @param vs vertical coordinates for interpolation window
  2452. * @param du horizontal relative coordinate
  2453. * @param dv vertical relative coordinate
  2454. */
  2455. static int xyz_to_cylindrical(const V360Context *s,
  2456. const float *vec, int width, int height,
  2457. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2458. {
  2459. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
  2460. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  2461. const float uf = (phi + 1.f) * (width - 1) / 2.f;
  2462. const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
  2463. const int ui = floorf(uf);
  2464. const int vi = floorf(vf);
  2465. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
  2466. theta <= M_PI * s->iv_fov / 180.f &&
  2467. theta >= -M_PI * s->iv_fov / 180.f;
  2468. *du = uf - ui;
  2469. *dv = vf - vi;
  2470. for (int i = 0; i < 4; i++) {
  2471. for (int j = 0; j < 4; j++) {
  2472. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2473. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2474. }
  2475. }
  2476. return visible;
  2477. }
  2478. /**
  2479. * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
  2480. *
  2481. * @param s filter private context
  2482. * @param i horizontal position on frame [0, width)
  2483. * @param j vertical position on frame [0, height)
  2484. * @param width frame width
  2485. * @param height frame height
  2486. * @param vec coordinates on sphere
  2487. */
  2488. static int perspective_to_xyz(const V360Context *s,
  2489. int i, int j, int width, int height,
  2490. float *vec)
  2491. {
  2492. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2493. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2494. const float rh = hypotf(uf, vf);
  2495. const float sinzz = 1.f - rh * rh;
  2496. const float h = 1.f + s->v_fov;
  2497. const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
  2498. const float sinz2 = sinz * sinz;
  2499. if (sinz2 <= 1.f) {
  2500. const float cosz = sqrtf(1.f - sinz2);
  2501. const float theta = asinf(cosz);
  2502. const float phi = atan2f(uf, vf);
  2503. const float sin_phi = sinf(phi);
  2504. const float cos_phi = cosf(phi);
  2505. const float sin_theta = sinf(theta);
  2506. const float cos_theta = cosf(theta);
  2507. vec[0] = cos_theta * sin_phi;
  2508. vec[1] = sin_theta;
  2509. vec[2] = cos_theta * cos_phi;
  2510. } else {
  2511. vec[0] = 0.f;
  2512. vec[1] = 1.f;
  2513. vec[2] = 0.f;
  2514. return 0;
  2515. }
  2516. normalize_vector(vec);
  2517. return 1;
  2518. }
  2519. /**
  2520. * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
  2521. *
  2522. * @param s filter private context
  2523. * @param i horizontal position on frame [0, width)
  2524. * @param j vertical position on frame [0, height)
  2525. * @param width frame width
  2526. * @param height frame height
  2527. * @param vec coordinates on sphere
  2528. */
  2529. static int tetrahedron_to_xyz(const V360Context *s,
  2530. int i, int j, int width, int height,
  2531. float *vec)
  2532. {
  2533. const float uf = (float)i / width;
  2534. const float vf = (float)j / height;
  2535. vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
  2536. vec[1] = 1.f - vf * 2.f;
  2537. vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
  2538. normalize_vector(vec);
  2539. return 1;
  2540. }
  2541. /**
  2542. * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
  2543. *
  2544. * @param s filter private context
  2545. * @param vec coordinates on sphere
  2546. * @param width frame width
  2547. * @param height frame height
  2548. * @param us horizontal coordinates for interpolation window
  2549. * @param vs vertical coordinates for interpolation window
  2550. * @param du horizontal relative coordinate
  2551. * @param dv vertical relative coordinate
  2552. */
  2553. static int xyz_to_tetrahedron(const V360Context *s,
  2554. const float *vec, int width, int height,
  2555. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2556. {
  2557. const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
  2558. const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
  2559. const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
  2560. const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
  2561. const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
  2562. float uf, vf, x, y, z;
  2563. int ui, vi;
  2564. x = vec[0] / d;
  2565. y = vec[1] / d;
  2566. z = -vec[2] / d;
  2567. vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
  2568. if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
  2569. (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
  2570. uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
  2571. } else {
  2572. uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
  2573. }
  2574. uf *= width;
  2575. vf *= height;
  2576. ui = floorf(uf);
  2577. vi = floorf(vf);
  2578. *du = uf - ui;
  2579. *dv = vf - vi;
  2580. for (int i = 0; i < 4; i++) {
  2581. for (int j = 0; j < 4; j++) {
  2582. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  2583. vs[i][j] = reflecty(vi + i - 1, height);
  2584. }
  2585. }
  2586. return 1;
  2587. }
  2588. /**
  2589. * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
  2590. *
  2591. * @param s filter private context
  2592. * @param i horizontal position on frame [0, width)
  2593. * @param j vertical position on frame [0, height)
  2594. * @param width frame width
  2595. * @param height frame height
  2596. * @param vec coordinates on sphere
  2597. */
  2598. static int dfisheye_to_xyz(const V360Context *s,
  2599. int i, int j, int width, int height,
  2600. float *vec)
  2601. {
  2602. const float scale = 1.f + s->out_pad;
  2603. const float ew = width / 2.f;
  2604. const float eh = height;
  2605. const int ei = i >= ew ? i - ew : i;
  2606. const float m = i >= ew ? 1.f : -1.f;
  2607. const float uf = ((2.f * ei) / ew - 1.f) * scale;
  2608. const float vf = ((2.f * j + 1.f) / eh - 1.f) * scale;
  2609. const float h = hypotf(uf, vf);
  2610. const float lh = h > 0.f ? h : 1.f;
  2611. const float theta = m * M_PI_2 * (1.f - h);
  2612. const float sin_theta = sinf(theta);
  2613. const float cos_theta = cosf(theta);
  2614. vec[0] = cos_theta * m * uf / lh;
  2615. vec[1] = cos_theta * vf / lh;
  2616. vec[2] = sin_theta;
  2617. normalize_vector(vec);
  2618. return 1;
  2619. }
  2620. /**
  2621. * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
  2622. *
  2623. * @param s filter private context
  2624. * @param vec coordinates on sphere
  2625. * @param width frame width
  2626. * @param height frame height
  2627. * @param us horizontal coordinates for interpolation window
  2628. * @param vs vertical coordinates for interpolation window
  2629. * @param du horizontal relative coordinate
  2630. * @param dv vertical relative coordinate
  2631. */
  2632. static int xyz_to_dfisheye(const V360Context *s,
  2633. const float *vec, int width, int height,
  2634. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2635. {
  2636. const float scale = 1.f - s->in_pad;
  2637. const float ew = width / 2.f;
  2638. const float eh = height;
  2639. const float h = hypotf(vec[0], vec[1]);
  2640. const float lh = h > 0.f ? h : 1.f;
  2641. const float theta = acosf(fabsf(vec[2])) / M_PI;
  2642. float uf = (theta * (vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
  2643. float vf = (theta * (vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
  2644. int ui, vi;
  2645. int u_shift;
  2646. if (vec[2] >= 0.f) {
  2647. u_shift = ceilf(ew);
  2648. } else {
  2649. u_shift = 0;
  2650. uf = ew - uf;
  2651. }
  2652. ui = floorf(uf);
  2653. vi = floorf(vf);
  2654. *du = uf - ui;
  2655. *dv = vf - vi;
  2656. for (int i = 0; i < 4; i++) {
  2657. for (int j = 0; j < 4; j++) {
  2658. us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
  2659. vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
  2660. }
  2661. }
  2662. return 1;
  2663. }
  2664. /**
  2665. * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
  2666. *
  2667. * @param s filter private context
  2668. * @param i horizontal position on frame [0, width)
  2669. * @param j vertical position on frame [0, height)
  2670. * @param width frame width
  2671. * @param height frame height
  2672. * @param vec coordinates on sphere
  2673. */
  2674. static int barrel_to_xyz(const V360Context *s,
  2675. int i, int j, int width, int height,
  2676. float *vec)
  2677. {
  2678. const float scale = 0.99f;
  2679. float l_x, l_y, l_z;
  2680. if (i < 4 * width / 5) {
  2681. const float theta_range = M_PI_4;
  2682. const int ew = 4 * width / 5;
  2683. const int eh = height;
  2684. const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
  2685. const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
  2686. const float sin_phi = sinf(phi);
  2687. const float cos_phi = cosf(phi);
  2688. const float sin_theta = sinf(theta);
  2689. const float cos_theta = cosf(theta);
  2690. l_x = cos_theta * sin_phi;
  2691. l_y = sin_theta;
  2692. l_z = cos_theta * cos_phi;
  2693. } else {
  2694. const int ew = width / 5;
  2695. const int eh = height / 2;
  2696. float uf, vf;
  2697. if (j < eh) { // UP
  2698. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2699. vf = 2.f * (j ) / eh - 1.f;
  2700. uf /= scale;
  2701. vf /= scale;
  2702. l_x = uf;
  2703. l_y = -1.f;
  2704. l_z = vf;
  2705. } else { // DOWN
  2706. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2707. vf = 2.f * (j - eh) / eh - 1.f;
  2708. uf /= scale;
  2709. vf /= scale;
  2710. l_x = uf;
  2711. l_y = 1.f;
  2712. l_z = -vf;
  2713. }
  2714. }
  2715. vec[0] = l_x;
  2716. vec[1] = l_y;
  2717. vec[2] = l_z;
  2718. normalize_vector(vec);
  2719. return 1;
  2720. }
  2721. /**
  2722. * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
  2723. *
  2724. * @param s filter private context
  2725. * @param vec coordinates on sphere
  2726. * @param width frame width
  2727. * @param height frame height
  2728. * @param us horizontal coordinates for interpolation window
  2729. * @param vs vertical coordinates for interpolation window
  2730. * @param du horizontal relative coordinate
  2731. * @param dv vertical relative coordinate
  2732. */
  2733. static int xyz_to_barrel(const V360Context *s,
  2734. const float *vec, int width, int height,
  2735. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2736. {
  2737. const float scale = 0.99f;
  2738. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  2739. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  2740. const float theta_range = M_PI_4;
  2741. int ew, eh;
  2742. int u_shift, v_shift;
  2743. float uf, vf;
  2744. int ui, vi;
  2745. if (theta > -theta_range && theta < theta_range) {
  2746. ew = 4 * width / 5;
  2747. eh = height;
  2748. u_shift = s->ih_flip ? width / 5 : 0;
  2749. v_shift = 0;
  2750. uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
  2751. vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
  2752. } else {
  2753. ew = width / 5;
  2754. eh = height / 2;
  2755. u_shift = s->ih_flip ? 0 : 4 * ew;
  2756. if (theta < 0.f) { // UP
  2757. uf = -vec[0] / vec[1];
  2758. vf = -vec[2] / vec[1];
  2759. v_shift = 0;
  2760. } else { // DOWN
  2761. uf = vec[0] / vec[1];
  2762. vf = -vec[2] / vec[1];
  2763. v_shift = eh;
  2764. }
  2765. uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
  2766. vf *= s->input_mirror_modifier[1];
  2767. uf = 0.5f * ew * (uf * scale + 1.f);
  2768. vf = 0.5f * eh * (vf * scale + 1.f);
  2769. }
  2770. ui = floorf(uf);
  2771. vi = floorf(vf);
  2772. *du = uf - ui;
  2773. *dv = vf - vi;
  2774. for (int i = 0; i < 4; i++) {
  2775. for (int j = 0; j < 4; j++) {
  2776. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  2777. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  2778. }
  2779. }
  2780. return 1;
  2781. }
  2782. /**
  2783. * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
  2784. *
  2785. * @param s filter private context
  2786. * @param vec coordinates on sphere
  2787. * @param width frame width
  2788. * @param height frame height
  2789. * @param us horizontal coordinates for interpolation window
  2790. * @param vs vertical coordinates for interpolation window
  2791. * @param du horizontal relative coordinate
  2792. * @param dv vertical relative coordinate
  2793. */
  2794. static int xyz_to_barrelsplit(const V360Context *s,
  2795. const float *vec, int width, int height,
  2796. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2797. {
  2798. const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
  2799. const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
  2800. const float theta_range = M_PI_4;
  2801. int ew, eh;
  2802. int u_shift, v_shift;
  2803. float uf, vf;
  2804. int ui, vi;
  2805. if (theta >= -theta_range && theta <= theta_range) {
  2806. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
  2807. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
  2808. ew = width / 3 * 2;
  2809. eh = height / 2;
  2810. u_shift = s->ih_flip ? width / 3 : 0;
  2811. v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
  2812. uf = fmodf(phi, M_PI_2) / M_PI_2;
  2813. vf = theta / M_PI_4;
  2814. if (v_shift)
  2815. uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
  2816. uf = (uf * scalew + 1.f) * width / 3.f;
  2817. vf = (vf * scaleh + 1.f) * height / 4.f;
  2818. } else {
  2819. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
  2820. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
  2821. int v_offset = 0;
  2822. ew = width / 3;
  2823. eh = height / 4;
  2824. u_shift = s->ih_flip ? 0 : 2 * ew;
  2825. if (theta <= 0.f && theta >= -M_PI_2 &&
  2826. phi <= M_PI_2 && phi >= -M_PI_2) {
  2827. uf = -vec[0] / vec[1];
  2828. vf = -vec[2] / vec[1];
  2829. v_shift = 0;
  2830. v_offset = -eh;
  2831. } else if (theta >= 0.f && theta <= M_PI_2 &&
  2832. phi <= M_PI_2 && phi >= -M_PI_2) {
  2833. uf = vec[0] / vec[1];
  2834. vf = -vec[2] / vec[1];
  2835. v_shift = height * 0.25f;
  2836. } else if (theta <= 0.f && theta >= -M_PI_2) {
  2837. uf = vec[0] / vec[1];
  2838. vf = vec[2] / vec[1];
  2839. v_shift = height * 0.5f;
  2840. v_offset = -eh;
  2841. } else {
  2842. uf = -vec[0] / vec[1];
  2843. vf = vec[2] / vec[1];
  2844. v_shift = height * 0.75f;
  2845. }
  2846. uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
  2847. vf *= s->input_mirror_modifier[1];
  2848. uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
  2849. vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
  2850. }
  2851. ui = floorf(uf);
  2852. vi = floorf(vf);
  2853. *du = uf - ui;
  2854. *dv = vf - vi;
  2855. for (int i = 0; i < 4; i++) {
  2856. for (int j = 0; j < 4; j++) {
  2857. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  2858. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  2859. }
  2860. }
  2861. return 1;
  2862. }
  2863. /**
  2864. * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
  2865. *
  2866. * @param s filter private context
  2867. * @param i horizontal position on frame [0, width)
  2868. * @param j vertical position on frame [0, height)
  2869. * @param width frame width
  2870. * @param height frame height
  2871. * @param vec coordinates on sphere
  2872. */
  2873. static int barrelsplit_to_xyz(const V360Context *s,
  2874. int i, int j, int width, int height,
  2875. float *vec)
  2876. {
  2877. const float x = (i + 0.5f) / width;
  2878. const float y = (j + 0.5f) / height;
  2879. float l_x, l_y, l_z;
  2880. if (x < 2.f / 3.f) {
  2881. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
  2882. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
  2883. const float back = floorf(y * 2.f);
  2884. const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
  2885. const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
  2886. const float sin_phi = sinf(phi);
  2887. const float cos_phi = cosf(phi);
  2888. const float sin_theta = sinf(theta);
  2889. const float cos_theta = cosf(theta);
  2890. l_x = cos_theta * sin_phi;
  2891. l_y = sin_theta;
  2892. l_z = cos_theta * cos_phi;
  2893. } else {
  2894. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
  2895. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
  2896. const int face = floorf(y * 4.f);
  2897. float uf, vf;
  2898. uf = x * 3.f - 2.f;
  2899. switch (face) {
  2900. case 0:
  2901. vf = y * 2.f;
  2902. uf = 1.f - uf;
  2903. vf = 0.5f - vf;
  2904. l_x = (0.5f - uf) / scalew;
  2905. l_y = -0.5f;
  2906. l_z = (0.5f - vf) / scaleh;
  2907. break;
  2908. case 1:
  2909. vf = y * 2.f;
  2910. uf = 1.f - uf;
  2911. vf = 1.f - (vf - 0.5f);
  2912. l_x = (0.5f - uf) / scalew;
  2913. l_y = 0.5f;
  2914. l_z = (-0.5f + vf) / scaleh;
  2915. break;
  2916. case 2:
  2917. vf = y * 2.f - 0.5f;
  2918. vf = 1.f - (1.f - vf);
  2919. l_x = (0.5f - uf) / scalew;
  2920. l_y = -0.5f;
  2921. l_z = (0.5f - vf) / scaleh;
  2922. break;
  2923. case 3:
  2924. vf = y * 2.f - 1.5f;
  2925. l_x = (0.5f - uf) / scalew;
  2926. l_y = 0.5f;
  2927. l_z = (-0.5f + vf) / scaleh;
  2928. break;
  2929. }
  2930. }
  2931. vec[0] = l_x;
  2932. vec[1] = l_y;
  2933. vec[2] = l_z;
  2934. normalize_vector(vec);
  2935. return 1;
  2936. }
  2937. /**
  2938. * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
  2939. *
  2940. * @param s filter private context
  2941. * @param i horizontal position on frame [0, width)
  2942. * @param j vertical position on frame [0, height)
  2943. * @param width frame width
  2944. * @param height frame height
  2945. * @param vec coordinates on sphere
  2946. */
  2947. static int tspyramid_to_xyz(const V360Context *s,
  2948. int i, int j, int width, int height,
  2949. float *vec)
  2950. {
  2951. const float x = (i + 0.5f) / width;
  2952. const float y = (j + 0.5f) / height;
  2953. if (x < 0.5f) {
  2954. vec[0] = x * 4.f - 1.f;
  2955. vec[1] = (y * 2.f - 1.f);
  2956. vec[2] = 1.f;
  2957. } else if (x >= 0.6875f && x < 0.8125f &&
  2958. y >= 0.375f && y < 0.625f) {
  2959. vec[0] = -(x - 0.6875f) * 16.f + 1.f;
  2960. vec[1] = (y - 0.375f) * 8.f - 1.f;
  2961. vec[2] = -1.f;
  2962. } else if (0.5f <= x && x < 0.6875f &&
  2963. ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
  2964. (0.375f <= y && y < 0.625f) ||
  2965. (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
  2966. vec[0] = 1.f;
  2967. vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
  2968. vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
  2969. } else if (0.8125f <= x && x < 1.f &&
  2970. ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
  2971. (0.375f <= y && y < 0.625f) ||
  2972. (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
  2973. vec[0] = -1.f;
  2974. vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
  2975. vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
  2976. } else if (0.f <= y && y < 0.375f &&
  2977. ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
  2978. (0.6875f <= x && x < 0.8125f) ||
  2979. (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
  2980. vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
  2981. vec[1] = -1.f;
  2982. vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
  2983. } else {
  2984. vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
  2985. vec[1] = 1.f;
  2986. vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
  2987. }
  2988. normalize_vector(vec);
  2989. return 1;
  2990. }
  2991. /**
  2992. * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
  2993. *
  2994. * @param s filter private context
  2995. * @param vec coordinates on sphere
  2996. * @param width frame width
  2997. * @param height frame height
  2998. * @param us horizontal coordinates for interpolation window
  2999. * @param vs vertical coordinates for interpolation window
  3000. * @param du horizontal relative coordinate
  3001. * @param dv vertical relative coordinate
  3002. */
  3003. static int xyz_to_tspyramid(const V360Context *s,
  3004. const float *vec, int width, int height,
  3005. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3006. {
  3007. float uf, vf;
  3008. int ui, vi;
  3009. int face;
  3010. xyz_to_cube(s, vec, &uf, &vf, &face);
  3011. uf = (uf + 1.f) * 0.5f;
  3012. vf = (vf + 1.f) * 0.5f;
  3013. switch (face) {
  3014. case UP:
  3015. uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
  3016. vf = 0.375f - 0.375f * vf;
  3017. break;
  3018. case FRONT:
  3019. uf = 0.5f * uf;
  3020. break;
  3021. case DOWN:
  3022. uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
  3023. vf = 1.f - 0.375f * vf;
  3024. break;
  3025. case LEFT:
  3026. vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
  3027. uf = 0.1875f * uf + 0.8125f;
  3028. break;
  3029. case RIGHT:
  3030. vf = 0.375f * uf - 0.75f * uf * vf + vf;
  3031. uf = 0.1875f * uf + 0.5f;
  3032. break;
  3033. case BACK:
  3034. uf = 0.125f * uf + 0.6875f;
  3035. vf = 0.25f * vf + 0.375f;
  3036. break;
  3037. }
  3038. uf *= width;
  3039. vf *= height;
  3040. ui = floorf(uf);
  3041. vi = floorf(vf);
  3042. *du = uf - ui;
  3043. *dv = vf - vi;
  3044. for (int i = 0; i < 4; i++) {
  3045. for (int j = 0; j < 4; j++) {
  3046. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  3047. vs[i][j] = reflecty(vi + i - 1, height);
  3048. }
  3049. }
  3050. return 1;
  3051. }
  3052. static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
  3053. {
  3054. for (int i = 0; i < 3; i++) {
  3055. for (int j = 0; j < 3; j++) {
  3056. float sum = 0.f;
  3057. for (int k = 0; k < 3; k++)
  3058. sum += a[i][k] * b[k][j];
  3059. c[i][j] = sum;
  3060. }
  3061. }
  3062. }
  3063. /**
  3064. * Calculate rotation matrix for yaw/pitch/roll angles.
  3065. */
  3066. static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
  3067. float rot_mat[3][3],
  3068. const int rotation_order[3])
  3069. {
  3070. const float yaw_rad = yaw * M_PI / 180.f;
  3071. const float pitch_rad = pitch * M_PI / 180.f;
  3072. const float roll_rad = roll * M_PI / 180.f;
  3073. const float sin_yaw = sinf(yaw_rad);
  3074. const float cos_yaw = cosf(yaw_rad);
  3075. const float sin_pitch = sinf(pitch_rad);
  3076. const float cos_pitch = cosf(pitch_rad);
  3077. const float sin_roll = sinf(roll_rad);
  3078. const float cos_roll = cosf(roll_rad);
  3079. float m[3][3][3];
  3080. float temp[3][3];
  3081. m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
  3082. m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
  3083. m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
  3084. m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
  3085. m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
  3086. m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
  3087. m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
  3088. m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
  3089. m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
  3090. multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
  3091. multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
  3092. }
  3093. /**
  3094. * Rotate vector with given rotation matrix.
  3095. *
  3096. * @param rot_mat rotation matrix
  3097. * @param vec vector
  3098. */
  3099. static inline void rotate(const float rot_mat[3][3],
  3100. float *vec)
  3101. {
  3102. const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
  3103. const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
  3104. const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
  3105. vec[0] = x_tmp;
  3106. vec[1] = y_tmp;
  3107. vec[2] = z_tmp;
  3108. }
  3109. static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
  3110. float *modifier)
  3111. {
  3112. modifier[0] = h_flip ? -1.f : 1.f;
  3113. modifier[1] = v_flip ? -1.f : 1.f;
  3114. modifier[2] = d_flip ? -1.f : 1.f;
  3115. }
  3116. static inline void mirror(const float *modifier, float *vec)
  3117. {
  3118. vec[0] *= modifier[0];
  3119. vec[1] *= modifier[1];
  3120. vec[2] *= modifier[2];
  3121. }
  3122. static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
  3123. {
  3124. if (!s->u[p])
  3125. s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
  3126. if (!s->v[p])
  3127. s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
  3128. if (!s->u[p] || !s->v[p])
  3129. return AVERROR(ENOMEM);
  3130. if (sizeof_ker) {
  3131. if (!s->ker[p])
  3132. s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
  3133. if (!s->ker[p])
  3134. return AVERROR(ENOMEM);
  3135. }
  3136. if (sizeof_mask && !p) {
  3137. if (!s->mask)
  3138. s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
  3139. if (!s->mask)
  3140. return AVERROR(ENOMEM);
  3141. }
  3142. return 0;
  3143. }
  3144. static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
  3145. {
  3146. switch (format) {
  3147. case STEREOGRAPHIC:
  3148. {
  3149. const float d = 0.5f * hypotf(w, h);
  3150. const float l = d / (tanf(d_fov * M_PI / 720.f));
  3151. *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
  3152. *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
  3153. }
  3154. break;
  3155. case FISHEYE:
  3156. {
  3157. const float d = 0.5f * hypotf(w, h);
  3158. *h_fov = d / w * d_fov;
  3159. *v_fov = d / h * d_fov;
  3160. }
  3161. break;
  3162. case FLAT:
  3163. default:
  3164. {
  3165. const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
  3166. const float d = hypotf(w, h);
  3167. *h_fov = atan2f(da * w, d) * 360.f / M_PI;
  3168. *v_fov = atan2f(da * h, d) * 360.f / M_PI;
  3169. if (*h_fov < 0.f)
  3170. *h_fov += 360.f;
  3171. if (*v_fov < 0.f)
  3172. *v_fov += 360.f;
  3173. }
  3174. break;
  3175. }
  3176. }
  3177. static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
  3178. {
  3179. outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
  3180. outw[0] = outw[3] = w;
  3181. outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  3182. outh[0] = outh[3] = h;
  3183. }
  3184. // Calculate remap data
  3185. static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  3186. {
  3187. V360Context *s = ctx->priv;
  3188. for (int p = 0; p < s->nb_allocated; p++) {
  3189. const int max_value = s->max_value;
  3190. const int width = s->pr_width[p];
  3191. const int uv_linesize = s->uv_linesize[p];
  3192. const int height = s->pr_height[p];
  3193. const int in_width = s->inplanewidth[p];
  3194. const int in_height = s->inplaneheight[p];
  3195. const int slice_start = (height * jobnr ) / nb_jobs;
  3196. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  3197. float du, dv;
  3198. float vec[3];
  3199. XYRemap rmap;
  3200. for (int j = slice_start; j < slice_end; j++) {
  3201. for (int i = 0; i < width; i++) {
  3202. int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
  3203. int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
  3204. int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
  3205. uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
  3206. uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
  3207. int in_mask, out_mask;
  3208. if (s->out_transpose)
  3209. out_mask = s->out_transform(s, j, i, height, width, vec);
  3210. else
  3211. out_mask = s->out_transform(s, i, j, width, height, vec);
  3212. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3213. rotate(s->rot_mat, vec);
  3214. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3215. normalize_vector(vec);
  3216. mirror(s->output_mirror_modifier, vec);
  3217. if (s->in_transpose)
  3218. in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
  3219. else
  3220. in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
  3221. av_assert1(!isnan(du) && !isnan(dv));
  3222. s->calculate_kernel(du, dv, &rmap, u, v, ker);
  3223. if (!p && s->mask) {
  3224. if (s->mask_size == 1) {
  3225. mask8[0] = 255 * (out_mask & in_mask);
  3226. } else {
  3227. mask16[0] = max_value * (out_mask & in_mask);
  3228. }
  3229. }
  3230. }
  3231. }
  3232. }
  3233. return 0;
  3234. }
  3235. static int config_output(AVFilterLink *outlink)
  3236. {
  3237. AVFilterContext *ctx = outlink->src;
  3238. AVFilterLink *inlink = ctx->inputs[0];
  3239. V360Context *s = ctx->priv;
  3240. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  3241. const int depth = desc->comp[0].depth;
  3242. const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
  3243. int sizeof_uv;
  3244. int sizeof_ker;
  3245. int err;
  3246. int h, w;
  3247. int in_offset_h, in_offset_w;
  3248. int out_offset_h, out_offset_w;
  3249. float hf, wf;
  3250. int (*prepare_out)(AVFilterContext *ctx);
  3251. int have_alpha;
  3252. s->max_value = (1 << depth) - 1;
  3253. s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
  3254. s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
  3255. switch (s->interp) {
  3256. case NEAREST:
  3257. s->calculate_kernel = nearest_kernel;
  3258. s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
  3259. s->elements = 1;
  3260. sizeof_uv = sizeof(int16_t) * s->elements;
  3261. sizeof_ker = 0;
  3262. break;
  3263. case BILINEAR:
  3264. s->calculate_kernel = bilinear_kernel;
  3265. s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
  3266. s->elements = 2 * 2;
  3267. sizeof_uv = sizeof(int16_t) * s->elements;
  3268. sizeof_ker = sizeof(int16_t) * s->elements;
  3269. break;
  3270. case LAGRANGE9:
  3271. s->calculate_kernel = lagrange_kernel;
  3272. s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
  3273. s->elements = 3 * 3;
  3274. sizeof_uv = sizeof(int16_t) * s->elements;
  3275. sizeof_ker = sizeof(int16_t) * s->elements;
  3276. break;
  3277. case BICUBIC:
  3278. s->calculate_kernel = bicubic_kernel;
  3279. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3280. s->elements = 4 * 4;
  3281. sizeof_uv = sizeof(int16_t) * s->elements;
  3282. sizeof_ker = sizeof(int16_t) * s->elements;
  3283. break;
  3284. case LANCZOS:
  3285. s->calculate_kernel = lanczos_kernel;
  3286. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3287. s->elements = 4 * 4;
  3288. sizeof_uv = sizeof(int16_t) * s->elements;
  3289. sizeof_ker = sizeof(int16_t) * s->elements;
  3290. break;
  3291. case SPLINE16:
  3292. s->calculate_kernel = spline16_kernel;
  3293. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3294. s->elements = 4 * 4;
  3295. sizeof_uv = sizeof(int16_t) * s->elements;
  3296. sizeof_ker = sizeof(int16_t) * s->elements;
  3297. break;
  3298. case GAUSSIAN:
  3299. s->calculate_kernel = gaussian_kernel;
  3300. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3301. s->elements = 4 * 4;
  3302. sizeof_uv = sizeof(int16_t) * s->elements;
  3303. sizeof_ker = sizeof(int16_t) * s->elements;
  3304. break;
  3305. default:
  3306. av_assert0(0);
  3307. }
  3308. ff_v360_init(s, depth);
  3309. for (int order = 0; order < NB_RORDERS; order++) {
  3310. const char c = s->rorder[order];
  3311. int rorder;
  3312. if (c == '\0') {
  3313. av_log(ctx, AV_LOG_WARNING,
  3314. "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
  3315. s->rotation_order[0] = YAW;
  3316. s->rotation_order[1] = PITCH;
  3317. s->rotation_order[2] = ROLL;
  3318. break;
  3319. }
  3320. rorder = get_rorder(c);
  3321. if (rorder == -1) {
  3322. av_log(ctx, AV_LOG_WARNING,
  3323. "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
  3324. s->rotation_order[0] = YAW;
  3325. s->rotation_order[1] = PITCH;
  3326. s->rotation_order[2] = ROLL;
  3327. break;
  3328. }
  3329. s->rotation_order[order] = rorder;
  3330. }
  3331. switch (s->in_stereo) {
  3332. case STEREO_2D:
  3333. w = inlink->w;
  3334. h = inlink->h;
  3335. in_offset_w = in_offset_h = 0;
  3336. break;
  3337. case STEREO_SBS:
  3338. w = inlink->w / 2;
  3339. h = inlink->h;
  3340. in_offset_w = w;
  3341. in_offset_h = 0;
  3342. break;
  3343. case STEREO_TB:
  3344. w = inlink->w;
  3345. h = inlink->h / 2;
  3346. in_offset_w = 0;
  3347. in_offset_h = h;
  3348. break;
  3349. default:
  3350. av_assert0(0);
  3351. }
  3352. set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
  3353. set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
  3354. s->in_width = s->inplanewidth[0];
  3355. s->in_height = s->inplaneheight[0];
  3356. if (s->id_fov > 0.f)
  3357. fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
  3358. if (s->in_transpose)
  3359. FFSWAP(int, s->in_width, s->in_height);
  3360. switch (s->in) {
  3361. case EQUIRECTANGULAR:
  3362. s->in_transform = xyz_to_equirect;
  3363. err = 0;
  3364. wf = w;
  3365. hf = h;
  3366. break;
  3367. case CUBEMAP_3_2:
  3368. s->in_transform = xyz_to_cube3x2;
  3369. err = prepare_cube_in(ctx);
  3370. wf = w / 3.f * 4.f;
  3371. hf = h;
  3372. break;
  3373. case CUBEMAP_1_6:
  3374. s->in_transform = xyz_to_cube1x6;
  3375. err = prepare_cube_in(ctx);
  3376. wf = w * 4.f;
  3377. hf = h / 3.f;
  3378. break;
  3379. case CUBEMAP_6_1:
  3380. s->in_transform = xyz_to_cube6x1;
  3381. err = prepare_cube_in(ctx);
  3382. wf = w / 3.f * 2.f;
  3383. hf = h * 2.f;
  3384. break;
  3385. case EQUIANGULAR:
  3386. s->in_transform = xyz_to_eac;
  3387. err = prepare_eac_in(ctx);
  3388. wf = w;
  3389. hf = h / 9.f * 8.f;
  3390. break;
  3391. case FLAT:
  3392. s->in_transform = xyz_to_flat;
  3393. err = prepare_flat_in(ctx);
  3394. wf = w;
  3395. hf = h;
  3396. break;
  3397. case PERSPECTIVE:
  3398. av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
  3399. return AVERROR(EINVAL);
  3400. case DUAL_FISHEYE:
  3401. s->in_transform = xyz_to_dfisheye;
  3402. err = 0;
  3403. wf = w;
  3404. hf = h;
  3405. break;
  3406. case BARREL:
  3407. s->in_transform = xyz_to_barrel;
  3408. err = 0;
  3409. wf = w / 5.f * 4.f;
  3410. hf = h;
  3411. break;
  3412. case STEREOGRAPHIC:
  3413. s->in_transform = xyz_to_stereographic;
  3414. err = prepare_stereographic_in(ctx);
  3415. wf = w;
  3416. hf = h / 2.f;
  3417. break;
  3418. case MERCATOR:
  3419. s->in_transform = xyz_to_mercator;
  3420. err = 0;
  3421. wf = w;
  3422. hf = h / 2.f;
  3423. break;
  3424. case BALL:
  3425. s->in_transform = xyz_to_ball;
  3426. err = 0;
  3427. wf = w;
  3428. hf = h / 2.f;
  3429. break;
  3430. case HAMMER:
  3431. s->in_transform = xyz_to_hammer;
  3432. err = 0;
  3433. wf = w;
  3434. hf = h;
  3435. break;
  3436. case SINUSOIDAL:
  3437. s->in_transform = xyz_to_sinusoidal;
  3438. err = 0;
  3439. wf = w;
  3440. hf = h;
  3441. break;
  3442. case FISHEYE:
  3443. s->in_transform = xyz_to_fisheye;
  3444. err = prepare_fisheye_in(ctx);
  3445. wf = w * 2;
  3446. hf = h;
  3447. break;
  3448. case PANNINI:
  3449. s->in_transform = xyz_to_pannini;
  3450. err = 0;
  3451. wf = w;
  3452. hf = h;
  3453. break;
  3454. case CYLINDRICAL:
  3455. s->in_transform = xyz_to_cylindrical;
  3456. err = prepare_cylindrical_in(ctx);
  3457. wf = w;
  3458. hf = h * 2.f;
  3459. break;
  3460. case TETRAHEDRON:
  3461. s->in_transform = xyz_to_tetrahedron;
  3462. err = 0;
  3463. wf = w;
  3464. hf = h;
  3465. break;
  3466. case BARREL_SPLIT:
  3467. s->in_transform = xyz_to_barrelsplit;
  3468. err = 0;
  3469. wf = w * 4.f / 3.f;
  3470. hf = h;
  3471. break;
  3472. case TSPYRAMID:
  3473. s->in_transform = xyz_to_tspyramid;
  3474. err = 0;
  3475. wf = w;
  3476. hf = h;
  3477. break;
  3478. case HEQUIRECTANGULAR:
  3479. s->in_transform = xyz_to_hequirect;
  3480. err = 0;
  3481. wf = w * 2.f;
  3482. hf = h;
  3483. break;
  3484. default:
  3485. av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
  3486. return AVERROR_BUG;
  3487. }
  3488. if (err != 0) {
  3489. return err;
  3490. }
  3491. switch (s->out) {
  3492. case EQUIRECTANGULAR:
  3493. s->out_transform = equirect_to_xyz;
  3494. prepare_out = NULL;
  3495. w = lrintf(wf);
  3496. h = lrintf(hf);
  3497. break;
  3498. case CUBEMAP_3_2:
  3499. s->out_transform = cube3x2_to_xyz;
  3500. prepare_out = prepare_cube_out;
  3501. w = lrintf(wf / 4.f * 3.f);
  3502. h = lrintf(hf);
  3503. break;
  3504. case CUBEMAP_1_6:
  3505. s->out_transform = cube1x6_to_xyz;
  3506. prepare_out = prepare_cube_out;
  3507. w = lrintf(wf / 4.f);
  3508. h = lrintf(hf * 3.f);
  3509. break;
  3510. case CUBEMAP_6_1:
  3511. s->out_transform = cube6x1_to_xyz;
  3512. prepare_out = prepare_cube_out;
  3513. w = lrintf(wf / 2.f * 3.f);
  3514. h = lrintf(hf / 2.f);
  3515. break;
  3516. case EQUIANGULAR:
  3517. s->out_transform = eac_to_xyz;
  3518. prepare_out = prepare_eac_out;
  3519. w = lrintf(wf);
  3520. h = lrintf(hf / 8.f * 9.f);
  3521. break;
  3522. case FLAT:
  3523. s->out_transform = flat_to_xyz;
  3524. prepare_out = prepare_flat_out;
  3525. w = lrintf(wf);
  3526. h = lrintf(hf);
  3527. break;
  3528. case DUAL_FISHEYE:
  3529. s->out_transform = dfisheye_to_xyz;
  3530. prepare_out = NULL;
  3531. w = lrintf(wf);
  3532. h = lrintf(hf);
  3533. break;
  3534. case BARREL:
  3535. s->out_transform = barrel_to_xyz;
  3536. prepare_out = NULL;
  3537. w = lrintf(wf / 4.f * 5.f);
  3538. h = lrintf(hf);
  3539. break;
  3540. case STEREOGRAPHIC:
  3541. s->out_transform = stereographic_to_xyz;
  3542. prepare_out = prepare_stereographic_out;
  3543. w = lrintf(wf);
  3544. h = lrintf(hf * 2.f);
  3545. break;
  3546. case MERCATOR:
  3547. s->out_transform = mercator_to_xyz;
  3548. prepare_out = NULL;
  3549. w = lrintf(wf);
  3550. h = lrintf(hf * 2.f);
  3551. break;
  3552. case BALL:
  3553. s->out_transform = ball_to_xyz;
  3554. prepare_out = NULL;
  3555. w = lrintf(wf);
  3556. h = lrintf(hf * 2.f);
  3557. break;
  3558. case HAMMER:
  3559. s->out_transform = hammer_to_xyz;
  3560. prepare_out = NULL;
  3561. w = lrintf(wf);
  3562. h = lrintf(hf);
  3563. break;
  3564. case SINUSOIDAL:
  3565. s->out_transform = sinusoidal_to_xyz;
  3566. prepare_out = NULL;
  3567. w = lrintf(wf);
  3568. h = lrintf(hf);
  3569. break;
  3570. case FISHEYE:
  3571. s->out_transform = fisheye_to_xyz;
  3572. prepare_out = prepare_fisheye_out;
  3573. w = lrintf(wf * 0.5f);
  3574. h = lrintf(hf);
  3575. break;
  3576. case PANNINI:
  3577. s->out_transform = pannini_to_xyz;
  3578. prepare_out = NULL;
  3579. w = lrintf(wf);
  3580. h = lrintf(hf);
  3581. break;
  3582. case CYLINDRICAL:
  3583. s->out_transform = cylindrical_to_xyz;
  3584. prepare_out = prepare_cylindrical_out;
  3585. w = lrintf(wf);
  3586. h = lrintf(hf * 0.5f);
  3587. break;
  3588. case PERSPECTIVE:
  3589. s->out_transform = perspective_to_xyz;
  3590. prepare_out = NULL;
  3591. w = lrintf(wf / 2.f);
  3592. h = lrintf(hf);
  3593. break;
  3594. case TETRAHEDRON:
  3595. s->out_transform = tetrahedron_to_xyz;
  3596. prepare_out = NULL;
  3597. w = lrintf(wf);
  3598. h = lrintf(hf);
  3599. break;
  3600. case BARREL_SPLIT:
  3601. s->out_transform = barrelsplit_to_xyz;
  3602. prepare_out = NULL;
  3603. w = lrintf(wf / 4.f * 3.f);
  3604. h = lrintf(hf);
  3605. break;
  3606. case TSPYRAMID:
  3607. s->out_transform = tspyramid_to_xyz;
  3608. prepare_out = NULL;
  3609. w = lrintf(wf);
  3610. h = lrintf(hf);
  3611. break;
  3612. case HEQUIRECTANGULAR:
  3613. s->out_transform = hequirect_to_xyz;
  3614. prepare_out = NULL;
  3615. w = lrintf(wf / 2.f);
  3616. h = lrintf(hf);
  3617. break;
  3618. default:
  3619. av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
  3620. return AVERROR_BUG;
  3621. }
  3622. // Override resolution with user values if specified
  3623. if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  3624. s->out == FLAT && s->d_fov == 0.f) {
  3625. w = s->width;
  3626. h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
  3627. } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  3628. s->out == FLAT && s->d_fov == 0.f) {
  3629. h = s->height;
  3630. w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
  3631. } else if (s->width > 0 && s->height > 0) {
  3632. w = s->width;
  3633. h = s->height;
  3634. } else if (s->width > 0 || s->height > 0) {
  3635. av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
  3636. return AVERROR(EINVAL);
  3637. } else {
  3638. if (s->out_transpose)
  3639. FFSWAP(int, w, h);
  3640. if (s->in_transpose)
  3641. FFSWAP(int, w, h);
  3642. }
  3643. s->width = w;
  3644. s->height = h;
  3645. if (s->d_fov > 0.f)
  3646. fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
  3647. if (prepare_out) {
  3648. err = prepare_out(ctx);
  3649. if (err != 0)
  3650. return err;
  3651. }
  3652. set_dimensions(s->pr_width, s->pr_height, w, h, desc);
  3653. switch (s->out_stereo) {
  3654. case STEREO_2D:
  3655. out_offset_w = out_offset_h = 0;
  3656. break;
  3657. case STEREO_SBS:
  3658. out_offset_w = w;
  3659. out_offset_h = 0;
  3660. w *= 2;
  3661. break;
  3662. case STEREO_TB:
  3663. out_offset_w = 0;
  3664. out_offset_h = h;
  3665. h *= 2;
  3666. break;
  3667. default:
  3668. av_assert0(0);
  3669. }
  3670. set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
  3671. set_dimensions(s->planewidth, s->planeheight, w, h, desc);
  3672. for (int i = 0; i < 4; i++)
  3673. s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
  3674. outlink->h = h;
  3675. outlink->w = w;
  3676. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  3677. have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  3678. if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
  3679. s->nb_allocated = 1;
  3680. s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
  3681. } else {
  3682. s->nb_allocated = 2;
  3683. s->map[0] = s->map[3] = 0;
  3684. s->map[1] = s->map[2] = 1;
  3685. }
  3686. for (int i = 0; i < s->nb_allocated; i++)
  3687. allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
  3688. calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
  3689. set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
  3690. ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  3691. return 0;
  3692. }
  3693. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  3694. {
  3695. AVFilterContext *ctx = inlink->dst;
  3696. AVFilterLink *outlink = ctx->outputs[0];
  3697. V360Context *s = ctx->priv;
  3698. AVFrame *out;
  3699. ThreadData td;
  3700. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  3701. if (!out) {
  3702. av_frame_free(&in);
  3703. return AVERROR(ENOMEM);
  3704. }
  3705. av_frame_copy_props(out, in);
  3706. td.in = in;
  3707. td.out = out;
  3708. ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  3709. av_frame_free(&in);
  3710. return ff_filter_frame(outlink, out);
  3711. }
  3712. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  3713. char *res, int res_len, int flags)
  3714. {
  3715. int ret;
  3716. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  3717. if (ret < 0)
  3718. return ret;
  3719. return config_output(ctx->outputs[0]);
  3720. }
  3721. static av_cold void uninit(AVFilterContext *ctx)
  3722. {
  3723. V360Context *s = ctx->priv;
  3724. for (int p = 0; p < s->nb_allocated; p++) {
  3725. av_freep(&s->u[p]);
  3726. av_freep(&s->v[p]);
  3727. av_freep(&s->ker[p]);
  3728. }
  3729. av_freep(&s->mask);
  3730. }
  3731. static const AVFilterPad inputs[] = {
  3732. {
  3733. .name = "default",
  3734. .type = AVMEDIA_TYPE_VIDEO,
  3735. .filter_frame = filter_frame,
  3736. },
  3737. { NULL }
  3738. };
  3739. static const AVFilterPad outputs[] = {
  3740. {
  3741. .name = "default",
  3742. .type = AVMEDIA_TYPE_VIDEO,
  3743. .config_props = config_output,
  3744. },
  3745. { NULL }
  3746. };
  3747. AVFilter ff_vf_v360 = {
  3748. .name = "v360",
  3749. .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
  3750. .priv_size = sizeof(V360Context),
  3751. .uninit = uninit,
  3752. .query_formats = query_formats,
  3753. .inputs = inputs,
  3754. .outputs = outputs,
  3755. .priv_class = &v360_class,
  3756. .flags = AVFILTER_FLAG_SLICE_THREADS,
  3757. .process_command = process_command,
  3758. };