View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.videosegmenter.ffmpeg;
23  
24  import org.opencastproject.metadata.mpeg7.Mpeg7Catalog;
25  import org.opencastproject.metadata.mpeg7.Segment;
26  
27  import java.util.LinkedList;
28  
29  /**
30   * An optimization step is one step in the optimization of the number of segments.
31   * This class stores parameters of such an optimization step and calculates error and
32   * absolute error of optimization
33   *
34   */
35  public class OptimizationStep implements Comparable<OptimizationStep> {
36  
37    private float changesThreshold;
38    private float error;
39    private float errorAbs;
40    private int segmentNum;
41    private int prefNum;
42    private Mpeg7Catalog mpeg7;
43    private LinkedList<Segment> segments;
44  
45    /**
46     * creates a new optimization step with given parameters
47     *
48     * @param changesThreshold
49     * @param segNum
50     * @param prefNum
51     * @param mpeg7
52     * @param segments unfiltered list of segments
53     */
54    public OptimizationStep(float changesThreshold, int segNum, int prefNum, Mpeg7Catalog mpeg7,
55            LinkedList<Segment> segments) {
56      this.changesThreshold = changesThreshold;
57      this.segmentNum = segNum;
58      this.prefNum = prefNum;
59      this.mpeg7 = mpeg7;
60      this.segments = segments;
61      calcErrors();
62    }
63  
64    /**
65     *  creates a new optimization step with default values
66     */
67    public OptimizationStep() {
68      changesThreshold = 0.0f;
69      segmentNum = 1;
70      prefNum = 1;
71      mpeg7 = null;
72      segments = null;
73      calcErrors();
74    }
75  
76    /**
77     * calculate error of optimization and absolute error of optimization
78     */
79    private void calcErrors() {
80      error = (float)(segmentNum - prefNum) / (float)prefNum;
81      errorAbs = Math.abs(error);
82    }
83  
84    /**
85     * get changesThreshold
86     *
87     * @return changesThreshold
88     */
89    public float getChangesThreshold() {
90      return changesThreshold;
91    }
92  
93    /**
94     * get error of optimization
95     *
96     * @return error error of optimization
97     */
98    public float getError() {
99      return error;
100   }
101 
102   /**
103    * get absolute error
104    *
105    * @return errorAbs absolute error
106    */
107   public float getErrorAbs() {
108     return errorAbs;
109   }
110 
111   /**
112    * get number of segments
113    *
114    * @return segmentNum number of segments
115    */
116   public int getSegmentNum() {
117     return segmentNum;
118   }
119 
120   /**
121    * set number of segments
122    *
123    * @param segNum number of segments
124    */
125   public void setSegmentNumAndRecalcErrors(int segNum) {
126     segmentNum = segNum;
127     calcErrors();
128   }
129 
130   /**
131    * get Mpeg7Catalog with segments
132    *
133    * @return mpeg7 Mpeg7Catalog with segments
134    */
135   public Mpeg7Catalog getMpeg7() {
136     return mpeg7;
137   }
138 
139   /**
140    * get list of segments
141    *
142    * @return segments  list of segments
143    */
144   public LinkedList<Segment> getSegments() {
145     return segments;
146   }
147 
148   /**
149    * calculates absolute error from given number of segments and preferred
150    * number of segments
151    *
152    * @param segmentNum number of segments
153    * @param prefNum preferred number of segments
154    * @return
155    */
156   public static float calculateErrorAbs(int segmentNum, int prefNum) {
157     return Math.abs((float)(segmentNum - prefNum) / (float)prefNum);
158   }
159 
160 /**
161  * With this method a list of OptimizationSteps can be sorted such that the smallest
162  * positive error is the first element of the list and the smallest negative
163  * error is the last element of the list
164  *
165  * @param o the OptimizationStep to be compared.
166  * @return a negative integer or a positive integer as this OptimizationStep should be placed to the left or right of
167  * the specified OptimizationStep or zero if their errors are equal.
168  */
169   @Override
170   public int compareTo(OptimizationStep o) {
171     if (error == o.getError()) {
172       return 0;
173     }
174     // positive
175     if (error >= 0) {
176       // if other error is negative put to the left of it
177       if (o.getError() < 0) {
178         return -1;
179       } else {
180         // if other error is also positive, compare errors, so that smaller positive error will be left
181         if (error < o.getError()) {
182           return -1;
183         } else {
184           return 1;
185         }
186       }
187     // negative
188     } else {
189       // if other error is positive put to the right of it
190       if (o.getError() >= 0) {
191         return 1;
192       } else {
193         // if other error is also negative, compare errors, so that smaller negative error will be right
194         if (error < o.getError()) {
195           return -1;
196         } else {
197           return 1;
198         }
199       }
200     }
201   }
202 }