我正在用 libav 制作电影播放器。我正在解码视频数据包,我正在反向工作,我正在寻找工作。所有这些都无法在 x86 android 模拟器上运行,但无法在真正的 android 手机上运行(arm64-v8a)
失败在 sws_scale()
- 它返回 0。视频帧继续正确解码,没有错误。
libav 没有错误、警告和警报。我已连接 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);
执行 sws_scale()
的代码是: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);
}
设置 codex 和 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
);
通配符是:其他注意事项:
block
是一个简单的 C 结构,带有指向相关 AV 内存结构的指针。 我很确定它与像素对齐有关。但是关于这个主题的文档实际上是不存在的。它也可能是像素格式 RGBA 和 RGB32 之间的差异,或者可能是小端与大端之间的差异。
最佳答案
这是 ARM 架构上的 FFMPEG 中的一个已知错误。
mytv 发布了一个解决方法,其中涉及从目标宽度中减去 1 以绕过损坏的优化代码。
https://code.mythtv.org/trac/ticket/12888
https://code.mythtv.org/trac/changeset/7de03a90c1b144fc0067261af1c9cfdd8d358972/mythtv
针对 FFMPEG 3.2.1 报告
http://trac.ffmpeg.org/ticket/6192
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);
更新:使用
--debug
构建 mobile-ffmpeg 4.3.2选项产生一个工作二进制文件。更新 2:
--debug 标志似乎不再起作用。一定是一些不干净的构建。这似乎与 alpha channel 有关。
Android.Bitmap.Config 和 ffmpeg 唯一支持的非 alpha RGB 格式是 RGB_565。使用 565 适用于偶数目标宽度。
在
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;
...
我的源是 YUV420P,不支持 alpha channel (如 yuv A 420p)。当我的目标格式是 RGB565 时,这种方法似乎没有任何问题。很遗憾我无法为 Android 制作任何 Alpha channel 。我想真正的答案是只使用 OpenGL 并使用直接支持 Blitting YUV 的表面。
更新 3
我在
libswscale/swscale_unscaled.c
中找到了代码路径和 libswscale/yuv2rgb.c
如果缩放输出,则可以转换为 RGBA dest 格式。如果您将标志 SWS_ACCURATE_RND 添加到您的 SwsContext 中,那么您也可以避免错误的代码路径。
关于android - libav sws_scale() 在真实设备上的色彩空间转换失败,适用于模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63549287/