1 /*****************************************************************************
2 * frame.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
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.
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.
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 *****************************************************************************/
27 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
29 x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
33 int i_mb_count = h->mb.i_mb_count;
34 int i_stride, i_width, i_lines;
35 int i_padv = PADV << h->param.b_interlaced;
37 int chroma_plane_size;
38 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
40 CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
42 /* allocate frame data (+64 for extra data for me) */
43 i_width = h->sps->i_mb_width*16;
44 i_stride = ALIGN( i_width + 2*PADH, align );
45 i_lines = h->sps->i_mb_height*16;
48 for( int i = 0; i < 3; i++ )
50 frame->i_stride[i] = ALIGN( i_stride >> !!i, align );
51 frame->i_width[i] = i_width >> !!i;
52 frame->i_lines[i] = i_lines >> !!i;
55 luma_plane_size = (frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv));
56 chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + 2*i_padv));
57 for( int i = 1; i < 3; i++ )
59 CHECKED_MALLOC( frame->buffer[i], chroma_plane_size * sizeof(pixel) );
60 frame->plane[i] = frame->buffer[i] + (frame->i_stride[i] * i_padv + PADH)/2;
63 for( int i = 0; i < h->param.i_bframe + 2; i++ )
64 for( int j = 0; j < h->param.i_bframe + 2; j++ )
65 CHECKED_MALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
68 frame->i_type = X264_TYPE_AUTO;
72 frame->i_frame_num = -1;
73 frame->i_lines_completed = -1;
74 frame->b_fdec = b_fdec;
75 frame->i_pic_struct = PIC_STRUCT_AUTO;
76 frame->i_field_cnt = -1;
78 frame->i_cpb_duration =
79 frame->i_dpb_output_delay =
80 frame->i_cpb_delay = 0;
81 frame->i_coded_fields_lookahead =
82 frame->i_cpb_delay_lookahead = -1;
86 /* all 4 luma planes allocated together, since the cacheline split code
87 * requires them to be in-phase wrt cacheline alignment. */
88 if( h->param.analyse.i_subpel_refine && b_fdec )
90 CHECKED_MALLOC( frame->buffer[0], 4*luma_plane_size * sizeof(pixel) );
91 for( int i = 0; i < 4; i++ )
92 frame->filtered[i] = frame->buffer[0] + i*luma_plane_size + frame->i_stride[0] * i_padv + PADH;
93 frame->plane[0] = frame->filtered[0];
97 CHECKED_MALLOC( frame->buffer[0], luma_plane_size * sizeof(pixel) );
98 frame->filtered[0] = frame->plane[0] = frame->buffer[0] + frame->i_stride[0] * i_padv + PADH;
101 frame->b_duplicate = 0;
103 if( b_fdec ) /* fdec frame */
105 CHECKED_MALLOC( frame->mb_type, i_mb_count * sizeof(int8_t));
106 CHECKED_MALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t));
107 CHECKED_MALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
108 CHECKED_MALLOC( frame->mv16x16, 2*i_mb_count * sizeof(int16_t) );
109 CHECKED_MALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
110 if( h->param.i_bframe )
112 CHECKED_MALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
113 CHECKED_MALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
118 frame->ref[1] = NULL;
120 CHECKED_MALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
121 CHECKED_MALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
122 if( h->param.analyse.i_me_method >= X264_ME_ESA )
124 CHECKED_MALLOC( frame->buffer[3],
125 frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) << h->frames.b_have_sub8x8_esa );
126 frame->integral = (uint16_t*)frame->buffer[3] + frame->i_stride[0] * i_padv + PADH;
129 else /* fenc frame */
131 if( h->frames.b_have_lowres )
133 frame->i_width_lowres = frame->i_width[0]/2;
134 frame->i_stride_lowres = ALIGN( frame->i_width_lowres + 2*PADH, align );
135 frame->i_lines_lowres = frame->i_lines[0]/2;
137 luma_plane_size = frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV);
139 CHECKED_MALLOC( frame->buffer_lowres[0], 4 * luma_plane_size * sizeof(pixel) );
140 for( int i = 0; i < 4; i++ )
141 frame->lowres[i] = frame->buffer_lowres[0] + (frame->i_stride_lowres * PADV + PADH) + i * luma_plane_size;
143 for( int j = 0; j <= !!h->param.i_bframe; j++ )
144 for( int i = 0; i <= h->param.i_bframe; i++ )
146 CHECKED_MALLOCZERO( frame->lowres_mvs[j][i], 2*h->mb.i_mb_count*sizeof(int16_t) );
147 CHECKED_MALLOC( frame->lowres_mv_costs[j][i], h->mb.i_mb_count*sizeof(int) );
149 CHECKED_MALLOC( frame->i_propagate_cost, (i_mb_count+3) * sizeof(uint16_t) );
150 for( int j = 0; j <= h->param.i_bframe+1; j++ )
151 for( int i = 0; i <= h->param.i_bframe+1; i++ )
152 CHECKED_MALLOC( frame->lowres_costs[j][i], (i_mb_count+3) * sizeof(uint16_t) );
153 frame->i_intra_cost = frame->lowres_costs[0][0];
154 memset( frame->i_intra_cost, -1, (i_mb_count+3) * sizeof(uint16_t) );
156 if( h->param.rc.i_aq_mode )
158 CHECKED_MALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
159 CHECKED_MALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
160 if( h->frames.b_have_lowres )
161 /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
162 CHECKED_MALLOCZERO( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
166 if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
168 if( x264_pthread_cond_init( &frame->cv, NULL ) )
178 void x264_frame_delete( x264_frame_t *frame )
180 /* Duplicate frames are blank copies of real frames (including pointers),
181 * so freeing those pointers would cause a double free later. */
182 if( !frame->b_duplicate )
184 for( int i = 0; i < 4; i++ )
185 x264_free( frame->buffer[i] );
186 for( int i = 0; i < 4; i++ )
187 x264_free( frame->buffer_lowres[i] );
188 for( int i = 0; i < X264_BFRAME_MAX+2; i++ )
189 for( int j = 0; j < X264_BFRAME_MAX+2; j++ )
190 x264_free( frame->i_row_satds[i][j] );
191 for( int j = 0; j < 2; j++ )
192 for( int i = 0; i <= X264_BFRAME_MAX; i++ )
194 x264_free( frame->lowres_mvs[j][i] );
195 x264_free( frame->lowres_mv_costs[j][i] );
197 x264_free( frame->i_propagate_cost );
198 for( int j = 0; j <= X264_BFRAME_MAX+1; j++ )
199 for( int i = 0; i <= X264_BFRAME_MAX+1; i++ )
200 x264_free( frame->lowres_costs[j][i] );
201 x264_free( frame->f_qp_offset );
202 x264_free( frame->f_qp_offset_aq );
203 x264_free( frame->i_inv_qscale_factor );
204 x264_free( frame->i_row_bits );
205 x264_free( frame->f_row_qp );
206 x264_free( frame->mb_type );
207 x264_free( frame->mb_partition );
208 x264_free( frame->mv[0] );
209 x264_free( frame->mv[1] );
210 x264_free( frame->mv16x16 );
211 x264_free( frame->ref[0] );
212 x264_free( frame->ref[1] );
213 x264_pthread_mutex_destroy( &frame->mutex );
214 x264_pthread_cond_destroy( &frame->cv );
219 int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
221 int i_csp = src->img.i_csp & X264_CSP_MASK;
222 if( i_csp != X264_CSP_I420 && i_csp != X264_CSP_YV12 )
224 x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
228 dst->i_type = src->i_type;
229 dst->i_qpplus1 = src->i_qpplus1;
230 dst->i_pts = dst->i_reordered_pts = src->i_pts;
231 dst->param = src->param;
232 dst->i_pic_struct = src->i_pic_struct;
234 for( int i = 0; i < 3; i++ )
236 int s = (i_csp == X264_CSP_YV12 && i) ? i^3 : i;
237 uint8_t *plane = src->img.plane[s];
238 int stride = src->img.i_stride[s];
239 int width = h->param.i_width >> !!i;
240 int height = h->param.i_height >> !!i;
241 if( src->img.i_csp & X264_CSP_VFLIP )
243 plane += (height-1)*stride;
246 if( width > abs(stride) )
248 x264_log( h, X264_LOG_ERROR, "Input picture width is greater than stride\n" );
251 h->mc.plane_copy( dst->plane[i], dst->i_stride[i], plane, stride, width, height );
256 static void ALWAYS_INLINE pixel_memset( pixel *dst, int value, int size )
258 for( int i = 0; i < size; i++ )
262 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 )
264 #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
265 for( int y = 0; y < i_height; y++ )
268 pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y)[0], i_padh );
270 pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_padh );
274 for( int y = 0; y < i_padv; y++ )
275 memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * sizeof(pixel) );
278 for( int y = 0; y < i_padv; y++ )
279 memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * sizeof(pixel) );
283 void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
286 if( mb_y & h->sh.b_mbaff )
288 for( int i = 0; i < frame->i_plane; i++ )
290 int stride = frame->i_stride[i];
291 int width = 16*h->sps->i_mb_width >> !!i;
292 int height = (b_end ? 16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff : 16) >> !!i;
293 int padh = PADH >> !!i;
294 int padv = PADV >> !!i;
295 // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
296 pixel *pix = frame->plane[i] + X264_MAX(0, (16*mb_y-4)*stride >> !!i);
297 if( b_end && !b_start )
298 height += 4 >> (!!i + h->sh.b_mbaff);
301 plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
302 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
306 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
311 void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
313 /* during filtering, 8 extra pixels were filtered on each edge,
314 * but up to 3 of the horizontal ones may be wrong.
315 we want to expand border from the last filtered pixel */
317 int stride = frame->i_stride[0];
318 int width = 16*h->sps->i_mb_width + 8;
319 int height = b_end ? (16*(h->sps->i_mb_height - mb_y) >> h->sh.b_mbaff) + 16 : 16;
322 for( int i = 1; i < 4; i++ )
324 // buffer: 8 luma, to match the hpel filter
325 pixel *pix = frame->filtered[i] + (16*mb_y - (8 << h->sh.b_mbaff)) * stride - 4;
328 plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end );
329 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end );
332 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
336 void x264_frame_expand_border_lowres( x264_frame_t *frame )
338 for( int i = 0; i < 4; i++ )
339 plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1 );
342 void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
344 for( int i = 0; i < frame->i_plane; i++ )
346 int i_subsample = i ? 1 : 0;
347 int i_width = h->param.i_width >> i_subsample;
348 int i_height = h->param.i_height >> i_subsample;
349 int i_padx = (h->sps->i_mb_width * 16 - h->param.i_width) >> i_subsample;
350 int i_pady = (h->sps->i_mb_height * 16 - h->param.i_height) >> i_subsample;
354 for( int y = 0; y < i_height; y++ )
356 pixel value = frame->plane[i][y*frame->i_stride[i] + i_width - 1];
357 pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width], value, i_padx );
362 for( int y = i_height; y < i_height + i_pady; y++ )
363 memcpy( &frame->plane[i][y*frame->i_stride[i]],
364 &frame->plane[i][(i_height-(~y&h->param.b_interlaced)-1)*frame->i_stride[i]],
365 (i_width + i_padx) * sizeof(pixel) );
371 void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
373 x264_pthread_mutex_lock( &frame->mutex );
374 frame->i_lines_completed = i_lines_completed;
375 x264_pthread_cond_broadcast( &frame->cv );
376 x264_pthread_mutex_unlock( &frame->mutex );
379 void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
381 x264_pthread_mutex_lock( &frame->mutex );
382 while( frame->i_lines_completed < i_lines_completed )
383 x264_pthread_cond_wait( &frame->cv, &frame->mutex );
384 x264_pthread_mutex_unlock( &frame->mutex );
389 void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
392 while( list[i] ) i++;
396 x264_frame_t *x264_frame_pop( x264_frame_t **list )
401 while( list[i+1] ) i++;
407 void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
410 while( list[i] ) i++;
416 x264_frame_t *x264_frame_shift( x264_frame_t **list )
418 x264_frame_t *frame = list[0];
420 for( i = 0; list[i]; i++ )
426 void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
428 assert( frame->i_reference_count > 0 );
429 frame->i_reference_count--;
430 if( frame->i_reference_count == 0 )
431 x264_frame_push( h->frames.unused[frame->b_fdec], frame );
434 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
437 if( h->frames.unused[b_fdec][0] )
438 frame = x264_frame_pop( h->frames.unused[b_fdec] );
440 frame = x264_frame_new( h, b_fdec );
443 frame->b_last_minigop_bframe = 0;
444 frame->i_reference_count = 1;
445 frame->b_intra_calculated = 0;
446 frame->b_scenecut = 1;
447 frame->b_keyframe = 0;
449 memset( frame->weight, 0, sizeof(frame->weight) );
450 memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
455 void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
457 assert( frame->i_reference_count > 0 );
458 frame->i_reference_count--;
459 if( frame->i_reference_count == 0 )
460 x264_frame_push( h->frames.blank_unused, frame );
463 x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
466 if( h->frames.blank_unused[0] )
467 frame = x264_frame_pop( h->frames.blank_unused );
469 frame = x264_malloc( sizeof(x264_frame_t) );
472 frame->b_duplicate = 1;
473 frame->i_reference_count = 1;
477 void x264_frame_sort( x264_frame_t **list, int b_dts )
482 for( int i = 0; list[i+1]; i++ )
484 int dtype = list[i]->i_type - list[i+1]->i_type;
485 int dtime = list[i]->i_frame - list[i+1]->i_frame;
486 int swap = b_dts ? dtype > 0 || ( dtype == 0 && dtime > 0 )
490 XCHG( x264_frame_t*, list[i], list[i+1] );
497 void x264_weight_scale_plane( x264_t *h, pixel *dst, int i_dst_stride, pixel *src, int i_src_stride,
498 int i_width, int i_height, x264_weight_t *w )
500 /* Weight horizontal strips of height 16. This was found to be the optimal height
501 * in terms of the cache loads. */
502 while( i_height > 0 )
504 for( int x = 0; x < i_width; x += 16 )
505 w->weightfn[16>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
507 dst += 16 * i_dst_stride;
508 src += 16 * i_src_stride;
512 void x264_frame_delete_list( x264_frame_t **list )
518 x264_frame_delete( list[i++] );
522 int x264_synch_frame_list_init( x264_synch_frame_list_t *slist, int max_size )
526 slist->i_max_size = max_size;
528 CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
529 if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
530 x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
531 x264_pthread_cond_init( &slist->cv_empty, NULL ) )
538 void x264_synch_frame_list_delete( x264_synch_frame_list_t *slist )
540 x264_pthread_mutex_destroy( &slist->mutex );
541 x264_pthread_cond_destroy( &slist->cv_fill );
542 x264_pthread_cond_destroy( &slist->cv_empty );
543 x264_frame_delete_list( slist->list );
546 void x264_synch_frame_list_push( x264_synch_frame_list_t *slist, x264_frame_t *frame )
548 x264_pthread_mutex_lock( &slist->mutex );
549 while( slist->i_size == slist->i_max_size )
550 x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
551 slist->list[ slist->i_size++ ] = frame;
552 x264_pthread_mutex_unlock( &slist->mutex );
553 x264_pthread_cond_broadcast( &slist->cv_fill );