upnp: remove m_server_list_lock, p_server_list is already protected by s_lock
[vlc.git] / modules / video_filter / fps.c
1 /*****************************************************************************
2  * fps.c : fps conversion plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2014 VLC authors and VideoLAN
5  *
6  * Author: Ilkka Ollakka <ileoo at videolan dot org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34
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);
38
39 #define CFG_PREFIX "fps-"
40
41 #define FPS_TEXT N_( "Frame rate" )
42
43 vlc_module_begin ()
44     set_description( N_("FPS conversion video filter") )
45     set_shortname( N_("FPS Converter" ))
46     set_capability( "video filter2", 0 )
47     set_category( CAT_VIDEO )
48     set_subcategory( SUBCAT_VIDEO_VFILTER )
49
50     add_shortcut( "fps" )
51     add_string( CFG_PREFIX "fps", NULL, FPS_TEXT, FPS_TEXT, false )
52     set_callbacks( Open, Close )
53 vlc_module_end ()
54
55 static const char *const ppsz_filter_options[] = {
56     "fps",
57     NULL
58 };
59
60 /* We'll store pointer for previous picture we have received
61    and copy that if needed on framerate increase (not preferred)*/
62 struct filter_sys_t
63 {
64     date_t          next_output_pts; /**< output calculated PTS */
65     picture_t       *p_previous_pic;
66     int             i_output_frame_interval;
67 };
68
69 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture)
70 {
71     filter_sys_t *p_sys = p_filter->p_sys;
72     /* If input picture doesn't have actual valid timestamp,
73         we don't really have currently a way to know what else
74         to do with it other than drop it for now*/
75     if( unlikely( p_picture->date < VLC_TS_0) )
76     {
77         msg_Dbg( p_filter, "skipping non-dated picture");
78         picture_Release( p_picture );
79         return NULL;
80     }
81     /* First time we get some valid timestamp, we'll take it as base for output*/
82     if( unlikely( date_Get( &p_sys->next_output_pts ) == VLC_TS_INVALID ) )
83     {
84         msg_Dbg( p_filter, "Resetting timestamps" );
85         date_Set( &p_sys->next_output_pts, p_picture->date );
86         p_sys->p_previous_pic = picture_Hold( p_picture );
87         date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
88         return p_picture;
89     }
90
91     /* Check if we can skip input as better should follow */
92     if( p_picture->date <
93         ( date_Get( &p_sys->next_output_pts ) - (mtime_t)p_sys->i_output_frame_interval ) )
94     {
95         if( p_sys->p_previous_pic )
96             picture_Release( p_sys->p_previous_pic );
97         p_sys->p_previous_pic = p_picture;
98         return NULL;
99     }
100
101     p_sys->p_previous_pic->date = date_Get( &p_sys->next_output_pts );
102     date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
103
104     picture_t *last_pic = p_sys->p_previous_pic;
105     /* Duplicating pictures are not that effective and framerate increase
106         should be avoided, it's only here as filter should work in that direction too*/
107     while( unlikely( (date_Get( &p_sys->next_output_pts ) + p_sys->i_output_frame_interval ) < p_picture->date ) )
108     {
109         picture_t *p_tmp = NULL;
110         p_tmp = picture_NewFromFormat( &p_filter->fmt_in.video );
111
112         picture_Copy( p_tmp, p_sys->p_previous_pic);
113         p_tmp->date = date_Get( &p_sys->next_output_pts );
114         p_tmp->p_next = NULL;
115
116         last_pic->p_next = p_tmp;
117         last_pic = p_tmp;
118         date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
119     }
120
121     last_pic = p_sys->p_previous_pic;
122     p_sys->p_previous_pic = p_picture;
123     return last_pic;
124 }
125
126 static int Open( vlc_object_t *p_this)
127 {
128     filter_t *p_filter = (filter_t*)p_this;
129     filter_sys_t *p_sys;
130
131     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
132
133     if( unlikely( !p_sys ) )
134         return VLC_ENOMEM;
135
136     unsigned frame_rate = p_filter->fmt_out.video.i_frame_rate, frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
137
138     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
139                        p_filter->p_cfg );
140
141     /* If we don't have fps option, use filter output values */
142     if( var_InheritURational( p_filter, &frame_rate, &frame_rate_base, CFG_PREFIX "fps" ) )
143     {
144         frame_rate = p_filter->fmt_out.video.i_frame_rate;
145         frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
146     }
147
148
149     memcpy( &p_filter->fmt_out.video, &p_filter->fmt_in.video, sizeof(video_format_t));
150     p_filter->fmt_out.video.i_frame_rate = frame_rate;
151     p_filter->fmt_out.video.i_frame_rate_base = frame_rate_base;
152
153     msg_Dbg( p_filter, "Converting fps from %d/%d -> %d/%d",
154             p_filter->fmt_in.video.i_frame_rate, p_filter->fmt_in.video.i_frame_rate_base,
155             p_filter->fmt_out.video.i_frame_rate, p_filter->fmt_out.video.i_frame_rate_base );
156
157     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
159     date_Init( &p_sys->next_output_pts,
160                p_filter->fmt_out.video.i_frame_rate, 1);
161
162     date_Set( &p_sys->next_output_pts, VLC_TS_INVALID );
163
164     p_filter->pf_video_filter = Filter;
165     return VLC_SUCCESS;
166 }
167
168 static void Close( vlc_object_t *p_this )
169 {
170     filter_t *p_filter = (filter_t*)p_this;
171     if( p_filter->p_sys->p_previous_pic )
172         picture_Release( p_filter->p_sys->p_previous_pic );
173     free( p_filter->p_sys );
174 }