1 /*****************************************************************************
2 * fps.c : fps conversion plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * Author: Ilkka Ollakka <ileoo at videolan dot org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
35 static int Open( vlc_object_t *p_this);
36 static void Close( vlc_object_t *p_this);
37 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture);
39 #define CFG_PREFIX "fps-"
42 set_description( N_("FPS conversion video filter") )
43 set_shortname( N_("FPS Converter" ))
44 set_capability( "video filter2", 0 )
45 set_category( CAT_VIDEO )
46 set_subcategory( SUBCAT_VIDEO_VFILTER )
49 add_string( CFG_PREFIX "fps", NULL, NULL,
51 set_callbacks( Open, Close )
54 static const char *const ppsz_filter_options[] = {
59 /* We'll store pointer for previous picture we have received
60 and copy that if needed on framerate increase (not preferred)*/
63 date_t next_output_pts; /**< output calculated PTS */
64 picture_t *p_previous_pic;
65 int i_output_frame_interval;
68 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture)
70 filter_sys_t *p_sys = p_filter->p_sys;
71 /* If input picture doesn't have actual valid timestamp,
72 we don't really have currently a way to know what else
73 to do with it other than drop it for now*/
74 if( unlikely( p_picture->date < VLC_TS_0) )
76 msg_Dbg( p_filter, "skipping non-dated picture");
77 picture_Release( p_picture );
80 /* First time we get some valid timestamp, we'll take it as base for output*/
81 if( unlikely( date_Get( &p_sys->next_output_pts ) == VLC_TS_INVALID ) )
83 msg_Dbg( p_filter, "Resetting timestamps" );
84 date_Set( &p_sys->next_output_pts, p_picture->date );
85 p_sys->p_previous_pic = picture_Hold( p_picture );
86 date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
90 /* Check if we can skip input as better should follow */
92 ( date_Get( &p_sys->next_output_pts ) - (mtime_t)p_sys->i_output_frame_interval ) )
94 if( p_sys->p_previous_pic )
95 picture_Release( p_sys->p_previous_pic );
96 p_sys->p_previous_pic = p_picture;
100 p_sys->p_previous_pic->date = date_Get( &p_sys->next_output_pts );
101 date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
103 picture_t *last_pic = p_sys->p_previous_pic;
104 /* Duplicating pictures are not that effective and framerate increase
105 should be avoided, it's only here as filter should work in that direction too*/
106 while( unlikely( (date_Get( &p_sys->next_output_pts ) + p_sys->i_output_frame_interval ) < p_picture->date ) )
108 picture_t *p_tmp = NULL;
109 p_tmp = picture_NewFromFormat( &p_filter->fmt_in.video );
111 picture_Copy( p_tmp, p_sys->p_previous_pic);
112 p_tmp->date = date_Get( &p_sys->next_output_pts );
113 p_tmp->p_next = NULL;
115 last_pic->p_next = p_tmp;
117 date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
120 last_pic = p_sys->p_previous_pic;
121 p_sys->p_previous_pic = p_picture;
125 static int Open( vlc_object_t *p_this)
127 filter_t *p_filter = (filter_t*)p_this;
130 p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
132 if( unlikely( !p_sys ) )
135 unsigned frame_rate = p_filter->fmt_out.video.i_frame_rate, frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
137 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
140 /* If we don't have fps option, use filter output values */
141 if( var_InheritURational( p_filter, &frame_rate, &frame_rate_base, CFG_PREFIX "fps" ) )
143 frame_rate = p_filter->fmt_out.video.i_frame_rate;
144 frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
148 memcpy( &p_filter->fmt_out.video, &p_filter->fmt_in.video, sizeof(video_format_t));
149 p_filter->fmt_out.video.i_frame_rate = frame_rate;
150 p_filter->fmt_out.video.i_frame_rate_base = frame_rate_base;
152 msg_Dbg( p_filter, "Converting fps from %d/%d -> %d/%d",
153 p_filter->fmt_in.video.i_frame_rate, p_filter->fmt_in.video.i_frame_rate_base,
154 p_filter->fmt_out.video.i_frame_rate, p_filter->fmt_out.video.i_frame_rate_base );
156 p_sys->i_output_frame_interval = p_filter->fmt_out.video.i_frame_rate_base * CLOCK_FREQ / p_filter->fmt_out.video.i_frame_rate;
158 date_Init( &p_sys->next_output_pts,
159 p_filter->fmt_out.video.i_frame_rate, 1);
161 date_Set( &p_sys->next_output_pts, VLC_TS_INVALID );
163 p_filter->pf_video_filter = Filter;
167 static void Close( vlc_object_t *p_this )
169 filter_t *p_filter = (filter_t*)p_this;
170 if( p_filter->p_sys->p_previous_pic )
171 picture_Release( p_filter->p_sys->p_previous_pic );
172 free( p_filter->p_sys );