1 /*****************************************************************************
2 * frame.c: frame handling
3 *****************************************************************************
4 * Copyright (C) 2003-2014 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
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.
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.
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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
30 static int align_stride( int x, int align, int disalign )
32 x = ALIGN( x, align );
33 if( !(x&(disalign-1)) )
38 static int align_plane_size( int x, int disalign )
40 if( !(x&(disalign-1)) )
45 static int x264_frame_internal_csp( int external_csp )
47 switch( external_csp & X264_CSP_MASK )
68 static x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
71 int i_csp = x264_frame_internal_csp( h->param.i_csp );
72 int i_mb_count = h->mb.i_mb_count;
73 int i_stride, i_width, i_lines, luma_plane_count;
74 int i_padv = PADV << PARAM_INTERLACED;
76 #if ARCH_X86 || ARCH_X86_64
77 if( h->param.cpu&X264_CPU_CACHELINE_64 )
79 else if( h->param.cpu&X264_CPU_CACHELINE_32 || h->param.cpu&X264_CPU_AVX2 )
88 CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
91 /* allocate frame data (+64 for extra data for me) */
92 i_width = h->mb.i_mb_width*16;
93 i_lines = h->mb.i_mb_height*16;
94 i_stride = align_stride( i_width + 2*PADH, align, disalign );
96 if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
100 for( int i = 0; i < 2; i++ )
102 frame->i_width[i] = i_width >> i;
103 frame->i_lines[i] = i_lines >> (i && i_csp == X264_CSP_NV12);
104 frame->i_stride[i] = i_stride;
107 else if( i_csp == X264_CSP_I444 )
109 luma_plane_count = 3;
111 for( int i = 0; i < 3; i++ )
113 frame->i_width[i] = i_width;
114 frame->i_lines[i] = i_lines;
115 frame->i_stride[i] = i_stride;
121 frame->i_csp = i_csp;
122 frame->i_width_lowres = frame->i_width[0]/2;
123 frame->i_lines_lowres = frame->i_lines[0]/2;
124 frame->i_stride_lowres = align_stride( frame->i_width_lowres + 2*PADH, align, disalign<<1 );
126 for( int i = 0; i < h->param.i_bframe + 2; i++ )
127 for( int j = 0; j < h->param.i_bframe + 2; j++ )
128 PREALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
131 frame->i_type = X264_TYPE_AUTO;
132 frame->i_qpplus1 = X264_QP_AUTO;
135 frame->i_frame_num = -1;
136 frame->i_lines_completed = -1;
137 frame->b_fdec = b_fdec;
138 frame->i_pic_struct = PIC_STRUCT_AUTO;
139 frame->i_field_cnt = -1;
141 frame->i_cpb_duration =
142 frame->i_dpb_output_delay =
143 frame->i_cpb_delay = 0;
144 frame->i_coded_fields_lookahead =
145 frame->i_cpb_delay_lookahead = -1;
149 if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
151 int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
152 int chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + 2*chroma_padv));
153 PREALLOC( frame->buffer[1], chroma_plane_size * sizeof(pixel) );
154 if( PARAM_INTERLACED )
155 PREALLOC( frame->buffer_fld[1], chroma_plane_size * sizeof(pixel) );
158 /* all 4 luma planes allocated together, since the cacheline split code
159 * requires them to be in-phase wrt cacheline alignment. */
161 for( int p = 0; p < luma_plane_count; p++ )
163 int luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
164 if( h->param.analyse.i_subpel_refine && b_fdec )
166 /* FIXME: Don't allocate both buffers in non-adaptive MBAFF. */
167 PREALLOC( frame->buffer[p], 4*luma_plane_size * sizeof(pixel) );
168 if( PARAM_INTERLACED )
169 PREALLOC( frame->buffer_fld[p], 4*luma_plane_size * sizeof(pixel) );
173 PREALLOC( frame->buffer[p], luma_plane_size * sizeof(pixel) );
174 if( PARAM_INTERLACED )
175 PREALLOC( frame->buffer_fld[p], luma_plane_size * sizeof(pixel) );
179 frame->b_duplicate = 0;
181 if( b_fdec ) /* fdec frame */
183 PREALLOC( frame->mb_type, i_mb_count * sizeof(int8_t) );
184 PREALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t) );
185 PREALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
186 PREALLOC( frame->mv16x16, 2*(i_mb_count+1) * sizeof(int16_t) );
187 PREALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
188 if( h->param.i_bframe )
190 PREALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
191 PREALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
196 frame->ref[1] = NULL;
198 PREALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
199 PREALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
200 PREALLOC( frame->f_row_qscale, i_lines/16 * sizeof(float) );
201 if( h->param.analyse.i_me_method >= X264_ME_ESA )
202 PREALLOC( frame->buffer[3], frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) << h->frames.b_have_sub8x8_esa );
203 if( PARAM_INTERLACED )
204 PREALLOC( frame->field, i_mb_count * sizeof(uint8_t) );
205 if( h->param.analyse.b_mb_info )
206 PREALLOC( frame->effective_qp, i_mb_count * sizeof(uint8_t) );
208 else /* fenc frame */
210 if( h->frames.b_have_lowres )
212 int luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
214 PREALLOC( frame->buffer_lowres[0], 4 * luma_plane_size * sizeof(pixel) );
216 for( int j = 0; j <= !!h->param.i_bframe; j++ )
217 for( int i = 0; i <= h->param.i_bframe; i++ )
219 PREALLOC( frame->lowres_mvs[j][i], 2*h->mb.i_mb_count*sizeof(int16_t) );
220 PREALLOC( frame->lowres_mv_costs[j][i], h->mb.i_mb_count*sizeof(int) );
222 PREALLOC( frame->i_propagate_cost, (i_mb_count+7) * sizeof(uint16_t) );
223 for( int j = 0; j <= h->param.i_bframe+1; j++ )
224 for( int i = 0; i <= h->param.i_bframe+1; i++ )
225 PREALLOC( frame->lowres_costs[j][i], (i_mb_count+3) * sizeof(uint16_t) );
228 if( h->param.rc.i_aq_mode )
230 PREALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
231 PREALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
232 if( h->frames.b_have_lowres )
233 PREALLOC( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
237 PREALLOC_END( frame->base );
239 if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
241 int chroma_padv = i_padv >> (i_csp == X264_CSP_NV12);
242 frame->plane[1] = frame->buffer[1] + frame->i_stride[1] * chroma_padv + PADH;
243 if( PARAM_INTERLACED )
244 frame->plane_fld[1] = frame->buffer_fld[1] + frame->i_stride[1] * chroma_padv + PADH;
247 for( int p = 0; p < luma_plane_count; p++ )
249 int luma_plane_size = align_plane_size( frame->i_stride[p] * (frame->i_lines[p] + 2*i_padv), disalign );
250 if( h->param.analyse.i_subpel_refine && b_fdec )
252 for( int i = 0; i < 4; i++ )
254 frame->filtered[p][i] = frame->buffer[p] + i*luma_plane_size + frame->i_stride[p] * i_padv + PADH;
255 frame->filtered_fld[p][i] = frame->buffer_fld[p] + i*luma_plane_size + frame->i_stride[p] * i_padv + PADH;
257 frame->plane[p] = frame->filtered[p][0];
258 frame->plane_fld[p] = frame->filtered_fld[p][0];
262 frame->filtered[p][0] = frame->plane[p] = frame->buffer[p] + frame->i_stride[p] * i_padv + PADH;
263 frame->filtered_fld[p][0] = frame->plane_fld[p] = frame->buffer_fld[p] + frame->i_stride[p] * i_padv + PADH;
269 M32( frame->mv16x16[0] ) = 0;
272 if( h->param.analyse.i_me_method >= X264_ME_ESA )
273 frame->integral = (uint16_t*)frame->buffer[3] + frame->i_stride[0] * i_padv + PADH;
277 if( h->frames.b_have_lowres )
279 int luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
280 for( int i = 0; i < 4; i++ )
281 frame->lowres[i] = frame->buffer_lowres[0] + (frame->i_stride_lowres * PADV + PADH) + i * luma_plane_size;
283 for( int j = 0; j <= !!h->param.i_bframe; j++ )
284 for( int i = 0; i <= h->param.i_bframe; i++ )
285 memset( frame->lowres_mvs[j][i], 0, 2*h->mb.i_mb_count*sizeof(int16_t) );
287 frame->i_intra_cost = frame->lowres_costs[0][0];
288 memset( frame->i_intra_cost, -1, (i_mb_count+3) * sizeof(uint16_t) );
290 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 memset( frame->i_inv_qscale_factor, 0, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
296 if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
298 if( x264_pthread_cond_init( &frame->cv, NULL ) )
302 frame->opencl.ocl = h->opencl.ocl;
312 void x264_frame_delete( x264_frame_t *frame )
314 /* Duplicate frames are blank copies of real frames (including pointers),
315 * so freeing those pointers would cause a double free later. */
316 if( !frame->b_duplicate )
318 x264_free( frame->base );
320 if( frame->param && frame->param->param_free )
321 frame->param->param_free( frame->param );
322 if( frame->mb_info_free )
323 frame->mb_info_free( frame->mb_info );
324 if( frame->extra_sei.sei_free )
326 for( int i = 0; i < frame->extra_sei.num_payloads; i++ )
327 frame->extra_sei.sei_free( frame->extra_sei.payloads[i].payload );
328 frame->extra_sei.sei_free( frame->extra_sei.payloads );
330 x264_pthread_mutex_destroy( &frame->mutex );
331 x264_pthread_cond_destroy( &frame->cv );
333 x264_opencl_frame_delete( frame );
339 static int get_plane_ptr( x264_t *h, x264_picture_t *src, uint8_t **pix, int *stride, int plane, int xshift, int yshift )
341 int width = h->param.i_width >> xshift;
342 int height = h->param.i_height >> yshift;
343 *pix = src->img.plane[plane];
344 *stride = src->img.i_stride[plane];
345 if( src->img.i_csp & X264_CSP_VFLIP )
347 *pix += (height-1) * *stride;
350 if( width > abs(*stride) )
352 x264_log( h, X264_LOG_ERROR, "Input picture width (%d) is greater than stride (%d)\n", width, *stride );
358 #define get_plane_ptr(...) do{ if( get_plane_ptr(__VA_ARGS__) < 0 ) return -1; }while(0)
360 int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
362 int i_csp = src->img.i_csp & X264_CSP_MASK;
363 if( dst->i_csp != x264_frame_internal_csp( i_csp ) )
365 x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
370 if( !(src->img.i_csp & X264_CSP_HIGH_DEPTH) )
372 x264_log( h, X264_LOG_ERROR, "This build of x264 requires high depth input. Rebuild to support 8-bit input.\n" );
376 if( src->img.i_csp & X264_CSP_HIGH_DEPTH )
378 x264_log( h, X264_LOG_ERROR, "This build of x264 requires 8-bit input. Rebuild to support high depth input.\n" );
383 dst->i_type = src->i_type;
384 dst->i_qpplus1 = src->i_qpplus1;
385 dst->i_pts = dst->i_reordered_pts = src->i_pts;
386 dst->param = src->param;
387 dst->i_pic_struct = src->i_pic_struct;
388 dst->extra_sei = src->extra_sei;
389 dst->opaque = src->opaque;
390 dst->mb_info = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
391 dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;
395 if ( i_csp >= X264_CSP_BGR )
397 stride[0] = src->img.i_stride[0];
398 pix[0] = src->img.plane[0];
399 if( src->img.i_csp & X264_CSP_VFLIP )
401 pix[0] += (h->param.i_height-1) * stride[0];
402 stride[0] = -stride[0];
404 int b = i_csp==X264_CSP_RGB;
405 h->mc.plane_copy_deinterleave_rgb( dst->plane[1+b], dst->i_stride[1+b],
406 dst->plane[0], dst->i_stride[0],
407 dst->plane[2-b], dst->i_stride[2-b],
408 (pixel*)pix[0], stride[0]/sizeof(pixel), i_csp==X264_CSP_BGRA ? 4 : 3, h->param.i_width, h->param.i_height );
412 int v_shift = CHROMA_V_SHIFT;
413 get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
414 h->mc.plane_copy( dst->plane[0], dst->i_stride[0], (pixel*)pix[0],
415 stride[0]/sizeof(pixel), h->param.i_width, h->param.i_height );
416 if( i_csp == X264_CSP_NV12 || i_csp == X264_CSP_NV16 )
418 get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, v_shift );
419 h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
420 stride[1]/sizeof(pixel), h->param.i_width, h->param.i_height>>v_shift );
422 else if( i_csp == X264_CSP_I420 || i_csp == X264_CSP_I422 || i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16 )
424 int uv_swap = i_csp == X264_CSP_YV12 || i_csp == X264_CSP_YV16;
425 get_plane_ptr( h, src, &pix[1], &stride[1], uv_swap ? 2 : 1, 1, v_shift );
426 get_plane_ptr( h, src, &pix[2], &stride[2], uv_swap ? 1 : 2, 1, v_shift );
427 h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
428 (pixel*)pix[1], stride[1]/sizeof(pixel),
429 (pixel*)pix[2], stride[2]/sizeof(pixel),
430 h->param.i_width>>1, h->param.i_height>>v_shift );
432 else //if( i_csp == X264_CSP_I444 || i_csp == X264_CSP_YV24 )
434 get_plane_ptr( h, src, &pix[1], &stride[1], i_csp==X264_CSP_I444 ? 1 : 2, 0, 0 );
435 get_plane_ptr( h, src, &pix[2], &stride[2], i_csp==X264_CSP_I444 ? 2 : 1, 0, 0 );
436 h->mc.plane_copy( dst->plane[1], dst->i_stride[1], (pixel*)pix[1],
437 stride[1]/sizeof(pixel), h->param.i_width, h->param.i_height );
438 h->mc.plane_copy( dst->plane[2], dst->i_stride[2], (pixel*)pix[2],
439 stride[2]/sizeof(pixel), h->param.i_width, h->param.i_height );
445 static void ALWAYS_INLINE pixel_memset( pixel *dst, pixel *src, int len, int size )
447 uint8_t *dstp = (uint8_t*)dst;
449 uint32_t v2 = size == 1 ? v1 + (v1 << 8) : M16( src );
450 uint32_t v4 = size <= 2 ? v2 + (v2 << 16) : M32( src );
454 /* Align the input pointer if it isn't already */
455 if( (intptr_t)dstp & (WORD_SIZE - 1) )
457 if( size <= 2 && ((intptr_t)dstp & 3) )
459 if( size == 1 && ((intptr_t)dstp & 1) )
461 if( (intptr_t)dstp & 2 )
467 if( WORD_SIZE == 8 && (intptr_t)dstp & 4 )
477 uint64_t v8 = v4 + ((uint64_t)v4<<32);
478 for( ; i < len - 7; i+=8 )
481 for( ; i < len - 3; i+=4 )
484 /* Finish up the last few bytes */
492 if( size == 1 && i != len )
497 static void ALWAYS_INLINE 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 )
499 #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
500 for( int y = 0; y < i_height; y++ )
503 pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y), i_padh>>b_chroma, sizeof(pixel)<<b_chroma );
505 pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1-b_chroma, y), i_padh>>b_chroma, sizeof(pixel)<<b_chroma );
509 for( int y = 0; y < i_padv; y++ )
510 memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * sizeof(pixel) );
513 for( int y = 0; y < i_padv; y++ )
514 memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * sizeof(pixel) );
518 void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y )
520 int pad_top = mb_y == 0;
521 int pad_bot = mb_y == h->mb.i_mb_height - (1 << SLICE_MBAFF);
522 int b_start = mb_y == h->i_threadslice_start;
523 int b_end = mb_y == h->i_threadslice_end - (1 << SLICE_MBAFF);
524 if( mb_y & SLICE_MBAFF )
526 for( int i = 0; i < frame->i_plane; i++ )
528 int h_shift = i && CHROMA_H_SHIFT;
529 int v_shift = i && CHROMA_V_SHIFT;
530 int stride = frame->i_stride[i];
531 int width = 16*h->mb.i_mb_width;
532 int height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF : 16) >> v_shift;
534 int padv = PADV >> v_shift;
535 // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
536 if( b_end && !b_start )
537 height += 4 >> (v_shift + SLICE_MBAFF);
539 int starty = 16*mb_y - 4*!b_start;
542 // border samples for each field are extended separately
543 pix = frame->plane_fld[i] + (starty*stride >> v_shift);
544 plane_expand_border( pix, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
545 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, pad_top, pad_bot, h_shift );
547 height = (pad_bot ? 16*(h->mb.i_mb_height - mb_y) : 32) >> v_shift;
548 if( b_end && !b_start )
549 height += 4 >> v_shift;
550 pix = frame->plane[i] + (starty*stride >> v_shift);
551 plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
555 pix = frame->plane[i] + (starty*stride >> v_shift);
556 plane_expand_border( pix, stride, width, height, padh, padv, pad_top, pad_bot, h_shift );
561 void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
563 /* during filtering, 8 extra pixels were filtered on each edge,
564 * but up to 3 of the horizontal ones may be wrong.
565 we want to expand border from the last filtered pixel */
567 int width = 16*h->mb.i_mb_width + 8;
568 int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> SLICE_MBAFF) + 16 : 16;
571 for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
572 for( int i = 1; i < 4; i++ )
574 int stride = frame->i_stride[p];
575 // buffer: 8 luma, to match the hpel filter
579 pix = frame->filtered_fld[p][i] + (16*mb_y - 16) * stride - 4;
580 plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end, 0 );
581 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end, 0 );
584 pix = frame->filtered[p][i] + (16*mb_y - 8) * stride - 4;
585 plane_expand_border( pix, stride, width, height << SLICE_MBAFF, padh, padv, b_start, b_end, 0 );
589 void x264_frame_expand_border_lowres( x264_frame_t *frame )
591 for( int i = 0; i < 4; i++ )
592 plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
595 void x264_frame_expand_border_chroma( x264_t *h, x264_frame_t *frame, int plane )
597 int v_shift = CHROMA_V_SHIFT;
598 plane_expand_border( frame->plane[plane], frame->i_stride[plane], 16*h->mb.i_mb_width, 16*h->mb.i_mb_height>>v_shift,
599 PADH, PADV>>v_shift, 1, 1, CHROMA_H_SHIFT );
602 void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
604 for( int i = 0; i < frame->i_plane; i++ )
606 int i_width = h->param.i_width;
607 int h_shift = i && CHROMA_H_SHIFT;
608 int v_shift = i && CHROMA_V_SHIFT;
609 int i_height = h->param.i_height >> v_shift;
610 int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
611 int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
615 for( int y = 0; y < i_height; y++ )
616 pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
617 &frame->plane[i][y*frame->i_stride[i] + i_width - 1-h_shift],
618 i_padx>>h_shift, sizeof(pixel)<<h_shift );
622 for( int y = i_height; y < i_height + i_pady; y++ )
623 memcpy( &frame->plane[i][y*frame->i_stride[i]],
624 &frame->plane[i][(i_height-(~y&PARAM_INTERLACED)-1)*frame->i_stride[i]],
625 (i_width + i_padx) * sizeof(pixel) );
630 void x264_expand_border_mbpair( x264_t *h, int mb_x, int mb_y )
632 for( int i = 0; i < h->fenc->i_plane; i++ )
634 int v_shift = i && CHROMA_V_SHIFT;
635 int stride = h->fenc->i_stride[i];
636 int height = h->param.i_height >> v_shift;
637 int pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> v_shift;
638 pixel *fenc = h->fenc->plane[i] + 16*mb_x;
639 for( int y = height; y < height + pady; y++ )
640 memcpy( fenc + y*stride, fenc + (height-1)*stride, 16*sizeof(pixel) );
645 void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
647 x264_pthread_mutex_lock( &frame->mutex );
648 frame->i_lines_completed = i_lines_completed;
649 x264_pthread_cond_broadcast( &frame->cv );
650 x264_pthread_mutex_unlock( &frame->mutex );
653 void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
655 x264_pthread_mutex_lock( &frame->mutex );
656 while( frame->i_lines_completed < i_lines_completed )
657 x264_pthread_cond_wait( &frame->cv, &frame->mutex );
658 x264_pthread_mutex_unlock( &frame->mutex );
661 void x264_threadslice_cond_broadcast( x264_t *h, int pass )
663 x264_pthread_mutex_lock( &h->mutex );
664 h->i_threadslice_pass = pass;
666 x264_pthread_cond_broadcast( &h->cv );
667 x264_pthread_mutex_unlock( &h->mutex );
670 void x264_threadslice_cond_wait( x264_t *h, int pass )
672 x264_pthread_mutex_lock( &h->mutex );
673 while( h->i_threadslice_pass < pass )
674 x264_pthread_cond_wait( &h->cv, &h->mutex );
675 x264_pthread_mutex_unlock( &h->mutex );
678 int x264_frame_new_slice( x264_t *h, x264_frame_t *frame )
680 if( h->param.i_slice_count_max )
683 if( h->param.b_sliced_threads )
684 slice_count = x264_pthread_fetch_and_add( &frame->i_slice_count, 1, &frame->mutex );
686 slice_count = frame->i_slice_count++;
687 if( slice_count >= h->param.i_slice_count_max )
695 void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
698 while( list[i] ) i++;
702 x264_frame_t *x264_frame_pop( x264_frame_t **list )
707 while( list[i+1] ) i++;
713 void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
716 while( list[i] ) i++;
722 x264_frame_t *x264_frame_shift( x264_frame_t **list )
724 x264_frame_t *frame = list[0];
726 for( i = 0; list[i]; i++ )
732 void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
734 assert( frame->i_reference_count > 0 );
735 frame->i_reference_count--;
736 if( frame->i_reference_count == 0 )
737 x264_frame_push( h->frames.unused[frame->b_fdec], frame );
740 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
743 if( h->frames.unused[b_fdec][0] )
744 frame = x264_frame_pop( h->frames.unused[b_fdec] );
746 frame = x264_frame_new( h, b_fdec );
749 frame->b_last_minigop_bframe = 0;
750 frame->i_reference_count = 1;
751 frame->b_intra_calculated = 0;
752 frame->b_scenecut = 1;
753 frame->b_keyframe = 0;
754 frame->b_corrupt = 0;
755 frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
757 memset( frame->weight, 0, sizeof(frame->weight) );
758 memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
763 void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
765 assert( frame->i_reference_count > 0 );
766 frame->i_reference_count--;
767 if( frame->i_reference_count == 0 )
768 x264_frame_push( h->frames.blank_unused, frame );
771 x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
774 if( h->frames.blank_unused[0] )
775 frame = x264_frame_pop( h->frames.blank_unused );
777 frame = x264_malloc( sizeof(x264_frame_t) );
780 frame->b_duplicate = 1;
781 frame->i_reference_count = 1;
785 void x264_weight_scale_plane( x264_t *h, pixel *dst, intptr_t i_dst_stride, pixel *src, intptr_t i_src_stride,
786 int i_width, int i_height, x264_weight_t *w )
788 /* Weight horizontal strips of height 16. This was found to be the optimal height
789 * in terms of the cache loads. */
790 while( i_height > 0 )
793 for( x = 0; x < i_width-8; x += 16 )
794 w->weightfn[16>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
796 w->weightfn[ 8>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
798 dst += 16 * i_dst_stride;
799 src += 16 * i_src_stride;
803 void x264_frame_delete_list( x264_frame_t **list )
809 x264_frame_delete( list[i++] );
813 int x264_sync_frame_list_init( x264_sync_frame_list_t *slist, int max_size )
817 slist->i_max_size = max_size;
819 CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
820 if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
821 x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
822 x264_pthread_cond_init( &slist->cv_empty, NULL ) )
829 void x264_sync_frame_list_delete( x264_sync_frame_list_t *slist )
831 x264_pthread_mutex_destroy( &slist->mutex );
832 x264_pthread_cond_destroy( &slist->cv_fill );
833 x264_pthread_cond_destroy( &slist->cv_empty );
834 x264_frame_delete_list( slist->list );
837 void x264_sync_frame_list_push( x264_sync_frame_list_t *slist, x264_frame_t *frame )
839 x264_pthread_mutex_lock( &slist->mutex );
840 while( slist->i_size == slist->i_max_size )
841 x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
842 slist->list[ slist->i_size++ ] = frame;
843 x264_pthread_mutex_unlock( &slist->mutex );
844 x264_pthread_cond_broadcast( &slist->cv_fill );
847 x264_frame_t *x264_sync_frame_list_pop( x264_sync_frame_list_t *slist )
850 x264_pthread_mutex_lock( &slist->mutex );
851 while( !slist->i_size )
852 x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
853 frame = slist->list[ --slist->i_size ];
854 slist->list[ slist->i_size ] = NULL;
855 x264_pthread_cond_broadcast( &slist->cv_empty );
856 x264_pthread_mutex_unlock( &slist->mutex );