SWscaler 错误的 SRC 图像指针

swscaler bad src image pointers

本文关键字:图像 指针 SRC 错误 SWscaler      更新时间:2023-10-16

我完全迷路了。我正在尝试捕获 30 张屏幕截图并将它们放入 Windows 10 下带有FFMPEG的视频中。它一直告诉我[swscaler @ 073890a0] bad src image pointers.因此,视频完全是绿色的。如果我使用video=screen-capture-recorder将格式更改为dshow,则视频看起来主要是垃圾。这是我的简短代码。我完全被困住了,甚至不知道该朝哪个方向看。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFuture>
#include <QFutureWatcher>
#include <QMutex>
#include <QMutexLocker>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavcodec/avfft.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/avfiltergraph.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavutil/opt.h"
#include "libavutil/common.h"
#include "libavutil/channel_layout.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavutil/time.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/file.h"
#include "libswscale/swscale.h"
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    AVFormatContext *inputFormatContext = nullptr;
    AVFormatContext *outFormatContext = nullptr;
    AVStream* videoStream = nullptr;
    AVDictionary* options = nullptr;
    AVCodec* outCodec = nullptr;
    AVCodec* inputCodec = nullptr;
    AVCodecContext* inputCodecContext = nullptr;
    AVCodecContext* outCodecContext = nullptr;
    SwsContext* swsContext = nullptr;
private:
    void init();
    void initOutFile();
    void collectFrame();
};
#endif // MAINWINDOW_H

主窗口.cpp

