forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoShader.cpp
952 lines (881 loc) · 32.2 KB
/
VideoShader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014-2015 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/VideoShader.h"
#include "QtAV/private/VideoShader_p.h"
#include "QtAV/ColorTransform.h"
#include "utils/OpenGLHelper.h"
#include <cmath>
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include "utils/Logger.h"
#define YUVA_DONE 0
#define glsl(x) #x "\n"
namespace QtAV {
TexturedGeometry::TexturedGeometry(int count, Triangle t)
: tri(t)
{
v.resize(count);
}
int TexturedGeometry::mode() const
{
if (tri == Strip)
return GL_TRIANGLE_STRIP;
return GL_TRIANGLE_FAN;
}
void TexturedGeometry::setPoint(int index, const QPointF &p, const QPointF &tp)
{
setGeometryPoint(index, p);
setTexturePoint(index, tp);
}
void TexturedGeometry::setGeometryPoint(int index, const QPointF &p)
{
v[index].x = p.x();
v[index].y = p.y();
}
void TexturedGeometry::setTexturePoint(int index, const QPointF &tp)
{
v[index].tx = tp.x();
v[index].ty = tp.y();
}
void TexturedGeometry::setRect(const QRectF &r, const QRectF &tr)
{
setPoint(0, r.topLeft(), tr.topLeft());
setPoint(1, r.bottomLeft(), tr.bottomLeft());
if (tri == Strip) {
setPoint(2, r.topRight(), tr.topRight());
setPoint(3, r.bottomRight(), tr.bottomRight());
} else {
setPoint(3, r.topRight(), tr.topRight());
setPoint(2, r.bottomRight(), tr.bottomRight());
}
}
VideoShader::VideoShader(VideoShaderPrivate &d):
DPTR_INIT(&d)
{
}
VideoShader::VideoShader()
{
//d.planar_frag = shaderSourceFromFile("shaders/planar.f.glsl");
//d.packed_frag = shaderSourceFromFile("shaders/rgb.f.glsl");
}
VideoShader::~VideoShader()
{
}
/*
* use gl api to get active attributes/uniforms
* use glsl parser to get attributes?
*/
char const *const* VideoShader::attributeNames() const
{
static const char *names[] = {
"a_Position",
"a_TexCoords",
0
};
return names;
}
const char* VideoShader::vertexShader() const
{
static const char kVertexShader[] = glsl(
attribute vec4 a_Position;
attribute vec2 a_TexCoords;
uniform mat4 u_MVP_matrix;
varying vec2 v_TexCoords;
void main() {
gl_Position = u_MVP_matrix * a_Position;
v_TexCoords = a_TexCoords;
});
return kVertexShader;
}
const char* VideoShader::fragmentShader() const
{
DPTR_D(const VideoShader);
// because we have to modify the shader, and shader source must be kept, so read the origin
if (d.video_format.isPlanar()) {
d.planar_frag = shaderSourceFromFile("shaders/planar.f.glsl");
} else {
d.packed_frag = shaderSourceFromFile("shaders/packed.f.glsl");
}
QByteArray& frag = d.video_format.isPlanar() ? d.planar_frag : d.packed_frag;
if (frag.isEmpty()) {
qWarning("Empty fragment shader!");
return 0;
}
#if YUVA_DONE
if (d.video_format.planeCount() == 4) {
frag.prepend("#define PLANE_4\n");
}
#endif
if (d.video_format.isPlanar()) {
if (d.video_format.bytesPerPixel(0) == 2) {
if (d.video_format.isBigEndian())
frag.prepend("#define LA_16BITS_BE\n");
else
frag.prepend("#define LA_16BITS_LE\n");
}
} else {
if (!d.video_format.isRGB())
frag.prepend("#define PACKED_YUV");
}
if (d.texture_target == GL_TEXTURE_RECTANGLE) {
frag.prepend("#extension GL_ARB_texture_rectangle : enable\n"
"#define texture2D texture2DRect\n"
"#define sampler2D sampler2DRect\n");
}
return frag.constData();
}
void VideoShader::initialize(QOpenGLShaderProgram *shaderProgram)
{
DPTR_D(VideoShader);
if (!textureLocationCount())
return;
d.owns_program = !shaderProgram;
if (shaderProgram) {
d.program = shaderProgram;
}
shaderProgram = program();
if (!shaderProgram->isLinked()) {
compile(shaderProgram);
}
d.u_MVP_matrix = shaderProgram->uniformLocation("u_MVP_matrix");
// fragment shader
d.u_colorMatrix = shaderProgram->uniformLocation("u_colorMatrix");
d.u_bpp = shaderProgram->uniformLocation("u_bpp");
d.u_opacity = shaderProgram->uniformLocation("u_opacity");
d.u_c = shaderProgram->uniformLocation("u_c");
d.u_Texture.resize(textureLocationCount());
for (int i = 0; i < d.u_Texture.size(); i) {
const QString tex_var = QString("u_Texture%1").arg(i);
d.u_Texture[i] = shaderProgram->uniformLocation(tex_var);
qDebug("glGetUniformLocation(\"%s\") = %d", tex_var.toUtf8().constData(), d.u_Texture[i]);
}
qDebug("glGetUniformLocation(\"u_MVP_matrix\") = %d", d.u_MVP_matrix);
qDebug("glGetUniformLocation(\"u_colorMatrix\") = %d", d.u_colorMatrix);
qDebug("glGetUniformLocation(\"u_opacity\") = %d", d.u_opacity);
if (d.u_c >= 0)
qDebug("glGetUniformLocation(\"u_c\") = %d", d.u_c);
if (d.u_bpp >= 0)
qDebug("glGetUniformLocation(\"u_bpp\") = %d", d.u_bpp);
}
int VideoShader::textureLocationCount() const
{
DPTR_D(const VideoShader);
// TODO: avoid accessing video_format.
if (!d.video_format.isPlanar())
return 1;
return d.video_format.channels();
}
int VideoShader::textureLocation(int channel) const
{
DPTR_D(const VideoShader);
Q_ASSERT(channel < d.u_Texture.size());
return d.u_Texture[channel];
}
int VideoShader::matrixLocation() const
{
return d_func().u_MVP_matrix;
}
int VideoShader::colorMatrixLocation() const
{
return d_func().u_colorMatrix;
}
int VideoShader::bppLocation() const
{
return d_func().u_bpp;
}
int VideoShader::opacityLocation() const
{
return d_func().u_opacity;
}
int VideoShader::channelMapLocation() const
{
return d_func().u_c;
}
int VideoShader::textureTarget() const
{
return d_func().texture_target;
}
void VideoShader::setTextureTarget(int type)
{
d_func().texture_target = type;
}
VideoFormat VideoShader::videoFormat() const
{
return d_func().video_format;
}
void VideoShader::setVideoFormat(const VideoFormat &format)
{
d_func().video_format = format;
}
QOpenGLShaderProgram* VideoShader::program()
{
DPTR_D(VideoShader);
if (!d.program) {
d.owns_program = true;
d.program = new QOpenGLShaderProgram();
}
return d.program;
}
bool VideoShader::update(VideoMaterial *material)
{
if (!material)
return false;
if (!material->bind())
return false;
const VideoFormat fmt(material->currentFormat());
//format is out of date because we may use the same shader for different formats
setVideoFormat(fmt);
// uniforms begin
program()->bind(); //glUseProgram(id). for glUniform
// all texture ids should be binded when renderering even for packed plane!
const int nb_planes = fmt.planeCount(); //number of texture id
for (int i = 0; i < nb_planes; i) {
// use glUniform1i to swap planes. swap uv: i => (3-i)%3
// TODO: in shader, use uniform sample2D u_Texture[], and use glUniform1iv(u_Texture, 3, {...})
program()->setUniformValue(textureLocation(i), (GLint)i);
}
if (nb_planes < textureLocationCount()) {
for (int i = nb_planes; i < textureLocationCount(); i) {
program()->setUniformValue(textureLocation(i), (GLint)(nb_planes - 1));
}
}
//qDebug() << "color mat " << material->colorMatrix();
program()->setUniformValue(colorMatrixLocation(), material->colorMatrix());
if (bppLocation() >= 0)
program()->setUniformValue(bppLocation(), (GLfloat)material->bpp());
if (channelMapLocation() >= 0)
program()->setUniformValue(channelMapLocation(), material->channelMap());
//program()->setUniformValue(matrixLocation(), material->matrix()); //what about sgnode? state.combindMatrix()?
// uniform end. attribute begins
return true;
}
QByteArray VideoShader::shaderSourceFromFile(const QString &fileName) const
{
QFile f(qApp->applicationDirPath() "/" fileName);
if (!f.exists()) {
f.setFileName(":/" fileName);
}
if (!f.open(QIODevice::ReadOnly)) {
qWarning("Can not load shader %s: %s", f.fileName().toUtf8().constData(), f.errorString().toUtf8().constData());
return QByteArray();
}
QByteArray src = f.readAll();
f.close();
return src;
}
void VideoShader::compile(QOpenGLShaderProgram *shaderProgram)
{
Q_ASSERT_X(!shaderProgram->isLinked(), "VideoShader::compile()", "Compile called multiple times!");
shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader());
shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader());
int maxVertexAttribs = 0;
DYGL(glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs));
char const *const *attr = attributeNames();
for (int i = 0; attr[i]; i) {
if (i >= maxVertexAttribs) {
qFatal("List of attribute names is either too long or not null-terminated.\n"
"Maximum number of attributes on this hardware is %i.\n"
"Vertex shader:\n%s\n"
"Fragment shader:\n%s\n",
maxVertexAttribs, vertexShader(), fragmentShader());
}
// why must min location == 0?
if (*attr[i])
shaderProgram->bindAttributeLocation(attr[i], i);
}
if (!shaderProgram->link()) {
qWarning("QSGMaterialShader: Shader compilation failed:");
qWarning() << shaderProgram->log();
}
}
VideoMaterial::VideoMaterial()
{
}
void VideoMaterial::setCurrentFrame(const VideoFrame &frame)
{
DPTR_D(VideoMaterial);
d.update_texure = true;
// TODO: move to another function before rendering?
d.width = frame.width();
d.height = frame.height();
GLenum new_target = GL_TEXTURE_2D; // not d.target. because metadata "target" is not always set
QByteArray t = frame.metaData("target").toByteArray().toLower();
if (t == "rect")
new_target = GL_TEXTURE_RECTANGLE;
if (new_target != d.target) {
// FIXME: not thread safe (in qml)
d.target = new_target;
d.init_textures_required = true;
}
const VideoFormat fmt(frame.format());
d.bpp = fmt.bitsPerPixel(0);
// http://forum.doom9.org/archive/index.php/t-160211.html
ColorSpace cs = frame.colorSpace();// ColorSpace_RGB;
if (cs == ColorSpace_Unknow) {
if (fmt.isRGB()) {
if (fmt.isPlanar())
cs = ColorSpace_GBR;
else
cs = ColorSpace_RGB;
} else {
if (frame.width() >= 1280 || frame.height() > 576) //values from mpv
cs = ColorSpace_BT709;
else
cs = ColorSpace_BT601;
}
}
d.colorTransform.setInputColorSpace(cs);
d.frame = frame;
if (fmt != d.video_format) {
qDebug("pixel format changed: %s => %s", qPrintable(d.video_format.name()), qPrintable(fmt.name()));
d.video_format = fmt;
}
}
VideoFormat VideoMaterial::currentFormat() const
{
DPTR_D(const VideoMaterial);
return d.video_format;
}
VideoShader* VideoMaterial::createShader() const
{
DPTR_D(const VideoMaterial);
VideoShader *shader = new VideoShader();
shader->setVideoFormat(d.video_format);
shader->setTextureTarget(d.target);
//resize texture locations to avoid access format later
return shader;
}
const char *VideoMaterial::type() const
{
DPTR_D(const VideoMaterial);
const VideoFormat &fmt = d.video_format;
if (!fmt.isPlanar()) {
if (fmt.isRGB())
return "packed rgb material";
if (d.target == GL_TEXTURE_2D)
return "packed yuv material";
return "packed yuv rectangle texture material";
}
if (fmt.bytesPerPixel(0) == 1) {
if (fmt.planeCount() == 4)
return "8bit 4plane yuv material";
return "8bit yuv material";
}
if (fmt.isBigEndian()) {
if (fmt.planeCount() == 4)
return "4plane 16bit-be material";
return "planar 16bit-be material";
} else {
if (fmt.planeCount() == 4)
return "4plane 16bit-le material";
return "planar 16bit-le material";
}
return "invalid material";
}
bool VideoMaterial::bind()
{
DPTR_D(VideoMaterial);
if (!d.ensureResources())
return false;
const int nb_planes = d.textures.size(); //number of texture id
if (nb_planes <= 0)
return false;
if (nb_planes > 4) //why?
return false;
for (int i = 0; i < nb_planes; i) {
bindPlane((i 1) % nb_planes, d.update_texure); // why? i: quick items display wrong textures
}
if (d.update_texure) {
d.update_texure = false;
d.frame = VideoFrame();
}
d.init_textures_required = false;
return true;
}
void VideoMaterial::bindPlane(int p, bool updateTexture)
{
DPTR_D(VideoMaterial);
GLuint &tex = d.textures[p];
if (d.init_textures_required) {
if (tex) {
qDebug("deleting texture for plane: %d", p);
DYGL(glDeleteTextures(1, &tex));
tex = 0;
}
}
if (!tex) {
qDebug("creating texture for plane: %d", p);
GLuint* handle = (GLuint*)d.frame.createInteropHandle(&tex, GLTextureSurface, p);
if (handle) {
tex = *handle;
} else {
DYGL(glGenTextures(1, &tex));
d.initTexture(tex, d.internal_format[p], d.data_format[p], d.data_type[p], d.texture_size[p].width(), d.texture_size[p].height());
}
qDebug("texture for plane %d is created: %u", p, tex);
}
if (!updateTexture) {
OpenGLHelper::glActiveTexture(GL_TEXTURE0 p); //0 must active?
DYGL(glBindTexture(d.target, tex));
return;
}
//setupQuality?
// try_pbo ? pbo_id : 0. 0= > interop.createHandle
if (d.frame.map(GLTextureSurface, &tex, p)) {
//TODO: move to map()?
OpenGLHelper::glActiveTexture(GL_TEXTURE0 p); //0 must active?
DYGL(glBindTexture(d.target, tex));
return;
}
// FIXME: why happens on win?
if (d.frame.bytesPerLine(p) <= 0)
return;
if (d.try_pbo) {
//qDebug("bind PBO %d", p);
QOpenGLBuffer &pb = d.pbo[p];
pb.bind();
// glMapBuffer() causes sync issue.
// Call glBufferData() with NULL pointer before glMapBuffer(), the previous data in PBO will be discarded and
// glMapBuffer() returns a new allocated pointer or an unused block immediately even if GPU is still working with the previous data.
// https://www.opengl.org/wiki/Buffer_Object_Streaming#Buffer_re-specification
pb.allocate(pb.size());
GLubyte* ptr = (GLubyte*)pb.map(QOpenGLBuffer::WriteOnly);
if (ptr) {
memcpy(ptr, d.frame.bits(p), pb.size());
pb.unmap();
}
}
OpenGLHelper::glActiveTexture(GL_TEXTURE0 p);
//qDebug("bpl[%d]=%d width=%d", p, frame.bytesPerLine(p), frame.planeWidth(p));
DYGL(glBindTexture(d.target, tex));
//d.setupQuality();
// This is necessary for non-power-of-two textures
DYGL(glTexParameteri(d.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
DYGL(glTexParameteri(d.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
// TODO: data address use surfaceinterop.map()
DYGL(glTexSubImage2D(d.target, 0, 0, 0, d.texture_upload_size[p].width(), d.texture_upload_size[p].height(), d.data_format[p], d.data_type[p], d.try_pbo ? 0 : d.frame.bits(p)));
if (d.try_pbo) {
d.pbo[p].release();
}
}
int VideoMaterial::compare(const VideoMaterial *other) const
{
DPTR_D(const VideoMaterial);
for (int i = 0; i < d.textures.size(); i) {
const int diff = d.textures[i] - other->d_func().textures[i]; //TODO
if (diff)
return diff;
}
return d.bpp - other->bpp();
}
bool VideoMaterial::hasAlpha() const
{
return d_func().video_format.hasAlpha();
}
void VideoMaterial::unbind()
{
DPTR_D(VideoMaterial);
for (int i = 0; i < d.textures.size(); i) {
d.frame.unmap(&d.textures[i]);
}
}
const QMatrix4x4& VideoMaterial::colorMatrix() const
{
return d_func().colorTransform.matrixRef();
}
const QMatrix4x4& VideoMaterial::matrix() const
{
return d_func().matrix;
}
const QMatrix4x4 &VideoMaterial::channelMap() const
{
return d_func().channel_map;
}
int VideoMaterial::bpp() const
{
return d_func().bpp;
}
int VideoMaterial::planeCount() const
{
return d_func().frame.planeCount();
}
void VideoMaterial::setBrightness(qreal value)
{
d_func().colorTransform.setBrightness(value);
}
void VideoMaterial::setContrast(qreal value)
{
d_func().colorTransform.setContrast(value);
}
void VideoMaterial::setHue(qreal value)
{
d_func().colorTransform.setHue(value);
}
void VideoMaterial::setSaturation(qreal value)
{
d_func().colorTransform.setSaturation(value);
}
qreal VideoMaterial::validTextureWidth() const
{
return d_func().effective_tex_width_ratio;
}
QSize VideoMaterial::frameSize() const
{
return QSize(d_func().width, d_func().height);
}
QRectF VideoMaterial::normalizedROI(const QRectF &roi) const
{
return mapToTexture(roi, 1);
}
QPointF VideoMaterial::mapToTexture(const QPointF &p, int normalize) const
{
if (p.isNull())
return p;
DPTR_D(const VideoMaterial);
float x = p.x();
float y = p.y();
const qreal texW = d.texture_size[0].width();
const qreal s = texW/qreal(d.width); // only apply to unnormalized input roi
if (normalize < 0)
normalize = d.target != GL_TEXTURE_RECTANGLE;
if (normalize) {
if (qAbs(x) > 1) {
x /= (float)texW;
x *= s;
}
if (qAbs(y) > 1)
y /= (float)d.height;
} else {
if (qAbs(x) <= 1)
x *= (float)texW;
else
x *= s;
if (qAbs(y) <= 1)
y *= (float)d.height;
}
// multiply later because we compare with 1 before it
x *= d.effective_tex_width_ratio;
return QPointF(x, y);
}
// mapToTexture
QRectF VideoMaterial::mapToTexture(const QRectF &roi, int normalize) const
{
DPTR_D(const VideoMaterial);
const qreal texW = d.texture_size[0].width();
const qreal s = texW/qreal(d.width); // only apply to unnormalized input roi
if (!roi.isValid()) {
if (normalize)
return QRectF(0, 0, d.effective_tex_width_ratio, 1); //NOTE: not (0, 0, 1, 1)
return QRectF(0, 0, texW, d.height);
}
float x = roi.x();
float w = roi.width(); //TODO: texturewidth
float y = roi.y();
float h = roi.height();
if (normalize < 0)
normalize = d.target != GL_TEXTURE_RECTANGLE;
if (normalize) {
if (qAbs(x) > 1) {
x /= texW;
x *= s;
}
if (qAbs(y) > 1)
y /= (float)d.height;
if (qAbs(w) > 1) {
w /= texW;
w *= s;
}
if (qAbs(h) > 1)
h /= (float)d.height;
} else { //FIXME: what about ==1?
if (qAbs(x) <= 1)
x *= texW;
else
x *= s;
if (qAbs(y) <= 1)
y *= (float)d.height;
if (qAbs(w) <= 1)
w *= texW;
else
w *= s;
if (qAbs(h) <= 1)
h *= (float)d.height;
}
// multiply later because we compare with 1 before it
x *= d.effective_tex_width_ratio;
w *= d.effective_tex_width_ratio;
return QRectF(x, y, w, h);
}
bool VideoMaterialPrivate::initPBO(int plane, int size)
{
QOpenGLBuffer &pb = pbo[plane];
if (!pb.isCreated()) {
qDebug("Creating PBO for plane %d, size: %d...", plane, size);
pb.create();
}
if (!pb.bind()) {
qWarning("Failed to bind PBO for plane %d!!!!!!", plane);
try_pbo = false;
return false;
}
qDebug("Allocate PBO size %d", size);
pb.allocate(size);
return true;
}
bool VideoMaterialPrivate::initTexture(GLuint tex, GLint internal_format, GLenum format, GLenum dataType, int width, int height)
{
DYGL(glBindTexture(target, tex));
setupQuality();
// This is necessary for non-power-of-two textures
DYGL(glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
DYGL(glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
DYGL(glTexImage2D(target, 0, internal_format, width, height, 0/*border, ES not support*/, format, dataType, NULL));
DYGL(glBindTexture(target, 0));
return true;
}
VideoMaterialPrivate::~VideoMaterialPrivate()
{
if (!textures.isEmpty()) {
DYGL(glDeleteTextures(textures.size(), textures.data()));
}
for (int i = 0; i < pbo.size(); i)
pbo[i].destroy();
}
bool VideoMaterialPrivate::updateTextureParameters(const VideoFormat& fmt)
{
// isSupported(pixfmt)
if (!fmt.isValid())
return false;
//http://www.berkelium.com/OpenGL/GDC99/internalformat.html
//NV12: UV is 1 plane. 16 bits as a unit. GL_LUMINANCE4, 8, 16, ... 32?
//GL_LUMINANCE, GL_LUMINANCE_ALPHA are deprecated in GL3, removed in GL3.1
//replaced by GL_RED, GL_RG, GL_RGB, GL_RGBA? for 1, 2, 3, 4 channel image
//http://www.gamedev.net/topic/634850-do-luminance-textures-still-exist-to-opengl/
//https://github.com/kivy/kivy/issues/1738: GL_LUMINANCE does work on a Galaxy Tab 2. LUMINANCE_ALPHA very slow on Linux
//ALPHA: vec4(1,1,1,A), LUMINANCE: (L,L,L,1), LUMINANCE_ALPHA: (L,L,L,A)
/*
* To support both planar and packed use GL_ALPHA and in shader use r,g,a like xbmc does.
* or use Swizzle_mask to layout the channels: http://www.opengl.org/wiki/Texture#Swizzle_mask
* GL ES2 support: GL_RGB, GL_RGBA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA
* http://stackoverflow.com/questions/18688057/which-opengl-es-2-0-texture-formats-are-color-depth-or-stencil-renderable
*/
const int nb_planes = fmt.planeCount();
if (!fmt.isPlanar()) {
GLint internal_fmt;
GLenum data_fmt;
GLenum data_t;
if (!OpenGLHelper::videoFormatToGL(fmt, &internal_fmt, &data_fmt, &data_t)) {
qWarning("no opengl format found: internal_fmt=%d, data_fmt=%d, data_t=%d", internal_fmt, data_fmt, data_t);
qDebug() << fmt;
return false;
}
internal_format = QVector<GLint>(nb_planes, internal_fmt);
data_format = QVector<GLenum>(nb_planes, data_fmt);
data_type = QVector<GLenum>(nb_planes, data_t);
//glPixelStorei(GL_UNPACK_ALIGNMENT, fmt.bytesPerPixel());
// TODO: if no alpha, data_fmt is not GL_BGRA. align at every upload?
} else {
internal_format.resize(nb_planes);
data_format.resize(nb_planes);
data_type = QVector<GLenum>(nb_planes, GL_UNSIGNED_BYTE);
/*!
* GLES internal_format == data_format, GL_LUMINANCE_ALPHA is 2 bytes
* so if NV12 use GL_LUMINANCE_ALPHA, YV12 use GL_ALPHA
*/
qDebug("///////////bpp %d", fmt.bytesPerPixel());
internal_format[0] = data_format[0] = GL_LUMINANCE; //or GL_RED for GL
if (nb_planes == 2) {
// NV12/21 semi-planar
internal_format[1] = data_format[1] = GL_LUMINANCE_ALPHA;
} else {
if (fmt.bytesPerPixel(1) == 2) {
// read 16 bits and compute the real luminance in shader
internal_format.fill(GL_LUMINANCE_ALPHA); //vec4(L,L,L,A)
data_format.fill(GL_LUMINANCE_ALPHA);
} else {
internal_format[1] = data_format[1] = GL_LUMINANCE; //vec4(L,L,L,1)
internal_format[2] = data_format[2] = GL_ALPHA;//GL_ALPHA;
if (nb_planes == 4)
internal_format[3] = data_format[3] = GL_ALPHA; //GL_ALPHA
}
}
}
for (int i = 0; i < nb_planes; i) {
//qDebug("format: %#x GL_LUMINANCE_ALPHA=%#x", data_format[i], GL_LUMINANCE_ALPHA);
if (fmt.bytesPerPixel(i) == 2 && nb_planes == 3) {
//data_type[i] = GL_UNSIGNED_SHORT;
}
const int bpp_gl = OpenGLHelper::bytesOfGLFormat(data_format[i], data_type[i]);
const int pad = std::ceil((qreal)(texture_size[i].width() - effective_tex_width[i])/(qreal)bpp_gl);
texture_size[i].setWidth(std::ceil((qreal)texture_size[i].width()/(qreal)bpp_gl));
texture_upload_size[i].setWidth(std::ceil((qreal)texture_upload_size[i].width()/(qreal)bpp_gl));
effective_tex_width[i] /= bpp_gl; //fmt.bytesPerPixel(i);
//effective_tex_width_ratio =
qDebug("texture width: %d - %d = pad: %d. bpp(gl): %d", texture_size[i].width(), effective_tex_width[i], pad, bpp_gl);
}
/*
* there are 2 fragment shaders: rgb and yuv.
* only 1 texture for packed rgb. planar rgb likes yuv
* To support both planar and packed yuv, and mixed yuv(NV12), we give a texture sample
* for each channel. For packed, each (channel) texture sample is the same. For planar,
* packed channels has the same texture sample.
* But the number of actural textures we upload is plane count.
* Which means the number of texture id equals to plane count
*/
// always delete old textures otherwise old textures are not initialized with correct parameters
// TODO: use a struct for each plane: texid, initialized...
// FIXME: texture lazy will cause the first frame display red, seems some planes are not uploaded. why?
// currently only vda zero copy(uyvy) needs lazy delete, only 1 plane so no red display issue.
if (target == GL_TEXTURE_2D) {
if (textures.size() != nb_planes) {
const int nb_delete = textures.size();
qDebug("delete %d textures", nb_delete);
if (!textures.isEmpty()) {
DYGL(glDeleteTextures(nb_delete, textures.data()));
textures.clear();
}
textures.resize(nb_planes);
textures.fill(0);
DYGL(glGenTextures(textures.size(), textures.data()));
}
qDebug("init textures...");
for (int i = 0; i < textures.size(); i) {
// can not init for vda!
initTexture(textures[i], internal_format[i], data_format[i], data_type[i], texture_size[i].width(), texture_size[i].height());
}
init_textures_required = false;
} else {
if (textures.size() > nb_planes) {
const int nb_delete = textures.size() - nb_planes;
qDebug("delete %d textures", nb_delete);
if (!textures.isEmpty()) {
DYGL(glDeleteTextures(nb_delete, textures.data() nb_planes));
}
}
textures.resize(nb_planes);
init_textures_required = true;
}
return true;
}
void VideoMaterialPrivate::updateChannelMap(const VideoFormat &fmt)
{
channel_map = QMatrix4x4();
if (fmt.isPlanar() || fmt.isRGB())
return;
switch (fmt.pixelFormat()) {
case VideoFormat::Format_UYVY:
channel_map = QMatrix4x4(0.0f, 0.5f, 0.0f, 0.5f,
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
break;
case VideoFormat::Format_YUYV:
channel_map = QMatrix4x4(0.5f, 0.0f, 0.5f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f);
break;
case VideoFormat::Format_VYUY:
channel_map = QMatrix4x4(0.0f, 0.5f, 0.0f, 0.5f,
0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
break;
case VideoFormat::Format_YVYU:
channel_map = QMatrix4x4(0.5f, 0.0f, 0.5f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
break;
default:
break;
}
}
bool VideoMaterialPrivate::ensureResources()
{
if (!update_texure) //video frame is already uploaded and displayed
return true;
const VideoFormat &fmt = video_format;
if (!fmt.isValid())
return false;
bool update_textures = init_textures_required;
const int nb_planes = fmt.planeCount();
// will this take too much time?
const qreal wr = (qreal)frame.effectiveBytesPerLine(nb_planes-1)/(qreal)frame.bytesPerLine(nb_planes-1);
const int linsize0 = frame.bytesPerLine(0);
// effective size may change even if plane size not changed
if (update_textures
|| !qFuzzyCompare(wr, effective_tex_width_ratio)
|| linsize0 != plane0Size.width() || frame.height() != plane0Size.height()
|| (plane1_linesize > 0 && frame.bytesPerLine(1) != plane1_linesize)) { // no need to check height if plane 0 sizes are equal?
update_textures = true;
//qDebug("---------------------update texture: %dx%d, %s", width, frame.height(), video_format.name().toUtf8().constData());
texture_size.resize(nb_planes);
texture_upload_size.resize(nb_planes);
effective_tex_width.resize(nb_planes);
for (int i = 0; i < nb_planes; i) {
qDebug("plane linesize %d: padded = %d, effective = %d", i, frame.bytesPerLine(i), frame.effectiveBytesPerLine(i));
qDebug("plane width %d: effective = %d", frame.planeWidth(i), frame.effectivePlaneWidth(i));
qDebug("planeHeight %d = %d", i, frame.planeHeight(i));
// we have to consider size of opengl format. set bytesPerLine here and change to width later
texture_size[i] = QSize(frame.bytesPerLine(i), frame.planeHeight(i));
texture_upload_size[i] = texture_size[i];
effective_tex_width[i] = frame.effectiveBytesPerLine(i); //store bytes here, modify as width later
// TODO: ratio count the GL_UNPACK_ALIGN?
//effective_tex_width_ratio = qMin((qreal)1.0, (qreal)frame.effectiveBytesPerLine(i)/(qreal)frame.bytesPerLine(i));
}
plane1_linesize = 0;
if (nb_planes > 1) {
texture_size[0].setWidth(texture_size[1].width() * effective_tex_width[0]/effective_tex_width[1]);
// height? how about odd?
plane1_linesize = frame.bytesPerLine(1);
}
/*
let wr[i] = valid_bpl[i]/bpl[i], frame from avfilter maybe wr[1] < wr[0]
e.g. original frame plane 0: 720/768; plane 1,2: 360/384,
filtered frame plane 0: 720/736, ... (16 aligned?)
*/
effective_tex_width_ratio = (qreal)frame.effectiveBytesPerLine(nb_planes-1)/(qreal)frame.bytesPerLine(nb_planes-1);
qDebug("effective_tex_width_ratio=%f", effective_tex_width_ratio);
plane0Size.setWidth(linsize0);
plane0Size.setHeight(frame.height());
}
if (update_textures) {
updateTextureParameters(fmt);
updateChannelMap(fmt);
// check pbo support
// TODO: complete pbo extension set
try_pbo = try_pbo && OpenGLHelper::isPBOSupported();
// check PBO support with bind() is fine, no need to check extensions
if (try_pbo) {
for (int i = 0; i < nb_planes; i) {
//qDebug("Init PBO for plane %d", i);
if (!initPBO(i, frame.bytesPerLine(i)*frame.planeHeight(i))) {
qWarning("Failed to init PBO for plane %d", i);
break;
}
}
}
}
return true;
}
void VideoMaterialPrivate::setupQuality()
{
DYGL(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
DYGL(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
}
} //namespace QtAV