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.

3247 lines
106KB

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