FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_v360.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 2405 0.0%
Functions: 0 135 0.0%
Branches: 0 1230 0.0%

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