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 x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
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;
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;
38 CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
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;
46 for( int i = 0; i < 3; i++ )
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;
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++ )
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;
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) );
66 frame->i_type = X264_TYPE_AUTO;
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;
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;
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 )
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];
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;
99 frame->b_duplicate = 0;
101 if( b_fdec ) /* fdec frame */
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 )
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) );
116 frame->ref[1] = NULL;
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 )
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;
127 else /* fenc frame */
129 if( h->frames.b_have_lowres )
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;
135 luma_plane_size = frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV);
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;
141 for( int j = 0; j <= !!h->param.i_bframe; j++ )
142 for( int i = 0; i <= h->param.i_bframe; i++ )
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) );
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) );
154 if( h->param.rc.i_aq_mode )
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) );
164 if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
166 if( x264_pthread_cond_init( &frame->cv, NULL ) )
176 void x264_frame_delete( x264_frame_t *frame )
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 )
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++ )
192 x264_free( frame->lowres_mvs[j][i] );
193 x264_free( frame->lowres_mv_costs[j][i] );
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 );
217 int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
219 int i_csp = src->img.i_csp & X264_CSP_MASK;
220 if( i_csp != X264_CSP_I420 && i_csp != X264_CSP_YV12 )
222 x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
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;
232 for( int i = 0; i < 3; i++ )
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 )
241 plane += (height-1)*stride;
244 if( width > abs(stride) )
246 x264_log( h, X264_LOG_ERROR, "Input picture width is greater than stride\n" );
249 h->mc.plane_copy( dst->plane[i], dst->i_stride[i], plane, stride, width, height );
254 static void ALWAYS_INLINE pixel_memset( pixel *dst, int value, int size )
256 for( int i = 0; i < size; i++ )
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 )
262 #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
263 for( int y = 0; y < i_height; y++ )
266 pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y)[0], i_padh );
268 pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_padh );
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) );
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) );
281 void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
284 if( mb_y & h->sh.b_mbaff )
286 for( int i = 0; i < frame->i_plane; i++ )
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);
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 );
304 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
309 void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
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 */
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;
320 for( int i = 1; i < 4; i++ )
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;
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 );
330 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end );
334 void x264_frame_expand_border_lowres( x264_frame_t *frame )
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 );
340 void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
342 for( int i = 0; i < frame->i_plane; i++ )
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;
352 for( int y = 0; y < i_height; y++ )
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 );
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) );
369 void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
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 );
377 void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
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 );
387 void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
390 while( list[i] ) i++;
394 x264_frame_t *x264_frame_pop( x264_frame_t **list )
399 while( list[i+1] ) i++;
405 void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
408 while( list[i] ) i++;
414 x264_frame_t *x264_frame_shift( x264_frame_t **list )
416 x264_frame_t *frame = list[0];
418 for( i = 0; list[i]; i++ )
424 void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
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 );
432 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
435 if( h->frames.unused[b_fdec][0] )
436 frame = x264_frame_pop( h->frames.unused[b_fdec] );
438 frame = x264_frame_new( h, b_fdec );
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;
447 memset( frame->weight, 0, sizeof(frame->weight) );
448 memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
453 void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
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 );
461 x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
464 if( h->frames.blank_unused[0] )
465 frame = x264_frame_pop( h->frames.blank_unused );
467 frame = x264_malloc( sizeof(x264_frame_t) );
470 frame->b_duplicate = 1;
471 frame->i_reference_count = 1;
475 void x264_frame_sort( x264_frame_t **list, int b_dts )
480 for( int i = 0; list[i+1]; i++ )
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 )
488 XCHG( x264_frame_t*, list[i], list[i+1] );
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 )
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 )
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 ) );
505 dst += 16 * i_dst_stride;
506 src += 16 * i_src_stride;
510 void x264_frame_delete_list( x264_frame_t **list )
516 x264_frame_delete( list[i++] );
520 int x264_synch_frame_list_init( x264_synch_frame_list_t *slist, int max_size )
524 slist->i_max_size = max_size;
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 ) )
536 void x264_synch_frame_list_delete( x264_synch_frame_list_t *slist )
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 );
544 void x264_synch_frame_list_push( x264_synch_frame_list_t *slist, x264_frame_t *frame )
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 );