Fix compilation warning and alpha discarding.
[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     VLC_UNUSED(p_dst_orig);
855     int i_src2_pitch, i_dst_pitch;
856     uint8_t *p_src2_y, *p_dst_y;
857     uint8_t *p_src2_u, *p_dst_u;
858     uint8_t *p_src2_v, *p_dst_v;
859     int i_y;
860     bool b_even_scanline = i_y_offset % 2;
861
862     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
863     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
864               p_filter->fmt_out.video.i_x_offset +
865               p_dst->p[Y_PLANE].i_pitch *
866               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
867     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
868               p_filter->fmt_out.video.i_x_offset/2 +
869               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
870               p_dst->p[U_PLANE].i_pitch;
871     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
872               p_filter->fmt_out.video.i_x_offset/2 +
873               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
874               p_dst->p[V_PLANE].i_pitch;
875
876     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
877                                 0, 0, &p_filter->fmt_in.video, 1 );
878     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
879                                 0, 0, &p_filter->fmt_in.video, 2 );
880     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
881                                 0, 0, &p_filter->fmt_in.video, 2 );
882
883     i_width &= ~1;
884
885     /* Draw until we reach the bottom of the subtitle */
886     for( i_y = 0; i_y < i_height;
887             i_y++, p_dst_y += i_dst_pitch, p_src2_y += i_src2_pitch )
888     {
889         /* Completely opaque. Completely overwrite underlying pixel */
890         vlc_memcpy( p_dst_y, p_src2_y, i_width );
891         if( b_even_scanline )
892         {
893             p_dst_u  += i_dst_pitch/2;
894             p_dst_v  += i_dst_pitch/2;
895         }
896         else
897         {
898             vlc_memcpy( p_dst_u, p_src2_u, i_width/2 );
899             vlc_memcpy( p_dst_v, p_src2_v, i_width/2 );
900         }
901         b_even_scanline = !b_even_scanline;
902         if( i_y%2 == 1 )
903         {
904             p_src2_u += i_src2_pitch/2;
905             p_src2_v += i_src2_pitch/2;
906         }
907     }
908 }
909
910 static void BlendI420R16( filter_t *p_filter, picture_t *p_dst_pic,
911                           picture_t *p_dst_orig, picture_t *p_src,
912                           int i_x_offset, int i_y_offset,
913                           int i_width, int i_height, int i_alpha )
914 {
915     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
916     uint8_t *p_dst, *p_src1, *p_src2_y;
917     uint8_t *p_src2_u, *p_src2_v;
918     int i_x, i_y, i_pix_pitch;
919     int r, g, b;
920
921     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
922     i_dst_pitch = p_dst_pic->p->i_pitch;
923     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
924             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
925             p_dst_pic->p->i_pitch *
926             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
927
928     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
929     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
930                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
931                p_dst_orig->p->i_pitch *
932                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
933
934     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
935                                 0, 0, &p_filter->fmt_in.video, 1 );
936     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
937                                 0, 0, &p_filter->fmt_in.video, 2 );
938     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
939                                 0, 0, &p_filter->fmt_in.video, 2 );
940
941     /* Draw until we reach the bottom of the subtitle */
942     for( i_y = 0; i_y < i_height; i_y++,
943          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
944          p_src2_y += i_src2_pitch )
945     {
946         /* Draw until we reach the end of the line */
947         for( i_x = 0; i_x < i_width; i_x++ )
948         {
949             /* Blending */
950             yuv_to_rgb( &r, &g, &b,
951                         p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2] );
952
953             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
954                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
955                              r, g, b, i_alpha, &p_filter->fmt_out.video );
956         }
957         if( i_y%2 == 1 )
958         {
959             p_src2_u += i_src2_pitch/2;
960             p_src2_v += i_src2_pitch/2;
961         }
962     }
963 }
964
965 static void BlendI420R24( filter_t *p_filter, picture_t *p_dst_pic,
966                           picture_t *p_dst_orig, picture_t *p_src,
967                           int i_x_offset, int i_y_offset,
968                           int i_width, int i_height, int i_alpha )
969 {
970     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
971     uint8_t *p_dst, *p_src1, *p_src2_y;
972     uint8_t *p_src2_u, *p_src2_v;
973     int i_x, i_y, i_pix_pitch;
974     int i_rindex, i_gindex, i_bindex;
975     int r, g, b;
976
977     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
978     i_dst_pitch = p_dst_pic->p->i_pitch;
979     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
980             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
981             p_dst_pic->p->i_pitch *
982             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
983
984     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
985     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
986                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
987                p_dst_orig->p->i_pitch *
988                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
989
990     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
991                                 0, 0, &p_filter->fmt_in.video, 1 );
992     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
993                                 0, 0, &p_filter->fmt_in.video, 2 );
994     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
995                                 0, 0, &p_filter->fmt_in.video, 2 );
996
997     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
998
999     /* Draw until we reach the bottom of the subtitle */
1000     for( i_y = 0; i_y < i_height; i_y++,
1001          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1002          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
1003          p_src2_v += i_src2_pitch )
1004     {
1005         /* Draw until we reach the end of the line */
1006         for( i_x = 0; i_x < i_width; i_x++ )
1007         {
1008             if( !i_alpha )
1009                 continue;
1010
1011             /* Blending */
1012             yuv_to_rgb( &r, &g, &b,
1013                         p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2] );
1014
1015             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1016                               i_rindex, i_gindex, i_bindex, r, g, b, i_alpha, true );
1017         }
1018         if( i_y%2 == 1 )
1019         {
1020             p_src2_u += i_src2_pitch/2;
1021             p_src2_v += i_src2_pitch/2;
1022         }
1023     }
1024 }
1025
1026 static void BlendI420YUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1027                                 picture_t *p_dst_orig, picture_t *p_src,
1028                                 int i_x_offset, int i_y_offset,
1029                                 int i_width, int i_height, int i_alpha )
1030 {
1031     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1032     uint8_t *p_dst, *p_src1, *p_src2_y;
1033     uint8_t *p_src2_u, *p_src2_v;
1034     int i_x, i_y, i_pix_pitch;
1035     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1036     int i_l_offset, i_u_offset, i_v_offset;
1037
1038     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1039                           p_filter->fmt_out.video.i_chroma );
1040
1041     i_pix_pitch = 2;
1042     i_dst_pitch = p_dst_pic->p->i_pitch;
1043     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1044             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1045             p_dst_pic->p->i_pitch *
1046             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1047
1048     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1049     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1050                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1051                p_dst_orig->p->i_pitch *
1052                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1053
1054     p_src2_y = vlc_plane_start( &i_src2_pitch, p_src, Y_PLANE,
1055                                 0, 0, &p_filter->fmt_in.video, 1 );
1056     p_src2_u = vlc_plane_start( NULL, p_src, U_PLANE,
1057                                 0, 0, &p_filter->fmt_in.video, 2 );
1058     p_src2_v = vlc_plane_start( NULL, p_src, V_PLANE,
1059                                 0, 0, &p_filter->fmt_in.video, 2 );
1060
1061     i_width &= ~1; /* Needs to be a multiple of 2 */
1062
1063     /* Draw until we reach the bottom of the subtitle */
1064     for( i_y = 0; i_y < i_height; i_y++,
1065          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1066          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
1067          p_src2_v += i_src2_pitch )
1068     {
1069         /* Draw until we reach the end of the line */
1070         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1071         {
1072             if( !i_alpha )
1073                 continue;
1074
1075             /* Blending */
1076             vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1077                               i_l_offset, i_u_offset, i_v_offset,
1078                               p_src2_y[i_x], p_src2_u[i_x/2], p_src2_v[i_x/2], i_alpha, b_even );
1079         }
1080         if( i_y%2 == 1 )
1081         {
1082             p_src2_u += i_src2_pitch/2;
1083             p_src2_v += i_src2_pitch/2;
1084         }
1085     }
1086 }
1087
1088 /***********************************************************************
1089  * YUVP
1090  ***********************************************************************/
1091 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
1092                           picture_t *p_dst_orig, picture_t *p_src,
1093                           int i_x_offset, int i_y_offset,
1094                           int i_width, int i_height, int i_alpha )
1095 {
1096     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1097     uint8_t *p_src1_y, *p_src2, *p_dst_y;
1098     uint8_t *p_src1_u, *p_dst_u;
1099     uint8_t *p_src1_v, *p_dst_v;
1100     int i_x, i_y, i_trans;
1101     bool b_even_scanline = i_y_offset % 2;
1102
1103     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1104     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1105               p_filter->fmt_out.video.i_x_offset +
1106               p_dst->p[Y_PLANE].i_pitch *
1107               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1108     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1109               p_filter->fmt_out.video.i_x_offset/2 +
1110               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1111               p_dst->p[U_PLANE].i_pitch;
1112     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1113               p_filter->fmt_out.video.i_x_offset/2 +
1114               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1115               p_dst->p[V_PLANE].i_pitch;
1116
1117     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1118     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1119                p_filter->fmt_out.video.i_x_offset +
1120                p_dst_orig->p[Y_PLANE].i_pitch *
1121                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1122     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1123                p_filter->fmt_out.video.i_x_offset/2 +
1124                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1125                p_dst_orig->p[U_PLANE].i_pitch;
1126     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1127                p_filter->fmt_out.video.i_x_offset/2 +
1128                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1129                p_dst_orig->p[V_PLANE].i_pitch;
1130
1131     i_src2_pitch = p_src->p->i_pitch;
1132     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1133              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1134
1135 #define p_pal p_filter->fmt_in.video.p_palette->palette
1136
1137     /* Draw until we reach the bottom of the subtitle */
1138     for( i_y = 0; i_y < i_height; i_y++,
1139          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1140          p_src2 += i_src2_pitch,
1141          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1142          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1143          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1144          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
1145     {
1146         const uint8_t *p_trans = p_src2;
1147         b_even_scanline = !b_even_scanline;
1148
1149         /* Draw until we reach the end of the line */
1150         for( i_x = 0; i_x < i_width; i_x++ )
1151         {
1152             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1153             if( !i_trans )
1154                 continue;
1155
1156             /* Blending */
1157             p_dst_y[i_x] = vlc_blend( p_pal[p_src2[i_x]][0], p_src1_y[i_x], i_trans );
1158             if( b_even_scanline && ((i_x % 2) == 0) )
1159             {
1160                 p_dst_u[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][1], p_src1_u[i_x/2], i_trans );
1161                 p_dst_v[i_x/2] = vlc_blend( p_pal[p_src2[i_x]][2], p_src1_v[i_x/2], i_trans );
1162             }
1163         }
1164     }
1165 #undef p_pal
1166 }
1167
1168 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1169                                picture_t *p_dst_orig, picture_t *p_src,
1170                                int i_x_offset, int i_y_offset,
1171                                int i_width, int i_height, int i_alpha )
1172 {
1173     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1174     uint8_t *p_src1, *p_src2, *p_dst;
1175     int i_x, i_y, i_pix_pitch, i_trans;
1176     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1177     int i_l_offset, i_u_offset, i_v_offset;
1178
1179     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1180                           p_filter->fmt_out.video.i_chroma );
1181
1182     i_pix_pitch = 2;
1183     i_dst_pitch = p_dst_pic->p->i_pitch;
1184     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1185             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1186             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1187
1188     i_src1_pitch = p_dst_orig->p->i_pitch;
1189     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1190              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1191              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1192
1193     i_src2_pitch = p_src->p->i_pitch;
1194     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1195              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1196
1197     i_width &= ~1; /* Needs to be a multiple of 2 */
1198
1199 #define p_pal p_filter->fmt_in.video.p_palette->palette
1200
1201     /* Draw until we reach the bottom of the subtitle */
1202     for( i_y = 0; i_y < i_height; i_y++,
1203          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1204     {
1205         const uint8_t *p_trans = p_src2;
1206         /* Draw until we reach the end of the line */
1207         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1208         {
1209             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1210             if( !i_trans )
1211                 continue;
1212
1213             /* Blending */
1214             if( b_even )
1215             {
1216                 uint16_t i_u;
1217                 uint16_t i_v;
1218                 if( p_trans[i_x+1] > 0xaa )
1219                 {
1220                     i_u = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
1221                     i_v = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
1222                 }
1223                 else
1224                 {
1225                     i_u = p_pal[p_src2[i_x]][1];
1226                     i_v = p_pal[p_src2[i_x]][2];
1227                 }
1228
1229                 vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1230                                   i_l_offset, i_u_offset, i_v_offset,
1231                                   p_pal[p_src2[i_x]][0], i_u, i_v, i_trans, true );
1232             }
1233             else
1234             {
1235                 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 );
1236             }
1237         }
1238     }
1239 #undef p_pal
1240 }
1241
1242 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
1243                         picture_t *p_dst_orig, picture_t *p_src,
1244                         int i_x_offset, int i_y_offset,
1245                         int i_width, int i_height, int i_alpha )
1246 {
1247     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1248     uint8_t *p_src1, *p_src2, *p_dst;
1249     int i_x, i_y, i_pix_pitch, i_trans;
1250     int r, g, b;
1251     video_palette_t rgbpalette;
1252     int i_rindex, i_gindex, i_bindex;
1253
1254     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1255     i_dst_pitch = p_dst_pic->p->i_pitch;
1256     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
1257             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
1258             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1259
1260     i_src1_pitch = p_dst_orig->p->i_pitch;
1261     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
1262              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
1263              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1264
1265     i_src2_pitch = p_src->p->i_pitch;
1266     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
1267              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
1268
1269 #define p_pal p_filter->fmt_in.video.p_palette->palette
1270 #define rgbpal rgbpalette.palette
1271
1272     /* Convert palette first */
1273     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries && i_y < 256; i_y++ )
1274     {
1275         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
1276         rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
1277     }
1278
1279     /* */
1280     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1281
1282     /* Draw until we reach the bottom of the subtitle */
1283     for( i_y = 0; i_y < i_height; i_y++,
1284          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1285     {
1286         const uint8_t *p_trans = p_src2;
1287         /* Draw until we reach the end of the line */
1288         for( i_x = 0; i_x < i_width; i_x++ )
1289         {
1290             i_trans = vlc_alpha( p_pal[p_trans[i_x]][3], i_alpha );
1291             if( !i_trans )
1292                 continue;
1293
1294             /* Blending */
1295             if( p_filter->fmt_out.video.i_chroma == FCC_RV15 || p_filter->fmt_out.video.i_chroma == FCC_RV16 )
1296                 vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1297                                  (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1298                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1299                                   i_trans,
1300                                   &p_filter->fmt_out.video );
1301             else
1302                 vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1303                                   i_rindex, i_gindex, i_bindex,
1304                                   rgbpal[p_src2[i_x]][0], rgbpal[p_src2[i_x]][1], rgbpal[p_src2[i_x]][3],
1305                                   i_trans, true );
1306         }
1307     }
1308
1309 #undef p_pal
1310 #undef rgbpal
1311 }
1312
1313 /***********************************************************************
1314  * RGBA
1315  ***********************************************************************/
1316 static void BlendRGBAI420( filter_t *p_filter, picture_t *p_dst,
1317                            picture_t *p_dst_orig, picture_t *p_src,
1318                            int i_x_offset, int i_y_offset,
1319                            int i_width, int i_height, int i_alpha )
1320 {
1321     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1322     uint8_t *p_src1_y, *p_dst_y;
1323     uint8_t *p_src1_u, *p_dst_u;
1324     uint8_t *p_src1_v, *p_dst_v;
1325     uint8_t *p_src2;
1326     int i_x, i_y, i_trans;
1327     uint8_t y, u, v;
1328
1329     bool b_even_scanline = i_y_offset % 2;
1330
1331     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
1332     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
1333               p_filter->fmt_out.video.i_x_offset +
1334               p_dst->p[Y_PLANE].i_pitch *
1335               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1336     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
1337               p_filter->fmt_out.video.i_x_offset/2 +
1338               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1339               p_dst->p[U_PLANE].i_pitch;
1340     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
1341               p_filter->fmt_out.video.i_x_offset/2 +
1342               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1343               p_dst->p[V_PLANE].i_pitch;
1344
1345     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1346     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
1347                p_filter->fmt_out.video.i_x_offset +
1348                p_dst_orig->p[Y_PLANE].i_pitch *
1349                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1350     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
1351                p_filter->fmt_out.video.i_x_offset/2 +
1352                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1353                p_dst_orig->p[U_PLANE].i_pitch;
1354     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
1355                p_filter->fmt_out.video.i_x_offset/2 +
1356                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
1357                p_dst_orig->p[V_PLANE].i_pitch;
1358
1359     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1360     i_src2_pitch = p_src->p->i_pitch;
1361     p_src2 = p_src->p->p_pixels +
1362              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1363              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1364
1365
1366     /* Draw until we reach the bottom of the subtitle */
1367     for( i_y = 0; i_y < i_height; i_y++,
1368          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
1369          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
1370          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
1371          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
1372          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
1373          p_src2 += i_src2_pitch )
1374     {
1375         b_even_scanline = !b_even_scanline;
1376
1377         /* Draw until we reach the end of the line */
1378         for( i_x = 0; i_x < i_width; i_x++ )
1379         {
1380             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1381             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1382             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1383
1384             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1385             if( !i_trans )
1386                 continue;
1387
1388             /* Blending */
1389             rgb_to_yuv( &y, &u, &v, R, G, B );
1390
1391             p_dst_y[i_x] = vlc_blend( y, p_src1_y[i_x], i_trans );
1392             if( b_even_scanline && i_x % 2 == 0 )
1393             {
1394                 p_dst_u[i_x/2] = vlc_blend( u, p_src1_u[i_x/2], i_trans );
1395                 p_dst_v[i_x/2] = vlc_blend( v, p_src1_v[i_x/2], i_trans );
1396             }
1397         }
1398     }
1399 }
1400
1401 static void BlendRGBAR24( filter_t *p_filter, picture_t *p_dst_pic,
1402                           picture_t *p_dst_orig, picture_t *p_src,
1403                           int i_x_offset, int i_y_offset,
1404                           int i_width, int i_height, int i_alpha )
1405 {
1406     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1407     uint8_t *p_dst, *p_src1, *p_src2;
1408     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1409     int i_rindex, i_gindex, i_bindex;
1410
1411     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1412     i_dst_pitch = p_dst_pic->p->i_pitch;
1413     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1414             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1415             p_dst_pic->p->i_pitch *
1416             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1417
1418     i_src1_pitch = p_dst_orig->p->i_pitch;
1419     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1420              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1421              p_dst_orig->p->i_pitch *
1422              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1423
1424     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1425     i_src2_pitch = p_src->p->i_pitch;
1426     p_src2 = p_src->p->p_pixels +
1427              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1428              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1429
1430     vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
1431
1432     /* Draw until we reach the bottom of the subtitle */
1433     for( i_y = 0; i_y < i_height; i_y++,
1434          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1435     {
1436         /* Draw until we reach the end of the line */
1437         for( i_x = 0; i_x < i_width; i_x++ )
1438         {
1439             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1440             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1441             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1442
1443             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1444             if( !i_trans )
1445                 continue;
1446
1447             /* Blending */
1448             vlc_blend_packed( &p_dst[i_x * i_pix_pitch], &p_src1[i_x * i_pix_pitch],
1449                               i_rindex, i_gindex, i_bindex,
1450                               R, G, B, i_trans, true );
1451         }
1452     }
1453 }
1454
1455 static void BlendRGBAR16( filter_t *p_filter, picture_t *p_dst_pic,
1456                           picture_t *p_dst_orig, picture_t *p_src,
1457                           int i_x_offset, int i_y_offset,
1458                           int i_width, int i_height, int i_alpha )
1459 {
1460     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
1461     uint8_t *p_dst, *p_src1, *p_src2;
1462     int i_x, i_y, i_pix_pitch, i_trans, i_src_pix_pitch;
1463
1464     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
1465     i_dst_pitch = p_dst_pic->p->i_pitch;
1466     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1467             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1468             p_dst_pic->p->i_pitch *
1469             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1470
1471     i_src1_pitch = p_dst_orig->p->i_pitch;
1472     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1473              p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1474              p_dst_orig->p->i_pitch *
1475              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1476
1477     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1478     i_src2_pitch = p_src->p->i_pitch;
1479     p_src2 = p_src->p->p_pixels +
1480              p_filter->fmt_in.video.i_x_offset * i_pix_pitch +
1481              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1482
1483     /* Draw until we reach the bottom of the subtitle */
1484     for( i_y = 0; i_y < i_height; i_y++,
1485          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
1486     {
1487         /* Draw until we reach the end of the line */
1488         for( i_x = 0; i_x < i_width; i_x++ )
1489         {
1490             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1491             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1492             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1493
1494             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1495             if( !i_trans )
1496                 continue;
1497
1498             /* Blending */
1499             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
1500                              (const uint16_t*)&p_src1[i_x * i_pix_pitch],
1501                              R, G, B, i_trans, &p_filter->fmt_out.video );
1502         }
1503     }
1504 }
1505
1506 static void BlendRGBAYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
1507                                 picture_t *p_dst_orig, picture_t *p_src,
1508                                 int i_x_offset, int i_y_offset,
1509                                 int i_width, int i_height, int i_alpha )
1510 {
1511     int i_src1_pitch, i_src2_pitch, i_dst_pitch, i_src_pix_pitch;
1512     uint8_t *p_dst, *p_src1, *p_src2;
1513     uint8_t *p_trans;
1514     int i_x, i_y, i_pix_pitch, i_trans;
1515     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
1516     int i_l_offset, i_u_offset, i_v_offset;
1517     uint8_t y, u, v;
1518
1519     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
1520                           p_filter->fmt_out.video.i_chroma );
1521
1522     i_pix_pitch = 2;
1523     i_dst_pitch = p_dst_pic->p->i_pitch;
1524     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
1525             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1526             p_dst_pic->p->i_pitch *
1527             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1528
1529     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
1530     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
1531                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
1532                p_dst_orig->p->i_pitch *
1533                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
1534
1535     i_src_pix_pitch = p_src->p->i_pixel_pitch;
1536     i_src2_pitch = p_src->p->i_pitch;
1537     p_src2 = p_src->p->p_pixels +
1538              p_filter->fmt_in.video.i_x_offset * i_src2_pitch +
1539              p_src->p->i_pitch * p_filter->fmt_in.video.i_y_offset;
1540
1541     i_width &= ~1; /* Needs to be a multiple of 2 */
1542
1543     /* Draw until we reach the bottom of the subtitle */
1544     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
1545          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
1546          p_src2 += i_src2_pitch )
1547     {
1548         /* Draw until we reach the end of the line */
1549         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
1550         {
1551             const int R = p_src2[i_x * i_src_pix_pitch + 0];
1552             const int G = p_src2[i_x * i_src_pix_pitch + 1];
1553             const int B = p_src2[i_x * i_src_pix_pitch + 2];
1554
1555             i_trans = vlc_alpha( p_src2[i_x * i_src_pix_pitch + 3], i_alpha );
1556             if( !i_trans )
1557                 continue;
1558
1559             /* Blending */
1560             rgb_to_yuv( &y, &u, &v, R, G, B );
1561
1562             vlc_blend_packed( &p_dst[i_x * 2], &p_src1[i_x * 2],
1563                               i_l_offset, i_u_offset, i_v_offset,
1564                               y, u, v, i_trans, b_even );
1565         }
1566     }
1567 }