X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=input%2Fthread.c;h=7447489276023793de14ac08c258d893ad7569f6;hb=0db80bee2765676c2e0e4be21afc2ace900a606c;hp=a9330dcc05d19e4e741a3eacafe8a4840ad7229a;hpb=a0df454b358000eb4f5485f8d09a2620fa6c32e5;p=x264 diff --git a/input/thread.c b/input/thread.c index a9330dcc..74474892 100644 --- a/input/thread.c +++ b/input/thread.c @@ -1,7 +1,7 @@ /***************************************************************************** - * thread.c: x264 threaded input module + * thread.c: threaded input ***************************************************************************** - * Copyright (C) 2003-2009 x264 project + * Copyright (C) 2003-2012 x264 project * * Authors: Laurent Aimar * Loren Merritt @@ -19,80 +19,74 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. + * + * This program is also available under a commercial proprietary license. + * For more information, contact us at licensing@x264.com. *****************************************************************************/ -#include "muxers.h" - -extern cli_input_t input; +#include "input.h" typedef struct { cli_input_t input; hnd_t p_handle; - x264_picture_t pic; - x264_pthread_t tid; + cli_pic_t pic; + x264_threadpool_t *pool; int next_frame; int frame_total; - int in_progress; struct thread_input_arg_t *next_args; } thread_hnd_t; typedef struct thread_input_arg_t { thread_hnd_t *h; - x264_picture_t *pic; + cli_pic_t *pic; int i_frame; int status; } thread_input_arg_t; -static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ) +static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt ) { thread_hnd_t *h = malloc( sizeof(thread_hnd_t) ); - if( !h || x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height ) < 0 ) - { - fprintf( stderr, "x264 [error]: malloc failed\n" ); - return -1; - } - h->input = input; + FAIL_IF_ERR( !h || cli_input.picture_alloc( &h->pic, info->csp, info->width, info->height ), + "x264", "malloc failed\n" ) + h->input = cli_input; h->p_handle = *p_handle; - h->in_progress = 0; h->next_frame = -1; h->next_args = malloc( sizeof(thread_input_arg_t) ); if( !h->next_args ) return -1; h->next_args->h = h; h->next_args->status = 0; - h->frame_total = input.get_frame_total( h->p_handle ); + h->frame_total = info->num_frames; + thread_input.picture_alloc = h->input.picture_alloc; + thread_input.picture_clean = h->input.picture_clean; + + if( x264_threadpool_init( &h->pool, 1, NULL, NULL ) ) + return -1; *p_handle = h; return 0; } -static int get_frame_total( hnd_t handle ) -{ - thread_hnd_t *h = handle; - return h->frame_total; -} - static void read_frame_thread_int( thread_input_arg_t *i ) { i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame ); } -static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame ) +static int read_frame( cli_pic_t *p_pic, hnd_t handle, int i_frame ) { thread_hnd_t *h = handle; int ret = 0; if( h->next_frame >= 0 ) { - x264_pthread_join( h->tid, NULL ); + x264_threadpool_wait( h->pool, h->next_args ); ret |= h->next_args->status; - h->in_progress = 0; } if( h->next_frame == i_frame ) - XCHG( x264_picture_t, *p_pic, h->pic ); + XCHG( cli_pic_t, *p_pic, h->pic ); else ret |= h->input.read_frame( p_pic, h->p_handle, i_frame ); @@ -101,9 +95,7 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame ) h->next_frame = h->next_args->i_frame = i_frame+1; h->next_args->pic = &h->pic; - if( x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args ) ) - return -1; - h->in_progress = 1; + x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args ); } else h->next_frame = -1; @@ -111,16 +103,23 @@ static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame ) return ret; } +static int release_frame( cli_pic_t *pic, hnd_t handle ) +{ + thread_hnd_t *h = handle; + if( h->input.release_frame ) + return h->input.release_frame( pic, h->p_handle ); + return 0; +} + static int close_file( hnd_t handle ) { thread_hnd_t *h = handle; - if( h->in_progress ) - x264_pthread_join( h->tid, NULL ); + x264_threadpool_delete( h->pool ); h->input.close_file( h->p_handle ); - x264_picture_clean( &h->pic ); + h->input.picture_clean( &h->pic ); free( h->next_args ); free( h ); return 0; } -cli_input_t thread_input = { open_file, get_frame_total, read_frame, close_file }; +cli_input_t thread_input = { open_file, NULL, read_frame, release_frame, NULL, close_file };