]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb.h
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc] / modules / video_chroma / i420_rgb.h
1 /*****************************************************************************
2  * i420_rgb.h : YUV to bitmap RGB conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: i420_rgb.h,v 1.1 2002/08/04 17:23:43 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * chroma_sys_t: chroma method descriptor
26  *****************************************************************************
27  * This structure is part of the chroma transformation descriptor, it
28  * describes the yuv2rgb specific properties.
29  *****************************************************************************/
30 struct chroma_sys_t
31 {
32     u8  *p_buffer;
33     int *p_offset;
34
35 #ifdef MODULE_NAME_IS_i420_rgb
36     /* Pre-calculated conversion tables */
37     void *p_base;                          /* base for all conversion tables */
38     u8   *p_rgb8;                                        /* RGB 8 bits table */
39     u16  *p_rgb16;                                      /* RGB 16 bits table */
40     u32  *p_rgb32;                                      /* RGB 32 bits table */
41 #endif
42 };
43
44 /*****************************************************************************
45  * Prototypes
46  *****************************************************************************/
47 #ifdef MODULE_NAME_IS_i420_rgb
48 void E_(I420_RGB8) ( vout_thread_t *, picture_t *, picture_t * );
49 #endif
50 void E_(I420_RGB15)( vout_thread_t *, picture_t *, picture_t * );
51 void E_(I420_RGB16)( vout_thread_t *, picture_t *, picture_t * );
52 void E_(I420_RGB32)( vout_thread_t *, picture_t *, picture_t * );
53
54 /*****************************************************************************
55  * CONVERT_*_PIXEL: pixel conversion macros
56  *****************************************************************************
57  * These conversion routines are used by YUV conversion functions.
58  * conversion are made from p_y, p_u, p_v, which are modified, to p_buffer,
59  * which is also modified. CONVERT_4YUV_PIXEL is used for 8bpp dithering,
60  * CONVERT_4YUV_PIXEL_SCALE does the same but also scales the output.
61  *****************************************************************************/
62 #define CONVERT_Y_PIXEL( BPP )                                                \
63     /* Only Y sample is present */                                            \
64     p_ybase = p_yuv + *p_y++;                                                 \
65     *p_buffer++ = p_ybase[RED_OFFSET-((V_RED_COEF*128)>>SHIFT) + i_red] |     \
66         p_ybase[GREEN_OFFSET-(((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT)       \
67         + i_green ] | p_ybase[BLUE_OFFSET-((U_BLUE_COEF*128)>>SHIFT) + i_blue];
68
69 #define CONVERT_YUV_PIXEL( BPP )                                              \
70     /* Y, U and V samples are present */                                      \
71     i_uval =    *p_u++;                                                       \
72     i_vval =    *p_v++;                                                       \
73     i_red =     (V_RED_COEF * i_vval) >> SHIFT;                               \
74     i_green =   (U_GREEN_COEF * i_uval + V_GREEN_COEF * i_vval) >> SHIFT;     \
75     i_blue =    (U_BLUE_COEF * i_uval) >> SHIFT;                              \
76     CONVERT_Y_PIXEL( BPP )                                                    \
77
78 #define CONVERT_4YUV_PIXEL( CHROMA )                                          \
79     *p_pic++ = p_lookup[                                                      \
80         (((*p_y++ + dither10[i_real_y]) >> 4) << 7)                           \
81       + ((*p_u + dither20[i_real_y]) >> 5) * 9                                \
82       + ((*p_v + dither20[i_real_y]) >> 5) ];                                 \
83     *p_pic++ = p_lookup[                                                      \
84         (((*p_y++ + dither11[i_real_y]) >> 4) << 7)                           \
85       + ((*p_u++ + dither21[i_real_y]) >> 5) * 9                              \
86       + ((*p_v++ + dither21[i_real_y]) >> 5) ];                               \
87     *p_pic++ = p_lookup[                                                      \
88         (((*p_y++ + dither12[i_real_y]) >> 4) << 7)                           \
89       + ((*p_u + dither22[i_real_y]) >> 5) * 9                                \
90       + ((*p_v + dither22[i_real_y]) >> 5) ];                                 \
91     *p_pic++ = p_lookup[                                                      \
92         (((*p_y++ + dither13[i_real_y]) >> 4) << 7)                           \
93       + ((*p_u++ + dither23[i_real_y]) >> 5) * 9                              \
94       + ((*p_v++ + dither23[i_real_y]) >> 5) ];                               \
95
96 #define CONVERT_4YUV_PIXEL_SCALE( CHROMA )                                    \
97     *p_pic++ = p_lookup[                                                      \
98         ( ((*p_y + dither10[i_real_y]) >> 4) << 7)                            \
99         + ((*p_u + dither20[i_real_y]) >> 5) * 9                              \
100         + ((*p_v + dither20[i_real_y]) >> 5) ];                               \
101     p_y += *p_offset++;                                                       \
102     p_u += *p_offset;                                                         \
103     p_v += *p_offset++;                                                       \
104     *p_pic++ = p_lookup[                                                      \
105         ( ((*p_y + dither11[i_real_y]) >> 4) << 7)                            \
106         + ((*p_u + dither21[i_real_y]) >> 5) * 9                              \
107         + ((*p_v + dither21[i_real_y]) >> 5) ];                               \
108     p_y += *p_offset++;                                                       \
109     p_u += *p_offset;                                                         \
110     p_v += *p_offset++;                                                       \
111     *p_pic++ = p_lookup[                                                      \
112         ( ((*p_y + dither12[i_real_y]) >> 4) << 7)                            \
113         + ((*p_u + dither22[i_real_y]) >> 5) * 9                              \
114         + ((*p_v + dither22[i_real_y]) >> 5) ];                               \
115     p_y += *p_offset++;                                                       \
116     p_u += *p_offset;                                                         \
117     p_v += *p_offset++;                                                       \
118     *p_pic++ = p_lookup[                                                      \
119         ( ((*p_y + dither13[i_real_y]) >> 4) << 7)                            \
120         + ((*p_u + dither23[i_real_y]) >> 5) * 9                              \
121         + ((*p_v + dither23[i_real_y]) >> 5) ];                               \
122     p_y += *p_offset++;                                                       \
123     p_u += *p_offset;                                                         \
124     p_v += *p_offset++;                                                       \
125
126 /*****************************************************************************
127  * SCALE_WIDTH: scale a line horizontally
128  *****************************************************************************
129  * This macro scales a line using rendering buffer and offset array. It works
130  * for 1, 2 and 4 Bpp.
131  *****************************************************************************/
132 #define SCALE_WIDTH                                                           \
133     if( b_hscale )                                                            \
134     {                                                                         \
135         /* Horizontal scaling, conversion has been done to buffer.            \
136          * Rewind buffer and offset, then copy and scale line */              \
137         p_buffer = p_buffer_start;                                            \
138         p_offset = p_offset_start;                                            \
139         for( i_x = p_vout->output.i_width / 16; i_x--; )                      \
140         {                                                                     \
141             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
142             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
143             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
144             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
145             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
146             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
147             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
148             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
149             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
150             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
151             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
152             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
153             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
154             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
155             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
156             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
157         }                                                                     \
158         for( i_x = p_vout->output.i_width & 15; i_x--; )                      \
159         {                                                                     \
160             *p_pic++ = *p_buffer;   p_buffer += *p_offset++;                  \
161         }                                                                     \
162         p_pic += i_right_margin;                                              \
163     }                                                                         \
164     else                                                                      \
165     {                                                                         \
166         /* No scaling, conversion has been done directly in picture memory.   \
167          * Increment of picture pointer to end of line is still needed */     \
168         (u8*)p_pic += p_dest->p->i_pitch;                                     \
169     }                                                                         \
170
171 /*****************************************************************************
172  * SCALE_WIDTH_DITHER: scale a line horizontally for dithered 8 bpp
173  *****************************************************************************
174  * This macro scales a line using an offset array.
175  *****************************************************************************/
176 #define SCALE_WIDTH_DITHER( CHROMA )                                          \
177     if( b_hscale )                                                            \
178     {                                                                         \
179         /* Horizontal scaling - we can't use a buffer due to dithering */     \
180         p_offset = p_offset_start;                                            \
181         for( i_x = p_vout->output.i_width / 16; i_x--; )                      \
182         {                                                                     \
183             CONVERT_4YUV_PIXEL_SCALE( CHROMA )                                \
184             CONVERT_4YUV_PIXEL_SCALE( CHROMA )                                \
185             CONVERT_4YUV_PIXEL_SCALE( CHROMA )                                \
186             CONVERT_4YUV_PIXEL_SCALE( CHROMA )                                \
187         }                                                                     \
188     }                                                                         \
189     else                                                                      \
190     {                                                                         \
191         for( i_x = p_vout->render.i_width / 16; i_x--;  )                     \
192         {                                                                     \
193             CONVERT_4YUV_PIXEL( CHROMA )                                      \
194             CONVERT_4YUV_PIXEL( CHROMA )                                      \
195             CONVERT_4YUV_PIXEL( CHROMA )                                      \
196             CONVERT_4YUV_PIXEL( CHROMA )                                      \
197         }                                                                     \
198     }                                                                         \
199     /* Increment of picture pointer to end of line is still needed */         \
200     p_pic += i_right_margin;                                                  \
201                                                                               \
202     /* Increment the Y coordinate in the matrix, modulo 4 */                  \
203     i_real_y = (i_real_y + 1) & 0x3;                                          \
204
205 /*****************************************************************************
206  * SCALE_HEIGHT: handle vertical scaling
207  *****************************************************************************
208  * This macro handle vertical scaling for a picture. CHROMA may be 420, 422 or
209  * 444 for RGB conversion, or 400 for gray conversion. It works for 1, 2, 3
210  * and 4 Bpp.
211  *****************************************************************************/
212 #define SCALE_HEIGHT( CHROMA, BPP )                                           \
213     /* If line is odd, rewind 4:2:0 U and V samples */                        \
214     if( ((CHROMA == 420) || (CHROMA == 422)) && !(i_y & 0x1) )                \
215     {                                                                         \
216         p_u -= i_chroma_width;                                                \
217         p_v -= i_chroma_width;                                                \
218     }                                                                         \
219                                                                               \
220     /*                                                                        \
221      * Handle vertical scaling. The current line can be copied or next one    \
222      * can be ignored.                                                        \
223      */                                                                       \
224     switch( i_vscale )                                                        \
225     {                                                                         \
226     case -1:                             /* vertical scaling factor is < 1 */ \
227         while( (i_scale_count -= p_vout->output.i_height) > 0 )               \
228         {                                                                     \
229             /* Height reduction: skip next source line */                     \
230             p_y += p_vout->render.i_width;                                    \
231             i_y++;                                                            \
232             if( (CHROMA == 420) || (CHROMA == 422) )                          \
233             {                                                                 \
234                 if( i_y & 0x1 )                                               \
235                 {                                                             \
236                     p_u += i_chroma_width;                                    \
237                     p_v += i_chroma_width;                                    \
238                 }                                                             \
239             }                                                                 \
240             else if( CHROMA == 444 )                                          \
241             {                                                                 \
242                 p_u += p_vout->render.i_width;                                \
243                 p_v += p_vout->render.i_width;                                \
244             }                                                                 \
245         }                                                                     \
246         i_scale_count += p_vout->render.i_height;                             \
247         break;                                                                \
248     case 1:                              /* vertical scaling factor is > 1 */ \
249         while( (i_scale_count -= p_vout->render.i_height) > 0 )               \
250         {                                                                     \
251             /* Height increment: copy previous picture line */                \
252             p_vout->p_vlc->pf_memcpy( p_pic, p_pic_start,                     \
253                                       p_vout->output.i_width * BPP );         \
254             (u8*)p_pic += p_dest->p->i_pitch;                                 \
255         }                                                                     \
256         i_scale_count += p_vout->output.i_height;                             \
257         break;                                                                \
258     }                                                                         \
259
260 /*****************************************************************************
261  * SCALE_HEIGHT_DITHER: handle vertical scaling for dithered 8 bpp
262  *****************************************************************************
263  * This macro handles vertical scaling for a picture. CHROMA may be 420,
264  * 422 or 444 for RGB conversion, or 400 for gray conversion.
265  *****************************************************************************/
266 #define SCALE_HEIGHT_DITHER( CHROMA )                                         \
267                                                                               \
268     /* If line is odd, rewind 4:2:0 U and V samples */                        \
269     if( ((CHROMA == 420) || (CHROMA == 422)) && !(i_y & 0x1) )                \
270     {                                                                         \
271         p_u -= i_chroma_width;                                                \
272         p_v -= i_chroma_width;                                                \
273     }                                                                         \
274                                                                               \
275     /*                                                                        \
276      * Handle vertical scaling. The current line can be copied or next one    \
277      * can be ignored.                                                        \
278      */                                                                       \
279                                                                               \
280     switch( i_vscale )                                                        \
281     {                                                                         \
282     case -1:                             /* vertical scaling factor is < 1 */ \
283         while( (i_scale_count -= p_vout->output.i_height) > 0 )               \
284         {                                                                     \
285             /* Height reduction: skip next source line */                     \
286             p_y += p_vout->render.i_width;                                    \
287             i_y++;                                                            \
288             if( (CHROMA == 420) || (CHROMA == 422) )                          \
289             {                                                                 \
290                 if( i_y & 0x1 )                                               \
291                 {                                                             \
292                     p_u += i_chroma_width;                                    \
293                     p_v += i_chroma_width;                                    \
294                 }                                                             \
295             }                                                                 \
296             else if( CHROMA == 444 )                                          \
297             {                                                                 \
298                 p_u += p_vout->render.i_width;                                \
299                 p_v += p_vout->render.i_width;                                \
300             }                                                                 \
301         }                                                                     \
302         i_scale_count += p_vout->render.i_height;                             \
303         break;                                                                \
304     case 1:                              /* vertical scaling factor is > 1 */ \
305         while( (i_scale_count -= p_vout->render.i_height) > 0 )               \
306         {                                                                     \
307             p_y -= p_vout->render.i_width;                                    \
308             p_u -= i_chroma_width;                                            \
309             p_v -= i_chroma_width;                                            \
310             SCALE_WIDTH_DITHER( CHROMA );                                     \
311         }                                                                     \
312         i_scale_count += p_vout->output.i_height;                             \
313         break;                                                                \
314     }                                                                         \
315