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.

1975 lines
62KB

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