34 #include <OpenGL/gl3.h>
41 #if HAVE_GLXGETPROCADDRESS
75 #define FF_GL_RED_COMPONENT GL_RED
76 #elif defined(GL_LUMINANCE)
77 #define FF_GL_RED_COMPONENT GL_LUMINANCE
79 #define FF_GL_RED_COMPONENT 0x1903; //GL_RED
83 #define FF_GL_UNSIGNED_BYTE_3_3_2 0x8032
84 #define FF_GL_UNSIGNED_BYTE_2_3_3_REV 0x8362
85 #define FF_GL_UNSIGNED_SHORT_1_5_5_5_REV 0x8366
86 #define FF_GL_UNPACK_ROW_LENGTH 0x0CF2
89 #define FF_GL_ARRAY_BUFFER 0x8892
90 #define FF_GL_ELEMENT_ARRAY_BUFFER 0x8893
91 #define FF_GL_STATIC_DRAW 0x88E4
92 #define FF_GL_FRAGMENT_SHADER 0x8B30
93 #define FF_GL_VERTEX_SHADER 0x8B31
94 #define FF_GL_COMPILE_STATUS 0x8B81
95 #define FF_GL_LINK_STATUS 0x8B82
96 #define FF_GL_INFO_LOG_LENGTH 0x8B84
97 typedef void (
APIENTRY *FF_PFNGLACTIVETEXTUREPROC) (GLenum texture);
151 #define OPENGL_ERROR_CHECK(ctx) \
154 if ((err_code = glGetError()) != GL_NO_ERROR) { \
155 av_log(ctx, AV_LOG_ERROR, "OpenGL error occurred in '%s', line %d: %d\n", __FUNCTION__, __LINE__, err_code); \
177 SDL_Surface *surface;
346 opengl->surface = SDL_SetVideoMode(width, height,
347 32, SDL_OPENGL | SDL_RESIZABLE);
348 if (!opengl->surface) {
352 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
353 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
354 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
355 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
356 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
366 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS) > 0) {
367 switch (event.type) {
371 switch (event.key.keysym.sym) {
377 case SDL_VIDEORESIZE: {
382 SDL_VideoDriverName(buffer,
sizeof(buffer));
387 if ((ret = opengl_sdl_recreate_window(opengl, event.resize.w, event.resize.h)) < 0)
391 message.
width = opengl->surface->w;
392 message.
height = opengl->surface->h;
406 if (SDL_Init(SDL_INIT_VIDEO)) {
410 if ((ret = opengl_sdl_recreate_window(opengl, opengl->
width, opengl->
height)) < 0)
412 av_log(opengl,
AV_LOG_INFO,
"SDL driver: '%s'.\n", SDL_VideoDriverName(buffer,
sizeof(buffer)));
413 message.
width = opengl->surface->w;
414 message.
height = opengl->surface->h;
424 #define LOAD_OPENGL_FUN(name, type) \
425 procs->name = (type)SDL_GL_GetProcAddress(#name); \
426 if (!procs->name) { \
427 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
428 return AVERROR(ENOSYS); \
432 LOAD_OPENGL_FUN(glGenBuffers, FF_PFNGLGENBUFFERSPROC)
433 LOAD_OPENGL_FUN(glDeleteBuffers, FF_PFNGLDELETEBUFFERSPROC)
434 LOAD_OPENGL_FUN(glBufferData, FF_PFNGLBUFFERDATAPROC)
435 LOAD_OPENGL_FUN(glBindBuffer, FF_PFNGLBINDBUFFERPROC)
436 LOAD_OPENGL_FUN(glGetAttribLocation, FF_PFNGLGETATTRIBLOCATIONPROC)
437 LOAD_OPENGL_FUN(glGetUniformLocation, FF_PFNGLGETUNIFORMLOCATIONPROC)
438 LOAD_OPENGL_FUN(glUniform1f, FF_PFNGLUNIFORM1FPROC)
439 LOAD_OPENGL_FUN(glUniform1i, FF_PFNGLUNIFORM1IPROC)
440 LOAD_OPENGL_FUN(glUniformMatrix4fv, FF_PFNGLUNIFORMMATRIX4FVPROC)
441 LOAD_OPENGL_FUN(glCreateProgram, FF_PFNGLCREATEPROGRAMPROC)
442 LOAD_OPENGL_FUN(glDeleteProgram, FF_PFNGLDELETEPROGRAMPROC)
443 LOAD_OPENGL_FUN(glUseProgram, FF_PFNGLUSEPROGRAMPROC)
444 LOAD_OPENGL_FUN(glLinkProgram, FF_PFNGLLINKPROGRAMPROC)
445 LOAD_OPENGL_FUN(glGetProgramiv, FF_PFNGLGETPROGRAMIVPROC)
446 LOAD_OPENGL_FUN(glGetProgramInfoLog, FF_PFNGLGETPROGRAMINFOLOGPROC)
447 LOAD_OPENGL_FUN(glAttachShader, FF_PFNGLATTACHSHADERPROC)
448 LOAD_OPENGL_FUN(glCreateShader, FF_PFNGLCREATESHADERPROC)
449 LOAD_OPENGL_FUN(glDeleteShader, FF_PFNGLDELETESHADERPROC)
450 LOAD_OPENGL_FUN(glCompileShader, FF_PFNGLCOMPILESHADERPROC)
451 LOAD_OPENGL_FUN(glShaderSource, FF_PFNGLSHADERSOURCEPROC)
452 LOAD_OPENGL_FUN(glGetShaderiv, FF_PFNGLGETSHADERIVPROC)
453 LOAD_OPENGL_FUN(glGetShaderInfoLog, FF_PFNGLGETSHADERINFOLOGPROC)
454 LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
455 LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
459 #undef LOAD_OPENGL_FUN
463 #if defined(__APPLE__)
470 return opengl_sdl_load_procedures(opengl);
505 #if HAVE_GLXGETPROCADDRESS
506 #define SelectedGetProcAddress glXGetProcAddress
507 #elif HAVE_WGLGETPROCADDRESS
508 #define SelectedGetProcAddress wglGetProcAddress
511 #define LOAD_OPENGL_FUN(name, type) \
512 procs->name = (type)SelectedGetProcAddress(#name); \
513 if (!procs->name) { \
514 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
515 return AVERROR(ENOSYS); \
520 return opengl_sdl_load_procedures(opengl);
546 LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
547 LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
551 #undef SelectedGetProcAddress
552 #undef LOAD_OPENGL_FUN
558 memset(matrix, 0, 16 *
sizeof(
float));
559 matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0f;
563 float bottom,
float top,
float nearZ,
float farZ)
565 float ral = right + left;
566 float rsl = right - left;
567 float tab = top + bottom;
568 float tsb = top - bottom;
569 float fan = farZ + nearZ;
570 float fsn = farZ - nearZ;
572 memset(matrix, 0, 16 *
sizeof(
float));
573 matrix[0] = 2.0f / rsl;
574 matrix[5] = 2.0f / tsb;
575 matrix[10] = -2.0f / fsn;
576 matrix[12] = -ral / rsl;
577 matrix[13] = -tab / tsb;
578 matrix[14] = -fan / fsn;
585 const char *extention;
588 } required_extensions[] = {
589 {
"GL_ARB_multitexture", 1, 3 },
590 {
"GL_ARB_vertex_buffer_object", 1, 5 },
591 {
"GL_ARB_vertex_shader", 2, 0 },
592 {
"GL_ARB_fragment_shader", 2, 0 },
593 {
"GL_ARB_shader_objects", 2, 0 },
597 const char *extensions, *
version;
599 version = glGetString(GL_VERSION);
600 extensions = glGetString(GL_EXTENSIONS);
603 sscanf(version,
"%d.%d", &major, &minor);
605 for (i = 0; required_extensions[i].extention; i++) {
606 if (major < required_extensions[i].major &&
607 (major == required_extensions[i].major && minor < required_extensions[i].minor) &&
608 !strstr(extensions, required_extensions[i].extention)) {
610 required_extensions[i].extention);
617 opengl->
non_pow_2_textures = major >= 2 || strstr(extensions,
"GL_ARB_texture_non_power_of_two");
618 #if defined(GL_ES_VERSION_2_0)
619 opengl->
unpack_subimage = !!strstr(extensions,
"GL_EXT_unpack_subimage");
649 case GL_UNSIGNED_SHORT:
651 case GL_UNSIGNED_SHORT_5_6_5:
653 case GL_UNSIGNED_BYTE:
699 int *out_width,
int *out_height)
702 *out_width = in_width;
703 *out_height = in_height;
706 unsigned power_of_2 = 1;
707 while (power_of_2 < max)
709 *out_height = power_of_2;
710 *out_width = power_of_2;
712 in_width, in_height, *out_width, *out_height);
733 #define FILL_COMPONENT(i) { \
734 shift = desc->comp[i].depth_minus1 >> 3; \
735 opengl->color_map[(i << 2) + ((desc->comp[i].offset_plus1 - 1) >> shift)] = 1.0; \
745 #undef FILL_COMPONENT
784 if (!fragment_shader_code) {
797 fragment_shader_code);
849 GLsizei width, GLsizei height)
854 glBindTexture(GL_TEXTURE_2D, texture);
855 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
856 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
857 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
858 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
859 glTexImage2D(GL_TEXTURE_2D, 0, opengl->
format, new_width, new_height, 0,
919 for (i = 0; i < 4; i++)
945 if ((ret = opengl_sdl_create_window(h)) < 0) {
950 av_log(opengl,
AV_LOG_ERROR,
"FFmpeg is compiled without SDL. Cannot create default window.\n");
1025 for (i = 1; i < num_planes; i++)
1041 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1044 (
float)opengl->
background[2] / 255.0f, 1.0f);
1099 glClear(GL_COLOR_BUFFER_BIT);
1103 SDL_GL_SwapBuffers();
1127 int plane = desc->
comp[comp_index].
plane;
1133 data += opengl->
width * opengl->
height * wordsize;
1136 data += opengl->
width * opengl->
height * wordsize;
1137 data += width_chroma * height_chroma * wordsize;
1140 data += opengl->
width * opengl->
height * wordsize;
1141 data += 2 * width_chroma * height_chroma * wordsize;
1149 #define LOAD_TEXTURE_DATA(comp_index, sub) \
1151 int width = sub ? FF_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width; \
1152 int height = sub ? FF_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h): opengl->height; \
1154 int plane = desc->comp[comp_index].plane; \
1156 glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]); \
1158 GLint length = ((AVFrame *)input)->linesize[plane]; \
1159 int bytes_per_pixel = opengl_type_size(opengl->type); \
1160 if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) \
1161 bytes_per_pixel *= desc->nb_components; \
1162 data = ((AVFrame *)input)->data[plane]; \
1163 if (!(length % bytes_per_pixel) && \
1164 (opengl->unpack_subimage || ((length / bytes_per_pixel) == width))) { \
1165 length /= bytes_per_pixel; \
1166 if (length != width) \
1167 glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, length); \
1168 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
1169 opengl->format, opengl->type, data); \
1170 if (length != width) \
1171 glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, 0); \
1174 for (h = 0; h < height; h++) { \
1175 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, h, width, 1, \
1176 opengl->format, opengl->type, data); \
1181 data = opengl_get_plane_pointer(opengl, input, comp_index, desc); \
1182 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
1183 opengl->format, opengl->type, data); \
1195 if (!opengl->
no_window && (ret = opengl_sdl_process_events(h)) < 0)
1204 glClear(GL_COLOR_BUFFER_BIT);
1235 SDL_GL_SwapBuffers();
1261 #define OFFSET(x) offsetof(OpenGLContext, x)
1262 #define ENC AV_OPT_FLAG_ENCODING_PARAM
1289 .priv_class = &opengl_class,