OSDN Git Service

Soft Subs Part 2: Auto-detect CC during scan, add CC to subtitle list in title, if...
[handbrake-jp/handbrake-jp-git.git] / libhb / work.c
index 00edd05..b413c78 100644 (file)
@@ -7,6 +7,7 @@
 #include "hb.h"
 #include "a52dec/a52.h"
 #include "dca.h"
+#include "libavformat/avformat.h"
 
 typedef struct
 {
@@ -113,7 +114,7 @@ hb_work_object_t * hb_codec_encoder( int codec )
  * Displays job parameters in the debug log.
  * @param job Handle work hb_job_t.
  */
-hb_display_job_info( hb_job_t * job )
+void hb_display_job_info( hb_job_t * job )
 {
     hb_title_t * title = job->title;
     hb_audio_t   * audio;
@@ -153,6 +154,9 @@ hb_display_job_info( hb_job_t * job )
 
             if( job->mp4_optimize )
                 hb_log( "     + optimized for progressive web downloads");
+            
+            if( job->color_matrix )
+                hb_log( "     + custom color matrix: %s", job->color_matrix == 1 ? "ITU Bt.601 (SD)" : "ITU Bt.709 (HD)");
             break;
 
         case HB_MUX_AVI:
@@ -182,31 +186,30 @@ hb_display_job_info( hb_job_t * job )
         hb_log( "     + bitrate %d kbps", title->video_bitrate / 1000 );
     }
     
-    if( job->vfr)
-    {
-        hb_log( "   + frame rate: %.3f fps -> variable fps",
-            (float) title->rate / (float) title->rate_base );
-    }
-    else if( !job->cfr )
+    if( !job->cfr )
     {
         hb_log( "   + frame rate: same as source (around %.3f fps)",
             (float) title->rate / (float) title->rate_base );
     }
     else
     {
-        hb_log( "   + frame rate: %.3f fps -> constant %.3f fps",
-            (float) title->rate / (float) title->rate_base, (float) job->vrate / (float) job->vrate_base );
+        static const char *frtypes[] = {
+            "", "constant", "peak rate limited to"
+        };
+        hb_log( "   + frame rate: %.3f fps -> %s %.3f fps",
+            (float) title->rate / (float) title->rate_base, frtypes[job->cfr],
+            (float) job->vrate / (float) job->vrate_base );
     }
 
-    if( job->pixel_ratio )
+    if( job->anamorphic.mode )
     {
-        hb_log( "   + %s anamorphic", job->pixel_ratio == 1 ? "strict" : "loose" );
+        hb_log( "   + %s anamorphic", job->anamorphic.mode == 1 ? "strict" : "loose" );
         hb_log( "     + storage dimensions: %d * %d -> %d * %d, crop %d/%d/%d/%d",
                     title->width, title->height, job->width, job->height,
                     job->crop[0], job->crop[1], job->crop[2], job->crop[3] );
-        hb_log( "     + pixel aspect ratio: %i / %i", job->pixel_aspect_width, job->pixel_aspect_height );
+        hb_log( "     + pixel aspect ratio: %i / %i", job->anamorphic.par_width, job->anamorphic.par_height );
         hb_log( "     + display dimensions: %.0f * %i",
-            (float)( job->width * job->pixel_aspect_width / job->pixel_aspect_height ), job->height );
+            (float)( job->width * job->anamorphic.par_width / job->anamorphic.par_height ), job->height );
     }
     else
     {
@@ -214,15 +217,6 @@ hb_display_job_info( hb_job_t * job )
                 title->width, title->height, job->width, job->height,
                 job->crop[0], job->crop[1], job->crop[2], job->crop[3] );
     }
-    
-    if( job->color_matrix )
-    {
-        hb_log( "   + color space: %s", job->color_matrix == 1 ? "ITU Bt.601 (SD)" : "ITU Bt.709 (HD)");
-    }
-    else
-    {
-        hb_log( "   + color space: %s", ( title->width < 1280 || title->height < 720 ) ? "ITU Bt.601 (SD)" : "ITU Bt.709 (HD)");        
-    }
 
     if ( job->grayscale )
         hb_log( "   + grayscale mode" );
@@ -270,7 +264,7 @@ hb_display_job_info( hb_job_t * job )
         }
         else if( job->vquality > 1 )
         {
-            hb_log( "     + quality: %.0f %s", job->vquality, job->crf && job->vcodec == HB_VCODEC_X264 ? "(RF)" : "(QP)" ); 
+            hb_log( "     + quality: %.2f %s", job->vquality, job->crf && job->vcodec == HB_VCODEC_X264 ? "(RF)" : "(QP)" ); 
         }
         else
         {
@@ -278,13 +272,16 @@ hb_display_job_info( hb_job_t * job )
         }
     }
 
