Opened 2 years ago

Last modified 2 years ago

#9985 new defect

Flipping an image with sws_scale crashes in arm64

Reported by: diogo.r Owned by:
Priority: normal Component: swscale
Version: git-master Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:

Not sure if this is a bug, or if I'm just using the API in a way it was not meant to be used. I'm trying to convert and flip an image with sws_scale. From my tests, this works perfectly fine in windows and macos-x86_64, but blows up in macos-arm64. I've reproduced it with ffmpeg 5.1.1

How to reproduce:

Compile and run this on a mac with a new arm64 M1 processor, it should crash every time.

extern "C"
{
	#include <libswscale/swscale.h>
}

int main() {
    const int planes = 8;
    const int width = 720;
    const int height = 480;
    const int padding = 0;

    AVPixelFormat srcFormat = AVPixelFormat::AV_PIX_FMT_YUV420P;
    int srcStride[planes] = {
        padding + width,
        padding + width / 2,
        padding + width / 2
    };
    
    uint8_t *src[planes] = {
        new uint8_t[srcStride[0] * height],
        new uint8_t[srcStride[1] * height],
        new uint8_t[srcStride[2] * height],
    };

    AVPixelFormat dstFormat = AVPixelFormat::AV_PIX_FMT_BGRA;
    int dstStride[planes] = {
        padding + width * 4
    };

    uint8_t *dst[planes] = {
        new uint8_t[dstStride[0] * height]
    };

    struct SwsContext* sws_ctx = sws_getContext(
        width,
        height,
        srcFormat,
        width,
        height,
        dstFormat,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL
    );

    // Flip image
    dst[0] = dst[0] + (height - 1) * dstStride[0];
    dstStride[0] = -dstStride[0];

    sws_scale(
        sws_ctx,
        src,
        srcStride,
        0,
        height,
        dst,
        dstStride    
    );
}

Thanks for the help, and for your efforts to develop such a cool project : )

Change History (2)

comment:1 by mkver, 2 years ago

Can you provide a backtrace? Can you also check if the crash still happens if you call av_force_cpu_flags(0) at the beginning (this will disable CPU-specific assembly code)?

comment:2 by diogo.r, 2 years ago

Nope, it doesn't crash anymore if I call av_force_cpu_flags(0).

Here's the backtrace:

0   ffmpeg                              0x000000010478636c main + 348
1   ffmpeg                              0x0000000104788810 ff_get_unscaled_swscale_aarch64 + 2264
2   dyld                                0x0000000104b4d08c start + 520

Or in a different format:

ffmpeg_g!ff_yuv420p_to_bgra_neon (~/ffmpeg/libswscale/aarch64/yuv2rgb_neon.S:211)
ffmpeg_g!yuv420p_to_bgra_neon_wrapper (~/ffmpeg/libswscale/aarch64/swscale_unscaled.c:61)
ffmpeg_g!scale_internal (~/ffmpeg/libswscale/swscale.c:1040)
ffmpeg_g!sws_scale (~/ffmpeg/libswscale/swscale.c:1212)
ffmpeg_g!main (~/ffmpeg/fftools/ffmpeg.c:71)
dyld!start (Unknown Source:0)
Last edited 2 years ago by diogo.r (previous) (diff)
Note: See TracTickets for help on using tickets.