Coverage Report

Created: 2026-07-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/x264/common/frame.c
Line
Count
Source
1
/*****************************************************************************
2
 * frame.c: frame handling
3
 *****************************************************************************
4
 * Copyright (C) 2003-2025 x264 project
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Loren Merritt <lorenm@u.washington.edu>
8
 *          Fiona Glaser <fiona@x264.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
#include "common.h"
29
30
static int align_stride( int x, int align, int disalign )
31
1.98k
{
32
1.98k
    x = ALIGN( x, align );
33
1.98k
    if( !(x&(disalign-1)) )
34
0
        x += align;
35
1.98k
    return x;
36
1.98k
}
37
38
static int align_plane_size( int x, int disalign )
39
2.30k
{
40
2.30k
    if( !(x&(disalign-1)) )
41
978
        x += X264_MAX( 128, NATIVE_ALIGN ) / SIZEOF_PIXEL;
42
2.30k
    return x;
43
2.30k
}
44
45
static int frame_internal_csp( int external_csp )
46
1.15k
{
47
1.15k
    int csp = external_csp & X264_CSP_MASK;
48
1.15k
    if( csp == X264_CSP_I400 )
49
0
        return X264_CSP_I400;
50
1.15k
    if( csp >= X264_CSP_I420 && csp < X264_CSP_I422 )
51
1.15k
        return X264_CSP_NV12;
52
0
    if( csp >= X264_CSP_I422 && csp < X264_CSP_I444 )
53
0
        return X264_CSP_NV16;
54
0
    if( csp >= X264_CSP_I444 && csp <= X264_CSP_RGB )
55
0
        return X264_CSP_I444;
56
0
    return X264_CSP_NONE;
57
0
}
58
59
static x264_frame_t *frame_new( x264_t *h, int b_fdec )
60
991
{
61
991
    x264_frame_t *frame;
62
991
    int i_csp = frame_internal_csp( h->param.i_csp );
63
991
    int i_mb_count = h->mb.i_mb_count;
64
991
    int i_stride, i_width, i_lines, luma_plane_count;
65
991
    int i_padv = PADV << PARAM_INTERLACED;
66
991
    int align = NATIVE_ALIGN / SIZEOF_PIXEL;
67
991
#if ARCH_X86 || ARCH_X86_64
68
991
    if( h->param.cpu&X264_CPU_CACHELINE_64 || h->param.cpu&X264_CPU_AVX512 )
69
0
        align = 64 / SIZEOF_PIXEL;
70
991
    else if( h->param.cpu&X264_CPU_CACHELINE_32 || h->param.cpu&X264_CPU_AVX )
71
0
        align = 32 / SIZEOF_PIXEL;
72
991
    else
73
991
        align = 16 / SIZEOF_PIXEL;
74
991
#endif
75
#if ARCH_PPC
76
    int disalign = (1<<9) / SIZEOF_PIXEL;
77
#else
78
991
    int disalign = (1<<10) / SIZEOF_PIXEL;
79
991
#endif
80
81
991
    CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
82
991
    PREALLOC_INIT
83
84
    /* allocate frame data (+64 for extra data for me) */
85
991
    i_width  = h->mb.i_mb_width*16;
86
991
    i_lines  = h->mb.i_mb_height*16;
87
991
    i_stride = align_stride( i_width + PADH2, align, disalign );
88
89
991
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
90
991
    {
91
991
        luma_plane_count = 1;
92
991
        frame->i_plane = 2;
93
2.97k
        for( int i = 0; i < 2; i++ )
94
1.98k
        {
95
1.98k
            frame->i_width[i] = i_width >> i;
96
1.98k
            frame->i_lines[i] = i_lines >> (i && i_csp == X264_CSP_NV12);
97
1.98k
            frame->i_stride[i] = i_stride;
98
1.98k
        }
99
991
    }
100
0
    else if( i_csp == X264_CSP_I444 )
101
0
    {
102
0
        luma_plane_count = 3;
103
0
        frame->i_plane = 3;
104
0
        for( int i = 0; i < 3; i++ )
105
0
        {
106
0
            frame->i_width[i] = i_width;
107
0
            frame->i_lines[i] = i_lines;
108
0
            frame->i_stride[i] = i_stride;
109
0
        }
110
0
    }
111
0
    else if( i_csp == X264_CSP_I400 )
112
0
    {
113
0
        luma_plane_count = 1;
114
0
        frame->i_plane = 1;
115
0
        frame->i_width[0] = i_width;
116
0
        frame->i_lines[0] = i_lines;
117
0
        frame->i_stride[0] = i_stride;
118
0
    }
119
0
    else
120
0
        goto fail;
121
122
991
    frame->i_csp = i_csp;
123
991
    frame->i_width_lowres = frame->i_width[0]/2;
124
991
    frame->i_lines_lowres = frame->i_lines[0]/2;
125
991
    frame->i_stride_lowres = align_stride( frame->i_width_lowres + PADH2, align, disalign<<1 );
126
127
5.04k
    for( int i = 0; i < h->param.i_bframe + 2; i++ )
128
22.5k
        for( int j = 0; j < h->param.i_bframe + 2; j++ )
129
18.4k
            PREALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
130
131
991
    frame->i_poc = -1;
132
991
    frame->i_type = X264_TYPE_AUTO;
133
991
    frame->i_qpplus1 = X264_QP_AUTO;
134
991
    frame->i_pts = -1;
135
991
    frame->i_frame = -1;
136
991
    frame->i_frame_num = -1;
137
991
    frame->i_lines_completed = -1;
138
991
    frame->b_fdec = b_fdec;
139
991
    frame->i_pic_struct = PIC_STRUCT_AUTO;
140
991
    frame->i_field_cnt = -1;
141
991
    frame->i_duration =
142
991
    frame->i_cpb_duration =
143
991
    frame->i_dpb_output_delay =
144
991
    frame->i_cpb_delay = 0;
145
991
    frame->i_coded_fields_lookahead =
146
991
    frame->i_cpb_delay_lookahead = -1;
147
148
991
    frame->orig = frame;
149
150
991
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
151
991
    {
152
991
        int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
153
991
        int chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + 2*chroma_padv));
154
991
        PREALLOC( frame->buffer[1], chroma_plane_size * SIZEOF_PIXEL );
155
991
        if( PARAM_INTERLACED )
156
0
            PREALLOC( frame->buffer_fld[1], chroma_plane_size * SIZEOF_PIXEL );
157
991
    }
158
159
    /* all 4 luma planes allocated together, since the cacheline split code
160
     * requires them to be in-phase wrt cacheline alignment. */
161
162
1.98k
    for( int p = 0; p < luma_plane_count; p++ )
163
991
    {
164
991
        int64_t luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
165
991
        if( h->param.analyse.i_subpel_refine && b_fdec )
166
831
            luma_plane_size *= 4;
167
168
        /* FIXME: Don't allocate both buffers in non-adaptive MBAFF. */
169
991
        PREALLOC( frame->buffer[p], luma_plane_size * SIZEOF_PIXEL );
170
991
        if( PARAM_INTERLACED )
171
0
            PREALLOC( frame->buffer_fld[p], luma_plane_size * SIZEOF_PIXEL );
172
991
    }
173
174
991
    frame->b_duplicate = 0;
175
176
991
    if( b_fdec ) /* fdec frame */
177
831
    {
178
831
        PREALLOC( frame->mb_type, i_mb_count * sizeof(int8_t) );
179
831
        PREALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t) );
180
831
        PREALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
181
831
        PREALLOC( frame->mv16x16, 2*(i_mb_count+1) * sizeof(int16_t) );
