Skip to content

Commit df9ee68

Browse files
committedMay 14, 2024
Fix png fonts breaking recording (#146)
* tidy up font_variant_e / char inconsistencies between main code + recording, so that recording can work again * Consider the different header length when seeking * left the debugs in again
1 parent c1398ac commit df9ee68

File tree

7 files changed

+102
-26
lines changed

7 files changed

+102
-26
lines changed
 

‎ipk/goggle/control/control

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: msp-osd
2-
Version: 0.12.1
2+
Version: 0.12.2
33
Maintainer: bri3d
44
Description: MSP OSD service for the DJI HD FPV goggles.
55
Architecture: pigeon-glasses

‎jni/font/font.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include <stdlib.h>
66
#include <unistd.h>
7+
// #include <ctype.h>
78

89
#include "../libspng/spng.h"
910
#include "font.h"
@@ -14,7 +15,7 @@
1415

1516
/* Font helper methods */
1617

17-
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, char *font_variant)
18+
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, const char *font_variant)
1819
{
1920
char name_buf[len];
2021
char res_buf[len];
@@ -37,7 +38,7 @@ void get_font_path_with_extension(char *font_path_dest, const char *font_path, c
3738
DEBUG_PRINT("Font path: %s\n", font_path_dest);
3839
}
3940

40-
static int open_font(const char *filename, display_info_t *display_info, char *font_variant)
41+
static int open_font(const char *filename, display_info_t *display_info, const char *font_variant)
4142
{
4243
char file_path[255];
4344
int is_hd = (display_info->font_width == HD_FONT_WIDTH) ? 1 : 0;
@@ -135,19 +136,19 @@ static int open_font(const char *filename, display_info_t *display_info, char *f
135136
return -1;
136137
}
137138

138-
void load_font(display_info_t *display_info, char *font_variant) {
139+
void load_font(display_info_t *display_info, const char *font_variant) {
139140

140141
// Note: load_font will not replace an existing font.
141142
if(display_info->fonts[0] == NULL) {
142143
int loaded_font = 0;
143144
DEBUG_PRINT("IN LOAD_FONT\n");
144145
// create a copy of font_variant
145146
char font_variant_lower[5] = "";
146-
if (font_variant != NULL)
147+
if (font_variant != NULL)
147148
{
148149
DEBUG_PRINT("Lowercasing variant\n");
149-
int length = sizeof(font_variant) / sizeof(char);
150-
for (int i = 0; i < length; i++)
150+
size_t length = strlen(font_variant);
151+
for (size_t i = 0; i < length && i < 4; i++) // Ensure not to exceed array bounds
151152
{
152153
font_variant_lower[i] = tolower(font_variant[i]);
153154
}
@@ -160,12 +161,12 @@ void load_font(display_info_t *display_info, char *font_variant) {
160161
DEBUG_PRINT("Loading font %s\n", font_variant_lower);
161162

162163
char *fallback_font_variant = "";
163-
if (font_variant_lower!=NULL && strcmp(font_variant_lower, "btfl") == 0)
164+
if (strcmp(font_variant_lower, "btfl") == 0)
164165
{
165166
DEBUG_PRINT("Setting fallback font variant to bf\n");
166167
fallback_font_variant = "bf";
167168
}
168-
else if (font_variant_lower != NULL && strcmp(font_variant_lower, "ultr") == 0)
169+
else if (strcmp(font_variant_lower, "ultr") == 0)
169170
{
170171
DEBUG_PRINT("Setting fallback font variant to ultra\n");
171172
fallback_font_variant = "ultra";

‎jni/font/font.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define ENTWARE_FONT_PATH "/opt/fonts/font"
99
#define SDCARD_FONT_PATH "/storage/sdcard0/font"
1010

11+
#define FALLBACK_FONT NULL
12+
1113
typedef enum
1214
{
1315
FONT_VARIANT_GENERIC,
@@ -19,6 +21,6 @@ typedef enum
1921
FONT_VARIANT_COUNT
2022
} font_variant_e;
2123

22-
void load_font(display_info_t *display_info, char *font_variant);
24+
void load_font(display_info_t *display_info, const char *font_variant);
2325
void close_font(display_info_t *display_info);
24-
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, char *font_variant);
26+
void get_font_path_with_extension(char *font_path_dest, const char *font_path, const char *extension, uint8_t len, uint8_t is_hd, const char *font_variant);

‎jni/osd_dji_overlay_udp.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static void msp_callback(msp_msg_t *msp_message)
245245
displayport_process_message(display_driver, msp_message);
246246
}
247247

248-
static void load_fonts(font_variant_e font_variant) {
248+
static void load_fonts(char* font_variant) {
249249
char file_path[255];
250250
get_font_path_with_extension(file_path, "font", ".png", 255, 0, font_variant);
251251
toast(file_path);
@@ -346,7 +346,7 @@ static void process_data_packet(uint8_t *buf, int len, dji_shm_state_t *radio_sh
346346
close_all_fonts();
347347
load_fonts(current_fc_variant);
348348
// This is not a typo - fill in any missing fonts for the current variant with the generic one.
349-
load_fonts(FONT_VARIANT_GENERIC);
349+
load_fonts(FALLBACK_FONT);
350350
}
351351
}
352352
}
@@ -400,9 +400,9 @@ static void rec_msp_draw_complete_hook()
400400
.font_width = current_display_info->font_width,
401401
.font_height = current_display_info->font_height,
402402
.x_offset = current_display_info->x_offset,
403-
.y_offset = current_display_info->y_offset,
404-
.font_variant = current_fc_variant,
403+
.y_offset = current_display_info->y_offset
405404
};
405+
strcpy(config.font_variant, current_fc_variant);
406406

407407
rec_start(&config);
408408
}
@@ -480,7 +480,7 @@ static void rec_pb_timeout_hook()
480480
DEBUG_PRINT("msp_osd: playback config, x_offset: %d\n", osd_display_info->x_offset);
481481
DEBUG_PRINT("msp_osd: playback config, y_offset: %d\n", osd_display_info->y_offset);
482482

483-
DEBUG_PRINT("msp_osd: gls playing dvr, loading font variant %d\n", rec_config->font_variant);
483+
DEBUG_PRINT("msp_osd: gls playing dvr, loading font variant %s\n", rec_config->font_variant);
484484
uint8_t is_hd = osd_display_info->font_width != sd_display_info.font_width;
485485
load_font(osd_display_info, rec_config->font_variant);
486486

@@ -577,7 +577,7 @@ void osd_directfb(duss_disp_instance_handle_t *disp, duss_hal_obj_handle_t ion_h
577577
memset(&last_render, 0, sizeof(last_render));
578578
memset(&now, 0, sizeof(now));
579579

580-
load_fonts(FONT_VARIANT_GENERIC);
580+
load_fonts(FALLBACK_FONT);
581581
open_dji_radio_shm(&radio_shm);
582582
start_display(is_v2_goggles, disp, ion_handle);
583583

‎jni/rec/rec.c

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ void rec_start(rec_config_t *config)
4242
sizeof(rec_file_name));
4343
DEBUG_PRINT("rec_file_name: %s", rec_file_name);
4444

45+
DEBUG_PRINT("Config:\n");
46+
DEBUG_PRINT(" Char Width: %u\n", config->char_width);
47+
DEBUG_PRINT(" Char Height: %u\n", config->char_height);
48+
DEBUG_PRINT(" Font Width: %u\n", config->font_width);
49+
DEBUG_PRINT(" Font Height: %u\n", config->font_height);
50+
DEBUG_PRINT(" X Offset: %u\n", config->x_offset);
51+
DEBUG_PRINT(" Y Offset: %u\n", config->y_offset);
52+
DEBUG_PRINT(" Font Variant: %.5s\n", config->font_variant);
53+
4554
rec_fd = fopen(rec_file_name, "wb");
4655
if (rec_fd == NULL)
4756
{

‎jni/rec/rec.h

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdint.h>
55

66
#define REC_MAGIC "MSPOSD"
7-
#define REC_VERSION 1
7+
#define REC_VERSION 2
88

99
typedef struct rec_config_t
1010
{
@@ -14,7 +14,7 @@ typedef struct rec_config_t
1414
uint8_t font_height;
1515
uint16_t x_offset;
1616
uint16_t y_offset;
17-
uint8_t font_variant;
17+
char font_variant[5];
1818
} __attribute__((packed)) rec_config_t;
1919

2020
typedef struct rec_file_header_t
@@ -24,6 +24,24 @@ typedef struct rec_file_header_t
2424
rec_config_t config;
2525
} __attribute__((packed)) rec_file_header_t;
2626

27+
typedef struct rec_config_v1_t
28+
{
29+
uint8_t char_width;
30+
uint8_t char_height;
31+
uint8_t font_width;
32+
uint8_t font_height;
33+
uint16_t x_offset;
34+
uint16_t y_offset;
35+
uint8_t font_variant;
36+
} __attribute__((packed)) rec_config_v1_t;
37+
38+
typedef struct rec_file_header_v1_t
39+
{
40+
char magic[7];
41+
uint16_t version;
42+
rec_config_v1_t config;
43+
} __attribute__((packed)) rec_file_header_v1_t;
44+
2745
typedef struct rec_frame_header_t
2846
{
2947
uint32_t frame_idx;

‎jni/rec/rec_pb.c

+53-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "rec_pb.h"
1313

14+
#include "../font/font.h"
15+
1416
#define REC_PB_CONFIG_ENABLED_KEY "rec_pb_enabled"
1517

1618
#define MAX_X 60
@@ -34,6 +36,7 @@ static bool rec_pb_enabled = false;
3436
static FILE *osd_fd = NULL;
3537
static rec_config_t osd_config = {0};
3638

39+
static uint32_t header_size = 0;
3740
static int64_t frame_counter = 0;
3841

3942
static uint32_t *frame_idxs;
@@ -90,22 +93,64 @@ int rec_pb_start()
9093
return 1;
9194
}
9295

93-
if (file_header.version != REC_VERSION)
96+
if (file_header.version == REC_VERSION)
97+
{
98+
DEBUG_PRINT("header ok!");
99+
memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));
100+
header_size = sizeof(rec_file_header_t);
101+
}
102+
else if (file_header.version == 1)
103+
{
104+
DEBUG_PRINT("header is v1");
105+
rec_file_header_v1_t file_header_v1;
106+
fseek(osd_fd, 0, SEEK_SET);
107+
108+
fread(&file_header_v1, sizeof(rec_file_header_v1_t), 1, osd_fd);
109+
if (strncmp(file_header_v1.magic, REC_MAGIC, sizeof(REC_MAGIC)) != 0)
110+
{
111+
DEBUG_PRINT("invalid osd file");
112+
fclose(osd_fd);
113+
osd_fd = NULL;
114+
return 1;
115+
}
116+
117+
switch (file_header_v1.config.font_variant)
118+
{
119+
case FONT_VARIANT_BETAFLIGHT:
120+
strcpy(file_header.config.font_variant, "BTFL");
121+
break;
122+
case FONT_VARIANT_INAV:
123+
strcpy(file_header.config.font_variant, "INAV");
124+
break;
125+
case FONT_VARIANT_ARDUPILOT:
126+
strcpy(file_header.config.font_variant, "ARDU");
127+
break;
128+
case FONT_VARIANT_KISS_ULTRA:
129+
strcpy(file_header.config.font_variant, "ULTR");
130+
break;
131+
case FONT_VARIANT_QUICKSILVER:
132+
strcpy(file_header.config.font_variant, "QUIC");
133+
break;
134+
default:
135+
file_header.config.font_variant[0] = '\0'; // Empty string
136+
}
137+
138+
memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));
139+
header_size = sizeof(rec_file_header_v1_t);
140+
}
141+
else
94142
{
95143
DEBUG_PRINT("invalid osd file version! expected: %d, got: %d", REC_VERSION, file_header.version);
96144
fclose(osd_fd);
97145
osd_fd = NULL;
98146
return 1;
99147
}
100148

