Repository navigation
Expand file tree
/
Copy pathILDAFile.cpp
More file actions
578 lines (465 loc) · 16.3 KB
/
Copy pathILDAFile.cpp
File metadata and controls
578 lines (465 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include "ILDAFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// --- ILDAStreamParser实现 ---
ILDAStreamParser::ILDAStreamParser() {
reset_parser();
}
void ILDAStreamParser::reset_parser() {
buffer_pos = 0;
buffer_size = 0;
state = ILDA_PARSE_STATE_HEADER;
records_parsed = 0;
chunk_count = 0;
memset(¤t_header, 0, sizeof(ILDA_Header_t));
}
bool ILDAStreamParser::feed_data(const uint8_t* data, size_t size) {
// 检查是否有足够空间
if (buffer_size + size > ILDA_PARSE_BUFFER_SIZE) {
// 缓冲区已满,无法添加更多数据
return false;
}
// 复制数据到解析缓冲区
memcpy(parse_buffer + buffer_size, data, size);
buffer_size += size;
// 根据当前状态解析数据
bool result = false;
switch (state) {
case ILDA_PARSE_STATE_HEADER:
result = parse_header_chunk();
break;
case ILDA_PARSE_STATE_RECORDS:
result = parse_records_chunk();
break;
case ILDA_PARSE_STATE_COMPLETE:
case ILDA_PARSE_STATE_ERROR:
// 已完成或出错,不再解析
result = false;
break;
}
return result;
}
bool ILDAStreamParser::parse_header_chunk() {
// 检查是否有足够的数据解析头部
if (buffer_size < sizeof(ILDA_Header_t)) {
return false; // 数据不足,等待更多数据
}
// 复制头部数据
memcpy(¤t_header, parse_buffer, sizeof(ILDA_Header_t));
// 检查ILDA签名
if (strncmp(current_header.signature, "ILDA", 4) != 0) {
printf("[ILDA] Error: Invalid ILDA signature\n");
state = ILDA_PARSE_STATE_ERROR;
return false;
}
// 转换字节序(大端到小端)
current_header.record_count = (current_header.record_count >> 8) | (current_header.record_count << 8);
current_header.frame_number = (current_header.frame_number >> 8) | (current_header.frame_number << 8);
current_header.total_frames = (current_header.total_frames >> 8) | (current_header.total_frames << 8);
// 输出调试信息
printf("[ILDA] Format: %d, Records: %d, Frame: %d/%d\n",
current_header.format_code,
current_header.record_count,
current_header.frame_number,
current_header.total_frames);
// 更新解析状态
buffer_pos = sizeof(ILDA_Header_t);
state = ILDA_PARSE_STATE_RECORDS;
records_parsed = 0;
chunk_count = 0;
return true;
}
bool ILDAStreamParser::parse_records_chunk() {
// 计算可以解析的记录数量
uint16_t remaining_records = current_header.record_count - records_parsed;
uint16_t available_records = (buffer_size - buffer_pos) / sizeof(ILDA_Record_t);
uint16_t records_to_parse = (remaining_records < available_records) ?
remaining_records : available_records;
// 检查是否可以解析一个完整的块
if (records_to_parse == 0) {
return false; // 数据不足,等待更多数据
}
// 限制每块的大小
if (records_to_parse > ILDA_RECORD_CHUNK_SIZE) {
records_to_parse = ILDA_RECORD_CHUNK_SIZE;
}
// 解析记录
for (uint16_t i = 0; i < records_to_parse; i++) {
ILDA_Record_t* record = &record_chunk[i];
memcpy(record, parse_buffer + buffer_pos, sizeof(ILDA_Record_t));
// 转换字节序
record->x = (record->x >> 8) | (record->x << 8);
record->y = (record->y >> 8) | (record->y << 8);
record->z = (record->z >> 8) | (record->z << 8);
buffer_pos += sizeof(ILDA_Record_t);
}
// 更新状态
records_parsed += records_to_parse;
chunk_count = records_to_parse;
// 检查是否已解析完所有记录
if (records_parsed >= current_header.record_count) {
// 如果格式代码为0,表示帧结束
if (current_header.format_code == 0) {
state = ILDA_PARSE_STATE_COMPLETE;
} else {
// 否则继续解析下一个头部
state = ILDA_PARSE_STATE_HEADER;
// 移动剩余数据到缓冲区开始
if (buffer_pos < buffer_size) {
memmove(parse_buffer, parse_buffer + buffer_pos, buffer_size - buffer_pos);
buffer_size -= buffer_pos;
buffer_pos = 0;
} else {
buffer_size = 0;
buffer_pos = 0;
}
}
}
return true;
}
bool ILDAStreamParser::has_complete_header() const {
return (state != ILDA_PARSE_STATE_HEADER);
}
bool ILDAStreamParser::has_complete_chunk() const {
return (chunk_count > 0);
}
bool ILDAStreamParser::is_complete() const {
return (state == ILDA_PARSE_STATE_COMPLETE);
}
bool ILDAStreamParser::has_error() const {
return (state == ILDA_PARSE_STATE_ERROR);
}
const ILDA_Header_t* ILDAStreamParser::get_header() const {
return ¤t_header;
}
const ILDA_Record_t* ILDAStreamParser::get_record_chunk() const {
return record_chunk;
}
void ILDAStreamParser::reset() {
reset_parser();
}
float ILDAStreamParser::get_progress_percent() const {
if (current_header.record_count == 0) {
return 0.0f;
}
return (float)records_parsed * 100.0f / (float)current_header.record_count;
}
// --- ILDAFile实现 ---
ILDAFile::ILDAFile() : frame_records(nullptr), frame_record_counts(nullptr),
frame_count(0), max_frames(0), current_frame(0),
is_streaming(false) {
}
ILDAFile::~ILDAFile() {
free_frame_storage();
}
bool ILDAFile::allocate_frame_storage(uint16_t count) {
// 释放现有存储
free_frame_storage();
// 分配新存储
max_frames = count;
frame_records = (ILDA_Record_t**)malloc(max_frames * sizeof(ILDA_Record_t*));
if (!frame_records) {
printf("[ILDA] Error: Failed to allocate frame records array\n");
return false;
}
// 初始化为NULL
memset(frame_records, 0, max_frames * sizeof(ILDA_Record_t*));
// 分配帧记录计数数组
frame_record_counts = (uint16_t*)malloc(max_frames * sizeof(uint16_t));
if (!frame_record_counts) {
printf("[ILDA] Error: Failed to allocate frame record counts array\n");
free(frame_records);
frame_records = nullptr;
return false;
}
// 初始化为0
memset(frame_record_counts, 0, max_frames * sizeof(uint16_t));
return true;
}
void ILDAFile::free_frame_storage() {
if (frame_records) {
// 释放每帧的记录
for (uint16_t i = 0; i < max_frames; i++) {
free_frame_records(i);
}
// 释放记录数组
free(frame_records);
frame_records = nullptr;
}
if (frame_record_counts) {
free(frame_record_counts);
frame_record_counts = nullptr;
}
max_frames = 0;
frame_count = 0;
current_frame = 0;
}
bool ILDAFile::allocate_frame_records(uint16_t frame_idx, uint16_t record_count) {
// 检查索引是否有效
if (frame_idx >= max_frames) {
printf("[ILDA] Error: Frame index %d out of bounds (max %d)\n", frame_idx, max_frames);
return false;
}
// 释放现有记录
free_frame_records(frame_idx);
// 分配新记录
frame_records[frame_idx] = (ILDA_Record_t*)malloc(record_count * sizeof(ILDA_Record_t));
if (!frame_records[frame_idx]) {
printf("[ILDA] Error: Failed to allocate %d records for frame %d\n", record_count, frame_idx);
return false;
}
// 更新记录计数
frame_record_counts[frame_idx] = record_count;
return true;
}
void ILDAFile::free_frame_records(uint16_t frame_idx) {
// 检查索引是否有效
if (frame_idx < max_frames && frame_records[frame_idx]) {
free(frame_records[frame_idx]);
frame_records[frame_idx] = nullptr;
frame_record_counts[frame_idx] = 0;
}
}
bool ILDAFile::load_from_buffer(const uint8_t* data, size_t size) {
// 重置状态
free_frame_storage();
// 解析ILDA头部
if (size < sizeof(ILDA_Header_t)) {
printf("[ILDA] Error: Buffer too small for ILDA header\n");
return false;
}
ILDA_Header_t* header_ptr = (ILDA_Header_t*)data;
// 检查ILDA签名
if (strncmp(header_ptr->signature, "ILDA", 4) != 0) {
printf("[ILDA] Error: Invalid ILDA signature\n");
return false;
}
// 转换字节序
uint16_t total_frames = (header_ptr->total_frames >> 8) | (header_ptr->total_frames << 8);
// 分配帧存储
if (!allocate_frame_storage(total_frames)) {
return false;
}
// 解析所有帧
size_t offset = 0;
uint16_t frame_idx = 0;
while (offset < size && frame_idx < total_frames) {
// 确保有足够的数据解析头部
if (offset + sizeof(ILDA_Header_t) > size) {
break;
}
// 获取头部
ILDA_Header_t* header = (ILDA_Header_t*)(data + offset);
offset += sizeof(ILDA_Header_t);
// 转换字节序
uint16_t record_count = (header->record_count >> 8) | (header->record_count << 8);
// 分配记录存储
if (!allocate_frame_records(frame_idx, record_count)) {
return false;
}
// 确保有足够的数据解析记录
if (offset + record_count * sizeof(ILDA_Record_t) > size) {
break;
}
// 复制记录
for (uint16_t i = 0; i < record_count; i++) {
ILDA_Record_t* src_record = (ILDA_Record_t*)(data + offset);
ILDA_Record_t* dst_record = &frame_records[frame_idx][i];
// 复制并转换字节序
dst_record->x = (src_record->x >> 8) | (src_record->x << 8);
dst_record->y = (src_record->y >> 8) | (src_record->y << 8);
dst_record->z = (src_record->z >> 8) | (src_record->z << 8);
dst_record->status = src_record->status;
dst_record->color = src_record->color;
offset += sizeof(ILDA_Record_t);
}
frame_idx++;
}
// 更新帧计数
frame_count = frame_idx;
current_frame = 0;
printf("[ILDA] Loaded %d frames from buffer\n", frame_count);
return (frame_count > 0);
}
bool ILDAFile::load_from_file(const char* filename) {
FILE* file = fopen(filename, "rb");
if (!file) {
printf("[ILDA] Error: Cannot open file %s\n", filename);
return false;
}
// 获取文件大小
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0, SEEK_SET);
// 分配缓冲区
uint8_t* buffer = (uint8_t*)malloc(file_size);
if (!buffer) {
printf("[ILDA] Error: Failed to allocate %zu bytes for file buffer\n", file_size);
fclose(file);
return false;
}
// 读取文件
size_t bytes_read = fread(buffer, 1, file_size, file);
fclose(file);
if (bytes_read != file_size) {
printf("[ILDA] Error: Failed to read entire file (read %zu of %zu bytes)\n", bytes_read, file_size);
free(buffer);
return false;
}
// 从缓冲区加载
bool result = load_from_buffer(buffer, file_size);
free(buffer);
return result;
}
bool ILDAFile::start_stream_load(const char* filename) {
// 重置解析器
stream_parser.reset();
// 打开文件
FILE* file = fopen(filename, "rb");
if (!file) {
printf("[ILDA] Error: Cannot open file %s for streaming\n", filename);
return false;
}
// 读取初始块
uint8_t buffer[ILDA_PARSE_BUFFER_SIZE];
size_t bytes_read = fread(buffer, 1, ILDA_PARSE_BUFFER_SIZE, file);
fclose(file);
if (bytes_read == 0) {
printf("[ILDA] Error: Failed to read initial data from file\n");
return false;
}
// 开始解析
if (!stream_parser.feed_data(buffer, bytes_read)) {
printf("[ILDA] Error: Failed to start parsing\n");
return false;
}
// 如果解析了头部,分配存储空间
if (stream_parser.has_complete_header()) {
const ILDA_Header_t* header = stream_parser.get_header();
if (!allocate_frame_storage(header->total_frames)) {
return false;
}
}
is_streaming = true;
return true;
}
bool ILDAFile::continue_stream_load() {
// 检查是否正在流式加载
if (!is_streaming) {
return false;
}
// 检查是否已完成
if (stream_parser.is_complete()) {
is_streaming = false;
return true;
}
// 检查是否有完整的块
if (stream_parser.has_complete_chunk()) {
// 获取当前帧索引
const ILDA_Header_t* header = stream_parser.get_header();
uint16_t frame_idx = header->frame_number;
// 确保帧索引有效
if (frame_idx >= max_frames) {
printf("[ILDA] Error: Frame index %d out of bounds (max %d)\n", frame_idx, max_frames);
is_streaming = false;
return false;
}
// 如果是第一个块,分配记录存储
if (stream_parser.get_total_records_parsed() == stream_parser.get_chunk_size()) {
if (!allocate_frame_records(frame_idx, header->record_count)) {
is_streaming = false;
return false;
}
}
// 复制记录
const ILDA_Record_t* chunk = stream_parser.get_record_chunk();
uint16_t chunk_size = stream_parser.get_chunk_size();
uint16_t offset = stream_parser.get_total_records_parsed() - chunk_size;
if (offset + chunk_size > frame_record_counts[frame_idx]) {
printf("[ILDA] Error: Record offset %d + size %d exceeds frame size %d\n",
offset, chunk_size, frame_record_counts[frame_idx]);
is_streaming = false;
return false;
}
// 复制记录
memcpy(&frame_records[frame_idx][offset], chunk, chunk_size * sizeof(ILDA_Record_t));
// 如果完成了当前帧,更新帧计数
if (stream_parser.is_complete()) {
frame_count = frame_idx + 1;
is_streaming = false;
}
}
return true;
}
float ILDAFile::get_load_progress() const {
if (!is_streaming) {
return 100.0f;
}
return stream_parser.get_progress_percent();
}
void ILDAFile::play() {
// 实现播放逻辑
}
void ILDAFile::pause() {
// 实现暂停逻辑
}
void ILDAFile::stop() {
// 实现停止逻辑
}
void ILDAFile::next_frame() {
if (frame_count > 0) {
current_frame = (current_frame + 1) % frame_count;
}
}
void ILDAFile::prev_frame() {
if (frame_count > 0) {
current_frame = (current_frame > 0) ? (current_frame - 1) : (frame_count - 1);
}
}
void ILDAFile::set_frame(uint16_t frame_idx) {
if (frame_idx < frame_count) {
current_frame = frame_idx;
}
}
ILDA_Record_t* ILDAFile::get_frame_records(uint16_t frame_idx) {
if (frame_idx < frame_count && frame_records[frame_idx]) {
return frame_records[frame_idx];
}
return nullptr;
}
uint16_t ILDAFile::get_frame_record_count(uint16_t frame_idx) {
if (frame_idx < frame_count) {
return frame_record_counts[frame_idx];
}
return 0;
}
size_t ILDAFile::get_memory_usage() {
size_t usage = sizeof(ILDAFile);
// 添加帧记录数组大小
usage += max_frames * sizeof(ILDA_Record_t*);
// 添加记录计数数组大小
usage += max_frames * sizeof(uint16_t);
// 添加每帧记录大小
for (uint16_t i = 0; i < max_frames; i++) {
usage += frame_record_counts[i] * sizeof(ILDA_Record_t);
}
return usage;
}
void ILDAFile::optimize_memory() {
// 实现内存优化逻辑
// 例如,可以压缩记录或释放未使用的帧
}
bool ILDAFile::memory_check() {
// 实现内存检查逻辑
return true;
}
void ILDAFile::print_load_stats() {
printf("[ILDA] File stats: %d frames, current frame: %d\n", frame_count, current_frame);
printf("[ILDA] Memory usage: %zu bytes\n", get_memory_usage());
}
uint32_t ILDAFile::get_load_time_ms() const {
// 实现加载时间统计
return 0;
}