182
831
        PREALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
183
831
        if( h->param.i_bframe )
184
580
        {
185
580
            PREALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
186
580
            PREALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
187
580
        }
188
251
        else
189
251
        {
190
251
            frame->mv[1]  = NULL;
191
251
            frame->ref[1] = NULL;
192
251
        }
193
831
        PREALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
194
831
        PREALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
195
831
        PREALLOC( frame->f_row_qscale, i_lines/16 * sizeof(float) );
196
831
        if( h->param.analyse.i_me_method >= X264_ME_ESA )
197
0
            PREALLOC( frame->buffer[3], frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) << h->frames.b_have_sub8x8_esa );
198
831
        if( PARAM_INTERLACED )
199
0
            PREALLOC( frame->field, i_mb_count * sizeof(uint8_t) );
200
831
        if( h->param.analyse.b_mb_info )
201
0
            PREALLOC( frame->effective_qp, i_mb_count * sizeof(uint8_t) );
202
831
    }
203
160
    else /* fenc frame */
204
160
    {
205
160
        if( h->frames.b_have_lowres )
206
160
        {
207
160
            int64_t luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
208
209
160
            PREALLOC( frame->buffer_lowres, 4 * luma_plane_size * SIZEOF_PIXEL );
210
211
432
            for( int j = 0; j <= !!h->param.i_bframe; j++ )
212
1.21k
                for( int i = 0; i <= h->param.i_bframe; i++ )
213
944
                {
214
944
                    PREALLOC( frame->lowres_mvs[j][i], 2*i_mb_count*sizeof(int16_t) );
215
944
                    PREALLOC( frame->lowres_mv_costs[j][i], i_mb_count*sizeof(int) );
216
944
                }
217
160
            PREALLOC( frame->i_propagate_cost, i_mb_count * sizeof(uint16_t) );
218
816
            for( int j = 0; j <= h->param.i_bframe+1; j++ )
219
3.64k
                for( int i = 0; i <= h->param.i_bframe+1; i++ )
220
2.99k
                    PREALLOC( frame->lowres_costs[j][i], i_mb_count * sizeof(uint16_t) );
221
160
        }
222
160
        if( h->param.rc.i_aq_mode )
223
112
        {
224
112
            PREALLOC( frame->f_qp_offset, i_mb_count * sizeof(float) );
225
112
            PREALLOC( frame->f_qp_offset_aq, i_mb_count * sizeof(float) );
226
112
            if( h->frames.b_have_lowres )
227
112
                PREALLOC( frame->i_inv_qscale_factor, i_mb_count * sizeof(uint16_t) );
228
112
        }
229
230
        /* mbtree asm can overread the input buffers, make sure we don't read outside of allocated memory. */
231
160
        if( h->frames.b_have_lowres )
232
160
            prealloc_size += NATIVE_ALIGN;
233
160
    }
234
235
991
    PREALLOC_END( frame->base );
236
237
991
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
238
991
    {
239
991
        int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
240
991
        frame->plane[1] = frame->buffer[1] + frame->i_stride[1] * chroma_padv + PADH_ALIGN;
241
991
        if( PARAM_INTERLACED )
242
0
            frame->plane_fld[1] = frame->buffer_fld[1] + frame->i_stride[1] * chroma_padv + PADH_ALIGN;
243
991
    }
244
245
1.98k
    for( int p = 0; p < luma_plane_count; p++ )
246
991
    {
247
991
        int64_t luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
248
991
        if( h->param.analyse.i_subpel_refine && b_fdec )
249
831
        {
250
4.15k
            for( int i = 0; i < 4; i++ )
251
3.32k
            {
252
3.32k
                frame->filtered[p][i] = frame->buffer[p] + i*luma_plane_size + frame->i_stride[p] * i_padv + PADH_ALIGN;
253
3.32k
                if( PARAM_INTERLACED )
254
0
                    frame->filtered_fld[p][i] = frame->buffer_fld[p] + i*luma_plane_size + frame->i_stride[p] * i_padv + PADH_ALIGN;
255
3.32k
            }
256
831
            frame->plane[p] = frame->filtered[p][0];
257
831
            frame->plane_fld[p] = frame->filtered_fld[p][0];
258
831
        }
259
160
        else
260
160
        {
261
160
            frame->filtered[p][0] = frame->plane[p] = frame->buffer[p] + frame->i_stride[p] * i_padv + PADH_ALIGN;
262
160
            if( PARAM_INTERLACED )
263
0
                frame->filtered_fld[p][0] = frame->plane_fld[p] = frame->buffer_fld[p] + frame->i_stride[p] * i_padv + PADH_ALIGN;
264
160
        }
265
991
    }
266
267
991
    if( b_fdec )
268
831
    {
269
831
        M32( frame->mv16x16[0] ) = 0;
270
831
        frame->mv16x16++;
271
272
831
        if( h->param.analyse.i_me_method >= X264_ME_ESA )
273
0
            frame->integral = (uint16_t*)frame->buffer[3] + frame->i_stride[0] * i_padv + PADH_ALIGN;
274
831
    }
275
160
    else
276
160
    {
277
160
        if( h->frames.b_have_lowres )
278
160
        {
279
160
            int64_t luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
280
800
            for( int i = 0; i < 4; i++ )
281
640
                frame->lowres[i] = frame->buffer_lowres + frame->i_stride_lowres * PADV + PADH_ALIGN + i * luma_plane_size;
282
283
432
            for( int j = 0; j <= !!h->param.i_bframe; j++ )
284
1.21k
                for( int i = 0; i <= h->param.i_bframe; i++ )
285
944
                    memset( frame->lowres_mvs[j][i], 0, 2*i_mb_count*sizeof(int16_t) );
286
287
160
            frame->i_intra_cost = frame->lowres_costs[0][0];
288
160
            memset( frame->i_intra_cost, -1, i_mb_count * sizeof(uint16_t) );
289
290
160
            if( h->param.rc.i_aq_mode )
291
                /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
292
112
                memset( frame->i_inv_qscale_factor, 0, i_mb_count * sizeof(uint16_t) );
293
160
        }
294
160
    }
295
296
991
    if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
297
0
        goto fail;
298
991
    if( x264_pthread_cond_init( &frame->cv, NULL ) )
299
0
        goto fail;
300
301
991
#if HAVE_OPENCL
302
991
    frame->opencl.ocl = h->opencl.ocl;
303
991
#endif
304
305
991
    return frame;
306
307
0
fail:
308
0
    x264_free( frame );
309
0
    return NULL;
