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.

2257 lines
70KB

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