opengl_enc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * Copyright (c) 2014 Lukasz Marek
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. //TODO: support for more formats
  21. //TODO: support for more systems.
  22. //TODO: implement X11, Windows, Mac OS native default window. SDL 1.2 doesn't allow to render to custom thread.
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stddef.h>
  27. #include "config.h"
  28. #if HAVE_WINDOWS_H
  29. #include <windows.h>
  30. #endif
  31. #if HAVE_OPENGL_GL3_H
  32. #include <OpenGL/gl3.h>
  33. #elif HAVE_ES2_GL_H
  34. #include <ES2/gl.h>
  35. #else
  36. #include <GL/gl.h>
  37. #include <GL/glext.h>
  38. #endif
  39. #if HAVE_GLXGETPROCADDRESS
  40. #include <GL/glx.h>
  41. #endif
  42. #if CONFIG_SDL2
  43. #include <SDL.h>
  44. #endif
  45. #include "libavutil/common.h"
  46. #include "libavutil/frame.h"
  47. #include "libavutil/pixdesc.h"
  48. #include "libavutil/log.h"
  49. #include "libavutil/opt.h"
  50. #include "libavutil/avassert.h"
  51. #include "libavformat/avformat.h"
  52. #include "libavformat/internal.h"
  53. #include "libavformat/mux.h"
  54. #include "libavdevice/avdevice.h"
  55. #include "opengl_enc_shaders.h"
  56. #ifndef APIENTRY
  57. #define APIENTRY
  58. #endif
  59. /* FF_GL_RED_COMPONENT is used for planar pixel types.
  60. * Only red component is sampled in shaders.
  61. * On some platforms GL_RED is not available and GL_LUMINANCE have to be used,
  62. * but since OpenGL 3.0 GL_LUMINANCE is deprecated.
  63. * GL_RED produces RGBA = value, 0, 0, 1.
  64. * GL_LUMINANCE produces RGBA = value, value, value, 1.
  65. * Note: GL_INTENSITY may also be used which produce RGBA = value, value, value, value. */
  66. #if defined(GL_RED)
  67. #define FF_GL_RED_COMPONENT GL_RED
  68. #elif defined(GL_LUMINANCE)
  69. #define FF_GL_RED_COMPONENT GL_LUMINANCE
  70. #else
  71. #define FF_GL_RED_COMPONENT 0x1903; //GL_RED
  72. #endif
  73. /* Constants not defined for iOS */
  74. #define FF_GL_UNSIGNED_BYTE_3_3_2 0x8032
  75. #define FF_GL_UNSIGNED_BYTE_2_3_3_REV 0x8362
  76. #define FF_GL_UNSIGNED_SHORT_1_5_5_5_REV 0x8366
  77. #define FF_GL_UNPACK_ROW_LENGTH 0x0CF2
  78. /* MinGW exposes only OpenGL 1.1 API */
  79. #define FF_GL_ARRAY_BUFFER 0x8892
  80. #define FF_GL_ELEMENT_ARRAY_BUFFER 0x8893
  81. #define FF_GL_STATIC_DRAW 0x88E4
  82. #define FF_GL_FRAGMENT_SHADER 0x8B30
  83. #define FF_GL_VERTEX_SHADER 0x8B31
  84. #define FF_GL_COMPILE_STATUS 0x8B81
  85. #define FF_GL_LINK_STATUS 0x8B82
  86. #define FF_GL_INFO_LOG_LENGTH 0x8B84
  87. typedef void (APIENTRY *FF_PFNGLACTIVETEXTUREPROC) (GLenum texture);
  88. typedef void (APIENTRY *FF_PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
  89. typedef void (APIENTRY *FF_PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
  90. typedef void (APIENTRY *FF_PFNGLBUFFERDATAPROC) (GLenum target, ptrdiff_t size, const GLvoid *data, GLenum usage);
  91. typedef void (APIENTRY *FF_PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
  92. typedef GLint (APIENTRY *FF_PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const char *name);
  93. typedef void (APIENTRY *FF_PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
  94. typedef void (APIENTRY *FF_PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uintptr_t pointer);
  95. typedef GLint (APIENTRY *FF_PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const char *name);
  96. typedef void (APIENTRY *FF_PFNGLUNIFORM1FPROC) (GLint location, GLfloat v0);
  97. typedef void (APIENTRY *FF_PFNGLUNIFORM1IPROC) (GLint location, GLint v0);
  98. typedef void (APIENTRY *FF_PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
  99. typedef GLuint (APIENTRY *FF_PFNGLCREATEPROGRAMPROC) (void);
  100. typedef void (APIENTRY *FF_PFNGLDELETEPROGRAMPROC) (GLuint program);
  101. typedef void (APIENTRY *FF_PFNGLUSEPROGRAMPROC) (GLuint program);
  102. typedef void (APIENTRY *FF_PFNGLLINKPROGRAMPROC) (GLuint program);
  103. typedef void (APIENTRY *FF_PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params);
  104. typedef void (APIENTRY *FF_PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, char *infoLog);
  105. typedef void (APIENTRY *FF_PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
  106. typedef GLuint (APIENTRY *FF_PFNGLCREATESHADERPROC) (GLenum type);
  107. typedef void (APIENTRY *FF_PFNGLDELETESHADERPROC) (GLuint shader);
  108. typedef void (APIENTRY *FF_PFNGLCOMPILESHADERPROC) (GLuint shader);
  109. typedef void (APIENTRY *FF_PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const char* *string, const GLint *length);
  110. typedef void (APIENTRY *FF_PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
  111. typedef void (APIENTRY *FF_PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, char *infoLog);
  112. typedef struct FFOpenGLFunctions {
  113. FF_PFNGLACTIVETEXTUREPROC glActiveTexture; //Require GL ARB multitexture
  114. FF_PFNGLGENBUFFERSPROC glGenBuffers; //Require GL_ARB_vertex_buffer_object
  115. FF_PFNGLDELETEBUFFERSPROC glDeleteBuffers; //Require GL_ARB_vertex_buffer_object
  116. FF_PFNGLBUFFERDATAPROC glBufferData; //Require GL_ARB_vertex_buffer_object
  117. FF_PFNGLBINDBUFFERPROC glBindBuffer; //Require GL_ARB_vertex_buffer_object
  118. FF_PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation; //Require GL_ARB_vertex_shader
  119. FF_PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray; //Require GL_ARB_vertex_shader
  120. FF_PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer; //Require GL_ARB_vertex_shader
  121. FF_PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation; //Require GL_ARB_shader_objects
  122. FF_PFNGLUNIFORM1FPROC glUniform1f; //Require GL_ARB_shader_objects
  123. FF_PFNGLUNIFORM1IPROC glUniform1i; //Require GL_ARB_shader_objects
  124. FF_PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv; //Require GL_ARB_shader_objects
  125. FF_PFNGLCREATEPROGRAMPROC glCreateProgram; //Require GL_ARB_shader_objects
  126. FF_PFNGLDELETEPROGRAMPROC glDeleteProgram; //Require GL_ARB_shader_objects
  127. FF_PFNGLUSEPROGRAMPROC glUseProgram; //Require GL_ARB_shader_objects
  128. FF_PFNGLLINKPROGRAMPROC glLinkProgram; //Require GL_ARB_shader_objects
  129. FF_PFNGLGETPROGRAMIVPROC glGetProgramiv; //Require GL_ARB_shader_objects
  130. FF_PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog; //Require GL_ARB_shader_objects
  131. FF_PFNGLATTACHSHADERPROC glAttachShader; //Require GL_ARB_shader_objects
  132. FF_PFNGLCREATESHADERPROC glCreateShader; //Require GL_ARB_shader_objects
  133. FF_PFNGLDELETESHADERPROC glDeleteShader; //Require GL_ARB_shader_objects
  134. FF_PFNGLCOMPILESHADERPROC glCompileShader; //Require GL_ARB_shader_objects
  135. FF_PFNGLSHADERSOURCEPROC glShaderSource; //Require GL_ARB_shader_objects
  136. FF_PFNGLGETSHADERIVPROC glGetShaderiv; //Require GL_ARB_shader_objects
  137. FF_PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog; //Require GL_ARB_shader_objects
  138. } FFOpenGLFunctions;
  139. #define OPENGL_ERROR_CHECK(ctx) \
  140. {\
  141. GLenum err_code; \
  142. if ((err_code = glGetError()) != GL_NO_ERROR) { \
  143. av_log(ctx, AV_LOG_ERROR, "OpenGL error occurred in '%s', line %d: %d\n", __func__, __LINE__, err_code); \
  144. goto fail; \
  145. } \
  146. }\
  147. typedef struct OpenGLVertexInfo
  148. {
  149. float x, y, z; ///<Position
  150. float s0, t0; ///<Texture coords
  151. } OpenGLVertexInfo;
  152. /* defines 2 triangles to display */
  153. static const GLushort g_index[6] =
  154. {
  155. 0, 1, 2,
  156. 0, 3, 2,
  157. };
  158. typedef struct OpenGLContext {
  159. AVClass *class; ///< class for private options
  160. #if CONFIG_SDL2
  161. SDL_Window *window;
  162. SDL_GLContext glcontext;
  163. #endif
  164. FFOpenGLFunctions glprocs;
  165. int inited; ///< Set to 1 when write_header was successfully called.
  166. uint8_t background[4]; ///< Background color
  167. int no_window; ///< 0 for create default window
  168. char *window_title; ///< Title of the window
  169. /* OpenGL implementation limits */
  170. GLint max_texture_size; ///< Maximum texture size
  171. GLint max_viewport_width; ///< Maximum viewport size
  172. GLint max_viewport_height; ///< Maximum viewport size
  173. int non_pow_2_textures; ///< 1 when non power of 2 textures are supported
  174. int unpack_subimage; ///< 1 when GL_EXT_unpack_subimage is available
  175. /* Current OpenGL configuration */
  176. GLuint program; ///< Shader program
  177. GLuint vertex_shader; ///< Vertex shader
  178. GLuint fragment_shader; ///< Fragment shader for current pix_pmt
  179. GLuint texture_name[4]; ///< Textures' IDs
  180. GLuint index_buffer; ///< Index buffer
  181. GLuint vertex_buffer; ///< Vertex buffer
  182. OpenGLVertexInfo vertex[4]; ///< VBO
  183. GLint projection_matrix_location; ///< Uniforms' locations
  184. GLint model_view_matrix_location;
  185. GLint color_map_location;
  186. GLint chroma_div_w_location;
  187. GLint chroma_div_h_location;
  188. GLint texture_location[4];
  189. GLint position_attrib; ///< Attibutes' locations
  190. GLint texture_coords_attrib;
  191. GLfloat projection_matrix[16]; ///< Projection matrix
  192. GLfloat model_view_matrix[16]; ///< Modev view matrix
  193. GLfloat color_map[16]; ///< RGBA color map matrix
  194. GLfloat chroma_div_w; ///< Chroma subsampling w ratio
  195. GLfloat chroma_div_h; ///< Chroma subsampling h ratio
  196. /* Stream information */
  197. GLenum format;
  198. GLenum type;
  199. int width; ///< Stream width
  200. int height; ///< Stream height
  201. enum AVPixelFormat pix_fmt; ///< Stream pixel format
  202. int picture_width; ///< Rendered width
  203. int picture_height; ///< Rendered height
  204. int window_width;
  205. int window_height;
  206. } OpenGLContext;
  207. static const struct OpenGLFormatDesc {
  208. enum AVPixelFormat fixel_format;
  209. const char * const * fragment_shader;
  210. GLenum format;
  211. GLenum type;
  212. } opengl_format_desc[] = {
  213. { AV_PIX_FMT_YUV420P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  214. { AV_PIX_FMT_YUV444P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  215. { AV_PIX_FMT_YUV422P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  216. { AV_PIX_FMT_YUV410P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  217. { AV_PIX_FMT_YUV411P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  218. { AV_PIX_FMT_YUV440P, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  219. { AV_PIX_FMT_YUV420P16, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  220. { AV_PIX_FMT_YUV422P16, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  221. { AV_PIX_FMT_YUV444P16, &FF_OPENGL_FRAGMENT_SHADER_YUV_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  222. { AV_PIX_FMT_YUVA420P, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  223. { AV_PIX_FMT_YUVA444P, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  224. { AV_PIX_FMT_YUVA422P, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  225. { AV_PIX_FMT_YUVA420P16, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  226. { AV_PIX_FMT_YUVA422P16, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  227. { AV_PIX_FMT_YUVA444P16, &FF_OPENGL_FRAGMENT_SHADER_YUVA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  228. { AV_PIX_FMT_RGB24, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_BYTE },
  229. { AV_PIX_FMT_BGR24, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_BYTE },
  230. { AV_PIX_FMT_0RGB, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  231. { AV_PIX_FMT_RGB0, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  232. { AV_PIX_FMT_0BGR, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  233. { AV_PIX_FMT_BGR0, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  234. { AV_PIX_FMT_RGB565, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 },
  235. { AV_PIX_FMT_BGR565, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_SHORT_5_6_5 },
  236. { AV_PIX_FMT_RGB555, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, FF_GL_UNSIGNED_SHORT_1_5_5_5_REV },
  237. { AV_PIX_FMT_BGR555, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGBA, FF_GL_UNSIGNED_SHORT_1_5_5_5_REV },
  238. { AV_PIX_FMT_RGB8, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, FF_GL_UNSIGNED_BYTE_3_3_2 },
  239. { AV_PIX_FMT_BGR8, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, FF_GL_UNSIGNED_BYTE_2_3_3_REV },
  240. { AV_PIX_FMT_RGB48, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_SHORT },
  241. { AV_PIX_FMT_BGR48, &FF_OPENGL_FRAGMENT_SHADER_RGB_PACKET, GL_RGB, GL_UNSIGNED_SHORT },
  242. { AV_PIX_FMT_ARGB, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  243. { AV_PIX_FMT_RGBA, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  244. { AV_PIX_FMT_ABGR, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  245. { AV_PIX_FMT_BGRA, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_BYTE },
  246. { AV_PIX_FMT_RGBA64, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_SHORT },
  247. { AV_PIX_FMT_BGRA64, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PACKET, GL_RGBA, GL_UNSIGNED_SHORT },
  248. { AV_PIX_FMT_GBRP, &FF_OPENGL_FRAGMENT_SHADER_RGB_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  249. { AV_PIX_FMT_GBRP16, &FF_OPENGL_FRAGMENT_SHADER_RGB_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  250. { AV_PIX_FMT_GBRAP, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  251. { AV_PIX_FMT_GBRAP16, &FF_OPENGL_FRAGMENT_SHADER_RGBA_PLANAR, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  252. { AV_PIX_FMT_GRAY8, &FF_OPENGL_FRAGMENT_SHADER_GRAY, FF_GL_RED_COMPONENT, GL_UNSIGNED_BYTE },
  253. { AV_PIX_FMT_GRAY16, &FF_OPENGL_FRAGMENT_SHADER_GRAY, FF_GL_RED_COMPONENT, GL_UNSIGNED_SHORT },
  254. { AV_PIX_FMT_NONE, NULL }
  255. };
  256. static av_cold int opengl_prepare_vertex(AVFormatContext *s);
  257. static int opengl_draw(AVFormatContext *h, void *intput, int repaint, int is_pkt);
  258. static av_cold int opengl_init_context(OpenGLContext *opengl);
  259. static av_cold void opengl_deinit_context(OpenGLContext *opengl)
  260. {
  261. glDeleteTextures(4, opengl->texture_name);
  262. opengl->texture_name[0] = opengl->texture_name[1] =
  263. opengl->texture_name[2] = opengl->texture_name[3] = 0;
  264. if (opengl->glprocs.glUseProgram)
  265. opengl->glprocs.glUseProgram(0);
  266. if (opengl->glprocs.glDeleteProgram) {
  267. opengl->glprocs.glDeleteProgram(opengl->program);
  268. opengl->program = 0;
  269. }
  270. if (opengl->glprocs.glDeleteShader) {
  271. opengl->glprocs.glDeleteShader(opengl->vertex_shader);
  272. opengl->glprocs.glDeleteShader(opengl->fragment_shader);
  273. opengl->vertex_shader = opengl->fragment_shader = 0;
  274. }
  275. if (opengl->glprocs.glBindBuffer) {
  276. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, 0);
  277. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, 0);
  278. }
  279. if (opengl->glprocs.glDeleteBuffers) {
  280. opengl->glprocs.glDeleteBuffers(2, &opengl->index_buffer);
  281. opengl->vertex_buffer = opengl->index_buffer = 0;
  282. }
  283. }
  284. static int opengl_resize(AVFormatContext *h, int width, int height)
  285. {
  286. int ret = 0;
  287. OpenGLContext *opengl = h->priv_data;
  288. opengl->window_width = width;
  289. opengl->window_height = height;
  290. if (opengl->inited) {
  291. if (opengl->no_window &&
  292. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0)) < 0) {
  293. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  294. goto end;
  295. }
  296. if ((ret = opengl_prepare_vertex(h)) < 0)
  297. goto end;
  298. ret = opengl_draw(h, NULL, 1, 0);
  299. }
  300. end:
  301. return ret;
  302. }
  303. static int opengl_control_message(AVFormatContext *h, int type, void *data, size_t data_size)
  304. {
  305. OpenGLContext *opengl = h->priv_data;
  306. switch(type) {
  307. case AV_APP_TO_DEV_WINDOW_SIZE:
  308. if (data) {
  309. AVDeviceRect *message = data;
  310. return opengl_resize(h, message->width, message->height);
  311. }
  312. return AVERROR(EINVAL);
  313. case AV_APP_TO_DEV_WINDOW_REPAINT:
  314. return opengl_resize(h, opengl->window_width, opengl->window_height);
  315. }
  316. return AVERROR(ENOSYS);
  317. }
  318. #if CONFIG_SDL2
  319. static int opengl_sdl_process_events(AVFormatContext *h)
  320. {
  321. OpenGLContext *opengl = h->priv_data;
  322. AVDeviceRect message;
  323. SDL_Event event;
  324. SDL_PumpEvents();
  325. while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) {
  326. switch (event.type) {
  327. case SDL_QUIT:
  328. return AVERROR(EIO);
  329. case SDL_KEYDOWN:
  330. switch (event.key.keysym.sym) {
  331. case SDLK_ESCAPE:
  332. case SDLK_q:
  333. return AVERROR(EIO);
  334. }
  335. return 0;
  336. case SDL_WINDOWEVENT:
  337. switch(event.window.event) {
  338. case SDL_WINDOWEVENT_RESIZED:
  339. case SDL_WINDOWEVENT_SIZE_CHANGED:
  340. SDL_GL_GetDrawableSize(opengl->window, &message.width, &message.height);
  341. return opengl_control_message(h, AV_APP_TO_DEV_WINDOW_SIZE, &message, sizeof(AVDeviceRect));
  342. default:
  343. break;
  344. }
  345. }
  346. }
  347. return 0;
  348. }
  349. static int av_cold opengl_sdl_create_window(AVFormatContext *h)
  350. {
  351. OpenGLContext *opengl = h->priv_data;
  352. AVDeviceRect message;
  353. if (SDL_Init(SDL_INIT_VIDEO)) {
  354. av_log(opengl, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
  355. return AVERROR_EXTERNAL;
  356. }
  357. opengl->window = SDL_CreateWindow(opengl->window_title,
  358. SDL_WINDOWPOS_UNDEFINED,
  359. SDL_WINDOWPOS_UNDEFINED,
  360. opengl->window_width, opengl->window_height,
  361. SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
  362. if (!opengl->window) {
  363. av_log(opengl, AV_LOG_ERROR, "Unable to create default window: %s\n", SDL_GetError());
  364. return AVERROR_EXTERNAL;
  365. }
  366. opengl->glcontext = SDL_GL_CreateContext(opengl->window);
  367. if (!opengl->glcontext) {
  368. av_log(opengl, AV_LOG_ERROR, "Unable to create OpenGL context on default window: %s\n", SDL_GetError());
  369. return AVERROR_EXTERNAL;
  370. }
  371. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  372. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  373. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  374. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  375. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  376. av_log(opengl, AV_LOG_INFO, "SDL driver: '%s'.\n", SDL_GetCurrentVideoDriver());
  377. SDL_GL_GetDrawableSize(opengl->window, &message.width, &message.height);
  378. return opengl_control_message(h, AV_APP_TO_DEV_WINDOW_SIZE, &message, sizeof(AVDeviceRect));
  379. }
  380. static int av_cold opengl_sdl_load_procedures(OpenGLContext *opengl)
  381. {
  382. FFOpenGLFunctions *procs = &opengl->glprocs;
  383. #define LOAD_OPENGL_FUN(name, type) \
  384. procs->name = (type)SDL_GL_GetProcAddress(#name); \
  385. if (!procs->name) { \
  386. av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
  387. return AVERROR(ENOSYS); \
  388. }
  389. LOAD_OPENGL_FUN(glActiveTexture, FF_PFNGLACTIVETEXTUREPROC)
  390. LOAD_OPENGL_FUN(glGenBuffers, FF_PFNGLGENBUFFERSPROC)
  391. LOAD_OPENGL_FUN(glDeleteBuffers, FF_PFNGLDELETEBUFFERSPROC)
  392. LOAD_OPENGL_FUN(glBufferData, FF_PFNGLBUFFERDATAPROC)
  393. LOAD_OPENGL_FUN(glBindBuffer, FF_PFNGLBINDBUFFERPROC)
  394. LOAD_OPENGL_FUN(glGetAttribLocation, FF_PFNGLGETATTRIBLOCATIONPROC)
  395. LOAD_OPENGL_FUN(glGetUniformLocation, FF_PFNGLGETUNIFORMLOCATIONPROC)
  396. LOAD_OPENGL_FUN(glUniform1f, FF_PFNGLUNIFORM1FPROC)
  397. LOAD_OPENGL_FUN(glUniform1i, FF_PFNGLUNIFORM1IPROC)
  398. LOAD_OPENGL_FUN(glUniformMatrix4fv, FF_PFNGLUNIFORMMATRIX4FVPROC)
  399. LOAD_OPENGL_FUN(glCreateProgram, FF_PFNGLCREATEPROGRAMPROC)
  400. LOAD_OPENGL_FUN(glDeleteProgram, FF_PFNGLDELETEPROGRAMPROC)
  401. LOAD_OPENGL_FUN(glUseProgram, FF_PFNGLUSEPROGRAMPROC)
  402. LOAD_OPENGL_FUN(glLinkProgram, FF_PFNGLLINKPROGRAMPROC)
  403. LOAD_OPENGL_FUN(glGetProgramiv, FF_PFNGLGETPROGRAMIVPROC)
  404. LOAD_OPENGL_FUN(glGetProgramInfoLog, FF_PFNGLGETPROGRAMINFOLOGPROC)
  405. LOAD_OPENGL_FUN(glAttachShader, FF_PFNGLATTACHSHADERPROC)
  406. LOAD_OPENGL_FUN(glCreateShader, FF_PFNGLCREATESHADERPROC)
  407. LOAD_OPENGL_FUN(glDeleteShader, FF_PFNGLDELETESHADERPROC)
  408. LOAD_OPENGL_FUN(glCompileShader, FF_PFNGLCOMPILESHADERPROC)
  409. LOAD_OPENGL_FUN(glShaderSource, FF_PFNGLSHADERSOURCEPROC)
  410. LOAD_OPENGL_FUN(glGetShaderiv, FF_PFNGLGETSHADERIVPROC)
  411. LOAD_OPENGL_FUN(glGetShaderInfoLog, FF_PFNGLGETSHADERINFOLOGPROC)
  412. LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
  413. LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
  414. return 0;
  415. #undef LOAD_OPENGL_FUN
  416. }
  417. #endif /* CONFIG_SDL2 */
  418. #if defined(__APPLE__)
  419. static int av_cold opengl_load_procedures(OpenGLContext *opengl)
  420. {
  421. FFOpenGLFunctions *procs = &opengl->glprocs;
  422. #if CONFIG_SDL2
  423. if (!opengl->no_window)
  424. return opengl_sdl_load_procedures(opengl);
  425. #endif
  426. procs->glActiveTexture = glActiveTexture;
  427. procs->glGenBuffers = glGenBuffers;
  428. procs->glDeleteBuffers = glDeleteBuffers;
  429. procs->glBufferData = glBufferData;
  430. procs->glBindBuffer = glBindBuffer;
  431. procs->glGetAttribLocation = glGetAttribLocation;
  432. procs->glGetUniformLocation = glGetUniformLocation;
  433. procs->glUniform1f = glUniform1f;
  434. procs->glUniform1i = glUniform1i;
  435. procs->glUniformMatrix4fv = glUniformMatrix4fv;
  436. procs->glCreateProgram = glCreateProgram;
  437. procs->glDeleteProgram = glDeleteProgram;
  438. procs->glUseProgram = glUseProgram;
  439. procs->glLinkProgram = glLinkProgram;
  440. procs->glGetProgramiv = glGetProgramiv;
  441. procs->glGetProgramInfoLog = glGetProgramInfoLog;
  442. procs->glAttachShader = glAttachShader;
  443. procs->glCreateShader = glCreateShader;
  444. procs->glDeleteShader = glDeleteShader;
  445. procs->glCompileShader = glCompileShader;
  446. procs->glShaderSource = glShaderSource;
  447. procs->glGetShaderiv = glGetShaderiv;
  448. procs->glGetShaderInfoLog = glGetShaderInfoLog;
  449. procs->glEnableVertexAttribArray = glEnableVertexAttribArray;
  450. procs->glVertexAttribPointer = (FF_PFNGLVERTEXATTRIBPOINTERPROC) glVertexAttribPointer;
  451. return 0;
  452. }
  453. #else
  454. static int av_cold opengl_load_procedures(OpenGLContext *opengl)
  455. {
  456. FFOpenGLFunctions *procs = &opengl->glprocs;
  457. #if HAVE_GLXGETPROCADDRESS
  458. #define SelectedGetProcAddress glXGetProcAddress
  459. #elif HAVE_WGLGETPROCADDRESS
  460. #define SelectedGetProcAddress wglGetProcAddress
  461. #endif
  462. #define LOAD_OPENGL_FUN(name, type) \
  463. procs->name = (type)SelectedGetProcAddress(#name); \
  464. if (!procs->name) { \
  465. av_log(opengl, AV_LOG_ERROR, "Cannot load OpenGL function: '%s'\n", #name); \
  466. return AVERROR(ENOSYS); \
  467. }
  468. #if CONFIG_SDL2
  469. if (!opengl->no_window)
  470. return opengl_sdl_load_procedures(opengl);
  471. #endif
  472. LOAD_OPENGL_FUN(glActiveTexture, FF_PFNGLACTIVETEXTUREPROC)
  473. LOAD_OPENGL_FUN(glGenBuffers, FF_PFNGLGENBUFFERSPROC)
  474. LOAD_OPENGL_FUN(glDeleteBuffers, FF_PFNGLDELETEBUFFERSPROC)
  475. LOAD_OPENGL_FUN(glBufferData, FF_PFNGLBUFFERDATAPROC)
  476. LOAD_OPENGL_FUN(glBindBuffer, FF_PFNGLBINDBUFFERPROC)
  477. LOAD_OPENGL_FUN(glGetAttribLocation, FF_PFNGLGETATTRIBLOCATIONPROC)
  478. LOAD_OPENGL_FUN(glGetUniformLocation, FF_PFNGLGETUNIFORMLOCATIONPROC)
  479. LOAD_OPENGL_FUN(glUniform1f, FF_PFNGLUNIFORM1FPROC)
  480. LOAD_OPENGL_FUN(glUniform1i, FF_PFNGLUNIFORM1IPROC)
  481. LOAD_OPENGL_FUN(glUniformMatrix4fv, FF_PFNGLUNIFORMMATRIX4FVPROC)
  482. LOAD_OPENGL_FUN(glCreateProgram, FF_PFNGLCREATEPROGRAMPROC)
  483. LOAD_OPENGL_FUN(glDeleteProgram, FF_PFNGLDELETEPROGRAMPROC)
  484. LOAD_OPENGL_FUN(glUseProgram, FF_PFNGLUSEPROGRAMPROC)
  485. LOAD_OPENGL_FUN(glLinkProgram, FF_PFNGLLINKPROGRAMPROC)
  486. LOAD_OPENGL_FUN(glGetProgramiv, FF_PFNGLGETPROGRAMIVPROC)
  487. LOAD_OPENGL_FUN(glGetProgramInfoLog, FF_PFNGLGETPROGRAMINFOLOGPROC)
  488. LOAD_OPENGL_FUN(glAttachShader, FF_PFNGLATTACHSHADERPROC)
  489. LOAD_OPENGL_FUN(glCreateShader, FF_PFNGLCREATESHADERPROC)
  490. LOAD_OPENGL_FUN(glDeleteShader, FF_PFNGLDELETESHADERPROC)
  491. LOAD_OPENGL_FUN(glCompileShader, FF_PFNGLCOMPILESHADERPROC)
  492. LOAD_OPENGL_FUN(glShaderSource, FF_PFNGLSHADERSOURCEPROC)
  493. LOAD_OPENGL_FUN(glGetShaderiv, FF_PFNGLGETSHADERIVPROC)
  494. LOAD_OPENGL_FUN(glGetShaderInfoLog, FF_PFNGLGETSHADERINFOLOGPROC)
  495. LOAD_OPENGL_FUN(glEnableVertexAttribArray, FF_PFNGLENABLEVERTEXATTRIBARRAYPROC)
  496. LOAD_OPENGL_FUN(glVertexAttribPointer, FF_PFNGLVERTEXATTRIBPOINTERPROC)
  497. return 0;
  498. #undef SelectedGetProcAddress
  499. #undef LOAD_OPENGL_FUN
  500. }
  501. #endif
  502. static void opengl_make_identity(float matrix[16])
  503. {
  504. memset(matrix, 0, 16 * sizeof(float));
  505. matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0f;
  506. }
  507. static void opengl_make_ortho(float matrix[16], float left, float right,
  508. float bottom, float top, float nearZ, float farZ)
  509. {
  510. float ral = right + left;
  511. float rsl = right - left;
  512. float tab = top + bottom;
  513. float tsb = top - bottom;
  514. float fan = farZ + nearZ;
  515. float fsn = farZ - nearZ;
  516. memset(matrix, 0, 16 * sizeof(float));
  517. matrix[0] = 2.0f / rsl;
  518. matrix[5] = 2.0f / tsb;
  519. matrix[10] = -2.0f / fsn;
  520. matrix[12] = -ral / rsl;
  521. matrix[13] = -tab / tsb;
  522. matrix[14] = -fan / fsn;
  523. matrix[15] = 1.0f;
  524. }
  525. static av_cold int opengl_read_limits(AVFormatContext *h)
  526. {
  527. OpenGLContext *opengl = h->priv_data;
  528. static const struct{
  529. const char *extension;
  530. int major;
  531. int minor;
  532. } required_extensions[] = {
  533. { "GL_ARB_multitexture", 1, 3 },
  534. { "GL_ARB_vertex_buffer_object", 1, 5 }, //GLX_ARB_vertex_buffer_object
  535. { "GL_ARB_vertex_shader", 2, 0 },
  536. { "GL_ARB_fragment_shader", 2, 0 },
  537. { "GL_ARB_shader_objects", 2, 0 },
  538. { NULL, 0, 0 }
  539. };
  540. int i, major, minor;
  541. const char *extensions, *version;
  542. version = glGetString(GL_VERSION);
  543. extensions = glGetString(GL_EXTENSIONS);
  544. if (!version || !extensions) {
  545. av_log(h, AV_LOG_ERROR, "No OpenGL context initialized for the current thread\n");
  546. return AVERROR(ENOSYS);
  547. }
  548. av_log(h, AV_LOG_DEBUG, "OpenGL version: %s\n", version);
  549. if (sscanf(version, "%d.%d", &major, &minor) != 2)
  550. return AVERROR(ENOSYS);
  551. for (i = 0; required_extensions[i].extension; i++) {
  552. if (major < required_extensions[i].major &&
  553. (major == required_extensions[i].major && minor < required_extensions[i].minor) &&
  554. !strstr(extensions, required_extensions[i].extension)) {
  555. av_log(h, AV_LOG_ERROR, "Required extension %s is not supported.\n",
  556. required_extensions[i].extension);
  557. av_log(h, AV_LOG_DEBUG, "Supported extensions are: %s\n", extensions);
  558. return AVERROR(ENOSYS);
  559. }
  560. }
  561. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &opengl->max_texture_size);
  562. glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &opengl->max_viewport_width);
  563. opengl->non_pow_2_textures = major >= 2 || strstr(extensions, "GL_ARB_texture_non_power_of_two");
  564. #if defined(GL_ES_VERSION_2_0)
  565. opengl->unpack_subimage = !!strstr(extensions, "GL_EXT_unpack_subimage");
  566. #else
  567. opengl->unpack_subimage = 1;
  568. #endif
  569. av_log(h, AV_LOG_DEBUG, "Non Power of 2 textures support: %s\n", opengl->non_pow_2_textures ? "Yes" : "No");
  570. av_log(h, AV_LOG_DEBUG, "Unpack Subimage extension support: %s\n", opengl->unpack_subimage ? "Yes" : "No");
  571. av_log(h, AV_LOG_DEBUG, "Max texture size: %dx%d\n", opengl->max_texture_size, opengl->max_texture_size);
  572. av_log(h, AV_LOG_DEBUG, "Max viewport size: %dx%d\n",
  573. opengl->max_viewport_width, opengl->max_viewport_height);
  574. OPENGL_ERROR_CHECK(opengl);
  575. return 0;
  576. fail:
  577. return AVERROR_EXTERNAL;
  578. }
  579. static const char* opengl_get_fragment_shader_code(enum AVPixelFormat format)
  580. {
  581. int i;
  582. for (i = 0; i < FF_ARRAY_ELEMS(opengl_format_desc); i++) {
  583. if (opengl_format_desc[i].fixel_format == format)
  584. return *opengl_format_desc[i].fragment_shader;
  585. }
  586. return NULL;
  587. }
  588. static int opengl_type_size(GLenum type)
  589. {
  590. switch(type) {
  591. case GL_UNSIGNED_SHORT:
  592. case FF_GL_UNSIGNED_SHORT_1_5_5_5_REV:
  593. case GL_UNSIGNED_SHORT_5_6_5:
  594. return 2;
  595. case GL_UNSIGNED_BYTE:
  596. case FF_GL_UNSIGNED_BYTE_3_3_2:
  597. case FF_GL_UNSIGNED_BYTE_2_3_3_REV:
  598. default:
  599. break;
  600. }
  601. return 1;
  602. }
  603. static av_cold void opengl_get_texture_params(OpenGLContext *opengl)
  604. {
  605. int i;
  606. for (i = 0; i < FF_ARRAY_ELEMS(opengl_format_desc); i++) {
  607. if (opengl_format_desc[i].fixel_format == opengl->pix_fmt) {
  608. opengl->format = opengl_format_desc[i].format;
  609. opengl->type = opengl_format_desc[i].type;
  610. break;
  611. }
  612. }
  613. }
  614. static void opengl_compute_display_area(AVFormatContext *s)
  615. {
  616. AVRational sar, dar; /* sample and display aspect ratios */
  617. OpenGLContext *opengl = s->priv_data;
  618. AVStream *st = s->streams[0];
  619. AVCodecParameters *par = st->codecpar;
  620. /* compute overlay width and height from the codec context information */
  621. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  622. dar = av_mul_q(sar, (AVRational){ par->width, par->height });
  623. /* we suppose the screen has a 1/1 sample aspect ratio */
  624. /* fit in the window */
  625. if (av_cmp_q(dar, (AVRational){ opengl->window_width, opengl->window_height }) > 0) {
  626. /* fit in width */
  627. opengl->picture_width = opengl->window_width;
  628. opengl->picture_height = av_rescale(opengl->picture_width, dar.den, dar.num);
  629. } else {
  630. /* fit in height */
  631. opengl->picture_height = opengl->window_height;
  632. opengl->picture_width = av_rescale(opengl->picture_height, dar.num, dar.den);
  633. }
  634. }
  635. static av_cold void opengl_get_texture_size(OpenGLContext *opengl, int in_width, int in_height,
  636. int *out_width, int *out_height)
  637. {
  638. if (opengl->non_pow_2_textures) {
  639. *out_width = in_width;
  640. *out_height = in_height;
  641. } else {
  642. int max = FFMIN(FFMAX(in_width, in_height), opengl->max_texture_size);
  643. unsigned power_of_2 = 1;
  644. while (power_of_2 < max)
  645. power_of_2 *= 2;
  646. *out_height = power_of_2;
  647. *out_width = power_of_2;
  648. av_log(opengl, AV_LOG_DEBUG, "Texture size calculated from %dx%d into %dx%d\n",
  649. in_width, in_height, *out_width, *out_height);
  650. }
  651. }
  652. static av_cold void opengl_fill_color_map(OpenGLContext *opengl)
  653. {
  654. const AVPixFmtDescriptor *desc;
  655. int shift;
  656. enum AVPixelFormat pix_fmt = opengl->pix_fmt;
  657. /* We need order of components, not exact position, some minor HACKs here */
  658. if (pix_fmt == AV_PIX_FMT_RGB565 || pix_fmt == AV_PIX_FMT_BGR555 ||
  659. pix_fmt == AV_PIX_FMT_BGR8 || pix_fmt == AV_PIX_FMT_RGB8)
  660. pix_fmt = AV_PIX_FMT_RGB24;
  661. else if (pix_fmt == AV_PIX_FMT_BGR565 || pix_fmt == AV_PIX_FMT_RGB555)
  662. pix_fmt = AV_PIX_FMT_BGR24;
  663. desc = av_pix_fmt_desc_get(pix_fmt);
  664. if (!(desc->flags & AV_PIX_FMT_FLAG_RGB))
  665. return;
  666. #define FILL_COMPONENT(i) { \
  667. shift = (desc->comp[i].depth - 1) >> 3; \
  668. opengl->color_map[(i << 2) + (desc->comp[i].offset >> shift)] = 1.0; \
  669. }
  670. memset(opengl->color_map, 0, sizeof(opengl->color_map));
  671. FILL_COMPONENT(0);
  672. FILL_COMPONENT(1);
  673. FILL_COMPONENT(2);
  674. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
  675. FILL_COMPONENT(3);
  676. #undef FILL_COMPONENT
  677. }
  678. static av_cold GLuint opengl_load_shader(OpenGLContext *opengl, GLenum type, const char *source)
  679. {
  680. GLuint shader = opengl->glprocs.glCreateShader(type);
  681. GLint result;
  682. if (!shader) {
  683. av_log(opengl, AV_LOG_ERROR, "glCreateShader() failed\n");
  684. return 0;
  685. }
  686. opengl->glprocs.glShaderSource(shader, 1, &source, NULL);
  687. opengl->glprocs.glCompileShader(shader);
  688. opengl->glprocs.glGetShaderiv(shader, FF_GL_COMPILE_STATUS, &result);
  689. if (!result) {
  690. char *log;
  691. opengl->glprocs.glGetShaderiv(shader, FF_GL_INFO_LOG_LENGTH, &result);
  692. if (result) {
  693. if ((log = av_malloc(result))) {
  694. opengl->glprocs.glGetShaderInfoLog(shader, result, NULL, log);
  695. av_log(opengl, AV_LOG_ERROR, "Compile error: %s\n", log);
  696. av_free(log);
  697. }
  698. }
  699. goto fail;
  700. }
  701. OPENGL_ERROR_CHECK(opengl);
  702. return shader;
  703. fail:
  704. opengl->glprocs.glDeleteShader(shader);
  705. return 0;
  706. }
  707. static av_cold int opengl_compile_shaders(OpenGLContext *opengl, enum AVPixelFormat pix_fmt)
  708. {
  709. GLint result;
  710. const char *fragment_shader_code = opengl_get_fragment_shader_code(pix_fmt);
  711. if (!fragment_shader_code) {
  712. av_log(opengl, AV_LOG_ERROR, "Provided pixel format '%s' is not supported\n",
  713. av_get_pix_fmt_name(pix_fmt));
  714. return AVERROR(EINVAL);
  715. }
  716. opengl->vertex_shader = opengl_load_shader(opengl, FF_GL_VERTEX_SHADER,
  717. FF_OPENGL_VERTEX_SHADER);
  718. if (!opengl->vertex_shader) {
  719. av_log(opengl, AV_LOG_ERROR, "Vertex shader loading failed.\n");
  720. goto fail;
  721. }
  722. opengl->fragment_shader = opengl_load_shader(opengl, FF_GL_FRAGMENT_SHADER,
  723. fragment_shader_code);
  724. if (!opengl->fragment_shader) {
  725. av_log(opengl, AV_LOG_ERROR, "Fragment shader loading failed.\n");
  726. goto fail;
  727. }
  728. opengl->program = opengl->glprocs.glCreateProgram();
  729. if (!opengl->program)
  730. goto fail;
  731. opengl->glprocs.glAttachShader(opengl->program, opengl->vertex_shader);
  732. opengl->glprocs.glAttachShader(opengl->program, opengl->fragment_shader);
  733. opengl->glprocs.glLinkProgram(opengl->program);
  734. opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_LINK_STATUS, &result);
  735. if (!result) {
  736. char *log;
  737. opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_INFO_LOG_LENGTH, &result);
  738. if (result) {
  739. log = av_malloc(result);
  740. if (!log)
  741. goto fail;
  742. opengl->glprocs.glGetProgramInfoLog(opengl->program, result, NULL, log);
  743. av_log(opengl, AV_LOG_ERROR, "Link error: %s\n", log);
  744. av_free(log);
  745. }
  746. goto fail;
  747. }
  748. opengl->position_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_position");
  749. opengl->texture_coords_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_textureCoords");
  750. opengl->projection_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_projectionMatrix");
  751. opengl->model_view_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_modelViewMatrix");
  752. opengl->color_map_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_colorMap");
  753. opengl->texture_location[0] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture0");
  754. opengl->texture_location[1] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture1");
  755. opengl->texture_location[2] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture2");
  756. opengl->texture_location[3] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture3");
  757. opengl->chroma_div_w_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_w");
  758. opengl->chroma_div_h_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_h");
  759. OPENGL_ERROR_CHECK(opengl);
  760. return 0;
  761. fail:
  762. opengl->glprocs.glDeleteShader(opengl->vertex_shader);
  763. opengl->glprocs.glDeleteShader(opengl->fragment_shader);
  764. opengl->glprocs.glDeleteProgram(opengl->program);
  765. opengl->fragment_shader = opengl->vertex_shader = opengl->program = 0;
  766. return AVERROR_EXTERNAL;
  767. }
  768. static av_cold int opengl_configure_texture(OpenGLContext *opengl, GLuint texture,
  769. GLsizei width, GLsizei height)
  770. {
  771. if (texture) {
  772. int new_width, new_height;
  773. opengl_get_texture_size(opengl, width, height, &new_width, &new_height);
  774. glBindTexture(GL_TEXTURE_2D, texture);
  775. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  776. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  777. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  778. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  779. glTexImage2D(GL_TEXTURE_2D, 0, opengl->format, new_width, new_height, 0,
  780. opengl->format, opengl->type, NULL);
  781. OPENGL_ERROR_CHECK(NULL);
  782. }
  783. return 0;
  784. fail:
  785. return AVERROR_EXTERNAL;
  786. }
  787. static av_cold int opengl_prepare_vertex(AVFormatContext *s)
  788. {
  789. OpenGLContext *opengl = s->priv_data;
  790. int tex_w, tex_h;
  791. if (opengl->window_width > opengl->max_viewport_width || opengl->window_height > opengl->max_viewport_height) {
  792. opengl->window_width = FFMIN(opengl->window_width, opengl->max_viewport_width);
  793. opengl->window_height = FFMIN(opengl->window_height, opengl->max_viewport_height);
  794. av_log(opengl, AV_LOG_WARNING, "Too big viewport requested, limited to %dx%d", opengl->window_width, opengl->window_height);
  795. }
  796. glViewport(0, 0, opengl->window_width, opengl->window_height);
  797. opengl_make_ortho(opengl->projection_matrix,
  798. - (float)opengl->window_width / 2.0f, (float)opengl->window_width / 2.0f,
  799. - (float)opengl->window_height / 2.0f, (float)opengl->window_height / 2.0f,
  800. 1.0f, -1.0f);
  801. opengl_make_identity(opengl->model_view_matrix);
  802. opengl_compute_display_area(s);
  803. opengl->vertex[0].z = opengl->vertex[1].z = opengl->vertex[2].z = opengl->vertex[3].z = 0.0f;
  804. opengl->vertex[0].x = opengl->vertex[1].x = - (float)opengl->picture_width / 2.0f;
  805. opengl->vertex[2].x = opengl->vertex[3].x = (float)opengl->picture_width / 2.0f;
  806. opengl->vertex[1].y = opengl->vertex[2].y = - (float)opengl->picture_height / 2.0f;
  807. opengl->vertex[0].y = opengl->vertex[3].y = (float)opengl->picture_height / 2.0f;
  808. opengl_get_texture_size(opengl, opengl->width, opengl->height, &tex_w, &tex_h);
  809. opengl->vertex[0].s0 = 0.0f;
  810. opengl->vertex[0].t0 = 0.0f;
  811. opengl->vertex[1].s0 = 0.0f;
  812. opengl->vertex[1].t0 = (float)opengl->height / (float)tex_h;
  813. opengl->vertex[2].s0 = (float)opengl->width / (float)tex_w;
  814. opengl->vertex[2].t0 = (float)opengl->height / (float)tex_h;
  815. opengl->vertex[3].s0 = (float)opengl->width / (float)tex_w;
  816. opengl->vertex[3].t0 = 0.0f;
  817. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
  818. opengl->glprocs.glBufferData(FF_GL_ARRAY_BUFFER, sizeof(opengl->vertex), opengl->vertex, FF_GL_STATIC_DRAW);
  819. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, 0);
  820. OPENGL_ERROR_CHECK(opengl);
  821. return 0;
  822. fail:
  823. return AVERROR_EXTERNAL;
  824. }
  825. static int opengl_prepare(OpenGLContext *opengl)
  826. {
  827. int i;
  828. opengl->glprocs.glUseProgram(opengl->program);
  829. opengl->glprocs.glUniformMatrix4fv(opengl->projection_matrix_location, 1, GL_FALSE, opengl->projection_matrix);
  830. opengl->glprocs.glUniformMatrix4fv(opengl->model_view_matrix_location, 1, GL_FALSE, opengl->model_view_matrix);
  831. for (i = 0; i < 4; i++)
  832. if (opengl->texture_location[i] != -1) {
  833. opengl->glprocs.glActiveTexture(GL_TEXTURE0 + i);
  834. glBindTexture(GL_TEXTURE_2D, opengl->texture_name[i]);
  835. opengl->glprocs.glUniform1i(opengl->texture_location[i], i);
  836. }
  837. if (opengl->color_map_location != -1)
  838. opengl->glprocs.glUniformMatrix4fv(opengl->color_map_location, 1, GL_FALSE, opengl->color_map);
  839. if (opengl->chroma_div_h_location != -1)
  840. opengl->glprocs.glUniform1f(opengl->chroma_div_h_location, opengl->chroma_div_h);
  841. if (opengl->chroma_div_w_location != -1)
  842. opengl->glprocs.glUniform1f(opengl->chroma_div_w_location, opengl->chroma_div_w);
  843. OPENGL_ERROR_CHECK(opengl);
  844. return 0;
  845. fail:
  846. return AVERROR_EXTERNAL;
  847. }
  848. static int opengl_create_window(AVFormatContext *h)
  849. {
  850. OpenGLContext *opengl = h->priv_data;
  851. int ret;
  852. if (!opengl->no_window) {
  853. #if CONFIG_SDL2
  854. if ((ret = opengl_sdl_create_window(h)) < 0) {
  855. av_log(opengl, AV_LOG_ERROR, "Cannot create default SDL window.\n");
  856. return ret;
  857. }
  858. #else
  859. av_log(opengl, AV_LOG_ERROR, "FFmpeg is compiled without SDL. Cannot create default window.\n");
  860. return AVERROR(ENOSYS);
  861. #endif
  862. } else {
  863. AVDeviceRect message;
  864. message.x = message.y = 0;
  865. message.width = opengl->window_width;
  866. message.height = opengl->window_height;
  867. if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_CREATE_WINDOW_BUFFER,
  868. &message , sizeof(message))) < 0) {
  869. av_log(opengl, AV_LOG_ERROR, "Application failed to create window buffer.\n");
  870. return ret;
  871. }
  872. if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0)) < 0) {
  873. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  874. return ret;
  875. }
  876. }
  877. return 0;
  878. }
  879. static int opengl_release_window(AVFormatContext *h)
  880. {
  881. int ret;
  882. OpenGLContext *opengl = h->priv_data;
  883. if (!opengl->no_window) {
  884. #if CONFIG_SDL2
  885. SDL_GL_DeleteContext(opengl->glcontext);
  886. SDL_DestroyWindow(opengl->window);
  887. SDL_Quit();
  888. #endif
  889. } else if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER, NULL , 0)) < 0) {
  890. av_log(opengl, AV_LOG_ERROR, "Application failed to release window buffer.\n");
  891. return ret;
  892. }
  893. return 0;
  894. }
  895. static av_cold int opengl_write_trailer(AVFormatContext *h)
  896. {
  897. OpenGLContext *opengl = h->priv_data;
  898. if (opengl->no_window &&
  899. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0) < 0)
  900. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  901. opengl_deinit_context(opengl);
  902. opengl_release_window(h);
  903. return 0;
  904. }
  905. static av_cold int opengl_init_context(OpenGLContext *opengl)
  906. {
  907. int i, ret;
  908. const AVPixFmtDescriptor *desc;
  909. if ((ret = opengl_compile_shaders(opengl, opengl->pix_fmt)) < 0)
  910. goto fail;
  911. desc = av_pix_fmt_desc_get(opengl->pix_fmt);
  912. av_assert0(desc->nb_components > 0 && desc->nb_components <= 4);
  913. glGenTextures(desc->nb_components, opengl->texture_name);
  914. opengl->glprocs.glGenBuffers(2, &opengl->index_buffer);
  915. if (!opengl->index_buffer || !opengl->vertex_buffer) {
  916. av_log(opengl, AV_LOG_ERROR, "Buffer generation failed.\n");
  917. ret = AVERROR_EXTERNAL;
  918. goto fail;
  919. }
  920. opengl_configure_texture(opengl, opengl->texture_name[0], opengl->width, opengl->height);
  921. if (desc->nb_components > 1) {
  922. int has_alpha = desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  923. int num_planes = desc->nb_components - (has_alpha ? 1 : 0);
  924. if (opengl->non_pow_2_textures) {
  925. opengl->chroma_div_w = 1.0f;
  926. opengl->chroma_div_h = 1.0f;
  927. } else {
  928. opengl->chroma_div_w = 1 << desc->log2_chroma_w;
  929. opengl->chroma_div_h = 1 << desc->log2_chroma_h;
  930. }
  931. for (i = 1; i < num_planes; i++)
  932. if (opengl->non_pow_2_textures)
  933. opengl_configure_texture(opengl, opengl->texture_name[i],
  934. AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w),
  935. AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h));
  936. else
  937. opengl_configure_texture(opengl, opengl->texture_name[i], opengl->width, opengl->height);
  938. if (has_alpha)
  939. opengl_configure_texture(opengl, opengl->texture_name[3], opengl->width, opengl->height);
  940. }
  941. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
  942. opengl->glprocs.glBufferData(FF_GL_ELEMENT_ARRAY_BUFFER, sizeof(g_index), g_index, FF_GL_STATIC_DRAW);
  943. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, 0);
  944. glEnable(GL_BLEND);
  945. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  946. glClearColor((float)opengl->background[0] / 255.0f, (float)opengl->background[1] / 255.0f,
  947. (float)opengl->background[2] / 255.0f, 1.0f);
  948. ret = AVERROR_EXTERNAL;
  949. OPENGL_ERROR_CHECK(opengl);
  950. return 0;
  951. fail:
  952. return ret;
  953. }
  954. static av_cold int opengl_write_header(AVFormatContext *h)
  955. {
  956. OpenGLContext *opengl = h->priv_data;
  957. AVCodecParameters *par = h->streams[0]->codecpar;
  958. AVStream *st;
  959. int ret;
  960. if (h->nb_streams != 1 ||
  961. par->codec_type != AVMEDIA_TYPE_VIDEO ||
  962. (par->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME && par->codec_id != AV_CODEC_ID_RAWVIDEO)) {
  963. av_log(opengl, AV_LOG_ERROR, "Only a single raw or wrapped avframe video stream is supported.\n");
  964. return AVERROR(EINVAL);
  965. }
  966. st = h->streams[0];
  967. opengl->width = st->codecpar->width;
  968. opengl->height = st->codecpar->height;
  969. opengl->pix_fmt = st->codecpar->format;
  970. if (!opengl->window_width)
  971. opengl->window_width = opengl->width;
  972. if (!opengl->window_height)
  973. opengl->window_height = opengl->height;
  974. if (!opengl->window_title && !opengl->no_window)
  975. opengl->window_title = av_strdup(h->url);
  976. if ((ret = opengl_create_window(h)))
  977. goto fail;
  978. if ((ret = opengl_read_limits(h)) < 0)
  979. goto fail;
  980. if (opengl->width > opengl->max_texture_size || opengl->height > opengl->max_texture_size) {
  981. av_log(opengl, AV_LOG_ERROR, "Too big picture %dx%d, max supported size is %dx%d\n",
  982. opengl->width, opengl->height, opengl->max_texture_size, opengl->max_texture_size);
  983. ret = AVERROR(EINVAL);
  984. goto fail;
  985. }
  986. if ((ret = opengl_load_procedures(opengl)) < 0)
  987. goto fail;
  988. opengl_fill_color_map(opengl);
  989. opengl_get_texture_params(opengl);
  990. if ((ret = opengl_init_context(opengl)) < 0)
  991. goto fail;
  992. if ((ret = opengl_prepare_vertex(h)) < 0)
  993. goto fail;
  994. glClear(GL_COLOR_BUFFER_BIT);
  995. #if CONFIG_SDL2
  996. if (!opengl->no_window)
  997. SDL_GL_SwapWindow(opengl->window);
  998. #endif
  999. if (opengl->no_window &&
  1000. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {
  1001. av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
  1002. goto fail;
  1003. }
  1004. ret = AVERROR_EXTERNAL;
  1005. OPENGL_ERROR_CHECK(opengl);
  1006. opengl->inited = 1;
  1007. return 0;
  1008. fail:
  1009. opengl_write_trailer(h);
  1010. return ret;
  1011. }
  1012. static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, int comp_index,
  1013. const AVPixFmtDescriptor *desc)
  1014. {
  1015. uint8_t *data = pkt->data;
  1016. int wordsize = opengl_type_size(opengl->type);
  1017. int width_chroma = AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w);
  1018. int height_chroma = AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h);
  1019. int plane = desc->comp[comp_index].plane;
  1020. switch(plane) {
  1021. case 0:
  1022. break;
  1023. case 1:
  1024. data += opengl->width * opengl->height * wordsize;
  1025. break;
  1026. case 2:
  1027. data += opengl->width * opengl->height * wordsize;
  1028. data += width_chroma * height_chroma * wordsize;
  1029. break;
  1030. case 3:
  1031. data += opengl->width * opengl->height * wordsize;
  1032. data += 2 * width_chroma * height_chroma * wordsize;
  1033. break;
  1034. default:
  1035. return NULL;
  1036. }
  1037. return data;
  1038. }
  1039. #define LOAD_TEXTURE_DATA(comp_index, sub) \
  1040. { \
  1041. int width = sub ? AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width; \
  1042. int height = sub ? AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h): opengl->height; \
  1043. uint8_t *data; \
  1044. int plane = desc->comp[comp_index].plane; \
  1045. \
  1046. glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]); \
  1047. if (!is_pkt) { \
  1048. GLint length = ((AVFrame *)input)->linesize[plane]; \
  1049. int bytes_per_pixel = opengl_type_size(opengl->type); \
  1050. if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) \
  1051. bytes_per_pixel *= desc->nb_components; \
  1052. data = ((AVFrame *)input)->data[plane]; \
  1053. if (!(length % bytes_per_pixel) && \
  1054. (opengl->unpack_subimage || ((length / bytes_per_pixel) == width))) { \
  1055. length /= bytes_per_pixel; \
  1056. if (length != width) \
  1057. glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, length); \
  1058. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
  1059. opengl->format, opengl->type, data); \
  1060. if (length != width) \
  1061. glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, 0); \
  1062. } else { \
  1063. int h; \
  1064. for (h = 0; h < height; h++) { \
  1065. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, h, width, 1, \
  1066. opengl->format, opengl->type, data); \
  1067. data += length; \
  1068. } \
  1069. } \
  1070. } else { \
  1071. data = opengl_get_plane_pointer(opengl, input, comp_index, desc); \
  1072. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
  1073. opengl->format, opengl->type, data); \
  1074. } \
  1075. }
  1076. static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
  1077. {
  1078. OpenGLContext *opengl = h->priv_data;
  1079. enum AVPixelFormat pix_fmt = h->streams[0]->codecpar->format;
  1080. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  1081. int ret;
  1082. #if CONFIG_SDL2
  1083. /* At this point, opengl->glcontext implies opengl->glcontext */
  1084. if (opengl->glcontext)
  1085. SDL_GL_MakeCurrent(opengl->window, opengl->glcontext);
  1086. if (!opengl->no_window && (ret = opengl_sdl_process_events(h)) < 0)
  1087. goto fail;
  1088. #endif
  1089. if (opengl->no_window &&
  1090. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0)) < 0) {
  1091. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  1092. goto fail;
  1093. }
  1094. glClear(GL_COLOR_BUFFER_BIT);
  1095. if (!repaint) {
  1096. if (is_pkt)
  1097. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1098. LOAD_TEXTURE_DATA(0, 0)
  1099. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
  1100. LOAD_TEXTURE_DATA(1, 1)
  1101. LOAD_TEXTURE_DATA(2, 1)
  1102. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
  1103. LOAD_TEXTURE_DATA(3, 0)
  1104. }
  1105. }
  1106. ret = AVERROR_EXTERNAL;
  1107. OPENGL_ERROR_CHECK(opengl);
  1108. if ((ret = opengl_prepare(opengl)) < 0)
  1109. goto fail;
  1110. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
  1111. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
  1112. opengl->glprocs.glVertexAttribPointer(opengl->position_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 0);
  1113. opengl->glprocs.glEnableVertexAttribArray(opengl->position_attrib);
  1114. opengl->glprocs.glVertexAttribPointer(opengl->texture_coords_attrib, 2, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 12);
  1115. opengl->glprocs.glEnableVertexAttribArray(opengl->texture_coords_attrib);
  1116. glDrawElements(GL_TRIANGLES, FF_ARRAY_ELEMS(g_index), GL_UNSIGNED_SHORT, 0);
  1117. ret = AVERROR_EXTERNAL;
  1118. OPENGL_ERROR_CHECK(opengl);
  1119. #if CONFIG_SDL2
  1120. if (!opengl->no_window)
  1121. SDL_GL_SwapWindow(opengl->window);
  1122. #endif
  1123. if (opengl->no_window &&
  1124. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {
  1125. av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
  1126. goto fail;
  1127. }
  1128. return 0;
  1129. fail:
  1130. return ret;
  1131. }
  1132. static int opengl_write_packet(AVFormatContext *h, AVPacket *pkt)
  1133. {
  1134. AVCodecParameters *par = h->streams[0]->codecpar;
  1135. if (par->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  1136. AVFrame *frame = (AVFrame *)pkt->data;
  1137. return opengl_draw(h, frame, 0, 0);
  1138. } else {
  1139. return opengl_draw(h, pkt, 0, 1);
  1140. }
  1141. }
  1142. static int opengl_write_frame(AVFormatContext *h, int stream_index,
  1143. AVFrame **frame, unsigned flags)
  1144. {
  1145. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  1146. return 0;
  1147. return opengl_draw(h, *frame, 0, 0);
  1148. }
  1149. #define OFFSET(x) offsetof(OpenGLContext, x)
  1150. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  1151. static const AVOption options[] = {
  1152. { "background", "set background color", OFFSET(background), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, ENC },
  1153. { "no_window", "disable default window", OFFSET(no_window), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  1154. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, ENC },
  1155. { "window_size", "set window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, ENC },
  1156. { NULL }
  1157. };
  1158. static const AVClass opengl_class = {
  1159. .class_name = "opengl outdev",
  1160. .item_name = av_default_item_name,
  1161. .option = options,
  1162. .version = LIBAVUTIL_VERSION_INT,
  1163. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  1164. };
  1165. const FFOutputFormat ff_opengl_muxer = {
  1166. .p.name = "opengl",
  1167. .p.long_name = NULL_IF_CONFIG_SMALL("OpenGL output"),
  1168. .p.audio_codec = AV_CODEC_ID_NONE,
  1169. .p.video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
  1170. .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  1171. .p.priv_class = &opengl_class,
  1172. .priv_data_size = sizeof(OpenGLContext),
  1173. .write_header = opengl_write_header,
  1174. .write_packet = opengl_write_packet,
  1175. .write_uncoded_frame = opengl_write_frame,
  1176. .write_trailer = opengl_write_trailer,
  1177. .control_message = opengl_control_message,
  1178. };