X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=libhb%2Fmuxcommon.c;h=cb61739acc9df634077094d4cf21b2fcb5082a33;hb=4b72a63eb61a01275493c4bfb51ba02152d1c5e1;hp=1d27df64ee38c097b531775c9073e0621dc86b4c;hpb=9d5a277e5b03d410bf67524eb32f3da065c902a5;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/libhb/muxcommon.c b/libhb/muxcommon.c index 1d27df64..cb61739a 100644 --- a/libhb/muxcommon.c +++ b/libhb/muxcommon.c @@ -6,6 +6,8 @@ #include "hb.h" +#define MAX_BUFFERING (1024*1024*50) + struct hb_mux_object_s { HB_MUX_COMMON; @@ -25,6 +27,7 @@ typedef struct uint64_t frames; uint64_t bytes; mux_fifo_t mf; + int buffered_size; } hb_track_t; typedef struct @@ -103,18 +106,23 @@ static void add_mux_track( hb_mux_t *mux, hb_mux_data_t *mux_data, mux->allRdy |= is_continuous << t; } +static int mf_full( hb_track_t * track ) +{ + if ( track->buffered_size > MAX_BUFFERING ) + return 1; + + return 0; +} + static void mf_push( hb_mux_t * mux, int tk, hb_buffer_t *buf ) { hb_track_t * track = mux->track[tk]; uint32_t mask = track->mf.flen - 1; uint32_t in = track->mf.in; - if ( ( ( in + 2 ) & mask ) == ( track->mf.out & mask ) ) + if ( track->buffered_size > MAX_BUFFERING ) { - if ( track->mf.flen >= 256 ) - { - mux->rdy = mux->allRdy; - } + mux->rdy = mux->allRdy; } if ( ( ( in + 1 ) & mask ) == ( track->mf.out & mask ) ) { @@ -144,6 +152,7 @@ static void mf_push( hb_mux_t * mux, int tk, hb_buffer_t *buf ) } track->mf.fifo[in & mask] = buf; track->mf.in = in + 1; + track->buffered_size += buf->alloc; } static hb_buffer_t *mf_pull( hb_track_t *track ) @@ -154,6 +163,8 @@ static hb_buffer_t *mf_pull( hb_track_t *track ) // the fifo isn't empty b = track->mf.fifo[track->mf.out & (track->mf.flen - 1)]; ++track->mf.out; + + track->buffered_size -= b->alloc; } return b; } @@ -233,14 +244,23 @@ static int muxWork( hb_work_object_t * w, hb_buffer_t ** buf_in, return HB_WORK_OK; } + int more = mux->rdy; // all tracks have at least 'interleave' ticks of data. Output // all that we can in 'interleave' size chunks. - while ( ( mux->rdy & mux->allRdy ) == mux->allRdy ) + while ( ( mux->rdy & mux->allRdy ) == mux->allRdy && more ) { + more = 0; for ( i = 0; i < mux->ntracks; ++i ) { track = mux->track[i]; OutputTrackChunk( mux, track, mux->m ); + if ( mf_full( track ) ) + { + // If the track's fifo is still full, advance + // the currint interleave point and try again. + mux->rdy = mux->allRdy; + break; + } // if the track is at eof or still has data that's past // our next interleave point then leave it marked as rdy. @@ -252,6 +272,10 @@ static int muxWork( hb_work_object_t * w, hb_buffer_t ** buf_in, { mux->rdy &=~ ( 1 << i ); } + if ( track->mf.out != track->mf.in ) + { + more |= ( 1 << i ); + } } // if all the tracks are at eof we're just purging their @@ -289,6 +313,19 @@ void muxClose( hb_work_object_t * w ) hb_lock( mux->mutex ); if ( --mux->ref == 0 ) { + // Update state before closing muxer. Closing the muxer + // may initiate optimization which can take a while and + // we want the muxing state to be visible while this is + // happening. + if( job->pass == 0 || job->pass == 2 ) + { + /* Update the UI */ + hb_state_t state; + state.state = HB_STATE_MUXING; + state.param.muxing.progress = 0; + hb_set_state( job->h, &state ); + } + if( mux->m ) { mux->m->end( mux->m ); @@ -301,12 +338,6 @@ void muxClose( hb_work_object_t * w ) struct stat sb; uint64_t bytes_total, frames_total; - /* Update the UI */ - hb_state_t state; - state.state = HB_STATE_MUXING; - state.param.muxing.progress = 0; - hb_set_state( job->h, &state ); - if( !stat( job->file, &sb ) ) { hb_deep_log( 2, "mux: file size, %"PRId64" bytes", (uint64_t) sb.st_size ); @@ -341,7 +372,12 @@ void muxClose( hb_work_object_t * w ) for( i = 0; i < mux->ntracks; ++i ) { + hb_buffer_t * b; track = mux->track[i]; + while ( (b = mf_pull( track )) != NULL ) + { + hb_buffer_close( &b ); + } if( track->mux_data ) { free( track->mux_data ); @@ -385,6 +421,10 @@ static void mux_loop( void * _w ) } w->status = w->work( w, &buf_in, NULL ); + if( buf_in ) + { + hb_buffer_close( &buf_in ); + } } } @@ -445,8 +485,7 @@ hb_work_object_t * hb_muxer_init( hb_job_t * job ) muxer->private_data->track = mux->ntracks; muxer->fifo_in = job->fifo_mpeg4; add_mux_track( mux, job->mux_data, 1 ); - muxer->done = &job->done; - muxer->thread = hb_thread_init( muxer->name, mux_loop, muxer, HB_NORMAL_PRIORITY ); + muxer->done = &muxer->private_data->mux->done; for( i = 0; i < hb_list_count( title->list_audio ); i++ ) {