LCOV - code coverage report
Current view: top level - src/shaders - film_grain_av1.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 303 331 91.5 %
Date: 2025-03-29 09:04:10 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 165 254 65.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of libplacebo, which is normally licensed under the terms
       3                 :            :  * of the LGPL v2.1+. However, this file (film_grain_av1.c) is also available
       4                 :            :  * under the terms of the more permissive MIT license:
       5                 :            :  *
       6                 :            :  * Copyright (c) 2018-2019 Niklas Haas
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in all
      16                 :            :  * copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      24                 :            :  * SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "shaders.h"
      28                 :            : #include "shaders/film_grain.h"
      29                 :            : 
      30                 :            : // Taken from the spec. Range is [-2048, 2047], mean is 0 and stddev is 512
      31                 :            : static const int16_t gaussian_sequence[2048] = {
      32                 :            :   56,    568,   -180,  172,   124,   -84,   172,   -64,   -900,  24,   820,
      33                 :            :   224,   1248,  996,   272,   -8,    -916,  -388,  -732,  -104,  -188, 800,
      34                 :            :   112,   -652,  -320,  -376,  140,   -252,  492,   -168,  44,    -788, 588,
      35                 :            :   -584,  500,   -228,  12,    680,   272,   -476,  972,   -100,  652,  368,
      36                 :            :   432,   -196,  -720,  -192,  1000,  -332,  652,   -136,  -552,  -604, -4,
      37                 :            :   192,   -220,  -136,  1000,  -52,   372,   -96,   -624,  124,   -24,  396,
      38                 :            :   540,   -12,   -104,  640,   464,   244,   -208,  -84,   368,   -528, -740,
      39                 :            :   248,   -968,  -848,  608,   376,   -60,   -292,  -40,   -156,  252,  -292,
      40                 :            :   248,   224,   -280,  400,   -244,  244,   -60,   76,    -80,   212,  532,
      41                 :            :   340,   128,   -36,   824,   -352,  -60,   -264,  -96,   -612,  416,  -704,
      42                 :            :   220,   -204,  640,   -160,  1220,  -408,  900,   336,   20,    -336, -96,
      43                 :            :   -792,  304,   48,    -28,   -1232, -1172, -448,  104,   -292,  -520, 244,
      44                 :            :   60,    -948,  0,     -708,  268,   108,   356,   -548,  488,   -344, -136,
      45                 :            :   488,   -196,  -224,  656,   -236,  -1128, 60,    4,     140,   276,  -676,
      46                 :            :   -376,  168,   -108,  464,   8,     564,   64,    240,   308,   -300, -400,
      47                 :            :   -456,  -136,  56,    120,   -408,  -116,  436,   504,   -232,  328,  844,
      48                 :            :   -164,  -84,   784,   -168,  232,   -224,  348,   -376,  128,   568,  96,
      49                 :            :   -1244, -288,  276,   848,   832,   -360,  656,   464,   -384,  -332, -356,
      50                 :            :   728,   -388,  160,   -192,  468,   296,   224,   140,   -776,  -100, 280,
      51                 :            :   4,     196,   44,    -36,   -648,  932,   16,    1428,  28,    528,  808,
      52                 :            :   772,   20,    268,   88,    -332,  -284,  124,   -384,  -448,  208,  -228,
      53                 :            :   -1044, -328,  660,   380,   -148,  -300,  588,   240,   540,   28,   136,
      54                 :            :   -88,   -436,  256,   296,   -1000, 1400,  0,     -48,   1056,  -136, 264,
      55                 :            :   -528,  -1108, 632,   -484,  -592,  -344,  796,   124,   -668,  -768, 388,
      56                 :            :   1296,  -232,  -188,  -200,  -288,  -4,    308,   100,   -168,  256,  -500,
      57                 :            :   204,   -508,  648,   -136,  372,   -272,  -120,  -1004, -552,  -548, -384,
      58                 :            :   548,   -296,  428,   -108,  -8,    -912,  -324,  -224,  -88,   -112, -220,
      59                 :            :   -100,  996,   -796,  548,   360,   -216,  180,   428,   -200,  -212, 148,
      60                 :            :   96,    148,   284,   216,   -412,  -320,  120,   -300,  -384,  -604, -572,
      61                 :            :   -332,  -8,    -180,  -176,  696,   116,   -88,   628,   76,    44,   -516,
      62                 :            :   240,   -208,  -40,   100,   -592,  344,   -308,  -452,  -228,  20,   916,
      63                 :            :   -1752, -136,  -340,  -804,  140,   40,    512,   340,   248,   184,  -492,
      64                 :            :   896,   -156,  932,   -628,  328,   -688,  -448,  -616,  -752,  -100, 560,
      65                 :            :   -1020, 180,   -800,  -64,   76,    576,   1068,  396,   660,   552,  -108,
      66                 :            :   -28,   320,   -628,  312,   -92,   -92,   -472,  268,   16,    560,  516,
      67                 :            :   -672,  -52,   492,   -100,  260,   384,   284,   292,   304,   -148, 88,
      68                 :            :   -152,  1012,  1064,  -228,  164,   -376,  -684,  592,   -392,  156,  196,
      69                 :            :   -524,  -64,   -884,  160,   -176,  636,   648,   404,   -396,  -436, 864,
      70                 :            :   424,   -728,  988,   -604,  904,   -592,  296,   -224,  536,   -176, -920,
      71                 :            :   436,   -48,   1176,  -884,  416,   -776,  -824,  -884,  524,   -548, -564,
      72                 :            :   -68,   -164,  -96,   692,   364,   -692,  -1012, -68,   260,   -480, 876,
      73                 :            :   -1116, 452,   -332,  -352,  892,   -1088, 1220,  -676,  12,    -292, 244,
      74                 :            :   496,   372,   -32,   280,   200,   112,   -440,  -96,   24,    -644, -184,
      75                 :            :   56,    -432,  224,   -980,  272,   -260,  144,   -436,  420,   356,  364,
      76                 :            :   -528,  76,    172,   -744,  -368,  404,   -752,  -416,  684,   -688, 72,
      77                 :            :   540,   416,   92,    444,   480,   -72,   -1416, 164,   -1172, -68,  24,
      78                 :            :   424,   264,   1040,  128,   -912,  -524,  -356,  64,    876,   -12,  4,
      79                 :            :   -88,   532,   272,   -524,  320,   276,   -508,  940,   24,    -400, -120,
      80                 :            :   756,   60,    236,   -412,  100,   376,   -484,  400,   -100,  -740, -108,
      81                 :            :   -260,  328,   -268,  224,   -200,  -416,  184,   -604,  -564,  -20,  296,
      82                 :            :   60,    892,   -888,  60,    164,   68,    -760,  216,   -296,  904,  -336,
      83                 :            :   -28,   404,   -356,  -568,  -208,  -1480, -512,  296,   328,   -360, -164,
      84                 :            :   -1560, -776,  1156,  -428,  164,   -504,  -112,  120,   -216,  -148, -264,
      85                 :            :   308,   32,    64,    -72,   72,    116,   176,   -64,   -272,  460,  -536,
      86                 :            :   -784,  -280,  348,   108,   -752,  -132,  524,   -540,  -776,  116,  -296,
      87                 :            :   -1196, -288,  -560,  1040,  -472,  116,   -848,  -1116, 116,   636,  696,
      88                 :            :   284,   -176,  1016,  204,   -864,  -648,  -248,  356,   972,   -584, -204,
      89                 :            :   264,   880,   528,   -24,   -184,  116,   448,   -144,  828,   524,  212,
      90                 :            :   -212,  52,    12,    200,   268,   -488,  -404,  -880,  824,   -672, -40,
      91                 :            :   908,   -248,  500,   716,   -576,  492,   -576,  16,    720,   -108, 384,
      92                 :            :   124,   344,   280,   576,   -500,  252,   104,   -308,  196,   -188, -8,
      93                 :            :   1268,  296,   1032,  -1196, 436,   316,   372,   -432,  -200,  -660, 704,
      94                 :            :   -224,  596,   -132,  268,   32,    -452,  884,   104,   -1008, 424,  -1348,
      95                 :            :   -280,  4,     -1168, 368,   476,   696,   300,   -8,    24,    180,  -592,
      96                 :            :   -196,  388,   304,   500,   724,   -160,  244,   -84,   272,   -256, -420,
      97                 :            :   320,   208,   -144,  -156,  156,   364,   452,   28,    540,   316,  220,
      98                 :            :   -644,  -248,  464,   72,    360,   32,    -388,  496,   -680,  -48,  208,
      99                 :            :   -116,  -408,  60,    -604,  -392,  548,   -840,  784,   -460,  656,  -544,
     100                 :            :   -388,  -264,  908,   -800,  -628,  -612,  -568,  572,   -220,  164,  288,
     101                 :            :   -16,   -308,  308,   -112,  -636,  -760,  280,   -668,  432,   364,  240,
     102                 :            :   -196,  604,   340,   384,   196,   592,   -44,   -500,  432,   -580, -132,
     103                 :            :   636,   -76,   392,   4,     -412,  540,   508,   328,   -356,  -36,  16,
     104                 :            :   -220,  -64,   -248,  -60,   24,    -192,  368,   1040,  92,    -24,  -1044,
     105                 :            :   -32,   40,    104,   148,   192,   -136,  -520,  56,    -816,  -224, 732,
     106                 :            :   392,   356,   212,   -80,   -424,  -1008, -324,  588,   -1496, 576,  460,
     107                 :            :   -816,  -848,  56,    -580,  -92,   -1372, -112,  -496,  200,   364,  52,
     108                 :            :   -140,  48,    -48,   -60,   84,    72,    40,    132,   -356,  -268, -104,
     109                 :            :   -284,  -404,  732,   -520,  164,   -304,  -540,  120,   328,   -76,  -460,
     110                 :            :   756,   388,   588,   236,   -436,  -72,   -176,  -404,  -316,  -148, 716,
     111                 :            :   -604,  404,   -72,   -88,   -888,  -68,   944,   88,    -220,  -344, 960,
     112                 :            :   472,   460,   -232,  704,   120,   832,   -228,  692,   -508,  132,  -476,
     113                 :            :   844,   -748,  -364,  -44,   1116,  -1104, -1056, 76,    428,   552,  -692,
     114                 :            :   60,    356,   96,    -384,  -188,  -612,  -576,  736,   508,   892,  352,
     115                 :            :   -1132, 504,   -24,   -352,  324,   332,   -600,  -312,  292,   508,  -144,
     116                 :            :   -8,    484,   48,    284,   -260,  -240,  256,   -100,  -292,  -204, -44,
     117                 :            :   472,   -204,  908,   -188,  -1000, -256,  92,    1164,  -392,  564,  356,
     118                 :            :   652,   -28,   -884,  256,   484,   -192,  760,   -176,  376,   -524, -452,
     119                 :            :   -436,  860,   -736,  212,   124,   504,   -476,  468,   76,    -472, 552,
     120                 :            :   -692,  -944,  -620,  740,   -240,  400,   132,   20,    192,   -196, 264,
     121                 :            :   -668,  -1012, -60,   296,   -316,  -828,  76,    -156,  284,   -768, -448,
     122                 :            :   -832,  148,   248,   652,   616,   1236,  288,   -328,  -400,  -124, 588,
     123                 :            :   220,   520,   -696,  1032,  768,   -740,  -92,   -272,  296,   448,  -464,
     124                 :            :   412,   -200,  392,   440,   -200,  264,   -152,  -260,  320,   1032, 216,
     125                 :            :   320,   -8,    -64,   156,   -1016, 1084,  1172,  536,   484,   -432, 132,
     126                 :            :   372,   -52,   -256,  84,    116,   -352,  48,    116,   304,   -384, 412,
     127                 :            :   924,   -300,  528,   628,   180,   648,   44,    -980,  -220,  1320, 48,
     128                 :            :   332,   748,   524,   -268,  -720,  540,   -276,  564,   -344,  -208, -196,
     129                 :            :   436,   896,   88,    -392,  132,   80,    -964,  -288,  568,   56,   -48,
     130                 :            :   -456,  888,   8,     552,   -156,  -292,  948,   288,   128,   -716, -292,
     131                 :            :   1192,  -152,  876,   352,   -600,  -260,  -812,  -468,  -28,   -120, -32,
     132                 :            :   -44,   1284,  496,   192,   464,   312,   -76,   -516,  -380,  -456, -1012,
     133                 :            :   -48,   308,   -156,  36,    492,   -156,  -808,  188,   1652,  68,   -120,
     134                 :            :   -116,  316,   160,   -140,  352,   808,   -416,  592,   316,   -480, 56,
     135                 :            :   528,   -204,  -568,  372,   -232,  752,   -344,  744,   -4,    324,  -416,
     136                 :            :   -600,  768,   268,   -248,  -88,   -132,  -420,  -432,  80,    -288, 404,
     137                 :            :   -316,  -1216, -588,  520,   -108,  92,    -320,  368,   -480,  -216, -92,
     138                 :            :   1688,  -300,  180,   1020,  -176,  820,   -68,   -228,  -260,  436,  -904,
     139                 :            :   20,    40,    -508,  440,   -736,  312,   332,   204,   760,   -372, 728,
     140                 :            :   96,    -20,   -632,  -520,  -560,  336,   1076,  -64,   -532,  776,  584,
     141                 :            :   192,   396,   -728,  -520,  276,   -188,  80,    -52,   -612,  -252, -48,
     142                 :            :   648,   212,   -688,  228,   -52,   -260,  428,   -412,  -272,  -404, 180,
     143                 :            :   816,   -796,  48,    152,   484,   -88,   -216,  988,   696,   188,  -528,
     144                 :            :   648,   -116,  -180,  316,   476,   12,    -564,  96,    476,   -252, -364,
     145                 :            :   -376,  -392,  556,   -256,  -576,  260,   -352,  120,   -16,   -136, -260,
     146                 :            :   -492,  72,    556,   660,   580,   616,   772,   436,   424,   -32,  -324,
     147                 :            :   -1268, 416,   -324,  -80,   920,   160,   228,   724,   32,    -516, 64,
     148                 :            :   384,   68,    -128,  136,   240,   248,   -204,  -68,   252,   -932, -120,
     149                 :            :   -480,  -628,  -84,   192,   852,   -404,  -288,  -132,  204,   100,  168,
     150                 :            :   -68,   -196,  -868,  460,   1080,  380,   -80,   244,   0,     484,  -888,
     151                 :            :   64,    184,   352,   600,   460,   164,   604,   -196,  320,   -64,  588,
     152                 :            :   -184,  228,   12,    372,   48,    -848,  -344,  224,   208,   -200, 484,
     153                 :            :   128,   -20,   272,   -468,  -840,  384,   256,   -720,  -520,  -464, -580,
     154                 :            :   112,   -120,  644,   -356,  -208,  -608,  -528,  704,   560,   -424, 392,
     155                 :            :   828,   40,    84,    200,   -152,  0,     -144,  584,   280,   -120, 80,
     156                 :            :   -556,  -972,  -196,  -472,  724,   80,    168,   -32,   88,    160,  -688,
     157                 :            :   0,     160,   356,   372,   -776,  740,   -128,  676,   -248,  -480, 4,
     158                 :            :   -364,  96,    544,   232,   -1032, 956,   236,   356,   20,    -40,  300,
     159                 :            :   24,    -676,  -596,  132,   1120,  -104,  532,   -1096, 568,   648,  444,
     160                 :            :   508,   380,   188,   -376,  -604,  1488,  424,   24,    756,   -220, -192,
     161                 :            :   716,   120,   920,   688,   168,   44,    -460,  568,   284,   1144, 1160,
     162                 :            :   600,   424,   888,   656,   -356,  -320,  220,   316,   -176,  -724, -188,
     163                 :            :   -816,  -628,  -348,  -228,  -380,  1012,  -452,  -660,  736,   928,  404,
     164                 :            :   -696,  -72,   -268,  -892,  128,   184,   -344,  -780,  360,   336,  400,
     165                 :            :   344,   428,   548,   -112,  136,   -228,  -216,  -820,  -516,  340,  92,
     166                 :            :   -136,  116,   -300,  376,   -244,  100,   -316,  -520,  -284,  -12,  824,
     167                 :            :   164,   -548,  -180,  -128,  116,   -924,  -828,  268,   -368,  -580, 620,
     168                 :            :   192,   160,   0,     -1676, 1068,  424,   -56,   -360,  468,   -156, 720,
     169                 :            :   288,   -528,  556,   -364,  548,   -148,  504,   316,   152,   -648, -620,
     170                 :            :   -684,  -24,   -376,  -384,  -108,  -920,  -1032, 768,   180,   -264, -508,
     171                 :            :   -1268, -260,  -60,   300,   -240,  988,   724,   -376,  -576,  -212, -736,
     172                 :            :   556,   192,   1092,  -620,  -880,  376,   -56,   -4,    -216,  -32,  836,
     173                 :            :   268,   396,   1332,  864,   -600,  100,   56,    -412,  -92,   356,  180,
     174                 :            :   884,   -468,  -436,  292,   -388,  -804,  -704,  -840,  368,   -348, 140,
     175                 :            :   -724,  1536,  940,   372,   112,   -372,  436,   -480,  1136,  296,  -32,
     176                 :            :   -228,  132,   -48,   -220,  868,   -1016, -60,   -1044, -464,  328,  916,
     177                 :            :   244,   12,    -736,  -296,  360,   468,   -376,  -108,  -92,   788,  368,
     178                 :            :   -56,   544,   400,   -672,  -420,  728,   16,    320,   44,    -284, -380,
     179                 :            :   -796,  488,   132,   204,   -596,  -372,  88,    -152,  -908,  -636, -572,
     180                 :            :   -624,  -116,  -692,  -200,  -56,   276,   -88,   484,   -324,  948,  864,
     181                 :            :   1000,  -456,  -184,  -276,  292,   -296,  156,   676,   320,   160,  908,
     182                 :            :   -84,   -1236, -288,  -116,  260,   -372,  -644,  732,   -756,  -96,  84,
     183                 :            :   344,   -520,  348,   -688,  240,   -84,   216,   -1044, -136,  -676, -396,
     184                 :            :   -1500, 960,   -40,   176,   168,   1516,  420,   -504,  -344,  -364, -360,
     185                 :            :   1216,  -940,  -380,  -212,  252,   -660,  -708,  484,   -444,  -152, 928,
     186                 :            :   -120,  1112,  476,   -260,  560,   -148,  -344,  108,   -196,  228,  -288,
     187                 :            :   504,   560,   -328,  -88,   288,   -1008, 460,   -228,  468,   -836, -196,
     188                 :            :   76,    388,   232,   412,   -1168, -716,  -644,  756,   -172,  -356, -504,
     189                 :            :   116,   432,   528,   48,    476,   -168,  -608,  448,   160,   -532, -272,
     190                 :            :   28,    -676,  -12,   828,   980,   456,   520,   104,   -104,  256,  -344,
     191                 :            :   -4,    -28,   -368,  -52,   -524,  -572,  -556,  -200,  768,   1124, -208,
     192                 :            :   -512,  176,   232,   248,   -148,  -888,  604,   -600,  -304,  804,  -156,
     193                 :            :   -212,  488,   -192,  -804,  -256,  368,   -360,  -916,  -328,  228,  -240,
     194                 :            :   -448,  -472,  856,   -556,  -364,  572,   -12,   -156,  -368,  -340, 432,
     195                 :            :   252,   -752,  -152,  288,   268,   -580,  -848,  -592,  108,   -76,  244,
     196                 :            :   312,   -716,  592,   -80,   436,   360,   4,     -248,  160,   516,  584,
     197                 :            :   732,   44,    -468,  -280,  -292,  -156,  -588,  28,    308,   912,  24,
     198                 :            :   124,   156,   180,   -252,  944,   -924,  -772,  -520,  -428,  -624, 300,
     199                 :            :   -212,  -1144, 32,    -724,  800,   -1128, -212,  -1288, -848,  180,  -416,
     200                 :            :   440,   192,   -576,  -792,  -76,   -1080, 80,    -532,  -352,  -132, 380,
     201                 :            :   -820,  148,   1112,  128,   164,   456,   700,   -924,  144,   -668, -384,
     202                 :            :   648,   -832,  508,   552,   -52,   -100,  -656,  208,   -568,  748,  -88,
     203                 :            :   680,   232,   300,   192,   -408,  -1012, -152,  -252,  -268,  272,  -876,
     204                 :            :   -664,  -648,  -332,  -136,  16,    12,    1152,  -28,   332,   -536, 320,
     205                 :            :   -672,  -460,  -316,  532,   -260,  228,   -40,   1052,  -816,  180,  88,
     206                 :            :   -496,  -556,  -672,  -368,  428,   92,    356,   404,   -408,  252,  196,
     207                 :            :   -176,  -556,  792,   268,   32,    372,   40,    96,    -332,  328,  120,
     208                 :            :   372,   -900,  -40,   472,   -264,  -592,  952,   128,   656,   112,  664,
     209                 :            :   -232,  420,   4,     -344,  -464,  556,   244,   -416,  -32,   252,  0,
     210                 :            :   -412,  188,   -696,  508,   -476,  324,   -1096, 656,   -312,  560,  264,
     211                 :            :   -136,  304,   160,   -64,   -580,  248,   336,   -720,  560,   -348, -288,
     212                 :            :   -276,  -196,  -500,  852,   -544,  -236,  -1128, -992,  -776,  116,  56,
     213                 :            :   52,    860,   884,   212,   -12,   168,   1020,  512,   -552,  924,  -148,
     214                 :            :   716,   188,   164,   -340,  -520,  -184,  880,   -152,  -680,  -208, -1156,
     215                 :            :   -300,  -528,  -472,  364,   100,   -744,  -1056, -32,   540,   280,  144,
     216                 :            :   -676,  -32,   -232,  -280,  -224,  96,    568,   -76,   172,   148,  148,
     217                 :            :   104,   32,    -296,  -32,   788,   -80,   32,    -16,   280,   288,  944,
     218                 :            :   428,   -484
     219                 :            : };
     220                 :            : 
     221                 :            : static inline int get_random_number(int bits, uint16_t *state)
     222                 :            : {
     223                 :      95904 :     int r = *state;
     224                 :      95904 :     uint16_t bit = ((r >> 0) ^ (r >> 1) ^ (r >> 3) ^ (r >> 12)) & 1;
     225                 :      95904 :     *state = (r >> 1) | (bit << 15);
     226                 :            : 
     227                 :      95904 :     return (*state >> (16 - bits)) & ((1 << bits) - 1);
     228                 :            : }
     229                 :            : 
     230                 :            : static inline int round2(int x, int shift)
     231                 :            : {
     232                 :     223456 :     if (!shift)
     233                 :            :         return x;
     234                 :            : 
     235                 :     180896 :     return (x + (1 << (shift - 1))) >> shift;
     236                 :            : }
     237                 :            : 
     238                 :            : enum {
     239                 :            :     BLOCK_SIZE = 32,
     240                 :            :     SCALING_LUT_SIZE = 256,
     241                 :            : 
     242                 :            :     GRAIN_WIDTH = 82,
     243                 :            :     GRAIN_HEIGHT = 73,
     244                 :            :     // On the GPU we only need a subsection of this
     245                 :            :     GRAIN_WIDTH_LUT = 64,
     246                 :            :     GRAIN_HEIGHT_LUT = 64,
     247                 :            :     GRAIN_PAD_LUT = 9,
     248                 :            : 
     249                 :            :     // For subsampled grain textures
     250                 :            :     SUB_GRAIN_WIDTH = 44,
     251                 :            :     SUB_GRAIN_HEIGHT = 38,
     252                 :            :     SUB_GRAIN_WIDTH_LUT = GRAIN_WIDTH_LUT >> 1,
     253                 :            :     SUB_GRAIN_HEIGHT_LUT = GRAIN_HEIGHT_LUT >> 1,
     254                 :            :     SUB_GRAIN_PAD_LUT = 6,
     255                 :            : };
     256                 :            : 
     257                 :            : // Contains the shift by which the offsets are indexed
     258                 :            : enum offset {
     259                 :            :     OFFSET_TL = 24,
     260                 :            :     OFFSET_T  = 16,
     261                 :            :     OFFSET_L  = 8,
     262                 :            :     OFFSET_N  = 0,
     263                 :            : };
     264                 :            : 
     265                 :            : // Helper function to compute some common constants
     266                 :            : struct grain_scale {
     267                 :            :     int grain_center;
     268                 :            :     int grain_min;
     269                 :            :     int grain_max;
     270                 :            :     float texture_scale;
     271                 :            :     float grain_scale;
     272                 :            : };
     273                 :            : 
     274                 :         40 : static inline int bit_depth(const struct pl_color_repr *repr)
     275                 :            : {
     276   [ +  +  -  + ]:         40 :     int depth = PL_DEF(repr->bits.color_depth,
     277                 :            :                 PL_DEF(repr->bits.sample_depth, 8));
     278         [ -  + ]:         28 :     pl_assert(depth >= 8);
     279                 :         40 :     return PL_MIN(depth, 12);
     280                 :            : }
     281                 :            : 
     282                 :         24 : static struct grain_scale get_grain_scale(const struct pl_film_grain_params *params)
     283                 :            : {
     284                 :         24 :     int bits = bit_depth(params->repr);
     285                 :            :     struct grain_scale ret = {
     286                 :         24 :         .grain_center = 128 << (bits - 8),
     287                 :            :     };
     288                 :            : 
     289                 :         24 :     ret.grain_min = -ret.grain_center;
     290                 :         24 :     ret.grain_max = (256 << (bits - 8)) - 1 - ret.grain_center;
     291                 :            : 
     292                 :         24 :     struct pl_color_repr repr = *params->repr;
     293                 :         24 :     ret.texture_scale = pl_color_repr_normalize(&repr);
     294                 :            : 
     295                 :            :     // Since our color samples are normalized to the range [0, 1], we need to
     296                 :            :     // scale down grain values from the scale [0, 2^b - 1] to this range.
     297                 :         24 :     ret.grain_scale = 1.0 / ((1 << bits) - 1);
     298                 :            : 
     299                 :         24 :     return ret;
     300                 :            : }
     301                 :            : 
     302                 :            : // Generates the basic grain table (LumaGrain in the spec).
     303                 :          8 : static void generate_grain_y(float out[GRAIN_HEIGHT_LUT][GRAIN_WIDTH_LUT],
     304                 :            :                              int16_t buf[GRAIN_HEIGHT][GRAIN_WIDTH],
     305                 :            :                              const struct pl_film_grain_params *params)
     306                 :            : {
     307                 :            :     const struct pl_av1_grain_data *data = &params->data.params.av1;
     308                 :          8 :     struct grain_scale scale = get_grain_scale(params);
     309                 :          8 :     uint16_t seed = (uint16_t) params->data.seed;
     310                 :          8 :     int bits = bit_depth(params->repr);
     311                 :          8 :     int shift = 12 - bits + data->grain_scale_shift;
     312         [ -  + ]:          8 :     pl_assert(shift >= 0);
     313                 :            : 
     314         [ +  + ]:        592 :     for (int y = 0; y < GRAIN_HEIGHT; y++) {
     315         [ +  + ]:      48472 :         for (int x = 0; x < GRAIN_WIDTH; x++) {
     316                 :      47888 :             int16_t value = gaussian_sequence[ get_random_number(11, &seed) ];
     317         [ +  - ]:      95776 :             buf[y][x] = round2(value, shift);
     318                 :            :         }
     319                 :            :     }
     320                 :            : 
     321                 :            :     const int ar_pad = 3;
     322                 :          8 :     int ar_lag = data->ar_coeff_lag;
     323                 :            : 
     324         [ +  + ]:        568 :     for (int y = ar_pad; y < GRAIN_HEIGHT; y++) {
     325         [ +  + ]:      43120 :         for (int x = ar_pad; x < GRAIN_WIDTH - ar_pad; x++) {
     326                 :      42560 :             const int8_t *coeff = data->ar_coeffs_y;
     327                 :            :             int sum = 0;
     328         [ +  + ]:     212800 :             for (int dy = -ar_lag; dy <= 0; dy++) {
     329         [ +  + ]:    1191680 :                 for (int dx = -ar_lag; dx <= ar_lag; dx++) {
     330         [ +  + ]:    1064000 :                     if (!dx && !dy)
     331                 :            :                         break;
     332                 :    1021440 :                     sum += *(coeff++) * buf[y + dy][x + dx];
     333                 :            :                 }
     334                 :            :             }
     335                 :            : 
     336         [ +  - ]:      42560 :             int16_t grain = buf[y][x] + round2(sum, data->ar_coeff_shift);
     337         [ +  + ]:      42560 :             grain = PL_CLAMP(grain, scale.grain_min, scale.grain_max);
     338                 :      42560 :             buf[y][x] = grain;
     339                 :            :         }
     340                 :            :     }
     341                 :            : 
     342         [ +  + ]:        520 :     for (int y = 0; y < GRAIN_HEIGHT_LUT; y++) {
     343         [ +  + ]:      33280 :         for (int x = 0; x < GRAIN_WIDTH_LUT; x++) {
     344                 :      32768 :             int16_t grain = buf[y + GRAIN_PAD_LUT][x + GRAIN_PAD_LUT];
     345                 :      32768 :             out[y][x] = grain * scale.grain_scale;
     346                 :            :         }
     347                 :            :     }
     348                 :          8 : }
     349                 :            : 
     350                 :          8 : static void generate_grain_uv(float *out, int16_t buf[GRAIN_HEIGHT][GRAIN_WIDTH],
     351                 :            :                               const int16_t buf_y[GRAIN_HEIGHT][GRAIN_WIDTH],
     352                 :            :                               enum pl_channel channel, int sub_x, int sub_y,
     353                 :            :                               const struct pl_film_grain_params *params)
     354                 :            : {
     355                 :            :     const struct pl_av1_grain_data *data = &params->data.params.av1;
     356                 :          8 :     struct grain_scale scale = get_grain_scale(params);
     357                 :          8 :     int bits = bit_depth(params->repr);
     358                 :          8 :     int shift = 12 - bits + data->grain_scale_shift;
     359         [ -  + ]:          8 :     pl_assert(shift >= 0);
     360                 :            : 
     361                 :          8 :     uint16_t seed = params->data.seed;
     362         [ +  + ]:          8 :     if (channel == PL_CHANNEL_CB) {
     363                 :          4 :         seed ^= 0xb524;
     364         [ +  - ]:          4 :     } else if (channel == PL_CHANNEL_CR) {
     365                 :          4 :         seed ^= 0x49d8;
     366                 :            :     }
     367                 :            : 
     368         [ +  - ]:          8 :     int chromaW = sub_x ? SUB_GRAIN_WIDTH  : GRAIN_WIDTH;
     369         [ +  - ]:          8 :     int chromaH = sub_y ? SUB_GRAIN_HEIGHT : GRAIN_HEIGHT;
     370                 :            : 
     371                 :          8 :     const int8_t *coeffs[] = {
     372                 :          8 :         [PL_CHANNEL_CB] = data->ar_coeffs_uv[0],
     373                 :          8 :         [PL_CHANNEL_CR] = data->ar_coeffs_uv[1],
     374                 :            :     };
     375                 :            : 
     376         [ +  + ]:        592 :     for (int y = 0; y < chromaH; y++) {
     377         [ +  + ]:      48472 :         for (int x = 0; x < chromaW; x++) {
     378                 :      47888 :             int16_t value = gaussian_sequence[ get_random_number(11, &seed) ];
     379         [ +  - ]:      95776 :             buf[y][x] = round2(value, shift);
     380                 :            :         }
     381                 :            :     }
     382                 :            : 
     383                 :            :     const int ar_pad = 3;
     384                 :          8 :     int ar_lag = data->ar_coeff_lag;
     385                 :            : 
     386         [ +  + ]:        568 :     for (int y = ar_pad; y < chromaH; y++) {
     387         [ +  + ]:      43120 :         for (int x = ar_pad; x < chromaW - ar_pad; x++) {
     388                 :      42560 :             const int8_t *coeff = coeffs[channel];
     389         [ -  + ]:      42560 :             pl_assert(coeff);
     390                 :            :             int sum = 0;
     391         [ +  + ]:     212800 :             for (int dy = -ar_lag; dy <= 0; dy++) {
     392         [ +  + ]:    1191680 :                 for (int dx = -ar_lag; dx <= ar_lag; dx++) {
     393                 :            :                     // For the final (current) pixel, we need to add in the
     394                 :            :                     // contribution from the luma grain texture
     395         [ +  + ]:    1064000 :                     if (!dx && !dy) {
     396         [ +  - ]:      42560 :                         if (!data->num_points_y)
     397                 :            :                             break;
     398                 :            :                         int luma = 0;
     399                 :      42560 :                         int lumaX = ((x - ar_pad) << sub_x) + ar_pad;
     400                 :      42560 :                         int lumaY = ((y - ar_pad) << sub_y) + ar_pad;
     401         [ +  + ]:      85120 :                         for (int i = 0; i <= sub_y; i++) {
     402         [ +  + ]:      85120 :                             for (int j = 0; j <= sub_x; j++) {
     403                 :      42560 :                                 luma += buf_y[lumaY + i][lumaX + j];
     404                 :            :                             }
     405                 :            :                         }
     406         [ -  + ]:      42560 :                         luma = round2(luma, sub_x + sub_y);
     407                 :      42560 :                         sum += luma * (*coeff);
     408                 :      42560 :                         break;
     409                 :            :                     }
     410                 :            : 
     411                 :    1021440 :                     sum += *(coeff++) * buf[y + dy][x + dx];
     412                 :            :                 }
     413                 :            :             }
     414                 :            : 
     415         [ +  - ]:      42560 :             int16_t grain = buf[y][x] + round2(sum, data->ar_coeff_shift);
     416         [ +  + ]:      42560 :             grain = PL_CLAMP(grain, scale.grain_min, scale.grain_max);
     417                 :      42560 :             buf[y][x] = grain;
     418                 :            :         }
     419                 :            :     }
     420                 :            : 
     421                 :          8 :     int lutW = GRAIN_WIDTH_LUT >> sub_x;
     422                 :          8 :     int lutH = GRAIN_HEIGHT_LUT >> sub_y;
     423         [ +  - ]:          8 :     int padX = sub_x ? SUB_GRAIN_PAD_LUT : GRAIN_PAD_LUT;
     424         [ +  - ]:          8 :     int padY = sub_y ? SUB_GRAIN_PAD_LUT : GRAIN_PAD_LUT;
     425                 :            : 
     426         [ +  + ]:        520 :     for (int y = 0; y < lutH; y++) {
     427         [ +  + ]:      33280 :         for (int x = 0; x < lutW; x++) {
     428                 :      32768 :             int16_t grain = buf[y + padY][x + padX];
     429                 :      32768 :             out[y * lutW + x] = grain * scale.grain_scale;
     430                 :            :         }
     431                 :            :     }
     432                 :          8 : }
     433                 :            : 
     434                 :          8 : static void generate_offsets(void *pbuf, const struct sh_lut_params *params)
     435                 :            : {
     436                 :          8 :     const struct pl_film_grain_data *data = params->priv;
     437                 :            :     unsigned int *buf = pbuf;
     438                 :            :     pl_static_assert(sizeof(unsigned int) >= sizeof(uint32_t));
     439                 :            : 
     440         [ +  + ]:         40 :     for (int y = 0; y < params->height; y++) {
     441                 :         32 :         uint16_t state = data->seed;
     442                 :         32 :         state ^= ((y * 37 + 178) & 0xFF) << 8;
     443                 :         32 :         state ^= ((y * 173 + 105) & 0xFF);
     444                 :            : 
     445         [ +  + ]:        160 :         for (int x = 0; x < params->width; x++) {
     446                 :        128 :             unsigned int *offsets = &buf[y * params->width + x];
     447                 :            : 
     448                 :            :             uint8_t val = get_random_number(8, &state);
     449         [ +  + ]:        128 :             uint8_t val_l = x ? (offsets - 1)[0] : 0;
     450         [ +  + ]:        128 :             uint8_t val_t = y ? (offsets - params->width)[0] : 0;
     451         [ +  + ]:        128 :             uint8_t val_tl = x && y ? (offsets - params->width - 1)[0] : 0;
     452                 :            : 
     453                 :            :             // Encode four offsets into a single 32-bit integer for the
     454                 :            :             // convenience of the GPU. That way only one LUT fetch is
     455                 :            :             // required for the entire block.
     456                 :        128 :             *offsets = ((uint32_t) val_tl << OFFSET_TL)
     457                 :        128 :                      | ((uint32_t) val_t  << OFFSET_T)
     458                 :        128 :                      | ((uint32_t) val_l  << OFFSET_L)
     459                 :        128 :                      | ((uint32_t) val    << OFFSET_N);
     460                 :            :         }
     461                 :            :     }
     462                 :          8 : }
     463                 :            : 
     464                 :         10 : static void generate_scaling(void *pdata, const struct sh_lut_params *params)
     465                 :            : {
     466   [ +  -  -  + ]:         10 :     assert(params->width == SCALING_LUT_SIZE && params->comps == 1);
     467                 :            :     float *data = pdata;
     468                 :            : 
     469                 :            :     struct {
     470                 :            :         int num;
     471                 :            :         uint8_t (*points)[2];
     472                 :            :         const struct pl_av1_grain_data *data;
     473                 :         10 :     } *ctx = params->priv;
     474                 :            : 
     475                 :         10 :     float range = 1 << ctx->data->scaling_shift;
     476                 :            : 
     477                 :            :     // Fill up the preceding entries with the initial value
     478         [ -  + ]:         10 :     for (int i = 0; i < ctx->points[0][0]; i++)
     479                 :          0 :         data[i] = ctx->points[0][1] / range;
     480                 :            : 
     481                 :            :     // Linearly interpolate the values in the middle
     482         [ +  + ]:         44 :     for (int i = 0; i < ctx->num - 1; i++) {
     483                 :         34 :         int bx = ctx->points[i][0];
     484                 :         34 :         int by = ctx->points[i][1];
     485                 :         34 :         int dx = ctx->points[i + 1][0] - bx;
     486                 :         34 :         int dy = ctx->points[i + 1][1] - by;
     487                 :         34 :         int delta = dy * ((0x10000 + (dx >> 1)) / dx);
     488         [ +  + ]:       2584 :         for (int x = 0; x < dx; x++) {
     489                 :       2550 :             int v = by + ((x * delta + 0x8000) >> 16);
     490                 :       2550 :             data[bx + x] = v / range;
     491                 :            :         }
     492                 :            :     }
     493                 :            : 
     494                 :            :     // Fill up the remaining entries with the final value
     495         [ +  + ]:         20 :     for (int i = ctx->points[ctx->num - 1][0]; i < SCALING_LUT_SIZE; i++)
     496                 :         10 :         data[i] = ctx->points[ctx->num - 1][1] / range;
     497                 :         10 : }
     498                 :            : 
     499                 :         40 : static void sample(pl_shader sh, enum offset off, ident_t lut, int idx,
     500                 :            :                    int sub_x, int sub_y)
     501                 :            : {
     502                 :         40 :     int dx = (off & OFFSET_L) ? 1 : 0,
     503                 :         40 :         dy = (off & OFFSET_T) ? 1 : 0;
     504                 :            : 
     505                 :            :     static const char *index_strs[] = {
     506                 :            :         [0] = ".x",
     507                 :            :         [1] = ".y",
     508                 :            :     };
     509                 :            : 
     510   [ +  +  +  -  :        120 :     GLSL("offset = uvec2(%du, %du) * uvec2((data >> %d) & 0xFu, \n"
                   +  - ]
     511                 :            :          "                                 (data >> %d) & 0xFu);\n"
     512                 :            :          "pos = offset + local_id.xy + uvec2(%d, %d);           \n"
     513                 :            :          "val = "$"(pos)%s;                                     \n",
     514                 :            :          sub_x ? 1 : 2, sub_y ? 1 : 2, off + 4, off,
     515                 :            :          (BLOCK_SIZE >> sub_x) * dx,
     516                 :            :          (BLOCK_SIZE >> sub_y) * dy,
     517                 :            :          lut, idx >= 0 ? index_strs[idx] : "");
     518                 :         40 : }
     519                 :            : 
     520                 :            : struct grain_obj_av1 {
     521                 :            :     // LUT objects for the offsets, grain and scaling luts
     522                 :            :     pl_shader_obj lut_offsets;
     523                 :            :     pl_shader_obj lut_grain[2];
     524                 :            :     pl_shader_obj lut_scaling[3];
     525                 :            : 
     526                 :            :     // Previous parameters used to check reusability
     527                 :            :     struct pl_film_grain_data data;
     528                 :            :     struct pl_color_repr repr;
     529                 :            :     bool fg_has_y;
     530                 :            :     bool fg_has_u;
     531                 :            :     bool fg_has_v;
     532                 :            : 
     533                 :            :     // Space to store the temporary arrays, reused
     534                 :            :     uint32_t *offsets;
     535                 :            :     float grain[2][GRAIN_HEIGHT_LUT][GRAIN_WIDTH_LUT];
     536                 :            :     int16_t grain_tmp_y[GRAIN_HEIGHT][GRAIN_WIDTH];
     537                 :            :     int16_t grain_tmp_uv[GRAIN_HEIGHT][GRAIN_WIDTH];
     538                 :            : };
     539                 :            : 
     540                 :          6 : static void av1_grain_uninit(pl_gpu gpu, void *ptr)
     541                 :            : {
     542                 :            :     struct grain_obj_av1 *obj = ptr;
     543                 :          6 :     pl_shader_obj_destroy(&obj->lut_offsets);
     544         [ +  + ]:         18 :     for (int i = 0; i < PL_ARRAY_SIZE(obj->lut_grain); i++)
     545                 :         12 :         pl_shader_obj_destroy(&obj->lut_grain[i]);
     546         [ +  + ]:         24 :     for (int i = 0; i < PL_ARRAY_SIZE(obj->lut_scaling); i++)
     547                 :         18 :         pl_shader_obj_destroy(&obj->lut_scaling[i]);
     548                 :          6 :     *obj = (struct grain_obj_av1) {0};
     549                 :          6 : }
     550                 :            : 
     551                 :         16 : bool pl_needs_fg_av1(const struct pl_film_grain_params *params)
     552                 :            : {
     553                 :            :     const struct pl_av1_grain_data *data = &params->data.params.av1;
     554                 :         16 :     bool has_y = data->num_points_y > 0;
     555   [ -  +  -  - ]:         16 :     bool has_u = data->num_points_uv[0] > 0 || data->chroma_scaling_from_luma;
     556   [ -  +  -  - ]:         16 :     bool has_v = data->num_points_uv[1] > 0 || data->chroma_scaling_from_luma;
     557                 :            : 
     558         [ +  - ]:         16 :     for (int i = 0; i < 3; i++) {
     559                 :         16 :         enum pl_channel channel = channel_map(i, params);
     560         [ -  + ]:         16 :         if (channel == PL_CHANNEL_Y && has_y)
     561                 :            :             return true;
     562         [ #  # ]:          0 :         if (channel == PL_CHANNEL_CB && has_u)
     563                 :            :             return true;
     564         [ #  # ]:          0 :         if (channel == PL_CHANNEL_CR && has_v)
     565                 :            :             return true;
     566                 :            :     }
     567                 :            : 
     568                 :            :     return false;
     569                 :            : }
     570                 :            : 
     571                 :          8 : static inline bool av1_grain_data_eq(const struct pl_film_grain_data *da,
     572                 :            :                                      const struct pl_film_grain_data *db)
     573                 :            : {
     574                 :            :     const struct pl_av1_grain_data *a = &da->params.av1, *b = &db->params.av1;
     575                 :            : 
     576                 :            :     // Only check the fields that are relevant for grain LUT generation
     577                 :         12 :     return da->seed == db->seed &&
     578         [ +  - ]:          4 :            a->chroma_scaling_from_luma == b->chroma_scaling_from_luma &&
     579         [ -  + ]:          4 :            a->scaling_shift == b->scaling_shift &&
     580         [ #  # ]:          0 :            a->ar_coeff_lag == b->ar_coeff_lag &&
     581         [ #  # ]:          0 :            a->ar_coeff_shift == b->ar_coeff_shift &&
     582         [ #  # ]:          0 :            a->grain_scale_shift == b->grain_scale_shift &&
     583   [ +  +  -  - ]:          8 :            !memcmp(a->ar_coeffs_y, b->ar_coeffs_y, sizeof(a->ar_coeffs_y)) &&
     584         [ #  # ]:          0 :            !memcmp(a->ar_coeffs_uv, b->ar_coeffs_uv, sizeof(a->ar_coeffs_uv));
     585                 :            : }
     586                 :            : 
     587                 :         12 : static void fill_grain_lut(void *data, const struct sh_lut_params *params)
     588                 :            : {
     589                 :         12 :     struct grain_obj_av1 *obj = params->priv;
     590                 :         12 :     size_t entries = params->width * params->height * params->comps;
     591                 :         12 :     memcpy(data, obj->grain, entries * sizeof(float));
     592                 :         12 : }
     593                 :            : 
     594                 :          8 : bool pl_shader_fg_av1(pl_shader sh, pl_shader_obj *grain_state,
     595                 :            :                       const struct pl_film_grain_params *params)
     596                 :            : {
     597                 :            :     int sub_x = 0, sub_y = 0;
     598                 :          8 :     int tex_w = params->tex->params.w,
     599                 :          8 :         tex_h = params->tex->params.h;
     600                 :            : 
     601         [ +  + ]:          8 :     if (params->luma_tex) {
     602                 :          4 :         sub_x = params->luma_tex->params.w > tex_w;
     603                 :          4 :         sub_y = params->luma_tex->params.h > tex_h;
     604                 :            :     }
     605                 :            : 
     606                 :          8 :     const struct pl_av1_grain_data *data = &params->data.params.av1;
     607                 :          8 :     bool fg_has_y = data->num_points_y > 0;
     608   [ -  +  -  - ]:          8 :     bool fg_has_u = data->num_points_uv[0] > 0 || data->chroma_scaling_from_luma;
     609   [ -  +  -  - ]:          8 :     bool fg_has_v = data->num_points_uv[1] > 0 || data->chroma_scaling_from_luma;
     610                 :            : 
     611                 :            :     bool tex_is_y = false, tex_is_cb = false, tex_is_cr = false;
     612         [ +  + ]:         32 :     for (int i = 0; i < 3; i++) {
     613   [ +  +  +  + ]:         24 :         switch (channel_map(i, params)) {
     614                 :          8 :         case PL_CHANNEL_Y:  tex_is_y = true; break;
     615                 :          4 :         case PL_CHANNEL_CB: tex_is_cb = true; break;
     616                 :          4 :         case PL_CHANNEL_CR: tex_is_cr = true; break;
     617                 :            :         default: break;
     618                 :            :         };
     619                 :            :     }
     620                 :            : 
     621   [ +  -  -  + ]:          8 :     if (tex_is_y && (sub_x || sub_y)) {
     622                 :          0 :         PL_WARN(sh, "pl_film_grain_params.channels includes PL_CHANNEL_Y but "
     623                 :            :                 "plane is subsampled, this makes no sense. Continuing anyway "
     624                 :            :                 "but output is likely incorrect.");
     625                 :            :     }
     626                 :            : 
     627         [ -  + ]:          8 :     if (!sh_require(sh, PL_SHADER_SIG_NONE, tex_w, tex_h))
     628                 :            :         return false;
     629                 :            : 
     630                 :          8 :     pl_gpu gpu = SH_GPU(sh);
     631         [ -  + ]:          8 :     if (!gpu) {
     632                 :          0 :         PL_ERR(sh, "AV1 film grain synthesis requires a non-NULL pl_gpu!");
     633                 :          0 :         return false;
     634                 :            :     }
     635                 :            : 
     636                 :            :     // Disable generation for unneeded component types
     637                 :            :     fg_has_y &= tex_is_y;
     638                 :          8 :     fg_has_u &= tex_is_cb;
     639                 :          8 :     fg_has_v &= tex_is_cr;
     640                 :            : 
     641                 :          8 :     int bw = BLOCK_SIZE >> sub_x;
     642                 :          8 :     int bh = BLOCK_SIZE >> sub_y;
     643                 :          8 :     bool is_compute = sh_try_compute(sh, bw, bh, false, sizeof(uint32_t));
     644                 :            : 
     645                 :            :     struct grain_obj_av1 *obj;
     646                 :          8 :     obj = SH_OBJ(sh, grain_state, PL_SHADER_OBJ_AV1_GRAIN,
     647                 :            :                  struct grain_obj_av1, av1_grain_uninit);
     648         [ -  + ]:          8 :     if (!obj)
     649                 :            :         return false;
     650                 :            : 
     651                 :            :     // Note: In theory we could check only the parameters related to luma or
     652                 :            :     // only related to chroma and skip updating for changes to irrelevant
     653                 :            :     // parts, but this is probably not worth it since the seed is expected to
     654                 :            :     // change per frame anyway.
     655         [ #  # ]:          0 :     bool needs_update = !av1_grain_data_eq(&params->data, &obj->data) ||
     656                 :          0 :                         !pl_color_repr_equal(params->repr, &obj->repr) ||
     657         [ #  # ]:          0 :                         fg_has_y != obj->fg_has_y ||
     658   [ -  +  -  - ]:          8 :                         fg_has_u != obj->fg_has_u ||
     659         [ #  # ]:          0 :                         fg_has_v != obj->fg_has_v;
     660                 :            : 
     661                 :            :     if (needs_update) {
     662                 :            :         // This is needed even for chroma, so statically generate it
     663                 :          8 :         generate_grain_y(obj->grain[0], obj->grain_tmp_y, params);
     664                 :            :     }
     665                 :            : 
     666                 :            :     ident_t lut[3];
     667                 :          8 :     int idx[3] = {-1};
     668                 :            : 
     669         [ +  - ]:          8 :     if (fg_has_y) {
     670                 :          8 :         lut[0] = sh_lut(sh, sh_lut_params(
     671                 :            :             .object     = &obj->lut_grain[0],
     672                 :            :             .var_type   = PL_VAR_FLOAT,
     673                 :            :             .lut_type   = SH_LUT_TEXTURE,
     674                 :            :             .width      = GRAIN_WIDTH_LUT,
     675                 :            :             .height     = GRAIN_HEIGHT_LUT,
     676                 :            :             .comps      = 1,
     677                 :            :             .update     = needs_update,
     678                 :            :             .dynamic    = true,
     679                 :            :             .fill       = fill_grain_lut,
     680                 :            :             .priv       = obj,
     681                 :            :         ));
     682                 :            : 
     683         [ -  + ]:          8 :         if (!lut[0]) {
     684                 :          0 :             SH_FAIL(sh, "Failed generating/uploading luma grain LUT!");
     685                 :          0 :             return false;
     686                 :            :         }
     687                 :            :     }
     688                 :            : 
     689                 :            :     // Try merging the chroma LUTs into a single texture
     690                 :            :     int chroma_comps = 0;
     691         [ +  + ]:          8 :     if (fg_has_u) {
     692                 :          4 :         generate_grain_uv(&obj->grain[chroma_comps][0][0], obj->grain_tmp_uv,
     693                 :          4 :                           obj->grain_tmp_y, PL_CHANNEL_CB, sub_x, sub_y,
     694                 :            :                           params);
     695                 :            :         idx[1] = chroma_comps++;
     696                 :            :     }
     697         [ +  + ]:          8 :     if (fg_has_v) {
     698                 :          4 :         generate_grain_uv(&obj->grain[chroma_comps][0][0], obj->grain_tmp_uv,
     699                 :          4 :                           obj->grain_tmp_y, PL_CHANNEL_CR, sub_x, sub_y,
     700                 :            :                           params);
     701                 :          4 :         idx[2] = chroma_comps++;
     702                 :            :     }
     703                 :            : 
     704         [ -  + ]:          4 :     if (chroma_comps > 0) {
     705                 :          4 :         lut[1] = lut[2] = sh_lut(sh, sh_lut_params(
     706                 :            :             .object     = &obj->lut_grain[1],
     707                 :            :             .var_type   = PL_VAR_FLOAT,
     708                 :            :             .lut_type   = SH_LUT_TEXTURE,
     709                 :            :             .width      = GRAIN_WIDTH_LUT >> sub_x,
     710                 :            :             .height     = GRAIN_HEIGHT_LUT >> sub_y,
     711                 :            :             .comps      = chroma_comps,
     712                 :            :             .update     = needs_update,
     713                 :            :             .dynamic    = true,
     714                 :            :             .fill       = fill_grain_lut,
     715                 :            :             .priv       = obj,
     716                 :            :         ));
     717                 :            : 
     718         [ -  + ]:          4 :         if (!lut[1]) {
     719                 :          0 :             SH_FAIL(sh, "Failed generating/uploading chroma grain LUT!");
     720                 :          0 :             return false;
     721                 :            :         }
     722                 :            : 
     723         [ -  + ]:          4 :         if (chroma_comps == 1)
     724                 :          0 :             idx[1] = idx[2] = -1;
     725                 :            :     }
     726                 :            : 
     727                 :          8 :     ident_t offsets = sh_lut(sh, sh_lut_params(
     728                 :            :         .object     = &obj->lut_offsets,
     729                 :            :         .var_type   = PL_VAR_UINT,
     730                 :            :         .lut_type   = SH_LUT_AUTO,
     731                 :            :         .width      = PL_ALIGN2(tex_w << sub_x, 128) / 32,
     732                 :            :         .height     = PL_ALIGN2(tex_h << sub_y, 128) / 32,
     733                 :            :         .comps      = 1,
     734                 :            :         .update     = needs_update,
     735                 :            :         .dynamic    = true,
     736                 :            :         .fill       = generate_offsets,
     737                 :            :         .priv       = (void *) &params->data,
     738                 :            :     ));
     739                 :            : 
     740         [ -  + ]:          8 :     if (!offsets) {
     741                 :          0 :         SH_FAIL(sh, "Failed generating/uploading block offsets LUT!");
     742                 :          0 :         return false;
     743                 :            :     }
     744                 :            : 
     745                 :            :     // For the scaling LUTs, we assume they'll be relatively constant
     746                 :            :     // throughout the video so doing some extra work to avoid reinitializing
     747                 :            :     // them constantly is probably worth it. Probably.
     748                 :            :     const struct pl_av1_grain_data *obj_data = &obj->data.params.av1;
     749                 :            :     bool scaling_changed = false;
     750   [ -  +  -  - ]:          8 :     if (fg_has_y || data->chroma_scaling_from_luma) {
     751                 :          8 :         scaling_changed |= data->num_points_y != obj_data->num_points_y;
     752                 :          8 :         scaling_changed |= memcmp(data->points_y, obj_data->points_y,
     753                 :            :                                   sizeof(data->points_y));
     754                 :            :     }
     755                 :            : 
     756   [ +  +  +  - ]:          8 :     if (fg_has_u && !data->chroma_scaling_from_luma) {
     757                 :          4 :         scaling_changed |= data->num_points_uv[0] != obj_data->num_points_uv[0];
     758                 :          4 :         scaling_changed |= memcmp(data->points_uv[0],
     759                 :          4 :                                   obj_data->points_uv[0],
     760                 :            :                                   sizeof(data->points_uv[0]));
     761                 :            :     }
     762                 :            : 
     763   [ +  +  +  - ]:          8 :     if (fg_has_v && !data->chroma_scaling_from_luma) {
     764                 :          4 :         scaling_changed |= data->num_points_uv[1] != obj_data->num_points_uv[1];
     765                 :          4 :         scaling_changed |= memcmp(data->points_uv[1],
     766                 :          4 :                                   obj_data->points_uv[1],
     767                 :            :                                   sizeof(data->points_uv[1]));
     768                 :            :     }
     769                 :            : 
     770                 :          8 :     ident_t scaling[3] = {0};
     771         [ +  + ]:         32 :     for (int i = 0; i < 3; i++) {
     772                 :            :         struct {
     773                 :            :             int num;
     774                 :            :             const uint8_t (*points)[2];
     775                 :            :             const struct pl_av1_grain_data *data;
     776                 :            :         } priv;
     777                 :            : 
     778                 :         24 :         priv.data = data;
     779   [ +  +  -  + ]:         24 :         if (i == 0 || data->chroma_scaling_from_luma) {
     780                 :          8 :             priv.num = data->num_points_y;
     781                 :          8 :             priv.points = &data->points_y[0];
     782                 :            :         } else {
     783                 :         16 :             priv.num = data->num_points_uv[i - 1];
     784                 :         16 :             priv.points = &data->points_uv[i - 1][0];
     785                 :            :         }
     786                 :            : 
     787                 :            :         // Skip scaling for unneeded channels
     788                 :         24 :         bool has_c[3] = { fg_has_y, fg_has_u, fg_has_v };
     789   [ +  +  +  - ]:         24 :         if (has_c[i] && priv.num > 0) {
     790                 :         16 :             scaling[i] = sh_lut(sh, sh_lut_params(
     791                 :            :                 .object     = &obj->lut_scaling[i],
     792                 :            :                 .var_type   = PL_VAR_FLOAT,
     793                 :            :                 .method     = SH_LUT_LINEAR,
     794                 :            :                 .width      = SCALING_LUT_SIZE,
     795                 :            :                 .comps      = 1,
     796                 :            :                 .update     = scaling_changed,
     797                 :            :                 .dynamic    = true,
     798                 :            :                 .fill       = generate_scaling,
     799                 :            :                 .priv       = &priv,
     800                 :            :             ));
     801                 :            : 
     802         [ -  + ]:         16 :             if (!scaling[i]) {
     803                 :          0 :                 SH_FAIL(sh, "Failed generating/uploading scaling LUTs!");
     804                 :          0 :                 return false;
     805                 :            :             }
     806                 :            :         }
     807                 :            :     }
     808                 :            : 
     809                 :            :     // Done updating LUTs
     810                 :          8 :     obj->data = params->data;
     811                 :          8 :     obj->repr = *params->repr;
     812                 :          8 :     obj->fg_has_y = fg_has_y;
     813                 :          8 :     obj->fg_has_u = fg_has_u;
     814                 :          8 :     obj->fg_has_v = fg_has_v;
     815                 :            : 
     816                 :          8 :     sh_describe(sh, "AV1 film grain");
     817                 :          8 :     GLSL("vec4 color;                   \n"
     818                 :            :          "// pl_shader_film_grain (AV1) \n"
     819                 :            :          "{                             \n"
     820                 :            :          "uvec2 offset;                 \n"
     821                 :            :          "uvec2 pos;                    \n"
     822                 :            :          "float val;                    \n"
     823                 :            :          "float grain;                  \n");
     824                 :            : 
     825         [ +  + ]:          8 :     if (is_compute) {
     826                 :          6 :         GLSL("uvec2 block_id  = gl_WorkGroupID.xy;        \n"
     827                 :            :              "uvec2 local_id  = gl_LocalInvocationID.xy;  \n"
     828                 :            :              "uvec2 global_id = gl_GlobalInvocationID.xy; \n");
     829                 :            :     } else {
     830                 :          2 :         GLSL("uvec2 global_id = uvec2(gl_FragCoord);                  \n"
     831                 :            :              "uvec2 block_id  = global_id / uvec2(%d, %d);            \n"
     832                 :            :              "uvec2 local_id  = global_id - uvec2(%d, %d) * block_id; \n",
     833                 :            :              bw, bh, bw, bh);
     834                 :            :     }
     835                 :            : 
     836                 :            :     // Load the data vector which holds the offsets
     837         [ +  + ]:          8 :     if (is_compute) {
     838                 :          6 :         ident_t id = sh_fresh(sh, "data");
     839                 :          6 :         GLSLH("shared uint "$"; \n", id);
     840                 :          6 :         GLSL("if (gl_LocalInvocationIndex == 0u)    \n"
     841                 :            :              "    "$" = uint("$"(block_id));        \n"
     842                 :            :              "barrier();                            \n"
     843                 :            :              "uint data = "$";                      \n",
     844                 :            :              id, offsets, id);
     845                 :            :     } else {
     846                 :          2 :         GLSL("uint data = uint("$"(block_id)); \n", offsets);
     847                 :            :     }
     848                 :            : 
     849                 :          8 :     struct grain_scale scale = get_grain_scale(params);
     850                 :          8 :     pl_color_repr_normalize(params->repr);
     851         [ +  + ]:          8 :     int bits = PL_DEF(params->repr->bits.color_depth, 8);
     852         [ -  + ]:          4 :     pl_assert(bits >= 8);
     853                 :            : 
     854                 :            :     ident_t minValue, maxLuma, maxChroma;
     855         [ +  + ]:          8 :     if (pl_color_levels_guess(params->repr) == PL_COLOR_LEVELS_LIMITED) {
     856                 :          4 :         float out_scale = (1 << bits) / ((1 << bits) - 1.0);
     857                 :          4 :         minValue  = SH_FLOAT(16  / 256.0 * out_scale);
     858                 :          4 :         maxLuma   = SH_FLOAT(235 / 256.0 * out_scale);
     859                 :          4 :         maxChroma = SH_FLOAT(240 / 256.0 * out_scale);
     860         [ -  + ]:          4 :         if (!pl_color_system_is_ycbcr_like(params->repr->sys))
     861                 :            :             maxChroma = maxLuma;
     862                 :            :     } else {
     863                 :          4 :         minValue  = SH_FLOAT(0.0);
     864                 :          4 :         maxLuma   = SH_FLOAT(1.0);
     865                 :          4 :         maxChroma = SH_FLOAT(1.0);
     866                 :            :     }
     867                 :            : 
     868                 :            :     // Load the color value of the tex itself
     869                 :          8 :     ident_t tex = sh_desc(sh, (struct pl_shader_desc) {
     870                 :          8 :         .binding.object = params->tex,
     871                 :            :         .desc = (struct pl_desc) {
     872                 :            :             .name = "tex",
     873                 :            :             .type = PL_DESC_SAMPLED_TEX,
     874                 :            :         },
     875                 :            :     });
     876                 :            : 
     877                 :          8 :     ident_t tex_scale = SH_FLOAT(scale.texture_scale);
     878                 :          8 :     GLSL("color = vec4("$") * texelFetch("$", ivec2(global_id), 0); \n",
     879                 :            :          tex_scale, tex);
     880                 :            : 
     881                 :            :     // If we need access to the external luma plane, load it now
     882         [ +  + ]:          8 :     if (tex_is_cb || tex_is_cr) {
     883                 :          4 :         GLSL("float averageLuma; \n");
     884         [ +  - ]:          4 :         if (tex_is_y) {
     885                 :            :             // We already have the luma channel as part of the pre-sampled color
     886         [ +  - ]:          4 :             for (int i = 0; i < 3; i++) {
     887         [ +  - ]:          4 :                 if (channel_map(i, params) == PL_CHANNEL_Y) {
     888                 :          4 :                     GLSL("averageLuma = color["$"]; \n", SH_INT(i));
     889                 :          4 :                     break;
     890                 :            :                 }
     891                 :            :             }
     892                 :            :         } else {
     893                 :            :             // Luma channel not present in image, attach it separately
     894         [ #  # ]:          0 :             pl_assert(params->luma_tex);
     895                 :          0 :             ident_t luma = sh_desc(sh, (struct pl_shader_desc) {
     896                 :            :                 .binding.object = params->luma_tex,
     897                 :            :                 .desc = (struct pl_desc) {
     898                 :            :                     .name = "luma",
     899                 :            :                     .type = PL_DESC_SAMPLED_TEX,
     900                 :            :                 },
     901                 :            :             });
     902                 :            : 
     903                 :          0 :             GLSL("pos = global_id * uvec2(%du, %du);                    \n"
     904                 :            :                  "averageLuma = texelFetch("$", ivec2(pos), 0)["$"];    \n"
     905                 :            :                  "averageLuma *= "$";                                   \n",
     906                 :            :                  1 << sub_x, 1 << sub_y,
     907                 :            :                  luma, SH_INT(params->luma_comp),
     908                 :            :                  tex_scale);
     909                 :            :         }
     910                 :            :     }
     911                 :            : 
     912                 :          8 :     ident_t grain_min = SH_FLOAT(scale.grain_min * scale.grain_scale);
     913                 :          8 :     ident_t grain_max = SH_FLOAT(scale.grain_max * scale.grain_scale);
     914                 :            : 
     915         [ +  + ]:         24 :     for (int i = 0; i < params->components; i++) {
     916                 :         16 :         enum pl_channel c = channel_map(i, params);
     917         [ -  + ]:         16 :         if (c == PL_CHANNEL_NONE)
     918                 :          0 :             continue;
     919         [ -  + ]:         16 :         if (!scaling[c])
     920                 :          0 :             continue;
     921                 :            : 
     922                 :         16 :         sample(sh, OFFSET_N, lut[c], idx[c], sub_x, sub_y);
     923                 :         16 :         GLSL("grain = val; \n");
     924                 :            : 
     925         [ +  + ]:         16 :         if (data->overlap) {
     926                 :          6 :             const char *weights[] = { "vec2(27.0, 17.0)", "vec2(23.0, 22.0)" };
     927                 :            : 
     928                 :            :             // X-direction overlapping
     929                 :          6 :             GLSL("if (block_id.x > 0u && local_id.x < %du) {    \n"
     930                 :            :                  "vec2 w = %s / 32.0;                           \n"
     931                 :            :                  "if (local_id.x == 1u) w.xy = w.yx;            \n",
     932                 :            :                  2 >> sub_x, weights[sub_x]);
     933                 :          6 :             sample(sh, OFFSET_L, lut[c], idx[c], sub_x, sub_y);
     934                 :          6 :             GLSL("grain = dot(vec2(val, grain), w);             \n"
     935                 :            :                  "}                                             \n");
     936                 :            : 
     937                 :            :             // Y-direction overlapping
     938                 :          6 :             GLSL("if (block_id.y > 0u && local_id.y < %du) {    \n"
     939                 :            :                  "vec2 w = %s / 32.0;                           \n"
     940                 :            :                  "if (local_id.y == 1u) w.xy = w.yx;            \n",
     941                 :            :                  2 >> sub_y, weights[sub_y]);
     942                 :            : 
     943                 :            :             // We need to special-case the top left pixels since these need to
     944                 :            :             // pre-blend the top-left offset block before blending vertically
     945                 :          6 :             GLSL("    if (block_id.x > 0u && local_id.x < %du) {\n"
     946                 :            :                  "        vec2 w2 = %s / 32.0;                  \n"
     947                 :            :                  "        if (local_id.x == 1u) w2.xy = w2.yx;  \n",
     948                 :            :                  2 >> sub_x, weights[sub_x]);
     949                 :          6 :                           sample(sh, OFFSET_TL, lut[c], idx[c], sub_x, sub_y);
     950                 :          6 :             GLSL("        float tmp = val;                      \n");
     951                 :          6 :                           sample(sh, OFFSET_T, lut[c], idx[c], sub_x, sub_y);
     952                 :          6 :             GLSL("        val = dot(vec2(tmp, val), w2);        \n"
     953                 :            :                  "    } else {                                  \n");
     954                 :          6 :                           sample(sh, OFFSET_T, lut[c], idx[c], sub_x, sub_y);
     955                 :          6 :             GLSL("    }                                         \n"
     956                 :            :                  "grain = dot(vec2(val, grain), w);             \n"
     957                 :            :                  "}                                             \n");
     958                 :            : 
     959                 :            :             // Correctly clip the interpolated grain
     960                 :          6 :             GLSL("grain = clamp(grain, "$", "$"); \n", grain_min, grain_max);
     961                 :            :         }
     962                 :            : 
     963         [ +  + ]:         16 :         if (c == PL_CHANNEL_Y) {
     964                 :          8 :             GLSL("color[%d] += "$"(color[%d]) * grain;      \n"
     965                 :            :                  "color[%d] = clamp(color[%d], "$", "$");   \n",
     966                 :            :                  i, scaling[c], i,
     967                 :            :                  i, i, minValue, maxLuma);
     968                 :            :         } else {
     969                 :          8 :             GLSL("val = averageLuma; \n");
     970         [ +  - ]:          8 :             if (!data->chroma_scaling_from_luma) {
     971                 :            :                 // We need to load some extra variables for the mixing. Do this
     972                 :            :                 // using sh_var instead of hard-coding them to avoid shader
     973                 :            :                 // recompilation when these values change.
     974                 :         16 :                 ident_t mult = sh_var(sh, (struct pl_shader_var) {
     975                 :          8 :                     .var = pl_var_vec2("mult"),
     976                 :          8 :                     .data = &(float[2]){
     977                 :          8 :                         data->uv_mult_luma[c - 1] / 64.0,
     978                 :          8 :                         data->uv_mult[c - 1] / 64.0,
     979                 :            :                     },
     980                 :            :                 });
     981                 :            : 
     982                 :          8 :                 int c_offset = (unsigned) data->uv_offset[c - 1] << (bits - 8);
     983                 :         16 :                 ident_t offset = sh_var(sh, (struct pl_shader_var) {
     984                 :          8 :                     .var = pl_var_float("offset"),
     985                 :          8 :                     .data = &(float) { c_offset * scale.grain_scale },
     986                 :            :                 });
     987                 :            : 
     988                 :          8 :                 GLSL("val = dot(vec2(val, color[%d]), "$"); \n"
     989                 :            :                      "val += "$";                           \n",
     990                 :            :                      i, mult, offset);
     991                 :            :             }
     992                 :          8 :             GLSL("color[%d] += "$"(val) * grain;            \n"
     993                 :            :                  "color[%d] = clamp(color[%d], "$", "$");   \n",
     994                 :            :                  i, scaling[c],
     995                 :            :                  i, i, minValue, maxChroma);
     996                 :            :         }
     997                 :            :     }
     998                 :            : 
     999                 :          8 :     GLSL("} \n");
    1000                 :          8 :     return true;
    1001                 :            : }

Generated by: LCOV version 1.16