libhb: allow changing colorimetry in x264 options
authorjstebbins <jstebbins.hb@gmail.com>
Mon, 8 Aug 2011 20:24:30 +0000 (20:24 +0000)
committerjstebbins <jstebbins.hb@gmail.com>
Mon, 8 Aug 2011 20:24:30 +0000 (20:24 +0000)
    Setting "colorprim", "transfer", or "colormarix" in the x264 advanced
    options will no longer be ignored and will propagate to the mp4 muxer
    where those values also get set in the container.

git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4164 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/common.h
libhb/encx264.c
libhb/muxmp4.c
libhb/work.c
test/test.c

index 88bb8389dcd7506839d256aeb92d2d5c5310447c..a042c25fa222b1362d0b45b18e043601a911c89e 100644 (file)
@@ -243,6 +243,9 @@ struct hb_job_s
     int             pass;
     char            *advanced_opts;
     int             areBframes;
+    int             color_matrix_code;
+    int             color_prim;
+    int             color_transfer;
     int             color_matrix;
 
     /* List of audio settings. */
index 350b3a76c8ac0a72d34c5cf0fdd5808163fab09a..e42a3be21ed0cbd7f996a205d6f1dd3873b5cf15 100644 (file)
@@ -132,6 +132,31 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
 
     param.i_log_level  = X264_LOG_INFO;
     
+    /* set up the VUI color model & gamma to match what the COLR atom
+     * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
+    if( job->color_matrix_code == 3 )
+    {
+        // Custom
+        param.vui.i_colorprim = job->color_prim;
+        param.vui.i_transfer  = job->color_transfer;
+        param.vui.i_colmatrix = job->color_matrix;
+    }
+    else if( ( job->color_matrix_code == 2 ) || 
+             ( job->color_matrix_code == 0 && ( job->title->width >= 1280 || job->title->height >= 720 ) ) )
+    {
+        // ITU BT.709 HD content
+        param.vui.i_colorprim = 1;
+        param.vui.i_transfer  = 1;
+        param.vui.i_colmatrix = 1;
+    }
+    else
+    {
+        // ITU BT.601 DVD or SD TV content
+        param.vui.i_colorprim = 6;
+        param.vui.i_transfer  = 1;
+        param.vui.i_colmatrix = 6;
+    }
+
     /*
         This section passes the string advanced_opts to libx264 for parsing into
         parameter names and values.
@@ -185,6 +210,13 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
         free(x264opts_start);
     }
     
+    /* Reload colorimetry settings in case custom values were set
+     * in the advanced_opts string */
+    job->color_matrix_code = 3;
+    job->color_prim = param.vui.i_colorprim;
+    job->color_transfer = param.vui.i_transfer;
+    job->color_matrix = param.vui.i_colmatrix;
+
     /* B-frames are on by default.*/
     job->areBframes = 1;
     
@@ -228,37 +260,6 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
         hb_log( "encx264: min-keyint: %s, keyint: %s", min, max );
     }
 
