]> git.sesse.net Git - x264/blob - input/thread.c
Add speedcontrol file.
[x264] / input / thread.c
1 /*****************************************************************************
2  * thread.c: threaded input
3  *****************************************************************************
4  * Copyright (C) 2003-2016 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  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "input.h"
28
29 typedef struct
30 {
31     cli_input_t input;
32     hnd_t p_handle;
33     cli_pic_t pic;
34     x264_threadpool_t *pool;
35     int next_frame;
36     int frame_total;
37     struct thread_input_arg_t *next_args;
38 } thread_hnd_t;
39
40 typedef struct thread_input_arg_t
41 {
42     thread_hnd_t *h;
43     cli_pic_t *pic;
44     int i_frame;
45     int status;
46 } thread_input_arg_t;
47
48 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
49 {
50     thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
51     FAIL_IF_ERR( !h || cli_input.picture_alloc( &h->pic, *p_handle, info->csp, info->width, info->height ),
52                  "x264", "malloc failed\n" )
53     h->input = cli_input;
54     h->p_handle = *p_handle;
55     h->next_frame = -1;
56     h->next_args = malloc( sizeof(thread_input_arg_t) );
57     if( !h->next_args )
58         return -1;
59     h->next_args->h = h;
60     h->next_args->status = 0;
61     h->frame_total = info->num_frames;
62
63     if( x264_threadpool_init( &h->pool, 1, NULL, NULL ) )
64         return -1;
65
66     *p_handle = h;
67     return 0;
68 }
69
70 static void read_frame_thread_int( thread_input_arg_t *i )
71 {
72     i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
73 }
74
75 static int read_frame( cli_pic_t *p_pic, hnd_t handle, int i_frame )
76 {
77     thread_hnd_t *h = handle;
78     int ret = 0;
79
80     if( h->next_frame >= 0 )
81     {
82         x264_threadpool_wait( h->pool, h->next_args );
83         ret |= h->next_args->status;
84     }
85
86     if( h->next_frame == i_frame )
87         XCHG( cli_pic_t, *p_pic, h->pic );
88     else
89     {
90         if( h->next_frame >= 0 )
91             thread_input.release_frame( &h->pic, handle );
92         ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
93     }
94
95     if( !h->frame_total || i_frame+1 < h->frame_total )
96     {
97         h->next_frame =
98         h->next_args->i_frame = i_frame+1;
99         h->next_args->pic = &h->pic;
100         x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args );
101     }
102     else
103         h->next_frame = -1;
104
105     return ret;
106 }
107
108 static int release_frame( cli_pic_t *pic, hnd_t handle )
109 {
110     thread_hnd_t *h = handle;
111     if( h->input.release_frame )
112         return h->input.release_frame( pic, h->p_handle );
113     return 0;
114 }
115
116 static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
117 {
118     thread_hnd_t *h = handle;
119     return h->input.picture_alloc( pic, h->p_handle, csp, width, height );
120 }
121
122 static void picture_clean( cli_pic_t *pic, hnd_t handle )
123 {
124     thread_hnd_t *h = handle;
125     h->input.picture_clean( pic, h->p_handle );
126 }
127
128 static int close_file( hnd_t handle )
129 {
130     thread_hnd_t *h = handle;
131     x264_threadpool_delete( h->pool );
132     h->input.picture_clean( &h->pic, h->p_handle );
133     h->input.close_file( h->p_handle );
134     free( h->next_args );
135     free( h );
136     return 0;
137 }
138
139 const cli_input_t thread_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };