OSDN Git Service

Change the fifo size from being statically tuned for a Mac Pro with 4 CPUs to dynamic...
[handbrake-jp/handbrake-jp-git.git] / libhb / work.c
1 /* $Id: work.c,v 1.43 2005/03/17 16:38:49 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8 #include "a52dec/a52.h"
9 #include "dca.h"
10
11 typedef struct
12 {
13     hb_list_t * jobs;
14     hb_job_t  ** current_job;
15     int         cpu_count;
16     int       * error;
17     volatile int * die;
18
19 } hb_work_t;
20
21 static void work_func();
22 static void do_job( hb_job_t *, int cpu_count );
23 static void work_loop( void * );
24
25 #define FIFO_CPU_MULT 8
26
27 /**
28  * Allocates work object and launches work thread with work_func.
29  * @param jobs Handle to hb_list_t.
30  * @param cpu_count Humber of CPUs found in system.
31  * @param die Handle to user inititated exit indicator.
32  * @param error Handle to error indicator.
33  */
34 hb_thread_t * hb_work_init( hb_list_t * jobs, int cpu_count,
35                             volatile int * die, int * error, hb_job_t ** job )
36 {
37     hb_work_t * work = calloc( sizeof( hb_work_t ), 1 );
38
39     work->jobs      = jobs;
40     work->current_job = job;
41     work->cpu_count = cpu_count;
42     work->die       = die;
43     work->error     = error;
44
45     return hb_thread_init( "work", work_func, work, HB_LOW_PRIORITY );
46 }
47
48 /**
49  * Iterates through job list and calls do_job for each job.
50  * @param _work Handle work object.
51  */
52 static void work_func( void * _work )
53 {
54     hb_work_t  * work = _work;
55     hb_job_t   * job;
56
57     hb_log( "%d job(s) to process", hb_list_count( work->jobs ) );
58
59     while( !*work->die && ( job = hb_list_item( work->jobs, 0 ) ) )
60     {
61         hb_list_rem( work->jobs, job );
62         job->die = work->die;
63         *(work->current_job) = job;
64         do_job( job, work->cpu_count );
65         *(work->current_job) = NULL;
66     }
67
68     *(work->error) = HB_ERROR_NONE;
69
70     free( work );
71 }
72
73 static hb_work_object_t * getWork( int id )
74 {
75     hb_work_object_t * w;
76     for( w = hb_objects; w; w = w->next )
77     {
78         if( w->id == id )
79         {
80             return w;
81         }
82     }
83     return NULL;
84 }
85
86 /**
87  * Job initialization rountine.
88  * Initializes fifos.
89  * Creates work objects for synchronizer, video decoder, video renderer, video decoder, audio decoder, audio encoder, reader, muxer.
90  * Launches thread for each work object with work_loop.
91  * Loops while monitoring status of work threads and fifos.
92  * Exits loop when conversion is done and fifos are empty.
93  * Closes threads and frees fifos.
94  * @param job Handle work hb_job_t.
95  * @param cpu_count number of CPUs found in system.
96  */
97 static void do_job( hb_job_t * job, int cpu_count )
98 {
99     hb_title_t    * title;
100     int             i, j;
101     hb_work_object_t * w;
102     
103     /* FIXME: This feels really hackish, anything better? */
104     hb_work_object_t * audio_w = NULL;
105     hb_work_object_t * sub_w = NULL;
106
107     hb_audio_t   * audio;
108     hb_subtitle_t * subtitle;
109     int done;
110     unsigned int subtitle_highest = 0;
111     unsigned int subtitle_highest_id = 0;
112     unsigned int subtitle_lowest = -1;
113     unsigned int subtitle_lowest_id = 0;
114     unsigned int subtitle_forced_id = 0;
115     unsigned int subtitle_hit = 0;
116
117     title = job->title;
118
119     job->list_work = hb_list_init();
120
121     hb_log( "starting job" );
122     hb_log( " + device %s", title->dvd );
123     hb_log( " + title %d, chapter(s) %d to %d", title->index,
124             job->chapter_start, job->chapter_end );
125     if ( job->pixel_ratio == 1 )
126     {
127         /* Correct the geometry of the output movie when using PixelRatio */
128         job->height=title->height-job->crop[0]-job->crop[1];
129         job->width=title->width-job->crop[2]-job->crop[3];
130     }
131
132         /* Keep width and height within these boundaries */
133         if (job->maxHeight && (job->height > job->maxHeight) )
134         {
135                 job->height = job->maxHeight;
136                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
137                 hb_log("Height out of bounds, scaling down to %i", job->maxHeight);
138                 hb_log("New dimensions %i * %i", job->width, job->height);
139         }
140         if (job->maxWidth && (job->width > job->maxWidth) )
141         {
142                 job->width = job->maxWidth;
143                 hb_fix_aspect( job, HB_KEEP_WIDTH );   
144                 hb_log("Width out of bounds, scaling down to %i", job->maxWidth);
145                 hb_log("New dimensions %i * %i", job->width, job->height);
146         }
147
148     hb_log( " + %dx%d -> %dx%d, crop %d/%d/%d/%d",
149             title->width, title->height, job->width, job->height,
150             job->crop[0], job->crop[1], job->crop[2], job->crop[3] );
151     hb_log( " + grayscale %s", job->grayscale ? "on" : "off" );
152     
153     if( job->filters )
154     {
155         hb_log(" + filters");
156         for( i = 0; i < hb_list_count( job->filters ); i++ )
157         {
158             hb_filter_object_t * filter = hb_list_item( job->filters, i );
159             if (filter->settings)
160                 hb_log("   + %s (%s)", filter->name, filter->settings);
161             else
162                 hb_log("   + %s (default settings)", filter->name);
163         }
164     }
165     
166     if( job->vquality >= 0.0 && job->vquality <= 1.0 )
167     {
168         hb_log( " + %.3f fps, video quality %.2f", (float) job->vrate /
169                 (float) job->vrate_base, job->vquality );
170     }
171     else
172     {
173         hb_log( " + %.3f fps, video bitrate %d kbps, pass %d",
174                 (float) job->vrate / (float) job->vrate_base,
175                 job->vbitrate, job->pass );
176     }
177         hb_log (" + PixelRatio: %d, width:%d, height: %d",job->pixel_ratio,job->width, job->height);
178     job->fifo_mpeg2  = hb_fifo_init( 2048 );
179     job->fifo_raw    = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
180     job->fifo_sync   = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
181     job->fifo_render = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
182     job->fifo_mpeg4  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
183
184     /* Synchronization */
185     hb_list_add( job->list_work, ( w = getWork( WORK_SYNC ) ) );
186     w->fifo_in  = NULL;
187     w->fifo_out = NULL;
188
189     /* Video decoder */
190     hb_list_add( job->list_work, ( w = getWork( WORK_DECMPEG2 ) ) );
191     w->fifo_in  = job->fifo_mpeg2;
192     w->fifo_out = job->fifo_raw;
193
194     /* Video renderer */
195     hb_list_add( job->list_work, ( w = getWork( WORK_RENDER ) ) );
196     w->fifo_in  = job->fifo_sync;
197     w->fifo_out = job->fifo_render;
198
199     /* Video encoder */
200     switch( job->vcodec )
201     {
202         case HB_VCODEC_FFMPEG:
203             hb_log( " + encoder FFmpeg" );
204             w = getWork( WORK_ENCAVCODEC );
205             break;
206         case HB_VCODEC_XVID:
207             hb_log( " + encoder XviD" );
208             w = getWork( WORK_ENCXVID );
209             break;
210         case HB_VCODEC_X264:
211             hb_log( " + encoder x264" );
212             w = getWork( WORK_ENCX264 );
213             break;
214     }
215     w->fifo_in  = job->fifo_render;
216     w->fifo_out = job->fifo_mpeg4;
217     w->config   = &job->config;
218     
219     hb_list_add( job->list_work, w );
220
221     if( job->select_subtitle && !job->indepth_scan ) 
222     {
223         /*
224          * Must be second pass of a two pass with subtitle scan enabled, so
225          * add the subtitle that we found on the first pass for use in this
226          * pass.
227          */
228         if (*(job->select_subtitle))
229         {
230             hb_list_add( title->list_subtitle, *( job->select_subtitle ) );
231         }
232     }
233
234     for( i=0; i < hb_list_count(title->list_subtitle); i++ ) 
235     {
236         subtitle =  hb_list_item( title->list_subtitle, i );
237
238         if( subtitle )
239         {
240             hb_log( " + subtitle %x, %s", subtitle->id, subtitle->lang );
241             
242             subtitle->fifo_in  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
243             subtitle->fifo_raw = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
244             
245             /*
246              * Disable forced subtitles if we didn't find any in the scan
247              * so that we display normal subtitles instead.
248              *
249              * select_subtitle implies that we did a scan.
250              */
251             if( !job->indepth_scan && job->subtitle_force && 
252                 job->select_subtitle ) 
253             {
254                 if( subtitle->forced_hits == 0 )
255                 {
256                     job->subtitle_force = 0;
257                 }
258             }
259
260             if (!job->indepth_scan || job->subtitle_force) {
261                 /*
262                  * Don't add threads for subtitles when we are scanning, unless
263                  * looking for forced subtitles.
264                  */
265                 if( sub_w != NULL )
266                 { 
267                     /*
268                      * Need to copy the prior subtitle structure so that we
269                      * don't overwrite the fifos.
270                      */
271                     sub_w = calloc( sizeof( hb_work_object_t ), 1 );
272                     sub_w = memcpy( sub_w, w, sizeof( hb_work_object_t ));
273                 } else {
274                     w = sub_w = getWork( WORK_DECSUB );
275                 }
276                 hb_list_add( job->list_work, sub_w );
277                 sub_w->fifo_in  = subtitle->fifo_in;
278                 sub_w->fifo_out = subtitle->fifo_raw;
279             }
280         }
281     }
282
283     if( job->acodec & HB_ACODEC_AC3 )
284     {
285         hb_log( " + audio AC3 passthrough" );
286     }
287     else
288     {
289         hb_log( " + audio %d kbps, %d Hz", job->abitrate, job->arate );
290         hb_log( " + encoder %s", ( job->acodec & HB_ACODEC_FAAC ) ?
291                 "faac" : ( ( job->acodec & HB_ACODEC_LAME ) ? "lame" :
292                 "vorbis" ) );
293     }
294
295     /* if we are doing AC3 passthru, then remove any non-AC3 audios from the job */
296     /* otherwise, Bad Things will happen */
297     for( i = 0; i < hb_list_count( title->list_audio ); )
298     {
299         audio = hb_list_item( title->list_audio, i );
300         if( ( job->acodec & HB_ACODEC_AC3 ) && ( audio->codec != HB_ACODEC_AC3 ) )
301         {
302             hb_list_rem( title->list_audio, audio );
303             free( audio );
304             continue;
305         }
306         i++;
307     }
308
309     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
310     {
311         audio = hb_list_item( title->list_audio, i );
312         hb_log( "   + %x, %s", audio->id, audio->lang );
313                         
314                 /* sense-check the current mixdown options */
315
316                 /* log the requested mixdown */
317                 for (j = 0; j < hb_audio_mixdowns_count; j++) {
318                         if (hb_audio_mixdowns[j].amixdown == job->audio_mixdowns[i]) {
319                                 hb_log( "     + Requested mixdown: %s (%s)", hb_audio_mixdowns[j].human_readable_name, hb_audio_mixdowns[j].internal_name );
320                                 break;
321                         }
322                 }
323
324         /* sense-check the requested mixdown */
325
326         /* audioCodecsSupportMono and audioCodecsSupport6Ch are the same for now,
327            but this may change in the future, so they are separated for flexibility */
328         int audioCodecsSupportMono = ((audio->codec == HB_ACODEC_AC3 ||
329             audio->codec == HB_ACODEC_DCA) && (job->acodec == HB_ACODEC_FAAC || job->acodec == HB_ACODEC_VORBIS));
330         int audioCodecsSupport6Ch =  ((audio->codec == HB_ACODEC_AC3 ||
331             audio->codec == HB_ACODEC_DCA) && (job->acodec == HB_ACODEC_FAAC || job->acodec == HB_ACODEC_VORBIS));
332
333         /* find out what the format of our source audio is */
334         switch (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK) {
335         
336             /* mono sources */
337             case HB_INPUT_CH_LAYOUT_MONO:
338                 /* regardless of what stereo mixdown we've requested, a mono source always get mixed down
339                 to mono if we can, and mixed up to stereo if we can't */
340                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 1) {
341                     job->audio_mixdowns[i] = HB_AMIXDOWN_MONO;
342                 } else {
343                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
344                 }
345                 break;
346
347             /* stereo input */
348             case HB_INPUT_CH_LAYOUT_STEREO:
349                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
350                 /* use stereo if not supported */
351                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
352                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
353                 /* otherwise, preserve stereo regardless of if we requested something higher */
354                 } else if (job->audio_mixdowns[i] > HB_AMIXDOWN_STEREO) {
355                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
356                 }
357                 break;
358
359             /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
360             /* the A52 flags don't allow for a way to distinguish between DPL1 and DPL2 on a DVD,
361                so we always assume a DPL1 source for A52_DOLBY */
362             case HB_INPUT_CH_LAYOUT_DOLBY:
363                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
364                 /* preserve dolby if not supported */
365                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
366                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
367                 /* otherwise, preserve dolby even if we requested something higher */
368                 /* a stereo mixdown will still be honoured here */
369                 } else if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBY) {
370                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
371                 }
372                 break;
373
374             /* 3F/2R input */
375             case HB_INPUT_CH_LAYOUT_3F2R:
376                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
377                 /* use dpl2 if not supported */
378                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
379                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
380                 } else {
381                     /* check if we have 3F2R input and also have an LFE - i.e. we have a 5.1 source) */
382                     if (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE) {
383                         /* we have a 5.1 source */
384                         /* if we requested 6ch, but our audio format doesn't support it, then mix to DPLII instead */
385                         if (job->audio_mixdowns[i] == HB_AMIXDOWN_6CH && audioCodecsSupport6Ch == 0) {
386                             job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
387                         }
388                     } else {
389                         /* we have a 5.0 source, so we can't do 6ch conversion
390                         default to DPL II instead */
391                         if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBYPLII) {
392                             job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
393                         }
394                     }
395                 }
396                 /* all other mixdowns will have been preserved here */
397                 break;
398
399             /* 3F/1R input */
400             case HB_INPUT_CH_LAYOUT_3F1R:
401                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
402                 /* use dpl1 if not supported */
403                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
404                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
405                 } else {
406                     /* we have a 4.0 or 4.1 source, so we can't do DPLII or 6ch conversion
407                     default to DPL I instead */
408                     if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBY) {
409                         job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
410                     }
411                 }
412                 /* all other mixdowns will have been preserved here */
413                 break;
414
415             default:
416                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
417                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 1) {
418                     job->audio_mixdowns[i] = HB_AMIXDOWN_MONO;
419                 /* mix everything else down to stereo */
420                 } else {
421                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
422                 }
423
424         }
425
426                 /* log the output mixdown */
427                 for (j = 0; j < hb_audio_mixdowns_count; j++) {
428                         if (hb_audio_mixdowns[j].amixdown == job->audio_mixdowns[i]) {
429                                 hb_log( "     + Actual mixdown: %s (%s)", hb_audio_mixdowns[j].human_readable_name, hb_audio_mixdowns[j].internal_name );
430                                 break;
431                         }
432                 }
433
434                 /* we now know we have a valid mixdown for the input source and the audio output format */
435                 /* remember the mixdown for this track */
436                 audio->amixdown = job->audio_mixdowns[i];
437
438         audio->config.vorbis.language = audio->lang_simple;
439
440                 /* set up the audio work structures */
441         audio->fifo_in   = hb_fifo_init( 2048 );
442         audio->fifo_raw  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
443         audio->fifo_sync = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
444         audio->fifo_out  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
445
446         switch( audio->codec )
447         {
448             case HB_ACODEC_AC3:
449                 w = getWork( WORK_DECA52 );
450                 break;
451             case HB_ACODEC_DCA:
452                 w = getWork( WORK_DECDCA );
453                 break;
454             case HB_ACODEC_MPGA:
455                 w = getWork( WORK_DECAVCODEC );
456                 break;
457             case HB_ACODEC_LPCM:
458                 w = getWork( WORK_DECLPCM );
459                 break;
460         }
461         w->fifo_in       = audio->fifo_in;
462         w->fifo_out      = audio->fifo_raw;
463         w->config        = &audio->config;
464         w->amixdown      = audio->amixdown;
465         w->source_acodec = audio->codec;
466         
467         /* FIXME: This feels really hackish, anything better? */
468         audio_w = calloc( sizeof( hb_work_object_t ), 1 );
469         audio_w = memcpy( audio_w, w, sizeof( hb_work_object_t ));
470         
471         hb_list_add( job->list_work, audio_w );
472
473         switch( job->acodec )
474         {
475             case HB_ACODEC_FAAC:
476                 w = getWork( WORK_ENCFAAC );
477                 break;
478             case HB_ACODEC_LAME:
479                 w = getWork( WORK_ENCLAME );
480                 break;
481             case HB_ACODEC_VORBIS:
482                 w = getWork( WORK_ENCVORBIS );
483                 break;
484         }
485
486         if( job->acodec != HB_ACODEC_AC3 )
487         {
488             w->fifo_in       = audio->fifo_sync;
489             w->fifo_out      = audio->fifo_out;
490             w->config        = &audio->config;
491             w->amixdown      = audio->amixdown;
492             w->source_acodec = audio->codec;
493             
494             /* FIXME: This feels really hackish, anything better? */
495             audio_w = calloc( sizeof( hb_work_object_t ), 1 );
496             audio_w = memcpy( audio_w, w, sizeof( hb_work_object_t ));
497         
498             hb_list_add( job->list_work, audio_w );
499         }
500
501
502     }
503
504     /* Init read & write threads */
505     job->reader = hb_reader_init( job );
506
507     hb_log( " + output: %s", job->file );
508     job->muxer = hb_muxer_init( job );
509
510     job->done = 0;
511
512     /* Launch processing threads */
513     for( i = 1; i < hb_list_count( job->list_work ); i++ )
514     {
515         w = hb_list_item( job->list_work, i );
516         w->done = &job->done;
517         w->thread_sleep_interval = 10;
518         w->init( w, job );
519         w->thread = hb_thread_init( w->name, work_loop, w,
520                                     HB_LOW_PRIORITY );
521     }
522
523     done = 0;
524     w = hb_list_item( job->list_work, 0 );
525     w->thread_sleep_interval = 50;
526     w->init( w, job );
527     while( !*job->die )
528     {
529         if( w->work( w, NULL, NULL ) == HB_WORK_DONE )
530         {
531             done = 1;
532         }
533         if( done &&
534             !hb_fifo_size( job->fifo_sync ) &&
535             !hb_fifo_size( job->fifo_render ) &&
536             !hb_fifo_size( job->fifo_mpeg4 ) )
537         {
538             break;
539         }
540         hb_snooze( w->thread_sleep_interval );
541     }
542     hb_list_rem( job->list_work, w );
543     w->close( w );
544     job->done = 1;
545
546     /* Close work objects */
547     while( ( w = hb_list_item( job->list_work, 0 ) ) )
548     {
549         hb_list_rem( job->list_work, w );
550         hb_thread_close( &w->thread );
551         w->close( w );
552         
553         /* FIXME: This feels really hackish, anything better? */
554         if ( w->id == WORK_DECA52 ||
555              w->id == WORK_DECDCA ||
556              w->id == WORK_DECLPCM ||
557              w->id == WORK_ENCFAAC ||
558              w->id == WORK_ENCLAME ||
559              w->id == WORK_ENCVORBIS )
560         {
561             free( w );
562             w = NULL;
563         }
564     }
565     
566     hb_list_close( &job->list_work );
567
568     /* Stop read & write threads */
569     hb_thread_close( &job->reader );
570     hb_thread_close( &job->muxer );
571
572     /* Close fifos */
573     hb_fifo_close( &job->fifo_mpeg2 );
574     hb_fifo_close( &job->fifo_raw );
575     hb_fifo_close( &job->fifo_sync );
576     hb_fifo_close( &job->fifo_render );
577     hb_fifo_close( &job->fifo_mpeg4 );
578
579     for (i=0; i < hb_list_count(title->list_subtitle); i++) {
580         subtitle =  hb_list_item( title->list_subtitle, i);
581         if( subtitle )
582         {
583             hb_fifo_close( &subtitle->fifo_in );
584             hb_fifo_close( &subtitle->fifo_raw );
585         }
586     }
587     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
588     {
589         audio = hb_list_item( title->list_audio, i );
590         hb_fifo_close( &audio->fifo_in );
591         hb_fifo_close( &audio->fifo_raw );
592         hb_fifo_close( &audio->fifo_sync );
593         hb_fifo_close( &audio->fifo_out );
594     }
595
596     if( job->indepth_scan )
597     {
598         /*
599          * Before closing the title print out our subtitle stats if we need to
600          * Find the highest and lowest.
601          */
602         for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) 
603         {
604             subtitle =  hb_list_item( title->list_subtitle, i );
605             hb_log( "Subtitle stream 0x%x '%s': %d hits (%d forced)",
606                     subtitle->id, subtitle->lang, subtitle->hits,
607                     subtitle->forced_hits );
608             if( subtitle->hits > subtitle_highest ) 
609             {
610                 subtitle_highest = subtitle->hits;
611                 subtitle_highest_id = subtitle->id;
612             } 
613             
614             if( subtitle->hits < subtitle_lowest ) 
615             {
616                 subtitle_lowest = subtitle->hits;
617                 subtitle_lowest_id = subtitle->id;
618             }
619
620             if( subtitle->forced_hits > 0 )
621             {
622                 if( subtitle_forced_id == 0 )
623                 {
624                     subtitle_forced_id = subtitle->id;
625                 }
626             }
627         }
628         
629         if( job->native_language ) {
630             /*
631              * We still have a native_language, so the audio and subtitles are
632              * different, so in this case it is a foreign film and we want to
633              * select the subtitle with the highest hits in our language.
634              */
635             subtitle_hit = subtitle_highest_id;
636             hb_log( "Found a native-language subtitle id 0x%x", subtitle_hit);
637         } else {
638             if( subtitle_forced_id )
639             {
640                 /*
641                  * If there are any subtitle streams with forced subtitles
642                  * then select it in preference to the lowest.
643                  */
644                 subtitle_hit = subtitle_forced_id;
645                 hb_log("Found a subtitle candidate id 0x%x (contains forced subs)",
646                        subtitle_hit);
647             } else if( subtitle_lowest < subtitle_highest ) 
648             {
649                 /*
650                  * OK we have more than one, and the lowest is lower,
651                  * but how much lower to qualify for turning it on by
652                  * default?
653                  *
654                  * Let's say 10% as a default.
655                  */
656                 if( subtitle_lowest < ( subtitle_highest * 0.1 ) ) 
657                 {
658                     subtitle_hit = subtitle_lowest_id;
659                     hb_log( "Found a subtitle candidate id 0x%x",
660                             subtitle_hit );
661                 } else {
662                     hb_log( "No candidate subtitle detected during subtitle-scan");
663                 }
664             }
665         }
666     }
667
668     if( job->select_subtitle ) 
669     {
670         if( job->indepth_scan ) 
671         {
672             for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) 
673             {
674                 subtitle =  hb_list_item( title->list_subtitle, i );
675                 if( subtitle->id == subtitle_hit ) 
676                 {
677                     hb_list_rem( title->list_subtitle, subtitle );
678                     *( job->select_subtitle ) = subtitle;
679                 }
680             }
681         } else {
682             /*
683              * Must be the end of pass 0 or 2 - we don't need this anymore.
684              *
685              * Have to put the subtitle list back together in the title though
686              * or the GUI will have a hissy fit.
687              */
688             free( job->select_subtitle );
689             job->select_subtitle = NULL;
690         }
691     }
692
693     hb_buffer_pool_free();
694
695     hb_title_close( &job->title );
696     free( job );
697 }
698
699 /**
700  * Performs the work objects specific work function.
701  * Loops calling work function for associated work object. Sleeps when fifo is full.
702  * Monitors work done indicator.
703  * Exits loop when work indiactor is set.
704  * @param _w Handle to work object.
705  */
706 static void work_loop( void * _w )
707 {
708     hb_work_object_t * w = _w;
709     hb_buffer_t      * buf_in, * buf_out;
710
711     while( !*w->done )
712     {
713 #if 0
714         hb_lock( job->pause );
715         hb_unlock( job->pause );
716 #endif
717         if( hb_fifo_is_full( w->fifo_out ) ||
718 //        if( (hb_fifo_percent_full( w->fifo_out ) > 0.8) ||
719             !( buf_in = hb_fifo_get( w->fifo_in ) ) )
720         {
721             hb_snooze( w->thread_sleep_interval );
722 //                      w->thread_sleep_interval += 1;
723             continue;
724         }
725 //              w->thread_sleep_interval = MAX(1, (w->thread_sleep_interval - 1));
726
727         w->work( w, &buf_in, &buf_out );
728
729         // Propogate any chapter breaks for the worker
730         if( buf_in && buf_out && buf_in->new_chap )
731         {
732             hb_log("WORK: Copying Chapter Break");
733             buf_out->new_chap = 1;
734         }
735         
736         if( buf_in )
737         {
738             hb_buffer_close( &buf_in );
739         }
740         if( buf_out )
741         {
742             hb_fifo_push( w->fifo_out, buf_out );
743         }
744     }
745 }