gltransition

gl-transitions 使用glsl定义了转场框架, 使用者在框架内实现转场算法/效果.

它的展示站点https://gl-transitions.com/使用webgl展示了这些转场效果.

一个简单的fade效果实现如下:

1
2
3
4
5
6
7
8
// transition of a simple fade.
vec4 transition (vec2 uv) {
  return mix(
    getFromColor(uv),
    getToColor(uv),
    progress
  );
}

转场效果只用实现glsl函数vec4 transition (vec2 uv), 输入是vec2像素坐标,输出是vec4像素. 算法可以使用的上下文包括progress,转场进度等参数.

ffmpeg集成

ffmpeg-gl-transition项目将gl-transitions转场框架作为一个filter集成到了ffmpeg中, 集成后可以通过ffmpeg filter使用到gl-transitions内众多的转场效果处理视频转场.

纯CPU渲染

如果是在linux服务端,headless且无GPU的情况下,想要使用ffmpeg-gl-transition, 那么可以依赖于software opengl环境.

有两种方式,一种是使用xvfb,这是一个虚拟的X server, 它内置了cpu模拟的opengl渲染环境, 但是运行时需要套上一层xvfb-run.

第二种方式是直接使用osmesa, osmesa是linux下实现的一套software opengl, 后端渲染引擎是llvm-pipe, 使用多线程方式模拟GPU管线计算. 实际上xvfb内部也使用了这一套软件OPENGL实现.

要支持纯osmesa渲染,只要将ffmpeg-gl-transition项目实现的vf_gltransition.c改动一下即可,将OPENGL环境的初始化

diff如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
--- vf_gltransition.c		2019-08-31 18:16:04.424219700 +0800
+++ vf_gltransitionosmesa.c	2022-08-31 12:54:50.119997700 +0800
@@ -8,24 +8,7 @@
 #include "internal.h"
 #include "framesync.h"
 
-#ifndef __APPLE__
-# define GL_TRANSITION_USING_EGL //remove this line if you don't want to use EGL
-#endif
-
-#ifdef __APPLE__
-# define __gl_h_
-# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
-# include <OpenGL/gl3.h>
-#else
-# include <GL/glew.h>
-#endif
-
-#ifdef GL_TRANSITION_USING_EGL
-# include <EGL/egl.h>
-# include <EGL/eglext.h>
-#else
-# include <GLFW/glfw3.h>
-#endif
+#include "GL/osmesa.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -36,16 +19,6 @@
 
 #define PIXEL_FORMAT (GL_RGB)
 
-#ifdef GL_TRANSITION_USING_EGL
-static const EGLint configAttribs[] = {
-    EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
-    EGL_BLUE_SIZE, 8,
-    EGL_GREEN_SIZE, 8,
-    EGL_RED_SIZE, 8,
-    EGL_DEPTH_SIZE, 8,
-    EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
-    EGL_NONE};
-#endif
 static const float position[12] = {
   -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f
 };
@@ -114,14 +87,10 @@
   // internal state
   GLuint        posBuf;
   GLuint        program;
-#ifdef GL_TRANSITION_USING_EGL
-  EGLDisplay eglDpy;
-  EGLConfig eglCfg;
-  EGLSurface eglSurf;
-  EGLContext eglCtx;
-#else
-  GLFWwindow    *window;
-#endif
+
+  //
+  OSMesaContext osmesa_ctx;
+  void* buffer;
 
   GLchar *f_shader_source;
 } GLTransitionContext;
