(Translation) Enable by default for PC platforms, fix CXX_BUILD

This commit is contained in:
twinaphex 2019-08-21 20:57:08 +02:00
parent fea7694027
commit ccc6d65c43
2 changed files with 53 additions and 57 deletions

View File

@ -133,7 +133,7 @@ HAVE_CHEEVOS=yes # Retro Achievements
HAVE_LUA=no # Lua support (for Retro Achievements)
HAVE_DISCORD=yes # Discord Integration
C89_DISCORD=no
HAVE_TRANSLATE=no # OCR and Translation Server Integration
HAVE_TRANSLATE=yes # OCR and Translation Server Integration
HAVE_SHADERPIPELINE=yes # Additional shader-based pipelines
C89_SHADERPIPELINE=no
HAVE_VULKAN=auto # Vulkan support

View File

@ -26,10 +26,6 @@ typedef struct nbio_buf
char *path;
} nbio_buf_t;
/*
bool g_translation_service_status = false;
*/
static void form_bmp_header(uint8_t *header,
unsigned width, unsigned height,
bool is32bpp)
@ -113,7 +109,10 @@ static void form_bmp_header(uint8_t *header,
static void handle_translation_cb(
retro_task_t *task, void *task_data, void *user_data, const char *error)
{
size_t pitch;
char curr;
unsigned width, height;
unsigned image_width, image_height;
char* body_copy = NULL;
uint8_t* raw_output_data = NULL;
char* raw_bmp_data = NULL;
@ -131,10 +130,7 @@ static void handle_translation_cb(
int new_image_size = 0;
int new_sound_size = 0;
unsigned width, height;
unsigned image_width, image_height;
size_t pitch;
const void* dummy_data;
const void* dummy_data = NULL;
void* raw_image_data = NULL;
void* raw_sound_data = NULL;
settings_t *settings = config_get_ptr();
@ -165,7 +161,7 @@ static void handle_translation_cb(
while (true)
{
curr = (char) *(body_copy+i);
curr = (char)*(body_copy+i);
if (curr == '\0')
break;
if (curr == '\"')
@ -174,19 +170,19 @@ static void handle_translation_cb(
start = i;
else
{
found_string = malloc(i-start);
found_string = (char*)malloc(i-start);
strncpy(found_string, body_copy+start+1, i-start-1);
*(found_string+i-start-1) = '\0';
if (curr_state == 1)/*image*/
{
raw_bmp_data = (void*) unbase64(found_string,
raw_bmp_data = (char*)unbase64(found_string,
strlen(found_string),
&new_image_size);
curr_state = 0;
}
else if (curr_state == 2)
{
raw_sound_data = (void*) unbase64(found_string,
raw_sound_data = (void*)unbase64(found_string,
strlen(found_string), &new_sound_size);
curr_state = 0;
}
@ -221,33 +217,34 @@ static void handle_translation_cb(
{
/* Get the video frame dimensions reference */
video_driver_cached_frame_get(&dummy_data, &width, &height, &pitch);
/* Get image data (24 bit), and conver to the emulated pixel format */
image_width = ((uint32_t) ((uint8_t) raw_bmp_data[21]) << 24) +
((uint32_t) ((uint8_t) raw_bmp_data[20]) << 16) +
((uint32_t) ((uint8_t) raw_bmp_data[19]) << 8) +
((uint32_t) ((uint8_t) raw_bmp_data[18]) << 0);
((uint32_t) ((uint8_t) raw_bmp_data[20]) << 16) +
((uint32_t) ((uint8_t) raw_bmp_data[19]) << 8) +
((uint32_t) ((uint8_t) raw_bmp_data[18]) << 0);
image_height = ((uint32_t) ((uint8_t) raw_bmp_data[25]) << 24) +
((uint32_t) ((uint8_t) raw_bmp_data[24]) << 16) +
((uint32_t) ((uint8_t) raw_bmp_data[23]) << 8) +
((uint32_t) ((uint8_t) raw_bmp_data[22]) << 0);
((uint32_t) ((uint8_t) raw_bmp_data[24]) << 16) +
((uint32_t) ((uint8_t) raw_bmp_data[23]) << 8) +
((uint32_t) ((uint8_t) raw_bmp_data[22]) << 0);
raw_image_data = raw_bmp_data+54*sizeof(uint8_t);
scaler = calloc(1, sizeof(struct scaler_ctx));
scaler = (struct scaler_ctx*)calloc(1, sizeof(struct scaler_ctx));
if (!scaler)
goto finish;
if (dummy_data == RETRO_HW_FRAME_BUFFER_VALID)
{
/*
In this case, we used the viewport to grab the image
and translate it, and we have the translated image in
the raw_image_data buffer.
*/
In this case, we used the viewport to grab the image
and translate it, and we have the translated image in
the raw_image_data buffer.
*/
/* TODO: write to the viewport in this case */
RARCH_LOG("Hardware frame buffer... writing to viewport not yet supported.\n");
RARCH_LOG("Hardware frame buffer... writing to viewport"
" not yet supported.\n");
goto finish;
}
@ -255,37 +252,37 @@ static void handle_translation_cb(
the video frame can change during run-time, but the
pitch may not, so we just assign it as the width
times the byte depth.
*/
*/
if (video_driver_get_pixel_format() == RETRO_PIXEL_FORMAT_XRGB8888)
{
raw_output_data = (uint8_t*) malloc(width*height*4*sizeof(uint8_t));
raw_output_data = (uint8_t*)malloc(width*height*4*sizeof(uint8_t));
scaler->out_fmt = SCALER_FMT_ARGB8888;
pitch = width*4;
scaler->out_stride = width*4;
}
else
{
raw_output_data = (uint8_t*) malloc(width*height*2*sizeof(uint8_t));
raw_output_data = (uint8_t*)malloc(width*height*2*sizeof(uint8_t));
scaler->out_fmt = SCALER_FMT_RGB565;
pitch = width*2;
scaler->out_stride = width*1;
}
if (!raw_output_data)
goto finish;
scaler->in_fmt = SCALER_FMT_BGR24;
scaler->in_width = image_width;
scaler->in_height = image_height;
scaler->out_width = width;
scaler->out_height = height;
scaler->scaler_type = SCALER_TYPE_POINT;
scaler->in_fmt = SCALER_FMT_BGR24;
scaler->in_width = image_width;
scaler->in_height = image_height;
scaler->out_width = width;
scaler->out_height = height;
scaler->scaler_type = SCALER_TYPE_POINT;
scaler_ctx_gen_filter(scaler);
scaler->in_stride = -1*width*3;
scaler->in_stride = -1*width*3;
scaler_ctx_scale_direct(scaler, raw_output_data,
(uint8_t*)raw_image_data+(image_height-1)*width*3);
(uint8_t*)raw_image_data+(image_height-1)*width*3);
video_driver_frame(raw_output_data, image_width, image_height, pitch);
}
@ -354,9 +351,9 @@ finish:
static void call_translation_server(const char* body)
{
settings_t *settings = config_get_ptr();
settings_t *settings = config_get_ptr();
RARCH_LOG("Server url: %s\n", settings->arrays.ai_service_url);
RARCH_LOG("Server URL: %s\n", settings->arrays.ai_service_url);
task_push_http_post_transfer(settings->arrays.ai_service_url,
body, true, NULL, handle_translation_cb, NULL);
}
@ -365,7 +362,7 @@ bool run_translation_service(void)
{
/*
This function does all the stuff needed to translate the game screen,
using the url given in the settings. Once the image from the frame
using the URL given in the settings. Once the image from the frame
buffer is sent to the server, the callback will write the translated
image to the screen.
@ -384,9 +381,9 @@ bool run_translation_service(void)
an internet connection, and you may have to sign up for it.
To make your own server, it must listen for a POST request, which
will consist of a json body, with the "image" field as a base64
will consist of a JSON body, with the "image" field as a base64
encoded string of a 24bit-BMP that the will be translated. The server
must output the translated image in the form of a json body, with
must output the translated image in the form of a JSON body, with
the "image" field also as a base64 encoded, 24bit-BMP.
*/
@ -397,7 +394,8 @@ bool run_translation_service(void)
uint8_t *bit24_image_prev = NULL;
enum retro_pixel_format pixel_format = video_driver_get_pixel_format();
struct scaler_ctx *scaler = calloc(1, sizeof(struct scaler_ctx));
struct scaler_ctx *scaler = (struct scaler_ctx*)
calloc(1, sizeof(struct scaler_ctx));
bool error = false;
uint8_t* bmp_buffer = NULL;
@ -409,10 +407,8 @@ bool run_translation_service(void)
uint8_t header[54];
int out_length = 0;
char* rf1 = "{\"image\": \"";
char* rf2 = "\"}\0";
const char *rf1 = "{\"image\": \"";
const char *rf2 = "\"}\0";
if (!scaler)
goto finish;
@ -476,7 +472,7 @@ bool run_translation_service(void)
}
else
{
bit24_image = (uint8_t*) malloc(width*height*3);
bit24_image = (uint8_t*)malloc(width*height*3);
if (!bit24_image)
goto finish;
@ -492,7 +488,7 @@ bool run_translation_service(void)
}
video_frame_convert_to_bgr24(
scaler,
(uint8_t *) bit24_image,
(uint8_t *)bit24_image,
(const uint8_t*)data + ((int)height - 1)*pitch,
width, height,
-pitch);
@ -519,19 +515,19 @@ bool run_translation_service(void)
memcpy(bmp_buffer, header, 54*sizeof(uint8_t));
memcpy(bmp_buffer+54, bit24_image, width*height*3*sizeof(uint8_t));
bmp64_buffer = base64((void *) bmp_buffer, (int)(width*height*3+54),
&out_length);
bmp64_buffer = base64((void *)bmp_buffer, (int)(width*height*3+54),
&out_length);
if (!bmp64_buffer)
goto finish;
/* Form request... */
json_buffer = malloc(11+3+out_length);
json_buffer = (char*)malloc(11+3+out_length);
if (!json_buffer)
goto finish;
memcpy(json_buffer, rf1, 11*sizeof(uint8_t));
memcpy(json_buffer, (const void*)rf1, 11*sizeof(uint8_t));
memcpy(json_buffer+11, bmp64_buffer, (out_length)*sizeof(uint8_t));
memcpy(json_buffer+11+out_length, rf2, 3*sizeof(uint8_t));
memcpy(json_buffer+11+out_length, (const void*)rf2, 3*sizeof(uint8_t));
call_translation_server(json_buffer);
error = false;