Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 1 | #include "aos/vision/blob/threshold.h" |
| 2 | |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 3 | #include "aos/logging/logging.h" |
| 4 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 5 | namespace aos { |
| 6 | namespace vision { |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 7 | namespace { |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 8 | |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 9 | constexpr int kChunkSize = 8; |
| 10 | |
| 11 | } // namespace |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 12 | |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 13 | // At a high level, the algorithm is the same as the slow thresholding, except |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 14 | // it operates in kChunkSize-pixel chunks. |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 15 | RangeImage FastYuyvYThreshold(ImageFormat fmt, const char *data, |
| 16 | uint8_t value) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 17 | AOS_CHECK_EQ(0, fmt.w % kChunkSize); |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 18 | std::vector<std::vector<ImageRange>> result; |
| 19 | result.reserve(fmt.h); |
| 20 | |
| 21 | // Iterate through each row. |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 22 | for (int y = 0; y < fmt.h; ++y) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 23 | // The start of the data for the current row. |
| 24 | const char *const current_row = fmt.w * y * 2 + data; |
| 25 | bool in_range = false; |
| 26 | int current_range_start = -1; |
| 27 | std::vector<ImageRange> current_row_ranges; |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 28 | // Iterate through each kChunkSize-pixel chunk |
| 29 | for (int x = 0; x < fmt.w / kChunkSize; ++x) { |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 30 | // The per-channel (YUYV) values in the current chunk. |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 31 | uint8_t chunk_channels[2 * kChunkSize]; |
| 32 | memcpy(&chunk_channels[0], current_row + x * kChunkSize * 2, 2 * kChunkSize); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 33 | __builtin_prefetch(current_row + (x + 1) * kChunkSize * 2); |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 34 | |
| 35 | for (int i = 0; i < kChunkSize; ++i) { |
| 36 | if ((chunk_channels[i * 2] > value) != in_range) { |
| 37 | const int here = x * kChunkSize + i; |
| 38 | if (in_range) { |
| 39 | current_row_ranges.emplace_back(ImageRange(current_range_start, here)); |
| 40 | } else { |
| 41 | current_range_start = here; |
| 42 | } |
| 43 | in_range = !in_range; |
| 44 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 45 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 46 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 47 | if (in_range) { |
| 48 | current_row_ranges.emplace_back(ImageRange(current_range_start, fmt.w)); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 49 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 50 | result.push_back(current_row_ranges); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 51 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 52 | return RangeImage(0, std::move(result)); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 55 | FastYuyvYPooledThresholder::FastYuyvYPooledThresholder() { |
| 56 | states_.fill(ThreadState::kWaitingForInputData); |
| 57 | for (int i = 0; i < kThreads; ++i) { |
| 58 | threads_[i] = std::thread([this, i]() { RunThread(i); }); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | FastYuyvYPooledThresholder::~FastYuyvYPooledThresholder() { |
| 63 | { |
| 64 | std::unique_lock<std::mutex> locker(mutex_); |
| 65 | quit_ = true; |
| 66 | condition_variable_.notify_all(); |
| 67 | } |
| 68 | for (int i = 0; i < kThreads; ++i) { |
| 69 | threads_[i].join(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | RangeImage FastYuyvYPooledThresholder::Threshold(ImageFormat fmt, |
| 74 | const char *data, |
| 75 | uint8_t value) { |
| 76 | input_format_ = fmt; |
| 77 | input_data_ = data; |
| 78 | input_value_ = value; |
| 79 | { |
| 80 | std::unique_lock<std::mutex> locker(mutex_); |
| 81 | for (int i = 0; i < kThreads; ++i) { |
| 82 | states_[i] = ThreadState::kProcessing; |
| 83 | } |
| 84 | condition_variable_.notify_all(); |
| 85 | while (!AllThreadsDone()) { |
| 86 | condition_variable_.wait(locker); |
| 87 | } |
| 88 | } |
| 89 | std::vector<std::vector<ImageRange>> result; |
| 90 | result.reserve(fmt.h); |
| 91 | for (int i = 0; i < kThreads; ++i) { |
| 92 | result.insert(result.end(), outputs_[i].begin(), outputs_[i].end()); |
| 93 | } |
| 94 | return RangeImage(0, std::move(result)); |
| 95 | } |
| 96 | |
| 97 | void FastYuyvYPooledThresholder::RunThread(int i) { |
| 98 | while (true) { |
| 99 | { |
| 100 | std::unique_lock<std::mutex> locker(mutex_); |
| 101 | while (states_[i] == ThreadState::kWaitingForInputData) { |
| 102 | if (quit_) { |
| 103 | return; |
| 104 | } |
| 105 | condition_variable_.wait(locker); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ImageFormat shard_format = input_format_; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 110 | AOS_CHECK_EQ(shard_format.h % kThreads, 0); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 111 | shard_format.h /= kThreads; |
| 112 | |
| 113 | outputs_[i] = FastYuyvYThreshold( |
| 114 | shard_format, input_data_ + shard_format.w * 2 * shard_format.h * i, |
| 115 | input_value_); |
| 116 | { |
| 117 | std::unique_lock<std::mutex> locker(mutex_); |
| 118 | states_[i] = ThreadState::kWaitingForInputData; |
| 119 | condition_variable_.notify_all(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 124 | } // namespace vision |
| 125 | } // namespace aos |