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