Skip to content

Commit

Permalink
feat(frame): add quick crop for uyvy format
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Nov 20, 2023
1 parent f4ea0fe commit 835b336
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ffmpeg/frame.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class FFmpeg::Frame
when .rgb48_le?, .rgb48_be?
new_frame = FFmpeg::Frame.new(self)
new_frame.quick_crop_rgb48(top, left, bottom, right)
when .yuyv422?
new_frame = FFmpeg::Frame.new(self)
new_frame.quick_crop_uyvy(top, left, bottom, right)
end
new_frame
end
Expand All @@ -115,6 +118,18 @@ class FFmpeg::Frame
@frame.value.data[2] = @frame.value.data[2] + (chroma_top * @frame.value.linesize[2] + chroma_left) # V plane
end

protected def quick_crop_uyvy(top : Int32, left : Int32, bottom : Int32, right : Int32) : Nil
# Adjust width and height. Note that the width should be an even number.
@frame.value.height = height - top - bottom
@frame.value.width = (width - left - right) & ~1

# Adjust the pointer into the buffer.
# Since UYVY is a packed format, we need to adjust the starting point of the data buffer.
# We also need to ensure left is even to align correctly with the UYVY pairs.
adjusted_left = left & ~1
@frame.value.data[0] = @frame.value.data[0] + (top * @frame.value.linesize[0] + adjusted_left * 2)
end

protected def quick_crop_rgb(top : Int32, left : Int32, bottom : Int32, right : Int32) : Nil
# adjust width and height
@frame.value.height = height - top - bottom
Expand Down

0 comments on commit 835b336

Please sign in to comment.