]> git.sesse.net Git - x264/blob - encoder/lookahead.c
Preprocessing cosmetics
[x264] / encoder / lookahead.c
1 /*****************************************************************************
2  * lookahead.c: Lookahead slicetype decisions for x264
3  *****************************************************************************
4  * Lookahead.c and associated modifications:
5  *     Copyright (C) 2008 Avail Media
6  *
7  * Authors: Michael Kazmier <mkazmier@availmedia.com>
8  *          Alex Giladi <agiladi@availmedia.com>
9  *          Steven Walters <kemuri9@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /* LOOKAHEAD (threaded and non-threaded mode)
27  *
28  * Lookahead types:
29  *     [1] Slice type / scene cut;
30  *
31  * In non-threaded mode, we run the existing slicetype decision code as it was.
32  * In threaded mode, we run in a separate thread, that lives between the calls
33  * to x264_encoder_open() and x264_encoder_close(), and performs lookahead for
34  * the number of frames specified in rc_lookahead.  Recommended setting is
35  * # of bframes + # of threads.
36  */
37 #include "common/common.h"
38 #include "analyse.h"
39
40 static void x264_lookahead_shift( x264_synch_frame_list_t *dst, x264_synch_frame_list_t *src, int count )
41 {
42     int i = count;
43     while( i-- )
44     {
45         assert( dst->i_size < dst->i_max_size );
46         assert( src->i_size );
47         dst->list[ dst->i_size++ ] = x264_frame_shift( src->list );
48         src->i_size--;
49     }
50     if( count )
51     {
52         x264_pthread_cond_broadcast( &dst->cv_fill );
53         x264_pthread_cond_broadcast( &src->cv_empty );
54     }
55 }
56
57 static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
58 {
59     if( h->lookahead->last_nonb )
60         x264_frame_push_unused( h, h->lookahead->last_nonb );
61     h->lookahead->last_nonb = new_nonb;
62     new_nonb->i_reference_count++;
63 }
64
65 #if HAVE_PTHREAD
66 static void x264_lookahead_slicetype_decide( x264_t *h )
67 {
68     x264_stack_align( x264_slicetype_decide, h );
69
70     x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
71
72     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
73     while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
74         x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
75
76     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
77     x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, h->lookahead->next.list[0]->i_bframes + 1 );
78     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
79
80     /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
81     if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
82         x264_stack_align( x264_slicetype_analyse, h, 1 );
83
84     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
85 }
86
87 static void x264_lookahead_thread( x264_t *h )
88 {
89     int shift;
90 #if HAVE_MMX
91     if( h->param.cpu&X264_CPU_SSE_MISALIGN )
92         x264_cpu_mask_misalign_sse();
93 #endif
94     while( !h->lookahead->b_exit_thread )
95     {
96         x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
97         x264_pthread_mutex_lock( &h->lookahead->next.mutex );
98         shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
99         x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
100         x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
101         if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length )
102         {
103             while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
104                 x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
105             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
106         }
107         else
108         {
109             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
110             x264_lookahead_slicetype_decide( h );
111         }
112     }   /* end of input frames */
113     x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
114     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
115     x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
116     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
117     x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
118     while( h->lookahead->next.i_size )
119         x264_lookahead_slicetype_decide( h );
120     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
121     h->lookahead->b_thread_active = 0;
122     x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
123     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
124 }
125 #endif
126
127 int x264_lookahead_init( x264_t *h, int i_slicetype_length )
128 {
129     x264_lookahead_t *look;
130     CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
131     for( int i = 0; i < h->param.i_threads; i++ )
132         h->thread[i]->lookahead = look;
133
134     look->i_last_keyframe = - h->param.i_keyint_max;
135     look->b_analyse_keyframe = (h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead))
136                                && !h->param.rc.b_stat_read;
137     look->i_slicetype_length = i_slicetype_length;
138
139     /* init frame lists */
140     if( x264_synch_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
141         x264_synch_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
142         x264_synch_frame_list_init( &look->ofbuf, h->frames.i_delay+3 ) )
143         goto fail;
144
145     if( !h->param.i_sync_lookahead )
146         return 0;
147
148     x264_t *look_h = h->thread[h->param.i_threads];
149     *look_h = *h;
150     if( x264_macroblock_cache_allocate( look_h ) )
151         goto fail;
152
153     if( x264_macroblock_thread_allocate( look_h, 1 ) < 0 )
154         goto fail;
155
156     if( x264_pthread_create( &look_h->thread_handle, NULL, (void *)x264_lookahead_thread, look_h ) )
157         goto fail;
158     look->b_thread_active = 1;
159
160     return 0;
161 fail:
162     x264_free( look );
163     return -1;
164 }
165
166 void x264_lookahead_delete( x264_t *h )
167 {
168     if( h->param.i_sync_lookahead )
169     {
170         x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
171         h->lookahead->b_exit_thread = 1;
172         x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
173         x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
174         x264_pthread_join( h->thread[h->param.i_threads]->thread_handle, NULL );
175         x264_macroblock_cache_free( h->thread[h->param.i_threads] );
176         x264_macroblock_thread_free( h->thread[h->param.i_threads], 1 );
177         x264_free( h->thread[h->param.i_threads] );
178     }
179     x264_synch_frame_list_delete( &h->lookahead->ifbuf );
180     x264_synch_frame_list_delete( &h->lookahead->next );
181     if( h->lookahead->last_nonb )
182         x264_frame_push_unused( h, h->lookahead->last_nonb );
183     x264_synch_frame_list_delete( &h->lookahead->ofbuf );
184     x264_free( h->lookahead );
185 }
186
187 void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
188 {
189     if( h->param.i_sync_lookahead )
190         x264_synch_frame_list_push( &h->lookahead->ifbuf, frame );
191     else
192         x264_synch_frame_list_push( &h->lookahead->next, frame );
193 }
194
195 int x264_lookahead_is_empty( x264_t *h )
196 {
197     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
198     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
199     int b_empty = !h->lookahead->next.i_size && !h->lookahead->ofbuf.i_size;
200     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
201     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
202     return b_empty;
203 }
204
205 static void x264_lookahead_encoder_shift( x264_t *h )
206 {
207     if( !h->lookahead->ofbuf.i_size )
208         return;
209     int i_frames = h->lookahead->ofbuf.list[0]->i_bframes + 1;
210     while( i_frames-- )
211     {
212         x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
213         h->lookahead->ofbuf.i_size--;
214     }
215     x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
216 }
217
218 void x264_lookahead_get_frames( x264_t *h )
219 {
220     if( h->param.i_sync_lookahead )
221     {   /* We have a lookahead thread, so get frames from there */
222         x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
223         while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
224             x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
225         x264_lookahead_encoder_shift( h );
226         x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
227     }
228     else
229     {   /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
230
231         if( h->frames.current[0] || !h->lookahead->next.i_size )
232             return;
233
234         x264_stack_align( x264_slicetype_decide, h );
235         x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
236         x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, h->lookahead->next.list[0]->i_bframes + 1 );
237
238         /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
239         if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
240             x264_stack_align( x264_slicetype_analyse, h, 1 );
241
242         x264_lookahead_encoder_shift( h );
243     }
244 }