310
991
}
311
312
void x264_frame_delete( x264_frame_t *frame )
313
991
{
314
    /* Duplicate frames are blank copies of real frames (including pointers),
315
     * so freeing those pointers would cause a double free later. */
316
991
    if( !frame->b_duplicate )
317
991
    {
318
991
        x264_free( frame->base );
319
320
991
        if( frame->param && frame->param->param_free )
321
0
        {
322
0
            x264_param_cleanup( frame->param );
323
0
            frame->param->param_free( frame->param );
324
0
        }
325
991
        if( frame->mb_info_free )
326
0
            frame->mb_info_free( frame->mb_info );
327
991
        if( frame->extra_sei.sei_free )
328
0
        {
329
0
            for( int i = 0; i < frame->extra_sei.num_payloads; i++ )
330
0
                frame->extra_sei.sei_free( frame->extra_sei.payloads[i].payload );
331
0
            frame->extra_sei.sei_free( frame->extra_sei.payloads );
332
0
        }
333
991
        x264_pthread_mutex_destroy( &frame->mutex );
334
991
        x264_pthread_cond_destroy( &frame->cv );
335
#if HAVE_OPENCL
336
991
        x264_opencl_frame_delete( frame );
337
#endif
338
991
    }
339
991
    x264_free( frame );
340
991
}
x264_8_frame_delete
Line
Count
Source
313
991
{
314
    /* Duplicate frames are blank copies of real frames (including pointers),
315
     * so freeing those pointers would cause a double free later. */
316
991
    if( !frame->b_duplicate )
317
991
    {
318
991
        x264_free( frame->base );
319
320
991
        if( frame->param && frame->param->param_free )
321
0
        {
322
0
            x264_param_cleanup( frame->param );
323
0
            frame->param->param_free( frame->param );
324
0
        }
325
991
        if( frame->mb_info_free )
326
0
            frame->mb_info_free( frame->mb_info );
327
991
        if( frame->extra_sei.sei_free )
328
0
        {
329
0
            for( int i = 0; i < frame->extra_sei.num_payloads; i++ )
330
0
                frame->extra_sei.sei_free( frame->extra_sei.payloads[i].payload );
331
0
            frame->extra_sei.sei_free( frame->extra_sei.payloads );
332
0
        }
333
991
        x264_pthread_mutex_destroy( &frame->mutex );
334
991
        x264_pthread_cond_destroy( &frame->cv );
335
991
#if HAVE_OPENCL
336
991
        x264_opencl_frame_delete( frame );
337
991
#endif
338
991
    }
339
991
    x264_free( frame );
340
991
}
Unexecuted instantiation: x264_10_frame_delete
341
342
static int get_plane_ptr( x264_t *h, x264_picture_t *src, uint8_t **pix, int *stride, int plane, int xshift, int yshift )
343
480
{
344
480
    int width = h->param.i_width >> xshift;
345
480
    int height = h->param.i_height >> yshift;
346
480
    *pix = src->img.plane[plane];
347
480
    *stride = src->img.i_stride[plane];
348
480
    if( src->img.i_csp & X264_CSP_VFLIP )
349
0
    {
350
0
        *pix += (height-1) * *stride;
351
0
        *stride = -*stride;
352
0
    }
353
480
    if( width > abs(*stride) )
354
0
    {
355
0
        x264_log( h, X264_LOG_ERROR, "Input picture width (%d) is greater than stride (%d)\n", width, *stride );
356
0
        return -1;
357
0
    }
358
480
    return 0;
359
480
}
360
361
640
#define get_plane_ptr(...) do { if( get_plane_ptr(__VA_ARGS__) < 0 ) return -1; } while( 0 )
362
363
int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
364
160
{
365
160
    int i_csp = src->img.i_csp & X264_CSP_MASK;
366
160
    if( dst->i_csp != frame_internal_csp( i_csp ) )
367
0
    {
368
0
        x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
369
0
        return -1;
370
0
    }
371
372
#if HIGH_BIT_DEPTH
373
0
    if( !(src->img.i_csp & X264_CSP_HIGH_DEPTH) )
374
0
    {
375
0
        x264_log( h, X264_LOG_ERROR, "This build of x264 requires high depth input. Rebuild to support 8-bit input.\n" );
376
0
        return -1;
377
0
    }
378
#else
379
160
    if( src->img.i_csp & X264_CSP_HIGH_DEPTH )
380
0
    {
381
0
        x264_log( h, X264_LOG_ERROR, "This build of x264 requires 8-bit input. Rebuild to support high depth input.\n" );
382
0
        return -1;
383
0
    }
384
160
#endif
385
386
160
    if( BIT_DEPTH != 10 && i_csp == X264_CSP_V210 )
387
0
    {
388
0
        x264_log( h, X264_LOG_ERROR, "v210 input is only compatible with bit-depth of 10 bits\n" );
389
0
        return -1;
390
0
    }
391
392
160
    if( src->i_type < X264_TYPE_AUTO || src->i_type > X264_TYPE_KEYFRAME )
393
0
    {
394
0
        x264_log( h, X264_LOG_WARNING, "forced frame type (%d) at %d is unknown\n", src->i_type, h->frames.i_input );
395
0
        dst->i_forced_type = X264_TYPE_AUTO;
396
0
    }
397
160
    else
398
160
        dst->i_forced_type = src->i_type;
399
400
160
    dst->i_type     = dst->i_forced_type;
401
160
    dst->i_qpplus1  = src->i_qpplus1;
402
160
    dst->i_pts      = dst->i_reordered_pts = src->i_pts;
403
160
    dst->param      = src->param;
404
160
    dst->i_pic_struct = src->i_pic_struct;
405
160
    dst->extra_sei  = src->extra_sei;
406
160
    dst->opaque     = src->opaque;
407
160
    dst->mb_info    = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
408
160
    dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;
409
410
160
    uint8_t *pix[3];
411
160
    int stride[3];
412
160
    if( i_csp == X264_CSP_YUYV || i_csp == X264_CSP_UYVY )
413
0
    {
414
0
        int p = i_csp == X264_CSP_UYVY;
415
0
        h->mc.plane_copy_deinterleave_yuyv( dst->plane[p], dst->i_stride[p], dst->plane[p^1], dst->i_stride[p^1],
416
0
                                            (pixel*)src->img.plane[0], src->img.i_stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
417
0
    }
418
160
    else if( i_csp == X264_CSP_V210 )
419
0
    {
420
0
         stride[0] = src->img.i_stride[0];
421
0
         pix[0] = src->img.plane[0];
422
423
0
         h->mc.plane_copy_deinterleave_v210( dst->plane[0], dst->i_stride[0],
424
0
                                             dst->plane[1], dst->i_stride[1],
425
0
                                             (uint32_t *)pix[0], stride[0]/(int)sizeof(uint32_t), h->param.i_width, h->param.i_height );
426
0
    }
427
160
    else if( i_csp >= X264_CSP_BGR )
428
0
    {
429
0
         stride[0] = src->img.i_stride[0];
430
0
         pix[0] = src->img.plane[0];
431
0
         if( src->img.i_csp & X264_CSP_VFLIP )
432
0
         {
433
0
             pix[0] += (h->param.i_height-1) * stride[0];
434
0
             stride[0] = -stride[0];
435
0
         }
436
0
         int b = i_csp==X264_CSP_RGB;
437
0
         h->mc.plane_copy_deinterleave_rgb( dst->plane[1+b], dst->i_stride[1+b],
438
0
                                            dst->plane[0], dst->i_stride[0],
439
0
                                            dst->plane[2-b], dst->i_stride[2-b],
440
0
                                            (pixel*)pix[0], stride[0]/SIZEOF_PIXEL, i_csp==X264_CSP_BGRA ? 4 : 3, h->param.i_width, h->param.i_height );
441
0
    }
442
160
    else
443
160
    {
444
160
        int v_shift = CHROMA_V_SHIFT;
445
160
        get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
446
160
        h->mc.plane_copy( dst->plane[0], dst->i_stride[0], (pixel*)pix[0],
447
160
                          stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
448
160
        if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
449
0
        {
450
0
            get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, v_shift );
451
0
            h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
452
0
                              stride[1]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height>>v_shift );
453
0
        }
454
160
        else if( i_csp == X264_CSP_NV21 )
455
0
        {
456
0
            get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, v_shift );
457
0
            h->mc.plane_copy_swap( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
458
0
                                   stride[1]/SIZEOF_PIXEL, h->param.i_width>>1, h->param.i_height>>v_shift );
459
0
        }
460
160
        else if( i_csp == X264_CSP_I420 || i_csp == X264_CSP_I422 || i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16 )
461
160
        {
462
160
            int uv_swap = i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16;
463
160
            get_plane_ptr( h, src, &pix[1], &stride[1], uv_swap ? 2 : 1, 1, v_shift );
464
160
            get_plane_ptr( h, src, &pix[2], &stride[2], uv_swap ? 1 : 2, 1, v_shift );
465
160
            h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
466
160
                                         (pixel*)pix[1], stride[1]/SIZEOF_PIXEL,
467
160
                                         (pixel*)pix[2], stride[2]/SIZEOF_PIXEL,
468
160
                                         h->param.i_width>>1, h->param.i_height>>v_shift );
469
160
        }
