renderer_yuv420sp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2016 Bilibili
  3. * copyright (c) 2016 Zhang Rui <bbcallen@gmail.com>
  4. *
  5. * This file is part of ijkPlayer.
  6. *
  7. * ijkPlayer is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * ijkPlayer is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with ijkPlayer; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "internal.h"
  22. #ifdef __APPLE__
  23. #import <CoreVideo/CoreVideo.h>
  24. #include "ijksdl_vout_overlay_videotoolbox.h"
  25. #endif
  26. static GLboolean yuv420sp_use(IJK_GLES2_Renderer *renderer)
  27. {
  28. ALOGI("use render yuv420sp\n");
  29. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  30. glUseProgram(renderer->program); IJK_GLES2_checkError_TRACE("glUseProgram");
  31. if (0 == renderer->plane_textures[0])
  32. glGenTextures(2, renderer->plane_textures);
  33. for (int i = 0; i < 2; ++i) {
  34. glActiveTexture(GL_TEXTURE0 + i);
  35. glBindTexture(GL_TEXTURE_2D, renderer->plane_textures[i]);
  36. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  37. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  38. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  39. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  40. glUniform1i(renderer->us2_sampler[i], i);
  41. }
  42. glUniformMatrix3fv(renderer->um3_color_conversion, 1, GL_FALSE, IJK_GLES2_getColorMatrix_bt709());
  43. return GL_TRUE;
  44. }
  45. static GLsizei yuv420sp_getBufferWidth(IJK_GLES2_Renderer *renderer, SDL_VoutOverlay *overlay)
  46. {
  47. if (!overlay)
  48. return 0;
  49. return overlay->pitches[0] / 1;
  50. }
  51. static GLboolean yuv420sp_uploadTexture(IJK_GLES2_Renderer *renderer, SDL_VoutOverlay *overlay)
  52. {
  53. if (!renderer || !overlay)
  54. return GL_FALSE;
  55. const GLsizei widths[2] = { overlay->pitches[0], overlay->pitches[1] / 2 };
  56. const GLsizei heights[2] = { overlay->h, overlay->h / 2 };
  57. const GLubyte *pixels[2] = { overlay->pixels[0], overlay->pixels[1] };
  58. switch (overlay->format) {
  59. case SDL_FCC__VTB:
  60. break;
  61. default:
  62. ALOGE("[yuv420sp] unexpected format %x\n", overlay->format);
  63. return GL_FALSE;
  64. }
  65. glBindTexture(GL_TEXTURE_2D, renderer->plane_textures[0]);
  66. glTexImage2D(GL_TEXTURE_2D,
  67. 0,
  68. GL_RED_EXT,
  69. widths[0],
  70. heights[0],
  71. 0,
  72. GL_RED_EXT,
  73. GL_UNSIGNED_BYTE,
  74. pixels[0]);
  75. glBindTexture(GL_TEXTURE_2D, renderer->plane_textures[1]);
  76. glTexImage2D(GL_TEXTURE_2D,
  77. 0,
  78. GL_RG_EXT,
  79. widths[1],
  80. heights[1],
  81. 0,
  82. GL_RG_EXT,
  83. GL_UNSIGNED_BYTE,
  84. pixels[1]);
  85. return GL_TRUE;
  86. }
  87. IJK_GLES2_Renderer *IJK_GLES2_Renderer_create_yuv420sp()
  88. {
  89. IJK_GLES2_Renderer *renderer = IJK_GLES2_Renderer_create_base(IJK_GLES2_getFragmentShader_yuv420sp());
  90. if (!renderer)
  91. goto fail;
  92. renderer->us2_sampler[0] = glGetUniformLocation(renderer->program, "us2_SamplerX"); IJK_GLES2_checkError_TRACE("glGetUniformLocation(us2_SamplerX)");
  93. renderer->us2_sampler[1] = glGetUniformLocation(renderer->program, "us2_SamplerY"); IJK_GLES2_checkError_TRACE("glGetUniformLocation(us2_SamplerY)");
  94. renderer->um3_color_conversion = glGetUniformLocation(renderer->program, "um3_ColorConversion"); IJK_GLES2_checkError_TRACE("glGetUniformLocation(um3_ColorConversionMatrix)");
  95. renderer->func_use = yuv420sp_use;
  96. renderer->func_getBufferWidth = yuv420sp_getBufferWidth;
  97. renderer->func_uploadTexture = yuv420sp_uploadTexture;
  98. return renderer;
  99. fail:
  100. IJK_GLES2_Renderer_free(renderer);
  101. return NULL;
  102. }