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.

4873 lines
154KB

  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=0.f}, 0.f, 360.f,TFLAGS, "h_fov"},
  146. { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 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=0.f}, 0.f, 360.f,TFLAGS, "ih_fov"},
  156. { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 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. * Prepare data for processing equirectangular output format.
  1544. *
  1545. * @param ctx filter context
  1546. *
  1547. * @return error code
  1548. */
  1549. static int prepare_equirect_out(AVFilterContext *ctx)
  1550. {
  1551. V360Context *s = ctx->priv;
  1552. s->flat_range[0] = s->h_fov * M_PI / 360.f;
  1553. s->flat_range[1] = s->v_fov * M_PI / 360.f;
  1554. return 0;
  1555. }
  1556. /**
  1557. * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
  1558. *
  1559. * @param s filter private context
  1560. * @param i horizontal position on frame [0, width)
  1561. * @param j vertical position on frame [0, height)
  1562. * @param width frame width
  1563. * @param height frame height
  1564. * @param vec coordinates on sphere
  1565. */
  1566. static int equirect_to_xyz(const V360Context *s,
  1567. int i, int j, int width, int height,
  1568. float *vec)
  1569. {
  1570. const float phi = ((2.f * i + 0.5f) / width - 1.f) * s->flat_range[0];
  1571. const float theta = ((2.f * j + 0.5f) / height - 1.f) * s->flat_range[1];
  1572. const float sin_phi = sinf(phi);
  1573. const float cos_phi = cosf(phi);
  1574. const float sin_theta = sinf(theta);
  1575. const float cos_theta = cosf(theta);
  1576. vec[0] = cos_theta * sin_phi;
  1577. vec[1] = sin_theta;
  1578. vec[2] = cos_theta * cos_phi;
  1579. return 1;
  1580. }
  1581. /**
  1582. * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
  1583. *
  1584. * @param s filter private context
  1585. * @param i horizontal position on frame [0, width)
  1586. * @param j vertical position on frame [0, height)
  1587. * @param width frame width
  1588. * @param height frame height
  1589. * @param vec coordinates on sphere
  1590. */
  1591. static int hequirect_to_xyz(const V360Context *s,
  1592. int i, int j, int width, int height,
  1593. float *vec)
  1594. {
  1595. const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
  1596. const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
  1597. const float sin_phi = sinf(phi);
  1598. const float cos_phi = cosf(phi);
  1599. const float sin_theta = sinf(theta);
  1600. const float cos_theta = cosf(theta);
  1601. vec[0] = cos_theta * sin_phi;
  1602. vec[1] = sin_theta;
  1603. vec[2] = cos_theta * cos_phi;
  1604. return 1;
  1605. }
  1606. /**
  1607. * Prepare data for processing stereographic output format.
  1608. *
  1609. * @param ctx filter context
  1610. *
  1611. * @return error code
  1612. */
  1613. static int prepare_stereographic_out(AVFilterContext *ctx)
  1614. {
  1615. V360Context *s = ctx->priv;
  1616. s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
  1617. s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
  1618. return 0;
  1619. }
  1620. /**
  1621. * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
  1622. *
  1623. * @param s filter private context
  1624. * @param i horizontal position on frame [0, width)
  1625. * @param j vertical position on frame [0, height)
  1626. * @param width frame width
  1627. * @param height frame height
  1628. * @param vec coordinates on sphere
  1629. */
  1630. static int stereographic_to_xyz(const V360Context *s,
  1631. int i, int j, int width, int height,
  1632. float *vec)
  1633. {
  1634. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1635. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1636. const float r = hypotf(x, y);
  1637. const float theta = atanf(r) * 2.f;
  1638. const float sin_theta = sinf(theta);
  1639. vec[0] = x / r * sin_theta;
  1640. vec[1] = y / r * sin_theta;
  1641. vec[2] = cosf(theta);
  1642. normalize_vector(vec);
  1643. return 1;
  1644. }
  1645. /**
  1646. * Prepare data for processing stereographic input format.
  1647. *
  1648. * @param ctx filter context
  1649. *
  1650. * @return error code
  1651. */
  1652. static int prepare_stereographic_in(AVFilterContext *ctx)
  1653. {
  1654. V360Context *s = ctx->priv;
  1655. s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
  1656. s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
  1657. return 0;
  1658. }
  1659. /**
  1660. * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
  1661. *
  1662. * @param s filter private context
  1663. * @param vec coordinates on sphere
  1664. * @param width frame width
  1665. * @param height frame height
  1666. * @param us horizontal coordinates for interpolation window
  1667. * @param vs vertical coordinates for interpolation window
  1668. * @param du horizontal relative coordinate
  1669. * @param dv vertical relative coordinate
  1670. */
  1671. static int xyz_to_stereographic(const V360Context *s,
  1672. const float *vec, int width, int height,
  1673. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1674. {
  1675. const float theta = acosf(vec[2]);
  1676. const float r = tanf(theta * 0.5f);
  1677. const float c = r / hypotf(vec[0], vec[1]);
  1678. const float x = vec[0] * c / s->iflat_range[0];
  1679. const float y = vec[1] * c / s->iflat_range[1];
  1680. const float uf = (x + 1.f) * width / 2.f;
  1681. const float vf = (y + 1.f) * height / 2.f;
  1682. const int ui = floorf(uf);
  1683. const int vi = floorf(vf);
  1684. const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1685. *du = visible ? uf - ui : 0.f;
  1686. *dv = visible ? vf - vi : 0.f;
  1687. for (int i = 0; i < 4; i++) {
  1688. for (int j = 0; j < 4; j++) {
  1689. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1690. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1691. }
  1692. }
  1693. return visible;
  1694. }
  1695. /**
  1696. * Prepare data for processing equisolid output format.
  1697. *
  1698. * @param ctx filter context
  1699. *
  1700. * @return error code
  1701. */
  1702. static int prepare_equisolid_out(AVFilterContext *ctx)
  1703. {
  1704. V360Context *s = ctx->priv;
  1705. s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
  1706. s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
  1707. return 0;
  1708. }
  1709. /**
  1710. * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
  1711. *
  1712. * @param s filter private context
  1713. * @param i horizontal position on frame [0, width)
  1714. * @param j vertical position on frame [0, height)
  1715. * @param width frame width
  1716. * @param height frame height
  1717. * @param vec coordinates on sphere
  1718. */
  1719. static int equisolid_to_xyz(const V360Context *s,
  1720. int i, int j, int width, int height,
  1721. float *vec)
  1722. {
  1723. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1724. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1725. const float r = hypotf(x, y);
  1726. const float theta = asinf(r) * 2.f;
  1727. const float sin_theta = sinf(theta);
  1728. vec[0] = x / r * sin_theta;
  1729. vec[1] = y / r * sin_theta;
  1730. vec[2] = cosf(theta);
  1731. normalize_vector(vec);
  1732. return 1;
  1733. }
  1734. /**
  1735. * Prepare data for processing equisolid input format.
  1736. *
  1737. * @param ctx filter context
  1738. *
  1739. * @return error code
  1740. */
  1741. static int prepare_equisolid_in(AVFilterContext *ctx)
  1742. {
  1743. V360Context *s = ctx->priv;
  1744. s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
  1745. s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
  1746. return 0;
  1747. }
  1748. /**
  1749. * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
  1750. *
  1751. * @param s filter private context
  1752. * @param vec coordinates on sphere
  1753. * @param width frame width
  1754. * @param height frame height
  1755. * @param us horizontal coordinates for interpolation window
  1756. * @param vs vertical coordinates for interpolation window
  1757. * @param du horizontal relative coordinate
  1758. * @param dv vertical relative coordinate
  1759. */
  1760. static int xyz_to_equisolid(const V360Context *s,
  1761. const float *vec, int width, int height,
  1762. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1763. {
  1764. const float theta = acosf(vec[2]);
  1765. const float r = sinf(theta * 0.5f);
  1766. const float c = r / hypotf(vec[0], vec[1]);
  1767. const float x = vec[0] * c / s->iflat_range[0];
  1768. const float y = vec[1] * c / s->iflat_range[1];
  1769. const float uf = (x + 1.f) * width / 2.f;
  1770. const float vf = (y + 1.f) * height / 2.f;
  1771. const int ui = floorf(uf);
  1772. const int vi = floorf(vf);
  1773. const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1774. *du = visible ? uf - ui : 0.f;
  1775. *dv = visible ? vf - vi : 0.f;
  1776. for (int i = 0; i < 4; i++) {
  1777. for (int j = 0; j < 4; j++) {
  1778. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1779. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1780. }
  1781. }
  1782. return visible;
  1783. }
  1784. /**
  1785. * Prepare data for processing orthographic output format.
  1786. *
  1787. * @param ctx filter context
  1788. *
  1789. * @return error code
  1790. */
  1791. static int prepare_orthographic_out(AVFilterContext *ctx)
  1792. {
  1793. V360Context *s = ctx->priv;
  1794. s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
  1795. s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
  1796. return 0;
  1797. }
  1798. /**
  1799. * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
  1800. *
  1801. * @param s filter private context
  1802. * @param i horizontal position on frame [0, width)
  1803. * @param j vertical position on frame [0, height)
  1804. * @param width frame width
  1805. * @param height frame height
  1806. * @param vec coordinates on sphere
  1807. */
  1808. static int orthographic_to_xyz(const V360Context *s,
  1809. int i, int j, int width, int height,
  1810. float *vec)
  1811. {
  1812. const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
  1813. const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
  1814. const float r = hypotf(x, y);
  1815. const float theta = asinf(r);
  1816. vec[0] = x;
  1817. vec[1] = y;
  1818. vec[2] = cosf(theta);
  1819. normalize_vector(vec);
  1820. return 1;
  1821. }
  1822. /**
  1823. * Prepare data for processing orthographic input format.
  1824. *
  1825. * @param ctx filter context
  1826. *
  1827. * @return error code
  1828. */
  1829. static int prepare_orthographic_in(AVFilterContext *ctx)
  1830. {
  1831. V360Context *s = ctx->priv;
  1832. s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
  1833. s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
  1834. return 0;
  1835. }
  1836. /**
  1837. * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
  1838. *
  1839. * @param s filter private context
  1840. * @param vec coordinates on sphere
  1841. * @param width frame width
  1842. * @param height frame height
  1843. * @param us horizontal coordinates for interpolation window
  1844. * @param vs vertical coordinates for interpolation window
  1845. * @param du horizontal relative coordinate
  1846. * @param dv vertical relative coordinate
  1847. */
  1848. static int xyz_to_orthographic(const V360Context *s,
  1849. const float *vec, int width, int height,
  1850. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1851. {
  1852. const float theta = acosf(vec[2]);
  1853. const float r = sinf(theta);
  1854. const float c = r / hypotf(vec[0], vec[1]);
  1855. const float x = vec[0] * c / s->iflat_range[0];
  1856. const float y = vec[1] * c / s->iflat_range[1];
  1857. const float uf = (x + 1.f) * width / 2.f;
  1858. const float vf = (y + 1.f) * height / 2.f;
  1859. const int ui = floorf(uf);
  1860. const int vi = floorf(vf);
  1861. const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
  1862. *du = visible ? uf - ui : 0.f;
  1863. *dv = visible ? vf - vi : 0.f;
  1864. for (int i = 0; i < 4; i++) {
  1865. for (int j = 0; j < 4; j++) {
  1866. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  1867. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  1868. }
  1869. }
  1870. return visible;
  1871. }
  1872. /**
  1873. * Prepare data for processing equirectangular input format.
  1874. *
  1875. * @param ctx filter context
  1876. *
  1877. * @return error code
  1878. */
  1879. static int prepare_equirect_in(AVFilterContext *ctx)
  1880. {
  1881. V360Context *s = ctx->priv;
  1882. s->iflat_range[0] = s->ih_fov * M_PI / 360.f;
  1883. s->iflat_range[1] = s->iv_fov * M_PI / 360.f;
  1884. return 0;
  1885. }
  1886. /**
  1887. * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
  1888. *
  1889. * @param s filter private context
  1890. * @param vec coordinates on sphere
  1891. * @param width frame width
  1892. * @param height frame height
  1893. * @param us horizontal coordinates for interpolation window
  1894. * @param vs vertical coordinates for interpolation window
  1895. * @param du horizontal relative coordinate
  1896. * @param dv vertical relative coordinate
  1897. */
  1898. static int xyz_to_equirect(const V360Context *s,
  1899. const float *vec, int width, int height,
  1900. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1901. {
  1902. const float phi = atan2f(vec[0], vec[2]);
  1903. const float theta = asinf(vec[1]);
  1904. const float uf = (phi / s->iflat_range[0] + 1.f) * width / 2.f;
  1905. const float vf = (theta / s->iflat_range[1] + 1.f) * height / 2.f;
  1906. const int ui = floorf(uf);
  1907. const int vi = floorf(vf);
  1908. int visible;
  1909. *du = uf - ui;
  1910. *dv = vf - vi;
  1911. visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
  1912. for (int i = 0; i < 4; i++) {
  1913. for (int j = 0; j < 4; j++) {
  1914. us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
  1915. vs[i][j] = reflecty(vi + i - 1, height);
  1916. }
  1917. }
  1918. return visible;
  1919. }
  1920. /**
  1921. * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
  1922. *
  1923. * @param s filter private context
  1924. * @param vec coordinates on sphere
  1925. * @param width frame width
  1926. * @param height frame height
  1927. * @param us horizontal coordinates for interpolation window
  1928. * @param vs vertical coordinates for interpolation window
  1929. * @param du horizontal relative coordinate
  1930. * @param dv vertical relative coordinate
  1931. */
  1932. static int xyz_to_hequirect(const V360Context *s,
  1933. const float *vec, int width, int height,
  1934. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1935. {
  1936. const float phi = atan2f(vec[0], vec[2]);
  1937. const float theta = asinf(vec[1]);
  1938. const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
  1939. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  1940. const int ui = floorf(uf);
  1941. const int vi = floorf(vf);
  1942. const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
  1943. *du = uf - ui;
  1944. *dv = vf - vi;
  1945. for (int i = 0; i < 4; i++) {
  1946. for (int j = 0; j < 4; j++) {
  1947. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  1948. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  1949. }
  1950. }
  1951. return visible;
  1952. }
  1953. /**
  1954. * Prepare data for processing flat input format.
  1955. *
  1956. * @param ctx filter context
  1957. *
  1958. * @return error code
  1959. */
  1960. static int prepare_flat_in(AVFilterContext *ctx)
  1961. {
  1962. V360Context *s = ctx->priv;
  1963. s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
  1964. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  1965. return 0;
  1966. }
  1967. /**
  1968. * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
  1969. *
  1970. * @param s filter private context
  1971. * @param vec coordinates on sphere
  1972. * @param width frame width
  1973. * @param height frame height
  1974. * @param us horizontal coordinates for interpolation window
  1975. * @param vs vertical coordinates for interpolation window
  1976. * @param du horizontal relative coordinate
  1977. * @param dv vertical relative coordinate
  1978. */
  1979. static int xyz_to_flat(const V360Context *s,
  1980. const float *vec, int width, int height,
  1981. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  1982. {
  1983. const float theta = acosf(vec[2]);
  1984. const float r = tanf(theta);
  1985. const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
  1986. const float zf = vec[2];
  1987. const float h = hypotf(vec[0], vec[1]);
  1988. const float c = h <= 1e-6f ? 1.f : rr / h;
  1989. float uf = vec[0] * c / s->iflat_range[0];
  1990. float vf = vec[1] * c / s->iflat_range[1];
  1991. int visible, ui, vi;
  1992. uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
  1993. vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
  1994. ui = floorf(uf);
  1995. vi = floorf(vf);
  1996. visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
  1997. *du = uf - ui;
  1998. *dv = vf - vi;
  1999. for (int i = 0; i < 4; i++) {
  2000. for (int j = 0; j < 4; j++) {
  2001. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2002. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2003. }
  2004. }
  2005. return visible;
  2006. }
  2007. /**
  2008. * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
  2009. *
  2010. * @param s filter private context
  2011. * @param vec coordinates on sphere
  2012. * @param width frame width
  2013. * @param height frame height
  2014. * @param us horizontal coordinates for interpolation window
  2015. * @param vs vertical coordinates for interpolation window
  2016. * @param du horizontal relative coordinate
  2017. * @param dv vertical relative coordinate
  2018. */
  2019. static int xyz_to_mercator(const V360Context *s,
  2020. const float *vec, int width, int height,
  2021. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2022. {
  2023. const float phi = atan2f(vec[0], vec[2]);
  2024. const float theta = vec[1];
  2025. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  2026. 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;
  2027. const int ui = floorf(uf);
  2028. const int vi = floorf(vf);
  2029. *du = uf - ui;
  2030. *dv = vf - vi;
  2031. for (int i = 0; i < 4; i++) {
  2032. for (int j = 0; j < 4; j++) {
  2033. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2034. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2035. }
  2036. }
  2037. return 1;
  2038. }
  2039. /**
  2040. * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
  2041. *
  2042. * @param s filter private context
  2043. * @param i horizontal position on frame [0, width)
  2044. * @param j vertical position on frame [0, height)
  2045. * @param width frame width
  2046. * @param height frame height
  2047. * @param vec coordinates on sphere
  2048. */
  2049. static int mercator_to_xyz(const V360Context *s,
  2050. int i, int j, int width, int height,
  2051. float *vec)
  2052. {
  2053. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
  2054. const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
  2055. const float div = expf(2.f * y) + 1.f;
  2056. const float sin_phi = sinf(phi);
  2057. const float cos_phi = cosf(phi);
  2058. const float sin_theta = 2.f * expf(y) / div;
  2059. const float cos_theta = (expf(2.f * y) - 1.f) / div;
  2060. vec[0] = -sin_theta * cos_phi;
  2061. vec[1] = cos_theta;
  2062. vec[2] = sin_theta * sin_phi;
  2063. return 1;
  2064. }
  2065. /**
  2066. * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
  2067. *
  2068. * @param s filter private context
  2069. * @param vec coordinates on sphere
  2070. * @param width frame width
  2071. * @param height frame height
  2072. * @param us horizontal coordinates for interpolation window
  2073. * @param vs vertical coordinates for interpolation window
  2074. * @param du horizontal relative coordinate
  2075. * @param dv vertical relative coordinate
  2076. */
  2077. static int xyz_to_ball(const V360Context *s,
  2078. const float *vec, int width, int height,
  2079. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2080. {
  2081. const float l = hypotf(vec[0], vec[1]);
  2082. const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
  2083. const float uf = (1.f + r * vec[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
  2084. const float vf = (1.f + r * vec[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
  2085. const int ui = floorf(uf);
  2086. const int vi = floorf(vf);
  2087. *du = uf - ui;
  2088. *dv = vf - vi;
  2089. for (int i = 0; i < 4; i++) {
  2090. for (int j = 0; j < 4; j++) {
  2091. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2092. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2093. }
  2094. }
  2095. return 1;
  2096. }
  2097. /**
  2098. * Calculate 3D coordinates on sphere for corresponding frame position in ball 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 ball_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 l = hypotf(x, y);
  2114. if (l <= 1.f) {
  2115. const float z = 2.f * l * sqrtf(1.f - l * l);
  2116. vec[0] = z * x / (l > 0.f ? l : 1.f);
  2117. vec[1] = z * y / (l > 0.f ? l : 1.f);
  2118. vec[2] = 1.f - 2.f * l * l;
  2119. } else {
  2120. vec[0] = 0.f;
  2121. vec[1] = 1.f;
  2122. vec[2] = 0.f;
  2123. return 0;
  2124. }
  2125. return 1;
  2126. }
  2127. /**
  2128. * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
  2129. *
  2130. * @param s filter private context
  2131. * @param i horizontal position on frame [0, width)
  2132. * @param j vertical position on frame [0, height)
  2133. * @param width frame width
  2134. * @param height frame height
  2135. * @param vec coordinates on sphere
  2136. */
  2137. static int hammer_to_xyz(const V360Context *s,
  2138. int i, int j, int width, int height,
  2139. float *vec)
  2140. {
  2141. const float x = ((2.f * i + 1.f) / width - 1.f);
  2142. const float y = ((2.f * j + 1.f) / height - 1.f);
  2143. const float xx = x * x;
  2144. const float yy = y * y;
  2145. const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
  2146. const float a = M_SQRT2 * x * z;
  2147. const float b = 2.f * z * z - 1.f;
  2148. const float aa = a * a;
  2149. const float bb = b * b;
  2150. const float w = sqrtf(1.f - 2.f * yy * z * z);
  2151. vec[0] = w * 2.f * a * b / (aa + bb);
  2152. vec[1] = M_SQRT2 * y * z;
  2153. vec[2] = w * (bb - aa) / (aa + bb);
  2154. normalize_vector(vec);
  2155. return 1;
  2156. }
  2157. /**
  2158. * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
  2159. *
  2160. * @param s filter private context
  2161. * @param vec coordinates on sphere
  2162. * @param width frame width
  2163. * @param height frame height
  2164. * @param us horizontal coordinates for interpolation window
  2165. * @param vs vertical coordinates for interpolation window
  2166. * @param du horizontal relative coordinate
  2167. * @param dv vertical relative coordinate
  2168. */
  2169. static int xyz_to_hammer(const V360Context *s,
  2170. const float *vec, int width, int height,
  2171. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2172. {
  2173. const float theta = atan2f(vec[0], vec[2]);
  2174. const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
  2175. const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
  2176. const float y = vec[1] / z;
  2177. const float uf = (x + 1.f) * width / 2.f;
  2178. const float vf = (y + 1.f) * height / 2.f;
  2179. const int ui = floorf(uf);
  2180. const int vi = floorf(vf);
  2181. *du = uf - ui;
  2182. *dv = vf - vi;
  2183. for (int i = 0; i < 4; i++) {
  2184. for (int j = 0; j < 4; j++) {
  2185. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2186. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2187. }
  2188. }
  2189. return 1;
  2190. }
  2191. /**
  2192. * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
  2193. *
  2194. * @param s filter private context
  2195. * @param i horizontal position on frame [0, width)
  2196. * @param j vertical position on frame [0, height)
  2197. * @param width frame width
  2198. * @param height frame height
  2199. * @param vec coordinates on sphere
  2200. */
  2201. static int sinusoidal_to_xyz(const V360Context *s,
  2202. int i, int j, int width, int height,
  2203. float *vec)
  2204. {
  2205. const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
  2206. const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
  2207. const float sin_phi = sinf(phi);
  2208. const float cos_phi = cosf(phi);
  2209. const float sin_theta = sinf(theta);
  2210. const float cos_theta = cosf(theta);
  2211. vec[0] = cos_theta * sin_phi;
  2212. vec[1] = sin_theta;
  2213. vec[2] = cos_theta * cos_phi;
  2214. normalize_vector(vec);
  2215. return 1;
  2216. }
  2217. /**
  2218. * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
  2219. *
  2220. * @param s filter private context
  2221. * @param vec coordinates on sphere
  2222. * @param width frame width
  2223. * @param height frame height
  2224. * @param us horizontal coordinates for interpolation window
  2225. * @param vs vertical coordinates for interpolation window
  2226. * @param du horizontal relative coordinate
  2227. * @param dv vertical relative coordinate
  2228. */
  2229. static int xyz_to_sinusoidal(const V360Context *s,
  2230. const float *vec, int width, int height,
  2231. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2232. {
  2233. const float theta = asinf(vec[1]);
  2234. const float phi = atan2f(vec[0], vec[2]) * cosf(theta);
  2235. const float uf = (phi / M_PI + 1.f) * width / 2.f;
  2236. const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
  2237. const int ui = floorf(uf);
  2238. const int vi = floorf(vf);
  2239. *du = uf - ui;
  2240. *dv = vf - vi;
  2241. for (int i = 0; i < 4; i++) {
  2242. for (int j = 0; j < 4; j++) {
  2243. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2244. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2245. }
  2246. }
  2247. return 1;
  2248. }
  2249. /**
  2250. * Prepare data for processing equi-angular cubemap input format.
  2251. *
  2252. * @param ctx filter context
  2253. *
  2254. * @return error code
  2255. */
  2256. static int prepare_eac_in(AVFilterContext *ctx)
  2257. {
  2258. V360Context *s = ctx->priv;
  2259. s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
  2260. s->in_cubemap_face_order[LEFT] = TOP_LEFT;
  2261. s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
  2262. s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
  2263. s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
  2264. s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
  2265. s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2266. s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2267. s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2268. s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2269. s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2270. s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2271. return 0;
  2272. }
  2273. /**
  2274. * Prepare data for processing equi-angular cubemap output format.
  2275. *
  2276. * @param ctx filter context
  2277. *
  2278. * @return error code
  2279. */
  2280. static int prepare_eac_out(AVFilterContext *ctx)
  2281. {
  2282. V360Context *s = ctx->priv;
  2283. s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
  2284. s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
  2285. s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
  2286. s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
  2287. s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
  2288. s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
  2289. s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
  2290. s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
  2291. s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
  2292. s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
  2293. s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
  2294. s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
  2295. return 0;
  2296. }
  2297. /**
  2298. * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
  2299. *
  2300. * @param s filter private context
  2301. * @param i horizontal position on frame [0, width)
  2302. * @param j vertical position on frame [0, height)
  2303. * @param width frame width
  2304. * @param height frame height
  2305. * @param vec coordinates on sphere
  2306. */
  2307. static int eac_to_xyz(const V360Context *s,
  2308. int i, int j, int width, int height,
  2309. float *vec)
  2310. {
  2311. const float pixel_pad = 2;
  2312. const float u_pad = pixel_pad / width;
  2313. const float v_pad = pixel_pad / height;
  2314. int u_face, v_face, face;
  2315. float l_x, l_y, l_z;
  2316. float uf = (i + 0.5f) / width;
  2317. float vf = (j + 0.5f) / height;
  2318. // EAC has 2-pixel padding on faces except between faces on the same row
  2319. // Padding pixels seems not to be stretched with tangent as regular pixels
  2320. // Formulas below approximate original padding as close as I could get experimentally
  2321. // Horizontal padding
  2322. uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
  2323. if (uf < 0.f) {
  2324. u_face = 0;
  2325. uf -= 0.5f;
  2326. } else if (uf >= 3.f) {
  2327. u_face = 2;
  2328. uf -= 2.5f;
  2329. } else {
  2330. u_face = floorf(uf);
  2331. uf = fmodf(uf, 1.f) - 0.5f;
  2332. }
  2333. // Vertical padding
  2334. v_face = floorf(vf * 2.f);
  2335. vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
  2336. if (uf >= -0.5f && uf < 0.5f) {
  2337. uf = tanf(M_PI_2 * uf);
  2338. } else {
  2339. uf = 2.f * uf;
  2340. }
  2341. if (vf >= -0.5f && vf < 0.5f) {
  2342. vf = tanf(M_PI_2 * vf);
  2343. } else {
  2344. vf = 2.f * vf;
  2345. }
  2346. face = u_face + 3 * v_face;
  2347. switch (face) {
  2348. case TOP_LEFT:
  2349. l_x = -1.f;
  2350. l_y = vf;
  2351. l_z = uf;
  2352. break;
  2353. case TOP_MIDDLE:
  2354. l_x = uf;
  2355. l_y = vf;
  2356. l_z = 1.f;
  2357. break;
  2358. case TOP_RIGHT:
  2359. l_x = 1.f;
  2360. l_y = vf;
  2361. l_z = -uf;
  2362. break;
  2363. case BOTTOM_LEFT:
  2364. l_x = -vf;
  2365. l_y = 1.f;
  2366. l_z = -uf;
  2367. break;
  2368. case BOTTOM_MIDDLE:
  2369. l_x = -vf;
  2370. l_y = -uf;
  2371. l_z = -1.f;
  2372. break;
  2373. case BOTTOM_RIGHT:
  2374. l_x = -vf;
  2375. l_y = -1.f;
  2376. l_z = uf;
  2377. break;
  2378. default:
  2379. av_assert0(0);
  2380. }
  2381. vec[0] = l_x;
  2382. vec[1] = l_y;
  2383. vec[2] = l_z;
  2384. normalize_vector(vec);
  2385. return 1;
  2386. }
  2387. /**
  2388. * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
  2389. *
  2390. * @param s filter private context
  2391. * @param vec coordinates on sphere
  2392. * @param width frame width
  2393. * @param height frame height
  2394. * @param us horizontal coordinates for interpolation window
  2395. * @param vs vertical coordinates for interpolation window
  2396. * @param du horizontal relative coordinate
  2397. * @param dv vertical relative coordinate
  2398. */
  2399. static int xyz_to_eac(const V360Context *s,
  2400. const float *vec, int width, int height,
  2401. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2402. {
  2403. const float pixel_pad = 2;
  2404. const float u_pad = pixel_pad / width;
  2405. const float v_pad = pixel_pad / height;
  2406. float uf, vf;
  2407. int ui, vi;
  2408. int direction, face;
  2409. int u_face, v_face;
  2410. xyz_to_cube(s, vec, &uf, &vf, &direction);
  2411. face = s->in_cubemap_face_order[direction];
  2412. u_face = face % 3;
  2413. v_face = face / 3;
  2414. uf = M_2_PI * atanf(uf) + 0.5f;
  2415. vf = M_2_PI * atanf(vf) + 0.5f;
  2416. // These formulas are inversed from eac_to_xyz ones
  2417. uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
  2418. vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
  2419. uf *= width;
  2420. vf *= height;
  2421. uf -= 0.5f;
  2422. vf -= 0.5f;
  2423. ui = floorf(uf);
  2424. vi = floorf(vf);
  2425. *du = uf - ui;
  2426. *dv = vf - vi;
  2427. for (int i = 0; i < 4; i++) {
  2428. for (int j = 0; j < 4; j++) {
  2429. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  2430. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  2431. }
  2432. }
  2433. return 1;
  2434. }
  2435. /**
  2436. * Prepare data for processing flat output format.
  2437. *
  2438. * @param ctx filter context
  2439. *
  2440. * @return error code
  2441. */
  2442. static int prepare_flat_out(AVFilterContext *ctx)
  2443. {
  2444. V360Context *s = ctx->priv;
  2445. s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
  2446. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2447. return 0;
  2448. }
  2449. /**
  2450. * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
  2451. *
  2452. * @param s filter private context
  2453. * @param i horizontal position on frame [0, width)
  2454. * @param j vertical position on frame [0, height)
  2455. * @param width frame width
  2456. * @param height frame height
  2457. * @param vec coordinates on sphere
  2458. */
  2459. static int flat_to_xyz(const V360Context *s,
  2460. int i, int j, int width, int height,
  2461. float *vec)
  2462. {
  2463. const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
  2464. const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
  2465. vec[0] = l_x;
  2466. vec[1] = l_y;
  2467. vec[2] = 1.f;
  2468. normalize_vector(vec);
  2469. return 1;
  2470. }
  2471. /**
  2472. * Prepare data for processing fisheye output format.
  2473. *
  2474. * @param ctx filter context
  2475. *
  2476. * @return error code
  2477. */
  2478. static int prepare_fisheye_out(AVFilterContext *ctx)
  2479. {
  2480. V360Context *s = ctx->priv;
  2481. s->flat_range[0] = s->h_fov / 180.f;
  2482. s->flat_range[1] = s->v_fov / 180.f;
  2483. return 0;
  2484. }
  2485. /**
  2486. * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
  2487. *
  2488. * @param s filter private context
  2489. * @param i horizontal position on frame [0, width)
  2490. * @param j vertical position on frame [0, height)
  2491. * @param width frame width
  2492. * @param height frame height
  2493. * @param vec coordinates on sphere
  2494. */
  2495. static int fisheye_to_xyz(const V360Context *s,
  2496. int i, int j, int width, int height,
  2497. float *vec)
  2498. {
  2499. const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
  2500. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2501. const float phi = atan2f(vf, uf);
  2502. const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
  2503. const float sin_phi = sinf(phi);
  2504. const float cos_phi = cosf(phi);
  2505. const float sin_theta = sinf(theta);
  2506. const float cos_theta = cosf(theta);
  2507. vec[0] = cos_theta * cos_phi;
  2508. vec[1] = cos_theta * sin_phi;
  2509. vec[2] = sin_theta;
  2510. normalize_vector(vec);
  2511. return 1;
  2512. }
  2513. /**
  2514. * Prepare data for processing fisheye input format.
  2515. *
  2516. * @param ctx filter context
  2517. *
  2518. * @return error code
  2519. */
  2520. static int prepare_fisheye_in(AVFilterContext *ctx)
  2521. {
  2522. V360Context *s = ctx->priv;
  2523. s->iflat_range[0] = s->ih_fov / 180.f;
  2524. s->iflat_range[1] = s->iv_fov / 180.f;
  2525. return 0;
  2526. }
  2527. /**
  2528. * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
  2529. *
  2530. * @param s filter private context
  2531. * @param vec coordinates on sphere
  2532. * @param width frame width
  2533. * @param height frame height
  2534. * @param us horizontal coordinates for interpolation window
  2535. * @param vs vertical coordinates for interpolation window
  2536. * @param du horizontal relative coordinate
  2537. * @param dv vertical relative coordinate
  2538. */
  2539. static int xyz_to_fisheye(const V360Context *s,
  2540. const float *vec, int width, int height,
  2541. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2542. {
  2543. const float h = hypotf(vec[0], vec[1]);
  2544. const float lh = h > 0.f ? h : 1.f;
  2545. const float phi = atan2f(h, vec[2]) / M_PI;
  2546. float uf = vec[0] / lh * phi / s->iflat_range[0];
  2547. float vf = vec[1] / lh * phi / s->iflat_range[1];
  2548. const int visible = hypotf(uf, vf) <= 0.5f;
  2549. int ui, vi;
  2550. uf = (uf + 0.5f) * width;
  2551. vf = (vf + 0.5f) * height;
  2552. ui = floorf(uf);
  2553. vi = floorf(vf);
  2554. *du = visible ? uf - ui : 0.f;
  2555. *dv = visible ? vf - vi : 0.f;
  2556. for (int i = 0; i < 4; i++) {
  2557. for (int j = 0; j < 4; j++) {
  2558. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2559. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2560. }
  2561. }
  2562. return visible;
  2563. }
  2564. /**
  2565. * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
  2566. *
  2567. * @param s filter private context
  2568. * @param i horizontal position on frame [0, width)
  2569. * @param j vertical position on frame [0, height)
  2570. * @param width frame width
  2571. * @param height frame height
  2572. * @param vec coordinates on sphere
  2573. */
  2574. static int pannini_to_xyz(const V360Context *s,
  2575. int i, int j, int width, int height,
  2576. float *vec)
  2577. {
  2578. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2579. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2580. const float d = s->h_fov;
  2581. const float k = uf * uf / ((d + 1.f) * (d + 1.f));
  2582. const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
  2583. const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
  2584. const float S = (d + 1.f) / (d + clon);
  2585. const float lon = atan2f(uf, S * clon);
  2586. const float lat = atan2f(vf, S);
  2587. vec[0] = sinf(lon) * cosf(lat);
  2588. vec[1] = sinf(lat);
  2589. vec[2] = cosf(lon) * cosf(lat);
  2590. normalize_vector(vec);
  2591. return 1;
  2592. }
  2593. /**
  2594. * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
  2595. *
  2596. * @param s filter private context
  2597. * @param vec coordinates on sphere
  2598. * @param width frame width
  2599. * @param height frame height
  2600. * @param us horizontal coordinates for interpolation window
  2601. * @param vs vertical coordinates for interpolation window
  2602. * @param du horizontal relative coordinate
  2603. * @param dv vertical relative coordinate
  2604. */
  2605. static int xyz_to_pannini(const V360Context *s,
  2606. const float *vec, int width, int height,
  2607. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2608. {
  2609. const float phi = atan2f(vec[0], vec[2]);
  2610. const float theta = asinf(vec[1]);
  2611. const float d = s->ih_fov;
  2612. const float S = (d + 1.f) / (d + cosf(phi));
  2613. const float x = S * sinf(phi);
  2614. const float y = S * tanf(theta);
  2615. const float uf = (x + 1.f) * width / 2.f;
  2616. const float vf = (y + 1.f) * height / 2.f;
  2617. const int ui = floorf(uf);
  2618. const int vi = floorf(vf);
  2619. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
  2620. *du = uf - ui;
  2621. *dv = vf - vi;
  2622. for (int i = 0; i < 4; i++) {
  2623. for (int j = 0; j < 4; j++) {
  2624. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2625. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2626. }
  2627. }
  2628. return visible;
  2629. }
  2630. /**
  2631. * Prepare data for processing cylindrical output format.
  2632. *
  2633. * @param ctx filter context
  2634. *
  2635. * @return error code
  2636. */
  2637. static int prepare_cylindrical_out(AVFilterContext *ctx)
  2638. {
  2639. V360Context *s = ctx->priv;
  2640. s->flat_range[0] = M_PI * s->h_fov / 360.f;
  2641. s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
  2642. return 0;
  2643. }
  2644. /**
  2645. * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
  2646. *
  2647. * @param s filter private context
  2648. * @param i horizontal position on frame [0, width)
  2649. * @param j vertical position on frame [0, height)
  2650. * @param width frame width
  2651. * @param height frame height
  2652. * @param vec coordinates on sphere
  2653. */
  2654. static int cylindrical_to_xyz(const V360Context *s,
  2655. int i, int j, int width, int height,
  2656. float *vec)
  2657. {
  2658. const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
  2659. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
  2660. const float phi = uf;
  2661. const float theta = atanf(vf);
  2662. const float sin_phi = sinf(phi);
  2663. const float cos_phi = cosf(phi);
  2664. const float sin_theta = sinf(theta);
  2665. const float cos_theta = cosf(theta);
  2666. vec[0] = cos_theta * sin_phi;
  2667. vec[1] = sin_theta;
  2668. vec[2] = cos_theta * cos_phi;
  2669. normalize_vector(vec);
  2670. return 1;
  2671. }
  2672. /**
  2673. * Prepare data for processing cylindrical input format.
  2674. *
  2675. * @param ctx filter context
  2676. *
  2677. * @return error code
  2678. */
  2679. static int prepare_cylindrical_in(AVFilterContext *ctx)
  2680. {
  2681. V360Context *s = ctx->priv;
  2682. s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
  2683. s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
  2684. return 0;
  2685. }
  2686. /**
  2687. * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
  2688. *
  2689. * @param s filter private context
  2690. * @param vec coordinates on sphere
  2691. * @param width frame width
  2692. * @param height frame height
  2693. * @param us horizontal coordinates for interpolation window
  2694. * @param vs vertical coordinates for interpolation window
  2695. * @param du horizontal relative coordinate
  2696. * @param dv vertical relative coordinate
  2697. */
  2698. static int xyz_to_cylindrical(const V360Context *s,
  2699. const float *vec, int width, int height,
  2700. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2701. {
  2702. const float phi = atan2f(vec[0], vec[2]) / s->iflat_range[0];
  2703. const float theta = asinf(vec[1]);
  2704. const float uf = (phi + 1.f) * (width - 1) / 2.f;
  2705. const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
  2706. const int ui = floorf(uf);
  2707. const int vi = floorf(vf);
  2708. const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
  2709. theta <= M_PI * s->iv_fov / 180.f &&
  2710. theta >= -M_PI * s->iv_fov / 180.f;
  2711. *du = uf - ui;
  2712. *dv = vf - vi;
  2713. for (int i = 0; i < 4; i++) {
  2714. for (int j = 0; j < 4; j++) {
  2715. us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
  2716. vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
  2717. }
  2718. }
  2719. return visible;
  2720. }
  2721. /**
  2722. * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
  2723. *
  2724. * @param s filter private context
  2725. * @param i horizontal position on frame [0, width)
  2726. * @param j vertical position on frame [0, height)
  2727. * @param width frame width
  2728. * @param height frame height
  2729. * @param vec coordinates on sphere
  2730. */
  2731. static int perspective_to_xyz(const V360Context *s,
  2732. int i, int j, int width, int height,
  2733. float *vec)
  2734. {
  2735. const float uf = ((2.f * i + 1.f) / width - 1.f);
  2736. const float vf = ((2.f * j + 1.f) / height - 1.f);
  2737. const float rh = hypotf(uf, vf);
  2738. const float sinzz = 1.f - rh * rh;
  2739. const float h = 1.f + s->v_fov;
  2740. const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
  2741. const float sinz2 = sinz * sinz;
  2742. if (sinz2 <= 1.f) {
  2743. const float cosz = sqrtf(1.f - sinz2);
  2744. const float theta = asinf(cosz);
  2745. const float phi = atan2f(uf, vf);
  2746. const float sin_phi = sinf(phi);
  2747. const float cos_phi = cosf(phi);
  2748. const float sin_theta = sinf(theta);
  2749. const float cos_theta = cosf(theta);
  2750. vec[0] = cos_theta * sin_phi;
  2751. vec[1] = cos_theta * cos_phi;
  2752. vec[2] = sin_theta;
  2753. } else {
  2754. vec[0] = 0.f;
  2755. vec[1] = 1.f;
  2756. vec[2] = 0.f;
  2757. return 0;
  2758. }
  2759. return 1;
  2760. }
  2761. /**
  2762. * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
  2763. *
  2764. * @param s filter private context
  2765. * @param i horizontal position on frame [0, width)
  2766. * @param j vertical position on frame [0, height)
  2767. * @param width frame width
  2768. * @param height frame height
  2769. * @param vec coordinates on sphere
  2770. */
  2771. static int tetrahedron_to_xyz(const V360Context *s,
  2772. int i, int j, int width, int height,
  2773. float *vec)
  2774. {
  2775. const float uf = (float)i / width;
  2776. const float vf = (float)j / height;
  2777. vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
  2778. vec[1] = 1.f - vf * 2.f;
  2779. vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
  2780. normalize_vector(vec);
  2781. return 1;
  2782. }
  2783. /**
  2784. * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
  2785. *
  2786. * @param s filter private context
  2787. * @param vec coordinates on sphere
  2788. * @param width frame width
  2789. * @param height frame height
  2790. * @param us horizontal coordinates for interpolation window
  2791. * @param vs vertical coordinates for interpolation window
  2792. * @param du horizontal relative coordinate
  2793. * @param dv vertical relative coordinate
  2794. */
  2795. static int xyz_to_tetrahedron(const V360Context *s,
  2796. const float *vec, int width, int height,
  2797. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2798. {
  2799. const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
  2800. const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
  2801. const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
  2802. const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
  2803. const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
  2804. float uf, vf, x, y, z;
  2805. int ui, vi;
  2806. x = vec[0] / d;
  2807. y = vec[1] / d;
  2808. z = -vec[2] / d;
  2809. vf = 0.5f - y * 0.5f;
  2810. if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
  2811. (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
  2812. uf = 0.25f * x + 0.25f;
  2813. } else {
  2814. uf = 0.75f - 0.25f * x;
  2815. }
  2816. uf *= width;
  2817. vf *= height;
  2818. ui = floorf(uf);
  2819. vi = floorf(vf);
  2820. *du = uf - ui;
  2821. *dv = vf - vi;
  2822. for (int i = 0; i < 4; i++) {
  2823. for (int j = 0; j < 4; j++) {
  2824. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  2825. vs[i][j] = reflecty(vi + i - 1, height);
  2826. }
  2827. }
  2828. return 1;
  2829. }
  2830. /**
  2831. * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
  2832. *
  2833. * @param s filter private context
  2834. * @param i horizontal position on frame [0, width)
  2835. * @param j vertical position on frame [0, height)
  2836. * @param width frame width
  2837. * @param height frame height
  2838. * @param vec coordinates on sphere
  2839. */
  2840. static int dfisheye_to_xyz(const V360Context *s,
  2841. int i, int j, int width, int height,
  2842. float *vec)
  2843. {
  2844. const float ew = width / 2.f;
  2845. const float eh = height;
  2846. const int ei = i >= ew ? i - ew : i;
  2847. const float m = i >= ew ? 1.f : -1.f;
  2848. const float uf = s->flat_range[0] * ((2.f * ei) / ew - 1.f);
  2849. const float vf = s->flat_range[1] * ((2.f * j + 1.f) / eh - 1.f);
  2850. const float h = hypotf(uf, vf);
  2851. const float lh = h > 0.f ? h : 1.f;
  2852. const float theta = m * M_PI_2 * (1.f - h);
  2853. const float sin_theta = sinf(theta);
  2854. const float cos_theta = cosf(theta);
  2855. vec[0] = cos_theta * m * uf / lh;
  2856. vec[1] = cos_theta * vf / lh;
  2857. vec[2] = sin_theta;
  2858. normalize_vector(vec);
  2859. return 1;
  2860. }
  2861. /**
  2862. * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
  2863. *
  2864. * @param s filter private context
  2865. * @param vec coordinates on sphere
  2866. * @param width frame width
  2867. * @param height frame height
  2868. * @param us horizontal coordinates for interpolation window
  2869. * @param vs vertical coordinates for interpolation window
  2870. * @param du horizontal relative coordinate
  2871. * @param dv vertical relative coordinate
  2872. */
  2873. static int xyz_to_dfisheye(const V360Context *s,
  2874. const float *vec, int width, int height,
  2875. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2876. {
  2877. const float ew = width / 2.f;
  2878. const float eh = height;
  2879. const float h = hypotf(vec[0], vec[1]);
  2880. const float lh = h > 0.f ? h : 1.f;
  2881. const float theta = acosf(fabsf(vec[2])) / M_PI;
  2882. float uf = (theta * (vec[0] / lh) / s->iflat_range[0] + 0.5f) * ew;
  2883. float vf = (theta * (vec[1] / lh) / s->iflat_range[1] + 0.5f) * eh;
  2884. int ui, vi;
  2885. int u_shift;
  2886. if (vec[2] >= 0.f) {
  2887. u_shift = ceilf(ew);
  2888. } else {
  2889. u_shift = 0;
  2890. uf = ew - uf;
  2891. }
  2892. ui = floorf(uf);
  2893. vi = floorf(vf);
  2894. *du = uf - ui;
  2895. *dv = vf - vi;
  2896. for (int i = 0; i < 4; i++) {
  2897. for (int j = 0; j < 4; j++) {
  2898. us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
  2899. vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
  2900. }
  2901. }
  2902. return 1;
  2903. }
  2904. /**
  2905. * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
  2906. *
  2907. * @param s filter private context
  2908. * @param i horizontal position on frame [0, width)
  2909. * @param j vertical position on frame [0, height)
  2910. * @param width frame width
  2911. * @param height frame height
  2912. * @param vec coordinates on sphere
  2913. */
  2914. static int barrel_to_xyz(const V360Context *s,
  2915. int i, int j, int width, int height,
  2916. float *vec)
  2917. {
  2918. const float scale = 0.99f;
  2919. float l_x, l_y, l_z;
  2920. if (i < 4 * width / 5) {
  2921. const float theta_range = M_PI_4;
  2922. const int ew = 4 * width / 5;
  2923. const int eh = height;
  2924. const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
  2925. const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
  2926. const float sin_phi = sinf(phi);
  2927. const float cos_phi = cosf(phi);
  2928. const float sin_theta = sinf(theta);
  2929. const float cos_theta = cosf(theta);
  2930. l_x = cos_theta * sin_phi;
  2931. l_y = sin_theta;
  2932. l_z = cos_theta * cos_phi;
  2933. } else {
  2934. const int ew = width / 5;
  2935. const int eh = height / 2;
  2936. float uf, vf;
  2937. if (j < eh) { // UP
  2938. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2939. vf = 2.f * (j ) / eh - 1.f;
  2940. uf /= scale;
  2941. vf /= scale;
  2942. l_x = uf;
  2943. l_y = -1.f;
  2944. l_z = vf;
  2945. } else { // DOWN
  2946. uf = 2.f * (i - 4 * ew) / ew - 1.f;
  2947. vf = 2.f * (j - eh) / eh - 1.f;
  2948. uf /= scale;
  2949. vf /= scale;
  2950. l_x = uf;
  2951. l_y = 1.f;
  2952. l_z = -vf;
  2953. }
  2954. }
  2955. vec[0] = l_x;
  2956. vec[1] = l_y;
  2957. vec[2] = l_z;
  2958. normalize_vector(vec);
  2959. return 1;
  2960. }
  2961. /**
  2962. * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
  2963. *
  2964. * @param s filter private context
  2965. * @param vec coordinates on sphere
  2966. * @param width frame width
  2967. * @param height frame height
  2968. * @param us horizontal coordinates for interpolation window
  2969. * @param vs vertical coordinates for interpolation window
  2970. * @param du horizontal relative coordinate
  2971. * @param dv vertical relative coordinate
  2972. */
  2973. static int xyz_to_barrel(const V360Context *s,
  2974. const float *vec, int width, int height,
  2975. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  2976. {
  2977. const float scale = 0.99f;
  2978. const float phi = atan2f(vec[0], vec[2]);
  2979. const float theta = asinf(vec[1]);
  2980. const float theta_range = M_PI_4;
  2981. int ew, eh;
  2982. int u_shift, v_shift;
  2983. float uf, vf;
  2984. int ui, vi;
  2985. if (theta > -theta_range && theta < theta_range) {
  2986. ew = 4 * width / 5;
  2987. eh = height;
  2988. u_shift = 0;
  2989. v_shift = 0;
  2990. uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
  2991. vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
  2992. } else {
  2993. ew = width / 5;
  2994. eh = height / 2;
  2995. u_shift = 4 * ew;
  2996. if (theta < 0.f) { // UP
  2997. uf = -vec[0] / vec[1];
  2998. vf = -vec[2] / vec[1];
  2999. v_shift = 0;
  3000. } else { // DOWN
  3001. uf = vec[0] / vec[1];
  3002. vf = -vec[2] / vec[1];
  3003. v_shift = eh;
  3004. }
  3005. uf = 0.5f * ew * (uf * scale + 1.f);
  3006. vf = 0.5f * eh * (vf * scale + 1.f);
  3007. }
  3008. ui = floorf(uf);
  3009. vi = floorf(vf);
  3010. *du = uf - ui;
  3011. *dv = vf - vi;
  3012. for (int i = 0; i < 4; i++) {
  3013. for (int j = 0; j < 4; j++) {
  3014. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  3015. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  3016. }
  3017. }
  3018. return 1;
  3019. }
  3020. /**
  3021. * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
  3022. *
  3023. * @param s filter private context
  3024. * @param vec coordinates on sphere
  3025. * @param width frame width
  3026. * @param height frame height
  3027. * @param us horizontal coordinates for interpolation window
  3028. * @param vs vertical coordinates for interpolation window
  3029. * @param du horizontal relative coordinate
  3030. * @param dv vertical relative coordinate
  3031. */
  3032. static int xyz_to_barrelsplit(const V360Context *s,
  3033. const float *vec, int width, int height,
  3034. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3035. {
  3036. const float phi = atan2f(vec[0], vec[2]);
  3037. const float theta = asinf(vec[1]);
  3038. const float theta_range = M_PI_4;
  3039. int ew, eh;
  3040. int u_shift, v_shift;
  3041. float uf, vf;
  3042. int ui, vi;
  3043. if (theta >= -theta_range && theta <= theta_range) {
  3044. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
  3045. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
  3046. ew = width / 3 * 2;
  3047. eh = height / 2;
  3048. u_shift = 0;
  3049. v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
  3050. uf = fmodf(phi, M_PI_2) / M_PI_2;
  3051. vf = theta / M_PI_4;
  3052. if (v_shift)
  3053. uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
  3054. uf = (uf * scalew + 1.f) * width / 3.f;
  3055. vf = (vf * scaleh + 1.f) * height / 4.f;
  3056. } else {
  3057. const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
  3058. const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
  3059. int v_offset = 0;
  3060. ew = width / 3;
  3061. eh = height / 4;
  3062. u_shift = 2 * ew;
  3063. if (theta <= 0.f && theta >= -M_PI_2 &&
  3064. phi <= M_PI_2 && phi >= -M_PI_2) {
  3065. uf = -vec[0] / vec[1];
  3066. vf = -vec[2] / vec[1];
  3067. v_shift = 0;
  3068. v_offset = -eh;
  3069. } else if (theta >= 0.f && theta <= M_PI_2 &&
  3070. phi <= M_PI_2 && phi >= -M_PI_2) {
  3071. uf = vec[0] / vec[1];
  3072. vf = -vec[2] / vec[1];
  3073. v_shift = height * 0.25f;
  3074. } else if (theta <= 0.f && theta >= -M_PI_2) {
  3075. uf = vec[0] / vec[1];
  3076. vf = vec[2] / vec[1];
  3077. v_shift = height * 0.5f;
  3078. v_offset = -eh;
  3079. } else {
  3080. uf = -vec[0] / vec[1];
  3081. vf = vec[2] / vec[1];
  3082. v_shift = height * 0.75f;
  3083. }
  3084. uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
  3085. vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
  3086. }
  3087. ui = floorf(uf);
  3088. vi = floorf(vf);
  3089. *du = uf - ui;
  3090. *dv = vf - vi;
  3091. for (int i = 0; i < 4; i++) {
  3092. for (int j = 0; j < 4; j++) {
  3093. us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
  3094. vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
  3095. }
  3096. }
  3097. return 1;
  3098. }
  3099. /**
  3100. * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
  3101. *
  3102. * @param s filter private context
  3103. * @param i horizontal position on frame [0, width)
  3104. * @param j vertical position on frame [0, height)
  3105. * @param width frame width
  3106. * @param height frame height
  3107. * @param vec coordinates on sphere
  3108. */
  3109. static int barrelsplit_to_xyz(const V360Context *s,
  3110. int i, int j, int width, int height,
  3111. float *vec)
  3112. {
  3113. const float x = (i + 0.5f) / width;
  3114. const float y = (j + 0.5f) / height;
  3115. float l_x, l_y, l_z;
  3116. if (x < 2.f / 3.f) {
  3117. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
  3118. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
  3119. const float back = floorf(y * 2.f);
  3120. const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
  3121. const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
  3122. const float sin_phi = sinf(phi);
  3123. const float cos_phi = cosf(phi);
  3124. const float sin_theta = sinf(theta);
  3125. const float cos_theta = cosf(theta);
  3126. l_x = cos_theta * sin_phi;
  3127. l_y = sin_theta;
  3128. l_z = cos_theta * cos_phi;
  3129. } else {
  3130. const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
  3131. const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
  3132. const int face = floorf(y * 4.f);
  3133. float uf, vf;
  3134. uf = x * 3.f - 2.f;
  3135. switch (face) {
  3136. case 0:
  3137. vf = y * 2.f;
  3138. uf = 1.f - uf;
  3139. vf = 0.5f - vf;
  3140. l_x = (0.5f - uf) / scalew;
  3141. l_y = -0.5f;
  3142. l_z = (0.5f - vf) / scaleh;
  3143. break;
  3144. case 1:
  3145. vf = y * 2.f;
  3146. uf = 1.f - uf;
  3147. vf = 1.f - (vf - 0.5f);
  3148. l_x = (0.5f - uf) / scalew;
  3149. l_y = 0.5f;
  3150. l_z = (-0.5f + vf) / scaleh;
  3151. break;
  3152. case 2:
  3153. vf = y * 2.f - 0.5f;
  3154. vf = 1.f - (1.f - vf);
  3155. l_x = (0.5f - uf) / scalew;
  3156. l_y = -0.5f;
  3157. l_z = (0.5f - vf) / scaleh;
  3158. break;
  3159. case 3:
  3160. vf = y * 2.f - 1.5f;
  3161. l_x = (0.5f - uf) / scalew;
  3162. l_y = 0.5f;
  3163. l_z = (-0.5f + vf) / scaleh;
  3164. break;
  3165. }
  3166. }
  3167. vec[0] = l_x;
  3168. vec[1] = l_y;
  3169. vec[2] = l_z;
  3170. normalize_vector(vec);
  3171. return 1;
  3172. }
  3173. /**
  3174. * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
  3175. *
  3176. * @param s filter private context
  3177. * @param i horizontal position on frame [0, width)
  3178. * @param j vertical position on frame [0, height)
  3179. * @param width frame width
  3180. * @param height frame height
  3181. * @param vec coordinates on sphere
  3182. */
  3183. static int tspyramid_to_xyz(const V360Context *s,
  3184. int i, int j, int width, int height,
  3185. float *vec)
  3186. {
  3187. const float x = (i + 0.5f) / width;
  3188. const float y = (j + 0.5f) / height;
  3189. if (x < 0.5f) {
  3190. vec[0] = x * 4.f - 1.f;
  3191. vec[1] = (y * 2.f - 1.f);
  3192. vec[2] = 1.f;
  3193. } else if (x >= 0.6875f && x < 0.8125f &&
  3194. y >= 0.375f && y < 0.625f) {
  3195. vec[0] = -(x - 0.6875f) * 16.f + 1.f;
  3196. vec[1] = (y - 0.375f) * 8.f - 1.f;
  3197. vec[2] = -1.f;
  3198. } else if (0.5f <= x && x < 0.6875f &&
  3199. ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
  3200. (0.375f <= y && y < 0.625f) ||
  3201. (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
  3202. vec[0] = 1.f;
  3203. vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
  3204. vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
  3205. } else if (0.8125f <= x && x < 1.f &&
  3206. ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
  3207. (0.375f <= y && y < 0.625f) ||
  3208. (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
  3209. vec[0] = -1.f;
  3210. vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
  3211. vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
  3212. } else if (0.f <= y && y < 0.375f &&
  3213. ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
  3214. (0.6875f <= x && x < 0.8125f) ||
  3215. (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
  3216. vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
  3217. vec[1] = -1.f;
  3218. vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
  3219. } else {
  3220. vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
  3221. vec[1] = 1.f;
  3222. vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
  3223. }
  3224. normalize_vector(vec);
  3225. return 1;
  3226. }
  3227. /**
  3228. * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
  3229. *
  3230. * @param s filter private context
  3231. * @param vec coordinates on sphere
  3232. * @param width frame width
  3233. * @param height frame height
  3234. * @param us horizontal coordinates for interpolation window
  3235. * @param vs vertical coordinates for interpolation window
  3236. * @param du horizontal relative coordinate
  3237. * @param dv vertical relative coordinate
  3238. */
  3239. static int xyz_to_tspyramid(const V360Context *s,
  3240. const float *vec, int width, int height,
  3241. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3242. {
  3243. float uf, vf;
  3244. int ui, vi;
  3245. int face;
  3246. xyz_to_cube(s, vec, &uf, &vf, &face);
  3247. uf = (uf + 1.f) * 0.5f;
  3248. vf = (vf + 1.f) * 0.5f;
  3249. switch (face) {
  3250. case UP:
  3251. uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
  3252. vf = 0.375f - 0.375f * vf;
  3253. break;
  3254. case FRONT:
  3255. uf = 0.5f * uf;
  3256. break;
  3257. case DOWN:
  3258. uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
  3259. vf = 1.f - 0.375f * vf;
  3260. break;
  3261. case LEFT:
  3262. vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
  3263. uf = 0.1875f * uf + 0.8125f;
  3264. break;
  3265. case RIGHT:
  3266. vf = 0.375f * uf - 0.75f * uf * vf + vf;
  3267. uf = 0.1875f * uf + 0.5f;
  3268. break;
  3269. case BACK:
  3270. uf = 0.125f * uf + 0.6875f;
  3271. vf = 0.25f * vf + 0.375f;
  3272. break;
  3273. }
  3274. uf *= width;
  3275. vf *= height;
  3276. ui = floorf(uf);
  3277. vi = floorf(vf);
  3278. *du = uf - ui;
  3279. *dv = vf - vi;
  3280. for (int i = 0; i < 4; i++) {
  3281. for (int j = 0; j < 4; j++) {
  3282. us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
  3283. vs[i][j] = reflecty(vi + i - 1, height);
  3284. }
  3285. }
  3286. return 1;
  3287. }
  3288. /**
  3289. * Calculate 3D coordinates on sphere for corresponding frame position in octahedron format.
  3290. *
  3291. * @param s filter private context
  3292. * @param i horizontal position on frame [0, width)
  3293. * @param j vertical position on frame [0, height)
  3294. * @param width frame width
  3295. * @param height frame height
  3296. * @param vec coordinates on sphere
  3297. */
  3298. static int octahedron_to_xyz(const V360Context *s,
  3299. int i, int j, int width, int height,
  3300. float *vec)
  3301. {
  3302. const float x = ((i + 0.5f) / width) * 2.f - 1.f;
  3303. const float y = ((j + 0.5f) / height) * 2.f - 1.f;
  3304. const float ax = fabsf(x);
  3305. const float ay = fabsf(y);
  3306. vec[2] = 1.f - (ax + ay);
  3307. if (ax + ay > 1.f) {
  3308. vec[0] = (1.f - ay) * FFSIGN(x);
  3309. vec[1] = (1.f - ax) * FFSIGN(y);
  3310. } else {
  3311. vec[0] = x;
  3312. vec[1] = y;
  3313. }
  3314. normalize_vector(vec);
  3315. return 1;
  3316. }
  3317. /**
  3318. * Calculate frame position in octahedron format for corresponding 3D coordinates on sphere.
  3319. *
  3320. * @param s filter private context
  3321. * @param vec coordinates on sphere
  3322. * @param width frame width
  3323. * @param height frame height
  3324. * @param us horizontal coordinates for interpolation window
  3325. * @param vs vertical coordinates for interpolation window
  3326. * @param du horizontal relative coordinate
  3327. * @param dv vertical relative coordinate
  3328. */
  3329. static int xyz_to_octahedron(const V360Context *s,
  3330. const float *vec, int width, int height,
  3331. int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
  3332. {
  3333. float uf, vf, zf;
  3334. int ui, vi;
  3335. float div = fabsf(vec[0]) + fabsf(vec[1]) + fabsf(vec[2]);
  3336. uf = vec[0] / div;
  3337. vf = vec[1] / div;
  3338. zf = vec[2];
  3339. if (zf < 0.f) {
  3340. zf = vf;
  3341. vf = (1.f - fabsf(uf)) * FFSIGN(zf);
  3342. uf = (1.f - fabsf(zf)) * FFSIGN(uf);
  3343. }
  3344. uf = uf * 0.5f + 0.5f;
  3345. vf = vf * 0.5f + 0.5f;
  3346. uf *= width;
  3347. vf *= height;
  3348. ui = floorf(uf);
  3349. vi = floorf(vf);
  3350. *du = uf - ui;
  3351. *dv = vf - vi;
  3352. for (int i = 0; i < 4; i++) {
  3353. for (int j = 0; j < 4; j++) {
  3354. us[i][j] = av_clip(ui + j - 1, 0, width - 1);
  3355. vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
  3356. }
  3357. }
  3358. return 1;
  3359. }
  3360. static void multiply_quaternion(float c[4], const float a[4], const float b[4])
  3361. {
  3362. c[0] = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3];
  3363. c[1] = a[1] * b[0] + a[0] * b[1] + a[2] * b[3] - a[3] * b[2];
  3364. c[2] = a[2] * b[0] + a[0] * b[2] + a[3] * b[1] - a[1] * b[3];
  3365. c[3] = a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1];
  3366. }
  3367. static void conjugate_quaternion(float d[4], const float q[4])
  3368. {
  3369. d[0] = q[0];
  3370. d[1] = -q[1];
  3371. d[2] = -q[2];
  3372. d[3] = -q[3];
  3373. }
  3374. /**
  3375. * Calculate rotation quaternion for yaw/pitch/roll angles.
  3376. */
  3377. static inline void calculate_rotation(float yaw, float pitch, float roll,
  3378. float rot_quaternion[2][4],
  3379. const int rotation_order[3])
  3380. {
  3381. const float yaw_rad = yaw * M_PI / 180.f;
  3382. const float pitch_rad = pitch * M_PI / 180.f;
  3383. const float roll_rad = roll * M_PI / 180.f;
  3384. const float sin_yaw = sinf(yaw_rad * 0.5f);
  3385. const float cos_yaw = cosf(yaw_rad * 0.5f);
  3386. const float sin_pitch = sinf(pitch_rad * 0.5f);
  3387. const float cos_pitch = cosf(pitch_rad * 0.5f);
  3388. const float sin_roll = sinf(roll_rad * 0.5f);
  3389. const float cos_roll = cosf(roll_rad * 0.5f);
  3390. float m[3][4];
  3391. float tmp[2][4];
  3392. m[0][0] = cos_yaw; m[0][1] = 0.f; m[0][2] = sin_yaw; m[0][3] = 0.f;
  3393. m[1][0] = cos_pitch; m[1][1] = sin_pitch; m[1][2] = 0.f; m[1][3] = 0.f;
  3394. m[2][0] = cos_roll; m[2][1] = 0.f; m[2][2] = 0.f; m[2][3] = sin_roll;
  3395. multiply_quaternion(tmp[0], rot_quaternion[0], m[rotation_order[0]]);
  3396. multiply_quaternion(tmp[1], tmp[0], m[rotation_order[1]]);
  3397. multiply_quaternion(rot_quaternion[0], tmp[1], m[rotation_order[2]]);
  3398. conjugate_quaternion(rot_quaternion[1], rot_quaternion[0]);
  3399. }
  3400. /**
  3401. * Rotate vector with given rotation quaternion.
  3402. *
  3403. * @param rot_quaternion rotation quaternion
  3404. * @param vec vector
  3405. */
  3406. static inline void rotate(const float rot_quaternion[2][4],
  3407. float *vec)
  3408. {
  3409. float qv[4], temp[4], rqv[4];
  3410. qv[0] = 0.f;
  3411. qv[1] = vec[0];
  3412. qv[2] = vec[1];
  3413. qv[3] = vec[2];
  3414. multiply_quaternion(temp, rot_quaternion[0], qv);
  3415. multiply_quaternion(rqv, temp, rot_quaternion[1]);
  3416. vec[0] = rqv[1];
  3417. vec[1] = rqv[2];
  3418. vec[2] = rqv[3];
  3419. }
  3420. static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
  3421. float *modifier)
  3422. {
  3423. modifier[0] = h_flip ? -1.f : 1.f;
  3424. modifier[1] = v_flip ? -1.f : 1.f;
  3425. modifier[2] = d_flip ? -1.f : 1.f;
  3426. }
  3427. static inline void mirror(const float *modifier, float *vec)
  3428. {
  3429. vec[0] *= modifier[0];
  3430. vec[1] *= modifier[1];
  3431. vec[2] *= modifier[2];
  3432. }
  3433. static inline void input_flip(int16_t u[4][4], int16_t v[4][4], int w, int h, int hflip, int vflip)
  3434. {
  3435. if (hflip) {
  3436. for (int i = 0; i < 4; i++) {
  3437. for (int j = 0; j < 4; j++)
  3438. u[i][j] = w - 1 - u[i][j];
  3439. }
  3440. }
  3441. if (vflip) {
  3442. for (int i = 0; i < 4; i++) {
  3443. for (int j = 0; j < 4; j++)
  3444. v[i][j] = h - 1 - v[i][j];
  3445. }
  3446. }
  3447. }
  3448. static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
  3449. {
  3450. const int pr_height = s->pr_height[p];
  3451. for (int n = 0; n < s->nb_threads; n++) {
  3452. SliceXYRemap *r = &s->slice_remap[n];
  3453. const int slice_start = (pr_height * n ) / s->nb_threads;
  3454. const int slice_end = (pr_height * (n + 1)) / s->nb_threads;
  3455. const int height = slice_end - slice_start;
  3456. if (!r->u[p])
  3457. r->u[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
  3458. if (!r->v[p])
  3459. r->v[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
  3460. if (!r->u[p] || !r->v[p])
  3461. return AVERROR(ENOMEM);
  3462. if (sizeof_ker) {
  3463. if (!r->ker[p])
  3464. r->ker[p] = av_calloc(s->uv_linesize[p] * height, sizeof_ker);
  3465. if (!r->ker[p])
  3466. return AVERROR(ENOMEM);
  3467. }
  3468. if (sizeof_mask && !p) {
  3469. if (!r->mask)
  3470. r->mask = av_calloc(s->pr_width[p] * height, sizeof_mask);
  3471. if (!r->mask)
  3472. return AVERROR(ENOMEM);
  3473. }
  3474. }
  3475. return 0;
  3476. }
  3477. static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
  3478. {
  3479. switch (format) {
  3480. case EQUIRECTANGULAR:
  3481. *h_fov = d_fov;
  3482. *v_fov = d_fov * 0.5f;
  3483. break;
  3484. case ORTHOGRAPHIC:
  3485. {
  3486. const float d = 0.5f * hypotf(w, h);
  3487. const float l = sinf(d_fov * M_PI / 360.f) / d;
  3488. *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
  3489. *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
  3490. if (d_fov > 180.f) {
  3491. *h_fov = 180.f - *h_fov;
  3492. *v_fov = 180.f - *v_fov;
  3493. }
  3494. }
  3495. break;
  3496. case EQUISOLID:
  3497. {
  3498. const float d = 0.5f * hypotf(w, h);
  3499. const float l = d / (sinf(d_fov * M_PI / 720.f));
  3500. *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
  3501. *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
  3502. }
  3503. break;
  3504. case STEREOGRAPHIC:
  3505. {
  3506. const float d = 0.5f * hypotf(w, h);
  3507. const float l = d / (tanf(d_fov * M_PI / 720.f));
  3508. *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
  3509. *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
  3510. }
  3511. break;
  3512. case DUAL_FISHEYE:
  3513. {
  3514. const float d = 0.5f * hypotf(w * 0.5f, h);
  3515. *h_fov = d / w * 2.f * d_fov;
  3516. *v_fov = d / h * d_fov;
  3517. }
  3518. break;
  3519. case FISHEYE:
  3520. {
  3521. const float d = 0.5f * hypotf(w, h);
  3522. *h_fov = d / w * d_fov;
  3523. *v_fov = d / h * d_fov;
  3524. }
  3525. break;
  3526. case FLAT:
  3527. default:
  3528. {
  3529. const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
  3530. const float d = hypotf(w, h);
  3531. *h_fov = atan2f(da * w, d) * 360.f / M_PI;
  3532. *v_fov = atan2f(da * h, d) * 360.f / M_PI;
  3533. if (*h_fov < 0.f)
  3534. *h_fov += 360.f;
  3535. if (*v_fov < 0.f)
  3536. *v_fov += 360.f;
  3537. }
  3538. break;
  3539. }
  3540. }
  3541. static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
  3542. {
  3543. outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
  3544. outw[0] = outw[3] = w;
  3545. outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  3546. outh[0] = outh[3] = h;
  3547. }
  3548. // Calculate remap data
  3549. static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  3550. {
  3551. V360Context *s = ctx->priv;
  3552. SliceXYRemap *r = &s->slice_remap[jobnr];
  3553. for (int p = 0; p < s->nb_allocated; p++) {
  3554. const int max_value = s->max_value;
  3555. const int width = s->pr_width[p];
  3556. const int uv_linesize = s->uv_linesize[p];
  3557. const int height = s->pr_height[p];
  3558. const int in_width = s->inplanewidth[p];
  3559. const int in_height = s->inplaneheight[p];
  3560. const int slice_start = (height * jobnr ) / nb_jobs;
  3561. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  3562. const int elements = s->elements;
  3563. float du, dv;
  3564. float vec[3];
  3565. XYRemap rmap;
  3566. for (int j = slice_start; j < slice_end; j++) {
  3567. for (int i = 0; i < width; i++) {
  3568. int16_t *u = r->u[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3569. int16_t *v = r->v[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3570. int16_t *ker = r->ker[p] + ((j - slice_start) * uv_linesize + i) * elements;
  3571. uint8_t *mask8 = p ? NULL : r->mask + ((j - slice_start) * s->pr_width[0] + i);
  3572. uint16_t *mask16 = p ? NULL : (uint16_t *)r->mask + ((j - slice_start) * s->pr_width[0] + i);
  3573. int in_mask, out_mask;
  3574. if (s->out_transpose)
  3575. out_mask = s->out_transform(s, j, i, height, width, vec);
  3576. else
  3577. out_mask = s->out_transform(s, i, j, width, height, vec);
  3578. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3579. rotate(s->rot_quaternion, vec);
  3580. av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
  3581. normalize_vector(vec);
  3582. mirror(s->output_mirror_modifier, vec);
  3583. if (s->in_transpose)
  3584. in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
  3585. else
  3586. in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
  3587. input_flip(rmap.u, rmap.v, in_width, in_height, s->ih_flip, s->iv_flip);
  3588. av_assert1(!isnan(du) && !isnan(dv));
  3589. s->calculate_kernel(du, dv, &rmap, u, v, ker);
  3590. if (!p && r->mask) {
  3591. if (s->mask_size == 1) {
  3592. mask8[0] = 255 * (out_mask & in_mask);
  3593. } else {
  3594. mask16[0] = max_value * (out_mask & in_mask);
  3595. }
  3596. }
  3597. }
  3598. }
  3599. }
  3600. return 0;
  3601. }
  3602. static int config_output(AVFilterLink *outlink)
  3603. {
  3604. AVFilterContext *ctx = outlink->src;
  3605. AVFilterLink *inlink = ctx->inputs[0];
  3606. V360Context *s = ctx->priv;
  3607. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  3608. const int depth = desc->comp[0].depth;
  3609. const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
  3610. float default_h_fov = 360.f;
  3611. float default_v_fov = 180.f;
  3612. float default_ih_fov = 360.f;
  3613. float default_iv_fov = 180.f;
  3614. int sizeof_uv;
  3615. int sizeof_ker;
  3616. int err;
  3617. int h, w;
  3618. int in_offset_h, in_offset_w;
  3619. int out_offset_h, out_offset_w;
  3620. float hf, wf;
  3621. int (*prepare_out)(AVFilterContext *ctx);
  3622. int have_alpha;
  3623. s->max_value = (1 << depth) - 1;
  3624. switch (s->interp) {
  3625. case NEAREST:
  3626. s->calculate_kernel = nearest_kernel;
  3627. s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
  3628. s->elements = 1;
  3629. sizeof_uv = sizeof(int16_t) * s->elements;
  3630. sizeof_ker = 0;
  3631. break;
  3632. case BILINEAR:
  3633. s->calculate_kernel = bilinear_kernel;
  3634. s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
  3635. s->elements = 2 * 2;
  3636. sizeof_uv = sizeof(int16_t) * s->elements;
  3637. sizeof_ker = sizeof(int16_t) * s->elements;
  3638. break;
  3639. case LAGRANGE9:
  3640. s->calculate_kernel = lagrange_kernel;
  3641. s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
  3642. s->elements = 3 * 3;
  3643. sizeof_uv = sizeof(int16_t) * s->elements;
  3644. sizeof_ker = sizeof(int16_t) * s->elements;
  3645. break;
  3646. case BICUBIC:
  3647. s->calculate_kernel = bicubic_kernel;
  3648. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3649. s->elements = 4 * 4;
  3650. sizeof_uv = sizeof(int16_t) * s->elements;
  3651. sizeof_ker = sizeof(int16_t) * s->elements;
  3652. break;
  3653. case LANCZOS:
  3654. s->calculate_kernel = lanczos_kernel;
  3655. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3656. s->elements = 4 * 4;
  3657. sizeof_uv = sizeof(int16_t) * s->elements;
  3658. sizeof_ker = sizeof(int16_t) * s->elements;
  3659. break;
  3660. case SPLINE16:
  3661. s->calculate_kernel = spline16_kernel;
  3662. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3663. s->elements = 4 * 4;
  3664. sizeof_uv = sizeof(int16_t) * s->elements;
  3665. sizeof_ker = sizeof(int16_t) * s->elements;
  3666. break;
  3667. case GAUSSIAN:
  3668. s->calculate_kernel = gaussian_kernel;
  3669. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3670. s->elements = 4 * 4;
  3671. sizeof_uv = sizeof(int16_t) * s->elements;
  3672. sizeof_ker = sizeof(int16_t) * s->elements;
  3673. break;
  3674. case MITCHELL:
  3675. s->calculate_kernel = mitchell_kernel;
  3676. s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
  3677. s->elements = 4 * 4;
  3678. sizeof_uv = sizeof(int16_t) * s->elements;
  3679. sizeof_ker = sizeof(int16_t) * s->elements;
  3680. break;
  3681. default:
  3682. av_assert0(0);
  3683. }
  3684. ff_v360_init(s, depth);
  3685. for (int order = 0; order < NB_RORDERS; order++) {
  3686. const char c = s->rorder[order];
  3687. int rorder;
  3688. if (c == '\0') {
  3689. av_log(ctx, AV_LOG_WARNING,
  3690. "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
  3691. s->rotation_order[0] = YAW;
  3692. s->rotation_order[1] = PITCH;
  3693. s->rotation_order[2] = ROLL;
  3694. break;
  3695. }
  3696. rorder = get_rorder(c);
  3697. if (rorder == -1) {
  3698. av_log(ctx, AV_LOG_WARNING,
  3699. "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
  3700. s->rotation_order[0] = YAW;
  3701. s->rotation_order[1] = PITCH;
  3702. s->rotation_order[2] = ROLL;
  3703. break;
  3704. }
  3705. s->rotation_order[order] = rorder;
  3706. }
  3707. switch (s->in_stereo) {
  3708. case STEREO_2D:
  3709. w = inlink->w;
  3710. h = inlink->h;
  3711. in_offset_w = in_offset_h = 0;
  3712. break;
  3713. case STEREO_SBS:
  3714. w = inlink->w / 2;
  3715. h = inlink->h;
  3716. in_offset_w = w;
  3717. in_offset_h = 0;
  3718. break;
  3719. case STEREO_TB:
  3720. w = inlink->w;
  3721. h = inlink->h / 2;
  3722. in_offset_w = 0;
  3723. in_offset_h = h;
  3724. break;
  3725. default:
  3726. av_assert0(0);
  3727. }
  3728. set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
  3729. set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
  3730. s->in_width = s->inplanewidth[0];
  3731. s->in_height = s->inplaneheight[0];
  3732. switch (s->in) {
  3733. case CYLINDRICAL:
  3734. case FLAT:
  3735. default_ih_fov = 90.f;
  3736. default_iv_fov = 45.f;
  3737. break;
  3738. case EQUISOLID:
  3739. case ORTHOGRAPHIC:
  3740. case STEREOGRAPHIC:
  3741. case DUAL_FISHEYE:
  3742. case FISHEYE:
  3743. default_ih_fov = 180.f;
  3744. default_iv_fov = 180.f;
  3745. default:
  3746. break;
  3747. }
  3748. if (s->ih_fov == 0.f)
  3749. s->ih_fov = default_ih_fov;
  3750. if (s->iv_fov == 0.f)
  3751. s->iv_fov = default_iv_fov;
  3752. if (s->id_fov > 0.f)
  3753. fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
  3754. if (s->in_transpose)
  3755. FFSWAP(int, s->in_width, s->in_height);
  3756. switch (s->in) {
  3757. case EQUIRECTANGULAR:
  3758. s->in_transform = xyz_to_equirect;
  3759. err = prepare_equirect_in(ctx);
  3760. wf = w;
  3761. hf = h;
  3762. break;
  3763. case CUBEMAP_3_2:
  3764. s->in_transform = xyz_to_cube3x2;
  3765. err = prepare_cube_in(ctx);
  3766. wf = w / 3.f * 4.f;
  3767. hf = h;
  3768. break;
  3769. case CUBEMAP_1_6:
  3770. s->in_transform = xyz_to_cube1x6;
  3771. err = prepare_cube_in(ctx);
  3772. wf = w * 4.f;
  3773. hf = h / 3.f;
  3774. break;
  3775. case CUBEMAP_6_1:
  3776. s->in_transform = xyz_to_cube6x1;
  3777. err = prepare_cube_in(ctx);
  3778. wf = w / 3.f * 2.f;
  3779. hf = h * 2.f;
  3780. break;
  3781. case EQUIANGULAR:
  3782. s->in_transform = xyz_to_eac;
  3783. err = prepare_eac_in(ctx);
  3784. wf = w;
  3785. hf = h / 9.f * 8.f;
  3786. break;
  3787. case FLAT:
  3788. s->in_transform = xyz_to_flat;
  3789. err = prepare_flat_in(ctx);
  3790. wf = w;
  3791. hf = h;
  3792. break;
  3793. case PERSPECTIVE:
  3794. av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
  3795. return AVERROR(EINVAL);
  3796. case DUAL_FISHEYE:
  3797. s->in_transform = xyz_to_dfisheye;
  3798. err = prepare_fisheye_in(ctx);
  3799. wf = w;
  3800. hf = h;
  3801. break;
  3802. case BARREL:
  3803. s->in_transform = xyz_to_barrel;
  3804. err = 0;
  3805. wf = w / 5.f * 4.f;
  3806. hf = h;
  3807. break;
  3808. case STEREOGRAPHIC:
  3809. s->in_transform = xyz_to_stereographic;
  3810. err = prepare_stereographic_in(ctx);
  3811. wf = w;
  3812. hf = h / 2.f;
  3813. break;
  3814. case MERCATOR:
  3815. s->in_transform = xyz_to_mercator;
  3816. err = 0;
  3817. wf = w;
  3818. hf = h / 2.f;
  3819. break;
  3820. case BALL:
  3821. s->in_transform = xyz_to_ball;
  3822. err = 0;
  3823. wf = w;
  3824. hf = h / 2.f;
  3825. break;
  3826. case HAMMER:
  3827. s->in_transform = xyz_to_hammer;
  3828. err = 0;
  3829. wf = w;
  3830. hf = h;
  3831. break;
  3832. case SINUSOIDAL:
  3833. s->in_transform = xyz_to_sinusoidal;
  3834. err = 0;
  3835. wf = w;
  3836. hf = h;
  3837. break;
  3838. case FISHEYE:
  3839. s->in_transform = xyz_to_fisheye;
  3840. err = prepare_fisheye_in(ctx);
  3841. wf = w * 2;
  3842. hf = h;
  3843. break;
  3844. case PANNINI:
  3845. s->in_transform = xyz_to_pannini;
  3846. err = 0;
  3847. wf = w;
  3848. hf = h;
  3849. break;
  3850. case CYLINDRICAL:
  3851. s->in_transform = xyz_to_cylindrical;
  3852. err = prepare_cylindrical_in(ctx);
  3853. wf = w;
  3854. hf = h * 2.f;
  3855. break;
  3856. case TETRAHEDRON:
  3857. s->in_transform = xyz_to_tetrahedron;
  3858. err = 0;
  3859. wf = w;
  3860. hf = h;
  3861. break;
  3862. case BARREL_SPLIT:
  3863. s->in_transform = xyz_to_barrelsplit;
  3864. err = 0;
  3865. wf = w * 4.f / 3.f;
  3866. hf = h;
  3867. break;
  3868. case TSPYRAMID:
  3869. s->in_transform = xyz_to_tspyramid;
  3870. err = 0;
  3871. wf = w;
  3872. hf = h;
  3873. break;
  3874. case HEQUIRECTANGULAR:
  3875. s->in_transform = xyz_to_hequirect;
  3876. err = 0;
  3877. wf = w * 2.f;
  3878. hf = h;
  3879. break;
  3880. case EQUISOLID:
  3881. s->in_transform = xyz_to_equisolid;
  3882. err = prepare_equisolid_in(ctx);
  3883. wf = w;
  3884. hf = h / 2.f;
  3885. break;
  3886. case ORTHOGRAPHIC:
  3887. s->in_transform = xyz_to_orthographic;
  3888. err = prepare_orthographic_in(ctx);
  3889. wf = w;
  3890. hf = h / 2.f;
  3891. break;
  3892. case OCTAHEDRON:
  3893. s->in_transform = xyz_to_octahedron;
  3894. err = 0;
  3895. wf = w;
  3896. hf = h / 2.f;
  3897. break;
  3898. default:
  3899. av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
  3900. return AVERROR_BUG;
  3901. }
  3902. if (err != 0) {
  3903. return err;
  3904. }
  3905. switch (s->out) {
  3906. case EQUIRECTANGULAR:
  3907. s->out_transform = equirect_to_xyz;
  3908. prepare_out = prepare_equirect_out;
  3909. w = lrintf(wf);
  3910. h = lrintf(hf);
  3911. break;
  3912. case CUBEMAP_3_2:
  3913. s->out_transform = cube3x2_to_xyz;
  3914. prepare_out = prepare_cube_out;
  3915. w = lrintf(wf / 4.f * 3.f);
  3916. h = lrintf(hf);
  3917. break;
  3918. case CUBEMAP_1_6:
  3919. s->out_transform = cube1x6_to_xyz;
  3920. prepare_out = prepare_cube_out;
  3921. w = lrintf(wf / 4.f);
  3922. h = lrintf(hf * 3.f);
  3923. break;
  3924. case CUBEMAP_6_1:
  3925. s->out_transform = cube6x1_to_xyz;
  3926. prepare_out = prepare_cube_out;
  3927. w = lrintf(wf / 2.f * 3.f);
  3928. h = lrintf(hf / 2.f);
  3929. break;
  3930. case EQUIANGULAR:
  3931. s->out_transform = eac_to_xyz;
  3932. prepare_out = prepare_eac_out;
  3933. w = lrintf(wf);
  3934. h = lrintf(hf / 8.f * 9.f);
  3935. break;
  3936. case FLAT:
  3937. s->out_transform = flat_to_xyz;
  3938. prepare_out = prepare_flat_out;
  3939. w = lrintf(wf);
  3940. h = lrintf(hf);
  3941. break;
  3942. case DUAL_FISHEYE:
  3943. s->out_transform = dfisheye_to_xyz;
  3944. prepare_out = prepare_fisheye_out;
  3945. w = lrintf(wf);
  3946. h = lrintf(hf);
  3947. break;
  3948. case BARREL:
  3949. s->out_transform = barrel_to_xyz;
  3950. prepare_out = NULL;
  3951. w = lrintf(wf / 4.f * 5.f);
  3952. h = lrintf(hf);
  3953. break;
  3954. case STEREOGRAPHIC:
  3955. s->out_transform = stereographic_to_xyz;
  3956. prepare_out = prepare_stereographic_out;
  3957. w = lrintf(wf);
  3958. h = lrintf(hf * 2.f);
  3959. break;
  3960. case MERCATOR:
  3961. s->out_transform = mercator_to_xyz;
  3962. prepare_out = NULL;
  3963. w = lrintf(wf);
  3964. h = lrintf(hf * 2.f);
  3965. break;
  3966. case BALL:
  3967. s->out_transform = ball_to_xyz;
  3968. prepare_out = NULL;
  3969. w = lrintf(wf);
  3970. h = lrintf(hf * 2.f);
  3971. break;
  3972. case HAMMER:
  3973. s->out_transform = hammer_to_xyz;
  3974. prepare_out = NULL;
  3975. w = lrintf(wf);
  3976. h = lrintf(hf);
  3977. break;
  3978. case SINUSOIDAL:
  3979. s->out_transform = sinusoidal_to_xyz;
  3980. prepare_out = NULL;
  3981. w = lrintf(wf);
  3982. h = lrintf(hf);
  3983. break;
  3984. case FISHEYE:
  3985. s->out_transform = fisheye_to_xyz;
  3986. prepare_out = prepare_fisheye_out;
  3987. w = lrintf(wf * 0.5f);
  3988. h = lrintf(hf);
  3989. break;
  3990. case PANNINI:
  3991. s->out_transform = pannini_to_xyz;
  3992. prepare_out = NULL;
  3993. w = lrintf(wf);
  3994. h = lrintf(hf);
  3995. break;
  3996. case CYLINDRICAL:
  3997. s->out_transform = cylindrical_to_xyz;
  3998. prepare_out = prepare_cylindrical_out;
  3999. w = lrintf(wf);
  4000. h = lrintf(hf * 0.5f);
  4001. break;
  4002. case PERSPECTIVE:
  4003. s->out_transform = perspective_to_xyz;
  4004. prepare_out = NULL;
  4005. w = lrintf(wf / 2.f);
  4006. h = lrintf(hf);
  4007. break;
  4008. case TETRAHEDRON:
  4009. s->out_transform = tetrahedron_to_xyz;
  4010. prepare_out = NULL;
  4011. w = lrintf(wf);
  4012. h = lrintf(hf);
  4013. break;
  4014. case BARREL_SPLIT:
  4015. s->out_transform = barrelsplit_to_xyz;
  4016. prepare_out = NULL;
  4017. w = lrintf(wf / 4.f * 3.f);
  4018. h = lrintf(hf);
  4019. break;
  4020. case TSPYRAMID:
  4021. s->out_transform = tspyramid_to_xyz;
  4022. prepare_out = NULL;
  4023. w = lrintf(wf);
  4024. h = lrintf(hf);
  4025. break;
  4026. case HEQUIRECTANGULAR:
  4027. s->out_transform = hequirect_to_xyz;
  4028. prepare_out = NULL;
  4029. w = lrintf(wf / 2.f);
  4030. h = lrintf(hf);
  4031. break;
  4032. case EQUISOLID:
  4033. s->out_transform = equisolid_to_xyz;
  4034. prepare_out = prepare_equisolid_out;
  4035. w = lrintf(wf);
  4036. h = lrintf(hf * 2.f);
  4037. break;
  4038. case ORTHOGRAPHIC:
  4039. s->out_transform = orthographic_to_xyz;
  4040. prepare_out = prepare_orthographic_out;
  4041. w = lrintf(wf);
  4042. h = lrintf(hf * 2.f);
  4043. break;
  4044. case OCTAHEDRON:
  4045. s->out_transform = octahedron_to_xyz;
  4046. prepare_out = NULL;
  4047. w = lrintf(wf);
  4048. h = lrintf(hf * 2.f);
  4049. break;
  4050. default:
  4051. av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
  4052. return AVERROR_BUG;
  4053. }
  4054. // Override resolution with user values if specified
  4055. if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  4056. s->out == FLAT && s->d_fov == 0.f) {
  4057. w = s->width;
  4058. h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
  4059. } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
  4060. s->out == FLAT && s->d_fov == 0.f) {
  4061. h = s->height;
  4062. w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
  4063. } else if (s->width > 0 && s->height > 0) {
  4064. w = s->width;
  4065. h = s->height;
  4066. } else if (s->width > 0 || s->height > 0) {
  4067. av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
  4068. return AVERROR(EINVAL);
  4069. } else {
  4070. if (s->out_transpose)
  4071. FFSWAP(int, w, h);
  4072. if (s->in_transpose)
  4073. FFSWAP(int, w, h);
  4074. }
  4075. s->width = w;
  4076. s->height = h;
  4077. switch (s->out) {
  4078. case CYLINDRICAL:
  4079. case FLAT:
  4080. default_h_fov = 90.f;
  4081. default_v_fov = 45.f;
  4082. break;
  4083. case EQUISOLID:
  4084. case ORTHOGRAPHIC:
  4085. case STEREOGRAPHIC:
  4086. case DUAL_FISHEYE:
  4087. case FISHEYE:
  4088. default_h_fov = 180.f;
  4089. default_v_fov = 180.f;
  4090. break;
  4091. default:
  4092. break;
  4093. }
  4094. if (s->h_fov == 0.f)
  4095. s->h_fov = default_h_fov;
  4096. if (s->v_fov == 0.f)
  4097. s->v_fov = default_v_fov;
  4098. if (s->d_fov > 0.f)
  4099. fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
  4100. if (prepare_out) {
  4101. err = prepare_out(ctx);
  4102. if (err != 0)
  4103. return err;
  4104. }
  4105. set_dimensions(s->pr_width, s->pr_height, w, h, desc);
  4106. switch (s->out_stereo) {
  4107. case STEREO_2D:
  4108. out_offset_w = out_offset_h = 0;
  4109. break;
  4110. case STEREO_SBS:
  4111. out_offset_w = w;
  4112. out_offset_h = 0;
  4113. w *= 2;
  4114. break;
  4115. case STEREO_TB:
  4116. out_offset_w = 0;
  4117. out_offset_h = h;
  4118. h *= 2;
  4119. break;
  4120. default:
  4121. av_assert0(0);
  4122. }
  4123. set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
  4124. set_dimensions(s->planewidth, s->planeheight, w, h, desc);
  4125. for (int i = 0; i < 4; i++)
  4126. s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
  4127. outlink->h = h;
  4128. outlink->w = w;
  4129. s->nb_threads = FFMIN(outlink->h, ff_filter_get_nb_threads(ctx));
  4130. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  4131. have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  4132. if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
  4133. s->nb_allocated = 1;
  4134. s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
  4135. } else {
  4136. s->nb_allocated = 2;
  4137. s->map[0] = s->map[3] = 0;
  4138. s->map[1] = s->map[2] = 1;
  4139. }
  4140. if (!s->slice_remap)
  4141. s->slice_remap = av_calloc(s->nb_threads, sizeof(*s->slice_remap));
  4142. if (!s->slice_remap)
  4143. return AVERROR(ENOMEM);
  4144. for (int i = 0; i < s->nb_allocated; i++) {
  4145. err = allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
  4146. if (err < 0)
  4147. return err;
  4148. }
  4149. calculate_rotation(s->yaw, s->pitch, s->roll,
  4150. s->rot_quaternion, s->rotation_order);
  4151. set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
  4152. ctx->internal->execute(ctx, v360_slice, NULL, NULL, s->nb_threads);
  4153. return 0;
  4154. }
  4155. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  4156. {
  4157. AVFilterContext *ctx = inlink->dst;
  4158. AVFilterLink *outlink = ctx->outputs[0];
  4159. V360Context *s = ctx->priv;
  4160. AVFrame *out;
  4161. ThreadData td;
  4162. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  4163. if (!out) {
  4164. av_frame_free(&in);
  4165. return AVERROR(ENOMEM);
  4166. }
  4167. av_frame_copy_props(out, in);
  4168. td.in = in;
  4169. td.out = out;
  4170. ctx->internal->execute(ctx, s->remap_slice, &td, NULL, s->nb_threads);
  4171. av_frame_free(&in);
  4172. return ff_filter_frame(outlink, out);
  4173. }
  4174. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  4175. char *res, int res_len, int flags)
  4176. {
  4177. V360Context *s = ctx->priv;
  4178. int ret;
  4179. s->yaw = s->pitch = s->roll = 0.f;
  4180. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  4181. if (ret < 0)
  4182. return ret;
  4183. return config_output(ctx->outputs[0]);
  4184. }
  4185. static av_cold int init(AVFilterContext *ctx)
  4186. {
  4187. V360Context *s = ctx->priv;
  4188. s->rot_quaternion[0][0] = 1.f;
  4189. s->rot_quaternion[0][1] = s->rot_quaternion[0][2] = s->rot_quaternion[0][3] = 0.f;
  4190. return 0;
  4191. }
  4192. static av_cold void uninit(AVFilterContext *ctx)
  4193. {
  4194. V360Context *s = ctx->priv;
  4195. for (int n = 0; n < s->nb_threads && s->slice_remap; n++) {
  4196. SliceXYRemap *r = &s->slice_remap[n];
  4197. for (int p = 0; p < s->nb_allocated; p++) {
  4198. av_freep(&r->u[p]);
  4199. av_freep(&r->v[p]);
  4200. av_freep(&r->ker[p]);
  4201. }
  4202. av_freep(&r->mask);
  4203. }
  4204. av_freep(&s->slice_remap);
  4205. }
  4206. static const AVFilterPad inputs[] = {
  4207. {
  4208. .name = "default",
  4209. .type = AVMEDIA_TYPE_VIDEO,
  4210. .filter_frame = filter_frame,
  4211. },
  4212. { NULL }
  4213. };
  4214. static const AVFilterPad outputs[] = {
  4215. {
  4216. .name = "default",
  4217. .type = AVMEDIA_TYPE_VIDEO,
  4218. .config_props = config_output,
  4219. },
  4220. { NULL }
  4221. };
  4222. AVFilter ff_vf_v360 = {
  4223. .name = "v360",
  4224. .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
  4225. .priv_size = sizeof(V360Context),
  4226. .init = init,
  4227. .uninit = uninit,
  4228. .query_formats = query_formats,
  4229. .inputs = inputs,
  4230. .outputs = outputs,
  4231. .priv_class = &v360_class,
  4232. .flags = AVFILTER_FLAG_SLICE_THREADS,
  4233. .process_command = process_command,
  4234. };