Miam-Player  0.8.0
A nice music player
VideoShader_p.h
Go to the documentation of this file.
1 /******************************************************************************
2  QtAV: Multimedia framework based on Qt and FFmpeg
3  Copyright (C) 2012-2016 Wang Bin <wbsecg1@gmail.com>
4 
5 * This file is part of QtAV (from 2014)
6 
7  This library 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  This library 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 this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ******************************************************************************/
21 
22 #ifndef QTAV_VIDEOSHADER_P_H
23 #define QTAV_VIDEOSHADER_P_H
24 
25 #include "QtAV/OpenGLTypes.h"
26 #include "QtAV/VideoFrame.h"
27 #include "ColorTransform.h"
28 #include <QVector4D>
29 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
30 #include <QtGui/QOpenGLBuffer>
31 #include <QtGui/QOpenGLShaderProgram>
32 #include <QtGui/QOpenGLFunctions>
33 #else
34 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
35 #include <QtOpenGL/QGLFunctions>
36 #endif
37 #include <QtOpenGL/QGLBuffer>
38 #include <QtOpenGL/QGLShaderProgram>
39 typedef QGLBuffer QOpenGLBuffer;
40 #define QOpenGLShaderProgram QGLShaderProgram
41 #define QOpenGLShader QGLShader
42 #define QOpenGLFunctions QGLFunctions
43 #define QOpenGLContext QGLContext
44 #endif
45 
46 namespace QtAV {
47 // can not move to OpenGLHelper.h because that's not public/private header
48 enum ShaderType {
52 };
53 
54 class VideoShader;
56 {
57 public:
59  : owns_program(false)
60  , rebuild_program(false)
61  , update_builtin_uniforms(true)
62  , program(0)
63  , u_Matrix(-1)
64  , u_colorMatrix(-1)
65  , u_to8(-1)
66  , u_opacity(-1)
67  , u_c(-1)
68  , material_type(0)
69  , texture_target(GL_TEXTURE_2D)
70  {}
71  virtual ~VideoShaderPrivate() {
72  if (owns_program && program) {
73  if (QOpenGLContext::currentContext()) {
74  // FIXME: may be not called from renderering thread. so we still have to detach shaders
75  program->removeAllShaders();
76  }
77  delete program;
78  }
79  program = 0;
80  }
81 
82  bool owns_program; // shader program is not created by this. e.g. scene graph create it's own program and we store it here
84  bool update_builtin_uniforms; //builtin uniforms are static, set the values once is enough if no change
85  QOpenGLShaderProgram *program;
86  int u_Matrix;
88  int u_to8;
89  int u_opacity;
90  int u_c;
93  qint32 material_type;
94  QVector<int> u_Texture;
97  mutable QByteArray planar_frag, packed_frag;
98  mutable QByteArray vert;
99  QVector<Uniform> user_uniforms[ShaderTypeCount];
100 };
101 
102 class VideoMaterial;
103 class VideoMaterialPrivate : public DPtrPrivate<VideoMaterial>
104 {
105 public:
107  : update_texure(true)
108  , init_textures_required(true)
109  , bpc(0)
110  , width(0)
111  , height(0)
112  , video_format(VideoFormat::Format_Invalid)
113  , plane1_linesize(0)
114  , effective_tex_width_ratio(1.0)
115  , target(GL_TEXTURE_2D)
116  , dirty(true)
117  , try_pbo(true)
118  {
119  v_texel_size.reserve(4);
120  textures.reserve(4);
121  texture_size.reserve(4);
122  texture_upload_size.reserve(4);
123  effective_tex_width.reserve(4);
124  internal_format.reserve(4);
125  data_format.reserve(4);
126  data_type.reserve(4);
127  texture_coords.reserve(4);
128  static bool enable_pbo = qgetenv("QTAV_PBO").toInt() > 0;
129  if (try_pbo)
130  try_pbo = enable_pbo;
131  pbo.reserve(4);
132  colorTransform.setOutputColorSpace(ColorSpace_RGB);
133  }
135  bool initPBO(int plane, int size);
136  bool initTexture(GLuint tex, GLint internal_format, GLenum format, GLenum dataType, int width, int height);
137  bool updateTextureParameters(const VideoFormat& fmt);
138  bool ensureResources();
139  bool ensureTextures();
140  void setupQuality();
141 
142  bool update_texure; // reduce upload/map times. true: new frame not bound. false: current frame is bound
143  bool init_textures_required; // e.g. target changed
144  int bpc;
145  int width, height; //avoid accessing frame(need lock)
147  /*
148  * old format. used to check whether we have to update textures. set to current frame's format after textures are updated.
149  * TODO: only VideoMaterial.type() is enough to check and update shader. so remove it
150  */
152  QSize plane0Size;
153  // width is in bytes. different alignments may result in different plane 1 linesize even if plane 0 are the same
155 
156  // textures.d in updateTextureParameters() changed. happens in qml. why?
157  quint8 workaround_vector_crash_on_linux[8]; //TODO: remove
158  QVector<GLuint> textures; //texture ids. size is plane count
159  QHash<GLuint, bool> owns_texture;
160  QVector<QSize> texture_size;
161  /*
162  * actually if render a full frame, only plane 0 is enough. other planes are the same as texture size.
163  * because linesize[0]>=linesize[1]
164  * uploade size is required when
165  * 1. y/u is not an integer because of alignment. then padding size of y < padding size of u, and effective size y/u != texture size y/u
166  * 2. odd size. enlarge y
167  */
168  QVector<QSize> texture_upload_size;
169 
170  QVector<int> effective_tex_width; //without additional width for alignment
172  GLenum target;
173  QVector<GLint> internal_format;
174  QVector<GLenum> data_format;
175  QVector<GLenum> data_type;
176 
177  QVector<GLfloat> texture_coords;
178  bool dirty;
179  ColorTransform colorTransform;
180  bool try_pbo;
181  QVector<QOpenGLBuffer> pbo;
182  QVector2D vec_to8; //TODO: vec3 to support both RG and LA (.rga, vec_to8)
183  QMatrix4x4 channel_map;
184  QVector<QVector2D> v_texel_size;
185  QVector<QVector2D> v_texture_size;
186 };
187 
188 } //namespace QtAV
189 
190 #endif // QTAV_VideoShader_P_H
QVector< GLenum > data_type
Definition: VideoShader_p.h:175
int u_Matrix
Definition: VideoShader_p.h:86
QByteArray vert
Definition: VideoShader_p.h:98
bool dirty
Definition: VideoShader_p.h:178
int plane1_linesize
Definition: VideoShader_p.h:154
The VideoFormat class Describes the layout of video data.
Definition: VideoFormat.h:39
VideoFormat video_format
Definition: VideoShader_p.h:96
QVector< QVector2D > v_texture_size
Definition: VideoShader_p.h:185
Definition: VideoShader_p.h:51
QOpenGLShaderProgram * program
Definition: VideoShader_p.h:85
QVector< QOpenGLBuffer > pbo
Definition: VideoShader_p.h:181
int u_opacity
Definition: VideoShader_p.h:89
size_t size
Definition: mem.h:207
QVector< QSize > texture_upload_size
Definition: VideoShader_p.h:168
bool init_textures_required
Definition: VideoShader_p.h:143
Definition: VideoShader_p.h:50
#define Q_AV_PRIVATE_EXPORT
Definition: QtAV_Global.h:43
QVector< GLfloat > texture_coords
Definition: VideoShader_p.h:177
VideoMaterialPrivate()
Definition: VideoShader_p.h:106
VideoShaderPrivate()
Definition: VideoShader_p.h:58
VideoFormat video_format
Definition: VideoShader_p.h:151
VideoFrame frame
Definition: VideoShader_p.h:146
int width
Definition: VideoShader_p.h:145
bool owns_program
Definition: VideoShader_p.h:82
ColorTransform colorTransform
Definition: VideoShader_p.h:179
QVector< QVector2D > v_texel_size
Definition: VideoShader_p.h:184
int bpc
Definition: VideoShader_p.h:144
QVector< GLuint > textures
Definition: VideoShader_p.h:158
int u_to8
Definition: VideoShader_p.h:88
Definition: VideoShader_p.h:103
Definition: VideoShader_p.h:49
The VideoShader class Represents a shader for rendering a video frame.
Definition: VideoShader.h:53
QSize plane0Size
Definition: VideoShader_p.h:152
Definition: VideoShader_p.h:55
Definition: VideoFrame.h:32
ShaderType
Definition: VideoShader_p.h:48
GLenum texture_target
Definition: VideoShader_p.h:95
QHash< GLuint, bool > owns_texture
Definition: VideoShader_p.h:159
qreal effective_tex_width_ratio
Definition: VideoShader_p.h:171
QVector< GLint > internal_format
Definition: VideoShader_p.h:173
QVector< GLenum > data_format
Definition: VideoShader_p.h:174
int u_textureSize
Definition: VideoShader_p.h:92
Definition: dptr.h:123
The VideoMaterial class Encapsulates rendering state for a video shader program.
Definition: VideoShader.h:179
int u_texelSize
Definition: VideoShader_p.h:91
GLenum target
Definition: VideoShader_p.h:172
int u_colorMatrix
Definition: VideoShader_p.h:87
QVector< int > effective_tex_width
Definition: VideoShader_p.h:170
qint32 material_type
Definition: VideoShader_p.h:93
QVector< int > u_Texture
Definition: VideoShader_p.h:94
bool update_texure
Definition: VideoShader_p.h:142
int u_c
Definition: VideoShader_p.h:90
QByteArray planar_frag
Definition: VideoShader_p.h:97
bool try_pbo
Definition: VideoShader_p.h:180
AudioOutput ao; ao.setAudioFormat(fmt); ao.open(); while (has_data) { data = read_data(ao->bufferSize...
Definition: AudioDecoder.h:31
QVector< QSize > texture_size
Definition: VideoShader_p.h:160
virtual ~VideoShaderPrivate()
Definition: VideoShader_p.h:71
Definition: QtAV_Global.h:124
bool update_builtin_uniforms
Definition: VideoShader_p.h:84
QMatrix4x4 channel_map
Definition: VideoShader_p.h:183
bool rebuild_program
Definition: VideoShader_p.h:83
QVector2D vec_to8
Definition: VideoShader_p.h:182