diff --git a/src/zm_mpeg.cpp b/src/zm_mpeg.cpp index 4d0954e24b..eb2b2a65e3 100644 --- a/src/zm_mpeg.cpp +++ b/src/zm_mpeg.cpp @@ -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(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; diff --git a/src/zm_mpeg.h b/src/zm_mpeg.h index e975cf8378..5f2b13dff5 100644 --- a/src/zm_mpeg.h +++ b/src/zm_mpeg.h @@ -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 &out, SwsContext **context); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7459b4c0a6..5280bd40f7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 diff --git a/tests/zm_mpeg_raw_packet.cpp b/tests/zm_mpeg_raw_packet.cpp new file mode 100644 index 0000000000..6e17b18714 --- /dev/null +++ b/tests/zm_mpeg_raw_packet.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 . + */ + +#include "zm_catch2.h" + +#include "zm_mpeg.h" + +#include +#include + +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 pic(yuv_size); + for (size_t i = 0; i < pic.size(); i++) pic[i] = static_cast(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(pkt->data) != static_cast(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(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); +}