1 /*****************************************************************************
2 * fix_vfr_pts.c: vfr pts fixing video filter
3 *****************************************************************************
4 * Copyright (C) 2010 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
29 /* This filter calculates and store the frame's duration to the frame data
30 * (if it is not already calculated when the frame arrives to this point)
31 * so it can be used by filters that will need to reconstruct pts due to
32 * out-of-order frame requests */
37 cli_vid_filter_t prev_filter;
39 /* we need 1 buffer picture and 1 place holder */
46 int64_t last_duration;
49 cli_vid_filter_t fix_vfr_pts_filter;
51 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
53 /* if the input is not vfr, we don't do anything */
56 fix_vfr_pts_hnd_t *h = calloc( 1, sizeof(fix_vfr_pts_hnd_t) );
61 h->prev_hnd = *handle;
62 h->prev_filter = *filter;
64 *filter = fix_vfr_pts_filter;
69 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
71 fix_vfr_pts_hnd_t *h = handle;
72 /* if we want the holder picture and it errored, return the error. */
73 if( frame == h->holder_frame )
80 /* if we have a holder frame and we don't want it, release the frame */
81 if( h->holder_frame > 0 && h->holder_frame < frame && h->prev_filter.release_frame( h->prev_hnd, &h->holder, h->holder_frame ) )
84 if( h->prev_filter.get_frame( h->prev_hnd, &h->holder, frame ) )
88 /* if the frame's duration is not set already, read the next frame to set it. */
89 if( !h->holder.duration )
91 /* allocate a buffer picture if we didn't already */
92 if( !h->buffer_allocated )
94 if( x264_cli_pic_alloc( &h->buffer, h->holder.img.csp, h->holder.img.width, h->holder.img.height ) )
96 h->buffer_allocated = 1;
98 h->holder_frame = frame+1;
99 /* copy the current frame to the buffer, release it, and then read in the next frame to the placeholder */
100 if( x264_cli_pic_copy( &h->buffer, &h->holder ) || h->prev_filter.release_frame( h->prev_hnd, &h->holder, frame ) )
102 h->holder_ret = h->prev_filter.get_frame( h->prev_hnd, &h->holder, h->holder_frame );
103 /* suppress non-monotonic pts warnings by setting the duration to be at least 1 */
105 h->last_duration = X264_MAX( h->holder.pts - h->buffer.pts, 1 );
106 h->buffer.duration = h->last_duration;
112 output->pts = h->pts;
113 h->pts += output->duration;
118 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
120 fix_vfr_pts_hnd_t *h = handle;
121 /* if the frame is the buffered one, it's already been released */
122 if( frame == (h->holder_frame - 1) )
124 return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
127 static void free_filter( hnd_t handle )
129 fix_vfr_pts_hnd_t *h = handle;
130 h->prev_filter.free( h->prev_hnd );
131 if( h->buffer_allocated )
132 x264_cli_pic_clean( &h->buffer );
136 cli_vid_filter_t fix_vfr_pts_filter = { "fix_vfr_pts", NULL, init, get_frame, release_frame, free_filter, NULL };