470
0
        else if( i_csp == X264_CSP_I444 || i_csp == X264_CSP_YV24 )
471
0
        {
472
0
            get_plane_ptr( h, src, &pix[1], &stride[1], i_csp==X264_CSP_I444 ? 1 : 2, 0, 0 );
473
0
            get_plane_ptr( h, src, &pix[2], &stride[2], i_csp==X264_CSP_I444 ? 2 : 1, 0, 0 );
474
0
            h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
475
0
                              stride[1]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
476
0
            h->mc.plane_copy( dst->plane[2], dst->i_stride[2], (pixel*)pix[2],
477
0
                              stride[2]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
478
0
        }
479
160
    }
480
160
    return 0;
481
160
}
x264_8_frame_copy_picture
Line
Count
Source
364
160
{
365
160
    int i_csp = src->img.i_csp & X264_CSP_MASK;
366
160
    if( dst->i_csp != frame_internal_csp( i_csp ) )
367
0
    {
368
0
        x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
369
0
        return -1;
370
0
    }
371
372
#if HIGH_BIT_DEPTH
373
    if( !(src->img.i_csp & X264_CSP_HIGH_DEPTH) )
374
    {
375
        x264_log( h, X264_LOG_ERROR, "This build of x264 requires high depth input. Rebuild to support 8-bit input.\n" );
376
        return -1;
377
    }
378
#else
379
160
    if( src->img.i_csp & X264_CSP_HIGH_DEPTH )
380
0
    {
381
0
        x264_log( h, X264_LOG_ERROR, "This build of x264 requires 8-bit input. Rebuild to support high depth input.\n" );
382
0
        return -1;
383
0
    }
384
160
#endif
385
386
160
    if( BIT_DEPTH != 10 && i_csp == X264_CSP_V210 )
387
0
    {
388
0
        x264_log( h, X264_LOG_ERROR, "v210 input is only compatible with bit-depth of 10 bits\n" );
389
0
        return -1;
390
0
    }
391
392
160
    if( src->i_type < X264_TYPE_AUTO || src->i_type > X264_TYPE_KEYFRAME )
393
0
    {
394
0
        x264_log( h, X264_LOG_WARNING, "forced frame type (%d) at %d is unknown\n", src->i_type, h->frames.i_input );
395
0
        dst->i_forced_type = X264_TYPE_AUTO;
396
0
    }
397
160
    else
398
160
        dst->i_forced_type = src->i_type;
399
400
160
    dst->i_type     = dst->i_forced_type;
401
160
    dst->i_qpplus1  = src->i_qpplus1;
402
160
    dst->i_pts      = dst->i_reordered_pts = src->i_pts;
403
160
    dst->param      = src->param;
404
160
    dst->i_pic_struct = src->i_pic_struct;
405
160
    dst->extra_sei  = src->extra_sei;
406
160
    dst->opaque     = src->opaque;
407
160
    dst->mb_info    = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
408
160
    dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;
409
410
160
    uint8_t *pix[3];
411
160
    int stride[3];
412
160
    if( i_csp == X264_CSP_YUYV || i_csp == X264_CSP_UYVY )
413
0
    {
414
0
        int p = i_csp == X264_CSP_UYVY;
415
0
        h->mc.plane_copy_deinterleave_yuyv( dst->plane[p], dst->i_stride[p], dst->plane[p^1], dst->i_stride[p^1],
416
0
                                            (pixel*)src->img.plane[0], src->img.i_stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
417
0
    }
418
160
    else if( i_csp == X264_CSP_V210 )
419
0
    {
420
0
         stride[0] = src->img.i_stride[0];
421
0
         pix[0] = src->img.plane[0];
422
423
0
         h->mc.plane_copy_deinterleave_v210( dst->plane[0], dst->i_stride[0],
424
0
                                             dst->plane[1], dst->i_stride[1],
425
0
                                             (uint32_t *)pix[0], stride[0]/(int)sizeof(uint32_t), h->param.i_width, h->param.i_height );
426
0
    }
427
160
    else if( i_csp >= X264_CSP_BGR )
428
0
    {
429
0
         stride[0] = src->img.i_stride[0];
430
0
         pix[0] = src->img.plane[0];
431
0
         if( src->img.i_csp & X264_CSP_VFLIP )
432
0
         {
433
0
             pix[0] += (h->param.i_height-1) * stride[0];
434
0
             stride[0] = -stride[0];
435
0
         }
436
0
         int b = i_csp==X264_CSP_RGB;
437
0
         h->mc.plane_copy_deinterleave_rgb( dst->plane[1+b], dst->i_stride[1+b],
438
0
                                            dst->plane[0], dst->i_stride[0],
439
0
                                            dst->plane[2-b], dst->i_stride[2-b],
440
0
                                            (pixel*)pix[0], stride[0]/SIZEOF_PIXEL, i_csp==X264_CSP_BGRA ? 4 : 3, h->param.i_width, h->param.i_height );
441
0
    }
442
160
    else
443
160
    {
444
160
        int v_shift = CHROMA_V_SHIFT;
445
160
        get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
446
160
        h->mc.plane_copy( dst->plane[0], dst->i_stride[0], (pixel*)pix[0],
447
160
                          stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
448
160
        if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
449
0
        {
450
0
            get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, v_shift );
451
0
            h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
452
0
                              stride[1]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height>>v_shift );
453
0
        }
454
160
        else if( i_csp == X264_CSP_NV21 )
455
0
        {
456
0
            get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, v_shift );
457
0
            h->mc.plane_copy_swap( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
458
0
                                   stride[1]/SIZEOF_PIXEL, h->param.i_width>>1, h->param.i_height>>v_shift );
459
0
        }
460
160
        else if( i_csp == X264_CSP_I420 || i_csp == X264_CSP_I422 || i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16 )
461
160
        {
462
160
            int uv_swap = i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16;
463
160
            get_plane_ptr( h, src, &pix[1], &stride[1], uv_swap ? 2 : 1, 1, v_shift );
464
160
            get_plane_ptr( h, src, &pix[2], &stride[2], uv_swap ? 1 : 2, 1, v_shift );
465
160
            h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
466
160
                                         (pixel*)pix[1], stride[1]/SIZEOF_PIXEL,
467
160
                                         (pixel*)pix[2], stride[2]/SIZEOF_PIXEL,
468
160
                                         h->param.i_width>>1, h->param.i_height>>v_shift );
469
160
        }
470
0
        else if( i_csp == X264_CSP_I444 || i_csp == X264_CSP_YV24 )
471
0
        {
472
0
            get_plane_ptr( h, src, &pix[1], &stride[1], i_csp==X264_CSP_I444 ? 1 : 2, 0, 0 );
473
0
            get_plane_ptr( h, src, &pix[2], &stride[2], i_csp==X264_CSP_I444 ? 2 : 1, 0, 0 );
474
0
            h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
475
0
                              stride[1]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
476
0
            h->mc.plane_copy( dst->plane[2], dst->i_stride[2], (pixel*)pix[2],
477
0
                              stride[2]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
478
0
        }
479
160
    }