101-
DEBUG_PRINT("header ok!");
102-
memcpy(&osd_config, &file_header.config, sizeof(rec_config_t));
103-
104149
DEBUG_PRINT("loading frame indexes");
105150

106151
fseek(osd_fd, 0, SEEK_END);
107152
uint32_t file_size = ftell(osd_fd);
108-
fseek(osd_fd, sizeof(rec_file_header_t), SEEK_SET);
153+
fseek(osd_fd, header_size, SEEK_SET);
109154
DEBUG_PRINT("file size: %d", file_size);
110155

111156
frame_idx_len = file_size / FRAME_SIZE;
@@ -121,7 +166,7 @@ int rec_pb_start()
121166
fseek(osd_fd, sizeof(uint16_t) * MAX_T, SEEK_CUR);
122167
}
123168

124-
fseek(osd_fd, sizeof(rec_file_header_t), SEEK_SET);
169+
fseek(osd_fd, header_size, SEEK_SET);
125170

126171
current_frame_idx = 0;
127172
frame_counter = rec_pb_cp_vdec->frames_sent;
@@ -179,7 +224,7 @@ int rec_pb_do_next_frame(uint16_t *map_out)
179224

180225
fseek(
181226
osd_fd,
182-
sizeof(rec_file_header_t) + (closest_frame_idx * FRAME_SIZE) + sizeof(rec_frame_header_t),
227+
header_size + (closest_frame_idx * FRAME_SIZE) + sizeof(rec_frame_header_t),
183228
SEEK_SET);
184229
fread(map_out, sizeof(uint16_t), MAX_T, osd_fd);
185230

@@ -203,3 +248,4 @@ bool rec_pb_is_ready()
203248
{
204249
return rec_pb_cp_vdec != NULL && rec_pb_vdec_local_player != NULL;
205250
}
251+

0 commit comments

Comments
 (0)
Failed to load comments.