]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb16.c
* toolbox: use the "Output_Dir" property when generating the msvc project files so...
[vlc] / modules / video_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.5 2003/08/29 18:58:05 fenrir 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 <string.h>                                            /* strerror() */
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "i420_rgb.h"
34 #if defined (MODULE_NAME_IS_i420_rgb)
35 #   include "i420_rgb_c.h"
36 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
37 #   include "i420_rgb_mmx.h"
38 #endif
39
40 static void SetOffset( int, int, int, int, vlc_bool_t *, int *, int * );
41
42 #if defined (MODULE_NAME_IS_i420_rgb)
43 /*****************************************************************************
44  * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp with dithering
45  *****************************************************************************
46  * Horizontal alignment needed:
47  *  - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
48  *  - output: 1 pixel (2 bytes), margins allowed
49  * Vertical alignment needed:
50  *  - input: 2 lines (2 Y lines, 1 U/V line)
51  *  - output: 1 line
52  *****************************************************************************/
53 void E_(I420_RGB16_dithering)( vout_thread_t *p_vout, picture_t *p_src,
54                                                       picture_t *p_dest )
55 {
56     /* We got this one from the old arguments */
57     uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
58     uint8_t  *p_y   = p_src->Y_PIXELS;
59     uint8_t  *p_u   = p_src->U_PIXELS;
60     uint8_t  *p_v   = p_src->V_PIXELS;
61
62     vlc_bool_t   b_hscale;                        /* horizontal scaling type */
63     int          i_vscale;                          /* vertical scaling type */
64     unsigned int i_x, i_y;                /* horizontal and vertical indexes */
65     unsigned int i_real_y;                                          /* y % 4 */
66
67     int         i_right_margin;
68     int         i_rewind;
69     int         i_scale_count;                       /* scale modulo counter */
70     int         i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
71     uint16_t *  p_pic_start;       /* beginning of the current line for copy */
72     int         i_uval, i_vval;                           /* U and V samples */
73     int         i_red, i_green, i_blue;          /* U and V modified samples */
74     uint16_t *  p_yuv = p_vout->chroma.p_sys->p_rgb16;
75     uint16_t *  p_ybase;                     /* Y dependant conversion table */
76
77     /* Conversion buffer pointer */
78     uint16_t *  p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
79     uint16_t *  p_buffer;
80
81     /* Offset array pointer */
82     int *       p_offset_start = p_vout->chroma.p_sys->p_offset;
83     int *       p_offset;
84
85     /* The dithering matrices */
86     int dither10[4] = {  0x0,  0x8,  0x2,  0xa };
87     int dither11[4] = {  0xc,  0x4,  0xe,  0x6 };
88     int dither12[4] = {  0x3,  0xb,  0x1,  0x9 };
89     int dither13[4] = {  0xf,  0x7,  0xd,  0x5 };
90
91     for(i_x = 0; i_x < 4; i_x++)
92     {
93         dither10[i_x] = dither10[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
94         dither11[i_x] = dither11[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
95         dither12[i_x] = dither12[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
96         dither13[i_x] = dither13[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
97     }
98
99     i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
100
101     if( p_vout->render.i_width & 7 )
102     {
103         i_rewind = 8 - ( p_vout->render.i_width & 7 );
104     }
105     else
106     {
107         i_rewind = 0;
108     }
109
110     /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
111      * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
112      * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
113     SetOffset( p_vout->render.i_width, p_vout->render.i_height,
114                p_vout->output.i_width, p_vout->output.i_height,
115                &b_hscale, &i_vscale, p_offset_start );
116
117     /*
118      * Perform conversion
119      */
120     i_scale_count = ( i_vscale == 1 ) ?
121                     p_vout->output.i_height : p_vout->render.i_height;
122     for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
123     {
124         i_real_y = i_y & 0x3;
125         p_pic_start = p_pic;
126         p_buffer = b_hscale ? p_buffer_start : p_pic;
127
128         for ( i_x = p_vout->render.i_width / 8; i_x--; )
129         {
130             int *p_dither = dither10;
131             CONVERT_YUV_PIXEL_DITHER(2);
132             p_dither = dither11;
133             CONVERT_Y_PIXEL_DITHER(2);
134             p_dither = dither12;
135             CONVERT_YUV_PIXEL_DITHER(2);
136             p_dither = dither13;
137             CONVERT_Y_PIXEL_DITHER(2);
138             p_dither = dither10;
139             CONVERT_YUV_PIXEL_DITHER(2);
140             p_dither = dither11;
141             CONVERT_Y_PIXEL_DITHER(2);
142             p_dither = dither12;
143             CONVERT_YUV_PIXEL_DITHER(2);
144             p_dither = dither13;
145             CONVERT_Y_PIXEL_DITHER(2);
146         }
147
148         /* Here we do some unaligned reads and duplicate conversions, but
149          * at least we have all the pixels */
150         if( i_rewind )
151         {
152             int *p_dither = dither10;
153             p_y -= i_rewind;
154             p_u -= i_rewind >> 1;
155             p_v -= i_rewind >> 1;
156             p_buffer -= i_rewind;
157             CONVERT_YUV_PIXEL_DITHER(2);
158             p_dither = dither11;
159             CONVERT_Y_PIXEL_DITHER(2);
160             p_dither = dither12;
161             CONVERT_YUV_PIXEL_DITHER(2);
162             p_dither = dither13;
163             CONVERT_Y_PIXEL_DITHER(2);
164             p_dither = dither10;
165             CONVERT_YUV_PIXEL_DITHER(2);
166             p_dither = dither11;
167             CONVERT_Y_PIXEL_DITHER(2);
168             p_dither = dither12;
169             CONVERT_YUV_PIXEL_DITHER(2);
170             p_dither = dither13;
171             CONVERT_Y_PIXEL_DITHER(2);
172         }
173         SCALE_WIDTH;
174         SCALE_HEIGHT( 420, 2 );
175     }
176 }
177 #endif
178
179 /*****************************************************************************
180  * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
181  *****************************************************************************
182  * Horizontal alignment needed:
183  *  - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
184  *  - output: 1 pixel (2 bytes), margins allowed
185  * Vertical alignment needed:
186  *  - input: 2 lines (2 Y lines, 1 U/V line)
187  *  - output: 1 line
188  *****************************************************************************/
189 void E_(I420_RGB16)( vout_thread_t *p_vout, picture_t *p_src,
190                                             picture_t *p_dest )
191 {
192     /* We got this one from the old arguments */
193     uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
194     uint8_t  *p_y   = p_src->Y_PIXELS;
195     uint8_t  *p_u   = p_src->U_PIXELS;
196     uint8_t  *p_v   = p_src->V_PIXELS;
197
198     vlc_bool_t  b_hscale;                         /* horizontal scaling type */
199     unsigned int i_vscale;                          /* vertical scaling type */
200     unsigned int i_x, i_y;                /* horizontal and vertical indexes */
201
202     int         i_right_margin;
203     int         i_rewind;
204     int         i_scale_count;                       /* scale modulo counter */
205     int         i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
206     uint16_t *  p_pic_start;       /* beginning of the current line for copy */
207 #if defined (MODULE_NAME_IS_i420_rgb)
208     int         i_uval, i_vval;                           /* U and V samples */
209     int         i_red, i_green, i_blue;          /* U and V modified samples */
210     uint16_t *  p_yuv = p_vout->chroma.p_sys->p_rgb16;
211     uint16_t *  p_ybase;                     /* Y dependant conversion table */
212 #endif
213
214     /* Conversion buffer pointer */
215     uint16_t *  p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
216     uint16_t *  p_buffer;
217
218     /* Offset array pointer */
219     int *       p_offset_start = p_vout->chroma.p_sys->p_offset;
220     int *       p_offset;
221
222     i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
223
224     if( p_vout->render.i_width & 7 )
225     {
226         i_rewind = 8 - ( p_vout->render.i_width & 7 );
227     }
228     else
229     {
230         i_rewind = 0;
231     }
232
233     /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
234      * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
235      * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
236     SetOffset( p_vout->render.i_width, p_vout->render.i_height,
237                p_vout->output.i_width, p_vout->output.i_height,
238                &b_hscale, &i_vscale, p_offset_start );
239
240     /*
241      * Perform conversion
242      */
243     i_scale_count = ( i_vscale == 1 ) ?
244                     p_vout->output.i_height : p_vout->render.i_height;
245     for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
246     {
247         p_pic_start = p_pic;
248         p_buffer = b_hscale ? p_buffer_start : p_pic;
249
250 #if defined (MODULE_NAME_IS_i420_rgb)
251         for ( i_x = p_vout->render.i_width / 8; i_x--; )
252         {
253             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
254             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
255             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
256             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
257         }
258 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
259         if( p_vout->output.i_rmask == 0x7c00 )
260         {
261             /* 15bpp 5/5/5 */
262             for ( i_x = p_vout->render.i_width / 8; i_x--; )
263             {
264                 __asm__( MMX_INIT_16
265                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
266
267                 __asm__( ".align 8"
268                          MMX_YUV_MUL
269                          MMX_YUV_ADD
270                          MMX_UNPACK_15
271                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
272
273                 p_y += 8;
274                 p_u += 4;
275                 p_v += 4;
276                 p_buffer += 8;
277             }
278         }
279         else
280         {
281             /* 16bpp 5/6/5 */
282             for ( i_x = p_vout->render.i_width / 8; i_x--; )
283             {
284                 __asm__( MMX_INIT_16
285                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
286
287                 __asm__( ".align 8"
288                          MMX_YUV_MUL
289                          MMX_YUV_ADD
290                          MMX_UNPACK_16
291                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
292
293                 p_y += 8;
294                 p_u += 4;
295                 p_v += 4;
296                 p_buffer += 8;
297             }
298         }
299 #endif
300
301         /* Here we do some unaligned reads and duplicate conversions, but
302          * at least we have all the pixels */
303         if( i_rewind )
304         {
305             p_y -= i_rewind;
306             p_u -= i_rewind >> 1;
307             p_v -= i_rewind >> 1;
308             p_buffer -= i_rewind;
309 #if defined (MODULE_NAME_IS_i420_rgb)
310             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
311             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
312             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
313             CONVERT_YUV_PIXEL(2);  CONVERT_Y_PIXEL(2);
314 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
315             __asm__( MMX_INIT_16
316                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
317
318             if( p_vout->output.i_rmask == 0x7c00 )
319             {
320                 /* 15bpp 5/5/5 */
321                 __asm__( ".align 8"
322                          MMX_YUV_MUL
323                          MMX_YUV_ADD
324                          MMX_UNPACK_15
325                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
326             }
327             else
328             {
329                 /* 16bpp 5/6/5 */
330                 __asm__( ".align 8"
331                          MMX_YUV_MUL
332                          MMX_YUV_ADD
333                          MMX_UNPACK_16
334                          : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
335             }
336
337             p_y += 8;
338             p_u += 4;
339             p_v += 4;
340             p_buffer += 8;
341 #endif
342         }
343         SCALE_WIDTH;
344         SCALE_HEIGHT( 420, 2 );
345     }
346 }
347
348 /*****************************************************************************
349  * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
350  *****************************************************************************
351  * Horizontal alignment needed:
352  *  - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
353  *  - output: 1 pixel (2 bytes), margins allowed
354  * Vertical alignment needed:
355  *  - input: 2 lines (2 Y lines, 1 U/V line)
356  *  - output: 1 line
357  *****************************************************************************/
358 void E_(I420_RGB32)( vout_thread_t *p_vout, picture_t *p_src,
359                                             picture_t *p_dest )
360 {
361     /* We got this one from the old arguments */
362     uint32_t *p_pic = (uint32_t*)p_dest->p->p_pixels;
363     uint8_t  *p_y   = p_src->Y_PIXELS;
364     uint8_t  *p_u   = p_src->U_PIXELS;
365     uint8_t  *p_v   = p_src->V_PIXELS;
366
367     vlc_bool_t  b_hscale;                         /* horizontal scaling type */
368     unsigned int i_vscale;                          /* vertical scaling type */
369     unsigned int i_x, i_y;                /* horizontal and vertical indexes */
370
371     int         i_right_margin;
372     int         i_rewind;
373     int         i_scale_count;                       /* scale modulo counter */
374     int         i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
375     uint32_t *  p_pic_start;       /* beginning of the current line for copy */
376 #if defined (MODULE_NAME_IS_i420_rgb)
377     int         i_uval, i_vval;                           /* U and V samples */
378     int         i_red, i_green, i_blue;          /* U and V modified samples */
379     uint32_t *  p_yuv = p_vout->chroma.p_sys->p_rgb32;
380     uint32_t *  p_ybase;                     /* Y dependant conversion table */
381 #endif
382
383     /* Conversion buffer pointer */
384     uint32_t *  p_buffer_start = (uint32_t*)p_vout->chroma.p_sys->p_buffer;
385     uint32_t *  p_buffer;
386
387     /* Offset array pointer */
388     int *       p_offset_start = p_vout->chroma.p_sys->p_offset;
389     int *       p_offset;
390
391     i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
392
393     if( p_vout->render.i_width & 7 )
394     {
395         i_rewind = 8 - ( p_vout->render.i_width & 7 );
396     }
397     else
398     {
399         i_rewind = 0;
400     }
401
402     /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
403      * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
404      * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
405     SetOffset( p_vout->render.i_width, p_vout->render.i_height,
406                p_vout->output.i_width, p_vout->output.i_height,
407                &b_hscale, &i_vscale, p_offset_start );
408
409     /*
410      * Perform conversion
411      */
412     i_scale_count = ( i_vscale == 1 ) ?
413                     p_vout->output.i_height : p_vout->render.i_height;
414     for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
415     {
416         p_pic_start = p_pic;
417         p_buffer = b_hscale ? p_buffer_start : p_pic;
418
419         for ( i_x = p_vout->render.i_width / 8; i_x--; )
420         {
421 #if defined (MODULE_NAME_IS_i420_rgb)
422             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
423             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
424             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
425             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
426 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
427             __asm__( MMX_INIT_32
428                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
429
430             __asm__( ".align 8"
431                      MMX_YUV_MUL
432                      MMX_YUV_ADD
433                      MMX_UNPACK_32
434                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
435
436             p_y += 8;
437             p_u += 4;
438             p_v += 4;
439             p_buffer += 8;
440 #endif
441         }
442
443         /* Here we do some unaligned reads and duplicate conversions, but
444          * at least we have all the pixels */
445         if( i_rewind )
446         {
447             p_y -= i_rewind;
448             p_u -= i_rewind >> 1;
449             p_v -= i_rewind >> 1;
450             p_buffer -= i_rewind;
451 #if defined (MODULE_NAME_IS_i420_rgb)
452             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
453             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
454             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
455             CONVERT_YUV_PIXEL(4);  CONVERT_Y_PIXEL(4);
456 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
457             __asm__( MMX_INIT_32
458                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
459
460             __asm__( ".align 8"
461                      MMX_YUV_MUL
462                      MMX_YUV_ADD
463                      MMX_UNPACK_32
464                      : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
465
466             p_y += 8;
467             p_u += 4;
468             p_v += 4;
469             p_buffer += 8;
470 #endif
471         }
472         SCALE_WIDTH;
473         SCALE_HEIGHT( 420, 4 );
474     }
475 }
476
477 /* Following functions are local */
478
479 /*****************************************************************************
480  * SetOffset: build offset array for conversion functions
481  *****************************************************************************
482  * This function will build an offset array used in later conversion functions.
483  * It will also set horizontal and vertical scaling indicators.
484  *****************************************************************************/
485 static void SetOffset( int i_width, int i_height, int i_pic_width,
486                        int i_pic_height, vlc_bool_t *pb_hscale,
487                        int *pi_vscale, int *p_offset )
488 {
489     int i_x;                                    /* x position in destination */
490     int i_scale_count;                                     /* modulo counter */
491
492     /*
493      * Prepare horizontal offset array
494      */
495     if( i_pic_width - i_width == 0 )
496     {
497         /* No horizontal scaling: YUV conversion is done directly to picture */
498         *pb_hscale = 0;
499     }
500     else if( i_pic_width - i_width > 0 )
501     {
502         /* Prepare scaling array for horizontal extension */
503         *pb_hscale = 1;
504         i_scale_count = i_pic_width;
505         for( i_x = i_width; i_x--; )
506         {
507             while( (i_scale_count -= i_width) > 0 )
508             {
509                 *p_offset++ = 0;
510             }
511             *p_offset++ = 1;
512             i_scale_count += i_pic_width;
513         }
514     }
515     else /* if( i_pic_width - i_width < 0 ) */
516     {
517         /* Prepare scaling array for horizontal reduction */
518         *pb_hscale = 1;
519         i_scale_count = i_width;
520         for( i_x = i_pic_width; i_x--; )
521         {
522             *p_offset = 1;
523             while( (i_scale_count -= i_pic_width) > 0 )
524             {
525                 *p_offset += 1;
526             }
527             p_offset++;
528             i_scale_count += i_width;
529         }
530     }
531
532     /*
533      * Set vertical scaling indicator
534      */
535     if( i_pic_height - i_height == 0 )
536     {
537         *pi_vscale = 0;
538     }
539     else if( i_pic_height - i_height > 0 )
540     {
541         *pi_vscale = 1;
542     }
543     else /* if( i_pic_height - i_height < 0 ) */
544     {
545         *pi_vscale = -1;
546     }
547 }
548