OSDN Git Service

Fixes loose anamorphic
[handbrake-jp/handbrake-jp-git.git] / libhb / hb.c
index b2a6711..b91bddf 100644 (file)
@@ -1,7 +1,8 @@
 #include "hb.h"
 
-#include "ffmpeg/avcodec.h"
-#include "ffmpeg/swscale.h"
+#include "libavcodec/avcodec.h"
+#include "libavformat/avformat.h"
+#include "libswscale/swscale.h"
 
 struct hb_handle_s
 {
@@ -122,9 +123,7 @@ hb_handle_t * hb_init_real( int verbose, int update_check )
     h->pause_lock = hb_lock_init();
 
     /* libavcodec */
-    avcodec_init();
-    avcodec_register_all();
-    av_register_codec_parser( &mpegaudio_parser);
+    av_register_all();
 
     /* Start library thread */
     hb_log( "hb_init: starting libhb thread" );
@@ -220,6 +219,9 @@ hb_handle_t * hb_init_dl( int verbose, int update_check )
        hb_register( &hb_deca52 );
        hb_register( &hb_decdca );
        hb_register( &hb_decavcodec );
+       hb_register( &hb_decavcodecv );
+       hb_register( &hb_decavcodecvi );
+       hb_register( &hb_decavcodecai );
        hb_register( &hb_declpcm );
        hb_register( &hb_encfaac );
        hb_register( &hb_enclame );
@@ -432,6 +434,116 @@ void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
     avpicture_free( &pic_in );
 }
 
