Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/x264/encoder/lookahead.c
Line
Count
Source
1
/*****************************************************************************
2
 * lookahead.c: high-level lookahead functions
3
 *****************************************************************************
4
 * Copyright (C) 2010-2025 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
42
static void lookahead_shift( x264_sync_frame_list_t *dst, x264_sync_frame_list_t *src, int count )
43
0
{
44
0
    int i = count;
45
0
    while( i-- )
46
0
    {
47
0
        assert( dst->i_size < dst->i_max_size );
48
0
        assert( src->i_size );
49
0
        dst->list[ dst->i_size++ ] = x264_frame_shift( src->list );
50
0
        src->i_size--;
51
0
    }
52
0
    if( count )
53
0
    {
54
0
        x264_pthread_cond_broadcast( &dst->cv_fill );
55
0
        x264_pthread_cond_broadcast( &src->cv_empty );
56
0
    }
57
0
}
58
59
static void lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
60
0
{
61
0
    if( h->lookahead->last_nonb )
62
0
        x264_frame_push_unused( h, h->lookahead->last_nonb );
63
0
    h->lookahead->last_nonb = new_nonb;
64
0
    new_nonb->i_reference_count++;
65
0
}
66
67
#if HAVE_THREAD
68
static void lookahead_slicetype_decide( x264_t *h )
69
0
{
70
0
    x264_slicetype_decide( h );
71
72
0
    lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
73
0
    int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
74
75
0
    x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
76
0
    while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
77
0
        x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
78
79
0
    x264_pthread_mutex_lock( &h->lookahead->next.mutex );
80
0
    lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
81
0
    x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
82
83
    /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
84
0
    if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
85
0
        x264_slicetype_analyse( h, shift_frames );
86
87
0
    x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
88
0
}
89
90
REALIGN_STACK static void *lookahead_thread( x264_t *h )
91
0
{
92
0
    while( 1 )
93
0
    {
94
0
        x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
95
0
        if( h->lookahead->b_exit_thread )
96
0
        {
97
0
            x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
98
0
            break;
99
0
        }
100
0
        x264_pthread_mutex_lock( &h->lookahead->next.mutex );
101
0
        int shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
102
0
        lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
103
0
        x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
104
0
        if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length + h->param.b_vfr_input )
105
0
        {
106
0
            while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
107
0
                x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
108
0
            x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
109
0
        }
110
0
        else
111
0
        {
112
0
            x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
113
0
            lookahead_slicetype_decide( h );
114
0
        }
115
0
    }   /* end of input frames */
116
0
    x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
117
0
    x264_pthread_mutex_lock( &h->lookahead->next.mutex );
118
0
    lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
119
0
    x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
120
0
    x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
121
0
    while( h->lookahead->next.i_size )
122
0
        lookahead_slicetype_decide( h );
123
0
    x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
124
0
    h->lookahead->b_thread_active = 0;
125
0
    x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
126
0
    x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
127
0
    return NULL;
128
0
}
129
130
#endif
131
132
int x264_lookahead_init( x264_t *h, int i_slicetype_length )
133
0
{
134
0
    x264_lookahead_t *look;
135
0
    CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
136
0
    for( int i = 0; i < h->param.i_threads; i++ )
137
0
        h->thread[i]->lookahead = look;
138
139
0
    look->i_last_keyframe = - h->param.i_keyint_max;
140
0
    look->b_analyse_keyframe = (h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead))
141
0
                               && !h->param.rc.b_stat_read;
142
0
    look->i_slicetype_length = i_slicetype_length;
143
144
    /* init frame lists */
145
0
    if( x264_sync_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
146
0
        x264_sync_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
147
0
        x264_sync_frame_list_init( &look->ofbuf, h->frames.i_delay+3 ) )
148
0
        goto fail;
149
150
0
    if( !h->param.i_sync_lookahead )
151
0
        return 0;
152
153
0
    x264_t *look_h = h->thread[h->param.i_threads];
154
0
    *look_h = *h;
155
0
    if( x264_macroblock_cache_allocate( look_h ) )
156
0
        goto fail;
