av_picture_crop and sws_scale

For the developers that use FFmpeg in their software.
Post Reply
cugar
Posts: 2
Joined: Tue Jun 30, 2015 1:27 pm

av_picture_crop and sws_scale

Post by cugar » Mon Aug 17, 2015 12:11 pm

Hello all,
I am seeing a crash in sws_scale when scaling from a frame that has cropping applied to it.
Please let me know what I am doing wrong.
There are two cases described below, the code is the same for both (see code at the bottom).

User wants to crop a portion of the frame, and scale it into their buffer.
We have decoded a frame as YUV420P, 1920x1080
Cropping is specified as top=0, left=0, width=100, height=100

First case:
User's buffer is allocated as 1920x1080
First we update the frame with the crop rectangle using av_picture_crop.
Then we pass through sws_scale to copy/scale the required portion of the frame into the user's buffer.
This works; we get the 100x100 pixel rectangle scaled into the 1920x1080 buffer.

Second case:
User's buffer is allocated as 100x100
First we update the frame with the crop rectangle using av_picture_crop.
Then we pass through sws_scale to copy/scale the required portion of the frame into the user's buffer; should be a straight copy now, no scaling.
This does not work, application crash in swscale:
First-chance exception at 0x55E3E4E1 (swscale-3.dll) in sample.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.
Unhandled exception at 0x55E3E4E1 (swscale-3.dll) in sample.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.

Code: Select all

if(cropping)
{
// apply cropping, updates the start pointers in the frame
av_picture_crop( (AVPicture*)frame, (AVPicture*)frame, frame->format, 0, 0 );
frame.width = 100;
frame.height = 100;
}
// scale into user's buffer
context = sws_getContext( frame.width, frame.height, frame->format, userbufferwidth, userbufferheight, userbufferformat, 0, 0, 0, 0 );
sws_scale( context, frame.data, frame.linesize, 0, frame.height, userbuffer, userbufferstride );

In the second case, if I call sws_getContext with userbufferheight+1, it works; seems that userbufferheight cannot be the same as frame.height ..?
Normal usage without cropping, never see any issue using userbufferheight equal to frame.height.


Thank you


EDIT: it appears that this error occurs when the userbufferformat is NV12, but succeeds for other formats.
There is still a problem here though, this case is not closed!

rogerdpack
Posts: 1876
Joined: Fri Aug 05, 2011 9:56 pm

Re: av_picture_crop and sws_scale

Post by rogerdpack » Thu Sep 10, 2015 7:43 pm

perhaps repost this to libav-user mailing list...
https://www.ffmpeg.org/contact.html

User avatar
Hank
Posts: 1
Joined: Wed Jun 14, 2017 8:29 am

Re: av_picture_crop and sws_scale

Post by Hank » Thu Jun 15, 2017 3:50 am

When the userbufferformat is NV12, width and height of buffer must be aligned, a multiple of 32.
In the second case, 100x100 should be allocated as 128x128.

Post Reply