sws_scale screws up last pixel row

For the developers that use FFmpeg in their software.
Post Reply
TheSHEEEP
Posts: 34
Joined: Fri Sep 16, 2011 9:12 am
Location: Berlin

sws_scale screws up last pixel row

Post by TheSHEEEP » Fri Jul 20, 2012 1:35 pm

Hey,

I am muxing pictures in the PIX_FMT_ARGB format into an mp4 video.

All of it works well, except that the last pixel row of the outgoing image is screwed up, in most cases the last row is completely black, sometimes there are other colors, it seems somehow dependant on the machine it runs on.

I am absolutely sure that the error must be in sws_scale, as I am saving the images before and after the scaling. The input images do not have the error, but after the sws_scale() I save the yuv image and the error is apparent.

Here is an example:
Original:
Image
Yuvfile after sws_scale:
Image

At the bottom of the Yuvfile, you will see the black row.

This is how I do the scaling (it is after the official ffmpeg examples, more or less):

Code: Select all

static int sws_flags = SWS_FAST_BILINEAR | SWS_ACCURATE_RND; 

if (img_convert_ctx == NULL) 
			{
				img_convert_ctx = sws_getContext( srcWidth, srcHeight,
					PIX_FMT_ARGB,
					codecContext->width, codecContext->height,
					codecContext->pix_fmt,
					sws_flags, NULL, NULL, NULL );
				if (img_convert_ctx == NULL) 
				{
					av_log(c, AV_LOG_ERROR, "%s","Cannot initialize the conversion context\n");
					exit(1);
				}
			}
			fill_image(tmp_picture, pic, pic_size, frame_count, ptr->srcWidth, ptr->srcHeight );

			sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize,
				0, srcHeight, picture->data, picture->linesize);
I also tried a vast amount of different SWS_ flags, but all yield the same result.
Could this be a bug in sws_scale or am I doing something wrong?

I am using the latest version of the ffmpeg libraries.
Last edited by TheSHEEEP on Mon Jul 23, 2012 11:26 am, edited 1 time in total.

ramiro
Posts: 157
Joined: Tue May 10, 2011 12:56 am

Re: sws_scale screws up last pixel row

Post by ramiro » Fri Jul 20, 2012 6:33 pm

it's most likely a bug in sws_scale. fast_bilinear sucks a bunch, try avoiding it. we need to know what algorithm was used by swscale (turn on swscale debugging through sws_flags). we also need to know the output pixel format (I suppose it's yuv420), and how you are taking the output yuv file and turning it into the image you uploaded.

TheSHEEEP
Posts: 34
Joined: Fri Sep 16, 2011 9:12 am
Location: Berlin

Re: sws_scale screws up last pixel row

Post by TheSHEEEP » Mon Jul 23, 2012 7:31 am

I also tried "normal" BILINEAR and also BICUBIC, but it didn't matter.
The output format is YUV420P.

I save the image like this:

Code: Select all

ofstream testFile;
testFile.open(buffer, ios_base::binary | ios_base::out | ios_base::trunc);
testFile.write((char*)ptr->picture->data[0], ptr->picture->linesize[0]*srcHeight);
testFile.write((char*)ptr->picture->data[1], ptr->picture->linesize[1]*srcHeight/2);
testFile.write((char*)ptr->picture->data[2], ptr->picture->linesize[2]*srcHeight/2);
testFile.close();
I then open that file in IrfanView and save it as jpg.

How can I tell you the algorithm? I see no SWS_DEBUG flag or something like that.
Last edited by TheSHEEEP on Mon Jul 23, 2012 11:27 am, edited 1 time in total.

TheSHEEEP
Posts: 34
Joined: Fri Sep 16, 2011 9:12 am
Location: Berlin

Re: sws_scale screws up last pixel row

Post by TheSHEEEP » Mon Jul 23, 2012 8:24 am

A user on stackoverflow suggested trying out 184 and 192 as resolution (as it is a multiple of 8/16), but it made no difference.

Also, I just checked again and found out that the problem indeed also happens at 640x360 resolution. I'll dit my original post. So this is not a size-related problem.

Edit:

Aaannd solved.
The probled was the fill_image function, which indead skipped one row. Not sure why we didn't notice that before (maybe the new ffmpeg version made a change that let us notice this), but fixing the function fixed the problem. :)

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

Re: sws_scale screws up last pixel row

Post by rogerdpack » Wed Jul 25, 2012 4:32 pm

is fill_image your function? If it's broken in ffmpeg trunk could you file a trac ticket?
Thx.

Post Reply
'