Coverage Report

Created: 2026-07-30 06:27

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