1 /*****************************************************************************
2 * cache.c: cache video filter
3 *****************************************************************************
4 * Copyright (C) 2010-2016 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 #define LAST_FRAME (h->first_frame + h->cur_size - 1)
34 cli_vid_filter_t prev_filter;
37 int first_frame; /* first cached frame */
40 int eof; /* frame beyond end of the file */
43 cli_vid_filter_t cache_filter;
45 static int init( hnd_t *handle, cli_vid_filter_t *filter, video_info_t *info, x264_param_t *param, char *opt_string )
47 intptr_t size = (intptr_t)opt_string;
48 /* upon a <= 0 cache request, do nothing */
51 cache_hnd_t *h = calloc( 1, sizeof(cache_hnd_t) );
56 h->cache = malloc( (h->max_size+1) * sizeof(cli_pic_t*) );
60 for( int i = 0; i < h->max_size; i++ )
62 h->cache[i] = malloc( sizeof(cli_pic_t) );
63 if( !h->cache[i] || x264_cli_pic_alloc( h->cache[i], info->csp, info->width, info->height ) )
66 h->cache[h->max_size] = NULL; /* require null terminator for list methods */
68 h->prev_filter = *filter;
69 h->prev_hnd = *handle;
71 *filter = cache_filter;
76 static void fill_cache( cache_hnd_t *h, int frame )
78 /* shift frames out of the cache as the frame request is beyond the filled cache */
79 int shift = frame - LAST_FRAME;
80 /* no frames to shift or no frames left to read */
81 if( shift <= 0 || h->eof )
83 /* the next frames to read are either
84 * A) starting at the end of the current cache, or
85 * B) starting at a new frame that has the end of the cache at the desired frame
86 * and proceeding to fill the entire cache */
87 int cur_frame = X264_MAX( h->first_frame + h->cur_size, frame - h->max_size + 1 );
88 /* the new starting point is either
89 * A) the current one shifted the number of frames entering/leaving the cache, or
90 * B) at a new frame that has the end of the cache at the desired frame. */
91 h->first_frame = X264_MIN( h->first_frame + shift, cur_frame );
92 h->cur_size = X264_MAX( h->cur_size - shift, 0 );
93 while( h->cur_size < h->max_size )
96 /* the old front frame is going to shift off, overwrite it with the new frame */
97 cli_pic_t *cache = h->cache[0];
98 if( h->prev_filter.get_frame( h->prev_hnd, &temp, cur_frame ) ||
99 x264_cli_pic_copy( cache, &temp ) ||
100 h->prev_filter.release_frame( h->prev_hnd, &temp, cur_frame ) )
105 /* the read was successful, shift the frame off the front to the end */
106 x264_frame_push( (void*)h->cache, x264_frame_shift( (void*)h->cache ) );
112 static int get_frame( hnd_t handle, cli_pic_t *output, int frame )
114 cache_hnd_t *h = handle;
115 FAIL_IF_ERR( frame < h->first_frame, NAME, "frame %d is before first cached frame %d \n", frame, h->first_frame );
116 fill_cache( h, frame );
117 if( frame > LAST_FRAME ) /* eof */
119 int idx = frame - (h->eof ? h->eof - h->max_size : h->first_frame);
120 *output = *h->cache[idx];
124 static int release_frame( hnd_t handle, cli_pic_t *pic, int frame )
126 /* the parent filter's frame has already been released so do nothing here */
130 static void free_filter( hnd_t handle )
132 cache_hnd_t *h = handle;
133 h->prev_filter.free( h->prev_hnd );
134 for( int i = 0; i < h->max_size; i++ )
136 x264_cli_pic_clean( h->cache[i] );
143 cli_vid_filter_t cache_filter = { NAME, NULL, init, get_frame, release_frame, free_filter, NULL };