Lines Matching defs:opengl
281 static av_cold int opengl_init_context(OpenGLContext *opengl);
283 static av_cold void opengl_deinit_context(OpenGLContext *opengl)
285 glDeleteTextures(4, opengl->texture_name);
286 opengl->texture_name[0] = opengl->texture_name[1] =
287 opengl->texture_name[2] = opengl->texture_name[3] = 0;
288 if (opengl->glprocs.glUseProgram)
289 opengl->glprocs.glUseProgram(0);
290 if (opengl->glprocs.glDeleteProgram) {
291 opengl->glprocs.glDeleteProgram(opengl->program);
292 opengl->program = 0;
294 if (opengl->glprocs.glDeleteShader) {
295 opengl->glprocs.glDeleteShader(opengl->vertex_shader);
296 opengl->glprocs.glDeleteShader(opengl->fragment_shader);
297 opengl->vertex_shader = opengl->fragment_shader = 0;
299 if (opengl->glprocs.glBindBuffer) {
300 opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, 0);
301 opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, 0);
303 if (opengl->glprocs.glDeleteBuffers) {
304 opengl->glprocs.glDeleteBuffers(2, &opengl->index_buffer);
305 opengl->vertex_buffer = opengl->index_buffer = 0;
312 OpenGLContext *opengl = h->priv_data;
313 opengl->window_width = width;
314 opengl->window_height = height;
315 if (opengl->inited) {
316 if (opengl->no_window &&
318 av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
331 OpenGLContext *opengl = h->priv_data;
340 return opengl_resize(h, opengl->window_width, opengl->window_height);
348 OpenGLContext *opengl = h->priv_data;
367 SDL_GL_GetDrawableSize(opengl->window, &message.width, &message.height);
379 OpenGLContext *opengl = h->priv_data;
382 av_log(opengl, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
385 opengl->window = SDL_CreateWindow(opengl->window_title,
388 opengl->window_width, opengl->window_height,
390 if (!opengl->window) {
391 av_log(opengl, AV_LOG_ERROR, "Unable to create default window: %s\n", SDL_GetError());
394 opengl->glcontext = SDL_GL_CreateContext(opengl->window);
395 if (!opengl->glcontext) {
396 av_log(opengl, AV_LOG_ERROR, "Unable to create OpenGL context on default window: %s\n", SDL_GetError());
404 av_log(opengl, AV_LOG_INFO, "SDL driver: '%s'.\n", SDL_GetCurrentVideoDriver());
405 SDL_GL_GetDrawableSize(opengl->window, &message.width, &message.height);
409 static int av_cold opengl_sdl_load_procedures(OpenGLContext *opengl)
411 FFOpenGLFunctions *procs = &opengl->glprocs;
416 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
453 static int av_cold opengl_load_procedures(OpenGLContext *opengl)
455 FFOpenGLFunctions *procs = &opengl->glprocs;
458 if (!opengl->no_window)
459 return opengl_sdl_load_procedures(opengl);
490 static int av_cold opengl_load_procedures(OpenGLContext *opengl)
492 FFOpenGLFunctions *procs = &opengl->glprocs;
503 av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
508 if (!opengl->no_window)
509 return opengl_sdl_load_procedures(opengl);
573 OpenGLContext *opengl = h->priv_data;
609 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &opengl->max_texture_size);
610 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &opengl->max_viewport_width);
611 opengl->non_pow_2_textures = major >= 2 || strstr(extensions, "GL_ARB_texture_non_power_of_two");
613 opengl->unpack_subimage = !!strstr(extensions, "GL_EXT_unpack_subimage");
615 opengl->unpack_subimage = 1;
618 av_log(h, AV_LOG_DEBUG, "Non Power of 2 textures support: %s\n", opengl->non_pow_2_textures ? "Yes" : "No");
619 av_log(h, AV_LOG_DEBUG, "Unpack Subimage extension support: %s\n", opengl->unpack_subimage ? "Yes" : "No");
620 av_log(h, AV_LOG_DEBUG, "Max texture size: %dx%d\n", opengl->max_texture_size, opengl->max_texture_size);
622 opengl->max_viewport_width, opengl->max_viewport_height);
624 OPENGL_ERROR_CHECK(opengl);
656 static av_cold void opengl_get_texture_params(OpenGLContext *opengl)
660 if (opengl_format_desc[i].fixel_format == opengl->pix_fmt) {
661 opengl->format = opengl_format_desc[i].format;
662 opengl->type = opengl_format_desc[i].type;
671 OpenGLContext *opengl = s->priv_data;
681 if (av_cmp_q(dar, (AVRational){ opengl->window_width, opengl->window_height }) > 0) {
683 opengl->picture_width = opengl->window_width;
684 opengl->picture_height = av_rescale(opengl->picture_width, dar.den, dar.num);
687 opengl->picture_height = opengl->window_height;
688 opengl->picture_width = av_rescale(opengl->picture_height, dar.num, dar.den);
692 static av_cold void opengl_get_texture_size(OpenGLContext *opengl, int in_width, int in_height,
695 if (opengl->non_pow_2_textures) {
699 int max = FFMIN(FFMAX(in_width, in_height), opengl->max_texture_size);
705 av_log(opengl, AV_LOG_DEBUG, "Texture size calculated from %dx%d into %dx%d\n",
710 static av_cold void opengl_fill_color_map(OpenGLContext *opengl)
714 enum AVPixelFormat pix_fmt = opengl->pix_fmt;
729 opengl->color_map[(i << 2) + (desc->comp[i].offset >> shift)] = 1.0; \
732 memset(opengl->color_map, 0, sizeof(opengl->color_map));
742 static av_cold GLuint opengl_load_shader(OpenGLContext *opengl, GLenum type, const char *source)
744 GLuint shader = opengl->glprocs.glCreateShader(type);
747 av_log(opengl, AV_LOG_ERROR, "glCreateShader() failed\n");
750 opengl->glprocs.glShaderSource(shader, 1, &source, NULL);
751 opengl->glprocs.glCompileShader(shader);
753 opengl->glprocs.glGetShaderiv(shader, FF_GL_COMPILE_STATUS, &result);
756 opengl->glprocs.glGetShaderiv(shader, FF_GL_INFO_LOG_LENGTH, &result);
759 opengl->glprocs.glGetShaderInfoLog(shader, result, NULL, log);
760 av_log(opengl, AV_LOG_ERROR, "Compile error: %s\n", log);
766 OPENGL_ERROR_CHECK(opengl);
769 opengl->glprocs.glDeleteShader(shader);
773 static av_cold int opengl_compile_shaders(OpenGLContext *opengl, enum AVPixelFormat pix_fmt)
779 av_log(opengl, AV_LOG_ERROR, "Provided pixel format '%s' is not supported\n",
784 opengl->vertex_shader = opengl_load_shader(opengl, FF_GL_VERTEX_SHADER,
786 if (!opengl->vertex_shader) {
787 av_log(opengl, AV_LOG_ERROR, "Vertex shader loading failed.\n");
790 opengl->fragment_shader = opengl_load_shader(opengl, FF_GL_FRAGMENT_SHADER,
792 if (!opengl->fragment_shader) {
793 av_log(opengl, AV_LOG_ERROR, "Fragment shader loading failed.\n");
797 opengl->program = opengl->glprocs.glCreateProgram();
798 if (!opengl->program)
801 opengl->glprocs.glAttachShader(opengl->program, opengl->vertex_shader);
802 opengl->glprocs.glAttachShader(opengl->program, opengl->fragment_shader);
803 opengl->glprocs.glLinkProgram(opengl->program);
805 opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_LINK_STATUS, &result);
808 opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_INFO_LOG_LENGTH, &result);
813 opengl->glprocs.glGetProgramInfoLog(opengl->program, result, NULL, log);
814 av_log(opengl, AV_LOG_ERROR, "Link error: %s\n", log);
820 opengl->position_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_position");
821 opengl->texture_coords_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_textureCoords");
822 opengl->projection_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_projectionMatrix");
823 opengl->model_view_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_modelViewMatrix");
824 opengl->color_map_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_colorMap");
825 opengl->texture_location[0] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture0");
826 opengl->texture_location[1] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture1");
827 opengl->texture_location[2] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture2");
828 opengl->texture_location[3] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture3");
829 opengl->chroma_div_w_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_w");
830 opengl->chroma_div_h_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_h");
832 OPENGL_ERROR_CHECK(opengl);
835 opengl->glprocs.glDeleteShader(opengl->vertex_shader);
836 opengl->glprocs.glDeleteShader(opengl->fragment_shader);
837 opengl->glprocs.glDeleteProgram(opengl->program);
838 opengl->fragment_shader = opengl->vertex_shader = opengl->program = 0;
842 static av_cold int opengl_configure_texture(OpenGLContext *opengl, GLuint texture,
847 opengl_get_texture_size(opengl, width, height, &new_width, &new_height);
853 glTexImage2D(GL_TEXTURE_2D, 0, opengl->format, new_width, new_height, 0,
854 opengl->format, opengl->type, NULL);
864 OpenGLContext *opengl = s->priv_data;
867 if (opengl->window_width > opengl->max_viewport_width || opengl->window_height > opengl->max_viewport_height) {
868 opengl->window_width = FFMIN(opengl->window_width, opengl->max_viewport_width);
869 opengl->window_height = FFMIN(opengl->window_height, opengl->max_viewport_height);
870 av_log(opengl, AV_LOG_WARNING, "Too big viewport requested, limited to %dx%d", opengl->window_width, opengl->window_height);
872 glViewport(0, 0, opengl->window_width, opengl->window_height);
873 opengl_make_ortho(opengl->projection_matrix,
874 - (float)opengl->window_width / 2.0f, (float)opengl->window_width / 2.0f,
875 - (float)opengl->window_height / 2.0f, (float)opengl->window_height / 2.0f,
877 opengl_make_identity(opengl->model_view_matrix);
881 opengl->vertex[0].z = opengl->vertex[1].z = opengl->vertex[2].z = opengl->vertex[3].z = 0.0f;
882 opengl->vertex[0].x = opengl->vertex[1].x = - (float)opengl->picture_width / 2.0f;
883 opengl->vertex[2].x = opengl->vertex[3].x = (float)opengl->picture_width / 2.0f;
884 opengl->vertex[1].y = opengl->vertex[2].y = - (float)opengl->picture_height / 2.0f;
885 opengl->vertex[0].y = opengl->vertex[3].y = (float)opengl->picture_height / 2.0f;
887 opengl_get_texture_size(opengl, opengl->width, opengl->height, &tex_w, &tex_h);
889 opengl->vertex[0].s0 = 0.0f;
890 opengl->vertex[0].t0 = 0.0f;
891 opengl->vertex[1].s0 = 0.0f;
892 opengl->vertex[1].t0 = (float)opengl->height / (float)tex_h;
893 opengl->vertex[2].s0 = (float)opengl->width / (float)tex_w;
894 opengl->vertex[2].t0 = (float)opengl->height / (float)tex_h;
895 opengl->vertex[3].s0 = (float)opengl->width / (float)tex_w;
896 opengl->vertex[3].t0 = 0.0f;
898 opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
899 opengl->glprocs.glBufferData(FF_GL_ARRAY_BUFFER, sizeof(opengl->vertex), opengl->vertex, FF_GL_STATIC_DRAW);
900 opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, 0);
901 OPENGL_ERROR_CHECK(opengl);
907 static int opengl_prepare(OpenGLContext *opengl)
910 opengl->glprocs.glUseProgram(opengl->program);
911 opengl->glprocs.glUniformMatrix4fv(opengl->projection_matrix_location, 1, GL_FALSE, opengl->projection_matrix);
912 opengl->glprocs.glUniformMatrix4fv(opengl->model_view_matrix_location, 1, GL_FALSE, opengl->model_view_matrix);
914 if (opengl->texture_location[i] != -1) {
915 opengl->glprocs.glActiveTexture(GL_TEXTURE0 + i);
916 glBindTexture(GL_TEXTURE_2D, opengl->texture_name[i]);
917 opengl->glprocs.glUniform1i(opengl->texture_location[i], i);
919 if (opengl->color_map_location != -1)
920 opengl->glprocs.glUniformMatrix4fv(opengl->color_map_location, 1, GL_FALSE, opengl->color_map);
921 if (opengl->chroma_div_h_location != -1)
922 opengl->glprocs.glUniform1f(opengl->chroma_div_h_location, opengl->chroma_div_h);
923 if (opengl->chroma_div_w_location != -1)
924 opengl->glprocs.glUniform1f(opengl->chroma_div_w_location, opengl->chroma_div_w);
926 OPENGL_ERROR_CHECK(opengl);
934 OpenGLContext *opengl = h->priv_data;
937 if (!opengl->no_window) {
940 av_log(opengl, AV_LOG_ERROR, "Cannot create default SDL window.\n");
944 av_log(opengl, AV_LOG_ERROR, "FFmpeg is compiled without SDL. Cannot create default window.\n");
950 message.width = opengl->window_width;
951 message.height = opengl->window_height;
954 av_log(opengl, AV_LOG_ERROR, "Application failed to create window buffer.\n");
958 av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
968 OpenGLContext *opengl = h->priv_data;
969 if (!opengl->no_window) {
971 SDL_GL_DeleteContext(opengl->glcontext);
972 SDL_DestroyWindow(opengl->window);
976 av_log(opengl, AV_LOG_ERROR, "Application failed to release window buffer.\n");
984 OpenGLContext *opengl = h->priv_data;
986 if (opengl->no_window &&
988 av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
990 opengl_deinit_context(opengl);
996 static av_cold int opengl_init_context(OpenGLContext *opengl)
1001 if ((ret = opengl_compile_shaders(opengl, opengl->pix_fmt)) < 0)
1004 desc = av_pix_fmt_desc_get(opengl->pix_fmt);
1006 glGenTextures(desc->nb_components, opengl->texture_name);
1008 opengl->glprocs.glGenBuffers(2, &opengl->index_buffer);
1009 if (!opengl->index_buffer || !opengl->vertex_buffer) {
1010 av_log(opengl, AV_LOG_ERROR, "Buffer generation failed.\n");
1015 opengl_configure_texture(opengl, opengl->texture_name[0], opengl->width, opengl->height);
1019 if (opengl->non_pow_2_textures) {
1020 opengl->chroma_div_w = 1.0f;
1021 opengl->chroma_div_h = 1.0f;
1023 opengl->chroma_div_w = 1 << desc->log2_chroma_w;
1024 opengl->chroma_div_h = 1 << desc->log2_chroma_h;
1027 if (opengl->non_pow_2_textures)
1028 opengl_configure_texture(opengl, opengl->texture_name[i],
1029 AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w),
1030 AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h));
1032 opengl_configure_texture(opengl, opengl->texture_name[i], opengl->width, opengl->height);
1034 opengl_configure_texture(opengl, opengl->texture_name[3], opengl->width, opengl->height);
1037 opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
1038 opengl->glprocs.glBufferData(FF_GL_ELEMENT_ARRAY_BUFFER, sizeof(g_index), g_index, FF_GL_STATIC_DRAW);
1039 opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, 0);
1044 glClearColor((float)opengl->background[0] / 255.0f, (float)opengl->background[1] / 255.0f,
1045 (float)opengl->background[2] / 255.0f, 1.0f);
1048 OPENGL_ERROR_CHECK(opengl);
1057 OpenGLContext *opengl = h->priv_data;
1065 av_log(opengl, AV_LOG_ERROR, "Only a single raw or wrapped avframe video stream is supported.\n");
1069 opengl->width = st->codecpar->width;
1070 opengl->height = st->codecpar->height;
1071 opengl->pix_fmt = st->codecpar->format;
1072 if (!opengl->window_width)
1073 opengl->window_width = opengl->width;
1074 if (!opengl->window_height)
1075 opengl->window_height = opengl->height;
1077 if (!opengl->window_title && !opengl->no_window)
1078 opengl->window_title = av_strdup(h->url);
1086 if (opengl->width > opengl->max_texture_size || opengl->height > opengl->max_texture_size) {
1087 av_log(opengl, AV_LOG_ERROR, "Too big picture %dx%d, max supported size is %dx%d\n",
1088 opengl->width, opengl->height, opengl->max_texture_size, opengl->max_texture_size);
1093 if ((ret = opengl_load_procedures(opengl)) < 0)
1096 opengl_fill_color_map(opengl);
1097 opengl_get_texture_params(opengl);
1099 if ((ret = opengl_init_context(opengl)) < 0)
1108 if (!opengl->no_window)
1109 SDL_GL_SwapWindow(opengl->window);
1111 if (opengl->no_window &&
1113 av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
1118 OPENGL_ERROR_CHECK(opengl);
1120 opengl->inited = 1;
1128 static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, int comp_index,
1132 int wordsize = opengl_type_size(opengl->type);
1133 int width_chroma = AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w);
1134 int height_chroma = AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h);
1141 data += opengl->width * opengl->height * wordsize;
1144 data += opengl->width * opengl->height * wordsize;
1148 data += opengl->width * opengl->height * wordsize;
1159 int width = sub ? AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width; \
1160 int height = sub ? AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h): opengl->height; \
1164 glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]); \
1167 int bytes_per_pixel = opengl_type_size(opengl->type); \
1172 (opengl->unpack_subimage || ((length / bytes_per_pixel) == width))) { \
1177 opengl->format, opengl->type, data); \
1184 opengl->format, opengl->type, data); \
1189 data = opengl_get_plane_pointer(opengl, input, comp_index, desc); \
1191 opengl->format, opengl->type, data); \
1197 OpenGLContext *opengl = h->priv_data;
1203 if (!opengl->no_window && (ret = opengl_sdl_process_events(h)) < 0)
1206 if (opengl->no_window &&
1208 av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
1226 OPENGL_ERROR_CHECK(opengl);
1228 if ((ret = opengl_prepare(opengl)) < 0)
1231 opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
1232 opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
1233 opengl->glprocs.glVertexAttribPointer(opengl->position_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 0);
1234 opengl->glprocs.glEnableVertexAttribArray(opengl->position_attrib);
1235 opengl->glprocs.glVertexAttribPointer(opengl->texture_coords_attrib, 2, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 12);
1236 opengl->glprocs.glEnableVertexAttribArray(opengl->texture_coords_attrib);
1241 OPENGL_ERROR_CHECK(opengl);
1244 if (!opengl->no_window)
1245 SDL_GL_SwapWindow(opengl->window);
1247 if (opengl->no_window &&
1249 av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
1288 .class_name = "opengl outdev",
1296 .name = "opengl",