480
160
    return 0;
481
160
}
Unexecuted instantiation: x264_10_frame_copy_picture
482
483
static ALWAYS_INLINE void pixel_memset( pixel *dst, pixel *src, int len, int size )
484
413k
{
485
413k
    uint8_t *dstp = (uint8_t*)dst;
486
413k
    uint32_t v1 = *src;
487
413k
    uint32_t v2 = size == 1 ? v1 + (v1 <<  8) : M16( src );
488
413k
    uint32_t v4 = size <= 2 ? v2 + (v2 << 16) : M32( src );
489
413k
    int i = 0;
490
413k
    len *= size;
491
492
    /* Align the input pointer if it isn't already */
493
413k
    if( (intptr_t)dstp & (WORD_SIZE - 1) )
494
120k
    {
495
120k
        if( size <= 2 && ((intptr_t)dstp & 3) )
496
22.1k
        {
497
22.1k
            if( size == 1 && ((intptr_t)dstp & 1) )
498
0
                dstp[i++] = v1;
499
22.1k
            if( (intptr_t)dstp & 2 )
500
22.1k
            {
501
22.1k
                M16( dstp+i ) = v2;
502
22.1k
                i += 2;
503
22.1k
            }
504
22.1k
        }
505
120k
        if( WORD_SIZE == 8 && (intptr_t)dstp & 4 )
506
108k
        {
507
108k
            M32( dstp+i ) = v4;
508
108k
            i += 4;
509
108k
        }
510
120k
    }
511
512
    /* Main copy loop */
513
413k
    if( WORD_SIZE == 8 )
514
413k
    {
515
413k
        uint64_t v8 = v4 + ((uint64_t)v4<<32);
516
1.77M
        for( ; i < len - 7; i+=8 )
517
1.35M
            M64( dstp+i ) = v8;
518
413k
    }
519
520k
    for( ; i < len - 3; i+=4 )
520
106k
        M32( dstp+i ) = v4;
521
522
    /* Finish up the last few bytes */
523
413k
    if( size <= 2 )
524
413k
    {
525
413k
        if( i < len - 1 )
526
0
        {
527
0
            M16( dstp+i ) = v2;
528
0
            i += 2;
529
0
        }
530
413k
        if( size == 1 && i != len )
531
4.67k
            dstp[i] = v1;
532
413k
    }
533
413k
}
534
535
static ALWAYS_INLINE void plane_expand_border( pixel *pix, int i_stride, int i_width, int i_height, int i_padh, int i_padv, int b_pad_top, int b_pad_bottom, int b_chroma )
536
9.39k
{
537
921k
#define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
538
200k
    for( int y = 0; y < i_height; y++ )
539
190k
    {
540
        /* left band */
541
190k
        pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y), i_padh>>b_chroma, SIZEOF_PIXEL<<b_chroma );
542
        /* right band */
543
190k
        pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1-b_chroma, y), i_padh>>b_chroma, SIZEOF_PIXEL<<b_chroma );
544
190k
    }
545
    /* upper band */
546
9.39k
    if( b_pad_top )
547
41.1k
        for( int y = 0; y < i_padv; y++ )
548
39.6k
            memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * SIZEOF_PIXEL );
549
    /* lower band */
550
9.39k
    if( b_pad_bottom )
551
41.1k
        for( int y = 0; y < i_padv; y++ )
552
39.6k
            memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * SIZEOF_PIXEL );
553
9.39k
#undef PPIXEL
554
9.39k
}
555
556
void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y )
557
1.75k
{
558
1.75k
    int pad_top = mb_y == 0;
559
1.75k
    int pad_bot = mb_y == h->mb.i_mb_height - (1 << SLICE_MBAFF);
560
1.75k
    int b_start = mb_y == h->i_threadslice_start;
561
1.75k
    int b_end   = mb_y == h->i_threadslice_end - (1 << SLICE_MBAFF);
562
1.75k
    if( mb_y & SLICE_MBAFF )
563
0
        return;
564
5.25k
    for( int i = 0; i < frame->i_plane; i++ )
565
3.50k
    {
566
3.50k
        int h_shift = i && CHROMA_H_SHIFT;
567
3.50k
        int v_shift = i && CHROMA_V_SHIFT;
568
3.50k
        int stride = frame->i_stride[i];
569
3.50k
        int width = 16*h->mb.i_mb_width;
570
3.50k
        int height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF : 16) >> v_shift;
571
3.50k
        int padh = PADH;
572
3.50k
        int padv = PADV >> v_shift;
573
        // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
574
3.50k
        if( b_end && !b_start )
575
320
            height += 4 >> (v_shift + SLICE_MBAFF);
576
3.50k
        pixel *pix;
577
3.50k
        int starty = 16*mb_y - 4*!b_start;
578
3.50k
        if( SLICE_MBAFF )
579
0
        {
580
            // border samples for each field are extended separately
581
0
            pix = frame->plane_fld[i] + (starty*stride >> v_shift);
582
0
            plane_expand_border( pix, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
583
0
            plane_expand_border( pix+stride, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
584
585
0
            height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) : 32) >> v_shift;
586
0
            if( b_end && !b_start )
587
0
                height += 4 >> v_shift;
588
0
            pix = frame->plane[i] + (starty*stride >> v_shift);
589
0
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
590
0
        }
591
3.50k
        else
592
3.50k
        {
593
3.50k
            pix = frame->plane[i] + (starty*stride >> v_shift);
594
3.50k
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
595
3.50k
        }
596
3.50k
    }
597
1.75k
}
x264_8_frame_expand_border
Line
Count
Source
557
1.75k
{
558
1.75k
    int pad_top = mb_y == 0;
559
1.75k
    int pad_bot = mb_y == h->mb.i_mb_height - (1 << SLICE_MBAFF);
560
1.75k
    int b_start = mb_y == h->i_threadslice_start;
561
1.75k
    int b_end   = mb_y == h->i_threadslice_end - (1 << SLICE_MBAFF);
562
1.75k
    if( mb_y & SLICE_MBAFF )
563
0
        return;
564
5.25k
    for( int i = 0; i < frame->i_plane; i++ )
565
3.50k
    {
566
3.50k
        int h_shift = i && CHROMA_H_SHIFT;
567
3.50k
        int v_shift = i && CHROMA_V_SHIFT;
568
3.50k
        int stride = frame->i_stride[i];
569
3.50k
        int width = 16*h->mb.i_mb_width;
570
3.50k
        int height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF : 16) >> v_shift;
571
3.50k
        int padh = PADH;
572
3.50k
        int padv = PADV >> v_shift;
573
        // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
574
3.50k
        if( b_end && !b_start )
575
320
            height += 4 >> (v_shift + SLICE_MBAFF);
576
3.50k
        pixel *pix;
577
3.50k
        int starty = 16*mb_y - 4*!b_start;
578
3.50k
        if( SLICE_MBAFF )
579
0
        {
580
            // border samples for each field are extended separately
581
0
            pix = frame->plane_fld[i] + (starty*stride >> v_shift);
582
0
            plane_expand_border( pix, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
583
0
            plane_expand_border( pix+stride, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
584
585
0
            height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) : 32) >> v_shift;
586
0
            if( b_end && !b_start )
587
0
                height += 4 >> v_shift;
588
0
            pix = frame->plane[i] + (starty*stride >> v_shift);
589
0
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
590
0
        }
591
3.50k
        else
592
3.50k
        {
593
3.50k
            pix = frame->plane[i] + (starty*stride >> v_shift);
594
3.50k
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
595
3.50k
        }
596
3.50k
    }
