RetroArch/record/record_driver.c

425 lines
12 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2011-12-24 12:46:12 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-12-24 12:46:12 +00:00
* 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-12-24 12:46:12 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-12-24 12:46:12 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2014-05-04 09:47:55 +00:00
#include <string.h>
2015-04-15 10:42:36 +00:00
#include <file/file_path.h>
2015-01-12 20:55:41 +00:00
#include "record_driver.h"
#include "../driver.h"
#include "../dynamic.h"
#include "../general.h"
#include "../retroarch.h"
#include "../runloop.h"
#include "../intl/intl.h"
2015-03-22 20:57:17 +00:00
#include "../gfx/video_driver.h"
2015-02-14 04:52:05 +00:00
#include "../gfx/video_viewport.h"
2011-12-25 11:47:47 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2015-04-15 11:37:38 +00:00
static const record_driver_t *record_drivers[] = {
2014-05-04 09:47:55 +00:00
#ifdef HAVE_FFMPEG
&ffemu_ffmpeg,
2012-09-02 21:49:30 +00:00
#endif
2015-04-15 11:37:38 +00:00
&ffemu_null,
2014-05-04 09:47:55 +00:00
NULL,
2011-12-25 15:11:48 +00:00
};
2011-01-03 16:51:17 +00:00
2015-04-15 11:37:38 +00:00
/**
* record_driver_find_ident:
* @idx : index of driver to get handle to.
*
* Returns: Human-readable identifier of record driver at index. Can be NULL
* if nothing found.
**/
const char *record_driver_find_ident(int idx)
{
const record_driver_t *drv = record_drivers[idx];
if (!drv)
return NULL;
return drv->ident;
}
/**
* record_driver_find_handle:
* @idx : index of driver to get handle to.
*
* Returns: handle to record driver at index. Can be NULL
* if nothing found.
**/
const void *record_driver_find_handle(int idx)
{
const void *drv = record_drivers[idx];
if (!drv)
return NULL;
return drv;
}
/**
* config_get_record_driver_options:
*
* Get an enumerated list of all record driver names, separated by '|'.
*
* Returns: string listing of all record driver names, separated by '|'.
**/
const char* config_get_record_driver_options(void)
{
union string_list_elem_attr attr;
unsigned i;
char *options = NULL;
int options_len = 0;
struct string_list *options_l = string_list_new();
attr.i = 0;
if (!options_l)
return NULL;
for (i = 0; record_driver_find_handle(i); i++)
{
const char *opt = record_driver_find_ident(i);
options_len += strlen(opt) + 1;
string_list_append(options_l, opt, attr);
}
options = (char*)calloc(options_len, sizeof(char));
if (!options)
{
string_list_free(options_l);
options_l = NULL;
return NULL;
}
string_list_join_concat(options, options_len, options_l, "|");
string_list_free(options_l);
options_l = NULL;
return options;
}
void find_record_driver(void)
{
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
int i = find_driver_index("record_driver", settings->record.driver);
if (i >= 0)
driver->recording = (const record_driver_t*)audio_driver_find_handle(i);
else
{
unsigned d;
RARCH_ERR("Couldn't find any audio driver named \"%s\"\n",
settings->audio.driver);
RARCH_LOG_OUTPUT("Available audio drivers are:\n");
for (d = 0; audio_driver_find_handle(d); d++)
RARCH_LOG_OUTPUT("\t%s\n", record_driver_find_ident(d));
RARCH_WARN("Going to default to first audio driver...\n");
driver->audio = (const audio_driver_t*)audio_driver_find_handle(0);
if (!driver->audio)
rarch_fail(1, "find_audio_driver()");
}
}
2015-01-12 02:50:54 +00:00
/**
* ffemu_find_backend:
* @ident : Identifier of driver to find.
*
* Finds a recording driver with the name @ident.
*
* Returns: recording driver handle if successful, otherwise
* NULL.
**/
2015-04-15 11:37:38 +00:00
const record_driver_t *ffemu_find_backend(const char *ident)
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-01-12 02:50:54 +00:00
2015-04-15 11:37:38 +00:00
for (i = 0; record_drivers[i]; i++)
{
2015-04-15 11:37:38 +00:00
if (!strcmp(record_drivers[i]->ident, ident))
return record_drivers[i];
}
2011-01-03 16:51:17 +00:00
return NULL;
}
2015-04-15 11:37:38 +00:00
2015-01-12 02:50:54 +00:00
/**
* gfx_ctx_init_first:
* @backend : Recording backend handle.
* @data : Recording data handle.
* @params : Recording info parameters.
*
* Finds first suitable recording context driver and initializes.
*
* Returns: true (1) if successful, otherwise false (0).
**/
2015-04-15 11:37:38 +00:00
bool record_driver_init_first(const record_driver_t **backend, void **data,
const struct ffemu_params *params)
2011-01-03 16:51:17 +00:00
{
2014-05-04 09:47:55 +00:00
unsigned i;
2015-01-12 02:50:54 +00:00
2015-04-15 11:37:38 +00:00
for (i = 0; record_drivers[i]; i++)
2011-06-14 19:35:31 +00:00
{
2015-04-15 11:37:38 +00:00
void *handle = record_drivers[i]->init(params);
2015-01-12 02:50:54 +00:00
if (!handle)
continue;
2015-04-15 11:37:38 +00:00
*backend = record_drivers[i];
2015-01-12 02:50:54 +00:00
*data = handle;
return true;
2011-06-14 19:35:31 +00:00
}
2014-05-04 09:47:55 +00:00
return false;
2011-06-14 19:35:31 +00:00
}
void recording_dump_frame(const void *data, unsigned width,
unsigned height, size_t pitch)
{
struct ffemu_video_data ffemu_data = {0};
driver_t *driver = driver_get_ptr();
2015-03-21 03:43:18 +00:00
global_t *global = global_get_ptr();
if (!driver->recording_data)
return;
ffemu_data.pitch = pitch;
ffemu_data.width = width;
ffemu_data.height = height;
ffemu_data.data = data;
2015-03-21 03:43:18 +00:00
if (global->record.gpu_buffer)
{
2015-02-14 04:52:05 +00:00
struct video_viewport vp = {0};
2015-03-22 20:28:50 +00:00
video_driver_viewport_info(&vp);
if (!vp.width || !vp.height)
{
RARCH_WARN("Viewport size calculation failed! Will continue using raw data. This will probably not work right ...\n");
event_command(EVENT_CMD_GPU_RECORD_DEINIT);
recording_dump_frame(data, width, height, pitch);
return;
}
/* User has resized. We kinda have a problem now. */
2015-03-21 03:43:18 +00:00
if (vp.width != global->record.gpu_width ||
vp.height != global->record.gpu_height)
{
static const char msg[] = "Recording terminated due to resize.";
RARCH_WARN("%s\n", msg);
rarch_main_msg_queue_push(msg, 1, 180, true);
event_command(EVENT_CMD_RECORD_DEINIT);
return;
}
/* Big bottleneck.
* Since we might need to do read-backs asynchronously,
* it might take 3-4 times before this returns true. */
2015-03-22 20:57:17 +00:00
if (!video_driver_read_viewport(global->record.gpu_buffer))
return;
2015-03-21 03:43:18 +00:00
ffemu_data.pitch = global->record.gpu_width * 3;
ffemu_data.width = global->record.gpu_width;
ffemu_data.height = global->record.gpu_height;
ffemu_data.data = global->record.gpu_buffer +
(ffemu_data.height - 1) * ffemu_data.pitch;
ffemu_data.pitch = -ffemu_data.pitch;
}
2015-03-21 03:43:18 +00:00
if (!global->record.gpu_buffer)
ffemu_data.is_dupe = !data;
if (driver->recording && driver->recording->push_video)
driver->recording->push_video(driver->recording_data, &ffemu_data);
}
bool recording_deinit(void)
{
driver_t *driver = driver_get_ptr();
if (!driver->recording_data || !driver->recording)
return false;
if (driver->recording->finalize)
driver->recording->finalize(driver->recording_data);
if (driver->recording->free)
driver->recording->free(driver->recording_data);
driver->recording_data = NULL;
driver->recording = NULL;
event_command(EVENT_CMD_GPU_RECORD_DEINIT);
return true;
}
/**
* recording_init:
*
* Initializes recording.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool recording_init(void)
{
2015-04-15 10:42:36 +00:00
char recording_file[PATH_MAX_LENGTH];
struct ffemu_params params = {0};
2015-03-21 03:43:18 +00:00
global_t *global = global_get_ptr();
2015-03-20 19:43:22 +00:00
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
struct retro_system_av_info *av_info = video_viewport_get_system_av_info();
const struct retro_hw_render_callback *hw_render =
(const struct retro_hw_render_callback*)video_driver_callback();
2015-03-21 03:43:18 +00:00
if (!global->record.enable)
return false;
2015-03-21 03:43:18 +00:00
if (global->libretro_dummy)
{
RARCH_WARN(RETRO_LOG_INIT_RECORDING_SKIPPED);
return false;
}
if (!settings->video.gpu_record && hw_render->context_type)
{
RARCH_WARN("Libretro core is hardware rendered. Must use post-shaded recording as well.\n");
return false;
}
RARCH_LOG("Custom timing given: FPS: %.4f, Sample rate: %.4f\n",
(float)av_info->timing.fps,
(float)av_info->timing.sample_rate);
2015-04-15 10:42:36 +00:00
strlcpy(recording_file, global->record.path, sizeof(recording_file));
if (global->record.use_output_dir)
fill_pathname_join(recording_file,
global->record.output_dir,
global->record.path, sizeof(recording_file));
params.out_width = av_info->geometry.base_width;
params.out_height = av_info->geometry.base_height;
params.fb_width = av_info->geometry.max_width;
params.fb_height = av_info->geometry.max_height;
params.channels = 2;
2015-04-15 10:42:36 +00:00
params.filename = recording_file;
params.fps = av_info->timing.fps;
params.samplerate = av_info->timing.sample_rate;
2015-05-20 18:59:12 +00:00
params.pix_fmt = (video_driver_get_pixel_format() == RETRO_PIXEL_FORMAT_XRGB8888) ?
FFEMU_PIX_ARGB8888 : FFEMU_PIX_RGB565;
params.config = NULL;
2015-03-21 03:43:18 +00:00
if (*global->record.config)
params.config = global->record.config;
2015-03-20 19:43:22 +00:00
if (settings->video.gpu_record && driver->video->read_viewport)
{
2015-02-14 04:52:05 +00:00
struct video_viewport vp = {0};
2015-03-22 20:28:50 +00:00
video_driver_viewport_info(&vp);
if (!vp.width || !vp.height)
{
RARCH_ERR("Failed to get viewport information from video driver. "
"Cannot start recording ...\n");
return false;
}
params.out_width = vp.width;
params.out_height = vp.height;
params.fb_width = next_pow2(vp.width);
params.fb_height = next_pow2(vp.height);
2015-03-20 19:43:22 +00:00
if (settings->video.force_aspect &&
(video_driver_get_aspect_ratio() > 0.0f))
params.aspect_ratio = video_driver_get_aspect_ratio();
else
params.aspect_ratio = (float)vp.width / vp.height;
params.pix_fmt = FFEMU_PIX_BGR24;
2015-03-21 03:43:18 +00:00
global->record.gpu_width = vp.width;
global->record.gpu_height = vp.height;
RARCH_LOG("Detected viewport of %u x %u\n",
vp.width, vp.height);
2015-03-21 03:43:18 +00:00
global->record.gpu_buffer = (uint8_t*)malloc(vp.width * vp.height * 3);
if (!global->record.gpu_buffer)
{
RARCH_ERR("Failed to allocate GPU record buffer.\n");
return false;
}
}
else
{
2015-03-21 03:43:18 +00:00
if (global->record.width || global->record.height)
{
2015-03-21 03:43:18 +00:00
params.out_width = global->record.width;
params.out_height = global->record.height;
}
2015-03-20 19:43:22 +00:00
if (settings->video.force_aspect &&
(video_driver_get_aspect_ratio() > 0.0f))
params.aspect_ratio = video_driver_get_aspect_ratio();
else
params.aspect_ratio = (float)params.out_width / params.out_height;
if (settings->video.post_filter_record && video_driver_frame_filter_alive())
{
unsigned max_width = 0;
unsigned max_height = 0;
if (video_driver_frame_filter_is_32bit())
params.pix_fmt = FFEMU_PIX_ARGB8888;
else
params.pix_fmt = FFEMU_PIX_RGB565;
rarch_softfilter_get_max_output_size(
video_driver_frame_filter_get_ptr(),
&max_width, &max_height);
params.fb_width = next_pow2(max_width);
params.fb_height = next_pow2(max_height);
}
}
RARCH_LOG("Recording to %s @ %ux%u. (FB size: %ux%u pix_fmt: %u)\n",
2015-03-21 03:43:18 +00:00
global->record.path,
params.out_width, params.out_height,
params.fb_width, params.fb_height,
(unsigned)params.pix_fmt);
2015-04-15 11:37:38 +00:00
if (!record_driver_init_first(&driver->recording, &driver->recording_data, &params))
{
RARCH_ERR(RETRO_LOG_INIT_RECORDING_FAILED);
event_command(EVENT_CMD_GPU_RECORD_DEINIT);
return false;
}
return true;
}