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