@@ -37,16 +37,22 @@ std::optional<FrameDims> ResizeTransform::getOutputFrameDims() const {
3737 return outputDims_;
3838}
3939
40+ CropTransform::CropTransform (const FrameDims& dims) : outputDims_(dims) {}
41+
4042CropTransform::CropTransform (const FrameDims& dims, int x, int y)
4143 : outputDims_(dims), x_(x), y_(y) {
4244 TORCH_CHECK (x_ >= 0 , " Crop x position must be >= 0, got: " , x_);
4345 TORCH_CHECK (y_ >= 0 , " Crop y position must be >= 0, got: " , y_);
4446}
4547
4648std::string CropTransform::getFilterGraphCpu () const {
49+ // For the FFmpeg filter crop, if the x and y coordinates are left
50+ // unspecified, it defaults to a center crop.
51+ std::string coordinates = x_.has_value ()
52+ ? (" :" + std::to_string (x_.value ()) + " :" + std::to_string (y_.value ()))
53+ : " " ;
4754 return " crop=" + std::to_string (outputDims_.width ) + " :" +
48- std::to_string (outputDims_.height ) + " :" + std::to_string (x_) + " :" +
49- std::to_string (y_) + " :exact=1" ;
55+ std::to_string (outputDims_.height ) + coordinates + " :exact=1" ;
5056}
5157
5258std::optional<FrameDims> CropTransform::getOutputFrameDims () const {
@@ -69,29 +75,34 @@ void CropTransform::validate(const FrameDims& inputDims) const {
6975 inputDims.width ,
7076 " )" );
7177 TORCH_CHECK (
72- x_ <= inputDims.width ,
73- " Crop x start position, " ,
74- x_,
75- " , out of bounds of input width, " ,
76- inputDims.width );
77- TORCH_CHECK (
78- x_ + outputDims_.width <= inputDims.width ,
79- " Crop x end position, " ,
80- x_ + outputDims_.width ,
81- " , out of bounds of input width " ,
82- inputDims.width );
83- TORCH_CHECK (
84- y_ <= inputDims.height ,
85- " Crop y start position, " ,
86- y_,
87- " , out of bounds of input height, " ,
88- inputDims.height );
89- TORCH_CHECK (
90- y_ + outputDims_.height <= inputDims.height ,
91- " Crop y end position, " ,
92- y_ + outputDims_.height ,
93- " , out of bounds of input height " ,
94- inputDims.height );
78+ x_.has_value () == y_.has_value (),
79+ " Crop x and y values must be both set or both unset" );
80+ if (x_.has_value ()) {
81+ TORCH_CHECK (
82+ x_.value () <= inputDims.width ,
83+ " Crop x start position, " ,
84+ x_.value (),
85+ " , out of bounds of input width, " ,
86+ inputDims.width );
87+ TORCH_CHECK (
88+ x_.value () + outputDims_.width <= inputDims.width ,
89+ " Crop x end position, " ,
90+ x_.value () + outputDims_.width ,
91+ " , out of bounds of input width " ,
92+ inputDims.width );
93+ TORCH_CHECK (
94+ y_.value () <= inputDims.height ,
95+ " Crop y start position, " ,
96+ y_.value (),
97+ " , out of bounds of input height, " ,
98+ inputDims.height );
99+ TORCH_CHECK (
100+ y_.value () + outputDims_.height <= inputDims.height ,
101+ " Crop y end position, " ,
102+ y_.value () + outputDims_.height ,
103+ " , out of bounds of input height " ,
104+ inputDims.height );
105+ }
95106}
96107
97108} // namespace facebook::torchcodec
0 commit comments