OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / libhb / common.c
index 073abb9..ce98398 100644 (file)
@@ -9,6 +9,8 @@
 #include <sys/time.h>
 
 #include "common.h"
+#include "lang.h"
+#include "hb.h"
 
 /**********************************************************************
  * Global variables
@@ -31,7 +33,8 @@ hb_rate_t hb_audio_bitrates[] =
 { {  "32",  32 }, {  "40",  40 }, {  "48",  48 }, {  "56",  56 },
   {  "64",  64 }, {  "80",  80 }, {  "96",  96 }, { "112", 112 },
   { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
-  { "256", 256 }, { "320", 320 }, { "384", 384 } };
+  { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 },
+  { "768", 768 } };
 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
                               sizeof( hb_rate_t );
 int hb_audio_bitrates_default = 8; /* 128 kbps */
@@ -121,8 +124,8 @@ void hb_fix_aspect( hb_job_t * job, int keep )
     if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
     {
         hb_log( "hb_fix_aspect: incomplete info for title %d: "
-                "height = %d, width = %d, aspect = %d",
-                title->height, title->width, title->aspect );
+                "height = %d, width = %d, aspect = %.3f",
+                title->index, title->height, title->width, title->aspect );
         return;
     }
 
@@ -214,9 +217,21 @@ int hb_calc_bitrate( hb_job_t * job, int size )
     length += 135000;
     length /= 90000;
 
+    if( size == -1 )
+    {
+        hb_interjob_t * interjob = hb_interjob_get( job->h );
+        avail = job->vbitrate * 125 * length;
+        avail += length * interjob->vrate * overhead / interjob->vrate_base;
+    }
+
     /* Video overhead */
     avail -= length * job->vrate * overhead / job->vrate_base;
 
