1 /* $Id: muxcommon.c,v 1.23 2005/03/30 17:27:19 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
24 hb_mux_data_t * mux_data;
30 static hb_track_t * GetTrack( hb_list_t * list, hb_job_t *job )
33 hb_track_t * track = NULL, * track2;
37 for( i = 0; i < hb_list_count( list ); i++ )
39 track2 = hb_list_item( list, i );
40 buf = hb_fifo_see( track2->fifo );
43 // XXX libmkv uses a very simple minded muxer that will fail if the
44 // audio & video are out of sync. To keep them in sync we require
45 // that *all* fifos have a buffer then we take the oldest.
46 // Unfortunately this means we can hang in a deadlock with the
47 // reader process filling the fifos. With the current libmkv
48 // there's no way to avoid occasional deadlocks & we can only
49 // suggest that users evolve to using mp4s.
50 if ( job->mux == HB_MUX_MKV )
55 // To make sure we don't camp on one fifo & prevent the others
56 // from making progress we take the earliest data of all the
57 // data that's currently available but we don't care if some
58 // fifos don't have data.
61 if( !track || buf->start < pts )
70 static void MuxerFunc( void * _mux )
72 hb_mux_t * mux = _mux;
73 hb_job_t * job = mux->job;
74 hb_title_t * title = job->title;
81 hb_mux_object_t * m = NULL;
83 /* Get a real muxer */
84 if( job->pass == 0 || job->pass == 2)
91 m = hb_mux_mp4_init( job );
94 m = hb_mux_avi_init( job );
97 m = hb_mux_ogm_init( job );
100 m = hb_mux_mkv_init( job );
104 /* Wait for one buffer for each track */
105 while( !*job->die && !job->done )
110 if( !hb_fifo_size( job->fifo_mpeg4 ) )
114 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
116 audio = hb_list_item( title->list_audio, i );
117 if( !hb_fifo_size( audio->priv.fifo_out ) )
132 /* Create file, write headers */
133 if( job->pass == 0 || job->pass == 2 )
138 /* Build list of fifos we're interested in */
139 list = hb_list_init();
141 track = calloc( sizeof( hb_track_t ), 1 );
142 track->fifo = job->fifo_mpeg4;
143 track->mux_data = job->mux_data;
144 hb_list_add( list, track );
146 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
148 audio = hb_list_item( title->list_audio, i );
149 track = calloc( sizeof( hb_track_t ), 1 );
150 track->fifo = audio->priv.fifo_out;
151 track->mux_data = audio->priv.mux_data;
152 hb_list_add( list, track );
155 int thread_sleep_interval = 50;
156 while( !*job->die && !job->done )
158 if( !( track = GetTrack( list, job ) ) )
160 hb_snooze( thread_sleep_interval );
164 buf = hb_fifo_get( track->fifo );
165 if( job->pass == 0 || job->pass == 2 )
167 m->mux( m, track->mux_data, buf );
169 track->bytes += buf->size;
170 mux->pts = buf->stop;
172 hb_buffer_close( &buf );
175 if( job->pass == 0 || job->pass == 2 )
178 uint64_t bytes_total, frames_total;
180 #define p state.param.muxing
183 state.state = HB_STATE_MUXING;
185 hb_set_state( job->h, &state );
189 if( !stat( job->file, &sb ) )
191 hb_log( "mux: file size, %lld bytes", (uint64_t) sb.st_size );
195 for( i = 0; i < hb_list_count( list ); i++ )
197 track = hb_list_item( list, i );
198 hb_log( "mux: track %d, %lld bytes, %.2f kbps",
200 90000.0 * track->bytes / mux->pts / 125 );
201 if( !i && ( job->vquality < 0.0 || job->vquality > 1.0 ) )
204 hb_log( "mux: video bitrate error, %+lld bytes",
205 track->bytes - mux->pts * job->vbitrate *
208 bytes_total += track->bytes;
209 frames_total += track->frames;
212 if( bytes_total && frames_total )
214 hb_log( "mux: overhead, %.2f bytes per frame",
215 (float) ( sb.st_size - bytes_total ) /
223 for( i = 0; i < hb_list_count( list ); i++ )
225 track = hb_list_item( list, i );
226 if( track->mux_data )
228 free( track->mux_data );
232 hb_list_close( &list );
237 hb_thread_t * hb_muxer_init( hb_job_t * job )
239 hb_mux_t * mux = calloc( sizeof( hb_mux_t ), 1 );
241 return hb_thread_init( "muxer", MuxerFunc, mux,
242 HB_NORMAL_PRIORITY );