diff options
Diffstat (limited to 'libhb')
-rw-r--r-- | libhb/common.h | 6 | ||||
-rw-r--r-- | libhb/hb.c | 84 | ||||
-rw-r--r-- | libhb/reader.c | 24 | ||||
-rw-r--r-- | libhb/work.c | 119 |
4 files changed, 211 insertions, 22 deletions
diff --git a/libhb/common.h b/libhb/common.h index 8b09c85c9..116f99a93 100644 --- a/libhb/common.h +++ b/libhb/common.h @@ -245,6 +245,10 @@ struct hb_job_s int mux; const char * file; + int subtitle_scan; + hb_subtitle_t ** select_subtitle; + char * native_language; + #ifdef __LIBHB__ /* Internal data */ hb_handle_t * h; @@ -360,6 +364,8 @@ struct hb_subtitle_s char lang[1024]; char iso639_2[4]; + int hits; /* How many hits/occurrences of this subtitle */ + #ifdef __LIBHB__ /* Internal data */ hb_fifo_t * fifo_in; /* SPU ES */ diff --git a/libhb/hb.c b/libhb/hb.c index 467ba92ad..0550c3cac 100644 --- a/libhb/hb.c +++ b/libhb/hb.c @@ -546,13 +546,87 @@ void hb_add( hb_handle_t * h, hb_job_t * job ) } } - /* Copy the subtitle we want (or not) */ title_copy->list_subtitle = hb_list_init(); - if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) ) + + if( job->pass != 1 ) { - subtitle_copy = malloc( sizeof( hb_subtitle_t ) ); - memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) ); - hb_list_add( title_copy->list_subtitle, subtitle_copy ); + /* Copy the subtitle we want (or not) + */ + if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) ) + { + subtitle_copy = malloc( sizeof( hb_subtitle_t ) ); + memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) ); + hb_list_add( title_copy->list_subtitle, subtitle_copy ); + } + } else { + char audio_lang[4]; + + memset( audio_lang, 0, sizeof( audio_lang ) ); + + /* + * Pass 1, do we want to do a subtitle scan? + */ + if( job->subtitle_scan ) + { + /* + * Search for all occurances of the audio language in the + * subtitles and add them all to the + * title_copy->list_subtitle. First of all find the + * language for the audio. + */ + for( i = 0; i < 8; i++ ) + { + if( job->audios[i] < 0 ) + { + break; + } + if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) ) + { + strncpy(audio_lang, audio->iso639_2, sizeof(audio_lang)); + } + } + } + + /* + * If we have a native language now is the time to see whether we want + * to enable subtitles for it if it differs from the audio language. + */ + if( job->native_language ) + { + if( strncasecmp( job->native_language, audio_lang, + sizeof( audio_lang ) ) != 0 ) + { + + hb_log( "Enabled subtitles in native language '%s', audio is in '%s'", + job->native_language, audio_lang); + /* + * The main audio track is not in our native language, so switch + * the subtitle scan to use our native language instead. + */ + strncpy( audio_lang, job->native_language, sizeof( audio_lang ) ); + } else { + /* + * native language is irrelevent, free it. + */ + free( job->native_language ); + job->native_language = NULL; + } + } + + for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) + { + subtitle = hb_list_item( title->list_subtitle, i ); + if( strcmp( subtitle->iso639_2, audio_lang ) == 0 ) + { + /* + * Matched subtitle language with audio language, so + * add this to our list to scan. + */ + subtitle_copy = malloc( sizeof( hb_subtitle_t ) ); + memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) ); + hb_list_add( title_copy->list_subtitle, subtitle_copy ); + } + } } /* Copy the job */ diff --git a/libhb/reader.c b/libhb/reader.c index 35939f96b..4767f87a7 100644 --- a/libhb/reader.c +++ b/libhb/reader.c @@ -138,12 +138,26 @@ static hb_fifo_t * GetFifoForId( hb_job_t * job, int id ) return job->fifo_mpeg2; } - if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) && - id == subtitle->id ) - { - return subtitle->fifo_in; + if (job->subtitle_scan) { + /* + * Count the occurances of the subtitles, don't actually return any to encode. + */ + for (i=0; i < hb_list_count(title->list_subtitle); i++) { + subtitle = hb_list_item( title->list_subtitle, i); + if (id == subtitle->id) { + /* + * A hit, count it. + */ + subtitle->hits++; + } + } + } else { + if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) && + id == subtitle->id ) + { + return subtitle->fifo_in; + } } - for( i = 0; i < hb_list_count( title->list_audio ); i++ ) { audio = hb_list_item( title->list_audio, i ); diff --git a/libhb/work.c b/libhb/work.c index aa7709719..b7d02cd5c 100644 --- a/libhb/work.c +++ b/libhb/work.c @@ -100,6 +100,11 @@ static void do_job( hb_job_t * job, int cpu_count ) hb_audio_t * audio; hb_subtitle_t * subtitle; int done; + unsigned int subtitle_highest = 0; + unsigned int subtitle_highest_id = 0; + unsigned int subtitle_lowest = -1; + unsigned int subtitle_lowest_id = 0; + unsigned int subtitle_hit = 0; title = job->title; @@ -191,17 +196,31 @@ static void do_job( hb_job_t * job, int cpu_count ) w->config = &job->config; hb_list_add( job->list_work, w ); - subtitle = hb_list_item( title->list_subtitle, 0 ); - if( subtitle ) + if( job->select_subtitle && !job->subtitle_scan ) { - hb_log( " + subtitle %x, %s", subtitle->id, subtitle->lang ); + hb_list_add( title->list_subtitle, *( job->select_subtitle ) ); + } - subtitle->fifo_in = hb_fifo_init( 8 ); - subtitle->fifo_raw = hb_fifo_init( 8 ); + for( i=0; i < hb_list_count(title->list_subtitle); i++ ) + { + subtitle = hb_list_item( title->list_subtitle, i ); - hb_list_add( job->list_work, ( w = getWork( WORK_DECSUB ) ) ); - w->fifo_in = subtitle->fifo_in; - w->fifo_out = subtitle->fifo_raw; + if( subtitle ) + { + hb_log( " + subtitle %x, %s", subtitle->id, subtitle->lang ); + + subtitle->fifo_in = hb_fifo_init( 8 ); + subtitle->fifo_raw = hb_fifo_init( 8 ); + + if (!job->subtitle_scan) { + /* + * Don't add threads for subtitles when we are scanning + */ + hb_list_add( job->list_work, ( w = getWork( WORK_DECSUB ) ) ); + w->fifo_in = subtitle->fifo_in; + w->fifo_out = subtitle->fifo_raw; + } + } } if( job->acodec & HB_ACODEC_AC3 ) @@ -499,10 +518,13 @@ static void do_job( hb_job_t * job, int cpu_count ) hb_fifo_close( &job->fifo_sync ); hb_fifo_close( &job->fifo_render ); hb_fifo_close( &job->fifo_mpeg4 ); - if( subtitle ) - { - hb_fifo_close( &subtitle->fifo_in ); - hb_fifo_close( &subtitle->fifo_raw ); + for (i=0; i < hb_list_count(title->list_subtitle); i++) { + subtitle = hb_list_item( title->list_subtitle, i); + if( subtitle ) + { + hb_fifo_close( &subtitle->fifo_in ); + hb_fifo_close( &subtitle->fifo_raw ); + } } for( i = 0; i < hb_list_count( title->list_audio ); i++ ) { @@ -513,6 +535,79 @@ static void do_job( hb_job_t * job, int cpu_count ) hb_fifo_close( &audio->fifo_out ); } + /* + * Before closing the title print out our subtitle stats if we need to + * Find the highest and lowest. + */ + for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) + { + subtitle = hb_list_item( title->list_subtitle, i ); + hb_log( "Subtitle stream 0x%x '%s': %d hits", + subtitle->id, subtitle->lang, subtitle->hits ); + if( subtitle->hits > subtitle_highest ) + { + subtitle_highest = subtitle->hits; + subtitle_highest_id = subtitle->id; + } + + if( subtitle->hits < subtitle_lowest ) + { + subtitle_lowest = subtitle->hits; + subtitle_lowest_id = subtitle->id; + } + } + + if( job->native_language ) { + /* + * We still have a native_language, so the audio and subtitles are + * different, so in this case it is a foreign film and we want to + * select the first subtitle in our language. + */ + subtitle = hb_list_item( title->list_subtitle, 0 ); + subtitle_hit = subtitle->id; + hb_log( "Found a native-language subtitle id 0x%x", subtitle_hit); + } else { + if( subtitle_lowest < subtitle_highest ) + { + /* + * OK we have more than one, and the lowest is lower, but how much + * lower to qualify for turning it on by default? + * + * Let's say 20% as a default. + */ + if( subtitle_lowest < ( subtitle_highest * 0.2 ) ) + { + subtitle_hit = subtitle_lowest_id; + hb_log( "Found a subtitle candidate id 0x%x", + subtitle_hit ); + } else { + hb_log( "No candidate subtitle detected during subtitle-scan"); + } + } + } + + if( job->select_subtitle ) + { + if( job->subtitle_scan ) + { + for( i=0; i < hb_list_count( title->list_subtitle ); i++ ) + { + subtitle = hb_list_item( title->list_subtitle, i ); + if( subtitle->id = subtitle_hit ) + { + hb_list_rem( title->list_subtitle, subtitle ); + *( job->select_subtitle ) = subtitle; + } + } + } else { + /* + * Must be the second pass - we don't need this anymore. + */ + free( job->select_subtitle ); + job->select_subtitle = NULL; + } + } + hb_title_close( &job->title ); free( job ); } |