@@ -130,7 +99,7 @@
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption gltransition_options[] = {
-  { "duration", "transition duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, DBL_MAX, FLAGS },
+  { "duration", "transition duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, DBL_MAX, FLAGS },
   { "offset", "delay before startingtransition in seconds", OFFSET(offset), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0, DBL_MAX, FLAGS },
   { "source", "path to the gl-transition source file (defaults to basic fade)", OFFSET(source), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
   {NULL}
@@ -269,7 +238,8 @@
   GLTransitionContext *c = ctx->priv;
 
   c->progress = glGetUniformLocation(c->program, "progress");
-  glUniform1f(c->progress, 0.0f);
+  float progress_init = 0.0;
+  glUniform1fv(c->progress, 1, &progress_init);
 
   // TODO: this should be output ratio
   c->ratio = glGetUniformLocation(c->program, "ratio");
@@ -289,63 +259,24 @@
   GLTransitionContext *c = ctx->priv;
 
 
-#ifdef GL_TRANSITION_USING_EGL
-  //init EGL
-  // 1. Initialize EGL
-  // c->eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-
-  #define MAX_DEVICES 4
-  EGLDeviceEXT eglDevs[MAX_DEVICES];
-  EGLint numDevices;
-
-  PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT =(PFNEGLQUERYDEVICESEXTPROC)
-  eglGetProcAddress("eglQueryDevicesEXT");
-
-  eglQueryDevicesEXT(MAX_DEVICES, eglDevs, &numDevices);
-
-  PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =  (PFNEGLGETPLATFORMDISPLAYEXTPROC)
-  eglGetProcAddress("eglGetPlatformDisplayEXT");
-
-  c->eglDpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, eglDevs[0], 0);
-
-  EGLint major, minor;
-  eglInitialize(c->eglDpy, &major, &minor);
-  av_log(ctx, AV_LOG_DEBUG, "%d%d", major, minor);
-  // 2. Select an appropriate configuration
-  EGLint numConfigs;
-  EGLint pbufferAttribs[] = {
-      EGL_WIDTH,
-      inLink->w,
-      EGL_HEIGHT,
-      inLink->h,
-      EGL_NONE,
-  };
-  eglChooseConfig(c->eglDpy, configAttribs, &c->eglCfg, 1, &numConfigs);
-  // 3. Create a surface
-  c->eglSurf = eglCreatePbufferSurface(c->eglDpy, c->eglCfg,
-                                       pbufferAttribs);
-  // 4. Bind the API
-  eglBindAPI(EGL_OPENGL_API);
-  // 5. Create a context and make it current
-  c->eglCtx = eglCreateContext(c->eglDpy, c->eglCfg, EGL_NO_CONTEXT, NULL);
-  eglMakeCurrent(c->eglDpy, c->eglSurf, c->eglSurf, c->eglCtx);
-#else
-  //glfw
-
-  glfwWindowHint(GLFW_VISIBLE, 0);
-  c->window = glfwCreateWindow(inLink->w, inLink->h, "", NULL, NULL);
-  if (!c->window) {
-    av_log(ctx, AV_LOG_ERROR, "setup_gl ERROR");
-    return -1;
+
+  c->osmesa_ctx = OSMesaCreateContext(OSMESA_RGBA, NULL);
+
+  if (!c->osmesa_ctx) {
+      printf("OSMesaCreateContext failed!\n");
+      return 0;
   }
-  glfwMakeContextCurrent(c->window);
 
-#endif
+  c->buffer = malloc( inLink->w* inLink->h* 4 * sizeof(GLubyte) );
+  if (!c->buffer) {
+      printf("Alloc image buffer failed!\n");
+      return 0;
+  }
 
-#ifndef __APPLE__
-  glewExperimental = GL_TRUE;
-  glewInit();
-#endif
+  if (!OSMesaMakeCurrent(c->osmesa_ctx, c->buffer, GL_UNSIGNED_BYTE, inLink->w, inLink->h)) {
+      printf("OSMesaMakeCurrent failed!\n");
+      return 0;
+  }
 
   glViewport(0, 0, inLink->w, inLink->h);
 
@@ -380,18 +311,20 @@
 
   av_frame_copy_props(outFrame, fromFrame);
 
-#ifdef GL_TRANSITION_USING_EGL
-  eglMakeCurrent(c->eglDpy, c->eglSurf, c->eglSurf, c->eglCtx);
-#else
-  glfwMakeContextCurrent(c->window);
-#endif
+
+  if (!OSMesaMakeCurrent( c->osmesa_ctx, c->buffer, GL_UNSIGNED_BYTE, outLink->w, outLink->h)) {
+      printf("OSMesaMakeCurrent failed!\n");
+      return 0;
+  }
 
   glUseProgram(c->program);
 
   const float ts = ((fs->pts - c->first_pts) / (float)fs->time_base.den) - c->offset;
   const float progress = FFMAX(0.0f, FFMIN(1.0f, ts / c->duration));
-  // av_log(ctx, AV_LOG_ERROR, "transition '%s' %llu %f %f\n", c->source, fs->pts - c->first_pts, ts, progress);
-  glUniform1f(c->progress, progress);
+  //av_log(ctx, AV_LOG_ERROR, "transition '%s' %llu %f %f %d\n", c->source, fs->pts - c->first_pts, ts, progress, c->progress);
+  glUniform1fv(c->progress, 1, &progress);
+  float pp = -1.0;
+  glGetUniformfv(c->program, c->progress, &pp);
 
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
@@ -454,12 +387,6 @@
   c->first_pts = AV_NOPTS_VALUE;
 
 
-#ifndef GL_TRANSITION_USING_EGL
-  if (!glfwInit())
-  {
-    return -1;
-  }
-#endif
 
   return 0;
 }
@@ -468,23 +395,13 @@
   GLTransitionContext *c = ctx->priv;
   ff_framesync_uninit(&c->fs);
 
-#ifdef GL_TRANSITION_USING_EGL
-  if (c->eglDpy) {
-    glDeleteTextures(1, &c->from);
-    glDeleteTextures(1, &c->to);
-    glDeleteBuffers(1, &c->posBuf);
-    glDeleteProgram(c->program);
-    eglTerminate(c->eglDpy);
-  }
-#else
-  if (c->window) {
-    glDeleteTextures(1, &c->from);
-    glDeleteTextures(1, &c->to);
-    glDeleteBuffers(1, &c->posBuf);
-    glDeleteProgram(c->program);
-    glfwDestroyWindow(c->window);
-  }
-#endif
+  glDeleteTextures(1, &c->from);
+  glDeleteTextures(1, &c->to);
+  glDeleteBuffers(1, &c->posBuf);
+  glDeleteProgram(c->program);
+
+  OSMesaDestroyContext(c->osmesa_ctx);
+  free(c->buffer);
 
   if (c->f_shader_source) {
     av_freep(&c->f_shader_source);