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