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 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];
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) )
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) )
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) )
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 )
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;
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;
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;
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;
669 #define MAX_TRANS 255
671 #define p_trans p_src2
672 #define p_pal p_filter->fmt_in.video.p_palette->palette
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 )
683 b_even_scanline = !b_even_scanline;
685 /* Draw until we reach the end of the line */
686 for( i_x = 0; i_x < i_width; i_x++ )
688 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
691 /* Completely transparent. Don't change pixel */
694 else if( i_trans == MAX_TRANS )
696 /* Completely opaque. Completely overwrite underlying pixel */
697 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
699 if( b_even_scanline && i_x % 2 == 0 )
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];
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) )
712 if( b_even_scanline && i_x % 2 == 0 )
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) )
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) )
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 )
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;
743 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
749 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
755 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','Y','U') )
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 );
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 );
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;
777 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
779 #define MAX_TRANS 255
781 #define p_trans p_src2
782 #define p_pal p_filter->fmt_in.video.p_palette->palette
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 )
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 )
791 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
794 /* Completely transparent. Don't change pixel */
796 else if( i_trans == MAX_TRANS )
798 /* Completely opaque. Completely overwrite underlying pixel */
799 p_dst[i_x * 2 + i_l_offset] = p_pal[p_src2[i_x]][0];
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];
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;
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;
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 )
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;
844 video_palette_t rgbpalette;
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 );
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 );
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;
861 #define MAX_TRANS 255
863 #define p_trans p_src2
864 #define p_pal p_filter->fmt_in.video.p_palette->palette
865 #define rgbpal rgbpalette.palette
867 /* Convert palette first */
868 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
871 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
873 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
875 *(uint16_t *)rgbpal[i_y] =
876 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
880 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
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 )
888 /* Draw until we reach the end of the line */
889 for( i_x = 0; i_x < i_width; i_x++ )
891 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
894 /* Completely transparent. Don't change pixel */
897 else if( i_trans == MAX_TRANS ||
898 p_filter->fmt_out.video.i_chroma ==
899 VLC_FOURCC('R','V','1','6') )
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];
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;
932 /*****************************************************************************
933 * CloseFilter: clean up the filter
934 *****************************************************************************/
935 static void CloseFilter( vlc_object_t *p_this )
937 filter_t *p_filter = (filter_t*)p_this;
938 filter_sys_t *p_sys = p_filter->p_sys;