Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#12888 closed Bug Report - Crash (fixed)

Memory Alignment Bug when calling swscale

Reported by: Robert Allan <rallan@…> Owned by: Peter Bennett
Priority: minor Milestone: unknown
Component: Ports - rPi Version: 0.28.0
Severity: medium Keywords:
Cc: Ticket locked: no


There is a memory misalignment bug when calling swscale.cpp from mythavutil.cpp. This shows itself a ‘Bus Error’ when run on an Arm based Raspberry Pi3. Mythpreviewgen currently causes this failure and previews cannot be generated.

Whilst memory misalignment appears to be a well known fault with swscale due to the packed format, I cannot work out what needs to be changed in the calling routine to wrap the buffers to get the correct alignment.

The offending line is (as found by putting diagnostic output above and below):

sws_scale(d->swsctx, src->data, src->linesize, 0, height, dst->data, dst->linesize);

in function

int MythAVCopy::Copy(AVPicture *dst, AVPixelFormat dst_pix_fmt, const AVPicture *src, AVPixelFormat pix_fmt, int width, int height)

Web searching appears to indicate that the function avcodec_align_dimensions2 should be called but this requires a AVCodecContext object in the call and where to find this is not readily apparent to me.

MythTV is version 0.28 with many of the subsequent fixes indicated by Trac implemented. My compiler is gcc v4.9.2, being the latest readily available on the Raspberry Pi.

Any help would be much appreciated.

Change History (12)

comment:1 Changed 5 years ago by Peter Bennett

Raspberry Pi support is really only for the frontend at this point, so I have not looked at this. However, sws_scale is used at some places in the frontend. It is probably only used if you are using default playback profiles, such as "Slim". The raspberry pi actually plays surprisingly well using the "Slim" profile, I just tried it. It looks like it is using sws_scale to resize frames and display them using XVideo.

Look at these and search for sws_scale

If you solve the problem with Mythpreviewgen please provide a patch here so that I can include it in the main code base.

comment:2 Changed 5 years ago by Peter Bennett

Component: MythTV - GeneralPorts - rPi
Owner: set to Peter Bennett
Status: newassigned

comment:3 Changed 5 years ago by Robert Allan <rallan@…>

Yes I had detected errors in other parts of MythTv? but could not guarantee them so isolated it to Mythpreviewgen which generates a repeatable fault.

I have subsequently further identified the fault to be on the 1st line of the image in swscale.c where RA_FIX puts in debug code. See below

copy strides, so they can safely be modified

if (c->sliceDir == 1) {

slices go from top to bottom int srcStride2[4] = { srcStride[0], srcStride[1], srcStride[2],

srcStride[3] };

int dstStride2[4] = { dstStride[0], dstStride[1], dstStride[2],

dstStride[3] };

reset_ptr(src2, c->srcFormat); reset_ptr((void*)dst2, c->dstFormat);

/* reset slice direction at end of frame */ if (srcSliceY + srcSliceH == c->srcH)

c->sliceDir = 0;





#ifndef RA_FIX

ret = c->swscale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2,



ret = 1;





Commenting out the conflicting line enables the rest of the image to be created. Shame that the resultant picture is not a valid picture format (presumably missing header info)! Previews are also now generated for all recordings, but again unreadable.

comment:4 Changed 5 years ago by Robert Allan <rallan@…>

Further investigation indicates that the bug is related to ffmpeg and its implementation of neon for Raspberry Pi3. A variable c->swscale is used as a function pointer and when this is set by the line

SET_FF_NVX_TO_ALL_RGBX_FUNC(yuv420p, YUV420P, accurate_rnd);

near the bottom of .../external/FFmpeg/libswscale/arm/swscale_unscaled.c, the bus error occurs. Commenting out this line enables previews to be generated but as neon optimisation has been effectively removed, live TV decoding is very jerky and unusable.

Debugging further is probably too hard for me as everything is written as macros and then calls into 'macro'ed arm assembler code. I cannot find anything on web regarding this error on ffmpeg. There is a note that detecting arm v8 architecture is not correctly set for Pi3 but my understanding is that neon implementation is unaffected.

Further background info: a) Raspberry Pi3 with MPEG2 license b) Using test command : mythpreviewgen --loglevel debug --size 0x0 --chanid 1001 --starttime 20160430204000 --outfile /hdd/dvr/recordings/preview.jpg

comment:5 Changed 5 years ago by acp@…

I am able to replicate this on both the Pi3 and the Pi2 as well without the MPEG-2 license.

comment:6 Changed 4 years ago by Peter Barker <pb@…>

I have seen same error on an another ARMv7 computer, an ODROID XU-4, reported in ticket #12967

comment:7 Changed 4 years ago by rallan@…

I installed Mythtv from scratch on another Raspberry Pi3 and repeated the fault. My original hack then enabled previews.

comment:8 Changed 4 years ago by Peter Bennett <pbennett@…>

Resolution: fixed
Status: assignedclosed

In 57d61f7412e5caf9b91772926bdb651354205581/mythtv:

Fix Bus Error in mythpreviewgen on Raspberry Pi

Fixes #12888

comment:9 Changed 4 years ago by Peter Bennett <pbennett@…>

In 7de03a90c1b144fc0067261af1c9cfdd8d358972/mythtv:

Fix Bus Error in mythpreviewgen on Raspberry Pi

Fixes #12888

(cherry picked from commit 57d61f7412e5caf9b91772926bdb651354205581)

comment:10 Changed 4 years ago by rallan@…

Well done. I copied the patch to my two Mythtv implementations and it worked on both. Unlike a QA guy on a former project, I won't be counting the pixels (albeit in pre HD days)!

comment:11 Changed 4 years ago by Peter Bennett

comment:12 Changed 3 years ago by Peter Bennett

Owner: changed from Peter Bennett to Peter Bennett
Note: See TracTickets for help on using tickets.