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