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.

3694 lines
120KB

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