]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
Improvements to preferences
[vlc] / modules / video_filter / blend.c
1 /*****************************************************************************
2  * blend.c: alpha blend 2 pictures together
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 BlendYUY2( 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 BlendPalYUY2( 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_category( CAT_VIDEO );
69     set_subcategory( SUBCAT_VIDEO_VFILTER );
70     set_capability( "video blending", 100 );
71     set_callbacks( OpenFilter, CloseFilter );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * OpenFilter: probe the filter and return score
76  *****************************************************************************/
77 static int OpenFilter( vlc_object_t *p_this )
78 {
79     filter_t *p_filter = (filter_t*)p_this;
80     filter_sys_t *p_sys;
81
82     /* Check if we can handle that format.
83      * We could try to use a chroma filter if we can't. */
84     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
85           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
86         ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
87           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
88           p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
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     {
145         BlendYUY2( p_filter, p_dst, p_dst_orig, p_src,
146                    i_x_offset, i_y_offset, i_width, i_height, i_alpha );
147         return;
148     }
149     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
150         p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
151     {
152         BlendR16( p_filter, p_dst, p_dst_orig, p_src,
153                   i_x_offset, i_y_offset, i_width, i_height, i_alpha );
154         return;
155     }
156     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
157         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
158           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
159     {
160         BlendR24( p_filter, p_dst, p_dst_orig, p_src,
161                   i_x_offset, i_y_offset, i_width, i_height, i_alpha );
162         return;
163     }
164     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
165         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
166           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
167     {
168         BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
169                       i_x_offset, i_y_offset, i_width, i_height, i_alpha );
170         return;
171     }
172     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
173         p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
174     {
175         BlendPalYUY2( p_filter, p_dst, p_dst_orig, p_src,
176                       i_x_offset, i_y_offset, i_width, i_height, i_alpha );
177         return;
178     }
179     if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
180         ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
181           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
182           p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
183     {
184         BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
185                     i_x_offset, i_y_offset, i_width, i_height, i_alpha );
186         return;
187     }
188
189     msg_Dbg( p_filter, "no matching alpha blending routine" );
190 }
191
192 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
193                        picture_t *p_dst_orig, picture_t *p_src,
194                        int i_x_offset, int i_y_offset,
195                        int i_width, int i_height, int i_alpha )
196 {
197     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
198     uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
199     uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
200     uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
201     uint8_t *p_trans;
202     int i_x, i_y, i_trans;
203     vlc_bool_t b_even_scanline = i_y_offset % 2;
204
205     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
206     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
207               p_filter->fmt_out.video.i_x_offset +
208               p_dst->p[Y_PLANE].i_pitch *
209               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
210     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
211               p_filter->fmt_out.video.i_x_offset/2 +
212               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
213               p_dst->p[U_PLANE].i_pitch;
214     p_dst_v = p_dst->p[V_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[V_PLANE].i_pitch;
218
219     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
220     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
221                p_filter->fmt_out.video.i_x_offset +
222                p_dst_orig->p[Y_PLANE].i_pitch *
223                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
224     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
225                p_filter->fmt_out.video.i_x_offset/2 +
226                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
227                p_dst_orig->p[U_PLANE].i_pitch;
228     p_src1_v = p_dst_orig->p[V_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[V_PLANE].i_pitch;
232
233     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
234     p_src2_y = p_src->p[Y_PLANE].p_pixels +
235                p_filter->fmt_in.video.i_x_offset +
236                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
237     p_src2_u = p_src->p[U_PLANE].p_pixels +
238                p_filter->fmt_in.video.i_x_offset/2 +
239                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
240     p_src2_v = p_src->p[V_PLANE].p_pixels +
241                p_filter->fmt_in.video.i_x_offset/2 +
242                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
243
244     p_trans = p_src->p[A_PLANE].p_pixels +
245               p_filter->fmt_in.video.i_x_offset +
246               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
247
248 #define MAX_TRANS 255
249 #define TRANS_BITS  8
250
251     /* Draw until we reach the bottom of the subtitle */
252     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
253          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
254          p_src2_y += i_src2_pitch,
255          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
256          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
257          p_src2_u += i_src2_pitch,
258          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
259          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
260          p_src2_v += i_src2_pitch )
261     {
262         b_even_scanline = !b_even_scanline;
263
264         /* Draw until we reach the end of the line */
265         for( i_x = 0; i_x < i_width; i_x++ )
266         {
267             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
268             if( !i_trans )
269             {
270                 /* Completely transparent. Don't change pixel */
271                 continue;
272             }
273             else if( i_trans == MAX_TRANS )
274             {
275                 /* Completely opaque. Completely overwrite underlying pixel */
276                 p_dst_y[i_x] = p_src2_y[i_x];
277
278                 if( b_even_scanline && i_x % 2 == 0 )
279                 {
280                     p_dst_u[i_x/2] = p_src2_u[i_x];
281                     p_dst_v[i_x/2] = p_src2_v[i_x];
282                 }
283                 continue;
284             }
285
286             /* Blending */
287             p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * i_trans +
288                 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
289                 >> TRANS_BITS;
290
291             if( b_even_scanline && i_x % 2 == 0 )
292             {
293                 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * i_trans +
294                 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
295                 >> TRANS_BITS;
296                 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * i_trans +
297                 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
298                 >> TRANS_BITS;
299             }
300         }
301     }
302
303 #undef MAX_TRANS
304 #undef TRANS_BITS
305
306     return;
307 }
308
309 static inline void yuv_to_rgb( int *r, int *g, int *b,
310                                uint8_t y1, uint8_t u1, uint8_t v1 )
311 {
312     /* macros used for YUV pixel conversions */
313 #   define SCALEBITS 10
314 #   define ONE_HALF  (1 << (SCALEBITS - 1))
315 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
316 #   define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
317
318     int y, cb, cr, r_add, g_add, b_add;
319
320     cb = u1 - 128;
321     cr = v1 - 128;
322     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
323     g_add = - FIX(0.34414*255.0/224.0) * cb
324             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
325     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
326     y = (y1 - 16) * FIX(255.0/219.0);
327     *r = CLAMP((y + r_add) >> SCALEBITS);
328     *g = CLAMP((y + g_add) >> SCALEBITS);
329     *b = CLAMP((y + b_add) >> SCALEBITS);
330 }
331
332 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
333                       picture_t *p_dst_orig, picture_t *p_src,
334                       int i_x_offset, int i_y_offset,
335                       int i_width, int i_height, int i_alpha )
336 {
337     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
338     uint8_t *p_dst, *p_src1, *p_src2_y;
339     uint8_t *p_src2_u, *p_src2_v;
340     uint8_t *p_trans;
341     int i_x, i_y, i_pix_pitch;
342     int r, g, b;
343
344     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
345     i_dst_pitch = p_dst_pic->p->i_pitch;
346     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
347             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
348             p_dst_pic->p->i_pitch *
349             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
350
351     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
352     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
353                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
354                p_dst_orig->p->i_pitch *
355                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
356
357     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
358     p_src2_y = p_src->p[Y_PLANE].p_pixels +
359                p_filter->fmt_in.video.i_x_offset +
360                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
361     p_src2_u = p_src->p[U_PLANE].p_pixels +
362                p_filter->fmt_in.video.i_x_offset/2 +
363                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
364     p_src2_v = p_src->p[V_PLANE].p_pixels +
365                p_filter->fmt_in.video.i_x_offset/2 +
366                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
367
368     p_trans = p_src->p[A_PLANE].p_pixels +
369               p_filter->fmt_in.video.i_x_offset +
370               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
371
372 #define MAX_TRANS 255
373 #define TRANS_BITS  8
374
375     /* Draw until we reach the bottom of the subtitle */
376     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
377          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
378          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
379          p_src2_v += i_src2_pitch )
380     {
381         /* Draw until we reach the end of the line */
382         for( i_x = 0; i_x < i_width; i_x++ )
383         {
384             if( !p_trans[i_x] )
385             {
386                 /* Completely transparent. Don't change pixel */
387                 continue;
388             }
389             else if( p_trans[i_x] == MAX_TRANS )
390             {
391                 /* Completely opaque. Completely overwrite underlying pixel */
392                 yuv_to_rgb( &r, &g, &b,
393                             p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
394
395     ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
396                 continue;
397             }
398
399             /* Blending */
400             yuv_to_rgb( &r, &g, &b,
401                         p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
402
403     ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
404         }
405     }
406
407 #undef MAX_TRANS
408 #undef TRANS_BITS
409
410     return;
411 }
412
413 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
414                       picture_t *p_dst_orig, picture_t *p_src,
415                       int i_x_offset, int i_y_offset,
416                       int i_width, int i_height, int i_alpha )
417 {
418     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
419     uint8_t *p_dst, *p_src1, *p_src2_y;
420     uint8_t *p_src2_u, *p_src2_v;
421     uint8_t *p_trans;
422     int i_x, i_y, i_pix_pitch, i_trans;
423     int r, g, b;
424
425     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
426     i_dst_pitch = p_dst_pic->p->i_pitch;
427     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
428             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
429             p_dst_pic->p->i_pitch *
430             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
431
432     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
433     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
434                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
435                p_dst_orig->p->i_pitch *
436                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
437
438     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
439     p_src2_y = p_src->p[Y_PLANE].p_pixels +
440                p_filter->fmt_in.video.i_x_offset +
441                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
442     p_src2_u = p_src->p[U_PLANE].p_pixels +
443                p_filter->fmt_in.video.i_x_offset/2 +
444                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
445     p_src2_v = p_src->p[V_PLANE].p_pixels +
446                p_filter->fmt_in.video.i_x_offset/2 +
447                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
448
449     p_trans = p_src->p[A_PLANE].p_pixels +
450               p_filter->fmt_in.video.i_x_offset +
451               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
452
453 #define MAX_TRANS 255
454 #define TRANS_BITS  8
455
456     /* Draw until we reach the bottom of the subtitle */
457     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
458          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
459          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
460          p_src2_v += i_src2_pitch )
461     {
462         /* Draw until we reach the end of the line */
463         for( i_x = 0; i_x < i_width; i_x++ )
464         {
465             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
466             if( !i_trans )
467             {
468                 /* Completely transparent. Don't change pixel */
469                 continue;
470             }
471             else if( i_trans == MAX_TRANS )
472             {
473                 /* Completely opaque. Completely overwrite underlying pixel */
474                 yuv_to_rgb( &r, &g, &b,
475                             p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
476
477                 p_dst[i_x * i_pix_pitch]     = r;
478                 p_dst[i_x * i_pix_pitch + 1] = g;
479                 p_dst[i_x * i_pix_pitch + 2] = b;
480                 continue;
481             }
482
483             /* Blending */
484             yuv_to_rgb( &r, &g, &b,
485                         p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
486
487             p_dst[i_x * i_pix_pitch]     = ( r * i_trans +
488                 (uint16_t)p_src1[i_x * i_pix_pitch] *
489                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
490             p_dst[i_x * i_pix_pitch + 1] = ( g * i_trans +
491                 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
492                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
493             p_dst[i_x * i_pix_pitch + 2] = ( b * i_trans +
494                 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
495                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
496         }
497     }
498
499 #undef MAX_TRANS
500 #undef TRANS_BITS
501
502     return;
503 }
504
505 static void BlendYUY2( filter_t *p_filter, picture_t *p_dst_pic,
506                        picture_t *p_dst_orig, picture_t *p_src,
507                        int i_x_offset, int i_y_offset,
508                        int i_width, int i_height, int i_alpha )
509 {
510     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
511     uint8_t *p_dst, *p_src1, *p_src2_y;
512     uint8_t *p_src2_u, *p_src2_v;
513     uint8_t *p_trans;
514     int i_x, i_y, i_pix_pitch, i_trans;
515
516     i_pix_pitch = 2;
517     i_dst_pitch = p_dst_pic->p->i_pitch;
518     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
519             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
520             p_dst_pic->p->i_pitch *
521             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
522
523     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
524     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
525                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
526                p_dst_orig->p->i_pitch *
527                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
528
529     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
530     p_src2_y = p_src->p[Y_PLANE].p_pixels +
531                p_filter->fmt_in.video.i_x_offset +
532                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
533     p_src2_u = p_src->p[U_PLANE].p_pixels +
534                p_filter->fmt_in.video.i_x_offset/2 +
535                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
536     p_src2_v = p_src->p[V_PLANE].p_pixels +
537                p_filter->fmt_in.video.i_x_offset/2 +
538                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
539
540     p_trans = p_src->p[A_PLANE].p_pixels +
541               p_filter->fmt_in.video.i_x_offset +
542               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
543
544 #define MAX_TRANS 255
545 #define TRANS_BITS  8
546
547     /* Draw until we reach the bottom of the subtitle */
548     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
549          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
550          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
551          p_src2_v += i_src2_pitch )
552     {
553         /* Draw until we reach the end of the line */
554         for( i_x = 0; i_x < i_width; i_x += 2 )
555         {
556             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
557             if( !i_trans )
558             {
559                 /* Completely transparent. Don't change pixel */
560             }
561             else if( i_trans == MAX_TRANS )
562             {
563                 /* Completely opaque. Completely overwrite underlying pixel */
564                 p_dst[i_x * 2]     = p_src2_y[i_x];
565                 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
566                 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
567             }
568             else
569             {
570                 /* Blending */
571                 p_dst[i_x * 2]     = ( (uint16_t)p_src2_y[i_x] * i_trans +
572                     (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
573                     >> TRANS_BITS;
574                 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
575                     (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
576                     >> TRANS_BITS;
577                 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
578                     (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
579                     >> TRANS_BITS;
580             }
581
582             i_trans = ( p_trans[i_x+1] * i_alpha ) / 255;
583             if( !i_trans )
584             {
585                 /* Completely transparent. Don't change pixel */
586             }
587             else if( i_trans == MAX_TRANS )
588             {
589                 /* Completely opaque. Completely overwrite underlying pixel */
590                 p_dst[i_x * 2 + 2] = p_src2_y[i_x + 1];
591             }
592             else
593             {
594                 /* Blending */
595                 p_dst[i_x * 2 + 2] = ( (uint16_t)p_src2_y[i_x+1] * i_trans +
596                     (uint16_t)p_src1[i_x * 2 + 2] * (MAX_TRANS - i_trans) )
597                     >> TRANS_BITS;
598             }
599         }
600     }
601
602 #undef MAX_TRANS
603 #undef TRANS_BITS
604
605     return;
606 }
607
608 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
609                           picture_t *p_dst_orig, picture_t *p_src,
610                           int i_x_offset, int i_y_offset,
611                           int i_width, int i_height, int i_alpha )
612 {
613     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
614     uint8_t *p_src1_y, *p_src2, *p_dst_y;
615     uint8_t *p_src1_u, *p_dst_u;
616     uint8_t *p_src1_v, *p_dst_v;
617     int i_x, i_y, i_trans;
618     vlc_bool_t b_even_scanline = i_y_offset % 2;
619
620     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
621     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
622               p_filter->fmt_out.video.i_x_offset +
623               p_dst->p[Y_PLANE].i_pitch *
624               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
625     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
626               p_filter->fmt_out.video.i_x_offset/2 +
627               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
628               p_dst->p[U_PLANE].i_pitch;
629     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
630               p_filter->fmt_out.video.i_x_offset/2 +
631               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
632               p_dst->p[V_PLANE].i_pitch;
633
634     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
635     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
636                p_filter->fmt_out.video.i_x_offset +
637                p_dst_orig->p[Y_PLANE].i_pitch *
638                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
639     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
640                p_filter->fmt_out.video.i_x_offset/2 +
641                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
642                p_dst_orig->p[U_PLANE].i_pitch;
643     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
644                p_filter->fmt_out.video.i_x_offset/2 +
645                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
646                p_dst_orig->p[V_PLANE].i_pitch;
647
648     i_src2_pitch = p_src->p->i_pitch;
649     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
650              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
651
652 #define MAX_TRANS 255
653 #define TRANS_BITS  8
654 #define p_trans p_src2
655 #define p_pal p_filter->fmt_in.video.p_palette->palette
656
657     /* Draw until we reach the bottom of the subtitle */
658     for( i_y = 0; i_y < i_height; i_y++,
659          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
660          p_src2 += i_src2_pitch,
661          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
662          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
663          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
664          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
665     {
666         b_even_scanline = !b_even_scanline;
667
668         /* Draw until we reach the end of the line */
669         for( i_x = 0; i_x < i_width; i_x++ )
670         {
671             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
672             if( !i_trans )
673             {
674                 /* Completely transparent. Don't change pixel */
675                 continue;
676             }
677             else if( i_trans == MAX_TRANS )
678             {
679                 /* Completely opaque. Completely overwrite underlying pixel */
680                 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
681
682                 if( b_even_scanline && i_x % 2 == 0 )
683                 {
684                     p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
685                     p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
686                 }
687                 continue;
688             }
689
690             /* Blending */
691             p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
692                 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
693                 >> TRANS_BITS;
694
695             if( b_even_scanline && i_x % 2 == 0 )
696             {
697                 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
698                     (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
699                     >> TRANS_BITS;
700                 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
701                     (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
702                     >> TRANS_BITS;
703             }
704         }
705     }
706
707 #undef MAX_TRANS
708 #undef TRANS_BITS
709 #undef p_trans
710 #undef p_pal
711
712     return;
713 }
714
715 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
716                           picture_t *p_dst_orig, picture_t *p_src,
717                           int i_x_offset, int i_y_offset,
718                           int i_width, int i_height, int i_alpha )
719 {
720     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
721     uint8_t *p_src1, *p_src2, *p_dst;
722     int i_x, i_y, i_pix_pitch, i_trans;
723
724     i_pix_pitch = 2;
725     i_dst_pitch = p_dst_pic->p->i_pitch;
726     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
727             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
728             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
729
730     i_src1_pitch = p_dst_orig->p->i_pitch;
731     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
732              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
733              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
734
735     i_src2_pitch = p_src->p->i_pitch;
736     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
737              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
738
739 #define MAX_TRANS 255
740 #define TRANS_BITS  8
741 #define p_trans p_src2
742 #define p_pal p_filter->fmt_in.video.p_palette->palette
743
744     /* Draw until we reach the bottom of the subtitle */
745     for( i_y = 0; i_y < i_height; i_y++,
746          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
747     {
748         /* Draw until we reach the end of the line */
749         for( i_x = 0; i_x < i_width; i_x += 2 )
750         {
751             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
752             if( !i_trans )
753             {
754                 /* Completely transparent. Don't change pixel */
755             }
756             else if( i_trans == MAX_TRANS )
757             {
758                 /* Completely opaque. Completely overwrite underlying pixel */
759                 p_dst[i_x * 2]     = p_pal[p_src2[i_x]][0];
760                 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
761                 p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
762             }
763             else
764             {
765                 /* Blending */
766                 p_dst[i_x * 2]     = ( (uint16_t)p_pal[p_src2[i_x]][0] *
767                     i_trans + (uint16_t)p_src1[i_x * 2] *
768                     (MAX_TRANS - i_trans) ) >> TRANS_BITS;
769                 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
770                     i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
771                     (MAX_TRANS - i_trans) ) >> TRANS_BITS;
772                 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
773                     i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
774                     (MAX_TRANS - i_trans) ) >> TRANS_BITS;
775             }
776
777             i_trans = ( p_pal[p_trans[i_x+1]][3] * i_alpha ) / 255;
778             if( !i_trans )
779             {
780                 /* Completely transparent. Don't change pixel */
781             }
782             else if( i_trans == MAX_TRANS )
783             {
784                 /* Completely opaque. Completely overwrite underlying pixel */
785                 p_dst[i_x * 2 + 2] = p_pal[p_src2[i_x + 1]][0];
786             }
787             else
788             {
789                 /* Blending */
790                 p_dst[i_x * 2 + 2] = ( (uint16_t)p_pal[p_src2[i_x+1]][0] *
791                     i_trans + (uint16_t)p_src1[i_x * 2 + 2] *
792                     (MAX_TRANS - i_trans) ) >> TRANS_BITS;
793             }
794         }
795     }
796
797 #undef MAX_TRANS
798 #undef TRANS_BITS
799 #undef p_trans
800 #undef p_pal
801
802     return;
803 }
804
805 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
806                         picture_t *p_dst_orig, picture_t *p_src,
807                         int i_x_offset, int i_y_offset,
808                         int i_width, int i_height, int i_alpha )
809 {
810     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
811     uint8_t *p_src1, *p_src2, *p_dst;
812     int i_x, i_y, i_pix_pitch, i_trans;
813     int r, g, b;
814     video_palette_t rgbpalette;
815
816     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
817     i_dst_pitch = p_dst_pic->p->i_pitch;
818     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
819             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
820             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
821
822     i_src1_pitch = p_dst_orig->p->i_pitch;
823     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
824              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
825              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
826
827     i_src2_pitch = p_src->p->i_pitch;
828     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
829              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
830
831 #define MAX_TRANS 255
832 #define TRANS_BITS  8
833 #define p_trans p_src2
834 #define p_pal p_filter->fmt_in.video.p_palette->palette
835 #define rgbpal rgbpalette.palette
836
837     /* Convert palette first */
838     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
839          i_y < 256; i_y++ )
840     {
841         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
842
843         if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
844         {
845             *(uint16_t *)rgbpal[i_y] =
846                 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
847         }
848         else
849         {
850             rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
851         }
852     }
853
854     /* Draw until we reach the bottom of the subtitle */
855     for( i_y = 0; i_y < i_height; i_y++,
856          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
857     {
858         /* Draw until we reach the end of the line */
859         for( i_x = 0; i_x < i_width; i_x++ )
860         {
861             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
862             if( !i_trans )
863             {
864                 /* Completely transparent. Don't change pixel */
865                 continue;
866             }
867             else if( i_trans == MAX_TRANS ||
868                      p_filter->fmt_out.video.i_chroma ==
869                      VLC_FOURCC('R','V','1','6') )
870             {
871                 /* Completely opaque. Completely overwrite underlying pixel */
872                 p_dst[i_x * i_pix_pitch]     = rgbpal[p_src2[i_x]][0];
873                 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
874                 if( p_filter->fmt_out.video.i_chroma !=
875                     VLC_FOURCC('R','V','1','6') )
876                 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
877                 continue;
878             }
879
880             /* Blending */
881             p_dst[i_x * i_pix_pitch]     = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
882                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
883                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
884             p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
885                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
886                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
887             p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
888                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
889                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
890         }
891     }
892
893 #undef MAX_TRANS
894 #undef TRANS_BITS
895 #undef p_trans
896 #undef p_pal
897 #undef rgbpal
898
899     return;
900 }
901
902 /*****************************************************************************
903  * CloseFilter: clean up the filter
904  *****************************************************************************/
905 static void CloseFilter( vlc_object_t *p_this )
906 {
907     filter_t *p_filter = (filter_t*)p_this;
908     filter_sys_t *p_sys = p_filter->p_sys;
909
910     free( p_sys );
911 }