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 *****************************************************************************/
31 x264_threadpool_t *pool;
34 struct thread_input_arg_t *next_args;
37 typedef struct thread_input_arg_t
45 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
47 thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
48 FAIL_IF_ERR( !h || input.picture_alloc( &h->pic, info->csp, info->width, info->height ),
49 "x264", "malloc failed\n" )
51 h->p_handle = *p_handle;
53 h->next_args = malloc( sizeof(thread_input_arg_t) );
57 h->next_args->status = 0;
58 h->frame_total = info->num_frames;
59 thread_input.picture_alloc = h->input.picture_alloc;
60 thread_input.picture_clean = h->input.picture_clean;
62 if( x264_threadpool_init( &h->pool, 1, NULL, NULL ) )
69 static void read_frame_thread_int( thread_input_arg_t *i )
71 i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
74 static int read_frame( cli_pic_t *p_pic, hnd_t handle, int i_frame )
76 thread_hnd_t *h = handle;
79 if( h->next_frame >= 0 )
81 x264_threadpool_wait( h->pool, h->next_args );
82 ret |= h->next_args->status;
85 if( h->next_frame == i_frame )
86 XCHG( cli_pic_t, *p_pic, h->pic );
88 ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
90 if( !h->frame_total || i_frame+1 < h->frame_total )
93 h->next_args->i_frame = i_frame+1;
94 h->next_args->pic = &h->pic;
95 x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args );
103 static int release_frame( cli_pic_t *pic, hnd_t handle )
105 thread_hnd_t *h = handle;
106 if( h->input.release_frame )
107 return h->input.release_frame( pic, h->p_handle );
111 static int close_file( hnd_t handle )
113 thread_hnd_t *h = handle;
114 x264_threadpool_delete( h->pool );
115 h->input.close_file( h->p_handle );
116 h->input.picture_clean( &h->pic );
117 free( h->next_args );
122 cli_input_t thread_input = { open_file, NULL, read_frame, release_frame, NULL, close_file };