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.

2928 lines
96KB

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