-2
我有兴趣获得解决方案,我使用libvlc库从h264流获取视频帧。我已回调至libvlc_video_set_format_callbacks
和我的回调函数收到以下信息(format_callback
)参数:色度:“J420”,宽度:1088,高度:1922如何正确使用libvlc(C++)从h264 videostream获取帧?
当我打电话主
Player p;
p.play("rtsp://path/to/camera?videocodec=h264");
它打印出错以下错误
chroma "J420" width: 1088 , height: 1922
[00007fddfc001268] core vout display error: Failed to change zoom
[00007fddfc001268] core vout display error: Failed to set on top
[00007fddfc001268] core vout display error: Failed to change source AR
[h264 @ 0x7fde1c06cea0] error while decoding MB 24 111, bytestream -15
[swscaler @ 0x7fddfc002ca0] bad dst image pointers
[swscaler @ 0x7fddfc002ca0] bad dst image pointers
我的猜测是缓冲区及其大小有问题。在哪里,用哪种类型和多大的缓冲区来逐帧获取视频?后来我打算将帧数据转发给QImage。下面是Player.h文件:
const int ResoHeight = 1922;
const int ResoWidth = 1088;
const int BytesPerPixel = 3; // not sure about this
struct ImageData
{
QVector<unsigned char> raw;
QVector<unsigned char> picture;
ImageData()
{
raw.resize(BytesPerPixel * ResoHeight * ResoWidth);
picture.resize(BytesPerPixel * ResoHeight * ResoWidth);
}
};
class Player : public QObject
{
Q_OBJECT
public:
explicit Player(QObject *parent = nullptr);
~Player();
void play(const std::string& path);
signals:
void newImage(const QImage& image);
private:
libvlc_instance_t* vlcInstance;
libvlc_media_player_t* player;
libvlc_media_t* media;
ImageData buffer;
};
Player.cpp是以下几点:
namespace {
void* lock_frame(void *opaque, void **planes)
{
ImageData* buf = (ImageData*)(opaque);
*planes = buf->raw.data();
return buf->picture.data();
}
void unlock_frame(void *opaque, void *picture, void *const *planes)
{
// will be logic to announce new image
}
unsigned format_callback(void** opaque, char* chroma, unsigned *width, unsigned *height, unsigned *pitches, unsigned *lines)
{
qDebug() << "chroma:" << QString(chroma) << "width:" << *width << ", height:" << *height;
*pitches= (*width) * BytesPerPixel;
*lines= *height;
return 1;
}
} // namespace
Player::Player(QObject* parent)
: QObject(parent)
, vlcInstance(libvlc_new(0, nullptr))
, player(libvlc_media_player_new(vlcInstance))
, media(nullptr)
{
}
Player::~Player()
{
libvlc_media_player_stop(player);
libvlc_media_player_release(player);
libvlc_release(vlcInstance);
}
void Player::play(const std::string& path)
{
media = libvlc_media_new_location(vlcInstance, path.c_str());
libvlc_media_player_set_media(player, media);
libvlc_media_release(media);
libvlc_video_set_callbacks(player, lock_frame, unlock_frame, nullptr, &buffer);
libvlc_video_set_format_callbacks(player, format_callback, nullptr);
libvlc_media_player_play(player);
}
'buffer'未初始化。你应该在format_callback函数中初始化它。 – seleciii44
'buffer'是ImageData的类型,它在构造函数中被启动。当然,在'format_callback'函数中初始化它是正确的,但它不应该是问题的原因。通过将缓冲区大小调整方法移动到'format_callback'来试验它。同样的错误。 – psalong
对不起,没有意识到。 J420色度是一种平面YUV格式https://wiki.videolan.org/YUV/。这意味着你必须在lock_frame中提供3个图像指针。如果你只是想要一个RGB(RV24)图像看到这个问题https://stackoverflow.com/q/40365353/1632887 – seleciii44