Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(711)

Side by Side Diff: content/renderer/media/repetition_detector.cc

Issue 1357013006: Add detection for repeated audio in capturing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: relaxing dcheck on sample rate Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/media/repetition_detector.h"
6
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/metrics/histogram_macros.h"
10
11 namespace content {
12
13 namespace {
14
15 const AudioRepetitionDetector::Pattern kRepetitionPatterns[] = {
16 {1, 10, 10},
17 {2, 20, 10},
18 {3, 30, 10},
19 {4, 40, 10},
20 {5, 50, 10},
21 {6, 60, 10},
22 {7, 70, 10},
23 {8, 80, 10},
24 {9, 90, 10},
25 {10, 100, 10},
26 {20, 200, 10},
27 };
28
29 // This is used for increasing the efficiency of copying data into the buffer.
30 // Input longer than |kMaxFrames| won't be a problem, and will be devided into
31 // trunks automatically.
32 const size_t kMaxFrames = 480; // 10 ms * 48 kHz
33
34 } // namespace
35
36 AudioRepetitionDetector::State::State(const Pattern &pattern)
37 : pattern_(pattern) {
38 Reset();
39 }
40
41 void AudioRepetitionDetector::State::Increment(bool zero) {
42 if (0 == count_frames_ && zero) {
43 // If a repetition starts with zeros, we enter the all zero mode until
44 // a non zero is found later. The point is that the beginning zeros should
45 // be counted in the length of the repetition as long as the repetition does
46 // not comprise only zeros.
47 all_zero_ = true;
48 }
49 ++count_frames_;
50 if (!zero) {
51 all_zero_ = false;
52 }
53 }
54
55 bool AudioRepetitionDetector::State::HasValidReport(int sample_rate_hz) const {
56 return (!all_zero_ && count_frames_ >=
57 static_cast<size_t>(pattern_.min_length_ms * sample_rate_hz / 1000));
58 }
59
60 void AudioRepetitionDetector::State::Reset() {
61 count_frames_ = 0;
62 all_zero_ = true;
63 reported_ = false;
64 }
65
66 AudioRepetitionDetector::AudioRepetitionDetector()
67 : max_look_back_ms_(0),
68 sample_rate_hz_(0),
69 buffer_size_frames_(0),
70 buffer_end_index_(0),
71 max_frames_(kMaxFrames) {
72 RegisterRepetitionPatterns(kRepetitionPatterns,
73 arraysize(kRepetitionPatterns));
74 }
75
76 AudioRepetitionDetector::~AudioRepetitionDetector() {
77 DCHECK(thread_checker_.CalledOnValidThread());
78 }
79
80 void AudioRepetitionDetector::RegisterRepetitionPatterns(
81 const Pattern* patterns, size_t num_patterns) {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 Pattern pattern;
84 for (size_t idx = 0; idx < num_patterns; idx++) {
85 pattern = patterns[idx];
86 ids_.push_back(pattern.id);
87 states_.push_back(new State(pattern));
88 if (pattern.look_back_ms > max_look_back_ms_) {
89 max_look_back_ms_ = pattern.look_back_ms;
90 }
91 }
92 }
93
94 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate_hz) {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 num_channels_ = num_channels;
97 sample_rate_hz_ = sample_rate_hz;
98
99 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
100 buffer_size_frames_ =
101 (max_look_back_ms_ * sample_rate_hz_ + 999) / 1000 + max_frames_;
102
103 audio_buffer_.resize(buffer_size_frames_ * num_channels_);
104 for (auto state : states_) {
105 state->Reset();
106 }
107 }
108
109 void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
110 size_t num_frames) {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 DCHECK_LE(num_frames, buffer_size_frames_);
113 const size_t margin = buffer_size_frames_ - buffer_end_index_;
114 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
115 if (num_frames <= margin) {
116 std::copy(data, data + num_frames * num_channels_, it);
117 buffer_end_index_ += num_frames;
118 } else {
119 std::copy(data, data + margin * num_channels_, it);
120 std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
121 audio_buffer_.begin());
122 buffer_end_index_ = num_frames - margin;
123 }
124 }
125
126 bool AudioRepetitionDetector::Equal(const float* frame,
127 int look_back_frames) const {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 const size_t look_back_index =
130 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
131 buffer_size_frames_ ;
132 auto it = audio_buffer_.begin() + look_back_index * num_channels_;
133 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
134 if (*frame != *it) {
135 return false;
136 }
137 }
138 return true;
139 }
140
141 bool AudioRepetitionDetector::IsZero(const float* frame,
142 size_t num_channels) const {
143 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
144 if (*frame != 0) {
145 return false;
146 }
147 }
148 return true;
149 }
150
151 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
152 size_t num_channels, int sample_rate_hz) {
153 DCHECK(thread_checker_.CalledOnValidThread());
154 DCHECK_GT(states_.size(), 0ul);
155 if (num_channels != num_channels_ || sample_rate_hz != sample_rate_hz_) {
156 Reset(num_channels, sample_rate_hz);
157 }
158
159 while (num_frames > max_frames_) {
160 Detect(data, max_frames_, num_channels, sample_rate_hz);
161 data += max_frames_ * num_channels;
162 num_frames -= max_frames_;
163 }
164
165 if (num_frames == 0)
166 return;
167
168 AddFramesToBuffer(data, num_frames);
169
170 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
171 for (auto state : states_) {
172 // Look back position depends on the sample rate. It is rounded down to
173 // the closest integer.
minyue 2015/09/27 12:44:09 new change
174 const size_t look_back_frames =
175 state->look_back_ms() * sample_rate_hz_ / 1000;
176 // Equal(data, offset) checks if |data| equals the audio frame located
177 // |offset| frames from the end of buffer. Now a full frame has been
178 // inserted to the buffer, and thus |offset| should compensate for it.
179 if (Equal(data, look_back_frames + idx)) {
180 if (!state->reported()) {
181 state->Increment(IsZero(data, num_channels));
182 if (state->HasValidReport(sample_rate_hz)) {
183 ReportRepetition(state->id());
184 state->set_reported(true);
185 }
186 }
187 } else {
188 state->Reset();
189 }
190 }
191 }
192 }
193
194 void AudioRepetitionDetector::ReportRepetition(int id) {
195 DCHECK(thread_checker_.CalledOnValidThread());
196 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
197 "Media.AudioCapturerRepetition", id, ids_);
198 }
199
200 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698