Skip to content

Commit

Permalink
Merge branch 'kaudio' into lsmash
Browse files Browse the repository at this point in the history
  • Loading branch information
VFR-maniac committed May 26, 2012
2 parents 96092fd + faeb40c commit 54929fe
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 68 deletions.
9 changes: 9 additions & 0 deletions common/common.c
Expand Up @@ -50,6 +50,7 @@ void x264_param_default( x264_param_t *param )
/* CPU autodetect */
param->cpu = x264_cpu_detect();
param->i_threads = X264_THREADS_AUTO;
param->i_lookahead_threads = X264_THREADS_AUTO;
param->b_deterministic = 1;
param->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;

Expand Down Expand Up @@ -632,6 +633,13 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
else
p->i_threads = atoi(value);
}
OPT("lookahead-threads")
{
if( !strcmp(value, "auto") )
p->i_lookahead_threads = X264_THREADS_AUTO;
else
p->i_lookahead_threads = atoi(value);
}
OPT("sliced-threads")
p->b_sliced_threads = atobool(value);
OPT("sync-lookahead")
Expand Down Expand Up @@ -1285,6 +1293,7 @@ char *x264_param2string( x264_param_t *p, int b_res )
s += sprintf( s, " fast_pskip=%d", p->analyse.b_fast_pskip );
s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
s += sprintf( s, " threads=%d", p->i_threads );
s += sprintf( s, " lookahead_threads=%d", p->i_lookahead_threads );
s += sprintf( s, " sliced_threads=%d", p->b_sliced_threads );
if( p->i_slice_count )
s += sprintf( s, " slices=%d", p->i_slice_count );
Expand Down
4 changes: 4 additions & 0 deletions common/common.h
Expand Up @@ -56,6 +56,7 @@ do {\
#define X264_BFRAME_MAX 16
#define X264_REF_MAX 16
#define X264_THREAD_MAX 128
#define X264_LOOKAHEAD_THREAD_MAX 16
#define X264_PCM_COST (FRAME_SIZE(256*BIT_DEPTH)+16)
#define X264_LOOKAHEAD_MAX 250
#define QP_BD_OFFSET (6*(BIT_DEPTH-8))
Expand Down Expand Up @@ -469,13 +470,15 @@ struct x264_t
x264_param_t param;

x264_t *thread[X264_THREAD_MAX+1];
x264_t *lookahead_thread[X264_LOOKAHEAD_THREAD_MAX];
int b_thread_active;
int i_thread_phase; /* which thread to use for the next frame */
int i_thread_idx; /* which thread this is */
int i_threadslice_start; /* first row in this thread slice */
int i_threadslice_end; /* row after the end of this thread slice */
int i_threadslice_pass; /* which pass of encoding we are on */
x264_threadpool_t *threadpool;
x264_threadpool_t *lookaheadpool;
x264_pthread_mutex_t mutex;
x264_pthread_cond_t cv;

Expand Down Expand Up @@ -915,6 +918,7 @@ struct x264_t

/* Buffers that are allocated per-thread even in sliced threads. */
void *scratch_buffer; /* for any temporary storage that doesn't want repeated malloc */
void *scratch_buffer2; /* if the first one's already in use */
pixel *intra_border_backup[5][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
/* Deblock strength values are stored for each 4x4 partition. In MBAFF
* there are four extra values that need to be stored, located in [4][i]. */
Expand Down
6 changes: 3 additions & 3 deletions common/deblock.c
Expand Up @@ -506,9 +506,9 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
/* Any MB that was coded, or that analysis decided to skip, has quality commensurate with its QP.
* But if deblocking affects neighboring MBs that were force-skipped, blur might accumulate there.
* So reset their effective QP to max, to indicate that lack of guarantee. */
if( h->fdec->mb_info && M32( bs[0][0] ) )
if( h->fenc->mb_info && M32( bs[0][0] ) )
{
#define RESET_EFFECTIVE_QP(xy) h->fdec->effective_qp[xy] |= 0xff * !!(h->fdec->mb_info[xy] & X264_MBINFO_CONSTANT);
#define RESET_EFFECTIVE_QP(xy) h->fdec->effective_qp[xy] |= 0xff * !!(h->fenc->mb_info[xy] & X264_MBINFO_CONSTANT);
RESET_EFFECTIVE_QP(mb_xy);
RESET_EFFECTIVE_QP(h->mb.i_mb_left_xy[0]);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ void x264_frame_deblock_row( x264_t *h, int mb_y )
int intra_deblock = intra_cur || intra_top;

/* This edge has been modified, reset effective qp to max. */
if( h->fdec->mb_info && M32( bs[1][0] ) )
if( h->fenc->mb_info && M32( bs[1][0] ) )
{
RESET_EFFECTIVE_QP(mb_xy);
RESET_EFFECTIVE_QP(h->mb.i_mb_top_xy);
Expand Down
4 changes: 2 additions & 2 deletions common/frame.c
Expand Up @@ -357,8 +357,8 @@ int x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src )
dst->i_pic_struct = src->i_pic_struct;
dst->extra_sei = src->extra_sei;
dst->opaque = src->opaque;
dst->mb_info = src->prop.mb_info;
dst->mb_info_free = src->prop.mb_info_free;
dst->mb_info = h->param.analyse.b_mb_info ? src->prop.mb_info : NULL;
dst->mb_info_free = h->param.analyse.b_mb_info ? src->prop.mb_info_free : NULL;

uint8_t *pix[3];
int stride[3];
Expand Down
4 changes: 4 additions & 0 deletions common/macroblock.c
Expand Up @@ -401,6 +401,9 @@ int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
else
h->scratch_buffer = NULL;

int buf_lookahead_threads = (h->mb.i_mb_height + (4 + 32) * h->param.i_lookahead_threads) * sizeof(int) * 2;
CHECKED_MALLOC( h->scratch_buffer2, buf_lookahead_threads );

return 0;
fail:
return -1;
Expand All @@ -418,6 +421,7 @@ void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
x264_free( h->intra_border_backup[i][j] - 16 );
}
x264_free( h->scratch_buffer );
x264_free( h->scratch_buffer2 );
}

void x264_macroblock_slice_init( x264_t *h )
Expand Down
4 changes: 2 additions & 2 deletions common/threadpool.c
Expand Up @@ -66,7 +66,7 @@ static void x264_threadpool_thread( x264_threadpool_t *pool )
x264_pthread_mutex_unlock( &pool->run.mutex );
if( !job )
continue;
job->ret = job->func( job->arg ); /* execute the function */
job->ret = (void*)x264_stack_align( job->func, job->arg ); /* execute the function */
x264_sync_frame_list_push( &pool->done, (void*)job );
}
}
Expand All @@ -83,7 +83,7 @@ int x264_threadpool_init( x264_threadpool_t **p_pool, int threads,

pool->init_func = init_func;
pool->init_arg = init_arg;
pool->threads = X264_MIN( threads, X264_THREAD_MAX );
pool->threads = threads;

CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );

Expand Down
40 changes: 34 additions & 6 deletions encoder/encoder.c
Expand Up @@ -395,6 +395,15 @@ static void x264_encoder_thread_init( x264_t *h )
x264_cpu_mask_misalign_sse();
#endif
}

static void x264_lookahead_thread_init( x264_t *h )
{
#if HAVE_MMX
/* Misalign mask has to be set separately for each thread. */
if( h->param.cpu&X264_CPU_SSE_MISALIGN )
x264_cpu_mask_misalign_sse();
#endif
}
#endif

/****************************************************************************
Expand Down Expand Up @@ -494,6 +503,9 @@ static int x264_validate_parameters( x264_t *h, int b_open )

if( h->param.i_threads == X264_THREADS_AUTO )
h->param.i_threads = x264_cpu_num_processors() * (h->param.b_sliced_threads?2:3)/2;
if( h->param.i_lookahead_threads == X264_THREADS_AUTO )
h->param.i_lookahead_threads = h->param.i_threads / (h->param.b_sliced_threads?1:6);
int max_sliced_threads = X264_MAX( 1, (h->param.i_height+15)/16 / 4 );
if( h->param.i_threads > 1 )
{
#if !HAVE_THREAD
Expand All @@ -503,14 +515,15 @@ static int x264_validate_parameters( x264_t *h, int b_open )
/* Avoid absurdly small thread slices as they can reduce performance
* and VBV compliance. Capped at an arbitrary 4 rows per thread. */
if( h->param.b_sliced_threads )
{
int max_threads = (h->param.i_height+15)/16 / 4;
h->param.i_threads = X264_MIN( h->param.i_threads, max_threads );
}
h->param.i_threads = X264_MIN( h->param.i_threads, max_sliced_threads );
}
h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
h->param.i_lookahead_threads = x264_clip3( h->param.i_lookahead_threads, 1, X264_MIN( max_sliced_threads, X264_LOOKAHEAD_THREAD_MAX ) );
if( h->param.i_threads == 1 )
{
h->param.b_sliced_threads = 0;
h->param.i_lookahead_threads = 1;
}
h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads;
if( h->i_thread_frames > 1 )
h->param.nalu_process = NULL;
Expand Down Expand Up @@ -1271,10 +1284,19 @@ x264_t *x264_encoder_open( x264_param_t *param )
if( h->param.i_threads > 1 &&
x264_threadpool_init( &h->threadpool, h->param.i_threads, (void*)x264_encoder_thread_init, h ) )
goto fail;
if( h->param.i_lookahead_threads > 1 &&
x264_threadpool_init( &h->lookaheadpool, h->param.i_lookahead_threads, (void*)x264_lookahead_thread_init, h ) )
goto fail;