597
1.75k
}
Unexecuted instantiation: x264_10_frame_expand_border
598
599
void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
600
1.75k
{
601
    /* during filtering, 8 extra pixels were filtered on each edge,
602
     * but up to 3 of the horizontal ones may be wrong.
603
       we want to expand border from the last filtered pixel */
604
1.75k
    int b_start = !mb_y;
605
1.75k
    int width = 16*h->mb.i_mb_width + 8;
606
1.75k
    int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF) + 16 : 16;
607
1.75k
    int padh = PADH - 4;
608
1.75k
    int padv = PADV - 8;
609
3.50k
    for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
610
7.00k
        for( int i = 1; i < 4; i++ )
611
5.25k
        {
612
5.25k
            int stride = frame->i_stride[p];
613
            // buffer: 8 luma, to match the hpel filter
614
5.25k
            pixel *pix;
615
5.25k
            if( SLICE_MBAFF )
616
0
            {
617
0
                pix = frame->filtered_fld[p][i] + (16*mb_y - 16) * stride - 4;
618
0
                plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end, 0 );
619
0
                plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end, 0 );
620
0
            }
621
622
5.25k
            pix = frame->filtered[p][i] + (16*mb_y - 8) * stride - 4;
623
5.25k
            plane_expand_border( pix, stride, width, height << SLICE_MBAFF, padh, padv, b_start, b_end, 0 );
624
5.25k
        }
625
1.75k
}
x264_8_frame_expand_border_filtered
Line
Count
Source
600
1.75k
{
601
    /* during filtering, 8 extra pixels were filtered on each edge,
602
     * but up to 3 of the horizontal ones may be wrong.
603
       we want to expand border from the last filtered pixel */
604
1.75k
    int b_start = !mb_y;
605
1.75k
    int width = 16*h->mb.i_mb_width + 8;
606
1.75k
    int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF) + 16 : 16;
607
1.75k
    int padh = PADH - 4;
608
1.75k
    int padv = PADV - 8;
609
3.50k
    for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
610
7.00k
        for( int i = 1; i < 4; i++ )
611
5.25k
        {
612
5.25k
            int stride = frame->i_stride[p];
613
            // buffer: 8 luma, to match the hpel filter
614
5.25k
            pixel *pix;
615
5.25k
            if( SLICE_MBAFF )
616
0
            {
617
0
                pix = frame->filtered_fld[p][i] + (16*mb_y - 16) * stride - 4;
618
0
                plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end, 0 );
619
0
                plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end, 0 );
620
0
            }
621
622
5.25k
            pix = frame->filtered[p][i] + (16*mb_y - 8) * stride - 4;
623
5.25k
            plane_expand_border( pix, stride, width, height << SLICE_MBAFF, padh, padv, b_start, b_end, 0 );
624
5.25k
        }
625
1.75k
}
Unexecuted instantiation: x264_10_frame_expand_border_filtered
626
627
void x264_frame_expand_border_lowres( x264_frame_t *frame )
628
160
{
629
800
    for( int i = 0; i < 4; i++ )
630
640
        plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
631
160
}
x264_8_frame_expand_border_lowres
Line
Count
Source
628
160
{
629
800
    for( int i = 0; i < 4; i++ )
630
640
        plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
631
160
}
Unexecuted instantiation: x264_10_frame_expand_border_lowres
632
633
void x264_frame_expand_border_chroma( x264_t *h, x264_frame_t *frame, int plane )
634
0
{
635
0
    int v_shift = CHROMA_V_SHIFT;
636
0
    plane_expand_border( frame->plane[plane], frame->i_stride[plane], 16*h->mb.i_mb_width, 16*h->mb.i_mb_height>>v_shift,
637
0
                         PADH, PADV>>v_shift, 1, 1, CHROMA_H_SHIFT );
638
0
}
Unexecuted instantiation: x264_8_frame_expand_border_chroma
Unexecuted instantiation: x264_10_frame_expand_border_chroma
639
640
void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
641
140
{
642
420
    for( int i = 0; i < frame->i_plane; i++ )
643
280
    {
644
280
        int i_width = h->param.i_width;
645
280
        int h_shift = i && CHROMA_H_SHIFT;
646
280
        int v_shift = i && CHROMA_V_SHIFT;
647
280
        int i_height = h->param.i_height >> v_shift;
648
280
        int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
649
280
        int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
650
651
280
        if( i_padx )
652
248
        {
653
32.5k
            for( int y = 0; y < i_height; y++ )
654
32.3k
                pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
655
32.3k
                              &frame->plane[i][y*frame->i_stride[i] + i_width - 1-h_shift],
656
32.3k
                              i_padx>>h_shift, SIZEOF_PIXEL<<h_shift );
657
248
        }
658
280
        if( i_pady )
659
226
        {
660
1.52k
            for( int y = i_height; y < i_height + i_pady; y++ )
661
1.29k
                memcpy( &frame->plane[i][y*frame->i_stride[i]],
662
1.29k
                        &frame->plane[i][(i_height-(~y&PARAM_INTERLACED)-1)*frame->i_stride[i]],
663
1.29k
                        (i_width + i_padx) * SIZEOF_PIXEL );
664
226
        }
665
280
    }
666
140
}
x264_8_frame_expand_border_mod16
Line
Count
Source
641
140
{
642
420
    for( int i = 0; i < frame->i_plane; i++ )
643
280
    {
644
280
        int i_width = h->param.i_width;
645
280
        int h_shift = i && CHROMA_H_SHIFT;
646
280
        int v_shift = i && CHROMA_V_SHIFT;
647
280
        int i_height = h->param.i_height >> v_shift;
648
280
        int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
649
280
        int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
650
651
280
        if( i_padx )
652
248
        {
653
32.5k
            for( int y = 0; y < i_height; y++ )
654
32.3k
                pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
655
32.3k
                              &frame->plane[i][y*frame->i_stride[i] + i_width - 1-h_shift],
656
32.3k
                              i_padx>>h_shift, SIZEOF_PIXEL<<h_shift );
657
248
        }
658
280
        if( i_pady )
659
226
        {
660
1.52k
            for( int y = i_height; y < i_height + i_pady; y++ )
661
1.29k
                memcpy( &frame->plane[i][y*frame->i_stride[i]],
662
1.29k
                        &frame->plane[i][(i_height-(~y&PARAM_INTERLACED)-1)*frame->i_stride[i]],
663
1.29k
                        (i_width + i_padx) * SIZEOF_PIXEL );
664
226
        }
665
280
    }