-    for( i=0; i < hb_list_count(title->list_subtitle); i++ )
+    for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
     {
         subtitle =  hb_list_item( title->list_subtitle, i );
 
         if( subtitle )
         {
-            hb_log( " * subtitle track %i, %s (id %x)", job->subtitle+1, subtitle->lang, subtitle->id);
+            hb_log( " * subtitle track %i, %s (id %x) %s [%s] -> %s ", subtitle->track, subtitle->lang, subtitle->id,
+                    subtitle->format == PICTURESUB ? "Picture" : "Text",
+                    subtitle->source == VOBSUB ? "VOBSUB" : (subtitle->source == CCSUB ? "CC" : "SRT"),
+                    subtitle->dest == RENDERSUB ? "Render/Burn in" : "Pass-Through");
         }
     }
 
@@ -317,7 +314,7 @@ hb_display_job_info( hb_job_t * job )
                 }
             }
 
-            if ( audio->config.out.dynamic_range_compression > 1 && (audio->config.out.codec != HB_ACODEC_AC3) && (audio->config.out.codec != HB_ACODEC_DCA))
+            if ( audio->config.out.dynamic_range_compression && (audio->config.out.codec != HB_ACODEC_AC3) && (audio->config.out.codec != HB_ACODEC_DCA))
             {
                 hb_log("   + dynamic range compression: %f", audio->config.out.dynamic_range_compression);
             }
@@ -370,45 +367,36 @@ static void do_job( hb_job_t * job, int cpu_count )
 
     hb_log( "starting job" );
 
-    if ( job->pixel_ratio == 1 )
-    {
-       /* Correct the geometry of the output movie when using PixelRatio */
-       job->height=title->height-job->crop[0]-job->crop[1];
-       job->width=title->width-job->crop[2]-job->crop[3];
-    }
-    else if ( job->pixel_ratio == 2 )
+    if( job->anamorphic.mode )
     {
+        hb_set_anamorphic_size(job, &job->width, &job->height, &job->anamorphic.par_width, &job->anamorphic.par_height);
 
-        /* While keeping the DVD storage aspect, resize the job width and height
-           so they fit into the user's specified dimensions. */
-        hb_set_anamorphic_size(job, &job->width, &job->height, &job->pixel_aspect_width, &job->pixel_aspect_height);
-    }
-
-    if( job->pixel_ratio && job->vcodec == HB_VCODEC_FFMPEG)
-    {
-        /* Just to make working with ffmpeg even more fun,
-           lavc's MPEG-4 encoder can't handle PAR values >= 255,
-           even though AVRational does. Adjusting downwards
-           distorts the display aspect slightly, but such is life. */
-        while ((job->pixel_aspect_width & ~0xFF) ||
-               (job->pixel_aspect_height & ~0xFF))
+        if( job->vcodec == HB_VCODEC_FFMPEG )
         {
-            job->pixel_aspect_width >>= 1;
-            job->pixel_aspect_height >>= 1;
+            /* Just to make working with ffmpeg even more fun,
+               lavc's MPEG-4 encoder can't handle PAR values >= 255,
+               even though AVRational does. Adjusting downwards
+               distorts the display aspect slightly, but such is life. */
+            while ((job->anamorphic.par_width & ~0xFF) ||
+                   (job->anamorphic.par_height & ~0xFF))
+            {
+                job->anamorphic.par_width >>= 1;
+                job->anamorphic.par_height >>= 1;
+            }
         }
     }
-
+    
     /* Keep width and height within these boundaries,
        but ignore for anamorphic. For "loose" anamorphic encodes,
        this stuff is covered in the pixel_ratio section above.    */
-    if ( job->maxHeight && ( job->height > job->maxHeight ) && ( !job->pixel_ratio ) )
+    if ( job->maxHeight && ( job->height > job->maxHeight ) && ( !job->anamorphic.mode ) )
     {
         job->height = job->maxHeight;
         hb_fix_aspect( job, HB_KEEP_HEIGHT );
         hb_log( "Height out of bounds, scaling down to %i", job->maxHeight );
         hb_log( "New dimensions %i * %i", job->width, job->height );
     }
