Win32: add a new build-script helper
[vlc.git] / modules / video_chroma / i420_nv12.c
blob4a1a606a6c4aeb0aabb1821e9734af2f3c091f68
1 /*****************************************************************************
2 * i420_nv12.c : Planar YUV 4:2:0 to SemiPlanar NV12 4:2:0
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
6 * Authors: Steve Lhomme <robux4@videolabs.io>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
35 #include "copy.h"
37 /*****************************************************************************
38 * Local and extern prototypes.
39 *****************************************************************************/
40 static void I420_NV12( filter_t *, picture_t *, picture_t * );
41 static void YV12_NV12( filter_t *, picture_t *, picture_t * );
42 static picture_t *I420_NV12_Filter( filter_t *, picture_t * );
43 static picture_t *YV12_NV12_Filter( filter_t *, picture_t * );
45 struct filter_sys_t
47 copy_cache_t cache;
50 /*****************************************************************************
51 * Create: allocate a chroma function
52 *****************************************************************************
53 * This function allocates and initializes a chroma function
54 *****************************************************************************/
55 static int Create( vlc_object_t *p_this )
57 filter_t *p_filter = (filter_t *)p_this;
59 if ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_NV12 )
60 return -1;
62 /* video must be even, because 4:2:0 is subsampled by 2 in both ways */
63 if( p_filter->fmt_in.video.i_width & 1
64 || p_filter->fmt_in.video.i_height & 1 )
66 return -1;
69 /* resizing not supported */
70 if( p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width !=
71 p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width
72 || p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height !=
73 p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height
74 || p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
75 return -1;
77 switch( p_filter->fmt_in.video.i_chroma )
79 case VLC_CODEC_I420:
80 case VLC_CODEC_J420:
81 p_filter->pf_video_filter = I420_NV12_Filter;
82 break;
84 case VLC_CODEC_YV12:
85 p_filter->pf_video_filter = YV12_NV12_Filter;
86 break;
88 default:
89 return -1;
92 filter_sys_t *p_sys = vlc_obj_alloc( VLC_OBJECT( p_filter ), 1,
93 sizeof(filter_sys_t) );
94 if (!p_sys)
95 return VLC_ENOMEM;
97 CopyInitCache( &p_sys->cache, p_filter->fmt_in.video.i_x_offset +
98 p_filter->fmt_in.video.i_visible_width );
99 p_filter->p_sys = p_sys;
101 return 0;
104 static void Delete(vlc_object_t *p_this)
106 filter_t *p_filter = (filter_t *)p_this;
107 filter_sys_t *p_sys = p_filter->p_sys;
108 CopyCleanCache( &p_sys->cache );
111 /* Following functions are local */
112 VIDEO_FILTER_WRAPPER( I420_NV12 )
113 VIDEO_FILTER_WRAPPER( YV12_NV12 )
115 static void I420_YUV( filter_sys_t *p_sys, picture_t *p_src, picture_t *p_dst, bool invertUV )
117 p_dst->format.i_x_offset = p_src->format.i_x_offset;
118 p_dst->format.i_y_offset = p_src->format.i_y_offset;
120 const size_t u_plane = invertUV ? V_PLANE : U_PLANE;
121 const size_t v_plane = invertUV ? U_PLANE : V_PLANE;
123 size_t pitch[3] = {
124 p_src->p[Y_PLANE].i_pitch,
125 p_src->p[u_plane].i_pitch,
126 p_src->p[v_plane].i_pitch,
129 uint8_t *plane[3] = {
130 (uint8_t*)p_src->p[Y_PLANE].p_pixels,
131 (uint8_t*)p_src->p[u_plane].p_pixels,
132 (uint8_t*)p_src->p[v_plane].p_pixels,
135 CopyFromI420ToNv12( p_dst, plane, pitch,
136 p_src->format.i_y_offset + p_src->format.i_visible_height,
137 &p_sys->cache );
140 /*****************************************************************************
141 * planar I420 4:2:0 Y:U:V to planar NV12 4:2:0 Y:UV
142 *****************************************************************************/
143 static void I420_NV12( filter_t *p_filter, picture_t *p_src,
144 picture_t *p_dst )
146 I420_YUV( p_filter->p_sys, p_src, p_dst, false );
149 /*****************************************************************************
150 * planar YV12 4:2:0 Y:V:U to planar NV12 4:2:0 Y:UV
151 *****************************************************************************/
152 static void YV12_NV12( filter_t *p_filter, picture_t *p_src,
153 picture_t *p_dst )
155 I420_YUV( p_filter->p_sys, p_src, p_dst, true );
159 /*****************************************************************************
160 * Module descriptor
161 *****************************************************************************/
162 vlc_module_begin ()
163 set_description( N_("YUV planar to semiplanar conversions") )
164 set_capability( "video converter", 160 )
165 set_callbacks( Create, Delete )
166 vlc_module_end ()