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