]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / video_filter / blend.c
1 /*****************************************************************************
2  * blend.c: alpha blend 2 pictures together
3  *****************************************************************************
4  * Copyright (C) 2003-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea @t videolan dot 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenFilter ( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("Video pictures blending") )
46     set_capability( "video blending", 100 )
47     set_callbacks( OpenFilter, CloseFilter )
48 vlc_module_end ()
49
50
51 /*****************************************************************************
52  * filter_sys_t : filter descriptor
53  *****************************************************************************/
54 struct filter_sys_t
55 {
56     int i_dummy;
57 };
58
59 #define FCC_YUVA VLC_FOURCC('Y','U','V','A')
60 #define FCC_YUVP VLC_FOURCC('Y','U','V','P')
61 #define FCC_RGBA VLC_FOURCC('R','G','B','A')
62
63 #define FCC_I420 VLC_FOURCC('I','4','2','0')
64 #define FCC_YV12 VLC_FOURCC('Y','V','1','2')
65 #define FCC_YUY2 VLC_FOURCC('Y','U','Y','2')
66 #define FCC_UYVY VLC_FOURCC('U','Y','V','Y')
67 #define FCC_YVYU VLC_FOURCC('Y','V','Y','U')
68 #define FCC_RV15 VLC_FOURCC('R','V','1','5')
69 #define FCC_RV16 VLC_FOURCC('R','V','1','6')
70 #define FCC_RV24 VLC_FOURCC('R','V','2','4')
71 #define FCC_RV32 VLC_FOURCC('R','V','3','2')
72
73 /****************************************************************************
74  * Local prototypes
75  ****************************************************************************/
76 static void Blend( filter_t *, picture_t *, picture_t *,
77                    int, int, int );
78
79 /* YUVA */
80 static void BlendYUVAI420( filter_t *, picture_t *, picture_t *,
81                            int, int, int, int, int );
82 static void BlendYUVARV16( filter_t *, picture_t *, picture_t *,
83                            int, int, int, int, int );
84 static void BlendYUVARV24( filter_t *, picture_t *, picture_t *,
85                            int, int, int, int, int );
86 static void BlendYUVAYUVPacked( filter_t *, picture_t *, picture_t *,
87                                 int, int, int, int, int );
88
89 /* I420, YV12 */
90 static void BlendI420I420( filter_t *, picture_t *, picture_t *,
91                            int, int, int, int, int );
92 static void BlendI420I420_no_alpha(
93                            filter_t *, picture_t *, picture_t *,
94                            int, int, int, int );
95 static void BlendI420R16( filter_t *, picture_t *, picture_t *,
96                            int, int, int, int, int );
97 static void BlendI420R24( filter_t *, picture_t *, picture_t *,
98                           int, int, int, int, int );
99 static void BlendI420YUVPacked( filter_t *, picture_t *,
100                                 picture_t *, int, int, int, int, int );
101
102 /* YUVP */
103 static void BlendPalI420( filter_t *, picture_t *, picture_t *,
104                           int, int, int, int, int );
105 static void BlendPalYUVPacked( filter_t *, picture_t *, picture_t *,
106                                int, int, int, int, int );
107 static void BlendPalRV( filter_t *, picture_t *, picture_t *,
108                         int, int, int, int, int );
109
110 /* RGBA */
111 static void BlendRGBAI420( filter_t *, picture_t *, picture_t *,
112                            int, int, int, int, int );
113 static void BlendRGBAYUVPacked( filter_t *, picture_t *,
114                                 picture_t *, int, int, int, int, int );
115 static void BlendRGBAR16( filter_t *, picture_t *, picture_t *,
116                           int, int, int, int, int );
117 static void BlendRGBAR24( filter_t *, picture_t *, picture_t *,
118                           int, int, int, int, int );
119
120 /*****************************************************************************
121  * OpenFilter: probe the filter and return score
122  *****************************************************************************/
123 static int OpenFilter( vlc_object_t *p_this )
124 {
125     filter_t *p_filter = (filter_t*)p_this;
126     filter_sys_t *p_sys;
127
128     /* Check if we can handle that format.
129      * We could try to use a chroma filter if we can't. */
130     int in_chroma = p_filter->fmt_in.video.i_chroma;
131     int out_chroma = p_filter->fmt_out.video.i_chroma;
132     if( ( in_chroma  != FCC_YUVA && in_chroma  != FCC_I420 &&
133           in_chroma  != FCC_YV12 && in_chroma  != FCC_YUVP &&
134           in_chroma  != FCC_RGBA ) ||
135         ( out_chroma != FCC_I420 && out_chroma != FCC_YUY2 &&
136           out_chroma != FCC_YV12 && out_chroma != FCC_UYVY &&
137           out_chroma != FCC_YVYU && out_chroma != FCC_RV15 &&
138           out_chroma != FCC_YVYU && out_chroma != FCC_RV16 &&
139           out_chroma != FCC_RV24 && out_chroma != FCC_RV32 ) )
140     {
141         return VLC_EGENERIC;
142     }
143
144     /* Allocate the memory needed to store the decoder's structure */
145     p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t));
146     if( !p_sys )
147         return VLC_ENOMEM;
148
149     /* Misc init */
150     p_filter->pf_video_blend = Blend;
151
152     msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
153              (char *)&p_filter->fmt_in.video.i_chroma,
154              (char *)&p_filter->fmt_out.video.i_chroma );
155
156     return VLC_SUCCESS;
157 }
158
159 /*****************************************************************************
160  * CloseFilter: clean up the filter
161  *****************************************************************************/
162 static void CloseFilter( vlc_object_t *p_this )
163 {
164     filter_t *p_filter = (filter_t*)p_this;
165     filter_sys_t *p_sys = p_filter->p_sys;
166
167     free( p_sys );
168 }
169
170 /****************************************************************************
171  * Blend: the whole thing
172  ****************************************************************************
173  * This function is called just after the thread is launched.
174  ****************************************************************************/
175 typedef void (*BlendFunction)( filter_t *,
176                        picture_t *, picture_t *,
177                        int , int , int , int , int );
178
179 #define FCC_PLANAR_420 { FCC_I420, FCC_YV12, 0 }
180 #define FCC_PACKED_422 { FCC_YUY2, FCC_UYVY, FCC_YVYU, 0 }
181 #define FCC_RGB_16 { FCC_RV15, FCC_RV16, 0 }
182 #define FCC_RGB_24 { FCC_RV24, FCC_RV32, 0 }
183
184 #define BLEND_CFG( fccSrc, fctPlanar, fctPacked, fctRgb16, fctRgb24  ) \
185     { .src = fccSrc, .p_dst = FCC_PLANAR_420, .pf_blend = fctPlanar }, \
186     { .src = fccSrc, .p_dst = FCC_PACKED_422, .pf_blend = fctPacked }, \
187     { .src = fccSrc, .p_dst = FCC_RGB_16,     .pf_blend = fctRgb16  }, \
188     { .src = fccSrc, .p_dst = FCC_RGB_24,     .pf_blend = fctRgb24  }
189
190 static const struct
191 {
192     vlc_fourcc_t src;
193     vlc_fourcc_t p_dst[16];
194     BlendFunction pf_blend;
195 } p_blend_cfg[] = {
196
197     BLEND_CFG( FCC_YUVA, BlendYUVAI420, BlendYUVAYUVPacked, BlendYUVARV16, BlendYUVARV24 ),
198
199     BLEND_CFG( FCC_YUVP, BlendPalI420, BlendPalYUVPacked, BlendPalRV, BlendPalRV ),
200
201     BLEND_CFG( FCC_RGBA, BlendRGBAI420, BlendRGBAYUVPacked, BlendRGBAR16, BlendRGBAR24 ),
202
203     BLEND_CFG( FCC_I420, BlendI420I420, BlendI420YUVPacked, BlendI420R16, BlendI420R24 ),
204
205     BLEND_CFG( FCC_YV12, BlendI420I420, BlendI420YUVPacked, BlendI420R16, BlendI420R24 ),
206
207     { 0, {0,}, NULL }
208 };
209
210 static void Blend( filter_t *p_filter,
211                    picture_t *p_dst, picture_t *p_src,
212                    int i_x_offset, int i_y_offset, int i_alpha )
213 {
214     int i_width, i_height;
215
216     if( i_alpha == 0 )
217         return;
218
219     i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
220                     (int)p_filter->fmt_in.video.i_visible_width);
221
222     i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
223                      (int)p_filter->fmt_in.video.i_visible_height);
224
225     if( i_width <= 0 || i_height <= 0 )
226         return;
227
228     video_format_FixRgb( &p_filter->fmt_out.video );
229     video_format_FixRgb( &p_filter->fmt_in.video );
230
231 #if 0
232     msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s\n",
233              (char *)&p_filter->fmt_in.video.i_chroma,
234              (char *)&p_filter->fmt_out.video.i_chroma );
235 #endif
236
237     for( int i = 0; p_blend_cfg[i].src != 0; i++ )
238     {
239         if( p_blend_cfg[i].src != p_filter->fmt_in.video.i_chroma )
240             continue;
241         for( int j = 0; p_blend_cfg[i].p_dst[j] != 0; j++ )
242         {
243             if( p_blend_cfg[i].p_dst[j] != p_filter->fmt_out.video.i_chroma )
244                 continue;
245
246             p_blend_cfg[i].pf_blend( p_filter, p_dst, p_src,
247                                      i_x_offset, i_y_offset,
248                                      i_width, i_height, i_alpha );
249             return;
250         }
251     }
252
253     msg_Dbg( p_filter, "no matching alpha blending routine "
254              "(chroma: %4.4s -> %4.4s)",
255              (char *)&p_filter->fmt_in.video.i_chroma,
256              (char *)&p_filter->fmt_out.video.i_chroma );
257 }
258
259 /***********************************************************************
260  * Utils
261  ***********************************************************************/
262 static inline uint8_t vlc_uint8( int v )
263 {
264     if( v > 255 )
265         return 255;
266     else if( v < 0 )
267         return 0;
268     return v;
269 }
270
271 #define MAX_TRANS 255
272 #define TRANS_BITS  8
273
274 static inline int vlc_blend( int v1, int v2, int a )
275 {
276     /* TODO bench if the tests really increase speed */
277     if( a == 0 )
278         return v2;
279     else if( a == MAX_TRANS )
280         return v1;
281     return ( v1 * a + v2 * (MAX_TRANS - a ) ) >> TRANS_BITS;
282 }
283
284 static inline int vlc_alpha( int t, int a )
285 {
286     if( a == 255 )
287         return t;
288     return (t * a) / 255;
289 }
290
291 static inline void yuv_to_rgb( int *r, int *g, int *b,
292                                uint8_t y1, uint8_t u1, uint8_t v1 )
293 {
294     /* macros used for YUV pixel conversions */
295 #   define SCALEBITS 10
296 #   define ONE_HALF  (1 << (SCALEBITS - 1))
297 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
298
299     int y, cb, cr, r_add, g_add, b_add;
300
301     cb = u1 - 128;
302     cr = v1 - 128;
303     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
304     g_add = - FIX(0.34414*255.0/224.0) * cb
305             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
306     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
307     y = (y1 - 16) * FIX(255.0/219.0);
308     *r = vlc_uint8( (y + r_add) >> SCALEBITS );
309     *g = vlc_uint8( (y + g_add) >> SCALEBITS );
310     *b = vlc_uint8( (y + b_add) >> SCALEBITS );
311 #undef FIX
312 #undef ONE_HALF
313 #undef SCALEBITS
314 }
315
316 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
317                                int r, int g, int b )
318 {
319     *y = ( ( (  66 * r + 129 * g +  25 * b + 128 ) >> 8 ) + 16 );
320     *u =   ( ( -38 * r -  74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
321     *v =   ( ( 112 * r -  94 * g -  18 * b + 128 ) >> 8 ) + 128 ;
322 }
323
324 static uint8_t *vlc_plane_start( int *pi_pitch,
325                                  picture_t *p_picture,
326                                  int i_plane,
327                                  int i_x_offset, int i_y_offset,
328                                  const video_format_t *p_fmt,
329                                  int r )
330 {
331     const int i_pitch = p_picture->p[i_plane].i_pitch;
332     uint8_t *p_pixels = p_picture->p[i_plane].p_pixels;
333
334     const int i_dx = ( i_x_offset + p_fmt->i_x_offset ) / r;
335     const int i_dy = ( i_y_offset + p_fmt->i_y_offset ) / r;
336
337     if( pi_pitch )
338         *pi_pitch = i_pitch;
339     return &p_pixels[ i_dy * i_pitch + i_dx ];
340 }
341
342 static void vlc_yuv_packed_index( int *pi_y, int *pi_u, int *pi_v, vlc_fourcc_t i_chroma )
343 {
344     static const struct {
345         vlc_fourcc_t chroma;
346         int y, u ,v;
347     } p_index[] = {
348         { FCC_YUY2, 0, 1, 3 },
349         { FCC_UYVY, 1, 0, 2 },
350         { FCC_YVYU, 0, 3, 1 },
351         { 0, 0, 0, 0 }
352     };
353     int i;
354
355     for( i = 0; p_index[i].chroma != 0; i++ )
356     {
357         if( p_index[i].chroma == i_chroma )
358             break;
359     }
360     *pi_y = p_index[i].y;
361     *pi_u = p_index[i].u;
362     *pi_v = p_index[i].v;
363 }
364
365 static void vlc_blend_packed( uint8_t *p_dst,
366                               int i_offset0, int i_offset1, int i_offset2,
367                               int c0, int c1, int c2, int i_alpha,
368                               bool b_do12 )
369 {
370     p_dst[i_offset0] = vlc_blend( c0, p_dst[i_offset0], i_alpha );
371     if( b_do12 )
372     {
373         p_dst[i_offset1] = vlc_blend( c1, p_dst[i_offset1], i_alpha );
374         p_dst[i_offset2] = vlc_blend( c2, p_dst[i_offset2], i_alpha );
375     }
376 }
377
378 static void vlc_blend_rgb16( uint16_t *p_dst,
379                              int R, int G, int B, int i_alpha,
380                              const video_format_t *p_fmt )
381 {
382     const int i_pix = *p_dst;
383     const int r = ( i_pix & p_fmt->i_rmask ) >> p_fmt->i_lrshift;
384     const int g = ( i_pix & p_fmt->i_gmask ) >> p_fmt->i_lgshift;
385     const int b = ( i_pix & p_fmt->i_bmask ) >> p_fmt->i_lbshift;
386
387     *p_dst = ( vlc_blend( R >> p_fmt->i_rrshift, r, i_alpha ) << p_fmt->i_lrshift ) |
388              ( vlc_blend( G >> p_fmt->i_rgshift, g, i_alpha ) << p_fmt->i_lgshift ) |
389              ( vlc_blend( B >> p_fmt->i_rbshift, b, i_alpha ) << p_fmt->i_lbshift );
390 }
391
392 static void vlc_rgb_index( int *pi_rindex, int *pi_gindex, int *pi_bindex,
393                            const video_format_t *p_fmt )
394 {
395     if( p_fmt->i_chroma != FCC_RV24 && p_fmt->i_chroma != FCC_RV32 )
396         return;
397
398     /* XXX it will works only if mask are 8 bits aligned */
399 #ifdef WORDS_BIGENDIAN
400     const int i_mask_bits = p_fmt->i_chroma == FCC_RV24 ? 24 : 32;
401     *pi_rindex = ( i_mask_bits - p_fmt->i_lrshift ) / 8;
402     *pi_gindex = ( i_mask_bits - p_fmt->i_lgshift ) / 8;
403     *pi_bindex = ( i_mask_bits - p_fmt->i_lbshift ) / 8;
404 #else
405     *pi_rindex = p_fmt->i_lrshift / 8;
406     *pi_gindex = p_fmt->i_lgshift / 8;
407     *pi_bindex = p_fmt->i_lbshift / 8;
408 #endif
409 }
410
411 /***********************************************************************
412  * YUVA
413  ***********************************************************************/
414 static void BlendYUVAI420( filter_t *p_filter,
415                            picture_t *p_dst, picture_t *p_src,
416                            int i_x_offset, int i_y_offset,
417                            int i_width, int i_height, int i_alpha )
418 {
419     int i_src_pitch, i_dst_pitch;
420     uint8_t *p_src_y, *p_dst_y;
421     uint8_t *p_src_u, *p_dst_u;
422     uint8_t *p_src_v, *p_dst_v;
423     uint8_t *p_trans;
424     int i_x, i_y, i_trans = 0;
425     bool b_even_scanline = i_y_offset % 2;
426
427     p_dst_y = vlc_plane_start( &i_dst_pitch, p_dst, Y_PLANE,
428                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 1 );
429     p_dst_u = vlc_plane_start( NULL, p_dst, U_PLANE,
430                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
431     p_dst_v = vlc_plane_start( NULL, p_dst, V_PLANE,
432                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
433
434     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
435                                0, 0, &p_filter->fmt_in.video, 1 );
436     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
437                                0, 0, &p_filter->fmt_in.video, 2 );
438     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
439                                0, 0, &p_filter->fmt_in.video, 2 );
440     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
441                                0, 0, &p_filter->fmt_in.video, 1 );
442
443     /* Draw until we reach the bottom of the subtitle */
444     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
445          p_dst_y += i_dst_pitch, p_src_y += i_src_pitch,
446          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
447          p_src_u += i_src_pitch,
448          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
449          p_src_v += i_src_pitch )
450     {
451         b_even_scanline = !b_even_scanline;
452
453         /* Draw until we reach the end of the line */
454         for( i_x = 0; i_x < i_width; i_x++ )
455         {
456             if( p_trans )
457                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
458
459             if( !i_trans )
460                 continue;
461
462             /* Blending */
463             p_dst_y[i_x] = vlc_blend( p_src_y[i_x], p_dst_y[i_x], i_trans );
464             if( b_even_scanline && i_x % 2 == 0 )
465             {
466                 p_dst_u[i_x/2] = vlc_blend( p_src_u[i_x], p_dst_u[i_x/2], i_trans );
467                 p_dst_v[i_x/2] = vlc_blend( p_src_v[i_x], p_dst_v[i_x/2], i_trans );
468             }
469         }
470     }
471 }
472
473 static void BlendYUVARV16( filter_t *p_filter,
474                            picture_t *p_dst_pic, picture_t *p_src,
475                            int i_x_offset, int i_y_offset,
476                            int i_width, int i_height, int i_alpha )
477 {
478     int i_src_pitch, i_dst_pitch;
479     uint8_t *p_dst, *p_src_y;
480     uint8_t *p_src_u, *p_src_v;
481     uint8_t *p_trans;
482     int i_x, i_y, i_pix_pitch, i_trans = 0;
483     int r, g, b;
484
485     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
486     i_dst_pitch = p_dst_pic->p->i_pitch;
487     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
488             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
489             p_dst_pic->p->i_pitch *
490             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
491
492     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
493                                0, 0, &p_filter->fmt_in.video, 1 );
494     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
495                                0, 0, &p_filter->fmt_in.video, 2 );
496     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
497                                0, 0, &p_filter->fmt_in.video, 2 );
498     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
499                                0, 0, &p_filter->fmt_in.video, 1 );
500
501     /* Draw until we reach the bottom of the subtitle */
502     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
503          p_dst += i_dst_pitch,
504          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
505          p_src_v += i_src_pitch )
506     {
507         /* Draw until we reach the end of the line */
508         for( i_x = 0; i_x < i_width; i_x++ )
509         {
510             if( p_trans )
511                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
512             if( !i_trans )
513                 continue;
514
515             /* Blending */
516             yuv_to_rgb( &r, &g, &b,
517                         p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
518
519             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
520                              r, g, b, i_trans, &p_filter->fmt_out.video );
521         }
522     }
523 }
524
525 static void BlendYUVARV24( filter_t *p_filter,
526                            picture_t *p_dst_pic, picture_t *p_src,
527                            int i_x_offset, int i_y_offset,
528                            int i_width, int i_height, int i_alpha )
529 {
530     int i_src_pitch, i_dst_pitch;
531     uint8_t *p_dst, *p_src_y;
532     uint8_t *p_src_u, *p_src_v;
533     uint8_t *p_trans;
534     int i_x, i_y, i_pix_pitch, i_trans = 0;
535     int r, g, b;
536
537     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
538     i_dst_pitch = p_dst_pic->p->i_pitch;
539     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
540             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
541             p_dst_pic->p->i_pitch *
542             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
543
544     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
545                                0, 0, &p_filter->fmt_in.video, 1 );
546     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
547                                0, 0, &p_filter->fmt_in.video, 2 );
548     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
549                                0, 0, &p_filter->fmt_in.video, 2 );
550     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
551                                0, 0, &p_filter->fmt_in.video, 1 );
552
553     if( (i_pix_pitch == 4)
554      && (((((intptr_t)p_dst)|i_dst_pitch) /* FIXME? */
555           & 3) == 0) )
556     {
557         /*
558         ** if picture pixels are 32 bits long and lines addresses are 32 bit
559         ** aligned, optimize rendering
560         */
561         uint32_t *p32_dst = (uint32_t *)p_dst;
562         uint32_t i32_dst_pitch = (uint32_t)(i_dst_pitch>>2);
563
564         int i_rshift, i_gshift, i_bshift;
565         uint32_t i_rmask, i_gmask, i_bmask;
566
567         i_rmask = p_filter->fmt_out.video.i_rmask;
568         i_gmask = p_filter->fmt_out.video.i_gmask;
569         i_bmask = p_filter->fmt_out.video.i_bmask;
570         i_rshift = p_filter->fmt_out.video.i_lrshift;
571         i_gshift = p_filter->fmt_out.video.i_lgshift;
572         i_bshift = p_filter->fmt_out.video.i_lbshift;
573
574         /* Draw until we reach the bottom of the subtitle */
575         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
576              p32_dst += i32_dst_pitch,
577              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
578              p_src_v += i_src_pitch )
579         {
580             /* Draw until we reach the end of the line */
581             for( i_x = 0; i_x < i_width; i_x++ )
582             {
583                 if( p_trans )
584                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
585                 if( !i_trans )
586                     continue;
587
588                 if( i_trans == MAX_TRANS )
589                 {
590                     /* Completely opaque. Completely overwrite underlying pixel */
591                     yuv_to_rgb( &r, &g, &b,
592                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
593
594                     p32_dst[i_x] = (r<<i_rshift) |
595                                    (g<<i_gshift) |
596                                    (b<<i_bshift);
597                 }
598                 else
599                 {
600                     /* Blending */
601                     uint32_t i_pix_dst = p32_dst[i_x];
602                     yuv_to_rgb( &r, &g, &b,
603                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
604
605                     p32_dst[i_x] = ( vlc_blend( r, (i_pix_dst & i_rmask)>>i_rshift, i_trans ) << i_rshift ) |
606                                    ( vlc_blend( g, (i_pix_dst & i_gmask)>>i_gshift, i_trans ) << i_gshift ) |
607                                    ( vlc_blend( b, (i_pix_dst & i_bmask)>>i_bshift, i_trans ) << i_bshift );
608                 }
609             }
610         }
611     }
612     else
613     {
614         int i_rindex, i_gindex, i_bindex;
615         uint32_t i_rmask, i_gmask, i_bmask;
616
617         i_rmask = p_filter->fmt_out.video.i_rmask;
618         i_gmask = p_filter->fmt_out.video.i_gmask;
619         i_bmask = p_filter->fmt_out.video.i_bmask;
620
621         vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
622
623         /* Draw until we reach the bottom of the subtitle */
624         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
625              p_dst += i_dst_pitch,
626              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
627              p_src_v += i_src_pitch )
628         {
629             /* Draw until we reach the end of the line */
630             for( i_x = 0; i_x < i_width; i_x++ )
631             {
632                 if( p_trans )
633                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
634                 if( !i_trans )
635                     continue;
636
637                 /* Blending */
638                 yuv_to_rgb( &r, &g, &b,
639                             p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
640
641                 vlc_blend_packed( &p_dst[ i_x * i_pix_pitch],
642                                   i_rindex, i_gindex, i_bindex,
643                                   r, g, b, i_alpha, true );
644             }
645         }
646     }
647 }
648
649 static void BlendYUVAYUVPacked( filter_t *p_filter,
650                                 picture_t *p_dst_pic, picture_t *p_src,
651                                 int i_x_offset, int i_y_offset,
652                                 int i_width, int i_height, int i_alpha )
653 {
654     int i_src_pitch, i_dst_pitch;
655     uint8_t *p_dst, *p_src_y;
656     uint8_t *p_src_u, *p_src_v;
657     uint8_t *p_trans;
658     int i_x, i_y, i_pix_pitch, i_trans = 0;
659     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
660     int i_l_offset, i_u_offset, i_v_offset;
661
662     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
663                           p_filter->fmt_out.video.i_chroma );
664
665     i_pix_pitch = 2;
666     i_dst_pitch = p_dst_pic->p->i_pitch;
667     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
668             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
669             p_dst_pic->p->i_pitch *
670             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
671
672     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
673                                0, 0, &p_filter->fmt_in.video, 1 );
674     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
675                                0, 0, &p_filter->fmt_in.video, 2 );
676     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
677                                0, 0, &p_filter->fmt_in.video, 2 );
678     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
679                                0, 0, &p_filter->fmt_in.video, 1 );
680
681     i_width &= ~1; /* Needs to be a multiple of 2 */
682
683     /* Draw until we reach the bottom of the subtitle */
684     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
685          p_dst += i_dst_pitch,
686          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
687          p_src_v += i_src_pitch )
688     {
689         /* Draw until we reach the end of the line */
690         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
691         {
692             i_trans = vlc_alpha( p_trans[i_x], i_alpha );
693             if( !i_trans )
694                 continue;
695
696             /* Blending */
697             if( b_even )
698             {
699                 int i_u;
700                 int i_v;
701                 /* FIXME what's with 0xaa ? */
702                 if( p_trans[i_x+1] > 0xaa )
703                 {
704                     i_u = (p_src_u[i_x]+p_src_u[i_x+1])>>1;
705                     i_v = (p_src_v[i_x]+p_src_v[i_x+1])>>1;
706                 }
707                 else
708                 {
709                     i_u = p_src_u[i_x];
710                     i_v = p_src_v[i_x];
711                 }
712
713                 vlc_blend_packed( &p_dst[i_x * 2],
714                                   i_l_offset, i_u_offset, i_v_offset,
715                                   p_src_y[i_x], i_u, i_v, i_trans, true );
716             }
717             else
718             {
719                 p_dst[i_x * 2 + i_l_offset] = vlc_blend( p_src_y[i_x], p_dst[i_x * 2 + i_l_offset], i_trans );
720             }
721         }
722     }
723 }
724 /***********************************************************************
725  * I420, YV12
726  ***********************************************************************/
727 static void BlendI420I420( filter_t *p_filter,
728                            picture_t *p_dst, picture_t *p_src,
729                            int i_x_offset, int i_y_offset,
730                            int i_width, int i_height, int i_alpha )
731 {
732     int i_src_pitch, i_dst_pitch;
733     uint8_t *p_src_y, *p_dst_y;
734     uint8_t *p_src_u, *p_dst_u;
735     uint8_t *p_src_v, *p_dst_v;
736     int i_x, i_y;
737     bool b_even_scanline = i_y_offset % 2;
738
739     if( i_alpha == 0xff )
740     {
741         BlendI420I420_no_alpha( p_filter, p_dst, p_src,
742                                 i_x_offset, i_y_offset, i_width, i_height );
743         return;
744     }
745
746
747     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
748     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
749               p_filter->fmt_out.video.i_x_offset +
750               p_dst->p[Y_PLANE].i_pitch *
751               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
752     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
753               p_filter->fmt_out.video.i_x_offset/2 +
754               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
755               p_dst->p[U_PLANE].i_pitch;
756     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
757               p_filter->fmt_out.video.i_x_offset/2 +
758               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
759               p_dst->p[V_PLANE].i_pitch;
760
761     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
762                                0, 0, &p_filter->fmt_in.video, 1 );
763     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
764                                0, 0, &p_filter->fmt_in.video, 2 );
765     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
766                                0, 0, &p_filter->fmt_in.video, 2 );
767     i_width &= ~1;
768
769     /* Draw until we reach the bottom of the subtitle */
770     for( i_y = 0; i_y < i_height; i_y++,
771          p_dst_y += i_dst_pitch,
772          p_src_y += i_src_pitch )
773     {
774         if( b_even_scanline )
775         {
776             p_dst_u  += i_dst_pitch/2;
777             p_dst_v  += i_dst_pitch/2;
778         }
779         b_even_scanline = !b_even_scanline;
780
781         /* Draw until we reach the end of the line */
782         for( i_x = 0; i_x < i_width; i_x++ )
783         {
784             if( !i_alpha )
785                 continue;
786
787             /* Blending */
788             p_dst_y[i_x] = vlc_blend( p_src_y[i_x], p_dst_y[i_x], i_alpha );
789             if( b_even_scanline && i_x % 2 == 0 )
790             {
791                 p_dst_u[i_x/2] = vlc_blend( p_src_u[i_x/2], p_dst_u[i_x/2], i_alpha );
792                 p_dst_v[i_x/2] = vlc_blend( p_src_v[i_x/2], p_dst_v[i_x/2], i_alpha );
793             }
794         }
795         if( i_y%2 == 1 )
796         {
797             p_src_u += i_src_pitch/2;
798             p_src_v += i_src_pitch/2;
799         }
800     }
801 }
802 static void BlendI420I420_no_alpha( filter_t *p_filter,
803                                     picture_t *p_dst, picture_t *p_src,
804                                     int i_x_offset, int i_y_offset,
805                                     int i_width, int i_height )
806 {
807     int i_src_pitch, i_dst_pitch;
808     uint8_t *p_src_y, *p_dst_y;
809     uint8_t *p_src_u, *p_dst_u;
810     uint8_t *p_src_v, *p_dst_v;
811     int i_y;
812     bool b_even_scanline = i_y_offset % 2;
813
814     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
815     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
816               p_filter->fmt_out.video.i_x_offset +
817               p_dst->p[Y_PLANE].i_pitch *
818               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
819     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
820               p_filter->fmt_out.video.i_x_offset/2 +
821               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
822               p_dst->p[U_PLANE].i_pitch;
823     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
824               p_filter->fmt_out.video.i_x_offset/2 +
825               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
826               p_dst->p[V_PLANE].i_pitch;
827
828     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
829                                0, 0, &p_filter->fmt_in.video, 1 );
830     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
831                                0, 0, &p_filter->fmt_in.video, 2 );
832     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
833                                0, 0, &p_filter->fmt_in.video, 2 );
834
835     i_width &= ~1;
836
837     /* Draw until we reach the bottom of the subtitle */
838     for( i_y = 0; i_y < i_height;
839             i_y++, p_dst_y += i_dst_pitch, p_src_y += i_src_pitch )
840     {
841         /* Completely opaque. Completely overwrite underlying pixel */
842         vlc_memcpy( p_dst_y, p_src_y, i_width );
843         if( b_even_scanline )
844         {
845             p_dst_u  += i_dst_pitch/2;
846             p_dst_v  += i_dst_pitch/2;
847         }
848         else
849         {
850             vlc_memcpy( p_dst_u, p_src_u, i_width/2 );
851             vlc_memcpy( p_dst_v, p_src_v, i_width/2 );
852         }
853         b_even_scanline = !b_even_scanline;
854         if( i_y%2 == 1 )
855         {
856             p_src_u += i_src_pitch/2;
857             p_src_v += i_src_pitch/2;
858         }
859     }
860 }
861
862 static void BlendI420R16( filter_t *p_filter,
863                           picture_t *p_dst_pic, picture_t *p_src,
864                           int i_x_offset, int i_y_offset,
865                           int i_width, int i_height, int i_alpha )
866 {
867     int i_src_pitch, i_dst_pitch;
868     uint8_t *p_dst, *p_src_y;
869     uint8_t *p_src_u, *p_src_v;
870     int i_x, i_y, i_pix_pitch;
871     int r, g, b;
872
873     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
874     i_dst_pitch = p_dst_pic->p->i_pitch;
875     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
876             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
877             p_dst_pic->p->i_pitch *
878             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
879
880     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
881                                0, 0, &p_filter->fmt_in.video, 1 );
882     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
883                                0, 0, &p_filter->fmt_in.video, 2 );
884     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
885                                 0, 0, &p_filter->fmt_in.video, 2 );
886
887     /* Draw until we reach the bottom of the subtitle */
888     for( i_y = 0; i_y < i_height; i_y++,
889          p_dst += i_dst_pitch,
890          p_src_y += i_src_pitch )
891     {
892         /* Draw until we reach the end of the line */
893         for( i_x = 0; i_x < i_width; i_x++ )
894         {
895             /* Blending */
896             yuv_to_rgb( &r, &g, &b,
897                         p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2] );
898
899             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
900                              r, g, b, i_alpha, &p_filter->fmt_out.video );
901         }
902         if( i_y%2 == 1 )
903         {
904             p_src_u += i_src_pitch/2;
905             p_src_v += i_src_pitch/2;
906         }
907     }
908 }
909
910 static void BlendI420R24( filter_t *p_filter,
911                           picture_t *p_dst_pic, picture_t *p_src,
912                           int i_x_offset, int i_y_offset,
913                           int i_width, int i_height, int i_alpha )
914 {
915     int i_src_pitch, i_dst_pitch;
916     uint8_t *p_dst, *p_src_y;
917     uint8_t *p_src_u, *p_src_v;
918     int i_x, i_y, i_pix_pitch;
919     int i_rindex, i_gindex, i_bindex;
920     int r, g, b;
921
922     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
923     i_dst_pitch = p_dst_pic->p->i_pitch;
924     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
925             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
926             p_dst_pic->p->i_pitch *
927             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
928
929     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
930                                0, 0, &p_filter->fmt_in.video, 1 );
931     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
932                                0, 0, &p_filter->fmt_in.video, 2 );
933     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
934                                0, 0, &p_filter->fmt_in.video, 2 );
935
936     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
937
938     /* Draw until we reach the bottom of the subtitle */
939     for( i_y = 0; i_y < i_height; i_y++,
940          p_dst += i_dst_pitch,
941          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
942          p_src_v += i_src_pitch )
943     {
944         /* Draw until we reach the end of the line */
945         for( i_x = 0; i_x < i_width; i_x++ )
946         {
947             if( !i_alpha )
948                 continue;
949
950             /* Blending */
951             yuv_to_rgb( &r, &g, &b,
952                         p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2] );
953
954             vlc_blend_packed( &p_dst[i_x * i_pix_pitch],
955                               i_rindex, i_gindex, i_bindex, r, g, b, i_alpha, true );
956         }
957         if( i_y%2 == 1 )
958         {
959             p_src_u += i_src_pitch/2;
960             p_src_v += i_src_pitch/2;
961         }
962     }
963 }
964
965 static void BlendI420YUVPacked( filter_t *p_filter,
966                                 picture_t *p_dst_pic, picture_t *p_src,
967                                 int i_x_offset, int i_y_offset,
968                                 int i_width, int i_height, int i_alpha )
969 {
970     int i_src_pitch, i_dst_pitch;
971     uint8_t *p_dst, *p_src_y;
972     uint8_t *p_src_u, *p_src_v;
973     int i_x, i_y, i_pix_pitch;
974     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
975     int i_l_offset, i_u_offset, i_v_offset;
976
977     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
978                           p_filter->fmt_out.video.i_chroma );
979
980     i_pix_pitch = 2;
981     i_dst_pitch = p_dst_pic->p->i_pitch;
982     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
983             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
984             p_dst_pic->p->i_pitch *
985             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
986
987     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
988                                0, 0, &p_filter->fmt_in.video, 1 );
989     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
990                                0, 0, &p_filter->fmt_in.video, 2 );
991     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
992                                0, 0, &p_filter->fmt_in.video, 2 );
993
994     i_width &= ~1; /* Needs to be a multiple of 2 */
995
996     /* Draw until we reach the bottom of the subtitle */
997     for( i_y = 0; i_y < i_height; i_y++,
998          p_dst += i_dst_pitch,
999          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
1000          p_src_v += i_src_pitch )
1001     {
1002         /* Draw until we reach the end of the line */
1003         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1004         {
1005             if( !i_alpha )
1006                 continue;
1007
1008             /* Blending */
1009             vlc_blend_packed( &p_dst[i_x * 2],
1010                               i_l_offset, i_u_offset, i_v_offset,
1011                               p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2], i_alpha, b_even );
1012         }
1013         if( i_y%2 == 1 )
1014         {
1015             p_src_u += i_src_pitch/2;
1016             p_src_v += i_src_pitch/2;
1017         }
1018     }
1019 }
1020
1021 /***********************************************************************
1022  * YUVP
1023  ***********************************************************************/
1024 static void BlendPalI420( filter_t *p_filter,
1025                           picture_t *p_dst, picture_t *p_src_pic,
1026                           int i_x_offset, int i_y_offset,
1027                           int i_width, int i_height, int i_alpha )
1028 {
1029     int i_src_pitch, i_dst_pitch;
1030     uint8_t *p_src, *p_dst_y;
1031     uint8_t *p_dst_u;
1032     uint8_t *p_dst_v;
1033     int i_x, i_y, i_trans;
1034     bool b_even_scanline = i_y_offset % 2;
1035
1036     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1037     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1038               p_filter->fmt_out.video.i_x_offset +
1039               p_dst->p[Y_PLANE].i_pitch *
1040               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1041     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1042               p_filter->fmt_out.video.i_x_offset/2 +
1043               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1044               p_dst->p[U_PLANE].i_pitch;
1045     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1046               p_filter->fmt_out.video.i_x_offset/2 +
1047               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1048               p_dst->p[V_PLANE].i_pitch;
1049
1050     i_src_pitch = p_src_pic->p->i_pitch;
1051     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1052             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1053
1054 #define p_pal p_filter->fmt_in.video.p_palette->palette
1055
1056     /* Draw until we reach the bottom of the subtitle */
1057     for( i_y = 0; i_y < i_height; i_y++,
1058          p_dst_y += i_dst_pitch,
1059          p_src += i_src_pitch,
1060          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1061          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0 )
1062     {
1063         const uint8_t *p_trans = p_src;
1064         b_even_scanline = !b_even_scanline;
1065
1066         /* Draw until we reach the end of the line */
1067         for( i_x = 0; i_x < i_width; i_x++ )
1068         {
1069             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1070             if( !i_trans )
1071                 continue;
1072
1073             /* Blending */
1074             p_dst_y[i_x] = vlc_blend( p_pal[p_src[i_x]][0], p_dst_y[i_x], i_trans );
1075             if( b_even_scanline && ((i_x % 2) == 0) )
1076             {
1077                 p_dst_u[i_x/2] = vlc_blend( p_pal[p_src[i_x]][1], p_dst_u[i_x/2], i_trans );
1078                 p_dst_v[i_x/2] = vlc_blend( p_pal[p_src[i_x]][2], p_dst_v[i_x/2], i_trans );
1079             }
1080         }
1081     }
1082 #undef p_pal
1083 }
1084
1085 static void BlendPalYUVPacked( filter_t *p_filter,
1086                                picture_t *p_dst_pic, picture_t *p_src_pic,
1087                                int i_x_offset, int i_y_offset,
1088                                int i_width, int i_height, int i_alpha )
1089 {
1090     int i_src_pitch, i_dst_pitch;
1091     uint8_t *p_src, *p_dst;
1092     int i_x, i_y, i_pix_pitch, i_trans;
1093     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1094     int i_l_offset, i_u_offset, i_v_offset;
1095
1096     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1097                           p_filter->fmt_out.video.i_chroma );
1098
1099     i_pix_pitch = 2;
1100     i_dst_pitch = p_dst_pic->p->i_pitch;
1101     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1102             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1103             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1104
1105     i_src_pitch = p_src_pic->p->i_pitch;
1106     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1107             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1108
1109     i_width &= ~1; /* Needs to be a multiple of 2 */
1110
1111 #define p_pal p_filter->fmt_in.video.p_palette->palette
1112
1113     /* Draw until we reach the bottom of the subtitle */
1114     for( i_y = 0; i_y < i_height; i_y++,
1115          p_dst += i_dst_pitch, p_src += i_src_pitch )
1116     {
1117         const uint8_t *p_trans = p_src;
1118         /* Draw until we reach the end of the line */
1119         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1120         {
1121             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1122             if( !i_trans )
1123                 continue;
1124
1125             /* Blending */
1126             if( b_even )
1127             {
1128                 uint16_t i_u;
1129                 uint16_t i_v;
1130                 if( p_trans[i_x+1] > 0xaa )
1131                 {
1132                     i_u = (p_pal[p_src[i_x]][1] + p_pal[p_src[i_x+1]][1]) >> 1;
1133                     i_v = (p_pal[p_src[i_x]][2] + p_pal[p_src[i_x+1]][2]) >> 1;
1134                 }
1135                 else
1136                 {
1137                     i_u = p_pal[p_src[i_x]][1];
1138                     i_v = p_pal[p_src[i_x]][2];
1139                 }
1140
1141                 vlc_blend_packed( &p_dst[i_x * 2],
1142                                   i_l_offset, i_u_offset, i_v_offset,
1143                                   p_pal[p_src[i_x]][0], i_u, i_v, i_trans, true );
1144             }
1145             else
1146             {
1147                 p_dst[i_x * 2 + i_l_offset] = vlc_blend( p_pal[p_src[i_x]][0], p_dst[i_x * 2 + i_l_offset], i_trans );
1148             }
1149         }
1150     }
1151 #undef p_pal
1152 }
1153
1154 static void BlendPalRV( filter_t *p_filter,
1155                         picture_t *p_dst_pic, picture_t *p_src_pic,
1156                         int i_x_offset, int i_y_offset,
1157                         int i_width, int i_height, int i_alpha )
1158 {
1159     int i_src_pitch, i_dst_pitch;
1160     uint8_t *p_src, *p_dst;
1161     int i_x, i_y, i_pix_pitch, i_trans;
1162     video_palette_t rgbpalette;
1163     int i_rindex, i_gindex, i_bindex;
1164
1165     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1166     i_dst_pitch = p_dst_pic->p->i_pitch;
1167     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1168             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1169             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1170
1171     i_src_pitch = p_src_pic->p->i_pitch;
1172     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1173             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1174
1175 #define p_pal p_filter->fmt_in.video.p_palette->palette
1176 #define rgbpal rgbpalette.palette
1177
1178     /* Convert palette first */
1179     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries && i_y < 256; i_y++ )
1180     {
1181         int r, g, b;
1182
1183         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
1184         rgbpal[i_y][0] = r;
1185         rgbpal[i_y][1] = g;
1186         rgbpal[i_y][2] = b;
1187     }
1188
1189     /* */
1190     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1191
1192     /* Draw until we reach the bottom of the subtitle */
1193     for( i_y = 0; i_y < i_height; i_y++,
1194          p_dst += i_dst_pitch, p_src += i_src_pitch )
1195     {
1196         const uint8_t *p_trans = p_src;
1197         /* Draw until we reach the end of the line */
1198         for( i_x = 0; i_x < i_width; i_x++ )
1199         {
1200             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1201             if( !i_trans )
1202                 continue;
1203
1204             /* Blending */
1205             if( p_filter->fmt_out.video.i_chroma == FCC_RV15 || p_filter->fmt_out.video.i_chroma == FCC_RV16 )
1206                 vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1207                                   rgbpal[p_src[i_x]][0], rgbpal[p_src[i_x]][1], rgbpal[p_src[i_x]][2],
1208                                   i_trans,
1209                                   &p_filter->fmt_out.video );
1210             else
1211                 vlc_blend_packed( &p_dst[i_x * i_pix_pitch],
1212                                   i_rindex, i_gindex, i_bindex,
1213                                   rgbpal[p_src[i_x]][0], rgbpal[p_src[i_x]][1], rgbpal[p_src[i_x]][2],
1214                                   i_trans, true );
1215         }
1216     }
1217
1218 #undef p_pal
1219 #undef rgbpal
1220 }
1221
1222 /***********************************************************************
1223  * RGBA
1224  ***********************************************************************/
1225 static void BlendRGBAI420( filter_t *p_filter,
1226                            picture_t *p_dst, picture_t *p_src_pic,
1227                            int i_x_offset, int i_y_offset,
1228                            int i_width, int i_height, int i_alpha )
1229 {
1230     int i_src_pitch, i_dst_pitch, i_src_pix_pitch;
1231     uint8_t *p_dst_y;
1232     uint8_t *p_dst_u;
1233     uint8_t *p_dst_v;
1234     uint8_t *p_src;
1235     int i_x, i_y, i_trans;
1236     uint8_t y, u, v;
1237
1238     bool b_even_scanline = i_y_offset % 2;
1239
1240     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1241     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1242               p_filter->fmt_out.video.i_x_offset +
1243               p_dst->p[Y_PLANE].i_pitch *
1244               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1245     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1246               p_filter->fmt_out.video.i_x_offset/2 +
1247               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1248               p_dst->p[U_PLANE].i_pitch;
1249     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1250               p_filter->fmt_out.video.i_x_offset/2 +
1251               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1252               p_dst->p[V_PLANE].i_pitch;
1253
1254     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1255     i_src_pitch = p_src_pic->p->i_pitch;
1256     p_src = p_src_pic->p->p_pixels +
1257             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1258             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1259
1260
1261     /* Draw until we reach the bottom of the subtitle */
1262     for( i_y = 0; i_y < i_height; i_y++,
1263          p_dst_y += i_dst_pitch,
1264          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1265          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1266          p_src += i_src_pitch )
1267     {
1268         b_even_scanline = !b_even_scanline;
1269
1270         /* Draw until we reach the end of the line */
1271         for( i_x = 0; i_x < i_width; i_x++ )
1272         {
1273             const int R = p_src[i_x * i_src_pix_pitch + 0];
1274             const int G = p_src[i_x * i_src_pix_pitch + 1];
1275             const int B = p_src[i_x * i_src_pix_pitch + 2];
1276
1277             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1278             if( !i_trans )
1279                 continue;
1280
1281             /* Blending */
1282             rgb_to_yuv( &y, &u, &v, R, G, B );
1283
1284             p_dst_y[i_x] = vlc_blend( y, p_dst_y[i_x], i_trans );
1285             if( b_even_scanline && i_x % 2 == 0 )
1286             {
1287                 p_dst_u[i_x/2] = vlc_blend( u, p_dst_u[i_x/2], i_trans );
1288                 p_dst_v[i_x/2] = vlc_blend( v, p_dst_v[i_x/2], i_trans );
1289             }
1290         }
1291     }
1292 }
1293
1294 static void BlendRGBAR24( filter_t *p_filter,
1295                           picture_t *p_dst_pic, picture_t *p_src_pic,
1296                           int i_x_offset, int i_y_offset,
1297                           int i_width, int i_height, int i_alpha )
1298 {
1299     int i_src_pitch, i_dst_pitch;
1300     uint8_t *p_dst, *p_src;
1301     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1302     int i_rindex, i_gindex, i_bindex;
1303
1304     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1305     i_dst_pitch = p_dst_pic->p->i_pitch;
1306     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1307             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1308             p_dst_pic->p->i_pitch *
1309             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1310
1311     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1312     i_src_pitch = p_src_pic->p->i_pitch;
1313     p_src = p_src_pic->p->p_pixels +
1314             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1315             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1316
1317     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1318
1319     /* Draw until we reach the bottom of the subtitle */
1320     for( i_y = 0; i_y < i_height; i_y++,
1321          p_dst += i_dst_pitch, p_src += i_src_pitch )
1322     {
1323         /* Draw until we reach the end of the line */
1324         for( i_x = 0; i_x < i_width; i_x++ )
1325         {
1326             const int R = p_src[i_x * i_src_pix_pitch + 0];
1327             const int G = p_src[i_x * i_src_pix_pitch + 1];
1328             const int B = p_src[i_x * i_src_pix_pitch + 2];
1329
1330             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1331             if( !i_trans )
1332                 continue;
1333
1334             /* Blending */
1335             vlc_blend_packed( &p_dst[i_x * i_pix_pitch],
1336                               i_rindex, i_gindex, i_bindex,
1337                               R, G, B, i_trans, true );
1338         }
1339     }
1340 }
1341
1342 static void BlendRGBAR16( filter_t *p_filter,
1343                           picture_t *p_dst_pic, picture_t *p_src_pic,
1344                           int i_x_offset, int i_y_offset,
1345                           int i_width, int i_height, int i_alpha )
1346 {
1347     int i_src_pitch, i_dst_pitch;
1348     uint8_t *p_dst, *p_src;
1349     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1350
1351     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1352     i_dst_pitch = p_dst_pic->p->i_pitch;
1353     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1354             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1355             p_dst_pic->p->i_pitch *
1356             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1357
1358     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1359     i_src_pitch = p_src_pic->p->i_pitch;
1360     p_src = p_src_pic->p->p_pixels +
1361             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1362             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1363
1364     /* Draw until we reach the bottom of the subtitle */
1365     for( i_y = 0; i_y < i_height; i_y++,
1366          p_dst += i_dst_pitch, p_src += i_src_pitch )
1367     {
1368         /* Draw until we reach the end of the line */
1369         for( i_x = 0; i_x < i_width; i_x++ )
1370         {
1371             const int R = p_src[i_x * i_src_pix_pitch + 0];
1372             const int G = p_src[i_x * i_src_pix_pitch + 1];
1373             const int B = p_src[i_x * i_src_pix_pitch + 2];
1374
1375             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1376             if( !i_trans )
1377                 continue;
1378
1379             /* Blending */
1380             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1381                              R, G, B, i_trans, &p_filter->fmt_out.video );
1382         }
1383     }
1384 }
1385
1386 static void BlendRGBAYUVPacked( filter_t *p_filter,
1387                                 picture_t *p_dst_pic, picture_t *p_src_pic,
1388                                 int i_x_offset, int i_y_offset,
1389                                 int i_width, int i_height, int i_alpha )
1390 {
1391     int i_src_pitch, i_dst_pitch, i_src_pix_pitch;
1392     uint8_t *p_dst, *p_src;
1393     int i_x, i_y, i_pix_pitch, i_trans;
1394     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1395     int i_l_offset, i_u_offset, i_v_offset;
1396     uint8_t y, u, v;
1397
1398     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1399                           p_filter->fmt_out.video.i_chroma );
1400
1401     i_pix_pitch = 2;
1402     i_dst_pitch = p_dst_pic->p->i_pitch;
1403     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1404             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1405             p_dst_pic->p->i_pitch *
1406             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1407
1408     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1409     i_src_pitch = p_src_pic->p->i_pitch;
1410     p_src = p_src_pic->p->p_pixels +
1411             p_filter->fmt_in.video.i_x_offset * i_src_pitch +
1412             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1413
1414     i_width &= ~1; /* Needs to be a multiple of 2 */
1415
1416     /* Draw until we reach the bottom of the subtitle */
1417     for( i_y = 0; i_y < i_height; i_y++,
1418          p_dst += i_dst_pitch,
1419          p_src += i_src_pitch )
1420     {
1421         /* Draw until we reach the end of the line */
1422         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1423         {
1424             const int R = p_src[i_x * i_src_pix_pitch + 0];
1425             const int G = p_src[i_x * i_src_pix_pitch + 1];
1426             const int B = p_src[i_x * i_src_pix_pitch + 2];
1427
1428             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1429             if( !i_trans )
1430                 continue;
1431
1432             /* Blending */
1433             rgb_to_yuv( &y, &u, &v, R, G, B );
1434
1435             vlc_blend_packed( &p_dst[i_x * 2],
1436                               i_l_offset, i_u_offset, i_v_offset,
1437                               y, u, v, i_trans, b_even );
1438         }
1439     }
1440 }