Coverage Report

Created: 2026-09-02 06:42

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.96k
{
32
1.96k
    x = ALIGN( x, align );
33
1.96k
    if( !(x&(disalign-1)) )
34
0
        x += align;
35
1.96k
    return x;
36
1.96k
}
37
38
static int align_plane_size( int x, int disalign )
39
2.28k
{
40
2.28k
    if( !(x&(disalign-1)) )
41
1.03k
        x += X264_MAX( 128, NATIVE_ALIGN ) / SIZEOF_PIXEL;
42
2.28k
    return x;
43
2.28k
}
44
45
static int frame_internal_csp( int external_csp )
46
1.14k
{
47
1.14k
    int csp = external_csp & X264_CSP_MASK;
48
1.14k
    if( csp == X264_CSP_I400 )
49
0
        return X264_CSP_I400;
50
1.14k
    if( csp >= X264_CSP_I420 && csp < X264_CSP_I422 )
51
1.14k
        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
984
{
61
984
    x264_frame_t *frame;
62
984
    int i_csp = frame_internal_csp( h->param.i_csp );
63
984
    int i_mb_count = h->mb.i_mb_count;
64
984
    int i_stride, i_width, i_lines, luma_plane_count;
65
984
    int i_padv = PADV << PARAM_INTERLACED;
66
984
    int align = NATIVE_ALIGN / SIZEOF_PIXEL;
67
984
#if ARCH_X86 || ARCH_X86_64
68
984
    if( h->param.cpu&X264_CPU_CACHELINE_64 || h->param.cpu&X264_CPU_AVX512 )
69
0
        align = 64 / SIZEOF_PIXEL;
70
984
    else if( h->param.cpu&X264_CPU_CACHELINE_32 || h->param.cpu&X264_CPU_AVX )
71
0
        align = 32 / SIZEOF_PIXEL;
72
984
    else
73
984
        align = 16 / SIZEOF_PIXEL;
74
984
#endif
75
#if ARCH_PPC
76
    int disalign = (1<<9) / SIZEOF_PIXEL;
77
#else
78
984
    int disalign = (1<<10) / SIZEOF_PIXEL;
79
984
#endif
80
81
984
    CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
82
984
    PREALLOC_INIT
83
84
    /* allocate frame data (+64 for extra data for me) */
85
984
    i_width  = h->mb.i_mb_width*16;
86
984
    i_lines  = h->mb.i_mb_height*16;
87
984
    i_stride = align_stride( i_width + PADH2, align, disalign );
88
89
984
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
90
984
    {
91
984
        luma_plane_count = 1;
92
984
        frame->i_plane = 2;
93
2.95k
        for( int i = 0; i < 2; i++ )
94
1.96k
        {
95
1.96k
            frame->i_width[i] = i_width >> i;
96
1.96k
            frame->i_lines[i] = i_lines >> (i && i_csp == X264_CSP_NV12);
97
1.96k
            frame->i_stride[i] = i_stride;
98
1.96k
        }
99
984
    }
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
984
    frame->i_csp = i_csp;
123
984
    frame->i_width_lowres = frame->i_width[0]/2;
124
984
    frame->i_lines_lowres = frame->i_lines[0]/2;
125
984
    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.6k
        for( int j = 0; j < h->param.i_bframe + 2; j++ )
129
18.5k
            PREALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
130
131
984
    frame->i_poc = -1;
132
984
    frame->i_type = X264_TYPE_AUTO;
133
984
    frame->i_qpplus1 = X264_QP_AUTO;
134
984
    frame->i_pts = -1;
135
984
    frame->i_frame = -1;
136
984
    frame->i_frame_num = -1;
137
984
    frame->i_lines_completed = -1;
138
984
    frame->b_fdec = b_fdec;
139
984
    frame->i_pic_struct = PIC_STRUCT_AUTO;
140
984
    frame->i_field_cnt = -1;
141
984
    frame->i_duration =
142
984
    frame->i_cpb_duration =
143
984
    frame->i_dpb_output_delay =
144
984
    frame->i_cpb_delay = 0;
145
984
    frame->i_coded_fields_lookahead =
146
984
    frame->i_cpb_delay_lookahead = -1;
147
148
984
    frame->orig = frame;
149
150
984
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
151
984
    {
152
984
        int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
153
984
        int chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + 2*chroma_padv));
154
984
        PREALLOC( frame->buffer[1], chroma_plane_size * SIZEOF_PIXEL );
155
984
        if( PARAM_INTERLACED )
156
0
            PREALLOC( frame->buffer_fld[1], chroma_plane_size * SIZEOF_PIXEL );
157
984
    }
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.96k
    for( int p = 0; p < luma_plane_count; p++ )
