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