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
594
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
|
/*
* common OpenGL routines
*
* copyleft (C) 2005 Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
* Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
* gave me lots of good ideas.
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* \file gl_common.c
* \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "gl_common.h"
#include "libavutil/common.h"
void (GLAPIENTRY *Begin)(GLenum);
void (GLAPIENTRY *End)(void);
void (GLAPIENTRY *Viewport)(GLint, GLint, GLsizei, GLsizei);
void (GLAPIENTRY *MatrixMode)(GLenum);
void (GLAPIENTRY *LoadIdentity)(void);
void (GLAPIENTRY *Scaled)(double, double, double);
void (GLAPIENTRY *Ortho)(double, double, double, double, double, double);
void (GLAPIENTRY *PushMatrix)(void);
void (GLAPIENTRY *PopMatrix)(void);
void (GLAPIENTRY *Clear)(GLbitfield);
GLuint (GLAPIENTRY *GenLists)(GLsizei);
void (GLAPIENTRY *DeleteLists)(GLuint, GLsizei);
void (GLAPIENTRY *NewList)(GLuint, GLenum);
void (GLAPIENTRY *EndList)(void);
void (GLAPIENTRY *CallList)(GLuint);
void (GLAPIENTRY *CallLists)(GLsizei, GLenum, const GLvoid *);
void (GLAPIENTRY *GenTextures)(GLsizei, GLuint *);
void (GLAPIENTRY *DeleteTextures)(GLsizei, const GLuint *);
void (GLAPIENTRY *TexEnvi)(GLenum, GLenum, GLint);
void (GLAPIENTRY *Color4ub)(GLubyte, GLubyte, GLubyte, GLubyte);
void (GLAPIENTRY *Color3f)(GLfloat, GLfloat, GLfloat);
void (GLAPIENTRY *ClearColor)(GLclampf, GLclampf, GLclampf, GLclampf);
void (GLAPIENTRY *Enable)(GLenum);
void (GLAPIENTRY *Disable)(GLenum);
const GLubyte *(GLAPIENTRY *GetString)(GLenum);
void (GLAPIENTRY *DrawBuffer)(GLenum);
void (GLAPIENTRY *DepthMask)(GLboolean);
void (GLAPIENTRY *BlendFunc)(GLenum, GLenum);
void (GLAPIENTRY *Flush)(void);
void (GLAPIENTRY *Finish)(void);
void (GLAPIENTRY *PixelStorei)(GLenum, GLint);
void (GLAPIENTRY *TexImage1D)(GLenum, GLint, GLint, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
void (GLAPIENTRY *TexImage2D)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
void (GLAPIENTRY *TexSubImage2D)(GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const GLvoid *);
void (GLAPIENTRY *TexParameteri)(GLenum, GLenum, GLint);
void (GLAPIENTRY *TexParameterf)(GLenum, GLenum, GLfloat);
void (GLAPIENTRY *TexParameterfv)(GLenum, GLenum, const GLfloat *);
void (GLAPIENTRY *TexCoord2f)(GLfloat, GLfloat);
void (GLAPIENTRY *Vertex2f)(GLfloat, GLfloat);
void (GLAPIENTRY *GetIntegerv)(GLenum, GLint *);
/**
* \defgroup glextfunctions OpenGL extension functions
*
* the pointers to these functions are acquired when the OpenGL
* context is created
* \{
*/
void (GLAPIENTRY *GenBuffers)(GLsizei, GLuint *);
void (GLAPIENTRY *DeleteBuffers)(GLsizei, const GLuint *);
void (GLAPIENTRY *BindBuffer)(GLenum, GLuint);
GLvoid* (GLAPIENTRY *MapBuffer)(GLenum, GLenum);
GLboolean (GLAPIENTRY *UnmapBuffer)(GLenum);
void (GLAPIENTRY *BufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
void (GLAPIENTRY *CombinerParameterfv)(GLenum, const GLfloat *);
void (GLAPIENTRY *CombinerParameteri)(GLenum, GLint);
void (GLAPIENTRY *CombinerInput)(GLenum, GLenum, GLenum, GLenum, GLenum,
GLenum);
void (GLAPIENTRY *CombinerOutput)(GLenum, GLenum, GLenum, GLenum, GLenum,
GLenum, GLenum, GLboolean, GLboolean,
GLboolean);
void (GLAPIENTRY *BeginFragmentShader)(void);
void (GLAPIENTRY *EndFragmentShader)(void);
void (GLAPIENTRY *SampleMap)(GLuint, GLuint, GLenum);
void (GLAPIENTRY *ColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
GLuint, GLuint, GLuint, GLuint, GLuint);
void (GLAPIENTRY *ColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
GLuint, GLuint, GLuint, GLuint, GLuint,
GLuint, GLuint, GLuint);
void (GLAPIENTRY *SetFragmentShaderConstant)(GLuint, const GLfloat *);
void (GLAPIENTRY *ActiveTexture)(GLenum);
void (GLAPIENTRY *BindTexture)(GLenum, GLuint);
void (GLAPIENTRY *MultiTexCoord2f)(GLenum, GLfloat, GLfloat);
void (GLAPIENTRY *GenPrograms)(GLsizei, GLuint *);
void (GLAPIENTRY *DeletePrograms)(GLsizei, const GLuint *);
void (GLAPIENTRY *BindProgram)(GLenum, GLuint);
void (GLAPIENTRY *ProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
void (GLAPIENTRY *GetProgramiv)(GLenum, GLenum, GLint *);
void (GLAPIENTRY *ProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
GLfloat, GLfloat);
int (GLAPIENTRY *SwapInterval)(int);
void (GLAPIENTRY *TexImage3D)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLsizei,
GLint, GLenum, GLenum, const GLvoid *);
void* (GLAPIENTRY *AllocateMemoryMESA)(void *, int, size_t, float, float, float);
void (GLAPIENTRY *FreeMemoryMESA)(void *, int, void *);
/** \} */ // end of glextfunctions group
//! \defgroup glgeneral OpenGL general helper functions
//! \defgroup glcontext OpenGL context management helper functions
//! \defgroup gltexture OpenGL texture handling helper functions
//! \defgroup glconversion OpenGL conversion helper functions
static GLint hqtexfmt;
/**
* \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
* \param stride number of bytes per line for which alignment should fit.
* \ingroup glgeneral
*/
void glAdjustAlignment(int stride) {
GLint gl_alignment;
if (stride % 8 == 0)
gl_alignment=8;
else if (stride % 4 == 0)
gl_alignment=4;
else if (stride % 2 == 0)
gl_alignment=2;
else
gl_alignment=1;
PixelStorei(GL_UNPACK_ALIGNMENT, gl_alignment);
}
struct gl_name_map_struct {
GLint value;
const char *name;
};
#undef MAP
#define MAP(a) {a, #a}
//! mapping table for the glValName function
static const struct gl_name_map_struct gl_name_map[] = {
// internal format
MAP(GL_R3_G3_B2), MAP(GL_RGB4), MAP(GL_RGB5), MAP(GL_RGB8),
MAP(GL_RGB10), MAP(GL_RGB12), MAP(GL_RGB16), MAP(GL_RGBA2),
MAP(GL_RGBA4), MAP(GL_RGB5_A1), MAP(GL_RGBA8), MAP(GL_RGB10_A2),
MAP(GL_RGBA12), MAP(GL_RGBA16), MAP(GL_LUMINANCE8),
// format
MAP(GL_RGB), MAP(GL_RGBA), MAP(GL_RED), MAP(GL_GREEN), MAP(GL_BLUE),
MAP(GL_ALPHA), MAP(GL_LUMINANCE), MAP(GL_LUMINANCE_ALPHA),
MAP(GL_COLOR_INDEX),
// rest 1.2 only
MAP(GL_BGR), MAP(GL_BGRA),
//type
MAP(GL_BYTE), MAP(GL_UNSIGNED_BYTE), MAP(GL_SHORT), MAP(GL_UNSIGNED_SHORT),
MAP(GL_INT), MAP(GL_UNSIGNED_INT), MAP(GL_FLOAT), MAP(GL_DOUBLE),
MAP(GL_2_BYTES), MAP(GL_3_BYTES), MAP(GL_4_BYTES),
// rest 1.2 only
MAP(GL_UNSIGNED_BYTE_3_3_2), MAP(GL_UNSIGNED_BYTE_2_3_3_REV),
MAP(GL_UNSIGNED_SHORT_5_6_5), MAP(GL_UNSIGNED_SHORT_5_6_5_REV),
MAP(GL_UNSIGNED_SHORT_4_4_4_4), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV),
MAP(GL_UNSIGNED_SHORT_5_5_5_1), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV),
MAP(GL_UNSIGNED_INT_8_8_8_8), MAP(GL_UNSIGNED_INT_8_8_8_8_REV),
MAP(GL_UNSIGNED_INT_10_10_10_2), MAP(GL_UNSIGNED_INT_2_10_10_10_REV),
{0, 0}
};
#undef MAP
/**
* \brief return the name of an OpenGL constant
* \param value the constant
* \return name of the constant or "Unknown format!"
* \ingroup glgeneral
*/
const char *glValName(GLint value)
{
int i = 0;
while (gl_name_map[i].name) {
if (gl_name_map[i].value == value)
return gl_name_map[i].name;
i++;
}
return "Unknown format!";
}
//! always return this format as internal texture format in glFindFormat
#define TEXTUREFORMAT_ALWAYS GL_RGB8
#undef TEXTUREFORMAT_ALWAYS
/**
* \brief find the OpenGL settings coresponding to format.
*
* All parameters may be NULL.
* \param fmt MPlayer format to analyze.
* \param bpp [OUT] bits per pixel of that format.
* \param gl_texfmt [OUT] internal texture format that fits the
* image format, not necessarily the best for performance.
* \param gl_format [OUT] OpenGL format for this image format.
* \param gl_type [OUT] OpenGL type for this image format.
* \return 1 if format is supported by OpenGL, 0 if not.
* \ingroup gltexture
*/
int glFindFormat(uint32_t fmt, int *bpp, GLint *gl_texfmt,
GLenum *gl_format, GLenum *gl_type)
{
int supported = 1;
int dummy1;
GLenum dummy2;
GLint dummy3;
if (!bpp) bpp = &dummy1;
if (!gl_texfmt) gl_texfmt = &dummy3;
if (!gl_format) gl_format = &dummy2;
if (!gl_type) gl_type = &dummy2;
*bpp = IMGFMT_IS_BGR(fmt)?IMGFMT_BGR_DEPTH(fmt):IMGFMT_RGB_DEPTH(fmt);
*gl_texfmt = 3;
switch (fmt) {
case IMGFMT_RGB48NE:
*gl_format = GL_RGB;
*gl_type = GL_UNSIGNED_SHORT;
break;
case IMGFMT_RGB24:
*gl_format = GL_RGB;
*gl_type = GL_UNSIGNED_BYTE;
break;
case IMGFMT_RGBA:
*gl_texfmt = 4;
*gl_format = GL_RGBA;
*gl_type = GL_UNSIGNED_BYTE;
break;
case IMGFMT_YV12:
supported = 0; // no native YV12 support
case IMGFMT_Y800:
case IMGFMT_Y8:
*gl_texfmt = 1;
*bpp = 8;
*gl_format = GL_LUMINANCE;
*gl_type = GL_UNSIGNED_BYTE;
break;
case IMGFMT_UYVY:
case IMGFMT_YUY2:
*gl_texfmt = GL_YCBCR_MESA;
*bpp = 16;
*gl_format = GL_YCBCR_MESA;
*gl_type = fmt == IMGFMT_UYVY ? GL_UNSIGNED_SHORT_8_8 : GL_UNSIGNED_SHORT_8_8_REV;
break;
#if 0
// we do not support palettized formats, although the format the
// swscale produces works
case IMGFMT_RGB8:
gl_format = GL_RGB;
gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
break;
#endif
case IMGFMT_RGB15:
*gl_format = GL_RGBA;
*gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
break;
case IMGFMT_RGB16:
*gl_format = GL_RGB;
*gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
break;
#if 0
case IMGFMT_BGR8:
// special case as red and blue have a differen number of bits.
// GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
// by nVidia drivers, and in addition would give more bits to
// blue than to red, which isn't wanted
gl_format = GL_RGB;
gl_type = GL_UNSIGNED_BYTE_3_3_2;
break;
#endif
case IMGFMT_BGR15:
*gl_format = GL_BGRA;
*gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
break;
case IMGFMT_BGR16:
*gl_format = GL_RGB;
*gl_type = GL_UNSIGNED_SHORT_5_6_5;
break;
case IMGFMT_BGR24:
*gl_format = GL_BGR;
*gl_type = GL_UNSIGNED_BYTE;
break;
case IMGFMT_BGRA:
*gl_texfmt = 4;
*gl_format = GL_BGRA;
*gl_type = GL_UNSIGNED_BYTE;
break;
default:
*gl_texfmt = 4;
*gl_format = GL_RGBA;
*gl_type = GL_UNSIGNED_BYTE;
supported = 0;
}
#ifdef TEXTUREFORMAT_ALWAYS
*gl_texfmt = TEXTUREFORMAT_ALWAYS;
#endif
return supported;
}
static void *setNull(const GLubyte *s) {
return NULL;
}
typedef struct {
void *funcptr;
const char *extstr;
const char *funcnames[7];
void *fallback;
} extfunc_desc_t;
#define DEF_FUNC_DESC(name) {&name, NULL, {"gl"#name, NULL}, gl ##name}
static const extfunc_desc_t extfuncs[] = {
// these aren't extension functions but we query them anyway to allow
// different "backends" with one binary
DEF_FUNC_DESC(Begin),
DEF_FUNC_DESC(End),
DEF_FUNC_DESC(Viewport),
DEF_FUNC_DESC(MatrixMode),
DEF_FUNC_DESC(LoadIdentity),
DEF_FUNC_DESC(Scaled),
DEF_FUNC_DESC(Ortho),
DEF_FUNC_DESC(PushMatrix),
DEF_FUNC_DESC(PopMatrix),
DEF_FUNC_DESC(Clear),
DEF_FUNC_DESC(GenLists),
DEF_FUNC_DESC(DeleteLists),
DEF_FUNC_DESC(NewList),
DEF_FUNC_DESC(EndList),
DEF_FUNC_DESC(CallList),
DEF_FUNC_DESC(CallLists),
DEF_FUNC_DESC(GenTextures),
DEF_FUNC_DESC(DeleteTextures),
DEF_FUNC_DESC(TexEnvi),
DEF_FUNC_DESC(Color4ub),
DEF_FUNC_DESC(Color3f),
DEF_FUNC_DESC(ClearColor),
DEF_FUNC_DESC(Enable),
DEF_FUNC_DESC(Disable),
DEF_FUNC_DESC(DrawBuffer),
DEF_FUNC_DESC(DepthMask),
DEF_FUNC_DESC(BlendFunc),
DEF_FUNC_DESC(Flush),
DEF_FUNC_DESC(Finish),
DEF_FUNC_DESC(PixelStorei),
DEF_FUNC_DESC(TexImage1D),
DEF_FUNC_DESC(TexImage2D),
DEF_FUNC_DESC(TexSubImage2D),
DEF_FUNC_DESC(TexParameteri),
DEF_FUNC_DESC(TexParameterf),
DEF_FUNC_DESC(TexParameterfv),
DEF_FUNC_DESC(TexCoord2f),
DEF_FUNC_DESC(Vertex2f),
DEF_FUNC_DESC(GetIntegerv),
// here start the real extensions
{&GenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
{&DeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
{&BindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
{&MapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
{&UnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
{&BufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
{&CombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
{&CombinerParameteri, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
{&CombinerInput, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL}},
{&CombinerOutput, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL}},
{&BeginFragmentShader, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL}},
{&EndFragmentShader, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL}},
{&SampleMap, "ATI_fragment_shader", {"glSampleMapATI", NULL}},
{&ColorFragmentOp2, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL}},
{&ColorFragmentOp3, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL}},
{&SetFragmentShaderConstant, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL}},
{&ActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
{&BindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
{&MultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
{&GenPrograms, "_program", {"glGenProgramsARB", NULL}},
{&DeletePrograms, "_program", {"glDeleteProgramsARB", NULL}},
{&BindProgram, "_program", {"glBindProgramARB", NULL}},
{&ProgramString, "_program", {"glProgramStringARB", NULL}},
{&GetProgramiv, "_program", {"glGetProgramivARB", NULL}},
{&ProgramEnvParameter4f, "_program", {"glProgramEnvParameter4fARB", NULL}},
{&SwapInterval, "_swap_control", {"glXSwapInterval", "glXSwapIntervalEXT", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL}},
{&TexImage3D, NULL, {"glTexImage3D", NULL}},
{&AllocateMemoryMESA, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL}},
{&FreeMemoryMESA, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL}},
{NULL}
};
/**
* \brief find the function pointers of some useful OpenGL extensions
* \param getProcAddress function to resolve function names, may be NULL
* \param ext2 an extra extension string
*/
static void getFunctions(void *(*getProcAddress)(const GLubyte *),
const char *ext2) {
const extfunc_desc_t *dsc;
const char *extensions;
char *allexts;
if (!getProcAddress)
getProcAddress = setNull;
// special case, we need glGetString before starting to find the other functions
GetString = getProcAddress("glGetString");
if (!GetString)
GetString = glGetString;
extensions = (const char *)GetString(GL_EXTENSIONS);
if (!extensions) extensions = "";
if (!ext2) ext2 = "";
allexts = malloc(strlen(extensions) + strlen(ext2) + 2);
strcpy(allexts, extensions);
strcat(allexts, " ");
strcat(allexts, ext2);
mp_msg(MSGT_VO, MSGL_DBG2, "OpenGL extensions string:\n%s\n", allexts);
for (dsc = extfuncs; dsc->funcptr; dsc++) {
void *ptr = NULL;
int i;
if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
for (i = 0; !ptr && dsc->funcnames[i]; i++)
ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
}
if (!ptr)
ptr = dsc->fallback;
*(void **)dsc->funcptr = ptr;
}
if (strstr(allexts, "_texture_float"))
hqtexfmt = GL_RGB32F;
else if (strstr(allexts, "NV_float_buffer"))
hqtexfmt = GL_FLOAT_RGB32_NV;
else
hqtexfmt = GL_RGB16;
free(allexts);
}
/**
* \brief create a texture and set some defaults
* \param target texture taget, usually GL_TEXTURE_2D
* \param fmt internal texture format
* \param format texture host data format
* \param type texture host data type
* \param filter filter used for scaling, e.g. GL_LINEAR
* \param w texture width
* \param h texture height
* \param val luminance value to fill texture with
* \ingroup gltexture
*/
void glCreateClearTex(GLenum target, GLenum fmt, GLenum format, GLenum type, GLint filter,
int w, int h, unsigned char val) {
GLfloat fval = (GLfloat)val / 255.0;
GLfloat border[4] = {fval, fval, fval, fval};
int stride = w * glFmt2bpp(format, type);
char *init;
if (!stride) return;
init = malloc(stride * h);
memset(init, val, stride * h);
glAdjustAlignment(stride);
PixelStorei(GL_UNPACK_ROW_LENGTH, w);
TexImage2D(target, 0, fmt, w, h, 0, format, type, init);
TexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Border texels should not be used with CLAMP_TO_EDGE
// We set a sane default anyway.
TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
free(init);
}
/**
* \brief skips whitespace and comments
* \param f file to read from
*/
static void ppm_skip(FILE *f) {
int c, comment = 0;
do {
c = fgetc(f);
if (c == '#')
comment = 1;
if (c == '\n')
comment = 0;
} while (c != EOF && (isspace(c) || comment));
if (c != EOF)
ungetc(c, f);
}
#define MAXDIM (16 * 1024)
/**
* \brief creates a texture from a PPM file
* \param target texture taget, usually GL_TEXTURE_2D
* \param fmt internal texture format, 0 for default
* \param filter filter used for scaling, e.g. GL_LINEAR
* \param f file to read PPM from
* \param width [out] width of texture
* \param height [out] height of texture
* \param maxval [out] maxval value from PPM file
* \return 0 on error, 1 otherwise
* \ingroup gltexture
*/
int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
FILE *f, int *width, int *height, int *maxval) {
unsigned w, h, m, val, bpp;
char *data;
GLenum type;
ppm_skip(f);
if (fgetc(f) != 'P' || fgetc(f) != '6')
return 0;
ppm_skip(f);
if (fscanf(f, "%u", &w) != 1)
return 0;
ppm_skip(f);
if (fscanf(f, "%u", &h) != 1)
return 0;
ppm_skip(f);
if (fscanf(f, "%u", &m) != 1)
return 0;
val = fgetc(f);
if (!isspace(val))
return 0;
if (w > MAXDIM || h > MAXDIM)
return 0;
bpp = (m > 255) ? 6 : 3;
data = malloc(w * h * bpp);
if (fread(data, w * bpp, h, f) != h)
return 0;
if (!fmt) {
fmt = (m > 255) ? hqtexfmt : 3;
if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
fmt = GL_RGB16;
}
type = m > 255 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
glUploadTex(target, GL_RGB, type,
data, w * bpp, 0, 0, w, h, 0);
free(data);
if (width) *width = w;
if (height) *height = h;
if (maxval) *maxval = m;
return 1;
}
/**
* \brief return the number of bytes per pixel for the given format
* \param format OpenGL format
* \param type OpenGL type
* \return bytes per pixel
* \ingroup glgeneral
*
* Does not handle all possible variants, just those used by MPlayer
*/
int glFmt2bpp(GLenum format, GLenum type) {
int component_size = 0;
switch (type) {
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
return 1;
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
return 2;
case GL_UNSIGNED_BYTE:
component_size = 1;
break;
case GL_UNSIGNED_SHORT:
component_size = 2;
break;
}
switch (format) {
case GL_LUMINANCE:
case GL_ALPHA:
return component_size;
case GL_YCBCR_MESA:
return 2;
case GL_RGB:
case GL_BGR:
return 3 * component_size;
case GL_RGBA:
case GL_BGRA:
return 4 * component_size;
}
return 0; // unknown
}
/**
* \brief upload a texture, handling things like stride and slices
* \param target texture target, usually GL_TEXTURE_2D
* \param format OpenGL format of data
* \param type OpenGL type of data
* \param dataptr data to upload
* \param stride data stride
* \param x x offset in texture
* \param y y offset in texture
* \param w width of the texture part to upload
* \param h height of the texture part to upload
* \param slice height of an upload slice, 0 for all at once
* \ingroup gltexture
*/
void glUploadTex(GLenum target, GLenum format, GLenum type,
const void *dataptr, int stride,
int x, int y, int w, int h, int slice) {
const uint8_t *data = dataptr;
int y_max = y + h;
if (w <= 0 || h <= 0) return;
if (slice <= 0)
slice = h;
if (stride < 0) {
data += (h - 1) * stride;
stride = -stride;
}
// this is not always correct, but should work for MPlayer
glAdjustAlignment(stride);
PixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
for (; y + slice <= y_max; y += slice) {
TexSubImage2D(target, 0, x, y, w, slice, format, type, data);
data += stride * slice;
}
if (y < y_max)
TexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
}
static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
float uvcos, float uvsin) {
int i;
ucoef[0] = 0 * uvcos + 1.403 * uvsin;
vcoef[0] = 0 * uvsin + 1.403 * uvcos;
ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
ucoef[2] = 1.770 * uvcos + 0 * uvsin;
vcoef[2] = 1.770 * uvsin + 0 * uvcos;
ucoef[3] = 0;
vcoef[3] = 0;
// Coefficients (probably) must be in [0, 1] range, whereas they originally
// are in [-2, 2] range, so here comes the trick:
// First put them in the [-0.5, 0.5] range, then add 0.5.
// This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
// for CombinerInput and CombinerOutput (or the respective ATI variants)
for (i = 0; i < 4; i++) {
ucoef[i] = ucoef[i] * 0.25 + 0.5;
vcoef[i] = vcoef[i] * 0.25 + 0.5;
}
}
/**
* \brief Setup register combiners for YUV to RGB conversion.
* \param uvcos used for saturation and hue adjustment
* \param uvsin used for saturation and hue adjustment
*/
static void glSetupYUVCombiners(float uvcos, float uvsin) {
GLfloat ucoef[4];
GLfloat vcoef[4];
GLint i;
if (!CombinerInput || !CombinerOutput ||
!CombinerParameterfv || !CombinerParameteri) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
return;
}
GetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
if (i < 2)
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
GetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
if (i < 3)
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
CombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
CombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
// UV first, like this green component cannot overflow
CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
CombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
GL_FALSE, GL_FALSE);
// stage 2
CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
GL_SIGNED_IDENTITY_NV, GL_RGB);
CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
GL_UNSIGNED_INVERT_NV, GL_RGB);
CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
GL_UNSIGNED_INVERT_NV, GL_RGB);
CombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
GL_FALSE, GL_FALSE);
// leave final combiner stage in default mode
CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
}
/**
* \brief Setup ATI version of register combiners for YUV to RGB conversion.
* \param uvcos used for saturation and hue adjustment
* \param uvsin used for saturation and hue adjustment
*
* ATI called this fragment shader, but the name is confusing in the
* light of a very different OpenGL 2.0 extension with the same name
*/
static void glSetupYUVCombinersATI(float uvcos, float uvsin) {
GLfloat ucoef[4];
GLfloat vcoef[4];
GLint i;
if (!BeginFragmentShader || !EndFragmentShader ||
!SetFragmentShaderConstant || !SampleMap ||
!ColorFragmentOp2 || !ColorFragmentOp3) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
return;
}
GetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
if (i < 3)
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
GetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
if (i < 3)
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
BeginFragmentShader();
SetFragmentShaderConstant(GL_CON_0_ATI, ucoef);
SetFragmentShaderConstant(GL_CON_1_ATI, vcoef);
SampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
SampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
SampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
// UV first, like this green component cannot overflow
ColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI);
ColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_4X_BIT_ATI,
GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
GL_REG_1_ATI, GL_NONE, GL_NONE);
ColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
GL_REG_0_ATI, GL_NONE, GL_NONE,
GL_REG_2_ATI, GL_NONE, GL_NONE);
EndFragmentShader();
}
/**
* \brief helper function for gen_spline_lookup_tex
* \param x subpixel-position ((0,1) range) to calculate weights for
* \param dst where to store transformed weights, must provide space for 4 GLfloats
*
* calculates the weights and stores them after appropriate transformation
* for the scaler fragment program.
*/
static void store_weights(float x, GLfloat *dst) {
float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
*dst++ = 1 + x - w1 / (w0 + w1);
*dst++ = 1 - x + w3 / (w2 + w3);
*dst++ = w0 + w1;
*dst++ = 0;
}
//! to avoid artefacts this should be rather large
#define LOOKUP_BSPLINE_RES (2 * 1024)
/**
* \brief creates the 1D lookup texture needed for fast higher-order filtering
* \param unit texture unit to attach texture to
*/
static void gen_spline_lookup_tex(GLenum unit) {
GLfloat *tex = calloc(4 * LOOKUP_BSPLINE_RES, sizeof(*tex));
GLfloat *tp = tex;
int i;
for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
store_weights(x, tp);
tp += 4;
}
store_weights(0, tex);
store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
ActiveTexture(unit);
TexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
TexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
ActiveTexture(GL_TEXTURE0);
free(tex);
}
static const char *bilin_filt_template =
"TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;";
#define BICUB_FILT_MAIN(textype) \
/* first y-interpolation */ \
"ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;" \
"ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;" \
"TEX a.r, coord.xyxy, texture[%c], "textype";" \
"TEX a.g, coord.zwzw, texture[%c], "textype";" \
/* second y-interpolation */ \
"TEX b.r, coord2.xyxy, texture[%c], "textype";" \
"TEX b.g, coord2.zwzw, texture[%c], "textype";" \
"LRP a.b, parmy.b, a.rrrr, a.gggg;" \
"LRP a.a, parmy.b, b.rrrr, b.gggg;" \
/* x-interpolation */ \
"LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;"
static const char *bicub_filt_template_2D =
"MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
"TEX parmx, coord.x, texture[%c], 1D;"
"MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
"TEX parmy, coord.y, texture[%c], 1D;"
"MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
BICUB_FILT_MAIN("2D");
static const char *bicub_filt_template_RECT =
"ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
"TEX parmx, coord.x, texture[%c], 1D;"
"MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
"TEX parmy, coord.y, texture[%c], 1D;"
"MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
BICUB_FILT_MAIN("RECT");
#define CALCWEIGHTS(t, s) \
"MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};" \
"MAD "t", "t", "s", {0, 0, -0.5, 0.5};" \
"MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};" \
"RCP a.x, "t".z;" \
"RCP a.y, "t".w;" \
"MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};" \
"ADD "t".x, "t".xxxx, "s";" \
"SUB "t".y, "t".yyyy, "s";"
static const char *bicub_notex_filt_template_2D =
"MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
"FRC coord.xy, coord.xyxy;"
CALCWEIGHTS("parmx", "coord.xxxx")
"MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
CALCWEIGHTS("parmy", "coord.yyyy")
"MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
BICUB_FILT_MAIN("2D");
static const char *bicub_notex_filt_template_RECT =
"ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
"FRC coord.xy, coord.xyxy;"
CALCWEIGHTS("parmx", "coord.xxxx")
"MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
CALCWEIGHTS("parmy", "coord.yyyy")
"MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
BICUB_FILT_MAIN("RECT");
#define BICUB_X_FILT_MAIN(textype) \
"ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;" \
"ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;" \
"TEX a.r, coord, texture[%c], "textype";" \
"TEX b.r, coord2, texture[%c], "textype";" \
/* x-interpolation */ \
"LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;"
static const char *bicub_x_filt_template_2D =
"MAD coord.x, fragment.texcoord[%c], {%f}, {0.5};"
"TEX parmx, coord, texture[%c], 1D;"
"MUL cdelta.xyz, parmx.rrgg, {-%f, 0, %f};"
BICUB_X_FILT_MAIN("2D");
static const char *bicub_x_filt_template_RECT =
"ADD coord.x, fragment.texcoord[%c], {0.5};"
"TEX parmx, coord, texture[%c], 1D;"
"MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};"
BICUB_X_FILT_MAIN("RECT");
static const char *unsharp_filt_template =
"PARAM dcoord%c = {%f, %f, %f, %f};"
"ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
"SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
"TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
"TEX b.r, coord.xyxy, texture[%c], %s;"
"TEX b.g, coord.zwzw, texture[%c], %s;"
"ADD b.r, b.r, b.g;"
"TEX b.b, coord2.xyxy, texture[%c], %s;"
"TEX b.g, coord2.zwzw, texture[%c], %s;"
"DP3 b, b, {0.25, 0.25, 0.25};"
"SUB b.r, a.r, b.r;"
"MAD yuv.%c, b.r, {%f}, a.r;";
static const char *unsharp_filt_template2 =
"PARAM dcoord%c = {%f, %f, %f, %f};"
"PARAM dcoord2%c = {%f, 0, 0, %f};"
"ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
"SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
"TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
"TEX b.r, coord.xyxy, texture[%c], %s;"
"TEX b.g, coord.zwzw, texture[%c], %s;"
"ADD b.r, b.r, b.g;"
"TEX b.b, coord2.xyxy, texture[%c], %s;"
"TEX b.g, coord2.zwzw, texture[%c], %s;"
"ADD b.r, b.r, b.b;"
"ADD b.a, b.r, b.g;"
"ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;"
"SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;"
"TEX b.r, coord.xyxy, texture[%c], %s;"
"TEX b.g, coord.zwzw, texture[%c], %s;"
"ADD b.r, b.r, b.g;"
"TEX b.b, coord2.xyxy, texture[%c], %s;"
"TEX b.g, coord2.zwzw, texture[%c], %s;"
"DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};"
"MAD b.r, a.r, {0.859375}, b.r;"
"MAD yuv.%c, b.r, {%f}, a.r;";
static const char *yuv_prog_template =
"PARAM ycoef = {%.4f, %.4f, %.4f};"
"PARAM ucoef = {%.4f, %.4f, %.4f};"
"PARAM vcoef = {%.4f, %.4f, %.4f};"
"PARAM offsets = {%.4f, %.4f, %.4f};"
"TEMP res;"
"MAD res.rgb, yuv.rrrr, ycoef, offsets;"
"MAD res.rgb, yuv.gggg, ucoef, res;"
"MAD result.color.rgb, yuv.bbbb, vcoef, res;"
"END";
static const char *yuv_pow_prog_template =
"PARAM ycoef = {%.4f, %.4f, %.4f};"
"PARAM ucoef = {%.4f, %.4f, %.4f};"
"PARAM vcoef = {%.4f, %.4f, %.4f};"
"PARAM offsets = {%.4f, %.4f, %.4f};"
"PARAM gamma = {%.4f, %.4f, %.4f};"
"TEMP res;"
"MAD res.rgb, yuv.rrrr, ycoef, offsets;"
"MAD res.rgb, yuv.gggg, ucoef, res;"
"MAD_SAT res.rgb, yuv.bbbb, vcoef, res;"
"POW result.color.r, res.r, gamma.r;"
"POW result.color.g, res.g, gamma.g;"
"POW result.color.b, res.b, gamma.b;"
"END";
static const char *yuv_lookup_prog_template =
"PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
"PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
"PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
"PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
"TEMP res;"
"MAD res, yuv.rrrr, ycoef, offsets;"
"MAD res.rgb, yuv.gggg, ucoef, res;"
"MAD res.rgb, yuv.bbbb, vcoef, res;"
"TEX result.color.r, res.raaa, texture[%c], 2D;"
"ADD res.a, res.a, 0.25;"
"TEX result.color.g, res.gaaa, texture[%c], 2D;"
"ADD res.a, res.a, 0.25;"
"TEX result.color.b, res.baaa, texture[%c], 2D;"
"END";
static const char *yuv_lookup3d_prog_template =
"TEX result.color, yuv, texture[%c], 3D;"
"END";
/**
* \brief creates and initializes helper textures needed for scaling texture read
* \param scaler scaler type to create texture for
* \param texu contains next free texture unit number
* \param texs texture unit ids for the scaler are stored in this array
*/
static void create_scaler_textures(int scaler, int *texu, char *texs) {
switch (scaler) {
case YUV_SCALER_BILIN:
case YUV_SCALER_BICUB_NOTEX:
case YUV_SCALER_UNSHARP:
case YUV_SCALER_UNSHARP2:
break;
case YUV_SCALER_BICUB:
case YUV_SCALER_BICUB_X:
texs[0] = (*texu)++;
gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
texs[0] += '0';
break;
default:
mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
}
}
static void gen_gamma_map(unsigned char *map, int size, float gamma);
#define ROW_R 0
#define ROW_G 1
#define ROW_B 2
#define COL_Y 0
#define COL_U 1
#define COL_V 2
#define COL_C 3
static void get_yuv2rgb_coeffs(gl_conversion_params_t *params, float yuv2rgb[3][4]) {
float uvcos = params->saturation * cos(params->hue);
float uvsin = params->saturation * sin(params->hue);
int i;
float uv_coeffs[3][2] = {
{ 0.000, 1.596},
{-0.391, -0.813},
{ 2.018, 0.000}
};
for (i = 0; i < 3; i++) {
yuv2rgb[i][COL_C] = params->brightness;
yuv2rgb[i][COL_Y] = 1.164 * params->contrast;
yuv2rgb[i][COL_C] += (-16 / 255.0) * yuv2rgb[i][COL_Y];
yuv2rgb[i][COL_U] = uv_coeffs[i][0] * uvcos + uv_coeffs[i][1] * uvsin;
yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_U];
yuv2rgb[i][COL_V] = uv_coeffs[i][0] * uvsin + uv_coeffs[i][1] * uvcos;
yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_V];
// this "centers" contrast control so that e.g. a contrast of 0
// leads to a grey image, not a black one
yuv2rgb[i][COL_C] += 0.5 - params->contrast / 2.0;
}
}
//! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
#define GMAP_SIZE (1024)
/**
* \brief generate a 3D YUV -> RGB map
* \param params struct containing parameters like brightness, gamma, ...
* \param map where to store map. Must provide space for (size + 2)^3 elements
* \param size size of the map, excluding border
*/
static void gen_yuv2rgb_map(gl_conversion_params_t *params, unsigned char *map, int size) {
int i, j, k, l;
float step = 1.0 / size;
float y, u, v;
float yuv2rgb[3][4];
unsigned char gmaps[3][GMAP_SIZE];
gen_gamma_map(gmaps[0], GMAP_SIZE, params->rgamma);
gen_gamma_map(gmaps[1], GMAP_SIZE, params->ggamma);
gen_gamma_map(gmaps[2], GMAP_SIZE, params->bgamma);
get_yuv2rgb_coeffs(params, yuv2rgb);
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
yuv2rgb[i][j] *= GMAP_SIZE - 1;
v = 0;
for (i = -1; i <= size; i++) {
u = 0;
for (j = -1; j <= size; j++) {
y = 0;
for (k = -1; k <= size; k++) {
for (l = 0; l < 3; l++) {
float rgb = yuv2rgb[l][COL_Y] * y + yuv2rgb[l][COL_U] * u + yuv2rgb[l][COL_V] * v + yuv2rgb[l][COL_C];
*map++ = gmaps[l][av_clip(rgb, 0, GMAP_SIZE - 1)];
}
y += (k == -1 || k == size - 1) ? step / 2 : step;
}
u += (j == -1 || j == size - 1) ? step / 2 : step;
}
v += (i == -1 || i == size - 1) ? step / 2 : step;
}
}
//! resolution of texture for gamma lookup table
#define LOOKUP_RES 512
//! resolution for 3D yuv->rgb conversion lookup table
#define LOOKUP_3DRES 32
/**
* \brief creates and initializes helper textures needed for yuv conversion
* \param params struct containing parameters like brightness, gamma, ...
* \param texu contains next free texture unit number
* \param texs texture unit ids for the conversion are stored in this array
*/
static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
unsigned char *lookup_data = NULL;
int conv = YUV_CONVERSION(params->type);
switch (conv) {
case YUV_CONVERSION_FRAGMENT:
case YUV_CONVERSION_FRAGMENT_POW:
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP:
texs[0] = (*texu)++;
ActiveTexture(GL_TEXTURE0 + texs[0]);
lookup_data = malloc(4 * LOOKUP_RES);
gen_gamma_map(lookup_data, LOOKUP_RES, params->rgamma);
gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->ggamma);
gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->bgamma);
glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
LOOKUP_RES, 4, 0);
glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
ActiveTexture(GL_TEXTURE0);
texs[0] += '0';
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
{
int sz = LOOKUP_3DRES + 2; // texture size including borders
if (!TexImage3D) {
mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
break;
}
texs[0] = (*texu)++;
ActiveTexture(GL_TEXTURE0 + texs[0]);
lookup_data = malloc(3 * sz * sz * sz);
gen_yuv2rgb_map(params, lookup_data, LOOKUP_3DRES);
glAdjustAlignment(sz);
PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
TexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
TexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
ActiveTexture(GL_TEXTURE0);
texs[0] += '0';
}
break;
default:
mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
}
if (lookup_data)
free(lookup_data);
}
/**
* \brief adds a scaling texture read at the current fragment program position
* \param scaler type of scaler to insert
* \param prog_pos current position in fragment program
* \param remain how many bytes remain in the buffer given by prog_pos
* \param texs array containing the texture unit identifiers for this scaler
* \param in_tex texture unit the scaler should read from
* \param out_comp component of the yuv variable the scaler stores the result in
* \param rect if rectangular (pixel) adressing should be used for in_tex
* \param texw width of the in_tex texture
* \param texh height of the in_tex texture
* \param strength strength of filter effect if the scaler does some kind of filtering
*/
static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
char in_tex, char out_comp, int rect, int texw, int texh,
double strength) {
const char *ttype = rect ? "RECT" : "2D";
const float ptw = rect ? 1.0 : 1.0 / texw;
const float pth = rect ? 1.0 : 1.0 / texh;
switch (scaler) {
case YUV_SCALER_BILIN:
snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
in_tex, ttype);
break;
case YUV_SCALER_BICUB:
if (rect)
snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
in_tex, texs[0], texs[0],
in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
else
snprintf(*prog_pos, *remain, bicub_filt_template_2D,
in_tex, (float)texw, (float)texh,
texs[0], ptw, ptw, texs[0], pth, pth,
in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
break;
case YUV_SCALER_BICUB_X:
if (rect)
snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
in_tex, texs[0],
in_tex, in_tex, in_tex, in_tex, out_comp);
else
snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
in_tex, (float)texw,
texs[0], ptw, ptw,
in_tex, in_tex, in_tex, in_tex, out_comp);
break;
case YUV_SCALER_BICUB_NOTEX:
if (rect)
snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
in_tex,
in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
else
snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
break;
case YUV_SCALER_UNSHARP:
snprintf(*prog_pos, *remain, unsharp_filt_template,
out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
in_tex, out_comp, in_tex, out_comp, in_tex,
in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
in_tex, ttype, out_comp, strength);
break;
case YUV_SCALER_UNSHARP2:
snprintf(*prog_pos, *remain, unsharp_filt_template2,
out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
out_comp, 1.5 * ptw, 1.5 * pth,
in_tex, out_comp, in_tex, out_comp, in_tex,
in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
in_tex, ttype, in_tex, ttype, in_tex, ttype,
in_tex, ttype, out_comp, strength);
break;
}
*remain -= strlen(*prog_pos);
*prog_pos += strlen(*prog_pos);
}
static const struct {
const char *name;
GLenum cur;
GLenum max;
} progstats[] = {
{"instructions", 0x88A0, 0x88A1},
{"native instructions", 0x88A2, 0x88A3},
{"temporaries", 0x88A4, 0x88A5},
{"native temporaries", 0x88A6, 0x88A7},
{"parameters", 0x88A8, 0x88A9},
{"native parameters", 0x88AA, 0x88AB},
{"attribs", 0x88AC, 0x88AD},
{"native attribs", 0x88AE, 0x88AF},
{"ALU instructions", 0x8805, 0x880B},
{"TEX instructions", 0x8806, 0x880C},
{"TEX indirections", 0x8807, 0x880D},
{"native ALU instructions", 0x8808, 0x880E},
{"native TEX instructions", 0x8809, 0x880F},
{"native TEX indirections", 0x880A, 0x8810},
{NULL, 0, 0}
};
/**
* \brief load the specified GPU Program
* \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
* \param prog program string
* \return 1 on success, 0 otherwise
*/
int loadGPUProgram(GLenum target, char *prog) {
int i;
GLint cur = 0, max = 0, err = 0;
if (!ProgramString) {
mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
return 0;
}
ProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
GetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
if (err != -1) {
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] Error compiling fragment program, make sure your card supports\n"
"[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
"[gl] Error message:\n %s at %.10s\n",
GetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
return 0;
}
if (!GetProgramiv || !mp_msg_test(MSGT_VO, MSGL_DBG2))
return 1;
mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
for (i = 0; progstats[i].name; i++) {
GetProgramiv(target, progstats[i].cur, &cur);
GetProgramiv(target, progstats[i].max, &max);
mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
}
return 1;
}
#define MAX_PROGSZ (1024*1024)
/**
* \brief setup a fragment program that will do YUV->RGB conversion
* \param parms struct containing parameters like conversion and scaler type,
* brightness, ...
*/
static void glSetupYUVFragprog(gl_conversion_params_t *params) {
int type = params->type;
int texw = params->texw;
int texh = params->texh;
int rect = params->target == GL_TEXTURE_RECTANGLE;
static const char prog_hdr[] =
"!!ARBfp1.0\n"
"OPTION ARB_precision_hint_fastest;"
// all scaler variables must go here so they aren't defined
// multiple times when the same scaler is used more than once
"TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;";
int prog_remain;
char *yuv_prog, *prog_pos;
int cur_texu = 3;
char lum_scale_texs[1];
char chrom_scale_texs[1];
char conv_texs[1];
GLint i;
// this is the conversion matrix, with y, u, v factors
// for red, green, blue and the constant offsets
float yuv2rgb[3][4];
create_conv_textures(params, &cur_texu, conv_texs);
create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
else
create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
if (i < cur_texu)
mp_msg(MSGT_VO, MSGL_ERR,
"[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
cur_texu, i);
if (!ProgramString) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
return;
}
yuv_prog = malloc(MAX_PROGSZ);
strcpy(yuv_prog, prog_hdr);
prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
'0', 'r', rect, texw, texh, params->filter_strength);
add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
'1', 'g', rect, texw / 2, texh / 2, params->filter_strength);
add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
'2', 'b', rect, texw / 2, texh / 2, params->filter_strength);
get_yuv2rgb_coeffs(params, yuv2rgb);
switch (YUV_CONVERSION(type)) {
case YUV_CONVERSION_FRAGMENT:
snprintf(prog_pos, prog_remain, yuv_prog_template,
yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
break;
case YUV_CONVERSION_FRAGMENT_POW:
snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
(float)1.0 / params->rgamma, (float)1.0 / params->bgamma, (float)1.0 / params->bgamma);
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP:
snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
conv_texs[0], conv_texs[0], conv_texs[0]);
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
break;
default:
mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
break;
}
mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", yuv_prog);
loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
free(yuv_prog);
}
/**
* \brief little helper function to create a lookup table for gamma
* \param map buffer to create map into
* \param size size of buffer
* \param gamma gamma value
*/
static void gen_gamma_map(unsigned char *map, int size, float gamma) {
int i;
if (gamma == 1.0) {
for (i = 0; i < size; i++)
map[i] = 255 * i / (size - 1);
return;
}
gamma = 1.0 / gamma;
for (i = 0; i < size; i++) {
float tmp = (float)i / (size - 1.0);
tmp = pow(tmp, gamma);
if (tmp > 1.0) tmp = 1.0;
if (tmp < 0.0) tmp = 0.0;
map[i] = 255 * tmp;
}
}
/**
* \brief setup YUV->RGB conversion
* \param parms struct containing parameters like conversion and scaler type,
* brightness, ...
* \ingroup glconversion
*/
void glSetupYUVConversion(gl_conversion_params_t *params) {
float uvcos = params->saturation * cos(params->hue);
float uvsin = params->saturation * sin(params->hue);
switch (YUV_CONVERSION(params->type)) {
case YUV_CONVERSION_COMBINERS:
glSetupYUVCombiners(uvcos, uvsin);
break;
case YUV_CONVERSION_COMBINERS_ATI:
glSetupYUVCombinersATI(uvcos, uvsin);
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP:
case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
case YUV_CONVERSION_FRAGMENT:
case YUV_CONVERSION_FRAGMENT_POW:
glSetupYUVFragprog(params);
break;
case YUV_CONVERSION_NONE:
break;
default:
mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
}
}
/**
* \brief enable the specified YUV conversion
* \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
* \param type type of YUV conversion
* \ingroup glconversion
*/
void glEnableYUVConversion(GLenum target, int type) {
switch (YUV_CONVERSION(type)) {
case YUV_CONVERSION_COMBINERS:
ActiveTexture(GL_TEXTURE1);
Enable(target);
ActiveTexture(GL_TEXTURE2);
Enable(target);
ActiveTexture(GL_TEXTURE0);
Enable(GL_REGISTER_COMBINERS_NV);
break;
case YUV_CONVERSION_COMBINERS_ATI:
ActiveTexture(GL_TEXTURE1);
Enable(target);
ActiveTexture(GL_TEXTURE2);
Enable(target);
ActiveTexture(GL_TEXTURE0);
Enable(GL_FRAGMENT_SHADER_ATI);
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
case YUV_CONVERSION_FRAGMENT_LOOKUP:
case YUV_CONVERSION_FRAGMENT_POW:
case YUV_CONVERSION_FRAGMENT:
case YUV_CONVERSION_NONE:
Enable(GL_FRAGMENT_PROGRAM);
break;
}
}
/**
* \brief disable the specified YUV conversion
* \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
* \param type type of YUV conversion
* \ingroup glconversion
*/
void glDisableYUVConversion(GLenum target, int type) {
switch (YUV_CONVERSION(type)) {
case YUV_CONVERSION_COMBINERS:
ActiveTexture(GL_TEXTURE1);
Disable(target);
ActiveTexture(GL_TEXTURE2);
Disable(target);
ActiveTexture(GL_TEXTURE0);
Disable(GL_REGISTER_COMBINERS_NV);
break;
case YUV_CONVERSION_COMBINERS_ATI:
ActiveTexture(GL_TEXTURE1);
Disable(target);
ActiveTexture(GL_TEXTURE2);
Disable(target);
ActiveTexture(GL_TEXTURE0);
Disable(GL_FRAGMENT_SHADER_ATI);
break;
case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
case YUV_CONVERSION_FRAGMENT_LOOKUP:
case YUV_CONVERSION_FRAGMENT_POW:
case YUV_CONVERSION_FRAGMENT:
case YUV_CONVERSION_NONE:
Disable(GL_FRAGMENT_PROGRAM);
break;
}
}
/**
* \brief draw a texture part at given 2D coordinates
* \param x screen top coordinate
* \param y screen left coordinate
* \param w screen width coordinate
* \param h screen height coordinate
* \param tx texture top coordinate in pixels
* \param ty texture left coordinate in pixels
* \param tw texture part width in pixels
* \param th texture part height in pixels
* \param sx width of texture in pixels
* \param sy height of texture in pixels
* \param rect_tex whether this texture uses texture_rectangle extension
* \param is_yv12 if set, also draw the textures from units 1 and 2
* \param flip flip the texture upside down
* \ingroup gltexture
*/
void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
int sx, int sy, int rect_tex, int is_yv12, int flip) {
GLfloat tx2 = tx / 2, ty2 = ty / 2, tw2 = tw / 2, th2 = th / 2;
if (!rect_tex) {
tx /= sx; ty /= sy; tw /= sx; th /= sy;
tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
}
if (flip) {
y += h;
h = -h;
}
Begin(GL_QUADS);
TexCoord2f(tx, ty);
if (is_yv12) {
MultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
MultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
}
Vertex2f(x, y);
TexCoord2f(tx, ty + th);
if (is_yv12) {
MultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
MultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
}
Vertex2f(x, y + h);
TexCoord2f(tx + tw, ty + th);
if (is_yv12) {
MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
}
Vertex2f(x + w, y + h);
TexCoord2f(tx + tw, ty);
if (is_yv12) {
MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
}
Vertex2f(x + w, y);
End();
}
#ifdef CONFIG_GL_WIN32
#include "w32_common.h"
/**
* \brief little helper since wglGetProcAddress definition does not fit our
* getProcAddress
* \param procName name of function to look up
* \return function pointer returned by wglGetProcAddress
*/
static void *w32gpa(const GLubyte *procName) {
HMODULE oglmod;
void *res = wglGetProcAddress(procName);
if (res) return res;
oglmod = GetModuleHandle("opengl32.dll");
return GetProcAddress(oglmod, procName);
}
static int setGlWindow_w32(MPGLContext *ctx)
{
HWND win = vo_w32_window;
int *vinfo = &ctx->vinfo.w32;
HGLRC *context = &ctx->context.w32;
int new_vinfo;
HDC windc = vo_w32_get_dc(win);
HGLRC new_context = 0;
int keep_context = 0;
int res = SET_WINDOW_FAILED;
// should only be needed when keeping context, but not doing glFinish
// can cause flickering even when we do not keep it.
if (*context)
Finish();
new_vinfo = GetPixelFormat(windc);
if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
// we can keep the wglContext
new_context = *context;
keep_context = 1;
} else {
// create a context
new_context = wglCreateContext(windc);
if (!new_context) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
goto out;
}
}
// set context
if (!wglMakeCurrent(windc, new_context)) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
if (!keep_context) {
wglDeleteContext(new_context);
}
goto out;
}
// set new values
vo_w32_window = win;
{
RECT rect;
GetClientRect(win, &rect);
vo_dwidth = rect.right;
vo_dheight = rect.bottom;
}
if (!keep_context) {
if (*context)
wglDeleteContext(*context);
*context = new_context;
*vinfo = new_vinfo;
getFunctions(w32gpa, NULL);
// and inform that reinit is neccessary
res = SET_WINDOW_REINIT;
} else
res = SET_WINDOW_OK;
out:
vo_w32_release_dc(win, windc);
return res;
}
static void releaseGlContext_w32(MPGLContext *ctx) {
int *vinfo = &ctx->vinfo.w32;
HGLRC *context = &ctx->context.w32;
*vinfo = 0;
if (*context) {
wglMakeCurrent(0, 0);
wglDeleteContext(*context);
}
*context = 0;
}
static void swapGlBuffers_w32(MPGLContext *ctx) {
HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
SwapBuffers(vo_hdc);
vo_w32_release_dc(vo_w32_window, vo_hdc);
}
#endif
#ifdef CONFIG_GL_X11
#ifdef HAVE_LIBDL
#include <dlfcn.h>
#endif
#include "x11_common.h"
/**
* \brief find address of a linked function
* \param s name of function to find
* \return address of function or NULL if not found
*/
static void *getdladdr(const char *s) {
void *ret = NULL;
#ifdef HAVE_LIBDL
void *handle = dlopen(NULL, RTLD_LAZY);
if (!handle)
return NULL;
ret = dlsym(handle, s);
dlclose(handle);
#endif
return ret;
}
/**
* \brief Returns the XVisualInfo associated with Window win.
* \param win Window whose XVisualInfo is returne.
* \return XVisualInfo of the window. Caller must use XFree to free it.
*/
static XVisualInfo *getWindowVisualInfo(Window win) {
XWindowAttributes xw_attr;
XVisualInfo vinfo_template;
int tmp;
XGetWindowAttributes(mDisplay, win, &xw_attr);
vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
}
static void appendstr(char **dst, const char *str)
{
int newsize;
char *newstr;
if (!str)
return;
newsize = strlen(*dst) + 1 + strlen(str) + 1;
newstr = realloc(*dst, newsize);
if (!newstr)
return;
*dst = newstr;
strcat(*dst, " ");
strcat(*dst, str);
}
/**
* \brief Changes the window in which video is displayed.
* If possible only transfers the context to the new window, otherwise
* creates a new one, which must be initialized by the caller.
* \param vinfo Currently used visual.
* \param context Currently used context.
* \param win window that should be used for drawing.
* \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
* In case of SET_WINDOW_REINIT the context could not be transfered
* and the caller must initialize it correctly.
* \ingroup glcontext
*/
static int setGlWindow_x11(MPGLContext *ctx)
{
XVisualInfo **vinfo = &ctx->vinfo.x11;
GLXContext *context = &ctx->context.x11;
Window win = vo_window;
XVisualInfo *new_vinfo;
GLXContext new_context = NULL;
int keep_context = 0;
// should only be needed when keeping context, but not doing glFinish
// can cause flickering even when we do not keep it.
if (*context)
Finish();
new_vinfo = getWindowVisualInfo(win);
if (*context && *vinfo && new_vinfo &&
(*vinfo)->visualid == new_vinfo->visualid) {
// we can keep the GLXContext
new_context = *context;
XFree(new_vinfo);
new_vinfo = *vinfo;
keep_context = 1;
} else {
// create a context
new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
if (!new_context) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
XFree(new_vinfo);
return SET_WINDOW_FAILED;
}
}
// set context
if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
if (!keep_context) {
glXDestroyContext(mDisplay, new_context);
XFree(new_vinfo);
}
return SET_WINDOW_FAILED;
}
// set new values
vo_window = win;
vo_x11_update_geometry();
if (!keep_context) {
void *(*getProcAddress)(const GLubyte *);
const char *(*glXExtStr)(Display *, int);
char *glxstr = strdup("");
if (*context)
glXDestroyContext(mDisplay, *context);
*context = new_context;
if (*vinfo)
XFree(*vinfo);
*vinfo = new_vinfo;
getProcAddress = getdladdr("glXGetProcAddress");
if (!getProcAddress)
getProcAddress = getdladdr("glXGetProcAddressARB");
if (!getProcAddress)
getProcAddress = (void *)getdladdr;
glXExtStr = getdladdr("glXQueryExtensionsString");
if (glXExtStr)
appendstr(&glxstr, glXExtStr(mDisplay, DefaultScreen(mDisplay)));
glXExtStr = getdladdr("glXGetClientString");
if (glXExtStr)
appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
glXExtStr = getdladdr("glXGetServerString");
if (glXExtStr)
appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
getFunctions(getProcAddress, glxstr);
free(glxstr);
// and inform that reinit is neccessary
return SET_WINDOW_REINIT;
}
return SET_WINDOW_OK;
}
/**
* \brief free the VisualInfo and GLXContext of an OpenGL context.
* \ingroup glcontext
*/
static void releaseGlContext_x11(MPGLContext *ctx) {
XVisualInfo **vinfo = &ctx->vinfo.x11;
GLXContext *context = &ctx->context.x11;
if (*vinfo)
XFree(*vinfo);
*vinfo = NULL;
if (*context)
{
Finish();
glXMakeCurrent(mDisplay, None, NULL);
glXDestroyContext(mDisplay, *context);
}
*context = 0;
}
static void swapGlBuffers_x11(MPGLContext *ctx) {
glXSwapBuffers(mDisplay, vo_window);
}
static int x11_check_events(void) {
return vo_x11_check_events(mDisplay);
}
#endif
int init_mpglcontext(MPGLContext *ctx, enum MPGLType type) {
memset(ctx, 0, sizeof(*ctx));
ctx->type = type;
switch (ctx->type) {
#ifdef CONFIG_GL_WIN32
case GLTYPE_W32:
ctx->setGlWindow = setGlWindow_w32;
ctx->releaseGlContext = releaseGlContext_w32;
ctx->swapGlBuffers = swapGlBuffers_w32;
ctx->update_xinerama_info = w32_update_xinerama_info;
ctx->border = vo_w32_border;
ctx->check_events = vo_w32_check_events;
ctx->fullscreen = vo_w32_fullscreen;
ctx->ontop = vo_w32_ontop;
return vo_w32_init();
#endif
#ifdef CONFIG_GL_X11
case GLTYPE_X11:
ctx->setGlWindow = setGlWindow_x11;
ctx->releaseGlContext = releaseGlContext_x11;
ctx->swapGlBuffers = swapGlBuffers_x11;
ctx->update_xinerama_info = update_xinerama_info;
ctx->border = vo_x11_border;
ctx->check_events = x11_check_events;
ctx->fullscreen = vo_x11_fullscreen;
ctx->ontop = vo_x11_ontop;
return vo_init();
#endif
default:
return 0;
}
}
void uninit_mpglcontext(MPGLContext *ctx) {
ctx->releaseGlContext(ctx);
switch (ctx->type) {
#ifdef CONFIG_GL_WIN32
case GLTYPE_W32:
vo_w32_uninit();
break;
#endif
#ifdef CONFIG_GL_X11
case GLTYPE_X11:
vo_x11_uninit();
break;
#endif
}
memset(ctx, 0, sizeof(*ctx));
}
|