]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
* Improved renderering of YUVA/P onto YUV variants. refs #539
[vlc] / modules / video_filter / blend.c
1 /*****************************************************************************
2  * blend.c: alpha blend 2 pictures together
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29 #include "vlc_filter.h"
30
31 /*****************************************************************************
32  * filter_sys_t : filter descriptor
33  *****************************************************************************/
34 struct filter_sys_t
35 {
36     int i_dummy;
37 };
38
39 /****************************************************************************
40  * Local prototypes
41  ****************************************************************************/
42 static int  OpenFilter ( vlc_object_t * );
43 static void CloseFilter( vlc_object_t * );
44
45 /* TODO i_alpha support for BlendR16 */
46 static void Blend( filter_t *, picture_t *, picture_t *, picture_t *,
47                    int, int, int );
48 static void BlendI420( filter_t *, picture_t *, picture_t *, picture_t *,
49                        int, int, int, int, int );
50 static void BlendR16( filter_t *, picture_t *, picture_t *, picture_t *,
51                       int, int, int, int, int );
52 static void BlendR24( filter_t *, picture_t *, picture_t *, picture_t *,
53                       int, int, int, int, int );
54 static void BlendYUVPacked( filter_t *, picture_t *, picture_t *, picture_t *,
55                             int, int, int, int, int );
56 static void BlendPalI420( filter_t *, picture_t *, picture_t *, picture_t *,
57                           int, int, int, int, int );
58 static void BlendPalYUVPacked( filter_t *, picture_t *, picture_t *, picture_t *,
59                                int, int, int, int, int );
60 static void BlendPalRV( filter_t *, picture_t *, picture_t *, picture_t *,
61                         int, int, int, int, int );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin();
67     set_description( _("Video pictures blending") );
68     set_capability( "video blending", 100 );
69     set_callbacks( OpenFilter, CloseFilter );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * OpenFilter: probe the filter and return score
74  *****************************************************************************/
75 static int OpenFilter( vlc_object_t *p_this )
76 {
77     filter_t *p_filter = (filter_t*)p_this;
78     filter_sys_t *p_sys;
79
80     /* Check if we can handle that format.
81      * We could try to use a chroma filter if we can't. */
82     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
83           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
84         ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
85           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
86           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
87           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('U','Y','V','Y') &&
88           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','Y','U') &&
89           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
90           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
91           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
92     {
93         return VLC_EGENERIC;
94     }
95
96     /* Allocate the memory needed to store the decoder's structure */
97     if( ( p_filter->p_sys = p_sys =
98           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
99     {
100         msg_Err( p_filter, "out of memory" );
101         return VLC_EGENERIC;
102     }
103
104     /* Misc init */
105     p_filter->pf_video_blend = Blend;
106
107     msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
108              (char *)&p_filter->fmt_in.video.i_chroma,
109              (char *)&p_filter->fmt_out.video.i_chroma );
110
111
112     return VLC_SUCCESS;
113 }
114
115 /****************************************************************************
116  * Blend: the whole thing
117  ****************************************************************************
118  * This function is called just after the thread is launched.
119  ****************************************************************************/
120 static void Blend( filter_t *p_filter, picture_t *p_dst,
121                    picture_t *p_dst_orig, picture_t *p_src,
122                    int i_x_offset, int i_y_offset, int i_alpha )
123 {
124     int i_width, i_height;
125
126     i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
127                     (int)p_filter->fmt_in.video.i_visible_width);
128
129     i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
130                      (int)p_filter->fmt_in.video.i_visible_height);
131
132     if( i_width <= 0 || i_height <= 0 ) return;
133
134     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
135         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
136           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
137     {
138         BlendI420( p_filter, p_dst, p_dst_orig, p_src,
139                    i_x_offset, i_y_offset, i_width, i_height, i_alpha );
140         return;
141     }
142     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
143         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') ||
144           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') ||
145           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') ) )
146     {
147         BlendYUVPacked( p_filter, p_dst, p_dst_orig, p_src,
148                    i_x_offset, i_y_offset, i_width, i_height, i_alpha );
149         return;
150     }
151     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
152         p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
153     {
154         BlendR16( p_filter, p_dst, p_dst_orig, p_src,
155                   i_x_offset, i_y_offset, i_width, i_height, i_alpha );
156         return;
157     }
158     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
159         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
160           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
161     {
162         BlendR24( p_filter, p_dst, p_dst_orig, p_src,
163                   i_x_offset, i_y_offset, i_width, i_height, i_alpha );
164         return;
165     }
166     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
167         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
168           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
169     {
170         BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
171                       i_x_offset, i_y_offset, i_width, i_height, i_alpha );
172         return;
173     }
174     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
175         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') ||
176           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') ||
177           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') ) )
178     {
179         BlendPalYUVPacked( p_filter, p_dst, p_dst_orig, p_src,
180                       i_x_offset, i_y_offset, i_width, i_height, i_alpha );
181         return;
182     }
183     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
184         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
185           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
186           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
187     {
188         BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
189                     i_x_offset, i_y_offset, i_width, i_height, i_alpha );
190         return;
191     }
192
193     msg_Dbg( p_filter, "no matching alpha blending routine" );
194 }
195
196 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
197                        picture_t *p_dst_orig, picture_t *p_src,
198                        int i_x_offset, int i_y_offset,
199                        int i_width, int i_height, int i_alpha )
200 {
201     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
202     uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
203     uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
204     uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
205     uint8_t *p_trans;
206     int i_x, i_y, i_trans;
207     vlc_bool_t b_even_scanline = i_y_offset % 2;
208
209     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
210     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
211               p_filter->fmt_out.video.i_x_offset +
212               p_dst->p[Y_PLANE].i_pitch *
213               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
214     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
215               p_filter->fmt_out.video.i_x_offset/2 +
216               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
217               p_dst->p[U_PLANE].i_pitch;
218     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
219               p_filter->fmt_out.video.i_x_offset/2 +
220               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
221               p_dst->p[V_PLANE].i_pitch;
222
223     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
224     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
225                p_filter->fmt_out.video.i_x_offset +
226                p_dst_orig->p[Y_PLANE].i_pitch *
227                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
228     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
229                p_filter->fmt_out.video.i_x_offset/2 +
230                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
231                p_dst_orig->p[U_PLANE].i_pitch;
232     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
233                p_filter->fmt_out.video.i_x_offset/2 +
234                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
235                p_dst_orig->p[V_PLANE].i_pitch;
236
237     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
238     p_src2_y = p_src->p[Y_PLANE].p_pixels +
239                p_filter->fmt_in.video.i_x_offset +
240                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
241     p_src2_u = p_src->p[U_PLANE].p_pixels +
242                p_filter->fmt_in.video.i_x_offset/2 +
243                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
244     p_src2_v = p_src->p[V_PLANE].p_pixels +
245                p_filter->fmt_in.video.i_x_offset/2 +
246                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
247
248     p_trans = p_src->p[A_PLANE].p_pixels +
249               p_filter->fmt_in.video.i_x_offset +
250               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
251
252 #define MAX_TRANS 255
253 #define TRANS_BITS  8
254
255     /* Draw until we reach the bottom of the subtitle */
256     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
257          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
258          p_src2_y += i_src2_pitch,
259          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
260          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
261          p_src2_u += i_src2_pitch,
262          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
263          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
264          p_src2_v += i_src2_pitch )
265     {
266         b_even_scanline = !b_even_scanline;
267
268         /* Draw until we reach the end of the line */
269         for( i_x = 0; i_x < i_width; i_x++ )
270         {
271             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
272             if( !i_trans )
273             {
274                 /* Completely transparent. Don't change pixel */
275                 continue;
276             }
277             else if( i_trans == MAX_TRANS )
278             {
279                 /* Completely opaque. Completely overwrite underlying pixel */
280                 p_dst_y[i_x] = p_src2_y[i_x];
281
282                 if( b_even_scanline && i_x % 2 == 0 )
283                 {
284                     p_dst_u[i_x/2] = p_src2_u[i_x];
285                     p_dst_v[i_x/2] = p_src2_v[i_x];
286                 }
287                 continue;
288             }
289
290             /* Blending */
291             p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * i_trans +
292                 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
293                 >> TRANS_BITS;
294
295             if( b_even_scanline && i_x % 2 == 0 )
296             {
297                 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * i_trans +
298                 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
299                 >> TRANS_BITS;
300                 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * i_trans +
301                 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
302                 >> TRANS_BITS;
303             }
304         }
305     }
306
307 #undef MAX_TRANS
308 #undef TRANS_BITS
309
310     return;
311 }
312
313 static inline void yuv_to_rgb( int *r, int *g, int *b,
314                                uint8_t y1, uint8_t u1, uint8_t v1 )
315 {
316     /* macros used for YUV pixel conversions */
317 #   define SCALEBITS 10
318 #   define ONE_HALF  (1 << (SCALEBITS - 1))
319 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
320 #   define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
321
322     int y, cb, cr, r_add, g_add, b_add;
323
324     cb = u1 - 128;
325     cr = v1 - 128;
326     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
327     g_add = - FIX(0.34414*255.0/224.0) * cb
328             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
329     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
330     y = (y1 - 16) * FIX(255.0/219.0);
331     *r = CLAMP((y + r_add) >> SCALEBITS);
332     *g = CLAMP((y + g_add) >> SCALEBITS);
333     *b = CLAMP((y + b_add) >> SCALEBITS);
334 }
335
336 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
337                       picture_t *p_dst_orig, picture_t *p_src,
338                       int i_x_offset, int i_y_offset,
339                       int i_width, int i_height, int i_alpha )
340 {
341     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
342     uint8_t *p_dst, *p_src1, *p_src2_y;
343     uint8_t *p_src2_u, *p_src2_v;
344     uint8_t *p_trans;
345     int i_x, i_y, i_pix_pitch;
346     int r, g, b;
347
348     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
349     i_dst_pitch = p_dst_pic->p->i_pitch;
350     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
351             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
352             p_dst_pic->p->i_pitch *
353             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
354
355     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
356     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
357                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
358                p_dst_orig->p->i_pitch *
359                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
360
361     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
362     p_src2_y = p_src->p[Y_PLANE].p_pixels +
363                p_filter->fmt_in.video.i_x_offset +
364                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
365     p_src2_u = p_src->p[U_PLANE].p_pixels +
366                p_filter->fmt_in.video.i_x_offset/2 +
367                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
368     p_src2_v = p_src->p[V_PLANE].p_pixels +
369                p_filter->fmt_in.video.i_x_offset/2 +
370                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
371
372     p_trans = p_src->p[A_PLANE].p_pixels +
373               p_filter->fmt_in.video.i_x_offset +
374               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
375
376 #define MAX_TRANS 255
377 #define TRANS_BITS  8
378
379     /* Draw until we reach the bottom of the subtitle */
380     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
381          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
382          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
383          p_src2_v += i_src2_pitch )
384     {
385         /* Draw until we reach the end of the line */
386         for( i_x = 0; i_x < i_width; i_x++ )
387         {
388             if( !p_trans[i_x] )
389             {
390                 /* Completely transparent. Don't change pixel */
391                 continue;
392             }
393             else if( p_trans[i_x] == MAX_TRANS )
394             {
395                 /* Completely opaque. Completely overwrite underlying pixel */
396                 yuv_to_rgb( &r, &g, &b,
397                             p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
398
399     ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
400                 continue;
401             }
402
403             /* Blending */
404             yuv_to_rgb( &r, &g, &b,
405                         p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
406
407     ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
408         }
409     }
410
411 #undef MAX_TRANS
412 #undef TRANS_BITS
413
414     return;
415 }
416
417 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
418                       picture_t *p_dst_orig, picture_t *p_src,
419                       int i_x_offset, int i_y_offset,
420                       int i_width, int i_height, int i_alpha )
421 {
422     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
423     uint8_t *p_dst, *p_src1, *p_src2_y;
424     uint8_t *p_src2_u, *p_src2_v;
425     uint8_t *p_trans;
426     int i_x, i_y, i_pix_pitch, i_trans;
427     int r, g, b;
428
429     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
430     i_dst_pitch = p_dst_pic->p->i_pitch;
431     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
432             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
433             p_dst_pic->p->i_pitch *
434             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
435
436     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
437     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
438                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
439                p_dst_orig->p->i_pitch *
440                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
441
442     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
443     p_src2_y = p_src->p[Y_PLANE].p_pixels +
444                p_filter->fmt_in.video.i_x_offset +
445                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
446     p_src2_u = p_src->p[U_PLANE].p_pixels +
447                p_filter->fmt_in.video.i_x_offset/2 +
448                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
449     p_src2_v = p_src->p[V_PLANE].p_pixels +
450                p_filter->fmt_in.video.i_x_offset/2 +
451                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
452
453     p_trans = p_src->p[A_PLANE].p_pixels +
454               p_filter->fmt_in.video.i_x_offset +
455               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
456
457 #define MAX_TRANS 255
458 #define TRANS_BITS  8
459
460     /* Draw until we reach the bottom of the subtitle */
461     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
462          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
463          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
464          p_src2_v += i_src2_pitch )
465     {
466         /* Draw until we reach the end of the line */
467         for( i_x = 0; i_x < i_width; i_x++ )
468         {
469             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
470             if( !i_trans )
471             {
472                 /* Completely transparent. Don't change pixel */
473                 continue;
474             }
475             else if( i_trans == MAX_TRANS )
476             {
477                 /* Completely opaque. Completely overwrite underlying pixel */
478                 yuv_to_rgb( &r, &g, &b,
479                             p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
480
481                 p_dst[i_x * i_pix_pitch]     = r;
482                 p_dst[i_x * i_pix_pitch + 1] = g;
483                 p_dst[i_x * i_pix_pitch + 2] = b;
484                 continue;
485             }
486
487             /* Blending */
488             yuv_to_rgb( &r, &g, &b,
489                         p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
490
491             p_dst[i_x * i_pix_pitch]     = ( r * i_trans +
492                 (uint16_t)p_src1[i_x * i_pix_pitch] *
493                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
494             p_dst[i_x * i_pix_pitch + 1] = ( g * i_trans +
495                 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
496                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
497             p_dst[i_x * i_pix_pitch + 2] = ( b * i_trans +
498                 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
499                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
500         }
501     }
502
503 #undef MAX_TRANS
504 #undef TRANS_BITS
505
506     return;
507 }
508
509 static void BlendYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
510                             picture_t *p_dst_orig, picture_t *p_src,
511                             int i_x_offset, int i_y_offset,
512                             int i_width, int i_height, int i_alpha )
513 {
514     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
515     uint8_t *p_dst, *p_src1, *p_src2_y;
516     uint8_t *p_src2_u, *p_src2_v;
517     uint8_t *p_trans;
518     int i_x, i_y, i_pix_pitch, i_trans;
519     vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
520     int i_l_offset = 0, i_u_offset = 0, i_v_offset = 0;
521
522     if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
523     {
524         i_l_offset = 0;
525         i_u_offset = 1;
526         i_v_offset = 3;
527     }
528     else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
529     {
530         i_l_offset = 1;
531         i_u_offset = 0;
532         i_v_offset = 2;
533     }
534     else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') )
535     {
536         i_l_offset = 0;
537         i_u_offset = 3;
538         i_v_offset = 1;
539     }
540
541     i_pix_pitch = 2;
542     i_dst_pitch = p_dst_pic->p->i_pitch;
543     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
544             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
545             p_dst_pic->p->i_pitch *
546             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
547
548     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
549     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
550                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
551                p_dst_orig->p->i_pitch *
552                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
553
554     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
555     p_src2_y = p_src->p[Y_PLANE].p_pixels +
556                p_filter->fmt_in.video.i_x_offset +
557                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
558     p_src2_u = p_src->p[U_PLANE].p_pixels +
559                p_filter->fmt_in.video.i_x_offset/2 +
560                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
561     p_src2_v = p_src->p[V_PLANE].p_pixels +
562                p_filter->fmt_in.video.i_x_offset/2 +
563                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
564
565     p_trans = p_src->p[A_PLANE].p_pixels +
566               p_filter->fmt_in.video.i_x_offset +
567               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
568
569     i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
570
571 #define MAX_TRANS 255
572 #define TRANS_BITS  8
573
574     /* Draw until we reach the bottom of the subtitle */
575     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
576          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
577          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
578          p_src2_v += i_src2_pitch )
579     {
580         /* Draw until we reach the end of the line */
581         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
582         {
583             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
584             if( !i_trans )
585             {
586                 /* Completely transparent. Don't change pixel */
587             }
588             else if( i_trans == MAX_TRANS )
589             {
590                 /* Completely opaque. Completely overwrite underlying pixel */
591                 p_dst[i_x * 2 + i_l_offset]     = p_src2_y[i_x];
592
593                 if( b_even )
594                 {
595                     if( p_trans[i_x+1] > 0xaa )
596                     {
597                         p_dst[i_x * 2 + i_u_offset] = (p_src2_u[i_x]+p_src2_u[i_x+1])>>1;
598                         p_dst[i_x * 2 + i_v_offset] = (p_src2_v[i_x]+p_src2_v[i_x+1])>>1;
599                     }
600                     else
601                     {
602                         p_dst[i_x * 2 + i_u_offset] = p_src2_u[i_x];
603                         p_dst[i_x * 2 + i_v_offset] = p_src2_v[i_x];
604                     }
605                 }
606             }
607             else
608             {
609                 /* Blending */
610                 p_dst[i_x * 2 + i_l_offset]     = ( (uint16_t)p_src2_y[i_x] * i_trans +
611                     (uint16_t)p_src1[i_x * 2 + i_l_offset] * (MAX_TRANS - i_trans) )
612                     >> TRANS_BITS;
613
614                 if( b_even )
615                 {
616                     uint16_t i_u = 0;
617                     uint16_t i_v = 0;
618                     if( p_trans[i_x+1] > 0xaa )
619                     {
620                         i_u = (p_src2_u[i_x]+p_src2_u[i_x+1])>>1;
621                         i_v = (p_src2_v[i_x]+p_src2_v[i_x+1])>>1;
622                     }
623                     else 
624                     {
625                         i_u = p_src2_u[i_x];
626                         i_v = p_src2_v[i_x];
627                     }
628                     p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)i_u * i_trans +
629                         (uint16_t)p_src1[i_x * 2 + i_u_offset] * (MAX_TRANS - i_trans) )
630                         >> TRANS_BITS;
631                     p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)i_v * i_trans +
632                         (uint16_t)p_src1[i_x * 2 + i_v_offset] * (MAX_TRANS - i_trans) )
633                         >> TRANS_BITS;
634                 }
635             }
636         }
637     }
638
639 #undef MAX_TRANS
640 #undef TRANS_BITS
641
642     return;
643 }
644
645 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
646                           picture_t *p_dst_orig, picture_t *p_src,
647                           int i_x_offset, int i_y_offset,
648                           int i_width, int i_height, int i_alpha )
649 {
650     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
651     uint8_t *p_src1_y, *p_src2, *p_dst_y;
652     uint8_t *p_src1_u, *p_dst_u;
653     uint8_t *p_src1_v, *p_dst_v;
654     int i_x, i_y, i_trans;
655     vlc_bool_t b_even_scanline = i_y_offset % 2;
656
657     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
658     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
659               p_filter->fmt_out.video.i_x_offset +
660               p_dst->p[Y_PLANE].i_pitch *
661               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
662     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
663               p_filter->fmt_out.video.i_x_offset/2 +
664               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
665               p_dst->p[U_PLANE].i_pitch;
666     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
667               p_filter->fmt_out.video.i_x_offset/2 +
668               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
669               p_dst->p[V_PLANE].i_pitch;
670
671     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
672     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
673                p_filter->fmt_out.video.i_x_offset +
674                p_dst_orig->p[Y_PLANE].i_pitch *
675                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
676     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
677                p_filter->fmt_out.video.i_x_offset/2 +
678                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
679                p_dst_orig->p[U_PLANE].i_pitch;
680     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
681                p_filter->fmt_out.video.i_x_offset/2 +
682                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
683                p_dst_orig->p[V_PLANE].i_pitch;
684
685     i_src2_pitch = p_src->p->i_pitch;
686     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
687              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
688
689 #define MAX_TRANS 255
690 #define TRANS_BITS  8
691 #define p_trans p_src2
692 #define p_pal p_filter->fmt_in.video.p_palette->palette
693
694     /* Draw until we reach the bottom of the subtitle */
695     for( i_y = 0; i_y < i_height; i_y++,
696          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
697          p_src2 += i_src2_pitch,
698          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
699          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
700          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
701          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
702     {
703         b_even_scanline = !b_even_scanline;
704
705         /* Draw until we reach the end of the line */
706         for( i_x = 0; i_x < i_width; i_x++ )
707         {
708             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
709             if( !i_trans )
710             {
711                 /* Completely transparent. Don't change pixel */
712                 continue;
713             }
714             else if( i_trans == MAX_TRANS )
715             {
716                 /* Completely opaque. Completely overwrite underlying pixel */
717                 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
718
719                 if( b_even_scanline && i_x % 2 == 0 )
720                 {
721                     p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
722                     p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
723                 }
724                 continue;
725             }
726
727             /* Blending */
728             p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
729                 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
730                 >> TRANS_BITS;
731
732             if( b_even_scanline && i_x % 2 == 0 )
733             {
734                 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
735                     (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
736                     >> TRANS_BITS;
737                 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
738                     (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
739                     >> TRANS_BITS;
740             }
741         }
742     }
743
744 #undef MAX_TRANS
745 #undef TRANS_BITS
746 #undef p_trans
747 #undef p_pal
748
749     return;
750 }
751
752 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
753                                picture_t *p_dst_orig, picture_t *p_src,
754                                int i_x_offset, int i_y_offset,
755                                int i_width, int i_height, int i_alpha )
756 {
757     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
758     uint8_t *p_src1, *p_src2, *p_dst;
759     int i_x, i_y, i_pix_pitch, i_trans;
760     vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
761     int i_l_offset = 0, i_u_offset = 0, i_v_offset = 0;
762
763     if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
764     {
765         i_l_offset = 0;
766         i_u_offset = 1;
767         i_v_offset = 3;
768     }
769     else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
770     {
771         i_l_offset = 1;
772         i_u_offset = 0;
773         i_v_offset = 2;
774     }
775     else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') )
776     {
777         i_l_offset = 0;
778         i_u_offset = 3;
779         i_v_offset = 1;
780     }
781
782     i_pix_pitch = 2;
783     i_dst_pitch = p_dst_pic->p->i_pitch;
784     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
785             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
786             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
787
788     i_src1_pitch = p_dst_orig->p->i_pitch;
789     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
790              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
791              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
792
793     i_src2_pitch = p_src->p->i_pitch;
794     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
795              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
796
797     i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
798
799 #define MAX_TRANS 255
800 #define TRANS_BITS  8
801 #define p_trans p_src2
802 #define p_pal p_filter->fmt_in.video.p_palette->palette
803
804     /* Draw until we reach the bottom of the subtitle */
805     for( i_y = 0; i_y < i_height; i_y++,
806          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
807     {
808         /* Draw until we reach the end of the line */
809         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
810         {
811             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
812             if( !i_trans )
813             {
814                 /* Completely transparent. Don't change pixel */
815             }
816             else if( i_trans == MAX_TRANS )
817             {
818                 /* Completely opaque. Completely overwrite underlying pixel */
819                 p_dst[i_x * 2 + i_l_offset]     = p_pal[p_src2[i_x]][0];
820
821                 if( b_even )
822                 {
823                     if( p_trans[i_x+1] > 0xaa )
824                     {
825                         p_dst[i_x * 2 + i_u_offset] = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
826                         p_dst[i_x * 2 + i_v_offset] = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
827                     }
828                     else
829                     {
830                         p_dst[i_x * 2 + i_u_offset] = p_pal[p_src2[i_x]][1];
831                         p_dst[i_x * 2 + i_v_offset] = p_pal[p_src2[i_x]][2];
832                     }
833                 }
834             }
835             else
836             {
837                 /* Blending */
838                 p_dst[i_x * 2 + i_l_offset]     = ( (uint16_t)p_pal[p_src2[i_x]][0] *
839                     i_trans + (uint16_t)p_src1[i_x * 2 + i_l_offset] *
840                     (MAX_TRANS - i_trans) ) >> TRANS_BITS;
841
842                 if( b_even )
843                 {
844                     uint16_t i_u = 0;
845                     uint16_t i_v = 0;
846                     if( p_trans[i_x+1] > 0xaa )
847                     {
848                         i_u = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
849                         i_v = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
850                     }
851                     else 
852                     {
853                         i_u = p_pal[p_src2[i_x]][1];
854                         i_v = p_pal[p_src2[i_x]][2];
855                     }
856
857                     p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)i_u *
858                         i_trans + (uint16_t)p_src1[i_x * 2 + i_u_offset] *
859                         (MAX_TRANS - i_trans) ) >> TRANS_BITS;
860                     p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)i_v *
861                         i_trans + (uint16_t)p_src1[i_x * 2 + i_v_offset] *
862                         (MAX_TRANS - i_trans) ) >> TRANS_BITS;
863                 }
864             }
865         }
866     }
867
868 #undef MAX_TRANS
869 #undef TRANS_BITS
870 #undef p_trans
871 #undef p_pal
872
873     return;
874 }
875
876 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
877                         picture_t *p_dst_orig, picture_t *p_src,
878                         int i_x_offset, int i_y_offset,
879                         int i_width, int i_height, int i_alpha )
880 {
881     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
882     uint8_t *p_src1, *p_src2, *p_dst;
883     int i_x, i_y, i_pix_pitch, i_trans;
884     int r, g, b;
885     video_palette_t rgbpalette;
886
887     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
888     i_dst_pitch = p_dst_pic->p->i_pitch;
889     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
890             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
891             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
892
893     i_src1_pitch = p_dst_orig->p->i_pitch;
894     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
895              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
896              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
897
898     i_src2_pitch = p_src->p->i_pitch;
899     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
900              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
901
902 #define MAX_TRANS 255
903 #define TRANS_BITS  8
904 #define p_trans p_src2
905 #define p_pal p_filter->fmt_in.video.p_palette->palette
906 #define rgbpal rgbpalette.palette
907
908     /* Convert palette first */
909     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
910          i_y < 256; i_y++ )
911     {
912         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
913
914         if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
915         {
916             *(uint16_t *)rgbpal[i_y] =
917                 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
918         }
919         else
920         {
921             rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
922         }
923     }
924
925     /* Draw until we reach the bottom of the subtitle */
926     for( i_y = 0; i_y < i_height; i_y++,
927          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
928     {
929         /* Draw until we reach the end of the line */
930         for( i_x = 0; i_x < i_width; i_x++ )
931         {
932             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
933             if( !i_trans )
934             {
935                 /* Completely transparent. Don't change pixel */
936                 continue;
937             }
938             else if( i_trans == MAX_TRANS ||
939                      p_filter->fmt_out.video.i_chroma ==
940                      VLC_FOURCC('R','V','1','6') )
941             {
942                 /* Completely opaque. Completely overwrite underlying pixel */
943                 p_dst[i_x * i_pix_pitch]     = rgbpal[p_src2[i_x]][0];
944                 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
945                 if( p_filter->fmt_out.video.i_chroma !=
946                     VLC_FOURCC('R','V','1','6') )
947                 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
948                 continue;
949             }
950
951             /* Blending */
952             p_dst[i_x * i_pix_pitch]     = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
953                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
954                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
955             p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
956                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
957                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
958             p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
959                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
960                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
961         }
962     }
963
964 #undef MAX_TRANS
965 #undef TRANS_BITS
966 #undef p_trans
967 #undef p_pal
968 #undef rgbpal
969
970     return;
971 }
972
973 /*****************************************************************************
974  * CloseFilter: clean up the filter
975  *****************************************************************************/
976 static void CloseFilter( vlc_object_t *p_this )
977 {
978     filter_t *p_filter = (filter_t*)p_this;
979     filter_sys_t *p_sys = p_filter->p_sys;
980
981     free( p_sys );
982 }