0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-19 19:42:24 +02:00

vo_opengl: split into multiple files, convert to new option API

gl_video.c contains all rendering code, gl_lcms.c the .icc loader and
creation of 3D LUT (and all LittleCMS specific code). vo_opengl.c is
reduced to interfacing between the various parts.
This commit is contained in:
wm4 2013-03-01 21:19:20 +01:00
parent 16e951c2cd
commit 6ef06aa145
12 changed files with 2196 additions and 1971 deletions

2
.gitignore vendored
View File

@ -13,7 +13,7 @@
/core/input/input_conf.h
/tags
/TAGS
/video/out/vo_opengl_shaders.h
/video/out/gl_video_shaders.h
/video/out/vdpau_template.c
/demux/ebml_defs.c
/demux/ebml_types.h

View File

@ -91,7 +91,8 @@ SOURCES-$(DIRECT3D) += video/out/vo_direct3d.c \
video/out/w32_common.c
SOURCES-$(DSOUND) += audio/out/ao_dsound.c
SOURCES-$(GL) += video/out/gl_common.c video/out/gl_osd.c \
video/out/vo_opengl.c \
video/out/vo_opengl.c video/out/gl_lcms.c \
video/out/gl_video.c \
video/out/vo_opengl_old.c \
video/out/pnm_loader.c
@ -371,8 +372,8 @@ demux/ebml.c: demux/ebml_defs.c
demux/ebml_defs.c: TOOLS/matroska.pl $(MKVLIB_DEPS)
./$< --generate-definitions > $@
video/out/vo_opengl.c: video/out/vo_opengl_shaders.h
video/out/vo_opengl_shaders.h: TOOLS/file2string.pl video/out/vo_opengl_shaders.glsl
video/out/gl_video.c: video/out/gl_video_shaders.h
video/out/gl_video_shaders.h: TOOLS/file2string.pl video/out/gl_video_shaders.glsl
./$^ >$@
sub/osd_libass.c: sub/osd_font.h
@ -447,7 +448,7 @@ clean:
-$(RM) core/input/input_conf.h
-$(RM) video/out/vdpau_template.c
-$(RM) demux/ebml_types.h demux/ebml_defs.c
-$(RM) video/out/vo_opengl_shaders.h
-$(RM) video/out/gl_video_shaders.h
-$(RM) sub/osd_font.h
distclean: clean

View File

