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