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