also scale I420/YV12. (untested) (btw, why don't we use the "normal" scaling modules...
[vlc.git] / modules / video_filter / scale.c
1 /*****************************************************************************
2  * resize.c: video scaling module for YUVP/A pictures
3  *  Uses the low quality "nearest neighbour" algorithm.
4  *****************************************************************************
5  * Copyright (C) 2003 the VideoLAN team
6  * $Id$
7  *
8  * Author: Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30 #include "vlc_filter.h"
31
32 /*****************************************************************************
33  * filter_sys_t : filter descriptor
34  *****************************************************************************/
35 struct filter_sys_t
36 {
37     es_format_t fmt_in;
38     es_format_t fmt_out;
39 };
40
41 /****************************************************************************
42  * Local prototypes
43  ****************************************************************************/
44 static int  OpenFilter ( vlc_object_t * );
45 static void CloseFilter( vlc_object_t * );
46
47 static picture_t *Filter( filter_t *, picture_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("Video scaling filter") );
54     set_capability( "video filter2", 10000 );
55 //    set_category( CAT_VIDEO );
56 //    set_subcategory( SUBCAT_VIDEO_VFILTER2 );
57     set_callbacks( OpenFilter, CloseFilter );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * OpenFilter: probe the filter and return score
62  *****************************************************************************/
63 static int OpenFilter( vlc_object_t *p_this )
64 {
65     filter_t *p_filter = (filter_t*)p_this;
66     filter_sys_t *p_sys;
67
68     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
69           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
70           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
71           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
72           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') ) ||
73         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
74     {
75         return VLC_EGENERIC;
76     }
77
78     /* Allocate the memory needed to store the decoder's structure */
79     if( ( p_filter->p_sys = p_sys =
80           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
81     {
82         msg_Err( p_filter, "out of memory" );
83         return VLC_EGENERIC;
84     }
85
86     p_filter->pf_video_filter = Filter;
87
88     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
89              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
90              p_filter->fmt_out.video.i_height );
91
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * CloseFilter: clean up the filter
97  *****************************************************************************/
98 static void CloseFilter( vlc_object_t *p_this )
99 {
100     filter_t *p_filter = (filter_t*)p_this;
101     filter_sys_t *p_sys = p_filter->p_sys;
102
103     free( p_sys );
104 }
105
106 /****************************************************************************
107  * Filter: the whole thing
108  ****************************************************************************/
109 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
110 {
111     picture_t *p_pic_dst;
112     int i_plane, i, j, k, l;
113
114     if( !p_pic ) return NULL;
115
116     /* Request output picture */
117     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
118     if( !p_pic_dst )
119     {
120         msg_Warn( p_filter, "can't get output picture" );
121         if( p_pic->pf_release )
122             p_pic->pf_release( p_pic );
123         return NULL;
124     }
125
126     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') )
127     {
128         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
129         {
130             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
131             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
132             int i_src_pitch = p_pic->p[i_plane].i_pitch;
133             int i_dst_pitch = p_pic_dst->p[i_plane].i_pitch;
134
135             for( i = 0; i < p_pic_dst->p[i_plane].i_visible_lines; i++ )
136             {
137                 l = ( p_filter->fmt_in.video.i_height * i +
138                       p_filter->fmt_out.video.i_height / 2 ) /
139                     p_filter->fmt_out.video.i_height;
140
141                 l = __MIN( (int)p_filter->fmt_in.video.i_height - 1, l );
142
143                 for( j = 0; j < p_pic_dst->p[i_plane].i_visible_pitch; j++ )
144                 {
145                     k = ( p_filter->fmt_in.video.i_width * j +
146                           p_filter->fmt_out.video.i_width / 2 ) /
147                         p_filter->fmt_out.video.i_width;
148
149                     k = __MIN( (int)p_filter->fmt_in.video.i_width - 1, k );
150
151                     p_dst[i * i_dst_pitch + j] = p_src[l * i_src_pitch + k];
152                 }
153             }
154         }
155     }
156     else /* RGBA */
157     {
158         uint8_t *p_src = p_pic->p->p_pixels;
159         uint8_t *p_dst = p_pic_dst->p->p_pixels;
160         int i_src_pitch = p_pic->p->i_pitch;
161         int i_dst_pitch = p_pic_dst->p->i_pitch;
162         for( i = 0; i < p_pic_dst->p->i_visible_lines; i++ )
163         {
164             l = ( p_filter->fmt_in.video.i_height * i +
165                   p_filter->fmt_out.video.i_height / 2 ) /
166                 p_filter->fmt_out.video.i_height;
167
168             l = __MIN( (int)p_filter->fmt_in.video.i_height - 1, l );
169
170             for( j = 0; j < p_pic_dst->p->i_visible_pitch/4; j++ )
171             {
172                 k = ( p_filter->fmt_in.video.i_width * j +
173                       p_filter->fmt_out.video.i_width / 2 ) /
174                     p_filter->fmt_out.video.i_width;
175
176                 k = __MIN( (int)p_filter->fmt_in.video.i_width - 1, k );
177
178                 *(uint32_t*)(&p_dst[i * i_dst_pitch + 4*j]) =
179                     *(uint32_t*)(&p_src[l * i_src_pitch + 4*k]);
180             }
181         }
182     }
183
184     p_pic_dst->date = p_pic->date;
185     p_pic_dst->b_force = p_pic->b_force;
186     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
187     p_pic_dst->b_progressive = p_pic->b_progressive;
188     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
189
190     p_pic->pf_release( p_pic );
191     return p_pic_dst;
192 }