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.

2573 lines
83KB

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