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