Skip to content

Commit 8c3d662

Browse files
committed
Add QFormatOrFloatingPoint proto.
- In the future, we can use this at the proto level for fields that would read better as floating point (b/391851526). - Named `q7_dot8` to comply with [proto style on underscores](https://protobuf.dev/programming-guides/style/). PiperOrigin-RevId: 856764867
1 parent a56292d commit 8c3d662

File tree

9 files changed

+126
-7
lines changed

9 files changed

+126
-7
lines changed

iamf/cli/proto/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ proto_library(
9999
srcs = ["test_vector_metadata.proto"],
100100
)
101101

102+
proto_library(
103+
name = "types_proto",
104+
srcs = ["types.proto"],
105+
)
106+
102107
proto_library(
103108
name = "user_metadata_proto",
104109
srcs = ["user_metadata.proto"],
@@ -186,6 +191,11 @@ cc_proto_library(
186191
deps = [":test_vector_metadata_proto"],
187192
)
188193

194+
cc_proto_library(
195+
name = "types_cc_proto",
196+
deps = [":types_proto"],
197+
)
198+
189199
cc_proto_library(
190200
name = "user_metadata_cc_proto",
191201
deps = [":user_metadata_proto"],

iamf/cli/proto/types.proto

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// Copyright (c) 2026, Alliance for Open Media. All rights reserved
3+
//
4+
// This source code is subject to the terms of the BSD 3-Clause Clear License
5+
// and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6+
// License was not distributed with this source code in the LICENSE file, you
7+
// can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8+
// Alliance for Open Media Patent License 1.0 was not distributed with this
9+
// source code in the PATENTS file, you can obtain it at
10+
// www.aomedia.org/license/patent.
11+
12+
edition = "2023";
13+
14+
package iamf_tools_cli_proto;
15+
16+
option java_outer_classname = "Types";
17+
option java_multiple_files = true;
18+
19+
// For use when it could be convenient to represent the same logical field as
20+
// either floating point or Q-format in different contexts.
21+
message QFormatOrFloatingPoint {
22+
oneof value {
23+
float floating_point = 1;
24+
int32 q7_dot8 = 2;
25+
// TODO: b/391851526 - Add Q8.0 format.
26+
}
27+
}

iamf/cli/proto_conversion/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ cc_library(
130130
"//iamf/cli/proto:param_definitions_cc_proto",
131131
"//iamf/cli/proto:parameter_data_cc_proto",
132132
"//iamf/cli/proto:test_vector_metadata_cc_proto",
133+
"//iamf/cli/proto:types_cc_proto",
133134
"//iamf/common:leb_generator",
135+
"//iamf/common:q_format_or_floating_point",
134136
"//iamf/common/utils:macros",
135137
"//iamf/common/utils:map_utils",
136138
"//iamf/common/utils:numeric_utils",
@@ -140,6 +142,7 @@ cc_library(
140142
"//iamf/obu/param_definitions:param_definition_base",
141143
"@abseil-cpp//absl/log:absl_log",
142144
"@abseil-cpp//absl/status",
145+
"@abseil-cpp//absl/status:statusor",
143146
"@abseil-cpp//absl/strings:string_view",
144147
],
145148
)

iamf/cli/proto_conversion/proto_utils.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
#include "absl/log/absl_log.h"
2222
#include "absl/status/status.h"
23+
#include "absl/status/statusor.h"
2324
#include "absl/strings/string_view.h"
2425
#include "iamf/cli/proto/obu_header.pb.h"
2526
#include "iamf/cli/proto/param_definitions.pb.h"
2627
#include "iamf/cli/proto/parameter_data.pb.h"
28+
#include "iamf/cli/proto/types.pb.h"
2729
#include "iamf/cli/proto_conversion/lookup_tables.h"
2830
#include "iamf/common/leb_generator.h"
31+
#include "iamf/common/q_format_or_floating_point.h"
2932
#include "iamf/common/utils/macros.h"
3033
#include "iamf/common/utils/map_utils.h"
3134
#include "iamf/common/utils/numeric_utils.h"
@@ -36,6 +39,24 @@
3639

3740
namespace iamf_tools {
3841

42+
absl::StatusOr<QFormatOrFloatingPoint> ProtoToQFormatOrFloatingPoint(
43+
const iamf_tools_cli_proto::QFormatOrFloatingPoint&
44+
input_q_format_or_floating_point) {
45+
if (input_q_format_or_floating_point.has_q7_dot8()) {
46+
int16_t q7_8_int16;
47+
RETURN_IF_NOT_OK(StaticCastIfInRange(
48+
"QFormatOrFloatingPoint.q7_dot8",
49+
input_q_format_or_floating_point.q7_dot8(), q7_8_int16));
50+
return QFormatOrFloatingPoint::MakeFromQ7_8(q7_8_int16);
51+
} else if (input_q_format_or_floating_point.has_floating_point()) {
52+
return QFormatOrFloatingPoint::CreateFromFloatingPoint(
53+
input_q_format_or_floating_point.floating_point());
54+
} else {
55+
// By default, return a safe 0.
56+
return QFormatOrFloatingPoint::MakeFromQ7_8(0);
57+
}
58+
}
59+
3960
absl::Status CopyParamDefinition(
4061
const iamf_tools_cli_proto::ParamDefinition& input_param_definition,
4162
ParamDefinition& param_definition) {

iamf/cli/proto_conversion/proto_utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,30 @@
1616
#include <memory>
1717

1818
#include "absl/status/status.h"
19+
#include "absl/status/statusor.h"
1920
#include "iamf/cli/proto/obu_header.pb.h"
2021
#include "iamf/cli/proto/param_definitions.pb.h"
2122
#include "iamf/cli/proto/parameter_data.pb.h"
2223
#include "iamf/cli/proto/test_vector_metadata.pb.h"
24+
#include "iamf/cli/proto/types.pb.h"
2325
#include "iamf/common/leb_generator.h"
26+
#include "iamf/common/q_format_or_floating_point.h"
2427
#include "iamf/obu/demixing_info_parameter_data.h"
2528
#include "iamf/obu/obu_header.h"
2629
#include "iamf/obu/param_definitions/param_definition_base.h"
2730

2831
namespace iamf_tools {
2932

33+
/*!\brief Converts to the internal representation of the input protocol buffer.
34+
*
35+
* \param q_format_or_floating_point Input protocol buffer.
36+
* \return Internal representation of a `QFormatOrFloatingPoint` on success. A
37+
* specific status on failure.
38+
*/
39+
absl::StatusOr<QFormatOrFloatingPoint> ProtoToQFormatOrFloatingPoint(
40+
const iamf_tools_cli_proto::QFormatOrFloatingPoint&
41+
input_q_format_or_floating_point);
42+
3043
/*!\brief Copies param definitions from the corresponding protocol buffer.
3144
*
3245
* \param input_param_definition Input protocol buffer.

iamf/cli/proto_conversion/tests/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ cc_test(
6969
srcs = ["proto_utils_test.cc"],
7070
deps = [
7171
"//iamf/cli/proto:obu_header_cc_proto",
72+
"//iamf/cli/proto:param_definitions_cc_proto",
7273
"//iamf/cli/proto:parameter_data_cc_proto",
7374
"//iamf/cli/proto_conversion:proto_utils",
7475
"//iamf/common:leb_generator",
7576
"//iamf/obu:obu_header",
7677
"//iamf/obu:parameter_data",
7778
"//iamf/obu/param_definitions:mix_gain_param_definition",
78-
"@abseil-cpp//absl/status:status_matchers",
7979
"@com_google_googletest//:gtest_main",
8080
"@com_google_protobuf//:protobuf",
8181
],

iamf/cli/proto_conversion/tests/proto_utils_test.cc

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
#include <memory>
1616
#include <vector>
1717

18-
#include "absl/status/status_matchers.h"
1918
#include "gmock/gmock.h"
2019
#include "gtest/gtest.h"
2120
#include "iamf/cli/proto/obu_header.pb.h"
21+
#include "iamf/cli/proto/param_definitions.pb.h"
2222
#include "iamf/cli/proto/parameter_data.pb.h"
2323
#include "iamf/common/leb_generator.h"
2424
#include "iamf/obu/demixing_info_parameter_data.h"
@@ -30,6 +30,51 @@ namespace iamf_tools {
3030
namespace {
3131

3232
using ::absl_testing::IsOk;
33+
using ::testing::FloatNear;
34+
using ::testing::Not;
35+
36+
TEST(GetQ7_8FromProto, Q7_8) {
37+
iamf_tools_cli_proto::QFormatOrFloatingPoint q_format_or_floating_point;
38+
q_format_or_floating_point.set_q7_dot8(256);
39+
auto converted = ProtoToQFormatOrFloatingPoint(q_format_or_floating_point);
40+
41+
ASSERT_THAT(converted, IsOk());
42+
EXPECT_EQ(converted->GetQ7_8(), 256);
43+
}
44+
45+
TEST(GetQ7_8FromProto, Float) {
46+
iamf_tools_cli_proto::QFormatOrFloatingPoint q_format_or_floating_point;
47+
q_format_or_floating_point.set_floating_point(1.5);
48+
auto converted = ProtoToQFormatOrFloatingPoint(q_format_or_floating_point);
49+
50+
ASSERT_THAT(converted, IsOk());
51+
EXPECT_THAT(converted->GetFloatingPoint(), FloatNear(1.5, 1e-6));
52+
}
53+
54+
TEST(GetQ7_8FromProto, NeitherSetDefaultsToZero) {
55+
iamf_tools_cli_proto::QFormatOrFloatingPoint q_format_or_floating_point;
56+
57+
auto converted = ProtoToQFormatOrFloatingPoint(q_format_or_floating_point);
58+
59+
ASSERT_THAT(converted, IsOk());
60+
EXPECT_EQ(converted->GetQ7_8(), 0);
61+
}
62+
63+
TEST(GetQ7_8FromProto, Q7_8OutOfRangeReturnsError) {
64+
iamf_tools_cli_proto::QFormatOrFloatingPoint q_format_or_floating_point;
65+
q_format_or_floating_point.set_q7_dot8(65536);
66+
67+
EXPECT_THAT(ProtoToQFormatOrFloatingPoint(q_format_or_floating_point),
68+
Not(IsOk()));
69+
}
70+
71+
TEST(GetQ7_8FromProto, FloatOutOfRangeReturnsError) {
72+
iamf_tools_cli_proto::QFormatOrFloatingPoint q_format_or_floating_point;
73+
q_format_or_floating_point.set_floating_point(1000.0);
74+
75+
EXPECT_THAT(ProtoToQFormatOrFloatingPoint(q_format_or_floating_point),
76+
Not(IsOk()));
77+
}
3378

3479
TEST(CopyDemixingInfoParameterData, Basic) {
3580
iamf_tools_cli_proto::DemixingInfoParameterData

iamf/common/q_format_or_floating_point.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ QFormatOrFloatingPoint::CreateFromFloatingPoint(float value) {
3131
return QFormatOrFloatingPoint(q7_8);
3232
}
3333

34-
QFormatOrFloatingPoint QFormatOrFloatingPoint::MakeFromQ7_8(int16_t q78) {
35-
return QFormatOrFloatingPoint(q78);
34+
QFormatOrFloatingPoint QFormatOrFloatingPoint::MakeFromQ7_8(int16_t q7_8) {
35+
return QFormatOrFloatingPoint(q7_8);
3636
}
3737

3838
QFormatOrFloatingPoint::QFormatOrFloatingPoint(int16_t q7_8)

iamf/common/q_format_or_floating_point.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class QFormatOrFloatingPoint {
3232
public:
3333
/*!\brief Creates an instance from a Q7.8 value.
3434
*
35-
* \param q78 Q7.8 value to initialize from.
36-
* \return `QFormatOrFloatingPoint` created from `q78`.
35+
* \param q7_8 Q7.8 value to initialize from.
36+
* \return `QFormatOrFloatingPoint` created from `q7_8`.
3737
*/
38-
static QFormatOrFloatingPoint MakeFromQ7_8(int16_t q78);
38+
static QFormatOrFloatingPoint MakeFromQ7_8(int16_t q7_8);
3939

4040
/*!\brief Creates an instance from a floating point value.
4141
*

0 commit comments

Comments
 (0)