-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHDR10_Content_Metadata.avsi
215 lines (180 loc) · 8.5 KB
/
HDR10_Content_Metadata.avsi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
A script for calculating MaxFall and MaxCLL of HDR10 video.
Reference - https://ieeexplore.ieee.org/document/9508136
*/
### Requirements - AviSynth+ >= r3573, avsresize, RT_Stat, RoundHalfToEven.
### Usage ###
###
# Function HDR10_Content_Metadata(clip c, string "log_path", bool "MaxCLL", bool "MaxFALL", float "MaxCLL_percentile", float "MaxFALL_percentile", string "temp_files_suffix", bool "chunks")
###
## Parameters ##
#---------------
# c: Input clip.
# Must be in YUV420 format.
# Must have frame properties _Transfer(16) and _Matrix(9).
# Must be cropped.
#---------------
# log_path (default: the script location): The path for the log.
#---------------
# MaxCLL (default: true): Whether to calculate MaxCLL.
#---------------
# MaxFALL (default: true): Whether to calculate MaxFALL.
#---------------
# MaxCLL_percentile (default: 99.5): Compute percentile of the frame's max brightness.
# Must be between 0.0..100.0.
#---------------
# MaxFALL_percentile (default: 99.75): Compute percentile of the frame's max brightness.
# Must be between 0.0..100.0.
#---------------
# temp_files_suffix (default: not specified): Whether to add suffix to the temporary files.
# This will allow to run instances simultaneously.
#---------------
# chunks (default: false): Whether to perform the final calculation.
### Temporary files ###
###
# In order to save some intermediate results, temporary file(s) is created in the same location as log_path. At the end of the analysis this file(s) is deleted.
###
### Parallelization ###
###
# You can try HDR10_Content_Metadata_chunks.
###
### How to use ###
/*
Two options:
1.) If you don't want to create a script then open AvsPmod and write the scrript, hit Refresh and wait until you see the message 'Done.'.
Note - in this case if default "log_path" is used, the log will be saved in the AvsPmod folder.
2.) If you create .avs script then you could open it with:
- avsr ( https://forum.doom9.org/showthread.php?t=173259 ): avsr(64) script_name.avs;
- avsmeter ( https://forum.doom9.org/showthread.php?t=174797 ): avsmeter(64) script_name.avs;
- ffmpeg: ffmpeg -i script_name.avs -f null - ;
- AvsPmod: 1) import("script_name.avs") and 2) Video->Run analysis pass or hit Refresh.
*/
### Changelog ###
#---------------
# 1.0.3
# Fixed duplicated last frame value.
# Added parameter chunks.
#---------------
# 1.0.2
# Removed repeating code.
#---------------
# 1.0.1
# Actually give error message if MaxCLL_percentile / MaxCLL_percentile aren't between 0.0..100.0.
# Added parameter temp_files_suffix - allows multi instances simultaneously.
#---------------
# 1.0.0
# Initial version.
Function HDR10_Content_Metadata(clip c, string "log_path", bool "MaxCLL", bool "MaxFALL", float "MaxCLL_percentile", float "MaxFALL_percentile", string "temp_files_suffix", bool "chunks")
{
Assert(Is420(c), "HDR10_Content_Metadata: the clip must be in YUV420 format.")
Assert(propGetInt(c, "_Transfer") == 16, "HDR10_Content_Metadata: the clip must have _Transfer frame property 16.")
Assert(propGetInt(c, "_Matrix") == 9, "HDR10_Content_Metadata: the clip must have _Matrix frame property 9.")
log_path = (Defined(log_path)) ? (log_path + "\") : ScriptDir
maxcll = Default(MaxCLL, true)
maxfall = Default(MaxFALL, true)
maxcll_percentile = Default(MaxCLL_percentile, 99.5)
maxfall_percentile = Default(MaxFALL_percentile, 99.75)
temp_files_suffix = Default(temp_files_suffix, "")
chunks = Default(chunks, false)
Assert(MaxCLL_percentile >= 0.0 && MaxCLL_percentile <= 100.0, "HDR10_Content_Metadata: MaxCLL_percentile must be between 0.0..100.0.")
Assert(MaxFALL_percentile >= 0.0 && MaxFALL_percentile <= 100.0, "HDR10_Content_Metadata: MaxFALL_percentile must be between 0.0..100.0.")
c
frames_count_minus_1 = FrameCount(c) - 1
z_ConvertFormat(pixel_type="rgbps", colorspace_op="2020:st2084:2020:l=>rgb:linear:2020:f", chromaloc_op="top_left=>top_left", nominal_luminance=10000)
Expr("x 0 1 clip")
if (!maxcll && maxfall)
{
pass1_avg_file = log_path + "pass1_avg" + temp_files_suffix + ".txt"
if (Exist(pass1_avg_file))
{
RT_FileDelete(pass1_avg_file)
}
for (i = 0, frames_count_minus_1)
{
current_frame = i
rmax = RplaneMax()
gmax = GplaneMax()
bmax = BplaneMax()
max_rgb = (rmax >= Max(gmax, bmax)) ? 1
\ : (gmax >= Max(rmax, bmax)) ? 2
\ : 3
RT_WriteFile(pass1_avg_file, "%s", String((max_rgb < 3) ? (max_rgb < 2) ? AverageR() : AverageG() : AverageB()), append=true)
}
if (!chunks)
{
pass1 = RT_ReadTxtFromFile(pass1_avg_file)
RT_FileDelete(pass1_avg_file)
sorted_pass = RT_TxtSort(pass1)
max_avg = RT_TxtGetLine(sorted_pass, (maxfall_percentile < 100.0) ? RoundHalfToEven((maxfall_percentile / 100.0) * (RT_TxtQueryLines(sorted_pass) - 1))
\ : RT_TxtQueryLines(sorted_pass) - 1)
RT_WriteFile(log_path + "HDR10_Content_Metadata_MaxFALL.txt", "%s%f%s%i", "MaxFALL: ", Value(max_avg) * 10000, " at frame ", RT_TxtFindStr(pass1, max_avg))
}
}
else if (maxcll && !maxfall)
{
maxcll_percentile_100 = maxcll_percentile == 100.0
threshold_ = (maxcll_percentile_100) ? 0 : 0.01
pass1_max_file = log_path + "pass1_max" + temp_files_suffix + ".txt"
if (Exist(pass1_max_file))
{
RT_FileDelete(pass1_max_file)
}
for (i = 0, frames_count_minus_1)
{
current_frame = i
RT_WriteFile(pass1_max_file, "%s", String(Max(RplaneMax(threshold=threshold_), GplaneMax(threshold=threshold_), BplaneMax(threshold=threshold_))), append=true)
}
if (!chunks)
{
pass1 = RT_ReadTxtFromFile(pass1_max_file)
RT_FileDelete(pass1_max_file)
sorted_pass = RT_TxtSort(pass1)
max_ = RT_TxtGetLine(sorted_pass, (!maxcll_percentile_100) ? RoundHalfToEven((maxcll_percentile / 100.0) * (RT_TxtQueryLines(sorted_pass) - 1))
\ : RT_TxtQueryLines(sorted_pass) - 1)
RT_WriteFile(log_path + "HDR10_Content_Metadata_MaxCLL.txt", "%s%f%s%i", "MaxCLL: ", Value(max_) * 10000, " at frame ", RT_TxtFindStr(pass1, max_))
}
}
else if (maxcll && maxfall)
{
maxcll_percentile_100 = maxcll_percentile == 100.0
threshold_ = (maxcll_percentile_100) ? 0 : 0.01
pass1_avg_file = log_path + "pass1_avg" + temp_files_suffix + ".txt"
pass1_max_file = log_path + "pass1_max" + temp_files_suffix + ".txt"
if (Exist(pass1_avg_file))
{
RT_FileDelete(pass1_avg_file)
}
if (Exist(pass1_max_file))
{
RT_FileDelete(pass1_max_file)
}
for (i = 0, frames_count_minus_1)
{
current_frame = i
rmax = RplaneMax()
gmax = GplaneMax()
bmax = BplaneMax()
max_rgb = (rmax >= Max(gmax, bmax)) ? 1
\ : (gmax >= Max(rmax, bmax)) ? 2
\ : 3
RT_WriteFile(pass1_avg_file, "%s", String((max_rgb < 3) ? (max_rgb < 2) ? AverageR() : AverageG() : AverageB()), append=true)
RT_WriteFile(pass1_max_file, "%s", String((maxcll_percentile_100) ? Max(rmax, gmax, bmax)
\ : Max(RplaneMax(threshold=threshold_), GplaneMax(threshold=threshold_), BplaneMax(threshold=threshold_))), append=true)
}
if (!chunks)
{
pass1_avg = RT_ReadTxtFromFile(pass1_avg_file)
pass1_max = RT_ReadTxtFromFile(pass1_max_file)
RT_FileDelete(pass1_avg_file)
RT_FileDelete(pass1_max_file)
sorted_pass_avg = RT_TxtSort(pass1_avg)
sorted_pass_max = RT_TxtSort(pass1_max)
max_avg = RT_TxtGetLine(sorted_pass_avg, (maxfall_percentile < 100.0) ? RoundHalfToEven((maxfall_percentile / 100.0) * (RT_TxtQueryLines(sorted_pass_avg) - 1))
\ : RT_TxtQueryLines(sorted_pass_avg) - 1)
max_ = RT_TxtGetLine(sorted_pass_max, (!maxcll_percentile_100) ? RoundHalfToEven((maxcll_percentile / 100.0) * (RT_TxtQueryLines(sorted_pass_max) - 1))
\ : RT_TxtQueryLines(sorted_pass_max) - 1)
RT_WriteFile(log_path + "HDR10_Content_Metadata.txt", "%s%f%s%i%s%s%f%s%i", "MaxCLL: ", Value(max_) * 10000, " at frame ", RT_TxtFindStr(pass1_max, max_), Chr(10), "MaxFALL: ", Value(max_avg) * 10000, " at frame ", RT_TxtFindStr(pass1_avg, max_avg))
}
}
return MessageClip("Done.")
}