@ -152,6 +152,10 @@ struct m_opt_choice_alternatives {
int value;
};
// For OPT_STRING_VALIDATE(). Behaves like m_option_type.parse().
typedef int (*m_opt_string_validate_fn)(const m_option_t *opt, struct bstr name,
struct bstr param);
// m_option.priv points to this if M_OPT_TYPE_USE_SUBSTRUCT is used
struct m_sub_options {
const struct m_option *opts;
@ -614,6 +618,12 @@ static inline void m_option_free(const m_option_t *opt, void *dst)
#define OPT_TRACKCHOICE(name, var) \
OPT_CHOICE_OR_INT(name, var, 0, 0, 8190, ({"no", -2}, {"auto", -1}))
#define OPT_STRING_VALIDATE_(optname, varname, flags, validate_fn, ...) \
OPT_GENERAL(char*, optname, varname, flags, __VA_ARGS__, \
.priv = MP_EXPECT_TYPE(m_opt_string_validate_fn, validate_fn))
#define OPT_STRING_VALIDATE(...) \
OPT_STRING_VALIDATE_(__VA_ARGS__, .type = &m_option_type_string)
// subconf must have the type struct m_sub_options.
// All sub-options are prefixed with "name-" and are added to the current
// (containing) option list.

View File

@ -478,6 +478,12 @@ void mpgl_load_functions(GL *gl, void *(*getProcAddress)(const GLubyte *),
gl->version = MPGL_VER(major, minor);
mp_msg(MSGT_VO, MSGL_V, "[gl] Detected OpenGL %d.%d.\n", major, minor);
mp_msg(MSGT_VO, MSGL_V, "GL_VENDOR='%s'\n", gl->GetString(GL_VENDOR));
mp_msg(MSGT_VO, MSGL_V, "GL_RENDERER='%s'\n", gl->GetString(GL_RENDERER));
mp_msg(MSGT_VO, MSGL_V, "GL_VERSION='%s'\n", gl->GetString(GL_VERSION));
mp_msg(MSGT_VO, MSGL_V, "GL_SHADING_LANGUAGE_VERSION='%s'\n",
gl->GetString(GL_SHADING_LANGUAGE_VERSION));
// Note: This code doesn't handle CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
// on OpenGL 3.0 correctly. Apparently there's no way to detect this
// situation, because GL_ARB_compatibility is specified only for 3.1

212
video/out/gl_lcms.c Normal file
View File

@ -0,0 +1,212 @@
/*
* This file is part of mpv.
*
* mpv 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*
* You can alternatively redistribute this file and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#include <string.h>
#include <lcms2.h>
#include "talloc.h"
#include "config.h"
#include "stream/stream.h"
#include "core/mp_common.h"
#include "core/bstr.h"
#include "core/mp_msg.h"
#include "core/m_option.h"
#include "gl_video.h"
#include "gl_lcms.h"
static bool parse_3dlut_size(const char *s, int *p1, int *p2, int *p3)
{
if (sscanf(s, "%dx%dx%d", p1, p2, p3) != 3)
return false;
for (int n = 0; n < 3; n++) {
int s = ((int[]) { *p1, *p2, *p3 })[n];
if (s < 2 || s > 256 || ((s - 1) & s))
return false;
}
return true;
}
static int validate_3dlut_size_opt(const m_option_t *opt, struct bstr name,
struct bstr param)
{
int p1, p2, p3;
char s[20];
snprintf(s, sizeof(s), "%.*s", BSTR_P(param));
return parse_3dlut_size(s, &p1, &p2, &p3);
}
#define OPT_BASE_STRUCT struct mp_icc_opts
const struct m_sub_options mp_icc_conf = {
.opts = (m_option_t[]) {
OPT_STRING("icc-profile", profile, 0),
OPT_STRING("icc-cache", cache, 0),
OPT_INT("icc-intent", intent, 0),
OPT_STRING_VALIDATE("3dlut-size", size_str, 0, validate_3dlut_size_opt),
{0}
},
.size = sizeof(struct mp_icc_opts),
.defaults = &(const struct mp_icc_opts) {
.size_str = "128x256x64",
.intent = INTENT_ABSOLUTE_COLORIMETRIC,
},
};
#ifdef CONFIG_LCMS2
static void lcms2_error_handler(cmsContext ctx, cmsUInt32Number code,
const char *msg)
{
mp_msg(MSGT_VO, MSGL_ERR, "[gl] lcms2: %s\n", msg);
}
static struct bstr load_file(void *talloc_ctx, const char *filename)
{
struct bstr res = {0};
stream_t *s = open_stream(filename, NULL, NULL);
if (s) {
res = stream_read_complete(s, talloc_ctx, 1000000000, 0);
free_stream(s);
}
return res;
}
#define LUT3D_CACHE_HEADER "mpv 3dlut cache 1.0\n"
struct lut3d *mp_load_icc(struct mp_icc_opts *opts)
{
int s_r, s_g, s_b;
if (!parse_3dlut_size(opts->size_str, &s_r, &s_g, &s_b))
return NULL;
if (!opts->profile)
return NULL;
void *tmp = talloc_new(NULL);
uint16_t *output = talloc_array(tmp, uint16_t, s_r * s_g * s_b * 3);
mp_msg(MSGT_VO, MSGL_INFO, "[gl] Opening ICC profile '%s'\n", opts->profile);
struct bstr iccdata = load_file(tmp, opts->profile);
if (!iccdata.len)
goto error_exit;
char *cache_info = talloc_asprintf(tmp, "intent=%d, size=%dx%dx%d\n",
opts->intent, s_r, s_g, s_b);
// check cache
if (opts->cache) {
mp_msg(MSGT_VO, MSGL_INFO, "[gl] Opening 3D LUT cache in file '%s'.\n",
opts->cache);
struct bstr cachedata = load_file(tmp, opts->cache);
if (bstr_eatstart(&cachedata, bstr0(LUT3D_CACHE_HEADER))
&& bstr_eatstart(&cachedata, bstr0(cache_info))
&& bstr_eatstart(&cachedata, iccdata)
&& cachedata.len == talloc_get_size(output))
{
memcpy(output, cachedata.start, cachedata.len);
goto done;
} else {
mp_msg(MSGT_VO, MSGL_WARN, "[gl] 3D LUT cache invalid!\n");
}
}
cmsSetLogErrorHandler(lcms2_error_handler);
cmsHPROFILE profile = cmsOpenProfileFromMem(iccdata.start, iccdata.len);
if (!profile)
goto error_exit;
cmsCIExyY d65;
cmsWhitePointFromTemp(&d65, 6504);
static const cmsCIExyYTRIPLE bt709prim = {
.Red = {0.64, 0.33, 1.0},
.Green = {0.30, 0.60, 1.0},
.Blue = {0.15, 0.06, 1.0},
};
cmsToneCurve *tonecurve = cmsBuildGamma(NULL, 1.0/0.45);
cmsHPROFILE vid_profile = cmsCreateRGBProfile(&d65, &bt709prim,
(cmsToneCurve*[3]){tonecurve, tonecurve, tonecurve});
cmsFreeToneCurve(tonecurve);
cmsHTRANSFORM trafo = cmsCreateTransform(vid_profile, TYPE_RGB_16,
profile, TYPE_RGB_16,
opts->intent,
cmsFLAGS_HIGHRESPRECALC);
cmsCloseProfile(profile);
cmsCloseProfile(vid_profile);
if (!trafo)
goto error_exit;
// transform a (s_r)x(s_g)x(s_b) cube, with 3 components per channel
uint16_t *input = talloc_array(tmp, uint16_t, s_r * 3);
for (int b = 0; b < s_b; b++) {
for (int g = 0; g < s_g; g++) {
for (int r = 0; r < s_r; r++) {
input[r * 3 + 0] = r * 65535 / (s_r - 1);
input[r * 3 + 1] = g * 65535 / (s_g - 1);
input[r * 3 + 2] = b * 65535 / (s_b - 1);
}
size_t base = (b * s_r * s_g + g * s_r) * 3;
cmsDoTransform(trafo, input, output + base, s_r);
}
}
cmsDeleteTransform(trafo);
if (opts->cache) {
FILE *out = fopen(opts->cache, "wb");
if (out) {
fprintf(out, "%s%s", LUT3D_CACHE_HEADER, cache_info);
fwrite(iccdata.start, iccdata.len, 1, out);
fwrite(output, talloc_get_size(output), 1, out);
fclose(out);
}
}
done: ;
struct lut3d *lut = talloc_ptrtype(NULL, lut);
*lut = (struct lut3d) {
.data = talloc_steal(lut, output),
.size = {s_r, s_g, s_b},
};
talloc_free(tmp);
return lut;
error_exit:
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Error loading ICC profile.\n");
talloc_free(tmp);
return NULL;
}
#else /* CONFIG_LCMS2 */
struct lut3d *mp_load_icc(struct mp_icc_opts *opts)
{
mp_msg(MSGT_VO, MSGL_FATAL, "[gl] LCMS2 support not compiled.\n");
return NULL;
}
#endif

16
video/out/gl_lcms.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef MP_GL_LCMS_H
#define MP_GL_LCMS_H
extern const struct m_sub_options mp_icc_conf;
struct mp_icc_opts {
char *profile;
char *cache;
char *size_str;
int intent;
};
struct lut3d;
struct lut3d *mp_load_icc(struct mp_icc_opts *opts);
#endif

1732
video/out/gl_video.c Normal file

File diff suppressed because it is too large Load Diff

71
video/out/gl_video.h Normal file
View File

@ -0,0 +1,71 @@
/*
* This file is part of mpv.
*
* mpv 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MP_GL_VIDEO_H
#define MP_GL_VIDEO_H
#include <stdbool.h>
#include "sub/sub.h"
#include "gl_common.h"
struct lut3d {
uint16_t *data;
int size[3];
};
struct gl_video_opts {
char *scalers[2];
float scaler_params[2];
int indirect;
int gamma;
int srgb;
int scale_sep;
int fancy_downscaling;
int npot;
int pbo;
int dither_depth;
int fbo_format;
int stereo_mode;
};
extern const struct m_sub_options gl_video_conf;
struct gl_video;
struct gl_video *gl_video_init(GL *gl);
void gl_video_uninit(struct gl_video *p);
void gl_video_set_options(struct gl_video *p, struct gl_video_opts *opts);
void gl_video_config(struct gl_video *p, int format, int w, int h, int dw, int dh);
void gl_video_set_output_depth(struct gl_video *p, int r, int g, int b);
void gl_video_set_lut3d(struct gl_video *p, struct lut3d *lut3d);
void gl_video_draw_osd(struct gl_video *p, struct osd_state *osd);
void gl_video_upload_image(struct gl_video *p, struct mp_image *img);
void gl_video_render_frame(struct gl_video *p);
struct mp_image *gl_video_download_image(struct gl_video *p);
void gl_video_resize(struct gl_video *p, struct mp_rect *window,
struct mp_rect *src, struct mp_rect *dst,
struct mp_osd_res *osd);
bool gl_video_get_csp_override(struct gl_video *p, struct mp_csp_details *csp);
bool gl_video_set_csp_override(struct gl_video *p, struct mp_csp_details *csp);
bool gl_video_set_equalizer(struct gl_video *p, const char *name, int val);
bool gl_video_get_equalizer(struct gl_video *p, const char *name, int *val);
void gl_video_set_debug(struct gl_video *p, bool enable);
bool gl_video_check_format(int mp_format);
#endif

View File

@ -1,19 +1,23 @@
/*
* This file is part of mplayer2.
* This file is part of mpv.
*
* mplayer2 is free software; you can redistribute it and/or modify
* mpv 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mplayer2 is distributed in the hope that it will be useful,
* mpv 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 mplayer2; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*
* You can alternatively redistribute this file and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
// Note that this file is not directly passed as shader, but run through some

View File

@ -119,9 +119,16 @@ static int vo_preinit(struct vo *vo, char *arg)
char n[50];
int l = snprintf(n, sizeof(n), "vo/%s", vo->driver->info->short_name);
assert(l < sizeof(n));
if (vo->driver->init_option_string) {
m_config_parse_suboptions(cfg, n,
(char *)vo->driver->init_option_string);
}
int r = m_config_parse_suboptions(cfg, n, arg);
if (r < 0)
if (r < 0) {
if (vo->driver->help_text)
mp_msg(MSGT_VO, MSGL_FATAL, "%s\n", vo->driver->help_text);
return r;
}
}
return vo->driver->preinit(vo, arg);
}

View File

@ -220,6 +220,12 @@ struct vo_driver {
// List of options to parse into priv struct (requires privsize to be set)
const struct m_option *options;
// Help text to print when option parsing fails
const char *help_text;
// Parse these options before parsing user options
const char *init_option_string;
};
struct vo {

File diff suppressed because it is too large Load Diff