c++ - swscaler - 错误的 src 图像指针 MacOS
问题描述
我正在编写一个可在不同平台上运行的屏幕录像机,但我坚持使用 MacOs 版本。
这是进行视频编码的代码片段
void ScreenRecorder::StartEncode()
{
int ret;
AVFrame *inputFrame = av_frame_alloc();
AVPacket *inputPacket = av_packet_alloc();
AVFrame *outputFrame = av_frame_alloc();
AVPacket *outputPacket = av_packet_alloc();
uint64_t audioFrameCount = 0;
uint64_t videoFrameCount = 0;
int64_t nextVideoPTS = 0, nextAudioPTS = 0;
auto src_pix_fmt = correct_for_deprecated_pixel_format(videoInCodecCtx->pix_fmt);
auto swsContext = sws_getContext(width, height,src_pix_fmt, width, height, PIX_SWS_CONTEXT, SWS_BICUBIC, nullptr,
nullptr, nullptr);
while (isRun)
{
int choice = 0;
if (videoInFormatCtx && audioInFormatCtx)
choice = av_compare_ts(nextVideoPTS, videoOutStream->time_base, nextAudioPTS, audioOutStream->time_base);
else if (videoInFormatCtx)
choice = -1;
else if (audioInFormatCtx)
choice = 1;
if (choice == -1)
{
// Video packet
av_read_frame(videoInFormatCtx, inputPacket);
avcodec_send_packet(videoInCodecCtx, inputPacket);
avcodec_receive_frame(videoInCodecCtx, inputFrame);
outputFrame->format = PIX_OUTPUT_FMT;
outputFrame->width = width;
outputFrame->height = height;
ret = av_frame_get_buffer(outputFrame, 0);
if (ret < 0)
{
throw std::runtime_error("Unable to allocate video frame");
}
sws_scale(swsContext, inputFrame->data, inputFrame->linesize, 0, height, outputFrame->data, outputFrame->linesize);
outputFrame->pts = videoFrameCount++ * videoOutStream->time_base.den / framerate;
avcodec_send_frame(videoOutCodecCtx, outputFrame);
if (avcodec_receive_packet(videoOutCodecCtx, outputPacket) == AVERROR(EAGAIN))
continue;
outputPacket->stream_index = videoOutStream->index;
outputPacket->duration = 0; //videoOutStream->time_base.den / 30;
outputPacket->dts = outputPacket->pts = videoFrameCount * videoOutStream->time_base.den / framerate;
// std::cerr << "\tVideo::PTS (" << outputFrame->pts << ") timebase " << videoOutStream->time_base.num << "/" << videoOutStream->time_base.den << " real: " << (outputPacket->pts / (double)videoOutStream->time_base.den) << std::endl;
nextVideoPTS = outputFrame->pts;
av_interleaved_write_frame(outFormatCtx, outputPacket);
//av_write_frame(outFormatCtx, outputPacket);
av_frame_unref(inputFrame);
av_packet_unref(inputPacket);
av_frame_unref(outputFrame);
av_packet_unref(outputPacket);
}
else
{
// decoding
ret = av_read_frame(audioInFormatCtx, inputPacket);
if (ret < 0)
{
throw std::runtime_error("can not read frame");
}
ret = avcodec_send_packet(audioInCodecCtx, inputPacket);
if (ret < 0)
{
throw std::runtime_error("can not send pkt in decoding");
}
ret = avcodec_receive_frame(audioInCodecCtx, inputFrame);
if (ret < 0)
{
throw std::runtime_error("can not receive frame in decoding");
}
//--------------------------------
// encoding
uint8_t **cSamples = nullptr;
ret = av_samples_alloc_array_and_samples(&cSamples, NULL, audioOutCodecCtx->channels, inputFrame->nb_samples, requireAudioFmt, 0);
if (ret < 0)
{
throw std::runtime_error("Fail to alloc samples by av_samples_alloc_array_and_samples.");
}
ret = swr_convert(audioConverter, cSamples, inputFrame->nb_samples, (const uint8_t **)inputFrame->extended_data, inputFrame->nb_samples);
if (ret < 0)
{
throw std::runtime_error("Fail to swr_convert.");
}
if (av_audio_fifo_space(audioFifo) < inputFrame->nb_samples)
{
throw std::runtime_error("audio buffer is too small.");
}
ret = av_audio_fifo_write(audioFifo, (void **)cSamples, inputFrame->nb_samples);
if (ret < 0)
{
throw std::runtime_error("Fail to write fifo");
}
av_freep(&cSamples[0]);
av_frame_unref(inputFrame);
av_packet_unref(inputPacket);
while (av_audio_fifo_size(audioFifo) >= audioOutCodecCtx->frame_size)
{
AVFrame *outputFrame = av_frame_alloc();
outputFrame->nb_samples = audioOutCodecCtx->frame_size;
outputFrame->channels = audioInCodecCtx->channels;
outputFrame->channel_layout = av_get_default_channel_layout(audioInCodecCtx->channels);
outputFrame->format = requireAudioFmt;
outputFrame->sample_rate = audioOutCodecCtx->sample_rate;
ret = av_frame_get_buffer(outputFrame, 0);
assert(ret >= 0);
ret = av_audio_fifo_read(audioFifo, (void **)outputFrame->data, audioOutCodecCtx->frame_size);
assert(ret >= 0);
outputFrame->pts = audioFrameCount * audioOutStream->time_base.den * audioOutCodecCtx->frame_size / audioOutCodecCtx->sample_rate;
ret = avcodec_send_frame(audioOutCodecCtx, outputFrame);
if (ret < 0)
{
throw std::runtime_error("Fail to send frame in encoding");
}
av_frame_free(&outputFrame);
ret = avcodec_receive_packet(audioOutCodecCtx, outputPacket);
if (ret == AVERROR(EAGAIN))
{
continue;
}
else if (ret < 0)
{
throw std::runtime_error("Fail to receive packet in encoding");
}
outputPacket->stream_index = audioOutStream->index;
outputPacket->duration = audioOutStream->time_base.den * audioOutCodecCtx->frame_size / audioOutCodecCtx->sample_rate;
outputPacket->dts = outputPacket->pts = audioFrameCount * audioOutStream->time_base.den * audioOutCodecCtx->frame_size / audioOutCodecCtx->sample_rate;
audioFrameCount++;
nextAudioPTS = outputPacket->pts;
// std::cerr << "Audio::PTS (" << nextAudioPTS << ") timebase " << audioOutStream->time_base.num << "/" << audioOutStream->time_base.den << " real: " << (outputPacket->pts / (double)videoOutStream->time_base.den) << std::endl;
ret = av_write_frame(outFormatCtx, outputPacket);
av_packet_unref(outputPacket);
av_frame_unref(outputFrame);
}
}
}
av_packet_free(&inputPacket);
av_packet_free(&outputPacket);
av_frame_free(&inputFrame);
av_frame_free(&outputFrame);
sws_freeContext(swsContext);
printf("encode %lu audio packets in total.\n", audioFrameCount);
}
当我开始录制时,我收到错误 * [swscaler] bad src pointers *,结果是一个全绿色的视频,除了我正在录制的窗口的第一帧。经过一些调试后,我注意到问题在于avcodec_receive_frame (videoInCodecCtx, inputFrame)
没有正确设置inputFrame-> data
全部为 NULL 的指针,但我不知道如何解决它,知道吗?
解决方案
我认为问题最初出在 in 中,avcodec_receive_frame
但在av_read_frame
which 中返回EAGAIN
。
现在我通过检查这个错误来修复,if-statement
当EAGAIN
返回时我做了一个简单的continue
. 现在录制很顺利!
推荐阅读
- r - 平滑 ggplot2 地图中的边缘 - SpatialPolygonsDataFrame
- javascript - Nuxt.js POST 到 asyncData 内的解析服务器会导致跨域错误
- git - 当同时从两个分支提交时,Jenkins 多分支管道作业运行不明确
- initialization - ngx-cookieconsent 初始化 $ 不起作用
- vba - VBA:为word中的超链接添加样式
- perl - 使用 Mojo 在两个 perl 项目之间传递 multipart/form-data
- react-native - 反应原生导航跨屏通知“snackbar”
- gradle - 使用 slf4j 和 log4j2 进行 Gradle 日志记录
- angular - ngx翻译角度,如何将html格式的文本传递给占位符属性
- android - Android - 标签栏 - 图标旁边的标题