+    if( size == -1 )
+    {
+        goto ret;
+    }
+
     for( i = 0; i < hb_list_count(job->list_audio); i++ )
     {
         /* Audio data */
@@ -227,6 +242,7 @@ int hb_calc_bitrate( hb_job_t * job, int size )
         switch( audio->config.out.codec )
         {
             case HB_ACODEC_FAAC:
+            case HB_ACODEC_CA_AAC:
             case HB_ACODEC_VORBIS:
                 samples_per_frame = 1024;
                 break;
@@ -234,6 +250,7 @@ int hb_calc_bitrate( hb_job_t * job, int size )
                 samples_per_frame = 1152;
                 break;
             case HB_ACODEC_AC3:
+            case HB_ACODEC_DCA:
                 samples_per_frame = 1536;
                 break;
             default:
@@ -263,6 +280,7 @@ int hb_calc_bitrate( hb_job_t * job, int size )
         avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
     }
 
+ret:
     if( avail < 0 )
     {
         return 0;
@@ -595,13 +613,84 @@ void hb_deep_log( hb_debug_level_t level, char * log, ... )
 void hb_error( char * log, ... )
 {
     char        string[181]; /* 180 chars + \0 */
+    char        rep_string[181];
+    static char last_string[181];
+    static int  last_error_count = 0;
+    static uint64_t last_series_error_time = 0;
+    static hb_lock_t *mutex = 0;
     va_list     args;
+    uint64_t time_now;
 
     /* Convert the message to a string */
     va_start( args, log );
     vsnprintf( string, 180, log, args );
     va_end( args );
 
+    if( !mutex )
+    {
+        mutex = hb_lock_init();
+    }
+
+    hb_lock( mutex );
+
+    time_now = hb_get_date();
+
+    if( strcmp( string, last_string) == 0 )
+    {
+        /*
+         * The last error and this one are the same, don't log it
+         * just count it instead, unless it was more than one second
+         * ago.
+         */
+        last_error_count++;
+        if( last_series_error_time + ( 1000 * 1 ) > time_now )
+        {
+            hb_unlock( mutex );
+            return;
+        } 
+    }
+    
+    /*
+     * A new error, or the same one more than 10sec since the last one
+     * did we have any of the same counted up?
+     */
+    if( last_error_count > 0 )
+    {
+        /*
+         * Print out the last error to ensure context for the last 
+         * repeated message.
+         */
+        if( error_handler )
+        {
+            error_handler( last_string );
+        } else {
+            hb_log( "%s", last_string );
+        }
+        
+        if( last_error_count > 1 )
+        {
+            /*
+             * Only print out the repeat message for more than 2 of the
+             * same, since we just printed out two of them already.
+             */
+            snprintf( rep_string, 180, "Last error repeated %d times", 
+                      last_error_count - 1 );
+            
+            if( error_handler )
+            {
+                error_handler( rep_string );
+            } else {
+                hb_log( "%s", rep_string );
+            }
+        }
+        
+        last_error_count = 0;
+    }
+
+    last_series_error_time = time_now;
+
+    strcpy( last_string, string );
+
     /*
      * Got the error in a single string, send it off to be dispatched.
      */
@@ -609,8 +698,10 @@ void hb_error( char * log, ... )
     {
         error_handler( string );
     } else {
-        hb_log( string );
+        hb_log( "%s", string );
     }
+
+    hb_unlock( mutex );
 }
 
 void hb_register_error_handler( hb_error_handler_t * handler )
@@ -623,7 +714,7 @@ void hb_register_error_handler( hb_error_handler_t * handler )
  **********************************************************************
  *
  *********************************************************************/
-hb_title_t * hb_title_init( char * dvd, int index )
+hb_title_t * hb_title_init( char * path, int index )
 {
     hb_title_t * t;
 
@@ -633,7 +724,7 @@ hb_title_t * hb_title_init( char * dvd, int index )
     t->list_audio    = hb_list_init();
     t->list_chapter  = hb_list_init();
     t->list_subtitle = hb_list_init();
-    strcat( t->dvd, dvd );
+    strcat( t->path, path );
     // default to decoding mpeg2
     t->video_id      = 0xE0;
     t->video_codec   = WORK_DECMPEG2;
@@ -674,6 +765,15 @@ void hb_title_close( hb_title_t ** _t )
     }
     hb_list_close( &t->list_subtitle );
 
+    if( t->metadata )
+    {
+        if( t->metadata->coverart )
+        {
+            free( t->metadata->coverart );
+        }
+        free( t->metadata );
+    }
+
     free( t );
     *_t = NULL;
 }
@@ -727,6 +827,8 @@ void hb_audio_config_init(hb_audio_config_t * audiocfg)
     audiocfg->in.bitrate = -1;
     audiocfg->in.samplerate = -1;
     audiocfg->in.channel_layout = 0;
+    audiocfg->in.version = 0;
+    audiocfg->in.mode = 0;
     audiocfg->flags.ac3 = 0;
     audiocfg->lang.description[0] = 0;
     audiocfg->lang.simple[0] = 0;
@@ -739,6 +841,7 @@ void hb_audio_config_init(hb_audio_config_t * audiocfg)
     audiocfg->out.samplerate = 44100;
     audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
     audiocfg->out.dynamic_range_compression = 0;
+    audiocfg->out.name = NULL;
 }
 
 /**********************************************************************
@@ -801,3 +904,114 @@ hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
 
     return NULL;
 }
+
+/**********************************************************************
+ * hb_subtitle_copy
+ **********************************************************************
+ *
+ *********************************************************************/
+hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
+{
+    hb_subtitle_t *subtitle = NULL;
+
+    if( src )
+    {
+        subtitle = calloc(1, sizeof(*subtitle));
+        memcpy(subtitle, src, sizeof(*subtitle));
+    }
+    return subtitle;
+}
+
+/**********************************************************************
+ * hb_subtitle_add
+ **********************************************************************
+ *
+ *********************************************************************/
+int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
+{
+    hb_title_t *title = job->title;
+    hb_subtitle_t *subtitle;
+
+    subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
+    if( subtitle == NULL )
+    {
+        /* We fail! */
+        return 0;
+    }
+    subtitle->config = *subtitlecfg;
+    hb_list_add(job->list_subtitle, subtitle);
+    return 1;
+}
+
+int hb_srt_add( const hb_job_t * job, 
+                const hb_subtitle_config_t * subtitlecfg, 
+                const char *lang )
+{
+    hb_subtitle_t *subtitle;
+    iso639_lang_t *language = NULL;
+    int retval = 0;
+
+    subtitle = calloc( 1, sizeof( *subtitle ) );
+    
+    subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
+    subtitle->format = TEXTSUB;
+    subtitle->source = SRTSUB;
+
+    language = lang_for_code2( lang );
+
+    if( language )
+    {
+
+        strcpy( subtitle->lang, language->eng_name );
+        strncpy( subtitle->iso639_2, lang, 4 );
+        
+        subtitle->config = *subtitlecfg;
+        subtitle->config.dest = PASSTHRUSUB;
+
+        hb_list_add(job->list_subtitle, subtitle);
+        retval = 1;
+    }
+    return retval;
+}
+
+char * hb_strdup_printf( char * fmt, ... )
+{
+    int       len;
+    va_list   ap;
+    int       size = 256;
+    char    * str;
+    char    * tmp;
+
+    str = malloc( size );
+    if ( str == NULL )
+        return NULL;
+
+    while (1) 
+    {
+        /* Try to print in the allocated space. */
+        va_start( ap, fmt );
+        len = vsnprintf( str, size, fmt, ap );
+        va_end( ap );
+
+        /* If that worked, return the string. */
+        if ( len > -1 && len < size )
+        {
+            return str;
+        }
+
+        /* Else try again with more space. */
+        if ( len > -1 )     /* glibc 2.1 */
+            size = len + 1; /* precisely what is needed */
+        else                /* glibc 2.0 */
+            size *= 2;      /* twice the old size */
+        tmp = realloc( str, size );
+        if ( tmp == NULL )
+        {
+            free( str );
+            return NULL;
+        }
+        else
+            str = tmp;
+    }
+}
+