RetroArch/gfx/video_filter.c

539 lines
14 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 17:06:50 +00:00
* Copyright (C) 2011-2015 - 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/>.
*/
2015-09-05 18:03:27 +00:00
#include <stdlib.h>
2014-10-22 01:35:04 +00:00
#include <file/config_file_userdata.h>
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
2014-10-21 23:13:05 +00:00
#include <file/dir_list.h>
2015-09-05 18:03:27 +00:00
#include "../dynamic.h"
#include "../file_ext.h"
2015-06-02 15:17:46 +00:00
#include "../general.h"
2014-04-14 21:24:10 +00:00
#include "../performance.h"
2015-09-05 18:03:27 +00:00
#include "video_filter.h"
#include "video_filters/softfilter.h"
struct rarch_soft_plug
{
#ifdef HAVE_DYLIB
dylib_t lib;
#endif
const struct softfilter_implementation *impl;
};
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
2014-04-15 02:05:56 +00:00
struct filter_thread_data
{
sthread_t *thread;
const struct softfilter_work_packet *packet;
scond_t *cond;
slock_t *lock;
void *userdata;
bool die;
bool done;
};
static void filter_thread_loop(void *data)
{
struct filter_thread_data *thr = (struct filter_thread_data*)data;
for (;;)
{
2015-01-09 00:23:58 +00:00
bool die;
2014-04-15 02:05:56 +00:00
slock_lock(thr->lock);
while (thr->done && !thr->die)
scond_wait(thr->cond, thr->lock);
2015-01-09 00:23:58 +00:00
die = thr->die;
2014-04-15 02:05:56 +00:00
slock_unlock(thr->lock);
if (die)
break;
2014-04-28 22:41:29 +00:00
if (thr->packet && thr->packet->work)
thr->packet->work(thr->userdata, thr->packet->thread_data);
2014-04-15 02:05:56 +00:00
slock_lock(thr->lock);
thr->done = true;
scond_signal(thr->cond);
slock_unlock(thr->lock);
}
}
#endif
struct rarch_softfilter
{
config_file_t *conf;
const struct softfilter_implementation *impl;
void *impl_data;
struct rarch_soft_plug *plugs;
unsigned num_plugs;
unsigned max_width, max_height;
enum retro_pixel_format pix_fmt, out_pix_fmt;
2014-04-14 21:46:47 +00:00
struct softfilter_work_packet *packets;
unsigned threads;
2014-04-15 02:05:56 +00:00
#ifdef HAVE_THREADS
struct filter_thread_data *thread_data;
#endif
};
2014-09-09 03:56:12 +00:00
static const struct softfilter_implementation *
softfilter_find_implementation(rarch_softfilter_t *filt, const char *ident)
{
unsigned i;
2015-01-09 00:23:58 +00:00
for (i = 0; i < filt->num_plugs; i++)
{
if (!strcmp(filt->plugs[i].impl->short_ident, ident))
return filt->plugs[i].impl;
}
return NULL;
}
static const struct softfilter_config softfilter_config = {
config_userdata_get_float,
config_userdata_get_int,
config_userdata_get_float_array,
config_userdata_get_int_array,
config_userdata_get_string,
config_userdata_free,
};
static bool create_softfilter_graph(rarch_softfilter_t *filt,
enum retro_pixel_format in_pixel_format,
unsigned max_width, unsigned max_height,
softfilter_simd_mask_t cpu_features,
unsigned threads)
{
2015-08-30 21:51:10 +00:00
unsigned input_fmts, input_fmt, output_fmts, i = 0;
struct config_file_userdata userdata;
2015-06-12 23:18:13 +00:00
char key[64] = {0};
char name[64] = {0};
(void)i;
snprintf(key, sizeof(key), "filter");
if (!config_get_array(filt->conf, key, name, sizeof(name)))
2015-01-24 19:18:44 +00:00
{
RARCH_ERR("Could not find 'filter' array in config.\n");
return false;
2015-01-24 19:18:44 +00:00
}
if (filt->num_plugs == 0)
{
RARCH_ERR("No filter plugs found. Exiting...\n");
return false;
}
filt->impl = softfilter_find_implementation(filt, name);
if (!filt->impl)
2015-01-24 19:18:44 +00:00
{
RARCH_ERR("Could not find implementation.\n");
return false;
2015-01-24 19:18:44 +00:00
}
2014-04-14 22:33:13 +00:00
userdata.conf = filt->conf;
2014-09-09 03:56:12 +00:00
/* Index-specific configs take priority over ident-specific. */
userdata.prefix[0] = key;
userdata.prefix[1] = filt->impl->short_ident;
2014-04-14 22:33:13 +00:00
2014-09-09 03:56:12 +00:00
/* Simple assumptions. */
2014-04-14 22:03:55 +00:00
filt->pix_fmt = in_pixel_format;
2014-04-15 10:40:27 +00:00
input_fmts = filt->impl->query_input_formats();
switch (in_pixel_format)
{
case RETRO_PIXEL_FORMAT_XRGB8888:
input_fmt = SOFTFILTER_FMT_XRGB8888;
break;
case RETRO_PIXEL_FORMAT_RGB565:
input_fmt = SOFTFILTER_FMT_RGB565;
break;
2014-04-15 10:40:27 +00:00
default:
return false;
}
2014-04-14 21:46:47 +00:00
if (!(input_fmt & input_fmts))
2014-04-14 22:33:13 +00:00
{
RARCH_ERR("Softfilter does not support input format.\n");
return false;
2014-04-14 22:33:13 +00:00
}
2014-04-14 21:46:47 +00:00
2014-04-15 10:40:27 +00:00
output_fmts = filt->impl->query_output_formats(input_fmt);
2014-09-09 03:56:12 +00:00
/* If we have a match of input/output formats, use that. */
if (output_fmts & input_fmt)
2014-04-14 22:33:13 +00:00
filt->out_pix_fmt = in_pixel_format;
else if (output_fmts & SOFTFILTER_FMT_XRGB8888)
filt->out_pix_fmt = RETRO_PIXEL_FORMAT_XRGB8888;
else if (output_fmts & SOFTFILTER_FMT_RGB565)
filt->out_pix_fmt = RETRO_PIXEL_FORMAT_RGB565;
else
{
RARCH_ERR("Did not find suitable output format for softfilter.\n");
return false;
2014-04-14 22:33:13 +00:00
}
2014-04-14 21:46:47 +00:00
filt->max_width = max_width;
filt->max_height = max_height;
2014-04-14 21:24:10 +00:00
2014-09-09 03:56:12 +00:00
filt->impl_data = filt->impl->create(
&softfilter_config, input_fmt, input_fmt, max_width, max_height,
threads != RARCH_SOFTFILTER_THREADS_AUTO ? threads :
2015-09-20 08:02:47 +00:00
retro_get_cpu_cores(), cpu_features,
&userdata);
2014-04-14 21:46:47 +00:00
if (!filt->impl_data)
2014-04-14 22:33:13 +00:00
{
RARCH_ERR("Failed to create softfilter state.\n");
return false;
2014-04-14 22:33:13 +00:00
}
2014-04-14 21:46:47 +00:00
threads = filt->impl->query_num_threads(filt->impl_data);
2014-04-14 22:33:13 +00:00
if (!threads)
{
RARCH_ERR("Invalid number of threads.\n");
return false;
2014-04-14 22:33:13 +00:00
}
RARCH_LOG("Using %u threads for softfilter.\n", threads);
2014-09-09 03:56:12 +00:00
filt->packets = (struct softfilter_work_packet*)
calloc(threads, sizeof(*filt->packets));
2014-04-14 21:46:47 +00:00
if (!filt->packets)
2014-04-14 22:33:13 +00:00
{
RARCH_ERR("Failed to allocate softfilter packets.\n");
return false;
2014-04-14 22:33:13 +00:00
}
2014-04-14 21:46:47 +00:00
2014-04-15 02:05:56 +00:00
#ifdef HAVE_THREADS
2014-09-09 03:56:12 +00:00
filt->thread_data = (struct filter_thread_data*)
calloc(threads, sizeof(*filt->thread_data));
2014-04-15 02:05:56 +00:00
if (!filt->thread_data)
return false;
2014-04-14 22:33:13 +00:00
filt->threads = threads;
2014-04-15 02:05:56 +00:00
for (i = 0; i < threads; i++)
{
filt->thread_data[i].userdata = filt->impl_data;
filt->thread_data[i].done = true;
filt->thread_data[i].lock = slock_new();
if (!filt->thread_data[i].lock)
return false;
2014-04-15 02:05:56 +00:00
filt->thread_data[i].cond = scond_new();
if (!filt->thread_data[i].cond)
return false;
2014-09-09 03:56:12 +00:00
filt->thread_data[i].thread = sthread_create(
filter_thread_loop, &filt->thread_data[i]);
2014-04-15 02:05:56 +00:00
if (!filt->thread_data[i].thread)
return false;
}
#endif
return true;
}
2014-08-22 08:45:58 +00:00
#ifdef HAVE_DYLIB
2014-09-09 03:56:12 +00:00
static bool append_softfilter_plugs(rarch_softfilter_t *filt,
struct string_list *list)
{
unsigned i;
2015-09-20 08:02:47 +00:00
softfilter_simd_mask_t mask = retro_get_cpu_features();
for (i = 0; i < list->size; i++)
{
2015-01-09 00:23:58 +00:00
softfilter_get_implementation_t cb;
const struct softfilter_implementation *impl = NULL;
struct rarch_soft_plug *new_plugs = NULL;
dylib_t lib = dylib_load(list->elems[i].data);
2015-01-09 00:23:58 +00:00
if (!lib)
continue;
2015-01-09 00:23:58 +00:00
cb = (softfilter_get_implementation_t)
2014-09-09 03:56:12 +00:00
dylib_proc(lib, "softfilter_get_implementation");
2015-01-09 00:23:58 +00:00
if (!cb)
{
dylib_close(lib);
continue;
}
2015-01-09 00:23:58 +00:00
impl = cb(mask);
if (!impl)
{
dylib_close(lib);
continue;
}
if (impl->api_version != SOFTFILTER_API_VERSION)
{
dylib_close(lib);
continue;
}
2015-01-09 00:23:58 +00:00
new_plugs = (struct rarch_soft_plug*)
2014-09-09 03:56:12 +00:00
realloc(filt->plugs, sizeof(*filt->plugs) * (filt->num_plugs + 1));
if (!new_plugs)
{
dylib_close(lib);
return false;
}
2014-09-09 03:56:12 +00:00
RARCH_LOG("[SoftFilter]: Found plug: %s (%s).\n",
impl->ident, impl->short_ident);
filt->plugs = new_plugs;
filt->plugs[filt->num_plugs].lib = lib;
filt->plugs[filt->num_plugs].impl = impl;
filt->num_plugs++;
}
return true;
}
2015-01-01 02:26:22 +00:00
#elif defined(HAVE_FILTERS_BUILTIN)
2014-08-22 08:45:58 +00:00
extern const struct softfilter_implementation *blargg_ntsc_snes_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *lq2x_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *phosphor2x_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *twoxbr_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *epx_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *twoxsai_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *supereagle_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *supertwoxsai_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *twoxbr_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *darken_get_implementation(softfilter_simd_mask_t simd);
extern const struct softfilter_implementation *scale2x_get_implementation(softfilter_simd_mask_t simd);
static const softfilter_get_implementation_t soft_plugs_builtin[] = {
blargg_ntsc_snes_get_implementation,
lq2x_get_implementation,
phosphor2x_get_implementation,
twoxbr_get_implementation,
darken_get_implementation,
twoxsai_get_implementation,
supertwoxsai_get_implementation,
supereagle_get_implementation,
epx_get_implementation,
scale2x_get_implementation,
};
2015-01-07 20:48:42 +00:00
static bool append_softfilter_plugs(rarch_softfilter_t *filt,
struct string_list *list)
2014-08-22 08:45:58 +00:00
{
unsigned i;
2015-09-20 08:02:47 +00:00
softfilter_simd_mask_t mask = retro_get_cpu_features();
2014-08-22 08:45:58 +00:00
2015-01-07 20:48:42 +00:00
(void)list;
2014-09-09 03:56:12 +00:00
filt->plugs = (struct rarch_soft_plug*)
calloc(ARRAY_SIZE(soft_plugs_builtin), sizeof(*filt->plugs));
2015-01-09 00:23:58 +00:00
2014-08-22 08:45:58 +00:00
if (!filt->plugs)
return false;
2015-01-09 00:23:58 +00:00
2014-08-22 08:45:58 +00:00
filt->num_plugs = ARRAY_SIZE(soft_plugs_builtin);
for (i = 0; i < ARRAY_SIZE(soft_plugs_builtin); i++)
{
filt->plugs[i].impl = soft_plugs_builtin[i](mask);
if (!filt->plugs[i].impl)
return false;
}
return true;
}
2015-01-01 02:26:22 +00:00
#else
2015-01-07 20:49:40 +00:00
static bool append_softfilter_plugs(rarch_softfilter_t *filt,
struct string_list *list)
2015-01-01 02:26:22 +00:00
{
2015-01-07 20:49:40 +00:00
(void)filt;
(void)list;
2015-01-01 02:26:22 +00:00
return false;
}
#endif
rarch_softfilter_t *rarch_softfilter_new(const char *filter_config,
unsigned threads,
enum retro_pixel_format in_pixel_format,
unsigned max_width, unsigned max_height)
{
2015-09-20 08:02:47 +00:00
softfilter_simd_mask_t cpu_features = retro_get_cpu_features();
2015-06-12 23:18:13 +00:00
char basedir[PATH_MAX_LENGTH] = {0};
struct string_list *plugs = NULL;
rarch_softfilter_t *filt = NULL;
2015-01-07 20:48:42 +00:00
(void)basedir;
2015-01-07 20:48:42 +00:00
filt = (rarch_softfilter_t*)calloc(1, sizeof(*filt));
if (!filt)
return NULL;
filt->conf = config_file_new(filter_config);
if (!filt->conf)
{
RARCH_ERR("[SoftFilter]: Did not find config: %s\n", filter_config);
goto error;
2014-04-15 02:05:56 +00:00
}
2014-08-22 08:45:58 +00:00
#if defined(HAVE_DYLIB)
fill_pathname_basedir(basedir, filter_config, sizeof(basedir));
plugs = dir_list_new(basedir, EXT_EXECUTABLES, false, false);
if (!plugs)
2015-01-24 19:18:44 +00:00
{
RARCH_ERR("[SoftFilter]: Could not build up string list...\n");
goto error;
2015-01-24 19:18:44 +00:00
}
2015-01-07 20:48:42 +00:00
#endif
if (!append_softfilter_plugs(filt, plugs))
2015-01-24 19:18:44 +00:00
{
RARCH_ERR("[SoftFitler]: Failed to append softfilter plugins...\n");
goto error;
2015-01-24 19:18:44 +00:00
}
2015-01-07 20:48:42 +00:00
if (plugs)
string_list_free(plugs);
plugs = NULL;
2014-04-15 02:05:56 +00:00
if (!create_softfilter_graph(filt, in_pixel_format,
max_width, max_height, cpu_features, threads))
2015-01-24 19:18:44 +00:00
{
RARCH_ERR("[SoftFitler]: Failed to create softfilter graph...\n");
goto error;
2015-01-24 19:18:44 +00:00
}
return filt;
error:
2015-01-07 20:48:42 +00:00
if (plugs)
string_list_free(plugs);
plugs = NULL;
rarch_softfilter_free(filt);
return NULL;
}
void rarch_softfilter_free(rarch_softfilter_t *filt)
{
unsigned i = 0;
2014-04-17 20:10:37 +00:00
(void)i;
if (!filt)
return;
2014-04-14 21:46:47 +00:00
free(filt->packets);
if (filt->impl && filt->impl_data)
filt->impl->destroy(filt->impl_data);
#ifdef HAVE_DYLIB
for (i = 0; i < filt->num_plugs; i++)
{
if (filt->plugs[i].lib)
dylib_close(filt->plugs[i].lib);
}
free(filt->plugs);
#endif
2014-04-15 02:05:56 +00:00
#ifdef HAVE_THREADS
for (i = 0; i < filt->threads; i++)
{
if (!filt->thread_data[i].thread)
continue;
slock_lock(filt->thread_data[i].lock);
filt->thread_data[i].die = true;
scond_signal(filt->thread_data[i].cond);
slock_unlock(filt->thread_data[i].lock);
sthread_join(filt->thread_data[i].thread);
slock_free(filt->thread_data[i].lock);
scond_free(filt->thread_data[i].cond);
}
free(filt->thread_data);
#endif
free(filt);
}
void rarch_softfilter_get_max_output_size(rarch_softfilter_t *filt,
unsigned *width, unsigned *height)
{
2014-09-09 03:56:12 +00:00
rarch_softfilter_get_output_size(filt, width, height,
filt->max_width, filt->max_height);
}
void rarch_softfilter_get_output_size(rarch_softfilter_t *filt,
unsigned *out_width, unsigned *out_height,
unsigned width, unsigned height)
{
if (filt && filt->impl && filt->impl->query_output_size)
2014-09-09 03:56:12 +00:00
filt->impl->query_output_size(filt->impl_data, out_width,
out_height, width, height);
}
2014-09-09 03:56:12 +00:00
enum retro_pixel_format rarch_softfilter_get_output_format(
rarch_softfilter_t *filt)
{
return filt->out_pix_fmt;
}
void rarch_softfilter_process(rarch_softfilter_t *filt,
void *output, size_t output_stride,
const void *input, unsigned width, unsigned height, size_t input_stride)
{
2014-04-14 21:46:47 +00:00
unsigned i;
if (filt && filt->impl && filt->impl->get_work_packets)
filt->impl->get_work_packets(filt->impl_data, filt->packets,
output, output_stride, input, width, height, input_stride);
2014-04-14 21:46:47 +00:00
2014-04-15 02:05:56 +00:00
#ifdef HAVE_THREADS
2014-09-09 03:56:12 +00:00
/* Fire off workers */
2014-04-15 02:05:56 +00:00
for (i = 0; i < filt->threads; i++)
{
2014-09-09 03:56:12 +00:00
#if 0
RARCH_LOG("Firing off filter thread %u ...\n", i);
#endif
2014-04-15 02:05:56 +00:00
filt->thread_data[i].packet = &filt->packets[i];
slock_lock(filt->thread_data[i].lock);
filt->thread_data[i].done = false;
scond_signal(filt->thread_data[i].cond);
slock_unlock(filt->thread_data[i].lock);
}
2014-09-09 03:56:12 +00:00
/* Wait for workers */
2014-04-15 02:05:56 +00:00
for (i = 0; i < filt->threads; i++)
{
2014-09-09 03:56:12 +00:00
#if 0
RARCH_LOG("Waiting for filter thread %u ...\n", i);
#endif
2014-04-15 02:05:56 +00:00
slock_lock(filt->thread_data[i].lock);
while (!filt->thread_data[i].done)
scond_wait(filt->thread_data[i].cond, filt->thread_data[i].lock);
slock_unlock(filt->thread_data[i].lock);
}
#else
2014-04-14 21:46:47 +00:00
for (i = 0; i < filt->threads; i++)
filt->packets[i].work(filt->impl_data, filt->packets[i].thread_data);
2014-04-15 02:05:56 +00:00
#endif
}