Skip to content

Instantly share code, notes, and snippets.

@sharpbai
Last active March 28, 2022 20:50
Show Gist options
  • Select an option

  • Save sharpbai/5710e4bd06c35ce144e2945f1a9a7bb8 to your computer and use it in GitHub Desktop.

Select an option

Save sharpbai/5710e4bd06c35ce144e2945f1a9a7bb8 to your computer and use it in GitHub Desktop.
A optimization for ffmpeg tonemap_opencl

A optimization for ffmpeg tonemap_opencl

Introduction

On my MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports), i5 four Cores, originally using tonemap_opencl the speed of converting hevc 10bit smpte2084 to bt2020-10 is about 28fps.

After optimization, the speed is about 43fps. Without tonemap the speed is 48fps.

The optimization is at early stage. The perpose for the optimization is to convert 4k hdr 10bit video to 4k sdr 8bit video at an acceptable speed and quality using macbookPro.

Installation

replace colorspace_common.cl for ffmpeg-4.3.1, then configure with --enable-opencl and compile.

Test

To 4k SDR bt2020

ffmpeg -hwaccel opencl -init_hw_device opencl=gpu:0.1 -filter_hw_device gpu -i HDR10-Life-Untouched.mp4 -sws_flags bitexact -filter_threads 1 -vf "format=p010,hwupload,tonemap_opencl=tonemap=hable:t=bt709:m=bt2020:p=bt2020:desat=0:format=nv12,hwdownload" -c:a copy -pix_fmt nv12 -c:v h264_videotoolbox -bsf:v filter_units=remove_types=6,h264_metadata=transfer_characteristics=14:colour_primaries=9:matrix_coefficients=9 -b:v 15M -y bar_opencl_sdr_bt2020.mp4

To 4k SDR bt709

ffmpeg -hwaccel opencl -init_hw_device opencl=gpu:0.1 -filter_hw_device gpu -i HDR10-Life-Untouched.mp4 -sws_flags bitexact -filter_threads 1 -vf "format=p010,hwupload,tonemap_opencl=tonemap=hable:t=bt709:m=bt709:p=bt709:desat=0:format=nv12,hwdownload" -c:a copy -pix_fmt nv12 -c:v h264_videotoolbox -bsf:v filter_units=remove_types=6,h264_metadata=transfer_characteristics=1:colour_primaries=1:matrix_coefficients=1 -b:v 15M -y bar_opencl_sdr_bt709.mp4

Sample file link: https://www.demolandia.net/downloads.html?id=887452455

