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.

4781 lines
152KB

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