]> git.sesse.net Git - vlc/blob - modules/video_filter/blendbench.c
269ad72bc41edf1c4337b1f7e0072b4b605b6215
[vlc] / modules / video_filter / blendbench.c
1 /*****************************************************************************
2  * blendbench.c : blending benchmark plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_plugin.h>
34 #include <vlc_sout.h>
35 #include <vlc_vout.h>
36
37 #include "vlc_filter.h"
38 #include "vlc_image.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int Create( vlc_object_t * );
44 static void Destroy( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51
52 #define LOOPS_TEXT N_("Number of time to blend")
53 #define LOOPS_LONGTEXT N_("The number of time the blend will be performed")
54
55 #define ALPHA_TEXT N_("Alpha of the blended image")
56 #define ALPHA_LONGTEXT N_("Alpha with which the blend image is blended")
57
58 #define BASE_IMAGE_TEXT N_("Image to be blended onto")
59 #define BASE_IMAGE_LONGTEXT N_("The image which will be used to blend onto")
60
61 #define BASE_CHROMA_TEXT N_("Chroma for the base image")
62 #define BASE_CHROMA_LONGTEXT N_("Chroma which the base image will be loaded in")
63
64 #define BLEND_IMAGE_TEXT N_("Image which will be blended.")
65 #define BLEND_IMAGE_LONGTEXT N_("The image blended onto the base image")
66
67 #define BLEND_CHROMA_TEXT N_("Chroma for the blend image")
68 #define BLEND_CHROMA_LONGTEXT N_("Chroma which the blend image will be loaded" \
69                                  "in")
70
71 #define CFG_PREFIX "blendbench-"
72
73 vlc_module_begin();
74     set_description( _("Blending benchmark filter") );
75     set_shortname( _("blendbench" ));
76     set_category( CAT_VIDEO );
77     set_subcategory( SUBCAT_VIDEO_VFILTER );
78     set_capability( "video filter2", 0 );
79
80     set_section( N_("Benchmarking"), NULL );
81     add_integer( CFG_PREFIX "loops", 1000, NULL, LOOPS_TEXT,
82               LOOPS_LONGTEXT, false );
83     add_integer_with_range( CFG_PREFIX "alpha", 128, 0, 255, NULL, ALPHA_TEXT,
84               ALPHA_LONGTEXT, false );
85
86     set_section( N_("Base image"), NULL );
87     add_file( CFG_PREFIX "base-image", NULL, NULL, BASE_IMAGE_TEXT,
88               BASE_IMAGE_LONGTEXT, false );
89     add_string( CFG_PREFIX "base-chroma", "I420", NULL, BASE_CHROMA_TEXT,
90               BASE_CHROMA_LONGTEXT, false );
91
92     set_section( N_("Blend image"), NULL );
93     add_file( CFG_PREFIX "blend-image", NULL, NULL, BLEND_IMAGE_TEXT,
94               BLEND_IMAGE_LONGTEXT, false );
95     add_string( CFG_PREFIX "blend-chroma", "YUVA", NULL, BLEND_CHROMA_TEXT,
96               BLEND_CHROMA_LONGTEXT, false );
97
98     set_callbacks( Create, Destroy );
99 vlc_module_end();
100
101 static const char *ppsz_filter_options[] = {
102     "loops", "alpha", "base-image", "base-chroma", "blend-image",
103     "blend-chroma", NULL
104 };
105
106 /*****************************************************************************
107  * filter_sys_t: filter method descriptor
108  *****************************************************************************/
109 struct filter_sys_t
110 {
111     bool b_done;
112     int i_loops, i_alpha;
113
114     picture_t *p_base_image;
115     picture_t *p_blend_image;
116
117     vlc_fourcc_t i_base_chroma;
118     vlc_fourcc_t i_blend_chroma;
119 };
120
121 static int blendbench_LoadImage( vlc_object_t *p_this, picture_t **pp_pic,
122                                  vlc_fourcc_t i_chroma, char *psz_file, const char *psz_name )
123 {
124     image_handler_t *p_image;
125     video_format_t fmt_in, fmt_out;
126
127     memset( &fmt_in, 0, sizeof(video_format_t) );
128     memset( &fmt_out, 0, sizeof(video_format_t) );
129
130     fmt_out.i_chroma = i_chroma;
131     p_image = image_HandlerCreate( p_this );
132     *pp_pic = image_ReadUrl( p_image, psz_file, &fmt_in, &fmt_out );
133     image_HandlerDelete( p_image );
134
135     if( *pp_pic == NULL )
136     {
137         msg_Err( p_this, "Unable to load %s image", psz_name );
138         return VLC_EGENERIC;
139     }
140
141     msg_Dbg( p_this, "%s image has dim %d x %d (Y plane)", psz_name,
142              (*pp_pic)->p[Y_PLANE].i_visible_pitch,
143              (*pp_pic)->p[Y_PLANE].i_visible_lines );
144
145     return VLC_SUCCESS;
146 }
147
148 /*****************************************************************************
149  * Create: allocates video thread output method
150  *****************************************************************************/
151 static int Create( vlc_object_t *p_this )
152 {
153     filter_t *p_filter = (filter_t *)p_this;
154     filter_sys_t *p_sys;
155     char *psz_temp;
156
157     /* Allocate structure */
158     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
159     if( p_filter->p_sys == NULL )
160         return VLC_ENOMEM;
161
162     p_sys = p_filter->p_sys;
163     p_sys->b_done = false;
164
165     p_filter->pf_video_filter = Filter;
166
167     /* needed to get options passed in transcode using the
168      * adjust{name=value} syntax */
169     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
170                        p_filter->p_cfg );
171
172     p_sys->i_loops = var_CreateGetIntegerCommand( p_filter,
173                                                   CFG_PREFIX "loops" );
174     p_sys->i_alpha = var_CreateGetIntegerCommand( p_filter,
175                                                   CFG_PREFIX "alpha" );
176
177     psz_temp = var_CreateGetStringCommand( p_filter, CFG_PREFIX "base-chroma" );
178     p_sys->i_base_chroma = VLC_FOURCC( psz_temp[0], psz_temp[1],
179                                        psz_temp[2], psz_temp[3] );
180     blendbench_LoadImage( p_this, &p_sys->p_base_image, p_sys->i_base_chroma,
181                var_CreateGetStringCommand( p_filter, CFG_PREFIX "base-image" ),
182                "Base" );
183
184     psz_temp = var_CreateGetStringCommand( p_filter,
185                                            CFG_PREFIX "blend-chroma" );
186     p_sys->i_blend_chroma = VLC_FOURCC( psz_temp[0], psz_temp[1],
187                                         psz_temp[2], psz_temp[3] );
188     blendbench_LoadImage( p_this, &p_sys->p_blend_image, p_sys->i_blend_chroma,
189                var_CreateGetStringCommand( p_filter, CFG_PREFIX "blend-image" ),
190                "Blend" );
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Destroy: destroy video thread output method
197  *****************************************************************************/
198 static void Destroy( vlc_object_t *p_this )
199 {
200     filter_t *p_filter = (filter_t *)p_this;
201     filter_sys_t *p_sys = p_filter->p_sys;
202
203     p_sys->p_base_image->pf_release( p_sys->p_base_image );
204     p_sys->p_blend_image->pf_release( p_sys->p_blend_image );
205 }
206
207 /*****************************************************************************
208  * Render: displays previously rendered output
209  *****************************************************************************/
210 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
211 {
212     filter_sys_t *p_sys = p_filter->p_sys;
213     filter_t *p_blend;
214
215     if( p_sys->b_done )
216         return p_pic;
217
218     p_blend = vlc_object_create( p_filter, VLC_OBJECT_FILTER );
219     if( !p_blend )
220     {
221         p_pic->pf_release( p_pic );
222         return NULL;
223     }
224     vlc_object_attach( p_blend, p_filter );
225     p_blend->fmt_out.video = p_sys->p_base_image->format;
226     p_blend->fmt_in.video = p_sys->p_blend_image->format;
227     p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
228     if( !p_blend->p_module )
229     {
230         p_pic->pf_release( p_pic );
231         vlc_object_detach( p_blend );
232         vlc_object_release( p_blend );
233         return NULL;
234     }
235
236     mtime_t time = mdate();
237     for( int i_iter = 0; i_iter < p_sys->i_loops; ++i_iter )
238     {
239         p_blend->pf_video_blend( p_blend, p_sys->p_base_image,
240                                  p_sys->p_base_image, p_sys->p_blend_image,
241                                  0, 0, p_sys->i_alpha );
242     }
243     time = mdate() - time;
244
245     msg_Info( p_filter, "Blended %d images in %f sec.", p_sys->i_loops,
246               time / 1000000.0f );
247     msg_Info( p_filter, "Speed is: %f images/second, %f pixels/second",
248               (float) p_sys->i_loops / time * 1000000,
249               (float) p_sys->i_loops / time * 1000000 *
250                   p_sys->p_blend_image->p[Y_PLANE].i_visible_pitch *
251                   p_sys->p_blend_image->p[Y_PLANE].i_visible_lines );
252
253     module_Unneed( p_blend, p_blend->p_module );
254
255     vlc_object_detach( p_blend );
256     vlc_object_release( p_blend );
257
258     p_sys->b_done = true;
259     return p_pic;
260 }