/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define ST2084_MAX_LUMINANCE 10000.0f
#define REFERENCE_WHITE 100.0f
#if chroma_loc == 1
#define chroma_sample(a,b,c,d) (((a) + (c)) * 0.5f)
#elif chroma_loc == 3
#define chroma_sample(a,b,c,d) (a)
#elif chroma_loc == 4
#define chroma_sample(a,b,c,d) (((a) + (b)) * 0.5f)
#elif chroma_loc == 5
#define chroma_sample(a,b,c,d) (c)
#elif chroma_loc == 6
#define chroma_sample(a,b,c,d) (((c) + (d)) * 0.5f)
#else
#define chroma_sample(a,b,c,d) (((a) + (b) + (c) + (d)) * 0.25f)
#endif
constant const float ST2084_M1 = 0.1593017578125f;
constant const float ST2084_M2 = 78.84375f;
constant const float ST2084_C1 = 0.8359375f;
constant const float ST2084_C2 = 18.8515625f;
constant const float ST2084_C3 = 18.6875f;
constant const float eotf_st2084_table[1025] = {
0.000000, 0.000000, 0.000001, 0.000003, 0.000004, 0.000006, 0.000009, 0.000012, 0.000015, 0.000018, 0.000022, 0.000026, 0.000031, 0.000036, 0.000041, 0.000047, 0.000054, 0.000060, 0.000067, 0.000075, 0.000083, 0.000091, 0.000100, 0.000109, 0.000119, 0.000129, 0.000140, 0.000151, 0.000163, 0.000175, 0.000188, 0.000201, 0.000215, 0.000230, 0.000245, 0.000260, 0.000276, 0.000293, 0.000310, 0.000328, 0.000347, 0.000366, 0.000386, 0.000406, 0.000427, 0.000449, 0.000472, 0.000495, 0.000519, 0.000543, 0.000569, 0.000595, 0.000622, 0.000649, 0.000677, 0.000707, 0.000737, 0.000767, 0.000799, 0.000831, 0.000864, 0.000899, 0.000934, 0.000969, 0.001006, 0.001044, 0.001082, 0.001122, 0.001163, 0.001204, 0.001246, 0.001290, 0.001334, 0.001380, 0.001426, 0.001474, 0.001522, 0.001572, 0.001623, 0.001675, 0.001728, 0.001782, 0.001837, 0.001894, 0.001952, 0.002010, 0.002071, 0.002132, 0.002195, 0.002258, 0.002324, 0.002390, 0.002458, 0.002527, 0.002597, 0.002669, 0.002743, 0.002817, 0.002893, 0.002971, 0.003050, 0.003130, 0.003212, 0.003296, 0.003381, 0.003468, 0.003556, 0.003646, 0.003737, 0.003830, 0.003925, 0.004021, 0.004119, 0.004219, 0.004321, 0.004424, 0.004529, 0.004636, 0.004745, 0.004856, 0.004968, 0.005083, 0.005199, 0.005317, 0.005438, 0.005560, 0.005684, 0.005811, 0.005939, 0.006069, 0.006202, 0.006337, 0.006474, 0.006613, 0.006754, 0.006897, 0.007043, 0.007192, 0.007342, 0.007495, 0.007650, 0.007808, 0.007967, 0.008130, 0.008295, 0.008462, 0.008632, 0.008805, 0.008980, 0.009158, 0.009338, 0.009522, 0.009707, 0.009896, 0.010088, 0.010282, 0.010479, 0.010679, 0.010882, 0.011088, 0.011296, 0.011508, 0.011723, 0.011941, 0.012162, 0.012387, 0.012614, 0.012844, 0.013078, 0.013315, 0.013556, 0.013800, 0.014047, 0.014297, 0.014551, 0.014809, 0.015070, 0.015335, 0.015603, 0.015875, 0.016151, 0.016431, 0.016714, 0.017001, 0.017292, 0.017587, 0.017885, 0.018188, 0.018495, 0.018806, 0.019121, 0.019440, 0.019763, 0.020091, 0.020423, 0.020759, 0.021099, 0.021445, 0.021794, 0.022148, 0.022506, 0.022870, 0.023238, 0.023610, 0.023987, 0.024369, 0.024756, 0.025148, 0.025545, 0.025947, 0.026354, 0.026766, 0.027183, 0.027605, 0.028033, 0.028466, 0.028904, 0.029348, 0.029797, 0.030253, 0.030713, 0.031179, 0.031651, 0.032129, 0.032612, 0.033101, 0.033597, 0.034098, 0.034605, 0.035119, 0.035639, 0.036165, 0.036697, 0.037235, 0.037780, 0.038333, 0.038891, 0.039455, 0.040027, 0.040605, 0.041191, 0.041783, 0.042381, 0.042988, 0.043602, 0.044222, 0.044850, 0.045484, 0.046127, 0.046776, 0.047435, 0.048100, 0.048772, 0.049452, 0.050141, 0.050837, 0.051542, 0.052254, 0.052975, 0.053704, 0.054441, 0.055187, 0.055941, 0.056703, 0.057475, 0.058255, 0.059042, 0.059841, 0.060648, 0.061464, 0.062288, 0.063123, 0.063966, 0.064819, 0.065682, 0.066553, 0.067436, 0.068328, 0.069228, 0.070142, 0.071063, 0.071995, 0.072937, 0.073890, 0.074852, 0.075826, 0.076810, 0.077805, 0.078811, 0.079828, 0.080855, 0.081894, 0.082946, 0.084007, 0.085081, 0.086166, 0.087263, 0.088370, 0.089492, 0.090625, 0.091769, 0.092926, 0.094096, 0.095276, 0.096473, 0.097681, 0.098900, 0.100133, 0.101379, 0.102640, 0.103915, 0.105201, 0.106501, 0.107813, 0.109143, 0.110484, 0.111840, 0.113211, 0.114598, 0.115996, 0.117409, 0.118838, 0.120285, 0.121742, 0.123218, 0.124706, 0.126210, 0.127731, 0.129267, 0.130820, 0.132390, 0.133976, 0.135576, 0.137196, 0.138829, 0.140479, 0.142147, 0.143837, 0.145535, 0.147256, 0.148994, 0.150755, 0.152525, 0.154318, 0.156130, 0.157960, 0.159809, 0.161672, 0.163558, 0.165464, 0.167385, 0.169329, 0.171293, 0.173278, 0.175282, 0.177301, 0.179345, 0.181410, 0.183495, 0.185601, 0.187728, 0.189876, 0.192050, 0.194240, 0.196452, 0.198692, 0.200947, 0.203230, 0.205536, 0.207864, 0.210214, 0.212587, 0.214983, 0.217408, 0.219850, 0.222322, 0.224817, 0.227330, 0.229873, 0.232440, 0.235039, 0.237654, 0.240302, 0.242975, 0.245673, 0.248396, 0.251144, 0.253925, 0.256725, 0.259567, 0.262426, 0.265311, 0.268232, 0.271180, 0.274146, 0.277156, 0.280185, 0.283250, 0.286343, 0.289465, 0.292614, 0.295800, 0.299016, 0.302260, 0.305533, 0.308845, 0.312188, 0.315559, 0.318960, 0.322402, 0.325874, 0.329376, 0.332909, 0.336485, 0.340091, 0.343740, 0.347421, 0.351122, 0.354878, 0.358667, 0.362476, 0.366331, 0.370231, 0.374163, 0.378130, 0.382130, 0.386177, 0.390258, 0.394387, 0.398552, 0.402751, 0.406999, 0.411268, 0.415588, 0.419957, 0.424363, 0.428806, 0.433301, 0.437832, 0.442401, 0.447022, 0.451682, 0.456395, 0.461131, 0.465922, 0.470768, 0.475652, 0.480594, 0.485558, 0.490580, 0.495658, 0.500779, 0.505940, 0.511160, 0.516422, 0.521744, 0.527108, 0.532514, 0.538002, 0.543513, 0.549088, 0.554687, 0.560367, 0.566095, 0.571865, 0.577702, 0.583583, 0.589532, 0.595547, 0.601588, 0.607697, 0.613875, 0.620100, 0.626374, 0.632717, 0.639110, 0.645573, 0.652086, 0.658672, 0.665308, 0.672018, 0.678778, 0.685615, 0.692503, 0.699467, 0.706484, 0.713553, 0.720700, 0.727928, 0.735209, 0.742544, 0.749961, 0.757459, 0.765013, 0.772622, 0.780315, 0.788094, 0.795931, 0.803822, 0.811801, 0.819839, 0.827996, 0.836180, 0.844456, 0.852821, 0.861247, 0.869766, 0.878344, 0.887019, 0.895755, 0.904586, 0.913480, 0.922437, 0.931527, 0.940681, 0.949934, 0.959218, 0.968637, 0.978124, 0.987714, 0.997370, 1.007094, 1.016962, 1.026859, 1.036863, 1.047017, 1.057200, 1.067494, 1.077899, 1.088375, 1.098965, 1.109628, 1.120406, 1.131255, 1.142267, 1.153307, 1.164471, 1.175751, 1.187109, 1.198588, 1.210191, 1.221823, 1.233629, 1.245514, 1.257528, 1.269617, 1.281839, 1.294190, 1.306626, 1.319193, 1.331843, 1.344681, 1.357548, 1.370558, 1.383701, 1.396932, 1.410305, 1.423817, 1.437420, 1.451167, 1.465005, 1.478984, 1.493118, 1.507339, 1.521714, 1.536238, 1.550795, 1.565569, 1.580436, 1.595526, 1.610646, 1.625925, 1.641365, 1.656903, 1.672676, 1.688475, 1.704516, 1.720651, 1.736959, 1.753368, 1.769876, 1.786632, 1.803496, 1.820531, 1.837752, 1.855074, 1.872503, 1.890192, 1.907991, 1.925976, 1.944069, 1.962354, 1.980748, 1.999419, 2.018202, 2.037101, 2.056191, 2.075572, 2.094980, 2.114598, 2.134412, 2.154438, 2.174489, 2.194846, 2.215417, 2.236102, 2.257006, 2.278132, 2.299379, 2.320848, 2.342443, 2.364261, 2.386409, 2.408584, 2.430993, 2.453633, 2.476504, 2.499513, 2.522763, 2.546138, 2.569873, 2.593743, 2.617858, 2.642108, 2.666618, 2.691375, 2.716276, 2.741553, 2.766855, 2.792535, 2.818363, 2.844463, 2.870828, 2.897339, 2.924129, 2.951064, 2.978409, 3.005904, 3.033554, 3.061620, 3.089845, 3.118357, 3.147029, 3.176132, 3.205401, 3.234826, 3.264700, 3.294734, 3.325082, 3.355592, 3.386418, 3.417557, 3.449021, 3.480651, 3.512610, 3.544894, 3.577513, 3.610304, 3.643436, 3.676913, 3.710555, 3.744557, 3.778902, 3.813424, 3.848477, 3.883721, 3.919321, 3.955103, 3.991258, 4.027785, 4.064684, 4.101770, 4.139250, 4.176907, 4.215156, 4.253795, 4.292627, 4.331663, 4.371291, 4.411131, 4.451383, 4.492039, 4.532898, 4.574190, 4.615885, 4.658024, 4.700360, 4.743147, 4.786361, 4.829786, 4.873660, 4.918217, 4.962759, 5.007995, 5.053450, 5.099374, 5.145769, 5.192634, 5.239724, 5.287301, 5.335366, 5.383661, 5.432711, 5.482008, 5.531800, 5.581825, 5.632648, 5.683428, 5.735015, 5.787119, 5.839469, 5.892358, 5.945789, 5.999761, 6.053995, 6.109078, 6.164124, 6.220019, 6.276505, 6.332932, 6.390259, 6.448164, 6.506660, 6.565426, 6.624799, 6.684762, 6.745012, 6.806215, 6.867695, 6.929809, 6.992201, 7.055231, 7.119257, 7.183219, 7.248190, 7.313830, 7.379763, 7.446354, 7.513647, 7.581240, 7.649882, 7.718851, 7.788520, 7.858912, 7.929607, 8.001011, 8.073162, 8.146012, 8.219219, 8.293573, 8.368268, 8.443711, 8.519493, 8.596489, 8.673814, 8.751931, 8.830843, 8.910549, 8.990616, 9.071476, 9.153659, 9.235693, 9.319068, 9.402792, 9.487350, 9.573290, 9.659097, 9.745761, 9.833813, 9.922253, 10.011603, 10.101849, 10.192486, 10.284024, 10.376501, 10.469922, 10.564303, 10.659626, 10.755363, 10.852057, 10.949738, 11.048420, 11.147506, 11.247595, 11.348716, 11.450840, 11.554016, 11.657629, 11.762896, 11.868616, 11.975417, 12.082639, 12.191606, 12.301009, 12.411555, 12.523203, 12.635294, 12.749232, 12.864320, 12.979157, 13.096604, 13.214518, 13.332890, 13.452507, 13.574043, 13.695312, 13.818604, 13.943121, 14.068125, 14.195203, 14.322782, 14.451663, 14.581036, 14.711723, 14.844562, 14.977945, 15.112655, 15.248752, 15.385357, 15.523358, 15.663632, 15.804473, 15.946729, 16.089542, 16.234718, 16.380444, 16.527651, 16.676365, 16.826614, 16.978355, 17.130709, 17.284569, 17.440004, 17.597036, 17.755684, 17.914900, 18.076771, 18.239277, 18.403408, 18.569260, 18.735668, 18.904902, 19.074736, 19.246311, 19.419653, 19.594727, 19.771591, 19.949141, 20.129663, 20.310841, 20.493860, 20.677540, 20.864334, 21.053011, 21.241123, 21.433681, 21.626930, 21.820906, 22.018091, 22.217354, 22.417320, 22.617958, 22.823372, 23.028162, 23.236414, 23.446810, 23.657936, 23.871233, 24.086720, 24.302927, 24.522808, 24.743488, 24.967913, 25.193121, 25.420692, 25.650494, 25.881184, 26.115732, 26.351130, 26.588957, 26.830839, 27.073547, 27.317120, 27.563122, 27.813398, 28.064505, 28.318211, 28.574457, 28.833410, 29.094986, 29.357483, 29.622629, 29.890493, 30.161125, 30.434555, 30.710806, 30.989857, 31.269880, 31.552727, 31.838562, 32.127277, 32.418976, 32.713718, 33.009380, 33.308117, 33.609917, 33.916954, 34.222866, 34.534081, 34.848515, 35.164017, 35.482719, 35.804710, 36.132305, 36.461037, 36.790779, 37.126263, 37.462883, 37.805374, 38.146545, 38.493656, 38.844383, 39.198658, 39.554211, 39.915890, 40.278778, 40.645348, 41.015812, 41.390045, 41.768177, 42.147461, 42.530781, 42.920742, 43.311996, 43.707333, 44.103832, 44.507313, 44.915020, 45.324074, 45.737259, 46.157848, 46.579731, 47.002853, 47.433559, 47.865543, 48.305164, 48.746246, 49.195126, 49.642105, 50.097012, 50.556686, 51.021194, 51.487038, 51.957767, 52.436871, 52.917515, 53.403202, 53.893856, 54.386055, 54.887047, 55.389469, 55.901031, 56.414085, 56.932514, 57.456398, 57.981777, 58.516571, 59.053017, 59.599102, 60.146915, 60.700333, 61.259586, 61.824718, 62.395828, 62.968510, 63.547237, 64.136398, 64.727402, 65.324623, 65.927971, 66.537727, 67.153923, 67.771980, 68.396378, 69.032104, 69.669807, 70.314232, 70.965462, 71.623451, 72.283401, 72.955345, 73.629333, 74.315628, 75.003807, 75.699303, 76.402145, 77.112473, 77.824692, 78.544472, 79.277412, 80.012558, 80.761215, 81.506134, 82.264763, 83.031464, 83.806366, 84.589500, 85.374756, 86.168350, 86.970390, 87.787247, 88.599968, 89.427971, 90.264549, 91.110130, 91.958160, 92.814980, 93.680939, 94.563240, 95.440765, 96.341789, 97.245461, 98.151505, 99.074432, 100.00
};
constant const float inverse_eotf_bt1886_table[1032] = {
0.000000, 0.055681, 0.074325, 0.088005, 0.099213, 0.108879, 0.117473, 0.125266, 0.132433, 0.139094, 0.145337, 0.151224, 0.156808, 0.162126, 0.167210, 0.172086, 0.176777, 0.181299, 0.185669, 0.189899, 0.194001, 0.197985, 0.201860, 0.205634, 0.209313, 0.212904, 0.216412, 0.219842, 0.223198, 0.226486, 0.229708, 0.232868, 0.235969, 0.239014, 0.242005, 0.244946, 0.247838, 0.250683, 0.253485, 0.256243, 0.258960, 0.261638, 0.264279, 0.266883, 0.269451, 0.271986, 0.274488, 0.276959, 0.279399, 0.281810, 0.284192, 0.286547, 0.288875, 0.291177, 0.293453, 0.295706, 0.297934, 0.300139, 0.302322, 0.304483, 0.306623, 0.308742, 0.310841, 0.312920, 0.314980, 0.317022, 0.319045, 0.321050, 0.323038, 0.325009, 0.326963, 0.328902, 0.330824, 0.332731, 0.334622, 0.336499, 0.338361, 0.340209, 0.342043, 0.343864, 0.345671, 0.347465, 0.349245, 0.351014, 0.352770, 0.354514, 0.356245, 0.357966, 0.359674, 0.361372, 0.363058, 0.364733, 0.366398, 0.368052, 0.369696, 0.371330, 0.372954, 0.374567, 0.376171, 0.377766, 0.379351, 0.380927, 0.382494, 0.384052, 0.385602, 0.387142, 0.388674, 0.390198, 0.391713, 0.393220, 0.394720, 0.396211, 0.397694, 0.399170, 0.400638, 0.402099, 0.403552, 0.404998, 0.406436, 0.407868, 0.409293, 0.410710, 0.412121, 0.413525, 0.414923, 0.416314, 0.417698, 0.419076, 0.420448, 0.421814, 0.423173, 0.424526, 0.425874, 0.427215, 0.428551, 0.429880, 0.431204, 0.432522, 0.433835, 0.435142, 0.436444, 0.437740, 0.439031, 0.440317, 0.441597, 0.442872, 0.444142, 0.445407, 0.446667, 0.447922, 0.449172, 0.450418, 0.451658, 0.452894, 0.454125, 0.455351, 0.456573, 0.457790, 0.459003, 0.460211, 0.461415, 0.462614, 0.463809, 0.465000, 0.466187, 0.467369, 0.468547, 0.469721, 0.470891, 0.472057, 0.473219, 0.474377, 0.475531, 0.476681, 0.477827, 0.478969, 0.480108, 0.481242, 0.482373, 0.483501, 0.484624, 0.485744, 0.486861, 0.487974, 0.489083, 0.490189, 0.491291, 0.492390, 0.493485, 0.494577, 0.495666, 0.496751, 0.497833, 0.498912, 0.499987, 0.501060, 0.502129, 0.503195, 0.504257, 0.505317, 0.506373, 0.507427, 0.508477, 0.509524, 0.510569, 0.511610, 0.512649, 0.513684, 0.514717, 0.515746, 0.516773, 0.517797, 0.518818, 0.519836, 0.520852, 0.521864, 0.522874, 0.523882, 0.524886, 0.525888, 0.526887, 0.527884, 0.528878, 0.529869, 0.530858, 0.531844, 0.532828, 0.533809, 0.534787, 0.535764, 0.536737, 0.537708, 0.538677, 0.539643, 0.540607, 0.541568, 0.542527, 0.543484, 0.544438, 0.545390, 0.546340, 0.547287, 0.548232, 0.549175, 0.550116, 0.551054, 0.551990, 0.552924, 0.553856, 0.554785, 0.555712, 0.556637, 0.557560, 0.558481, 0.559400, 0.560317, 0.561231, 0.562143, 0.563054, 0.563962, 0.564868, 0.565773, 0.566675, 0.567575, 0.568473, 0.569369, 0.570264, 0.571156, 0.572046, 0.572935, 0.573821, 0.574706, 0.575588, 0.576469, 0.577348, 0.578225, 0.579100, 0.579974, 0.580845, 0.581715, 0.582583, 0.583449, 0.584313, 0.585175, 0.586036, 0.586895, 0.587752, 0.588608, 0.589461, 0.590313, 0.591163, 0.592012, 0.592859, 0.593704, 0.594547, 0.595389, 0.596229, 0.597068, 0.597904, 0.598740, 0.599573, 0.600405, 0.601235, 0.602064, 0.602891, 0.603717, 0.604541, 0.605363, 0.606184, 0.607003, 0.607821, 0.608637, 0.609452, 0.610265, 0.611077, 0.611887, 0.612695, 0.613503, 0.614308, 0.615112, 0.615915, 0.616716, 0.617516, 0.618314, 0.619111, 0.619907, 0.620701, 0.621494, 0.622285, 0.623075, 0.623863, 0.624650, 0.625436, 0.626220, 0.627003, 0.627784, 0.628564, 0.629343, 0.630121, 0.630897, 0.631671, 0.632445, 0.633217, 0.633988, 0.634757, 0.635526, 0.636292, 0.637058, 0.637822, 0.638585, 0.639347, 0.640108, 0.640867, 0.641625, 0.642382, 0.643137, 0.643891, 0.644644, 0.645396, 0.646147, 0.646896, 0.647644, 0.648391, 0.649137, 0.649881, 0.650624, 0.651367, 0.652108, 0.652847, 0.653586, 0.654323, 0.655060, 0.655795, 0.656529, 0.657261, 0.657993, 0.658724, 0.659453, 0.660181, 0.660909, 0.661635, 0.662359, 0.663083, 0.663806, 0.664528, 0.665248, 0.665968, 0.666686, 0.667403, 0.668119, 0.668834, 0.669548, 0.670261, 0.670973, 0.671684, 0.672394, 0.673103, 0.673810, 0.674517, 0.675223, 0.675927, 0.676631, 0.677333, 0.678035, 0.678736, 0.679435, 0.680134, 0.680831, 0.681528, 0.682223, 0.682918, 0.683611, 0.684304, 0.684995, 0.685686, 0.686375, 0.687064, 0.687752, 0.688438, 0.689124, 0.689809, 0.690493, 0.691176, 0.691858, 0.692539, 0.693219, 0.693898, 0.694576, 0.695254, 0.695930, 0.696605, 0.697280, 0.697954, 0.698626, 0.699298, 0.699969, 0.700639, 0.701308, 0.701976, 0.702644, 0.703310, 0.703976, 0.704641, 0.705304, 0.705967, 0.706629, 0.707291, 0.707951, 0.708610, 0.709269, 0.709927, 0.710584, 0.711240, 0.711895, 0.712549, 0.713203, 0.713856, 0.714507, 0.715159, 0.715809, 0.716458, 0.717107, 0.717754, 0.718401, 0.719047, 0.719693, 0.720337, 0.720981, 0.721624, 0.722266, 0.722907, 0.723548, 0.724187, 0.724826, 0.725464, 0.726102, 0.726738, 0.727374, 0.728009, 0.728643, 0.729276, 0.729909, 0.730541, 0.731172, 0.731803, 0.732432, 0.733061, 0.733689, 0.734316, 0.734943, 0.735569, 0.736194, 0.736818, 0.737442, 0.738065, 0.738687, 0.739309, 0.739929, 0.740549, 0.741168, 0.741787, 0.742405, 0.743022, 0.743638, 0.744254, 0.744869, 0.745483, 0.746096, 0.746709, 0.747321, 0.747933, 0.748544, 0.749154, 0.749763, 0.750371, 0.750979, 0.751587, 0.752193, 0.752799, 0.753404, 0.754009, 0.754613, 0.755216, 0.755818, 0.756420, 0.757021, 0.757622, 0.758222, 0.758821, 0.759419, 0.760017, 0.760614, 0.761211, 0.761807, 0.762402, 0.762996, 0.763590, 0.764183, 0.764776, 0.765368, 0.765959, 0.766550, 0.767140, 0.767730, 0.768318, 0.768907, 0.769494, 0.770081, 0.770667, 0.771253, 0.771838, 0.772422, 0.773006, 0.773589, 0.774172, 0.774754, 0.775335, 0.775916, 0.776496, 0.777076, 0.777655, 0.778233, 0.778811, 0.779388, 0.779964, 0.780540, 0.781115, 0.781690, 0.782264, 0.782838, 0.783411, 0.783983, 0.784555, 0.785126, 0.785697, 0.786267, 0.786836, 0.787405, 0.787974, 0.788541, 0.789108, 0.789675, 0.790241, 0.790807, 0.791371, 0.791936, 0.792500, 0.793063, 0.793625, 0.794188, 0.794749, 0.795310, 0.795870, 0.796430, 0.796990, 0.797548, 0.798107, 0.798664, 0.799222, 0.799778, 0.800334, 0.800890, 0.801445, 0.801999, 0.802553, 0.803106, 0.803659, 0.804212, 0.804763, 0.805315, 0.805865, 0.806415, 0.806965, 0.807514, 0.808063, 0.808611, 0.809159, 0.809706, 0.810252, 0.810798, 0.811344, 0.811889, 0.812433, 0.812977, 0.813521, 0.814064, 0.814606, 0.815148, 0.815690, 0.816230, 0.816771, 0.817311, 0.817850, 0.818389, 0.818928, 0.819466, 0.820003, 0.820540, 0.821077, 0.821613, 0.822148, 0.822683, 0.823218, 0.823752, 0.824285, 0.824818, 0.825351, 0.825883, 0.826415, 0.826946, 0.827476, 0.828007, 0.828536, 0.829066, 0.829594, 0.830123, 0.830650, 0.831178, 0.831705, 0.832231, 0.832757, 0.833283, 0.833808, 0.834332, 0.834856, 0.835380, 0.835903, 0.836426, 0.836948, 0.837470, 0.837991, 0.838512, 0.839033, 0.839553, 0.840072, 0.840591, 0.841110, 0.841628, 0.842146, 0.842663, 0.843180, 0.843697, 0.844213, 0.844728, 0.845243, 0.845758, 0.846272, 0.846786, 0.847299, 0.847812, 0.848325, 0.848837, 0.849348, 0.849860, 0.850370, 0.850881, 0.851391, 0.851900, 0.852409, 0.852918, 0.853426, 0.853934, 0.854441, 0.854948, 0.855455, 0.855961, 0.856466, 0.856972, 0.857476, 0.857981, 0.858485, 0.858989, 0.859492, 0.859994, 0.860497, 0.860999, 0.861500, 0.862001, 0.862502, 0.863002, 0.863502, 0.864002, 0.864501, 0.865000, 0.865498, 0.865996, 0.866493, 0.866991, 0.867487, 0.867984, 0.868479, 0.868975, 0.869470, 0.869965, 0.870459, 0.870953, 0.871447, 0.871940, 0.872433, 0.872925, 0.873417, 0.873908, 0.874400, 0.874891, 0.875381, 0.875871, 0.876361, 0.876850, 0.877339, 0.877827, 0.878316, 0.878803, 0.879291, 0.879778, 0.880264, 0.880751, 0.881236, 0.881722, 0.882207, 0.882692, 0.883176, 0.883660, 0.884144, 0.884627, 0.885110, 0.885593, 0.886075, 0.886557, 0.887038, 0.887519, 0.888000, 0.888480, 0.888960, 0.889440, 0.889919, 0.890398, 0.890876, 0.891354, 0.891832, 0.892310, 0.892787, 0.893264, 0.893740, 0.894216, 0.894692, 0.895167, 0.895642, 0.896117, 0.896591, 0.897065, 0.897538, 0.898012, 0.898484, 0.898957, 0.899429, 0.899901, 0.900372, 0.900843, 0.901314, 0.901785, 0.902255, 0.902725, 0.903194, 0.903663, 0.904132, 0.904600, 0.905068, 0.905536, 0.906003, 0.906470, 0.906937, 0.907403, 0.907869, 0.908335, 0.908800, 0.909265, 0.909730, 0.910195, 0.910659, 0.911122, 0.911586, 0.912049, 0.912511, 0.912974, 0.913436, 0.913898, 0.914359, 0.914820, 0.915281, 0.915741, 0.916201, 0.916661, 0.917121, 0.917580, 0.918038, 0.918497, 0.918955, 0.919413, 0.919870, 0.920328, 0.920785, 0.921241, 0.921697, 0.922153, 0.922609, 0.923064, 0.923519, 0.923974, 0.924428, 0.924882, 0.925336, 0.925790, 0.926243, 0.926696, 0.927148, 0.927600, 0.928052, 0.928504, 0.928955, 0.929406, 0.929857, 0.930307, 0.930757, 0.931207, 0.931656, 0.932105, 0.932554, 0.933003, 0.933451, 0.933899, 0.934347, 0.934794, 0.935241, 0.935688, 0.936134, 0.936580, 0.937026, 0.937472, 0.937917, 0.938362, 0.938807, 0.939251, 0.939695, 0.940139, 0.940582, 0.941025, 0.941468, 0.941911, 0.942353, 0.942795, 0.943237, 0.943678, 0.944120, 0.944561, 0.945001, 0.945441, 0.945881, 0.946321, 0.946761, 0.947200, 0.947639, 0.948077, 0.948515, 0.948953, 0.949391, 0.949829, 0.950266, 0.950703, 0.951139, 0.951576, 0.952012, 0.952447, 0.952883, 0.953318, 0.953753, 0.954188, 0.954622, 0.955056, 0.955490, 0.955923, 0.956357, 0.956790, 0.957223, 0.957655, 0.958087, 0.958519, 0.958951, 0.959382, 0.959813, 0.960244, 0.960674, 0.961105, 0.961535, 0.961964, 0.962394, 0.962823, 0.963252, 0.963681, 0.964109, 0.964537, 0.964965, 0.965393, 0.965820, 0.966247, 0.966674, 0.967101, 0.967527, 0.967953, 0.968379, 0.968804, 0.969229, 0.969654, 0.970079, 0.970503, 0.970928, 0.971352, 0.971775, 0.972199, 0.972622, 0.973045, 0.973467, 0.973890, 0.974312, 0.974734, 0.975155, 0.975577, 0.975998, 0.976419, 0.976839, 0.977260, 0.977680, 0.978099, 0.978519, 0.978938, 0.979357, 0.979776, 0.980195, 0.980613, 0.981031, 0.981449, 0.981867, 0.982284, 0.982701, 0.983118, 0.983535, 0.983951, 0.984367, 0.984783, 0.985198, 0.985614, 0.986029, 0.986444, 0.986858, 0.987273, 0.987687, 0.988101, 0.988515, 0.988928, 0.989341, 0.989754, 0.990167, 0.990579, 0.990991, 0.991403, 0.991815, 0.992227, 0.992638, 0.993049, 0.993460, 0.993870, 0.994281, 0.994691, 0.995100, 0.995510, 0.995919, 0.996328, 0.996737, 0.997146, 0.997554, 0.997963, 0.998371, 0.998778, 0.999186, 0.999593, 1.0, 0, 0, 0, 0, 0, 0, 0
};
constant const float inverse_eotf_bt1886_small_table[129] = {
0.000000, 0.025947, 0.034635, 0.041010, 0.046233, 0.050738, 0.054742, 0.058374, 0.061713, 0.064818, 0.067726, 0.070470, 0.073072, 0.075550, 0.077919, 0.080192, 0.082377, 0.084485, 0.086521, 0.088492, 0.090404, 0.092261, 0.094066, 0.095825, 0.097539, 0.099213, 0.100847, 0.102446, 0.104010, 0.105542, 0.107043, 0.108516, 0.109961, 0.111380, 0.112774, 0.114144, 0.115492, 0.116818, 0.118123, 0.119409, 0.120675, 0.121923, 0.123153, 0.124367, 0.125564, 0.126745, 0.127911, 0.129062, 0.130199, 0.131323, 0.132433, 0.133530, 0.134615, 0.135688, 0.136748, 0.137798, 0.138836, 0.139864, 0.140881, 0.141888, 0.142885, 0.143873, 0.144851, 0.145820, 0.146780, 0.147731, 0.148674, 0.149608, 0.150535, 0.151453, 0.152364, 0.153267, 0.154163, 0.155052, 0.155933, 0.156808, 0.157675, 0.158537, 0.159391, 0.160240, 0.161082, 0.161917, 0.162747, 0.163571, 0.164390, 0.165202, 0.166009, 0.166811, 0.167607, 0.168398, 0.169184, 0.169965, 0.170741, 0.171511, 0.172277, 0.173039, 0.173795, 0.174547, 0.175295, 0.176038, 0.176777, 0.177511, 0.178241, 0.178967, 0.179689, 0.180407, 0.181121, 0.181831, 0.182537, 0.183240, 0.183938, 0.184633, 0.185324, 0.186012, 0.186696, 0.187377, 0.188054, 0.188728, 0.189398, 0.190065, 0.190729, 0.191390, 0.192047, 0.192702, 0.193353, 0.194001, 0.194646, 0.195288, 0.195928
};
float get_luma_dst(float3 c) {
return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
}
float get_luma_src(float3 c) {
return luma_src.x * c.x + luma_src.y * c.y + luma_src.z * c.z;
}
float3 get_chroma_sample(float3 a, float3 b, float3 c, float3 d) {
return chroma_sample(a, b, c, d);
}
float eotf_st2084(float x) {
unsigned short y = x > 0.0f ? (unsigned short)(x * 1024) : 0;
return eotf_st2084_table[y];
/*
float p = powr(x, 1.0f / ST2084_M2);
float a = max(p -ST2084_C1, 0.0f);
float b = max(ST2084_C2 - ST2084_C3 * p, 1e-6f);
float c = powr(a / b, 1.0f / ST2084_M1);
return x > 0.0f ? c * ST2084_MAX_LUMINANCE / REFERENCE_WHITE : 0.0f;
*/
}
__constant const float HLG_A = 0.17883277f;
__constant const float HLG_B = 0.28466892f;
__constant const float HLG_C = 0.55991073f;
// linearizer for HLG
float inverse_oetf_hlg(float x) {
float a = 4.0f * x * x;
float b = exp((x - HLG_C) / HLG_A) + HLG_B;
return x < 0.5f ? a : b;
}
// delinearizer for HLG
float oetf_hlg(float x) {
float a = 0.5f * sqrt(x);
float b = HLG_A * log(x - HLG_B) + HLG_C;
return x <= 1.0f ? a : b;
}
float3 ootf_hlg(float3 c, float peak) {
float luma = get_luma_src(c);
float gamma = 1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
gamma = max(1.0f, gamma);
float factor = peak * powr(luma, gamma - 1.0f) / powr(12.0f, gamma);
return c * factor;
}
float3 inverse_ootf_hlg(float3 c, float peak) {
float gamma = 1.2f + 0.42f * log10(peak * REFERENCE_WHITE / 1000.0f);
c *= powr(12.0f, gamma) / peak;
c /= powr(get_luma_dst(c), (gamma - 1.0f) / gamma);
return c;
}
float inverse_eotf_bt1886(float c) {
if (c > 0.02) {
unsigned short d = (unsigned short)(c * 1024);
return inverse_eotf_bt1886_table[d];
} else {
unsigned short d = (unsigned short)(c * 6400);
return inverse_eotf_bt1886_small_table[d];
}
//return c < 0.0f ? 0.0f : powr(c, 1.0f / 2.4f);
}
float oetf_bt709(float c) {
c = c < 0.0f ? 0.0f : c;
float r1 = 4.5f * c;
float r2 = 1.099f * powr(c, 0.45f) - 0.099f;
return c < 0.018f ? r1 : r2;
}
float inverse_oetf_bt709(float c) {
float r1 = c / 4.5f;
float r2 = powr((c + 0.099f) / 1.099f, 1.0f / 0.45f);
return c < 0.081f ? r1 : r2;
}
float3 yuv2rgb(float y, float u, float v) {
#ifdef FULL_RANGE_IN
u -= 0.5f; v -= 0.5f;
#else
y = (y * 255.0f - 16.0f) / 219.0f;
u = (u * 255.0f - 128.0f) / 224.0f;
v = (v * 255.0f - 128.0f) / 224.0f;
#endif
float r = y * rgb_matrix[0] + u * rgb_matrix[1] + v * rgb_matrix[2];
float g = y * rgb_matrix[3] + u * rgb_matrix[4] + v * rgb_matrix[5];
float b = y * rgb_matrix[6] + u * rgb_matrix[7] + v * rgb_matrix[8];
return (float3)(r, g, b);
}
float3 yuv2lrgb(float3 yuv) {
float3 rgb = yuv2rgb(yuv.x, yuv.y, yuv.z);
#ifdef linearize
float r = linearize(rgb.x);
float g = linearize(rgb.y);
float b = linearize(rgb.z);
return (float3)(r, g, b);
#else
return rgb;
#endif
}
float3 rgb2yuv(float r, float g, float b) {
float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
float u = r*yuv_matrix[3] + g*yuv_matrix[4] + b*yuv_matrix[5];
float v = r*yuv_matrix[6] + g*yuv_matrix[7] + b*yuv_matrix[8];
#ifdef FULL_RANGE_OUT
u += 0.5f; v += 0.5f;
#else
y = (219.0f * y + 16.0f) / 255.0f;
u = (224.0f * u + 128.0f) / 255.0f;
v = (224.0f * v + 128.0f) / 255.0f;
#endif
return (float3)(y, u, v);
}
float rgb2y(float r, float g, float b) {
float y = r*yuv_matrix[0] + g*yuv_matrix[1] + b*yuv_matrix[2];
y = (219.0f * y + 16.0f) / 255.0f;
return y;
}
float3 lrgb2yuv(float3 c) {
#ifdef delinearize
float r = delinearize(c.x);
float g = delinearize(c.y);
float b = delinearize(c.z);
return rgb2yuv(r, g, b);
#else
return rgb2yuv(c.x, c.y, c.z);
#endif
}
float lrgb2y(float3 c) {
#ifdef delinearize
float r = delinearize(c.x);
float g = delinearize(c.y);
float b = delinearize(c.z);
return rgb2y(r, g, b);
#else
return rgb2y(c.x, c.y, c.z);
#endif
}
float3 lrgb2lrgb(float3 c) {
#ifdef RGB2RGB_PASSTHROUGH
return c;
#else
float r = c.x, g = c.y, b = c.z;
float rr = rgb2rgb[0] * r + rgb2rgb[1] * g + rgb2rgb[2] * b;
float gg = rgb2rgb[3] * r + rgb2rgb[4] * g + rgb2rgb[5] * b;
float bb = rgb2rgb[6] * r + rgb2rgb[7] * g + rgb2rgb[8] * b;
return (float3)(rr, gg, bb);
#endif
}
float3 ootf(float3 c, float peak) {
#ifdef ootf_impl
return ootf_impl(c, peak);
#else
return c;
#endif
}
float3 inverse_ootf(float3 c, float peak) {
#ifdef inverse_ootf_impl
return inverse_ootf_impl(c, peak);
#else
return c;
#endif
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static inline float eotf_st2084(float x, int npl) {
const float ST2084_M1 = 0.1593017578125f;
const float ST2084_M2 = 78.84375f;
const float ST2084_C1 = 0.8359375f;
const float ST2084_C2 = 18.8515625f;
const float ST2084_C3 = 18.6875f;
static const float ST2084_MAX_LUMINANCE = 10000.0f;
float p = powf(x, 1.0f / ST2084_M2);
float a = fmax(p -ST2084_C1, 0.0f);
float b = fmax(ST2084_C2 - ST2084_C3 * p, 1e-6f);
float c = powf(a / b, 1.0f / ST2084_M1);
return x > 0.0f ? c * ST2084_MAX_LUMINANCE / npl : 0.0f;
}
static inline float inverse_eotf_bt1886(float c) {
return c < 0.0f ? 0.0f : powf(c, 1.0f / 2.4f);
}
static int fill_eotf_st2084_table(float *table, int len, float upper_limit, int npl) {
for(int i = 0; i < len * upper_limit; ++i) {
float L0 = (i + 0.0) / len;
float L1 = eotf_st2084(L0, npl);
table[i] = (float)(L1);
}
return 0;
}
static int fill_inverse_bt1886_table(float *table, int len, float upper_limit) {
for(int i = 0; i < len * upper_limit; ++i) {
float L0 = (i + 0.0) / len;
float L1 = inverse_eotf_bt1886(L0);
table[i] = (float)(L1);
}
return 0;
}
static int print_table(float *table, int len, float upper_limit) {
int count = 0;
for(int i = 0; i < len * upper_limit; i++) {
if (count >= 1) {
printf(", ");
}
printf("%f", table[i]);
++count;
}
printf("\ntablesize is %d, count is %d\n", (int)(len * upper_limit), len);
return 0;
}
int main() {
const float RANGE = 1.0f;
const float SMALL_RANGE = 0.02f;
const int TALBE_SIZE = ((int)(1 / (RANGE / 2048)));
const int SMALL_TALBE_SIZE = ((int)(1 / (SMALL_RANGE / 512)));
// Generate
float eoft_st2084_table[TALBE_SIZE] = {0};
fill_eotf_st2084_table(eoft_st2084_table, TALBE_SIZE, RANGE, 100);
print_table(eoft_st2084_table, TALBE_SIZE, RANGE);
float inverse_bt1886_table[TALBE_SIZE] = {0};
fill_inverse_bt1886_table(inverse_bt1886_table, TALBE_SIZE, RANGE);
print_table(inverse_bt1886_table, TALBE_SIZE, RANGE);
float inverse_bt1886_small_table[SMALL_TALBE_SIZE] = {0};
fill_inverse_bt1886_table(inverse_bt1886_small_table, SMALL_TALBE_SIZE, SMALL_RANGE);
print_table(inverse_bt1886_small_table, SMALL_TALBE_SIZE, SMALL_RANGE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment