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 * Jason Garrett-Glaser <darkshikari@gmail.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 static int align_stride( int x, int align, int disalign )
29 x = ALIGN( x, align );
30 if( !(x&(disalign-1)) )
35 static int align_plane_size( int x, int disalign )
37 if( !(x&(disalign-1)) )
42 x264_frame_t *x264_frame_new( x264_t *h, int b_fdec )
46 int i_mb_count = h->mb.i_mb_count;
47 int i_stride, i_width, i_lines;
48 int i_padv = PADV << h->param.b_interlaced;
49 int luma_plane_size, chroma_plane_size;
50 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
51 int disalign = h->param.cpu&X264_CPU_ALTIVEC ? 1<<9 : 1<<10;
53 CHECKED_MALLOCZERO( frame, sizeof(x264_frame_t) );
55 /* allocate frame data (+64 for extra data for me) */
56 i_width = h->mb.i_mb_width*16;
57 i_lines = h->mb.i_mb_height*16;
58 i_stride = align_stride( i_width + 2*PADH, align, disalign );
61 for( int i = 0; i < 2; i++ )
63 frame->i_width[i] = i_width >> i;
64 frame->i_lines[i] = i_lines >> i;
65 frame->i_stride[i] = i_stride;
68 frame->i_width_lowres = frame->i_width[0]/2;
69 frame->i_lines_lowres = frame->i_lines[0]/2;
70 frame->i_stride_lowres = align_stride( frame->i_width_lowres + 2*PADH, align, disalign<<1 );
72 for( int i = 0; i < h->param.i_bframe + 2; i++ )
73 for( int j = 0; j < h->param.i_bframe + 2; j++ )
74 CHECKED_MALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) );
77 frame->i_type = X264_TYPE_AUTO;
81 frame->i_frame_num = -1;
82 frame->i_lines_completed = -1;
83 frame->b_fdec = b_fdec;
84 frame->i_pic_struct = PIC_STRUCT_AUTO;
85 frame->i_field_cnt = -1;
87 frame->i_cpb_duration =
88 frame->i_dpb_output_delay =
89 frame->i_cpb_delay = 0;
90 frame->i_coded_fields_lookahead =
91 frame->i_cpb_delay_lookahead = -1;
95 luma_plane_size = align_plane_size( frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv), disalign );
96 chroma_plane_size = (frame->i_stride[1] * (frame->i_lines[1] + i_padv));
98 CHECKED_MALLOC( frame->buffer[1], chroma_plane_size * sizeof(pixel) );
99 frame->plane[1] = frame->buffer[1] + frame->i_stride[1] * i_padv/2 + PADH;
101 /* all 4 luma planes allocated together, since the cacheline split code
102 * requires them to be in-phase wrt cacheline alignment. */
103 if( h->param.analyse.i_subpel_refine && b_fdec )
105 CHECKED_MALLOC( frame->buffer[0], 4*luma_plane_size * sizeof(pixel) );
106 for( int i = 0; i < 4; i++ )
107 frame->filtered[i] = frame->buffer[0] + i*luma_plane_size + frame->i_stride[0] * i_padv + PADH;
108 frame->plane[0] = frame->filtered[0];
112 CHECKED_MALLOC( frame->buffer[0], luma_plane_size * sizeof(pixel) );
113 frame->filtered[0] = frame->plane[0] = frame->buffer[0] + frame->i_stride[0] * i_padv + PADH;
116 frame->b_duplicate = 0;
118 if( b_fdec ) /* fdec frame */
120 CHECKED_MALLOC( frame->mb_type, i_mb_count * sizeof(int8_t));
121 CHECKED_MALLOC( frame->mb_partition, i_mb_count * sizeof(uint8_t));
122 CHECKED_MALLOC( frame->mv[0], 2*16 * i_mb_count * sizeof(int16_t) );
123 CHECKED_MALLOC( frame->mv16x16, 2*(i_mb_count+1) * sizeof(int16_t) );
124 M32( frame->mv16x16[0] ) = 0;
126 CHECKED_MALLOC( frame->ref[0], 4 * i_mb_count * sizeof(int8_t) );
127 if( h->param.i_bframe )
129 CHECKED_MALLOC( frame->mv[1], 2*16 * i_mb_count * sizeof(int16_t) );
130 CHECKED_MALLOC( frame->ref[1], 4 * i_mb_count * sizeof(int8_t) );
135 frame->ref[1] = NULL;
137 CHECKED_MALLOC( frame->i_row_bits, i_lines/16 * sizeof(int) );
138 CHECKED_MALLOC( frame->f_row_qp, i_lines/16 * sizeof(float) );
139 if( h->param.analyse.i_me_method >= X264_ME_ESA )
141 CHECKED_MALLOC( frame->buffer[3],
142 frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) << h->frames.b_have_sub8x8_esa );
143 frame->integral = (uint16_t*)frame->buffer[3] + frame->i_stride[0] * i_padv + PADH;
146 else /* fenc frame */
148 if( h->frames.b_have_lowres )
150 luma_plane_size = align_plane_size( frame->i_stride_lowres * (frame->i_lines[0]/2 + 2*PADV), disalign );
152 CHECKED_MALLOC( frame->buffer_lowres[0], 4 * luma_plane_size * sizeof(pixel) );
153 for( int i = 0; i < 4; i++ )
154 frame->lowres[i] = frame->buffer_lowres[0] + (frame->i_stride_lowres * PADV + PADH) + i * luma_plane_size;
156 for( int j = 0; j <= !!h->param.i_bframe; j++ )
157 for( int i = 0; i <= h->param.i_bframe; i++ )
159 CHECKED_MALLOCZERO( frame->lowres_mvs[j][i], 2*h->mb.i_mb_count*sizeof(int16_t) );
160 CHECKED_MALLOC( frame->lowres_mv_costs[j][i], h->mb.i_mb_count*sizeof(int) );
162 CHECKED_MALLOC( frame->i_propagate_cost, (i_mb_count+3) * sizeof(uint16_t) );
163 for( int j = 0; j <= h->param.i_bframe+1; j++ )
164 for( int i = 0; i <= h->param.i_bframe+1; i++ )
165 CHECKED_MALLOC( frame->lowres_costs[j][i], (i_mb_count+3) * sizeof(uint16_t) );
166 frame->i_intra_cost = frame->lowres_costs[0][0];
167 memset( frame->i_intra_cost, -1, (i_mb_count+3) * sizeof(uint16_t) );
169 if( h->param.rc.i_aq_mode )
171 CHECKED_MALLOC( frame->f_qp_offset, h->mb.i_mb_count * sizeof(float) );
172 CHECKED_MALLOC( frame->f_qp_offset_aq, h->mb.i_mb_count * sizeof(float) );
173 if( h->frames.b_have_lowres )
174 /* shouldn't really be initialized, just silences a valgrind false-positive in x264_mbtree_propagate_cost_sse2 */
175 CHECKED_MALLOCZERO( frame->i_inv_qscale_factor, (h->mb.i_mb_count+3) * sizeof(uint16_t) );
179 if( x264_pthread_mutex_init( &frame->mutex, NULL ) )
181 if( x264_pthread_cond_init( &frame->cv, NULL ) )
191 void x264_frame_delete( x264_frame_t *frame )
193 /* Duplicate frames are blank copies of real frames (including pointers),
194 * so freeing those pointers would cause a double free later. */
195 if( !frame->b_duplicate )
197 for( int i = 0; i < 4; i++ )
198 x264_free( frame->buffer[i] );
199 for( int i = 0; i < 4; i++ )
200 x264_free( frame->buffer_lowres[i] );
201 for( int i = 0; i < X264_BFRAME_MAX+2; i++ )
202 for( int j = 0; j < X264_BFRAME_MAX+2; j++ )
203 x264_free( frame->i_row_satds[i][j] );
204 for( int j = 0; j < 2; j++ )
205 for( int i = 0; i <= X264_BFRAME_MAX; i++ )
207 x264_free( frame->lowres_mvs[j][i] );
208 x264_free( frame->lowres_mv_costs[j][i] );
210 x264_free( frame->i_propagate_cost );
211 for( int j = 0; j <= X264_BFRAME_MAX+1; j++ )
212 for( int i = 0; i <= X264_BFRAME_MAX+1; i++ )
213 x264_free( frame->lowres_costs[j][i] );
214 x264_free( frame->f_qp_offset );
215 x264_free( frame->f_qp_offset_aq );
216 x264_free( frame->i_inv_qscale_factor );
217 x264_free( frame->i_row_bits );
218 x264_free( frame->f_row_qp );
219 x264_free( frame->mb_type );
220 x264_free( frame->mb_partition );
221 x264_free( frame->mv[0] );
222 x264_free( frame->mv[1] );
224 x264_free( frame->mv16x16-1 );
225 x264_free( frame->ref[0] );
226 x264_free( frame->ref[1] );
227 x264_pthread_mutex_destroy( &frame->mutex );
228 x264_pthread_cond_destroy( &frame->cv );
233 static int get_plane_ptr( x264_t *h, x264_picture_t *src, uint8_t **pix, int *stride, int plane, int xshift, int yshift )
235 int width = h->param.i_width >> xshift;
236 int height = h->param.i_height >> yshift;
237 *pix = src->img.plane[plane];
238 *stride = src->img.i_stride[plane];
239 if( src->img.i_csp & X264_CSP_VFLIP )
241 *pix += (height-1) * *stride;
244 if( width > abs(*stride) )
246 x264_log( h, X264_LOG_ERROR, "Input picture width (%d) is greater than stride (%d)\n", width, *stride );
252 #define get_plane_ptr(...) do{ if( get_plane_ptr(__VA_ARGS__) < 0 ) return -1; }while(0)
254 int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
256 int i_csp = src->img.i_csp & X264_CSP_MASK;
257 if( i_csp <= X264_CSP_NONE || i_csp >= X264_CSP_MAX )
259 x264_log( h, X264_LOG_ERROR, "Invalid input colorspace\n" );
263 dst->i_type = src->i_type;
264 dst->i_qpplus1 = src->i_qpplus1;
265 dst->i_pts = dst->i_reordered_pts = src->i_pts;
266 dst->param = src->param;
267 dst->i_pic_struct = src->i_pic_struct;
271 get_plane_ptr( h, src, &pix[0], &stride[0], 0, 0, 0 );
272 h->mc.plane_copy( dst->plane[0], dst->i_stride[0], pix[0], stride[0],
273 h->param.i_width, h->param.i_height );
274 if( i_csp == X264_CSP_NV12 )
276 get_plane_ptr( h, src, &pix[1], &stride[1], 1, 0, 1 );
277 h->mc.plane_copy( dst->plane[1], dst->i_stride[1], pix[1], stride[1],
278 h->param.i_width, h->param.i_height>>1 );
282 get_plane_ptr( h, src, &pix[1], &stride[1], i_csp==X264_CSP_I420 ? 1 : 2, 1, 1 );
283 get_plane_ptr( h, src, &pix[2], &stride[2], i_csp==X264_CSP_I420 ? 2 : 1, 1, 1 );
284 h->mc.plane_copy_interleave( dst->plane[1], dst->i_stride[1],
285 pix[1], stride[1], pix[2], stride[2],
286 h->param.i_width>>1, h->param.i_height>>1 );
291 static void ALWAYS_INLINE pixel_memset( pixel *dst, pixel *src, int len, int size )
293 uint8_t *dstp = (uint8_t*)dst;
295 memset(dst, *src, len);
296 } else if(size == 2) {
298 for(int i=0; i<len; i++)
300 } else if(size == 4) {
302 for(int i=0; i<len; i++)
307 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 )
309 #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride )
310 for( int y = 0; y < i_height; y++ )
313 pixel_memset( PPIXEL(-i_padh, y), PPIXEL(0, y), i_padh>>b_chroma, sizeof(pixel)<<b_chroma );
315 pixel_memset( PPIXEL(i_width, y), PPIXEL(i_width-1-b_chroma, y), i_padh>>b_chroma, sizeof(pixel)<<b_chroma );
319 for( int y = 0; y < i_padv; y++ )
320 memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), (i_width+2*i_padh) * sizeof(pixel) );
323 for( int y = 0; y < i_padv; y++ )
324 memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), (i_width+2*i_padh) * sizeof(pixel) );
328 void x264_frame_expand_border( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
331 if( mb_y & h->sh.b_mbaff )
333 for( int i = 0; i < frame->i_plane; i++ )
335 int stride = frame->i_stride[i];
336 int width = 16*h->sps->i_mb_width;
337 int height = (b_end ? 16*(h->mb.i_mb_height - mb_y) >> h->sh.b_mbaff : 16) >> !!i;
339 int padv = PADV >> !!i;
340 // buffer: 2 chroma, 3 luma (rounded to 4) because deblocking goes beyond the top of the mb
341 pixel *pix = frame->plane[i] + X264_MAX(0, (16*mb_y-4)*stride >> !!i);
342 if( b_end && !b_start )
343 height += 4 >> (!!i + h->sh.b_mbaff);
346 plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end, i );
347 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end, i );
351 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end, i );
356 void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
358 /* during filtering, 8 extra pixels were filtered on each edge,
359 * but up to 3 of the horizontal ones may be wrong.
360 we want to expand border from the last filtered pixel */
362 int stride = frame->i_stride[0];
363 int width = 16*h->mb.i_mb_width + 8;
364 int height = b_end ? (16*(h->mb.i_mb_height - mb_y) >> h->sh.b_mbaff) + 16 : 16;
367 for( int i = 1; i < 4; i++ )
369 // buffer: 8 luma, to match the hpel filter
370 pixel *pix = frame->filtered[i] + (16*mb_y - (8 << h->sh.b_mbaff)) * stride - 4;
373 plane_expand_border( pix, stride*2, width, height, padh, padv, b_start, b_end, 0 );
374 plane_expand_border( pix+stride, stride*2, width, height, padh, padv, b_start, b_end, 0 );
377 plane_expand_border( pix, stride, width, height, padh, padv, b_start, b_end, 0 );
381 void x264_frame_expand_border_lowres( x264_frame_t *frame )
383 for( int i = 0; i < 4; i++ )
384 plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres, PADH, PADV, 1, 1, 0 );
387 void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame )
389 for( int i = 0; i < frame->i_plane; i++ )
391 int i_width = h->param.i_width;
392 int i_height = h->param.i_height >> !!i;
393 int i_padx = (h->mb.i_mb_width * 16 - h->param.i_width);
394 int i_pady = (h->mb.i_mb_height * 16 - h->param.i_height) >> !!i;
398 for( int y = 0; y < i_height; y++ )
399 pixel_memset( &frame->plane[i][y*frame->i_stride[i] + i_width],
400 &frame->plane[i][y*frame->i_stride[i] + i_width - 1-i],
401 i_padx>>i, sizeof(pixel)<<i );
405 for( int y = i_height; y < i_height + i_pady; y++ )
406 memcpy( &frame->plane[i][y*frame->i_stride[i]],
407 &frame->plane[i][(i_height-(~y&h->param.b_interlaced)-1)*frame->i_stride[i]],
408 (i_width + i_padx) * sizeof(pixel) );
414 void x264_frame_cond_broadcast( x264_frame_t *frame, int i_lines_completed )
416 x264_pthread_mutex_lock( &frame->mutex );
417 frame->i_lines_completed = i_lines_completed;
418 x264_pthread_cond_broadcast( &frame->cv );
419 x264_pthread_mutex_unlock( &frame->mutex );
422 void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed )
424 x264_pthread_mutex_lock( &frame->mutex );
425 while( frame->i_lines_completed < i_lines_completed )
426 x264_pthread_cond_wait( &frame->cv, &frame->mutex );
427 x264_pthread_mutex_unlock( &frame->mutex );
432 void x264_frame_push( x264_frame_t **list, x264_frame_t *frame )
435 while( list[i] ) i++;
439 x264_frame_t *x264_frame_pop( x264_frame_t **list )
444 while( list[i+1] ) i++;
450 void x264_frame_unshift( x264_frame_t **list, x264_frame_t *frame )
453 while( list[i] ) i++;
459 x264_frame_t *x264_frame_shift( x264_frame_t **list )
461 x264_frame_t *frame = list[0];
463 for( i = 0; list[i]; i++ )
469 void x264_frame_push_unused( x264_t *h, x264_frame_t *frame )
471 assert( frame->i_reference_count > 0 );
472 frame->i_reference_count--;
473 if( frame->i_reference_count == 0 )
474 x264_frame_push( h->frames.unused[frame->b_fdec], frame );
477 x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
480 if( h->frames.unused[b_fdec][0] )
481 frame = x264_frame_pop( h->frames.unused[b_fdec] );
483 frame = x264_frame_new( h, b_fdec );
486 frame->b_last_minigop_bframe = 0;
487 frame->i_reference_count = 1;
488 frame->b_intra_calculated = 0;
489 frame->b_scenecut = 1;
490 frame->b_keyframe = 0;
491 frame->b_corrupt = 0;
493 memset( frame->weight, 0, sizeof(frame->weight) );
494 memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
499 void x264_frame_push_blank_unused( x264_t *h, x264_frame_t *frame )
501 assert( frame->i_reference_count > 0 );
502 frame->i_reference_count--;
503 if( frame->i_reference_count == 0 )
504 x264_frame_push( h->frames.blank_unused, frame );
507 x264_frame_t *x264_frame_pop_blank_unused( x264_t *h )
510 if( h->frames.blank_unused[0] )
511 frame = x264_frame_pop( h->frames.blank_unused );
513 frame = x264_malloc( sizeof(x264_frame_t) );
516 frame->b_duplicate = 1;
517 frame->i_reference_count = 1;
521 void x264_frame_sort( x264_frame_t **list, int b_dts )
526 for( int i = 0; list[i+1]; i++ )
528 int dtype = list[i]->i_type - list[i+1]->i_type;
529 int dtime = list[i]->i_frame - list[i+1]->i_frame;
530 int swap = b_dts ? dtype > 0 || ( dtype == 0 && dtime > 0 )
534 XCHG( x264_frame_t*, list[i], list[i+1] );
541 void x264_weight_scale_plane( x264_t *h, pixel *dst, int i_dst_stride, pixel *src, int i_src_stride,
542 int i_width, int i_height, x264_weight_t *w )
544 /* Weight horizontal strips of height 16. This was found to be the optimal height
545 * in terms of the cache loads. */
546 while( i_height > 0 )
548 for( int x = 0; x < i_width; x += 16 )
549 w->weightfn[16>>2]( dst+x, i_dst_stride, src+x, i_src_stride, w, X264_MIN( i_height, 16 ) );
551 dst += 16 * i_dst_stride;
552 src += 16 * i_src_stride;
556 void x264_frame_delete_list( x264_frame_t **list )
562 x264_frame_delete( list[i++] );
566 int x264_sync_frame_list_init( x264_sync_frame_list_t *slist, int max_size )
570 slist->i_max_size = max_size;
572 CHECKED_MALLOCZERO( slist->list, (max_size+1) * sizeof(x264_frame_t*) );
573 if( x264_pthread_mutex_init( &slist->mutex, NULL ) ||
574 x264_pthread_cond_init( &slist->cv_fill, NULL ) ||
575 x264_pthread_cond_init( &slist->cv_empty, NULL ) )
582 void x264_sync_frame_list_delete( x264_sync_frame_list_t *slist )
584 x264_pthread_mutex_destroy( &slist->mutex );
585 x264_pthread_cond_destroy( &slist->cv_fill );
586 x264_pthread_cond_destroy( &slist->cv_empty );
587 x264_frame_delete_list( slist->list );
590 void x264_sync_frame_list_push( x264_sync_frame_list_t *slist, x264_frame_t *frame )
592 x264_pthread_mutex_lock( &slist->mutex );
593 while( slist->i_size == slist->i_max_size )
594 x264_pthread_cond_wait( &slist->cv_empty, &slist->mutex );
595 slist->list[ slist->i_size++ ] = frame;
596 x264_pthread_mutex_unlock( &slist->mutex );
597 x264_pthread_cond_broadcast( &slist->cv_fill );
600 x264_frame_t *x264_sync_frame_list_pop( x264_sync_frame_list_t *slist )
603 x264_pthread_mutex_lock( &slist->mutex );
604 while( !slist->i_size )
605 x264_pthread_cond_wait( &slist->cv_fill, &slist->mutex );
606 frame = slist->list[ --slist->i_size ];
607 slist->list[ slist->i_size ] = NULL;
608 x264_pthread_cond_broadcast( &slist->cv_empty );
609 x264_pthread_mutex_unlock( &slist->mutex );