demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / video_filter / fps.c
blobc4bc35e1e6f576684de9d9b4caedf26fa7c5caa0
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 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
36 static int Open( vlc_object_t *p_this);
37 static void Close( vlc_object_t *p_this);
38 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture);
40 #define CFG_PREFIX "fps-"
42 #define FPS_TEXT N_( "Frame rate" )
44 vlc_module_begin ()
45 set_description( N_("FPS conversion video filter") )
46 set_shortname( N_("FPS Converter" ))
47 set_capability( "video filter", 0 )
48 set_category( CAT_VIDEO )
49 set_subcategory( SUBCAT_VIDEO_VFILTER )
51 add_shortcut( "fps" )
52 add_string( CFG_PREFIX "fps", NULL, FPS_TEXT, FPS_TEXT, false )
53 set_callbacks( Open, Close )
54 vlc_module_end ()
56 static const char *const ppsz_filter_options[] = {
57 "fps",
58 NULL
61 /* We'll store pointer for previous picture we have received
62 and copy that if needed on framerate increase (not preferred)*/
63 struct filter_sys_t
65 date_t next_output_pts; /**< output calculated PTS */
66 picture_t *p_previous_pic;
67 int i_output_frame_interval;
70 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture)
72 filter_sys_t *p_sys = p_filter->p_sys;
73 /* If input picture doesn't have actual valid timestamp,
74 we don't really have currently a way to know what else
75 to do with it other than drop it for now*/
76 if( unlikely( p_picture->date < VLC_TS_0) )
78 msg_Dbg( p_filter, "skipping non-dated picture");
79 picture_Release( p_picture );
80 return NULL;
83 p_picture->format.i_frame_rate = p_filter->fmt_out.video.i_frame_rate;
84 p_picture->format.i_frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
86 /* First time we get some valid timestamp, we'll take it as base for output
87 later on we retake new timestamp if it has jumped too much */
88 if( unlikely( ( date_Get( &p_sys->next_output_pts ) == VLC_TS_INVALID ) ||
89 ( p_picture->date > ( date_Get( &p_sys->next_output_pts ) + (mtime_t)p_sys->i_output_frame_interval ) )
90 ) )
92 msg_Dbg( p_filter, "Resetting timestamps" );
93 date_Set( &p_sys->next_output_pts, p_picture->date );
94 if( p_sys->p_previous_pic )
95 picture_Release( p_sys->p_previous_pic );
96 p_sys->p_previous_pic = picture_Hold( p_picture );
97 date_Increment( &p_sys->next_output_pts, 1 );
98 return p_picture;
101 /* Check if we can skip input as better should follow */
102 if( p_picture->date <
103 ( date_Get( &p_sys->next_output_pts ) - (mtime_t)p_sys->i_output_frame_interval ) )
105 if( p_sys->p_previous_pic )
106 picture_Release( p_sys->p_previous_pic );
107 p_sys->p_previous_pic = p_picture;
108 return NULL;
111 p_sys->p_previous_pic->date = date_Get( &p_sys->next_output_pts );
112 date_Increment( &p_sys->next_output_pts, 1 );
114 picture_t *last_pic = p_sys->p_previous_pic;
115 /* Duplicating pictures are not that effective and framerate increase
116 should be avoided, it's only here as filter should work in that direction too*/
117 while( unlikely( (date_Get( &p_sys->next_output_pts ) + p_sys->i_output_frame_interval ) < p_picture->date ) )
119 picture_t *p_tmp = NULL;
120 p_tmp = picture_NewFromFormat( &p_filter->fmt_out.video );
122 picture_Copy( p_tmp, p_sys->p_previous_pic);
123 p_tmp->date = date_Get( &p_sys->next_output_pts );
124 p_tmp->p_next = NULL;
126 last_pic->p_next = p_tmp;
127 last_pic = p_tmp;
128 date_Increment( &p_sys->next_output_pts, 1 );
131 last_pic = p_sys->p_previous_pic;
132 p_sys->p_previous_pic = p_picture;
133 return last_pic;
136 static int Open( vlc_object_t *p_this)
138 filter_t *p_filter = (filter_t*)p_this;
139 filter_sys_t *p_sys;
141 p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
143 if( unlikely( !p_sys ) )
144 return VLC_ENOMEM;
146 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
147 p_filter->p_cfg );
149 video_format_Clean( &p_filter->fmt_out.video );
150 video_format_Copy( &p_filter->fmt_out.video, &p_filter->fmt_in.video );
152 /* If we don't have fps option, use filter output values */
153 if( var_InheritURational( p_filter, &p_filter->fmt_out.video.i_frame_rate,
154 &p_filter->fmt_out.video.i_frame_rate_base, CFG_PREFIX "fps" ) )
156 p_filter->fmt_out.video.i_frame_rate = p_filter->fmt_in.video.i_frame_rate;
157 p_filter->fmt_out.video.i_frame_rate_base = p_filter->fmt_in.video.i_frame_rate_base;
160 msg_Dbg( p_filter, "Converting fps from %d/%d -> %d/%d",
161 p_filter->fmt_in.video.i_frame_rate, p_filter->fmt_in.video.i_frame_rate_base,
162 p_filter->fmt_out.video.i_frame_rate, p_filter->fmt_out.video.i_frame_rate_base );
164 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;
166 date_Init( &p_sys->next_output_pts,
167 p_filter->fmt_out.video.i_frame_rate, p_filter->fmt_out.video.i_frame_rate_base );
169 date_Set( &p_sys->next_output_pts, VLC_TS_INVALID );
170 p_sys->p_previous_pic = NULL;
172 p_filter->pf_video_filter = Filter;
173 return VLC_SUCCESS;
176 static void Close( vlc_object_t *p_this )
178 filter_t *p_filter = (filter_t*)p_this;
179 if( p_filter->p_sys->p_previous_pic )
180 picture_Release( p_filter->p_sys->p_previous_pic );
181 free( p_filter->p_sys );