Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mp4/MP4Interval.h
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef INTERVAL_H_
6
#define INTERVAL_H_
7
8
#include "nsTArray.h"
9
#include <algorithm>
10
11
namespace mozilla
12
{
13
14
template <typename T>
15
struct MP4Interval
16
{
17
0
  MP4Interval() : start(0), end(0) {}
Unexecuted instantiation: mozilla::MP4Interval<long>::MP4Interval()
Unexecuted instantiation: mozilla::MP4Interval<int>::MP4Interval()
18
  MP4Interval(T aStart, T aEnd) : start(aStart), end(aEnd)
19
0
  {
20
0
    MOZ_ASSERT(aStart <= aEnd);
21
0
  }
Unexecuted instantiation: mozilla::MP4Interval<long>::MP4Interval(long, long)
Unexecuted instantiation: mozilla::MP4Interval<int>::MP4Interval(int, int)
22
0
  T Length() { return end - start; }
Unexecuted instantiation: mozilla::MP4Interval<long>::Length()
Unexecuted instantiation: mozilla::MP4Interval<int>::Length()
23
  MP4Interval Intersection(const MP4Interval& aOther) const
24
0
  {
25
0
    T s = start > aOther.start ? start : aOther.start;
26
0
    T e = end < aOther.end ? end : aOther.end;
27
0
    if (s > e) {
28
0
      return MP4Interval();
29
0
    }
30
0
    return MP4Interval(s, e);
31
0
  }
32
  bool Contains(const MP4Interval& aOther) const
33
0
  {
34
0
    return aOther.start >= start && aOther.end <= end;
35
0
  }
Unexecuted instantiation: mozilla::MP4Interval<long>::Contains(mozilla::MP4Interval<long> const&) const
Unexecuted instantiation: mozilla::MP4Interval<int>::Contains(mozilla::MP4Interval<int> const&) const
36
  bool operator==(const MP4Interval& aOther) const
37
0
  {
38
0
    return start == aOther.start && end == aOther.end;
39
0
  }
40
0
  bool operator!=(const MP4Interval& aOther) const { return !(*this == aOther); }
41
  bool IsNull() const
42
  {
43
    return end == start;
44
  }
45
  MP4Interval Extents(const MP4Interval& aOther) const
46
0
  {
47
0
    if (IsNull()) {
48
0
      return aOther;
49
0
    }
50
0
    return MP4Interval(std::min(start, aOther.start),
51
0
                    std::max(end, aOther.end));
52
0
  }
53
54
  T start;
55
  T end;
56
57
  static void SemiNormalAppend(nsTArray<MP4Interval<T>>& aIntervals,
58
                               MP4Interval<T> aMP4Interval)
59
0
  {
60
0
    if (!aIntervals.IsEmpty() &&
61
0
        aIntervals.LastElement().end == aMP4Interval.start) {
62
0
      aIntervals.LastElement().end = aMP4Interval.end;
63
0
    } else {
64
0
      aIntervals.AppendElement(aMP4Interval);
65
0
    }
66
0
  }
67
68
  static void Normalize(const nsTArray<MP4Interval<T>>& aIntervals,
69
                        nsTArray<MP4Interval<T>>* aNormalized)
70
0
  {
71
0
    if (!aNormalized || !aIntervals.Length()) {
72
0
      MOZ_ASSERT(aNormalized);
73
0
      return;
74
0
    }
75
0
    MOZ_ASSERT(aNormalized->IsEmpty());
76
0
77
0
    nsTArray<MP4Interval<T>> sorted;
78
0
    sorted = aIntervals;
79
0
    sorted.Sort(Compare());
80
0
81
0
    MP4Interval<T> current = sorted[0];
82
0
    for (size_t i = 1; i < sorted.Length(); i++) {
83
0
      MOZ_ASSERT(sorted[i].start <= sorted[i].end);
84
0
      if (current.Contains(sorted[i])) {
85
0
        continue;
86
0
      }
87
0
      if (current.end >= sorted[i].start) {
88
0
        current.end = sorted[i].end;
89
0
      } else {
90
0
        aNormalized->AppendElement(current);
91
0
        current = sorted[i];
92
0
      }
93
0
    }
94
0
    aNormalized->AppendElement(current);
95
0
  }
Unexecuted instantiation: mozilla::MP4Interval<long>::Normalize(nsTArray<mozilla::MP4Interval<long> > const&, nsTArray<mozilla::MP4Interval<long> >*)
Unexecuted instantiation: mozilla::MP4Interval<int>::Normalize(nsTArray<mozilla::MP4Interval<int> > const&, nsTArray<mozilla::MP4Interval<int> >*)
96
97
  static void Intersection(const nsTArray<MP4Interval<T>>& a0,
98
                           const nsTArray<MP4Interval<T>>& a1,
99
                           nsTArray<MP4Interval<T>>* aIntersection)
100
0
  {
101
0
    MOZ_ASSERT(IsNormalized(a0));
102
0
    MOZ_ASSERT(IsNormalized(a1));
103
0
    size_t i0 = 0;
104
0
    size_t i1 = 0;
105
0
    while (i0 < a0.Length() && i1 < a1.Length()) {
106
0
      MP4Interval i = a0[i0].Intersection(a1[i1]);
107
0
      if (i.Length()) {
108
0
        aIntersection->AppendElement(i);
109
0
      }
110
0
      if (a0[i0].end < a1[i1].end) {
111
0
        i0++;
112
0
        // Assert that the array is sorted
113
0
        MOZ_ASSERT(i0 == a0.Length() || a0[i0 - 1].start < a0[i0].start);
114
0
      } else {
115
0
        i1++;
116
0
        // Assert that the array is sorted
117
0
        MOZ_ASSERT(i1 == a1.Length() || a1[i1 - 1].start < a1[i1].start);
118
0
      }
119
0
    }
120
0
  }
121
122
  static bool IsNormalized(const nsTArray<MP4Interval<T>>& aIntervals)
123
  {
124
    for (size_t i = 1; i < aIntervals.Length(); i++) {
125
      if (aIntervals[i - 1].end >= aIntervals[i].start) {
126
        return false;
127
      }
128
    }
129
    return true;
130
  }
131
132
  struct Compare
133
  {
134
    bool Equals(const MP4Interval<T>& a0, const MP4Interval<T>& a1) const
135
0
    {
136
0
      return a0.start == a1.start && a0.end == a1.end;
137
0
    }
Unexecuted instantiation: mozilla::MP4Interval<long>::Compare::Equals(mozilla::MP4Interval<long> const&, mozilla::MP4Interval<long> const&) const
Unexecuted instantiation: mozilla::MP4Interval<int>::Compare::Equals(mozilla::MP4Interval<int> const&, mozilla::MP4Interval<int> const&) const
138
139
    bool LessThan(const MP4Interval<T>& a0, const MP4Interval<T>& a1) const
140
0
    {
141
0
      return a0.start < a1.start;
142
0
    }
Unexecuted instantiation: mozilla::MP4Interval<long>::Compare::LessThan(mozilla::MP4Interval<long> const&, mozilla::MP4Interval<long> const&) const
Unexecuted instantiation: mozilla::MP4Interval<int>::Compare::LessThan(mozilla::MP4Interval<int> const&, mozilla::MP4Interval<int> const&) const
143
  };
144
};
145
}
146
147
#endif