Split i422->packed YUV and i422->planar YUV. Add i422->YUVA. This still needs some...
[vlc.git] / modules / video_chroma / i422_i420.c
1 /*****************************************************************************
2  * i422_i420.c : Planar YUV 4:2:2 to Planar YUV 4:2:0 conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 - 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31
32 #define SRC_FOURCC  "I422,J422"
33 #define DEST_FOURCC "I420,IYUV,J420,YV12,YUVA"
34
35 /*****************************************************************************
36  * Local and extern prototypes.
37  *****************************************************************************/
38 static int  Activate ( vlc_object_t * );
39
40 static void I422_I420( vout_thread_t *, picture_t *, picture_t * );
41 static void I422_YV12( vout_thread_t *, picture_t *, picture_t * );
42 static void I422_YUVA( vout_thread_t *, picture_t *, picture_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
49     set_capability( "chroma", 60 );
50     set_callbacks( Activate, NULL );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Activate: allocate a chroma function
55  *****************************************************************************
56  * This function allocates and initializes a chroma function
57  *****************************************************************************/
58 static int Activate( vlc_object_t *p_this )
59 {
60     vout_thread_t *p_vout = (vout_thread_t *)p_this;
61
62     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
63     {
64         return -1;
65     }
66
67     switch( p_vout->render.i_chroma )
68     {
69         case VLC_FOURCC('I','4','2','2'):
70         case VLC_FOURCC('J','4','2','2'):
71             switch( p_vout->output.i_chroma )
72             {
73                 case VLC_FOURCC('I','4','2','0'):
74                 case VLC_FOURCC('I','Y','U','V'):
75                 case VLC_FOURCC('J','4','2','0'):
76                     p_vout->chroma.pf_convert = I422_I420;
77                     break;
78
79                 case VLC_FOURCC('Y','V','1','2'):
80                     p_vout->chroma.pf_convert = I422_YV12;
81                     break;
82
83                 case VLC_FOURCC('Y','U','V','A'):
84                     p_vout->chroma.pf_convert = I422_YUVA;
85                     break;
86
87                 default:
88                     return -1;
89             }
90             break;
91
92         default:
93             return -1;
94     }
95     return 0;
96 }
97
98 /* Following functions are local */
99
100 /*****************************************************************************
101  * I422_I420: planar YUV 4:2:2 to planar I420 4:2:0 Y:U:V
102  *****************************************************************************/
103 static void I422_I420( vout_thread_t *p_vout, picture_t *p_source,
104                                               picture_t *p_dest )
105 {
106     uint16_t i_dpy = p_dest->p[Y_PLANE].i_pitch;
107     uint16_t i_spy = p_source->p[Y_PLANE].i_pitch;
108     uint16_t i_dpuv = p_dest->p[U_PLANE].i_pitch;
109     uint16_t i_spuv = p_source->p[U_PLANE].i_pitch;
110     uint16_t i_width = p_vout->render.i_width;
111     uint16_t i_y = p_vout->render.i_height;
112     uint8_t *p_dy = p_dest->Y_PIXELS + (i_y-1)*i_dpy;
113     uint8_t *p_y = p_source->Y_PIXELS + (i_y-1)*i_spy;
114     uint8_t *p_du = p_dest->U_PIXELS + (i_y/2-1)*i_dpuv;
115     uint8_t *p_u = p_source->U_PIXELS + (i_y-1)*i_spuv;
116     uint8_t *p_dv = p_dest->V_PIXELS + (i_y/2-1)*i_dpuv;
117     uint8_t *p_v = p_source->V_PIXELS + (i_y-1)*i_spuv;
118     void *(*pf_memcpy)(void *, const void *, size_t) = p_vout->p_libvlc->pf_memcpy;
119     i_y /= 2;
120
121     for ( ; i_y--; )
122     {
123         pf_memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
124         pf_memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
125         pf_memcpy(p_du, p_u, i_width/2); p_du -= i_dpuv; p_u -= 2*i_spuv;
126         pf_memcpy(p_dv, p_v, i_width/2); p_dv -= i_dpuv; p_v -= 2*i_spuv;
127     }
128 }
129
130 /*****************************************************************************
131  * I422_YV12: planar YUV 4:2:2 to planar YV12 4:2:0 Y:V:U
132  *****************************************************************************/
133 static void I422_YV12( vout_thread_t *p_vout, picture_t *p_source,
134                                               picture_t *p_dest )
135 {
136     uint16_t i_dpy = p_dest->p[Y_PLANE].i_pitch;
137     uint16_t i_spy = p_source->p[Y_PLANE].i_pitch;
138     uint16_t i_dpuv = p_dest->p[U_PLANE].i_pitch;
139     uint16_t i_spuv = p_source->p[U_PLANE].i_pitch;
140     uint16_t i_width = p_vout->render.i_width;
141     uint16_t i_y = p_vout->render.i_height;
142     uint8_t *p_dy = p_dest->Y_PIXELS + (i_y-1)*i_dpy;
143     uint8_t *p_y = p_source->Y_PIXELS + (i_y-1)*i_spy;
144     uint8_t *p_du = p_dest->V_PIXELS + (i_y/2-1)*i_dpuv; /* U and V are swapped */
145     uint8_t *p_u = p_source->U_PIXELS + (i_y-1)*i_spuv;
146     uint8_t *p_dv = p_dest->U_PIXELS + (i_y/2-1)*i_dpuv; /* U and V are swapped */
147     uint8_t *p_v = p_source->V_PIXELS + (i_y-1)*i_spuv;
148     void *(*pf_memcpy)(void *, const void *, size_t) = p_vout->p_libvlc->pf_memcpy;
149     i_y /= 2;
150
151     for ( ; i_y--; )
152     {
153         pf_memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
154         pf_memcpy(p_dy, p_y, i_width); p_dy -= i_dpy; p_y -= i_spy;
155         pf_memcpy(p_du, p_u, i_width/2); p_du -= i_dpuv; p_u -= 2*i_spuv;
156         pf_memcpy(p_dv, p_v, i_width/2); p_dv -= i_dpuv; p_v -= 2*i_spuv;
157     }
158 }
159
160 /*****************************************************************************
161  * I422_YUVA: planar YUV 4:2:2 to planar YUVA 4:2:0:4 Y:U:V:A
162  *****************************************************************************/
163 static void I422_YUVA( vout_thread_t *p_vout, picture_t *p_source,
164                                               picture_t *p_dest )
165 {
166     I422_I420( p_vout, p_source, p_dest );
167     p_vout->p_libvlc->pf_memset( p_dest->p[A_PLANE].p_pixels, 0xff,
168                                  p_dest->p[A_PLANE].i_lines
169                                  * p_dest->p[A_PLANE].i_pitch );
170 }