]> sigrok.org Git - pulseview.git/blame - pv/data/logicsegment.cpp
Rework all subthread-based workers to make notifications more robust
[pulseview.git] / pv / data / logicsegment.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28a4c9c5
JH
18 */
19
1ec3e43f
UH
20#include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
21
cef18fc6 22#include <extdef.h>
28a4c9c5 23
eb8269e3 24#include <cassert>
d9e71737 25#include <cmath>
aca9aa83
UH
26#include <cstdlib>
27#include <cstring>
72708385 28#include <cstdint>
4ceab49a 29
7db61e77 30#include "logic.hpp"
f3d66e52 31#include "logicsegment.hpp"
f556bc6a 32
fe3a1c21 33#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 34
3b68d03d
JH
35using std::lock_guard;
36using std::recursive_mutex;
819f4c25
JH
37using std::max;
38using std::min;
e8d00928 39using std::shared_ptr;
6f925ba9 40using std::vector;
e8d00928
ML
41
42using sigrok::Logic;
28a4c9c5 43
51e77110 44namespace pv {
1b1ec774 45namespace data {
51e77110 46
f3d66e52
JH
47const int LogicSegment::MipMapScalePower = 4;
48const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
49const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
c063290a 50const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes
4ceab49a 51
85a70280
SA
52LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
53 unsigned int unit_size, uint64_t samplerate) :
54 Segment(segment_id, samplerate, unit_size),
9d22929c 55 owner_(owner),
0df28cd5
JB
56 last_append_sample_(0),
57 last_append_accumulator_(0),
58 last_append_extra_(0)
9d22929c
SA
59{
60 memset(mip_map_, 0, sizeof(mip_map_));
61}
62
f3d66e52 63LogicSegment::~LogicSegment()
4ceab49a 64{
8dbbc7f0
JH
65 lock_guard<recursive_mutex> lock(mutex_);
66 for (MipMapLevel &l : mip_map_)
4ceab49a
JH
67 free(l.data);
68}
69
0df28cd5
JB
70template <class T>
71void LogicSegment::downsampleTmain(const T*&in, T &acc, T &prev)
72{
73 // Accumulate one sample at a time
74 for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
75 T sample = *in++;
76 acc |= prev ^ sample;
77 prev = sample;
78 }
79}
80
81template <>
82void LogicSegment::downsampleTmain<uint8_t>(const uint8_t*&in, uint8_t &acc, uint8_t &prev)
83{
84 // Handle 8 bit samples in 32 bit steps
85 uint32_t prev32 = prev | prev << 8 | prev << 16 | prev << 24;
86 uint32_t acc32 = acc;
87 const uint32_t *in32 = (const uint32_t*)in;
88 for (uint64_t i = 0; i < MipMapScaleFactor; i += 4) {
89 uint32_t sample32 = *in32++;
90 acc32 |= prev32 ^ sample32;
91 prev32 = sample32;
92 }
93 // Reduce result back to uint8_t
94#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
95 prev = (prev32 >> 24) & 0xff; // MSB is last
96#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
97 prev = prev32 & 0xff; // LSB is last
98#else
99#error Endianness unknown
100#endif
101 acc |= acc32 & 0xff;
102 acc |= (acc32 >> 8) & 0xff;
103 acc |= (acc32 >> 16) & 0xff;
104 acc |= (acc32 >> 24) & 0xff;
105 in = (const uint8_t*)in32;
106}
107
108template <>
109void LogicSegment::downsampleTmain<uint16_t>(const uint16_t*&in, uint16_t &acc, uint16_t &prev)
110{
111 // Handle 16 bit samples in 32 bit steps
112 uint32_t prev32 = prev | prev << 16;
113 uint32_t acc32 = acc;
114 const uint32_t *in32 = (const uint32_t*)in;
115 for (uint64_t i = 0; i < MipMapScaleFactor; i += 2) {
116 uint32_t sample32 = *in32++;
117 acc32 |= prev32 ^ sample32;
118 prev32 = sample32;
119 }
120 // Reduce result back to uint16_t
121#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
122 prev = (prev32 >> 16) & 0xffff; // MSB is last
123#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
124 prev = prev32 & 0xffff; // LSB is last
125#else
126#error Endian unknown
127#endif
128 acc |= acc32 & 0xffff;
129 acc |= (acc32 >> 16) & 0xffff;
130 in = (const uint16_t*)in32;
131}
132
133template <class T>
134void LogicSegment::downsampleT(const uint8_t *in_, uint8_t *&out_, uint64_t len)
135{
136 const T *in = (const T*)in_;
137 T *out = (T*)out_;
138 T prev = last_append_sample_;
139 T acc = last_append_accumulator_;
140
141 // Try to complete the previous downsample
142 if (last_append_extra_) {
143 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
144 T sample = *in++;
145 acc |= prev ^ sample;
146 prev = sample;
147 last_append_extra_++;
148 len--;
149 }
150 if (!len) {
151 // Not enough samples available to complete downsample
152 last_append_sample_ = prev;
153 last_append_accumulator_ = acc;
154 return;
155 }
156 // We have a complete downsample
157 *out++ = acc;
158 acc = 0;
159 last_append_extra_ = 0;
160 }
161
162 // Handle complete blocks of MipMapScaleFactor samples
163 while (len >= MipMapScaleFactor) {
164 downsampleTmain<T>(in, acc, prev);
165 len -= MipMapScaleFactor;
166 // Output downsample
167 *out++ = acc;
168 acc = 0;
169 }
170
171 // Process remainder, not enough for a complete sample
172 while (len > 0) {
173 T sample = *in++;
174 acc |= prev ^ sample;
175 prev = sample;
176 last_append_extra_++;
177 len--;
178 }
179
180 // Update context
181 last_append_sample_ = prev;
182 last_append_accumulator_ = acc;
183 out_ = (uint8_t *)out;
184}
185
186void LogicSegment::downsampleGeneric(const uint8_t *in, uint8_t *&out, uint64_t len)
187{
188 // Downsample using the generic unpack_sample()
189 // which can handle any width between 1 and 8 bytes
190 uint64_t prev = last_append_sample_;
191 uint64_t acc = last_append_accumulator_;
192
193 // Try to complete the previous downsample
194 if (last_append_extra_) {
195 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
196 const uint64_t sample = unpack_sample(in);
197 in += unit_size_;
198 acc |= prev ^ sample;
199 prev = sample;
200 last_append_extra_++;
201 len--;
202 }
203 if (!len) {
204 // Not enough samples available to complete downsample
205 last_append_sample_ = prev;
206 last_append_accumulator_ = acc;
207 return;
208 }
209 // We have a complete downsample
210 pack_sample(out, acc);
211 out += unit_size_;
212 acc = 0;
213 last_append_extra_ = 0;
214 }
215
216 // Handle complete blocks of MipMapScaleFactor samples
217 while (len >= MipMapScaleFactor) {
218 // Accumulate one sample at a time
219 for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
220 const uint64_t sample = unpack_sample(in);
221 in += unit_size_;
222 acc |= prev ^ sample;
223 prev = sample;
224 }
225 len -= MipMapScaleFactor;
226 // Output downsample
227 pack_sample(out, acc);
228 out += unit_size_;
229 acc = 0;
230 }
231
232 // Process remainder, not enough for a complete sample
233 while (len > 0) {
234 const uint64_t sample = unpack_sample(in);
235 in += unit_size_;
236 acc |= prev ^ sample;
237 prev = sample;
238 last_append_extra_++;
239 len--;
240 }
241
242 // Update context
243 last_append_sample_ = prev;
244 last_append_accumulator_ = acc;
245}
246
97cb532f 247inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
8cb71705 248{
9df8453f
MC
249#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
250 return *(uint64_t*)ptr;
251#else
8cb71705 252 uint64_t value = 0;
2ad82c2e 253 switch (unit_size_) {
8cb71705
MC
254 default:
255 value |= ((uint64_t)ptr[7]) << 56;
256 /* FALLTHRU */
257 case 7:
258 value |= ((uint64_t)ptr[6]) << 48;
259 /* FALLTHRU */
260 case 6:
261 value |= ((uint64_t)ptr[5]) << 40;
262 /* FALLTHRU */
263 case 5:
264 value |= ((uint64_t)ptr[4]) << 32;
265 /* FALLTHRU */
266 case 4:
267 value |= ((uint32_t)ptr[3]) << 24;
268 /* FALLTHRU */
269 case 3:
270 value |= ((uint32_t)ptr[2]) << 16;
271 /* FALLTHRU */
272 case 2:
273 value |= ptr[1] << 8;
274 /* FALLTHRU */
275 case 1:
276 value |= ptr[0];
277 /* FALLTHRU */
278 case 0:
279 break;
280 }
281 return value;
9df8453f 282#endif
8cb71705
MC
283}
284
97cb532f 285inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
8cb71705 286{
9df8453f
MC
287#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
288 *(uint64_t*)ptr = value;
289#else
2ad82c2e 290 switch (unit_size_) {
8cb71705
MC
291 default:
292 ptr[7] = value >> 56;
293 /* FALLTHRU */
294 case 7:
295 ptr[6] = value >> 48;
296 /* FALLTHRU */
297 case 6:
298 ptr[5] = value >> 40;
299 /* FALLTHRU */
300 case 5:
301 ptr[4] = value >> 32;
302 /* FALLTHRU */
303 case 4:
304 ptr[3] = value >> 24;
305 /* FALLTHRU */
306 case 3:
307 ptr[2] = value >> 16;
308 /* FALLTHRU */
309 case 2:
310 ptr[1] = value >> 8;
311 /* FALLTHRU */
312 case 1:
313 ptr[0] = value;
314 /* FALLTHRU */
315 case 0:
316 break;
317 }
9df8453f 318#endif
8cb71705
MC
319}
320
7db61e77 321void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
28a4c9c5 322{
8dbbc7f0
JH
323 assert(unit_size_ == logic->unit_size());
324 assert((logic->data_length() % unit_size_) == 0);
f556bc6a 325
9d22929c
SA
326 append_payload(logic->data_pointer(), logic->data_length());
327}
328
329void LogicSegment::append_payload(void *data, uint64_t data_size)
330{
bee54d9e 331 assert(unit_size_ > 0);
9d22929c
SA
332 assert((data_size % unit_size_) == 0);
333
8dbbc7f0 334 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 335
cc69e8ae
UH
336 const uint64_t prev_sample_count = sample_count_;
337 const uint64_t sample_count = data_size / unit_size_;
7db61e77 338
9d22929c 339 append_samples(data, sample_count);
4ceab49a
JH
340
341 // Generate the first mip-map from the data
342 append_payload_to_mipmap();
7db61e77
SA
343
344 if (sample_count > 1)
720f4762 345 owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()),
1f3033cb 346 prev_sample_count + 1, prev_sample_count + 1 + sample_count);
7db61e77 347 else
720f4762 348 owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()),
1f3033cb 349 prev_sample_count + 1, prev_sample_count + 1);
4ceab49a
JH
350}
351
b82243f7 352void LogicSegment::get_samples(int64_t start_sample,
aa78b2df 353 int64_t end_sample, uint8_t* dest) const
ed990f11 354{
ed990f11 355 assert(start_sample >= 0);
8dbbc7f0 356 assert(start_sample <= (int64_t)sample_count_);
ed990f11 357 assert(end_sample >= 0);
8dbbc7f0 358 assert(end_sample <= (int64_t)sample_count_);
ed990f11 359 assert(start_sample <= end_sample);
b82243f7 360 assert(dest != nullptr);
ed990f11 361
8dbbc7f0 362 lock_guard<recursive_mutex> lock(mutex_);
ed990f11 363
b82243f7 364 get_raw_samples(start_sample, (end_sample - start_sample), dest);
26a883ed
SA
365}
366
f3d66e52 367void LogicSegment::get_subsampled_edges(
6f925ba9 368 vector<EdgePair> &edges,
60b0c2da 369 uint64_t start, uint64_t end,
b4bc9b55 370 float min_length, int sig_index, bool first_change_only)
2858b391 371{
60b0c2da
JH
372 uint64_t index = start;
373 unsigned int level;
7d0d64f9
JH
374 bool last_sample;
375 bool fast_forward;
0b02e057 376
2858b391 377 assert(start <= end);
0b02e057 378 assert(min_length > 0);
2858b391 379 assert(sig_index >= 0);
80d50141 380 assert(sig_index < 64);
2858b391 381
8dbbc7f0 382 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 383
4cc0df94
SA
384 // Make sure we only process as many samples as we have
385 if (end > get_sample_count())
386 end = get_sample_count();
387
60b0c2da
JH
388 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
389 const unsigned int min_level = max((int)floorf(logf(min_length) /
0b02e057 390 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 391 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 392
7d0d64f9 393 // Store the initial state
26a883ed 394 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
b4bc9b55
SA
395 if (!first_change_only)
396 edges.emplace_back(index++, last_sample);
2858b391 397
2ad82c2e 398 while (index + block_length <= end) {
7d0d64f9 399 //----- Continue to search -----//
0b02e057 400 level = min_level;
f06ab143
JH
401
402 // We cannot fast-forward if there is no mip-map data at
53aa9bb4 403 // the minimum level.
4c60462b 404 fast_forward = (mip_map_[level].data != nullptr);
2858b391 405
2ad82c2e 406 if (min_length < MipMapScaleFactor) {
0b02e057
JH
407 // Search individual samples up to the beginning of
408 // the next first level mip map block
53aa9bb4 409 const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
0b02e057 410
333d5bbc 411 for (; index < final_index &&
c28fa62b 412 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
2ad82c2e 413 index++) {
53aa9bb4
SA
414
415 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
7d0d64f9
JH
416
417 // If there was a change we cannot fast forward
333d5bbc 418 if (sample != last_sample) {
7d0d64f9 419 fast_forward = false;
0b02e057 420 break;
7d0d64f9 421 }
0b02e057 422 }
2ad82c2e 423 } else {
0b02e057
JH
424 // If resolution is less than a mip map block,
425 // round up to the beginning of the mip-map block
426 // for this level of detail
53aa9bb4 427 const int min_level_scale_power = (level + 1) * MipMapScalePower;
0b02e057 428 index = pow2_ceil(index, min_level_scale_power);
333d5bbc 429 if (index >= end)
0b02e057
JH
430 break;
431
7d0d64f9 432 // We can fast forward only if there was no change
53aa9bb4 433 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
f06ab143
JH
434 if (last_sample != sample)
435 fast_forward = false;
0b02e057
JH
436 }
437
333d5bbc 438 if (fast_forward) {
7d0d64f9
JH
439
440 // Fast forward: This involves zooming out to higher
441 // levels of the mip map searching for changes, then
442 // zooming in on them to find the point where the edge
443 // begins.
444
445 // Slide right and zoom out at the beginnings of mip-map
446 // blocks until we encounter a change
1f1d55ce 447 while (true) {
53aa9bb4
SA
448 const int level_scale_power = (level + 1) * MipMapScalePower;
449 const uint64_t offset = index >> level_scale_power;
7d0d64f9
JH
450
451 // Check if we reached the last block at this
452 // level, or if there was a change in this block
8dbbc7f0 453 if (offset >= mip_map_[level].length ||
53aa9bb4 454 (get_subsample(level, offset) & sig_mask))
0b02e057
JH
455 break;
456
c28fa62b 457 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
7d0d64f9
JH
458 // If we are now at the beginning of a
459 // higher level mip-map block ascend one
460 // level
53aa9bb4 461 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
7d0d64f9
JH
462 break;
463
464 level++;
465 } else {
466 // Slide right to the beginning of the
467 // next mip map block
53aa9bb4 468 index = pow2_ceil(index + 1, level_scale_power);
7d0d64f9 469 }
0b02e057 470 }
7d0d64f9
JH
471
472 // Zoom in, and slide right until we encounter a change,
473 // and repeat until we reach min_level
1f1d55ce 474 while (true) {
8dbbc7f0 475 assert(mip_map_[level].data);
7d0d64f9 476
53aa9bb4
SA
477 const int level_scale_power = (level + 1) * MipMapScalePower;
478 const uint64_t offset = index >> level_scale_power;
7d0d64f9
JH
479
480 // Check if we reached the last block at this
481 // level, or if there was a change in this block
8dbbc7f0 482 if (offset >= mip_map_[level].length ||
53aa9bb4 483 (get_subsample(level, offset) & sig_mask)) {
7d0d64f9
JH
484 // Zoom in unless we reached the minimum
485 // zoom
333d5bbc 486 if (level == min_level)
7d0d64f9
JH
487 break;
488
489 level--;
490 } else {
491 // Slide right to the beginning of the
492 // next mip map block
53aa9bb4 493 index = pow2_ceil(index + 1, level_scale_power);
7d0d64f9 494 }
0b02e057 495 }
0b02e057 496
7d0d64f9
JH
497 // If individual samples within the limit of resolution,
498 // do a linear search for the next transition within the
499 // block
333d5bbc
UH
500 if (min_length < MipMapScaleFactor) {
501 for (; index < end; index++) {
53aa9bb4 502 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
333d5bbc 503 if (sample != last_sample)
7d0d64f9
JH
504 break;
505 }
0b02e057
JH
506 }
507 }
508
7d0d64f9
JH
509 //----- Store the edge -----//
510
511 // Take the last sample of the quanization block
512 const int64_t final_index = index + block_length;
333d5bbc 513 if (index + block_length > end)
7d0d64f9
JH
514 break;
515
516 // Store the final state
53aa9bb4 517 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
326cf6fe 518 edges.emplace_back(index, final_sample);
7d0d64f9
JH
519
520 index = final_index;
521 last_sample = final_sample;
b4bc9b55
SA
522
523 if (first_change_only)
524 break;
2858b391
JH
525 }
526
527 // Add the final state
b4bc9b55
SA
528 if (!first_change_only) {
529 const bool end_sample = get_unpacked_sample(end) & sig_mask;
530 if (last_sample != end_sample)
531 edges.emplace_back(end, end_sample);
532 edges.emplace_back(end + 1, end_sample);
533 }
534}
535
536void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
537 uint64_t origin_sample, float min_length, int sig_index)
538{
8e9e525e
SA
539 if (origin_sample >= sample_count_)
540 return;
541
b4bc9b55
SA
542 // Put the edges vector on the heap, it can become quite big until we can
543 // use a get_subsampled_edges() implementation that searches backwards
544 vector<EdgePair>* edges = new vector<EdgePair>;
545
e0544801 546 // Get all edges to the left of origin_sample
b4bc9b55
SA
547 get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
548
549 // If we don't specify "first only", the first and last edge are the states
550 // at samples 0 and origin_sample. If only those exist, there are no edges
551 if (edges->size() == 2) {
552 delete edges;
553 return;
554 }
555
556 // Dismiss the entry for origin_sample so that back() gives us the
557 // real last entry
558 edges->pop_back();
559 dest.push_back(edges->back());
560 edges->clear();
561
e0544801 562 // Get first edge to the right of origin_sample
b4bc9b55
SA
563 get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
564
565 // "first only" is specified, so nothing needs to be dismissed
566 if (edges->size() == 0) {
567 delete edges;
568 return;
569 }
570
571 dest.push_back(edges->front());
572
573 delete edges;
574}
575
576void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
577{
578 lock_guard<recursive_mutex> lock(mutex_);
579
580 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
581 MipMapDataUnit) * MipMapDataUnit;
582
583 if (new_data_length > m.data_length) {
584 m.data_length = new_data_length;
585
586 // Padding is added to allow for the uint64_t write word
587 m.data = realloc(m.data, new_data_length * unit_size_ +
588 sizeof(uint64_t));
589 }
590}
591
592void LogicSegment::append_payload_to_mipmap()
593{
594 MipMapLevel &m0 = mip_map_[0];
595 uint64_t prev_length;
596 uint8_t *dest_ptr;
65c92359 597 SegmentDataIterator* it;
b4bc9b55
SA
598 uint64_t accumulator;
599 unsigned int diff_counter;
600
601 // Expand the data buffer to fit the new samples
602 prev_length = m0.length;
603 m0.length = sample_count_ / MipMapScaleFactor;
604
605 // Break off if there are no new samples to compute
606 if (m0.length == prev_length)
607 return;
608
609 reallocate_mipmap_level(m0);
610
611 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
612
613 // Iterate through the samples to populate the first level mipmap
614 const uint64_t start_sample = prev_length * MipMapScaleFactor;
615 const uint64_t end_sample = m0.length * MipMapScaleFactor;
0df28cd5 616 uint64_t len_sample = end_sample - start_sample;
65c92359 617 it = begin_sample_iteration(start_sample);
0df28cd5
JB
618 while (len_sample > 0) {
619 // Number of samples available in this chunk
620 uint64_t count = get_iterator_valid_length(it);
621 // Reduce if less than asked for
622 count = std::min(count, len_sample);
623 uint8_t *src_ptr = get_iterator_value(it);
624 // Submit these contiguous samples to downsampling in bulk
625 if (unit_size_ == 1)
626 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
627 else if (unit_size_ == 2)
628 downsampleT<uint16_t>(src_ptr, dest_ptr, count);
629 else if (unit_size_ == 4)
630 downsampleT<uint32_t>(src_ptr, dest_ptr, count);
631 else if (unit_size_ == 8)
632 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
633 else
634 downsampleGeneric(src_ptr, dest_ptr, count);
635 len_sample -= count;
636 // Advance iterator, should move to start of next chunk
637 continue_sample_iteration(it, count);
b4bc9b55 638 }
65c92359 639 end_sample_iteration(it);
b4bc9b55
SA
640
641 // Compute higher level mipmaps
642 for (unsigned int level = 1; level < ScaleStepCount; level++) {
643 MipMapLevel &m = mip_map_[level];
644 const MipMapLevel &ml = mip_map_[level - 1];
645
646 // Expand the data buffer to fit the new samples
647 prev_length = m.length;
648 m.length = ml.length / MipMapScaleFactor;
649
650 // Break off if there are no more samples to be computed
651 if (m.length == prev_length)
652 break;
653
654 reallocate_mipmap_level(m);
655
656 // Subsample the lower level
657 const uint8_t* src_ptr = (uint8_t*)ml.data +
658 unit_size_ * prev_length * MipMapScaleFactor;
659 const uint8_t *const end_dest_ptr =
660 (uint8_t*)m.data + unit_size_ * m.length;
661
662 for (dest_ptr = (uint8_t*)m.data +
663 unit_size_ * prev_length;
664 dest_ptr < end_dest_ptr;
665 dest_ptr += unit_size_) {
666 accumulator = 0;
667 diff_counter = MipMapScaleFactor;
668 while (diff_counter-- > 0) {
669 accumulator |= unpack_sample(src_ptr);
670 src_ptr += unit_size_;
671 }
672
673 pack_sample(dest_ptr, accumulator);
674 }
675 }
676}
677
678uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
679{
680 assert(index < sample_count_);
681
682 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
683 uint8_t data[8];
684
685 get_raw_samples(index, 1, data);
686
687 return unpack_sample(data);
2858b391 688}
0b02e057 689
f3d66e52 690uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
b2bcbe51
JH
691{
692 assert(level >= 0);
8dbbc7f0
JH
693 assert(mip_map_[level].data);
694 return unpack_sample((uint8_t*)mip_map_[level].data +
695 unit_size_ * offset);
b2bcbe51
JH
696}
697
f3d66e52 698uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 699{
72708385 700 const uint64_t p = UINT64_C(1) << power;
60b0c2da 701 return (x + p - 1) / p * p;
0b02e057 702}
51e77110 703
1b1ec774 704} // namespace data
51e77110 705} // namespace pv