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]; |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 32 | memcpy(&chunk_channels[0], current_row + x * kChunkSize * 2, |
| 33 | 2 * kChunkSize); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 34 | __builtin_prefetch(current_row + (x + 1) * kChunkSize * 2); |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 35 | |
| 36 | for (int i = 0; i < kChunkSize; ++i) { |
| 37 | if ((chunk_channels[i * 2] > value) != in_range) { |
| 38 | const int here = x * kChunkSize + i; |
| 39 | if (in_range) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 40 | current_row_ranges.emplace_back( |
| 41 | ImageRange(current_range_start, here)); |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 42 | } else { |
| 43 | current_range_start = here; |
| 44 | } |
| 45 | in_range = !in_range; |
| 46 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 47 | } |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 48 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 49 | if (in_range) { |
| 50 | current_row_ranges.emplace_back(ImageRange(current_range_start, fmt.w)); |
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 | result.push_back(current_row_ranges); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 53 | } |
Brian Silverman | 4482db5 | 2019-03-10 16:14:48 -0700 | [diff] [blame] | 54 | return RangeImage(0, std::move(result)); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 57 | FastYuyvYPooledThresholder::FastYuyvYPooledThresholder() { |
| 58 | states_.fill(ThreadState::kWaitingForInputData); |
| 59 | for (int i = 0; i < kThreads; ++i) { |
| 60 | threads_[i] = std::thread([this, i]() { RunThread(i); }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | FastYuyvYPooledThresholder::~FastYuyvYPooledThresholder() { |
| 65 | { |
| 66 | std::unique_lock<std::mutex> locker(mutex_); |
| 67 | quit_ = true; |
| 68 | condition_variable_.notify_all(); |
| 69 | } |
| 70 | for (int i = 0; i < kThreads; ++i) { |
| 71 | threads_[i].join(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | RangeImage FastYuyvYPooledThresholder::Threshold(ImageFormat fmt, |
| 76 | const char *data, |
| 77 | uint8_t value) { |
| 78 | input_format_ = fmt; |
| 79 | input_data_ = data; |
| 80 | input_value_ = value; |
| 81 | { |
| 82 | std::unique_lock<std::mutex> locker(mutex_); |
| 83 | for (int i = 0; i < kThreads; ++i) { |
| 84 | states_[i] = ThreadState::kProcessing; |
| 85 | } |
| 86 | condition_variable_.notify_all(); |
| 87 | while (!AllThreadsDone()) { |
| 88 | condition_variable_.wait(locker); |
| 89 | } |
| 90 | } |
| 91 | std::vector<std::vector<ImageRange>> result; |
| 92 | result.reserve(fmt.h); |
| 93 | for (int i = 0; i < kThreads; ++i) { |
| 94 | result.insert(result.end(), outputs_[i].begin(), outputs_[i].end()); |
| 95 | } |
| 96 | return RangeImage(0, std::move(result)); |
| 97 | } |
| 98 | |
| 99 | void FastYuyvYPooledThresholder::RunThread(int i) { |
| 100 | while (true) { |
| 101 | { |
| 102 | std::unique_lock<std::mutex> locker(mutex_); |
| 103 | while (states_[i] == ThreadState::kWaitingForInputData) { |
| 104 | if (quit_) { |
| 105 | return; |
| 106 | } |
| 107 | condition_variable_.wait(locker); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | ImageFormat shard_format = input_format_; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 112 | AOS_CHECK_EQ(shard_format.h % kThreads, 0); |
Brian Silverman | 20b5777 | 2019-03-23 22:02:49 -0700 | [diff] [blame] | 113 | shard_format.h /= kThreads; |
| 114 | |
| 115 | outputs_[i] = FastYuyvYThreshold( |
| 116 | shard_format, input_data_ + shard_format.w * 2 * shard_format.h * i, |
| 117 | input_value_); |
| 118 | { |
| 119 | std::unique_lock<std::mutex> locker(mutex_); |
| 120 | states_[i] = ThreadState::kWaitingForInputData; |
| 121 | condition_variable_.notify_all(); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 126 | } // namespace vision |
| 127 | } // namespace aos |