+ /**
+ * Analyzes a frame to detect interlacing artifacts
+ * and returns true if interlacing (combing) is found.
+ *
+ * Code taken from Thomas Oestreich's 32detect filter
+ * in the Transcode project, with minor formatting changes.
+ *
+ * @param buf         An hb_buffer structure holding valid frame data
+ * @param width       The frame's width in pixels
+ * @param height      The frame's height in pixels
+ * @param color_equal Sensitivity for detecting similar colors
+ * @param color_diff  Sensitivity for detecting different colors
+ * @param threshold   Sensitivity for flagging planes as combed
+ * @param prog_equal  Sensitivity for detecting similar colors on progressive frames
+ * @param prog_diff   Sensitivity for detecting different colors on progressive frames
+ * @param prog_threshold Sensitivity for flagging progressive frames as combed
+ */
+int hb_detect_comb( hb_buffer_t * buf, int width, int height, int color_equal, int color_diff, int threshold, int prog_equal, int prog_diff, int prog_threshold )
+{
+    int j, k, n, off, cc_1, cc_2, cc[3], flag[3] ;
+    uint16_t s1, s2, s3, s4;
+    cc_1 = 0; cc_2 = 0;
+
+    int offset = 0;
+    
+    if ( buf->flags & 16 )
+    {
+        /* Frame is progressive, be more discerning. */
+        color_diff = prog_diff;
+        color_equal = prog_equal;
+        threshold = prog_threshold;
+    }
+
+    /* One pas for Y, one pass for Cb, one pass for Cr */    
+    for( k = 0; k < 3; k++ )
+    {
+        if( k == 1 )
+        {
+            /* Y has already been checked, now offset by Y's dimensions
+               and divide all the other values by 2, since Cr and Cb
+               are half-size compared to Y.                               */
+            offset = width * height;
+            width >>= 1;
+            height >>= 1;
+        }
+        else if ( k == 2 )
+        {
+            /* Y and Cb are done, so the offset needs to be bumped
+               so it's width*height + (width / 2) * (height / 2)  */
+            offset *= 5/4;
+        }
+
+        for( j = 0; j < width; ++j )
+        {
+            off = 0;
+
+            for( n = 0; n < ( height - 4 ); n = n + 2 )
+            {
+                /* Look at groups of 4 sequential horizontal lines */
+                s1 = ( ( buf->data + offset )[ off + j             ] & 0xff );
+                s2 = ( ( buf->data + offset )[ off + j + width     ] & 0xff );
+                s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
+                s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
+
+                /* Note if the 1st and 2nd lines are more different in
+                   color than the 1st and 3rd lines are similar in color.*/
+                if ( ( abs( s1 - s3 ) < color_equal ) &&
+                     ( abs( s1 - s2 ) > color_diff ) )
+                        ++cc_1;
+
+                /* Note if the 2nd and 3rd lines are more different in
+                   color than the 2nd and 4th lines are similar in color.*/
+                if ( ( abs( s2 - s4 ) < color_equal ) &&
+                     ( abs( s2 - s3 ) > color_diff) )
+                        ++cc_2;
+
+                /* Now move down 2 horizontal lines before starting over.*/
+                off += 2 * width;
+            }
+        }
+
+        // compare results
+        /*  The final cc score for a plane is the percentage of combed pixels it contains.
+            Because sensitivity goes down to hundreths of a percent, multiply by 1000
+            so it will be easy to compare against the threhold value which is an integer. */
+        cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
+    }
+
+
+    /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
+    int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
+    
+    /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
+    if( average_cc > threshold )
+    {
+#if 0
+            hb_log("Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
+#endif
+        return 1;
+    }
+
+#if 0
+    hb_log("SKIPPED Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
+#endif
+
+    /* Reaching this point means no combing detected. */
+    return 0;
+
+}
+
 /**
  * Calculates job width and height for anamorphic content,
  *
@@ -544,7 +656,9 @@ void hb_set_anamorphic_size( hb_job_t * job,
 
     int pixel_aspect_width = job->pixel_aspect_width;
     int pixel_aspect_height = job->pixel_aspect_height;
-
+    
+    /* If a source was really 704*480 and hard matted with cropping
+       to 720*480, replace the PAR values with the ITU broadcast ones. */
     if (cropped_width <= 706)
     {
         /* Handle ITU PARs */
@@ -557,7 +671,7 @@ void hb_set_anamorphic_size( hb_job_t * job,
                 pixel_aspect_width = 40;
                 pixel_aspect_height = 33;
             }
-            else
+            else if (aspect == 12)
             {
                 /* It's 4:3 */
                 pixel_aspect_width = 10;
@@ -573,7 +687,7 @@ void hb_set_anamorphic_size( hb_job_t * job,
                 pixel_aspect_width = 16;
                 pixel_aspect_height = 11;
             }
-            else
+            else if (aspect == 12)
             {
                 /* It's 4:3 */
                 pixel_aspect_width = 12;
@@ -728,7 +842,7 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
     hb_job_t      * job_copy;
     hb_title_t    * title,    * title_copy;
     hb_chapter_t  * chapter,  * chapter_copy;
-    hb_audio_t    * audio,    * audio_copy;
+    hb_audio_t    * audio;
     hb_subtitle_t * subtitle, * subtitle_copy;
     int             i;
     char            audio_lang[4];
@@ -753,17 +867,11 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
     /* Do nothing about audio during first pass */
     if( job->pass == 0 || job->pass == 2 )
     {
-        for( i = 0; i < 8; i++ )
+        for( i = 0; i < hb_list_count(job->list_audio); i++ )
         {
-            if( job->audios[i] < 0 )
+            if( ( audio = hb_list_item( job->list_audio, i ) ) )
             {
-                break;
-            }
-            if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
-            {
-                audio_copy = malloc( sizeof( hb_audio_t ) );
-                memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
-                hb_list_add( title_copy->list_audio, audio_copy );
+                hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
             }
         }
     }
@@ -794,15 +902,11 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
         /*
          * Find the first audio language that is being encoded
          */
-        for( i = 0; i < 8; i++ )
+        for( i = 0; i < hb_list_count(job->list_audio); i++ )
         {
-            if( job->audios[i] < 0 )
-            {
-                break;
-            }
-            if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
+            if( ( audio = hb_list_item( job->list_audio, i ) ) )
             {
-                strncpy(audio_lang, audio->iso639_2, sizeof(audio_lang));
+                strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
                 break;
             }
         }
@@ -929,6 +1033,7 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
     memcpy( job_copy, job, sizeof( hb_job_t ) );
     title_copy->job = job_copy;
     job_copy->title = title_copy;
+    job_copy->list_audio = title_copy->list_audio;
     job_copy->file  = strdup( job->file );
     job_copy->h     = h;
     job_copy->pause = h->pause_lock;