Swscaler坏的源图像指针。

8
我完全不知所措。我正在尝试在 Windows 10 下使用 FFMPEG 捕获 30 张屏幕截图,并将它们放入视频中。但一直提示[swscaler @ 073890a0] bad src image pointers,结果整个视频都是绿色的。如果我改用 dshow 格式并使用 video=screen-capture-recorder,则视频看起来大部分是垃圾。以下是我为此写的简短代码。我完全被卡住了,不知道应该往哪个方向查找。

MainWindow.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

MainWindow.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);
}
1个回答

5

我认为问题在于你使用了一些不必要的代码和已弃用的函数,这些行是无关紧要的:

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);

这样做会破坏已经正确的帧,并且还会导致内存泄漏问题。因为您已经使用 av_image_alloc 分配了正确的 yuv420p 平面缓冲区空间,尽管在失败的情况下您没有检查其返回代码。您可能希望将此缓冲区大小保留给其他用途。哦,别忘了也要删除 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

希望能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接