root/modules/video_filter/blend.c

Revision 06ff9b78d59171ce194af316dffbb62faf460c79, 53.2 kB (checked in by Antoine Cellerier <dionoea@videolan.org>, 3 months ago)

Simplify.

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * blend.c: alpha blend 2 pictures together
3  *****************************************************************************
4  * Copyright (C) 2003-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea @t videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenFilter ( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
43
44 vlc_module_begin();
45     set_description( N_("Video pictures blending") );
46     set_capability( "video blending", 100 );
47     set_callbacks( OpenFilter, CloseFilter );
48 vlc_module_end();
49
50
51 /*****************************************************************************
52  * filter_sys_t : filter descriptor
53  *****************************************************************************/
54 struct filter_sys_t
55 {
56     int i_dummy;
57 };
58
59 #define FCC_YUVA VLC_FOURCC('Y','U','V','A')
60 #define FCC_YUVP VLC_FOURCC('Y','U','V','P')
61 #define FCC_RGBA VLC_FOURCC('R','G','B','A')
62
63 #define FCC_I420 VLC_FOURCC('I','4','2','0')
64 #define FCC_YV12 VLC_FOURCC('Y','V','1','2')
65 #define FCC_YUY2 VLC_FOURCC('Y','U','Y','2')
66 #define FCC_UYVY VLC_FOURCC('U','Y','V','Y')
67 #define FCC_YVYU VLC_FOURCC('Y','V','Y','U')
68 #define FCC_RV15 VLC_FOURCC('R','V','1','5')
69 #define FCC_RV16 VLC_FOURCC('R','V','1','6')
70 #define FCC_RV24 VLC_FOURCC('R','V','2','4')
71 #define FCC_RV32 VLC_FOURCC('R','V','3','2')
72
73 /****************************************************************************
74  * Local prototypes
75  ****************************************************************************/
76 static void Blend( filter_t *, picture_t *, picture_t *,
77                    int, int, int );
78
79 /* YUVA */
80 static void BlendYUVAI420( filter_t *, picture_t *, picture_t *,
81                            int, int, int, int, int );
82 static void BlendYUVARV16( filter_t *, picture_t *, picture_t *,
83                            int, int, int, int, int );
84 static void BlendYUVARV24( filter_t *, picture_t *, picture_t *,
85                            int, int, int, int, int );
86 static void BlendYUVAYUVPacked( filter_t *, picture_t *, picture_t *,
87                                 int, int, int, int, int );
88
89 /* I420, YV12 */
90 static void BlendI420I420( filter_t *, picture_t *, picture_t *,
91                            int, int, int, int, int );
92 static void BlendI420I420_no_alpha(
93                            filter_t *, picture_t *, picture_t *,
94                            int, int, int, int );
95 static void BlendI420R16( filter_t *, picture_t *, picture_t *,
96                            int, int, int, int, int );
97 static void BlendI420R24( filter_t *, picture_t *, picture_t *,
98                           int, int, int, int, int );
99 static void BlendI420YUVPacked( filter_t *, picture_t *,
100                                 picture_t *, int, int, int, int, int );
101
102 /* YUVP */
103 static void BlendPalI420( filter_t *, picture_t *, picture_t *,
104                           int, int, int, int, int );
105 static void BlendPalYUVPacked( filter_t *, picture_t *, picture_t *,
106                                int, int, int, int, int );
107 static void BlendPalRV( filter_t *, picture_t *, picture_t *,
108                         int, int, int, int, int );
109
110 /* RGBA */
111 static void BlendRGBAI420( filter_t *, picture_t *, picture_t *,
112                            int, int, int, int, int );
113 static void BlendRGBAYUVPacked( filter_t *, picture_t *,
114                                 picture_t *, int, int, int, int, int );
115 static void BlendRGBAR16( filter_t *, picture_t *, picture_t *,
116                           int, int, int, int, int );
117 static void BlendRGBAR24( filter_t *, picture_t *, picture_t *,
118                           int, int, int, int, int );
119
120 /*****************************************************************************
121  * OpenFilter: probe the filter and return score
122  *****************************************************************************/
123 static int OpenFilter( vlc_object_t *p_this )
124 {
125     filter_t *p_filter = (filter_t*)p_this;
126     filter_sys_t *p_sys;
127
128     /* Check if we can handle that format.
129      * We could try to use a chroma filter if we can't. */
130     int in_chroma = p_filter->fmt_in.video.i_chroma;
131     int out_chroma = p_filter->fmt_out.video.i_chroma;
132     if( ( in_chroma  != FCC_YUVA && in_chroma  != FCC_I420 &&
133           in_chroma  != FCC_YV12 && in_chroma  != FCC_YUVP &&
134           in_chroma  != FCC_RGBA ) ||
135         ( out_chroma != FCC_I420 && out_chroma != FCC_YUY2 &&
136           out_chroma != FCC_YV12 && out_chroma != FCC_UYVY &&
137           out_chroma != FCC_YVYU && out_chroma != FCC_RV15 &&
138           out_chroma != FCC_YVYU && out_chroma != FCC_RV16 &&
139           out_chroma != FCC_RV24 && out_chroma != FCC_RV32 ) )
140     {
141         return VLC_EGENERIC;
142     }
143
144     /* Allocate the memory needed to store the decoder's structure */
145     p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t));
146     if( !p_sys )
147         return VLC_ENOMEM;
148
149     /* Misc init */
150     p_filter->pf_video_blend = Blend;
151
152     msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
153              (char *)&p_filter->fmt_in.video.i_chroma,
154              (char *)&p_filter->fmt_out.video.i_chroma );
155
156     return VLC_SUCCESS;
157 }
158
159 /*****************************************************************************
160  * CloseFilter: clean up the filter
161  *****************************************************************************/
162 static void CloseFilter( vlc_object_t *p_this )
163 {
164     filter_t *p_filter = (filter_t*)p_this;
165     filter_sys_t *p_sys = p_filter->p_sys;
166
167     free( p_sys );
168 }
169
170 /****************************************************************************
171  * Blend: the whole thing
172  ****************************************************************************
173  * This function is called just after the thread is launched.
174  ****************************************************************************/
175 typedef void (*BlendFunction)( filter_t *,
176                        picture_t *, picture_t *,
177                        int , int , int , int , int );
178
179 #define FCC_PLANAR_420 { FCC_I420, FCC_YV12, 0 }
180 #define FCC_PACKED_422 { FCC_YUY2, FCC_UYVY, FCC_YVYU, 0 }
181 #define FCC_RGB_16 { FCC_RV15, FCC_RV16, 0 }
182 #define FCC_RGB_24 { FCC_RV24, FCC_RV32, 0 }
183
184 #define BLEND_CFG( fccSrc, fctPlanar, fctPacked, fctRgb16, fctRgb24  ) \
185     { .src = fccSrc, .p_dst = FCC_PLANAR_420, .pf_blend = fctPlanar }, \
186     { .src = fccSrc, .p_dst = FCC_PACKED_422, .pf_blend = fctPacked }, \
187     { .src = fccSrc, .p_dst = FCC_RGB_16,     .pf_blend = fctRgb16  }, \
188     { .src = fccSrc, .p_dst = FCC_RGB_24,     .pf_blend = fctRgb24  }
189
190 static const struct
191 {
192     vlc_fourcc_t src;
193     vlc_fourcc_t p_dst[16];
194     BlendFunction pf_blend;
195 } p_blend_cfg[] = {
196
197     BLEND_CFG( FCC_YUVA, BlendYUVAI420, BlendYUVAYUVPacked, BlendYUVARV16, BlendYUVARV24 ),
198
199     BLEND_CFG( FCC_YUVP, BlendPalI420, BlendPalYUVPacked, BlendPalRV, BlendPalRV ),
200
201     BLEND_CFG( FCC_RGBA, BlendRGBAI420, BlendRGBAYUVPacked, BlendRGBAR16, BlendRGBAR24 ),
202
203     BLEND_CFG( FCC_I420, BlendI420I420, BlendI420YUVPacked, BlendI420R16, BlendI420R24 ),
204
205     BLEND_CFG( FCC_YV12, BlendI420I420, BlendI420YUVPacked, BlendI420R16, BlendI420R24 ),
206
207     { 0, {0,}, NULL }
208 };
209
210 static void Blend( filter_t *p_filter,
211                    picture_t *p_dst, picture_t *p_src,
212                    int i_x_offset, int i_y_offset, int i_alpha )
213 {
214     int i_width, i_height;
215
216     if( i_alpha == 0 )
217         return;
218
219     i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
220                     (int)p_filter->fmt_in.video.i_visible_width);
221
222     i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
223                      (int)p_filter->fmt_in.video.i_visible_height);
224
225     if( i_width <= 0 || i_height <= 0 )
226         return;
227
228     video_format_FixRgb( &p_filter->fmt_out.video );
229     video_format_FixRgb( &p_filter->fmt_in.video );
230
231 #if 0
232     msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s\n",
233              (char *)&p_filter->fmt_in.video.i_chroma,
234              (char *)&p_filter->fmt_out.video.i_chroma );
235 #endif
236
237     for( int i = 0; p_blend_cfg[i].src != 0; i++ )
238     {
239         if( p_blend_cfg[i].src != p_filter->fmt_in.video.i_chroma )
240             continue;
241         for( int j = 0; p_blend_cfg[i].p_dst[j] != 0; j++ )
242         {
243             if( p_blend_cfg[i].p_dst[j] != p_filter->fmt_out.video.i_chroma )
244                 continue;
245
246             p_blend_cfg[i].pf_blend( p_filter, p_dst, p_src,
247                                      i_x_offset, i_y_offset,
248                                      i_width, i_height, i_alpha );
249             return;
250         }
251     }
252
253     msg_Dbg( p_filter, "no matching alpha blending routine "
254              "(chroma: %4.4s -> %4.4s)",
255              (char *)&p_filter->fmt_in.video.i_chroma,
256              (char *)&p_filter->fmt_out.video.i_chroma );
257 }
258
259 /***********************************************************************
260  * Utils
261  ***********************************************************************/
262 static inline uint8_t vlc_uint8( int v )
263 {
264     if( v > 255 )
265         return 255;
266     else if( v < 0 )
267         return 0;
268     return v;
269 }
270
271 #define MAX_TRANS 255
272 #define TRANS_BITS  8
273
274 static inline int vlc_blend( int v1, int v2, int a )
275 {
276     /* TODO bench if the tests really increase speed */
277     if( a == 0 )
278         return v2;
279     else if( a == MAX_TRANS )
280         return v1;
281     return ( v1 * a + v2 * (MAX_TRANS - a ) ) >> TRANS_BITS;
282 }
283
284 static inline int vlc_alpha( int t, int a )
285 {
286     if( a == 255 )
287         return t;
288     return (t * a) / 255;
289 }
290
291 static inline void yuv_to_rgb( int *r, int *g, int *b,
292                                uint8_t y1, uint8_t u1, uint8_t v1 )
293 {
294     /* macros used for YUV pixel conversions */
295 #   define SCALEBITS 10
296 #   define ONE_HALF  (1 << (SCALEBITS - 1))
297 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
298
299     int y, cb, cr, r_add, g_add, b_add;
300
301     cb = u1 - 128;
302     cr = v1 - 128;
303     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
304     g_add = - FIX(0.34414*255.0/224.0) * cb
305             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
306     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
307     y = (y1 - 16) * FIX(255.0/219.0);
308     *r = vlc_uint8( (y + r_add) >> SCALEBITS );
309     *g = vlc_uint8( (y + g_add) >> SCALEBITS );
310     *b = vlc_uint8( (y + b_add) >> SCALEBITS );
311 #undef FIX
312 #undef ONE_HALF
313 #undef SCALEBITS
314 }
315
316 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
317                                int r, int g, int b )
318 {
319     *y = ( ( (  66 * r + 129 * g +  25 * b + 128 ) >> 8 ) + 16 );
320     *u =   ( ( -38 * r -  74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
321     *v =   ( ( 112 * r -  94 * g -  18 * b + 128 ) >> 8 ) + 128 ;
322 }
323
324 static uint8_t *vlc_plane_start( int *pi_pitch,
325                                  picture_t *p_picture,
326                                  int i_plane,
327                                  int i_x_offset, int i_y_offset,
328                                  const video_format_t *p_fmt,
329                                  int r )
330 {
331     const int i_pitch = p_picture->p[i_plane].i_pitch;
332     uint8_t *p_pixels = p_picture->p[i_plane].p_pixels;
333
334     const int i_dx = ( i_x_offset + p_fmt->i_x_offset ) / r;
335     const int i_dy = ( i_y_offset + p_fmt->i_y_offset ) / r;
336
337     if( pi_pitch )
338         *pi_pitch = i_pitch;
339     return &p_pixels[ i_dy * i_pitch + i_dx ];
340 }
341
342 static void vlc_yuv_packed_index( int *pi_y, int *pi_u, int *pi_v, vlc_fourcc_t i_chroma )
343 {
344     static const struct {
345         vlc_fourcc_t chroma;
346         int y, u ,v;
347     } p_index[] = {
348         { FCC_YUY2, 0, 1, 3 },
349         { FCC_UYVY, 1, 0, 2 },
350         { FCC_YVYU, 0, 3, 1 },
351         { 0, 0, 0, 0 }
352     };
353     int i;
354
355     for( i = 0; p_index[i].chroma != 0; i++ )
356     {
357         if( p_index[i].chroma == i_chroma )
358             break;
359     }
360     *pi_y = p_index[i].y;
361     *pi_u = p_index[i].u;
362     *pi_v = p_index[i].v;
363 }
364
365 static void vlc_blend_packed( uint8_t *p_dst,
366                               int i_offset0, int i_offset1, int i_offset2,
367                               int c0, int c1, int c2, int i_alpha,
368                               bool b_do12 )
369 {
370     p_dst[i_offset0] = vlc_blend( c0, p_dst[i_offset0], i_alpha );
371     if( b_do12 )
372     {
373         p_dst[i_offset1] = vlc_blend( c1, p_dst[i_offset1], i_alpha );
374         p_dst[i_offset2] = vlc_blend( c2, p_dst[i_offset2], i_alpha );
375     }
376 }
377
378 static void vlc_blend_rgb16( uint16_t *p_dst,
379                              int R, int G, int B, int i_alpha,
380                              const video_format_t *p_fmt )
381 {
382     const int i_pix = *p_dst;
383     const int r = ( i_pix & p_fmt->i_rmask ) >> p_fmt->i_lrshift;
384     const int g = ( i_pix & p_fmt->i_gmask ) >> p_fmt->i_lgshift;
385     const int b = ( i_pix & p_fmt->i_bmask ) >> p_fmt->i_lbshift;
386
387     *p_dst = ( vlc_blend( R >> p_fmt->i_rrshift, r, i_alpha ) << p_fmt->i_lrshift ) |
388              ( vlc_blend( G >> p_fmt->i_rgshift, g, i_alpha ) << p_fmt->i_lgshift ) |
389              ( vlc_blend( B >> p_fmt->i_rbshift, b, i_alpha ) << p_fmt->i_lbshift );
390 }
391
392 static void vlc_rgb_index( int *pi_rindex, int *pi_gindex, int *pi_bindex,
393                            const video_format_t *p_fmt )
394 {
395     if( p_fmt->i_chroma != FCC_RV24 && p_fmt->i_chroma != FCC_RV32 )
396         return;
397
398     /* XXX it will works only if mask are 8 bits aligned */
399 #ifdef WORDS_BIGENDIAN
400     const int i_mask_bits = p_fmt->i_chroma == FCC_RV24 ? 24 : 32;
401     *pi_rindex = ( i_mask_bits - p_fmt->i_lrshift ) / 8;
402     *pi_gindex = ( i_mask_bits - p_fmt->i_lgshift ) / 8;
403     *pi_bindex = ( i_mask_bits - p_fmt->i_lbshift ) / 8;
404 #else
405     *pi_rindex = p_fmt->i_lrshift / 8;
406     *pi_gindex = p_fmt->i_lgshift / 8;
407     *pi_bindex = p_fmt->i_lbshift / 8;
408 #endif
409 }
410
411 /***********************************************************************
412  * YUVA
413  ***********************************************************************/
414 static void BlendYUVAI420( filter_t *p_filter,
415                            picture_t *p_dst, picture_t *p_src,
416                            int i_x_offset, int i_y_offset,
417                            int i_width, int i_height, int i_alpha )
418 {
419     int i_src_pitch, i_dst_pitch;
420     uint8_t *p_src_y, *p_dst_y;
421     uint8_t *p_src_u, *p_dst_u;
422     uint8_t *p_src_v, *p_dst_v;
423     uint8_t *p_trans;
424     int i_x, i_y, i_trans = 0;
425     bool b_even_scanline = i_y_offset % 2;
426
427     p_dst_y = vlc_plane_start( &i_dst_pitch, p_dst, Y_PLANE,
428                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 1 );
429     p_dst_u = vlc_plane_start( NULL, p_dst, U_PLANE,
430                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
431     p_dst_v = vlc_plane_start( NULL, p_dst, V_PLANE,
432                                i_x_offset, i_y_offset, &p_filter->fmt_out.video, 2 );
433
434     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
435                                0, 0, &p_filter->fmt_in.video, 1 );
436     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
437                                0, 0, &p_filter->fmt_in.video, 2 );
438     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
439                                0, 0, &p_filter->fmt_in.video, 2 );
440     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
441                                0, 0, &p_filter->fmt_in.video, 1 );
442
443     /* Draw until we reach the bottom of the subtitle */
444     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
445          p_dst_y += i_dst_pitch, p_src_y += i_src_pitch,
446          p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
447          p_src_u += i_src_pitch,
448          p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
449          p_src_v += i_src_pitch )
450     {
451         b_even_scanline = !b_even_scanline;
452
453         /* Draw until we reach the end of the line */
454         for( i_x = 0; i_x < i_width; i_x++ )
455         {
456             if( p_trans )
457                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
458
459             if( !i_trans )
460                 continue;
461
462             /* Blending */
463             p_dst_y[i_x] = vlc_blend( p_src_y[i_x], p_dst_y[i_x], i_trans );
464             if( b_even_scanline && i_x % 2 == 0 )
465             {
466                 p_dst_u[i_x/2] = vlc_blend( p_src_u[i_x], p_dst_u[i_x/2], i_trans );
467                 p_dst_v[i_x/2] = vlc_blend( p_src_v[i_x], p_dst_v[i_x/2], i_trans );
468             }
469         }
470     }
471 }
472
473 static void BlendYUVARV16( filter_t *p_filter,
474                            picture_t *p_dst_pic, picture_t *p_src,
475                            int i_x_offset, int i_y_offset,
476                            int i_width, int i_height, int i_alpha )
477 {
478     int i_src_pitch, i_dst_pitch;
479     uint8_t *p_dst, *p_src_y;
480     uint8_t *p_src_u, *p_src_v;
481     uint8_t *p_trans;
482     int i_x, i_y, i_pix_pitch, i_trans = 0;
483     int r, g, b;
484
485     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
486     i_dst_pitch = p_dst_pic->p->i_pitch;
487     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
488             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
489             p_dst_pic->p->i_pitch *
490             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
491
492     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
493                                0, 0, &p_filter->fmt_in.video, 1 );
494     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
495                                0, 0, &p_filter->fmt_in.video, 2 );
496     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
497                                0, 0, &p_filter->fmt_in.video, 2 );
498     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
499                                0, 0, &p_filter->fmt_in.video, 1 );
500
501     /* Draw until we reach the bottom of the subtitle */
502     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
503          p_dst += i_dst_pitch,
504          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
505          p_src_v += i_src_pitch )
506     {
507         /* Draw until we reach the end of the line */
508         for( i_x = 0; i_x < i_width; i_x++ )
509         {
510             if( p_trans )
511                 i_trans = vlc_alpha( p_trans[i_x], i_alpha );
512             if( !i_trans )
513                 continue;
514
515             /* Blending */
516             yuv_to_rgb( &r, &g, &b,
517                         p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
518
519             vlc_blend_rgb16( (uint16_t*)&p_dst[i_x * i_pix_pitch],
520                              r, g, b, i_trans, &p_filter->fmt_out.video );
521         }
522     }
523 }
524
525 static void BlendYUVARV24( filter_t *p_filter,
526                            picture_t *p_dst_pic, picture_t *p_src,
527                            int i_x_offset, int i_y_offset,
528                            int i_width, int i_height, int i_alpha )
529 {
530     int i_src_pitch, i_dst_pitch;
531     uint8_t *p_dst, *p_src_y;
532     uint8_t *p_src_u, *p_src_v;
533     uint8_t *p_trans;
534     int i_x, i_y, i_pix_pitch, i_trans = 0;
535     int r, g, b;
536
537     i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
538     i_dst_pitch = p_dst_pic->p->i_pitch;
539     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
540             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
541             p_dst_pic->p->i_pitch *
542             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
543
544     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
545                                0, 0, &p_filter->fmt_in.video, 1 );
546     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
547                                0, 0, &p_filter->fmt_in.video, 2 );
548     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
549                                0, 0, &p_filter->fmt_in.video, 2 );
550     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
551                                0, 0, &p_filter->fmt_in.video, 1 );
552
553     if( (i_pix_pitch == 4)
554      && (((((intptr_t)p_dst)|i_dst_pitch) /* FIXME? */
555           & 3) == 0) )
556     {
557         /*
558         ** if picture pixels are 32 bits long and lines addresses are 32 bit
559         ** aligned, optimize rendering
560         */
561         uint32_t *p32_dst = (uint32_t *)p_dst;
562         uint32_t i32_dst_pitch = (uint32_t)(i_dst_pitch>>2);
563
564         int i_rshift, i_gshift, i_bshift;
565         uint32_t i_rmask, i_gmask, i_bmask;
566
567         i_rmask = p_filter->fmt_out.video.i_rmask;
568         i_gmask = p_filter->fmt_out.video.i_gmask;
569         i_bmask = p_filter->fmt_out.video.i_bmask;
570         i_rshift = p_filter->fmt_out.video.i_lrshift;
571         i_gshift = p_filter->fmt_out.video.i_lgshift;
572         i_bshift = p_filter->fmt_out.video.i_lbshift;
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_src_pitch,
576              p32_dst += i32_dst_pitch,
577              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
578              p_src_v += i_src_pitch )
579         {
580             /* Draw until we reach the end of the line */
581             for( i_x = 0; i_x < i_width; i_x++ )
582             {
583                 if( p_trans )
584                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
585                 if( !i_trans )
586                     continue;
587
588                 if( i_trans == MAX_TRANS )
589                 {
590                     /* Completely opaque. Completely overwrite underlying pixel */
591                     yuv_to_rgb( &r, &g, &b,
592                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
593
594                     p32_dst[i_x] = (r<<i_rshift) |
595                                    (g<<i_gshift) |
596                                    (b<<i_bshift);
597                 }
598                 else
599                 {
600                     /* Blending */
601                     uint32_t i_pix_dst = p32_dst[i_x];
602                     yuv_to_rgb( &r, &g, &b,
603                                 p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
604
605                     p32_dst[i_x] = ( vlc_blend( r, (i_pix_dst & i_rmask)>>i_rshift, i_trans ) << i_rshift ) |
606                                    ( vlc_blend( g, (i_pix_dst & i_gmask)>>i_gshift, i_trans ) << i_gshift ) |
607                                    ( vlc_blend( b, (i_pix_dst & i_bmask)>>i_bshift, i_trans ) << i_bshift );
608                 }
609             }
610         }
611     }
612     else
613     {
614         int i_rindex, i_gindex, i_bindex;
615         uint32_t i_rmask, i_gmask, i_bmask;
616
617         i_rmask = p_filter->fmt_out.video.i_rmask;
618         i_gmask = p_filter->fmt_out.video.i_gmask;
619         i_bmask = p_filter->fmt_out.video.i_bmask;
620
621         vlc_rgb_index( &i_rindex, &i_gindex, &i_bindex, &p_filter->fmt_out.video );
622
623         /* Draw until we reach the bottom of the subtitle */
624         for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
625              p_dst += i_dst_pitch,
626              p_src_y += i_src_pitch, p_src_u += i_src_pitch,
627              p_src_v += i_src_pitch )
628         {
629             /* Draw until we reach the end of the line */
630             for( i_x = 0; i_x < i_width; i_x++ )
631             {
632                 if( p_trans )
633                     i_trans = vlc_alpha( p_trans[i_x], i_alpha );
634                 if( !i_trans )
635                     continue;
636
637                 /* Blending */
638                 yuv_to_rgb( &r, &g, &b,
639                             p_src_y[i_x], p_src_u[i_x], p_src_v[i_x] );
640
641                 vlc_blend_packed( &p_dst[ i_x * i_pix_pitch],
642                                   i_rindex, i_gindex, i_bindex,
643                                   r, g, b, i_alpha, true );
644             }
645         }
646     }
647 }
648
649 static void BlendYUVAYUVPacked( filter_t *p_filter,
650                                 picture_t *p_dst_pic, picture_t *p_src,
651                                 int i_x_offset, int i_y_offset,
652                                 int i_width, int i_height, int i_alpha )
653 {
654     int i_src_pitch, i_dst_pitch;
655     uint8_t *p_dst, *p_src_y;
656     uint8_t *p_src_u, *p_src_v;
657     uint8_t *p_trans;
658     int i_x, i_y, i_pix_pitch, i_trans = 0;
659     bool b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
660     int i_l_offset, i_u_offset, i_v_offset;
661
662     vlc_yuv_packed_index( &i_l_offset, &i_u_offset, &i_v_offset,
663                           p_filter->fmt_out.video.i_chroma );
664
665     i_pix_pitch = 2;
666     i_dst_pitch = p_dst_pic->p->i_pitch;
667     p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
668             p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
669             p_dst_pic->p->i_pitch *
670             ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
671
672     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
673                                0, 0, &p_filter->fmt_in.video, 1 );
674     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
675                                0, 0, &p_filter->fmt_in.video, 2 );
676     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
677                                0, 0, &p_filter->fmt_in.video, 2 );
678     p_trans = vlc_plane_start( NULL, p_src, A_PLANE,
679                                0, 0, &p_filter->fmt_in.video, 1 );
680
681     i_width &= ~1; /* Needs to be a multiple of 2 */
682
683     /* Draw until we reach the bottom of the subtitle */
684     for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src_pitch,
685          p_dst += i_dst_pitch,
686          p_src_y += i_src_pitch, p_src_u += i_src_pitch,
687          p_src_v += i_src_pitch )
688     {
689         /* Draw until we reach the end of the line */
690         for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
691         {
692             i_trans = vlc_alpha( p_trans[i_x], i_alpha );
693             if( !i_trans )
694                 continue;
695
696             /* Blending */
697             if( b_even )
698             {
699                 int i_u;
700                 int i_v;
701                 /* FIXME what's with 0xaa ? */
702                 if( p_trans[i_x+1] > 0xaa )
703                 {
704                     i_u = (p_src_u[i_x]+p_src_u[i_x+1])>>1;
705                     i_v = (p_src_v[i_x]+p_src_v[i_x+1])>>1;
706                 }
707                 else
708                 {
709                     i_u = p_src_u[i_x];
710                     i_v = p_src_v[i_x];
711                 }
712
713                 vlc_blend_packed( &p_dst[i_x * 2],
714                                   i_l_offset, i_u_offset, i_v_offset,
715                                   p_src_y[i_x], i_u, i_v, i_trans, true );
716             }
717             else
718             {
719                 p_dst[i_x * 2 + i_l_offset] = vlc_blend( p_src_y[i_x], p_dst[i_x * 2 + i_l_offset], i_trans );
720             }
721         }
722     }
723 }
724 /***********************************************************************
725  * I420, YV12
726  ***********************************************************************/
727 static void BlendI420I420( filter_t *p_filter,
728                            picture_t *p_dst, picture_t *p_src,
729                            int i_x_offset, int i_y_offset,
730                            int i_width, int i_height, int i_alpha )
731 {
732     int i_src_pitch, i_dst_pitch;
733     uint8_t *p_src_y, *p_dst_y;
734     uint8_t *p_src_u, *p_dst_u;
735     uint8_t *p_src_v, *p_dst_v;
736     int i_x, i_y;
737     bool b_even_scanline = i_y_offset % 2;
738
739     if( i_alpha == 0xff )
740     {
741         BlendI420I420_no_alpha( p_filter, p_dst, p_src,
742                                 i_x_offset, i_y_offset, i_width, i_height );
743         return;
744     }
745
746
747     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
748     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
749               p_filter->fmt_out.video.i_x_offset +
750               p_dst->p[Y_PLANE].i_pitch *
751               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
752     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
753               p_filter->fmt_out.video.i_x_offset/2 +
754               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
755               p_dst->p[U_PLANE].i_pitch;
756     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
757               p_filter->fmt_out.video.i_x_offset/2 +
758               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
759               p_dst->p[V_PLANE].i_pitch;
760
761     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
762                                0, 0, &p_filter->fmt_in.video, 1 );
763     p_src_u = vlc_plane_start( NULL, p_src, U_PLANE,
764                                0, 0, &p_filter->fmt_in.video, 2 );
765     p_src_v = vlc_plane_start( NULL, p_src, V_PLANE,
766                                0, 0, &p_filter->fmt_in.video, 2 );
767     i_width &= ~1;
768
769     /* Draw until we reach the bottom of the subtitle */
770     for( i_y = 0; i_y < i_height; i_y++,
771          p_dst_y += i_dst_pitch,
772          p_src_y += i_src_pitch )
773     {
774         if( b_even_scanline )
775         {
776             p_dst_u  += i_dst_pitch/2;
777             p_dst_v  += i_dst_pitch/2;
778         }
779         b_even_scanline = !b_even_scanline;
780
781         /* Draw until we reach the end of the line */
782         for( i_x = 0; i_x < i_width; i_x++ )
783         {
784             if( !i_alpha )
785                 continue;
786
787             /* Blending */
788             p_dst_y[i_x] = vlc_blend( p_src_y[i_x], p_dst_y[i_x], i_alpha );
789             if( b_even_scanline && i_x % 2 == 0 )
790             {
791                 p_dst_u[i_x/2] = vlc_blend( p_src_u[i_x/2], p_dst_u[i_x/2], i_alpha );
792                 p_dst_v[i_x/2] = vlc_blend( p_src_v[i_x/2], p_dst_v[i_x/2], i_alpha );
793             }
794         }
795         if( i_y%2 == 1 )
796         {
797             p_src_u += i_src_pitch/2;
798             p_src_v += i_src_pitch/2;
799         }
800     }
801 }
802 static void BlendI420I420_no_alpha( filter_t *p_filter,
803                                     picture_t *p_dst, picture_t *p_src,
804                                     int i_x_offset, int i_y_offset,
805                                     int i_width, int i_height )
806 {
807     int i_src_pitch, i_dst_pitch;
808     uint8_t *p_src_y, *p_dst_y;
809     uint8_t *p_src_u, *p_dst_u;
810     uint8_t *p_src_v, *p_dst_v;
811     int i_y;
812     bool b_even_scanline = i_y_offset % 2;
813
814     i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
815     p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
816               p_filter->fmt_out.video.i_x_offset +
817               p_dst->p[Y_PLANE].i_pitch *
818               ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
819     p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
820               p_filter->fmt_out.video.i_x_offset/2 +
821               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
822               p_dst->p[U_PLANE].i_pitch;
823     p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
824               p_filter->fmt_out.video.i_x_offset/2 +
825               ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
826               p_dst->p[V_PLANE].i_pitch;
827
828     p_src_y = vlc_plane_start( &i_src_pitch, p_src, Y_PLANE,
829                                0, 0, &p_filter->fmt_in.video, 1 );
830 &nbs