1 /*****************************************************************************
2  * fix_vfr_pts.c: vfr pts fixing video filter
3  *****************************************************************************
4  * Copyright (C) 2010-2014 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *
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.
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 General Public License for more details.
17  *
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.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25 
26 #include "video.h"
27 #include "internal.h"
28 
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 */
33 
34 typedef struct
35 {
36     hnd_t prev_hnd;
37     cli_vid_filter_t prev_filter;
38 
39     /* we need 1 buffer picture and 1 place holder */
40     cli_pic_t buffer;
41     cli_pic_t holder;
42     int buffer_allocated;
43     int holder_frame;
44     int holder_ret;
45     int64_t pts;
46     int64_t last_duration;
47 } fix_vfr_pts_hnd_t;
48 
49 cli_vid_filter_t fix_vfr_pts_filter;
50 
init(hnd_t * handle,cli_vid_filter_t * filter,video_info_t * info,x264_param_t * param,char * opt_string)51 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
52 {
53     fix_vfr_pts_hnd_t *h;
54 
55 	/* if the input is not vfr, we don't do anything */
56     if( !info->vfr )
57         return 0;
58     h = calloc( 1, sizeof(fix_vfr_pts_hnd_t) );
59     if( !h )
60         return -1;
61 
62     h->holder_frame = -1;
63     h->prev_hnd = *handle;
64     h->prev_filter = *filter;
65     *handle = h;
66     *filter = fix_vfr_pts_filter;
67 
68     return 0;
69 }
70 
get_frame(hnd_t handle,cli_pic_t * output,int frame)71 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
72 {
73     fix_vfr_pts_hnd_t *h = handle;
74     /* if we want the holder picture and it errored, return the error. */
75     if( frame == h->holder_frame )
76     {
77         if( h->holder_ret )
78             return h->holder_ret;
79     }
80     else
81     {
82         /* if we have a holder frame and we don't want it, release the frame */
83         if( h->holder_frame > 0 && h->holder_frame < frame && h->prev_filter.release_frame( h->prev_hnd, &h->holder, h->holder_frame ) )
84             return -1;
85         h->holder_frame = -1;
86         if( h->prev_filter.get_frame( h->prev_hnd, &h->holder, frame ) )
87             return -1;
88     }
89 
90     /* if the frame's duration is not set already, read the next frame to set it. */
91     if( !h->holder.duration )
92     {
93         /* allocate a buffer picture if we didn't already */
94         if( !h->buffer_allocated )
95         {
96             if( x264_cli_pic_alloc( &h->buffer, h->holder.img.csp, h->holder.img.width, h->holder.img.height ) )
97                 return -1;
98             h->buffer_allocated = 1;
99         }
100         h->holder_frame = frame+1;
101         /* copy the current frame to the buffer, release it, and then read in the next frame to the placeholder */
102         if( x264_cli_pic_copy( &h->buffer, &h->holder ) || h->prev_filter.release_frame( h->prev_hnd, &h->holder, frame ) )
103             return -1;
104         h->holder_ret = h->prev_filter.get_frame( h->prev_hnd, &h->holder, h->holder_frame );
105         /* suppress non-monotonic pts warnings by setting the duration to be at least 1 */
106         if( !h->holder_ret )
107             h->last_duration = X264_MAX( h->holder.pts - h->buffer.pts, 1 );
108         h->buffer.duration = h->last_duration;
109         *output = h->buffer;
110     }
111     else
112         *output = h->holder;
113 
114     output->pts = h->pts;
115     h->pts += output->duration;
116 
117     return 0;
118 }
119 
release_frame(hnd_t handle,cli_pic_t * pic,int frame)120 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
121 {
122     fix_vfr_pts_hnd_t *h = handle;
123     /* if the frame is the buffered one, it's already been released */
124     if( frame == (h->holder_frame - 1) )
125         return 0;
126     return h->prev_filter.release_frame( h->prev_hnd, pic, frame );
127 }
128 
free_filter(hnd_t handle)129 static void free_filter( hnd_t handle )
130 {
131     fix_vfr_pts_hnd_t *h = handle;
132     h->prev_filter.free( h->prev_hnd );
133     if( h->buffer_allocated )
134         x264_cli_pic_clean( &h->buffer );
135     free( h );
136 }
137 
138 cli_vid_filter_t fix_vfr_pts_filter = { "fix_vfr_pts", NULL, init, get_frame, release_frame, free_filter, NULL };
139