OSDN Git Service

fix sporadic incorrect bitrate calculation of muxed tracks
[handbrake-jp/handbrake-jp-git.git] / libhb / muxcommon.c
1 /* $Id: muxcommon.c,v 1.23 2005/03/30 17:27:19 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 struct hb_mux_object_s
10 {
11     HB_MUX_COMMON;
12 };
13
14 typedef struct
15 {
16     hb_buffer_t **fifo;
17     uint32_t    in;     // number of bufs put into fifo
18     uint32_t    out;    // number of bufs taken out of fifo
19     uint32_t    flen;   // fifo length (must be power of two)
20 } mux_fifo_t;
21
22 typedef struct
23 {
24     hb_mux_data_t * mux_data;
25     uint64_t        frames;
26     uint64_t        bytes;
27     mux_fifo_t      mf;
28 } hb_track_t;
29
30 typedef struct
31 {
32     hb_lock_t       * mutex;
33     int               ref;
34     int               done;
35     hb_mux_object_t * m;
36     double            pts;        // end time of next muxing chunk
37     double            interleave; // size in 90KHz ticks of media chunks we mux
38     uint32_t          ntracks;    // total number of tracks we're muxing
39     uint32_t          eof;        // bitmask of track with eof
40     uint32_t          rdy;        // bitmask of tracks ready to output
41     uint32_t          allEof;     // valid bits in eof (all tracks)
42     uint32_t          allRdy;     // valid bits in rdy (audio & video tracks)
43     hb_track_t      * track[32];  // array of tracks to mux ('ntrack' elements)
44                                   // NOTE- this array could be dynamically 
45                                   // allocated but the eof & rdy logic has to 
46                                   // be changed to handle more than 32 tracks 
47                                   // anyway so we keep it simple and fast.
48 } hb_mux_t;
49
50 struct hb_work_private_s
51 {
52     hb_job_t * job;
53     int        track;
54     hb_mux_t * mux;
55 };
56
57 // The muxer handles two different kinds of media: Video and audio tracks
58 // are continuous: once they start they generate continuous, consecutive
59 // sequence of bufs until they end. The muxer will time align all continuous
60 // media tracks so that their data will be well interleaved in the output file.
61 // (Smooth, low latency playback with minimal player buffering requires that
62 // data that's going to be presented close together in time also be close
63 // together in the output file). Since HB's audio and video encoders run at
64 // different speeds, the time-aligning involves buffering *all* the continuous
65 // media tracks until a frame with a timestamp beyond the current alignment
66 // point arrives on the slowest fifo (usually the video encoder).
67 //
68 // The other kind of media, subtitles, close-captions, vobsubs and
69 // similar tracks, are intermittent. They generate frames sporadically or on
70 // human time scales (seconds) rather than near the video frame rate (milliseconds).
71 // If intermittent sources were treated like continuous sources huge sections of
72 // audio and video would get buffered waiting for the next subtitle to show up.
73 // To keep this from happening the muxer doesn't wait for intermittent tracks
74 // (essentially it assumes that they will always go through the HB processing
75 // pipeline faster than the associated video). They are still time aligned and
76 // interleaved at the appropriate point in the output file.
77
78 // This routine adds another track for the muxer to process. The media input
79 // stream will be read from HandBrake fifo 'fifo'. Buffers read from that
80 // stream will be time-aligned with all the other media streams then passed
81 // to the container-specific 'mux' routine with argument 'mux_data' (see
82 // routine OutputTrackChunk). 'is_continuous' must be 1 for an audio or video
83 // track and 0 otherwise (see above).
84
85 static void add_mux_track( hb_mux_t *mux, hb_mux_data_t *mux_data,
86                            int is_continuous )
87 {
88     int max_tracks = sizeof(mux->track) / sizeof(*(mux->track));
89     if ( mux->ntracks >= max_tracks )
90     {
91         hb_error( "add_mux_track: too many tracks (>%d)", max_tracks );
92         return;
93     }
94
95     hb_track_t *track = calloc( sizeof( hb_track_t ), 1 );
96     track->mux_data = mux_data;
97     track->mf.flen = 8;
98     track->mf.fifo = calloc( sizeof(track->mf.fifo[0]), track->mf.flen );
99
100     int t = mux->ntracks++;
101     mux->track[t] = track;
102     mux->allEof |= 1 << t;
103     mux->allRdy |= is_continuous << t;
104 }
105
106 static void mf_push( hb_mux_t * mux, int tk, hb_buffer_t *buf )
107 {
108     hb_track_t * track = mux->track[tk];
109     uint32_t mask = track->mf.flen - 1;
110     uint32_t in = track->mf.in;
111
112     if ( ( ( in + 2 ) & mask ) == ( track->mf.out & mask ) )
113     {
114         if ( track->mf.flen >= 256 )
115         {
116             mux->rdy = mux->allRdy;
117         }
118     }
119     if ( ( ( in + 1 ) & mask ) == ( track->mf.out & mask ) )
120     {
121         // fifo is full - expand it to double the current size.
122         // This is a bit tricky because when we change the size
123         // it changes the modulus (mask) used to convert the in
124         // and out counters to fifo indices. Since existing items
125         // will be referenced at a new location after the expand
126         // we can't just realloc the fifo. If there were
127         // hundreds of fifo entries it would be worth it to have code
128         // for each of the four possible before/after configurations
129         // but these fifos are small so we just allocate a new chunk
130         // of memory then do element by element copies using the old &
131         // new masks then free the old fifo's memory..
132         track->mf.flen *= 2;
133         uint32_t nmask = track->mf.flen - 1;
134         hb_buffer_t **nfifo = malloc( track->mf.flen * sizeof(*nfifo) );
135         int indx = track->mf.out;
136         while ( indx != track->mf.in )
137         {
138             nfifo[indx & nmask] = track->mf.fifo[indx & mask];
139             ++indx;
140         }
141         free( track->mf.fifo );
142         track->mf.fifo = nfifo;
143         mask = nmask;
144     }
145     track->mf.fifo[in & mask] = buf;
146     track->mf.in = in + 1;
147 }
148
149 static hb_buffer_t *mf_pull( hb_track_t *track )
150 {
151     hb_buffer_t *b = NULL;
152     if ( track->mf.out != track->mf.in )
153     {
154         // the fifo isn't empty
155         b = track->mf.fifo[track->mf.out & (track->mf.flen - 1)];
156         ++track->mf.out;
157     }
158     return b;
159 }
160
161 static hb_buffer_t *mf_peek( hb_track_t *track )
162 {
163     return track->mf.out == track->mf.in ?
164                 NULL : track->mf.fifo[track->mf.out & (track->mf.flen - 1)];
165 }
166
167 static void MoveToInternalFifos( int tk, hb_mux_t *mux, hb_buffer_t * buf )
168 {
169     // move all the buffers on the track's fifo to our internal
170     // fifo so that (a) we don't deadlock in the reader and
171     // (b) we can control how data from multiple tracks is
172     // interleaved in the output file.
173     mf_push( mux, tk, buf );
174     if ( buf->stop >= mux->pts )
175     {
176         // buffer is past our next interleave point so
177         // note that this track is ready to be output.
178         mux->rdy |= ( 1 << tk );
179     }
180 }
181
182 static void OutputTrackChunk( hb_mux_t *mux, hb_track_t *track, hb_mux_object_t *m )
183 {
184     hb_buffer_t *buf;
185
186     while ( ( buf = mf_peek( track ) ) != NULL && buf->start < mux->pts )
187     {
188         buf = mf_pull( track );
189         track->frames += 1;
190         track->bytes  += buf->size;
191         m->mux( m, track->mux_data, buf );
192     }
193 }
194
195 static int muxWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
196                      hb_buffer_t ** buf_out )
197 {
198     hb_work_private_t * pv = w->private_data;
199     hb_job_t    * job = pv->job;
200     hb_mux_t    * mux = pv->mux;
201     hb_track_t  * track;
202     int           i;
203     hb_buffer_t * buf = *buf_in;
204
205     hb_lock( mux->mutex );
206     if ( mux->done )
207     {
208         hb_unlock( mux->mutex );
209         return HB_WORK_DONE;
210     }
211
212     if ( buf->size <= 0 )
213     {
214         // EOF - mark this track as done
215         hb_buffer_close( &buf );
216         mux->eof |= ( 1 << pv->track );
217         mux->rdy |= ( 1 << pv->track );
218     }
219     else if ( ( job->pass != 0 && job->pass != 2 ) ||
220               ( mux->eof & (1 << pv->track) ) )
221     {
222         hb_buffer_close( &buf );
223     }
224     else
225     {
226         MoveToInternalFifos( pv->track, mux, buf );
227     }
228     *buf_in = NULL;
229
230     if ( ( mux->rdy & mux->allRdy ) != mux->allRdy )
231     {
232         hb_unlock( mux->mutex );
233         return HB_WORK_OK;
234     }
235
236     // all tracks have at least 'interleave' ticks of data. Output
237     // all that we can in 'interleave' size chunks.
238     while ( ( mux->rdy & mux->allRdy ) == mux->allRdy )
239     {
240         for ( i = 0; i < mux->ntracks; ++i )
241         {
242             track = mux->track[i];
243             OutputTrackChunk( mux, track, mux->m );
244
245             // if the track is at eof or still has data that's past
246             // our next interleave point then leave it marked as rdy.
247             // Otherwise clear rdy.
248             if ( ( mux->eof & (1 << i) ) == 0 &&
249                  ( track->mf.out == track->mf.in ||
250                    track->mf.fifo[(track->mf.in-1) & (track->mf.flen-1)]->stop
251                      < mux->pts + mux->interleave ) )
252             {
253                 mux->rdy &=~ ( 1 << i );
254             }
255         }
256
257         // if all the tracks are at eof we're just purging their
258         // remaining data -- keep going until all internal fifos are empty.
259         if ( mux->eof == mux->allEof )
260         {
261             for ( i = 0; i < mux->ntracks; ++i )
262             {
263                 if ( mux->track[i]->mf.out != mux->track[i]->mf.in )
264                 {
265                     break;
266                 }
267             }
268             if ( i >= mux->ntracks )
269             {
270                 mux->done = 1;
271                 hb_unlock( mux->mutex );
272                 return HB_WORK_DONE;
273             }
274         }
275         mux->pts += mux->interleave;
276     }
277     hb_unlock( mux->mutex );
278     return HB_WORK_OK;
279 }
280
281 void muxClose( hb_work_object_t * w )
282 {
283     hb_work_private_t * pv = w->private_data;
284     hb_mux_t    * mux = pv->mux;
285     hb_job_t    * job = pv->job;
286     hb_track_t  * track;
287     int           i;
288
289     hb_lock( mux->mutex );
290     if ( --mux->ref == 0 )
291     {
292         if( mux->m )
293         {
294             mux->m->end( mux->m );
295             free( mux->m );
296         }
297
298         // we're all done muxing -- print final stats and cleanup.
299         if( job->pass == 0 || job->pass == 2 )
300         {
301             struct stat sb;
302             uint64_t bytes_total, frames_total;
303
304             /* Update the UI */
305             hb_state_t state;
306             state.state = HB_STATE_MUXING;
307             state.param.muxing.progress = 0;
308             hb_set_state( job->h, &state );
309
310             if( !stat( job->file, &sb ) )
311             {
312                 hb_deep_log( 2, "mux: file size, %"PRId64" bytes", (uint64_t) sb.st_size );
313
314                 bytes_total  = 0;
315                 frames_total = 0;
316                 for( i = 0; i < mux->ntracks; ++i )
317                 {
318                     track = mux->track[i];
319                     hb_log( "mux: track %d, %"PRId64" frames, %"PRId64" bytes, %.2f kbps, fifo %d",
320                             i, track->frames, track->bytes,
321                             90000.0 * track->bytes / mux->pts / 125,
322                             track->mf.flen );
323                     if( !i && ( job->vquality < 0.0 || job->vquality > 1.0 ) )
324                     {
325                         /* Video */
326                         hb_deep_log( 2, "mux: video bitrate error, %+"PRId64" bytes",
327                                 (int64_t)(track->bytes - mux->pts * job->vbitrate * 125 / 90000) );
328                     }
329                     bytes_total  += track->bytes;
330                     frames_total += track->frames;
331                 }
332
333                 if( bytes_total && frames_total )
334                 {
335                     hb_deep_log( 2, "mux: overhead, %.2f bytes per frame",
336                             (float) ( sb.st_size - bytes_total ) /
337                             frames_total );
338                 }
339             }
340         }
341     
342         for( i = 0; i < mux->ntracks; ++i )
343         {
344             track = mux->track[i];
345             if( track->mux_data )
346             {
347                 free( track->mux_data );
348                 free( track->mf.fifo );
349             }
350             free( track );
351         }
352         hb_unlock( mux->mutex );
353         hb_lock_close( &mux->mutex );
354         free( mux );
355     }
356     else
357     {
358         hb_unlock( mux->mutex );
359     }
360     free( pv );
361     w->private_data = NULL;
362 }
363
364 static void mux_loop( void * _w )
365 {
366     hb_work_object_t  * w = _w;
367     hb_work_private_t * pv = w->private_data;
368     hb_job_t          * job = pv->job;
369     hb_buffer_t       * buf_in;
370
371     while ( !*job->die && w->status != HB_WORK_DONE )
372     {
373         buf_in = hb_fifo_get_wait( w->fifo_in );
374         if ( pv->mux->done )
375             break;
376         if ( buf_in == NULL )
377             continue;
378         if ( *job->die )
379         {
380             if( buf_in )
381             {
382                 hb_buffer_close( &buf_in );
383             }
384             break;
385         }
386
387         w->status = w->work( w, &buf_in, NULL );
388     }
389 }
390
391 hb_work_object_t * hb_muxer_init( hb_job_t * job )
392 {
393     hb_title_t  * title = job->title;
394     int           i;
395     hb_mux_t    * mux = calloc( sizeof( hb_mux_t ), 1 );
396     hb_work_object_t  * w;
397     hb_work_object_t  * muxer;
398
399     mux->mutex = hb_lock_init();
400
401     // set up to interleave track data in blocks of 1 video frame time.
402     // (the best case for buffering and playout latency). The container-
403     // specific muxers can reblock this into bigger chunks if necessary.
404     mux->interleave = 90000. * (double)job->vrate_base / (double)job->vrate;
405     mux->pts = mux->interleave;
406
407     /* Get a real muxer */
408     if( job->pass == 0 || job->pass == 2)
409     {
410         switch( job->mux )
411         {
412         case HB_MUX_MP4:
413         case HB_MUX_PSP:
414         case HB_MUX_IPOD:
415             mux->m = hb_mux_mp4_init( job );
416             break;
417         case HB_MUX_AVI:
418             mux->m = hb_mux_avi_init( job );
419             break;
420         case HB_MUX_OGM:
421             mux->m = hb_mux_ogm_init( job );
422             break;
423         case HB_MUX_MKV:
424             mux->m = hb_mux_mkv_init( job );
425             break;
426         default:
427             hb_error( "No muxer selected, exiting" );
428             *job->die = 1;
429             return NULL;
430         }
431         /* Create file, write headers */
432         if( mux->m )
433         {
434             mux->m->init( mux->m );
435         }
436     }
437
438     /* Initialize the work objects that will receive fifo data */
439
440     muxer = hb_get_work( WORK_MUX );
441     muxer->private_data = calloc( sizeof( hb_work_private_t ), 1 );
442     muxer->private_data->job = job;
443     muxer->private_data->mux = mux;
444     mux->ref++;
445     muxer->private_data->track = mux->ntracks;
446     muxer->fifo_in = job->fifo_mpeg4;
447     add_mux_track( mux, job->mux_data, 1 );
448     muxer->done = &job->done;
449     muxer->thread = hb_thread_init( muxer->name, mux_loop, muxer, HB_NORMAL_PRIORITY );
450
451     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
452     {
453         hb_audio_t  *audio = hb_list_item( title->list_audio, i );
454
455         w = hb_get_work( WORK_MUX );
456         w->private_data = calloc( sizeof( hb_work_private_t ), 1 );
457         w->private_data->job = job;
458         w->private_data->mux = mux;
459         mux->ref++;
460         w->private_data->track = mux->ntracks;
461         w->fifo_in = audio->priv.fifo_out;
462         add_mux_track( mux, audio->priv.mux_data, 1 );
463         w->done = &job->done;
464         hb_list_add( job->list_work, w );
465         w->thread = hb_thread_init( w->name, mux_loop, w, HB_NORMAL_PRIORITY );
466     }
467
468     for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
469     {
470         hb_subtitle_t  *subtitle = hb_list_item( title->list_subtitle, i );
471
472         if (subtitle->config.dest != PASSTHRUSUB)
473             continue;
474
475         w = hb_get_work( WORK_MUX );
476         w->private_data = calloc( sizeof( hb_work_private_t ), 1 );
477         w->private_data->job = job;
478         w->private_data->mux = mux;
479         mux->ref++;
480         w->private_data->track = mux->ntracks;
481         w->fifo_in = subtitle->fifo_out;
482         add_mux_track( mux, subtitle->mux_data, 0 );
483         w->done = &job->done;
484         hb_list_add( job->list_work, w );
485         w->thread = hb_thread_init( w->name, mux_loop, w, HB_NORMAL_PRIORITY );
486     }
487     return muxer;
488 }
489
490 // muxInit does nothing because the muxer has a special initializer
491 // that takes care of initializing all muxer work objects
492 static int muxInit( hb_work_object_t * w, hb_job_t * job )
493 {
494     return 0;
495 }
496
497 hb_work_object_t hb_muxer =
498 {
499     WORK_MUX,
500     "Muxer",
501     muxInit,
502     muxWork,
503     muxClose
504 };
505