OSDN Git Service

First attempt at variable frame rate detelecining for NTSC video sources.
[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     else if ( job->pixel_ratio == 2 )
132     {
133
134         /* While keeping the DVD storage aspect, resize the job width and height
135            so they fit into the user's specified dimensions. */
136         hb_set_anamorphic_size(job);
137     }
138
139         /* Keep width and height within these boundaries,
140            but ignore for "loose" anamorphic encodes, for
141            which this stuff is covered in the pixel_ratio
142            section right above.*/
143         if (job->maxHeight && (job->height > job->maxHeight) && (job->pixel_ratio != 2))
144         {
145                 job->height = job->maxHeight;
146                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
147                 hb_log("Height out of bounds, scaling down to %i", job->maxHeight);
148                 hb_log("New dimensions %i * %i", job->width, job->height);
149         }
150         if (job->maxWidth && (job->width > job->maxWidth) && (job->pixel_ratio != 2))
151         {
152                 job->width = job->maxWidth;
153                 hb_fix_aspect( job, HB_KEEP_WIDTH );   
154                 hb_log("Width out of bounds, scaling down to %i", job->maxWidth);
155                 hb_log("New dimensions %i * %i", job->width, job->height);
156         }
157
158     hb_log( " + %dx%d -> %dx%d, crop %d/%d/%d/%d",
159             title->width, title->height, job->width, job->height,
160             job->crop[0], job->crop[1], job->crop[2], job->crop[3] );
161     hb_log( " + grayscale %s", job->grayscale ? "on" : "off" );
162     
163     if ( job->vfr )
164     {
165         job->vrate_base = 900900;
166
167         int detelecine_present = 0;        
168         if ( job->filters )
169         {
170             for( i = 0; i < hb_list_count( job->filters ); i++ )
171             {
172                 hb_filter_object_t * filter = hb_list_item( job->filters, i );
173                 if (filter->id == FILTER_DETELECINE)
174                     detelecine_present = 1;
175             }            
176         }
177         
178         if (!detelecine_present)
179             hb_list_add( job->filters, &hb_filter_detelecine );
180         
181         hb_log("work: VFR mode -- Switching FPS to 29.97 and detelecining.");
182     }
183     
184     if( job->filters )
185     {
186         hb_log(" + filters");
187         for( i = 0; i < hb_list_count( job->filters ); i++ )
188         {
189             hb_filter_object_t * filter = hb_list_item( job->filters, i );
190             if (filter->settings)
191                 hb_log("   + %s (%s)", filter->name, filter->settings);
192             else
193                 hb_log("   + %s (default settings)", filter->name);
194         }
195     }
196     
197     if( job->vquality >= 0.0 && job->vquality <= 1.0 )
198     {
199         hb_log( " + %.3f fps, video quality %.2f", (float) job->vrate /
200                 (float) job->vrate_base, job->vquality );
201     }
202     else
203     {
204         hb_log( " + %.3f fps, video bitrate %d kbps, pass %d",
205                 (float) job->vrate / (float) job->vrate_base,
206                 job->vbitrate, job->pass );
207     }
208         hb_log (" + PixelRatio: %d, width:%d, height: %d",job->pixel_ratio,job->width, job->height);
209     job->fifo_mpeg2  = hb_fifo_init( 2048 );
210     job->fifo_raw    = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
211     job->fifo_sync   = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
212     job->fifo_render = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
213     job->fifo_mpeg4  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
214
215     /* Synchronization */
216     hb_list_add( job->list_work, ( w = getWork( WORK_SYNC ) ) );
217     w->fifo_in  = NULL;
218     w->fifo_out = NULL;
219
220     /* Video decoder */
221     hb_list_add( job->list_work, ( w = getWork( WORK_DECMPEG2 ) ) );
222     w->fifo_in  = job->fifo_mpeg2;
223     w->fifo_out = job->fifo_raw;
224
225     /* Video renderer */
226     hb_list_add( job->list_work, ( w = getWork( WORK_RENDER ) ) );
227     w->fifo_in  = job->fifo_sync;
228     w->fifo_out = job->fifo_render;
229
230     /* Video encoder */
231     switch( job->vcodec )
232     {
233         case HB_VCODEC_FFMPEG:
234             hb_log( " + encoder FFmpeg" );
235             w = getWork( WORK_ENCAVCODEC );
236             break;
237         case HB_VCODEC_XVID:
238             hb_log( " + encoder XviD" );
239             w = getWork( WORK_ENCXVID );
240             break;
241         case HB_VCODEC_X264:
242             hb_log( " + encoder x264" );
243             w = getWork( WORK_ENCX264 );
244             break;
245     }
246     w->fifo_in  = job->fifo_render;
247     w->fifo_out = job->fifo_mpeg4;
248     w->config   = &job->config;
249     
250     hb_list_add( job->list_work, w );
251
252     if( job->select_subtitle && !job->indepth_scan ) 
253     {
254         /*
255          * Must be second pass of a two pass with subtitle scan enabled, so
256          * add the subtitle that we found on the first pass for use in this
257          * pass.
258          */
259         if (*(job->select_subtitle))
260         {
261             hb_list_add( title->list_subtitle, *( job->select_subtitle ) );
262         }
263     }
264
265     for( i=0; i < hb_list_count(title->list_subtitle); i++ ) 
266     {
267         subtitle =  hb_list_item( title->list_subtitle, i );
268
269         if( subtitle )
270         {
271             hb_log( " + subtitle %x, %s", subtitle->id, subtitle->lang );
272             
273             subtitle->fifo_in  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
274             subtitle->fifo_raw = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
275             
276             /*
277              * Disable forced subtitles if we didn't find any in the scan
278              * so that we display normal subtitles instead.
279              *
280              * select_subtitle implies that we did a scan.
281              */
282             if( !job->indepth_scan && job->subtitle_force && 
283                 job->select_subtitle ) 
284             {
285                 if( subtitle->forced_hits == 0 )
286                 {
287                     job->subtitle_force = 0;
288                 }
289             }
290
291             if (!job->indepth_scan || job->subtitle_force) {
292                 /*
293                  * Don't add threads for subtitles when we are scanning, unless
294                  * looking for forced subtitles.
295                  */
296                 if( sub_w != NULL )
297                 { 
298                     /*
299                      * Need to copy the prior subtitle structure so that we
300                      * don't overwrite the fifos.
301                      */
302                     sub_w = calloc( sizeof( hb_work_object_t ), 1 );
303                     sub_w = memcpy( sub_w, w, sizeof( hb_work_object_t ));
304                 } else {
305                     w = sub_w = getWork( WORK_DECSUB );
306                 }
307                 hb_list_add( job->list_work, sub_w );
308                 sub_w->fifo_in  = subtitle->fifo_in;
309                 sub_w->fifo_out = subtitle->fifo_raw;
310             }
311         }
312     }
313
314     if( job->acodec & HB_ACODEC_AC3 )
315     {
316         hb_log( " + audio AC3 passthrough" );
317     }
318     else
319     {
320         hb_log( " + audio %d kbps, %d Hz", job->abitrate, job->arate );
321         hb_log( " + encoder %s", ( job->acodec & HB_ACODEC_FAAC ) ?
322                 "faac" : ( ( job->acodec & HB_ACODEC_LAME ) ? "lame" :
323                 "vorbis" ) );
324     }
325
326     /* if we are doing AC3 passthru, then remove any non-AC3 audios from the job */
327     /* otherwise, Bad Things will happen */
328     for( i = 0; i < hb_list_count( title->list_audio ); )
329     {
330         audio = hb_list_item( title->list_audio, i );
331         if( ( job->acodec & HB_ACODEC_AC3 ) && ( audio->codec != HB_ACODEC_AC3 ) )
332         {
333             hb_list_rem( title->list_audio, audio );
334             free( audio );
335             continue;
336         }
337         i++;
338     }
339
340     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
341     {
342         audio = hb_list_item( title->list_audio, i );
343         hb_log( "   + %x, %s", audio->id, audio->lang );
344                         
345                 /* sense-check the current mixdown options */
346
347                 /* log the requested mixdown */
348                 for (j = 0; j < hb_audio_mixdowns_count; j++) {
349                         if (hb_audio_mixdowns[j].amixdown == job->audio_mixdowns[i]) {
350                                 hb_log( "     + Requested mixdown: %s (%s)", hb_audio_mixdowns[j].human_readable_name, hb_audio_mixdowns[j].internal_name );
351                                 break;
352                         }
353                 }
354
355         /* sense-check the requested mixdown */
356
357         /* audioCodecsSupportMono and audioCodecsSupport6Ch are the same for now,
358            but this may change in the future, so they are separated for flexibility */
359         int audioCodecsSupportMono = ((audio->codec == HB_ACODEC_AC3 ||
360             audio->codec == HB_ACODEC_DCA) && (job->acodec == HB_ACODEC_FAAC || job->acodec == HB_ACODEC_VORBIS));
361         int audioCodecsSupport6Ch =  ((audio->codec == HB_ACODEC_AC3 ||
362             audio->codec == HB_ACODEC_DCA) && (job->acodec == HB_ACODEC_FAAC || job->acodec == HB_ACODEC_VORBIS));
363
364         /* find out what the format of our source audio is */
365         switch (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK) {
366         
367             /* mono sources */
368             case HB_INPUT_CH_LAYOUT_MONO:
369                 /* regardless of what stereo mixdown we've requested, a mono source always get mixed down
370                 to mono if we can, and mixed up to stereo if we can't */
371                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 1) {
372                     job->audio_mixdowns[i] = HB_AMIXDOWN_MONO;
373                 } else {
374                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
375                 }
376                 break;
377
378             /* stereo input */
379             case HB_INPUT_CH_LAYOUT_STEREO:
380                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
381                 /* use stereo if not supported */
382                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
383                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
384                 /* otherwise, preserve stereo regardless of if we requested something higher */
385                 } else if (job->audio_mixdowns[i] > HB_AMIXDOWN_STEREO) {
386                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
387                 }
388                 break;
389
390             /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
391             /* the A52 flags don't allow for a way to distinguish between DPL1 and DPL2 on a DVD,
392                so we always assume a DPL1 source for A52_DOLBY */
393             case HB_INPUT_CH_LAYOUT_DOLBY:
394                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
395                 /* preserve dolby if not supported */
396                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
397                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
398                 /* otherwise, preserve dolby even if we requested something higher */
399                 /* a stereo mixdown will still be honoured here */
400                 } else if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBY) {
401                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
402                 }
403                 break;
404
405             /* 3F/2R input */
406             case HB_INPUT_CH_LAYOUT_3F2R:
407                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
408                 /* use dpl2 if not supported */
409                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
410                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
411                 } else {
412                     /* check if we have 3F2R input and also have an LFE - i.e. we have a 5.1 source) */
413                     if (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE) {
414                         /* we have a 5.1 source */
415                         /* if we requested 6ch, but our audio format doesn't support it, then mix to DPLII instead */
416                         if (job->audio_mixdowns[i] == HB_AMIXDOWN_6CH && audioCodecsSupport6Ch == 0) {
417                             job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
418                         }
419                     } else {
420                         /* we have a 5.0 source, so we can't do 6ch conversion
421                         default to DPL II instead */
422                         if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBYPLII) {
423                             job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBYPLII;
424                         }
425                     }
426                 }
427                 /* all other mixdowns will have been preserved here */
428                 break;
429
430             /* 3F/1R input */
431             case HB_INPUT_CH_LAYOUT_3F1R:
432                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
433                 /* use dpl1 if not supported */
434                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 0) {
435                     job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
436                 } else {
437                     /* we have a 4.0 or 4.1 source, so we can't do DPLII or 6ch conversion
438                     default to DPL I instead */
439                     if (job->audio_mixdowns[i] > HB_AMIXDOWN_DOLBY) {
440                         job->audio_mixdowns[i] = HB_AMIXDOWN_DOLBY;
441                     }
442                 }
443                 /* all other mixdowns will have been preserved here */
444                 break;
445
446             default:
447                 /* if we've requested a mono mixdown, and it is supported, then do the mix */
448                 if (job->audio_mixdowns[i] == HB_AMIXDOWN_MONO && audioCodecsSupportMono == 1) {
449                     job->audio_mixdowns[i] = HB_AMIXDOWN_MONO;
450                 /* mix everything else down to stereo */
451                 } else {
452                     job->audio_mixdowns[i] = HB_AMIXDOWN_STEREO;
453                 }
454
455         }
456
457                 /* log the output mixdown */
458                 for (j = 0; j < hb_audio_mixdowns_count; j++) {
459                         if (hb_audio_mixdowns[j].amixdown == job->audio_mixdowns[i]) {
460                                 hb_log( "     + Actual mixdown: %s (%s)", hb_audio_mixdowns[j].human_readable_name, hb_audio_mixdowns[j].internal_name );
461                                 break;
462                         }
463                 }
464
465                 /* we now know we have a valid mixdown for the input source and the audio output format */
466                 /* remember the mixdown for this track */
467                 audio->amixdown = job->audio_mixdowns[i];
468
469         audio->config.vorbis.language = audio->lang_simple;
470
471                 /* set up the audio work structures */
472         audio->fifo_in   = hb_fifo_init( 2048 );
473         audio->fifo_raw  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
474         audio->fifo_sync = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
475         audio->fifo_out  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
476
477         switch( audio->codec )
478         {
479             case HB_ACODEC_AC3:
480                 w = getWork( WORK_DECA52 );
481                 break;
482             case HB_ACODEC_DCA:
483                 w = getWork( WORK_DECDCA );
484                 break;
485             case HB_ACODEC_MPGA:
486                 w = getWork( WORK_DECAVCODEC );
487                 break;
488             case HB_ACODEC_LPCM:
489                 w = getWork( WORK_DECLPCM );
490                 break;
491         }
492         w->fifo_in       = audio->fifo_in;
493         w->fifo_out      = audio->fifo_raw;
494         w->config        = &audio->config;
495         w->amixdown      = audio->amixdown;
496         w->source_acodec = audio->codec;
497         
498         /* FIXME: This feels really hackish, anything better? */
499         audio_w = calloc( sizeof( hb_work_object_t ), 1 );
500         audio_w = memcpy( audio_w, w, sizeof( hb_work_object_t ));
501         
502         hb_list_add( job->list_work, audio_w );
503
504         switch( job->acodec )
505         {
506             case HB_ACODEC_FAAC:
507                 w = getWork( WORK_ENCFAAC );
508                 break;
509             case HB_ACODEC_LAME:
510                 w = getWork( WORK_ENCLAME );
511                 break;
512             case HB_ACODEC_VORBIS:
513                 w = getWork( WORK_ENCVORBIS );
514                 break;
515         }
516
517         if( job->acodec != HB_ACODEC_AC3 )
518         {
519             w->fifo_in       = audio->fifo_sync;
520             w->fifo_out      = audio->fifo_out;
521             w->config        = &audio->config;
522             w->amixdown      = audio->amixdown;
523             w->source_acodec = audio->codec;
524             
525             /* FIXME: This feels really hackish, anything better? */
526             audio_w = calloc( sizeof( hb_work_object_t ), 1 );
527             audio_w = memcpy( audio_w, w, sizeof( hb_work_object_t ));
528         
529             hb_list_add( job->list_work, audio_w );
530         }
531
532
533     }
534
535     /* Init read & write threads */
536     job->reader = hb_reader_init( job );
537
538     hb_log( " + output: %s", job->file );
539     job->muxer = hb_muxer_init( job );
540
541     job->done = 0;
542
543     /* Launch processing threads */
544     for( i = 1; i < hb_list_count( job->list_work ); i++ )
545     {
546         w = hb_list_item( job->list_work, i );
547         w->done = &job->done;
548         w->thread_sleep_interval = 10;
549         w->init( w, job );
550         w->thread = hb_thread_init( w->name, work_loop, w,
551                                     HB_LOW_PRIORITY );
552     }
553
554     done = 0;
555     w = hb_list_item( job->list_work, 0 );
556     w->thread_sleep_interval = 50;
557     w->init( w, job );
558     while( !*job->die )
559     {
560         if( w->work( w, NULL, NULL ) == HB_WORK_DONE )
561         {
562             done = 1;
563         }
564         if( done &&
565             !hb_fifo_size( job->fifo_sync ) &&
566             !hb_fifo_size( job->fifo_render ) &&
567             !hb_fifo_size( job->fifo_mpeg4 ) )
568         {
569             break;
570         }
571         hb_snooze( w->thread_sleep_interval );
572     }
573     hb_list_rem( job->list_work, w );
574     w->close( w );
575     job->done = 1;
576
577     /* Close work objects */
578     while( ( w = hb_list_item( job->list_work, 0 ) ) )
579     {
580         hb_list_rem( job->list_work, w );
581         hb_thread_close( &w->thread );
582         w->close( w );
583         
584         /* FIXME: This feels really hackish, anything better? */
585         if ( w->id == WORK_DECA52 ||
586              w->id == WORK_DECDCA ||
587              w->id == WORK_DECLPCM ||
588              w->id == WORK_ENCFAAC ||
589              w->id == WORK_ENCLAME ||
590              w->id == WORK_ENCVORBIS )
591         {
592             free( w );
593             w = NULL;
594         }
595     }
596     
597     hb_list_close( &job->list_work );
598
599     /* Stop read & write threads */
600     hb_thread_close( &job->reader );
601     hb_thread_close( &job->muxer );
602
603     /* Close fifos */
604     hb_fifo_close( &job->fifo_mpeg2 );
605     hb_fifo_close( &job->fifo_raw );
606     hb_fifo_close( &job->fifo_sync );
607     hb_fifo_close( &job->fifo_render );
608     hb_fifo_close( &job->fifo_mpeg4 );
609
610     for (i=0; i < hb_list_count(title->list_subtitle); i++) {
611         subtitle =  hb_list_item( title->list_subtitle, i);
612         if( subtitle )
613         {
614             hb_fifo_close( &subtitle->fifo_in );
615             hb_fifo_close( &subtitle->fifo_raw );
616         }
617     }
618     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
619     {
620         audio = hb_list_item( title->list_audio, i );
621         hb_fifo_close( &audio->fifo_in );
622         hb_fifo_close( &audio->fifo_raw );
623         hb_fifo_close( &audio->fifo_sync );
624         hb_fifo_close( &audio->fifo_out );
625     }
626
627     if( job->indepth_scan )
628     {
629         /*
630          * Before closing the title print out our subtitle stats if we need to
631          * Find the highest and lowest.
632          */
633         for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) 
634         {
635             subtitle =  hb_list_item( title->list_subtitle, i );
636             hb_log( "Subtitle stream 0x%x '%s': %d hits (%d forced)",
637                     subtitle->id, subtitle->lang, subtitle->hits,
638                     subtitle->forced_hits );
639             if( subtitle->hits > subtitle_highest ) 
640             {
641                 subtitle_highest = subtitle->hits;
642                 subtitle_highest_id = subtitle->id;
643             } 
644             
645             if( subtitle->hits < subtitle_lowest ) 
646             {
647                 subtitle_lowest = subtitle->hits;
648                 subtitle_lowest_id = subtitle->id;
649             }
650
651             if( subtitle->forced_hits > 0 )
652             {
653                 if( subtitle_forced_id == 0 )
654                 {
655                     subtitle_forced_id = subtitle->id;
656                 }
657             }
658         }
659         
660         if( job->native_language ) {
661             /*
662              * We still have a native_language, so the audio and subtitles are
663              * different, so in this case it is a foreign film and we want to
664              * select the subtitle with the highest hits in our language.
665              */
666             subtitle_hit = subtitle_highest_id;
667             hb_log( "Found a native-language subtitle id 0x%x", subtitle_hit);
668         } else {
669             if( subtitle_forced_id )
670             {
671                 /*
672                  * If there are any subtitle streams with forced subtitles
673                  * then select it in preference to the lowest.
674                  */
675                 subtitle_hit = subtitle_forced_id;
676                 hb_log("Found a subtitle candidate id 0x%x (contains forced subs)",
677                        subtitle_hit);
678             } else if( subtitle_lowest < subtitle_highest ) 
679             {
680                 /*
681                  * OK we have more than one, and the lowest is lower,
682                  * but how much lower to qualify for turning it on by
683                  * default?
684                  *
685                  * Let's say 10% as a default.
686                  */
687                 if( subtitle_lowest < ( subtitle_highest * 0.1 ) ) 
688                 {
689                     subtitle_hit = subtitle_lowest_id;
690                     hb_log( "Found a subtitle candidate id 0x%x",
691                             subtitle_hit );
692                 } else {
693                     hb_log( "No candidate subtitle detected during subtitle-scan");
694                 }
695             }
696         }
697     }
698
699     if( job->select_subtitle ) 
700     {
701         if( job->indepth_scan ) 
702         {
703             for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) 
704             {
705                 subtitle =  hb_list_item( title->list_subtitle, i );
706                 if( subtitle->id == subtitle_hit ) 
707                 {
708                     hb_list_rem( title->list_subtitle, subtitle );
709                     *( job->select_subtitle ) = subtitle;
710                 }
711             }
712         } else {
713             /*
714              * Must be the end of pass 0 or 2 - we don't need this anymore.
715              *
716              * Have to put the subtitle list back together in the title though
717              * or the GUI will have a hissy fit.
718              */
719             free( job->select_subtitle );
720             job->select_subtitle = NULL;
721         }
722     }
723
724     hb_buffer_pool_free();
725
726     hb_title_close( &job->title );
727     free( job );
728 }
729
730 /**
731  * Performs the work objects specific work function.
732  * Loops calling work function for associated work object. Sleeps when fifo is full.
733  * Monitors work done indicator.
734  * Exits loop when work indiactor is set.
735  * @param _w Handle to work object.
736  */
737 static void work_loop( void * _w )
738 {
739     hb_work_object_t * w = _w;
740     hb_buffer_t      * buf_in, * buf_out;
741
742     while( !*w->done )
743     {
744 #if 0
745         hb_lock( job->pause );
746         hb_unlock( job->pause );
747 #endif
748         if( hb_fifo_is_full( w->fifo_out ) ||
749 //        if( (hb_fifo_percent_full( w->fifo_out ) > 0.8) ||
750             !( buf_in = hb_fifo_get( w->fifo_in ) ) )
751         {
752             hb_snooze( w->thread_sleep_interval );
753 //                      w->thread_sleep_interval += 1;
754             continue;
755         }
756 //              w->thread_sleep_interval = MAX(1, (w->thread_sleep_interval - 1));
757
758         w->work( w, &buf_in, &buf_out );
759
760         // Propogate any chapter breaks for the worker
761         if( buf_in && buf_out && buf_in->new_chap )
762         {
763             hb_log("WORK: Copying Chapter Break");
764             buf_out->new_chap = 1;
765         }
766         
767         if( buf_in )
768         {
769             hb_buffer_close( &buf_in );
770         }
771         if( buf_out )
772         {
773             hb_fifo_push( w->fifo_out, buf_out );
774         }
775     }
776 }