• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 androidx.media.filterpacks.numeric;
18 
19 import android.util.Log;
20 import androidx.media.filterfw.Filter;
21 import androidx.media.filterfw.FrameType;
22 import androidx.media.filterfw.FrameValue;
23 import androidx.media.filterfw.MffContext;
24 import androidx.media.filterfw.OutputPort;
25 import androidx.media.filterfw.Signature;
26 
27 /**
28  * Filter to calculate the 2-norm of the inputs. i.e. sqrt(x^2 + y^2)
29  * TODO: Add support for more norms in the future.
30  */
31 public final class NormFilter extends Filter {
32    private static final String TAG = "NormFilter";
33    private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
34 
NormFilter(MffContext context, String name)35    public NormFilter(MffContext context, String name) {
36        super(context, name);
37    }
38 
39    @Override
getSignature()40    public Signature getSignature() {
41        FrameType floatT = FrameType.single(float.class);
42        return new Signature()
43            .addInputPort("x", Signature.PORT_REQUIRED, floatT)
44            .addInputPort("y", Signature.PORT_REQUIRED, floatT)
45            .addOutputPort("norm", Signature.PORT_REQUIRED, floatT)
46            .disallowOtherPorts();
47    }
48 
49    @Override
onProcess()50    protected void onProcess() {
51      FrameValue xFrameValue = getConnectedInputPort("x").pullFrame().asFrameValue();
52      float xValue = ((Float)xFrameValue.getValue()).floatValue();
53      FrameValue yFrameValue = getConnectedInputPort("y").pullFrame().asFrameValue();
54      float yValue = ((Float)yFrameValue.getValue()).floatValue();
55 
56      float norm = (float) Math.hypot(xValue, yValue);
57      if (mLogVerbose) Log.v(TAG, "Norm = " + norm);
58      OutputPort outPort = getConnectedOutputPort("norm");
59      FrameValue outFrame = outPort.fetchAvailableFrame(null).asFrameValue();
60      outFrame.setValue(norm);
61      outPort.pushFrame(outFrame);
62    }
63 }
64