d3d11_surface: avoid a cast between ID3D11Texture2D* and ID3D11Resource*
[vlc.git] / modules / video_chroma / dxa9.c
1 /*****************************************************************************
2  * dxa9.c : DXVA2 GPU surface conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2015 VLC authors, VideoLAN and VideoLabs
5  * $Id$
6  *
7  * Authors: Steve Lhomme <robux4@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
36
37 #include "copy.h"
38
39 static int  OpenConverter( vlc_object_t * );
40 static void CloseConverter( vlc_object_t * );
41
42 /*****************************************************************************
43  * Module descriptor.
44  *****************************************************************************/
45 vlc_module_begin ()
46     set_description( N_("Conversions from DxVA2 to YUV") )
47     set_capability( "video converter", 10 )
48     set_callbacks( OpenConverter, CloseConverter )
49 vlc_module_end ()
50
51 #include <windows.h>
52 #include <d3d9.h>
53
54 struct picture_sys_t
55 {
56     LPDIRECT3DSURFACE9 surface;
57 };
58
59 static bool GetLock(filter_t *p_filter, LPDIRECT3DSURFACE9 d3d,
60                     D3DLOCKED_RECT *p_lock, D3DSURFACE_DESC *p_desc)
61 {
62     if (FAILED( IDirect3DSurface9_GetDesc(d3d, p_desc)))
63         return false;
64
65     /* */
66     if (FAILED(IDirect3DSurface9_LockRect(d3d, p_lock, NULL, D3DLOCK_READONLY))) {
67         msg_Err(p_filter, "Failed to lock surface");
68         return false;
69     }
70
71     return true;
72 }
73
74 static void DXA9_YV12(filter_t *p_filter, picture_t *src, picture_t *dst)
75 {
76     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
77
78     D3DSURFACE_DESC desc;
79     D3DLOCKED_RECT lock;
80     if (!GetLock(p_filter, src->p_sys->surface, &lock, &desc))
81         return;
82
83     if (dst->format.i_chroma == VLC_CODEC_I420) {
84         uint8_t *tmp = dst->p[1].p_pixels;
85         dst->p[1].p_pixels = dst->p[2].p_pixels;
86         dst->p[2].p_pixels = tmp;
87     }
88
89     if (desc.Format == MAKEFOURCC('Y','V','1','2') ||
90         desc.Format == MAKEFOURCC('I','M','C','3')) {
91         bool imc3 = desc.Format == MAKEFOURCC('I','M','C','3');
92         size_t chroma_pitch = imc3 ? lock.Pitch : (lock.Pitch / 2);
93
94         size_t pitch[3] = {
95             lock.Pitch,
96             chroma_pitch,
97             chroma_pitch,
98         };
99
100         uint8_t *plane[3] = {
101             (uint8_t*)lock.pBits,
102             (uint8_t*)lock.pBits + pitch[0] * src->format.i_height,
103             (uint8_t*)lock.pBits + pitch[0] * src->format.i_height
104                                  + pitch[1] * src->format.i_height / 2,
105         };
106
107         if (imc3) {
108             uint8_t *V = plane[1];
109             plane[1] = plane[2];
110             plane[2] = V;
111         }
112         CopyFromYv12(dst, plane, pitch, src->format.i_height, p_copy_cache);
113     } else if (desc.Format == MAKEFOURCC('N','V','1','2')) {
114         uint8_t *plane[2] = {
115             lock.pBits,
116             (uint8_t*)lock.pBits + lock.Pitch * src->format.i_height
117         };
118         size_t  pitch[2] = {
119             lock.Pitch,
120             lock.Pitch,
121         };
122         CopyFromNv12(dst, plane, pitch, src->format.i_height, p_copy_cache);
123     } else {
124         msg_Err(p_filter, "Unsupported DXA9 conversion from 0x%08X to YV12", desc.Format);
125     }
126
127     if (dst->format.i_chroma == VLC_CODEC_I420) {
128         uint8_t *tmp = dst->p[1].p_pixels;
129         dst->p[1].p_pixels = dst->p[2].p_pixels;
130         dst->p[2].p_pixels = tmp;
131     }
132
133     /* */
134     IDirect3DSurface9_UnlockRect(src->p_sys->surface);
135 }
136
137 static void DXA9_NV12(filter_t *p_filter, picture_t *src, picture_t *dst)
138 {
139     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
140
141     D3DSURFACE_DESC desc;
142     D3DLOCKED_RECT lock;
143     if (!GetLock(p_filter, src->p_sys->surface, &lock, &desc))
144         return;
145
146     if (desc.Format == MAKEFOURCC('N','V','1','2')) {
147         uint8_t *plane[2] = {
148             lock.pBits,
149             (uint8_t*)lock.pBits + lock.Pitch * src->format.i_height
150         };
151         size_t  pitch[2] = {
152             lock.Pitch,
153             lock.Pitch,
154         };
155         CopyFromNv12ToNv12(dst, plane, pitch, src->format.i_height, p_copy_cache);
156     } else {
157         msg_Err(p_filter, "Unsupported DXA9 conversion from 0x%08X to NV12", desc.Format);
158     }
159
160     /* */
161     IDirect3DSurface9_UnlockRect(src->p_sys->surface);
162 }
163
164 VIDEO_FILTER_WRAPPER (DXA9_YV12)
165 VIDEO_FILTER_WRAPPER (DXA9_NV12)
166
167 static int OpenConverter( vlc_object_t *obj )
168 {
169     filter_t *p_filter = (filter_t *)obj;
170     if ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_D3D9_OPAQUE )
171         return VLC_EGENERIC;
172
173     if ( p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height
174          || p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
175         return VLC_EGENERIC;
176
177     switch( p_filter->fmt_out.video.i_chroma ) {
178     case VLC_CODEC_I420:
179     case VLC_CODEC_YV12:
180         p_filter->pf_video_filter = DXA9_YV12_Filter;
181         break;
182     case VLC_CODEC_NV12:
183         p_filter->pf_video_filter = DXA9_NV12_Filter;
184         break;
185     default:
186         return VLC_EGENERIC;
187     }
188
189     copy_cache_t *p_copy_cache = calloc(1, sizeof(*p_copy_cache));
190     if (!p_copy_cache)
191          return VLC_ENOMEM;
192     CopyInitCache(p_copy_cache, p_filter->fmt_in.video.i_width );
193     p_filter->p_sys = (filter_sys_t*) p_copy_cache;
194
195     return VLC_SUCCESS;
196 }
197
198 static void CloseConverter( vlc_object_t *obj )
199 {
200     filter_t *p_filter = (filter_t *)obj;
201     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
202     CopyCleanCache(p_copy_cache);
203     free( p_copy_cache );
204     p_filter->p_sys = NULL;
205 }