-    /* set up the VUI color model & gamma to match what the COLR atom
-     * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
-    if( job->color_matrix == 1 )
-    {
-        // ITU BT.601 DVD or SD TV content
-        param.vui.i_colorprim = 6;
-        param.vui.i_transfer = 1;
-        param.vui.i_colmatrix = 6;
-    }
-    else if( job->color_matrix == 2 )
-    {
-        // ITU BT.709 HD content
-        param.vui.i_colorprim = 1;
-        param.vui.i_transfer = 1;
-        param.vui.i_colmatrix = 1;
-    }
-    else if ( job->title->width >= 1280 || job->title->height >= 720 )
-    {
-        // we guess that 720p or above is ITU BT.709 HD content
-        param.vui.i_colorprim = 1;
-        param.vui.i_transfer = 1;
-        param.vui.i_colmatrix = 1;
-    }
-    else
-    {
-        // ITU BT.601 DVD or SD TV content
-        param.vui.i_colorprim = 6;
-        param.vui.i_transfer = 1;
-        param.vui.i_colmatrix = 6;
-    }
-
     if( job->anamorphic.mode )
     {
         param.vui.i_sar_width  = job->anamorphic.par_width;
index 05bce561a27a30338c760b7273ee39acf419b87c..c1ac9994c7610033401df7e28ff6c6daa19ec4d8 100644 (file)
@@ -218,23 +218,19 @@ static int MP4Init( hb_mux_object_t * m )
     // Per the notes at:
     //   http://developer.apple.com/quicktime/icefloe/dispatch019.html#colr
     //   http://forum.doom9.org/showthread.php?t=133982#post1090068
-    // the user can set it from job->color_matrix, otherwise by default
+    // the user can set it from job->color_matrix_code, otherwise by default
     // we say anything that's likely to be HD content is ITU BT.709 and
     // DVD, SD TV & other content is ITU BT.601.  We look at the title height
     // rather than the job height here to get uncropped input dimensions.
-    if( job->color_matrix == 1 )
+    if( job->color_matrix_code == 3 )
     {
-        // ITU BT.601 DVD or SD TV content
-        MP4AddColr(m->file, mux_data->track, 6, 1, 6);
+        // Custom
+        MP4AddColr(m->file, mux_data->track, job->color_prim, job->color_transfer, job->color_matrix);        
     }
-    else if( job->color_matrix == 2 )
+    else if( ( job->color_matrix_code == 2 ) || 
+             ( job->color_matrix_code == 0 && ( job->title->width >= 1280 || job->title->height >= 720 ) ) )
     {
         // ITU BT.709 HD content
-        MP4AddColr(m->file, mux_data->track, 1, 1, 1);        
-    }
-    else if ( job->title->width >= 1280 || job->title->height >= 720 )
-    {
-        // we guess that 720p or above is ITU BT.709 HD content
         MP4AddColr(m->file, mux_data->track, 1, 1, 1);
     }
     else
index 222ae240f1d436961ce79355ac8e0133b22ee927..9ab11fec9b9f43e46250efac23ec05c5df2c169f 100644 (file)
@@ -194,8 +194,8 @@ void 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)");
+            if( job->color_matrix_code )
+                hb_log( "     + custom color matrix: %s", job->color_matrix_code == 1 ? "ITU Bt.601 (SD)" : job->color_matrix_code == 2 ? "ITU Bt.709 (HD)" : "Custom" );
             break;
 
         case HB_MUX_MKV:
index 4fd7285376840a98ae7f30d77b38fd2242bf7e6e..55144dd3ae6db08f12bd1f1db34cde58e10bf452 100644 (file)
@@ -123,7 +123,7 @@ static char * preset_name   = 0;
 static int    cfr           = 0;
 static int    mp4_optimize  = 0;
 static int    ipod_atom     = 0;
-static int    color_matrix  = 0;
+static int    color_matrix_code = 0;
 static int    preview_count = 10;
 static int    store_previews = 0;
 static int    start_at_preview = 0;
@@ -2229,9 +2229,9 @@ static int HandleEvents( hb_handle_t * h )
 
             job->file = strdup( output );
 
-            if( color_matrix )
+            if( color_matrix_code )
             {
-                job->color_matrix = color_matrix;
+                job->color_matrix_code = color_matrix_code;
             }
 
             if( advanced_opts != NULL && *advanced_opts != '\0' )
@@ -3461,9 +3461,9 @@ static int ParseOptions( int argc, char ** argv )
                 break;
             case 'M':
                 if( atoi( optarg ) == 601 )
-                    color_matrix = 1;
+                    color_matrix_code = 1;
                 else if( atoi( optarg ) == 709 )
-                    color_matrix = 2;
+                    color_matrix_code = 2;
                 break;
             case MIN_DURATION:
                 min_title_duration = strtol( optarg, NULL, 0 );