163
984
    {
164
984
        int64_t luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
165
984
        if( h->param.analyse.i_subpel_refine && b_fdec )
166
825
            luma_plane_size *= 4;
167
168
        /* FIXME: Don't allocate both buffers in non-adaptive MBAFF. */
169
984
        PREALLOC( frame->buffer[p], luma_plane_size * SIZEOF_PIXEL );
170
984
        if( PARAM_INTERLACED )
171
0
            PREALLOC( frame->buffer_fld[p], luma_plane_size * SIZEOF_PIXEL );
172
984
    }
173
174
984
    frame->b_duplicate = 0;
175
176
984
    if( b_fdec ) /* fdec frame */
177
825
    {
178
825
        PREALLOC( frame->mb_type, i_mb_count * sizeof(int8_t) );
179
825
        PREALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t) );
180
825
        PREALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
181
825
        PREALLOC( frame->mv16x16, 2*(i_mb_count+1) * sizeof(int16_t) );
182
825
        PREALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
183
825
        if( h->param.i_bframe )
184
583
        {
185
583
            PREALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
186
583
            PREALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
187
583
        }
188
242
        else
189
242
        {
190
242
            frame->mv[1]  = NULL;
191
242
            frame->ref[1] = NULL;
192
242
        }
193
825
        PREALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
194
825
        PREALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
195
825
        PREALLOC( frame->f_row_qscale, i_lines/16 * sizeof(float) );
196
825
        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
825
        if( PARAM_INTERLACED )
199
0
            PREALLOC( frame->field, i_mb_count * sizeof(uint8_t) );
200
825
        if( h->param.analyse.b_mb_info )
201
0
            PREALLOC( frame->effective_qp, i_mb_count * sizeof(uint8_t) );
202
825
    }
203
159
    else /* fenc frame */
204
159
    {
205
159
        if( h->frames.b_have_lowres )
206
159
        {
207
159
            int64_t luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
208
209
159
            PREALLOC( frame->buffer_lowres, 4 * luma_plane_size * SIZEOF_PIXEL );
210
211
431
            for( int j = 0; j <= !!h->param.i_bframe; j++ )
212
1.22k
                for( int i = 0; i <= h->param.i_bframe; i++ )
213
950
                {
214
950
                    PREALLOC( frame->lowres_mvs[j][i], 2*i_mb_count*sizeof(int16_t) );
215
950
                    PREALLOC( frame->lowres_mv_costs[j][i], i_mb_count*sizeof(int) );
216
950
                }
217
159
            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.66k
                for( int i = 0; i <= h->param.i_bframe+1; i++ )
220
3.00k
                    PREALLOC( frame->lowres_costs[j][i], i_mb_count * sizeof(uint16_t) );
221
159
        }
222
159
        if( h->param.rc.i_aq_mode )
223
113
        {
224
113
            PREALLOC( frame->f_qp_offset, i_mb_count * sizeof(float) );
225
113
            PREALLOC( frame->f_qp_offset_aq, i_mb_count * sizeof(float) );
226
113
            if( h->frames.b_have_lowres )
227
113
                PREALLOC( frame->i_inv_qscale_factor, i_mb_count * sizeof(uint16_t) );
228
113
        }
229
230
        /* mbtree asm can overread the input buffers, make sure we don't read outside of allocated memory. */
231
159
        if( h->frames.b_have_lowres )
232
159
            prealloc_size += NATIVE_ALIGN;
233
159
    }
234
235
984
    PREALLOC_END( frame->base );
236
237
984
    if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
238
984
    {
239
984
        int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
240
984
        frame->plane[1] = frame->buffer[1] + frame->i_stride[1] * chroma_padv + PADH_ALIGN;
241
984
        if( PARAM_INTERLACED )
242
0
            frame->plane_fld[1] = frame->buffer_fld[1] + frame->i_stride[1] * chroma_padv + PADH_ALIGN;
243
984
    }
244
245
1.96k
    for( int p = 0; p < luma_plane_count; p++ )
246
984
    {
247
984
        int64_t luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
248
984
        if( h->param.analyse.i_subpel_refine && b_fdec )
249
825
        {
250
4.12k
            for( int i = 0; i < 4; i++ )
251
3.30k
            {
252
3.30k
                frame->filtered[p][i] = frame->buffer[p] + i*luma_plane_size + frame->i_stride[p] * i_padv + PADH_ALIGN;
253
3.30k
                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.30k
            }
256
825
            frame->plane[p] = frame->filtered[p][0];
257
825
            frame->plane_fld[p] = frame->filtered_fld[p][0];
258
825
        }
259
159
        else
260
159
        {
261
159
            frame->filtered[p][0] = frame->plane[p] = frame->buffer[p] + frame->i_stride[p] * i_padv + PADH_ALIGN;
262
159
            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
159
        }
265
984
    }