666
140
}
Unexecuted instantiation: x264_10_frame_expand_border_mod16
667
668
void x264_expand_border_mbpair( x264_t *h, int mb_x, int mb_y )
669
0
{
670
0
    for( int i = 0; i < h->fenc->i_plane; i++ )
671
0
    {
672
0
        int v_shift = i && CHROMA_V_SHIFT;
673
0
        int stride = h->fenc->i_stride[i];
674
0
        int height = h->param.i_height >> v_shift;
675
0
        int pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
676
0
        pixel *fenc = h->fenc->plane[i] + 16*mb_x;
677
0
        for( int y = height; y < height + pady; y++ )
678
0
            memcpy( fenc + y*stride, fenc + (height-1)*stride, 16*SIZEOF_PIXEL );
679
0
    }
680
0
}
Unexecuted instantiation: x264_8_expand_border_mbpair
Unexecuted instantiation: x264_10_expand_border_mbpair
681
682
/* threading */
683
void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
684
1.75k
{
685
1.75k
    x264_pthread_mutex_lock( &frame->mutex );
686
1.75k
    frame->i_lines_completed = i_lines_completed;
687
1.75k
    x264_pthread_cond_broadcast( &frame->cv );
688
1.75k
    x264_pthread_mutex_unlock( &frame->mutex );
689
1.75k
}
x264_8_frame_cond_broadcast
Line
Count
Source
684
1.75k
{
685
1.75k
    x264_pthread_mutex_lock( &frame->mutex );
686
1.75k
    frame->i_lines_completed = i_lines_completed;
687
1.75k
    x264_pthread_cond_broadcast( &frame->cv );
688
1.75k
    x264_pthread_mutex_unlock( &frame->mutex );
689
1.75k
}
Unexecuted instantiation: x264_10_frame_cond_broadcast
690
691
int x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
692
0
{
693
0
    int completed;
694
0
    x264_pthread_mutex_lock( &frame->mutex );
695
0
    while( (completed = frame->i_lines_completed) < i_lines_completed && i_lines_completed >= 0 )
696
0
        x264_pthread_cond_wait( &frame->cv, &frame->mutex );
697
0
    x264_pthread_mutex_unlock( &frame->mutex );
698
0
    return completed;
699
0
}
Unexecuted instantiation: x264_8_frame_cond_wait
Unexecuted instantiation: x264_10_frame_cond_wait
700
701
void x264_threadslice_cond_broadcast( x264_t *h, int pass )
702
0
{
703
0
    x264_pthread_mutex_lock( &h->mutex );
704
0
    h->i_threadslice_pass = pass;
705
0
    if( pass > 0 )
706
0
        x264_pthread_cond_broadcast( &h->cv );
707
0
    x264_pthread_mutex_unlock( &h->mutex );
708
0
}
Unexecuted instantiation: x264_8_threadslice_cond_broadcast
Unexecuted instantiation: x264_10_threadslice_cond_broadcast
709
710
void x264_threadslice_cond_wait( x264_t *h, int pass )
711
0
{
712
0
    x264_pthread_mutex_lock( &h->mutex );
713
0
    while( h->i_threadslice_pass < pass )
714
0
        x264_pthread_cond_wait( &h->cv, &h->mutex );
715
0
    x264_pthread_mutex_unlock( &h->mutex );
716
0
}
Unexecuted instantiation: x264_8_threadslice_cond_wait
Unexecuted instantiation: x264_10_threadslice_cond_wait
717
718
int x264_frame_new_slice( x264_t *h, x264_frame_t *frame )
719
0
{
720
0
    if( h->param.i_slice_count_max )
721
0
    {
722
0
        int slice_count;
723
0
        if( h->param.b_sliced_threads )
724
0
            slice_count = x264_pthread_fetch_and_add( &frame->i_slice_count, 1, &frame->mutex );
725
0
        else
726
0
            slice_count = frame->i_slice_count++;
727
0
        if( slice_count >= h->param.i_slice_count_max )
728
0
            return -1;
729
0
    }
730
0
    return 0;
731
0
}
Unexecuted instantiation: x264_8_frame_new_slice
Unexecuted instantiation: x264_10_frame_new_slice
732
733
/* list operators */
734
735
void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
736
1.15k
{
737
1.15k
    int i = 0;
738
2.72k
    while( list[i] ) i++;
739
1.15k
    list[i] = frame;
740
1.15k
}
x264_8_frame_push
Line
Count
Source
736
1.15k
{
737
1.15k
    int i = 0;
738
2.72k
    while( list[i] ) i++;
739
1.15k
    list[i] = frame;
740
1.15k
}
Unexecuted instantiation: x264_10_frame_push
741
742
x264_frame_t *x264_frame_pop( x264_frame_t **list )
743
160
{
744
160
    x264_frame_t *frame;
745
160
    int i = 0;
746
160
    assert( list[0] );
747
290
    while( list[i+1] ) i++;
748
160
    frame = list[i];
749
160
    list[i] = NULL;
750
160
    return frame;
751
160
}
x264_8_frame_pop
Line
Count
Source
743
160
{
744
160
    x264_frame_t *frame;
745
160
    int i = 0;
746
160
    assert( list[0] );
747
290
    while( list[i+1] ) i++;
748
160
    frame = list[i];
749
    list[i] = NULL;
750
160
    return frame;
751
160
}
Unexecuted instantiation: x264_10_frame_pop
752
753
void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
754
0
{
755
0
    int i = 0;
756
0
    while( list[i] ) i++;
757
0
    while( i-- )
758
0
        list[i+1] = list[i];
759
0
    list[0] = frame;
760
0
}
Unexecuted instantiation: x264_8_frame_unshift
Unexecuted instantiation: x264_10_frame_unshift
761
762
x264_frame_t *x264_frame_shift( x264_frame_t **list )
763
960
{
764
960
    x264_frame_t *frame = list[0];
765
960
    int i;
766
1.92k
    for( i = 0; list[i]; i++ )
767
960
        list[i] = list[i+1];
768
960
    assert(frame);
769
960
    return frame;
770
960
}
x264_8_frame_shift
Line
Count
Source
763
960
{
764
960
    x264_frame_t *frame = list[0];
765
960
    int i;
766
1.92k
    for( i = 0; list[i]; i++ )
767
960
        list[i] = list[i+1];
768
960
    assert(frame);
769
960
    return frame;
770
960
}
Unexecuted instantiation: x264_10_frame_shift
771
772
void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
773
1.47k
{
774
1.47k
    assert( frame->i_reference_count > 0 );
775
1.47k
    frame->i_reference_count--;
776
1.47k
    if( frame->i_reference_count == 0 )
777
991
        x264_frame_push( h->frames.unused[frame->b_fdec], frame );
778
1.47k
}
x264_8_frame_push_unused
Line
Count
Source
773
1.47k
{
774
1.47k
    assert( frame->i_reference_count > 0 );
775
1.47k
    frame->i_reference_count--;
776
1.47k
    if( frame->i_reference_count == 0 )
777
991
        x264_frame_push( h->frames.unused[frame->b_fdec], frame );
778
1.47k
}
Unexecuted instantiation: x264_10_frame_push_unused
779
780
x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
781
1.15k
{
782
1.15k
    x264_frame_t *frame;
783
1.15k
    if( h->frames.unused[b_fdec][0] )
784
160
        frame = x264_frame_pop( h->frames.unused[b_fdec] );
785
991
    else
786
991
        frame = frame_new( h, b_fdec );
787
1.15k
    if( !frame )
788
0
        return NULL;
789
1.15k
    frame->b_last_minigop_bframe = 0;
790
1.15k
    frame->i_reference_count = 1;
791
1.15k
    frame->b_intra_calculated = 0;
792
1.15k
    frame->b_scenecut = 1;
793
1.15k
    frame->b_keyframe = 0;
794
1.15k
    frame->b_corrupt = 0;
795
1.15k
    frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
796
797
1.15k
    memset( frame->weight, 0, sizeof(frame->weight) );
798
1.15k
    memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
799
800
1.15k
    return frame;
801
1.15k
}
x264_8_frame_pop_unused
Line
Count
Source
781
1.15k
{
782
1.15k
    x264_frame_t *frame;
783
1.15k
    if( h->frames.unused[b_fdec][0] )
784
160
        frame = x264_frame_pop( h->frames.unused[b_fdec] );
785
991
    else
786
991
        frame = frame_new( h, b_fdec );
787
1.15k
    if( !frame )
788
0
        return NULL;
789
1.15k
    frame->b_last_minigop_bframe = 0;
790
1.15k
    frame->i_reference_count = 1;
791
1.15k
    frame->b_intra_calculated = 0;
792
1.15k
    frame->b_scenecut = 1;
793
1.15k
    frame->b_keyframe = 0;
794
1.15k
    frame->b_corrupt = 0;
795
1.15k
    frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
796
797
1.15k
    memset( frame->weight, 0, sizeof(frame->weight) );
798
1.15k
    memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
799
800
1.15k
    return frame;
801
1.15k
}
Unexecuted instantiation: x264_10_frame_pop_unused
802
803
void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
804
0
{
805
0
    assert( frame->i_reference_count > 0 );
806
0
    frame->i_reference_count--;
807
0
    if( frame->i_reference_count == 0 )
808
0
        x264_frame_push( h->frames.blank_unused, frame );
809
0
}
Unexecuted instantiation: x264_8_frame_push_blank_unused
Unexecuted instantiation: x264_10_frame_push_blank_unused
810
811
x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
812
0
{
813
0
    x264_frame_t *frame;
814
0
    if( h->frames.blank_unused[0] )
815
0
        frame = x264_frame_pop( h->frames.blank_unused );
816
0
    else
817
0
        frame = x264_malloc( sizeof(x264_frame_t) );
818
0
    if( !frame )
819
0
        return NULL;
820
0
    frame->b_duplicate = 1;
821
0
    frame->i_reference_count = 1;
822
0
    return frame;
823
0
}
Unexecuted instantiation: x264_8_frame_pop_blank_unused
Unexecuted instantiation: x264_10_frame_pop_blank_unused
824
825
void x264_weight_scale_plane( x264_t *h, pixel *dst, intptr_t i_dst_stride, pixel *src, intptr_t i_src_stride,
826
                              int i_width, int i_height, x264_weight_t *w )
