chroma: move cvpx_i420 to cxpx
[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 #include "d3d9_fmt.h"
54
55 static bool GetLock(filter_t *p_filter, LPDIRECT3DSURFACE9 d3d,
56                     D3DLOCKED_RECT *p_lock, D3DSURFACE_DESC *p_desc)
57 {
58     if (FAILED( IDirect3DSurface9_GetDesc(d3d, p_desc)))
59         return false;
60
61     /* */
62     if (FAILED(IDirect3DSurface9_LockRect(d3d, p_lock, NULL, D3DLOCK_READONLY))) {
63         msg_Err(p_filter, "Failed to lock surface");
64         return false;
65     }
66
67     return true;
68 }
69
70 static void DXA9_YV12(filter_t *p_filter, picture_t *src, picture_t *dst)
71 {
72     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
73
74     D3DSURFACE_DESC desc;
75     D3DLOCKED_RECT lock;
76     if (!GetLock(p_filter, src->p_sys->surface, &lock, &desc))
77         return;
78
79     if (dst->format.i_chroma == VLC_CODEC_I420) {
80         uint8_t *tmp = dst->p[1].p_pixels;
81         dst->p[1].p_pixels = dst->p[2].p_pixels;
82         dst->p[2].p_pixels = tmp;
83     }
84
85     if (desc.Format == MAKEFOURCC('Y','V','1','2') ||
86         desc.Format == MAKEFOURCC('I','M','C','3')) {
87         bool imc3 = desc.Format == MAKEFOURCC('I','M','C','3');
88         size_t chroma_pitch = imc3 ? lock.Pitch : (lock.Pitch / 2);
89
90         size_t pitch[3] = {
91             lock.Pitch,
92             chroma_pitch,
93             chroma_pitch,
94         };
95
96         uint8_t *plane[3] = {
97             (uint8_t*)lock.pBits,
98             (uint8_t*)lock.pBits + pitch[0] * src->format.i_height,
99             (uint8_t*)lock.pBits + pitch[0] * src->format.i_height
100                                  + pitch[1] * src->format.i_height / 2,
101         };
102
103         if (imc3) {
104             uint8_t *V = plane[1];
105             plane[1] = plane[2];
106             plane[2] = V;
107         }
108         CopyFromYv12ToYv12(dst, plane, pitch,
109                            src->format.i_height, p_copy_cache);
110     } else if (desc.Format == MAKEFOURCC('N','V','1','2')) {
111         uint8_t *plane[2] = {
112             lock.pBits,
113             (uint8_t*)lock.pBits + lock.Pitch * src->format.i_height
114         };
115         size_t  pitch[2] = {
116             lock.Pitch,
117             lock.Pitch,
118         };
119         CopyFromNv12ToYv12(dst, plane, pitch,
120                            src->format.i_height, p_copy_cache);
121     } else {
122         msg_Err(p_filter, "Unsupported DXA9 conversion from 0x%08X to YV12", desc.Format);
123     }
124
125     if (dst->format.i_chroma == VLC_CODEC_I420) {
126         uint8_t *tmp = dst->p[1].p_pixels;
127         dst->p[1].p_pixels = dst->p[2].p_pixels;
128         dst->p[2].p_pixels = tmp;
129     }
130
131     /* */
132     IDirect3DSurface9_UnlockRect(src->p_sys->surface);
133 }
134
135 static void DXA9_NV12(filter_t *p_filter, picture_t *src, picture_t *dst)
136 {
137     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
138
139     D3DSURFACE_DESC desc;
140     D3DLOCKED_RECT lock;
141     if (!GetLock(p_filter, src->p_sys->surface, &lock, &desc))
142         return;
143
144     if (desc.Format == MAKEFOURCC('N','V','1','2')) {
145         uint8_t *plane[2] = {
146             lock.pBits,
147             (uint8_t*)lock.pBits + lock.Pitch * src->format.i_height
148         };
149         size_t  pitch[2] = {
150             lock.Pitch,
151             lock.Pitch,
152         };
153         CopyFromNv12ToNv12(dst, plane, pitch, src->format.i_height, p_copy_cache);
154     } else {
155         msg_Err(p_filter, "Unsupported DXA9 conversion from 0x%08X to NV12", desc.Format);
156     }
157
158     /* */
159     IDirect3DSurface9_UnlockRect(src->p_sys->surface);
160 }
161
162 VIDEO_FILTER_WRAPPER (DXA9_YV12)
163 VIDEO_FILTER_WRAPPER (DXA9_NV12)
164
165 static int OpenConverter( vlc_object_t *obj )
166 {
167     filter_t *p_filter = (filter_t *)obj;
168     if ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_D3D9_OPAQUE )
169         return VLC_EGENERIC;
170
171     if ( p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height
172          || p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
173         return VLC_EGENERIC;
174
175     switch( p_filter->fmt_out.video.i_chroma ) {
176     case VLC_CODEC_I420:
177     case VLC_CODEC_YV12:
178         p_filter->pf_video_filter = DXA9_YV12_Filter;
179         break;
180     case VLC_CODEC_NV12:
181         p_filter->pf_video_filter = DXA9_NV12_Filter;
182         break;
183     default:
184         return VLC_EGENERIC;
185     }
186
187     copy_cache_t *p_copy_cache = calloc(1, sizeof(*p_copy_cache));
188     if (!p_copy_cache)
189          return VLC_ENOMEM;
190     CopyInitCache(p_copy_cache, p_filter->fmt_in.video.i_width );
191     p_filter->p_sys = (filter_sys_t*) p_copy_cache;
192
193     return VLC_SUCCESS;
194 }
195
196 static void CloseConverter( vlc_object_t *obj )
197 {
198     filter_t *p_filter = (filter_t *)obj;
199     copy_cache_t *p_copy_cache = (copy_cache_t*) p_filter->p_sys;
200     CopyCleanCache(p_copy_cache);
201     free( p_copy_cache );
202     p_filter->p_sys = NULL;
203 }