mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
fix: zms rawvideo stream sends the picture bytes instead of the frame struct and heap refs #7518
This commit is contained in:
1 parent
182b5a7c8f
commit
06f30caafb
4 files changed
+70
-2
No files matched your search
+9
-2
@@ -528,6 +528,14 @@ double VideoStream::EncodeImage(const Image &image, bool p_add_timestamp, unsign
|
||||
return EncodeFrame(packed_image.data(), packed_image.size(), p_add_timestamp, p_timestamp);
|
||||
}
|
||||
|
||||
// A raw video packet is the picture itself, sized for the frame's own format.
|
||||
// The frame struct is not picture data, and the caller's buffer_size is for the
|
||||
// input format, which can be larger than the frame's.
|
||||
void VideoStream::SetRawPacket(AVPacket *pkt, const AVFrame *frame) {
|
||||
pkt->data = frame->data[0];
|
||||
pkt->size = av_image_get_buffer_size(static_cast<AVPixelFormat>(frame->format), frame->width, frame->height, 1);
|
||||
}
|
||||
|
||||
double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp ) {
|
||||
if ( codec_context->pix_fmt != pf ) {
|
||||
static struct SwsContext *img_convert_ctx = nullptr;
|
||||
@@ -549,8 +557,7 @@ double VideoStream::ActuallyEncodeFrame( const uint8_t *buffer, int buffer_size,
|
||||
codec_context->codec_id == AV_CODEC_ID_RAWVIDEO) {
|
||||
pkt->flags |= AV_PKT_FLAG_KEY;
|
||||
pkt->stream_index = ost->index;
|
||||
pkt->data = (uint8_t *)opicture_ptr;
|
||||
pkt->size = buffer_size;
|
||||
SetRawPacket(pkt, opicture_ptr);
|
||||
} else {
|
||||
#if LIBAVCODEC_VERSION_CHECK(60, 2, 0, 2, 100)
|
||||
opicture_ptr->pts = codec_context->frame_num;
|
||||
|
||||
@@ -97,6 +97,8 @@ class VideoStream {
|
||||
// Convert image into out as a packed (align 1) buffer of pf at width x height,
|
||||
// reusing *context. An image of another shape is fitted inside, keeping its
|
||||
// aspect ratio, on black. Returns false if the conversion could not be set up.
|
||||
// Point pkt at frame's packed picture data, for the rawvideo format.
|
||||
static void SetRawPacket(AVPacket *pkt, const AVFrame *frame);
|
||||
static void FillBlack(AVPixelFormat pf, int height, uint8_t *data[4], const int linesize[4]);
|
||||
static bool PackImage(const Image &image, AVPixelFormat pf, int width, int height,
|
||||
std::vector<uint8_t> &out, SwsContext **context);
|
||||
|
||||
@@ -51,6 +51,7 @@ set(TEST_SOURCES
|
||||
zm_monitorlink_token.cpp
|
||||
zm_monitorlink_score.cpp
|
||||
zm_mp4_sidx.cpp
|
||||
zm_mpeg_raw_packet.cpp
|
||||
zm_packetqueue.cpp
|
||||
zm_packet_lock.cpp
|
||||
zm_packet_y_image.cpp
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "zm_catch2.h"
|
||||
|
||||
#include "zm_mpeg.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE("VideoStream::SetRawPacket points a raw packet at the picture", "[VideoStream]") {
|
||||
const int width = 64;
|
||||
const int height = 48;
|
||||
const size_t yuv_size = width * height * 3 / 2;
|
||||
|
||||
std::vector<uint8_t> pic(yuv_size);
|
||||
for (size_t i = 0; i < pic.size(); i++) pic[i] = static_cast<uint8_t>(i % 251);
|
||||
|
||||
AVFrame *frame = av_frame_alloc();
|
||||
REQUIRE(frame != nullptr);
|
||||
frame->format = AV_PIX_FMT_YUV420P;
|
||||
frame->width = width;
|
||||
frame->height = height;
|
||||
frame->data[0] = pic.data();
|
||||
|
||||
AVPacket *pkt = av_packet_alloc();
|
||||
REQUIRE(pkt != nullptr);
|
||||
|
||||
VideoStream::SetRawPacket(pkt, frame);
|
||||
|
||||
// The packet is the picture bytes, not the AVFrame struct that describes them.
|
||||
REQUIRE(pkt->data == pic.data());
|
||||
REQUIRE(static_cast<const void *>(pkt->data) != static_cast<const void *>(frame));
|
||||
// Sized for the frame's own format. The RGB24 buffer a caller passes in is
|
||||
// larger than the YUV420 picture, so taking its size would read past it.
|
||||
REQUIRE(static_cast<size_t>(pkt->size) == yuv_size);
|
||||
REQUIRE(pkt->size < width * height * 3);
|
||||
REQUIRE(std::memcmp(pkt->data, pic.data(), pic.size()) == 0);
|
||||
|
||||
frame->data[0] = nullptr; // not owned by the frame
|
||||
av_frame_free(&frame);
|
||||
pkt->data = nullptr;
|
||||
av_packet_free(&pkt);
|
||||
}
|
||||
Reference in new issue
Block a user