1 /*****************************************************************************
2 * blend.c: alpha blend 2 pictures together
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/decoder.h>
29 #include "vlc_filter.h"
31 /*****************************************************************************
32 * filter_sys_t : filter descriptor
33 *****************************************************************************/
39 /****************************************************************************
41 ****************************************************************************/
42 static int OpenFilter ( vlc_object_t * );
43 static void CloseFilter( vlc_object_t * );
45 /* TODO i_alpha support for BlendR16 */
46 static void Blend( filter_t *, picture_t *, picture_t *, picture_t *,
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 );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( _("Video pictures blending") );
68 set_capability( "video blending", 100 );
69 set_callbacks( OpenFilter, CloseFilter );
72 /*****************************************************************************
73 * OpenFilter: probe the filter and return score
74 *****************************************************************************/
75 static int OpenFilter( vlc_object_t *p_this )
77 filter_t *p_filter = (filter_t*)p_this;
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') ) )
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 )
100 msg_Err( p_filter, "out of memory" );
105 p_filter->pf_video_blend = Blend;
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 );
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 )
124 int i_width, i_height;
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);
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);
132 if( i_width <= 0 || i_height <= 0 ) return;
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') ) )
138 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
139 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') ) )
147 BlendYUVPacked( p_filter, p_dst, p_dst_orig, p_src,
148 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') )
154 BlendR16( p_filter, p_dst, p_dst_orig, p_src,
155 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') ) )
162 BlendR24( p_filter, p_dst, p_dst_orig, p_src,
163 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') ) )
170 BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
171 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') ) )
179 BlendPalYUVPacked( p_filter, p_dst, p_dst_orig, p_src,
180 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
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') ) )
188 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
189 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
193 msg_Dbg( p_filter, "no matching alpha blending routine" );
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 )
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;
206 int i_x, i_y, i_trans;
207 vlc_bool_t b_even_scanline = i_y_offset % 2;
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;
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;
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;
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;
252 #define MAX_TRANS 255
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 )
266 b_even_scanline = !b_even_scanline;
268 /* Draw until we reach the end of the line */
269 for( i_x = 0; i_x < i_width; i_x++ )
271 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
274 /* Completely transparent. Don't change pixel */
277 else if( i_trans == MAX_TRANS )
279 /* Completely opaque. Completely overwrite underlying pixel */
280 p_dst_y[i_x] = p_src2_y[i_x];
282 if( b_even_scanline && i_x % 2 == 0 )
284 p_dst_u[i_x/2] = p_src2_u[i_x];
285 p_dst_v[i_x/2] = p_src2_v[i_x];
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) )
295 if( b_even_scanline && i_x % 2 == 0 )
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) )
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) )
313 static inline void yuv_to_rgb( int *r, int *g, int *b,
314 uint8_t y1, uint8_t u1, uint8_t v1 )
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));
322 int y, cb, cr, r_add, g_add, b_add;
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);
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 )
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;
345 int i_x, i_y, i_pix_pitch;
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 );
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 );
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;
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;
376 #define MAX_TRANS 255
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 )
385 /* Draw until we reach the end of the line */
386 for( i_x = 0; i_x < i_width; i_x++ )
390 /* Completely transparent. Don't change pixel */
393 else if( p_trans[i_x] == MAX_TRANS )
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] );
399 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
404 yuv_to_rgb( &r, &g, &b,
405 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
407 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
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 )
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;
426 int i_x, i_y, i_pix_pitch, i_trans;
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 );
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 );
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;
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;
457 #define MAX_TRANS 255
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 )
466 /* Draw until we reach the end of the line */
467 for( i_x = 0; i_x < i_width; i_x++ )
469 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
472 /* Completely transparent. Don't change pixel */
475 else if( i_trans == MAX_TRANS )
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] );
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;
488 yuv_to_rgb( &r, &g, &b,
489 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
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;
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 )
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;
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;
522 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
528 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
534 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') )
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 );
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 );
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;
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;
569 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
571 #define MAX_TRANS 255
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 )
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 )
583 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
586 /* Completely transparent. Don't change pixel */
588 else if( i_trans == MAX_TRANS )
590 /* Completely opaque. Completely overwrite underlying pixel */
591 p_dst[i_x * 2 + i_l_offset] = p_src2_y[i_x];
595 if( p_trans[i_x+1] > 0xaa )
597 p_dst[i_x * 2 + i_u_offset] = (p_src2_u[i_x]+p_src2_u[i_x+1])>>1;
598 p_dst[i_x * 2 + i_v_offset] = (p_src2_v[i_x]+p_src2_v[i_x+1])>>1;
602 p_dst[i_x * 2 + i_u_offset] = p_src2_u[i_x];
603 p_dst[i_x * 2 + i_v_offset] = p_src2_v[i_x];
610 p_dst[i_x * 2 + i_l_offset] = ( (uint16_t)p_src2_y[i_x] * i_trans +
611 (uint16_t)p_src1[i_x * 2 + i_l_offset] * (MAX_TRANS - i_trans) )
618 if( p_trans[i_x+1] > 0xaa )
620 i_u = (p_src2_u[i_x]+p_src2_u[i_x+1])>>1;
621 i_v = (p_src2_v[i_x]+p_src2_v[i_x+1])>>1;
628 p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)i_u * i_trans +
629 (uint16_t)p_src1[i_x * 2 + i_u_offset] * (MAX_TRANS - i_trans) )
631 p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)i_v * i_trans +
632 (uint16_t)p_src1[i_x * 2 + i_v_offset] * (MAX_TRANS - i_trans) )
645 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
646 picture_t *p_dst_orig, picture_t *p_src,
647 int i_x_offset, int i_y_offset,
648 int i_width, int i_height, int i_alpha )
650 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
651 uint8_t *p_src1_y, *p_src2, *p_dst_y;
652 uint8_t *p_src1_u, *p_dst_u;
653 uint8_t *p_src1_v, *p_dst_v;
654 int i_x, i_y, i_trans;
655 vlc_bool_t b_even_scanline = i_y_offset % 2;
657 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
658 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
659 p_filter->fmt_out.video.i_x_offset +
660 p_dst->p[Y_PLANE].i_pitch *
661 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
662 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
663 p_filter->fmt_out.video.i_x_offset/2 +
664 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
665 p_dst->p[U_PLANE].i_pitch;
666 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
667 p_filter->fmt_out.video.i_x_offset/2 +
668 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
669 p_dst->p[V_PLANE].i_pitch;
671 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
672 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
673 p_filter->fmt_out.video.i_x_offset +
674 p_dst_orig->p[Y_PLANE].i_pitch *
675 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
676 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
677 p_filter->fmt_out.video.i_x_offset/2 +
678 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
679 p_dst_orig->p[U_PLANE].i_pitch;
680 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
681 p_filter->fmt_out.video.i_x_offset/2 +
682 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
683 p_dst_orig->p[V_PLANE].i_pitch;
685 i_src2_pitch = p_src->p->i_pitch;
686 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
687 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
689 #define MAX_TRANS 255
691 #define p_trans p_src2
692 #define p_pal p_filter->fmt_in.video.p_palette->palette
694 /* Draw until we reach the bottom of the subtitle */
695 for( i_y = 0; i_y < i_height; i_y++,
696 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
697 p_src2 += i_src2_pitch,
698 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
699 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
700 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
701 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
703 b_even_scanline = !b_even_scanline;
705 /* Draw until we reach the end of the line */
706 for( i_x = 0; i_x < i_width; i_x++ )
708 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
711 /* Completely transparent. Don't change pixel */
714 else if( i_trans == MAX_TRANS )
716 /* Completely opaque. Completely overwrite underlying pixel */
717 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
719 if( b_even_scanline && i_x % 2 == 0 )
721 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
722 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
728 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
729 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
732 if( b_even_scanline && i_x % 2 == 0 )
734 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
735 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
737 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
738 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
752 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
753 picture_t *p_dst_orig, picture_t *p_src,
754 int i_x_offset, int i_y_offset,
755 int i_width, int i_height, int i_alpha )
757 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
758 uint8_t *p_src1, *p_src2, *p_dst;
759 int i_x, i_y, i_pix_pitch, i_trans;
760 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
761 int i_l_offset = 0, i_u_offset = 0, i_v_offset = 0;
763 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
769 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
775 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') )
783 i_dst_pitch = p_dst_pic->p->i_pitch;
784 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
785 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
786 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
788 i_src1_pitch = p_dst_orig->p->i_pitch;
789 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
790 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
791 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
793 i_src2_pitch = p_src->p->i_pitch;
794 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
795 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
797 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
799 #define MAX_TRANS 255
801 #define p_trans p_src2
802 #define p_pal p_filter->fmt_in.video.p_palette->palette
804 /* Draw until we reach the bottom of the subtitle */
805 for( i_y = 0; i_y < i_height; i_y++,
806 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
808 /* Draw until we reach the end of the line */
809 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
811 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
814 /* Completely transparent. Don't change pixel */
816 else if( i_trans == MAX_TRANS )
818 /* Completely opaque. Completely overwrite underlying pixel */
819 p_dst[i_x * 2 + i_l_offset] = p_pal[p_src2[i_x]][0];
823 if( p_trans[i_x+1] > 0xaa )
825 p_dst[i_x * 2 + i_u_offset] = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
826 p_dst[i_x * 2 + i_v_offset] = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
830 p_dst[i_x * 2 + i_u_offset] = p_pal[p_src2[i_x]][1];
831 p_dst[i_x * 2 + i_v_offset] = p_pal[p_src2[i_x]][2];
838 p_dst[i_x * 2 + i_l_offset] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
839 i_trans + (uint16_t)p_src1[i_x * 2 + i_l_offset] *
840 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
846 if( p_trans[i_x+1] > 0xaa )
848 i_u = (p_pal[p_src2[i_x]][1] + p_pal[p_src2[i_x+1]][1]) >> 1;
849 i_v = (p_pal[p_src2[i_x]][2] + p_pal[p_src2[i_x+1]][2]) >> 1;
853 i_u = p_pal[p_src2[i_x]][1];
854 i_v = p_pal[p_src2[i_x]][2];
857 p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)i_u *
858 i_trans + (uint16_t)p_src1[i_x * 2 + i_u_offset] *
859 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
860 p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)i_v *
861 i_trans + (uint16_t)p_src1[i_x * 2 + i_v_offset] *
862 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
876 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
877 picture_t *p_dst_orig, picture_t *p_src,
878 int i_x_offset, int i_y_offset,
879 int i_width, int i_height, int i_alpha )
881 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
882 uint8_t *p_src1, *p_src2, *p_dst;
883 int i_x, i_y, i_pix_pitch, i_trans;
885 video_palette_t rgbpalette;
887 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
888 i_dst_pitch = p_dst_pic->p->i_pitch;
889 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
890 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
891 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
893 i_src1_pitch = p_dst_orig->p->i_pitch;
894 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
895 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
896 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
898 i_src2_pitch = p_src->p->i_pitch;
899 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
900 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
902 #define MAX_TRANS 255
904 #define p_trans p_src2
905 #define p_pal p_filter->fmt_in.video.p_palette->palette
906 #define rgbpal rgbpalette.palette
908 /* Convert palette first */
909 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
912 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
914 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
916 *(uint16_t *)rgbpal[i_y] =
917 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
921 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
925 /* Draw until we reach the bottom of the subtitle */
926 for( i_y = 0; i_y < i_height; i_y++,
927 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
929 /* Draw until we reach the end of the line */
930 for( i_x = 0; i_x < i_width; i_x++ )
932 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
935 /* Completely transparent. Don't change pixel */
938 else if( i_trans == MAX_TRANS ||
939 p_filter->fmt_out.video.i_chroma ==
940 VLC_FOURCC('R','V','1','6') )
942 /* Completely opaque. Completely overwrite underlying pixel */
943 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
944 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
945 if( p_filter->fmt_out.video.i_chroma !=
946 VLC_FOURCC('R','V','1','6') )
947 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
952 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
953 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
954 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
955 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
956 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
957 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
958 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
959 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
960 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
973 /*****************************************************************************
974 * CloseFilter: clean up the filter
975 *****************************************************************************/
976 static void CloseFilter( vlc_object_t *p_this )
978 filter_t *p_filter = (filter_t*)p_this;
979 filter_sys_t *p_sys = p_filter->p_sys;