]> git.sesse.net Git - vlc/blob - modules/video_filter/fps.c
mediacodec: don't loop in GetOutput
[vlc] / 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 vlc_module_begin ()
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 )
47
48     add_shortcut( "fps" )
49     add_string( CFG_PREFIX "fps", NULL, NULL,
50                NULL, false )
51     set_callbacks( Open, Close )
52 vlc_module_end ()
53
54 static const char *const ppsz_filter_options[] = {
55     "fps",
56     NULL
57 };
58
59 /* We'll store pointer for previous picture we have received
60    and copy that if needed on framerate increase (not preferred)*/
61 struct filter_sys_t
62 {
63     date_t          next_output_pts; /**< output calculated PTS */
64     picture_t       *p_previous_pic;
65     int             i_output_frame_interval;
66 };
67
68 static picture_t *Filter( filter_t *p_filter, picture_t *p_picture)
69 {
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) )
75     {
76         msg_Dbg( p_filter, "skipping non-dated picture");
77         picture_Release( p_picture );
78         return NULL;
79     }
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 ) )
82     {
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 );
87         return p_picture;
88     }
89
90     /* Check if we can skip input as better should follow */
91     if( p_picture->date <
92         ( date_Get( &p_sys->next_output_pts ) - (mtime_t)p_sys->i_output_frame_interval ) )
93     {
94         if( p_sys->p_previous_pic )
95             picture_Release( p_sys->p_previous_pic );
96         p_sys->p_previous_pic = p_picture;
97         return NULL;
98     }
99
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 );
102
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 ) )
107     {
108         picture_t *p_tmp = NULL;
109         p_tmp = picture_NewFromFormat( &p_filter->fmt_in.video );
110
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;
114
115         last_pic->p_next = p_tmp;
116         last_pic = p_tmp;
117         date_Increment( &p_sys->next_output_pts, p_filter->fmt_out.video.i_frame_rate_base );
118     }
119
120     last_pic = p_sys->p_previous_pic;
121     p_sys->p_previous_pic = p_picture;
122     return last_pic;
123 }
124
125 static int Open( vlc_object_t *p_this)
126 {
127     filter_t *p_filter = (filter_t*)p_this;
128     filter_sys_t *p_sys;
129
130     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
131
132     if( unlikely( !p_sys ) )
133         return VLC_ENOMEM;
134
135     unsigned frame_rate = p_filter->fmt_out.video.i_frame_rate, frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
136
137     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
138                        p_filter->p_cfg );
139
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" ) )
142     {
143         frame_rate = p_filter->fmt_out.video.i_frame_rate;
144         frame_rate_base = p_filter->fmt_out.video.i_frame_rate_base;
145     }
146
147
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;
151
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 );
155
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;
157
158     date_Init( &p_sys->next_output_pts,
159                p_filter->fmt_out.video.i_frame_rate, 1);
160
161     date_Set( &p_sys->next_output_pts, VLC_TS_INVALID );
162
163     p_filter->pf_video_filter = Filter;
164     return VLC_SUCCESS;
165 }
166
167 static void Close( vlc_object_t *p_this )
168 {
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 );
173 }