Search code examples
androidffmpeglibavswscale

libav sws_scale() fails colorspace conversion on real device, works on emulator


I'm making a movie player with libav. I have decoding video packets working, I have play in reverse working, I have seeking working. All this works no an x86 android emulator, but fails to work on a real android phone (arm64-v8a)

The failure is in sws_scale() - it returns 0. The video frames continue to be decoded properly with no errors.

There are no errors, warnings, alerts from libav. I have connected an avlog_callback

void log_callback(void *ptr, int level, const char *fmt, va_list vargs) {
    if (level<= AV_LOG_WARNING)
        __android_log_print( level, LOG_TAG, fmt, vargs);
}
uint64_t openMovie( char* path, int rotate, float javaDuration )
{
    av_log_set_level(AV_LOG_WARNING);
    av_log_set_callback(log_callback);

The code to do the sws_scale() is:

int JVM_getBitmapBuffer( JNIEnv* env, jobject thiz, jlong av, jobject bufferAsInt, jbyte transparent ) { 
    avblock *block = (avblock *) av;
    if (!block) {
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  avblock is null");
        return AVERROR(EINVAL);
    }
    if (!block->pCodecCtx) {
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  codecctx is null");
        return AVERROR(EINVAL);
    }

    int width = block->pCodecCtx->width;
    int height = block->pCodecCtx->height;

    if (NULL == block->sws) {
        __android_log_print( ANDROID_LOG_ERROR, LOG_TAG, "getBitmapBuffer:\n  *** invalid sws context ***" );
    }

    int scaleRet = sws_scale( block->sws,
            block->pFrame->data,
            block->pFrame->linesize,
            0,
            height,
            block->pFrameRGB->data,
            block->pFrameRGB->linesize
    );
    if (scaleRet == 0 ) {
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  scale failed");
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  pframe linesize    %d", block->pFrame->linesize[0]); 
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  pframergb linesize %d", block->pFrameRGB->linesize[0]); 
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "  height  %d",
        height);
        return AVERROR(EINVAL);
    }

Setting up the codex and avframes:

//i have tried every combination of 1, 8, 16, and 32 for these values
int alignRGB = 32;
int align    = 16; 
int width    = block->pCodecCtx->width;
int height   = block->pCodecCtx->height;
block->pFrame    = av_frame_alloc();
block->pFrameRGB = av_frame_alloc();

block->pFrameRGBBuffer = av_malloc(
    (size_t)av_image_get_buffer_size(AV_PIX_FMT_RGB32, width, height, alignRGB) 
);

av_image_fill_arrays(
    block->pFrameRGB->data,
    block->pFrameRGB->linesize,
    block->pFrameRGBBuffer,
    AV_PIX_FMT_RGB32,
    width,
    height,
    alignRGB
);

block->pFrameBuffer = av_malloc(
        (size_t) av_image_get_buffer_size(block->pCodecCtx->pix_fmt,
                                          width, height, align
        )
);
av_image_fill_arrays(
    block->pFrame->data,
    block->pFrame->linesize,
    block->pFrameBuffer,
    block->pCodecCtx->pix_fmt,
    width, height,
    align
);
block->sws = sws_getContext(
    width, height,
    AV_PIX_FMT_YUV420P,
    width, height,
    AV_PIX_FMT_RGB32,
    SWS_BILINEAR, NULL, NULL, 0
);

Wildcards are that:

  • I'm using React-Native
  • My emulator is x86 android api 28
  • My real-device is arm64-v8a AOSP (around api 28, don't remember exactly(

Other notes:

I'm pretty certain it has something to do with the pixel alignment. But documentation is practically non-existent on this topic. It could also be the difference between pixel formats RGBA and RGB32, or possibly little-endian vs big-endian.


Solution

  • This is a known bug in FFMPEG on ARM architecture.

    A workaround was posted by mythtv that involves subtracting 1 from the destination width in order to bypass broken optimization code.

    https://code.mythtv.org/trac/ticket/12888

    https://code.mythtv.org/trac/changeset/7de03a90c1b144fc0067261af1c9cfdd8d358972/mythtv

    Reported against FFMPEG 3.2.1 http://trac.ffmpeg.org/ticket/6192

    Still exists in FFMPEG 4.3.2

        int new_width = width;
    #if ARCH_ARM
        // The ARM build of FFMPEG has a bug that if sws_scale is
        // called with source and dest sizes the same, and
        // formats as shown below, it causes a bus error and the
        // application core dumps. To avoid this I make a -1
        // difference in the new width, causing it to bypass
        // the code optimization which is failing.
        if (pix_fmt == AV_PIX_FMT_YUV420P
          && dst_pix_fmt == AV_PIX_FMT_BGRA)
            new_width = width - 1;
    #endif
        d->swsctx = sws_getCachedContext(d->swsctx, width, height, pix_fmt,
                                         new_width, height, dst_pix_fmt,
                                         SWS_FAST_BILINEAR, NULL, NULL, NULL);
    

    UPDATE:

    Building mobile-ffmpeg 4.3.2 with --debug option produces a working binary.

    UPDATE 2:

    The --debug flag does not seem to be working anymore. Must've been some unclean builds. This does seem to be related to alpha channel.

    The only non-alpha RGB format supported by both Android.Bitmap.Config and ffmpeg is RGB_565. Using 565 works with even numbered destination widths.

    in libswscale/yuv2rgb.c:

    SwsFunc ff_yuv2rgb_get_ptr(SwsContext *c) {
       ...
       switch(c->dstFormat) {
       case AV_PIX_FMT_RGBA:
           return (CONFIG_SWSCALE_ALPHA & isALPHA(c->srcFormat)) ? yuva2rgb_c: yuv2rgb_c_32;
       ....
       case AV_PIX_FMT_RGB565:
           return yuv2rgb_c_16_ordered_dither;
       ...
    

    My source is YUV420P, and does not support alpha channels (like yuv A 420p). When my destination format is RGB565, this method seems to not have any problems.

    It's a shame that I can't produce any alpha channel for Android. I guess the real answer is to just use OpenGL and use a surface that supports blitting YUV directly.

    UPDATE 3

    I have found the codepaths in libswscale/swscale_unscaled.c and libswscale/yuv2rgb.c

    If you scale the output then you can convert to RGBA dest format.

    If you add the flag SWS_ACCURATE_RND to your SwsContext then you also avoid the bad codepath.