opengl_enc.c 55 KB

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