| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <limits.h> |
|---|
| 33 |
|
|---|
| 34 |
#include <vlc_common.h> |
|---|
| 35 |
#include <vlc_plugin.h> |
|---|
| 36 |
#include <vlc_filter.h> |
|---|
| 37 |
#include <vlc_vout.h> |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
static int Activate( vlc_object_t * ); |
|---|
| 43 |
static void Destroy( vlc_object_t * ); |
|---|
| 44 |
static picture_t *Filter( filter_t *, picture_t * ); |
|---|
| 45 |
static int alloc_init( filter_t *, void * ); |
|---|
| 46 |
|
|---|
| 47 |
#define WIDTH_TEXT N_( "Image width" ) |
|---|
| 48 |
#define WIDTH_LONGTEXT N_( \ |
|---|
| 49 |
"Image width" ) |
|---|
| 50 |
#define HEIGHT_TEXT N_( "Image height" ) |
|---|
| 51 |
#define HEIGHT_LONGTEXT N_( \ |
|---|
| 52 |
"Image height" ) |
|---|
| 53 |
#define ASPECT_TEXT N_( "Aspect ratio" ) |
|---|
| 54 |
#define ASPECT_LONGTEXT N_( \ |
|---|
| 55 |
"Set aspect (like 4:3) of the video canvas" ) |
|---|
| 56 |
#define PADD_TEXT N_( "Padd video" ) |
|---|
| 57 |
#define PADD_LONGTEXT N_( \ |
|---|
| 58 |
"If enabled, video will be padded to fit in canvas after scaling. " \ |
|---|
| 59 |
"Otherwise, video will be cropped to fix in canvas after scaling." ) |
|---|
| 60 |
|
|---|
| 61 |
#define CFG_PREFIX "canvas-" |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
vlc_module_begin(); |
|---|
| 67 |
set_description( N_("Automatically resize and padd a video") ); |
|---|
| 68 |
set_capability( "video filter2", 0 ); |
|---|
| 69 |
set_callbacks( Activate, Destroy ); |
|---|
| 70 |
|
|---|
| 71 |
add_integer_with_range( CFG_PREFIX "width", 0, 0, INT_MAX, NULL, |
|---|
| 72 |
WIDTH_TEXT, WIDTH_LONGTEXT, false ); |
|---|
| 73 |
add_integer_with_range( CFG_PREFIX "height", 0, 0, INT_MAX, NULL, |
|---|
| 74 |
HEIGHT_TEXT, HEIGHT_LONGTEXT, false ); |
|---|
| 75 |
|
|---|
| 76 |
add_string( CFG_PREFIX "aspect", "4:3", NULL, |
|---|
| 77 |
ASPECT_TEXT, ASPECT_LONGTEXT, false ); |
|---|
| 78 |
|
|---|
| 79 |
add_bool( CFG_PREFIX "padd", true, NULL, |
|---|
| 80 |
PADD_TEXT, PADD_LONGTEXT, false ); |
|---|
| 81 |
vlc_module_end(); |
|---|
| 82 |
|
|---|
| 83 |
static const char *const ppsz_filter_options[] = { |
|---|
| 84 |
"width", "height", "aspect", "padd", NULL |
|---|
| 85 |
}; |
|---|
| 86 |
|
|---|
| 87 |
struct filter_sys_t |
|---|
| 88 |
{ |
|---|
| 89 |
filter_chain_t *p_chain; |
|---|
| 90 |
}; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
static int Activate( vlc_object_t *p_this ) |
|---|
| 96 |
{ |
|---|
| 97 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 98 |
unsigned int i_width, i_height; |
|---|
| 99 |
es_format_t fmt; |
|---|
| 100 |
char psz_croppadd[100]; |
|---|
| 101 |
int i_padd,i_offset; |
|---|
| 102 |
char *psz_aspect, *psz_parser; |
|---|
| 103 |
int i_aspect; |
|---|
| 104 |
bool b_padd; |
|---|
| 105 |
|
|---|
| 106 |
if( !p_filter->b_allow_fmt_out_change ) |
|---|
| 107 |
{ |
|---|
| 108 |
msg_Err( p_filter, "Picture format change isn't allowed" ); |
|---|
| 109 |
return VLC_EGENERIC; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma ) |
|---|
| 113 |
{ |
|---|
| 114 |
msg_Err( p_filter, "Input and output chromas don't match" ); |
|---|
| 115 |
return VLC_EGENERIC; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options, |
|---|
| 119 |
p_filter->p_cfg ); |
|---|
| 120 |
|
|---|
| 121 |
i_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" ); |
|---|
| 122 |
i_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" ); |
|---|
| 123 |
|
|---|
| 124 |
if( i_width == 0 || i_height == 0 ) |
|---|
| 125 |
{ |
|---|
| 126 |
msg_Err( p_filter, "Width and height options must be set" ); |
|---|
| 127 |
return VLC_EGENERIC; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
if( i_width & 1 || i_height & 1 ) |
|---|
| 131 |
{ |
|---|
| 132 |
msg_Err( p_filter, "Width and height options must be even integers" ); |
|---|
| 133 |
return VLC_EGENERIC; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" ); |
|---|
| 137 |
if( !psz_aspect ) |
|---|
| 138 |
{ |
|---|
| 139 |
msg_Err( p_filter, "Aspect ratio must be set" ); |
|---|
| 140 |
return VLC_EGENERIC; |
|---|
| 141 |
} |
|---|
| 142 |
psz_parser = strchr( psz_aspect, ':' ); |
|---|
| 143 |
if( psz_parser ) psz_parser++; |
|---|
| 144 |
if( psz_parser && atoi( psz_parser ) > 0 ) |
|---|
| 145 |
i_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR / atoi( psz_parser ); |
|---|
| 146 |
else |
|---|
| 147 |
i_aspect = atof( psz_aspect ) * VOUT_ASPECT_FACTOR; |
|---|
| 148 |
free( psz_aspect ); |
|---|
| 149 |
|
|---|
| 150 |
if( i_aspect <= 0 ) |
|---|
| 151 |
{ |
|---|
| 152 |
msg_Err( p_filter, "Aspect ratio must be strictly positive" ); |
|---|
| 153 |
return VLC_EGENERIC; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" ); |
|---|
| 157 |
|
|---|
| 158 |
filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) ); |
|---|
| 159 |
if( !p_sys ) |
|---|
| 160 |
return VLC_ENOMEM; |
|---|
| 161 |
p_filter->p_sys = p_sys; |
|---|
| 162 |
|
|---|
| 163 |
p_sys->p_chain = filter_chain_New( p_filter, "video filter2", true, |
|---|
| 164 |
alloc_init, NULL, p_filter ); |
|---|
| 165 |
if( !p_sys->p_chain ) |
|---|
| 166 |
{ |
|---|
| 167 |
msg_Err( p_filter, "Could not allocate filter chain" ); |
|---|
| 168 |
free( p_sys ); |
|---|
| 169 |
return VLC_EGENERIC; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
es_format_Copy( &fmt, &p_filter->fmt_in ); |
|---|
| 173 |
|
|---|
| 174 |
fmt.video.i_width = i_width; |
|---|
| 175 |
fmt.video.i_height = ( p_filter->fmt_in.video.i_height * i_width ) |
|---|
| 176 |
/ p_filter->fmt_in.video.i_width; |
|---|
| 177 |
fmt.video.i_height = ( fmt.video.i_height * i_aspect ) |
|---|
| 178 |
/ p_filter->fmt_in.video.i_aspect; |
|---|
| 179 |
|
|---|
| 180 |
if( b_padd ) |
|---|
| 181 |
{ |
|---|
| 182 |
|
|---|
| 183 |
if( fmt.video.i_height > i_height ) |
|---|
| 184 |
{ |
|---|
| 185 |
fmt.video.i_height = i_height; |
|---|
| 186 |
fmt.video.i_width = ( p_filter->fmt_in.video.i_width * i_height ) |
|---|
| 187 |
/ p_filter->fmt_in.video.i_height; |
|---|
| 188 |
fmt.video.i_width = ( fmt.video.i_width * p_filter->fmt_in.video.i_aspect ) |
|---|
| 189 |
/ i_aspect; |
|---|
| 190 |
if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1; |
|---|
| 191 |
|
|---|
| 192 |
i_padd = (i_width - fmt.video.i_width) / 2; |
|---|
| 193 |
i_offset = (i_padd & 1); |
|---|
| 194 |
|
|---|
| 195 |
snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}", |
|---|
| 196 |
i_padd - i_offset, i_padd + i_offset ); |
|---|
| 197 |
} |
|---|
| 198 |
else |
|---|
| 199 |
{ |
|---|
| 200 |
if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1; |
|---|
| 201 |
i_padd = (i_height - fmt.video.i_height ) / 2; |
|---|
| 202 |
i_offset = (i_padd & 1); |
|---|
| 203 |
|
|---|
| 204 |
snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}", |
|---|
| 205 |
i_padd - i_offset, i_padd + i_offset ); |
|---|
| 206 |
} |
|---|
| 207 |
} |
|---|
| 208 |
else |
|---|
| 209 |
{ |
|---|
| 210 |
|
|---|
| 211 |
if( fmt.video.i_height < i_height ) |
|---|
| 212 |
{ |
|---|
| 213 |
fmt.video.i_height = i_height; |
|---|
| 214 |
fmt.video.i_width = ( p_filter->fmt_in.video.i_width * i_height ) |
|---|
| 215 |
/ p_filter->fmt_in.video.i_height; |
|---|
| 216 |
fmt.video.i_width = ( fmt.video.i_width * p_filter->fmt_in.video.i_aspect ) |
|---|
| 217 |
/ i_aspect; |
|---|
| 218 |
if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1; |
|---|
| 219 |
|
|---|
| 220 |
i_padd = (fmt.video.i_width - i_width) / 2; |
|---|
| 221 |
i_offset = (i_padd & 1); |
|---|
| 222 |
|
|---|
| 223 |
snprintf( psz_croppadd, 100, "croppadd{cropleft=%d,cropright=%d}", |
|---|
| 224 |
i_padd - i_offset, i_padd + i_offset ); |
|---|
| 225 |
} |
|---|
| 226 |
else |
|---|
| 227 |
{ |
|---|
| 228 |
if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1; |
|---|
| 229 |
i_padd = (fmt.video.i_height - i_height) / 2; |
|---|
| 230 |
i_offset = (i_padd & 1); |
|---|
| 231 |
|
|---|
| 232 |
snprintf( psz_croppadd, 100, "croppadd{croptop=%d,cropbottom=%d}", |
|---|
| 233 |
i_padd - i_offset, i_padd + i_offset ); |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
fmt.video.i_visible_width = fmt.video.i_width; |
|---|
| 238 |
fmt.video.i_visible_height = fmt.video.i_height; |
|---|
| 239 |
|
|---|
| 240 |
filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt ); |
|---|
| 241 |
|
|---|
| 242 |
filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL ); |
|---|
| 243 |
|
|---|
| 244 |
filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd ); |
|---|
| 245 |
|
|---|
| 246 |
fmt = *filter_chain_GetFmtOut( p_sys->p_chain ); |
|---|
| 247 |
es_format_Copy( &p_filter->fmt_out, &fmt ); |
|---|
| 248 |
|
|---|
| 249 |
p_filter->fmt_out.video.i_aspect = i_aspect * i_width / i_height; |
|---|
| 250 |
|
|---|
| 251 |
if( p_filter->fmt_out.video.i_width != i_width |
|---|
| 252 |
|| p_filter->fmt_out.video.i_height != i_height ) |
|---|
| 253 |
{ |
|---|
| 254 |
msg_Warn( p_filter, "Looks like something went wrong. " |
|---|
| 255 |
"Output size is %dx%d while we asked for %dx%d", |
|---|
| 256 |
p_filter->fmt_out.video.i_width, |
|---|
| 257 |
p_filter->fmt_out.video.i_height, |
|---|
| 258 |
i_width, i_height ); |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
p_filter->pf_video_filter = Filter; |
|---|
| 262 |
|
|---|
| 263 |
return VLC_SUCCESS; |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
static void Destroy( vlc_object_t *p_this ) |
|---|
| 270 |
{ |
|---|
| 271 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 272 |
filter_chain_Delete( p_filter->p_sys->p_chain ); |
|---|
| 273 |
free( p_filter->p_sys ); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic ) |
|---|
| 280 |
{ |
|---|
| 281 |
return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic ); |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
static picture_t *video_new( filter_t *p_filter ) |
|---|
| 288 |
{ |
|---|
| 289 |
return ((filter_t*)p_filter->p_owner)->pf_vout_buffer_new( (filter_t*)p_filter->p_owner ); |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
static void video_del( filter_t *p_filter, picture_t *p_pic ) |
|---|
| 293 |
{ |
|---|
| 294 |
if( ((filter_t*)p_filter->p_owner)->pf_vout_buffer_del ) |
|---|
| 295 |
((filter_t*)p_filter->p_owner)->pf_vout_buffer_del( (filter_t*)p_filter->p_owner, p_pic ); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
static int alloc_init( filter_t *p_filter, void *p_data ) |
|---|
| 299 |
{ |
|---|
| 300 |
p_filter->p_owner = p_data; |
|---|
| 301 |
p_filter->pf_vout_buffer_new = video_new; |
|---|
| 302 |
p_filter->pf_vout_buffer_del = video_del; |
|---|
| 303 |
return VLC_SUCCESS; |
|---|
| 304 |
} |
|---|