mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-23 00:36:42 -10:00
Reuse decoded PNG storage for a matching RGB565 software frame and narrow its pixels in place. This removes the output allocation and scaler context while preserving the decoder buffer's existing lifetime and the conversion path for resized replies. Verify pointer identity, colors, zero additional image-task allocations, and allocation failures in both reuse and resized RGB565 paths.
1706 lines
54 KiB
C
1706 lines
54 KiB
C
/* RetroArch - A frontend for libretro.
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
|
* Copyright (C) 2011-2021 - Daniel De Matteis
|
|
*
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <boolean.h>
|
|
#include <compat/strl.h>
|
|
#include <string/stdstring.h>
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "../config.h"
|
|
#endif
|
|
|
|
#include <encodings/base64.h>
|
|
#include <formats/rpng.h>
|
|
#include <formats/rjson.h>
|
|
#include <gfx/scaler/pixconv.h>
|
|
#include <gfx/scaler/scaler.h>
|
|
#include <gfx/video_frame.h>
|
|
#include "../translation_defines.h"
|
|
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
#include "../gfx/gfx_widgets.h"
|
|
#endif
|
|
|
|
#include "../accessibility.h"
|
|
#include "../audio/audio_driver.h"
|
|
#include "../gfx/video_driver.h"
|
|
#include "../frontend/frontend_driver.h"
|
|
#include "../input/input_driver.h"
|
|
#include "../command.h"
|
|
#include "../paths.h"
|
|
#include "../runloop.h"
|
|
#include "../verbosity.h"
|
|
|
|
#include "tasks_internal.h"
|
|
|
|
#ifdef HAVE_TRANSLATE_APPLE
|
|
#include "../ui/drivers/cocoa/translation_driver_apple.h"
|
|
#endif
|
|
|
|
/* ============================================================
|
|
* AI SERVICE BACKEND CONFIGURATION
|
|
* ============================================================ */
|
|
|
|
const char *config_get_ai_service_backend_options(void)
|
|
{
|
|
#ifdef HAVE_TRANSLATE_APPLE
|
|
return "http|apple";
|
|
#else
|
|
return "http";
|
|
#endif
|
|
}
|
|
|
|
const char *config_get_default_ai_service_backend(void)
|
|
{
|
|
return "http";
|
|
}
|
|
|
|
/* ============================================================
|
|
* TRANSLATION DRIVER INTERFACE
|
|
* ============================================================
|
|
* Abstraction layer for translation backends (HTTP, local, etc.)
|
|
*/
|
|
|
|
/* A little-endian 32-bit field of an image header. */
|
|
#define TRANSLATION_LE32(p) \
|
|
( (uint32_t)(p)[0] | ((uint32_t)(p)[1] << 8) \
|
|
| ((uint32_t)(p)[2] << 16) | ((uint32_t)(p)[3] << 24))
|
|
|
|
/* The largest side a server's image may have: past it the decode is
|
|
* refused rather than sized from untrusted fields. */
|
|
#define TRANSLATION_IMAGE_DIM_MAX 16384
|
|
|
|
typedef struct translation_response
|
|
{
|
|
/* Decoded image data (BMP/PNG file bytes, NOT base64) */
|
|
void *image_data;
|
|
size_t image_size;
|
|
|
|
/* Decoded audio data (WAV bytes, NOT base64) */
|
|
void *sound_data;
|
|
size_t sound_size;
|
|
|
|
/* Text for accessibility/TTS */
|
|
char *text;
|
|
|
|
/* Error message (NULL on success) */
|
|
char *error;
|
|
|
|
/* Auto-translate: if true, trigger another translation */
|
|
bool auto_translate;
|
|
|
|
/* Key presses to simulate (space/comma separated) */
|
|
char *key_presses;
|
|
} translation_response_t;
|
|
|
|
typedef void (*translation_response_cb_t)(
|
|
translation_response_t *response,
|
|
void *userdata);
|
|
|
|
typedef struct translation_driver
|
|
{
|
|
/* Human-readable identifier */
|
|
const char *ident;
|
|
|
|
/* Initialize the driver */
|
|
bool (*init)(void);
|
|
|
|
/* Free driver resources */
|
|
void (*free)(void);
|
|
|
|
/* Perform translation (async - results via callback)
|
|
*
|
|
* bgr24_data: Frame data in BGR24 format
|
|
* dims: Frame size, one packed word (VIDEO_SCALE_PACK)
|
|
* source_lang: Source language code or NULL for auto-detect
|
|
* target_lang: Target language code
|
|
* mode: 0=image, 1=speech, 2=narrator, 3=image+speech
|
|
* game_label: "system__gamename" identifier
|
|
* paused: Current pause state
|
|
* callback: Function to call with results
|
|
* userdata: Passed to callback
|
|
*/
|
|
bool (*translate)(
|
|
const uint8_t *bgr24_data,
|
|
unsigned dims,
|
|
const char *source_lang,
|
|
const char *target_lang,
|
|
unsigned mode,
|
|
const char *game_label,
|
|
bool paused,
|
|
translation_response_cb_t callback,
|
|
void *userdata);
|
|
} translation_driver_t;
|
|
|
|
/* Driver declarations */
|
|
static bool http_translate(
|
|
const uint8_t *bit24_image,
|
|
unsigned dims,
|
|
const char *source_lang,
|
|
const char *target_lang,
|
|
unsigned mode,
|
|
const char *sys_lbl,
|
|
bool paused,
|
|
translation_response_cb_t callback,
|
|
void *userdata);
|
|
static translation_driver_t http_translation_driver = {
|
|
"http",
|
|
NULL, /* init */
|
|
NULL, /* free */
|
|
http_translate
|
|
};
|
|
|
|
#ifdef HAVE_TRANSLATE_APPLE
|
|
static translation_driver_t apple_translation_driver;
|
|
#endif
|
|
|
|
static translation_driver_t *translation_drivers[] = {
|
|
&http_translation_driver,
|
|
#ifdef HAVE_TRANSLATE_APPLE
|
|
&apple_translation_driver,
|
|
#endif
|
|
NULL
|
|
};
|
|
|
|
static translation_driver_t *translation_driver_find(const char *ident)
|
|
{
|
|
int i;
|
|
for (i = 0; translation_drivers[i]; i++)
|
|
{
|
|
if (string_is_equal(translation_drivers[i]->ident, ident))
|
|
return translation_drivers[i];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* ============================================================
|
|
* GENERIC RESPONSE HANDLER
|
|
* ============================================================
|
|
* Processes translation results from any backend.
|
|
* Handles: image overlay, audio playback, key presses, TTS
|
|
*/
|
|
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
bool is_narrator_running(bool accessibility_enable)
|
|
{
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
if (is_accessibility_enabled(
|
|
accessibility_enable,
|
|
access_st->enabled))
|
|
{
|
|
frontend_ctx_driver_t *frontend =
|
|
frontend_state_get_ptr()->current_frontend_ctx;
|
|
if (frontend && frontend->is_narrator_running)
|
|
return frontend->is_narrator_running();
|
|
}
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
/* Between looks at whether the speech or the narration has ended. */
|
|
#define AUTO_TRANSLATE_PARK_USEC 50000
|
|
|
|
static void task_auto_translate_handler(retro_task_t *task)
|
|
{
|
|
int *mode_ptr = (int*)task->user_data;
|
|
uint32_t runloop_flags = runloop_get_flags();
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
bool accessibility_enable = config_get_ptr()->bools.accessibility_enable;
|
|
#endif
|
|
uint8_t flg = task_get_flags(task);
|
|
|
|
if ((flg & RETRO_TASK_FLG_CANCELLED) > 0)
|
|
goto task_finished;
|
|
|
|
switch (*mode_ptr)
|
|
{
|
|
case 1: /* Speech Mode */
|
|
#ifdef HAVE_AUDIOMIXER
|
|
if (!audio_driver_is_ai_service_speech_running())
|
|
goto task_finished;
|
|
#endif
|
|
break;
|
|
case 2: /* Narrator Mode */
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (!is_narrator_running(accessibility_enable))
|
|
goto task_finished;
|
|
#endif
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
/* Nothing signals the end of the speech or the narration, so look
|
|
* again shortly. */
|
|
task_queue_park(task, AUTO_TRANSLATE_PARK_USEC);
|
|
return;
|
|
|
|
task_finished:
|
|
if (access_st->ai_service_auto == 1)
|
|
access_st->ai_service_auto = 2;
|
|
|
|
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
|
|
|
|
if (*mode_ptr == 1 || *mode_ptr == 2)
|
|
{
|
|
bool was_paused = (runloop_flags & RUNLOOP_FLAG_PAUSED) ? true : false;
|
|
command_event(CMD_EVENT_AI_SERVICE_CALL, &was_paused);
|
|
}
|
|
if (task->user_data)
|
|
free(task->user_data);
|
|
}
|
|
|
|
static void ai_service_call_auto_translate_task(access_state_t *access_st,
|
|
int ai_service_mode, bool *was_paused)
|
|
{
|
|
/*Image Mode*/
|
|
if (ai_service_mode == 0)
|
|
{
|
|
if (access_st->ai_service_auto == 1)
|
|
access_st->ai_service_auto = 2;
|
|
|
|
command_event(CMD_EVENT_AI_SERVICE_CALL, was_paused);
|
|
}
|
|
else /* Speech or Narrator Mode */
|
|
{
|
|
int* mode = (int*)malloc(sizeof(int));
|
|
retro_task_t *t = task_init();
|
|
if (!t || !mode)
|
|
{
|
|
free(t);
|
|
free(mode);
|
|
return;
|
|
}
|
|
|
|
*mode = ai_service_mode;
|
|
t->user_data = mode;
|
|
t->handler = task_auto_translate_handler;
|
|
/* It waits on the mixer and the narrator, then sets the next
|
|
* request off: the frontend's state, read and written where the
|
|
* frontend runs. */
|
|
t->flags |= RETRO_TASK_FLG_MUTE | RETRO_TASK_FLG_MAIN_THREAD;
|
|
|
|
task_queue_push(t);
|
|
}
|
|
}
|
|
|
|
static void handle_translation_response(
|
|
translation_response_t *response,
|
|
void *user_data)
|
|
{
|
|
uint8_t* raw_output_data = NULL;
|
|
struct scaler_ctx* scaler = NULL;
|
|
const void* raw_image_data = NULL;
|
|
void* raw_image_data_alpha = NULL;
|
|
settings_t* settings = config_get_ptr();
|
|
uint32_t runloop_flags = runloop_get_flags();
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
#endif
|
|
video_driver_state_t
|
|
*video_st = video_state_get_ptr();
|
|
const enum retro_pixel_format
|
|
video_driver_pix_fmt = video_st->pix_fmt;
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
bool gfx_widgets_paused = (video_st->main_flags &
|
|
VIDEO_FLAG_WIDGETS_PAUSED) ? true : false;
|
|
#endif
|
|
bool ai_service_pause = settings->bools.ai_service_pause;
|
|
unsigned ai_service_mode = settings->uints.ai_service_mode;
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
bool accessibility_enable = settings->bools.accessibility_enable;
|
|
unsigned accessibility_narrator_speech_speed = settings->uints.accessibility_narrator_speech_speed;
|
|
#endif
|
|
|
|
if (!response)
|
|
goto finish;
|
|
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
/* When auto mode is on, we turn off the overlay
|
|
* once we have the result for the next call.*/
|
|
if (gfx_widgets_ai_service_overlay_get_state() != 0
|
|
&& access_st->ai_service_auto == 2)
|
|
gfx_widgets_ai_service_overlay_unload();
|
|
#endif
|
|
#endif
|
|
|
|
if (response->error)
|
|
{
|
|
RARCH_ERR("[Translation] %s\n", response->error);
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
if ( string_is_equal(response->error, "No text found.")
|
|
&& gfx_widgets_paused)
|
|
{
|
|
/* In this case we have to unpause and then repause for a frame */
|
|
gfx_widgets_ai_service_overlay_set_state(2);
|
|
command_event(CMD_EVENT_UNPAUSE, NULL);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/* Handle image overlay */
|
|
/* Shorter than a PNG signature is no image at all, and both paths
|
|
* below read the first bytes to tell the format. */
|
|
if ( response->image_data
|
|
&& response->image_size >= 8
|
|
&& response->image_size <= 0x7fffffff)
|
|
{
|
|
char *raw_image_file_data = (char*)response->image_data;
|
|
int new_image_size = (int)response->image_size;
|
|
unsigned image_width, image_height;
|
|
/* Bytes from one row of raw_image_data to the next, and whether
|
|
* the first row in memory is the top of the image. */
|
|
size_t image_row = 0;
|
|
bool image_top_down = false;
|
|
enum scaler_pix_fmt image_fmt = SCALER_FMT_BGR24;
|
|
/* Get the video frame dimensions reference */
|
|
unsigned dims = 0;
|
|
bool is_hw_fb;
|
|
video_driver_cached_frame_info(&dims, NULL, NULL);
|
|
is_hw_fb = video_driver_cached_frame_is_hw_render()
|
|
|| video_driver_pix_fmt == RETRO_PIXEL_FORMAT_SCRGB_RGBA16F;
|
|
|
|
/* try two different modes for text display *
|
|
* In the first mode, we use display widget overlays, but they require
|
|
* the video poke interface to be able to load image buffers.
|
|
*
|
|
* The other method is to draw to the video buffer directly, which needs
|
|
* a software core to be running. */
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
if ( video_st->poke
|
|
&& video_st->poke->load_texture
|
|
&& video_st->poke->unload_texture)
|
|
{
|
|
enum image_type_enum image_type;
|
|
/* Write to overlay */
|
|
if ( raw_image_file_data[0] == 'B'
|
|
&& raw_image_file_data[1] == 'M')
|
|
image_type = IMAGE_TYPE_BMP;
|
|
else if ( raw_image_file_data[1] == 'P'
|
|
&& raw_image_file_data[2] == 'N'
|
|
&& raw_image_file_data[3] == 'G')
|
|
image_type = IMAGE_TYPE_PNG;
|
|
else
|
|
{
|
|
RARCH_LOG("[Translation] Invalid image type.\n");
|
|
goto finish;
|
|
}
|
|
|
|
if (!gfx_widgets_ai_service_overlay_load(
|
|
raw_image_file_data, (unsigned)new_image_size,
|
|
image_type))
|
|
{
|
|
/* TODO/FIXME - localize */
|
|
const char *_msg = "Video driver not supported.";
|
|
RARCH_LOG("[Translation] %s\n", _msg);
|
|
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
|
|
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
|
|
}
|
|
else if (gfx_widgets_paused)
|
|
{
|
|
/* In this case we have to unpause and then repause for a frame */
|
|
/* Unpausing state */
|
|
gfx_widgets_ai_service_overlay_set_state(2);
|
|
command_event(CMD_EVENT_UNPAUSE, NULL);
|
|
}
|
|
}
|
|
else
|
|
#endif
|
|
/* Can't use display widget overlays, so try writing to video buffer */
|
|
{
|
|
size_t pitch;
|
|
/* Write to video buffer directly (software cores only) */
|
|
|
|
/* This is a BMP file coming back: a 54-byte header and 24-bit
|
|
* rows padded to four bytes, bottom-up unless the height is
|
|
* negative. Every field comes from the server's reply, so the
|
|
* pixels it describes must be in that reply. */
|
|
if ( new_image_size >= 54
|
|
&& raw_image_file_data[0] == 'B'
|
|
&& raw_image_file_data[1] == 'M')
|
|
{
|
|
const uint8_t *hdr = (const uint8_t*)raw_image_file_data;
|
|
uint32_t offset = TRANSLATION_LE32(hdr + 10);
|
|
int32_t width = (int32_t)TRANSLATION_LE32(hdr + 18);
|
|
int32_t height = (int32_t)TRANSLATION_LE32(hdr + 22);
|
|
unsigned bpp = hdr[28] | ((unsigned)hdr[29] << 8);
|
|
|
|
image_top_down = height < 0;
|
|
image_width = width > 0 ? (unsigned)width : 0;
|
|
image_height = height < 0 ? (unsigned)-(int64_t)height
|
|
: (unsigned)height;
|
|
image_row = ((size_t)image_width * 3 + 3) & ~(size_t)3;
|
|
|
|
if ( bpp != 24
|
|
|| !image_width
|
|
|| !image_height
|
|
|| image_width > TRANSLATION_IMAGE_DIM_MAX
|
|
|| image_height > TRANSLATION_IMAGE_DIM_MAX
|
|
|| offset < 54
|
|
|| offset > (uint32_t)new_image_size
|
|
|| (uint64_t)image_row * image_height
|
|
> (uint64_t)new_image_size - offset)
|
|
{
|
|
RARCH_LOG("[Translation] BMP size does not match its data.\n");
|
|
goto finish;
|
|
}
|
|
|
|
raw_image_data = raw_image_file_data + offset;
|
|
}
|
|
else if ( new_image_size >= 24
|
|
&& raw_image_file_data[1] == 'P'
|
|
&& raw_image_file_data[2] == 'N'
|
|
&& raw_image_file_data[3] == 'G')
|
|
{
|
|
/* PNG file */
|
|
#ifdef HAVE_RPNG
|
|
int retval = 0;
|
|
rpng_t *rpng = rpng_alloc();
|
|
if (!rpng)
|
|
goto finish;
|
|
|
|
image_width =
|
|
((uint32_t) ((uint8_t)raw_image_file_data[16]) << 24)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[17]) << 16)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[18]) << 8)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[19]) << 0);
|
|
image_height =
|
|
((uint32_t) ((uint8_t)raw_image_file_data[20]) << 24)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[21]) << 16)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[22]) << 8)
|
|
+ ((uint32_t) ((uint8_t)raw_image_file_data[23]) << 0);
|
|
|
|
rpng_set_buf_ptr(rpng, raw_image_file_data, (size_t)new_image_size);
|
|
rpng_start(rpng);
|
|
while (rpng_iterate_image(rpng));
|
|
|
|
do
|
|
{
|
|
retval = rpng_process_image(rpng, &raw_image_data_alpha,
|
|
(size_t)new_image_size, &image_width, &image_height,
|
|
false);
|
|
} while (retval == IMAGE_PROCESS_NEXT);
|
|
|
|
if ( retval == IMAGE_PROCESS_ERROR
|
|
|| retval == IMAGE_PROCESS_ERROR_END
|
|
|| !raw_image_data_alpha
|
|
|| !image_width
|
|
|| !image_height
|
|
|| image_width > TRANSLATION_IMAGE_DIM_MAX
|
|
|| image_height > TRANSLATION_IMAGE_DIM_MAX)
|
|
{
|
|
RARCH_LOG("[Translation] PNG reply did not decode.\n");
|
|
rpng_free(rpng);
|
|
goto finish;
|
|
}
|
|
raw_image_data = raw_image_data_alpha;
|
|
image_row = (size_t)image_width * sizeof(uint32_t);
|
|
image_top_down = true;
|
|
image_fmt = SCALER_FMT_ARGB8888;
|
|
rpng_free(rpng);
|
|
#else
|
|
/* PNG decoding requires RPNG; without it we cannot handle this
|
|
* screenshot format, so fail the request cleanly. */
|
|
goto finish;
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
RARCH_LOG("[Translation] Unsupported image format.\n");
|
|
goto finish;
|
|
}
|
|
|
|
if ( !VIDEO_SCALE_W(dims)
|
|
|| !VIDEO_SCALE_H(dims))
|
|
goto finish;
|
|
|
|
if (is_hw_fb)
|
|
{
|
|
/*
|
|
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.
|
|
*/
|
|
RARCH_LOG("[Translation] Hardware frame buffer core, but selected video driver isn't supported.\n");
|
|
goto finish;
|
|
}
|
|
|
|
if (image_fmt == SCALER_FMT_ARGB8888
|
|
&& (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888
|
|
|| video_driver_pix_fmt == RETRO_PIXEL_FORMAT_RGB565)
|
|
&& image_width == VIDEO_SCALE_W(dims)
|
|
&& image_height == VIDEO_SCALE_H(dims))
|
|
{
|
|
raw_output_data = (uint8_t*)raw_image_data_alpha;
|
|
raw_image_data_alpha = NULL;
|
|
pitch = image_row;
|
|
if (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_RGB565)
|
|
{
|
|
pitch = (size_t)image_width * sizeof(uint16_t);
|
|
conv_argb8888_rgb565(raw_output_data, raw_output_data,
|
|
image_width, image_height, (int)pitch, (int)image_row);
|
|
}
|
|
goto draw_image;
|
|
}
|
|
|
|
if (!(scaler = (struct scaler_ctx*)calloc(1, sizeof(struct scaler_ctx))))
|
|
goto finish;
|
|
|
|
/* The assigned pitch may not be reliable. The width of
|
|
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_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888)
|
|
{
|
|
raw_output_data = (uint8_t*)malloc(VIDEO_SCALE_AREA(dims) * 4 * sizeof(uint8_t));
|
|
scaler->out_fmt = SCALER_FMT_ARGB8888;
|
|
pitch = VIDEO_SCALE_W(dims) * 4;
|
|
scaler->out_stride = (int)pitch;
|
|
}
|
|
else
|
|
{
|
|
raw_output_data = (uint8_t*)malloc(VIDEO_SCALE_AREA(dims) * 2 * sizeof(uint8_t));
|
|
scaler->out_fmt = SCALER_FMT_RGB565;
|
|
pitch = VIDEO_SCALE_W(dims) * 2;
|
|
scaler->out_stride = (int)pitch;
|
|
}
|
|
|
|
if (!raw_output_data)
|
|
goto finish;
|
|
|
|
scaler->in_fmt = image_fmt;
|
|
scaler->in_width = image_width;
|
|
scaler->in_height = image_height;
|
|
scaler->out_width = VIDEO_SCALE_W(dims);
|
|
scaler->out_height = VIDEO_SCALE_H(dims);
|
|
scaler->scaler_type = SCALER_TYPE_POINT;
|
|
if (!scaler_ctx_gen_filter(scaler))
|
|
goto finish;
|
|
|
|
/* The server's image need not match the frame: the input
|
|
* steps by its own rows, top row first, and the output is the
|
|
* frame's size. */
|
|
if (image_top_down)
|
|
{
|
|
scaler->in_stride = (int)image_row;
|
|
scaler_ctx_scale_direct(scaler, raw_output_data,
|
|
raw_image_data);
|
|
}
|
|
else
|
|
{
|
|
scaler->in_stride = -(int)image_row;
|
|
scaler_ctx_scale_direct(scaler, raw_output_data,
|
|
(const uint8_t*)raw_image_data
|
|
+ (image_height - 1) * image_row);
|
|
}
|
|
draw_image:
|
|
if (image_fmt == SCALER_FMT_ARGB8888
|
|
&& video_driver_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888)
|
|
{
|
|
uint32_t *pixels = (uint32_t*)raw_output_data;
|
|
size_t i;
|
|
/* The software frame has always discarded the PNG alpha. */
|
|
for (i = 0; i < VIDEO_SCALE_AREA(dims); i++)
|
|
pixels[i] |= 0xff000000u;
|
|
}
|
|
video_driver_frame(raw_output_data,
|
|
VIDEO_SCALE_W(dims), VIDEO_SCALE_H(dims), pitch);
|
|
}
|
|
}
|
|
|
|
#ifdef HAVE_AUDIOMIXER
|
|
if (response->sound_data && response->sound_size > 0)
|
|
{
|
|
audio_mixer_stream_params_t params;
|
|
|
|
params.volume = 1.0f;
|
|
params.slot_selection_type = AUDIO_MIXER_SLOT_SELECTION_MANUAL; /* user->slot_selection_type; */
|
|
params.slot_selection_idx = 10;
|
|
params.stream_type = AUDIO_STREAM_TYPE_SYSTEM; /* user->stream_type; */
|
|
params.type = AUDIO_MIXER_TYPE_WAV;
|
|
params.state = AUDIO_STREAM_STATE_PLAYING;
|
|
params.buf = response->sound_data;
|
|
params.bufsize = response->sound_size;
|
|
params.cb = NULL;
|
|
params.buf_owner = NULL;
|
|
params.buf_owner_free = NULL;
|
|
params.out_slot = NULL;
|
|
params.basename = NULL;
|
|
|
|
audio_driver_mixer_add_stream(¶ms);
|
|
}
|
|
#endif
|
|
|
|
if (response->key_presses)
|
|
{
|
|
size_t i;
|
|
char key[8];
|
|
size_t _len = strlen(response->key_presses);
|
|
size_t start = 0;
|
|
|
|
for (i = 1; i < _len; i++)
|
|
{
|
|
char t = response->key_presses[i];
|
|
if (i == _len - 1 || t == ' ' || t == ',')
|
|
{
|
|
size_t key_len;
|
|
if (i == _len - 1 && t != ' ' && t!= ',')
|
|
i++;
|
|
|
|
key_len = i - start;
|
|
if (key_len > 7)
|
|
{
|
|
start = i;
|
|
continue;
|
|
}
|
|
|
|
memcpy(key, response->key_presses + start, key_len);
|
|
key[key_len] = '\0';
|
|
|
|
switch (key_len)
|
|
{
|
|
case 1:
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (key[0] == 'b')
|
|
input_driver_ai_gamepad_press(0);
|
|
else if (key[0] == 'y')
|
|
input_driver_ai_gamepad_press(1);
|
|
else if (key[0] == 'a')
|
|
input_driver_ai_gamepad_press(8);
|
|
else if (key[0] == 'x')
|
|
input_driver_ai_gamepad_press(9);
|
|
else if (key[0] == 'l')
|
|
input_driver_ai_gamepad_press(10);
|
|
else if (key[0] == 'r')
|
|
input_driver_ai_gamepad_press(11);
|
|
#endif
|
|
break;
|
|
case 2:
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (memcmp(key, "up", 2) == 0)
|
|
input_driver_ai_gamepad_press(4);
|
|
else if (memcmp(key, "l2", 2) == 0)
|
|
input_driver_ai_gamepad_press(12);
|
|
else if (memcmp(key, "r2", 2) == 0)
|
|
input_driver_ai_gamepad_press(13);
|
|
else if (memcmp(key, "l3", 2) == 0)
|
|
input_driver_ai_gamepad_press(14);
|
|
else if (memcmp(key, "r3", 2) == 0)
|
|
input_driver_ai_gamepad_press(15);
|
|
#endif
|
|
break;
|
|
case 4:
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (memcmp(key, "down", 4) == 0)
|
|
input_driver_ai_gamepad_press(5);
|
|
else if (memcmp(key, "left", 4) == 0)
|
|
input_driver_ai_gamepad_press(6);
|
|
#endif
|
|
break;
|
|
case 5:
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (memcmp(key, "start", 5) == 0)
|
|
input_driver_ai_gamepad_press(3);
|
|
else if (memcmp(key, "right", 5) == 0)
|
|
input_driver_ai_gamepad_press(7);
|
|
else
|
|
#endif
|
|
if (memcmp(key, "pause", 5) == 0)
|
|
command_event(CMD_EVENT_PAUSE, NULL);
|
|
break;
|
|
case 6:
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if (memcmp(key, "select", 6) == 0)
|
|
input_driver_ai_gamepad_press(2);
|
|
#endif
|
|
break;
|
|
case 7:
|
|
if (memcmp(key, "unpause", 7) == 0)
|
|
command_event(CMD_EVENT_UNPAUSE, NULL);
|
|
break;
|
|
}
|
|
|
|
start = i+1;
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
if ( response->text
|
|
&& is_accessibility_enabled(
|
|
accessibility_enable, access_st->enabled))
|
|
accessibility_speak_priority(
|
|
accessibility_enable,
|
|
accessibility_narrator_speech_speed,
|
|
response->text, 10);
|
|
#endif
|
|
|
|
finish:
|
|
if (raw_image_data_alpha)
|
|
free(raw_image_data_alpha);
|
|
if (scaler)
|
|
{
|
|
scaler_ctx_gen_reset(scaler);
|
|
free(scaler);
|
|
}
|
|
if (raw_output_data)
|
|
free(raw_output_data);
|
|
|
|
/* Handle auto-translate */
|
|
if (response->auto_translate)
|
|
{
|
|
bool was_paused = (runloop_flags & RUNLOOP_FLAG_PAUSED) ? true : false;
|
|
if (access_st->ai_service_auto != 0 && !ai_service_pause)
|
|
ai_service_call_auto_translate_task(access_st, ai_service_mode,
|
|
&was_paused);
|
|
}
|
|
}
|
|
|
|
static const char *ai_service_get_str(enum translation_lang id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case TRANSLATION_LANG_EN:
|
|
return "en";
|
|
case TRANSLATION_LANG_ES:
|
|
return "es";
|
|
case TRANSLATION_LANG_FR:
|
|
return "fr";
|
|
case TRANSLATION_LANG_IT:
|
|
return "it";
|
|
case TRANSLATION_LANG_DE:
|
|
return "de";
|
|
case TRANSLATION_LANG_JP:
|
|
return "ja";
|
|
case TRANSLATION_LANG_NL:
|
|
return "nl";
|
|
case TRANSLATION_LANG_CS:
|
|
return "cs";
|
|
case TRANSLATION_LANG_DA:
|
|
return "da";
|
|
case TRANSLATION_LANG_SV:
|
|
return "sv";
|
|
case TRANSLATION_LANG_HR:
|
|
return "hr";
|
|
case TRANSLATION_LANG_KO:
|
|
return "ko";
|
|
case TRANSLATION_LANG_ZH_CN:
|
|
return "zh-CN";
|
|
case TRANSLATION_LANG_ZH_TW:
|
|
return "zh-TW";
|
|
case TRANSLATION_LANG_CA:
|
|
return "ca";
|
|
case TRANSLATION_LANG_BE:
|
|
return "be";
|
|
case TRANSLATION_LANG_BG:
|
|
return "bg";
|
|
case TRANSLATION_LANG_BN:
|
|
return "bn";
|
|
case TRANSLATION_LANG_EU:
|
|
return "eu";
|
|
case TRANSLATION_LANG_AZ:
|
|
return "az";
|
|
case TRANSLATION_LANG_AR:
|
|
return "ar";
|
|
case TRANSLATION_LANG_AST:
|
|
return "ast";
|
|
case TRANSLATION_LANG_SQ:
|
|
return "sq";
|
|
case TRANSLATION_LANG_AF:
|
|
return "af";
|
|
case TRANSLATION_LANG_EO:
|
|
return "eo";
|
|
case TRANSLATION_LANG_ET:
|
|
return "et";
|
|
case TRANSLATION_LANG_TL:
|
|
return "tl";
|
|
case TRANSLATION_LANG_FI:
|
|
return "fi";
|
|
case TRANSLATION_LANG_GL:
|
|
return "gl";
|
|
case TRANSLATION_LANG_KA:
|
|
return "ka";
|
|
case TRANSLATION_LANG_EL:
|
|
return "el";
|
|
case TRANSLATION_LANG_GU:
|
|
return "gu";
|
|
case TRANSLATION_LANG_HT:
|
|
return "ht";
|
|
case TRANSLATION_LANG_HE:
|
|
return "he";
|
|
case TRANSLATION_LANG_HI:
|
|
return "hi";
|
|
case TRANSLATION_LANG_HU:
|
|
return "hu";
|
|
case TRANSLATION_LANG_IS:
|
|
return "is";
|
|
case TRANSLATION_LANG_ID:
|
|
return "id";
|
|
case TRANSLATION_LANG_GA:
|
|
return "ga";
|
|
case TRANSLATION_LANG_KN:
|
|
return "kn";
|
|
case TRANSLATION_LANG_LA:
|
|
return "la";
|
|
case TRANSLATION_LANG_LV:
|
|
return "lv";
|
|
case TRANSLATION_LANG_LT:
|
|
return "lt";
|
|
case TRANSLATION_LANG_MK:
|
|
return "mk";
|
|
case TRANSLATION_LANG_MS:
|
|
return "ms";
|
|
case TRANSLATION_LANG_MT:
|
|
return "mt";
|
|
case TRANSLATION_LANG_NO:
|
|
return "no";
|
|
case TRANSLATION_LANG_FA:
|
|
return "fa";
|
|
case TRANSLATION_LANG_PL:
|
|
return "pl";
|
|
case TRANSLATION_LANG_PT:
|
|
return "pt";
|
|
case TRANSLATION_LANG_RO:
|
|
return "ro";
|
|
case TRANSLATION_LANG_RU:
|
|
return "ru";
|
|
case TRANSLATION_LANG_SR:
|
|
return "sr";
|
|
case TRANSLATION_LANG_SK:
|
|
return "sk";
|
|
case TRANSLATION_LANG_SL:
|
|
return "sl";
|
|
case TRANSLATION_LANG_SW:
|
|
return "sw";
|
|
case TRANSLATION_LANG_TA:
|
|
return "ta";
|
|
case TRANSLATION_LANG_TE:
|
|
return "te";
|
|
case TRANSLATION_LANG_TH:
|
|
return "th";
|
|
case TRANSLATION_LANG_TR:
|
|
return "tr";
|
|
case TRANSLATION_LANG_UK:
|
|
return "uk";
|
|
case TRANSLATION_LANG_UR:
|
|
return "ur";
|
|
case TRANSLATION_LANG_VI:
|
|
return "vi";
|
|
case TRANSLATION_LANG_CY:
|
|
return "cy";
|
|
case TRANSLATION_LANG_YI:
|
|
return "yi";
|
|
case TRANSLATION_LANG_DONT_CARE:
|
|
case TRANSLATION_LANG_LAST:
|
|
break;
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
/* A caller of run_translation_service_notify(), told after the usual
|
|
* handling. */
|
|
typedef struct
|
|
{
|
|
retro_task_callback_t cb;
|
|
void *user_data;
|
|
} translation_notify_t;
|
|
|
|
static void translation_notify(void *userdata, const char *text,
|
|
const char *error)
|
|
{
|
|
translation_notify_t *n = (translation_notify_t*)userdata;
|
|
n->cb(NULL, (void*)text, n->user_data, error);
|
|
free(n);
|
|
}
|
|
|
|
static void handle_translation_response_notify(
|
|
translation_response_t *response, void *userdata)
|
|
{
|
|
handle_translation_response(response, NULL);
|
|
translation_notify(userdata, response->text, response->error);
|
|
}
|
|
|
|
/* Read-side callback used by run_translation_service for the SW
|
|
* core path: convert the cached frame's pixels to BGR24 in-place
|
|
* into a pre-allocated heap buffer. The callback runs inside
|
|
* video_driver_cached_frame_read's lifetime envelope so the
|
|
* source pointer is guaranteed valid for the duration. */
|
|
struct translation_sw_ctx
|
|
{
|
|
struct scaler_ctx *scaler;
|
|
uint8_t *dst;
|
|
unsigned dims;
|
|
bool success;
|
|
};
|
|
|
|
static void translation_sw_convert_cb(void *userdata,
|
|
const void *data,
|
|
unsigned dims, size_t pitch)
|
|
{
|
|
unsigned width = VIDEO_SCALE_W(dims);
|
|
unsigned height = VIDEO_SCALE_H(dims);
|
|
struct translation_sw_ctx *ctx = (struct translation_sw_ctx*)userdata;
|
|
if (!data || !ctx || !ctx->dst)
|
|
return;
|
|
/* The destination was allocated for the earlier snapshot. */
|
|
if (dims != ctx->dims)
|
|
return;
|
|
ctx->success = video_frame_convert_to_bgr24(
|
|
ctx->scaler,
|
|
ctx->dst,
|
|
(const uint8_t*)data + ((int)height - 1) * pitch,
|
|
width, height,
|
|
(int)-pitch,
|
|
width, height,
|
|
width * 3);
|
|
}
|
|
|
|
bool run_translation_service(settings_t *settings, bool paused)
|
|
{
|
|
return run_translation_service_notify(settings, paused, NULL, NULL);
|
|
}
|
|
|
|
bool run_translation_service_notify(settings_t *settings, bool paused,
|
|
retro_task_callback_t cb, void *user_data)
|
|
{
|
|
translation_notify_t *notify = NULL;
|
|
struct video_viewport vp;
|
|
size_t pitch;
|
|
unsigned dims = 0;
|
|
uint8_t *bit24_image = NULL;
|
|
uint8_t *bit24_image_prev = NULL;
|
|
struct scaler_ctx *scaler = NULL;
|
|
bool success = false;
|
|
char *sys_lbl = NULL;
|
|
core_info_t *core_info = NULL;
|
|
video_driver_state_t *video_st = video_state_get_ptr();
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
/* For the case when ai service pause is disabled. */
|
|
if ( (gfx_widgets_ai_service_overlay_get_state() != 0)
|
|
&& (access_st->ai_service_auto == 1))
|
|
{
|
|
gfx_widgets_ai_service_overlay_unload();
|
|
goto finish;
|
|
}
|
|
#endif
|
|
|
|
if (!(scaler = (struct scaler_ctx*)calloc(1, sizeof(struct scaler_ctx))))
|
|
goto finish;
|
|
|
|
/* get the core info here so we can pass long the game name */
|
|
core_info_get_current_core(&core_info);
|
|
|
|
if (core_info)
|
|
{
|
|
size_t lbl_len;
|
|
const char *lbl = NULL;
|
|
const char *sys_id = core_info->system_id
|
|
? core_info->system_id : "core";
|
|
size_t sys_id_len = strlen(sys_id);
|
|
const struct playlist_entry *entry = NULL;
|
|
playlist_t *current_playlist = playlist_get_cached();
|
|
|
|
if (current_playlist)
|
|
{
|
|
playlist_get_index_by_path(
|
|
current_playlist, path_get(RARCH_PATH_CONTENT), &entry);
|
|
|
|
if (entry && entry->label && *entry->label)
|
|
lbl = entry->label;
|
|
}
|
|
|
|
if (!lbl)
|
|
lbl = path_basename(path_get(RARCH_PATH_BASENAME));
|
|
lbl_len = strlen(lbl);
|
|
sys_lbl = (char*)malloc(lbl_len + sys_id_len + 3);
|
|
|
|
if (sys_lbl)
|
|
{
|
|
memcpy(sys_lbl, sys_id, sys_id_len);
|
|
memcpy(sys_lbl + sys_id_len, "__", 2);
|
|
memcpy(sys_lbl + 2 + sys_id_len, lbl, lbl_len);
|
|
sys_lbl[sys_id_len + 2 + lbl_len] = '\0';
|
|
}
|
|
}
|
|
|
|
{
|
|
bool has_cpu_pixels = false;
|
|
if (!video_driver_cached_frame_info(&dims, &pitch,
|
|
&has_cpu_pixels))
|
|
goto finish;
|
|
|
|
if (!has_cpu_pixels
|
|
|| video_st->pix_fmt == RETRO_PIXEL_FORMAT_SCRGB_RGBA16F)
|
|
{
|
|
/* HW render core, or no cached frame yet. Fall back to
|
|
* read_viewport for OCR -- not ideal (the viewport may
|
|
* have shaders applied that degrade OCR quality), but
|
|
* better than nothing.
|
|
*
|
|
* RETRO_HW_FRAME_BUFFER_VALID is treated identically to
|
|
* "no cached frame yet" here: in both cases there are no
|
|
* CPU-side pixels to read directly. */
|
|
vp.pos = VIDEO_POS_PACK(0, 0);
|
|
vp.dims = 0;
|
|
vp.full_dims = 0;
|
|
|
|
video_driver_get_viewport_info(&vp);
|
|
|
|
if (!VIDEO_SCALE_W(vp.dims) || !VIDEO_SCALE_H(vp.dims))
|
|
goto finish;
|
|
|
|
bit24_image_prev = (uint8_t*)malloc(VIDEO_SCALE_AREA(vp.dims) * 3);
|
|
|
|
if (!bit24_image_prev)
|
|
goto finish;
|
|
|
|
if (!( video_st->current_video->read_viewport
|
|
&& video_st->current_video->read_viewport(
|
|
video_st->data, bit24_image_prev, false)))
|
|
{
|
|
RARCH_LOG("[Translation] Could not read viewport for translation service.\n");
|
|
goto finish;
|
|
}
|
|
|
|
if (vp.dims == dims)
|
|
{
|
|
bit24_image = bit24_image_prev;
|
|
bit24_image_prev = NULL;
|
|
}
|
|
else
|
|
{
|
|
bit24_image = (uint8_t*)malloc(VIDEO_SCALE_AREA(dims) * 3);
|
|
if (!bit24_image)
|
|
goto finish;
|
|
scaler->in_fmt = SCALER_FMT_BGR24;
|
|
if (!video_frame_convert_to_bgr24(scaler, bit24_image,
|
|
bit24_image_prev,
|
|
VIDEO_SCALE_W(vp.dims), VIDEO_SCALE_H(vp.dims),
|
|
VIDEO_SCALE_W(vp.dims) * 3,
|
|
VIDEO_SCALE_W(dims), VIDEO_SCALE_H(dims),
|
|
VIDEO_SCALE_W(dims) * 3))
|
|
goto finish;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* SW core path: convert the cached frame to BGR24 inside
|
|
* the cached_frame_read callback. The conversion fully
|
|
* consumes the cached frame's pointer before the call
|
|
* returns, so the heap-owned bit24_image is the only
|
|
* thing that escapes the read API's lifetime envelope.
|
|
*
|
|
* Pre-stage everything the conversion needs (scaler
|
|
* settings, output buffer) before invoking the read --
|
|
* the callback should be as short as possible to keep
|
|
* the lifetime lock (once enforced in the final commit)
|
|
* held briefly. */
|
|
const enum retro_pixel_format
|
|
video_driver_pix_fmt = video_st->pix_fmt;
|
|
|
|
if (!(bit24_image = (uint8_t*)malloc(VIDEO_SCALE_AREA(dims) * 3)))
|
|
goto finish;
|
|
|
|
if (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888)
|
|
scaler->in_fmt = SCALER_FMT_ARGB8888;
|
|
else
|
|
scaler->in_fmt = SCALER_FMT_RGB565;
|
|
|
|
{
|
|
struct translation_sw_ctx ctx;
|
|
ctx.scaler = scaler;
|
|
ctx.dst = bit24_image;
|
|
ctx.dims = dims;
|
|
ctx.success = false;
|
|
video_driver_cached_frame_read(&ctx,
|
|
translation_sw_convert_cb);
|
|
if (!ctx.success)
|
|
goto finish;
|
|
}
|
|
}
|
|
}
|
|
scaler_ctx_gen_reset(scaler);
|
|
|
|
if (!bit24_image)
|
|
goto finish;
|
|
|
|
/* Dispatch to the appropriate backend */
|
|
{
|
|
translation_driver_t *driver = translation_driver_find(
|
|
settings->arrays.ai_service_backend);
|
|
if (!driver || (driver->init && !driver->init()))
|
|
driver = &http_translation_driver; /* fallback */
|
|
|
|
if (driver->translate)
|
|
{
|
|
unsigned ai_service_source_lang = settings->uints.ai_service_source_lang;
|
|
unsigned ai_service_target_lang = settings->uints.ai_service_target_lang;
|
|
unsigned ai_service_mode = settings->uints.ai_service_mode;
|
|
const char *source_lang = NULL;
|
|
const char *target_lang = NULL;
|
|
|
|
if (ai_service_source_lang != TRANSLATION_LANG_DONT_CARE)
|
|
source_lang = ai_service_get_str(
|
|
(enum translation_lang)ai_service_source_lang);
|
|
|
|
if (ai_service_target_lang != TRANSLATION_LANG_DONT_CARE)
|
|
target_lang = ai_service_get_str(
|
|
(enum translation_lang)ai_service_target_lang);
|
|
|
|
if (cb)
|
|
{
|
|
if (!(notify = (translation_notify_t*)malloc(sizeof(*notify))))
|
|
goto finish;
|
|
notify->cb = cb;
|
|
notify->user_data = user_data;
|
|
}
|
|
if (!(success = driver->translate(
|
|
bit24_image, dims,
|
|
source_lang, target_lang,
|
|
ai_service_mode,
|
|
sys_lbl, paused,
|
|
notify ? handle_translation_response_notify
|
|
: handle_translation_response, notify)))
|
|
free(notify);
|
|
}
|
|
}
|
|
|
|
finish:
|
|
if (bit24_image_prev)
|
|
free(bit24_image_prev);
|
|
if (bit24_image)
|
|
free(bit24_image);
|
|
if (scaler)
|
|
free(scaler);
|
|
if (sys_lbl)
|
|
free(sys_lbl);
|
|
return success;
|
|
}
|
|
|
|
/* ============================================================
|
|
* HTTP TRANSLATION DRIVER
|
|
* ============================================================
|
|
* Handles server-based translation via HTTP POST with JSON.
|
|
*/
|
|
|
|
/* Context passed through HTTP task for callback */
|
|
typedef struct
|
|
{
|
|
translation_response_cb_t callback;
|
|
void *userdata;
|
|
} http_translate_ctx_t;
|
|
|
|
static void handle_translation_cb(
|
|
retro_task_t *task, void *task_data,
|
|
void *user_data, const char *error)
|
|
{
|
|
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
|
|
http_translate_ctx_t *ctx = (http_translate_ctx_t*)user_data;
|
|
rjson_t *json = NULL;
|
|
int json_current_key = 0;
|
|
translation_response_t response;
|
|
int new_image_size = 0;
|
|
int new_sound_size = 0;
|
|
char *err_str = NULL;
|
|
char *auto_str = NULL;
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
|
|
/* Initialize response */
|
|
memset(&response, 0, sizeof(response));
|
|
|
|
#ifdef DEBUG
|
|
if (access_st->ai_service_auto != 2)
|
|
RARCH_LOG("[Translation] HTTP: Response received\n");
|
|
#endif
|
|
|
|
if (!data || error || !data->data)
|
|
{
|
|
if (error)
|
|
RARCH_ERR("[Translation] HTTP error: %s\n", error);
|
|
err_str = strdup(error ? error : "The AI service did not answer.");
|
|
goto failed;
|
|
}
|
|
|
|
if (!(json = rjson_open_buffer(data->data, data->len)))
|
|
{
|
|
err_str = strdup("Invalid JSON body.");
|
|
goto failed;
|
|
}
|
|
|
|
/* Parse JSON body for the image and sound data */
|
|
for (;;)
|
|
{
|
|
static const char *keys[] = {
|
|
"image", "sound", "text", "error", "auto", "press"
|
|
};
|
|
const char *str = NULL;
|
|
size_t str_len = 0;
|
|
enum rjson_type json_type = rjson_next(json);
|
|
|
|
if (json_type == RJSON_DONE || json_type == RJSON_ERROR)
|
|
break;
|
|
if (json_type != RJSON_STRING)
|
|
continue;
|
|
if (rjson_get_context_type(json) != RJSON_OBJECT)
|
|
continue;
|
|
str = rjson_get_string(json, &str_len);
|
|
|
|
if ((rjson_get_context_count(json) & 1) == 1)
|
|
{
|
|
int i;
|
|
json_current_key = -1;
|
|
for (i = 0; i < (int)ARRAY_SIZE(keys); i++)
|
|
{
|
|
if (string_is_equal(str, keys[i]))
|
|
{
|
|
json_current_key = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
switch (json_current_key)
|
|
{
|
|
case 0: /* image - base64 encoded */
|
|
response.image_data = unbase64(str, (int)str_len, &new_image_size);
|
|
response.image_size = new_image_size;
|
|
break;
|
|
case 1: /* sound - base64 encoded */
|
|
response.sound_data = unbase64(str, (int)str_len, &new_sound_size);
|
|
response.sound_size = new_sound_size;
|
|
break;
|
|
case 2: /* text */
|
|
response.text = strdup(str);
|
|
break;
|
|
case 3: /* error */
|
|
err_str = strdup(str);
|
|
break;
|
|
case 4: /* auto */
|
|
auto_str = strdup(str);
|
|
break;
|
|
case 5: /* press */
|
|
response.key_presses = strdup(str);
|
|
break;
|
|
}
|
|
json_current_key = -1;
|
|
}
|
|
}
|
|
|
|
/* Handle "No text found" as a special error */
|
|
if (string_is_equal(err_str, "No text found."))
|
|
response.error = err_str;
|
|
else if (err_str)
|
|
{
|
|
response.error = err_str;
|
|
err_str = NULL; /* Transfer ownership */
|
|
}
|
|
|
|
/* Check for auto-translate flag */
|
|
if (auto_str && string_is_equal(auto_str, "auto"))
|
|
response.auto_translate = true;
|
|
|
|
/* Validate we have something to process */
|
|
if ( !response.image_data
|
|
&& !response.sound_data
|
|
&& !response.text
|
|
&& !response.key_presses
|
|
&& !response.error
|
|
&& access_st->ai_service_auto != 2)
|
|
{
|
|
RARCH_ERR("[Translation] Invalid JSON body.\n");
|
|
if (!err_str)
|
|
err_str = strdup("Invalid JSON body.");
|
|
goto failed;
|
|
}
|
|
|
|
/* Hand off to caller's response handler */
|
|
if (ctx && ctx->callback)
|
|
ctx->callback(&response, ctx->userdata);
|
|
goto finish;
|
|
|
|
failed:
|
|
/* Only a notified caller hears of a request that came to nothing;
|
|
* the usual handling never did. */
|
|
if (ctx && ctx->callback == handle_translation_response_notify)
|
|
translation_notify(ctx->userdata, NULL, err_str);
|
|
|
|
finish:
|
|
if (ctx)
|
|
free(ctx);
|
|
if (json)
|
|
rjson_free(json);
|
|
if (auto_str)
|
|
free(auto_str);
|
|
/* Note: response fields are freed by caller or handle_translation_response
|
|
* except for these which we own: */
|
|
if (response.image_data)
|
|
free(response.image_data);
|
|
if (response.sound_data)
|
|
free(response.sound_data);
|
|
if (response.text)
|
|
free(response.text);
|
|
if (response.error && response.error != err_str)
|
|
free(response.error);
|
|
if (err_str)
|
|
free(err_str);
|
|
if (response.key_presses)
|
|
free(response.key_presses);
|
|
}
|
|
|
|
/* ============================================================
|
|
* HTTP TRANSLATION BACKEND
|
|
* ============================================================
|
|
* Sends frame data to a remote translation service via HTTP POST.
|
|
*/
|
|
|
|
static bool http_translate(
|
|
const uint8_t *bit24_image,
|
|
unsigned dims,
|
|
const char *source_lang,
|
|
const char *target_lang,
|
|
unsigned mode,
|
|
const char *sys_lbl,
|
|
bool paused,
|
|
translation_response_cb_t callback,
|
|
void *userdata)
|
|
{
|
|
uint8_t *bmp_buffer = NULL;
|
|
uint64_t buffer_bytes = 0;
|
|
char *bmp64_buffer = NULL;
|
|
rjsonwriter_t *jsonwriter = NULL;
|
|
const char *json_buffer = NULL;
|
|
http_translate_ctx_t *ctx = NULL;
|
|
int bmp64_len = 0;
|
|
bool success = false;
|
|
settings_t *settings = config_get_ptr();
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
video_driver_state_t *video_st = video_state_get_ptr();
|
|
#endif
|
|
#ifdef DEBUG
|
|
access_state_t *access_st = access_state_get_ptr();
|
|
#endif
|
|
|
|
/* Encode the framebuffer screenshot as a PNG (BGR24) for the
|
|
* translation service. rpng_save_image_bgr24_string handles the
|
|
* vertical flip via negative stride + bottom-row pointer, so no
|
|
* intermediate buffer is needed. Variable is named bmp_buffer for
|
|
* historical reasons (the request format was originally a BMP
|
|
* blob gated on an always-false TRANSLATE_USE_BMP local; that
|
|
* branch has been deleted as dead code). */
|
|
{
|
|
size_t pitch = VIDEO_SCALE_W(dims) * 3;
|
|
#ifdef HAVE_RPNG
|
|
bmp_buffer = rpng_save_image_bgr24_string(
|
|
bit24_image + VIDEO_SCALE_W(dims) * (VIDEO_SCALE_H(dims) - 1) * 3,
|
|
VIDEO_SCALE_W(dims), VIDEO_SCALE_H(dims), (signed)-pitch, &buffer_bytes);
|
|
#else
|
|
/* Encoding the screenshot requires RPNG; without it the translation
|
|
* request cannot be built, so fail cleanly below on the NULL buffer. */
|
|
(void)pitch;
|
|
bmp_buffer = NULL;
|
|
#endif
|
|
}
|
|
|
|
if (!(bmp64_buffer = base64((void *)bmp_buffer,
|
|
(int)(sizeof(uint8_t) * buffer_bytes),
|
|
&bmp64_len)))
|
|
goto finish;
|
|
|
|
if (!(jsonwriter = rjsonwriter_open_memory()))
|
|
goto finish;
|
|
|
|
rjsonwriter_raw(jsonwriter, "{", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, "image");
|
|
rjsonwriter_raw(jsonwriter, ":", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string_len(jsonwriter, bmp64_buffer, bmp64_len);
|
|
|
|
/* Form request... */
|
|
if (sys_lbl)
|
|
{
|
|
rjsonwriter_raw(jsonwriter, ",", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, "label");
|
|
rjsonwriter_raw(jsonwriter, ":", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, sys_lbl);
|
|
}
|
|
|
|
rjsonwriter_raw(jsonwriter, ",", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, "state");
|
|
rjsonwriter_raw(jsonwriter, ":", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_raw(jsonwriter, "{", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, "paused");
|
|
rjsonwriter_raw(jsonwriter, ":", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_rawf(jsonwriter, "%u", (paused ? 1 : 0));
|
|
{
|
|
static const char* state_labels[] = { "b", "y", "select", "start", "up", "down", "left", "right", "a", "x", "l", "r", "l2", "r2", "l3", "r3" };
|
|
int i;
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
/* in RetroPad order, as the labels are */
|
|
uint16_t held = input_driver_ai_gamepad_held();
|
|
#endif
|
|
for (i = 0; i < (int)ARRAY_SIZE(state_labels); i++)
|
|
{
|
|
rjsonwriter_raw(jsonwriter, ",", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_add_string(jsonwriter, state_labels[i]);
|
|
rjsonwriter_raw(jsonwriter, ":", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
#ifdef HAVE_ACCESSIBILITY
|
|
rjsonwriter_rawf(jsonwriter, "%u",
|
|
(unsigned)((held >> i) & 1u));
|
|
#else
|
|
rjsonwriter_rawf(jsonwriter, "%u", 0);
|
|
#endif
|
|
}
|
|
}
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_raw(jsonwriter, "}", 1);
|
|
rjsonwriter_raw(jsonwriter, " ", 1);
|
|
rjsonwriter_raw(jsonwriter, "}", 1);
|
|
|
|
if ((json_buffer = rjsonwriter_get_memory_buffer(jsonwriter, NULL)))
|
|
{
|
|
#ifdef DEBUG
|
|
if (access_st->ai_service_auto != 2)
|
|
RARCH_LOG("[Translation] Request size: %d\n", bmp64_len);
|
|
#endif
|
|
|
|
{
|
|
char new_ai_service_url[PATH_MAX_LENGTH];
|
|
char separator = '?';
|
|
const char *ai_service_url = settings->arrays.ai_service_url;
|
|
size_t _len = strlcpy(new_ai_service_url,
|
|
ai_service_url, sizeof(new_ai_service_url));
|
|
|
|
/* if query already exists in url, then use &'s instead */
|
|
if (strrchr(new_ai_service_url, '?'))
|
|
separator = '&';
|
|
|
|
/* source lang */
|
|
if (source_lang && *source_lang)
|
|
{
|
|
new_ai_service_url[ _len] = separator;
|
|
new_ai_service_url[++_len] = '\0';
|
|
_len += strlcpy_lit(new_ai_service_url + _len,
|
|
"source_lang=",
|
|
sizeof(new_ai_service_url) - _len);
|
|
_len += strlcpy(new_ai_service_url + _len,
|
|
source_lang,
|
|
sizeof(new_ai_service_url) - _len);
|
|
separator = '&';
|
|
}
|
|
|
|
/* target lang */
|
|
if (target_lang && *target_lang)
|
|
{
|
|
new_ai_service_url[ _len] = separator;
|
|
new_ai_service_url[++_len] = '\0';
|
|
_len += strlcpy_lit(new_ai_service_url + _len,
|
|
"target_lang=",
|
|
sizeof(new_ai_service_url) - _len);
|
|
_len += strlcpy(new_ai_service_url + _len,
|
|
target_lang,
|
|
sizeof(new_ai_service_url) - _len);
|
|
separator = '&';
|
|
}
|
|
|
|
/* mode */
|
|
{
|
|
/*"image" is included for backwards compatibility with
|
|
* vgtranslate < 1.04 */
|
|
|
|
new_ai_service_url[ _len] = separator;
|
|
new_ai_service_url[++_len] = '\0';
|
|
_len += strlcpy_lit(new_ai_service_url + _len,
|
|
"output=",
|
|
sizeof(new_ai_service_url) - _len);
|
|
|
|
switch (mode)
|
|
{
|
|
case 2:
|
|
strlcpy_lit(new_ai_service_url + _len,
|
|
"text",
|
|
sizeof(new_ai_service_url) - _len);
|
|
break;
|
|
case 1:
|
|
case 3:
|
|
_len += strlcpy_lit(new_ai_service_url + _len,
|
|
"sound,wav",
|
|
sizeof(new_ai_service_url) - _len);
|
|
if (mode == 1)
|
|
break;
|
|
/* fall-through intentional for mode == 3 */
|
|
case 0:
|
|
_len += strlcpy_lit(new_ai_service_url + _len,
|
|
"image,png",
|
|
sizeof(new_ai_service_url) - _len);
|
|
#ifdef HAVE_GFX_WIDGETS
|
|
if ( video_st->poke
|
|
&& video_st->poke->load_texture
|
|
&& video_st->poke->unload_texture)
|
|
strlcpy_lit(new_ai_service_url + _len,
|
|
",png-a",
|
|
sizeof(new_ai_service_url) - _len);
|
|
#endif
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
#ifdef DEBUG
|
|
if (access_st->ai_service_auto != 2)
|
|
RARCH_LOG("[Translation] SENDING... %s\n", new_ai_service_url);
|
|
#endif
|
|
/* Allocate context to pass callback through task system */
|
|
ctx = (http_translate_ctx_t*)malloc(sizeof(*ctx));
|
|
if (ctx)
|
|
{
|
|
ctx->callback = callback;
|
|
ctx->userdata = userdata;
|
|
task_push_http_post_transfer(new_ai_service_url,
|
|
json_buffer, true, NULL, handle_translation_cb, ctx);
|
|
success = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
finish:
|
|
if (bmp_buffer)
|
|
free(bmp_buffer);
|
|
if (bmp64_buffer)
|
|
free(bmp64_buffer);
|
|
if (jsonwriter)
|
|
rjsonwriter_free(jsonwriter);
|
|
return success;
|
|
}
|
|
|
|
/* ============================================================
|
|
* APPLE TRANSLATION DRIVER (Vision OCR)
|
|
* ============================================================
|
|
* On-device OCR using Apple's Vision framework.
|
|
* Available on macOS 10.15+ and iOS 13+.
|
|
*/
|
|
|
|
#ifdef HAVE_TRANSLATE_APPLE
|
|
|
|
/* Log function callable from Swift */
|
|
void apple_translate_log(const char *message)
|
|
{
|
|
RARCH_LOG("%s\n", message);
|
|
}
|
|
|
|
/* Context for async Apple translation callback */
|
|
static struct
|
|
{
|
|
translation_response_cb_t callback;
|
|
void *userdata;
|
|
} apple_translate_ctx;
|
|
|
|
/* Callback for async Apple translation */
|
|
static void handle_apple_translation_cb(
|
|
char *text,
|
|
void *image_data,
|
|
size_t image_size,
|
|
void *sound_data,
|
|
size_t sound_size,
|
|
const char *error,
|
|
void *userdata)
|
|
{
|
|
if (text || image_data || sound_data)
|
|
{
|
|
translation_response_t response;
|
|
memset(&response, 0, sizeof(response));
|
|
response.text = text;
|
|
response.image_data = image_data;
|
|
response.image_size = image_size;
|
|
response.sound_data = sound_data;
|
|
response.sound_size = sound_size;
|
|
response.auto_translate = true;
|
|
if (apple_translate_ctx.callback)
|
|
apple_translate_ctx.callback(&response, apple_translate_ctx.userdata);
|
|
if (text)
|
|
apple_translate_free_string(text);
|
|
if (image_data)
|
|
apple_translate_free_data(image_data);
|
|
if (sound_data)
|
|
apple_translate_free_data(sound_data);
|
|
}
|
|
else
|
|
{
|
|
RARCH_ERR("[Translation] Apple translation failed: %s\n",
|
|
error ? error : "unknown error");
|
|
/* as for the HTTP backend: only a notified caller hears of it */
|
|
if (apple_translate_ctx.callback == handle_translation_response_notify)
|
|
translation_notify(apple_translate_ctx.userdata, NULL,
|
|
error ? error : "Apple translation failed.");
|
|
}
|
|
}
|
|
|
|
static bool apple_translate(
|
|
const uint8_t *bgr24_data,
|
|
unsigned dims,
|
|
const char *source_lang,
|
|
const char *target_lang,
|
|
unsigned mode,
|
|
const char *game_label,
|
|
bool paused,
|
|
translation_response_cb_t callback,
|
|
void *userdata)
|
|
{
|
|
(void)game_label;
|
|
(void)paused;
|
|
|
|
apple_translate_ctx.callback = callback;
|
|
apple_translate_ctx.userdata = userdata;
|
|
|
|
/* Async: callback will be invoked on main thread when done */
|
|
apple_translate_image(
|
|
bgr24_data, VIDEO_SCALE_W(dims), VIDEO_SCALE_H(dims),
|
|
VIDEO_SCALE_W(dims) * 3,
|
|
source_lang, target_lang,
|
|
mode,
|
|
handle_apple_translation_cb, NULL);
|
|
|
|
return true;
|
|
}
|
|
|
|
static translation_driver_t apple_translation_driver = {
|
|
"apple",
|
|
apple_translate_init,
|
|
NULL,
|
|
apple_translate
|
|
};
|
|
|
|
#endif /* HAVE_TRANSLATE_APPLE */
|