Skip to content

Commit a2a63e3

Browse files
MediaPipe Teamjqtang
MediaPipe Team
authored and
jqtang
committed
Project import generated by Copybara.
GitOrigin-RevId: 796203faee20d7aae2876aac8ca5a1827dee4fe3
1 parent 412ab42 commit a2a63e3

File tree

122 files changed

+7340
-2026
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+7340
-2026
lines changed

Diff for: WORKSPACE

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ http_archive(
6464
sha256 = "267103f8a1e9578978aa1dc256001e6529ef593e5aea38193d31c2872ee025e8",
6565
strip_prefix = "glog-0.3.5",
6666
build_file = "@//third_party:glog.BUILD",
67+
patches = [
68+
"@//third_party:com_github_glog_glog_9779e5ea6ef59562b030248947f787d1256132ae.diff"
69+
],
70+
patch_args = [
71+
"-p1",
72+
],
6773
)
6874

6975
# libyuv

Diff for: mediapipe/calculators/audio/audio_decoder_calculator.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class AudioDecoderCalculator : public CalculatorBase {
6161
::mediapipe::Status AudioDecoderCalculator::GetContract(
6262
CalculatorContract* cc) {
6363
cc->InputSidePackets().Tag("INPUT_FILE_PATH").Set<std::string>();
64-
64+
if (cc->InputSidePackets().HasTag("OPTIONS")) {
65+
cc->InputSidePackets().Tag("OPTIONS").Set<mediapipe::AudioDecoderOptions>();
66+
}
6567
cc->Outputs().Tag("AUDIO").Set<Matrix>();
6668
if (cc->Outputs().HasTag("AUDIO_HEADER")) {
6769
cc->Outputs().Tag("AUDIO_HEADER").SetNone();
@@ -72,7 +74,9 @@ ::mediapipe::Status AudioDecoderCalculator::GetContract(
7274
::mediapipe::Status AudioDecoderCalculator::Open(CalculatorContext* cc) {
7375
const std::string& input_file_path =
7476
cc->InputSidePackets().Tag("INPUT_FILE_PATH").Get<std::string>();
75-
const auto& decoder_options = cc->Options<mediapipe::AudioDecoderOptions>();
77+
const auto& decoder_options =
78+
tool::RetrieveOptions(cc->Options<mediapipe::AudioDecoderOptions>(),
79+
cc->InputSidePackets(), "OPTIONS");
7680
decoder_ = absl::make_unique<AudioDecoder>();
7781
MP_RETURN_IF_ERROR(decoder_->Initialize(input_file_path, decoder_options));
7882
std::unique_ptr<mediapipe::TimeSeriesHeader> header =

Diff for: mediapipe/calculators/audio/stabilized_log_calculator.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ class StabilizedLogCalculator : public CalculatorBase {
7575

7676
::mediapipe::Status Process(CalculatorContext* cc) override {
7777
auto input_matrix = cc->Inputs().Index(0).Get<Matrix>();
78+
if (input_matrix.array().isNaN().any()) {
79+
return ::mediapipe::InvalidArgumentError("NaN input to log operation.");
80+
}
7881
if (check_nonnegativity_) {
79-
CHECK_GE(input_matrix.minCoeff(), 0);
82+
if (input_matrix.minCoeff() < 0.0) {
83+
return ::mediapipe::OutOfRangeError("Negative input to log operation.");
84+
}
8085
}
8186
std::unique_ptr<Matrix> output_frame(new Matrix(
8287
output_scale_ * (input_matrix.array() + stabilizer_).log().matrix()));

Diff for: mediapipe/calculators/audio/stabilized_log_calculator_test.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#include <cmath>
1415

1516
#include "Eigen/Core"
1617
#include "mediapipe/calculators/audio/stabilized_log_calculator.pb.h"
@@ -108,13 +109,22 @@ TEST_F(StabilizedLogCalculatorTest, ZerosAreStabilized) {
108109
runner_->Outputs().Index(0).packets[0].Get<Matrix>());
109110
}
110111

111-
TEST_F(StabilizedLogCalculatorTest, NegativeValuesCheckFail) {
112+
TEST_F(StabilizedLogCalculatorTest, NanValuesReturnError) {
113+
InitializeGraph();
114+
FillInputHeader();
115+
AppendInputPacket(
116+
new Matrix(Matrix::Constant(kNumChannels, kNumSamples, std::nanf(""))),
117+
0 /* timestamp */);
118+
ASSERT_FALSE(RunGraph().ok());
119+
}
120+
121+
TEST_F(StabilizedLogCalculatorTest, NegativeValuesReturnError) {
112122
InitializeGraph();
113123
FillInputHeader();
114124
AppendInputPacket(
115125
new Matrix(Matrix::Constant(kNumChannels, kNumSamples, -1.0)),
116126
0 /* timestamp */);
117-
ASSERT_DEATH(RunGraphNoReturn(), "");
127+
ASSERT_FALSE(RunGraph().ok());
118128
}
119129

120130
TEST_F(StabilizedLogCalculatorTest, NegativeValuesDoNotCheckFailIfCheckIsOff) {

Diff for: mediapipe/calculators/audio/time_series_framer_calculator.cc

+40-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ namespace mediapipe {
5656
// If pad_final_packet is true, all input samples will be emitted and the final
5757
// packet will be zero padded as necessary. If pad_final_packet is false, some
5858
// samples may be dropped at the end of the stream.
59+
//
60+
// If use_local_timestamp is true, the output packet's timestamp is based on the
61+
// last sample of the packet. The timestamp of this sample is inferred by
62+
// input_packet_timesamp + local_sample_index / sampling_rate_. If false, the
63+
// output packet's timestamp is based on the cumulative timestamping, which is
64+
// done by adopting the timestamp of the first sample of the packet and this
65+
// sample's timestamp is inferred by initial_input_timestamp_ +
66+
// cumulative_completed_samples / sample_rate_.
5967
class TimeSeriesFramerCalculator : public CalculatorBase {
6068
public:
6169
static ::mediapipe::Status GetContract(CalculatorContract* cc) {
@@ -86,11 +94,26 @@ class TimeSeriesFramerCalculator : public CalculatorBase {
8694
void FrameOutput(CalculatorContext* cc);
8795

8896
Timestamp CurrentOutputTimestamp() {
97+
if (use_local_timestamp_) {
98+
return current_timestamp_;
99+
}
100+
return CumulativeOutputTimestamp();
101+
}
102+
103+
Timestamp CumulativeOutputTimestamp() {
89104
return initial_input_timestamp_ +
90105
round(cumulative_completed_samples_ / sample_rate_ *
91106
Timestamp::kTimestampUnitsPerSecond);
92107
}
93108

109+
// Returns the timestamp of a sample on a base, which is usually the time
110+
// stamp of a packet.
111+
Timestamp CurrentSampleTimestamp(const Timestamp& timestamp_base,
112+
int64 number_of_samples) {
113+
return timestamp_base + round(number_of_samples / sample_rate_ *
114+
Timestamp::kTimestampUnitsPerSecond);
115+
}
116+
94117
// The number of input samples to advance after the current output frame is
95118
// emitted.
96119
int next_frame_step_samples() const {
@@ -118,22 +141,27 @@ class TimeSeriesFramerCalculator : public CalculatorBase {
118141
// any overlap).
119142
int64 cumulative_completed_samples_;
120143
Timestamp initial_input_timestamp_;
144+
// The current timestamp is updated along with the incoming packets.
145+
Timestamp current_timestamp_;
121146
int num_channels_;
122147

123148
// Each entry in this deque consists of a single sample, i.e. a
124-
// single column vector.
125-
std::deque<Matrix> sample_buffer_;
149+
// single column vector, and its timestamp.
150+
std::deque<std::pair<Matrix, Timestamp>> sample_buffer_;
126151

127152
bool use_window_;
128153
Matrix window_;
154+
155+
bool use_local_timestamp_;
129156
};
130157
REGISTER_CALCULATOR(TimeSeriesFramerCalculator);
131158

132159
void TimeSeriesFramerCalculator::EnqueueInput(CalculatorContext* cc) {
133160
const Matrix& input_frame = cc->Inputs().Index(0).Get<Matrix>();
134161

135162
for (int i = 0; i < input_frame.cols(); ++i) {
136-
sample_buffer_.emplace_back(input_frame.col(i));
163+
sample_buffer_.emplace_back(std::make_pair(
164+
input_frame.col(i), CurrentSampleTimestamp(cc->InputTimestamp(), i)));
137165
}
138166

139167
cumulative_input_samples_ += input_frame.cols();
@@ -151,14 +179,16 @@ void TimeSeriesFramerCalculator::FrameOutput(CalculatorContext* cc) {
151179
new Matrix(num_channels_, frame_duration_samples_));
152180
for (int i = 0; i < std::min(frame_step_samples, frame_duration_samples_);
153181
++i) {
154-
output_frame->col(i) = sample_buffer_.front();
182+
output_frame->col(i) = sample_buffer_.front().first;
183+
current_timestamp_ = sample_buffer_.front().second;
155184
sample_buffer_.pop_front();
156185
}
157186
const int frame_overlap_samples =
158187
frame_duration_samples_ - frame_step_samples;
159188
if (frame_overlap_samples > 0) {
160189
for (int i = 0; i < frame_overlap_samples; ++i) {
161-
output_frame->col(i + frame_step_samples) = sample_buffer_[i];
190+
output_frame->col(i + frame_step_samples) = sample_buffer_[i].first;
191+
current_timestamp_ = sample_buffer_[i].second;
162192
}
163193
} else {
164194
samples_still_to_drop_ = -frame_overlap_samples;
@@ -178,6 +208,7 @@ void TimeSeriesFramerCalculator::FrameOutput(CalculatorContext* cc) {
178208
::mediapipe::Status TimeSeriesFramerCalculator::Process(CalculatorContext* cc) {
179209
if (initial_input_timestamp_ == Timestamp::Unstarted()) {
180210
initial_input_timestamp_ = cc->InputTimestamp();
211+
current_timestamp_ = initial_input_timestamp_;
181212
}
182213

183214
EnqueueInput(cc);
@@ -195,7 +226,8 @@ ::mediapipe::Status TimeSeriesFramerCalculator::Close(CalculatorContext* cc) {
195226
std::unique_ptr<Matrix> output_frame(new Matrix);
196227
output_frame->setZero(num_channels_, frame_duration_samples_);
197228
for (int i = 0; i < sample_buffer_.size(); ++i) {
198-
output_frame->col(i) = sample_buffer_[i];
229+
output_frame->col(i) = sample_buffer_[i].first;
230+
current_timestamp_ = sample_buffer_[i].second;
199231
}
200232

201233
cc->Outputs().Index(0).Add(output_frame.release(),
@@ -258,6 +290,7 @@ ::mediapipe::Status TimeSeriesFramerCalculator::Open(CalculatorContext* cc) {
258290
cumulative_output_frames_ = 0;
259291
samples_still_to_drop_ = 0;
260292
initial_input_timestamp_ = Timestamp::Unstarted();
293+
current_timestamp_ = Timestamp::Unstarted();
261294

262295
std::vector<double> window_vector;
263296
use_window_ = false;
@@ -282,6 +315,7 @@ ::mediapipe::Status TimeSeriesFramerCalculator::Open(CalculatorContext* cc) {
282315
frame_duration_samples_)
283316
.cast<float>();
284317
}
318+
use_local_timestamp_ = framer_options.use_local_timestamp();
285319

286320
return ::mediapipe::OkStatus();
287321
}

Diff for: mediapipe/calculators/audio/time_series_framer_calculator.proto

+7
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ message TimeSeriesFramerCalculatorOptions {
6262
HANN = 2;
6363
}
6464
optional WindowFunction window_function = 4 [default = NONE];
65+
66+
// If use_local_timestamp is true, the output packet's timestamp is based on
67+
// the last sample of the packet and it's inferred from the latest input
68+
// packet's timestamp. If false, the output packet's timestamp is based on
69+
// the cumulative timestamping, which is inferred from the intial input
70+
// timestamp and the cumulative number of samples.
71+
optional bool use_local_timestamp = 6 [default = false];
6572
}

Diff for: mediapipe/calculators/audio/time_series_framer_calculator_test.cc

+91-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace mediapipe {
3535
namespace {
3636

3737
const int kInitialTimestampOffsetMicroseconds = 4;
38+
const int kGapBetweenPacketsInSeconds = 1;
39+
const int kUniversalInputPacketSize = 50;
3840

3941
class TimeSeriesFramerCalculatorTest
4042
: public TimeSeriesCalculatorTest<TimeSeriesFramerCalculatorOptions> {
@@ -391,5 +393,93 @@ TEST_F(TimeSeriesFramerCalculatorWindowingSanityTest, HannWindowSanityCheck) {
391393
RunAndTestSinglePacketAverage(0.5f);
392394
}
393395

394-
} // anonymous namespace
396+
// A simple test class that checks the local packet time stamp. This class
397+
// generate a series of packets with and without gaps between packets and tests
398+
// the behavior with cumulative timestamping and local packet timestamping.
399+
class TimeSeriesFramerCalculatorTimestampingTest
400+
: public TimeSeriesFramerCalculatorTest {
401+
protected:
402+
// Creates test input and saves a reference copy.
403+
void InitializeInputForTimeStampingTest() {
404+
concatenated_input_samples_.resize(0, num_input_channels_);
405+
num_input_samples_ = 0;
406+
for (int i = 0; i < 10; ++i) {
407+
// This range of packet sizes was chosen such that some input
408+
// packets will be smaller than the output packet size and other
409+
// input packets will be larger.
410+
int packet_size = kUniversalInputPacketSize;
411+
double timestamp_seconds = kInitialTimestampOffsetMicroseconds * 1.0e-6 +
412+
num_input_samples_ / input_sample_rate_;
413+
if (options_.use_local_timestamp()) {
414+
timestamp_seconds += kGapBetweenPacketsInSeconds * i;
415+
}
416+
417+
Matrix* data_frame =
418+
NewTestFrame(num_input_channels_, packet_size, timestamp_seconds);
419+
420+
AppendInputPacket(data_frame, round(timestamp_seconds *
421+
Timestamp::kTimestampUnitsPerSecond));
422+
num_input_samples_ += packet_size;
423+
}
424+
}
425+
426+
void CheckOutputTimestamps() {
427+
int num_full_packets = output().packets.size();
428+
if (options_.pad_final_packet()) {
429+
num_full_packets -= 1;
430+
}
431+
432+
int64 num_samples = 0;
433+
for (int packet_num = 0; packet_num < num_full_packets; ++packet_num) {
434+
const Packet& packet = output().packets[packet_num];
435+
num_samples += FrameDurationSamples();
436+
double expected_timestamp =
437+
options_.use_local_timestamp()
438+
? GetExpectedLocalTimestampForSample(num_samples - 1)
439+
: GetExpectedCumulativeTimestamp(num_samples - 1);
440+
ASSERT_NEAR(packet.Timestamp().Seconds(), expected_timestamp, 1e-10);
441+
}
442+
}
443+
444+
::mediapipe::Status RunTimestampTest() {
445+
InitializeGraph();
446+
InitializeInputForTimeStampingTest();
447+
FillInputHeader();
448+
return RunGraph();
449+
}
450+
451+
private:
452+
// Returns the timestamp in seconds based on local timestamping.
453+
double GetExpectedLocalTimestampForSample(int sample_index) {
454+
return kInitialTimestampOffsetMicroseconds * 1.0e-6 +
455+
sample_index / input_sample_rate_ +
456+
(sample_index / kUniversalInputPacketSize) *
457+
kGapBetweenPacketsInSeconds;
458+
}
459+
460+
// Returns the timestamp inseconds based on cumulative timestamping.
461+
double GetExpectedCumulativeTimestamp(int sample_index) {
462+
return kInitialTimestampOffsetMicroseconds * 1.0e-6 +
463+
sample_index / FrameDurationSamples() * FrameDurationSamples() /
464+
input_sample_rate_;
465+
}
466+
};
467+
468+
TEST_F(TimeSeriesFramerCalculatorTimestampingTest, UseLocalTimeStamp) {
469+
options_.set_frame_duration_seconds(100.0 / input_sample_rate_);
470+
options_.set_use_local_timestamp(true);
471+
472+
MP_ASSERT_OK(RunTimestampTest());
473+
CheckOutputTimestamps();
474+
}
475+
476+
TEST_F(TimeSeriesFramerCalculatorTimestampingTest, UseCumulativeTimeStamp) {
477+
options_.set_frame_duration_seconds(100.0 / input_sample_rate_);
478+
options_.set_use_local_timestamp(false);
479+
480+
MP_ASSERT_OK(RunTimestampTest());
481+
CheckOutputTimestamps();
482+
}
483+
484+
} // namespace
395485
} // namespace mediapipe

Diff for: mediapipe/calculators/core/BUILD

+7-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ cc_library(
166166
"//mediapipe/framework/port:ret_check",
167167
"//mediapipe/framework/port:status",
168168
"@org_tensorflow//tensorflow/lite:framework",
169-
],
169+
] + select({
170+
"//mediapipe/gpu:disable_gpu": [],
171+
"//mediapipe:ios": [],
172+
"//conditions:default": [
173+
"@org_tensorflow//tensorflow/lite/delegates/gpu/gl:gl_buffer",
174+
],
175+
}),
170176
alwayslink = 1,
171177
)
172178

Diff for: mediapipe/calculators/core/concatenate_vector_calculator.cc

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "mediapipe/framework/formats/landmark.pb.h"
2020
#include "tensorflow/lite/interpreter.h"
2121

22+
#if !defined(MEDIAPIPE_DISABLE_GPU) && !defined(__APPLE__)
23+
#include "tensorflow/lite/delegates/gpu/gl/gl_buffer.h"
24+
#endif // !MEDIAPIPE_DISABLE_GPU
25+
2226
namespace mediapipe {
2327

2428
// Example config:
@@ -45,4 +49,11 @@ REGISTER_CALCULATOR(ConcatenateTfLiteTensorVectorCalculator);
4549
typedef ConcatenateVectorCalculator<::mediapipe::NormalizedLandmark>
4650
ConcatenateLandmarkVectorCalculator;
4751
REGISTER_CALCULATOR(ConcatenateLandmarkVectorCalculator);
52+
53+
#if !defined(MEDIAPIPE_DISABLE_GPU) && !defined(__APPLE__)
54+
typedef ConcatenateVectorCalculator<::tflite::gpu::gl::GlBuffer>
55+
ConcatenateGlBufferVectorCalculator;
56+
REGISTER_CALCULATOR(ConcatenateGlBufferVectorCalculator);
57+
#endif
58+
4859
} // namespace mediapipe

0 commit comments

Comments
 (0)