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