827
0
{
828
    /* Weight horizontal strips of height 16. This was found to be the optimal height
829
     * in terms of the cache loads. */
830
0
    while( i_height > 0 )
831
0
    {
832
0
        int x;
833
0
        for( x = 0; x < i_width-8; x += 16 )
834
0
            w->weightfn[16>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
835
0
        if( x < i_width )
836
0
            w->weightfn[ 8>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
837
0
        i_height -= 16;
838
0
        dst += 16 * i_dst_stride;
839
0
        src += 16 * i_src_stride;
840
0
    }
841
0
}
Unexecuted instantiation: x264_8_weight_scale_plane
Unexecuted instantiation: x264_10_weight_scale_plane
842
843
void x264_frame_delete_list( x264_frame_t **list )
844
1.60k
{
845
1.60k
    int i = 0;
846
1.60k
    if( !list )
847
0
        return;
848
2.43k
    while( list[i] )
849
831
        x264_frame_delete( list[i++] );
850
1.60k
    x264_free( list );
851
1.60k
}
x264_8_frame_delete_list
Line
Count
Source
844
1.60k
{
845
1.60k
    int i = 0;
846
1.60k
    if( !list )
847
0
        return;
848
2.43k
    while( list[i] )
849
831
        x264_frame_delete( list[i++] );
850
1.60k
    x264_free( list );
851
1.60k
}
Unexecuted instantiation: x264_10_frame_delete_list
852
853
int x264_sync_frame_list_init( x264_sync_frame_list_t *slist, int max_size )
854
960
{
855
960
    if( max_size < 0 )
856
0
        return -1;
857
960
    slist->i_max_size = max_size;
858
960
    slist->i_size = 0;
859
960
    CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
860
960
    if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
861
960
        x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
862
960
        x264_pthread_cond_init( &slist->cv_empty, NULL ) )
863
0
        return -1;
864
960
    return 0;
865
0
fail:
866
0
    return -1;
867
960
}
x264_8_sync_frame_list_init
Line
Count
Source
854
960
{
855
960
    if( max_size < 0 )
856
0
        return -1;
857
960
    slist->i_max_size = max_size;
858
960
    slist->i_size = 0;
859
960
    CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
860
960
    if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
861
960
        x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
862
960
        x264_pthread_cond_init( &slist->cv_empty, NULL ) )
863
0
        return -1;
864
960
    return 0;
865
0
fail:
866
0
    return -1;
867
960
}
Unexecuted instantiation: x264_10_sync_frame_list_init
868
869
void x264_sync_frame_list_delete( x264_sync_frame_list_t *slist )
870
960
{
871
960
    x264_pthread_mutex_destroy( &slist->mutex );
872
960
    x264_pthread_cond_destroy( &slist->cv_fill );
873
960
    x264_pthread_cond_destroy( &slist->cv_empty );
874
960
    x264_frame_delete_list( slist->list );
875
960
}
x264_8_sync_frame_list_delete
Line
Count
Source
870
960
{
871
960
    x264_pthread_mutex_destroy( &slist->mutex );
872
960
    x264_pthread_cond_destroy( &slist->cv_fill );
873
960
    x264_pthread_cond_destroy( &slist->cv_empty );
874
960
    x264_frame_delete_list( slist->list );
875
960
}
Unexecuted instantiation: x264_10_sync_frame_list_delete
876
877
void x264_sync_frame_list_push( x264_sync_frame_list_t *slist, x264_frame_t *frame )
878
1.47k
{
879
1.47k
    x264_pthread_mutex_lock( &slist->mutex );
880
1.47k
    while( slist->i_size == slist->i_max_size )
881
0
        x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
882
1.47k
    slist->list[ slist->i_size++ ] = frame;
883
1.47k
    x264_pthread_mutex_unlock( &slist->mutex );
884
1.47k
    x264_pthread_cond_broadcast( &slist->cv_fill );
885
1.47k
}
x264_8_sync_frame_list_push
Line
Count
Source
878
1.47k
{
879
1.47k
    x264_pthread_mutex_lock( &slist->mutex );
880
1.47k
    while( slist->i_size == slist->i_max_size )
881
0
        x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
882
1.47k
    slist->list[ slist->i_size++ ] = frame;
883
1.47k
    x264_pthread_mutex_unlock( &slist->mutex );
884
1.47k
    x264_pthread_cond_broadcast( &slist->cv_fill );
885
1.47k
}
Unexecuted instantiation: x264_10_sync_frame_list_push
886
887
x264_frame_t *x264_sync_frame_list_pop( x264_sync_frame_list_t *slist )
888
160
{
889
160
    x264_frame_t *frame;
890
160
    x264_pthread_mutex_lock( &slist->mutex );
891
160
    while( !slist->i_size )
892
0
        x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
893
160
    frame = slist->list[ --slist->i_size ];
894
160
    slist->list[ slist->i_size ] = NULL;
895
160
    x264_pthread_cond_broadcast( &slist->cv_empty );
896
160
    x264_pthread_mutex_unlock( &slist->mutex );
897
160
    return frame;
898
160
}
x264_8_sync_frame_list_pop
Line
Count
Source
888
160
{
889
160
    x264_frame_t *frame;
890
160
    x264_pthread_mutex_lock( &slist->mutex );
891
160
    while( !slist->i_size )
892
0
        x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
893
160
    frame = slist->list[ --slist->i_size ];
894
160
    slist->list[ slist->i_size ] = NULL;
895
160
    x264_pthread_cond_broadcast( &slist->cv_empty );
896
160
    x264_pthread_mutex_unlock( &slist->mutex );
897
160
    return frame;
898
160
}
Unexecuted instantiation: x264_10_sync_frame_list_pop