1 /*****************************************************************************
2 * blend.c: alpha blend 2 pictures together
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 static void Blend( filter_t *, picture_t *, picture_t *, picture_t *,
47 static void BlendI420( filter_t *, picture_t *, picture_t *, picture_t *,
49 static void BlendR16( filter_t *, picture_t *, picture_t *, picture_t *,
51 static void BlendR24( filter_t *, picture_t *, picture_t *, picture_t *,
53 static void BlendYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
55 static void BlendPalI420( filter_t *, picture_t *, picture_t *, picture_t *,
57 static void BlendPalYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
59 static void BlendPalRV( filter_t *, picture_t *, picture_t *, picture_t *,
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("Video pictures blending") );
67 set_capability( "video blending", 100 );
68 set_callbacks( OpenFilter, CloseFilter );
71 /*****************************************************************************
72 * OpenFilter: probe the filter and return score
73 *****************************************************************************/
74 static int OpenFilter( vlc_object_t *p_this )
76 filter_t *p_filter = (filter_t*)p_this;
79 /* Check if we can handle that format.
80 * We could try to use a chroma filter if we can't. */
81 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
82 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
83 ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
84 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
85 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
86 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
87 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
88 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
93 /* Allocate the memory needed to store the decoder's structure */
94 if( ( p_filter->p_sys = p_sys =
95 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
97 msg_Err( p_filter, "out of memory" );
102 p_filter->pf_video_blend = Blend;
104 msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
105 (char *)&p_filter->fmt_in.video.i_chroma,
106 (char *)&p_filter->fmt_out.video.i_chroma );
112 /****************************************************************************
113 * Blend: the whole thing
114 ****************************************************************************
115 * This function is called just after the thread is launched.
116 ****************************************************************************/
117 static void Blend( filter_t *p_filter, picture_t *p_dst,
118 picture_t *p_dst_orig, picture_t *p_src,
119 int i_x_offset, int i_y_offset )
121 int i_width, i_height;
123 i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
124 (int)p_filter->fmt_in.video.i_visible_width);
126 i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
127 (int)p_filter->fmt_in.video.i_visible_height);
129 if( i_width <= 0 || i_height <= 0 ) return;
131 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
132 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
133 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
135 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
136 i_x_offset, i_y_offset, i_width, i_height );
139 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
140 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
142 BlendYUY2( p_filter, p_dst, p_dst_orig, p_src,
143 i_x_offset, i_y_offset, i_width, i_height );
146 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
147 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
149 BlendR16( p_filter, p_dst, p_dst_orig, p_src,
150 i_x_offset, i_y_offset, i_width, i_height );
153 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
154 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
155 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
157 BlendR24( p_filter, p_dst, p_dst_orig, p_src,
158 i_x_offset, i_y_offset, i_width, i_height );
161 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
162 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
163 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
165 BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
166 i_x_offset, i_y_offset, i_width, i_height );
169 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
170 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
172 BlendPalYUY2( p_filter, p_dst, p_dst_orig, p_src,
173 i_x_offset, i_y_offset, i_width, i_height );
176 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
177 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
178 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
179 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
181 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
182 i_x_offset, i_y_offset, i_width, i_height );
186 msg_Dbg( p_filter, "no matching alpha blending routine" );
189 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
190 picture_t *p_dst_orig, picture_t *p_src,
191 int i_x_offset, int i_y_offset,
192 int i_width, int i_height )
194 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
195 uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
196 uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
197 uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
200 vlc_bool_t b_even_scanline = i_y_offset % 2;
202 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
203 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
204 p_filter->fmt_out.video.i_x_offset +
205 p_dst->p[Y_PLANE].i_pitch *
206 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
207 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
208 p_filter->fmt_out.video.i_x_offset/2 +
209 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
210 p_dst->p[U_PLANE].i_pitch;
211 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
212 p_filter->fmt_out.video.i_x_offset/2 +
213 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
214 p_dst->p[V_PLANE].i_pitch;
216 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
217 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
218 p_filter->fmt_out.video.i_x_offset +
219 p_dst_orig->p[Y_PLANE].i_pitch *
220 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
221 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
222 p_filter->fmt_out.video.i_x_offset/2 +
223 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
224 p_dst_orig->p[U_PLANE].i_pitch;
225 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
226 p_filter->fmt_out.video.i_x_offset/2 +
227 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
228 p_dst_orig->p[V_PLANE].i_pitch;
230 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
231 p_src2_y = p_src->p[Y_PLANE].p_pixels +
232 p_filter->fmt_in.video.i_x_offset +
233 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
234 p_src2_u = p_src->p[U_PLANE].p_pixels +
235 p_filter->fmt_in.video.i_x_offset/2 +
236 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
237 p_src2_v = p_src->p[V_PLANE].p_pixels +
238 p_filter->fmt_in.video.i_x_offset/2 +
239 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
241 p_trans = p_src->p[A_PLANE].p_pixels +
242 p_filter->fmt_in.video.i_x_offset +
243 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
245 #define MAX_TRANS 255
248 /* Draw until we reach the bottom of the subtitle */
249 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
250 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
251 p_src2_y += i_src2_pitch,
252 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
253 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
254 p_src2_u += i_src2_pitch,
255 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
256 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
257 p_src2_v += i_src2_pitch )
259 b_even_scanline = !b_even_scanline;
261 /* Draw until we reach the end of the line */
262 for( i_x = 0; i_x < i_width; i_x++ )
266 /* Completely transparent. Don't change pixel */
269 else if( p_trans[i_x] == MAX_TRANS )
271 /* Completely opaque. Completely overwrite underlying pixel */
272 p_dst_y[i_x] = p_src2_y[i_x];
274 if( b_even_scanline && i_x % 2 == 0 )
276 p_dst_u[i_x/2] = p_src2_u[i_x];
277 p_dst_v[i_x/2] = p_src2_v[i_x];
283 p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * p_trans[i_x] +
284 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - p_trans[i_x]) )
287 if( b_even_scanline && i_x % 2 == 0 )
289 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * p_trans[i_x] +
290 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - p_trans[i_x]) )
292 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * p_trans[i_x] +
293 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - p_trans[i_x]) )
305 static inline void yuv_to_rgb( int *r, int *g, int *b,
306 uint8_t y1, uint8_t u1, uint8_t v1 )
308 /* macros used for YUV pixel conversions */
309 # define SCALEBITS 10
310 # define ONE_HALF (1 << (SCALEBITS - 1))
311 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
312 # define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
314 int y, cb, cr, r_add, g_add, b_add;
318 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
319 g_add = - FIX(0.34414*255.0/224.0) * cb
320 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
321 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
322 y = (y1 - 16) * FIX(255.0/219.0);
323 *r = CLAMP((y + r_add) >> SCALEBITS);
324 *g = CLAMP((y + g_add) >> SCALEBITS);
325 *b = CLAMP((y + b_add) >> SCALEBITS);
328 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
329 picture_t *p_dst_orig, picture_t *p_src,
330 int i_x_offset, int i_y_offset,
331 int i_width, int i_height )
333 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
334 uint8_t *p_dst, *p_src1, *p_src2_y;
335 uint8_t *p_src2_u, *p_src2_v;
337 int i_x, i_y, i_pix_pitch;
340 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
341 i_dst_pitch = p_dst_pic->p->i_pitch;
342 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
343 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
344 p_dst_pic->p->i_pitch *
345 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
347 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
348 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
349 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
350 p_dst_orig->p->i_pitch *
351 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
353 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
354 p_src2_y = p_src->p[Y_PLANE].p_pixels +
355 p_filter->fmt_in.video.i_x_offset +
356 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
357 p_src2_u = p_src->p[U_PLANE].p_pixels +
358 p_filter->fmt_in.video.i_x_offset/2 +
359 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
360 p_src2_v = p_src->p[V_PLANE].p_pixels +
361 p_filter->fmt_in.video.i_x_offset/2 +
362 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
364 p_trans = p_src->p[A_PLANE].p_pixels +
365 p_filter->fmt_in.video.i_x_offset +
366 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
368 #define MAX_TRANS 255
371 /* Draw until we reach the bottom of the subtitle */
372 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
373 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
374 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
375 p_src2_v += i_src2_pitch )
377 /* Draw until we reach the end of the line */
378 for( i_x = 0; i_x < i_width; i_x++ )
382 /* Completely transparent. Don't change pixel */
385 else if( p_trans[i_x] == MAX_TRANS )
387 /* Completely opaque. Completely overwrite underlying pixel */
388 yuv_to_rgb( &r, &g, &b,
389 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
391 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
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);
409 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
410 picture_t *p_dst_orig, picture_t *p_src,
411 int i_x_offset, int i_y_offset,
412 int i_width, int i_height )
414 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
415 uint8_t *p_dst, *p_src1, *p_src2_y;
416 uint8_t *p_src2_u, *p_src2_v;
418 int i_x, i_y, i_pix_pitch;
421 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
422 i_dst_pitch = p_dst_pic->p->i_pitch;
423 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
424 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
425 p_dst_pic->p->i_pitch *
426 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
428 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
429 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
430 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
431 p_dst_orig->p->i_pitch *
432 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
434 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
435 p_src2_y = p_src->p[Y_PLANE].p_pixels +
436 p_filter->fmt_in.video.i_x_offset +
437 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
438 p_src2_u = p_src->p[U_PLANE].p_pixels +
439 p_filter->fmt_in.video.i_x_offset/2 +
440 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
441 p_src2_v = p_src->p[V_PLANE].p_pixels +
442 p_filter->fmt_in.video.i_x_offset/2 +
443 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
445 p_trans = p_src->p[A_PLANE].p_pixels +
446 p_filter->fmt_in.video.i_x_offset +
447 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
449 #define MAX_TRANS 255
452 /* Draw until we reach the bottom of the subtitle */
453 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
454 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
455 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
456 p_src2_v += i_src2_pitch )
458 /* Draw until we reach the end of the line */
459 for( i_x = 0; i_x < i_width; i_x++ )
463 /* Completely transparent. Don't change pixel */
466 else if( p_trans[i_x] == MAX_TRANS )
468 /* Completely opaque. Completely overwrite underlying pixel */
469 yuv_to_rgb( &r, &g, &b,
470 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
472 p_dst[i_x * i_pix_pitch] = r;
473 p_dst[i_x * i_pix_pitch + 1] = g;
474 p_dst[i_x * i_pix_pitch + 2] = b;
479 yuv_to_rgb( &r, &g, &b,
480 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
482 p_dst[i_x * i_pix_pitch] = ( r * p_trans[i_x] +
483 (uint16_t)p_src1[i_x * i_pix_pitch] *
484 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
485 p_dst[i_x * i_pix_pitch + 1] = ( g * p_trans[i_x] +
486 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
487 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
488 p_dst[i_x * i_pix_pitch + 2] = ( b * p_trans[i_x] +
489 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
490 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
500 static void BlendYUY2( filter_t *p_filter, picture_t *p_dst_pic,
501 picture_t *p_dst_orig, picture_t *p_src,
502 int i_x_offset, int i_y_offset,
503 int i_width, int i_height )
505 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
506 uint8_t *p_dst, *p_src1, *p_src2_y;
507 uint8_t *p_src2_u, *p_src2_v;
509 int i_x, i_y, i_pix_pitch;
512 i_dst_pitch = p_dst_pic->p->i_pitch;
513 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
514 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
515 p_dst_pic->p->i_pitch *
516 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
518 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
519 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
520 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
521 p_dst_orig->p->i_pitch *
522 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
524 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
525 p_src2_y = p_src->p[Y_PLANE].p_pixels +
526 p_filter->fmt_in.video.i_x_offset +
527 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
528 p_src2_u = p_src->p[U_PLANE].p_pixels +
529 p_filter->fmt_in.video.i_x_offset/2 +
530 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
531 p_src2_v = p_src->p[V_PLANE].p_pixels +
532 p_filter->fmt_in.video.i_x_offset/2 +
533 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
535 p_trans = p_src->p[A_PLANE].p_pixels +
536 p_filter->fmt_in.video.i_x_offset +
537 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
539 #define MAX_TRANS 255
542 /* Draw until we reach the bottom of the subtitle */
543 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
544 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
545 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
546 p_src2_v += i_src2_pitch )
548 /* Draw until we reach the end of the line */
549 for( i_x = 0; i_x < i_width; i_x += 2 )
553 /* Completely transparent. Don't change pixel */
555 else if( p_trans[i_x] == MAX_TRANS )
557 /* Completely opaque. Completely overwrite underlying pixel */
558 p_dst[i_x * 2] = p_src2_y[i_x];
559 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
560 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
565 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * p_trans[i_x] +
566 (uint16_t)p_src1[i_x * 2] *
567 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
568 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * p_trans[i_x] +
569 (uint16_t)p_src1[i_x * 2 + 1] *
570 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
571 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * p_trans[i_x] +
572 (uint16_t)p_src1[i_x * 2 + 3] *
573 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
576 if( !p_trans[i_x+1] )
578 /* Completely transparent. Don't change pixel */
580 else if( p_trans[i_x+1] == MAX_TRANS )
582 /* Completely opaque. Completely overwrite underlying pixel */
583 p_dst[i_x * 2 + 2] = p_src2_y[i_x + 1];
588 p_dst[i_x * 2 + 2] = ( (uint16_t)p_src2_y[i_x+1] *
589 p_trans[i_x+1] + (uint16_t)p_src1[i_x * 2 + 2] *
590 (MAX_TRANS - p_trans[i_x+1]) ) >> TRANS_BITS;
601 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
602 picture_t *p_dst_orig, picture_t *p_src,
603 int i_x_offset, int i_y_offset,
604 int i_width, int i_height )
606 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
607 uint8_t *p_src1_y, *p_src2, *p_dst_y;
608 uint8_t *p_src1_u, *p_dst_u;
609 uint8_t *p_src1_v, *p_dst_v;
611 vlc_bool_t b_even_scanline = i_y_offset % 2;
613 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
614 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
615 p_filter->fmt_out.video.i_x_offset +
616 p_dst->p[Y_PLANE].i_pitch *
617 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
618 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
619 p_filter->fmt_out.video.i_x_offset/2 +
620 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
621 p_dst->p[U_PLANE].i_pitch;
622 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
623 p_filter->fmt_out.video.i_x_offset/2 +
624 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
625 p_dst->p[V_PLANE].i_pitch;
627 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
628 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
629 p_filter->fmt_out.video.i_x_offset +
630 p_dst_orig->p[Y_PLANE].i_pitch *
631 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
632 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
633 p_filter->fmt_out.video.i_x_offset/2 +
634 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
635 p_dst_orig->p[U_PLANE].i_pitch;
636 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
637 p_filter->fmt_out.video.i_x_offset/2 +
638 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
639 p_dst_orig->p[V_PLANE].i_pitch;
641 i_src2_pitch = p_src->p->i_pitch;
642 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
643 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
645 #define MAX_TRANS 255
647 #define p_trans p_src2
648 #define p_pal p_filter->fmt_in.video.p_palette->palette
650 /* Draw until we reach the bottom of the subtitle */
651 for( i_y = 0; i_y < i_height; i_y++,
652 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
653 p_src2 += i_src2_pitch,
654 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
655 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
656 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
657 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
659 b_even_scanline = !b_even_scanline;
661 /* Draw until we reach the end of the line */
662 for( i_x = 0; i_x < i_width; i_x++ )
664 if( !p_pal[p_trans[i_x]][3] )
666 /* Completely transparent. Don't change pixel */
669 else if( p_pal[p_trans[i_x]][3] == MAX_TRANS )
671 /* Completely opaque. Completely overwrite underlying pixel */
672 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
674 if( b_even_scanline && i_x % 2 == 0 )
676 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
677 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
683 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
684 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_y[i_x] *
685 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
687 if( b_even_scanline && i_x % 2 == 0 )
689 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
690 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_u[i_x/2] *
691 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
692 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
693 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_v[i_x/2] *
694 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
707 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
708 picture_t *p_dst_orig, picture_t *p_src,
709 int i_x_offset, int i_y_offset,
710 int i_width, int i_height )
712 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
713 uint8_t *p_src1, *p_src2, *p_dst;
714 int i_x, i_y, i_pix_pitch;
717 i_dst_pitch = p_dst_pic->p->i_pitch;
718 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
719 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
720 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
722 i_src1_pitch = p_dst_orig->p->i_pitch;
723 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
724 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
725 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
727 i_src2_pitch = p_src->p->i_pitch;
728 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
729 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
731 #define MAX_TRANS 255
733 #define p_trans p_src2
734 #define p_pal p_filter->fmt_in.video.p_palette->palette
736 /* Draw until we reach the bottom of the subtitle */
737 for( i_y = 0; i_y < i_height; i_y++,
738 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
740 /* Draw until we reach the end of the line */
741 for( i_x = 0; i_x < i_width; i_x += 2 )
743 if( !p_pal[p_trans[i_x]][3] )
745 /* Completely transparent. Don't change pixel */
747 else if( p_pal[p_trans[i_x]][3] == MAX_TRANS )
749 /* Completely opaque. Completely overwrite underlying pixel */
750 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
751 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
752 p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
757 p_dst[i_x * 2] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
758 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2] *
759 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
760 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
761 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2 + 1] *
762 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
763 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
764 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2 + 3] *
765 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
768 if( !p_pal[p_trans[i_x+1]][3] )
770 /* Completely transparent. Don't change pixel */
772 else if( p_pal[p_trans[i_x+1]][3] == MAX_TRANS )
774 /* Completely opaque. Completely overwrite underlying pixel */
775 p_dst[i_x * 2 + 2] = p_pal[p_src2[i_x + 1]][0];
780 p_dst[i_x * 2 + 2] = ( (uint16_t)p_pal[p_src2[i_x+1]][0] *
781 p_pal[p_trans[i_x+1]][3] + (uint16_t)p_src1[i_x * 2 + 2] *
782 (MAX_TRANS - p_pal[p_trans[i_x+1]][3]) ) >> TRANS_BITS;
795 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
796 picture_t *p_dst_orig, picture_t *p_src,
797 int i_x_offset, int i_y_offset,
798 int i_width, int i_height )
800 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
801 uint8_t *p_src1, *p_src2, *p_dst;
802 int i_x, i_y, i_pix_pitch;
804 video_palette_t rgbpalette;
806 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
807 i_dst_pitch = p_dst_pic->p->i_pitch;
808 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
809 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
810 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
812 i_src1_pitch = p_dst_orig->p->i_pitch;
813 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
814 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
815 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
817 i_src2_pitch = p_src->p->i_pitch;
818 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
819 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
821 #define MAX_TRANS 255
823 #define p_trans p_src2
824 #define p_pal p_filter->fmt_in.video.p_palette->palette
825 #define rgbpal rgbpalette.palette
827 /* Convert palette first */
828 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
831 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
833 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
835 *(uint16_t *)rgbpal[i_y] =
836 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
840 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
844 /* Draw until we reach the bottom of the subtitle */
845 for( i_y = 0; i_y < i_height; i_y++,
846 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
848 /* Draw until we reach the end of the line */
849 for( i_x = 0; i_x < i_width; i_x++ )
851 if( !p_pal[p_trans[i_x]][3] )
853 /* Completely transparent. Don't change pixel */
856 else if( p_pal[p_trans[i_x]][3] == MAX_TRANS ||
857 p_filter->fmt_out.video.i_chroma ==
858 VLC_FOURCC('R','V','1','6') )
860 /* Completely opaque. Completely overwrite underlying pixel */
861 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
862 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
863 if( p_filter->fmt_out.video.i_chroma !=
864 VLC_FOURCC('R','V','1','6') )
865 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
870 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
871 p_pal[p_trans[i_x]][3] +
872 (uint16_t)p_src1[i_x * i_pix_pitch] *
873 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
874 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
875 p_pal[p_trans[i_x]][3] +
876 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
877 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
878 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
879 p_pal[p_trans[i_x]][3] +
880 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
881 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
894 /*****************************************************************************
895 * CloseFilter: clean up the filter
896 *****************************************************************************/
897 static void CloseFilter( vlc_object_t *p_this )
899 filter_t *p_filter = (filter_t*)p_this;
900 filter_sys_t *p_sys = p_filter->p_sys;