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