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