60874b219df52a4f9abb4fc912dea04b8dea407c
[vlc.git] / 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 *, picture_t *,
77                    int, int, int );
78
79 /* YUVA */
80 static void BlendYUVAI420( filter_t *, picture_t *, picture_t *, picture_t *,
81                            int, int, int, int, int );
82 static void BlendYUVARV16( filter_t *, picture_t *, picture_t *, picture_t *,
83                            int, int, int, int, int );
84 static void BlendYUVARV24( filter_t *, picture_t *, picture_t *, picture_t *,
85                            int, int, int, int, int );
86 static void BlendYUVAYUVPacked( filter_t *, picture_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 *, picture_t *,
91                            int, int, int, int, int );
92 static void BlendI420I420_no_alpha(
93                            filter_t *, picture_t *, picture_t *, picture_t *,
94                            int, int, int, int );
95 static void BlendI420R16( filter_t *, picture_t *, picture_t *, picture_t *,
96                            int, int, int, int, int );
97 static void BlendI420R24( filter_t *, picture_t *, picture_t *, picture_t *,
98                           int, int, int, int, int );
99 static void BlendI420YUVPacked( filter_t *, picture_t *, picture_t *,
100                                 picture_t *, int, int, int, int, int );
101
102 /* YUVP */
103 static void BlendPalI420( filter_t *, picture_t *, picture_t *, picture_t *,
104                           int, int, int, int, int );
105 static void BlendPalYUVPacked( filter_t *, picture_t *, picture_t *, picture_t *,
106                                int, int, int, int, int );
107 static void BlendPalRV( filter_t *, picture_t *, picture_t *, picture_t *,
108                         int, int, int, int, int );
109
110 /* RGBA */
111 static void BlendRGBAI420( filter_t *, picture_t *, picture_t *, picture_t *,
112                            int, int, int, int, int );
113 static void BlendRGBAYUVPacked( filter_t *, picture_t *, picture_t *,
114                                 picture_t *, int, int, int, int, int );
115 static void BlendRGBAR16( filter_t *, picture_t *, picture_t *, picture_t *,
116                           int, int, int, int, int );
117 static void BlendRGBAR24( filter_t *, picture_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 *, picture_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, picture_t *p_dst,
211                    picture_t *p_dst_orig, 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_dst_orig, p_src,
247                                      i_x_offset, i_y_offset, i_width, i_height, i_alpha );
248             return;
249         }
250     }
251
252     msg_Dbg( p_filter, "no matching alpha blending routine "
253              "(chroma: %4.4s -> %4.4s)",
254              (char *)&p_filter->fmt_in.video.i_chroma,
255              (char *)&p_filter->fmt_out.video.i_chroma );
256 }
257
258 /***********************************************************************
259  * Utils
260  ***********************************************************************/
261 static inline uint8_t vlc_uint8( int v )
262 {
263     if( v > 255 )
264         return 255;
265     else if( v < 0 )
266         return 0;
267     return v;
268 }
269
270 #define MAX_TRANS 255
271 #define TRANS_BITS  8
272
273 static inline int vlc_blend( int v1, int v2, int a )
274 {
275     /* TODO bench if the tests really increase speed */
276     if( a == 0 )
277         return v2;
278     else if( a == MAX_TRANS )
279         return v1;
280     return ( v1 * a + v2 * (MAX_TRANS - a ) ) >> TRANS_BITS;
281 }
282
283 static inline int vlc_alpha( int t, int a )
284 {
285     if( a == 255 )
286         return t;
287     return (t * a) / 255;
288 }
289
290 static inline void yuv_to_rgb( int *r, int *g, int *b,
291                                uint8_t y1, uint8_t u1, uint8_t v1 )
292 {
293     /* macros used for YUV pixel conversions */
294 #   define SCALEBITS 10
295 #   define ONE_HALF  (1 << (SCALEBITS - 1))
296 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
297
298     int y, cb, cr, r_add, g_add, b_add;
299
300     cb = u1 - 128;
301     cr = v1 - 128;
302     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
303     g_add = - FIX(0.34414*255.0/224.0) * cb
304             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
305     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
306     y = (y1 - 16) * FIX(255.0/219.0);
307     *r = vlc_uint8( (y + r_add) >> SCALEBITS );
308     *g = vlc_uint8( (y + g_add) >> SCALEBITS );
309     *b = vlc_uint8( (y + b_add) >> SCALEBITS );
310 #undef FIX
311 #undef ONE_HALF
312 #undef SCALEBITS
313 }
314
315 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
316                                int r, int g, int b )
317 {
318     *y = ( ( (  66 * r + 129 * g +  25 * b + 128 ) >> 8 ) + 16 );
319     *u =   ( ( -38 * r -  74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
320     *v =   ( ( 112 * r -  94 * g -  18 * b + 128 ) >> 8 ) + 128 ;
321 }
322
323 static uint8_t *vlc_plane_start( int *pi_pitch,
324                                  picture_t *p_picture,
325                                  int i_plane,
326                                  int i_x_offset, int i_y_offset,
327                                  const video_format_t *p_fmt,
328                                  int r )
329 {
330     const int i_pitch = p_picture->p[i_plane].i_pitch;
331     uint8_t *p_pixels = p_picture->p[i_plane].p_pixels;
332
333     const int i_dx = ( i_x_offset + p_fmt->i_x_offset ) / r;
334     const int i_dy = ( i_y_offset + p_fmt->i_y_offset ) / r;
335
336     if( pi_pitch )
337         *pi_pitch = i_pitch;
338     return &p_pixels[ i_dy * i_pitch + i_dx ];
339 }
340
341 static void vlc_yuv_packed_index( int *pi_y, int *pi_u, int *pi_v, vlc_fourcc_t i_chroma )
342 {
343     static const struct {
344         vlc_fourcc_t chroma;
345         int y, u ,v;
346     } p_index[] = {
347         { FCC_YUY2, 0, 1, 3 },
348         { FCC_UYVY, 1, 0, 2 },
349         { FCC_YVYU, 0, 3, 1 },
350         { 0, 0, 0, 0 }
351     };
352     int i;
353
354     for( i = 0; p_index[i].chroma != 0; i++ )
355     {
356         if( p_index[i].chroma == i_chroma )
357             break;
358     }
359     *pi_y = p_index[i].y;
360     *pi_u = p_index[i].u;
361     *pi_v = p_index[i].v;
362 }
363
364 static void vlc_blend_packed( uint8_t *p_dst, const uint8_t *p_src,
365                               int i_offset0, int i_offset1, int i_offset2,
366                               int c0, int c1, int c2, int i_alpha,
367                               bool b_do12 )
368 {
369     p_dst[i_offset0] = vlc_blend( c0, p_src[i_offset0], i_alpha );
370     if( b_do12 )
371     {
372         p_dst[i_offset1] = vlc_blend( c1, p_src[i_offset1], i_alpha );
373         p_dst[i_offset2] = vlc_blend( c2, p_src[i_offset2], i_alpha );
374     }
375 }
376
377 static void vlc_blend_rgb16( uint16_t *p_dst, const uint16_t *p_src,
378                              int R, int G, int B, int i_alpha,
379                              const video_format_t *p_fmt )
380 {
381     const int i_pix = *p_src;
382     const int r = ( i_pix & p_fmt->i_rmask ) >> p_fmt->i_lrshift;
383     const int g = ( i_pix & p_fmt->i_gmask ) >> p_fmt->i_lgshift;
384     const int b = ( i_pix & p_fmt->i_bmask ) >> p_fmt->i_lbshift;
385
386     *p_dst = ( vlc_blend( R >> p_fmt->i_rrshift, r, i_alpha ) << p_fmt->i_lrshift ) |
387              ( vlc_blend( G >> p_fmt->i_rgshift, g, i_alpha ) << p_fmt->i_lgshift ) |
388              ( vlc_blend( B >> p_fmt->i_rbshift, b, i_alpha ) << p_fmt->i_lbshift );
389 }
390
391 static void vlc_rgb_index( int *pi_rindex, int *pi_gindex, int *pi_bindex,
392                            const video_format_t *p_fmt )
393 {
394     if( p_fmt->i_chroma != FCC_RV24 && p_fmt->i_chroma != FCC_RV32 )
395         return;
396
397     /* XXX it will works only if mask are 8 bits aligned */
398 #ifdef WORDS_BIGENDIAN
399     const int i_mask_bits = p_fmt->i_chroma == FCC_RV24 ? 24 : 32;
400     *pi_rindex = ( i_mask_bits - p_fmt->i_lrshift ) / 8;
401     *pi_gindex = ( i_mask_bits - p_fmt->i_lgshift ) / 8;
402     *pi_bindex = ( i_mask_bits - p_fmt->i_lbshift ) / 8;
403 #else
404     *pi_rindex = p_fmt->i_lrshift / 8;
405     *pi_gindex = p_fmt->i_lgshift / 8;
406     *pi_bindex = p_fmt->i_lbshift / 8;
407 #endif
408 }
409
410 /***********************************************************************
411  * YUVA
412  ***********************************************************************/
413 static void BlendYUVAI420( filter_t *p_filter, picture_t *p_dst,
414                            picture_t *p_dst_orig, picture_t *p_src,
415                            int i_x_offset, int i_y_offset,
416                            int i_width, int i_height, int i_alpha )
417 {
418     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
419     uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
420     uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
421     uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
422     uint8_t *p_trans;
423     int i_x, i_y, i_trans = 0;
424     bool b_even_scanline = i_y_offset % 2;
425
426     p_dst_y = vlc_plane_start( &i_dst_pitch, p_dst, Y_PLANE,
427                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 1 );
428     p_dst_u = vlc_plane_start( NULL, p_dst, U_PLANE,
429                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
430     p_dst_v = vlc_plane_start( NULL, p_dst, V_PLANE,
431                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
432
433     p_src1_y = vlc_plane_start( &i_src1_pitch, p_dst_orig, Y_PLANE,
434                                 i_x_offset, i_y_offset, &p_filter->fmt_out.video, 1 );
435     p_src1_u = vlc_plane_start( NULL, p_dst_orig, U_PLANE,
436                                 i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
437     p_src1_v = vlc_plane_start( NULL, p_dst_orig, V_PLANE,
438                                 i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
439
440     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
441                                 0, 0, &p_filter->fmt_in.video, 1 );
442     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
443                                 0, 0, &p_filter->fmt_in.video, 2 );
444     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
445                                 0, 0, &p_filter->fmt_in.video, 2 );
446     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
447                                0, 0, &p_filter->fmt_in.video, 1 );
448
449     /* Draw until we reach the bottom of the subtitle */
450     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
451          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
452          p_src2_y += i_src2_pitch,
453          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
454          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
455          p_src2_u += i_src2_pitch,
456          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
457          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
458          p_src2_v += i_src2_pitch )
459     {
460         b_even_scanline = !b_even_scanline;
461
462         /* Draw until we reach the end of the line */
463         for( i_x = 0; i_x < i_width; i_x++ )
464         {
465             if( p_trans )
466                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
467
468             if( !i_trans )
469                 continue;
470
471             /* Blending */
472             p_dst_y[i_x] = vlc_blend( p_src2_y[i_x], p_src1_y[i_x], i_trans );
473             if( b_even_scanline && i_x % 2 == 0 )
474             {
475                 p_dst_u[i_x/2] = vlc_blend( p_src2_u[i_x], p_src1_u[i_x/2], i_trans );
476                 p_dst_v[i_x/2] = vlc_blend( p_src2_v[i_x], p_src1_v[i_x/2], i_trans );
477             }
478         }
479     }
480 }
481
482 static void BlendYUVARV16( filter_t *p_filter, picture_t *p_dst_pic,
483                            picture_t *p_dst_orig, picture_t *p_src,
484                            int i_x_offset, int i_y_offset,
485                            int i_width, int i_height, int i_alpha )
486 {
487     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
488     uint8_t *p_dst, *p_src1, *p_src2_y;
489     uint8_t *p_src2_u, *p_src2_v;
490     uint8_t *p_trans;
491     int i_x, i_y, i_pix_pitch, i_trans = 0;
492     int r, g, b;
493
494     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
495     i_dst_pitch = p_dst_pic->p->i_pitch;
496     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
497             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
498             p_dst_pic->p->i_pitch *
499             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
500
501     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
502     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
503                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
504                p_dst_orig->p->i_pitch *
505                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
506
507     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
508                                 0, 0, &p_filter->fmt_in.video, 1 );
509     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
510                                 0, 0, &p_filter->fmt_in.video, 2 );
511     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
512                                 0, 0, &p_filter->fmt_in.video, 2 );
513     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
514                                0, 0, &p_filter->fmt_in.video, 1 );
515
516     /* Draw until we reach the bottom of the subtitle */
517     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
518          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
519          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
520          p_src2_v += i_src2_pitch )
521     {
522         /* Draw until we reach the end of the line */
523         for( i_x = 0; i_x < i_width; i_x++ )
524         {
525             if( p_trans )
526                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
527             if( !i_trans )
528                 continue;
529
530             /* Blending */
531             yuv_to_rgb( &r, &g, &b,
532                         p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
533
534             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
535                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
536                              r, g, b, i_trans, &p_filter->fmt_out.video );
537         }
538     }
539 }
540
541 static void BlendYUVARV24( filter_t *p_filter, picture_t *p_dst_pic,
542                            picture_t *p_dst_orig, picture_t *p_src,
543                            int i_x_offset, int i_y_offset,
544                            int i_width, int i_height, int i_alpha )
545 {
546     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
547     uint8_t *p_dst, *p_src1, *p_src2_y;
548     uint8_t *p_src2_u, *p_src2_v;
549     uint8_t *p_trans;
550     int i_x, i_y, i_pix_pitch, i_trans = 0;
551     int r, g, b;
552
553     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
554     i_dst_pitch = p_dst_pic->p->i_pitch;
555     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
556             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
557             p_dst_pic->p->i_pitch *
558             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
559
560     i_src1_pitch = p_dst_orig->p->i_pitch;
561     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
562                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
563                p_dst_orig->p->i_pitch *
564                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
565
566     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
567                                 0, 0, &p_filter->fmt_in.video, 1 );
568     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
569                                 0, 0, &p_filter->fmt_in.video, 2 );
570     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
571                                 0, 0, &p_filter->fmt_in.video, 2 );
572     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
573                                0, 0, &p_filter->fmt_in.video, 1 );
574
575     if( (i_pix_pitch == 4)
576      && (((((intptr_t)p_dst)|((intptr_t)p_src1)|i_dst_pitch|i_src1_pitch)
577           & 3) == 0) )
578     {
579         /*
580         ** if picture pixels are 32 bits long and lines addresses are 32 bit
581         ** aligned, optimize rendering
582         */
583         uint32_t *p32_dst = (uint32_t *)p_dst;
584         uint32_t i32_dst_pitch = (uint32_t)(i_dst_pitch>>2);
585         uint32_t *p32_src1 = (uint32_t *)p_src1;
586         uint32_t i32_src1_pitch = (uint32_t)(i_src1_pitch>>2);
587
588         int i_rshift, i_gshift, i_bshift;
589         uint32_t i_rmask, i_gmask, i_bmask;
590
591         i_rmask = p_filter->fmt_out.video.i_rmask;
592         i_gmask = p_filter->fmt_out.video.i_gmask;
593         i_bmask = p_filter->fmt_out.video.i_bmask;
594         i_rshift = p_filter->fmt_out.video.i_lrshift;
595         i_gshift = p_filter->fmt_out.video.i_lgshift;
596         i_bshift = p_filter->fmt_out.video.i_lbshift;
597
598         /* Draw until we reach the bottom of the subtitle */
599         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
600              p32_dst += i32_dst_pitch, p32_src1 += i32_src1_pitch,
601              p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
602              p_src2_v += i_src2_pitch )
603         {
604             /* Draw until we reach the end of the line */
605             for( i_x = 0; i_x < i_width; i_x++ )
606             {
607                 if( p_trans )
608                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
609                 if( !i_trans )
610                     continue;
611
612                 if( i_trans == MAX_TRANS )
613                 {
614                     /* Completely opaque. Completely overwrite underlying pixel */
615                     yuv_to_rgb( &r, &g, &b,
616                                 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
617
618                     p32_dst[i_x] = (r<<i_rshift) |
619                                    (g<<i_gshift) |
620                                    (b<<i_bshift);
621                 }
622                 else
623                 {
624                     /* Blending */
625                     uint32_t i_pix_src1 = p32_src1[i_x];
626                     yuv_to_rgb( &r, &g, &b,
627                                 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
628
629                     p32_dst[i_x] = ( vlc_blend( r, (i_pix_src1 & i_rmask)>>i_rshift, i_trans ) << i_rshift ) |
630                                    ( vlc_blend( g, (i_pix_src1 & i_gmask)>>i_gshift, i_trans ) << i_gshift ) |
631                                    ( vlc_blend( b, (i_pix_src1 & i_bmask)>>i_bshift, i_trans ) << i_bshift );
632                 }
633             }
634         }
635     }
636     else
637     {
638         int i_rindex, i_gindex, i_bindex;
639         uint32_t i_rmask, i_gmask, i_bmask;
640
641         i_rmask = p_filter->fmt_out.video.i_rmask;
642         i_gmask = p_filter->fmt_out.video.i_gmask;
643         i_bmask = p_filter->fmt_out.video.i_bmask;
644
645         vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
646
647         /* Draw until we reach the bottom of the subtitle */
648         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
649              p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
650              p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
651              p_src2_v += i_src2_pitch )
652         {
653             /* Draw until we reach the end of the line */
654             for( i_x = 0; i_x < i_width; i_x++ )
655             {
656                 if( p_trans )
657                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
658                 if( !i_trans )
659                     continue;
660
661                 /* Blending */
662                 yuv_to_rgb( &r, &g, &b,
663                             p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
664
665                 vlc_blend_packed( &p_dst[ i_x * i_pix_pitch],
666                                   &p_src1[i_x * i_pix_pitch],
667                                   i_rindex, i_gindex, i_bindex,
668                                   r, g, b, i_alpha, true );
669             }
670         }
671     }
672 }
673
674 static void BlendYUVAYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
675                                 picture_t *p_dst_orig, picture_t *p_src,
676                                 int i_x_offset, int i_y_offset,
677                                 int i_width, int i_height, int i_alpha )
678 {
679     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
680     uint8_t *p_dst, *p_src1, *p_src2_y;
681     uint8_t *p_src2_u, *p_src2_v;
682     uint8_t *p_trans;
683     int i_x, i_y, i_pix_pitch, i_trans = 0;
684     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
685     int i_l_offset, i_u_offset, i_v_offset;
686
687     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
688                           p_filter->fmt_out.video.i_chroma );
689
690     i_pix_pitch = 2;
691     i_dst_pitch = p_dst_pic->p->i_pitch;
692     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
693             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
694             p_dst_pic->p->i_pitch *
695             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
696
697     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
698     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
699                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
700                p_dst_orig->p->i_pitch *
701                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
702
703     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
704                                 0, 0, &p_filter->fmt_in.video, 1 );
705     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
706                                 0, 0, &p_filter->fmt_in.video, 2 );
707     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
708                                 0, 0, &p_filter->fmt_in.video, 2 );
709     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
710                                0, 0, &p_filter->fmt_in.video, 1 );
711
712     i_width &= ~1; /* Needs to be a multiple of 2 */
713
714     /* Draw until we reach the bottom of the subtitle */
715     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
716          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
717          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
718          p_src2_v += i_src2_pitch )
719     {
720         /* Draw until we reach the end of the line */
721         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
722         {
723             i_trans = vlc_alpha( p_trans[i_x], i_alpha );
724             if( !i_trans )
725                 continue;
726
727             /* Blending */
728             if( b_even )
729             {
730                 int i_u;
731                 int i_v;
732                 /* FIXME what's with 0xaa ? */
733                 if( p_trans[i_x+1] > 0xaa )
734                 {
735                     i_u = (p_src2_u[i_x]+p_src2_u[i_x+1])>>1;
736                     i_v = (p_src2_v[i_x]+p_src2_v[i_x+1])>>1;
737                 }
738                 else
739                 {
740                     i_u = p_src2_u[i_x];
741                     i_v = p_src2_v[i_x];
742                 }
743
744                 vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
745                                   i_l_offset, i_u_offset, i_v_offset,
746                                   p_src2_y[i_x], i_u, i_v, i_trans, true );
747             }
748             else
749             {
750                 p_dst[i_x * 2 + i_l_offset] = vlc_blend( p_src2_y[i_x], p_src1[i_x * 2 + i_l_offset], i_trans );
751             }
752         }
753     }
754 }
755 /***********************************************************************
756  * I420, YV12
757  ***********************************************************************/
758 static void BlendI420I420( filter_t *p_filter, picture_t *p_dst,
759                            picture_t *p_dst_orig, picture_t *p_src,
760                            int i_x_offset, int i_y_offset,
761                            int i_width, int i_height, int i_alpha )
762 {
763     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
764     uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
765     uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
766     uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
767     int i_x, i_y;
768     bool b_even_scanline = i_y_offset % 2;
769
770     if( i_alpha == 0xff )
771     {
772         BlendI420I420_no_alpha( p_filter, p_dst, p_dst_orig, p_src,
773                                 i_x_offset, i_y_offset, i_width, i_height );
774         return;
775     }
776
777
778     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
779     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
780               p_filter->fmt_out.video.i_x_offset +
781               p_dst->p[Y_PLANE].i_pitch *
782               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
783     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
784               p_filter->fmt_out.video.i_x_offset/2 +
785               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
786               p_dst->p[U_PLANE].i_pitch;
787     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
788               p_filter->fmt_out.video.i_x_offset/2 +
789               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
790               p_dst->p[V_PLANE].i_pitch;
791
792     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
793     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
794                p_filter->fmt_out.video.i_x_offset +
795                p_dst_orig->p[Y_PLANE].i_pitch *
796                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
797     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
798                p_filter->fmt_out.video.i_x_offset/2 +
799                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
800                p_dst_orig->p[U_PLANE].i_pitch;
801     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
802                p_filter->fmt_out.video.i_x_offset/2 +
803                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
804                p_dst_orig->p[V_PLANE].i_pitch;
805
806     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
807                                 0, 0, &p_filter->fmt_in.video, 1 );
808     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
809                                 0, 0, &p_filter->fmt_in.video, 2 );
810     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
811                                 0, 0, &p_filter->fmt_in.video, 2 );
812     i_width &= ~1;
813
814     /* Draw until we reach the bottom of the subtitle */
815     for( i_y = 0; i_y < i_height; i_y++,
816          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
817          p_src2_y += i_src2_pitch )
818     {
819         if( b_even_scanline )
820         {
821             p_dst_u  += i_dst_pitch/2;
822             p_dst_v  += i_dst_pitch/2;
823             p_src1_u += i_src1_pitch/2;
824             p_src1_v += i_src1_pitch/2;
825         }
826         b_even_scanline = !b_even_scanline;
827
828         /* Draw until we reach the end of the line */
829         for( i_x = 0; i_x < i_width; i_x++ )
830         {
831             if( !i_alpha )
832                 continue;
833
834             /* Blending */
835             p_dst_y[i_x] = vlc_blend( p_src2_y[i_x], p_src1_y[i_x], i_alpha );
836             if( b_even_scanline && i_x % 2 == 0 )
837             {
838                 p_dst_u[i_x/2] = vlc_blend( p_src2_u[i_x/2], p_src1_u[i_x/2], i_alpha );
839                 p_dst_v[i_x/2] = vlc_blend( p_src2_v[i_x/2], p_src1_v[i_x/2], i_alpha );
840             }
841         }
842         if( i_y%2 == 1 )
843         {
844             p_src2_u += i_src2_pitch/2;
845             p_src2_v += i_src2_pitch/2;
846         }
847     }
848 }
849 static void BlendI420I420_no_alpha( filter_t *p_filter, picture_t *p_dst,
850                                     picture_t *p_dst_orig, picture_t *p_src,
851                                     int i_x_offset, int i_y_offset,
852                                     int i_width, int i_height )
853 {
854     int i_src2_pitch, i_dst_pitch;
855     uint8_t *p_src2_y, *p_dst_y;
856     uint8_t *p_src2_u, *p_dst_u;
857     uint8_t *p_src2_v, *p_dst_v;
858     int i_y;
859     bool b_even_scanline = i_y_offset % 2;
860
861     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
862     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
863               p_filter->fmt_out.video.i_x_offset +
864               p_dst->p[Y_PLANE].i_pitch *
865               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
866     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
867               p_filter->fmt_out.video.i_x_offset/2 +
868               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
869               p_dst->p[U_PLANE].i_pitch;
870     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
871               p_filter->fmt_out.video.i_x_offset/2 +
872               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
873               p_dst->p[V_PLANE].i_pitch;
874
875     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
876                                 0, 0, &p_filter->fmt_in.video, 1 );
877     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
878                                 0, 0, &p_filter->fmt_in.video, 2 );
879     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
880                                 0, 0, &p_filter->fmt_in.video, 2 );
881
882     i_width &= ~1;
883
884     /* Draw until we reach the bottom of the subtitle */
885     for( i_y = 0; i_y < i_height;
886             i_y++, p_dst_y += i_dst_pitch, p_src2_y += i_src2_pitch )
887     {
888         /* Completely opaque. Completely overwrite underlying pixel */
889         vlc_memcpy( p_dst_y, p_src2_y, i_width );
890         if( b_even_scanline )
891         {
892             p_dst_u  += i_dst_pitch/2;
893             p_dst_v  += i_dst_pitch/2;
894         }
895         else
896         {
897             vlc_memcpy( p_dst_u, p_src2_u, i_width/2 );
898             vlc_memcpy( p_dst_v, p_src2_v, i_width/2 );
899         }
900         b_even_scanline = !b_even_scanline;
901         if( i_y%2 == 1 )
902         {
903             p_src2_u += i_src2_pitch/2;
904             p_src2_v += i_src2_pitch/2;
905         }
906     }
907 }
908
909 static void BlendI420R16( filter_t *p_filter, picture_t *p_dst_pic,
910                           picture_t *p_dst_orig, picture_t *p_src,
911                           int i_x_offset, int i_y_offset,
912                           int i_width, int i_height, int i_alpha )
913 {
914     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
915     uint8_t *p_dst, *p_src1, *p_src2_y;
916     uint8_t *p_src2_u, *p_src2_v;
917     int i_x, i_y, i_pix_pitch;
918     int r, g, b;
919
920     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
921     i_dst_pitch = p_dst_pic->p->i_pitch;
922     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
923             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
924             p_dst_pic->p->i_pitch *
925             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
926
927     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
928     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
929                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
930                p_dst_orig->p->i_pitch *
931                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
932
933     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
934                                 0, 0, &p_filter->fmt_in.video, 1 );
935     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
936                                 0, 0, &p_filter->fmt_in.video, 2 );
937     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
938                                 0, 0, &p_filter->fmt_in.video, 2 );
939
940     /* Draw until we reach the bottom of the subtitle */
941     for( i_y = 0; i_y < i_height; i_y++,
942          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
943          p_src2_y += i_src2_pitch )
944     {
945         /* Draw until we reach the end of the line */
946         for( i_x = 0; i_x < i_width; i_x++ )
947         {
948             /* Blending */
949             yuv_to_rgb( &r, &g, &b,
950                         p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2] );
951
952             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
953                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
954                              r, g, b, 0xff, &p_filter->fmt_out.video );
955         }
956         if( i_y%2 == 1 )
957         {
958             p_src2_u += i_src2_pitch/2;
959             p_src2_v += i_src2_pitch/2;
960         }
961     }
962 }
963
964 static void BlendI420R24( filter_t *p_filter, picture_t *p_dst_pic,
965                           picture_t *p_dst_orig, picture_t *p_src,
966                           int i_x_offset, int i_y_offset,
967                           int i_width, int i_height, int i_alpha )
968 {
969     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
970     uint8_t *p_dst, *p_src1, *p_src2_y;
971     uint8_t *p_src2_u, *p_src2_v;
972     int i_x, i_y, i_pix_pitch;
973     int i_rindex, i_gindex, i_bindex;
974     int r, g, b;
975
976     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
977     i_dst_pitch = p_dst_pic->p->i_pitch;
978     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
979             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
980             p_dst_pic->p->i_pitch *
981             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
982
983     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
984     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
985                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
986                p_dst_orig->p->i_pitch *
987                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
988
989     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
990                                 0, 0, &p_filter->fmt_in.video, 1 );
991     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
992                                 0, 0, &p_filter->fmt_in.video, 2 );
993     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
994                                 0, 0, &p_filter->fmt_in.video, 2 );
995
996     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
997
998     /* Draw until we reach the bottom of the subtitle */
999     for( i_y = 0; i_y < i_height; i_y++,
1000          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1001          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
1002          p_src2_v += i_src2_pitch )
1003     {
1004         /* Draw until we reach the end of the line */
1005         for( i_x = 0; i_x < i_width; i_x++ )
1006         {
1007             if( !i_alpha )
1008                 continue;
1009
1010             /* Blending */
1011             yuv_to_rgb( &r, &g, &b,
1012                         p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2] );
1013
1014             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1015                               i_rindex, i_gindex, i_bindex, r, g, b, i_alpha, true );
1016         }
1017         if( i_y%2 == 1 )
1018         {
1019             p_src2_u += i_src2_pitch/2;
1020             p_src2_v += i_src2_pitch/2;
1021         }
1022     }
1023 }
1024
1025 static void BlendI420YUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1026                                 picture_t *p_dst_orig, picture_t *p_src,
1027                                 int i_x_offset, int i_y_offset,
1028                                 int i_width, int i_height, int i_alpha )
1029 {
1030     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1031     uint8_t *p_dst, *p_src1, *p_src2_y;
1032     uint8_t *p_src2_u, *p_src2_v;
1033     int i_x, i_y, i_pix_pitch;
1034     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1035     int i_l_offset, i_u_offset, i_v_offset;
1036
1037     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1038                           p_filter->fmt_out.video.i_chroma );
1039
1040     i_pix_pitch = 2;
1041     i_dst_pitch = p_dst_pic->p->i_pitch;
1042     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1043             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1044             p_dst_pic->p->i_pitch *
1045             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1046
1047     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1048     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1049                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1050                p_dst_orig->p->i_pitch *
1051                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1052
1053     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
1054                                 0, 0, &p_filter->fmt_in.video, 1 );
1055     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
1056                                 0, 0, &p_filter->fmt_in.video, 2 );
1057     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
1058                                 0, 0, &p_filter->fmt_in.video, 2 );
1059
1060     i_width &= ~1; /* Needs to be a multiple of 2 */
1061
1062     /* Draw until we reach the bottom of the subtitle */
1063     for( i_y = 0; i_y < i_height; i_y++,
1064          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1065          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
1066          p_src2_v += i_src2_pitch )
1067     {
1068         /* Draw until we reach the end of the line */
1069         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1070         {
1071             if( !i_alpha )
1072                 continue;
1073
1074             /* Blending */
1075             vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1076                               i_l_offset, i_u_offset, i_v_offset,
1077                               p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2], i_alpha, b_even );
1078         }
1079         if( i_y%2 == 1 )
1080         {
1081             p_src2_u += i_src2_pitch/2;
1082             p_src2_v += i_src2_pitch/2;
1083         }
1084     }
1085 }
1086
1087 /***********************************************************************
1088  * YUVP
1089  ***********************************************************************/
1090 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
1091                           picture_t *p_dst_orig, picture_t *p_src,
1092                           int i_x_offset, int i_y_offset,
1093                           int i_width, int i_height, int i_alpha )
1094 {
1095     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1096     uint8_t *p_src1_y, *p_src2, *p_dst_y;
1097     uint8_t *p_src1_u, *p_dst_u;
1098     uint8_t *p_src1_v, *p_dst_v;
1099     int i_x, i_y, i_trans;
1100     bool b_even_scanline = i_y_offset % 2;
1101
1102     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1103     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1104               p_filter->fmt_out.video.i_x_offset +
1105               p_dst->p[Y_PLANE].i_pitch *
1106               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1107     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1108               p_filter->fmt_out.video.i_x_offset/2 +
1109               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1110               p_dst->p[U_PLANE].i_pitch;
1111     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1112               p_filter->fmt_out.video.i_x_offset/2 +
1113               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1114               p_dst->p[V_PLANE].i_pitch;
1115
1116     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1117     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1118                p_filter->fmt_out.video.i_x_offset +
1119                p_dst_orig->p[Y_PLANE].i_pitch *
1120                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1121     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1122                p_filter->fmt_out.video.i_x_offset/2 +
1123                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1124                p_dst_orig->p[U_PLANE].i_pitch;
1125     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1126                p_filter->fmt_out.video.i_x_offset/2 +
1127                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1128                p_dst_orig->p[V_PLANE].i_pitch;
1129
1130     i_src2_pitch = p_src->p->i_pitch;
1131     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1132              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1133
1134 #define p_pal p_filter->fmt_in.video.p_palette->palette
1135
1136     /* Draw until we reach the bottom of the subtitle */
1137     for( i_y = 0; i_y < i_height; i_y++,
1138          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1139          p_src2 += i_src2_pitch,
1140          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1141          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1142          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1143          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
1144     {
1145         const uint8_t *p_trans = p_src2;
1146         b_even_scanline = !b_even_scanline;
1147
1148         /* Draw until we reach the end of the line */
1149         for( i_x = 0; i_x < i_width; i_x++ )
1150         {
1151             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1152             if( !i_trans )
1153                 continue;
1154
1155             /* Blending */
1156             p_dst_y[i_x] = vlc_blend( p_pal[p_src2[i_x]][0], p_src1_y[i_x], i_trans );
1157             if( b_even_scanline && ((i_x % 2) == 0) )
1158             {
1159                 p_dst_u[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][1], p_src1_u[i_x/2], i_trans );
1160                 p_dst_v[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][2], p_src1_v[i_x/2], i_trans );
1161             }
1162         }
1163     }
1164 #undef p_pal
1165 }
1166
1167 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1168                                picture_t *p_dst_orig, picture_t *p_src,
1169                                int i_x_offset, int i_y_offset,
1170                                int i_width, int i_height, int i_alpha )
1171 {
1172     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1173     uint8_t *p_src1, *p_src2, *p_dst;
1174     int i_x, i_y, i_pix_pitch, i_trans;
1175     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1176     int i_l_offset, i_u_offset, i_v_offset;
1177
1178     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1179                           p_filter->fmt_out.video.i_chroma );
1180
1181     i_pix_pitch = 2;
1182     i_dst_pitch = p_dst_pic->p->i_pitch;
1183     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1184             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1185             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1186
1187     i_src1_pitch = p_dst_orig->p->i_pitch;
1188     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1189              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1190              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1191
1192     i_src2_pitch = p_src->p->i_pitch;
1193     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1194              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1195
1196     i_width &= ~1; /* Needs to be a multiple of 2 */
1197
1198 #define p_pal p_filter->fmt_in.video.p_palette->palette
1199
1200     /* Draw until we reach the bottom of the subtitle */
1201     for( i_y = 0; i_y < i_height; i_y++,
1202          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1203     {
1204         const uint8_t *p_trans = p_src2;
1205         /* Draw until we reach the end of the line */
1206         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1207         {
1208             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1209             if( !i_trans )
1210                 continue;
1211
1212             /* Blending */
1213             if( b_even )
1214             {
1215                 uint16_t i_u;
1216                 uint16_t i_v;
1217                 if( p_trans[i_x+1] > 0xaa )
1218                 {
1219                     i_u = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
1220                     i_v = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
1221                 }
1222                 else
1223                 {
1224                     i_u = p_pal[p_src2[i_x]][1];
1225                     i_v = p_pal[p_src2[i_x]][2];
1226                 }
1227
1228                 vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1229                                   i_l_offset, i_u_offset, i_v_offset,
1230                                   p_pal[p_src2[i_x]][0], i_u, i_v, i_trans, true );
1231             }
1232             else
1233             {
1234                 p_dst[i_x * 2 + i_l_offset] = vlc_blend( p_pal[p_src2[i_x]][0], p_src1[i_x * 2 + i_l_offset], i_trans );
1235             }
1236         }
1237     }
1238 #undef p_pal
1239 }
1240
1241 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
1242                         picture_t *p_dst_orig, picture_t *p_src,
1243                         int i_x_offset, int i_y_offset,
1244                         int i_width, int i_height, int i_alpha )
1245 {
1246     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1247     uint8_t *p_src1, *p_src2, *p_dst;
1248     int i_x, i_y, i_pix_pitch, i_trans;
1249     int r, g, b;
1250     video_palette_t rgbpalette;
1251     int i_rindex, i_gindex, i_bindex;
1252
1253     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1254     i_dst_pitch = p_dst_pic->p->i_pitch;
1255     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1256             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1257             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1258
1259     i_src1_pitch = p_dst_orig->p->i_pitch;
1260     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1261              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1262              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1263
1264     i_src2_pitch = p_src->p->i_pitch;
1265     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1266              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1267
1268 #define p_pal p_filter->fmt_in.video.p_palette->palette
1269 #define rgbpal rgbpalette.palette
1270
1271     /* Convert palette first */
1272     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries && i_y < 256; i_y++ )
1273     {
1274         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
1275         rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
1276     }
1277
1278     /* */
1279     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1280
1281     /* Draw until we reach the bottom of the subtitle */
1282     for( i_y = 0; i_y < i_height; i_y++,
1283          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1284     {
1285         const uint8_t *p_trans = p_src2;
1286         /* Draw until we reach the end of the line */
1287         for( i_x = 0; i_x < i_width; i_x++ )
1288         {
1289             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1290             if( !i_trans )
1291                 continue;
1292
1293             /* Blending */
1294             if( p_filter->fmt_out.video.i_chroma == FCC_RV15 || p_filter->fmt_out.video.i_chroma == FCC_RV16 )
1295                 vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1296                                  (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1297                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1298                                   i_trans,
1299                                   &p_filter->fmt_out.video );
1300             else
1301                 vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1302                                   i_rindex, i_gindex, i_bindex,
1303                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1304                                   i_trans, true );
1305         }
1306     }
1307
1308 #undef p_pal
1309 #undef rgbpal
1310 }
1311
1312 /***********************************************************************
1313  * RGBA
1314  ***********************************************************************/
1315 static void BlendRGBAI420( filter_t *p_filter, picture_t *p_dst,
1316                            picture_t *p_dst_orig, picture_t *p_src,
1317                            int i_x_offset, int i_y_offset,
1318                            int i_width, int i_height, int i_alpha )
1319 {
1320     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1321     uint8_t *p_src1_y, *p_dst_y;
1322     uint8_t *p_src1_u, *p_dst_u;
1323     uint8_t *p_src1_v, *p_dst_v;
1324     uint8_t *p_src2;
1325     int i_x, i_y, i_trans;
1326     uint8_t y, u, v;
1327
1328     bool b_even_scanline = i_y_offset % 2;
1329
1330     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1331     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1332               p_filter->fmt_out.video.i_x_offset +
1333               p_dst->p[Y_PLANE].i_pitch *
1334               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1335     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1336               p_filter->fmt_out.video.i_x_offset/2 +
1337               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1338               p_dst->p[U_PLANE].i_pitch;
1339     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1340               p_filter->fmt_out.video.i_x_offset/2 +
1341               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1342               p_dst->p[V_PLANE].i_pitch;
1343
1344     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1345     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1346                p_filter->fmt_out.video.i_x_offset +
1347                p_dst_orig->p[Y_PLANE].i_pitch *
1348                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1349     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1350                p_filter->fmt_out.video.i_x_offset/2 +
1351                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1352                p_dst_orig->p[U_PLANE].i_pitch;
1353     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1354                p_filter->fmt_out.video.i_x_offset/2 +
1355                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1356                p_dst_orig->p[V_PLANE].i_pitch;
1357
1358     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1359     i_src2_pitch = p_src->p->i_pitch;
1360     p_src2 = p_src->p->p_pixels +
1361              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1362              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1363
1364
1365     /* Draw until we reach the bottom of the subtitle */
1366     for( i_y = 0; i_y < i_height; i_y++,
1367          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1368          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1369          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1370          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1371          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
1372          p_src2 += i_src2_pitch )
1373     {
1374         b_even_scanline = !b_even_scanline;
1375
1376         /* Draw until we reach the end of the line */
1377         for( i_x = 0; i_x < i_width; i_x++ )
1378         {
1379             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1380             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1381             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1382
1383             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1384             if( !i_trans )
1385                 continue;
1386
1387             /* Blending */
1388             rgb_to_yuv( &y, &u, &v, R, G, B );
1389
1390             p_dst_y[i_x] = vlc_blend( y, p_src1_y[i_x], i_trans );
1391             if( b_even_scanline && i_x % 2 == 0 )
1392             {
1393                 p_dst_u[i_x/2] = vlc_blend( u, p_src1_u[i_x/2], i_trans );
1394                 p_dst_v[i_x/2] = vlc_blend( v, p_src1_v[i_x/2], i_trans );
1395             }
1396         }
1397     }
1398 }
1399
1400 static void BlendRGBAR24( filter_t *p_filter, picture_t *p_dst_pic,
1401                           picture_t *p_dst_orig, picture_t *p_src,
1402                           int i_x_offset, int i_y_offset,
1403                           int i_width, int i_height, int i_alpha )
1404 {
1405     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1406     uint8_t *p_dst, *p_src1, *p_src2;
1407     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1408     int i_rindex, i_gindex, i_bindex;
1409
1410     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1411     i_dst_pitch = p_dst_pic->p->i_pitch;
1412     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1413             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1414             p_dst_pic->p->i_pitch *
1415             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1416
1417     i_src1_pitch = p_dst_orig->p->i_pitch;
1418     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1419              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1420              p_dst_orig->p->i_pitch *
1421              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1422
1423     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1424     i_src2_pitch = p_src->p->i_pitch;
1425     p_src2 = p_src->p->p_pixels +
1426              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1427              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1428
1429     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1430
1431     /* Draw until we reach the bottom of the subtitle */
1432     for( i_y = 0; i_y < i_height; i_y++,
1433          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1434     {
1435         /* Draw until we reach the end of the line */
1436         for( i_x = 0; i_x < i_width; i_x++ )
1437         {
1438             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1439             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1440             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1441
1442             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1443             if( !i_trans )
1444                 continue;
1445
1446             /* Blending */
1447             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1448                               i_rindex, i_gindex, i_bindex,
1449                               R, G, B, i_trans, true );
1450         }
1451     }
1452 }
1453
1454 static void BlendRGBAR16( filter_t *p_filter, picture_t *p_dst_pic,
1455                           picture_t *p_dst_orig, picture_t *p_src,
1456                           int i_x_offset, int i_y_offset,
1457                           int i_width, int i_height, int i_alpha )
1458 {
1459     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1460     uint8_t *p_dst, *p_src1, *p_src2;
1461     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1462
1463     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1464     i_dst_pitch = p_dst_pic->p->i_pitch;
1465     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1466             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1467             p_dst_pic->p->i_pitch *
1468             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1469
1470     i_src1_pitch = p_dst_orig->p->i_pitch;
1471     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1472              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1473              p_dst_orig->p->i_pitch *
1474              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1475
1476     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1477     i_src2_pitch = p_src->p->i_pitch;
1478     p_src2 = p_src->p->p_pixels +
1479              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1480              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1481
1482     /* Draw until we reach the bottom of the subtitle */
1483     for( i_y = 0; i_y < i_height; i_y++,
1484          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1485     {
1486         /* Draw until we reach the end of the line */
1487         for( i_x = 0; i_x < i_width; i_x++ )
1488         {
1489             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1490             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1491             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1492
1493             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1494             if( !i_trans )
1495                 continue;
1496
1497             /* Blending */
1498             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1499                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1500                              R, G, B, i_trans, &p_filter->fmt_out.video );
1501         }
1502     }
1503 }
1504
1505 static void BlendRGBAYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1506                                 picture_t *p_dst_orig, picture_t *p_src,
1507                                 int i_x_offset, int i_y_offset,
1508                                 int i_width, int i_height, int i_alpha )
1509 {
1510     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1511     uint8_t *p_dst, *p_src1, *p_src2;
1512     uint8_t *p_trans;
1513     int i_x, i_y, i_pix_pitch, i_trans;
1514     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1515     int i_l_offset, i_u_offset, i_v_offset;
1516     uint8_t y, u, v;
1517
1518     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1519                           p_filter->fmt_out.video.i_chroma );
1520
1521     i_pix_pitch = 2;
1522     i_dst_pitch = p_dst_pic->p->i_pitch;
1523     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1524             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1525             p_dst_pic->p->i_pitch *
1526             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1527
1528     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1529     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1530                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1531                p_dst_orig->p->i_pitch *
1532                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1533
1534     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1535     i_src2_pitch = p_src->p->i_pitch;
1536     p_src2 = p_src->p->p_pixels +
1537              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1538              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1539
1540     i_width &= ~1; /* Needs to be a multiple of 2 */
1541
1542     /* Draw until we reach the bottom of the subtitle */
1543     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
1544          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1545          p_src2 += i_src2_pitch )
1546     {
1547         /* Draw until we reach the end of the line */
1548         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1549         {
1550             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1551             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1552             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1553
1554             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1555             if( !i_trans )
1556                 continue;
1557
1558             /* Blending */
1559             rgb_to_yuv( &y, &u, &v, R, G, B );
1560
1561             vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1562                               i_l_offset, i_u_offset, i_v_offset,
1563                               y, u, v, i_trans, b_even );
1564         }
1565     }
1566 }