]> sigrok.org Git - pulseview.git/blame - pv/data/logicsegment.cpp
Rework signals for modularity
[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{
331 assert((data_size % unit_size_) == 0);
332
8dbbc7f0 333 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 334
cc69e8ae
UH
335 const uint64_t prev_sample_count = sample_count_;
336 const uint64_t sample_count = data_size / unit_size_;
7db61e77 337
9d22929c 338 append_samples(data, sample_count);
4ceab49a
JH
339
340 // Generate the first mip-map from the data
341 append_payload_to_mipmap();
7db61e77
SA
342
343 if (sample_count > 1)
1f3033cb
SA
344 owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
345 prev_sample_count + 1, prev_sample_count + 1 + sample_count);
7db61e77 346 else
1f3033cb
SA
347 owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
348 prev_sample_count + 1, prev_sample_count + 1);
4ceab49a
JH
349}
350
b82243f7 351void LogicSegment::get_samples(int64_t start_sample,
aa78b2df 352 int64_t end_sample, uint8_t* dest) const
ed990f11 353{
ed990f11 354 assert(start_sample >= 0);
8dbbc7f0 355 assert(start_sample <= (int64_t)sample_count_);
ed990f11 356 assert(end_sample >= 0);
8dbbc7f0 357 assert(end_sample <= (int64_t)sample_count_);
ed990f11 358 assert(start_sample <= end_sample);
b82243f7 359 assert(dest != nullptr);
ed990f11 360
8dbbc7f0 361 lock_guard<recursive_mutex> lock(mutex_);
ed990f11 362
b82243f7 363 get_raw_samples(start_sample, (end_sample - start_sample), dest);
26a883ed
SA
364}
365
f3d66e52 366void LogicSegment::get_subsampled_edges(
6f925ba9 367 vector<EdgePair> &edges,
60b0c2da 368 uint64_t start, uint64_t end,
b4bc9b55 369 float min_length, int sig_index, bool first_change_only)
2858b391 370{
60b0c2da
JH
371 uint64_t index = start;
372 unsigned int level;
7d0d64f9
JH
373 bool last_sample;
374 bool fast_forward;
0b02e057 375
2858b391 376 assert(start <= end);
0b02e057 377 assert(min_length > 0);
2858b391 378 assert(sig_index >= 0);
80d50141 379 assert(sig_index < 64);
2858b391 380
8dbbc7f0 381 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 382
4cc0df94
SA
383 // Make sure we only process as many samples as we have
384 if (end > get_sample_count())
385 end = get_sample_count();
386
60b0c2da
JH
387 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
388 const unsigned int min_level = max((int)floorf(logf(min_length) /
0b02e057 389 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 390 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 391
7d0d64f9 392 // Store the initial state
26a883ed 393 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
b4bc9b55
SA
394 if (!first_change_only)
395 edges.emplace_back(index++, last_sample);
2858b391 396
2ad82c2e 397 while (index + block_length <= end) {
7d0d64f9 398 //----- Continue to search -----//
0b02e057 399 level = min_level;
f06ab143
JH
400
401 // We cannot fast-forward if there is no mip-map data at
53aa9bb4 402 // the minimum level.
4c60462b 403 fast_forward = (mip_map_[level].data != nullptr);
2858b391 404
2ad82c2e 405 if (min_length < MipMapScaleFactor) {
0b02e057
JH
406 // Search individual samples up to the beginning of
407 // the next first level mip map block
53aa9bb4 408 const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
0b02e057 409
333d5bbc 410 for (; index < final_index &&
c28fa62b 411 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
2ad82c2e 412 index++) {
53aa9bb4
SA
413
414 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
7d0d64f9
JH
415
416 // If there was a change we cannot fast forward
333d5bbc 417 if (sample != last_sample) {
7d0d64f9 418 fast_forward = false;
0b02e057 419 break;
7d0d64f9 420 }
0b02e057 421 }
2ad82c2e 422 } else {
0b02e057
JH
423 // If resolution is less than a mip map block,
424 // round up to the beginning of the mip-map block
425 // for this level of detail
53aa9bb4 426 const int min_level_scale_power = (level + 1) * MipMapScalePower;
0b02e057 427 index = pow2_ceil(index, min_level_scale_power);
333d5bbc 428 if (index >= end)
0b02e057
JH
429 break;
430
7d0d64f9 431 // We can fast forward only if there was no change
53aa9bb4 432 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
f06ab143
JH
433 if (last_sample != sample)
434 fast_forward = false;
0b02e057
JH
435 }
436
333d5bbc 437 if (fast_forward) {
7d0d64f9
JH
438
439 // Fast forward: This involves zooming out to higher
440 // levels of the mip map searching for changes, then
441 // zooming in on them to find the point where the edge
442 // begins.
443
444 // Slide right and zoom out at the beginnings of mip-map
445 // blocks until we encounter a change
1f1d55ce 446 while (true) {
53aa9bb4
SA
447 const int level_scale_power = (level + 1) * MipMapScalePower;
448 const uint64_t offset = index >> level_scale_power;
7d0d64f9
JH
449
450 // Check if we reached the last block at this
451 // level, or if there was a change in this block
8dbbc7f0 452 if (offset >= mip_map_[level].length ||
53aa9bb4 453 (get_subsample(level, offset) & sig_mask))
0b02e057
JH
454 break;
455
c28fa62b 456 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
7d0d64f9
JH
457 // If we are now at the beginning of a
458 // higher level mip-map block ascend one
459 // level
53aa9bb4 460 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
7d0d64f9
JH
461 break;
462
463 level++;
464 } else {
465 // Slide right to the beginning of the
466 // next mip map block
53aa9bb4 467 index = pow2_ceil(index + 1, level_scale_power);
7d0d64f9 468 }
0b02e057 469 }
7d0d64f9
JH
470
471 // Zoom in, and slide right until we encounter a change,
472 // and repeat until we reach min_level
1f1d55ce 473 while (true) {
8dbbc7f0 474 assert(mip_map_[level].data);
7d0d64f9 475
53aa9bb4
SA
476 const int level_scale_power = (level + 1) * MipMapScalePower;
477 const uint64_t offset = index >> level_scale_power;
7d0d64f9
JH
478
479 // Check if we reached the last block at this
480 // level, or if there was a change in this block
8dbbc7f0 481 if (offset >= mip_map_[level].length ||
53aa9bb4 482 (get_subsample(level, offset) & sig_mask)) {
7d0d64f9
JH
483 // Zoom in unless we reached the minimum
484 // zoom
333d5bbc 485 if (level == min_level)
7d0d64f9
JH
486 break;
487
488 level--;
489 } else {
490 // Slide right to the beginning of the
491 // next mip map block
53aa9bb4 492 index = pow2_ceil(index + 1, level_scale_power);
7d0d64f9 493 }
0b02e057 494 }
0b02e057 495
7d0d64f9
JH
496 // If individual samples within the limit of resolution,
497 // do a linear search for the next transition within the
498 // block
333d5bbc
UH
499 if (min_length < MipMapScaleFactor) {
500 for (; index < end; index++) {
53aa9bb4 501 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
333d5bbc 502 if (sample != last_sample)
7d0d64f9
JH
503 break;
504 }
0b02e057
JH
505 }
506 }
507
7d0d64f9
JH
508 //----- Store the edge -----//
509
510 // Take the last sample of the quanization block
511 const int64_t final_index = index + block_length;
333d5bbc 512 if (index + block_length > end)
7d0d64f9
JH
513 break;
514
515 // Store the final state
53aa9bb4 516 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
326cf6fe 517 edges.emplace_back(index, final_sample);
7d0d64f9
JH
518
519 index = final_index;
520 last_sample = final_sample;
b4bc9b55
SA
521
522 if (first_change_only)
523 break;
2858b391
JH
524 }
525
526 // Add the final state
b4bc9b55
SA
527 if (!first_change_only) {
528 const bool end_sample = get_unpacked_sample(end) & sig_mask;
529 if (last_sample != end_sample)
530 edges.emplace_back(end, end_sample);
531 edges.emplace_back(end + 1, end_sample);
532 }
533}
534
535void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
536 uint64_t origin_sample, float min_length, int sig_index)
537{
8e9e525e
SA
538 if (origin_sample >= sample_count_)
539 return;
540
b4bc9b55
SA
541 // Put the edges vector on the heap, it can become quite big until we can
542 // use a get_subsampled_edges() implementation that searches backwards
543 vector<EdgePair>* edges = new vector<EdgePair>;
544
e0544801 545 // Get all edges to the left of origin_sample
b4bc9b55
SA
546 get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
547
548 // If we don't specify "first only", the first and last edge are the states
549 // at samples 0 and origin_sample. If only those exist, there are no edges
550 if (edges->size() == 2) {
551 delete edges;
552 return;
553 }
554
555 // Dismiss the entry for origin_sample so that back() gives us the
556 // real last entry
557 edges->pop_back();
558 dest.push_back(edges->back());
559 edges->clear();
560
e0544801 561 // Get first edge to the right of origin_sample
b4bc9b55
SA
562 get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
563
564 // "first only" is specified, so nothing needs to be dismissed
565 if (edges->size() == 0) {
566 delete edges;
567 return;
568 }
569
570 dest.push_back(edges->front());
571
572 delete edges;
573}
574
575void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
576{
577 lock_guard<recursive_mutex> lock(mutex_);
578
579 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
580 MipMapDataUnit) * MipMapDataUnit;
581
582 if (new_data_length > m.data_length) {
583 m.data_length = new_data_length;
584
585 // Padding is added to allow for the uint64_t write word
586 m.data = realloc(m.data, new_data_length * unit_size_ +
587 sizeof(uint64_t));
588 }
589}
590
591void LogicSegment::append_payload_to_mipmap()
592{
593 MipMapLevel &m0 = mip_map_[0];
594 uint64_t prev_length;
595 uint8_t *dest_ptr;
65c92359 596 SegmentDataIterator* it;
b4bc9b55
SA
597 uint64_t accumulator;
598 unsigned int diff_counter;
599
600 // Expand the data buffer to fit the new samples
601 prev_length = m0.length;
602 m0.length = sample_count_ / MipMapScaleFactor;
603
604 // Break off if there are no new samples to compute
605 if (m0.length == prev_length)
606 return;
607
608 reallocate_mipmap_level(m0);
609
610 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
611
612 // Iterate through the samples to populate the first level mipmap
613 const uint64_t start_sample = prev_length * MipMapScaleFactor;
614 const uint64_t end_sample = m0.length * MipMapScaleFactor;
0df28cd5 615 uint64_t len_sample = end_sample - start_sample;
65c92359 616 it = begin_sample_iteration(start_sample);
0df28cd5
JB
617 while (len_sample > 0) {
618 // Number of samples available in this chunk
619 uint64_t count = get_iterator_valid_length(it);
620 // Reduce if less than asked for
621 count = std::min(count, len_sample);
622 uint8_t *src_ptr = get_iterator_value(it);
623 // Submit these contiguous samples to downsampling in bulk
624 if (unit_size_ == 1)
625 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
626 else if (unit_size_ == 2)
627 downsampleT<uint16_t>(src_ptr, dest_ptr, count);
628 else if (unit_size_ == 4)
629 downsampleT<uint32_t>(src_ptr, dest_ptr, count);
630 else if (unit_size_ == 8)
631 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
632 else
633 downsampleGeneric(src_ptr, dest_ptr, count);
634 len_sample -= count;
635 // Advance iterator, should move to start of next chunk
636 continue_sample_iteration(it, count);
b4bc9b55 637 }
65c92359 638 end_sample_iteration(it);
b4bc9b55
SA
639
640 // Compute higher level mipmaps
641 for (unsigned int level = 1; level < ScaleStepCount; level++) {
642 MipMapLevel &m = mip_map_[level];
643 const MipMapLevel &ml = mip_map_[level - 1];
644
645 // Expand the data buffer to fit the new samples
646 prev_length = m.length;
647 m.length = ml.length / MipMapScaleFactor;
648
649 // Break off if there are no more samples to be computed
650 if (m.length == prev_length)
651 break;
652
653 reallocate_mipmap_level(m);
654
655 // Subsample the lower level
656 const uint8_t* src_ptr = (uint8_t*)ml.data +
657 unit_size_ * prev_length * MipMapScaleFactor;
658 const uint8_t *const end_dest_ptr =
659 (uint8_t*)m.data + unit_size_ * m.length;
660
661 for (dest_ptr = (uint8_t*)m.data +
662 unit_size_ * prev_length;
663 dest_ptr < end_dest_ptr;
664 dest_ptr += unit_size_) {
665 accumulator = 0;
666 diff_counter = MipMapScaleFactor;
667 while (diff_counter-- > 0) {
668 accumulator |= unpack_sample(src_ptr);
669 src_ptr += unit_size_;
670 }
671
672 pack_sample(dest_ptr, accumulator);
673 }
674 }
675}
676
677uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
678{
679 assert(index < sample_count_);
680
681 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
682 uint8_t data[8];
683
684 get_raw_samples(index, 1, data);
685
686 return unpack_sample(data);
2858b391 687}
0b02e057 688
f3d66e52 689uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
b2bcbe51
JH
690{
691 assert(level >= 0);
8dbbc7f0
JH
692 assert(mip_map_[level].data);
693 return unpack_sample((uint8_t*)mip_map_[level].data +
694 unit_size_ * offset);
b2bcbe51
JH
695}
696
f3d66e52 697uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 698{
72708385 699 const uint64_t p = UINT64_C(1) << power;
60b0c2da 700 return (x + p - 1) / p * p;
0b02e057 701}
51e77110 702
1b1ec774 703} // namespace data
51e77110 704} // namespace pv