]> git.sesse.net Git - x264/blob - input/thread.c
Use threadpools to avoid unnecessary thread creation
[x264] / input / thread.c
1 /*****************************************************************************
2  * thread.c: x264 threaded input module
3  *****************************************************************************
4  * Copyright (C) 2003-2009 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "muxers.h"
25
26 extern cli_input_t input;
27
28 typedef struct
29 {
30     cli_input_t input;
31     hnd_t p_handle;
32     x264_picture_t pic;
33     x264_threadpool_t *pool;
34     int next_frame;
35     int frame_total;
36     struct thread_input_arg_t *next_args;
37 } thread_hnd_t;
38
39 typedef struct thread_input_arg_t
40 {
41     thread_hnd_t *h;
42     x264_picture_t *pic;
43     int i_frame;
44     int status;
45 } thread_input_arg_t;
46
47 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
48 {
49     thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
50     if( !h || input.picture_alloc( &h->pic, info->csp, info->width, info->height ) )
51     {
52         fprintf( stderr, "x264 [error]: malloc failed\n" );
53         return -1;
54     }
55     h->input = input;
56     h->p_handle = *p_handle;
57     h->next_frame = -1;
58     h->next_args = malloc( sizeof(thread_input_arg_t) );
59     if( !h->next_args )
60         return -1;
61     h->next_args->h = h;
62     h->next_args->status = 0;
63     h->frame_total = input.get_frame_total( h->p_handle );
64     thread_input.picture_alloc = h->input.picture_alloc;
65     thread_input.picture_clean = h->input.picture_clean;
66
67     if( x264_threadpool_init( &h->pool, 1, NULL, NULL ) )
68         return -1;
69
70     *p_handle = h;
71     return 0;
72 }
73
74 static int get_frame_total( hnd_t handle )
75 {
76     thread_hnd_t *h = handle;
77     return h->frame_total;
78 }
79
80 static void read_frame_thread_int( thread_input_arg_t *i )
81 {
82     i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
83 }
84
85 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
86 {
87     thread_hnd_t *h = handle;
88     int ret = 0;
89
90     if( h->next_frame >= 0 )
91     {
92         x264_threadpool_wait( h->pool, h->next_args );
93         ret |= h->next_args->status;
94     }
95
96     if( h->next_frame == i_frame )
97         XCHG( x264_picture_t, *p_pic, h->pic );
98     else
99         ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
100
101     if( !h->frame_total || i_frame+1 < h->frame_total )
102     {
103         h->next_frame =
104         h->next_args->i_frame = i_frame+1;
105         h->next_args->pic = &h->pic;
106         x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args );
107     }
108     else
109         h->next_frame = -1;
110
111     return ret;
112 }
113
114 static int release_frame( x264_picture_t *pic, hnd_t handle )
115 {
116     thread_hnd_t *h = handle;
117     if( h->input.release_frame )
118         return h->input.release_frame( pic, h->p_handle );
119     return 0;
120 }
121
122 static int close_file( hnd_t handle )
123 {
124     thread_hnd_t *h = handle;
125     x264_threadpool_delete( h->pool );
126     h->input.close_file( h->p_handle );
127     h->input.picture_clean( &h->pic );
128     free( h->next_args );
129     free( h );
130     return 0;
131 }
132
133 cli_input_t thread_input = { open_file, get_frame_total, NULL, read_frame, release_frame, NULL, close_file };