1 /*****************************************************************************
2 * croppadd.c: Crop/Padd image filter
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea @t videolan dot 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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include "vlc_filter.h"
35 #include "filter_picture.h"
37 /****************************************************************************
39 ****************************************************************************/
40 static int OpenFilter ( vlc_object_t * );
41 static void CloseFilter( vlc_object_t * );
43 static picture_t *Filter( filter_t *, picture_t * );
45 /*****************************************************************************
47 *****************************************************************************/
49 set_description( N_("Video scaling filter") );
50 set_capability( "video filter2", 0 );
51 set_callbacks( OpenFilter, CloseFilter );
54 /*****************************************************************************
55 * OpenFilter: probe the filter and return score
56 *****************************************************************************/
57 static int OpenFilter( vlc_object_t *p_this )
59 filter_t *p_filter = (filter_t*)p_this;
61 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
66 p_filter->pf_video_filter = Filter;
68 msg_Dbg( p_filter, "%ix%i + %ix%i -> %ix%i + %ix%i",
69 p_filter->fmt_in.video.i_visible_width,
70 p_filter->fmt_in.video.i_visible_height,
71 p_filter->fmt_in.video.i_x_offset,
72 p_filter->fmt_in.video.i_y_offset,
73 p_filter->fmt_out.video.i_visible_width,
74 p_filter->fmt_out.video.i_visible_height,
75 p_filter->fmt_out.video.i_x_offset,
76 p_filter->fmt_out.video.i_y_offset );
81 /*****************************************************************************
82 * CloseFilter: clean up the filter
83 *****************************************************************************/
84 static void CloseFilter( vlc_object_t *p_this )
89 /****************************************************************************
90 * Filter: the whole thing
91 ****************************************************************************/
92 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
96 int i_width, i_height, i_xcrop, i_ycrop,
97 i_outwidth, i_outheight, i_xpadd, i_ypadd;
99 const int p_padd_color[] = { 0x00, 0x80, 0x80, 0xff };
101 if( !p_pic ) return NULL;
103 /* Request output picture */
104 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
107 msg_Warn( p_filter, "can't get output picture" );
108 if( p_pic->pf_release )
109 p_pic->pf_release( p_pic );
113 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
114 /* p_pic and p_outpic have the same chroma/number of planes but that's
117 plane_t *p_plane = p_pic->p+i_plane;
118 plane_t *p_outplane = p_outpic->p+i_plane;
119 uint8_t *p_in = p_plane->p_pixels;
120 uint8_t *p_out = p_outplane->p_pixels;
121 int i_pixel_pitch = p_plane->i_pixel_pitch;
122 int i_padd_color = i_plane > 3 ? p_padd_color[0]
123 : p_padd_color[i_plane];
125 /* These assignments assume that the first plane always has
126 * a width and height equal to the picture's */
127 i_width = ( p_filter->fmt_in.video.i_visible_width
128 * p_plane->i_visible_pitch )
129 / p_pic->p->i_visible_pitch;
130 i_height = ( p_filter->fmt_in.video.i_visible_height
131 * p_plane->i_visible_lines )
132 / p_pic->p->i_visible_lines;
133 i_xcrop = ( p_filter->fmt_in.video.i_x_offset
134 * p_plane->i_visible_pitch)
135 / p_pic->p->i_visible_pitch;
136 i_ycrop = ( p_filter->fmt_in.video.i_y_offset
137 * p_plane->i_visible_lines)
138 / p_pic->p->i_visible_lines;
139 i_outwidth = ( p_filter->fmt_out.video.i_visible_width
140 * p_outplane->i_visible_pitch )
141 / p_outpic->p->i_visible_pitch;
142 i_outheight = ( p_filter->fmt_out.video.i_visible_height
143 * p_outplane->i_visible_lines )
144 / p_outpic->p->i_visible_lines;
145 i_xpadd = ( p_filter->fmt_out.video.i_x_offset
146 * p_outplane->i_visible_pitch )
147 / p_outpic->p->i_visible_pitch;
148 i_ypadd = ( p_filter->fmt_out.video.i_y_offset
149 * p_outplane->i_visible_lines )
150 / p_outpic->p->i_visible_lines;
153 p_in += i_ycrop * p_plane->i_pitch;
155 /* Padd on the top */
156 vlc_memset( p_out, i_padd_color, i_ypadd * p_outplane->i_pitch );
157 p_out += i_ypadd * p_outplane->i_pitch;
160 for( i_line = 0; i_line < i_height; i_line++ )
162 uint8_t *p_in_next = p_in + p_plane->i_pitch;
163 uint8_t *p_out_next = p_out + p_outplane->i_pitch;
165 /* Crop on the left */
166 p_in += i_xcrop * i_pixel_pitch;
168 /* Padd on the left */
169 vlc_memset( p_out, i_padd_color, i_xpadd * i_pixel_pitch );
170 p_out += i_xpadd * i_pixel_pitch;
172 /* Copy the image and crop on the right */
173 vlc_memcpy( p_out, p_in, i_width * i_pixel_pitch );
174 p_out += i_width * i_pixel_pitch;
175 p_in += i_width * i_pixel_pitch;
177 /* Padd on the right */
178 vlc_memset( p_out, i_padd_color,
179 ( i_outwidth - i_width ) * i_pixel_pitch );
181 /* Got to begining of the next line */
186 /* Padd on the bottom */
187 vlc_memset( p_out, i_padd_color,
188 ( i_outheight - i_ypadd - i_height ) * p_outplane->i_pitch );
191 return CopyInfoAndRelease( p_outpic, p_pic );