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