1 /*****************************************************************************
2 * fix_vfr_pts.c: x264 video vfr pts fixing filter
3 *****************************************************************************
4 * Copyright (C) 2010 Steven Walters <kemuri9@gmail.com>
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.
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.
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 *****************************************************************************/
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 */
32 cli_vid_filter_t prev_filter;
34 /* we need 1 buffer picture and 1 place holder */
41 int64_t last_duration;
44 cli_vid_filter_t fix_vfr_pts_filter;
46 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
48 /* if the input is not vfr, we don't do anything */
51 fix_vfr_pts_hnd_t *h = calloc( 1, sizeof(fix_vfr_pts_hnd_t) );
56 h->prev_hnd = *handle;
57 h->prev_filter = *filter;
59 *filter = fix_vfr_pts_filter;
64 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
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 )
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 ) )
79 if( h->prev_filter.get_frame( h->prev_hnd, &h->holder, frame ) )
83 /* if the frame's duration is not set already, read the next frame to set it. */
84 if( !h->holder.duration )
86 /* allocate a buffer picture if we didn't already */
87 if( !h->buffer_allocated )
89 if( x264_cli_pic_alloc( &h->buffer, h->holder.img.csp, h->holder.img.width, h->holder.img.height ) )
91 h->buffer_allocated = 1;
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 ) )
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 */
100 h->last_duration = X264_MAX( h->holder.pts - h->buffer.pts, 1 );
101 h->buffer.duration = h->last_duration;
107 output->pts = h->pts;
108 h->pts += output->duration;
113 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
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) )
119 return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
122 static void free_filter( hnd_t handle )
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 );
131 cli_vid_filter_t fix_vfr_pts_filter = { "fix_vfr_pts", NULL, init, get_frame, release_frame, free_filter, NULL };