]> git.sesse.net Git - x264/blob - filters/video/fix_vfr_pts.c
Add video filtering system to x264cli
[x264] / filters / video / fix_vfr_pts.c
1 /*****************************************************************************
2  * fix_vfr_pts.c: x264 video vfr pts fixing filter
3  *****************************************************************************
4  * Copyright (C) 2010 Steven Walters <kemuri9@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19  *****************************************************************************/
20
21 #include "video.h"
22 #include "internal.h"
23
24 /* This filter calculates and store the frame's duration to the frame data
25  * (if it is not already calculated when the frame arrives to this point)
26  * so it can be used by filters that will need to reconstruct pts due to
27  * out-of-order frame requests */
28
29 typedef struct
30 {
31     hnd_t prev_hnd;
32     cli_vid_filter_t prev_filter;
33
34     /* we need 1 buffer picture and 1 place holder */
35     cli_pic_t buffer;
36     cli_pic_t holder;
37     int buffer_allocated;
38     int holder_frame;
39     int holder_ret;
40     int64_t pts;
41     int64_t last_duration;
42 } fix_vfr_pts_hnd_t;
43
44 cli_vid_filter_t fix_vfr_pts_filter;
45
46 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
47 {
48     /* if the input is not vfr, we don't do anything */
49     if( !info->vfr )
50         return 0;
51     fix_vfr_pts_hnd_t *h = calloc( 1, sizeof(fix_vfr_pts_hnd_t) );
52     if( !h )
53         return -1;
54
55     h->holder_frame = -1;
56     h->prev_hnd = *handle;
57     h->prev_filter = *filter;
58     *handle = h;
59     *filter = fix_vfr_pts_filter;
60
61     return 0;
62 }
63
64 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
65 {
66     fix_vfr_pts_hnd_t *h = handle;
67     /* if we want the holder picture and it errored, return the error. */
68     if( frame == h->holder_frame )
69     {
70         if( h->holder_ret )
71             return h->holder_ret;
72     }
73     else
74     {
75         /* if we have a holder frame and we don't want it, release the frame */
76         if( h->holder_frame > 0 && h->holder_frame < frame && h->prev_filter.release_frame( h->prev_hnd, &h->holder, h->holder_frame ) )
77             return -1;
78         h->holder_frame = -1;
79         if( h->prev_filter.get_frame( h->prev_hnd, &h->holder, frame ) )
80             return -1;
81     }
82
83     /* if the frame's duration is not set already, read the next frame to set it. */
84     if( !h->holder.duration )
85     {
86         /* allocate a buffer picture if we didn't already */
87         if( !h->buffer_allocated )
88         {
89             if( x264_cli_pic_alloc( &h->buffer, h->holder.img.csp, h->holder.img.width, h->holder.img.height ) )
90                 return -1;
91             h->buffer_allocated = 1;
92         }
93         h->holder_frame = frame+1;
94         /* copy the current frame to the buffer, release it, and then read in the next frame to the placeholder */
95         if( x264_cli_pic_copy( &h->buffer, &h->holder ) || h->prev_filter.release_frame( h->prev_hnd, &h->holder, frame ) )
96             return -1;
97         h->holder_ret = h->prev_filter.get_frame( h->prev_hnd, &h->holder, h->holder_frame );
98         /* suppress non-monotonic pts warnings by setting the duration to be at least 1 */
99         if( !h->holder_ret )
100             h->last_duration = X264_MAX( h->holder.pts - h->buffer.pts, 1 );
101         h->buffer.duration = h->last_duration;
102         *output = h->buffer;
103     }
104     else
105         *output = h->holder;
106
107     output->pts = h->pts;
108     h->pts += output->duration;
109
110     return 0;
111 }
112
113 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
114 {
115     fix_vfr_pts_hnd_t *h = handle;
116     /* if the frame is the buffered one, it's already been released */
117     if( frame == (h->holder_frame - 1) )
118         return 0;
119     return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
120 }
121
122 static void free_filter( hnd_t handle )
123 {
124     fix_vfr_pts_hnd_t *h = handle;
125     h->prev_filter.free( h->prev_hnd );
126     if( h->buffer_allocated )
127         x264_cli_pic_clean( &h->buffer );
128     free( h );
129 }
130
131 cli_vid_filter_t fix_vfr_pts_filter = { "fix_vfr_pts", NULL, init, get_frame, release_frame, free_filter, NULL };