-    if ( job->maxWidth && ( job->width > job->maxWidth ) && ( !job->pixel_ratio ) )
+    if ( job->maxWidth && ( job->width > job->maxWidth ) && ( !job->anamorphic.mode ) )
     {
         job->width = job->maxWidth;
         hb_fix_aspect( job, HB_KEEP_WIDTH );
@@ -416,16 +404,19 @@ static void do_job( hb_job_t * job, int cpu_count )
         hb_log( "New dimensions %i * %i", job->width, job->height );
     }
 
-    if( ( job->mux & HB_MUX_AVI ) || job->cfr )
+    if( job->mux & HB_MUX_AVI )
     {
-        /* VFR detelecine is not compatible with AVI or constant frame rates. */
-        job->vfr = 0;
+        // The concept of variable frame rate video was a bit too advanced
+        // for Microsoft so AVI doesn't support it. Since almost all dvd
+        // video is VFR we have to convert it to constant frame rate to
+        // put it in an AVI container. So duplicate, drop and
+        // otherwise trash video frames to appease the gods of Redmond.
+        job->cfr = 1;
     }
 
-    if ( job->vfr )
+    if ( job->cfr == 0 )
     {
-        /* Ensure we're using "Same as source" FPS,
-           aka VFR, if we're doing VFR detelecine. */
+        /* Ensure we're using "Same as source" FPS */
         job->vrate_base = title->rate_base;
     }
 
@@ -515,7 +506,7 @@ static void do_job( hb_job_t * job, int cpu_count )
                 }
             }
 
-            if (!job->indepth_scan || job->subtitle_force) {
+            if( !job->indepth_scan || job->subtitle_force ) {
                 /*
                  * Don't add threads for subtitles when we are scanning, unless
                  * looking for forced subtitles.
@@ -530,9 +521,14 @@ static void do_job( hb_job_t * job, int cpu_count )
 
     if( !job->indepth_scan )
     {
-    /* if we are doing passthru, and the input codec is not the same as the output
-     * codec, then remove this audio from the job */
-    /* otherwise, Bad Things will happen */
+    // if we are doing passthru, and the input codec is not the same as the output
+    // codec, then remove this audio from the job. If we're not doing passthru and
+    // the input codec is the 'internal' ffmpeg codec, make sure that only one
+    // audio references that audio stream since the codec context is specific to
+    // the audio id & multiple copies of the same stream will garble the audio
+    // or cause aborts.
+    uint8_t aud_id_uses[MAX_STREAMS];
+    memset( aud_id_uses, 0, sizeof(aud_id_uses) );
     for( i = 0; i < hb_list_count( title->list_audio ); )
     {
         audio = hb_list_item( title->list_audio, i );
@@ -545,6 +541,18 @@ static void do_job( hb_job_t * job, int cpu_count )
             free( audio );
             continue;
         }
+        if ( audio->config.in.codec == HB_ACODEC_FFMPEG )
+        {
+            if ( aud_id_uses[audio->id] )
+            {
+                hb_log( "Multiple decodes of audio id %d, removing track %d",
+                        audio->id, audio->config.out.track );
+                hb_list_rem( title->list_audio, audio );
+                free( audio );
+                continue;
+            }
+            ++aud_id_uses[audio->id];
+        }
         /* Adjust output track number, in case we removed one.
          * Output tracks sadly still need to be in sequential order.
          */
@@ -573,7 +581,8 @@ static void do_job( hb_job_t * job, int cpu_count )
             /* sense-check the requested mixdown */
 
             if( audio->config.out.mixdown == 0 &&
-                audio->config.out.codec != HB_ACODEC_AC3 )
+                audio->config.out.codec != HB_ACODEC_AC3 && 
+                audio->config.out.codec != HB_ACODEC_DCA )
             {
                 /*
                  * Mixdown wasn't specified and this is not pass-through,
@@ -716,7 +725,7 @@ static void do_job( hb_job_t * job, int cpu_count )
         audio->priv.fifo_in   = hb_fifo_init( 32 );
         audio->priv.fifo_raw  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
         audio->priv.fifo_sync = hb_fifo_init( 32 );
-        audio->priv.fifo_out  = hb_fifo_init( FIFO_CPU_MULT * cpu_count );
+        audio->priv.fifo_out  = hb_fifo_init( 8 * FIFO_CPU_MULT * cpu_count );
 
 
         /*
@@ -739,7 +748,8 @@ static void do_job( hb_job_t * job, int cpu_count )
         /*
          * Audio Encoder Thread
          */
-        if( audio->config.out.codec != HB_ACODEC_AC3 )
+        if( audio->config.out.codec != HB_ACODEC_AC3 &&
+            audio->config.out.codec != HB_ACODEC_DCA )
         {
             /*
              * Add the encoder thread if not doing AC-3 pass through