Android MediaCodec编码器产生黑色&;某些设备中的白色视频

Android MediaCodec编码器产生黑色&;某些设备中的白色视频,android,video,bitmap,android-mediacodec,yuv,Android,Video,Bitmap,Android Mediacodec,Yuv,我正在使用API对一组位图中的视频进行编码 由于最低项目API是22,我正在使用它来获得一个合适的编码器 问题是一些用户报告说输出的视频结果是黑白的,而不是彩色的。 通过使用以下算法将推入编码器的位图转换为YUV。 我最好的猜测是,在受影响的设备中,编码器可能需要特定的YUV格式 问题是,如何找到编码器期望的YUV格式?还有,难道没有一种通用的YUV格式可以适用于所有设备吗 用于将位图转换为YUV的算法: void toYuv(const uint32\u t*argb、int8\u t*yuv

我正在使用API对一组位图中的视频进行编码

由于最低项目API是22,我正在使用它来获得一个合适的编码器

问题是一些用户报告说输出的视频结果是黑白的,而不是彩色的。

通过使用以下算法将推入编码器的位图转换为YUV。 我最好的猜测是,在受影响的设备中,编码器可能需要特定的YUV格式

问题是,如何找到编码器期望的YUV格式?还有,难道没有一种通用的YUV格式可以适用于所有设备吗

用于将位图转换为YUV的算法:

void toYuv(const uint32\u t*argb、int8\u t*yuv、const uint32\u t宽度、const uint32\u t高度)
{
int32_t索引_y=0;
int32_t index_uv=宽度*高度;
int32_t指数=0;
对于(int j=0;j>0;
常量int G=(argb[index]&0xFF00)>>8;
常量intb=(argb[index]&0xFF0000)>>16;
常数Y=(((R*77)+(G*150))+(B*29))+128)>>8;
const int U=((((((R*127)-(G*106))-(B*21))+128)>>8)+128;
常量int V=((((((R*-43)-(G*84))+(B*127))+128>>8)+128;
yuv[index_y++]=static_cast((y<0)?0:((y>255)?255:y));
如果((j%2)==0)和((索引%2)==0))
{
yuv[index_uv++]=静态投射((V<0)?0:((V>255)?255:V));
yuv[index_uv++]=静态_投射((U<0)?0:((U>255)?255:U));
}
索引++;
}
}
}

这种转换极不可能成为问题所在。更可能的问题在于色度二次采样。在哪里从4:4:4转换到4:2:0?视频的编码方式是使用问题中显示的算法将每个位图(从ARGB)转换为YUV420,然后将生成的YUV420字节数组排队到带有QueueInputBuffer的MediaCodec。这段代码很难理解(有太多临时变量)。看起来您正在尝试打包的YUV。通过查看
fourcc.org
和ffmpeg
AV_PIX_FMT.*
我没有看到任何打包的4:2:0。99%的编码器使用
I420
planar4:2:0。有什么理由不起作用吗?
void toYuv(const uint32_t* argb, int8_t* yuv, const uint32_t width, const uint32_t height)
{
    int32_t index_y = 0;
    int32_t index_uv = width * height;
    int32_t index = 0;

    for (int j = 0; j < height; j++)
    {
        for (int i = 0; i < width; i++)
        {
            const int R = (argb[index] & 0x0000FF) >> 0;
            const int G = (argb[index] & 0xFF00) >> 8;
            const int B = (argb[index] & 0xFF0000) >> 16;

            const int Y = ((((R * 77) + (G * 150)) + (B * 29)) + 128) >> 8;
            const int U = (((((R * 127) - (G * 106)) - (B * 21)) + 128) >> 8) + 128;
            const int V = (((((R * -43) - (G * 84)) + (B * 127)) + 128) >> 8) + 128;

            yuv[index_y++] = static_cast<int8_t>((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));

            if (((j % 2) == 0) && ((index % 2) == 0))
            {
                yuv[index_uv++] = static_cast<int8_t>((V < 0) ? 0 : ((V > 255) ? 255 : V));
                yuv[index_uv++] = static_cast<int8_t>((U < 0) ? 0 : ((U > 255) ? 255 : U));
            }

            index++;
        }
    }
}