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.

3979 lines
128KB

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