266
267
984
    if( b_fdec )
268
825
    {
269
825
        M32( frame->mv16x16[0] ) = 0;
270
825
        frame->mv16x16++;
271
272
825
        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
825
    }
275
159
    else
276
159
    {
277
159
        if( h->frames.b_have_lowres )
278
159
        {
279
159
            int64_t luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
280
795
            for( int i = 0; i < 4; i++ )
281
636
                frame->lowres[i] = frame->buffer_lowres + frame->i_stride_lowres * PADV + PADH_ALIGN + i * luma_plane_size;
282
283
431
            for( int j = 0; j <= !!h->param.i_bframe; j++ )
284
1.22k
                for( int i = 0; i <= h->param.i_bframe; i++ )
285
950
                    memset( frame->lowres_mvs[j][i], 0, 2*i_mb_count*sizeof(int16_t) );
286
287
159
            frame->i_intra_cost = frame->lowres_costs[0][0];
288
159
            memset( frame->i_intra_cost, -1, i_mb_count * sizeof(uint16_t) );
289
290
159
            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
113
                memset( frame->i_inv_qscale_factor, 0, i_mb_count * sizeof(uint16_t) );
293
159
        }
294
159
    }
295
296
984
    if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
297
0
        goto fail;
298
984
    if( x264_pthread_cond_init( &frame->cv, NULL ) )
299
0
        goto fail;
300
301
984
#if HAVE_OPENCL
302
984
    frame->opencl.ocl = h->opencl.ocl;
303
984
#endif
304
305
984
    return frame;
306
307
0
fail:
308
0
    x264_free( frame );
309
0
    return NULL;
310
984
}
311
312
void x264_frame_delete( x264_frame_t *frame )
313
984
{
314
    /* Duplicate frames are blank copies of real frames (including pointers),
315
     * so freeing those pointers would cause a double free later. */
316
984
    if( !frame->b_duplicate )
317
984
    {
318
984
        x264_free( frame->base );
319
320
984
        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
984
        if( frame->mb_info_free )
326
0
            frame->mb_info_free( frame->mb_info );
327
984
        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
984
        x264_pthread_mutex_destroy( &frame->mutex );
334
984
        x264_pthread_cond_destroy( &frame->cv );
335
#if HAVE_OPENCL
336
984
        x264_opencl_frame_delete( frame );
337
#endif
338
984
    }
339
984
    x264_free( frame );
340
984
}
x264_8_frame_delete
Line
Count
Source
313
984
{
314
    /* Duplicate frames are blank copies of real frames (including pointers),
315
     * so freeing those pointers would cause a double free later. */
316
984
    if( !frame->b_duplicate )
317
984
    {
318
984
        x264_free( frame->base );
319
320
984
        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
984
        if( frame->mb_info_free )
326
0
            frame->mb_info_free( frame->mb_info );
327
984
        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
984
        x264_pthread_mutex_destroy( &frame->mutex );
334
984
        x264_pthread_cond_destroy( &frame->cv );
335
984
#if HAVE_OPENCL
336
984
        x264_opencl_frame_delete( frame );
337
984
#endif
338
984
    }
339
984
    x264_free( frame );
340
984
}
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
477
{
344
477
    int width = h->param.i_width >> xshift;
345
477
    int height = h->param.i_height >> yshift;
346
477
    *pix = src->img.plane[plane];
347
477
    *stride = src->img.i_stride[plane];
348
477
    if( src->img.i_csp & X264_CSP_VFLIP )
349
0
    {
350
0
        *pix += (height-1) * *stride;
351
0
        *stride = -*stride;
352
0
    }
353
477
    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
477
    return 0;
359
477
}
360
361
636
#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
159
{
365
159
    int i_csp = src->img.i_csp & X264_CSP_MASK;
366
159
    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
159
    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
159
#endif
385
386
159
    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
159
    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
159
    else
398
159
        dst->i_forced_type = src->i_type;
399
400
159
    dst->i_type     = dst->i_forced_type;
401
159
    dst->i_qpplus1  = src->i_qpplus1;
402
159
    dst->i_pts      = dst->i_reordered_pts = src->i_pts;
403
159
    dst->param      = src->param;
404
159
    dst->i_pic_struct = src->i_pic_struct;
405
159
    dst->extra_sei  = src->extra_sei;
406
159
    dst->opaque     = src->opaque;
407
159
    dst->mb_info    = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
408
159
    dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;
409
410
159
    uint8_t *pix[3];
411
159
    int stride[3];
412
159
    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
159
    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
159
    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
159
    else
443
159
    {
444
159
        int v_shift = CHROMA_V_SHIFT;
445
159
        get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
446
159
        h->mc.plane_copy( dst->plane[0], dst->i_stride[0], (pixel*)pix[0],
447
159
                          stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
448
159
        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
159
        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
159
        else if( i_csp == X264_CSP_I420 || i_csp == X264_CSP_I422 || i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16 )
461
159
        {
462
159
            int uv_swap = i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16;
463
159
            get_plane_ptr( h, src, &pix[1], &stride[1], uv_swap ? 2 : 1, 1, v_shift );
464
159
            get_plane_ptr( h, src, &pix[2], &stride[2], uv_swap ? 1 : 2, 1, v_shift );
465
159
            h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
466
159
                                         (pixel*)pix[1], stride[1]/SIZEOF_PIXEL,
467
159
                                         (pixel*)pix[2], stride[2]/SIZEOF_PIXEL,
468
159
                                         h->param.i_width>>1, h->param.i_height>>v_shift );
469
159
        }
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
159
    }
480
159
    return 0;
481
159
}
x264_8_frame_copy_picture
Line
Count
Source
364
159
{
365
159
    int i_csp = src->img.i_csp & X264_CSP_MASK;
366
159
    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
159
    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
159
#endif
385
386
159
    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
159
    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
159
    else
398
159
        dst->i_forced_type = src->i_type;
399
400
159
    dst->i_type     = dst->i_forced_type;
401
159
    dst->i_qpplus1  = src->i_qpplus1;
402
159
    dst->i_pts      = dst->i_reordered_pts = src->i_pts;
403
159
    dst->param      = src->param;
404
159
    dst->i_pic_struct = src->i_pic_struct;
405
159
    dst->extra_sei  = src->extra_sei;
406
159
    dst->opaque     = src->opaque;
407
159
    dst->mb_info    = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
408
159
    dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;
409
410
159
    uint8_t *pix[3];
411
159
    int stride[3];
412
159
    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
159
    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
159
    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
159
    else
443
159
    {
444
159
        int v_shift = CHROMA_V_SHIFT;
445
159
        get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
446
159
        h->mc.plane_copy( dst->plane[0], dst->i_stride[0], (pixel*)pix[0],
447
159
                          stride[0]/SIZEOF_PIXEL, h->param.i_width, h->param.i_height );
448
159
        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
159
        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
159
        else if( i_csp == X264_CSP_I420 || i_csp == X264_CSP_I422 || i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16 )
461
159
        {
462
159
            int uv_swap = i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16;
463
159
            get_plane_ptr( h, src, &pix[1], &stride[1], uv_swap ? 2 : 1, 1, v_shift );
464
159
            get_plane_ptr( h, src, &pix[2], &stride[2], uv_swap ? 1 : 2, 1, v_shift );
465
159
            h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
466
159
                                         (pixel*)pix[1], stride[1]/SIZEOF_PIXEL,
467
159
                                         (pixel*)pix[2], stride[2]/SIZEOF_PIXEL,
468
159
                                         h->param.i_width>>1, h->param.i_height>>v_shift );
469
159
        }
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
159
    }
