From 2eadb9436344cfccdcf9340545d813f4086c0ca4 Mon Sep 17 00:00:00 2001 From: Jason Garrett-Glaser Date: Thu, 18 Nov 2010 08:51:27 -0800 Subject: [PATCH] Support custom cropping rectangles Supposedly useful for 3D television applications. --- common/common.c | 3 +++ encoder/encoder.c | 9 +++++++++ encoder/set.c | 8 ++++---- x264.c | 3 +++ x264.h | 11 ++++++++++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/common/common.c b/common/common.c index 4e3f9342..6c88556d 100644 --- a/common/common.c +++ b/common/common.c @@ -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") diff --git a/encoder/encoder.c b/encoder/encoder.c index c42f48e7..9fe58d05 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -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 ); diff --git a/encoder/set.c b/encoder/set.c index ce1e9e89..6cf0c6a6 100644 --- a/encoder/set.c +++ b/encoder/set.c @@ -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 387d7e1c..bba17b84 100644 --- a/x264.c +++ b/x264.c @@ -680,6 +680,8 @@ static void help( x264_param_t *defaults, int longhelp ) H2( " --nal-hrd 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 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 aa0df35e..ce79d403 100644 --- a/x264.h +++ b/x264.h @@ -39,7 +39,7 @@ #include -#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 */ -- 2.47.3