h->thread[0] = h;
for( int i = 1; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ )
CHECKED_MALLOC( h->thread[i], sizeof(x264_t) );
if( h->param.i_lookahead_threads > 1 )
for( int i = 0; i < h->param.i_lookahead_threads; i++ )
{
CHECKED_MALLOC( h->lookahead_thread[i], sizeof(x264_t) );
*h->lookahead_thread[i] = *h;
}

for( int i = 0; i < h->param.i_threads; i++ )
{
Expand Down Expand Up @@ -3199,8 +3221,8 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,

x264_emms();

if( h->fdec->mb_info_free )
h->fdec->mb_info_free( h->fdec->mb_info );
if( h->fenc->mb_info_free )
h->fenc->mb_info_free( h->fenc->mb_info );

/* generate buffering period sei and insert it into place */
if( h->i_thread_frames > 1 && h->fenc->b_keyframe && h->sps->vui.b_nal_hrd_parameters_present )
Expand Down Expand Up @@ -3459,6 +3481,8 @@ void x264_encoder_close ( x264_t *h )
x264_threadpool_wait_all( h );
if( h->param.i_threads > 1 )
x264_threadpool_delete( h->threadpool );
if( h->param.i_lookahead_threads > 1 )
x264_threadpool_delete( h->lookaheadpool );
if( h->i_thread_frames > 1 )
{
for( int i = 0; i < h->i_thread_frames; i++ )
Expand Down Expand Up @@ -3768,6 +3792,10 @@ void x264_encoder_close ( x264_t *h )
if( h->thread[i]->fref[0][j] && h->thread[i]->fref[0][j]->b_duplicate )
x264_frame_delete( h->thread[i]->fref[0][j] );

if( h->param.i_lookahead_threads > 1 )
for( int i = 0; i < h->param.i_lookahead_threads; i++ )
x264_free( h->lookahead_thread[i] );

for( int i = h->param.i_threads - 1; i >= 0; i-- )
{
x264_frame_t **frame;
Expand Down

0 comments on commit 54929fe

Please sign in to comment.