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.

3955 lines
127KB

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