]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
Fixed YUVP blending (my bad for not testing it).
[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 *, 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     fprintf( stderr, "--------------\n" );
1103
1104     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1105     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1106               p_filter->fmt_out.video.i_x_offset +
1107               p_dst->p[Y_PLANE].i_pitch *
1108               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1109     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1110               p_filter->fmt_out.video.i_x_offset/2 +
1111               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1112               p_dst->p[U_PLANE].i_pitch;
1113     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1114               p_filter->fmt_out.video.i_x_offset/2 +
1115               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1116               p_dst->p[V_PLANE].i_pitch;
1117
1118     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1119     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1120                p_filter->fmt_out.video.i_x_offset +
1121                p_dst_orig->p[Y_PLANE].i_pitch *
1122                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1123     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1124                p_filter->fmt_out.video.i_x_offset/2 +
1125                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1126                p_dst_orig->p[U_PLANE].i_pitch;
1127     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1128                p_filter->fmt_out.video.i_x_offset/2 +
1129                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1130                p_dst_orig->p[V_PLANE].i_pitch;
1131
1132     i_src2_pitch = p_src->p->i_pitch;
1133     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1134              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1135
1136 #define p_pal p_filter->fmt_in.video.p_palette->palette
1137
1138     /* Draw until we reach the bottom of the subtitle */
1139     for( i_y = 0; i_y < i_height; i_y++,
1140          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1141          p_src2 += i_src2_pitch,
1142          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1143          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1144          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1145          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
1146     {
1147         const uint8_t *p_trans = p_src2;
1148         b_even_scanline = !b_even_scanline;
1149
1150         /* Draw until we reach the end of the line */
1151         for( i_x = 0; i_x < i_width; i_x++ )
1152         {
1153             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1154             if( !i_trans )
1155                 continue;
1156
1157             /* Blending */
1158             p_dst_y[i_x] = vlc_blend( p_pal[p_src2[i_x]][0], p_src1_y[i_x], i_trans );
1159             if( b_even_scanline && ((i_x % 2) == 0) )
1160             {
1161                 p_dst_u[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][1], p_src1_u[i_x/2], i_trans );
1162                 p_dst_v[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][2], p_src1_v[i_x/2], i_trans );
1163             }
1164         }
1165     }
1166 #undef p_pal
1167 }
1168
1169 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1170                                picture_t *p_dst_orig, picture_t *p_src,
1171                                int i_x_offset, int i_y_offset,
1172                                int i_width, int i_height, int i_alpha )
1173 {
1174     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1175     uint8_t *p_src1, *p_src2, *p_dst;
1176     int i_x, i_y, i_pix_pitch, i_trans;
1177     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1178     int i_l_offset, i_u_offset, i_v_offset;
1179
1180     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1181                           p_filter->fmt_out.video.i_chroma );
1182
1183     i_pix_pitch = 2;
1184     i_dst_pitch = p_dst_pic->p->i_pitch;
1185     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1186             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1187             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1188
1189     i_src1_pitch = p_dst_orig->p->i_pitch;
1190     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1191              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1192              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1193
1194     i_src2_pitch = p_src->p->i_pitch;
1195     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1196              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1197
1198     i_width &= ~1; /* Needs to be a multiple of 2 */
1199
1200 #define p_pal p_filter->fmt_in.video.p_palette->palette
1201
1202     /* Draw until we reach the bottom of the subtitle */
1203     for( i_y = 0; i_y < i_height; i_y++,
1204          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1205     {
1206         const uint8_t *p_trans = p_src2;
1207         /* Draw until we reach the end of the line */
1208         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1209         {
1210             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1211             if( !i_trans )
1212                 continue;
1213
1214             /* Blending */
1215             if( b_even )
1216             {
1217                 uint16_t i_u;
1218                 uint16_t i_v;
1219                 if( p_trans[i_x+1] > 0xaa )
1220                 {
1221                     i_u = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
1222                     i_v = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
1223                 }
1224                 else
1225                 {
1226                     i_u = p_pal[p_src2[i_x]][1];
1227                     i_v = p_pal[p_src2[i_x]][2];
1228                 }
1229
1230                 vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1231                                   i_l_offset, i_u_offset, i_v_offset,
1232                                   p_pal[p_src2[i_x]][0], i_u, i_v, i_trans, true );
1233             }
1234             else
1235             {
1236                 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 );
1237             }
1238         }
1239     }
1240 #undef p_pal
1241 }
1242
1243 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
1244                         picture_t *p_dst_orig, picture_t *p_src,
1245                         int i_x_offset, int i_y_offset,
1246                         int i_width, int i_height, int i_alpha )
1247 {
1248     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1249     uint8_t *p_src1, *p_src2, *p_dst;
1250     int i_x, i_y, i_pix_pitch, i_trans;
1251     int r, g, b;
1252     video_palette_t rgbpalette;
1253     int i_rindex, i_gindex, i_bindex;
1254
1255     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1256     i_dst_pitch = p_dst_pic->p->i_pitch;
1257     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1258             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1259             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1260
1261     i_src1_pitch = p_dst_orig->p->i_pitch;
1262     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1263              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1264              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1265
1266     i_src2_pitch = p_src->p->i_pitch;
1267     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1268              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1269
1270 #define p_pal p_filter->fmt_in.video.p_palette->palette
1271 #define rgbpal rgbpalette.palette
1272
1273     /* Convert palette first */
1274     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries && i_y < 256; i_y++ )
1275     {
1276         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
1277         rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
1278     }
1279
1280     /* */
1281     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1282
1283     /* Draw until we reach the bottom of the subtitle */
1284     for( i_y = 0; i_y < i_height; i_y++,
1285          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1286     {
1287         const uint8_t *p_trans = p_src2;
1288         /* Draw until we reach the end of the line */
1289         for( i_x = 0; i_x < i_width; i_x++ )
1290         {
1291             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1292             if( !i_trans )
1293                 continue;
1294
1295             /* Blending */
1296             if( p_filter->fmt_out.video.i_chroma == FCC_RV15 || p_filter->fmt_out.video.i_chroma == FCC_RV16 )
1297                 vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1298                                  (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1299                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1300                                   i_trans,
1301                                   &p_filter->fmt_out.video );
1302             else
1303                 vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1304                                   i_rindex, i_gindex, i_bindex,
1305                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1306                                   i_trans, true );
1307         }
1308     }
1309
1310 #undef p_pal
1311 #undef rgbpal
1312 }
1313
1314 /***********************************************************************
1315  * RGBA
1316  ***********************************************************************/
1317 static void BlendRGBAI420( filter_t *p_filter, picture_t *p_dst,
1318                            picture_t *p_dst_orig, picture_t *p_src,
1319                            int i_x_offset, int i_y_offset,
1320                            int i_width, int i_height, int i_alpha )
1321 {
1322     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1323     uint8_t *p_src1_y, *p_dst_y;
1324     uint8_t *p_src1_u, *p_dst_u;
1325     uint8_t *p_src1_v, *p_dst_v;
1326     uint8_t *p_src2;
1327     int i_x, i_y, i_trans;
1328     uint8_t y, u, v;
1329
1330     bool b_even_scanline = i_y_offset % 2;
1331
1332     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1333     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1334               p_filter->fmt_out.video.i_x_offset +
1335               p_dst->p[Y_PLANE].i_pitch *
1336               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1337     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1338               p_filter->fmt_out.video.i_x_offset/2 +
1339               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1340               p_dst->p[U_PLANE].i_pitch;
1341     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1342               p_filter->fmt_out.video.i_x_offset/2 +
1343               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1344               p_dst->p[V_PLANE].i_pitch;
1345
1346     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1347     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1348                p_filter->fmt_out.video.i_x_offset +
1349                p_dst_orig->p[Y_PLANE].i_pitch *
1350                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1351     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1352                p_filter->fmt_out.video.i_x_offset/2 +
1353                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1354                p_dst_orig->p[U_PLANE].i_pitch;
1355     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1356                p_filter->fmt_out.video.i_x_offset/2 +
1357                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1358                p_dst_orig->p[V_PLANE].i_pitch;
1359
1360     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1361     i_src2_pitch = p_src->p->i_pitch;
1362     p_src2 = p_src->p->p_pixels +
1363              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1364              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1365
1366
1367     /* Draw until we reach the bottom of the subtitle */
1368     for( i_y = 0; i_y < i_height; i_y++,
1369          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1370          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1371          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1372          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1373          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
1374          p_src2 += i_src2_pitch )
1375     {
1376         b_even_scanline = !b_even_scanline;
1377
1378         /* Draw until we reach the end of the line */
1379         for( i_x = 0; i_x < i_width; i_x++ )
1380         {
1381             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1382             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1383             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1384
1385             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1386             if( !i_trans )
1387                 continue;
1388
1389             /* Blending */
1390             rgb_to_yuv( &y, &u, &v, R, G, B );
1391
1392             p_dst_y[i_x] = vlc_blend( y, p_src1_y[i_x], i_trans );
1393             if( b_even_scanline && i_x % 2 == 0 )
1394             {
1395                 p_dst_u[i_x/2] = vlc_blend( u, p_src1_u[i_x/2], i_trans );
1396                 p_dst_v[i_x/2] = vlc_blend( v, p_src1_v[i_x/2], i_trans );
1397             }
1398         }
1399     }
1400 }
1401
1402 static void BlendRGBAR24( filter_t *p_filter, picture_t *p_dst_pic,
1403                           picture_t *p_dst_orig, picture_t *p_src,
1404                           int i_x_offset, int i_y_offset,
1405                           int i_width, int i_height, int i_alpha )
1406 {
1407     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1408     uint8_t *p_dst, *p_src1, *p_src2;
1409     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1410     int i_rindex, i_gindex, i_bindex;
1411
1412     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1413     i_dst_pitch = p_dst_pic->p->i_pitch;
1414     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1415             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1416             p_dst_pic->p->i_pitch *
1417             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1418
1419     i_src1_pitch = p_dst_orig->p->i_pitch;
1420     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1421              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1422              p_dst_orig->p->i_pitch *
1423              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1424
1425     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1426     i_src2_pitch = p_src->p->i_pitch;
1427     p_src2 = p_src->p->p_pixels +
1428              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1429              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1430
1431     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1432
1433     /* Draw until we reach the bottom of the subtitle */
1434     for( i_y = 0; i_y < i_height; i_y++,
1435          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1436     {
1437         /* Draw until we reach the end of the line */
1438         for( i_x = 0; i_x < i_width; i_x++ )
1439         {
1440             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1441             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1442             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1443
1444             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1445             if( !i_trans )
1446                 continue;
1447
1448             /* Blending */
1449             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1450                               i_rindex, i_gindex, i_bindex,
1451                               R, G, B, i_trans, true );
1452         }
1453     }
1454 }
1455
1456 static void BlendRGBAR16( filter_t *p_filter, picture_t *p_dst_pic,
1457                           picture_t *p_dst_orig, picture_t *p_src,
1458                           int i_x_offset, int i_y_offset,
1459                           int i_width, int i_height, int i_alpha )
1460 {
1461     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1462     uint8_t *p_dst, *p_src1, *p_src2;
1463     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1464
1465     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1466     i_dst_pitch = p_dst_pic->p->i_pitch;
1467     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1468             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1469             p_dst_pic->p->i_pitch *
1470             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1471
1472     i_src1_pitch = p_dst_orig->p->i_pitch;
1473     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1474              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1475              p_dst_orig->p->i_pitch *
1476              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1477
1478     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1479     i_src2_pitch = p_src->p->i_pitch;
1480     p_src2 = p_src->p->p_pixels +
1481              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1482              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1483
1484     /* Draw until we reach the bottom of the subtitle */
1485     for( i_y = 0; i_y < i_height; i_y++,
1486          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1487     {
1488         /* Draw until we reach the end of the line */
1489         for( i_x = 0; i_x < i_width; i_x++ )
1490         {
1491             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1492             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1493             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1494
1495             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1496             if( !i_trans )
1497                 continue;
1498
1499             /* Blending */
1500             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1501                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1502                              R, G, B, i_trans, &p_filter->fmt_out.video );
1503         }
1504     }
1505 }
1506
1507 static void BlendRGBAYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1508                                 picture_t *p_dst_orig, picture_t *p_src,
1509                                 int i_x_offset, int i_y_offset,
1510                                 int i_width, int i_height, int i_alpha )
1511 {
1512     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1513     uint8_t *p_dst, *p_src1, *p_src2;
1514     uint8_t *p_trans;
1515     int i_x, i_y, i_pix_pitch, i_trans;
1516     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1517     int i_l_offset, i_u_offset, i_v_offset;
1518     uint8_t y, u, v;
1519
1520     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1521                           p_filter->fmt_out.video.i_chroma );
1522
1523     i_pix_pitch = 2;
1524     i_dst_pitch = p_dst_pic->p->i_pitch;
1525     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1526             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1527             p_dst_pic->p->i_pitch *
1528             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1529
1530     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1531     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1532                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1533                p_dst_orig->p->i_pitch *
1534                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1535
1536     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1537     i_src2_pitch = p_src->p->i_pitch;
1538     p_src2 = p_src->p->p_pixels +
1539              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1540              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1541
1542     i_width &= ~1; /* Needs to be a multiple of 2 */
1543
1544     /* Draw until we reach the bottom of the subtitle */
1545     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
1546          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1547          p_src2 += i_src2_pitch )
1548     {
1549         /* Draw until we reach the end of the line */
1550         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1551         {
1552             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1553             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1554             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1555
1556             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1557             if( !i_trans )
1558                 continue;
1559
1560             /* Blending */
1561             rgb_to_yuv( &y, &u, &v, R, G, B );
1562
1563             vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1564                               i_l_offset, i_u_offset, i_v_offset,
1565                               y, u, v, i_trans, b_even );
1566         }
1567     }
1568 }