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