1 /* 2 * Copyright 2013 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 // Takes sharpness scores in RT and averages them over time 17 18 package androidx.media.filterfw.samples.simplecamera; 19 20 import android.util.Log; 21 import androidx.media.filterfw.Filter; 22 import androidx.media.filterfw.FrameType; 23 import androidx.media.filterfw.FrameValue; 24 import androidx.media.filterfw.MffContext; 25 import androidx.media.filterfw.OutputPort; 26 import androidx.media.filterfw.Signature; 27 28 public class AverageFilter extends Filter { 29 30 private static final String TAG = "AverageFilter"; 31 private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE); 32 33 private static final int NUM_FRAMES = 5; 34 private int counter = 0; 35 private float[] temp = new float[NUM_FRAMES]; 36 37 /** 38 * @param context 39 * @param name 40 */ 41 public AverageFilter(MffContext context, String name) { 42 super(context, name); 43 } 44 45 @Override 46 public Signature getSignature() { 47 FrameType floatT = FrameType.single(float.class); 48 return new Signature() 49 .addInputPort("sharpness", Signature.PORT_REQUIRED, floatT) 50 .addOutputPort("avg", Signature.PORT_REQUIRED, floatT) 51 .disallowOtherPorts(); 52 } 53 54 @Override 55 protected void onProcess() { 56 FrameValue inFrameValue = getConnectedInputPort("sharpness").pullFrame().asFrameValue(); 57 if (counter < NUM_FRAMES && counter >= 0) { 58 temp[counter] = ((Float)inFrameValue.getValue()).floatValue(); 59 } 60 61 counter = (counter + 1) % NUM_FRAMES; 62 63 float output = (temp[0] + temp[1] + temp[2] + temp[3] + temp[4]) / NUM_FRAMES; 64 if (mLogVerbose) Log.v(TAG, "Avg= " + output + "temp1= " + temp[0] + "temp2= " + 65 temp[1] + "temp3= " + temp[2] + "temp4=" + temp[3] + "temp5=" + temp[4]); 66 67 OutputPort outPort = getConnectedOutputPort("avg"); 68 FrameValue outFrame = outPort.fetchAvailableFrame(null).asFrameValue(); 69 outFrame.setValue(output); 70 outPort.pushFrame(outFrame); 71 } 72 } 73