1 /*****************************************************************************
2 * thread.c: x264 threaded input module
3 *****************************************************************************
4 * Copyright (C) 2003-2009 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
26 extern cli_input_t input;
37 struct thread_input_arg_t *next_args;
40 typedef struct thread_input_arg_t
48 static int open_file( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
50 thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
51 if( !h || x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height ) < 0 )
53 fprintf( stderr, "x264 [error]: malloc failed\n" );
57 h->p_handle = *p_handle;
60 h->next_args = malloc( sizeof(thread_input_arg_t) );
64 h->next_args->status = 0;
65 h->frame_total = input.get_frame_total( h->p_handle );
71 static int get_frame_total( hnd_t handle )
73 thread_hnd_t *h = handle;
74 return h->frame_total;
77 static void read_frame_thread_int( thread_input_arg_t *i )
79 i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
82 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
84 thread_hnd_t *h = handle;
87 if( h->next_frame >= 0 )
89 x264_pthread_join( h->tid, NULL );
90 ret |= h->next_args->status;
94 if( h->next_frame == i_frame )
95 XCHG( x264_picture_t, *p_pic, h->pic );
97 ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
99 if( !h->frame_total || i_frame+1 < h->frame_total )
102 h->next_args->i_frame = i_frame+1;
103 h->next_args->pic = &h->pic;
104 if( x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args ) )
114 static int close_file( hnd_t handle )
116 thread_hnd_t *h = handle;
118 x264_pthread_join( h->tid, NULL );
119 h->input.close_file( h->p_handle );
120 x264_picture_clean( &h->pic );
121 free( h->next_args );
126 cli_input_t thread_input = { open_file, get_frame_total, read_frame, close_file };