480
159
    return 0;
481
159
}
Unexecuted instantiation: x264_10_frame_copy_picture
482
483
static ALWAYS_INLINE void pixel_memset( pixel *dst, pixel *src, int len, int size )
484
409k
{
485
409k
    uint8_t *dstp = (uint8_t*)dst;
486
409k
    uint32_t v1 = *src;
487
409k
    uint32_t v2 = size == 1 ? v1 + (v1 <<  8) : M16( src );
488
409k
    uint32_t v4 = size <= 2 ? v2 + (v2 << 16) : M32( src );
489
409k
    int i = 0;
490
409k
    len *= size;
491
492
    /* Align the input pointer if it isn't already */
493
409k
    if( (intptr_t)dstp & (WORD_SIZE - 1) )
494
116k
    {
495
116k
        if( size <= 2 && ((intptr_t)dstp & 3) )
496
19.4k
        {
497
19.4k
            if( size == 1 && ((intptr_t)dstp & 1) )
498
0
                dstp[i++] = v1;
499
19.4k
            if( (intptr_t)dstp & 2 )
500
19.4k
            {
501
19.4k
                M16( dstp+i ) = v2;
502
19.4k
                i += 2;
503
19.4k
            }
504
19.4k
        }
505
116k
        if( WORD_SIZE == 8 && (intptr_t)dstp & 4 )
506
105k
        {
507
105k
            M32( dstp+i ) = v4;
508
105k
            i += 4;
509
105k
        }
510
116k
    }
511
512
    /* Main copy loop */
513
409k
    if( WORD_SIZE == 8 )
514
409k
    {
515
409k
        uint64_t v8 = v4 + ((uint64_t)v4<<32);
516
1.75M
        for( ; i < len - 7; i+=8 )
517
1.34M
            M64( dstp+i ) = v8;
518
409k
    }
519
512k
    for( ; i < len - 3; i+=4 )
520
103k
        M32( dstp+i ) = v4;
521
522
    /* Finish up the last few bytes */
523
409k
    if( size <= 2 )
524
409k
    {
525
409k
        if( i < len - 1 )
526
0
        {
527
0
            M16( dstp+i ) = v2;
528
0
            i += 2;
529
0
        }
530
409k
        if( size == 1 && i != len )
531
4.79k
            dstp[i] = v1;
532
409k
    }
533
409k
}
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.31k
{
537
913k
#define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
538
198k
    for( int y = 0; y < i_height; y++ )
539
189k
    {
540
        /* left band */
541
189k
        pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y), i_padh>>b_chroma, SIZEOF_PIXEL<<b_chroma );