157
158
0
    if( x264_macroblock_thread_allocate( look_h, 1 ) < 0 )
159
0
        goto fail;
160
161
0
    if( x264_pthread_create( &look->thread_handle, NULL, (void*)lookahead_thread, look_h ) )
162
0
        goto fail;
163
0
    look->b_thread_active = 1;
164
165
0
    return 0;
166
0
fail:
167
0
    x264_free( look );
168
0
    return -1;
169
0
}
Unexecuted instantiation: x264_8_lookahead_init
Unexecuted instantiation: x264_10_lookahead_init
170
171
void x264_lookahead_delete( x264_t *h )
172
0
{
173
0
    if( h->param.i_sync_lookahead )
174
0
    {
175
0
        x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
176
0
        h->lookahead->b_exit_thread = 1;
177
0
        x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
178
0
        x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
179
0
        x264_pthread_join( h->lookahead->thread_handle, NULL );
180
0
        x264_macroblock_cache_free( h->thread[h->param.i_threads] );
181
0
        x264_macroblock_thread_free( h->thread[h->param.i_threads], 1 );
182
0
        x264_free( h->thread[h->param.i_threads] );
183
0
    }
184
0
    x264_sync_frame_list_delete( &h->lookahead->ifbuf );
185
0
    x264_sync_frame_list_delete( &h->lookahead->next );
186
0
    if( h->lookahead->last_nonb )
187
0
        x264_frame_push_unused( h, h->lookahead->last_nonb );
188
0
    x264_sync_frame_list_delete( &h->lookahead->ofbuf );
189
0
    x264_free( h->lookahead );
190
0
}
Unexecuted instantiation: x264_8_lookahead_delete
Unexecuted instantiation: x264_10_lookahead_delete
191
192
void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
193
0
{
194
0
    if( h->param.i_sync_lookahead )
195
0
        x264_sync_frame_list_push( &h->lookahead->ifbuf, frame );
196
0
    else
197
0
        x264_sync_frame_list_push( &h->lookahead->next, frame );
198
0
}
Unexecuted instantiation: x264_8_lookahead_put_frame
Unexecuted instantiation: x264_10_lookahead_put_frame
199
200
int x264_lookahead_is_empty( x264_t *h )
201
0
{
202
0
    x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
203
0
    x264_pthread_mutex_lock( &h->lookahead->next.mutex );
204
0
    int b_empty = !h->lookahead->next.i_size && !h->lookahead->ofbuf.i_size;
205
0
    x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
206
0
    x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
207
0
    return b_empty;
208
0
}
Unexecuted instantiation: x264_8_lookahead_is_empty
Unexecuted instantiation: x264_10_lookahead_is_empty
209
210
static void lookahead_encoder_shift( x264_t *h )
211
0
{
212
0
    if( !h->lookahead->ofbuf.i_size )
213
0
        return;
214
0
    int i_frames = h->lookahead->ofbuf.list[0]->i_bframes + 1;
215
0
    while( i_frames-- )
216
0
    {
217
0
        x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
218
0
        h->lookahead->ofbuf.i_size--;
219
0
    }
220
0
    x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
221
0
}
222
223
void x264_lookahead_get_frames( x264_t *h )
224
0
{
225
0
    if( h->param.i_sync_lookahead )
226
0
    {   /* We have a lookahead thread, so get frames from there */
227
0
        x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
228
0
        while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
229
0
            x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
230
0
        lookahead_encoder_shift( h );
231
0
        x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
232
0
    }
233
0
    else
234
0
    {   /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
235
236
0
        if( h->frames.current[0] || !h->lookahead->next.i_size )
237
0
            return;
238
239
0
        x264_slicetype_decide( h );
240
0
        lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
241
0
        int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
242
0
        lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
243
244
        /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
245
0
        if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
246
0
            x264_slicetype_analyse( h, shift_frames );
247
248
0
        lookahead_encoder_shift( h );
249
0
    }
250
0
}
Unexecuted instantiation: x264_8_lookahead_get_frames
Unexecuted instantiation: x264_10_lookahead_get_frames