opengl_enc.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. #define WIN32_LEAN_AND_MEAN
  30. #include <windows.h>
  31. #endif
  32. #if HAVE_OPENGL_GL3_H
  33. #include <OpenGL/gl3.h>
  34. #elif HAVE_ES2_GL_H
  35. #include <ES2/gl.h>
  36. #else
  37. #include <GL/gl.h>
  38. #include <GL/glext.h>
  39. #endif
  40. #if HAVE_GLXGETPROCADDRESS
  41. #include <GL/glx.h>
  42. #endif
  43. #if CONFIG_SDL2
  44. #include <SDL.h>
  45. #endif
  46. #include "libavutil/common.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", __FUNCTION__, __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. sscanf(version, "%d.%d", &major, &minor);
  550. for (i = 0; required_extensions[i].extension; i++) {
  551. if (major < required_extensions[i].major &&
  552. (major == required_extensions[i].major && minor < required_extensions[i].minor) &&
  553. !strstr(extensions, required_extensions[i].extension)) {
  554. av_log(h, AV_LOG_ERROR, "Required extension %s is not supported.\n",
  555. required_extensions[i].extension);
  556. av_log(h, AV_LOG_DEBUG, "Supported extensions are: %s\n", extensions);
  557. return AVERROR(ENOSYS);
  558. }
  559. }
  560. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &opengl->max_texture_size);
  561. glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &opengl->max_viewport_width);
  562. opengl->non_pow_2_textures = major >= 2 || strstr(extensions, "GL_ARB_texture_non_power_of_two");
  563. #if defined(GL_ES_VERSION_2_0)
  564. opengl->unpack_subimage = !!strstr(extensions, "GL_EXT_unpack_subimage");
  565. #else
  566. opengl->unpack_subimage = 1;
  567. #endif
  568. av_log(h, AV_LOG_DEBUG, "Non Power of 2 textures support: %s\n", opengl->non_pow_2_textures ? "Yes" : "No");
  569. av_log(h, AV_LOG_DEBUG, "Unpack Subimage extension support: %s\n", opengl->unpack_subimage ? "Yes" : "No");
  570. av_log(h, AV_LOG_DEBUG, "Max texture size: %dx%d\n", opengl->max_texture_size, opengl->max_texture_size);
  571. av_log(h, AV_LOG_DEBUG, "Max viewport size: %dx%d\n",
  572. opengl->max_viewport_width, opengl->max_viewport_height);
  573. OPENGL_ERROR_CHECK(opengl);
  574. return 0;
  575. fail:
  576. return AVERROR_EXTERNAL;
  577. }
  578. static const char* opengl_get_fragment_shader_code(enum AVPixelFormat format)
  579. {
  580. int i;
  581. for (i = 0; i < FF_ARRAY_ELEMS(opengl_format_desc); i++) {
  582. if (opengl_format_desc[i].fixel_format == format)
  583. return *opengl_format_desc[i].fragment_shader;
  584. }
  585. return NULL;
  586. }
  587. static int opengl_type_size(GLenum type)
  588. {
  589. switch(type) {
  590. case GL_UNSIGNED_SHORT:
  591. case FF_GL_UNSIGNED_SHORT_1_5_5_5_REV:
  592. case GL_UNSIGNED_SHORT_5_6_5:
  593. return 2;
  594. case GL_UNSIGNED_BYTE:
  595. case FF_GL_UNSIGNED_BYTE_3_3_2:
  596. case FF_GL_UNSIGNED_BYTE_2_3_3_REV:
  597. default:
  598. break;
  599. }
  600. return 1;
  601. }
  602. static av_cold void opengl_get_texture_params(OpenGLContext *opengl)
  603. {
  604. int i;
  605. for (i = 0; i < FF_ARRAY_ELEMS(opengl_format_desc); i++) {
  606. if (opengl_format_desc[i].fixel_format == opengl->pix_fmt) {
  607. opengl->format = opengl_format_desc[i].format;
  608. opengl->type = opengl_format_desc[i].type;
  609. break;
  610. }
  611. }
  612. }
  613. static void opengl_compute_display_area(AVFormatContext *s)
  614. {
  615. AVRational sar, dar; /* sample and display aspect ratios */
  616. OpenGLContext *opengl = s->priv_data;
  617. AVStream *st = s->streams[0];
  618. AVCodecParameters *par = st->codecpar;
  619. /* compute overlay width and height from the codec context information */
  620. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  621. dar = av_mul_q(sar, (AVRational){ par->width, par->height });
  622. /* we suppose the screen has a 1/1 sample aspect ratio */
  623. /* fit in the window */
  624. if (av_cmp_q(dar, (AVRational){ opengl->window_width, opengl->window_height }) > 0) {
  625. /* fit in width */
  626. opengl->picture_width = opengl->window_width;
  627. opengl->picture_height = av_rescale(opengl->picture_width, dar.den, dar.num);
  628. } else {
  629. /* fit in height */
  630. opengl->picture_height = opengl->window_height;
  631. opengl->picture_width = av_rescale(opengl->picture_height, dar.num, dar.den);
  632. }
  633. }
  634. static av_cold void opengl_get_texture_size(OpenGLContext *opengl, int in_width, int in_height,
  635. int *out_width, int *out_height)
  636. {
  637. if (opengl->non_pow_2_textures) {
  638. *out_width = in_width;
  639. *out_height = in_height;
  640. } else {
  641. int max = FFMIN(FFMAX(in_width, in_height), opengl->max_texture_size);
  642. unsigned power_of_2 = 1;
  643. while (power_of_2 < max)
  644. power_of_2 *= 2;
  645. *out_height = power_of_2;
  646. *out_width = power_of_2;
  647. av_log(opengl, AV_LOG_DEBUG, "Texture size calculated from %dx%d into %dx%d\n",
  648. in_width, in_height, *out_width, *out_height);
  649. }
  650. }
  651. static av_cold void opengl_fill_color_map(OpenGLContext *opengl)
  652. {
  653. const AVPixFmtDescriptor *desc;
  654. int shift;
  655. enum AVPixelFormat pix_fmt = opengl->pix_fmt;
  656. /* We need order of components, not exact position, some minor HACKs here */
  657. if (pix_fmt == AV_PIX_FMT_RGB565 || pix_fmt == AV_PIX_FMT_BGR555 ||
  658. pix_fmt == AV_PIX_FMT_BGR8 || pix_fmt == AV_PIX_FMT_RGB8)
  659. pix_fmt = AV_PIX_FMT_RGB24;
  660. else if (pix_fmt == AV_PIX_FMT_BGR565 || pix_fmt == AV_PIX_FMT_RGB555)
  661. pix_fmt = AV_PIX_FMT_BGR24;
  662. desc = av_pix_fmt_desc_get(pix_fmt);
  663. if (!(desc->flags & AV_PIX_FMT_FLAG_RGB))
  664. return;
  665. #define FILL_COMPONENT(i) { \
  666. shift = (desc->comp[i].depth - 1) >> 3; \
  667. opengl->color_map[(i << 2) + (desc->comp[i].offset >> shift)] = 1.0; \
  668. }
  669. memset(opengl->color_map, 0, sizeof(opengl->color_map));
  670. FILL_COMPONENT(0);
  671. FILL_COMPONENT(1);
  672. FILL_COMPONENT(2);
  673. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
  674. FILL_COMPONENT(3);
  675. #undef FILL_COMPONENT
  676. }
  677. static av_cold GLuint opengl_load_shader(OpenGLContext *opengl, GLenum type, const char *source)
  678. {
  679. GLuint shader = opengl->glprocs.glCreateShader(type);
  680. GLint result;
  681. if (!shader) {
  682. av_log(opengl, AV_LOG_ERROR, "glCreateShader() failed\n");
  683. return 0;
  684. }
  685. opengl->glprocs.glShaderSource(shader, 1, &source, NULL);
  686. opengl->glprocs.glCompileShader(shader);
  687. opengl->glprocs.glGetShaderiv(shader, FF_GL_COMPILE_STATUS, &result);
  688. if (!result) {
  689. char *log;
  690. opengl->glprocs.glGetShaderiv(shader, FF_GL_INFO_LOG_LENGTH, &result);
  691. if (result) {
  692. if ((log = av_malloc(result))) {
  693. opengl->glprocs.glGetShaderInfoLog(shader, result, NULL, log);
  694. av_log(opengl, AV_LOG_ERROR, "Compile error: %s\n", log);
  695. av_free(log);
  696. }
  697. }
  698. goto fail;
  699. }
  700. OPENGL_ERROR_CHECK(opengl);
  701. return shader;
  702. fail:
  703. opengl->glprocs.glDeleteShader(shader);
  704. return 0;
  705. }
  706. static av_cold int opengl_compile_shaders(OpenGLContext *opengl, enum AVPixelFormat pix_fmt)
  707. {
  708. GLint result;
  709. const char *fragment_shader_code = opengl_get_fragment_shader_code(pix_fmt);
  710. if (!fragment_shader_code) {
  711. av_log(opengl, AV_LOG_ERROR, "Provided pixel format '%s' is not supported\n",
  712. av_get_pix_fmt_name(pix_fmt));
  713. return AVERROR(EINVAL);
  714. }
  715. opengl->vertex_shader = opengl_load_shader(opengl, FF_GL_VERTEX_SHADER,
  716. FF_OPENGL_VERTEX_SHADER);
  717. if (!opengl->vertex_shader) {
  718. av_log(opengl, AV_LOG_ERROR, "Vertex shader loading failed.\n");
  719. goto fail;
  720. }
  721. opengl->fragment_shader = opengl_load_shader(opengl, FF_GL_FRAGMENT_SHADER,
  722. fragment_shader_code);
  723. if (!opengl->fragment_shader) {
  724. av_log(opengl, AV_LOG_ERROR, "Fragment shader loading failed.\n");
  725. goto fail;
  726. }
  727. opengl->program = opengl->glprocs.glCreateProgram();
  728. if (!opengl->program)
  729. goto fail;
  730. opengl->glprocs.glAttachShader(opengl->program, opengl->vertex_shader);
  731. opengl->glprocs.glAttachShader(opengl->program, opengl->fragment_shader);
  732. opengl->glprocs.glLinkProgram(opengl->program);
  733. opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_LINK_STATUS, &result);
  734. if (!result) {
  735. char *log;
  736. opengl->glprocs.glGetProgramiv(opengl->program, FF_GL_INFO_LOG_LENGTH, &result);
  737. if (result) {
  738. log = av_malloc(result);
  739. if (!log)
  740. goto fail;
  741. opengl->glprocs.glGetProgramInfoLog(opengl->program, result, NULL, log);
  742. av_log(opengl, AV_LOG_ERROR, "Link error: %s\n", log);
  743. av_free(log);
  744. }
  745. goto fail;
  746. }
  747. opengl->position_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_position");
  748. opengl->texture_coords_attrib = opengl->glprocs.glGetAttribLocation(opengl->program, "a_textureCoords");
  749. opengl->projection_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_projectionMatrix");
  750. opengl->model_view_matrix_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_modelViewMatrix");
  751. opengl->color_map_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_colorMap");
  752. opengl->texture_location[0] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture0");
  753. opengl->texture_location[1] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture1");
  754. opengl->texture_location[2] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture2");
  755. opengl->texture_location[3] = opengl->glprocs.glGetUniformLocation(opengl->program, "u_texture3");
  756. opengl->chroma_div_w_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_w");
  757. opengl->chroma_div_h_location = opengl->glprocs.glGetUniformLocation(opengl->program, "u_chroma_div_h");
  758. OPENGL_ERROR_CHECK(opengl);
  759. return 0;
  760. fail:
  761. opengl->glprocs.glDeleteShader(opengl->vertex_shader);
  762. opengl->glprocs.glDeleteShader(opengl->fragment_shader);
  763. opengl->glprocs.glDeleteProgram(opengl->program);
  764. opengl->fragment_shader = opengl->vertex_shader = opengl->program = 0;
  765. return AVERROR_EXTERNAL;
  766. }
  767. static av_cold int opengl_configure_texture(OpenGLContext *opengl, GLuint texture,
  768. GLsizei width, GLsizei height)
  769. {
  770. if (texture) {
  771. int new_width, new_height;
  772. opengl_get_texture_size(opengl, width, height, &new_width, &new_height);
  773. glBindTexture(GL_TEXTURE_2D, texture);
  774. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  775. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  776. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  777. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  778. glTexImage2D(GL_TEXTURE_2D, 0, opengl->format, new_width, new_height, 0,
  779. opengl->format, opengl->type, NULL);
  780. OPENGL_ERROR_CHECK(NULL);
  781. }
  782. return 0;
  783. fail:
  784. return AVERROR_EXTERNAL;
  785. }
  786. static av_cold int opengl_prepare_vertex(AVFormatContext *s)
  787. {
  788. OpenGLContext *opengl = s->priv_data;
  789. int tex_w, tex_h;
  790. if (opengl->window_width > opengl->max_viewport_width || opengl->window_height > opengl->max_viewport_height) {
  791. opengl->window_width = FFMIN(opengl->window_width, opengl->max_viewport_width);
  792. opengl->window_height = FFMIN(opengl->window_height, opengl->max_viewport_height);
  793. av_log(opengl, AV_LOG_WARNING, "Too big viewport requested, limited to %dx%d", opengl->window_width, opengl->window_height);
  794. }
  795. glViewport(0, 0, opengl->window_width, opengl->window_height);
  796. opengl_make_ortho(opengl->projection_matrix,
  797. - (float)opengl->window_width / 2.0f, (float)opengl->window_width / 2.0f,
  798. - (float)opengl->window_height / 2.0f, (float)opengl->window_height / 2.0f,
  799. 1.0f, -1.0f);
  800. opengl_make_identity(opengl->model_view_matrix);
  801. opengl_compute_display_area(s);
  802. opengl->vertex[0].z = opengl->vertex[1].z = opengl->vertex[2].z = opengl->vertex[3].z = 0.0f;
  803. opengl->vertex[0].x = opengl->vertex[1].x = - (float)opengl->picture_width / 2.0f;
  804. opengl->vertex[2].x = opengl->vertex[3].x = (float)opengl->picture_width / 2.0f;
  805. opengl->vertex[1].y = opengl->vertex[2].y = - (float)opengl->picture_height / 2.0f;
  806. opengl->vertex[0].y = opengl->vertex[3].y = (float)opengl->picture_height / 2.0f;
  807. opengl_get_texture_size(opengl, opengl->width, opengl->height, &tex_w, &tex_h);
  808. opengl->vertex[0].s0 = 0.0f;
  809. opengl->vertex[0].t0 = 0.0f;
  810. opengl->vertex[1].s0 = 0.0f;
  811. opengl->vertex[1].t0 = (float)opengl->height / (float)tex_h;
  812. opengl->vertex[2].s0 = (float)opengl->width / (float)tex_w;
  813. opengl->vertex[2].t0 = (float)opengl->height / (float)tex_h;
  814. opengl->vertex[3].s0 = (float)opengl->width / (float)tex_w;
  815. opengl->vertex[3].t0 = 0.0f;
  816. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
  817. opengl->glprocs.glBufferData(FF_GL_ARRAY_BUFFER, sizeof(opengl->vertex), opengl->vertex, FF_GL_STATIC_DRAW);
  818. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, 0);
  819. OPENGL_ERROR_CHECK(opengl);
  820. return 0;
  821. fail:
  822. return AVERROR_EXTERNAL;
  823. }
  824. static int opengl_prepare(OpenGLContext *opengl)
  825. {
  826. int i;
  827. opengl->glprocs.glUseProgram(opengl->program);
  828. opengl->glprocs.glUniformMatrix4fv(opengl->projection_matrix_location, 1, GL_FALSE, opengl->projection_matrix);
  829. opengl->glprocs.glUniformMatrix4fv(opengl->model_view_matrix_location, 1, GL_FALSE, opengl->model_view_matrix);
  830. for (i = 0; i < 4; i++)
  831. if (opengl->texture_location[i] != -1) {
  832. opengl->glprocs.glActiveTexture(GL_TEXTURE0 + i);
  833. glBindTexture(GL_TEXTURE_2D, opengl->texture_name[i]);
  834. opengl->glprocs.glUniform1i(opengl->texture_location[i], i);
  835. }
  836. if (opengl->color_map_location != -1)
  837. opengl->glprocs.glUniformMatrix4fv(opengl->color_map_location, 1, GL_FALSE, opengl->color_map);
  838. if (opengl->chroma_div_h_location != -1)
  839. opengl->glprocs.glUniform1f(opengl->chroma_div_h_location, opengl->chroma_div_h);
  840. if (opengl->chroma_div_w_location != -1)
  841. opengl->glprocs.glUniform1f(opengl->chroma_div_w_location, opengl->chroma_div_w);
  842. OPENGL_ERROR_CHECK(opengl);
  843. return 0;
  844. fail:
  845. return AVERROR_EXTERNAL;
  846. }
  847. static int opengl_create_window(AVFormatContext *h)
  848. {
  849. OpenGLContext *opengl = h->priv_data;
  850. int ret;
  851. if (!opengl->no_window) {
  852. #if CONFIG_SDL2
  853. if ((ret = opengl_sdl_create_window(h)) < 0) {
  854. av_log(opengl, AV_LOG_ERROR, "Cannot create default SDL window.\n");
  855. return ret;
  856. }
  857. #else
  858. av_log(opengl, AV_LOG_ERROR, "FFmpeg is compiled without SDL. Cannot create default window.\n");
  859. return AVERROR(ENOSYS);
  860. #endif
  861. } else {
  862. AVDeviceRect message;
  863. message.x = message.y = 0;
  864. message.width = opengl->window_width;
  865. message.height = opengl->window_height;
  866. if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_CREATE_WINDOW_BUFFER,
  867. &message , sizeof(message))) < 0) {
  868. av_log(opengl, AV_LOG_ERROR, "Application failed to create window buffer.\n");
  869. return ret;
  870. }
  871. if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0)) < 0) {
  872. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  873. return ret;
  874. }
  875. }
  876. return 0;
  877. }
  878. static int opengl_release_window(AVFormatContext *h)
  879. {
  880. int ret;
  881. OpenGLContext *opengl = h->priv_data;
  882. if (!opengl->no_window) {
  883. #if CONFIG_SDL2
  884. SDL_GL_DeleteContext(opengl->glcontext);
  885. SDL_DestroyWindow(opengl->window);
  886. SDL_Quit();
  887. #endif
  888. } else if ((ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER, NULL , 0)) < 0) {
  889. av_log(opengl, AV_LOG_ERROR, "Application failed to release window buffer.\n");
  890. return ret;
  891. }
  892. return 0;
  893. }
  894. static av_cold int opengl_write_trailer(AVFormatContext *h)
  895. {
  896. OpenGLContext *opengl = h->priv_data;
  897. if (opengl->no_window &&
  898. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0) < 0)
  899. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  900. opengl_deinit_context(opengl);
  901. opengl_release_window(h);
  902. return 0;
  903. }
  904. static av_cold int opengl_init_context(OpenGLContext *opengl)
  905. {
  906. int i, ret;
  907. const AVPixFmtDescriptor *desc;
  908. if ((ret = opengl_compile_shaders(opengl, opengl->pix_fmt)) < 0)
  909. goto fail;
  910. desc = av_pix_fmt_desc_get(opengl->pix_fmt);
  911. av_assert0(desc->nb_components > 0 && desc->nb_components <= 4);
  912. glGenTextures(desc->nb_components, opengl->texture_name);
  913. opengl->glprocs.glGenBuffers(2, &opengl->index_buffer);
  914. if (!opengl->index_buffer || !opengl->vertex_buffer) {
  915. av_log(opengl, AV_LOG_ERROR, "Buffer generation failed.\n");
  916. ret = AVERROR_EXTERNAL;
  917. goto fail;
  918. }
  919. opengl_configure_texture(opengl, opengl->texture_name[0], opengl->width, opengl->height);
  920. if (desc->nb_components > 1) {
  921. int has_alpha = desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  922. int num_planes = desc->nb_components - (has_alpha ? 1 : 0);
  923. if (opengl->non_pow_2_textures) {
  924. opengl->chroma_div_w = 1.0f;
  925. opengl->chroma_div_h = 1.0f;
  926. } else {
  927. opengl->chroma_div_w = 1 << desc->log2_chroma_w;
  928. opengl->chroma_div_h = 1 << desc->log2_chroma_h;
  929. }
  930. for (i = 1; i < num_planes; i++)
  931. if (opengl->non_pow_2_textures)
  932. opengl_configure_texture(opengl, opengl->texture_name[i],
  933. AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w),
  934. AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h));
  935. else
  936. opengl_configure_texture(opengl, opengl->texture_name[i], opengl->width, opengl->height);
  937. if (has_alpha)
  938. opengl_configure_texture(opengl, opengl->texture_name[3], opengl->width, opengl->height);
  939. }
  940. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
  941. opengl->glprocs.glBufferData(FF_GL_ELEMENT_ARRAY_BUFFER, sizeof(g_index), g_index, FF_GL_STATIC_DRAW);
  942. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, 0);
  943. glEnable(GL_BLEND);
  944. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  945. glClearColor((float)opengl->background[0] / 255.0f, (float)opengl->background[1] / 255.0f,
  946. (float)opengl->background[2] / 255.0f, 1.0f);
  947. ret = AVERROR_EXTERNAL;
  948. OPENGL_ERROR_CHECK(opengl);
  949. return 0;
  950. fail:
  951. return ret;
  952. }
  953. static av_cold int opengl_write_header(AVFormatContext *h)
  954. {
  955. OpenGLContext *opengl = h->priv_data;
  956. AVCodecParameters *par = h->streams[0]->codecpar;
  957. AVStream *st;
  958. int ret;
  959. if (h->nb_streams != 1 ||
  960. par->codec_type != AVMEDIA_TYPE_VIDEO ||
  961. (par->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME && par->codec_id != AV_CODEC_ID_RAWVIDEO)) {
  962. av_log(opengl, AV_LOG_ERROR, "Only a single raw or wrapped avframe video stream is supported.\n");
  963. return AVERROR(EINVAL);
  964. }
  965. st = h->streams[0];
  966. opengl->width = st->codecpar->width;
  967. opengl->height = st->codecpar->height;
  968. opengl->pix_fmt = st->codecpar->format;
  969. if (!opengl->window_width)
  970. opengl->window_width = opengl->width;
  971. if (!opengl->window_height)
  972. opengl->window_height = opengl->height;
  973. if (!opengl->window_title && !opengl->no_window)
  974. opengl->window_title = av_strdup(h->url);
  975. if ((ret = opengl_create_window(h)))
  976. goto fail;
  977. if ((ret = opengl_read_limits(h)) < 0)
  978. goto fail;
  979. if (opengl->width > opengl->max_texture_size || opengl->height > opengl->max_texture_size) {
  980. av_log(opengl, AV_LOG_ERROR, "Too big picture %dx%d, max supported size is %dx%d\n",
  981. opengl->width, opengl->height, opengl->max_texture_size, opengl->max_texture_size);
  982. ret = AVERROR(EINVAL);
  983. goto fail;
  984. }
  985. if ((ret = opengl_load_procedures(opengl)) < 0)
  986. goto fail;
  987. opengl_fill_color_map(opengl);
  988. opengl_get_texture_params(opengl);
  989. if ((ret = opengl_init_context(opengl)) < 0)
  990. goto fail;
  991. if ((ret = opengl_prepare_vertex(h)) < 0)
  992. goto fail;
  993. glClear(GL_COLOR_BUFFER_BIT);
  994. #if CONFIG_SDL2
  995. if (!opengl->no_window)
  996. SDL_GL_SwapWindow(opengl->window);
  997. #endif
  998. if (opengl->no_window &&
  999. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {
  1000. av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
  1001. goto fail;
  1002. }
  1003. ret = AVERROR_EXTERNAL;
  1004. OPENGL_ERROR_CHECK(opengl);
  1005. opengl->inited = 1;
  1006. return 0;
  1007. fail:
  1008. opengl_write_trailer(h);
  1009. return ret;
  1010. }
  1011. static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, int comp_index,
  1012. const AVPixFmtDescriptor *desc)
  1013. {
  1014. uint8_t *data = pkt->data;
  1015. int wordsize = opengl_type_size(opengl->type);
  1016. int width_chroma = AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w);
  1017. int height_chroma = AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h);
  1018. int plane = desc->comp[comp_index].plane;
  1019. switch(plane) {
  1020. case 0:
  1021. break;
  1022. case 1:
  1023. data += opengl->width * opengl->height * wordsize;
  1024. break;
  1025. case 2:
  1026. data += opengl->width * opengl->height * wordsize;
  1027. data += width_chroma * height_chroma * wordsize;
  1028. break;
  1029. case 3:
  1030. data += opengl->width * opengl->height * wordsize;
  1031. data += 2 * width_chroma * height_chroma * wordsize;
  1032. break;
  1033. default:
  1034. return NULL;
  1035. }
  1036. return data;
  1037. }
  1038. #define LOAD_TEXTURE_DATA(comp_index, sub) \
  1039. { \
  1040. int width = sub ? AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width; \
  1041. int height = sub ? AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h): opengl->height; \
  1042. uint8_t *data; \
  1043. int plane = desc->comp[comp_index].plane; \
  1044. \
  1045. glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]); \
  1046. if (!is_pkt) { \
  1047. GLint length = ((AVFrame *)input)->linesize[plane]; \
  1048. int bytes_per_pixel = opengl_type_size(opengl->type); \
  1049. if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) \
  1050. bytes_per_pixel *= desc->nb_components; \
  1051. data = ((AVFrame *)input)->data[plane]; \
  1052. if (!(length % bytes_per_pixel) && \
  1053. (opengl->unpack_subimage || ((length / bytes_per_pixel) == width))) { \
  1054. length /= bytes_per_pixel; \
  1055. if (length != width) \
  1056. glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, length); \
  1057. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
  1058. opengl->format, opengl->type, data); \
  1059. if (length != width) \
  1060. glPixelStorei(FF_GL_UNPACK_ROW_LENGTH, 0); \
  1061. } else { \
  1062. int h; \
  1063. for (h = 0; h < height; h++) { \
  1064. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, h, width, 1, \
  1065. opengl->format, opengl->type, data); \
  1066. data += length; \
  1067. } \
  1068. } \
  1069. } else { \
  1070. data = opengl_get_plane_pointer(opengl, input, comp_index, desc); \
  1071. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, \
  1072. opengl->format, opengl->type, data); \
  1073. } \
  1074. }
  1075. static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
  1076. {
  1077. OpenGLContext *opengl = h->priv_data;
  1078. enum AVPixelFormat pix_fmt = h->streams[0]->codecpar->format;
  1079. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  1080. int ret;
  1081. #if CONFIG_SDL2
  1082. if (!opengl->no_window && (ret = opengl_sdl_process_events(h)) < 0)
  1083. goto fail;
  1084. #endif
  1085. if (opengl->no_window &&
  1086. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER, NULL , 0)) < 0) {
  1087. av_log(opengl, AV_LOG_ERROR, "Application failed to prepare window buffer.\n");
  1088. goto fail;
  1089. }
  1090. glClear(GL_COLOR_BUFFER_BIT);
  1091. if (!repaint) {
  1092. if (is_pkt)
  1093. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1094. LOAD_TEXTURE_DATA(0, 0)
  1095. if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
  1096. LOAD_TEXTURE_DATA(1, 1)
  1097. LOAD_TEXTURE_DATA(2, 1)
  1098. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
  1099. LOAD_TEXTURE_DATA(3, 0)
  1100. }
  1101. }
  1102. ret = AVERROR_EXTERNAL;
  1103. OPENGL_ERROR_CHECK(opengl);
  1104. if ((ret = opengl_prepare(opengl)) < 0)
  1105. goto fail;
  1106. opengl->glprocs.glBindBuffer(FF_GL_ARRAY_BUFFER, opengl->vertex_buffer);
  1107. opengl->glprocs.glBindBuffer(FF_GL_ELEMENT_ARRAY_BUFFER, opengl->index_buffer);
  1108. opengl->glprocs.glVertexAttribPointer(opengl->position_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 0);
  1109. opengl->glprocs.glEnableVertexAttribArray(opengl->position_attrib);
  1110. opengl->glprocs.glVertexAttribPointer(opengl->texture_coords_attrib, 2, GL_FLOAT, GL_FALSE, sizeof(OpenGLVertexInfo), 12);
  1111. opengl->glprocs.glEnableVertexAttribArray(opengl->texture_coords_attrib);
  1112. glDrawElements(GL_TRIANGLES, FF_ARRAY_ELEMS(g_index), GL_UNSIGNED_SHORT, 0);
  1113. ret = AVERROR_EXTERNAL;
  1114. OPENGL_ERROR_CHECK(opengl);
  1115. #if CONFIG_SDL2
  1116. if (!opengl->no_window)
  1117. SDL_GL_SwapWindow(opengl->window);
  1118. #endif
  1119. if (opengl->no_window &&
  1120. (ret = avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER, NULL , 0)) < 0) {
  1121. av_log(opengl, AV_LOG_ERROR, "Application failed to display window buffer.\n");
  1122. goto fail;
  1123. }
  1124. return 0;
  1125. fail:
  1126. return ret;
  1127. }
  1128. static int opengl_write_packet(AVFormatContext *h, AVPacket *pkt)
  1129. {
  1130. AVCodecParameters *par = h->streams[0]->codecpar;
  1131. if (par->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  1132. AVFrame *frame = (AVFrame *)pkt->data;
  1133. return opengl_draw(h, frame, 0, 0);
  1134. } else {
  1135. return opengl_draw(h, pkt, 0, 1);
  1136. }
  1137. }
  1138. static int opengl_write_frame(AVFormatContext *h, int stream_index,
  1139. AVFrame **frame, unsigned flags)
  1140. {
  1141. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  1142. return 0;
  1143. return opengl_draw(h, *frame, 0, 0);
  1144. }
  1145. #define OFFSET(x) offsetof(OpenGLContext, x)
  1146. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  1147. static const AVOption options[] = {
  1148. { "background", "set background color", OFFSET(background), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, ENC },
  1149. { "no_window", "disable default window", OFFSET(no_window), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  1150. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, ENC },
  1151. { "window_size", "set window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, ENC },
  1152. { NULL }
  1153. };
  1154. static const AVClass opengl_class = {
  1155. .class_name = "opengl outdev",
  1156. .item_name = av_default_item_name,
  1157. .option = options,
  1158. .version = LIBAVUTIL_VERSION_INT,
  1159. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  1160. };
  1161. const FFOutputFormat ff_opengl_muxer = {
  1162. .p.name = "opengl",
  1163. .p.long_name = NULL_IF_CONFIG_SMALL("OpenGL output"),
  1164. .p.audio_codec = AV_CODEC_ID_NONE,
  1165. .p.video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
  1166. .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  1167. .p.priv_class = &opengl_class,
  1168. .priv_data_size = sizeof(OpenGLContext),
  1169. .write_header = opengl_write_header,
  1170. .write_packet = opengl_write_packet,
  1171. .write_uncoded_frame = opengl_write_frame,
  1172. .write_trailer = opengl_write_trailer,
  1173. .control_message = opengl_control_message,
  1174. };