1/*
2 * Copyright (C) 2011 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
18package android.filterpacks.base;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.Frame;
23import android.filterfw.core.FrameFormat;
24import android.filterfw.core.GenerateFieldPort;
25import android.filterfw.core.GenerateFinalPort;
26import android.filterfw.core.KeyValueMap;
27import android.filterfw.core.MutableFrameFormat;
28
29/**
30 * @hide
31 */
32public class RetargetFilter extends Filter {
33
34    @GenerateFinalPort(name = "target", hasDefault = false)
35    private String mTargetString;
36
37    private MutableFrameFormat mOutputFormat;
38    private int mTarget = -1;
39
40    public RetargetFilter(String name) {
41        super(name);
42    }
43
44    @Override
45    public void setupPorts() {
46        // Setup target
47        mTarget = FrameFormat.readTargetString(mTargetString);
48
49        // Add ports
50        addInputPort("frame");
51        addOutputBasedOnInput("frame", "frame");
52    }
53
54    @Override
55    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
56        MutableFrameFormat retargeted = inputFormat.mutableCopy();
57        retargeted.setTarget(mTarget);
58        return retargeted;
59    }
60
61    @Override
62    public void process(FilterContext context) {
63        // Get input frame
64        Frame input = pullInput("frame");
65
66        // Create output frame
67        Frame output = context.getFrameManager().duplicateFrameToTarget(input, mTarget);
68
69        // Push output
70        pushOutput("frame", output);
71
72        // Release pushed frame
73        output.release();
74    }
75
76}
77