542
        /* right band */
543
189k
        pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1-b_chroma, y), i_padh>>b_chroma, SIZEOF_PIXEL<<b_chroma );
544
189k
    }
545
    /* upper band */
546
9.31k
    if( b_pad_top )
547
40.8k
        for( int y = 0; y < i_padv; y++ )
548
39.4k
            memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * SIZEOF_PIXEL );
549
    /* lower band */
550
9.31k
    if( b_pad_bottom )
551
40.8k
        for( int y = 0; y < i_padv; y++ )
552
39.4k
            memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * SIZEOF_PIXEL );
553
9.31k
#undef PPIXEL
554
9.31k
}
555
556
void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y )
557
1.73k
{
558
1.73k
    int pad_top = mb_y == 0;
559
1.73k
    int pad_bot = mb_y == h->mb.i_mb_height - (1 << SLICE_MBAFF);
560
1.73k
    int b_start = mb_y == h->i_threadslice_start;
561
1.73k
    int b_end   = mb_y == h->i_threadslice_end - (1 << SLICE_MBAFF);
562
1.73k
    if( mb_y & SLICE_MBAFF )
563
0
        return;
564
5.20k
    for( int i = 0; i < frame->i_plane; i++ )
565
3.47k
    {
566
3.47k
        int h_shift = i && CHROMA_H_SHIFT;
567
3.47k
        int v_shift = i && CHROMA_V_SHIFT;
568
3.47k
        int stride = frame->i_stride[i];
569
3.47k
        int width = 16*h->mb.i_mb_width;
570
3.47k
        int height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF : 16) >> v_shift;
571
3.47k
        int padh = PADH;
572
3.47k
        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.47k
        if( b_end && !b_start )
575
318
            height += 4 >> (v_shift + SLICE_MBAFF);
576
3.47k
        pixel *pix;
577
3.47k
        int starty = 16*mb_y - 4*!b_start;
578
3.47k
        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.47k
        else
592
3.47k
        {
593
3.47k
            pix = frame->plane[i] + (starty*stride >> v_shift);
594
3.47k
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
595
3.47k
        }
596
3.47k
    }
597
1.73k
}
x264_8_frame_expand_border
Line
Count
Source
557
1.73k
{
558
1.73k
    int pad_top = mb_y == 0;
559
1.73k
    int pad_bot = mb_y == h->mb.i_mb_height - (1 << SLICE_MBAFF);
560
1.73k
    int b_start = mb_y == h->i_threadslice_start;
561
1.73k
    int b_end   = mb_y == h->i_threadslice_end - (1 << SLICE_MBAFF);
562
1.73k
    if( mb_y & SLICE_MBAFF )
563
0
        return;
564
5.20k
    for( int i = 0; i < frame->i_plane; i++ )
565
3.47k
    {
566
3.47k
        int h_shift = i && CHROMA_H_SHIFT;
567
3.47k
        int v_shift = i && CHROMA_V_SHIFT;
568
3.47k
        int stride = frame->i_stride[i];
569
3.47k
        int width = 16*h->mb.i_mb_width;
570
3.47k
        int height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF : 16) >> v_shift;
571
3.47k
        int padh = PADH;
572
3.47k
        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.47k
        if( b_end && !b_start )
575
318
            height += 4 >> (v_shift + SLICE_MBAFF);
576
3.47k
        pixel *pix;
577
3.47k
        int starty = 16*mb_y - 4*!b_start;
578
3.47k
        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.47k
        else
592
3.47k
        {
593
3.47k
            pix = frame->plane[i] + (starty*stride >> v_shift);
594
3.47k
            plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
595
3.47k
        }
596
3.47k
    }
597
1.73k
}
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.73k
{
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.73k
    int b_start = !mb_y;
605
1.73k
    int width = 16*h->mb.i_mb_width + 8;
606
1.73k
    int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF) + 16 : 16;
607
1.73k
    int padh = PADH - 4;
