]> git.sesse.net Git - x264/commitdiff
Support custom cropping rectangles
authorFiona Glaser <fiona@x264.com>
Thu, 18 Nov 2010 16:51:27 +0000 (08:51 -0800)
committerFiona Glaser <fiona@x264.com>
Fri, 19 Nov 2010 22:48:12 +0000 (14:48 -0800)
Supposedly useful for 3D television applications.

common/common.c
encoder/encoder.c
encoder/set.c
x264.c
x264.h

index 4e3f9342e4fa30478e1e2283f8c944ac9726b33d..6c88556d32aca0353c2316d185d64c8d73bd9d22 100644 (file)
@@ -952,6 +952,9 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
         p->rc.f_complexity_blur = atof(value);
     OPT("zones")
         p->rc.psz_zones = strdup(value);
+    OPT("crop-rect")
+        b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top,
+                                                 &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4;
     OPT("psnr")
         p->analyse.b_psnr = atobool(value);
     OPT("ssim")
index 83a2b115b4c7ef7561b507081fd816bb14cc695b..b0851038dfed629769ea4d88b12c02935c31f439 100644 (file)
@@ -417,6 +417,14 @@ static int x264_validate_parameters( x264_t *h )
         return -1;
     }
 
+    if( (h->param.crop_rect.i_left + h->param.crop_rect.i_right ) >= h->param.i_width ||
+        (h->param.crop_rect.i_top  + h->param.crop_rect.i_bottom) >= h->param.i_height )
+    {
+        x264_log( h, X264_LOG_ERROR, "invalid crop-rect %u,%u,%u,%u\n", h->param.crop_rect.i_left,
+                  h->param.crop_rect.i_top, h->param.crop_rect.i_right,  h->param.crop_rect.i_bottom );
+        return -1;
+    }
+
     if( h->param.i_threads == X264_THREADS_AUTO )
         h->param.i_threads = x264_cpu_num_processors() * (h->param.b_sliced_threads?2:3)/2;
     h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
@@ -1210,6 +1218,7 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
     COPY( analyse.b_mixed_references );
     COPY( analyse.f_psy_rd );
     COPY( analyse.f_psy_trellis );
+    COPY( crop_rect );
     // can only twiddle these if they were enabled to begin with:
     if( h->param.analyse.i_me_method >= X264_ME_ESA || param->analyse.i_me_method < X264_ME_ESA )
         COPY( analyse.i_me_method );
index ce1e9e89b512c764487618e95d94c9425d4fa51e..6cf0c6a6f964d7f25b61f6443f98e07570b810be 100644 (file)
@@ -192,10 +192,10 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
     sps->b_mb_adaptive_frame_field = param->b_interlaced;
     sps->b_direct8x8_inference = 1;
 
-    sps->crop.i_left   = 0;
-    sps->crop.i_top    = 0;
-    sps->crop.i_right  = sps->i_mb_width*16 - param->i_width;
-    sps->crop.i_bottom = (sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
+    sps->crop.i_left   = param->crop_rect.i_left;
+    sps->crop.i_top    = param->crop_rect.i_top;
+    sps->crop.i_right  = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
+    sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
                   sps->crop.i_right || sps->crop.i_bottom;
 
diff --git a/x264.c b/x264.c
index e8de506bcb6b89204b4810e11f3ee6896e9bf21c..c9222a3da2c03483717fa129c3088055e54fbb8e 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -680,6 +680,8 @@ static void help( x264_param_t *defaults, int longhelp )
     H2( "      --nal-hrd <string>      Signal HRD information (requires vbv-bufsize)\n"
         "                                  - none, vbr, cbr (cbr not allowed in .mp4)\n" );
     H2( "      --pic-struct            Force pic_struct in Picture Timing SEI\n" );
+    H2( "      --crop-rect <string>    Add 'left,top,right,bottom' to the bitstream-level\n"
+        "                              cropping rectangle\n" );
 
     H0( "\n" );
     H0( "Input/Output:\n" );
@@ -913,6 +915,7 @@ static struct option long_options[] =
     { "tcfile-out",  required_argument, NULL, OPT_TCFILE_OUT },
     { "timebase",    required_argument, NULL, OPT_TIMEBASE },
     { "pic-struct",        no_argument, NULL, 0 },
+    { "crop-rect",   required_argument, NULL, 0 },
     { "nal-hrd",     required_argument, NULL, 0 },
     { "pulldown",    required_argument, NULL, OPT_PULLDOWN },
     { "fake-interlaced",   no_argument, NULL, 0 },
diff --git a/x264.h b/x264.h
index 2183de333869a47ca6b37b7c4bfcb14fb3aee636..3d07321ade4c8483131373edc841d69d6a9fa2b3 100644 (file)
--- a/x264.h
+++ b/x264.h
@@ -39,7 +39,7 @@
 
 #include <stdarg.h>
 
-#define X264_BUILD 107
+#define X264_BUILD 108
 
 /* x264_t:
  *      opaque handler for encoder */
@@ -379,6 +379,15 @@ typedef struct x264_param_t
         char        *psz_zones;     /* alternate method of specifying zones */
     } rc;
 
+    /* Cropping Rectangle parameters: added to those implicitly defined by
+       non-mod16 video resolutions. */
+    struct {
+        unsigned int i_left;
+        unsigned int i_top;
+        unsigned int i_right;
+        unsigned int i_bottom;
+    } crop_rect;
+
     /* Muxing parameters */
     int b_aud;                  /* generate access unit delimiters */
     int b_repeat_headers;       /* put SPS/PPS before each keyframe */