]> git.sesse.net Git - x264/blob - encoder/lookahead.c
Make B-pyramid spec-compliant
[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 "common/cpu.h"
39 #include "analyse.h"
40
41 static void x264_lookahead_shift( x264_synch_frame_list_t *dst, x264_synch_frame_list_t *src, int count )
42 {
43     int i = count;
44     while( i-- )
45     {
46         assert( dst->i_size < dst->i_max_size );
47         assert( src->i_size );
48         dst->list[ dst->i_size++ ] = x264_frame_shift( src->list );
49         src->i_size--;
50     }
51     if( count )
52     {
53         x264_pthread_cond_broadcast( &dst->cv_fill );
54         x264_pthread_cond_broadcast( &src->cv_empty );
55     }
56 }
57
58 static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
59 {
60     if( h->lookahead->last_nonb )
61         x264_frame_push_unused( h, h->lookahead->last_nonb );
62     h->lookahead->last_nonb = new_nonb;
63     new_nonb->i_reference_count++;
64 }
65
66 #ifdef HAVE_PTHREAD
67 static void x264_lookahead_slicetype_decide( x264_t *h )
68 {
69     int bframes = 0;
70     x264_stack_align( x264_slicetype_decide, h );
71
72     while( IS_X264_TYPE_B( h->lookahead->next.list[bframes]->i_type ) )
73         bframes++;
74     x264_lookahead_update_last_nonb( h, h->lookahead->next.list[bframes] );
75
76     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
77     while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
78         x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
79
80     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
81     x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, bframes + 1 );
82     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
83
84     /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
85     if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
86         x264_stack_align( x264_slicetype_analyse, h, 1 );
87
88     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
89 }
90
91 static void x264_lookahead_thread( x264_t *h )
92 {
93     int shift;
94 #ifdef HAVE_MMX
95     if( h->param.cpu&X264_CPU_SSE_MISALIGN )
96         x264_cpu_mask_misalign_sse();
97 #endif
98     while( !h->lookahead->b_exit_thread )
99     {
100         x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
101         x264_pthread_mutex_lock( &h->lookahead->next.mutex );
102         shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
103         x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
104         x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
105         if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length )
106         {
107             while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
108                 x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
109             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
110         }
111         else
112         {
113             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
114             x264_lookahead_slicetype_decide( h );
115         }
116     }   /* end of input frames */
117     x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
118     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
119     x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
120     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
121     x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
122     while( h->lookahead->next.i_size )
123         x264_lookahead_slicetype_decide( h );
124     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
125     h->lookahead->b_thread_active = 0;
126     x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
127     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
128 }
129 #endif
130
131 int x264_lookahead_init( x264_t *h, int i_slicetype_length )
132 {
133     x264_lookahead_t *look;
134     CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
135     int i;
136     for( i = 0; i < h->param.i_threads; i++ )
137         h->thread[i]->lookahead = look;
138
139     look->i_last_idr = - 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_synch_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
146         x264_synch_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
147         x264_synch_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     x264_t *look_h = h->thread[h->param.i_threads];
154     *look_h = *h;
155     if( x264_macroblock_cache_init( look_h ) )
156         goto fail;
157
158     if( x264_pthread_create( &look_h->thread_handle, NULL, (void *)x264_lookahead_thread, look_h ) )
159         goto fail;
160     look->b_thread_active = 1;
161
162     return 0;
163 fail:
164     x264_free( look );
165     return -1;
166 }
167
168 void x264_lookahead_delete( x264_t *h )
169 {
170     if( h->param.i_sync_lookahead )
171     {
172         h->lookahead->b_exit_thread = 1;
173         x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
174         x264_pthread_join( h->thread[h->param.i_threads]->thread_handle, NULL );
175         x264_macroblock_cache_end( h->thread[h->param.i_threads] );
176         x264_free( h->thread[h->param.i_threads] );
177     }
178     x264_synch_frame_list_delete( &h->lookahead->ifbuf );
179     x264_synch_frame_list_delete( &h->lookahead->next );
180     if( h->lookahead->last_nonb )
181         x264_frame_push_unused( h, h->lookahead->last_nonb );
182     x264_synch_frame_list_delete( &h->lookahead->ofbuf );
183     x264_free( h->lookahead );
184 }
185
186 void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
187 {
188     if( h->param.i_sync_lookahead )
189         x264_synch_frame_list_push( &h->lookahead->ifbuf, frame );
190     else
191         x264_synch_frame_list_push( &h->lookahead->next, frame );
192 }
193
194 int x264_lookahead_is_empty( x264_t *h )
195 {
196     int b_empty;
197     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
198     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
199     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     int bframes  = 0;
208     int i_frames = 0;
209
210     while( h->lookahead->ofbuf.list[i_frames] )
211     {
212         if( IS_X264_TYPE_B( h->lookahead->ofbuf.list[bframes]->i_type ) )
213             bframes++;
214         else
215             break;
216         i_frames++;
217     }
218     if( h->lookahead->ofbuf.list[i_frames] )
219     {
220         int i_dts = h->lookahead->ofbuf.list[0]->i_frame;
221         h->lookahead->ofbuf.list[bframes]->i_dts = i_dts;
222         x264_frame_push( h->frames.current, x264_frame_shift( &h->lookahead->ofbuf.list[bframes] ) );
223         h->lookahead->ofbuf.i_size--;
224         if( h->param.i_bframe_pyramid && bframes > 1 )
225         {
226             x264_frame_t *mid = x264_frame_shift( &h->lookahead->ofbuf.list[bframes/2] );
227             h->lookahead->ofbuf.i_size--;
228             mid->i_type = X264_TYPE_BREF;
229             mid->i_dts = ++i_dts;
230             x264_frame_push( h->frames.current, mid );
231             bframes--;
232         }
233         while( bframes-- )
234         {
235             h->lookahead->ofbuf.list[0]->i_dts = ++i_dts;
236             x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
237             h->lookahead->ofbuf.i_size--;
238         }
239         x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
240     }
241 }
242
243 void x264_lookahead_get_frames( x264_t *h )
244 {
245     if( h->param.i_sync_lookahead )
246     {   /* We have a lookahead thread, so get frames from there */
247         x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
248         while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
249             x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
250         x264_lookahead_encoder_shift( h );
251         x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
252     }
253     else
254     {   /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
255
256         if( h->frames.current[0] || !h->lookahead->next.i_size )
257             return;
258
259         x264_stack_align( x264_slicetype_decide, h );
260
261         int bframes=0;
262         while( IS_X264_TYPE_B( h->lookahead->next.list[bframes]->i_type ) )
263             bframes++;
264
265         x264_lookahead_update_last_nonb( h, h->lookahead->next.list[bframes] );
266         x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, bframes + 1 );
267
268         /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
269         if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
270             x264_stack_align( x264_slicetype_analyse, h, 1 );
271
272         x264_lookahead_encoder_shift( h );
273     }
274 }