608
1.73k
    int padv = PADV - 8;
609
3.47k
    for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
610
6.94k
        for( int i = 1; i < 4; i++ )
611
5.20k
        {
612
5.20k
            int stride = frame->i_stride[p];
613
            // buffer: 8 luma, to match the hpel filter
614
5.20k
            pixel *pix;
615
5.20k
            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.20k
            pix = frame->filtered[p][i] + (16*mb_y - 8) * stride - 4;
623
5.20k
            plane_expand_border( pix, stride, width, height << SLICE_MBAFF, padh, padv, b_start, b_end, 0 );
624
5.20k
        }
625
1.73k
}
x264_8_frame_expand_border_filtered
Line
Count
Source
600
1.73k
{
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.73k
    int b_start = !mb_y;
605
1.73k
    int width = 16*h->mb.i_mb_width + 8;
606
1.73k
    int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF) + 16 : 16;
607
1.73k
    int padh = PADH - 4;
608
1.73k
    int padv = PADV - 8;
609
3.47k
    for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
610
6.94k
        for( int i = 1; i < 4; i++ )
611
5.20k
        {
612
5.20k
            int stride = frame->i_stride[p];
613
            // buffer: 8 luma, to match the hpel filter
614
5.20k
            pixel *pix;
615
5.20k
            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.20k
            pix = frame->filtered[p][i] + (16*mb_y - 8) * stride - 4;
623
5.20k
            plane_expand_border( pix, stride, width, height << SLICE_MBAFF, padh, padv, b_start, b_end, 0 );
624
5.20k
        }
625
1.73k
}
Unexecuted instantiation: x264_10_frame_expand_border_filtered
626
627
void x264_frame_expand_border_lowres( x264_frame_t *frame )
628
159
{
629
795
    for( int i = 0; i < 4; i++ )
630
636
        plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
631
159
}
x264_8_frame_expand_border_lowres
Line
Count
Source
628
159
{
629
795
    for( int i = 0; i < 4; i++ )
630
636
        plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
631
159
}
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
141
{
642
423
    for( int i = 0; i < frame->i_plane; i++ )
643
282
    {
644
282
        int i_width = h->param.i_width;
645
282
        int h_shift = i && CHROMA_H_SHIFT;
646
282
        int v_shift = i && CHROMA_V_SHIFT;
647
282
        int i_height = h->param.i_height >> v_shift;
648
282
        int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
649
282
        int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
650
651
282
        if( i_padx )
652
234
        {
653
31.8k
            for( int y = 0; y < i_height; y++ )
654
31.6k
                pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
655
31.6k
                              &frame->plane[i][y*frame->i_stride[i] + i_width - 1-h_shift],
656
31.6k
                              i_padx>>h_shift, SIZEOF_PIXEL<<h_shift );
657
234
        }
658
282
        if( i_pady )
659
228
        {
660
1.59k
            for( int y = i_height; y < i_height + i_pady; y++ )
661
1.36k
                memcpy( &frame->plane[i][y*frame->i_stride[i]],
662
1.36k
                        &frame->plane[i][(i_height-(~y&PARAM_INTERLACED)-1)*frame->i_stride[i]],
663
1.36k
                        (i_width + i_padx) * SIZEOF_PIXEL );
664
228
        }
665
282
    }
666
141
}
x264_8_frame_expand_border_mod16
Line
Count
Source
641
141
{
642
423
    for( int i = 0; i < frame->i_plane; i++ )
643
282
    {
644
282
        int i_width = h->param.i_width;
645
282
        int h_shift = i && CHROMA_H_SHIFT;
646
282
        int v_shift = i && CHROMA_V_SHIFT;
647
282
        int i_height = h->param.i_height >> v_shift;
648
282
        int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
649
282
        int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
650
651
282
        if( i_padx )
652
234
        {
653
31.8k
            for( int y = 0; y < i_height; y++ )
654
31.6k
                pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
655
31.6k
                              &frame->plane[i][y*frame->i_stride[i] + i_width - 1-h_shift],
656
31.6k
                              i_padx>>h_shift, SIZEOF_PIXEL<<h_shift );
657
234
        }
658
282
        if( i_pady )
659
228
        {
660
1.59k
            for( int y = i_height; y < i_height + i_pady; y++ )
661
1.36k
                memcpy( &frame->plane[i][y*frame->i_stride[i]],
662
1.36k
                        &frame->plane[i][(i_height-(~y&PARAM_INTERLACED)-1)*frame->i_stride[i]],
663
1.36k
                        (i_width + i_padx) * SIZEOF_PIXEL );
664
228
        }
665
282
    }
