34 #define WIN32_LEAN_AND_MEAN
38 #include <OpenGL/gl3.h>
45 #if HAVE_GLXGETPROCADDRESS
76 #define FF_GL_RED_COMPONENT GL_RED
77 #elif defined(GL_LUMINANCE)
78 #define FF_GL_RED_COMPONENT GL_LUMINANCE
80 #define FF_GL_RED_COMPONENT 0x1903; //GL_RED
84 #define FF_GL_UNSIGNED_BYTE_3_3_2 0x8032
85 #define FF_GL_UNSIGNED_BYTE_2_3_3_REV 0x8362
86 #define FF_GL_UNSIGNED_SHORT_1_5_5_5_REV 0x8366
87 #define FF_GL_UNPACK_ROW_LENGTH 0x0CF2
90 #define FF_GL_ARRAY_BUFFER 0x8892
91 #define FF_GL_ELEMENT_ARRAY_BUFFER 0x8893
92 #define FF_GL_STATIC_DRAW 0x88E4
93 #define FF_GL_FRAGMENT_SHADER 0x8B30
94 #define FF_GL_VERTEX_SHADER 0x8B31
95 #define FF_GL_COMPILE_STATUS 0x8B81
96 #define FF_GL_LINK_STATUS 0x8B82
97 #define FF_GL_INFO_LOG_LENGTH 0x8B84
98 typedef void (
APIENTRY *FF_PFNGLACTIVETEXTUREPROC) (GLenum texture);
152 #define OPENGL_ERROR_CHECK(ctx) \
155 if ((err_code = glGetError()) != GL_NO_ERROR) { \
156 av_log(ctx, AV_LOG_ERROR, "OpenGL error occurred in '%s', line %d: %d\n", __FUNCTION__, __LINE__, err_code); \
178 SDL_Surface *surface;
347 opengl->surface = SDL_SetVideoMode(width, height,
348 32, SDL_OPENGL | SDL_RESIZABLE);
349 if (!opengl->surface) {
353 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
354 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
355 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
356 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
357 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
367 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS) > 0) {
368 switch (event.type) {
372 switch (event.key.keysym.sym) {
378 case SDL_VIDEORESIZE: {
383 SDL_VideoDriverName(buffer,
sizeof(buffer));
388 if ((ret = opengl_sdl_recreate_window(opengl, event.resize.w, event.resize.h)) < 0)
392 message.
width = opengl->surface->w;
393 message.
height = opengl->surface->h;
407 if (SDL_Init(SDL_INIT_VIDEO)) {
411 if ((ret = opengl_sdl_recreate_window(opengl, opengl->
window_width,
414 av_log(opengl,
AV_LOG_INFO,
"SDL driver: '%s'.\n", SDL_VideoDriverName(buffer,
sizeof(buffer)));
415 message.
width = opengl->surface->w;
416 message.
height = opengl->surface->h;
426 #define LOAD_OPENGL_FUN(name, type) \
427 procs->name = (type)SDL_GL_GetProcAddress(#name); \
428 if (!procs->name) { \
429 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
430 return AVERROR(ENOSYS); \
434 LOAD_OPENGL_FUN(glGenBuffers, FF_PFNGLGENBUFFERSPROC)
435 LOAD_OPENGL_FUN(glDeleteBuffers, FF_PFNGLDELETEBUFFERSPROC)
436 LOAD_OPENGL_FUN(glBufferData, FF_PFNGLBUFFERDATAPROC)
437 LOAD_OPENGL_FUN(glBindBuffer, FF_PFNGLBINDBUFFERPROC)
438 LOAD_OPENGL_FUN(glGetAttribLocation, FF_PFNGLGETATTRIBLOCATIONPROC)
439 LOAD_OPENGL_FUN(glGetUniformLocation, FF_PFNGLGETUNIFORMLOCATIONPROC)
440 LOAD_OPENGL_FUN(glUniform1f, FF_PFNGLUNIFORM1FPROC)
441 LOAD_OPENGL_FUN(glUniform1i, FF_PFNGLUNIFORM1IPROC)
442 LOAD_OPENGL_FUN(glUniformMatrix4fv, FF_PFNGLUNIFORMMATRIX4FVPROC)
443 LOAD_OPENGL_FUN(glCreateProgram, FF_PFNGLCREATEPROGRAMPROC)
444 LOAD_OPENGL_FUN(glDeleteProgram, FF_PFNGLDELETEPROGRAMPROC)
445 LOAD_OPENGL_FUN(glUseProgram, FF_PFNGLUSEPROGRAMPROC)
446 LOAD_OPENGL_FUN(glLinkProgram, FF_PFNGLLINKPROGRAMPROC)
447 LOAD_OPENGL_FUN(glGetProgramiv, FF_PFNGLGETPROGRAMIVPROC)
448 LOAD_OPENGL_FUN(glGetProgramInfoLog, FF_PFNGLGETPROGRAMINFOLOGPROC)
449 LOAD_OPENGL_FUN(glAttachShader, FF_PFNGLATTACHSHADERPROC)
450 LOAD_OPENGL_FUN(glCreateShader, FF_PFNGLCREATESHADERPROC)
451 LOAD_OPENGL_FUN(glDeleteShader, FF_PFNGLDELETESHADERPROC)
452 LOAD_OPENGL_FUN(glCompileShader, FF_PFNGLCOMPILESHADERPROC)
453 LOAD_OPENGL_FUN(glShaderSource, FF_PFNGLSHADERSOURCEPROC)
454 LOAD_OPENGL_FUN(glGetShaderiv, FF_PFNGLGETSHADERIVPROC)
455 LOAD_OPENGL_FUN(glGetShaderInfoLog, FF_PFNGLGETSHADERINFOLOGPROC)
456 LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
457 LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
461 #undef LOAD_OPENGL_FUN
465 #if defined(__APPLE__)
472 return opengl_sdl_load_procedures(opengl);
507 #if HAVE_GLXGETPROCADDRESS
508 #define SelectedGetProcAddress glXGetProcAddress
509 #elif HAVE_WGLGETPROCADDRESS
510 #define SelectedGetProcAddress wglGetProcAddress
513 #define LOAD_OPENGL_FUN(name, type) \
514 procs->name = (type)SelectedGetProcAddress(#name); \
515 if (!procs->name) { \
516 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
517 return AVERROR(ENOSYS); \
522 return opengl_sdl_load_procedures(opengl);
548 LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
549 LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
553 #undef SelectedGetProcAddress
554 #undef LOAD_OPENGL_FUN
560 memset(matrix, 0, 16 *
sizeof(
float));
561 matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0f;
565 float bottom,
float top,
float nearZ,
float farZ)
567 float ral = right + left;
568 float rsl = right - left;
569 float tab = top + bottom;
570 float tsb = top - bottom;
571 float fan = farZ + nearZ;
572 float fsn = farZ - nearZ;
574 memset(matrix, 0, 16 *
sizeof(
float));
575 matrix[0] = 2.0f / rsl;
576 matrix[5] = 2.0f / tsb;
577 matrix[10] = -2.0f / fsn;
578 matrix[12] = -ral / rsl;
579 matrix[13] = -tab / tsb;
580 matrix[14] = -fan / fsn;
587 const char *extension;
590 } required_extensions[] = {
591 {
"GL_ARB_multitexture", 1, 3 },
592 {
"GL_ARB_vertex_buffer_object", 1, 5 },
593 {
"GL_ARB_vertex_shader", 2, 0 },
594 {
"GL_ARB_fragment_shader", 2, 0 },
595 {
"GL_ARB_shader_objects", 2, 0 },
599 const char *extensions, *
version;
601 version = glGetString(GL_VERSION);
602 extensions = glGetString(GL_EXTENSIONS);
605 sscanf(version,
"%d.%d", &major, &minor);
607 for (i = 0; required_extensions[i].extension; i++) {
608 if (major < required_extensions[i].major &&
609 (major == required_extensions[i].major && minor < required_extensions[i].minor) &&
610 !strstr(extensions, required_extensions[i].extension)) {
612 required_extensions[i].extension);
619 opengl->
non_pow_2_textures = major >= 2 || strstr(extensions,
"GL_ARB_texture_non_power_of_two");
620 #if defined(GL_ES_VERSION_2_0)
621 opengl->
unpack_subimage = !!strstr(extensions,
"GL_EXT_unpack_subimage");
651 case GL_UNSIGNED_SHORT:
653 case GL_UNSIGNED_SHORT_5_6_5:
655 case GL_UNSIGNED_BYTE:
701 int *out_width,
int *out_height)
704 *out_width = in_width;
705 *out_height = in_height;
708 unsigned power_of_2 = 1;
709 while (power_of_2 < max)
711 *out_height = power_of_2;
712 *out_width = power_of_2;
714 in_width, in_height, *out_width, *out_height);
735 #define FILL_COMPONENT(i) { \
736 shift = desc->comp[i].depth_minus1 >> 3; \
737 opengl->color_map[(i << 2) + ((desc->comp[i].offset_plus1 - 1) >> shift)] = 1.0; \
747 #undef FILL_COMPONENT
786 if (!fragment_shader_code) {
799 fragment_shader_code);
851 GLsizei width, GLsizei height)
856 glBindTexture(GL_TEXTURE_2D, texture);
857 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
858 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
859 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
860 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
861 glTexImage2D(GL_TEXTURE_2D, 0, opengl->
format, new_width, new_height, 0,
921 for (i = 0; i < 4; i++)
947 if ((ret = opengl_sdl_create_window(h)) < 0) {
952 av_log(opengl,
AV_LOG_ERROR,
"FFmpeg is compiled without SDL. Cannot create default window.\n");
957 message.
x = message.
y = 0;
961 &message ,
sizeof(message))) < 0) {
1032 for (i = 1; i < num_planes; i++)
1048 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1051 (
float)opengl->
background[2] / 255.0f, 1.0f);
1110 glClear(GL_COLOR_BUFFER_BIT);
1114 SDL_GL_SwapBuffers();
1140 int plane = desc->
comp[comp_index].
plane;
1146 data += opengl->
width * opengl->
height * wordsize;
1149 data += opengl->
width * opengl->
height * wordsize;
1150 data += width_chroma * height_chroma * wordsize;
1153 data += opengl->
width * opengl->
height * wordsize;
1154 data += 2 * width_chroma * height_chroma * wordsize;
1162 #define LOAD_TEXTURE_DATA(comp_index, sub) \
1164 int width = sub ? FF_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width; \
1165 int height = sub ? FF_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h): opengl->height; \
1167 int plane = desc->comp[comp_index].plane; \
1169 glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]); \
1171 GLint length = ((AVFrame *)input)->linesize[plane]; \
1172 int bytes_per_pixel = opengl_type_size(opengl->type); \
1173 if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) \
1174 bytes_per_pixel *= desc->nb_components; \
1175 data = ((AVFrame *)input)->data[plane]; \
1176 if (!(length % bytes_per_pixel) && \
1177 (opengl->unpack_subimage || ((length / bytes_per_pixel) == width))) { \
1178 length /= bytes_per_pixel; \
1179 if (length != width) \
1180 glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, length); \
1181 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
1182 opengl->format, opengl->type, data); \
1183 if (length != width) \
1184 glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, 0); \
1187 for (h = 0; h < height; h++) { \
1188 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, h, width, 1, \
1189 opengl->format, opengl->type, data); \
1194 data = opengl_get_plane_pointer(opengl, input, comp_index, desc); \
1195 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
1196 opengl->format, opengl->type, data); \
1208 if (!opengl->
no_window && (ret = opengl_sdl_process_events(h)) < 0)
1217 glClear(GL_COLOR_BUFFER_BIT);
1221 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1250 SDL_GL_SwapBuffers();
1276 #define OFFSET(x) offsetof(OpenGLContext, x)
1277 #define ENC AV_OPT_FLAG_ENCODING_PARAM
1306 .priv_class = &opengl_class,