1 /*****************************************************************************
2 * scale.c: video scaling module for YUVP/A, I420 and RGBA pictures
3 * Uses the low quality "nearest neighbour" algorithm.
4 *****************************************************************************
5 * Copyright (C) 2003-2007 VLC authors and VideoLAN
8 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * Antoine Cellerier <dionoea @t videolan dot org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_picture.h>
38 /****************************************************************************
40 ****************************************************************************/
41 static int OpenFilter ( vlc_object_t
* );
42 static picture_t
*Filter( filter_t
*, picture_t
* );
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( N_("Video scaling filter") )
49 set_capability( "video converter", 10 )
50 set_callbacks( OpenFilter
, NULL
)
53 /*****************************************************************************
54 * OpenFilter: probe the filter and return score
55 *****************************************************************************/
56 static int OpenFilter( vlc_object_t
*p_this
)
58 filter_t
*p_filter
= (filter_t
*)p_this
;
60 if( ( p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_YUVP
&&
61 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_YUVA
&&
62 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_I420
&&
63 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_YV12
&&
64 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_RGB32
&&
65 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_RGBA
&&
66 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_ARGB
) ||
67 p_filter
->fmt_in
.video
.i_chroma
!= p_filter
->fmt_out
.video
.i_chroma
)
72 if( p_filter
->fmt_in
.video
.orientation
!= p_filter
->fmt_out
.video
.orientation
)
75 #warning Converter cannot (really) change output format.
76 video_format_ScaleCropAr( &p_filter
->fmt_out
.video
, &p_filter
->fmt_in
.video
);
77 p_filter
->pf_video_filter
= Filter
;
79 msg_Dbg( p_filter
, "%ix%i -> %ix%i", p_filter
->fmt_in
.video
.i_width
,
80 p_filter
->fmt_in
.video
.i_height
, p_filter
->fmt_out
.video
.i_width
,
81 p_filter
->fmt_out
.video
.i_height
);
86 /****************************************************************************
87 * Filter: the whole thing
88 ****************************************************************************/
89 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
93 if( !p_pic
) return NULL
;
95 #warning Converter cannot (really) change output format.
96 video_format_ScaleCropAr( &p_filter
->fmt_out
.video
, &p_filter
->fmt_in
.video
);
98 /* Request output picture */
99 p_pic_dst
= filter_NewPicture( p_filter
);
102 picture_Release( p_pic
);
106 if( p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_RGBA
&&
107 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_ARGB
&&
108 p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_RGB32
)
110 for( int i_plane
= 0; i_plane
< p_pic_dst
->i_planes
; i_plane
++ )
112 const int i_src_pitch
= p_pic
->p
[i_plane
].i_pitch
;
113 const int i_dst_pitch
= p_pic_dst
->p
[i_plane
].i_pitch
;
114 const int i_src_height
= p_filter
->fmt_in
.video
.i_height
;
115 const int i_src_width
= p_filter
->fmt_in
.video
.i_width
;
116 const int i_dst_height
= p_filter
->fmt_out
.video
.i_height
;
117 const int i_dst_width
= p_filter
->fmt_out
.video
.i_width
;
118 const int i_dst_visible_lines
=
119 p_pic_dst
->p
[i_plane
].i_visible_lines
;
120 const int i_dst_visible_pitch
=
121 p_pic_dst
->p
[i_plane
].i_visible_pitch
;
122 const int i_dst_hidden_pitch
= i_dst_pitch
- i_dst_visible_pitch
;
123 #define SHIFT_SIZE 16
124 const int i_height_coef
= ( i_src_height
<< SHIFT_SIZE
)
126 const int i_width_coef
= ( i_src_width
<< SHIFT_SIZE
)
128 const int i_src_height_1
= i_src_height
- 1;
129 const int i_src_width_1
= i_src_width
- 1;
131 uint8_t *p_src
= p_pic
->p
[i_plane
].p_pixels
;
132 uint8_t *p_dst
= p_pic_dst
->p
[i_plane
].p_pixels
;
133 uint8_t *p_dstendline
= p_dst
+ i_dst_visible_pitch
;
134 const uint8_t *p_dstend
= p_dst
+ i_dst_visible_lines
*i_dst_pitch
;
136 const int i_shift_height
= i_dst_height
/ i_src_height
;
137 const int i_shift_width
= i_dst_width
/ i_src_width
;
139 int l
= 1<<(SHIFT_SIZE
-i_shift_height
);
140 for( ; p_dst
< p_dstend
;
141 p_dst
+= i_dst_hidden_pitch
,
142 p_dstendline
+= i_dst_pitch
, l
+= i_height_coef
)
144 int k
= 1<<(SHIFT_SIZE
-i_shift_width
);
145 uint8_t *p_srcl
= p_src
146 + (__MIN( i_src_height_1
, l
>> SHIFT_SIZE
)*i_src_pitch
);
148 for( ; p_dst
< p_dstendline
; p_dst
++, k
+= i_width_coef
)
150 *p_dst
= p_srcl
[__MIN( i_src_width_1
, k
>> SHIFT_SIZE
)];
157 const int i_src_pitch
= p_pic
->p
->i_pitch
;
158 const int i_dst_pitch
= p_pic_dst
->p
->i_pitch
;
159 const int i_src_height
= p_filter
->fmt_in
.video
.i_height
;
160 const int i_src_width
= p_filter
->fmt_in
.video
.i_width
;
161 const int i_dst_height
= p_filter
->fmt_out
.video
.i_height
;
162 const int i_dst_width
= p_filter
->fmt_out
.video
.i_width
;
163 const int i_dst_visible_lines
=
164 p_pic_dst
->p
->i_visible_lines
;
165 const int i_dst_visible_pitch
=
166 p_pic_dst
->p
->i_visible_pitch
;
167 const int i_dst_hidden_pitch
= i_dst_pitch
- i_dst_visible_pitch
;
168 const int i_height_coef
= ( i_src_height
<< SHIFT_SIZE
)
170 const int i_width_coef
= ( i_src_width
<< SHIFT_SIZE
)
172 const int i_src_height_1
= i_src_height
- 1;
173 const int i_src_width_1
= i_src_width
- 1;
175 uint32_t *p_src
= (uint32_t*)p_pic
->p
->p_pixels
;
176 uint32_t *p_dst
= (uint32_t*)p_pic_dst
->p
->p_pixels
;
177 uint32_t *p_dstendline
= p_dst
+ (i_dst_visible_pitch
>>2);
178 const uint32_t *p_dstend
= p_dst
+ i_dst_visible_lines
*(i_dst_pitch
>>2);
180 const int i_shift_height
= i_dst_height
/ i_src_height
;
181 const int i_shift_width
= i_dst_width
/ i_src_width
;
183 int l
= 1<<(SHIFT_SIZE
-i_shift_height
);
184 for( ; p_dst
< p_dstend
;
185 p_dst
+= (i_dst_hidden_pitch
>>2),
186 p_dstendline
+= (i_dst_pitch
>>2),
189 int k
= 1<<(SHIFT_SIZE
-i_shift_width
);
190 uint32_t *p_srcl
= p_src
191 + (__MIN( i_src_height_1
, l
>> SHIFT_SIZE
)*(i_src_pitch
>>2));
192 for( ; p_dst
< p_dstendline
; p_dst
++, k
+= i_width_coef
)
194 *p_dst
= p_srcl
[__MIN( i_src_width_1
, k
>> SHIFT_SIZE
)];
199 picture_CopyProperties( p_pic_dst
, p_pic
);
200 picture_Release( p_pic
);