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.

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