VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_opengl_picture.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_opengl_picture.h: VLC OpenGL picture API
3 *****************************************************************************
4 * Copyright (C) 2020-2026 VLC authors and VideoLAN
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20
21#ifndef VLC_GL_PICTURE_H
22#define VLC_GL_PICTURE_H 1
23
24#include <math.h>
25
26#include <vlc_picture.h>
27
28/**
29 * \file
30 * This file defines the public OpenGL picture interface.
31 *
32 * A vlc_gl_picture stores the OpenGL textures and transformation matrix
33 * associated with a VLC picture, as provided by the interop/importer.
34 */
35
36/**
37 * An OpenGL picture state, with textures and a coordinate transformation matrix.
38 */
39struct vlc_gl_picture {
40 /**
41 * Texture handles for each plane.
42 */
45 /**
46 * Matrix to convert from 2D pictures coordinates to texture coordinates
47 *
48 * tex_coords = mtx × pic_coords
49 *
50 * / tex_x \ / a b c \ / pic_x \
51 * \ tex_y / = \ d e f / × | pic_y |
52 * \ 1 /
53 *
54 * It is stored in column-major order: [a, d, b, e, c, f].
55 */
56 float mtx[2*3];
58 /**
59 * Indicate if the transform to convert picture coordinates to textures
60 * coordinates have changed due to the last picture.
61 *
62 * The filters should check this flag on every draw() call, and update
63 * their coordinates if necessary.
64 *
65 * It is guaranteed to be true for the first picture.
66 */
67
68 bool mtx_has_changed;
69};
70
71/**
72 * Convert from picture coordinates to texture coordinates, which can be used to
73 * sample at the correct location.
74 *
75 * This is a equivalent to retrieve the matrix and multiply manually.
76 *
77 * The picture and texture coords may point to the same memory, in that case
78 * the transformation is applied in place (overwriting the picture coordinates
79 * by the texture coordinates).
80 *
81 * \param picture the OpenGL picture
82 * \param coords_count the number of coordinates (x,y) coordinates to convert
83 * \param pic_coords picture coordinates as an array of 2*coords_count floats
84 * \param tex_coords_out texture coordinates as an array of 2*coords_count
85 * floats
86 */
87static inline void
89 unsigned coords_count,
90 const float *pic_coords,
91 float *tex_coords_out)
92{
93 const float *mtx = pic->mtx;
94 assert(mtx);
95
96#define MTX(ROW,COL) mtx[(COL)*2+(ROW)]
97 for (unsigned i = 0; i < coords_count; ++i)
98 {
99 /* Store the coordinates, in case the transform must be applied in
100 * place (i.e. with pic_coords == tex_coords_out) */
101 float x = pic_coords[0];
102 float y = pic_coords[1];
103 tex_coords_out[0] = MTX(0,0) * x + MTX(0,1) * y + MTX(0,2);
104 tex_coords_out[1] = MTX(1,0) * x + MTX(1,1) * y + MTX(1,2);
105 pic_coords += 2;
106 tex_coords_out += 2;
107 }
108#undef MTX
109}
110
111/**
112 * Return a matrix to orient texture coordinates
113 *
114 * This matrix is 2x2 and is stored in column-major order.
115 *
116 * While pic_to_tex_matrix transforms any picture coordinates into texture
117 * coordinates, it may be useful for example for vertex or fragment shaders to
118 * sample one pixel to the left of the current one, or two pixels to the top.
119 * Since the input texture may be rotated or flipped, the shaders need to
120 * know in which direction is the top and in which direction is the right of
121 * the picture.
122 *
123 * This 2x2 matrix allows to transform a 2D vector expressed in picture
124 * coordinates into a 2D vector expressed in texture coordinates.
125 *
126 * Concretely, it contains the coordinates (U, V) of the transformed unit
127 * vectors u = / 1 \ and v = / 0 \:
128 * \ 0 / \ 1 /
129 *
130 * / Ux Vx \
131 * \ Uy Vy /
132 *
133 * It is guaranteed that:
134 * - both U and V are unit vectors (this matrix does not change the scaling);
135 * - only one of their components have a non-zero value (they may not be
136 * oblique); in other words, here are the possible values for U and V:
137 *
138 * / 0 \ or / 0 \ or / 1 \ or / -1 \
139 * \ -1 / \ 1 / \ 0 / \ 0 /
140 *
141 * - U and V are orthogonal.
142 *
143 * Therefore, there are 8 possible matrices (4 possible rotations, flipped or
144 * not).
145 *
146 * It may theoretically change on every picture (the transform matrix provided
147 * by Android may change). If it has changed since the last picture, then
148 * pic->mtx_has_changed is true.
149 */
150static inline void
152 float direction[static 2*2])
153{
154 /**
155 * The direction matrix is extracted from pic->mtx:
156 *
157 * mtx = / a b c \
158 * \ d e f /
159 *
160 * The last column (the offset part of the affine transformation) is
161 * discarded, and the 2 remaining column vectors are normalized to remove
162 * any scaling:
163 *
164 * direction = / a/unorm b/vnorm \
165 * \ d/unorm e/vnorm /
166 *
167 * where unorm = norm( / a \ ) and vnorm = norm( / b \ ).
168 * \ d / \ e /
169 */
170
171 float ux = pic->mtx[0];
172 float uy = pic->mtx[1];
173 float vx = pic->mtx[2];
174 float vy = pic->mtx[3];
175
176 float unorm = sqrt(ux * ux + uy * uy);
177 float vnorm = sqrt(vx * vx + vy * vy);
178
179 direction[0] = ux / unorm;
180 direction[1] = uy / unorm;
181 direction[2] = vx / vnorm;
182 direction[3] = vy / vnorm;
183}
184
185#endif /* VLC_GL_PICTURE_H */
#define PICTURE_PLANE_MAX
Maximum number of plane for a picture.
Definition vlc_picture.h:67
An OpenGL picture state, with textures and a coordinate transformation matrix.
Definition vlc_opengl_picture.h:40
bool mtx_has_changed
Indicate if the transform to convert picture coordinates to textures coordinates have changed due to ...
Definition vlc_opengl_picture.h:69
float mtx[2 *3]
Matrix to convert from 2D pictures coordinates to texture coordinates.
Definition vlc_opengl_picture.h:57
uint32_t textures[(5)]
Texture handles for each plane.
Definition vlc_opengl_picture.h:44
This file is a collection of common definitions and types.
#define MTX(ROW, COL)
static void vlc_gl_picture_ComputeDirectionMatrix(const struct vlc_gl_picture *pic, float direction[static 2 *2])
Return a matrix to orient texture coordinates.
Definition vlc_opengl_picture.h:152
static void vlc_gl_picture_ToTexCoords(const struct vlc_gl_picture *pic, unsigned coords_count, const float *pic_coords, float *tex_coords_out)
Convert from picture coordinates to texture coordinates, which can be used to sample at the correct l...
Definition vlc_opengl_picture.h:89
This file defines picture structures and functions in vlc.