1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.media.videoquality.bdrate;
18 
19 import com.google.auto.value.AutoValue;
20 import com.google.common.base.Splitter;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 @AutoValue
26 public abstract class VeqTestResult {
27 
28     private static final Splitter LINE_SPLITTER = Splitter.on(System.lineSeparator());
29 
30     private static final Splitter KEY_VALUE_SPLITTER = Splitter.on("=").trimResults();
31 
referenceFile()32     public abstract String referenceFile();
33 
curve()34     public abstract RateDistortionCurve curve();
35 
parseFromTestResult(String result)36     public static VeqTestResult parseFromTestResult(String result) {
37         String referenceFile = null;
38         ArrayList<Double> bitrates = new ArrayList<>();
39         ArrayList<Double> vmafs = new ArrayList<>();
40 
41         for (String line : LINE_SPLITTER.split(result)) {
42             List<String> keyValue = KEY_VALUE_SPLITTER.splitToList(line);
43             if (keyValue.size() != 2) {
44                 continue;
45             }
46 
47             String key = keyValue.get(0);
48             String value = keyValue.get(1);
49 
50             switch (key) {
51                 case "Y4M file":
52                     if (referenceFile == null) {
53                         referenceFile = value;
54                     } else if (!referenceFile.equals(value)) {
55                         throw new IllegalArgumentException(
56                                 "Test result data contained multiple reference files and cannot "
57                                         + "be parsed.");
58                     }
59                     break;
60                 case "Bitrate kbps":
61                     bitrates.add(Double.parseDouble(value));
62                     break;
63                 case "VMAF score":
64                     vmafs.add(Double.parseDouble(value));
65                     break;
66                 default:
67                     // Skip any unknown key/value pairs.
68             }
69         }
70 
71         if (bitrates.size() != vmafs.size()) {
72             throw new IllegalArgumentException(
73                     "Test result data did not have a matching number of bitrate and vmaf "
74                             + "values.");
75         }
76 
77         RateDistortionCurve.Builder curveBuilder = RateDistortionCurve.builder();
78         for (int i = 0; i < bitrates.size(); i++) {
79             curveBuilder.addPoint(RateDistortionPoint.create(bitrates.get(i), vmafs.get(i)));
80         }
81 
82         if (referenceFile.endsWith(".y4m")) {
83             referenceFile = referenceFile.substring(0, referenceFile.length() - 4);
84         }
85 
86         return new AutoValue_VeqTestResult(referenceFile, curveBuilder.build());
87     }
88 }
89