编辑
我检查了执行,发现错误不是(还)在 swscale 点。我当前的问题是找不到 JPG 图像:
No such file or directory
当avformat_open_input(&pFormatCtx, imageFileName, NULL, NULL);
你告诉我我需要注册任何东西之前,我可以告诉我已经注册了(我更新了下面的代码)。
我还添加了访问外部存储的 Android 权限(我认为它与 Android 无关,因为我已经可以写入图像所在的 /mnt/sdcard/)
END EDIT
我已经阅读了几个教程(包括从 SO 发布的几个教程,即http://dranger.com/ffmpeg/,如何为 Android 编译 ffmpeg...,通过 dolphin-player 源代码)。这是我所拥有的:
。为 android 编译 ffmpeg
。运行基本教程,使用 NDK 在我的 android 设备上创建虚拟视频
。能够使用上面的虚拟视频代码的修改版本和大量谷歌搜索从 Ubuntu 上的图像生成 MPEG2 视频
。在 Android 设备上运行新代码会提供绿屏视频(无论我编码多少帧,持续时间为 1 秒)
我在类似情况下看到另一篇关于 iPhone 的帖子,其中提到 ARM 处理器优化可能是罪魁祸首。我尝试了一些 ldextra-flags(-arch armv7-a 和类似的)都没有成功。
我在最后包含了加载图像的代码。Android 和 linux 有什么不同吗?我的 ffmpeg 构建是否不适合 Android 视频编码?
void copyFrame(AVCodecContext *destContext, AVFrame* dest,
AVCodecContext *srcContext, AVFrame* source) {
struct SwsContext *swsContext;
swsContext = sws_getContext(srcContext->width, srcContext->height, srcContext->pix_fmt,
destContext->width, destContext->height, destContext->pix_fmt,
SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(swsContext, source->data, source->linesize, 0, srcContext->height, dest->data, dest->linesize);
sws_freeContext(swsContext);
}
int loadFromFile(const char* imageFileName, AVFrame* realPicture, AVCodecContext* videoContext) {
AVFormatContext *pFormatCtx = NULL;
avcodec_register_all();
av_register_all();
int ret = avformat_open_input(&pFormatCtx, imageFileName, NULL, NULL);
if (ret != 0) {
// ERROR hapening here
// Can't open image file. Use strerror(AVERROR(ret))) for details
return ERR_CANNOT_OPEN_IMAGE;
}
AVCodecContext *pCodecCtx;
pCodecCtx = pFormatCtx->streams[0]->codec;
pCodecCtx->width = W_VIDEO;
pCodecCtx->height = H_VIDEO;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
// Find the decoder for the video stream
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (!pCodec) {
// Codec not found
return ERR_CODEC_NOT_FOUND;
}
// Open codec
if (avcodec_open(pCodecCtx, pCodec) < 0) {
// Could not open codec
return ERR_CANNOT_OPEN_CODEC;
}
//
AVFrame *pFrame;
pFrame = avcodec_alloc_frame();
if (!pFrame) {
// Can't allocate memory for AVFrame
return ERR_CANNOT_ALLOC_MEM;
}
int frameFinished;
int numBytes;
// Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof (uint8_t));
avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
AVPacket packet;
int res = 0;
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index != 0)
continue;
ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (ret > 0) {
// now, load the useful info into realPicture
copyFrame(videoContext, realPicture, pCodecCtx, pFrame);
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
return 0;
} else {
// Error decoding frame. Use strerror(AVERROR(ret))) for details
res = ERR_DECODE_FRAME;
}
}
av_free(pFrame);
// close codec
avcodec_close(pCodecCtx);
// Close the image file
av_close_input_file(pFormatCtx);
return res;
}
一些 ./config 选项:
--extra-cflags="-O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 -mfloat-abi=softfp -mfpu=vfp -marm -march=armv7-a -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE"
--extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog"
--arch=armv7-a --enable-armv5te --enable-armv6 --enable-armvfp --enable-memalign-hack