summaryrefslogtreecommitdiffstats
path: root/libvo/vo_direct3d.c
diff options
context:
space:
mode:
Diffstat (limited to 'libvo/vo_direct3d.c')
-rw-r--r--libvo/vo_direct3d.c872
1 files changed, 680 insertions, 192 deletions
diff --git a/libvo/vo_direct3d.c b/libvo/vo_direct3d.c
index a1124f5412..79c91cbea3 100644
--- a/libvo/vo_direct3d.c
+++ b/libvo/vo_direct3d.c
@@ -27,9 +27,13 @@
#include <assert.h>
#include <d3d9.h>
#include "config.h"
+#include "options.h"
+#include "subopt-helper.h"
#include "talloc.h"
#include "video_out.h"
#include "video_out_internal.h"
+#include "csputils.h"
+#include "libmpcodecs/img_format.h"
#include "fastmemcpy.h"
#include "mp_msg.h"
#include "aspect.h"
@@ -39,18 +43,26 @@
#include "sub/sub.h"
#include "eosd_packer.h"
+// shaders generated by fxc.exe from d3d_shader_yuv.hlsl
+#include "d3d_shader_yuv.h"
+
static const vo_info_t info =
{
"Direct3D 9 Renderer",
"direct3d",
- "Georgi Petrov (gogothebee) <gogothebee@gmail.com>",
+ "Georgi Petrov (gogothebee) <gogothebee@gmail.com> and others",
""
};
-// texture format for EOSD
-// 0: use D3DFMT_A8L8
-// 1: use D3DFMT_A8 (doesn't work with wine)
-#define USE_A8 0
+// TODO: beg someone to add this (there is already IMGFMT_Y8)
+// equals MAKEFOURCC('Y', '1', '6', ' ')
+#define IMGFMT_Y16 0x20363159
+
+#define IMGFMT_IS_Y(x) ((x) == IMGFMT_Y8 || (x) == IMGFMT_Y16)
+#define IMGFMT_Y_DEPTH(x) ((x) == IMGFMT_Y8 ? 8 : 16)
+
+#define DEVTYPE D3DDEVTYPE_HAL
+//#define DEVTYPE D3DDEVTYPE_REF
/*
* Link essential libvo functions: preinit, config, control, draw_frame,
@@ -74,6 +86,14 @@ typedef struct {
float tu, tv;
} vertex_eosd;
+#define D3DFVF_VIDEO_VERTEX (D3DFVF_XYZ | D3DFVF_TEX3)
+
+typedef struct {
+ float x, y, z;
+ // pairs of texture coordinates for up to 3 planes
+ float t[3][2];
+} vertex_video;
+
struct d3dtex {
// user-requested size
int w, h;
@@ -91,9 +111,32 @@ struct d3dtex {
IDirect3DTexture9 *device;
};
+struct texplane {
+ int bytes_per_pixel;
+ int bits_per_pixel;
+ // chroma shifts
+ // e.g. get the plane's width in pixels with (priv->src_width >> shift_x)
+ int shift_x, shift_y;
+ D3DFORMAT d3d_format;
+ struct d3dtex texture;
+ // temporary locking during uploading the frame (e.g. for draw_slice)
+ D3DLOCKED_RECT locked_rect;
+ // value used to clear the image with memset (YUV chroma planes do not use
+ // the value 0 for this)
+ uint8_t clearval;
+};
+
/* Global variables "priv" structure. I try to keep their count low.
*/
static struct global_priv {
+ int opt_prefer_stretchrect;
+ int opt_disable_textures;
+ int opt_disable_stretchrect;
+ int opt_disable_shaders;
+ int opt_only_8bit;
+ int opt_disable_eosd;
+ int opt_disable_texture_align;
+
int is_paused; /**< 1 = Movie is paused,
0 = Movie is not paused */
int is_clear_needed; /**< 1 = Clear the backbuffer before StretchRect
@@ -107,6 +150,17 @@ static struct global_priv {
int src_height; /**< Source (movie) heigth */
int border_x; /**< horizontal border value for OSD */
int border_y; /**< vertical border value for OSD */
+ int image_format; /**< mplayer image format */
+ bool use_textures; /**< use 3D texture rendering, instead of
+ StretchRect */
+ bool use_shaders; /**< use shader for YUV color conversion
+ (or possibly for RGB video equalizers) */
+
+ int plane_count;
+ struct texplane planes[3];
+
+ IDirect3DPixelShader9 *pixel_shader;
+ const BYTE *pixel_shader_data;
D3DFORMAT movie_src_fmt; /**< Movie colorspace format (depends on
the movie's codec) */
@@ -118,13 +172,14 @@ static struct global_priv {
LPDIRECT3D9 d3d_handle; /**< Direct3D Handle */
LPDIRECT3DDEVICE9 d3d_device; /**< The Direct3D Adapter */
+ bool d3d_in_scene; /**< BeginScene was called, EndScene not */
IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer
renders inside it. Uses colorspace
priv->movie_src_fmt */
struct d3dtex texture_osd; /**< RGBA */
IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to
display next frame) */
- struct d3dtex texture_eosd; /**< A8 or A8L8 (see USE_A8) */
+ struct d3dtex texture_eosd; /**< A8 */
int cur_backbuf_width; /**< Current backbuffer width */
int cur_backbuf_height; /**< Current backbuffer height */
int is_osd_populated; /**< 1 = OSD texture has something to display,
@@ -138,34 +193,44 @@ static struct global_priv {
int max_texture_width; /**< from the device capabilities */
int max_texture_height; /**< from the device capabilities */
+ D3DMATRIX d3d_colormatrix;
+ struct mp_csp_details colorspace;
+ struct mp_csp_equalizer video_eq;
+
struct eosd_packer *eosd; /**< EOSD packer (image positions etc.) */
vertex_eosd *eosd_vb; /**< temporary memory for D3D when rendering EOSD */
} *priv;
-typedef struct {
+struct fmt_entry {
const unsigned int mplayer_fmt; /**< Given by MPlayer */
const D3DFORMAT fourcc; /**< Required by D3D's test function */
-} struct_fmt_table;
+};
/* Map table from reported MPlayer format to the required
fourcc. This is needed to perform the format query. */
-static const struct_fmt_table fmt_table[] = {
+static const struct fmt_entry fmt_table[] = {
+ // planar YUV
{IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')},
{IMGFMT_I420, MAKEFOURCC('I','4','2','0')},
{IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')},
{IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')},
+ // packed YUV
{IMGFMT_YUY2, D3DFMT_YUY2},
{IMGFMT_UYVY, D3DFMT_UYVY},
+ // packed RGB
{IMGFMT_BGR32, D3DFMT_X8R8G8B8},
{IMGFMT_RGB32, D3DFMT_X8B8G8R8},
{IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested
{IMGFMT_BGR16, D3DFMT_R5G6B5},
{IMGFMT_BGR15, D3DFMT_X1R5G5B5},
{IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested
+ // grayscale (can be considered both packed and planar)
+ {IMGFMT_Y8, D3DFMT_L8},
+ {IMGFMT_Y16, D3DFMT_L16},
+ {0},
};
-#define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
typedef enum back_buffer_action {
BACKBUFFER_CREATE,
@@ -174,7 +239,28 @@ typedef enum back_buffer_action {
static void generate_eosd(mp_eosd_images_t *);
static void draw_eosd(void);
+static void update_colorspace(void);
+static void d3d_clear_video_textures(void);
+static int resize_d3d(void);
+static void d3d_matrix_identity(D3DMATRIX *m)
+{
+ memset(m, 0, sizeof(D3DMATRIX));
+ m->_11 = m->_22 = m->_33 = m->_44 = 1.0f;
+}
+
+static void d3d_matrix_ortho(D3DMATRIX *m, float left, float right,
+ float bottom, float top)
+{
+ d3d_matrix_identity(m);
+ m->_11 = 2.0f / (right - left);
+ m->_22 = 2.0f / (top - bottom);
+ m->_33 = 1.0f;
+ m->_41 = -(right + left) / (right - left);
+ m->_42 = -(top + bottom) / (top - bottom);
+ m->_43 = 0;
+ m->_44 = 1.0f;
+}
/****************************************************************************
* *
@@ -186,6 +272,18 @@ static void draw_eosd(void);
* *
****************************************************************************/
+static bool d3d_begin_scene(void)
+{
+ if (!priv->d3d_in_scene) {
+ if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
+ mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
+ return false;
+ }
+ priv->d3d_in_scene = true;
+ }
+ return true;
+}
+
/** @brief Calculate scaled fullscreen movie rectangle with
* preserved aspect ratio.
*/
@@ -241,9 +339,10 @@ static void d3d_fix_texture_size(int *width, int *height)
/* device only supports square textures */
tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height;
// better round up to a multiple of 16
- // (xxx: why???)
- tex_width = (tex_width + 15) & ~15;
- tex_height = (tex_height + 15) & ~15;
+ if (!priv->opt_disable_texture_align) {
+ tex_width = (tex_width + 15) & ~15;
+ tex_height = (tex_height + 15) & ~15;
+ }
*width = tex_width;
*height = tex_height;
@@ -304,35 +403,69 @@ error_exit:
static IDirect3DBaseTexture9 *d3dtex_get_render_texture(struct d3dtex *tex)
{
- return (IDirect3DBaseTexture9 *)
- (priv->device_texture_sys ? tex->system : tex->device);
+ return (IDirect3DBaseTexture9 *)(tex->device ? tex->device : tex->system);
}
// Copy system texture contents to device texture.
static bool d3dtex_update(struct d3dtex *tex)
{
- if (priv->device_texture_sys)
+ if (!tex->device)
return true;
return !FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device,
(IDirect3DBaseTexture9 *)tex->system,
(IDirect3DBaseTexture9 *)tex->device));
}
-/** @brief Destroy D3D Offscreen and Backbuffer surfaces.
- */
-static void destroy_d3d_surfaces(void)
+static void d3d_unlock_video_objects(void)
{
- mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
- /* Let's destroy the old (if any) D3D Surfaces */
+ bool any_failed = false;
- if (priv->locked_rect.pBits)
- IDirect3DSurface9_UnlockRect(priv->d3d_surface);
+ if (priv->locked_rect.pBits) {
+ if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface)))
+ any_failed = true;
+ }
priv->locked_rect.pBits = NULL;
+ for (int n = 0; n < priv->plane_count; n++) {
+ struct texplane *plane = &priv->planes[n];
+ if (plane->locked_rect.pBits) {
+ if (FAILED(IDirect3DTexture9_UnlockRect(plane->texture.system, 0)))
+ any_failed = true;
+ }
+ plane->locked_rect.pBits = NULL;
+ }
+
+ if (any_failed)
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Unlocking video objects failed.\n");
+}
+
+// Free video surface/textures, shaders, etc.
+static void d3d_destroy_video_objects(void)
+{
+ d3d_unlock_video_objects();
+
if (priv->d3d_surface)
IDirect3DSurface9_Release(priv->d3d_surface);
priv->d3d_surface = NULL;
+ for (int n = 0; n < priv->plane_count; n++) {
+ d3dtex_release(&priv->planes[n].texture);
+ }
+
+ if (priv->pixel_shader)
+ IDirect3DPixelShader9_Release(priv->pixel_shader);
+ priv->pixel_shader = NULL;
+}
+
+/** @brief Destroy D3D Offscreen and Backbuffer surfaces.
+ */
+static void destroy_d3d_surfaces(void)
+{
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
+ /* Let's destroy the old (if any) D3D Surfaces */
+
+ d3d_destroy_video_objects();
+
d3dtex_release(&priv->texture_osd);
if (priv->d3d_backbuf)
@@ -345,6 +478,102 @@ static void destroy_d3d_surfaces(void)
eosd_packer_reinit(priv->eosd, 0, 0);
}
+// Allocate video surface or textures, and create shaders if needed.
+static bool d3d_configure_video_objects(void)
+{
+ int n;
+ bool need_clear = false;
+
+ assert(priv->image_format != 0);
+
+ if (priv->use_textures) {
+ for (n = 0; n < priv->plane_count; n++) {
+ struct texplane *plane = &priv->planes[n];
+
+ if (!plane->texture.system) {
+ if (!d3dtex_allocate(&plane->texture,
+ plane->d3d_format,
+ priv->src_width >> plane->shift_x,
+ priv->src_height >> plane->shift_y))
+ {
+ mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating plane %d"
+ " failed.\n", n);
+ return false;
+ }
+
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Allocated plane %d:"
+ " %d bit, shift=%d/%d size=%d/%d (%d/%d).\n", n,
+ plane->bits_per_pixel,
+ plane->shift_x, plane->shift_y,
+ plane->texture.w, plane->texture.h,
+ plane->texture.tex_w, plane->texture.tex_h);
+
+ need_clear = true;
+ }
+ }
+
+ if (need_clear)
+ d3d_clear_video_textures();
+
+ if (priv->pixel_shader_data) {
+ if (!priv->pixel_shader &&
+ FAILED(IDirect3DDevice9_CreatePixelShader(priv->d3d_device,
+ (DWORD *)priv->pixel_shader_data, &priv->pixel_shader)))
+ {
+ mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Failed to create "
+ "YUV conversion pixel shader.\n");
+ return false;
+ }
+ }
+
+ } else {
+
+ if (!priv->d3d_surface &&
+ FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
+ priv->d3d_device, priv->src_width, priv->src_height,
+ priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
+ mp_msg(MSGT_VO, MSGL_ERR,
+ "<vo_direct3d>Allocating offscreen surface failed.\n");
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool d3d_lock_video_textures(void)
+{
+ for (int n = 0; n < priv->plane_count; n++) {
+ struct texplane *plane = &priv->planes[n];
+
+ if (!plane->locked_rect.pBits) {
+ if (FAILED(IDirect3DTexture9_LockRect(plane->texture.system, 0,
+ &plane->locked_rect, NULL, 0)))
+ {
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Texture lock failure.\n");
+ d3d_unlock_video_objects();
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+static void d3d_clear_video_textures(void)
+{
+ if (!d3d_lock_video_textures())
+ return;
+
+ for (int n = 0; n < priv->plane_count; n++) {
+ struct texplane *plane = &priv->planes[n];
+ memset(plane->locked_rect.pBits, plane->clearval,
+ plane->locked_rect.Pitch * plane->texture.tex_h);
+ }
+
+ d3d_unlock_video_objects();
+}
+
/** @brief Create D3D Offscreen and Backbuffer surfaces. Each
* surface is created only if it's not already present.
* @return 1 on success, 0 on failure
@@ -355,15 +584,6 @@ static int create_d3d_surfaces(void)
int tex_width = osd_width, tex_height = osd_height;
mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>create_d3d_surfaces called.\n");
- if (!priv->d3d_surface &&
- FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
- priv->d3d_device, priv->src_width, priv->src_height,
- priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
- mp_msg(MSGT_VO, MSGL_ERR,
- "<vo_direct3d>Allocating offscreen surface failed.\n");
- return 0;
- }
-
if (!priv->d3d_backbuf &&
FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0,
D3DBACKBUFFER_TYPE_MONO,
@@ -372,6 +592,9 @@ static int create_d3d_surfaces(void)
return 0;
}
+ if (!d3d_configure_video_objects())
+ return 0;
+
/* create OSD */
d3d_fix_texture_size(&tex_width, &tex_height);
@@ -405,13 +628,23 @@ static int create_d3d_surfaces(void)
priv->texture_osd.h = osd_height;
/* setup default renderstate */
- IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE);
+ IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0);
IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE);
- IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
+
+ // we use up to 3 samplers for up to 3 YUV planes
+ for (int n = 0; n < 3; n++) {
+ IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_MINFILTER,
+ D3DTEXF_LINEAR);
+ IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_MAGFILTER,
+ D3DTEXF_LINEAR);
+ IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_ADDRESSU,
+ D3DTADDRESS_CLAMP);
+ IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_ADDRESSV,
+ D3DTADDRESS_CLAMP);
+ }
if (priv->eosd && !priv->texture_eosd.system)
eosd_packer_reinit(priv->eosd, priv->max_texture_width,
@@ -466,7 +699,7 @@ static int change_d3d_backbuffer(back_buffer_action_e action)
if (action == BACKBUFFER_CREATE &&
FAILED(IDirect3D9_CreateDevice(priv->d3d_handle,
D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL, vo_w32_window,
+ DEVTYPE, vo_w32_window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&present_params, &priv->d3d_device))) {
mp_msg(MSGT_VO, MSGL_V,
@@ -496,7 +729,6 @@ static int change_d3d_backbuffer(back_buffer_action_e action)
static int configure_d3d(void)
{
D3DDISPLAYMODE disp_mode;
- D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>configure_d3d called.\n");
@@ -521,13 +753,7 @@ static int configure_d3d(void)
if (!create_d3d_surfaces())
return 0;
- if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
- &vp))) {
- mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
- return 0;
- }
-
- calc_fs_rect();
+ resize_d3d();
return 1;
}
@@ -597,6 +823,11 @@ static int resize_d3d(void)
return 0;
}
+ // so that screen coordinates map to D3D ones
+ D3DMATRIX view;
+ d3d_matrix_ortho(&view, 0.5f, vp.Width + 0.5f, vp.Height + 0.5f, 0.5f);
+ IDirect3DDevice9_SetTransform(priv->d3d_device, D3DTS_VIEW, &view);
+
calc_fs_rect();
#ifdef CONFIG_FREETYPE
@@ -632,6 +863,20 @@ static void uninit_d3d(void)
static uint32_t d3d_draw_frame(void);
+static uint32_t d3d_upload_and_render_frame_texture(mp_image_t *mpi)
+{
+ if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK))
+ draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
+
+ d3d_unlock_video_objects();
+
+ for (int n = 0; n < priv->plane_count; n++) {
+ d3dtex_update(&priv->planes[n].texture);
+ }
+
+ return d3d_draw_frame();
+}
+
/** @brief Render a frame on the screen.
* @param mpi mpi structure with the decoded frame inside
* @return VO_TRUE on success, VO_ERROR on failure
@@ -647,6 +892,9 @@ static uint32_t d3d_upload_and_render_frame(mp_image_t *mpi)
if (!priv->d3d_device)
return VO_TRUE;
+ if (priv->use_textures)
+ return d3d_upload_and_render_frame_texture(mpi);
+
if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
goto skip_upload;
@@ -668,22 +916,17 @@ static uint32_t d3d_upload_and_render_frame(mp_image_t *mpi)
mpi->height, priv->locked_rect.Pitch, mpi->stride[0]);
skip_upload:
- /* This unlock is used for both slice_draw path and DRAW_IMAGE path. */
- if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) {
- mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface unlock failed.\n");
- return VO_ERROR;
- }
- priv->locked_rect.pBits = NULL;
+ d3d_unlock_video_objects();
return d3d_draw_frame();
}
static uint32_t d3d_draw_frame(void)
{
- if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
- mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
+ int n;
+
+ if (!d3d_begin_scene())
return VO_ERROR;
- }
if (priv->is_clear_needed) {
IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL,
@@ -691,52 +934,257 @@ static uint32_t d3d_draw_frame(void)
priv->is_clear_needed = 0;
}
- if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
- priv->d3d_surface,
- &priv->fs_panscan_rect,
- priv->d3d_backbuf,
- &priv->fs_movie_rect,
- D3DTEXF_LINEAR))) {
- mp_msg(MSGT_VO, MSGL_ERR,
- "<vo_direct3d>Copying frame to the backbuffer failed.\n");
- return VO_ERROR;
- }
+ if (priv->use_textures) {
- if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
- mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>EndScene failed.\n");
- return VO_ERROR;
+ for (n = 0; n < priv->plane_count; n++) {
+ IDirect3DDevice9_SetTexture(priv->d3d_device, n,
+ d3dtex_get_render_texture(&priv->planes[n].texture));
+ }
+
+ RECT rm = priv->fs_movie_rect;
+ RECT rs = priv->fs_panscan_rect;
+
+ vertex_video vb[] = {
+ { rm.left, rm.top, 0.0f},
+ { rm.right, rm.top, 0.0f},
+ { rm.left, rm.bottom, 0.0f},
+ { rm.right, rm.bottom, 0.0f}
+ };
+
+ float texc[4][2] = {
+ { rs.left, rs.top},
+ { rs.right, rs.top},
+ { rs.left, rs.bottom},
+ { rs.right, rs.bottom}
+ };
+
+ for (n = 0; n < priv->plane_count; n++) {
+ float s_x = (1.0f / (1 << priv->planes[n].shift_x))
+ / priv->planes[n].texture.tex_w;
+ float s_y = (1.0f / (1 << priv->planes[n].shift_y))
+ / priv->planes[n].texture.tex_h;
+ for (int i = 0; i < 4; i++) {
+ vb[i].t[n][0] = texc[i][0] * s_x;
+ vb[i].t[n][1] = texc[i][1] * s_y;
+ }
+ }
+
+ if (priv->pixel_shader) {
+ IDirect3DDevice9_SetPixelShader(priv->d3d_device, priv->pixel_shader);
+ IDirect3DDevice9_SetPixelShaderConstantF(priv->d3d_device, 0,
+ &priv->d3d_colormatrix._11,
+ 4);
+ }
+
+ IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_VIDEO_VERTEX);
+ IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP,
+ 2, &vb[0], sizeof(vertex_video));
+
+ IDirect3DDevice9_SetPixelShader(priv->d3d_device, NULL);
+
+ for (n = 0; n < priv->plane_count; n++) {
+ IDirect3DDevice9_SetTexture(priv->d3d_device, n, NULL);
+ }
+
+ } else {
+ if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
+ priv->d3d_surface,
+ &priv->fs_panscan_rect,
+ priv->d3d_backbuf,
+ &priv->fs_movie_rect,
+ D3DTEXF_LINEAR))) {
+ mp_msg(MSGT_VO, MSGL_ERR,
+ "<vo_direct3d>Copying frame to the backbuffer failed.\n");
+ return VO_ERROR;
+ }
}
return VO_TRUE;
}
-static const struct_fmt_table *check_format(uint32_t movie_fmt)
+// Return the high byte of the value that represents white in chroma (U/V)
+static int get_chroma_clear_val(int bit_depth)
{
- int i;
- for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) {
- if (fmt_table[i].mplayer_fmt == movie_fmt) {
- /* Test conversion from Movie colorspace to
- * display's target colorspace. */
- if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
- D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- fmt_table[i].fourcc,
- priv->desktop_fmt))) {
- mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format: %s\n",
- vo_format_name(fmt_table[i].mplayer_fmt));
- return NULL;
+ return 1 << (bit_depth - 1 & 7);
+}
+
+// this macro is supposed to work on all formats supported by 3D rendering, and
+// that produce "reasonable" output (i.e. no mixed up colors)
+#define IMGFMT_IS_ANY_RND(x) (IMGFMT_IS_BGR(x) || IMGFMT_IS_RGB(x) || IMGFMT_IS_Y(x))
+
+// pixel size in bit for any IMGFMT_IS_ANY_RND(x)==true
+// we assume that the actual pixel strides are always aligned on bytes
+static int imgfmt_any_rnd_depth(int fmt)
+{
+ if (IMGFMT_IS_BGR(fmt))
+ return IMGFMT_BGR_DEPTH(fmt);
+ if (IMGFMT_IS_RGB(fmt))
+ return IMGFMT_RGB_DEPTH(fmt);
+ if (IMGFMT_IS_Y(fmt))
+ return IMGFMT_Y_DEPTH(fmt);
+ assert(false);
+ return 0;
+}
+
+static D3DFORMAT check_format(uint32_t movie_fmt, bool as_texture)
+{
+ const char *type = as_texture ? "texture rendering" : "StretchRect";
+ const struct fmt_entry *cur = &fmt_table[0];
+
+ // Don't try to handle weird packed texture formats (although I don't know
+ // if D3D9 would even accept any such format for 3D rendering; and we
+ // certainly don't try any tricks like matching it to RGB formats and
+ // applying a YUV conversion matrix)
+ if (as_texture && !IMGFMT_IS_ANY_RND(movie_fmt))
+ return 0;
+
+ while (cur->mplayer_fmt) {
+ if (cur->mplayer_fmt == movie_fmt) {
+ HRESULT res;
+ if (as_texture) {
+ res = IDirect3D9_CheckDeviceFormat(priv->d3d_handle,
+ D3DADAPTER_DEFAULT,
+ DEVTYPE,
+ priv->desktop_fmt,
+ D3DUSAGE_DYNAMIC | D3DUSAGE_QUERY_FILTER,
+ D3DRTYPE_TEXTURE,
+ cur->fourcc);
+ } else {
+ /* Test conversion from Movie colorspace to
+ * display's target colorspace. */
+ res = IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
+ D3DADAPTER_DEFAULT,
+ DEVTYPE,
+ cur->fourcc,
+ priv->desktop_fmt);
+ }
+ if (FAILED(res)) {
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format "
+ "(%s): %s\n", type, vo_format_name(cur->mplayer_fmt));
+ return 0;
}
- mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted image format: %s\n",
- vo_format_name(fmt_table[i].mplayer_fmt));
+ mp_msg(MSGT_VO, MSGL_DBG2, "<vo_direct3d>Accepted image format "
+ "(%s): %s\n", type, vo_format_name(cur->mplayer_fmt));
- return &fmt_table[i];
+ return cur->fourcc;
}
+ cur++;
}
return 0;
}
+// Check whether YUV conversion with shaders can be done.
+// Returns the format the individual planes should use (or 0 on failure)
+static D3DFORMAT check_shader_conversion(uint32_t fmt)
+{
+ if (priv->opt_disable_shaders)
+ return 0;
+ int component_bits;
+ if (!mp_get_chroma_shift(fmt, NULL, NULL, &component_bits))
+ return 0;
+ if (component_bits < 8 || component_bits > 16)
+ return 0;
+ bool is_8bit = component_bits == 8;
+ if (!is_8bit && priv->opt_only_8bit)
+ return 0;
+ return check_format(is_8bit ? IMGFMT_Y8 : IMGFMT_Y16, true);
+}
+
+// Return if the image format can be used. If it can, decide which rendering
+// and conversion mode to use.
+// If initialize is true, actually setup all variables to use the picked
+// rendering mode.
+static bool init_rendering_mode(uint32_t fmt, bool initialize)
+{
+ int n;
+ int blit_d3dfmt = check_format(fmt, false);
+ int texture_d3dfmt = check_format(fmt, true);
+ int shader_d3dfmt = check_shader_conversion(fmt);
+
+ if (priv->opt_disable_textures)
+ texture_d3dfmt = 0;
+ if (priv->opt_disable_shaders)
+ shader_d3dfmt = 0;
+ if (priv->opt_disable_stretchrect)
+ blit_d3dfmt = 0;
+
+ if (!(blit_d3dfmt || shader_d3dfmt || texture_d3dfmt))
+ return false;
+
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted rendering methods for "
+ "format='%s': StretchRect=%#x, Texture=%#x, Texture+Shader=%#x.\n",
+ vo_format_name(fmt), blit_d3dfmt, texture_d3dfmt, shader_d3dfmt);
+
+ if (!initialize)
+ return true;
+
+ // initialization doesn't fail beyond this point
+
+ priv->use_shaders = false;
+ priv->use_textures = false;
+ priv->movie_src_fmt = 0;
+ priv->pixel_shader_data = NULL;
+ priv->plane_count = 0;
+ priv->image_format = fmt;
+
+ if (blit_d3dfmt && priv->opt_prefer_stretchrect)
+ texture_d3dfmt = shader_d3dfmt = 0;
+
+ if (texture_d3dfmt) {
+ priv->use_textures = true;
+ } else if (shader_d3dfmt) {
+ priv->use_textures = true;
+ priv->use_shaders = true;
+ } else {
+ assert(!!blit_d3dfmt);
+ }
+
+ if (priv->use_textures) {
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Using 3D rendering.\n");
+
+ struct texplane *planes = &priv->planes[0];
+ planes[0].shift_x = planes[0].shift_y = 0;
+ planes[0].clearval = 0;
+
+ if (!priv->use_shaders) {
+ assert(IMGFMT_IS_ANY_RND(priv->image_format));
+ priv->plane_count = 1;
+ planes[0].bits_per_pixel = imgfmt_any_rnd_depth(priv->image_format);
+ planes[0].d3d_format = texture_d3dfmt;
+ } else {
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Using YUV shaders.\n");
+
+ int sx, sy, component_bits;
+ mp_get_chroma_shift(priv->image_format, &sx, &sy, &component_bits);
+ priv->plane_count = 3;
+ for (n = 0; n < 3; n++) {
+ planes[n].d3d_format = shader_d3dfmt;
+ planes[n].bits_per_pixel = component_bits;
+ if (n > 0) {
+ planes[n].shift_x = sx;
+ planes[n].shift_y = sy;
+ planes[n].clearval = get_chroma_clear_val(component_bits);
+ }
+ }
+ priv->pixel_shader_data = d3d_shader_yuv;
+ }
+
+ for (n = 0; n < priv->plane_count; n++) {
+ planes[n].bytes_per_pixel = (planes[n].bits_per_pixel + 7) / 8;
+ }
+
+ } else {
+ mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Using StretchRect.\n");
+
+ priv->movie_src_fmt = blit_d3dfmt;
+ }
+
+ update_colorspace();
+
+ return true;
+}
/** @brief Query if movie colorspace is supported by the HW.
* @return 0 on failure, device capabilities (not probed
@@ -744,7 +1192,7 @@ static const struct_fmt_table *check_format(uint32_t movie_fmt)
*/
static int query_format(uint32_t movie_fmt)
{
- if (!check_format(movie_fmt))
+ if (!init_rendering_mode(movie_fmt, false))
return 0;
int eosd_caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
@@ -764,8 +1212,52 @@ static int query_format(uint32_t movie_fmt)
* *
****************************************************************************/
+static void update_colorspace(void)
+{
+ float coeff[3][4];
+ struct mp_csp_params csp = { .colorspace = priv->colorspace };
+ mp_csp_copy_equalizer_values(&csp, &priv->video_eq);
+ if (priv->use_shaders) {
+ csp.input_bits = priv->planes[0].bits_per_pixel;
+ csp.texture_bits = (csp.input_bits + 7) & ~7;
+ }
+ mp_get_yuv2rgb_coeffs(&csp, coeff);
+ for (int row = 0; row < 3; row++) {
+ for (int col = 0; col < 4; col++) {
+ priv->d3d_colormatrix.m[row][col] = coeff[row][col];
+ }
+ }
+}
+
+const char *options_help_text = "-vo direct3d command line help:\n"
+"Example: -vo direct3d:disable-eosd:disable-textures\n"
+"Options:\n"
+" prefer-stretchrect\n"
+" Use IDirect3DDevice9::StretchRect over other methods if possible.\n"
+" disable-stretchrect\n"
+" Never render the video using IDirect3DDevice9::StretchRect.\n"
+" disable-textures\n"
+" Never render the video using D3D texture rendering. (Rendering with\n"
+" textures + shader will still be allowed. Add disable-shaders to\n"
+" completely disable video rendering with textures.)\n"
+" disable-shaders\n"
+" Never use shaders when rendering video.\n"
+" only-8bit\n"
+" Never render YUV video with more than 8 bits per component.\n"
+" (Using this flag will force software conversion to 8 bit.)\n"
+" disable-eosd\n"
+" Disable EOSD rendering for subtitles.\n"
+" (Using this flag might force the insertion of the 'ass' video filter,\n"
+" which will render the subtitles in software.)\n"
+" force-power-of-2\n"
+" Always force textures to power of 2, even if the device reports\n"
+" non-power-of-2 texture sizes as supported.\n"
+" disable-texture-align\n"
+" Normally texture sizes are always aligned to 16. With this option\n"
+" enabled, the video texture will always have exactly the same size as\n"
+" the video itself.\n";
/** @brief libvo Callback: Preinitialize the video card.
* Preinit the hardware just enough to be queried about
@@ -783,11 +1275,30 @@ static int preinit(const char *arg)
/* Set to zero all global variables. */
priv = talloc_zero(NULL, struct global_priv);
+ *priv = (struct global_priv) {
+ .colorspace = MP_CSP_DETAILS_DEFAULTS,
+ .video_eq = { MP_CSP_EQ_CAPS_COLORMATRIX },
+ };
+
+ int opt_force_power_of_2 = false;
+
+ const opt_t subopts[] = {
+ {"prefer-stretchrect", OPT_ARG_BOOL, &priv->opt_prefer_stretchrect, NULL},
+ {"disable-textures", OPT_ARG_BOOL, &priv->opt_disable_textures, NULL},
+ {"disable-stretchrect", OPT_ARG_BOOL, &priv->opt_disable_stretchrect, NULL},
+ {"disable-shaders", OPT_ARG_BOOL, &priv->opt_disable_shaders, NULL},
+ {"only-8bit", OPT_ARG_BOOL, &priv->opt_only_8bit, NULL},
+ {"disable-eosd", OPT_ARG_BOOL, &priv->opt_disable_eosd, NULL},
+ {"force-power-of-2", OPT_ARG_BOOL, &opt_force_power_of_2, NULL},
+ {"disable-texture-align", OPT_ARG_BOOL, &priv->opt_disable_texture_align, NULL},
+ {NULL}
+ };
+ if (subopt_parse(arg, subopts) != 0) {
+ mp_msg(MSGT_VO, MSGL_FATAL, options_help_text);
+ return -1;
+ }
- //xxx make it possible to disable eosd by command line
- bool enable_eosd = true;
-
- if (enable_eosd)
+ if (!priv->opt_disable_eosd)
priv->eosd = eosd_packer_create(priv);
priv->d3d9_dll = LoadLibraryA("d3d9.dll");
@@ -825,7 +1336,7 @@ static int preinit(const char *arg)
if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle,
D3DADAPTER_DEFAULT,
-