]> git.sesse.net Git - vlc/blob - plugins/chroma/i420_rgb16.c
* ./BUGS: added a list of known bugs. Please add your findings!
[vlc] / plugins / chroma / i420_rgb16.c
1 /*****************************************************************************
2  * i420_rgb16.c : YUV to bitmap RGB conversion module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: i420_rgb16.c,v 1.1 2002/01/04 14:01:34 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  * Preamble
26  *****************************************************************************/
27 #include <math.h>                                            /* exp(), pow() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdlib.h>                                      /* malloc(), free() */
31
32 #include <videolan/vlc.h>
33
34 #include "video.h"
35 #include "video_output.h"
36
37 #include "i420_rgb.h"
38 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
39 #   include "i420_rgb_c.h"
40 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
41 #   include "i420_rgb_mmx.h"
42 #endif
43
44 static void SetOffset( int, int, int, int, boolean_t *, int *, int * );
45
46 /*****************************************************************************
47  * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
48  *****************************************************************************
49  * Horizontal alignment needed:
50  *  - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
51  *  - output: 1 pixel (2 bytes), margins allowed
52  * Vertical alignment needed:
53  *  - input: 2 lines (2 Y lines, 1 U/V line)
54  *  - output: 1 line
55  *****************************************************************************/
56 void _M( I420_RGB16 )( vout_thread_t *p_vout, picture_t *p_src,
57                                               picture_t *p_dest )
58 {
59     /* We got this one from the old arguments */
60     u16 *p_pic = (u16*)p_dest->p->p_pixels;
61     u8  *p_y   = p_src->Y_PIXELS;
62     u8  *p_u   = p_src->U_PIXELS;
63     u8  *p_v   = p_src->V_PIXELS;
64
65     boolean_t   b_hscale;                         /* horizontal scaling type */
66     int         i_vscale;                           /* vertical scaling type */
67     int         i_x, i_y;                 /* horizontal and vertical indexes */
68     int         i_right_margin;
69     int         i_rewind;
70     int         i_scale_count;                       /* scale modulo counter */
71     int         i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
72     u16 *       p_pic_start;       /* beginning of the current line for copy */
73
74     /* Conversion buffer pointer */
75     u16 *       p_buffer_start = (u16*)p_vout->chroma.p_sys->p_buffer;
76     u16 *       p_buffer;
77
78     /* Offset array pointer */
79     int *       p_offset_start = p_vout->chroma.p_sys->p_offset;
80     int *       p_offset;
81
82     if( p_dest->p->b_margin )
83     {
84         i_right_margin = (p_dest->p->i_pitch - p_dest->p->i_visible_bytes) / 2;
85     }
86     else
87     {
88         i_right_margin = 0;
89     }
90
91     if( p_vout->render.i_width & 7 )
92     {
93         i_rewind = 8 - ( p_vout->render.i_width & 7 );
94     }
95     else
96     {
97         i_rewind = 0;
98     }
99
100     /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
101      * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
102      * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
103     SetOffset( p_vout->render.i_width, p_vout->render.i_height,
104                p_vout->output.i_width, p_vout->output.i_height,
105                &b_hscale, &i_vscale, p_offset_start );
106
107 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
108     intf_ErrMsg( "vout error: I420_RGB16 unimplemented, "
109                  "please harass sam@zoy.org" );
110 #endif
111
112     /*
113      * Perform conversion
114      */
115     i_scale_count = ( i_vscale == 1 ) ?
116                     p_vout->output.i_height : p_vout->render.i_height;
117     for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
118     {
119         p_pic_start = p_pic;
120         p_buffer = b_hscale ? p_buffer_start : p_pic;
121
122         for ( i_x = p_vout->render.i_width / 8; i_x--; )
123         {
124 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
125             /* FIXME: TODO */
126 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
127             __asm__( MMX_INIT_16
128                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
129
130             __asm__( ".align 8"
131                      MMX_YUV_MUL
132                      MMX_YUV_ADD
133                      MMX_UNPACK_16
134                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
135
136             p_y += 8;
137             p_u += 4;
138             p_v += 4;
139             p_buffer += 8;
140 #endif
141         }
142
143         /* Here we do some unaligned reads and duplicate conversions, but
144          * at least we have all the pixels */
145         if( i_rewind )
146         {
147             p_y -= i_rewind;
148             p_u -= i_rewind >> 1;
149             p_v -= i_rewind >> 1;
150             p_buffer -= i_rewind;
151 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
152             /* FIXME: TODO */
153 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
154             __asm__( MMX_INIT_16
155                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
156
157             __asm__( ".align 8"
158                      MMX_YUV_MUL
159                      MMX_YUV_ADD
160                      MMX_UNPACK_16
161                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
162
163             p_y += 8;
164             p_u += 4;
165             p_v += 4;
166             p_buffer += 8;
167 #endif
168         }
169         SCALE_WIDTH;
170         SCALE_HEIGHT( 420, 2 );
171     }
172 }
173
174 /*****************************************************************************
175  * I420_RGB24: color YUV 4:2:0 to RGB 24 bpp
176  *****************************************************************************/
177 void _M( I420_RGB24 )( vout_thread_t *p_vout, picture_t *p_src,
178                                               picture_t *p_dest )
179 {
180     intf_ErrMsg( "vout error: I420_RGB24 unimplemented, "
181                  "please harass sam@zoy.org" );
182 }
183
184 /*****************************************************************************
185  * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
186  *****************************************************************************/
187 void _M( I420_RGB32 )( vout_thread_t *p_vout, picture_t *p_src,
188                                               picture_t *p_dest )
189 {
190     intf_ErrMsg( "vout error: I420_RGB32 unimplemented, "
191                  "please harass sam@zoy.org" );
192 }
193
194 /* Following functions are local */
195
196 /*****************************************************************************
197  * SetOffset: build offset array for conversion functions
198  *****************************************************************************
199  * This function will build an offset array used in later conversion functions.
200  * It will also set horizontal and vertical scaling indicators.
201  *****************************************************************************/
202 static void SetOffset( int i_width, int i_height, int i_pic_width,
203                        int i_pic_height, boolean_t *pb_hscale,
204                        int *pi_vscale, int *p_offset )
205 {
206     int i_x;                                    /* x position in destination */
207     int i_scale_count;                                     /* modulo counter */
208
209     /*
210      * Prepare horizontal offset array
211      */
212     if( i_pic_width - i_width == 0 )
213     {
214         /* No horizontal scaling: YUV conversion is done directly to picture */
215         *pb_hscale = 0;
216     }
217     else if( i_pic_width - i_width > 0 )
218     {
219         /* Prepare scaling array for horizontal extension */
220         *pb_hscale = 1;
221         i_scale_count = i_pic_width;
222         for( i_x = i_width; i_x--; )
223         {
224             while( (i_scale_count -= i_width) > 0 )
225             {
226                 *p_offset++ = 0;
227             }
228             *p_offset++ = 1;
229             i_scale_count += i_pic_width;
230         }
231     }
232     else /* if( i_pic_width - i_width < 0 ) */
233     {
234         /* Prepare scaling array for horizontal reduction */
235         *pb_hscale = 1;
236         i_scale_count = i_width;
237         for( i_x = i_pic_width; i_x--; )
238         {
239             *p_offset = 1;
240             while( (i_scale_count -= i_pic_width) > 0 )
241             {
242                 *p_offset += 1;
243             }
244             p_offset++;
245             i_scale_count += i_width;
246         }
247      }
248
249     /*
250      * Set vertical scaling indicator
251      */
252     if( i_pic_height - i_height == 0 )
253     {
254         *pi_vscale = 0;
255     }
256     else if( i_pic_height - i_height > 0 )
257     {
258         *pi_vscale = 1;
259     }
260     else /* if( i_pic_height - i_height < 0 ) */
261     {
262         *pi_vscale = -1;
263     }
264 }
265