]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
Remove useless argument to pf_video_blend
[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, const uint8_t *p_src,
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_src[i_offset0], i_alpha );
371     if( b_do12 )
372     {
373         p_dst[i_offset1] = vlc_blend( c1, p_src[i_offset1], i_alpha );
374         p_dst[i_offset2] = vlc_blend( c2, p_src[i_offset2], i_alpha );
375     }
376 }
377
378 static void vlc_blend_rgb16( uint16_t *p_dst, const uint16_t *p_src,
379                              int R, int G, int B, int i_alpha,
380                              const video_format_t *p_fmt )
381 {
382     const int i_pix = *p_src;
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                              (const uint16_t*)&p_dst[i_x * i_pix_pitch],
521                              r, g, b, i_trans, &p_filter->fmt_out.video );
522         }
523     }
524 }
525
526 static void BlendYUVARV24( filter_t *p_filter,
527                            picture_t *p_dst_pic, picture_t *p_src,
528                            int i_x_offset, int i_y_offset,
529                            int i_width, int i_height, int i_alpha )
530 {
531     int i_src_pitch, i_dst_pitch;
532     uint8_t *p_dst, *p_src_y;
533     uint8_t *p_src_u, *p_src_v;
534     uint8_t *p_trans;
535     int i_x, i_y, i_pix_pitch, i_trans = 0;
536     int r, g, b;
537
538     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
539     i_dst_pitch = p_dst_pic->p->i_pitch;
540     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
541             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
542             p_dst_pic->p->i_pitch *
543             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
544
545     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
546                                0, 0, &p_filter->fmt_in.video, 1 );
547     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
548                                0, 0, &p_filter->fmt_in.video, 2 );
549     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
550                                0, 0, &p_filter->fmt_in.video, 2 );
551     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
552                                0, 0, &p_filter->fmt_in.video, 1 );
553
554     if( (i_pix_pitch == 4)
555      && (((((intptr_t)p_dst)|i_dst_pitch) /* FIXME? */
556           & 3) == 0) )
557     {
558         /*
559         ** if picture pixels are 32 bits long and lines addresses are 32 bit
560         ** aligned, optimize rendering
561         */
562         uint32_t *p32_dst = (uint32_t *)p_dst;
563         uint32_t i32_dst_pitch = (uint32_t)(i_dst_pitch>>2);
564
565         int i_rshift, i_gshift, i_bshift;
566         uint32_t i_rmask, i_gmask, i_bmask;
567
568         i_rmask = p_filter->fmt_out.video.i_rmask;
569         i_gmask = p_filter->fmt_out.video.i_gmask;
570         i_bmask = p_filter->fmt_out.video.i_bmask;
571         i_rshift = p_filter->fmt_out.video.i_lrshift;
572         i_gshift = p_filter->fmt_out.video.i_lgshift;
573         i_bshift = p_filter->fmt_out.video.i_lbshift;
574
575         /* Draw until we reach the bottom of the subtitle */
576         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
577              p32_dst += i32_dst_pitch,
578              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
579              p_src_v += i_src_pitch )
580         {
581             /* Draw until we reach the end of the line */
582             for( i_x = 0; i_x < i_width; i_x++ )
583             {
584                 if( p_trans )
585                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
586                 if( !i_trans )
587                     continue;
588
589                 if( i_trans == MAX_TRANS )
590                 {
591                     /* Completely opaque. Completely overwrite underlying pixel */
592                     yuv_to_rgb( &r, &g, &b,
593                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
594
595                     p32_dst[i_x] = (r<<i_rshift) |
596                                    (g<<i_gshift) |
597                                    (b<<i_bshift);
598                 }
599                 else
600                 {
601                     /* Blending */
602                     uint32_t i_pix_dst = p32_dst[i_x];
603                     yuv_to_rgb( &r, &g, &b,
604                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
605
606                     p32_dst[i_x] = ( vlc_blend( r, (i_pix_dst & i_rmask)>>i_rshift, i_trans ) << i_rshift ) |
607                                    ( vlc_blend( g, (i_pix_dst & i_gmask)>>i_gshift, i_trans ) << i_gshift ) |
608                                    ( vlc_blend( b, (i_pix_dst & i_bmask)>>i_bshift, i_trans ) << i_bshift );
609                 }
610             }
611         }
612     }
613     else
614     {
615         int i_rindex, i_gindex, i_bindex;
616         uint32_t i_rmask, i_gmask, i_bmask;
617
618         i_rmask = p_filter->fmt_out.video.i_rmask;
619         i_gmask = p_filter->fmt_out.video.i_gmask;
620         i_bmask = p_filter->fmt_out.video.i_bmask;
621
622         vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
623
624         /* Draw until we reach the bottom of the subtitle */
625         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
626              p_dst += i_dst_pitch,
627              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
628              p_src_v += i_src_pitch )
629         {
630             /* Draw until we reach the end of the line */
631             for( i_x = 0; i_x < i_width; i_x++ )
632             {
633                 if( p_trans )
634                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
635                 if( !i_trans )
636                     continue;
637
638                 /* Blending */
639                 yuv_to_rgb( &r, &g, &b,
640                             p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
641
642                 vlc_blend_packed( &p_dst[ i_x * i_pix_pitch],
643                                   &p_dst[i_x * i_pix_pitch],
644                                   i_rindex, i_gindex, i_bindex,
645                                   r, g, b, i_alpha, true );
646             }
647         }
648     }
649 }
650
651 static void BlendYUVAYUVPacked( filter_t *p_filter,
652                                 picture_t *p_dst_pic, picture_t *p_src,
653                                 int i_x_offset, int i_y_offset,
654                                 int i_width, int i_height, int i_alpha )
655 {
656     int i_src_pitch, i_dst_pitch;
657     uint8_t *p_dst, *p_src_y;
658     uint8_t *p_src_u, *p_src_v;
659     uint8_t *p_trans;
660     int i_x, i_y, i_pix_pitch, i_trans = 0;
661     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
662     int i_l_offset, i_u_offset, i_v_offset;
663
664     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
665                           p_filter->fmt_out.video.i_chroma );
666
667     i_pix_pitch = 2;
668     i_dst_pitch = p_dst_pic->p->i_pitch;
669     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
670             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
671             p_dst_pic->p->i_pitch *
672             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
673
674     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
675                                0, 0, &p_filter->fmt_in.video, 1 );
676     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
677                                0, 0, &p_filter->fmt_in.video, 2 );
678     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
679                                0, 0, &p_filter->fmt_in.video, 2 );
680     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
681                                0, 0, &p_filter->fmt_in.video, 1 );
682
683     i_width &= ~1; /* Needs to be a multiple of 2 */
684
685     /* Draw until we reach the bottom of the subtitle */
686     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
687          p_dst += i_dst_pitch,
688          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
689          p_src_v += i_src_pitch )
690     {
691         /* Draw until we reach the end of the line */
692         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
693         {
694             i_trans = vlc_alpha( p_trans[i_x], i_alpha );
695             if( !i_trans )
696                 continue;
697
698             /* Blending */
699             if( b_even )
700             {
701                 int i_u;
702                 int i_v;
703                 /* FIXME what's with 0xaa ? */
704                 if( p_trans[i_x+1] > 0xaa )
705                 {
706                     i_u = (p_src_u[i_x]+p_src_u[i_x+1])>>1;
707                     i_v = (p_src_v[i_x]+p_src_v[i_x+1])>>1;
708                 }
709                 else
710                 {
711                     i_u = p_src_u[i_x];
712                     i_v = p_src_v[i_x];
713                 }
714
715                 vlc_blend_packed( &p_dst[i_x * 2], &p_dst[i_x * 2],
716                                   i_l_offset, i_u_offset, i_v_offset,
717                                   p_src_y[i_x], i_u, i_v, i_trans, true );
718             }
719             else
720             {
721                 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 );
722             }
723         }
724     }
725 }
726 /***********************************************************************
727  * I420, YV12
728  ***********************************************************************/
729 static void BlendI420I420( filter_t *p_filter,
730                            picture_t *p_dst, picture_t *p_src,
731                            int i_x_offset, int i_y_offset,
732                            int i_width, int i_height, int i_alpha )
733 {
734     int i_src_pitch, i_dst_pitch;
735     uint8_t *p_src_y, *p_dst_y;
736     uint8_t *p_src_u, *p_dst_u;
737     uint8_t *p_src_v, *p_dst_v;
738     int i_x, i_y;
739     bool b_even_scanline = i_y_offset % 2;
740
741     if( i_alpha == 0xff )
742     {
743         BlendI420I420_no_alpha( p_filter, p_dst, p_src,
744                                 i_x_offset, i_y_offset, i_width, i_height );
745         return;
746     }
747
748
749     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
750     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
751               p_filter->fmt_out.video.i_x_offset +
752               p_dst->p[Y_PLANE].i_pitch *
753               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
754     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
755               p_filter->fmt_out.video.i_x_offset/2 +
756               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
757               p_dst->p[U_PLANE].i_pitch;
758     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
759               p_filter->fmt_out.video.i_x_offset/2 +
760               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
761               p_dst->p[V_PLANE].i_pitch;
762
763     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
764                                0, 0, &p_filter->fmt_in.video, 1 );
765     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
766                                0, 0, &p_filter->fmt_in.video, 2 );
767     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
768                                0, 0, &p_filter->fmt_in.video, 2 );
769     i_width &= ~1;
770
771     /* Draw until we reach the bottom of the subtitle */
772     for( i_y = 0; i_y < i_height; i_y++,
773          p_dst_y += i_dst_pitch,
774          p_src_y += i_src_pitch )
775     {
776         if( b_even_scanline )
777         {
778             p_dst_u  += i_dst_pitch/2;
779             p_dst_v  += i_dst_pitch/2;
780         }
781         b_even_scanline = !b_even_scanline;
782
783         /* Draw until we reach the end of the line */
784         for( i_x = 0; i_x < i_width; i_x++ )
785         {
786             if( !i_alpha )
787                 continue;
788
789             /* Blending */
790             p_dst_y[i_x] = vlc_blend( p_src_y[i_x], p_dst_y[i_x], i_alpha );
791             if( b_even_scanline && i_x % 2 == 0 )
792             {
793                 p_dst_u[i_x/2] = vlc_blend( p_src_u[i_x/2], p_dst_u[i_x/2], i_alpha );
794                 p_dst_v[i_x/2] = vlc_blend( p_src_v[i_x/2], p_dst_v[i_x/2], i_alpha );
795             }
796         }
797         if( i_y%2 == 1 )
798         {
799             p_src_u += i_src_pitch/2;
800             p_src_v += i_src_pitch/2;
801         }
802     }
803 }
804 static void BlendI420I420_no_alpha( filter_t *p_filter,
805                                     picture_t *p_dst, picture_t *p_src,
806                                     int i_x_offset, int i_y_offset,
807                                     int i_width, int i_height )
808 {
809     int i_src_pitch, i_dst_pitch;
810     uint8_t *p_src_y, *p_dst_y;
811     uint8_t *p_src_u, *p_dst_u;
812     uint8_t *p_src_v, *p_dst_v;
813     int i_y;
814     bool b_even_scanline = i_y_offset % 2;
815
816     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
817     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
818               p_filter->fmt_out.video.i_x_offset +
819               p_dst->p[Y_PLANE].i_pitch *
820               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
821     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
822               p_filter->fmt_out.video.i_x_offset/2 +
823               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
824               p_dst->p[U_PLANE].i_pitch;
825     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
826               p_filter->fmt_out.video.i_x_offset/2 +
827               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
828               p_dst->p[V_PLANE].i_pitch;
829
830     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
831                                0, 0, &p_filter->fmt_in.video, 1 );
832     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
833                                0, 0, &p_filter->fmt_in.video, 2 );
834     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
835                                0, 0, &p_filter->fmt_in.video, 2 );
836
837     i_width &= ~1;
838
839     /* Draw until we reach the bottom of the subtitle */
840     for( i_y = 0; i_y < i_height;
841             i_y++, p_dst_y += i_dst_pitch, p_src_y += i_src_pitch )
842     {
843         /* Completely opaque. Completely overwrite underlying pixel */
844         vlc_memcpy( p_dst_y, p_src_y, i_width );
845         if( b_even_scanline )
846         {
847             p_dst_u  += i_dst_pitch/2;
848             p_dst_v  += i_dst_pitch/2;
849         }
850         else
851         {
852             vlc_memcpy( p_dst_u, p_src_u, i_width/2 );
853             vlc_memcpy( p_dst_v, p_src_v, i_width/2 );
854         }
855         b_even_scanline = !b_even_scanline;
856         if( i_y%2 == 1 )
857         {
858             p_src_u += i_src_pitch/2;
859             p_src_v += i_src_pitch/2;
860         }
861     }
862 }
863
864 static void BlendI420R16( filter_t *p_filter,
865                           picture_t *p_dst_pic, picture_t *p_src,
866                           int i_x_offset, int i_y_offset,
867                           int i_width, int i_height, int i_alpha )
868 {
869     int i_src_pitch, i_dst_pitch;
870     uint8_t *p_dst, *p_src_y;
871     uint8_t *p_src_u, *p_src_v;
872     int i_x, i_y, i_pix_pitch;
873     int r, g, b;
874
875     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
876     i_dst_pitch = p_dst_pic->p->i_pitch;
877     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
878             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
879             p_dst_pic->p->i_pitch *
880             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
881
882     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
883                                0, 0, &p_filter->fmt_in.video, 1 );
884     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
885                                0, 0, &p_filter->fmt_in.video, 2 );
886     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
887                                 0, 0, &p_filter->fmt_in.video, 2 );
888
889     /* Draw until we reach the bottom of the subtitle */
890     for( i_y = 0; i_y < i_height; i_y++,
891          p_dst += i_dst_pitch,
892          p_src_y += i_src_pitch )
893     {
894         /* Draw until we reach the end of the line */
895         for( i_x = 0; i_x < i_width; i_x++ )
896         {
897             /* Blending */
898             yuv_to_rgb( &r, &g, &b,
899                         p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2] );
900
901             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
902                              (const uint16_t*)&p_dst[i_x * i_pix_pitch],
903                              r, g, b, i_alpha, &p_filter->fmt_out.video );
904         }
905         if( i_y%2 == 1 )
906         {
907             p_src_u += i_src_pitch/2;
908             p_src_v += i_src_pitch/2;
909         }
910     }
911 }
912
913 static void BlendI420R24( filter_t *p_filter,
914                           picture_t *p_dst_pic, picture_t *p_src,
915                           int i_x_offset, int i_y_offset,
916                           int i_width, int i_height, int i_alpha )
917 {
918     int i_src_pitch, i_dst_pitch;
919     uint8_t *p_dst, *p_src_y;
920     uint8_t *p_src_u, *p_src_v;
921     int i_x, i_y, i_pix_pitch;
922     int i_rindex, i_gindex, i_bindex;
923     int r, g, b;
924
925     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
926     i_dst_pitch = p_dst_pic->p->i_pitch;
927     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
928             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
929             p_dst_pic->p->i_pitch *
930             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
931
932     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
933                                0, 0, &p_filter->fmt_in.video, 1 );
934     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
935                                0, 0, &p_filter->fmt_in.video, 2 );
936     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
937                                0, 0, &p_filter->fmt_in.video, 2 );
938
939     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
940
941     /* Draw until we reach the bottom of the subtitle */
942     for( i_y = 0; i_y < i_height; i_y++,
943          p_dst += i_dst_pitch,
944          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
945          p_src_v += i_src_pitch )
946     {
947         /* Draw until we reach the end of the line */
948         for( i_x = 0; i_x < i_width; i_x++ )
949         {
950             if( !i_alpha )
951                 continue;
952
953             /* Blending */
954             yuv_to_rgb( &r, &g, &b,
955                         p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2] );
956
957             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_dst[i_x * i_pix_pitch],
958                               i_rindex, i_gindex, i_bindex, r, g, b, i_alpha, true );
959         }
960         if( i_y%2 == 1 )
961         {
962             p_src_u += i_src_pitch/2;
963             p_src_v += i_src_pitch/2;
964         }
965     }
966 }
967
968 static void BlendI420YUVPacked( filter_t *p_filter,
969                                 picture_t *p_dst_pic, picture_t *p_src,
970                                 int i_x_offset, int i_y_offset,
971                                 int i_width, int i_height, int i_alpha )
972 {
973     int i_src_pitch, i_dst_pitch;
974     uint8_t *p_dst, *p_src_y;
975     uint8_t *p_src_u, *p_src_v;
976     int i_x, i_y, i_pix_pitch;
977     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
978     int i_l_offset, i_u_offset, i_v_offset;
979
980     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
981                           p_filter->fmt_out.video.i_chroma );
982
983     i_pix_pitch = 2;
984     i_dst_pitch = p_dst_pic->p->i_pitch;
985     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
986             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
987             p_dst_pic->p->i_pitch *
988             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
989
990     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
991                                0, 0, &p_filter->fmt_in.video, 1 );
992     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
993                                0, 0, &p_filter->fmt_in.video, 2 );
994     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
995                                0, 0, &p_filter->fmt_in.video, 2 );
996
997     i_width &= ~1; /* Needs to be a multiple of 2 */
998
999     /* Draw until we reach the bottom of the subtitle */
1000     for( i_y = 0; i_y < i_height; i_y++,
1001          p_dst += i_dst_pitch,
1002          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
1003          p_src_v += i_src_pitch )
1004     {
1005         /* Draw until we reach the end of the line */
1006         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1007         {
1008             if( !i_alpha )
1009                 continue;
1010
1011             /* Blending */
1012             vlc_blend_packed( &p_dst[i_x * 2], &p_dst[i_x * 2],
1013                               i_l_offset, i_u_offset, i_v_offset,
1014                               p_src_y[i_x], p_src_u[i_x/2], p_src_v[i_x/2], i_alpha, b_even );
1015         }
1016         if( i_y%2 == 1 )
1017         {
1018             p_src_u += i_src_pitch/2;
1019             p_src_v += i_src_pitch/2;
1020         }
1021     }
1022 }
1023
1024 /***********************************************************************
1025  * YUVP
1026  ***********************************************************************/
1027 static void BlendPalI420( filter_t *p_filter,
1028                           picture_t *p_dst, picture_t *p_src_pic,
1029                           int i_x_offset, int i_y_offset,
1030                           int i_width, int i_height, int i_alpha )
1031 {
1032     int i_src_pitch, i_dst_pitch;
1033     uint8_t *p_src, *p_dst_y;
1034     uint8_t *p_dst_u;
1035     uint8_t *p_dst_v;
1036     int i_x, i_y, i_trans;
1037     bool b_even_scanline = i_y_offset % 2;
1038
1039     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1040     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1041               p_filter->fmt_out.video.i_x_offset +
1042               p_dst->p[Y_PLANE].i_pitch *
1043               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1044     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1045               p_filter->fmt_out.video.i_x_offset/2 +
1046               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1047               p_dst->p[U_PLANE].i_pitch;
1048     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1049               p_filter->fmt_out.video.i_x_offset/2 +
1050               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1051               p_dst->p[V_PLANE].i_pitch;
1052
1053     i_src_pitch = p_src_pic->p->i_pitch;
1054     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1055             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1056
1057 #define p_pal p_filter->fmt_in.video.p_palette->palette
1058
1059     /* Draw until we reach the bottom of the subtitle */
1060     for( i_y = 0; i_y < i_height; i_y++,
1061          p_dst_y += i_dst_pitch,
1062          p_src += i_src_pitch,
1063          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1064          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0 )
1065     {
1066         const uint8_t *p_trans = p_src;
1067         b_even_scanline = !b_even_scanline;
1068
1069         /* Draw until we reach the end of the line */
1070         for( i_x = 0; i_x < i_width; i_x++ )
1071         {
1072             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1073             if( !i_trans )
1074                 continue;
1075
1076             /* Blending */
1077             p_dst_y[i_x] = vlc_blend( p_pal[p_src[i_x]][0], p_dst_y[i_x], i_trans );
1078             if( b_even_scanline && ((i_x % 2) == 0) )
1079             {
1080                 p_dst_u[i_x/2] = vlc_blend( p_pal[p_src[i_x]][1], p_dst_u[i_x/2], i_trans );
1081                 p_dst_v[i_x/2] = vlc_blend( p_pal[p_src[i_x]][2], p_dst_v[i_x/2], i_trans );
1082             }
1083         }
1084     }
1085 #undef p_pal
1086 }
1087
1088 static void BlendPalYUVPacked( filter_t *p_filter,
1089                                picture_t *p_dst_pic, picture_t *p_src_pic,
1090                                int i_x_offset, int i_y_offset,
1091                                int i_width, int i_height, int i_alpha )
1092 {
1093     int i_src_pitch, i_dst_pitch;
1094     uint8_t *p_src, *p_dst;
1095     int i_x, i_y, i_pix_pitch, i_trans;
1096     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1097     int i_l_offset, i_u_offset, i_v_offset;
1098
1099     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1100                           p_filter->fmt_out.video.i_chroma );
1101
1102     i_pix_pitch = 2;
1103     i_dst_pitch = p_dst_pic->p->i_pitch;
1104     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1105             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1106             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1107
1108     i_src_pitch = p_src_pic->p->i_pitch;
1109     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1110             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1111
1112     i_width &= ~1; /* Needs to be a multiple of 2 */
1113
1114 #define p_pal p_filter->fmt_in.video.p_palette->palette
1115
1116     /* Draw until we reach the bottom of the subtitle */
1117     for( i_y = 0; i_y < i_height; i_y++,
1118          p_dst += i_dst_pitch, p_src += i_src_pitch )
1119     {
1120         const uint8_t *p_trans = p_src;
1121         /* Draw until we reach the end of the line */
1122         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1123         {
1124             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1125             if( !i_trans )
1126                 continue;
1127
1128             /* Blending */
1129             if( b_even )
1130             {
1131                 uint16_t i_u;
1132                 uint16_t i_v;
1133                 if( p_trans[i_x+1] > 0xaa )
1134                 {
1135                     i_u = (p_pal[p_src[i_x]][1] + p_pal[p_src[i_x+1]][1]) >> 1;
1136                     i_v = (p_pal[p_src[i_x]][2] + p_pal[p_src[i_x+1]][2]) >> 1;
1137                 }
1138                 else
1139                 {
1140                     i_u = p_pal[p_src[i_x]][1];
1141                     i_v = p_pal[p_src[i_x]][2];
1142                 }
1143
1144                 vlc_blend_packed( &p_dst[i_x * 2], &p_dst[i_x * 2],
1145                                   i_l_offset, i_u_offset, i_v_offset,
1146                                   p_pal[p_src[i_x]][0], i_u, i_v, i_trans, true );
1147             }
1148             else
1149             {
1150                 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 );
1151             }
1152         }
1153     }
1154 #undef p_pal
1155 }
1156
1157 static void BlendPalRV( filter_t *p_filter,
1158                         picture_t *p_dst_pic, picture_t *p_src_pic,
1159                         int i_x_offset, int i_y_offset,
1160                         int i_width, int i_height, int i_alpha )
1161 {
1162     int i_src_pitch, i_dst_pitch;
1163     uint8_t *p_src, *p_dst;
1164     int i_x, i_y, i_pix_pitch, i_trans;
1165     video_palette_t rgbpalette;
1166     int i_rindex, i_gindex, i_bindex;
1167
1168     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1169     i_dst_pitch = p_dst_pic->p->i_pitch;
1170     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1171             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1172             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1173
1174     i_src_pitch = p_src_pic->p->i_pitch;
1175     p_src = p_src_pic->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1176             i_src_pitch * p_filter->fmt_in.video.i_y_offset;
1177
1178 #define p_pal p_filter->fmt_in.video.p_palette->palette
1179 #define rgbpal rgbpalette.palette
1180
1181     /* Convert palette first */
1182     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries && i_y < 256; i_y++ )
1183     {
1184         int r, g, b;
1185
1186         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
1187         rgbpal[i_y][0] = r;
1188         rgbpal[i_y][1] = g;
1189         rgbpal[i_y][2] = b;
1190     }
1191
1192     /* */
1193     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1194
1195     /* Draw until we reach the bottom of the subtitle */
1196     for( i_y = 0; i_y < i_height; i_y++,
1197          p_dst += i_dst_pitch, p_src += i_src_pitch )
1198     {
1199         const uint8_t *p_trans = p_src;
1200         /* Draw until we reach the end of the line */
1201         for( i_x = 0; i_x < i_width; i_x++ )
1202         {
1203             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1204             if( !i_trans )
1205                 continue;
1206
1207             /* Blending */
1208             if( p_filter->fmt_out.video.i_chroma == FCC_RV15 || p_filter->fmt_out.video.i_chroma == FCC_RV16 )
1209                 vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1210                                  (const uint16_t*)&p_dst[i_x * i_pix_pitch],
1211                                   rgbpal[p_src[i_x]][0], rgbpal[p_src[i_x]][1], rgbpal[p_src[i_x]][2],
1212                                   i_trans,
1213                                   &p_filter->fmt_out.video );
1214             else
1215                 vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_dst[i_x * i_pix_pitch],
1216                                   i_rindex, i_gindex, i_bindex,
1217                                   rgbpal[p_src[i_x]][0], rgbpal[p_src[i_x]][1], rgbpal[p_src[i_x]][2],
1218                                   i_trans, true );
1219         }
1220     }
1221
1222 #undef p_pal
1223 #undef rgbpal
1224 }
1225
1226 /***********************************************************************
1227  * RGBA
1228  ***********************************************************************/
1229 static void BlendRGBAI420( filter_t *p_filter,
1230                            picture_t *p_dst, picture_t *p_src_pic,
1231                            int i_x_offset, int i_y_offset,
1232                            int i_width, int i_height, int i_alpha )
1233 {
1234     int i_src_pitch, i_dst_pitch, i_src_pix_pitch;
1235     uint8_t *p_dst_y;
1236     uint8_t *p_dst_u;
1237     uint8_t *p_dst_v;
1238     uint8_t *p_src;
1239     int i_x, i_y, i_trans;
1240     uint8_t y, u, v;
1241
1242     bool b_even_scanline = i_y_offset % 2;
1243
1244     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1245     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1246               p_filter->fmt_out.video.i_x_offset +
1247               p_dst->p[Y_PLANE].i_pitch *
1248               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1249     p_dst_u = p_dst->p[U_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[U_PLANE].i_pitch;
1253     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1254               p_filter->fmt_out.video.i_x_offset/2 +
1255               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1256               p_dst->p[V_PLANE].i_pitch;
1257
1258     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1259     i_src_pitch = p_src_pic->p->i_pitch;
1260     p_src = p_src_pic->p->p_pixels +
1261             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1262             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1263
1264
1265     /* Draw until we reach the bottom of the subtitle */
1266     for( i_y = 0; i_y < i_height; i_y++,
1267          p_dst_y += i_dst_pitch,
1268          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1269          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1270          p_src += i_src_pitch )
1271     {
1272         b_even_scanline = !b_even_scanline;
1273
1274         /* Draw until we reach the end of the line */
1275         for( i_x = 0; i_x < i_width; i_x++ )
1276         {
1277             const int R = p_src[i_x * i_src_pix_pitch + 0];
1278             const int G = p_src[i_x * i_src_pix_pitch + 1];
1279             const int B = p_src[i_x * i_src_pix_pitch + 2];
1280
1281             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1282             if( !i_trans )
1283                 continue;
1284
1285             /* Blending */
1286             rgb_to_yuv( &y, &u, &v, R, G, B );
1287
1288             p_dst_y[i_x] = vlc_blend( y, p_dst_y[i_x], i_trans );
1289             if( b_even_scanline && i_x % 2 == 0 )
1290             {
1291                 p_dst_u[i_x/2] = vlc_blend( u, p_dst_u[i_x/2], i_trans );
1292                 p_dst_v[i_x/2] = vlc_blend( v, p_dst_v[i_x/2], i_trans );
1293             }
1294         }
1295     }
1296 }
1297
1298 static void BlendRGBAR24( filter_t *p_filter,
1299                           picture_t *p_dst_pic, picture_t *p_src_pic,
1300                           int i_x_offset, int i_y_offset,
1301                           int i_width, int i_height, int i_alpha )
1302 {
1303     int i_src_pitch, i_dst_pitch;
1304     uint8_t *p_dst, *p_src;
1305     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1306     int i_rindex, i_gindex, i_bindex;
1307
1308     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1309     i_dst_pitch = p_dst_pic->p->i_pitch;
1310     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1311             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1312             p_dst_pic->p->i_pitch *
1313             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1314
1315     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1316     i_src_pitch = p_src_pic->p->i_pitch;
1317     p_src = p_src_pic->p->p_pixels +
1318             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1319             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1320
1321     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1322
1323     /* Draw until we reach the bottom of the subtitle */
1324     for( i_y = 0; i_y < i_height; i_y++,
1325          p_dst += i_dst_pitch, p_src += i_src_pitch )
1326     {
1327         /* Draw until we reach the end of the line */
1328         for( i_x = 0; i_x < i_width; i_x++ )
1329         {
1330             const int R = p_src[i_x * i_src_pix_pitch + 0];
1331             const int G = p_src[i_x * i_src_pix_pitch + 1];
1332             const int B = p_src[i_x * i_src_pix_pitch + 2];
1333
1334             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1335             if( !i_trans )
1336                 continue;
1337
1338             /* Blending */
1339             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_dst[i_x * i_pix_pitch],
1340                               i_rindex, i_gindex, i_bindex,
1341                               R, G, B, i_trans, true );
1342         }
1343     }
1344 }
1345
1346 static void BlendRGBAR16( filter_t *p_filter,
1347                           picture_t *p_dst_pic, picture_t *p_src_pic,
1348                           int i_x_offset, int i_y_offset,
1349                           int i_width, int i_height, int i_alpha )
1350 {
1351     int i_src_pitch, i_dst_pitch;
1352     uint8_t *p_dst, *p_src;
1353     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1354
1355     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1356     i_dst_pitch = p_dst_pic->p->i_pitch;
1357     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1358             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1359             p_dst_pic->p->i_pitch *
1360             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1361
1362     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1363     i_src_pitch = p_src_pic->p->i_pitch;
1364     p_src = p_src_pic->p->p_pixels +
1365             p_filter->fmt_in.video.i_x_offset * i_src_pix_pitch +
1366             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1367
1368     /* Draw until we reach the bottom of the subtitle */
1369     for( i_y = 0; i_y < i_height; i_y++,
1370          p_dst += i_dst_pitch, p_src += i_src_pitch )
1371     {
1372         /* Draw until we reach the end of the line */
1373         for( i_x = 0; i_x < i_width; i_x++ )
1374         {
1375             const int R = p_src[i_x * i_src_pix_pitch + 0];
1376             const int G = p_src[i_x * i_src_pix_pitch + 1];
1377             const int B = p_src[i_x * i_src_pix_pitch + 2];
1378
1379             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1380             if( !i_trans )
1381                 continue;
1382
1383             /* Blending */
1384             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1385                              (const uint16_t*)&p_dst[i_x * i_pix_pitch],
1386                              R, G, B, i_trans, &p_filter->fmt_out.video );
1387         }
1388     }
1389 }
1390
1391 static void BlendRGBAYUVPacked( filter_t *p_filter,
1392                                 picture_t *p_dst_pic, picture_t *p_src_pic,
1393                                 int i_x_offset, int i_y_offset,
1394                                 int i_width, int i_height, int i_alpha )
1395 {
1396     int i_src_pitch, i_dst_pitch, i_src_pix_pitch;
1397     uint8_t *p_dst, *p_src;
1398     uint8_t *p_trans;
1399     int i_x, i_y, i_pix_pitch, i_trans;
1400     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1401     int i_l_offset, i_u_offset, i_v_offset;
1402     uint8_t y, u, v;
1403
1404     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1405                           p_filter->fmt_out.video.i_chroma );
1406
1407     i_pix_pitch = 2;
1408     i_dst_pitch = p_dst_pic->p->i_pitch;
1409     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1410             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1411             p_dst_pic->p->i_pitch *
1412             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1413
1414     i_src_pix_pitch = p_src_pic->p->i_pixel_pitch;
1415     i_src_pitch = p_src_pic->p->i_pitch;
1416     p_src = p_src_pic->p->p_pixels +
1417             p_filter->fmt_in.video.i_x_offset * i_src_pitch +
1418             p_src_pic->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1419
1420     i_width &= ~1; /* Needs to be a multiple of 2 */
1421
1422     /* Draw until we reach the bottom of the subtitle */
1423     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
1424          p_dst += i_dst_pitch,
1425          p_src += i_src_pitch )
1426     {
1427         /* Draw until we reach the end of the line */
1428         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1429         {
1430             const int R = p_src[i_x * i_src_pix_pitch + 0];
1431             const int G = p_src[i_x * i_src_pix_pitch + 1];
1432             const int B = p_src[i_x * i_src_pix_pitch + 2];
1433
1434             i_trans = vlc_alpha( p_src[i_x * i_src_pix_pitch + 3], i_alpha );
1435             if( !i_trans )
1436                 continue;
1437
1438             /* Blending */
1439             rgb_to_yuv( &y, &u, &v, R, G, B );
1440
1441             vlc_blend_packed( &p_dst[i_x * 2], &p_dst[i_x * 2],
1442                               i_l_offset, i_u_offset, i_v_offset,
1443                               y, u, v, i_trans, b_even );
1444         }
1445     }
1446 }