-
Notifications
You must be signed in to change notification settings - Fork 0
/
vqmplot.go
168 lines (145 loc) · 3.99 KB
/
vqmplot.go
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
// Copyright ©2022 Evolution. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// ease tool's vqmplot subcommand implementation.
package main
import (
"flag"
"fmt"
"os"
"path"
"strings"
"github.com/evolution-gaming/ease/internal/analysis"
"github.com/evolution-gaming/ease/internal/logging"
"github.com/evolution-gaming/ease/internal/vqm"
)
// Support these metrics for plotting.
var supportedMetrics = "VMAF, PSNR, MS-SSIM"
// CreateVQMPlotCommand will create instance of VQMPlotApp.
func CreateVQMPlotCommand() *VQMPlotApp {
longHelp := `Subcommand "vqmplot" will create plot for given metric from JSON report as
generated by libvmaf.
Examples:
ease vqmplot -i libvmaf.json -o vmaf.png
ease vqmplot -m PSNR -i libvmaf.json -o psnr.png`
app := &VQMPlotApp{
fs: flag.NewFlagSet("vqmplot", flag.ContinueOnError),
gf: globalFlags{},
}
app.gf.Register(app.fs)
app.fs.StringVar(&app.flSrcFile, "i", "", "Input libvmaf JSON file (mandatory)")
app.fs.StringVar(&app.flOutFile, "o", "", "Output file")
app.fs.StringVar(&app.flMetric, "m", "VMAF", fmt.Sprintf("Metric to plot (%s)", supportedMetrics))
app.fs.Float64Var(&app.flFPS, "fps", 0, "Source video file FPS")
app.fs.Usage = func() {
printSubCommandUsage(longHelp, app.fs)
}
return app
}
// VQMPlotApp is vqmplot subcommand context that implements Commander interface.
type VQMPlotApp struct {
// FlagSet instance
fs *flag.FlagSet
// Source file containing per-frame libvmaf JSON data
flSrcFile string
// Output file to save plot to
flOutFile string
// Selected metric to plot
flMetric string
// Video file fps
flFPS float64
// Global flags
gf globalFlags
}
// Run is entry point to VQMPlotApp command execution.
func (a *VQMPlotApp) Run(args []string) error {
if err := a.fs.Parse(args); err != nil {
return &AppError{
exitCode: 2,
msg: "usage error",
}
}
if a.gf.Debug {
logging.EnableDebugLogger()
}
// Flag specifying libvmaf JSON metrics is mandatory.
if a.flSrcFile == "" {
a.fs.Usage()
return &AppError{
exitCode: 2,
msg: "mandatory option -i is missing",
}
}
// libvmaf JSON metrics file must exist.
if _, err := os.Stat(a.flSrcFile); os.IsNotExist(err) {
return &AppError{
exitCode: 2,
msg: fmt.Sprintf("input file missing: %s", err.Error()),
}
}
// Output file will be constructed if not specified.
if a.flOutFile == "" {
base := path.Base(a.flSrcFile)
base = strings.TrimSuffix(base, path.Ext(base))
a.flOutFile = base + ".png"
}
if !strings.Contains(supportedMetrics, a.flMetric) {
a.fs.Usage()
return &AppError{
exitCode: 2,
msg: fmt.Sprintf("unsupported metric, should be one of: %s\n", supportedMetrics),
}
}
if a.flFPS == 0 {
a.fs.Usage()
return &AppError{
exitCode: 2,
msg: "mandatory option -fps is missing",
}
}
logging.Info("Starting...")
jsonFd, err := os.Open(a.flSrcFile)
if err != nil {
return &AppError{
exitCode: 1,
msg: err.Error(),
}
}
defer jsonFd.Close()
var frameMetrics vqm.FrameMetrics
if err := frameMetrics.FromFfmpegVMAF(jsonFd); err != nil {
return &AppError{
exitCode: 1,
msg: err.Error(),
}
}
vqms := make(metricXYs, 0, len(frameMetrics))
switch a.flMetric {
case "VMAF":
for _, v := range frameMetrics {
vqms = append(vqms, metricXY{X: float64(v.FrameNum) / a.flFPS, Y: v.VMAF})
}
case "PSNR":
for _, v := range frameMetrics {
vqms = append(vqms, metricXY{X: float64(v.FrameNum) / a.flFPS, Y: v.PSNR})
}
case "MS-SSIM":
for _, v := range frameMetrics {
vqms = append(vqms, metricXY{X: float64(v.FrameNum) / a.flFPS, Y: v.MS_SSIM})
}
}
if len(vqms) == 0 {
return &AppError{
exitCode: 1,
msg: fmt.Sprintf("no records for %s in %s", a.flMetric, a.flSrcFile),
}
}
if err := analysis.MultiPlotVqm(vqms, a.flMetric, path.Base(a.flSrcFile), a.flOutFile); err != nil {
return &AppError{
exitCode: 1,
msg: err.Error(),
}
}
logging.Info("Done")
return nil
}