666
141
}
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.73k
{
685
1.73k
    x264_pthread_mutex_lock( &frame->mutex );
686
1.73k
    frame->i_lines_completed = i_lines_completed;
687
1.73k
    x264_pthread_cond_broadcast( &frame->cv );
688
1.73k
    x264_pthread_mutex_unlock( &frame->mutex );
689
1.73k
}
x264_8_frame_cond_broadcast
Line
Count
Source
684
1.73k
{
685
1.73k
    x264_pthread_mutex_lock( &frame->mutex );
686
1.73k
    frame->i_lines_completed = i_lines_completed;
687
1.73k
    x264_pthread_cond_broadcast( &frame->cv );
688
1.73k
    x264_pthread_mutex_unlock( &frame->mutex );
689
1.73k
}
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.14k
{
737
1.14k
    int i = 0;
738
2.72k
    while( list[i] ) i++;
739
1.14k
    list[i] = frame;
740
1.14k
}
x264_8_frame_push
Line
Count
Source
736
1.14k
{
737
1.14k
    int i = 0;
738
2.72k
    while( list[i] ) i++;
739
1.14k
    list[i] = frame;
740
1.14k
}
Unexecuted instantiation: x264_10_frame_push
741
742
x264_frame_t *x264_frame_pop( x264_frame_t **list )
743
159
{
744
159
    x264_frame_t *frame;
745
159
    int i = 0;
746
159
    assert( list[0] );
747
288
    while( list[i+1] ) i++;
748
159
    frame = list[i];
749
159
    list[i] = NULL;
750
159
    return frame;
751
159
}
x264_8_frame_pop
Line
Count
Source
743
159
{
744
159
    x264_frame_t *frame;
745
159
    int i = 0;
746
159
    assert( list[0] );
747
288
    while( list[i+1] ) i++;
748
159
    frame = list[i];
749
    list[i] = NULL;
750
159
    return frame;
751
159
}
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
954
{
764
954
    x264_frame_t *frame = list[0];
765
954
    int i;
766
1.90k
    for( i = 0; list[i]; i++ )
767
954
        list[i] = list[i+1];
768
954
    assert(frame);
769
954
    return frame;
770
954
}
x264_8_frame_shift
Line
Count
Source
763
954
{
764
954
    x264_frame_t *frame = list[0];
765
954
    int i;
766
1.90k
    for( i = 0; list[i]; i++ )
767
954
        list[i] = list[i+1];
768
954
    assert(frame);
769
954
    return frame;
770
954
}
Unexecuted instantiation: x264_10_frame_shift
771
772
void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
773
1.46k
{
774
1.46k
    assert( frame->i_reference_count > 0 );
775
1.46k
    frame->i_reference_count--;
776
1.46k
    if( frame->i_reference_count == 0 )
777
984
        x264_frame_push( h->frames.unused[frame->b_fdec], frame );
778
1.46k
}
x264_8_frame_push_unused
Line
Count
Source
773
1.46k
{
774
1.46k
    assert( frame->i_reference_count > 0 );
775
1.46k
    frame->i_reference_count--;
776
1.46k
    if( frame->i_reference_count == 0 )
777
984
        x264_frame_push( h->frames.unused[frame->b_fdec], frame );
778
1.46k
}
Unexecuted instantiation: x264_10_frame_push_unused
779
780
x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
781
1.14k
{
782
1.14k
    x264_frame_t *frame;
783
1.14k
    if( h->frames.unused[b_fdec][0] )
784
159
        frame = x264_frame_pop( h->frames.unused[b_fdec] );
785
984
    else
786
984
        frame = frame_new( h, b_fdec );
787
1.14k
    if( !frame )
788
0
        return NULL;
789
1.14k
    frame->b_last_minigop_bframe = 0;
790
1.14k
    frame->i_reference_count = 1;
791
1.14k
    frame->b_intra_calculated = 0;
792
1.14k
    frame->b_scenecut = 1;
793
1.14k
    frame->b_keyframe = 0;
794
1.14k
    frame->b_corrupt = 0;
795
1.14k
    frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
796
797
1.14k
    memset( frame->weight, 0, sizeof(frame->weight) );
798
1.14k
    memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
799
800
1.14k
    return frame;
801
1.14k
}
x264_8_frame_pop_unused
Line
Count
Source
781
1.14k
{
782
1.14k
    x264_frame_t *frame;
783
1.14k
    if( h->frames.unused[b_fdec][0] )
784
159
        frame = x264_frame_pop( h->frames.unused[b_fdec] );
785
984
    else
786
984
        frame = frame_new( h, b_fdec );
787
1.14k
    if( !frame )
788
0
        return NULL;
789
1.14k
    frame->b_last_minigop_bframe = 0;
790
1.14k
    frame->i_reference_count = 1;
791
1.14k
    frame->b_intra_calculated = 0;
792
1.14k
    frame->b_scenecut = 1;
793
1.14k
    frame->b_keyframe = 0;
794
1.14k
    frame->b_corrupt = 0;
795
1.14k
    frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
796
797
1.14k
    memset( frame->weight, 0, sizeof(frame->weight) );
798
1.14k
    memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
799
800
1.14k
    return frame;
801
1.14k
}
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.59k
{
845
1.59k
    int i = 0;
846
1.59k
    if( !list )
847
0
        return;
848
2.41k
    while( list[i] )
849
825
        x264_frame_delete( list[i++] );
850
1.59k
    x264_free( list );
851
1.59k
}
x264_8_frame_delete_list
Line
Count
Source
844
1.59k
{
845
1.59k
    int i = 0;
846
1.59k
    if( !list )
847
0
        return;
848
2.41k
    while( list[i] )
849
825
        x264_frame_delete( list[i++] );
850
1.59k
    x264_free( list );
851
1.59k
}
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
954
{
855
954
    if( max_size < 0 )
856
0
        return -1;
857
954
    slist->i_max_size = max_size;
858
954
    slist->i_size = 0;
859
954
    CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
860
954
    if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
861
954
        x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
862
954
        x264_pthread_cond_init( &slist->cv_empty, NULL ) )