#include "MainWindow.h"
#include <QGuiApplication>
#include <QLabel>
#include <QScreen>
#include <QTimer>
#include <QLayout>
#include <QImage>
#include <QtConcurrent/QtConcurrent>
#include <QThreadPool>
#include "ScreenCapture.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    resize(800, 600);
    auto label = new QLabel();
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    auto layout = new QHBoxLayout();
    layout->addWidget(label);
    auto widget = new QWidget();
    widget->setLayout(layout);
    setCentralWidget(widget);
    init();
    initOutFile();
    collectFrame();
}
MainWindow::~MainWindow()
{
    avformat_close_input(&inputFormatContext);
    avformat_free_context(inputFormatContext);
    QThreadPool::globalInstance()->waitForDone();
}
void MainWindow::init()
{
    av_register_all();
    avcodec_register_all();
    avdevice_register_all();
    avformat_network_init();
    auto screen = QGuiApplication::screens()[0];
    QRect geometry = screen->geometry();
    inputFormatContext = avformat_alloc_context();
    options = NULL;
    av_dict_set(&options, "framerate", "30", NULL);
    av_dict_set(&options, "offset_x", QString::number(geometry.x()).toLatin1().data(), NULL);
    av_dict_set(&options, "offset_y", QString::number(geometry.y()).toLatin1().data(), NULL);
    av_dict_set(&options, "video_size", QString(QString::number(geometry.width()) + "x" + QString::number(geometry.height())).toLatin1().data(), NULL);
    av_dict_set(&options, "show_region", "1", NULL);
    AVInputFormat* inputFormat = av_find_input_format("gdigrab");
    avformat_open_input(&inputFormatContext, "desktop", inputFormat, &options);
    int videoStreamIndex = av_find_best_stream(inputFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    inputCodecContext = inputFormatContext->streams[videoStreamIndex]->codec;
    inputCodecContext->width = geometry.width();
    inputCodecContext->height = geometry.height();
    inputCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
    inputCodec = avcodec_find_decoder(inputCodecContext->codec_id);
    avcodec_open2(inputCodecContext, inputCodec, NULL);
}
void MainWindow::initOutFile()
{
    const char* filename = "C:/Temp/output.mp4";
    avformat_alloc_output_context2(&outFormatContext, NULL, NULL, filename);
    outCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
    videoStream = avformat_new_stream(outFormatContext, outCodec);
    videoStream->time_base = {1, 30};
    outCodecContext = videoStream->codec;
    outCodecContext->codec_id = AV_CODEC_ID_MPEG4;
    outCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    outCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
    outCodecContext->bit_rate = 400000;
    outCodecContext->width = inputCodecContext->width;
    outCodecContext->height = inputCodecContext->height;
    outCodecContext->gop_size = 3;
    outCodecContext->max_b_frames = 2;
    outCodecContext->time_base = videoStream->time_base;
    if (outFormatContext->oformat->flags & AVFMT_GLOBALHEADER)
        outCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    avcodec_open2(outCodecContext, outCodec, NULL);
    if (!(outFormatContext->flags & AVFMT_NOFILE))
        avio_open2(&outFormatContext->pb, filename, AVIO_FLAG_WRITE, NULL, NULL);
    swsContext = sws_getContext(inputCodecContext->width,
                                inputCodecContext->height,
                                inputCodecContext->pix_fmt,
                                outCodecContext->width,
                                outCodecContext->height,
                                outCodecContext->pix_fmt,
                                SWS_BICUBIC, NULL, NULL, NULL);
    avformat_write_header(outFormatContext, &options);
}
void MainWindow::collectFrame()
{
    AVFrame* frame = av_frame_alloc();
    frame->data[0] = NULL;
    frame->width = inputCodecContext->width;
    frame->height = inputCodecContext->height;
    frame->format = inputCodecContext->pix_fmt;
    av_image_alloc(frame->data, frame->linesize, inputCodecContext->width, inputCodecContext->height, (AVPixelFormat)frame->format, 32);
    AVFrame* outFrame = av_frame_alloc();
    outFrame->data[0] = NULL;
    outFrame->width = outCodecContext->width;
    outFrame->height = outCodecContext->height;
    outFrame->format = outCodecContext->pix_fmt;
    av_image_alloc(outFrame->data, outFrame->linesize, outCodecContext->width, outCodecContext->height, (AVPixelFormat)outFrame->format, 32);
    int bufferSize = av_image_get_buffer_size(outCodecContext->pix_fmt,
                                              outCodecContext->width,
                                              outCodecContext->height,
                                              24);
    uint8_t* outBuffer = (uint8_t*)av_malloc(bufferSize);
    avpicture_fill((AVPicture*)outFrame, outBuffer,
                   AV_PIX_FMT_YUV420P,
                   outCodecContext->width, outCodecContext->height);
    int frameCount = 30;
    int count = 0;
    AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
    av_init_packet(packet);
    while(av_read_frame(inputFormatContext, packet) >= 0)
    {
        if(packet->stream_index == videoStream->index)
        {
            int frameFinished = 0;
            avcodec_decode_video2(inputCodecContext, frame, &frameFinished, packet);
            if(frameFinished)
            {
                if(++count > frameCount)
                {
                    qDebug() << "FINISHED!";
                    break;
                }
                sws_scale(swsContext, frame->data, frame->linesize, 0, inputCodecContext->height, outFrame->data, outFrame->linesize);
                AVPacket outPacket;
                av_init_packet(&outPacket);
                outPacket.data = NULL;
                outPacket.size = 0;
                int got_picture = 0;
                avcodec_encode_video2(outCodecContext, &outPacket, outFrame, &got_picture);
                if(got_picture)
                {
                    if(outPacket.pts != AV_NOPTS_VALUE) outPacket.pts = av_rescale_q(outPacket.pts, videoStream->codec->time_base, videoStream->time_base);
                    if(outPacket.dts != AV_NOPTS_VALUE) outPacket.dts = av_rescale_q(outPacket.dts, videoStream->codec->time_base, videoStream->time_base);
                    av_write_frame(outFormatContext , &outPacket);
                }
                av_packet_unref(&outPacket);
            }
        }
    }
    av_write_trailer(outFormatContext);
    av_free(outBuffer);
}
我认为

问题是您使用了一些不必要的代码和一些不推荐使用的函数,这些行是不必要的:

int bufferSize = av_image_get_buffer_size(outCodecContext->pix_fmt,
                                          outCodecContext->width,
                                          outCodecContext->height,
                                          24);
uint8_t* outBuffer = (uint8_t*)av_malloc(bufferSize);
avpicture_fill((AVPicture*)outFrame, outBuffer,
                AV_PIX_FMT_YUV420P,
                outCodecContext->width, outCodecContext->height);

这样做会损坏已经OK的帧,也会导致内存泄漏问题。因为您已经为 yuv420p 平面av_image_alloc缓冲区分配了正确的空间,尽管您没有在失败时检查其返回代码。您可以保留缓冲区大小以用于其他目的。哦,别忘了也删除av_free(outBuffer)

这些应替换:

AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
av_init_packet(packet);

有了这个:

AVPacket *packet = av_packet_alloc(); // also inits to defaults
if (packet == NULL) {
    //hande error
}

其他事情是,您的avcodec_decode_video2avcodec_encode_video2也已弃用,但仍应有效。最后,根据我的经验,av_interleaved_write_frameav_write_frame效果更好。

新的解码和编码 API 示例可在此处找到:https://github.com/FFmpeg/FFmpeg/tree/master/doc/examples

希望有帮助。