]> git.sesse.net Git - vlc/blob - modules/video_filter/scale.c
* modules/video_filter/scale.c: simple nearest neighbour rescaling module for YUVP...
[vlc] / 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 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/decoder.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_callbacks( OpenFilter, CloseFilter );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * OpenFilter: probe the filter and return score
60  *****************************************************************************/
61 static int OpenFilter( vlc_object_t *p_this )
62 {
63     filter_t *p_filter = (filter_t*)p_this;
64     filter_sys_t *p_sys;
65
66     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
67           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') ) ||
68         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
69     {
70         return VLC_EGENERIC;
71     }
72
73     /* Allocate the memory needed to store the decoder's structure */
74     if( ( p_filter->p_sys = p_sys =
75           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
76     {
77         msg_Err( p_filter, "out of memory" );
78         return VLC_EGENERIC;
79     }
80
81     p_filter->pf_video_filter = Filter;
82
83     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
84              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
85              p_filter->fmt_out.video.i_height );
86
87     return VLC_SUCCESS;
88 }
89
90 /*****************************************************************************
91  * CloseFilter: clean up the filter
92  *****************************************************************************/
93 static void CloseFilter( vlc_object_t *p_this )
94 {
95     filter_t *p_filter = (filter_t*)p_this;
96     filter_sys_t *p_sys = p_filter->p_sys;
97
98     free( p_sys );
99 }
100
101 /****************************************************************************
102  * Filter: the whole thing
103  ****************************************************************************/
104 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
105 {
106     filter_sys_t *p_sys = p_filter->p_sys;
107     picture_t *p_pic_dst;
108     int i_plane, i, j, k, l;
109
110     /* Request output picture */
111     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
112     if( !p_pic_dst )
113     {
114         msg_Warn( p_filter, "can't get output picture" );
115         return NULL;
116     }
117
118     for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
119     {
120         uint8_t *p_src = p_pic->p[i_plane].p_pixels;
121         uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
122         int i_src_pitch = p_pic->p[i_plane].i_pitch;
123         int i_dst_pitch = p_pic_dst->p[i_plane].i_pitch;
124
125         for( i = 0; i < p_pic_dst->p[i_plane].i_visible_lines; i++ )
126         {
127             l = p_filter->fmt_in.video.i_height * i /
128                 p_filter->fmt_out.video.i_height;
129
130             for( j = 0; j < p_pic_dst->p[i_plane].i_visible_pitch; j++ )
131             {
132                 k = p_filter->fmt_in.video.i_width * j /
133                     p_filter->fmt_out.video.i_width;
134
135                 p_dst[i * i_dst_pitch + j] = p_src[l * i_src_pitch + k];
136             }
137         }
138     }
139
140     p_pic_dst->date = p_pic->date;
141     p_pic_dst->b_force = p_pic->b_force;
142     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
143     p_pic_dst->b_progressive = p_pic->b_progressive;
144     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
145
146     p_pic->pf_release( p_pic );
147     return p_pic_dst;
148 }