863
0
        return -1;
864
954
    return 0;
865
0
fail:
866
0
    return -1;
867
954
}
x264_8_sync_frame_list_init
Line
Count
Source
854
954
{
855
954
    if( max_size < 0 )
856
0
        return -1;
857
954
    slist->i_max_size = max_size;
858
954
    slist->i_size = 0;
859
954
    CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
860
954
    if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
861
954
        x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
862
954
        x264_pthread_cond_init( &slist->cv_empty, NULL ) )
863
0
        return -1;
864
954
    return 0;
865
0
fail:
866
0
    return -1;
867
954
}
Unexecuted instantiation: x264_10_sync_frame_list_init
868
869
void x264_sync_frame_list_delete( x264_sync_frame_list_t *slist )
870
954
{
871
954
    x264_pthread_mutex_destroy( &slist->mutex );
872
954
    x264_pthread_cond_destroy( &slist->cv_fill );
873
954
    x264_pthread_cond_destroy( &slist->cv_empty );
874
954
    x264_frame_delete_list( slist->list );
875
954
}
x264_8_sync_frame_list_delete
Line
Count
Source
870
954
{
871
954
    x264_pthread_mutex_destroy( &slist->mutex );
872
954
    x264_pthread_cond_destroy( &slist->cv_fill );
873
954
    x264_pthread_cond_destroy( &slist->cv_empty );
874
954
    x264_frame_delete_list( slist->list );
875
954
}
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.46k
{
879
1.46k
    x264_pthread_mutex_lock( &slist->mutex );
880
1.46k
    while( slist->i_size == slist->i_max_size )
881
0
        x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
882
1.46k
    slist->list[ slist->i_size++ ] = frame;
883
1.46k
    x264_pthread_mutex_unlock( &slist->mutex );
884
1.46k
    x264_pthread_cond_broadcast( &slist->cv_fill );
885
1.46k
}
x264_8_sync_frame_list_push
Line
Count
Source
878
1.46k
{
879
1.46k
    x264_pthread_mutex_lock( &slist->mutex );
880
1.46k
    while( slist->i_size == slist->i_max_size )
881
0
        x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
882
1.46k
    slist->list[ slist->i_size++ ] = frame;
883
1.46k
    x264_pthread_mutex_unlock( &slist->mutex );
884
1.46k
    x264_pthread_cond_broadcast( &slist->cv_fill );
885
1.46k
}
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
159
{
889
159
    x264_frame_t *frame;
890
159
    x264_pthread_mutex_lock( &slist->mutex );
891
159
    while( !slist->i_size )
892
0
        x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
893
159
    frame = slist->list[ --slist->i_size ];
894
159
    slist->list[ slist->i_size ] = NULL;
895
159
    x264_pthread_cond_broadcast( &slist->cv_empty );
896
159
    x264_pthread_mutex_unlock( &slist->mutex );
897
159
    return frame;
898
159
}
x264_8_sync_frame_list_pop
Line
Count
Source
888
159
{
889
159
    x264_frame_t *frame;
890
159
    x264_pthread_mutex_lock( &slist->mutex );
891
159
    while( !slist->i_size )
892
0
        x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
893
159
    frame = slist->list[ --slist->i_size ];
894
159
    slist->list[ slist->i_size ] = NULL;
895
159
    x264_pthread_cond_broadcast( &slist->cv_empty );
896
159
    x264_pthread_mutex_unlock( &slist->mutex );
897
159
    return frame;
898
159
}
Unexecuted instantiation: x264_10_sync_frame_list_pop