]> git.sesse.net Git - vlc/blob - modules/video_filter/blend.c
48f2dd65043af93e0aa775126cf79aec2c481727
[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., 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     vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
516
517     i_pix_pitch = 2;
518     i_dst_pitch = p_dst_pic->p->i_pitch;
519     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
520             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
521             p_dst_pic->p->i_pitch *
522             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
523
524     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
525     p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
526                p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
527                p_dst_orig->p->i_pitch *
528                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
529
530     i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
531     p_src2_y = p_src->p[Y_PLANE].p_pixels +
532                p_filter->fmt_in.video.i_x_offset +
533                p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
534     p_src2_u = p_src->p[U_PLANE].p_pixels +
535                p_filter->fmt_in.video.i_x_offset/2 +
536                p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
537     p_src2_v = p_src->p[V_PLANE].p_pixels +
538                p_filter->fmt_in.video.i_x_offset/2 +
539                p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
540
541     p_trans = p_src->p[A_PLANE].p_pixels +
542               p_filter->fmt_in.video.i_x_offset +
543               p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
544
545     i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
546
547 #define MAX_TRANS 255
548 #define TRANS_BITS  8
549
550     /* Draw until we reach the bottom of the subtitle */
551     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
552          p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
553          p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
554          p_src2_v += i_src2_pitch )
555     {
556         /* Draw until we reach the end of the line */
557         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
558         {
559             i_trans = ( p_trans[i_x] * i_alpha ) / 255;
560             if( !i_trans )
561             {
562                 /* Completely transparent. Don't change pixel */
563             }
564             else if( i_trans == MAX_TRANS )
565             {
566                 /* Completely opaque. Completely overwrite underlying pixel */
567                 p_dst[i_x * 2]     = p_src2_y[i_x];
568
569                 if( b_even )
570                 {
571                     p_dst[i_x * 2 + 1] = p_src2_u[i_x];
572                     p_dst[i_x * 2 + 3] = p_src2_v[i_x];
573                 }
574             }
575             else
576             {
577                 /* Blending */
578                 p_dst[i_x * 2]     = ( (uint16_t)p_src2_y[i_x] * i_trans +
579                     (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
580                     >> TRANS_BITS;
581
582                 if( b_even )
583                 {
584                     p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
585                         (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
586                         >> TRANS_BITS;
587                     p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
588                         (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
589                         >> TRANS_BITS;
590                 }
591             }
592         }
593     }
594
595 #undef MAX_TRANS
596 #undef TRANS_BITS
597
598     return;
599 }
600
601 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
602                           picture_t *p_dst_orig, picture_t *p_src,
603                           int i_x_offset, int i_y_offset,
604                           int i_width, int i_height, int i_alpha )
605 {
606     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
607     uint8_t *p_src1_y, *p_src2, *p_dst_y;
608     uint8_t *p_src1_u, *p_dst_u;
609     uint8_t *p_src1_v, *p_dst_v;
610     int i_x, i_y, i_trans;
611     vlc_bool_t b_even_scanline = i_y_offset % 2;
612
613     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
614     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
615               p_filter->fmt_out.video.i_x_offset +
616               p_dst->p[Y_PLANE].i_pitch *
617               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
618     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
619               p_filter->fmt_out.video.i_x_offset/2 +
620               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
621               p_dst->p[U_PLANE].i_pitch;
622     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
623               p_filter->fmt_out.video.i_x_offset/2 +
624               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
625               p_dst->p[V_PLANE].i_pitch;
626
627     i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
628     p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
629                p_filter->fmt_out.video.i_x_offset +
630                p_dst_orig->p[Y_PLANE].i_pitch *
631                ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
632     p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
633                p_filter->fmt_out.video.i_x_offset/2 +
634                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
635                p_dst_orig->p[U_PLANE].i_pitch;
636     p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
637                p_filter->fmt_out.video.i_x_offset/2 +
638                ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
639                p_dst_orig->p[V_PLANE].i_pitch;
640
641     i_src2_pitch = p_src->p->i_pitch;
642     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
643              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
644
645 #define MAX_TRANS 255
646 #define TRANS_BITS  8
647 #define p_trans p_src2
648 #define p_pal p_filter->fmt_in.video.p_palette->palette
649
650     /* Draw until we reach the bottom of the subtitle */
651     for( i_y = 0; i_y < i_height; i_y++,
652          p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
653          p_src2 += i_src2_pitch,
654          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
655          p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
656          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
657          p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
658     {
659         b_even_scanline = !b_even_scanline;
660
661         /* Draw until we reach the end of the line */
662         for( i_x = 0; i_x < i_width; i_x++ )
663         {
664             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
665             if( !i_trans )
666             {
667                 /* Completely transparent. Don't change pixel */
668                 continue;
669             }
670             else if( i_trans == MAX_TRANS )
671             {
672                 /* Completely opaque. Completely overwrite underlying pixel */
673                 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
674
675                 if( b_even_scanline && i_x % 2 == 0 )
676                 {
677                     p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
678                     p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
679                 }
680                 continue;
681             }
682
683             /* Blending */
684             p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
685                 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
686                 >> TRANS_BITS;
687
688             if( b_even_scanline && i_x % 2 == 0 )
689             {
690                 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
691                     (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
692                     >> TRANS_BITS;
693                 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
694                     (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
695                     >> TRANS_BITS;
696             }
697         }
698     }
699
700 #undef MAX_TRANS
701 #undef TRANS_BITS
702 #undef p_trans
703 #undef p_pal
704
705     return;
706 }
707
708 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
709                           picture_t *p_dst_orig, picture_t *p_src,
710                           int i_x_offset, int i_y_offset,
711                           int i_width, int i_height, int i_alpha )
712 {
713     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
714     uint8_t *p_src1, *p_src2, *p_dst;
715     int i_x, i_y, i_pix_pitch, i_trans;
716     vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
717
718     i_pix_pitch = 2;
719     i_dst_pitch = p_dst_pic->p->i_pitch;
720     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
721             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
722             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
723
724     i_src1_pitch = p_dst_orig->p->i_pitch;
725     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
726              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
727              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
728
729     i_src2_pitch = p_src->p->i_pitch;
730     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
731              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
732
733     i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
734
735 #define MAX_TRANS 255
736 #define TRANS_BITS  8
737 #define p_trans p_src2
738 #define p_pal p_filter->fmt_in.video.p_palette->palette
739
740     /* Draw until we reach the bottom of the subtitle */
741     for( i_y = 0; i_y < i_height; i_y++,
742          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
743     {
744         /* Draw until we reach the end of the line */
745         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
746         {
747             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
748             if( !i_trans )
749             {
750                 /* Completely transparent. Don't change pixel */
751             }
752             else if( i_trans == MAX_TRANS )
753             {
754                 /* Completely opaque. Completely overwrite underlying pixel */
755                 p_dst[i_x * 2]     = p_pal[p_src2[i_x]][0];
756
757                 if( b_even )
758                 {
759                     p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
760                     p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
761                 }
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
770                 if( b_even )
771                 {
772                     p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
773                         i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
774                         (MAX_TRANS - i_trans) ) >> TRANS_BITS;
775                     p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
776                         i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
777                         (MAX_TRANS - i_trans) ) >> TRANS_BITS;
778                 }
779             }
780         }
781     }
782
783 #undef MAX_TRANS
784 #undef TRANS_BITS
785 #undef p_trans
786 #undef p_pal
787
788     return;
789 }
790
791 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
792                         picture_t *p_dst_orig, picture_t *p_src,
793                         int i_x_offset, int i_y_offset,
794                         int i_width, int i_height, int i_alpha )
795 {
796     int i_src1_pitch, i_src2_pitch, i_dst_pitch;
797     uint8_t *p_src1, *p_src2, *p_dst;
798     int i_x, i_y, i_pix_pitch, i_trans;
799     int r, g, b;
800     video_palette_t rgbpalette;
801
802     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
803     i_dst_pitch = p_dst_pic->p->i_pitch;
804     p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
805             p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
806             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
807
808     i_src1_pitch = p_dst_orig->p->i_pitch;
809     p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
810              p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
811              ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
812
813     i_src2_pitch = p_src->p->i_pitch;
814     p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
815              i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
816
817 #define MAX_TRANS 255
818 #define TRANS_BITS  8
819 #define p_trans p_src2
820 #define p_pal p_filter->fmt_in.video.p_palette->palette
821 #define rgbpal rgbpalette.palette
822
823     /* Convert palette first */
824     for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
825          i_y < 256; i_y++ )
826     {
827         yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
828
829         if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
830         {
831             *(uint16_t *)rgbpal[i_y] =
832                 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
833         }
834         else
835         {
836             rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
837         }
838     }
839
840     /* Draw until we reach the bottom of the subtitle */
841     for( i_y = 0; i_y < i_height; i_y++,
842          p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_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             i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
848             if( !i_trans )
849             {
850                 /* Completely transparent. Don't change pixel */
851                 continue;
852             }
853             else if( i_trans == MAX_TRANS ||
854                      p_filter->fmt_out.video.i_chroma ==
855                      VLC_FOURCC('R','V','1','6') )
856             {
857                 /* Completely opaque. Completely overwrite underlying pixel */
858                 p_dst[i_x * i_pix_pitch]     = rgbpal[p_src2[i_x]][0];
859                 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
860                 if( p_filter->fmt_out.video.i_chroma !=
861                     VLC_FOURCC('R','V','1','6') )
862                 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
863                 continue;
864             }
865
866             /* Blending */
867             p_dst[i_x * i_pix_pitch]     = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
868                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
869                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
870             p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
871                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
872                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
873             p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
874                 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
875                 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
876         }
877     }
878
879 #undef MAX_TRANS
880 #undef TRANS_BITS
881 #undef p_trans
882 #undef p_pal
883 #undef rgbpal
884
885     return;
886 }
887
888 /*****************************************************************************
889  * CloseFilter: clean up the filter
890  *****************************************************************************/
891 static void CloseFilter( vlc_object_t *p_this )
892 {
893     filter_t *p_filter = (filter_t*)p_this;
894     filter_sys_t *p_sys = p_filter->p_sys;
895
896     free( p_sys );
897 }